noalyss Version-9
progress_bar.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * Copyright (C) 2018 Dany De Bontridder <dany@alchimerys.be>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22/**
23 * @file
24 * @brief Manage the progress bar and display it with javascript
25 *
26 */
27
28/**
29 * @brief Use one db for tracking progress bar value, the task id must be unique
30 * and let you follow the progress of a task.
31 * how it works : when calling an ajax , you have to create the task id and start
32 * the monitoring of it (js function = progress_bar_start).
33 * In your php script called by ajax you call Progress_Bar->set_value to show
34 * the progress. The timer created by progress_bar_start will check regularly the
35 * progress in the db.
36 * The ajax parameter for following the task is task_id
37 *
38 *@note you have to use session_write_close(); in the ajax file , otherwise,
39 * the function progress_bar_check will be blocked and won't update the progress
40 * bar
41 *
42 *@see progress_bar_start
43 *@see progress_bar_check
44 *
45 *
46 */
48{
49 private $db ; //!< database connexion
50 private $task_id ; //! task id (progress_bar.p_id)
51 private $value; //!< value of progress (between 0 & 100)
52 /**
53 * @example progress-bar.test.php test of this class
54 * @param type $p_task_id
55 */
56 function __construct($p_task_id)
57 {
58 $this->db=new Database();
59 $this->task_id=$p_task_id;
60 // Find value from db
61 $this->value = $this->db->get_value("select p_value from progress where p_id=$1",
62 [$p_task_id]);
63
64 // if task doesn't exists, create it
65 if ( $this->db->size()==0)
66 {
67 $this->value=0;
68 $this->db->exec_sql("insert into progress(p_id,p_value) values ($1,0)",
69 [$p_task_id]);
70 $this->db->exec_sql("delete from progress where p_created < now() - interval '3 hours' ");
71 }
72 }
73 /**
74 * Store the progress value into the db
75 * @param integer $p_value value of the progress between 0 & 100
76 *@exceptions code 1005 - if p_value is not in between 0 & 100
77 */
78 function set_value($p_value)
79 {
80 if ( $p_value > 100 || $p_value < 0 ) {
81 throw new Exception("Invalid value",EXC_PARAM_VALUE);
82 }
83 $this->value=$p_value;
84 $this->db->start();
85 $this->db->exec_sql("update progress set p_value=$1 where p_id=$2",
86 [$this->value,$this->task_id]);
87 $this->db->commit();
88 }
89 /**
90 * Get the progress value from db
91 * @return integer between 0 & 100
92 */
93 function get_value()
94 {
95 $this->value = $this->db->get_value("select p_value from progress where p_id=$1",
96 [$this->task_id]);
97
98 return $this->value;
99 }
100 /**
101 * Json answer of the task progressing
102 * if value is equal or greater than 100 , delete the row
103 * @return type
104 */
105 function answer()
106 {
107 $this->get_value();
108
109 header('Content-Type: application/json');
110 echo json_encode(["value"=>$this->value]);
111 if ($this->value>=100) {
112 $this->db->exec_sql("delete from progress where p_id=$1",[$this->task_id]);
113 }
114 return;
115 }
116 /**
117 * increment value with $p_step
118 * @param int $p_step
119 */
121 {
122 if ($this->value+$p_step > 100 ) {
123 $this->set_value(100);
124 return;
125 }
126 $this->set_value($this->value+$p_step);
127 }
128}
contains the class for connecting to Noalyss
Use one db for tracking progress bar value, the task id must be unique and let you follow the progres...
increment($p_step)
increment value with $p_step
$db
database connexion
answer()
Json answer of the task progressing if value is equal or greater than 100 , delete the row.
__construct($p_task_id)
set_value($p_value)
Store the progress value into the db.
$value
task id (progress_bar.p_id)
get_value()
Get the progress value from db.
const EXC_PARAM_VALUE
Definition: constant.php:343
$SecUser db