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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Entity model representing template settings for the seb plugin.
  19   *
  20   * @package    quizaccess_seb
  21   * @author     Nicholas Hoobin <nicholashoobin@catalyst-au.net>
  22   * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
  23   * @copyright  2020 Catalyst IT
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace quizaccess_seb;
  28  
  29  use core\persistent;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Entity model representing template settings for the seb plugin.
  35   *
  36   * @copyright  2020 Catalyst IT
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class template extends persistent {
  40  
  41      /** Table name for the persistent. */
  42      const TABLE = 'quizaccess_seb_template';
  43  
  44      /** @var property_list $plist The SEB config represented as a Property List object. */
  45      private $plist;
  46  
  47      /**
  48       * Return the definition of the properties of this model.
  49       *
  50       * @return array
  51       */
  52      protected static function define_properties() {
  53          return [
  54              'name' => [
  55                  'type' => PARAM_TEXT,
  56                  'default' => '',
  57              ],
  58              'description' => [
  59                  'type' => PARAM_TEXT,
  60                  'default' => '',
  61              ],
  62              'content' => [
  63                  'type' => PARAM_RAW,
  64              ],
  65              'enabled' => [
  66                  'type' => PARAM_INT,
  67                  'default' => 0,
  68              ],
  69              'sortorder' => [
  70                  'type' => PARAM_INT,
  71                  'default' => 0,
  72              ],
  73          ];
  74      }
  75  
  76      /**
  77       * Hook to execute before an update.
  78       *
  79       * Please note that at this stage the data has already been validated and therefore
  80       * any new data being set will not be validated before it is sent to the database.
  81       */
  82      protected function before_update() {
  83          $this->before_save();
  84      }
  85  
  86      /**
  87       * Hook to execute before a create.
  88       *
  89       * Please note that at this stage the data has already been validated and therefore
  90       * any new data being set will not be validated before it is sent to the database.
  91       */
  92      protected function before_create() {
  93          $this->before_save();
  94      }
  95  
  96      /**
  97       * As there is no hook for before both create and update, this function is called by both hooks.
  98       */
  99      private function before_save() {
 100          $this->plist = new property_list($this->get('content'));
 101          $this->set('content', $this->plist->to_xml());
 102      }
 103  
 104      /**
 105       * Validate template content.
 106       *
 107       * @param string $content Content string to validate.
 108       *
 109       * @return bool|\lang_string
 110       */
 111      protected function validate_content(string $content) {
 112          if (helper::is_valid_seb_config($content)) {
 113              return true;
 114          } else {
 115              return new \lang_string('invalidtemplate', 'quizaccess_seb');
 116          }
 117      }
 118  
 119      /**
 120       * Check if we can delete the template.
 121       *
 122       * @return bool
 123       */
 124      public function can_delete() : bool {
 125          $result = true;
 126  
 127          if ($this->get('id')) {
 128              $settings = seb_quiz_settings::get_records(['templateid' => $this->get('id')]);
 129              $result = empty($settings);
 130          }
 131  
 132          return $result;
 133      }
 134  
 135  }