Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Output tracker.
  19   *
  20   * @package    tool_uploadcourse
  21   * @copyright  2013 Frédéric Massart
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  require_once($CFG->libdir . '/weblib.php');
  27  
  28  /**
  29   * Class output tracker.
  30   *
  31   * @package    tool_uploadcourse
  32   * @copyright  2013 Frédéric Massart
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class tool_uploadcourse_tracker {
  36  
  37      /**
  38       * Constant to output nothing.
  39       */
  40      const NO_OUTPUT = 0;
  41  
  42      /**
  43       * Constant to output HTML.
  44       */
  45      const OUTPUT_HTML = 1;
  46  
  47      /**
  48       * Constant to output plain text.
  49       */
  50      const OUTPUT_PLAIN = 2;
  51  
  52      /**
  53       * @var array columns to display.
  54       */
  55      protected $columns = array('line', 'result', 'id', 'shortname', 'fullname', 'idnumber', 'status');
  56  
  57      /**
  58       * @var int row number.
  59       */
  60      protected $rownb = 0;
  61  
  62      /**
  63       * @var int chosen output mode.
  64       */
  65      protected $outputmode;
  66  
  67      /**
  68       * @var object output buffer.
  69       */
  70      protected $buffer;
  71  
  72      /**
  73       * Constructor.
  74       *
  75       * @param int $outputmode desired output mode.
  76       */
  77      public function __construct($outputmode = self::NO_OUTPUT) {
  78          $this->outputmode = $outputmode;
  79          if ($this->outputmode == self::OUTPUT_PLAIN) {
  80              $this->buffer = new progress_trace_buffer(new text_progress_trace());
  81          }
  82      }
  83  
  84      /**
  85       * Finish the output.
  86       *
  87       * @return void
  88       */
  89      public function finish() {
  90          if ($this->outputmode == self::NO_OUTPUT) {
  91              return;
  92          }
  93  
  94          if ($this->outputmode == self::OUTPUT_HTML) {
  95              echo html_writer::end_tag('table');
  96          }
  97      }
  98  
  99      /**
 100       * Output the results.
 101       *
 102       * @param int $total total courses.
 103       * @param int $created count of courses created.
 104       * @param int $updated count of courses updated.
 105       * @param int $deleted count of courses deleted.
 106       * @param int $errors count of errors.
 107       * @return void
 108       */
 109      public function results($total, $created, $updated, $deleted, $errors) {
 110          if ($this->outputmode == self::NO_OUTPUT) {
 111              return;
 112          }
 113  
 114          $message = array(
 115              get_string('coursestotal', 'tool_uploadcourse', $total),
 116              get_string('coursescreated', 'tool_uploadcourse', $created),
 117              get_string('coursesupdated', 'tool_uploadcourse', $updated),
 118              get_string('coursesdeleted', 'tool_uploadcourse', $deleted),
 119              get_string('courseserrors', 'tool_uploadcourse', $errors)
 120          );
 121  
 122          if ($this->outputmode == self::OUTPUT_PLAIN) {
 123              foreach ($message as $msg) {
 124                  $this->buffer->output($msg);
 125              }
 126          } else if ($this->outputmode == self::OUTPUT_HTML) {
 127              $buffer = new progress_trace_buffer(new html_list_progress_trace());
 128              foreach ($message as $msg) {
 129                  $buffer->output($msg);
 130              }
 131              $buffer->finished();
 132          }
 133      }
 134  
 135      /**
 136       * Output one more line.
 137       *
 138       * @param int $line line number.
 139       * @param bool $outcome success or not?
 140       * @param array $status array of statuses.
 141       * @param array $data extra data to display.
 142       * @return void
 143       */
 144      public function output($line, $outcome, $status, $data) {
 145          global $OUTPUT;
 146          if ($this->outputmode == self::NO_OUTPUT) {
 147              return;
 148          }
 149  
 150          if ($this->outputmode == self::OUTPUT_PLAIN) {
 151              $message = array(
 152                  $line,
 153                  $outcome ? 'OK' : 'NOK',
 154                  isset($data['id']) ? $data['id'] : '',
 155                  isset($data['shortname']) ? $data['shortname'] : '',
 156                  isset($data['fullname']) ? $data['fullname'] : '',
 157                  isset($data['idnumber']) ? $data['idnumber'] : ''
 158              );
 159              $this->buffer->output(implode("\t", $message));
 160              if (!empty($status)) {
 161                  foreach ($status as $st) {
 162                      $this->buffer->output($st, 1);
 163                  }
 164              }
 165          } else if ($this->outputmode == self::OUTPUT_HTML) {
 166              $ci = 0;
 167              $this->rownb++;
 168              if (is_array($status)) {
 169                  $status = implode(html_writer::empty_tag('br'), $status);
 170              }
 171              if ($outcome) {
 172                  $outcome = $OUTPUT->pix_icon('i/valid', '');
 173              } else {
 174                  $outcome = $OUTPUT->pix_icon('i/invalid', '');
 175              }
 176              echo html_writer::start_tag('tr', array('class' => 'r' . $this->rownb % 2));
 177              echo html_writer::tag('td', $line, array('class' => 'c' . $ci++));
 178              echo html_writer::tag('td', $outcome, array('class' => 'c' . $ci++));
 179              echo html_writer::tag('td', isset($data['id']) ? $data['id'] : '', array('class' => 'c' . $ci++));
 180              echo html_writer::tag('td', isset($data['shortname']) ? $data['shortname'] : '', array('class' => 'c' . $ci++));
 181              echo html_writer::tag('td', isset($data['fullname']) ? $data['fullname'] : '', array('class' => 'c' . $ci++));
 182              echo html_writer::tag('td', isset($data['idnumber']) ? $data['idnumber'] : '', array('class' => 'c' . $ci++));
 183              echo html_writer::tag('td', $status, array('class' => 'c' . $ci++));
 184              echo html_writer::end_tag('tr');
 185          }
 186      }
 187  
 188      /**
 189       * Start the output.
 190       *
 191       * @return void
 192       */
 193      public function start() {
 194          if ($this->outputmode == self::NO_OUTPUT) {
 195              return;
 196          }
 197  
 198          if ($this->outputmode == self::OUTPUT_PLAIN) {
 199              $columns = array_flip($this->columns);
 200              unset($columns['status']);
 201              $columns = array_flip($columns);
 202              $this->buffer->output(implode("\t", $columns));
 203          } else if ($this->outputmode == self::OUTPUT_HTML) {
 204              $ci = 0;
 205              echo html_writer::start_tag('table', array('class' => 'generaltable boxaligncenter flexible-wrap',
 206                  'summary' => get_string('uploadcoursesresult', 'tool_uploadcourse')));
 207              echo html_writer::start_tag('tr', array('class' => 'heading r' . $this->rownb));
 208              echo html_writer::tag('th', get_string('csvline', 'tool_uploadcourse'),
 209                  array('class' => 'c' . $ci++, 'scope' => 'col'));
 210              echo html_writer::tag('th', get_string('result', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 211              echo html_writer::tag('th', get_string('id', 'tool_uploadcourse'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 212              echo html_writer::tag('th', get_string('shortname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 213              echo html_writer::tag('th', get_string('fullname'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 214              echo html_writer::tag('th', get_string('idnumber'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 215              echo html_writer::tag('th', get_string('status'), array('class' => 'c' . $ci++, 'scope' => 'col'));
 216              echo html_writer::end_tag('tr');
 217          }
 218      }
 219  
 220  }