Skip to content

Commit

Permalink
"MDL-14706, introduce a progress bar class"
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed May 23, 2008
1 parent 67784b2 commit ab50023
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4702,4 +4702,160 @@ function print_plugin_tables() {
echo $html;
}


class ProgressBar {
private $html_id;
private $percent;
private $width;
private $clr;
private $lastcall;
private $time_start;
function __construct($html_id = 'pid', $width = 100, $autostart = false){
$this->html_id = $html_id;
$this->percent = 0;
$this->width = $width;
$this->clr = new stdClass;
$this->clr->done = 'green';
$this->clr->process = '#FFCC66';
$this->lastcall = new stdClass;
$this->time_start = 0;
if($autostart){
$this->create();
}
}
/**
* set progress bar color, call before $this->create
* Usage:
* $clr->done = 'red';
* $clr->process = 'blue';
* $pb->setclr($clr);
* $pb->create();
* ......
*
* @param $clr object
*/
function setclr($clr){
foreach($clr as $n=>$v) {
$this->clr->$n = $v;
}
}
/**
* Create a new progress bar, this function will output
* html.
*
*/
function create(){
flush();
$this->lastcall->pt = 0;
$this->lastcall->time = time();
$htmlcode = <<<EOT
<script type="text/javascript">
Number.prototype.fixed=function(n){
with(Math)
return round(Number(this)*pow(10,n))/pow(10,n);
}
function up_{$this->html_id} (id, width, pt, msg, es){
percent = pt*100;
document.getElementById("status_"+id).innerHTML = msg;
document.getElementById("pt_"+id).innerHTML =
percent.fixed(2) + '%';
if(percent == 100) {
document.getElementById("progress_"+id).style.background
= "{$this->clr->done}";
}
document.getElementById("progress_"+id).style.width
= width + "px";
if (es == Infinity){
document.getElementById("time_"+id).innerHTML =
"Initializing...";
} else if(es == 0) {
document.getElementById("time_"+id).style.display
= "none";
}
document.getElementById("time_"+id).innerHTML =
es.fixed(2)+" sec";
}
</script>
<div style="text-align:center;width:{$this->width}px;clear:both;padding:0;margin:0;">
<h2 id="status_{$this->html_id}" style="text-align: center;margin:0 auto"></h2>
<p id="time_{$this->html_id}"></p>
<div id="progress_{$this->html_id}"
style="text-align:center;background:{$this->clr->process};width:4px;border:1px
solid gray">&nbsp;<span id="pt_{$this->html_id}"></span></div>
</div>
EOT;
echo $htmlcode;
flush();
}
function _update($percent, $msg, $es){
if(empty($this->time_start)){
$this->time_start = microtime(true);
}
$this->percent = $percent;
$this->lastcall->time = microtime(true);
$this->lastcall->pt = $percent;
$w = $this->percent * $this->width;
if ($es === null){
$es = "Infinity";
}
echo "<script type=\"text/javascript\">up_".$this->html_id."('$this->html_id', '$w', '$this->percent', '$msg', $es);</script>";
flush();
}
/**
* estimate time
*
* @param $curtime int the time call this function
* @param $percent int
*/
function estimate($curtime, $pt){
$consume = $curtime - $this->time_start;
$one = $curtime - $this->lastcall->time;
$this->percent = $pt;
$percent = $pt - $this->lastcall->pt;
if($percent != 0)
$left = ($one / $percent) - $consume;
else
return null;
if($left < 0)
return 0;
else
return $left;
}
/**
* Update progress bar according percent
*
* @param $percent int from 1-100
* @param $msg string the message needed to be shown
*/
function update_full($percent, $msg){
if ($percent >= 100){
$percent = 1;
}
if ($percent <= 0)
{
$percent = 0;
}
$this->_update($percent/100, $msg);
}
/**
* Update progress bar according the nubmer of tasks
*
* @param $cur int current task number
* @param $total int total task number
* @param $msg string message
*/
function update($cur, $total, $msg){
if ($cur >= $total){
$percent = 1;
}
if ($cur <= 0)
{
$percent = 0;
}
$percent = $cur / $total;
$es = $this->estimate(microtime(true), $percent);
$this->_update($percent, $msg, $es);
}
}
?>

0 comments on commit ab50023

Please sign in to comment.