From fc72f49da33c1397c13d6947d24f9a51907ef7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Massart?= Date: Mon, 10 Oct 2016 12:52:29 +0200 Subject: [PATCH] MDL-56095 output: Moving progress_bar from weblib to outputcomponents And revert the change in the order of the require_once in setup.php. --- lib/outputcomponents.php | 182 +++++++++++++++++++++++++++++++++++++++ lib/setup.php | 2 +- lib/weblib.php | 181 -------------------------------------- 3 files changed, 183 insertions(+), 182 deletions(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 1217fb043c8..fdd8b1858a8 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -4446,3 +4446,185 @@ class preferences_group implements renderable { $this->nodes = $nodes; } } + +/** + * Progress bar class. + * + * Manages the display of a progress bar. + * + * To use this class. + * - construct + * - call create (or use the 3rd param to the constructor) + * - call update or update_full() or update() repeatedly + * + * @copyright 2008 jamiesensei + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core + * @category output + */ +class progress_bar implements renderable, templatable { + /** @var string html id */ + private $html_id; + /** @var int total width */ + private $width; + /** @var int last percentage printed */ + private $percent = 0; + /** @var int time when last printed */ + private $lastupdate = 0; + /** @var int when did we start printing this */ + private $time_start = 0; + + /** + * Constructor + * + * Prints JS code if $autostart true. + * + * @param string $htmlid The container ID. + * @param int $width The suggested width. + * @param bool $autostart Whether to start the progress bar right away. + */ + public function __construct($htmlid = '', $width = 500, $autostart = false) { + if (!empty($htmlid)) { + $this->html_id = $htmlid; + } else { + $this->html_id = 'pbar_'.uniqid(); + } + + $this->width = $width; + + if ($autostart) { + $this->create(); + } + } + + /** + * Create a new progress bar, this function will output html. + * + * @return void Echo's output + */ + public function create() { + global $OUTPUT; + + $this->time_start = microtime(true); + if (CLI_SCRIPT) { + return; // Temporary solution for cli scripts. + } + + flush(); + echo $OUTPUT->render($this); + flush(); + } + + /** + * Update the progress bar. + * + * @param int $percent From 1-100. + * @param string $msg The message. + * @return void Echo's output + * @throws coding_exception + */ + private function _update($percent, $msg) { + if (empty($this->time_start)) { + throw new coding_exception('You must call create() (or use the $autostart ' . + 'argument to the constructor) before you try updating the progress bar.'); + } + + if (CLI_SCRIPT) { + return; // Temporary solution for cli scripts. + } + + $estimate = $this->estimate($percent); + + if ($estimate === null) { + // Always do the first and last updates. + } else if ($estimate == 0) { + // Always do the last updates. + } else if ($this->lastupdate + 20 < time()) { + // We must update otherwise browser would time out. + } else if (round($this->percent, 2) === round($percent, 2)) { + // No significant change, no need to update anything. + return; + } + + $estimatemsg = null; + if (is_numeric($estimate)) { + $estimatemsg = get_string('secondsleft', 'moodle', round($estimate, 2)); + } + + $this->percent = round($percent, 2); + $this->lastupdate = microtime(true); + + echo html_writer::script(js_writer::function_call('updateProgressBar', + array($this->html_id, $this->percent, $msg, $estimatemsg))); + flush(); + } + + /** + * Estimate how much time it is going to take. + * + * @param int $pt From 1-100. + * @return mixed Null (unknown), or int. + */ + private function estimate($pt) { + if ($this->lastupdate == 0) { + return null; + } + if ($pt < 0.00001) { + return null; // We do not know yet how long it will take. + } + if ($pt > 99.99999) { + return 0; // Nearly done, right? + } + $consumed = microtime(true) - $this->time_start; + if ($consumed < 0.001) { + return null; + } + + return (100 - $pt) * ($consumed / $pt); + } + + /** + * Update progress bar according percent. + * + * @param int $percent From 1-100. + * @param string $msg The message needed to be shown. + */ + public function update_full($percent, $msg) { + $percent = max(min($percent, 100), 0); + $this->_update($percent, $msg); + } + + /** + * Update progress bar according the number of tasks. + * + * @param int $cur Current task number. + * @param int $total Total task number. + * @param string $msg The message needed to be shown. + */ + public function update($cur, $total, $msg) { + $percent = ($cur / $total) * 100; + $this->update_full($percent, $msg); + } + + /** + * Restart the progress bar. + */ + public function restart() { + $this->percent = 0; + $this->lastupdate = 0; + $this->time_start = 0; + } + + /** + * Export for template. + * + * @param renderer_base $output The renderer. + * @return array + */ + public function export_for_template(renderer_base $output) { + return [ + 'id' => $this->html_id, + 'width' => $this->width, + ]; + } +} diff --git a/lib/setup.php b/lib/setup.php index 612f077728e..b5970239918 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -587,8 +587,8 @@ core_date::store_default_php_timezone(); // Load up standard libraries require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output require_once($CFG->libdir .'/ajax/ajaxlib.php'); // Functions for managing our use of JavaScript and YUI -require_once($CFG->libdir .'/outputlib.php'); // Functions for generating output require_once($CFG->libdir .'/weblib.php'); // Functions relating to HTTP and content +require_once($CFG->libdir .'/outputlib.php'); // Functions for generating output require_once($CFG->libdir .'/navigationlib.php'); // Class for generating Navigation structure require_once($CFG->libdir .'/dmllib.php'); // Database access require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions. diff --git a/lib/weblib.php b/lib/weblib.php index db4d76e6c78..6b178cc9af6 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3168,187 +3168,6 @@ function is_in_popup() { return ($inpopup); } -/** - * Progress bar class. - * - * Manages the display of a progress bar. - * - * To use this class. - * - construct - * - call create (or use the 3rd param to the constructor) - * - call update or update_full() or update() repeatedly - * - * @copyright 2008 jamiesensei - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package core - */ -class progress_bar implements renderable, templatable { - /** @var string html id */ - private $html_id; - /** @var int total width */ - private $width; - /** @var int last percentage printed */ - private $percent = 0; - /** @var int time when last printed */ - private $lastupdate = 0; - /** @var int when did we start printing this */ - private $time_start = 0; - - /** - * Constructor - * - * Prints JS code if $autostart true. - * - * @param string $html_id - * @param int $width - * @param bool $autostart Default to false - */ - public function __construct($htmlid = '', $width = 500, $autostart = false) { - if (!empty($htmlid)) { - $this->html_id = $htmlid; - } else { - $this->html_id = 'pbar_'.uniqid(); - } - - $this->width = $width; - - if ($autostart) { - $this->create(); - } - } - - /** - * Create a new progress bar, this function will output html. - * - * @return void Echo's output - */ - public function create() { - global $OUTPUT; - - $this->time_start = microtime(true); - if (CLI_SCRIPT) { - return; // Temporary solution for cli scripts. - } - - flush(); - echo $OUTPUT->render($this); - flush(); - } - - /** - * Update the progress bar - * - * @param int $percent from 1-100 - * @param string $msg - * @return void Echo's output - * @throws coding_exception - */ - private function _update($percent, $msg) { - if (empty($this->time_start)) { - throw new coding_exception('You must call create() (or use the $autostart ' . - 'argument to the constructor) before you try updating the progress bar.'); - } - - if (CLI_SCRIPT) { - return; // Temporary solution for cli scripts. - } - - $estimate = $this->estimate($percent); - - if ($estimate === null) { - // Always do the first and last updates. - } else if ($estimate == 0) { - // Always do the last updates. - } else if ($this->lastupdate + 20 < time()) { - // We must update otherwise browser would time out. - } else if (round($this->percent, 2) === round($percent, 2)) { - // No significant change, no need to update anything. - return; - } - - $estimatemsg = null; - if (is_numeric($estimate)) { - $estimatemsg = get_string('secondsleft', 'moodle', round($estimate, 2)); - } - - $this->percent = round($percent, 2); - $this->lastupdate = microtime(true); - - echo html_writer::script(js_writer::function_call('updateProgressBar', - array($this->html_id, $this->percent, $msg, $estimatemsg))); - flush(); - } - - /** - * Estimate how much time it is going to take. - * - * @param int $pt from 1-100 - * @return mixed Null (unknown), or int - */ - private function estimate($pt) { - if ($this->lastupdate == 0) { - return null; - } - if ($pt < 0.00001) { - return null; // We do not know yet how long it will take. - } - if ($pt > 99.99999) { - return 0; // Nearly done, right? - } - $consumed = microtime(true) - $this->time_start; - if ($consumed < 0.001) { - return null; - } - - return (100 - $pt) * ($consumed / $pt); - } - - /** - * Update progress bar according percent - * - * @param int $percent from 1-100 - * @param string $msg the message needed to be shown - */ - public function update_full($percent, $msg) { - $percent = max(min($percent, 100), 0); - $this->_update($percent, $msg); - } - - /** - * Update progress bar according the number of tasks - * - * @param int $cur current task number - * @param int $total total task number - * @param string $msg message - */ - public function update($cur, $total, $msg) { - $percent = ($cur / $total) * 100; - $this->update_full($percent, $msg); - } - - /** - * Restart the progress bar. - */ - public function restart() { - $this->percent = 0; - $this->lastupdate = 0; - $this->time_start = 0; - } - - /** - * Export for template. - * - * @param renderer_base $output The renderer. - * @return array - */ - public function export_for_template(renderer_base $output) { - return [ - 'id' => $this->html_id, - 'width' => $this->width, - ]; - } -} - /** * Progress trace class. *