Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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 loading/storing learning plan templates from the DB.
  19   *
  20   * @package    core_competency
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_competency;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use context;
  28  use lang_string;
  29  use stdClass;
  30  
  31  /**
  32   * Class for loading/storing learning plan templates from the DB.
  33   *
  34   * @copyright  2015 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class template extends persistent {
  38  
  39      const TABLE = 'competency_template';
  40  
  41      /** @var template object before update. */
  42      protected $beforeupdate = null;
  43  
  44      /**
  45       * Return the definition of the properties of this model.
  46       *
  47       * @return array
  48       */
  49      protected static function define_properties() {
  50          return array(
  51              'shortname' => array(
  52                  'type' => PARAM_TEXT,
  53              ),
  54              'description' => array(
  55                  'default' => '',
  56                  'type' => PARAM_CLEANHTML,
  57              ),
  58              'descriptionformat' => array(
  59                  'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
  60                  'type' => PARAM_INT,
  61                  'default' => FORMAT_HTML
  62              ),
  63              'duedate' => array(
  64                  'default' => 0,
  65                  'type' => PARAM_INT,
  66              ),
  67              'visible' => array(
  68                  'default' => 1,
  69                  'type' => PARAM_BOOL,
  70              ),
  71              'contextid' => array(
  72                  'type' => PARAM_INT
  73              ),
  74          );
  75      }
  76  
  77      /**
  78       * Hook to execute after an update.
  79       *
  80       * @param bool $result Whether or not the update was successful.
  81       * @return void
  82       */
  83      protected function after_update($result) {
  84          $this->beforeupdate = null;
  85      }
  86  
  87      /**
  88       * Hook to execute before validate.
  89       *
  90       * @return void
  91       */
  92      protected function before_validate() {
  93          $this->beforeupdate = null;
  94  
  95          // During update.
  96          if ($this->get('id')) {
  97              $this->beforeupdate = new self($this->get('id'));
  98          }
  99      }
 100  
 101      /**
 102       * Whether or not the current user can read the template.
 103       *
 104       * @return bool
 105       */
 106      public function can_manage() {
 107          return self::can_manage_context($this->get_context());
 108      }
 109  
 110      /**
 111       * Whether or not the current user can manage the template.
 112       *
 113       * @param  context $context
 114       * @return bool
 115       */
 116      public static function can_manage_context($context) {
 117          return has_capability('moodle/competency:templatemanage', $context);
 118      }
 119  
 120      /**
 121       * Whether or not the current user can read the template.
 122       *
 123       * @return bool
 124       */
 125      public function can_read() {
 126          return self::can_read_context($this->get_context());
 127      }
 128  
 129      /**
 130       * Whether or not the current user can read the template.
 131       *
 132       * @param  context $context
 133       * @return bool
 134       */
 135      public static function can_read_context($context) {
 136          return has_capability('moodle/competency:templateview', $context) || self::can_manage_context($context);
 137      }
 138  
 139      /**
 140       * Get the context.
 141       *
 142       * @return context The context
 143       */
 144      public function get_context() {
 145          return context::instance_by_id($this->get('contextid'));
 146      }
 147  
 148      /**
 149       * Validate the context ID.
 150       *
 151       * @param  int $value The context ID.
 152       * @return bool|lang_string
 153       */
 154      protected function validate_contextid($value) {
 155          $context = context::instance_by_id($value, IGNORE_MISSING);
 156          if (!$context) {
 157              return new lang_string('invalidcontext', 'error');
 158          } else if ($context->contextlevel != CONTEXT_SYSTEM && $context->contextlevel != CONTEXT_COURSECAT) {
 159              return new lang_string('invalidcontext', 'error');
 160          }
 161          return true;
 162      }
 163  
 164      /**
 165       * Validate the due date.
 166       *
 167       * The due date can always be changed, but when it is it must be:
 168       *  - unset
 169       *  - set in the future.
 170       *
 171       * @param  int $value The due date.
 172       * @return bool|lang_string
 173       */
 174      protected function validate_duedate($value) {
 175  
 176          // During update.
 177          if ($this->get('id')) {
 178              $before = $this->beforeupdate->get('duedate');
 179  
 180              // The value has not changed, then it's always OK.
 181              if ($before == $value) {
 182                  return true;
 183              }
 184          }
 185  
 186          // During create and update, the date must be set in the future, or not set.
 187          if (!empty($value) && $value <= time() - 600) {
 188              // We cannot set the date in the past. But we allow for 10 minutes of margin so that
 189              // a user can set the due date to "now" without risking to hit a validation error.
 190              return new lang_string('errorcannotsetduedateinthepast', 'core_competency');
 191          }
 192  
 193          return true;
 194      }
 195  
 196      /**
 197       * Returns true when the template has user learning plans.
 198       *
 199       * @return boolean
 200       */
 201      public function has_plans() {
 202          return plan::has_records_for_template($this->get('id'));
 203      }
 204  
 205  }