]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-14706, introduce a progress bar class"
authordongsheng <dongsheng>
Fri, 23 May 2008 05:35:38 +0000 (05:35 +0000)
committerdongsheng <dongsheng>
Fri, 23 May 2008 05:35:38 +0000 (05:35 +0000)
lib/adminlib.php

index fa286d03661946b1ab2613458ae3e5f7e8942658..70fd58a89f4b4a59fff26e21d47e53bf5430a14b 100644 (file)
@@ -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);
+    }
+}
 ?>