2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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;
45 * Return the definition of the properties of this model.
49 protected static function define_properties() {
54 'description' => array(
56 'type' => PARAM_CLEANHTML
,
58 'descriptionformat' => array(
59 'choices' => array(FORMAT_HTML
, FORMAT_MOODLE
, FORMAT_PLAIN
, FORMAT_MARKDOWN
),
61 'default' => FORMAT_HTML
78 * Hook to execute after an update.
80 * @param bool $result Whether or not the update was successful.
83 protected function after_update($result) {
84 $this->beforeupdate
= null;
88 * Hook to execute before validate.
92 protected function before_validate() {
93 $this->beforeupdate
= null;
96 if ($this->get('id')) {
97 $this->beforeupdate
= new self($this->get('id'));
102 * Whether or not the current user can read the template.
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
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.
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
135 public static function can_read_context($context) {
136 return has_capability('moodle/competency:templateview', $context) || self
::can_manage_context($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
);
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');
165 * Validate the due date.
167 * The due date can always be changed, but when it is it must be:
169 * - set in the future.
171 * @param int $value The due date.
172 * @return bool|lang_string
174 protected function validate_duedate($value) {
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) {
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');
197 * Returns true when the template has user learning plans.
201 public function has_plans() {
202 return plan
::has_records_for_template($this->get('id'));