New : code for progress bar

This commit is contained in:
Dany De Bontridder 2018-02-03 14:28:37 +01:00
parent f9b7948f60
commit 29da5f0570
9 changed files with 321 additions and 2 deletions

View file

@ -673,7 +673,6 @@ EXCLUDE_SYMLINKS = NO
EXCLUDE_PATTERNS = a?.php \
b?.php \
?.php \
test*.php \
calendar-*.js \
richtext.js \
builder.js \

View file

@ -46,6 +46,7 @@ require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
require_once NOALYSS_INCLUDE.'/class/user.class.php';
require_once NOALYSS_INCLUDE.'/lib/http_input.class.php';
require_once NOALYSS_INCLUDE.'/lib/icon_action.class.php';
require_once NOALYSS_INCLUDE.'/lib/progress_bar.class.php';
$http=new HttpInput();
mb_internal_encoding("UTF-8");
@ -86,6 +87,16 @@ else
$g_user = new User($cn);
$g_user->check(true);
}
// For progress bar, for saving time , we check and answer directly
if ($op == "progressBar") {
$task_id=$http->request("task_id");
$task=new Progress_Bar($task_id);
$task->answer();
return;
}
$html = var_export($_REQUEST, true);
set_language();
if ( LOGINPUT)

View file

@ -3394,3 +3394,70 @@ Periode.filter_exercice=function (p_table_id) {
}
};
// keep track of progress bar
var progressBar = [];
// idx of progress bar
var progressIdx = 0;
/**
* Start the progress bar
* @param {string} p_taskid id to monitor
* @param {int} p_dossier
*/
function progress_bar_start(p_taskid)
{
try {
progressIdx++;
// Create a div
add_div({id: "progressDiv" + progressIdx, cssclass: "progressbar", html: '<span id="progressValue">0</span>'});
// Check status every sec.
progressBar[progressIdx] = setInterval(progress_bar_check.bind(null, progressIdx, p_taskid), 1000);
} catch (e) {
console.error(e.message);
}
}
/**
* Check every second the status
* @param {integer} p_idx idx of progressbar
* @param {string} p_taskid id to monitor
*/
function progress_bar_check(p_idx, p_taskid)
{
try {
new Ajax.Request("ajax_misc.php", {
parameters: {gDossier: 0, task_id: p_taskid,op:"progressBar"},
onSuccess: function (req) {
try
{
var answer=req.responseText.evalJSON();
var progressValue = $('progressValue');
var progress = parseFloat(progressValue.innerHTML);
if ( answer.value <= progress ) {
return;
}
progressValue.innerHTML = answer.value;
progressValue.setStyle("width:" + answer.value + "%");
if (answer.value== 100) {
clearInterval(progressBar[p_idx]);
progressValue.innerHTML="Success";
Effect.BlindUp("progressDiv"+progressIdx,{duration:1.0,scaleContent:false})
}
} catch (e) {
clearInterval(progressBar[p_idx]);
document.getElementById("progressValue").innerHTML=req.responseText;
console.error(e.message);
}
}
});
} catch (e) {
clearInterval(progressBar[p_idx]);
console.error(e.message);
}
}

View file

@ -2200,4 +2200,30 @@ div.bxbutton .icon:hover
{
background-color: white;
color: blue;
}
/**
* progressBar
*/
div.progressbar {
width:300px;
height:30px;
position:fixed;
top:5px;
left:40%;
background-color: white;
color:blue;
z-index:800;
border-color: #596a72;
border-width: 1px;
border-style: solid;
}
#progressValue {
display:block;
width: 0px;
height: 100%;
margin:0px;
padding: 0px;
background-color:darkblue;
color:antiquewhite;
font-weight: bolder;
}

View file

@ -2227,4 +2227,30 @@ div.bxbutton .icon:hover
{
background-color: white;
color: blue;
}
/**
* progressBar
*/
div.progressbar {
width:300px;
height:30px;
position:fixed;
top:5px;
left:40%;
background-color: white;
color:blue;
z-index:800;
border-color: #596a72;
border-width: 1px;
border-style: solid;
}
#progressValue {
display:block;
width: 0px;
height: 100%;
margin:0px;
padding: 0px;
background-color:darkblue;
color:antiquewhite;
font-weight: bolder;
}

View file

@ -23,7 +23,7 @@
* It is only a quick and dirty testing. You should use a tool as PHPUNIT for the unit testing
*
* - first do not forget to create the authorized_debug file in the html folder
* - secund the test must adapted to this page : if you do a post (or get) from a test, you won't get any result
* - secund the test must be adapted to this page : if you do a post (or get) from a test, you won't get any result
* if the $_REQUEST[test_select] is not set, so set it .
*/

View file

