Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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          $table->id = 'singleview-grades';
 198  
 199          $table->head = $this->headers();
 200  
 201          $summary = $this->summary();
 202          if (!empty($summary)) {
 203              $table->caption = $summary;
 204              $table->captionhide = true;
 205          }
 206  
 207          // To be used for extra formatting.
 208          $this->index = 0;
 209          $this->total = count($this->items);
 210  
 211          foreach ($this->items as $item) {
 212              if ($this->index >= ($this->perpage * $this->page) &&
 213                  $this->index < ($this->perpage * ($this->page + 1))) {
 214                  $table->data[] = $this->format_line($item);
 215              }
 216              $this->index++;
 217          }
 218  
 219          $data = new stdClass();
 220          $data->table = $table;
 221          $data->instance = $this;
 222  
 223          $html = html_writer::table($table);
 224  
 225          return html_writer::div($html, 'reporttable position-relative');
 226      }
 227  
 228      /**
 229       * Get the HTML for the bulk insert form
 230       *
 231       * @return string
 232       */
 233      public function bulk_insert() {
 234          return html_writer::tag(
 235              'div',
 236              (new bulk_insert($this->item))->html(),
 237              ['class' => 'singleview_bulk', 'hidden' => 'hidden']
 238          );
 239      }
 240  
 241      /**
 242       * Return true if this is read-only.
 243       *
 244       * @return bool
 245       */
 246      public function is_readonly(): bool {
 247          global $USER;
 248          return empty($USER->editing);
 249      }
 250  
 251      /**
 252       * Get the buttons for saving changes.
 253       * @param bool $disabled If button is disabled
 254       *
 255       * @return array
 256       */
 257      public function buttons(bool $disabled = false): array {
 258          global $OUTPUT;
 259          $params = ['type' => 'submit', 'value' => get_string('save', 'gradereport_singleview')];
 260          if ($disabled) {
 261              $params['disabled'] = 'disabled';
 262          }
 263          return [$OUTPUT->render_from_template('gradereport_singleview/button', $params)];
 264      }
 265  }