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.
   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   * Form 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\local\form;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Form for manipulating with the template records.
  32   *
  33   * @copyright  2020 Catalyst IT
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class template extends \core\form\persistent {
  37  
  38      /** @var string Persistent class name. */
  39      protected static $persistentclass = 'quizaccess_seb\\template';
  40  
  41      /**
  42       * Form definition.
  43       */
  44      protected function definition() {
  45          $mform = $this->_form;
  46  
  47          $mform->addElement('text', 'name', get_string('name', 'quizaccess_seb'));
  48          $mform->addRule('name', get_string('required'), 'required', null, 'client');
  49          $mform->setType('name', PARAM_TEXT);
  50  
  51          $mform->addElement('textarea', 'description', get_string('description', 'quizaccess_seb'));
  52          $mform->setType('description', PARAM_TEXT);
  53  
  54          if ($this->get_persistent()->get('id')) {
  55              $mform->addElement('textarea', 'content', get_string('content', 'quizaccess_seb'), ['rows' => 20, 'cols' => 60]);
  56              $mform->addRule('content', get_string('required'), 'required');
  57          } else {
  58              $mform->addElement('filepicker', 'content', get_string('content', 'quizaccess_seb'));
  59              $mform->addRule('content', get_string('required'), 'required');
  60          }
  61  
  62          $mform->addElement('selectyesno', 'enabled', get_string('enabled', 'quizaccess_seb'));
  63          $mform->setType('enabled', PARAM_INT);
  64  
  65          $this->add_action_buttons();
  66  
  67          if (!empty($this->get_persistent()) && !$this->get_persistent()->can_delete()) {
  68              $mform->hardFreezeAllVisibleExcept([]);
  69              $mform->addElement('cancel');
  70          }
  71      }
  72  
  73      /**
  74       * Filter out the foreign fields of the persistent.
  75       *
  76       * @param \stdClass $data The data to filter the fields out of.
  77       * @return \stdClass.
  78       */
  79      protected function filter_data_for_persistent($data) {
  80          // Uploading a new template file.
  81          if (empty($this->get_persistent()->get('id'))) {
  82              $files = $this->get_draft_files('content');
  83              if ($files) {
  84                  $file = reset($files);
  85                  $data->content = $file->get_content();
  86              } else {
  87                  // No file found. Remove content data and let persistent to return an error.
  88                  unset($data->content);
  89              }
  90          }
  91  
  92          return parent::filter_data_for_persistent($data);
  93      }
  94  
  95      /**
  96       * Extra validation.
  97       *
  98       * @param  \stdClass $data Data to validate.
  99       * @param  array $files Array of files.
 100       * @param  array $errors Currently reported errors.
 101       * @return array of additional errors, or overridden errors.
 102       */
 103      protected function extra_validation($data, $files, array &$errors) {
 104          $newerrors = [];
 105  
 106          // Check name.
 107          if (empty($data->name)) {
 108              $newerrors['name'] = get_string('namerequired', 'quizaccess_seb');
 109          }
 110  
 111          return $newerrors;
 112      }
 113  }