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.
   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   * Class for manipulating with the template records.
  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;
  27  
  28  use core\notification;
  29  use quizaccess_seb\local\table\template_list;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Class for manipulating with the template records.
  35   *
  36   * @copyright  2020 Catalyst IT
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class template_controller {
  40      /**
  41       * View action.
  42       */
  43      const ACTION_VIEW = 'view';
  44  
  45      /**
  46       * Add action.
  47       */
  48      const ACTION_ADD = 'add';
  49  
  50      /**
  51       * Edit action.
  52       */
  53      const ACTION_EDIT = 'edit';
  54  
  55      /**
  56       * Delete action.
  57       */
  58      const ACTION_DELETE = 'delete';
  59  
  60      /**
  61       * Hide action.
  62       */
  63      const ACTION_HIDE = 'hide';
  64  
  65      /**
  66       * Show action.
  67       */
  68      const ACTION_SHOW = 'show';
  69  
  70  
  71      /**
  72       * Locally cached $OUTPUT object.
  73       * @var \bootstrap_renderer
  74       */
  75      protected $output;
  76  
  77      /**
  78       * region_manager constructor.
  79       */
  80      public function __construct() {
  81          global $OUTPUT;
  82  
  83          $this->output = $OUTPUT;
  84      }
  85  
  86      /**
  87       * Execute required action.
  88       *
  89       * @param string $action Action to execute.
  90       */
  91      public function execute($action) {
  92  
  93          $this->set_external_page();
  94  
  95          switch($action) {
  96              case self::ACTION_ADD:
  97              case self::ACTION_EDIT:
  98                  $this->edit($action, optional_param('id', null, PARAM_INT));
  99                  break;
 100  
 101              case self::ACTION_DELETE:
 102                  $this->delete(required_param('id', PARAM_INT));
 103                  break;
 104  
 105              case self::ACTION_HIDE:
 106                  $this->hide(required_param('id', PARAM_INT));
 107                  break;
 108  
 109              case self::ACTION_SHOW:
 110                  $this->show(required_param('id', PARAM_INT));
 111                  break;
 112  
 113              case self::ACTION_VIEW:
 114              default:
 115                  $this->view();
 116                  break;
 117          }
 118      }
 119  
 120      /**
 121       * Set external page for the manager.
 122       */
 123      protected function set_external_page() {
 124          admin_externalpage_setup('quizaccess_seb/template');
 125      }
 126  
 127      /**
 128       * Return record instance.
 129       *
 130       * @param int $id
 131       * @param \stdClass|null $data
 132       *
 133       * @return \quizaccess_seb\template
 134       */
 135      protected function get_instance($id = 0, \stdClass $data = null) {
 136          return new template($id, $data);
 137      }
 138  
 139      /**
 140       * Print out all records in a table.
 141       */
 142      protected function display_all_records() {
 143          $records = template::get_records([], 'id');
 144  
 145          $table = new template_list();
 146          $table->display($records);
 147      }
 148  
 149      /**
 150       * Returns a text for create new record button.
 151       * @return string
 152       */
 153      protected function get_create_button_text() : string {
 154          return get_string('addtemplate', 'quizaccess_seb');
 155      }
 156  
 157      /**
 158       * Returns form for the record.
 159       *
 160       * @param \quizaccess_seb\template|null $instance
 161       *
 162       * @return \quizaccess_seb\local\form\template
 163       */
 164      protected function get_form($instance) : \quizaccess_seb\local\form\template {
 165          global $PAGE;
 166  
 167          return new \quizaccess_seb\local\form\template($PAGE->url->out(false), ['persistent' => $instance]);
 168      }
 169  
 170      /**
 171       * View page heading string.
 172       * @return string
 173       */
 174      protected function get_view_heading() : string {
 175          return get_string('managetemplates', 'quizaccess_seb');
 176      }
 177  
 178      /**
 179       * New record heading string.
 180       * @return string
 181       */
 182      protected function get_new_heading() : string {
 183          return get_string('newtemplate', 'quizaccess_seb');
 184      }
 185  
 186      /**
 187       * Edit record heading string.
 188       * @return string
 189       */
 190      protected function get_edit_heading() : string {
 191          return get_string('edittemplate', 'quizaccess_seb');
 192      }
 193  
 194      /**
 195       * Returns base URL for the manager.
 196       * @return string
 197       */
 198      public static function get_base_url() : string {
 199          return '/mod/quiz/accessrule/seb/template.php';
 200      }
 201  
 202      /**
 203       * Execute edit action.
 204       *
 205       * @param string $action Could be edit or create.
 206       * @param null|int $id Id of the region or null if creating a new one.
 207       */
 208      protected function edit($action, $id = null) {
 209          global $PAGE;
 210  
 211          $PAGE->set_url(new \moodle_url(static::get_base_url(), ['action' => $action, 'id' => $id]));
 212          $instance = null;
 213  
 214          if ($id) {
 215              $instance = $this->get_instance($id);
 216          }
 217  
 218          $form = $this->get_form($instance);
 219  
 220          if ($form->is_cancelled()) {
 221              redirect(new \moodle_url(static::get_base_url()));
 222          } else if ($data = $form->get_data()) {
 223              unset($data->submitbutton);
 224              try {
 225                  if (empty($data->id)) {
 226                      $data->content = $form->get_file_content('content');
 227                      $persistent = $this->get_instance(0, $data);
 228                      $persistent->create();
 229  
 230                      \quizaccess_seb\event\template_created::create_strict(
 231                          $persistent,
 232                          \context_system::instance()
 233                      )->trigger();
 234                      $this->trigger_enabled_event($persistent);
 235                  } else {
 236                      $instance->from_record($data);
 237                      $instance->update();
 238  
 239                      \quizaccess_seb\event\template_updated::create_strict(
 240                          $instance,
 241                          \context_system::instance()
 242                      )->trigger();
 243                      $this->trigger_enabled_event($instance);
 244                  }
 245                  notification::success(get_string('changessaved'));
 246              } catch (\Exception $e) {
 247                  notification::error($e->getMessage());
 248              }
 249              redirect(new \moodle_url(static::get_base_url()));
 250          } else {
 251              if (empty($instance)) {
 252                  $this->header($this->get_new_heading());
 253              } else {
 254                  if (!$instance->can_delete()) {
 255                      notification::warning(get_string('cantedit', 'quizaccess_seb'));
 256                  }
 257                  $this->header($this->get_edit_heading());
 258              }
 259          }
 260  
 261          $form->display();
 262          $this->footer();
 263      }
 264  
 265      /**
 266       * Execute delete action.
 267       *
 268       * @param int $id ID of the region.
 269       */
 270      protected function delete($id) {
 271          require_sesskey();
 272          $instance = $this->get_instance($id);
 273  
 274          if ($instance->can_delete()) {
 275              $instance->delete();
 276              notification::success(get_string('deleted'));
 277  
 278              \quizaccess_seb\event\template_deleted::create_strict(
 279                  $id,
 280                  \context_system::instance()
 281              )->trigger();
 282  
 283              redirect(new \moodle_url(static::get_base_url()));
 284          } else {
 285              notification::warning(get_string('cantdelete', 'quizaccess_seb'));
 286              redirect(new \moodle_url(static::get_base_url()));
 287          }
 288      }
 289  
 290      /**
 291       * Execute view action.
 292       */
 293      protected function view() {
 294          global $PAGE;
 295  
 296          $this->header($this->get_view_heading());
 297          $this->print_add_button();
 298          $this->display_all_records();
 299  
 300          // JS for Template management.
 301          $PAGE->requires->js_call_amd('quizaccess_seb/managetemplates', 'setup');
 302  
 303          $this->footer();
 304      }
 305  
 306      /**
 307       * Show the template.
 308       *
 309       * @param int $id The ID of the template to show.
 310       */
 311      protected function show(int $id) {
 312          $this->show_hide($id, 1);
 313      }
 314  
 315      /**
 316       * Hide the template.
 317       *
 318       * @param int $id The ID of the template to hide.
 319       */
 320      protected function hide($id) {
 321          $this->show_hide($id, 0);
 322      }
 323  
 324      /**
 325       * Show or Hide the template.
 326       *
 327       * @param int $id The ID of the template to hide.
 328       * @param int $visibility The intended visibility.
 329       */
 330      protected function show_hide(int $id, int $visibility) {
 331          require_sesskey();
 332          $template = $this->get_instance($id);
 333          $template->set('enabled', $visibility);
 334          $template->save();
 335  
 336          $this->trigger_enabled_event($template);
 337  
 338          redirect(new \moodle_url(self::get_base_url()));
 339      }
 340  
 341      /**
 342       * Print out add button.
 343       */
 344      protected function print_add_button() {
 345          echo $this->output->single_button(
 346              new \moodle_url(static::get_base_url(), ['action' => self::ACTION_ADD]),
 347              $this->get_create_button_text()
 348          );
 349      }
 350  
 351      /**
 352       * Print out page header.
 353       * @param string $title Title to display.
 354       */
 355      protected function header($title) {
 356          echo $this->output->header();
 357          echo $this->output->heading($title);
 358      }
 359  
 360      /**
 361       * Print out the page footer.
 362       *
 363       * @return void
 364       */
 365      protected function footer() {
 366          echo $this->output->footer();
 367      }
 368  
 369      /**
 370       * Helper function to fire off an event that informs of if a template is enabled or not.
 371       *
 372       * @param template $template The template persistent object.
 373       */
 374      private function trigger_enabled_event(template $template) {
 375          $eventstring = ($template->get('enabled') == 0 ? 'disabled' : 'enabled');
 376  
 377          $func = '\quizaccess_seb\event\template_' . $eventstring;
 378          $func::create_strict(
 379              $template,
 380              \context_system::instance()
 381          )->trigger();
 382      }
 383  
 384  }