Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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       * Total items
  65       * @var int $total
  66       */
  67      protected $total;
  68  
  69      /**
  70       * Table tab index
  71       * @var int $index
  72       */
  73      protected $index;
  74  
  75      /**
  76       *  The grade item or user.
  77       * @var mixed $item
  78       */
  79      protected $item;
  80  
  81      /**
  82       * Format a row of the table
  83       *
  84       * @var mixed $item
  85       * @return array
  86       */
  87      abstract public function format_line($item): array;
  88  
  89      /**
  90       * Get the summary for this table.
  91       *
  92       * @return string
  93       */
  94      abstract public function summary(): string;
  95  
  96      /**
  97       * Get the table headers
  98       *
  99       * @return array
 100       */
 101      public function headers(): array {
 102          return $this->headers;
 103      }
 104  
 105      /**
 106       * Set the table headers
 107       *
 108       * @param array $overwrite New headers
 109       * @return tablelike This
 110       */
 111      public function set_headers(array $overwrite): tablelike {
 112          $this->headers = $overwrite;
 113          return $this;
 114      }
 115  
 116      /**
 117       * Get the list of errors
 118       *
 119       * @return array
 120       */
 121      public function init_errors(): array {
 122          return $this->initerrors;
 123      }
 124  
 125      /**
 126       * Set an error detected while building the page.
 127       *
 128       * @param string $mesg
 129       */
 130      public function set_init_error(string $mesg) {
 131          $this->initerrors[] = $mesg;
 132      }
 133  
 134      /**
 135       * Get the table definition
 136       *
 137       * @return array The definition.
 138       */
 139      public function definition(): array {
 140          return $this->definition;
 141      }
 142  
 143      /**
 144       * Set the table definition
 145       *
 146       * @param array $overwrite New definition
 147       * @return tablelike This
 148       */
 149      public function set_definition(array $overwrite): tablelike {
 150          $this->definition = $overwrite;
 151          return $this;
 152      }
 153  
 154      /**
 155       * Get a element to generate the HTML for this table row
 156       * @param grade_grade $grade The grade.
 157       * @return array
 158       */
 159      public function format_definition(grade_grade $grade): array {
 160          $line = [];
 161          foreach ($this->definition() as $i => $field) {
 162              // Table tab index.
 163              $tab = ($i * $this->total) + $this->index;
 164              $classname = '\\gradereport_singleview\\local\\ui\\' . $field;
 165              $html = new $classname($grade, $tab);
 166  
 167              if ($field == 'finalgrade' and !empty($this->structure)) {
 168                  $html .= $this->structure->get_grade_action_menu($grade);
 169              }
 170  
 171              // Singleview users without proper permissions should be presented
 172              // disabled checkboxes for the Exclude grade attribute.
 173              if ($field == 'exclude' && !has_capability('moodle/grade:manage', $this->context)) {
 174                  $html->disabled = true;
 175              }
 176  
 177              $line[$field] = $html;
 178          }
 179          return $line;
 180      }
 181  
 182      /**
 183       * Get the HTML for the whole table
 184       * @return string
 185       */
 186      public function html(): string {
 187          global $OUTPUT;
 188  
 189          if (!empty($this->initerrors)) {
 190              $warnings = '';
 191              foreach ($this->initerrors as $mesg) {
 192                  $warnings .= $OUTPUT->notification($mesg);
 193              }
 194              return $warnings;
 195          }
 196          $table = new html_table();
 197  
 198          $table->head = $this->headers();
 199  
 200          $summary = $this->summary();
 201          if (!empty($summary)) {
 202              $table->caption = $summary;
 203              $table->captionhide = true;
 204          }
 205  
 206          // To be used for extra formatting.
 207          $this->index = 0;
 208          $this->total = count($this->items);
 209  
 210          foreach ($this->items as $item) {
 211              if ($this->index >= ($this->perpage * $this->page) &&
 212                  $this->index < ($this->perpage * ($this->page + 1))) {
 213                  $table->data[] = $this->format_line($item);
 214              }
 215              $this->index++;
 216          }
 217  
 218          $data = new stdClass();
 219          $data->table = $table;
 220          $data->instance = $this;
 221  
 222          $buttonattr = ['class' => 'singleview_buttons submit'];
 223          $buttonhtml = implode(' ', $this->buttons($this->is_readonly()));
 224          $buttons = html_writer::tag('div', $buttonhtml, $buttonattr);
 225  
 226          $sessionvalidation = html_writer::empty_tag('input',
 227              ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
 228  
 229          $html = html_writer::tag('form',
 230              html_writer::table($table)  . $this->bulk_insert() . $buttons . $sessionvalidation,
 231              ['method' => 'POST']
 232          );
 233  
 234          return html_writer::div($html, 'reporttable position-relative');
 235      }
 236  
 237      /**
 238       * Get the HTML for the bulk insert form
 239       *
 240       * @return string
 241       */
 242      public function bulk_insert() {
 243          return html_writer::tag(
 244              'div',
 245              (new bulk_insert($this->item))->html(),
 246              ['class' => 'singleview_bulk', 'hidden' => 'hidden']
 247          );
 248      }
 249  
 250      /**
 251       * Return true if this is read-only.
 252       *
 253       * @return bool
 254       */
 255      public function is_readonly(): bool {
 256          global $USER;
 257          return empty($USER->editing);
 258      }
 259  
 260      /**
 261       * Get the buttons for saving changes.
 262       * @param bool $disabled If button is disabled
 263       *
 264       * @return array
 265       */
 266      public function buttons(bool $disabled = false): array {
 267          global $OUTPUT;
 268          $params = ['type' => 'submit', 'value' => get_string('save', 'gradereport_singleview')];
 269          if ($disabled) {
 270              $params['disabled'] = 'disabled';
 271          }
 272          return [$OUTPUT->render_from_template('gradereport_singleview/button', $params)];
 273      }
 274  }