Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * The gradebook simple view - base class for the table
  19   *
  20   * @package   gradereport_singleview
  21   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace gradereport_singleview\local\screen;
  26  
  27  use gradereport_singleview\local\ui\be_readonly;
  28  use html_table;
  29  use html_writer;
  30  use stdClass;
  31  use grade_grade;
  32  use gradereport_singleview\local\ui\bulk_insert;
  33  
  34  defined('MOODLE_INTERNAL') || die;
  35  
  36  /**
  37   * The gradebook simple view - base class for the table
  38   *
  39   * @package   gradereport_singleview
  40   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  abstract class tablelike extends screen implements be_readonly {
  44  
  45      /**
  46       * A list of table headers
  47       * @var array $headers
  48       */
  49      protected $headers = [];
  50  
  51      /**
  52       * A list of errors that mean we should not show the table
  53       * @var array $initerrors
  54       */
  55      protected $initerrors = [];
  56  
  57      /**
  58       * Describes the columns in the table
  59       * @var array $definition
  60       */
  61      protected $definition = [];
  62  
  63      /**
  64       * Format a row of the table
  65       *
  66       * @var mixed $item
  67       * @return array
  68       */
  69      abstract public function format_line($item): array;
  70  
  71      /**
  72       * Get the summary for this table.
  73       *
  74       * @return string
  75       */
  76      abstract public function summary(): string;
  77  
  78      /**
  79       * Get the table headers
  80       *
  81       * @return array
  82       */
  83      public function headers(): array {
  84          return $this->headers;
  85      }
  86  
  87      /**
  88       * Set the table headers
  89       *
  90       * @param array $overwrite New headers
  91       * @return tablelike This
  92       */
  93      public function set_headers(array $overwrite): tablelike {
  94          $this->headers = $overwrite;
  95          return $this;
  96      }
  97  
  98      /**
  99       * Get the list of errors
 100       *
 101       * @return array
 102       */
 103      public function init_errors(): array {
 104          return $this->initerrors;
 105      }
 106  
 107      /**
 108       * Set an error detected while building the page.
 109       *
 110       * @param string $mesg
 111       */
 112      public function set_init_error(string $mesg) {
 113          $this->initerrors[] = $mesg;
 114      }
 115  
 116      /**
 117       * Get the table definition
 118       *
 119       * @return array The definition.
 120       */
 121      public function definition(): array {
 122          return $this->definition;
 123      }
 124  
 125      /**
 126       * Set the table definition
 127       *
 128       * @param array $overwrite New definition
 129       * @return tablelike This
 130       */
 131      public function set_definition(array $overwrite): tablelike {
 132          $this->definition = $overwrite;
 133          return $this;
 134      }
 135  
 136      /**
 137       * Get a element to generate the HTML for this table row
 138       * @param grade_grade $grade The grade.
 139       * @return array
 140       */
 141      public function format_definition(grade_grade $grade): array {
 142          $line = [];
 143          foreach ($this->definition() as $i => $field) {
 144              // Table tab index.
 145              $tab = ($i * $this->total) + $this->index;
 146              $classname = '\\gradereport_singleview\\local\\ui\\' . $field;
 147              $html = new $classname($grade, $tab);
 148  
 149              if ($field == 'finalgrade' and !empty($this->structure)) {
 150                  $html .= $this->structure->get_grade_action_menu($grade);
 151              }
 152  
 153              // Singleview users without proper permissions should be presented
 154              // disabled checkboxes for the Exclude grade attribute.
 155              if ($field == 'exclude' && !has_capability('moodle/grade:manage', $this->context)) {
 156                  $html->disabled = true;
 157              }
 158  
 159              $line[$field] = $html;
 160          }
 161          return $line;
 162      }
 163  
 164      /**
 165       * Get the HTML for the whole table
 166       * @return string
 167       */
 168      public function html(): string {
 169          global $OUTPUT;
 170  
 171          if (!empty($this->initerrors)) {
 172              $warnings = '';
 173              foreach ($this->initerrors as $mesg) {
 174                  $warnings .= $OUTPUT->notification($mesg);
 175              }
 176              return $warnings;
 177          }
 178          $table = new html_table();
 179  
 180          $table->head = $this->headers();
 181  
 182          $summary = $this->summary();
 183          if (!empty($summary)) {
 184              $table->caption = $summary;
 185              $table->captionhide = true;
 186          }
 187  
 188          // To be used for extra formatting.
 189          $this->index = 0;
 190          $this->total = count($this->items);
 191  
 192          foreach ($this->items as $item) {
 193              if ($this->index >= ($this->perpage * $this->page) &&
 194                  $this->index < ($this->perpage * ($this->page + 1))) {
 195                  $table->data[] = $this->format_line($item);
 196              }
 197              $this->index++;
 198          }
 199  
 200          $data = new stdClass();
 201          $data->table = $table;
 202          $data->instance = $this;
 203  
 204          $buttonattr = ['class' => 'singleview_buttons submit'];
 205          $buttonhtml = implode(' ', $this->buttons($this->is_readonly()));
 206          $buttons = html_writer::tag('div', $buttonhtml, $buttonattr);
 207  
 208          $sessionvalidation = html_writer::empty_tag('input',
 209              ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
 210  
 211          $html = html_writer::tag('form',
 212              html_writer::table($table)  . $this->bulk_insert() . $buttons . $sessionvalidation,
 213              ['method' => 'POST']
 214          );
 215  
 216          return html_writer::div($html, 'reporttable position-relative');
 217      }
 218  
 219      /**
 220       * Get the HTML for the bulk insert form
 221       *
 222       * @return string
 223       */
 224      public function bulk_insert() {
 225          return html_writer::tag(
 226              'div',
 227              (new bulk_insert($this->item))->html(),
 228              ['class' => 'singleview_bulk', 'hidden' => 'hidden']
 229          );
 230      }
 231  
 232      /**
 233       * Return true if this is read-only.
 234       *
 235       * @return bool
 236       */
 237      public function is_readonly(): bool {
 238          global $USER;
 239          return empty($USER->editing);
 240      }
 241  
 242      /**
 243       * Get the buttons for saving changes.
 244       * @param bool $disabled If button is disabled
 245       *
 246       * @return array
 247       */
 248      public function buttons(bool $disabled = false): array {
 249          global $OUTPUT;
 250          $params = ['type' => 'submit', 'value' => get_string('save', 'gradereport_singleview')];
 251          if ($disabled) {
 252              $params['disabled'] = 'disabled';
 253          }
 254          return [$OUTPUT->render_from_template('gradereport_singleview/button', $params)];
 255      }
 256  }