1 |
efrain |
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 competencies 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 stdClass;
|
|
|
28 |
use lang_string;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Class for loading/storing course_module_competencies from the DB.
|
|
|
32 |
*
|
|
|
33 |
* @copyright 2015 Damyon Wiese
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class course_module_competency extends persistent {
|
|
|
37 |
|
|
|
38 |
const TABLE = 'competency_modulecomp';
|
|
|
39 |
|
|
|
40 |
/** Course competency ruleoutcome constant. */
|
|
|
41 |
const OUTCOME_NONE = 0;
|
|
|
42 |
/** Course competency ruleoutcome constant. */
|
|
|
43 |
const OUTCOME_EVIDENCE = 1;
|
|
|
44 |
/** Course competency ruleoutcome constant. */
|
|
|
45 |
const OUTCOME_RECOMMEND = 2;
|
|
|
46 |
/** Course competency ruleoutcome constant. */
|
|
|
47 |
const OUTCOME_COMPLETE = 3;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Return the definition of the properties of this model.
|
|
|
51 |
*
|
|
|
52 |
* @return array
|
|
|
53 |
*/
|
|
|
54 |
protected static function define_properties() {
|
|
|
55 |
return array(
|
|
|
56 |
'cmid' => array(
|
|
|
57 |
'type' => PARAM_INT
|
|
|
58 |
),
|
|
|
59 |
'competencyid' => array(
|
|
|
60 |
'type' => PARAM_INT
|
|
|
61 |
),
|
|
|
62 |
'sortorder' => array(
|
|
|
63 |
'type' => PARAM_INT
|
|
|
64 |
),
|
|
|
65 |
'ruleoutcome' => array(
|
|
|
66 |
'choices' => array(self::OUTCOME_NONE,
|
|
|
67 |
self::OUTCOME_EVIDENCE,
|
|
|
68 |
self::OUTCOME_RECOMMEND,
|
|
|
69 |
self::OUTCOME_COMPLETE
|
|
|
70 |
),
|
|
|
71 |
'default' => self::OUTCOME_EVIDENCE,
|
|
|
72 |
'type' => PARAM_INT,
|
|
|
73 |
),
|
|
|
74 |
'overridegrade' => array(
|
|
|
75 |
'default' => false,
|
|
|
76 |
'type' => PARAM_BOOL
|
|
|
77 |
),
|
|
|
78 |
);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Hook to execute before validate.
|
|
|
83 |
*
|
|
|
84 |
* @return void
|
|
|
85 |
*/
|
|
|
86 |
protected function before_validate() {
|
|
|
87 |
if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
|
|
|
88 |
$this->set('sortorder', $this->count_records(array('cmid' => $this->get('cmid'))));
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Return a list of rules.
|
|
|
94 |
*
|
|
|
95 |
* @return array Indexed by outcome value.
|
|
|
96 |
*/
|
|
|
97 |
public static function get_ruleoutcome_list() {
|
|
|
98 |
static $list = null;
|
|
|
99 |
|
|
|
100 |
if ($list === null) {
|
|
|
101 |
$list = array(
|
|
|
102 |
self::OUTCOME_NONE => self::get_ruleoutcome_name(self::OUTCOME_NONE),
|
|
|
103 |
self::OUTCOME_EVIDENCE => self::get_ruleoutcome_name(self::OUTCOME_EVIDENCE),
|
|
|
104 |
self::OUTCOME_RECOMMEND => self::get_ruleoutcome_name(self::OUTCOME_RECOMMEND),
|
|
|
105 |
self::OUTCOME_COMPLETE => self::get_ruleoutcome_name(self::OUTCOME_COMPLETE));
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
return $list;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Human readable rule name.
|
|
|
113 |
*
|
|
|
114 |
* @param int $ruleoutcome The value of ruleoutcome.
|
|
|
115 |
* @return lang_string
|
|
|
116 |
*/
|
|
|
117 |
public static function get_ruleoutcome_name($ruleoutcome) {
|
|
|
118 |
|
|
|
119 |
switch ($ruleoutcome) {
|
|
|
120 |
case self::OUTCOME_NONE:
|
|
|
121 |
$strname = 'none';
|
|
|
122 |
break;
|
|
|
123 |
case self::OUTCOME_EVIDENCE:
|
|
|
124 |
$strname = 'evidence';
|
|
|
125 |
break;
|
|
|
126 |
case self::OUTCOME_RECOMMEND:
|
|
|
127 |
$strname = 'recommend';
|
|
|
128 |
break;
|
|
|
129 |
case self::OUTCOME_COMPLETE:
|
|
|
130 |
$strname = 'complete';
|
|
|
131 |
break;
|
|
|
132 |
default:
|
|
|
133 |
throw new \moodle_exception('errorcompetencyrule', 'core_competency', '', $ruleoutcome);
|
|
|
134 |
break;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
return new lang_string('coursemodulecompetencyoutcome_' . $strname, 'core_competency');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Validate cmid ID.
|
|
|
142 |
*
|
|
|
143 |
* @param int $data The CM ID.
|
|
|
144 |
* @return true|lang_string
|
|
|
145 |
*/
|
|
|
146 |
protected function validate_cmid($data) {
|
|
|
147 |
global $DB;
|
|
|
148 |
if (!$DB->record_exists('course_modules', array('id' => $data))) {
|
|
|
149 |
return new lang_string('invalidmodule', 'error');
|
|
|
150 |
}
|
|
|
151 |
return true;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Validate competency ID.
|
|
|
156 |
*
|
|
|
157 |
* @param int $data The competency ID.
|
|
|
158 |
* @return true|lang_string
|
|
|
159 |
*/
|
|
|
160 |
protected function validate_competencyid($data) {
|
|
|
161 |
if (!competency::record_exists($data)) {
|
|
|
162 |
return new lang_string('invaliddata', 'error');
|
|
|
163 |
}
|
|
|
164 |
return true;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Return the module IDs and visible flags that include this competency in a single course.
|
|
|
169 |
*
|
|
|
170 |
* @param int $competencyid The competency id
|
|
|
171 |
* @param int $courseid The course ID.
|
|
|
172 |
* @return array of ints (cmids)
|
|
|
173 |
*/
|
|
|
174 |
public static function list_course_modules($competencyid, $courseid) {
|
|
|
175 |
global $DB;
|
|
|
176 |
|
|
|
177 |
$results = $DB->get_records_sql('SELECT coursemodules.id as id
|
|
|
178 |
FROM {' . self::TABLE . '} modcomp
|
|
|
179 |
JOIN {course_modules} coursemodules
|
|
|
180 |
ON modcomp.cmid = coursemodules.id
|
|
|
181 |
WHERE modcomp.competencyid = ? AND coursemodules.course = ?',
|
|
|
182 |
array($competencyid, $courseid));
|
|
|
183 |
|
|
|
184 |
return array_keys($results);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Count the competencies in this course module.
|
|
|
189 |
*
|
|
|
190 |
* @param int $cmid The course module id.
|
|
|
191 |
* @return int
|
|
|
192 |
*/
|
|
|
193 |
public static function count_competencies($cmid) {
|
|
|
194 |
global $DB;
|
|
|
195 |
|
|
|
196 |
$sql = 'SELECT COUNT(comp.id)
|
|
|
197 |
FROM {' . self::TABLE . '} coursemodulecomp
|
|
|
198 |
JOIN {' . competency::TABLE . '} comp
|
|
|
199 |
ON coursemodulecomp.competencyid = comp.id
|
|
|
200 |
WHERE coursemodulecomp.cmid = ? ';
|
|
|
201 |
$params = array($cmid);
|
|
|
202 |
|
|
|
203 |
$results = $DB->count_records_sql($sql, $params);
|
|
|
204 |
|
|
|
205 |
return $results;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* List the competencies in this course module.
|
|
|
210 |
*
|
|
|
211 |
* @param int $cmid The course module id
|
|
|
212 |
* @return competency[] Indexed by competency ID.
|
|
|
213 |
*/
|
|
|
214 |
public static function list_competencies($cmid) {
|
|
|
215 |
global $DB;
|
|
|
216 |
|
|
|
217 |
$sql = 'SELECT comp.*
|
|
|
218 |
FROM {' . competency::TABLE . '} comp
|
|
|
219 |
JOIN {' . self::TABLE . '} coursemodulecomp
|
|
|
220 |
ON coursemodulecomp.competencyid = comp.id
|
|
|
221 |
WHERE coursemodulecomp.cmid = ?
|
|
|
222 |
ORDER BY coursemodulecomp.sortorder ASC';
|
|
|
223 |
$params = array($cmid);
|
|
|
224 |
|
|
|
225 |
$results = $DB->get_recordset_sql($sql, $params);
|
|
|
226 |
$instances = array();
|
|
|
227 |
foreach ($results as $result) {
|
|
|
228 |
$comp = new competency(0, $result);
|
|
|
229 |
$instances[$comp->get('id')] = $comp;
|
|
|
230 |
}
|
|
|
231 |
$results->close();
|
|
|
232 |
|
|
|
233 |
return $instances;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Get a single competency from the course module (only if it is really in the course module).
|
|
|
238 |
*
|
|
|
239 |
* @param int $cmid The course module id
|
|
|
240 |
* @param int $competencyid The competency id
|
|
|
241 |
* @return competency
|
|
|
242 |
*/
|
|
|
243 |
public static function get_competency($cmid, $competencyid) {
|
|
|
244 |
global $DB;
|
|
|
245 |
|
|
|
246 |
$sql = 'SELECT comp.*
|
|
|
247 |
FROM {' . competency::TABLE . '} comp
|
|
|
248 |
JOIN {' . self::TABLE . '} crsmodcomp
|
|
|
249 |
ON crsmodcomp.competencyid = comp.id
|
|
|
250 |
WHERE crsmodcomp.cmid = ? AND crsmodcomp.competencyid = ?';
|
|
|
251 |
$params = array($cmid, $competencyid);
|
|
|
252 |
|
|
|
253 |
$result = $DB->get_record_sql($sql, $params);
|
|
|
254 |
if (!$result) {
|
|
|
255 |
throw new \coding_exception('The competency does not belong to this course module: ' . $competencyid . ', ' . $cmid);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
return new competency(0, $result);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Hook to execute after delete.
|
|
|
263 |
*
|
|
|
264 |
* @param bool $result Whether or not the delete was successful.
|
|
|
265 |
* @return void
|
|
|
266 |
*/
|
|
|
267 |
protected function after_delete($result) {
|
|
|
268 |
global $DB;
|
|
|
269 |
if (!$result) {
|
|
|
270 |
return;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
$table = '{' . self::TABLE . '}';
|
|
|
274 |
$sql = "UPDATE $table SET sortorder = sortorder -1 WHERE cmid = ? AND sortorder > ?";
|
|
|
275 |
$DB->execute($sql, array($this->get('cmid'), $this->get('sortorder')));
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* List the course_module_competencies in this course module.
|
|
|
280 |
*
|
|
|
281 |
* @param int $cmid The course module id
|
|
|
282 |
* @return course_module_competency[]
|
|
|
283 |
*/
|
|
|
284 |
public static function list_course_module_competencies($cmid) {
|
|
|
285 |
global $DB;
|
|
|
286 |
|
|
|
287 |
$sql = 'SELECT coursemodcomp.*
|
|
|
288 |
FROM {' . self::TABLE . '} coursemodcomp
|
|
|
289 |
JOIN {' . competency::TABLE . '} comp
|
|
|
290 |
ON coursemodcomp.competencyid = comp.id
|
|
|
291 |
WHERE coursemodcomp.cmid = ?
|
|
|
292 |
ORDER BY coursemodcomp.sortorder ASC';
|
|
|
293 |
$params = array($cmid);
|
|
|
294 |
|
|
|
295 |
$results = $DB->get_recordset_sql($sql, $params);
|
|
|
296 |
$instances = array();
|
|
|
297 |
foreach ($results as $result) {
|
|
|
298 |
array_push($instances, new course_module_competency(0, $result));
|
|
|
299 |
}
|
|
|
300 |
$results->close();
|
|
|
301 |
|
|
|
302 |
return $instances;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* List the relationship objects for a competency in a course.
|
|
|
307 |
*
|
|
|
308 |
* @param int $competencyid The competency ID.
|
|
|
309 |
* @param int $courseid The course ID.
|
|
|
310 |
* @return course_module_competency[]
|
|
|
311 |
*/
|
|
|
312 |
public static function get_records_by_competencyid_in_course($competencyid, $courseid) {
|
|
|
313 |
global $DB;
|
|
|
314 |
|
|
|
315 |
$sql = 'SELECT cmc.*
|
|
|
316 |
FROM {' . self::TABLE . '} cmc
|
|
|
317 |
JOIN {course_modules} cm
|
|
|
318 |
ON cm.course = ?
|
|
|
319 |
AND cmc.cmid = cm.id
|
|
|
320 |
WHERE cmc.competencyid = ?
|
|
|
321 |
ORDER BY cmc.sortorder ASC';
|
|
|
322 |
$params = array($courseid, $competencyid);
|
|
|
323 |
|
|
|
324 |
$results = $DB->get_recordset_sql($sql, $params);
|
|
|
325 |
$instances = array();
|
|
|
326 |
foreach ($results as $result) {
|
|
|
327 |
$instances[$result->id] = new course_module_competency(0, $result);
|
|
|
328 |
}
|
|
|
329 |
$results->close();
|
|
|
330 |
|
|
|
331 |
return $instances;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
}
|