MDL-53655 tool_lp: Allow closures in default values of persistents
[moodle.git] / admin / tool / lp / classes / course_competency_settings.php
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/>.
17 /**
18  * Class for course_competency_settings persistence.
19  *
20  * @package    tool_lp
21  * @copyright  2016 Damyon Wiese
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 namespace tool_lp;
26 use lang_string;
27 use context_course;
29 defined('MOODLE_INTERNAL') || die();
31 /**
32  * Class for course_competency_settings persistence.
33  *
34  * @copyright  2016 Damyon Wiese
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class course_competency_settings extends persistent {
39     /** Table name for plan_competency persistency */
40     const TABLE = 'tool_lp_coursecompsettings';
42     /**
43      * Return the definition of the properties of this model.
44      *
45      * @return array
46      */
47     protected static function define_properties() {
48         return array(
49             'courseid' => array(
50                 'type' => PARAM_INT,
51             ),
52             'pushratingstouserplans' => array(
53                 'type' => PARAM_BOOL,
54                 'default' => function() {
55                     return get_config('tool_lp', 'pushcourseratingstouserplans');
56                 }
57             ),
58         );
59     }
61     /**
62      * Get a the course settings for a single course.
63      *
64      * @param int $courseid The course id
65      * @return course_competency_settings
66      */
67     public static function get_by_courseid($courseid) {
68         global $DB;
70         $params = array(
71             'courseid' => $courseid
72         );
74         $settings = new static(null, (object) $params);
75         if ($record = $DB->get_record(self::TABLE, $params)) {
76             $settings->from_record($record);
77         }
79         return $settings;
80     }
82     /**
83      * Can the current user view competency settings for this course.
84      *
85      * @param int $data The course ID.
86      * @return bool
87      */
88     public static function can_read($courseid) {
89         $context = context_course::instance($courseid);
91         $capabilities = array('tool/lp:coursecompetencyview');
93         return has_any_capability($capabilities, $context);
94     }
96     /**
97      * Can the current user change competency settings for this course.
98      *
99      * @param int $data The course ID.
100      * @return bool
101      */
102     public static function can_manage_course($courseid) {
103         $context = context_course::instance($courseid);
105         $capabilities = array('tool/lp:coursecompetencyconfigure');
107         return has_any_capability($capabilities, $context);
108     }
110     /**
111      * Can the current user change competency settings for this course.
112      *
113      * @return bool
114      */
115     public function can_manage() {
116         return static::can_manage_course($this->get_courseid());
117     }
119     /**
120      * Validate course ID.
121      *
122      * @param int $data The course ID.
123      * @return true|lang_string
124      */
125     protected function validate_courseid($data) {
126         global $DB;
127         if (!$DB->record_exists('course', array('id' => $data))) {
128             return new lang_string('invalidcourseid', 'error');
129         }
130         return true;
131     }
133     /**
134      * Get the context.
135      *
136      * @return context The context
137      */
138     public function get_context() {
139         return context_course::instance($this->get_courseid());
140     }