MDL-61135 templates: improve loading icon for loading templates
[moodle.git] / competency / classes / template.php
blob2cb57134715ae4bab9304ad00380ef5c6c4ccaca
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.
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/>.
17 /**
18 * Class for loading/storing learning plan templates from the DB.
20 * @package core_competency
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_competency;
25 defined('MOODLE_INTERNAL') || die();
27 use context;
28 use lang_string;
29 use stdClass;
31 /**
32 * Class for loading/storing learning plan templates from the DB.
34 * @copyright 2015 Damyon Wiese
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class template extends persistent {
39 const TABLE = 'competency_template';
41 /** @var template object before update. */
42 protected $beforeupdate = null;
44 /**
45 * Return the definition of the properties of this model.
47 * @return array
49 protected static function define_properties() {
50 return array(
51 'shortname' => array(
52 'type' => PARAM_TEXT,
54 'description' => array(
55 'default' => '',
56 'type' => PARAM_CLEANHTML,
58 'descriptionformat' => array(
59 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
60 'type' => PARAM_INT,
61 'default' => FORMAT_HTML
63 'duedate' => array(
64 'default' => 0,
65 'type' => PARAM_INT,
67 'visible' => array(
68 'default' => 1,
69 'type' => PARAM_BOOL,
71 'contextid' => array(
72 'type' => PARAM_INT
77 /**
78 * Hook to execute after an update.
80 * @param bool $result Whether or not the update was successful.
81 * @return void
83 protected function after_update($result) {
84 $this->beforeupdate = null;
87 /**
88 * Hook to execute before validate.
90 * @return void
92 protected function before_validate() {
93 $this->beforeupdate = null;
95 // During update.
96 if ($this->get('id')) {
97 $this->beforeupdate = new self($this->get('id'));
102 * Whether or not the current user can read the template.
104 * @return bool
106 public function can_manage() {
107 return self::can_manage_context($this->get_context());
111 * Whether or not the current user can manage the template.
113 * @param context $context
114 * @return bool
116 public static function can_manage_context($context) {
117 return has_capability('moodle/competency:templatemanage', $context);
121 * Whether or not the current user can read the template.
123 * @return bool
125 public function can_read() {
126 return self::can_read_context($this->get_context());
130 * Whether or not the current user can read the template.
132 * @param context $context
133 * @return bool
135 public static function can_read_context($context) {
136 return has_capability('moodle/competency:templateview', $context) || self::can_manage_context($context);
140 * Get the context.
142 * @return context The context
144 public function get_context() {
145 return context::instance_by_id($this->get('contextid'));
149 * Validate the context ID.
151 * @param int $value The context ID.
152 * @return bool|lang_string
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');
161 return true;
165 * Validate the due date.
167 * The due date can always be changed, but when it is it must be:
168 * - unset
169 * - set in the future.
171 * @param int $value The due date.
172 * @return bool|lang_string
174 protected function validate_duedate($value) {
176 // During update.
177 if ($this->get('id')) {
178 $before = $this->beforeupdate->get('duedate');
180 // The value has not changed, then it's always OK.
181 if ($before == $value) {
182 return true;
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');
193 return true;
197 * Returns true when the template has user learning plans.
199 * @return boolean
201 public function has_plans() {
202 return plan::has_records_for_template($this->get('id'));