MDL-53655 tool_lp: Allow closures in default values of persistents
[moodle.git] / admin / tool / lp / classes / course_competency_settings.php
CommitLineData
7ba07487
DW
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 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 */
24namespace tool_lp;
25
26use lang_string;
27use context_course;
28
29defined('MOODLE_INTERNAL') || die();
30
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 */
37class course_competency_settings extends persistent {
38
39 /** Table name for plan_competency persistency */
40 const TABLE = 'tool_lp_coursecompsettings';
41
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,
3baf704d
FM
54 'default' => function() {
55 return get_config('tool_lp', 'pushcourseratingstouserplans');
56 }
7ba07487
DW
57 ),
58 );
59 }
60
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 */
9919b4af 67 public static function get_by_courseid($courseid) {
7ba07487
DW
68 global $DB;
69
70 $params = array(
71 'courseid' => $courseid
72 );
73
74 $settings = new static(null, (object) $params);
75 if ($record = $DB->get_record(self::TABLE, $params)) {
76 $settings->from_record($record);
7ba07487
DW
77 }
78
79 return $settings;
80 }
81
82 /**
14b60a44 83 * Can the current user view competency settings for this course.
7ba07487
DW
84 *
85 * @param int $data The course ID.
86 * @return bool
87 */
c01141d1 88 public static function can_read($courseid) {
7ba07487
DW
89 $context = context_course::instance($courseid);
90
14b60a44 91 $capabilities = array('tool/lp:coursecompetencyview');
7ba07487
DW
92
93 return has_any_capability($capabilities, $context);
94 }
95
96 /**
97 * Can the current user change competency settings for this course.
98 *
99 * @param int $data The course ID.
100 * @return bool
101 */
c01141d1 102 public static function can_manage_course($courseid) {
7ba07487
DW
103 $context = context_course::instance($courseid);
104
105 $capabilities = array('tool/lp:coursecompetencyconfigure');
106
107 return has_any_capability($capabilities, $context);
108 }
109
45bf83cf
DW
110 /**
111 * Can the current user change competency settings for this course.
112 *
113 * @return bool
114 */
c01141d1
DW
115 public function can_manage() {
116 return static::can_manage_course($this->get_courseid());
45bf83cf
DW
117 }
118
7ba07487
DW
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 }
132
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 }
141}