Merge branch 'MDL-69688' of https://github.com/stronk7/moodle
[moodle.git] / competency / classes / course_module_competency.php
blobebadd02b9219eab8a7e2a4ce35546237aa7759a5
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 competencies 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 stdClass;
28 use lang_string;
30 /**
31 * Class for loading/storing course_module_competencies from the DB.
33 * @copyright 2015 Damyon Wiese
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class course_module_competency extends persistent {
38 const TABLE = 'competency_modulecomp';
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;
49 /**
50 * Return the definition of the properties of this model.
52 * @return array
54 protected static function define_properties() {
55 return array(
56 'cmid' => array(
57 'type' => PARAM_INT
59 'competencyid' => array(
60 'type' => PARAM_INT
62 'sortorder' => array(
63 'type' => PARAM_INT
65 'ruleoutcome' => array(
66 'choices' => array(self::OUTCOME_NONE,
67 self::OUTCOME_EVIDENCE,
68 self::OUTCOME_RECOMMEND,
69 self::OUTCOME_COMPLETE
71 'default' => self::OUTCOME_EVIDENCE,
72 'type' => PARAM_INT,
74 'overridegrade' => array(
75 'default' => false,
76 'type' => PARAM_BOOL
81 /**
82 * Hook to execute before validate.
84 * @return void
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'))));
92 /**
93 * Return a list of rules.
95 * @return array Indexed by outcome value.
97 public static function get_ruleoutcome_list() {
98 static $list = null;
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));
108 return $list;
112 * Human readable rule name.
114 * @param int $ruleoutcome The value of ruleoutcome.
115 * @return lang_string
117 public static function get_ruleoutcome_name($ruleoutcome) {
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;
137 return new lang_string('coursemodulecompetencyoutcome_' . $strname, 'core_competency');
141 * Validate cmid ID.
143 * @param int $data The CM ID.
144 * @return true|lang_string
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');
151 return true;
155 * Validate competency ID.
157 * @param int $data The competency ID.
158 * @return true|lang_string
160 protected function validate_competencyid($data) {
161 if (!competency::record_exists($data)) {
162 return new lang_string('invaliddata', 'error');
164 return true;
168 * Return the module IDs and visible flags that include this competency in a single course.
170 * @param int $competencyid The competency id
171 * @param int $courseid The course ID.
172 * @return array of ints (cmids)
174 public static function list_course_modules($competencyid, $courseid) {
175 global $DB;
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));
184 return array_keys($results);
188 * Count the competencies in this course module.
190 * @param int $cmid The course module id.
191 * @return int
193 public static function count_competencies($cmid) {
194 global $DB;
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);
203 $results = $DB->count_records_sql($sql, $params);
205 return $results;
209 * List the competencies in this course module.
211 * @param int $cmid The course module id
212 * @return competency[] Indexed by competency ID.
214 public static function list_competencies($cmid) {
215 global $DB;
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);
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;
231 $results->close();
233 return $instances;
237 * Get a single competency from the course module (only if it is really in the course module).
239 * @param int $cmid The course module id
240 * @param int $competencyid The competency id
241 * @return competency
243 public static function get_competency($cmid, $competencyid) {
244 global $DB;
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);
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);
258 return new competency(0, $result);
262 * Hook to execute after delete.
264 * @param bool $result Whether or not the delete was successful.
265 * @return void
267 protected function after_delete($result) {
268 global $DB;
269 if (!$result) {
270 return;
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')));
279 * List the course_module_competencies in this course module.
281 * @param int $cmid The course module id
282 * @return course_module_competency[]
284 public static function list_course_module_competencies($cmid) {
285 global $DB;
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);
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));
300 $results->close();
302 return $instances;
306 * List the relationship objects for a competency in a course.
308 * @param int $competencyid The competency ID.
309 * @param int $courseid The course ID.
310 * @return course_module_competency[]
312 public static function get_records_by_competencyid_in_course($competencyid, $courseid) {
313 global $DB;
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);
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);
329 $results->close();
331 return $instances;