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.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 html_table;
  28  use html_writer;
  29  use stdClass;
  30  use grade_item;
  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 {
  44  
  45      /** @var array $headers A list of table headers */
  46      protected $headers = array();
  47  
  48      /** @var array $initerrors A list of errors that mean we should not show the table */
  49      protected $initerrors = array();
  50  
  51      /** @var array $definition Describes the columns in the table */
  52      protected $definition = array();
  53  
  54      /**
  55       * Format a row of the table
  56       *
  57       * @param mixed $item
  58       * @return string
  59       */
  60      public abstract function format_line($item);
  61  
  62      /**
  63       * Get the summary for this table.
  64       *
  65       * @return string
  66       */
  67      public abstract function summary();
  68  
  69      /**
  70       * Get the table headers
  71       *
  72       * @return array
  73       */
  74      public function headers() {
  75          return $this->headers;
  76      }
  77  
  78      /**
  79       * Set the table headers
  80       *
  81       * @param array $overwrite New headers
  82       * @return tablelike This
  83       */
  84      public function set_headers($overwrite) {
  85          $this->headers = $overwrite;
  86          return $this;
  87      }
  88  
  89      /**
  90       * Get the list of errors
  91       *
  92       * @return array
  93       */
  94      public function init_errors() {
  95          return $this->initerrors;
  96      }
  97  
  98      /**
  99       * Set an error detected while building the page.
 100       *
 101       * @param string $mesg
 102       */
 103      public function set_init_error($mesg) {
 104          $this->initerrors[] = $mesg;
 105      }
 106  
 107      /**
 108       * Get the table definition
 109       *
 110       * @return array The definition.
 111       */
 112      public function definition() {
 113          return $this->definition;
 114      }
 115  
 116      /**
 117       * Set the table definition
 118       *
 119       * @param array $overwrite New definition
 120       * @return tablelike This
 121       */
 122      public function set_definition($overwrite) {
 123          $this->definition = $overwrite;
 124          return $this;
 125      }
 126  
 127      /**
 128       * Get a element to generate the HTML for this table row
 129       * @param array $line This is a list of lines in the table (modified)
 130       * @param grade_grade $grade The grade.
 131       * @return string
 132       */
 133      public function format_definition($line, $grade) {
 134          foreach ($this->definition() as $i => $field) {
 135              // Table tab index.
 136              $tab = ($i * $this->total) + $this->index;
 137              $classname = '\\gradereport_singleview\\local\\ui\\' . $field;
 138              $html = new $classname($grade, $tab);
 139  
 140              if ($field == 'finalgrade' and !empty($this->structure)) {
 141                  $html .= $this->structure->get_grade_analysis_icon($grade);
 142              }
 143  
 144              // Singleview users without proper permissions should be presented
 145              // disabled checkboxes for the Exclude grade attribute.
 146              if ($field == 'exclude' && !has_capability('moodle/grade:manage', $this->context)){
 147                  $html->disabled = true;
 148              }
 149  
 150              $line[] = $html;
 151          }
 152          return $line;
 153      }
 154  
 155      /**
 156       * Get the HTML for the whole table
 157       * @return string
 158       */
 159      public function html() {
 160          global $OUTPUT;
 161  
 162          if (!empty($this->initerrors)) {
 163              $warnings = '';
 164              foreach ($this->initerrors as $mesg) {
 165                  $warnings .= $OUTPUT->notification($mesg);
 166              }
 167              return $warnings;
 168          }
 169          $table = new html_table();
 170  
 171          $table->head = $this->headers();
 172  
 173          $summary = $this->summary();
 174          if (!empty($summary)) {
 175              $table->caption = $summary;
 176              $table->captionhide = true;
 177          }
 178  
 179          // To be used for extra formatting.
 180          $this->index = 0;
 181          $this->total = count($this->items);
 182  
 183          foreach ($this->items as $item) {
 184              if ($this->index >= ($this->perpage * $this->page) &&
 185                  $this->index < ($this->perpage * ($this->page + 1))) {
 186                  $table->data[] = $this->format_line($item);
 187              }
 188              $this->index++;
 189          }
 190  
 191          $underlying = get_class($this);
 192  
 193          $data = new stdClass();
 194          $data->table = $table;
 195          $data->instance = $this;
 196  
 197          $buttonattr = array('class' => 'singleview_buttons submit');
 198          $buttonhtml = implode(' ', $this->buttons());
 199  
 200          $buttons = html_writer::tag('div', $buttonhtml, $buttonattr);
 201          $selectview = new select($this->courseid, $this->itemid, $this->groupid);
 202  
 203          $sessionvalidation = html_writer::empty_tag('input',
 204              array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 205  
 206          $html = $selectview->html();
 207          $html .= html_writer::tag('form',
 208              $buttons . html_writer::table($table) . $this->bulk_insert() . $buttons . $sessionvalidation,
 209              array('method' => 'POST')
 210          );
 211          $html .= $selectview->html();
 212          return $html;
 213      }
 214  
 215      /**
 216       * Get the HTML for the bulk insert form
 217       *
 218       * @return string
 219       */
 220      public function bulk_insert() {
 221          return html_writer::tag(
 222              'div',
 223              (new bulk_insert($this->item))->html(),
 224              array('class' => 'singleview_bulk')
 225          );
 226      }
 227  
 228      /**
 229       * Get the buttons for saving changes.
 230       *
 231       * @return array
 232       */
 233      public function buttons() {
 234          global $OUTPUT;
 235  
 236          $save = $OUTPUT->render_from_template('gradereport_singleview/button', [
 237              'type' => 'submit',
 238              'value' => get_string('save', 'gradereport_singleview'),
 239          ]);
 240  
 241          return array($save);
 242      }
 243  }