[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/report/singleview/classes/local/screen/ -> tablelike.php (source)

   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              $line[] = $html;
 145          }
 146          return $line;
 147      }
 148  
 149      /**
 150       * Get the HTML for the whole table
 151       * @return string
 152       */
 153      public function html() {
 154          global $OUTPUT;
 155  
 156          if (!empty($this->initerrors)) {
 157              $warnings = '';
 158              foreach ($this->initerrors as $mesg) {
 159                  $warnings .= $OUTPUT->notification($mesg);
 160              }
 161              return $warnings;
 162          }
 163          $table = new html_table();
 164  
 165          $table->head = $this->headers();
 166  
 167          $summary = $this->summary();
 168          if (!empty($summary)) {
 169              $table->summary = $summary;
 170          }
 171  
 172          // To be used for extra formatting.
 173          $this->index = 0;
 174          $this->total = count($this->items);
 175  
 176          foreach ($this->items as $item) {
 177              if ($this->index >= ($this->perpage * $this->page) &&
 178                  $this->index < ($this->perpage * ($this->page + 1))) {
 179                  $table->data[] = $this->format_line($item);
 180              }
 181              $this->index++;
 182          }
 183  
 184          $underlying = get_class($this);
 185  
 186          $data = new stdClass;
 187          $data->table = $table;
 188          $data->instance = $this;
 189  
 190          $buttonattr = array('class' => 'singleview_buttons submit');
 191          $buttonhtml = implode(' ', $this->buttons());
 192  
 193          $buttons = html_writer::tag('div', $buttonhtml, $buttonattr);
 194  
 195          $sessionvalidation = html_writer::empty_tag('input',
 196              array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 197  
 198          return html_writer::tag('form',
 199              $buttons . html_writer::table($table) . $this->bulk_insert() . $buttons . $sessionvalidation,
 200              array('method' => 'POST')
 201          );
 202      }
 203  
 204      /**
 205       * Get the HTML for the bulk insert form
 206       *
 207       * @return string
 208       */
 209      public function bulk_insert() {
 210          return html_writer::tag(
 211              'div',
 212              (new bulk_insert($this->item))->html(),
 213              array('class' => 'singleview_bulk')
 214          );
 215      }
 216  
 217      /**
 218       * Get the buttons for saving changes.
 219       *
 220       * @return array
 221       */
 222      public function buttons() {
 223          $save = html_writer::empty_tag('input', array(
 224              'type' => 'submit',
 225              'value' => get_string('update'),
 226          ));
 227  
 228          return array($save);
 229      }
 230  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1