@ -0,0 +1,115 @@
<?php
/*
* Copyright (C) 2018 Dany De Bontridder <dany@alchimerys.be>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* @file
* @brief Manage the progress bar and display it with javascript
*
*/
/**
* @brief Use one db for tracking progress bar value, the task id must be unique
* and let you follow the progress of a task.
* how it works : when calling an ajax , you have to create the task id and start
* the monitoring of it (js function = progress_bar_start).
* In your php script called by ajax you call Progress_Bar->set_value to show
* the progress. The timer created by progress_bar_start will check regularly the
* progress in the db.
* The ajax parameter for following the task is task_id
*
*@note you have to use session_write_close(); in the ajax file , otherwise,
* the function progress_bar_check will be blocked and won't update the progress
* bar
*
*@see progress_bar_start
*@see progress_bar_check
*
*
*/
class Progress_Bar
{
private $db ; //!< database connexion
private $task_id ; //! task id (progress_bar.p_id)
private $value; //!< value of progress (between 0 & 100)
/**
* @example progress-bar.test.php test of this class
* @param type $p_task_id
*/
function __construct($p_task_id)
{
$this->db=new Database();
$this->task_id=$p_task_id;
// Find value from db
$this->value = $this->db->exec_sql("select p_value from progress where p_id=$1",
[$p_task_id]);
// if task doesn't exists, create it
if ( $this->db->size()==0)
{
$this->value=0;
$this->db->exec_sql("insert into progress(p_id,p_value) values ($1,0)",
[$p_task_id]);
}
}
/**
* Store the progress value into the db
* @param integer $p_value value of the progress between 0 & 100
*@exceptions code 1005 - if p_value is not in between 0 & 100
*/
function set_value($p_value)
{
if ( $p_value > 100 || $p_value < 0 ) {
throw new Exception("Invalid value",EXC_PARAM_VALUE);
}
$this->value=$p_value;
$this->db->start();
$this->db->exec_sql("update progress set p_value=$1 where p_id=$2",
[$this->value,$this->task_id]);
$this->db->commit();
}
/**
* Get the progress value from db
* @return integer between 0 & 100
*/
function get_value()
{
$this->value = $this->db->get_value("select p_value from progress where p_id=$1",
[$this->task_id]);
return $this->value;
}
/**
* Json answer of the task progressing
* if value is equal or greater than 100 , delete the row
* @return type
*/
function answer()
{
$this->get_value();
header('Content-Type: application/json');
echo json_encode(["value"=>$this->value]);
if ($this->value>=100) {
$this->db->exec_sql("delete from progress where p_id=$1",[$this->task_id])
}
return;
}
}

View file

@ -0,0 +1,65 @@
<?php
/*
* Copyright (C) 2018 Dany De Bontridder <dany@alchimerys.be>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* @file
* @brief Example about Progress_Bar, the most important is the
* session_write_close in the ajax script to unblock the PHP script.
* Each time a step is reached , you use Progress_Bar->set_value
* @see Progress_Bar
* @see progress_bar_start
* @see progress_bar_check
*/
require_once NOALYSS_INCLUDE.'/lib/progress_bar.class.php';
$ajax=$http->request("TestAjaxFile","string","no");
// $ajax != no if we are in ajax mode
if ( $ajax != "no")
{
session_write_close();
$task=$http->request("task_id");
$progress=new Progress_Bar($task);
sleep(1);
$progress->set_value(10);
sleep(2);
$progress->set_value(20);
sleep(1);
$progress->set_value(90);
sleep(2);
$progress->set_value(91);
sleep(5);
$progress->set_value(95);
sleep(6);
$progress->set_value(100);
return;
}
?>
<script>
function start_test()
{
var task_id='<?php echo uniqid()?>';
progress_bar_start(task_id);
new Ajax.Request("ajax_test.php",{
parameters:{"TestAjaxFile":"<?php echo __FILE__?>",gDossier:<?php echo Dossier::id()?>,'task_id':task_id}
});
}
</script>
<button onclick="start_test()"> Start progress</button>

View file

@ -9,6 +9,14 @@ delete from theme where the_filestyle in ('style-mandarine.css','style-mobile.cs
update user_global_pref set parameter_value='style-classic7.css' where parameter_value in ('style-mandarine.css','style-mobile.css');
-- add constraint
alter table jnt_use_dos add CONSTRAINT use_id_dos_id_uniq UNIQUE (use_id,dos_id);
-- create table to check progress
create table progress
(
p_id varchar(16) primary key,
p_value integer not null ,
p_created timestamp default now()
);
*/
create sequence tmp_pcmn_id_seq;
ALTER TABLE tmp_pcmn ADD COLUMN id bigint;
@ -107,3 +115,5 @@ ALTER TABLE tags ADD CONSTRAINT tags_check CHECK (t_actif in ('N','Y')) ;
alter table tags alter t_actif set default 'Y';
COMMENT ON COLUMN tags.t_actif is 'Y if the tag is activate and can be used ';
-- in repo