From: tjhunt Date: Thu, 12 Feb 2009 08:33:42 +0000 (+0000) Subject: weblib: MDL-18224 Create a better mechanism for outputting progress information from... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e82e01d1e02be4b9a93adcf094d5444776294c45;p=moodle.git weblib: MDL-18224 Create a better mechanism for outputting progress information from batch jobs --- diff --git a/lib/weblib.php b/lib/weblib.php index a26cbc9f95..c5182b25e5 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -7366,5 +7366,86 @@ EOT; } } +/** + * Use this class from long operations where you want to output occasional information about + * what is going on, but don't know if, or in what format, the output should be. + */ +abstract class moodle_progress_trace { + /** + * Ouput an progress message in whatever format. + * @param string $message the message to output. + * @param integer $depth indent depth for this message. + */ + abstract public function output($message, $depth = 0); + + /** + * Called when the processing is finished. + */ + public function finished() { + + } +} + +/** + * This subclass of moodle_progress_trace does not ouput anything. + */ +class null_progress_trace extends moodle_progress_trace { + public function output($message, $depth = 0) { + } +} + +/** + * This subclass of moodle_progress_trace outputs to plain text. + */ +class text_progress_trace extends moodle_progress_trace { + public function output($message, $depth = 0) { + echo str_repeat(' ', $depth), $message, "\n"; + flush(); + } +} + +/** + * This subclass of moodle_progress_trace outputs as HTML. + */ +class html_progress_trace extends moodle_progress_trace { + public function output($message, $depth = 0) { + echo '

', str_repeat('  ', $depth), htmlspecialchars($message), "

\n"; + flush(); + } +} + +class html_list_progress_trace extends moodle_progress_trace { + protected $currentdepth = -1; + + public function output($message, $depth = 0) { + $samedepth = true; + while ($this->currentdepth > $depth) { + echo "\n\n"; + $this->currentdepth -= 1; + if ($this->currentdepth == $depth) { + echo '
  • '; + } + $samedepth = false; + } + while ($this->currentdepth < $depth) { + echo "\n"; + $this->currentdepth -= 1; + } + } +} + // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: ?>