diff --git a/lib/weblib.php b/lib/weblib.php index a26cbc9f957..c5182b25e50 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 '