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.
   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   * Templates table.
  19   *
  20   * @package    quizaccess_seb
  21   * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
  22   * @copyright  2020 Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace quizaccess_seb\local\table;
  27  
  28  use quizaccess_seb\helper;
  29  use quizaccess_seb\template;
  30  use quizaccess_seb\template_controller;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  require_once($CFG->libdir.'/tablelib.php');
  35  
  36  /**
  37   * Templates table.
  38   *
  39   * @copyright  2020 Catalyst IT
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class template_list extends \flexible_table {
  43  
  44      /**
  45       * @var int Autogenerated id.
  46       */
  47      private static $autoid = 0;
  48  
  49      /**
  50       * Constructor
  51       *
  52       * @param string|null $id to be used by the table, autogenerated if null.
  53       */
  54      public function __construct($id = null) {
  55          global $PAGE;
  56  
  57          $id = (is_null($id) ? self::$autoid++ : $id);
  58          parent::__construct('quizaccess_seb' . $id);
  59  
  60          $this->define_baseurl($PAGE->url);
  61          $this->set_attribute('class', 'generaltable admintable');
  62  
  63          // Column definition.
  64          $this->define_columns([
  65              'name',
  66              'description',
  67              'enabled',
  68              'used',
  69              'actions',
  70          ]);
  71  
  72          $this->define_headers([
  73              get_string('name', 'quizaccess_seb'),
  74              get_string('description', 'quizaccess_seb'),
  75              get_string('enabled', 'quizaccess_seb'),
  76              get_string('used', 'quizaccess_seb'),
  77              get_string('actions'),
  78          ]);
  79  
  80          $this->setup();
  81      }
  82  
  83      /**
  84       * Display name column.
  85       *
  86       * @param \quizaccess_seb\template $data Template for this row.
  87       * @return string
  88       */
  89      protected function col_name(template $data) : string {
  90          return \html_writer::link(
  91              new \moodle_url(template_controller::get_base_url(), [
  92                  'id' => $data->get('id'),
  93                  'action' => template_controller::ACTION_EDIT,
  94              ]),
  95              $data->get('name')
  96          );
  97      }
  98  
  99      /**
 100       * Display description column.
 101       *
 102       * @param \quizaccess_seb\template $data Template for this row.
 103       * @return string
 104       */
 105      protected function col_description(template $data) : string {
 106          return $data->get('description');
 107      }
 108  
 109      /**
 110       * Display enabled column.
 111       *
 112       * @param \quizaccess_seb\template $data Template for this row.
 113       * @return string
 114       */
 115      protected function col_enabled(template $data): string {
 116          return empty($data->get('enabled')) ? get_string('no') : get_string('yes');
 117      }
 118  
 119      /**
 120       * Display if a template is being used.
 121       *
 122       * @param \quizaccess_seb\template $data Template for this row.
 123       * @return string
 124       */
 125      protected function col_used(template $data): string {
 126          return $data->can_delete() ? get_string('no') : get_string('yes');
 127      }
 128  
 129      /**
 130       * Display actions column.
 131       *
 132       * @param \quizaccess_seb\template $data Template for this row.
 133       * @return string
 134       */
 135      protected function col_actions(template $data) : string {
 136          $actions = [];
 137  
 138          $actions[] = helper::format_icon_link(
 139              new \moodle_url(template_controller::get_base_url(), [
 140                  'id'        => $data->get('id'),
 141                  'action'    => template_controller::ACTION_EDIT,
 142              ]),
 143              't/edit',
 144              get_string('edit')
 145          );
 146  
 147          $actions[] = helper::format_icon_link(
 148              new \moodle_url(template_controller::get_base_url(), [
 149                  'id'        => $data->get('id'),
 150                  'action'    => template_controller::ACTION_DELETE,
 151                  'sesskey'   => sesskey(),
 152              ]),
 153              't/delete',
 154              get_string('delete'),
 155              null,
 156              [
 157              'data-action' => 'delete',
 158              'data-id' => $data->get('id'),
 159              ]
 160          );
 161  
 162          return implode('&nbsp;', $actions);
 163      }
 164  
 165      /**
 166       * Sets the data of the table.
 167       *
 168       * @param \quizaccess_seb\template[] $records An array with records.
 169       */
 170      public function display(array $records) {
 171          foreach ($records as $record) {
 172              $this->add_data_keyed($this->format_row($record));
 173          }
 174  
 175          $this->finish_output();
 176      }
 177  
 178  }