From: dongsheng Date: Fri, 23 May 2008 05:35:38 +0000 (+0000) Subject: "MDL-14706, introduce a progress bar class" X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ab500236814e5bc94533823985e2f26259f77fd6;p=moodle.git "MDL-14706, introduce a progress bar class" --- diff --git a/lib/adminlib.php b/lib/adminlib.php index fa286d0366..70fd58a89f 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -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 = << + 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"; + } + + +
+

+

+
 
+
+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 ""; + 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); + } +} ?>