Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Class text_progress_tracker
  19   *
  20   * @package     tool_uploaduser
  21   * @copyright   2020 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_uploaduser\local;
  26  
  27  /**
  28   * Tracks the progress of the user upload and echos it in a text format
  29   *
  30   * @package     tool_uploaduser
  31   * @copyright   2020 Marina Glancy
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class text_progress_tracker extends \uu_progress_tracker {
  35  
  36      /**
  37       * Print table header.
  38       * @return void
  39       */
  40      public function start() {
  41          $this->_row = null;
  42      }
  43  
  44      /**
  45       * Output one line (followed by newline)
  46       * @param string $line
  47       */
  48      protected function output_line(string $line): void {
  49          echo $line . PHP_EOL;
  50      }
  51  
  52      /**
  53       * Flush previous line and start a new one.
  54       * @return void
  55       */
  56      public function flush() {
  57          if (empty($this->_row) or empty($this->_row['line']['normal'])) {
  58              // Nothing to print - each line has to have at least number.
  59              $this->_row = array();
  60              foreach ($this->columns as $col) {
  61                  $this->_row[$col] = ['normal' => '', 'info' => '', 'warning' => '', 'error' => ''];
  62              }
  63              return;
  64          }
  65          $this->output_line(get_string('linex', 'tool_uploaduser', $this->_row['line']['normal']));
  66          $prefix = [
  67              'normal' => '',
  68              'info' => '',
  69              'warning' => get_string('warningprefix', 'tool_uploaduser') . ' ',
  70              'error' => get_string('errorprefix', 'tool_uploaduser') . ' ',
  71          ];
  72          foreach ($this->_row['status'] as $type => $content) {
  73              if (strlen($content)) {
  74                  $this->output_line('  '.$prefix[$type].$content);
  75              }
  76          }
  77  
  78          foreach ($this->_row as $key => $field) {
  79              foreach ($field as $type => $content) {
  80                  if ($key !== 'status' && $type !== 'normal' && strlen($content)) {
  81                      $this->output_line('  ' . $prefix[$type] . $this->headers[$key] . ': ' .
  82                          str_replace("\n", "\n".str_repeat(" ", strlen($prefix[$type] . $this->headers[$key]) + 4), $content));
  83                  }
  84              }
  85          }
  86          foreach ($this->columns as $col) {
  87              $this->_row[$col] = ['normal' => '', 'info' => '', 'warning' => '', 'error' => ''];
  88          }
  89      }
  90  
  91      /**
  92       * Add tracking info
  93       * @param string $col name of column
  94       * @param string $msg message
  95       * @param string $level 'normal', 'warning' or 'error'
  96       * @param bool $merge true means add as new line, false means override all previous text of the same type
  97       * @return void
  98       */
  99      public function track($col, $msg, $level = 'normal', $merge = true) {
 100          if (empty($this->_row)) {
 101              $this->flush();
 102          }
 103          if (!in_array($col, $this->columns)) {
 104              return;
 105          }
 106          if ($merge) {
 107              if ($this->_row[$col][$level] != '') {
 108                  $this->_row[$col][$level] .= "\n";
 109              }
 110              $this->_row[$col][$level] .= $msg;
 111          } else {
 112              $this->_row[$col][$level] = $msg;
 113          }
 114      }
 115  
 116      /**
 117       * Print the table end
 118       * @return void
 119       */
 120      public function close() {
 121          $this->flush();
 122          $this->output_line(str_repeat('-', 79));
 123      }
 124  }