MDL-61138 question: external function to get random question set
[moodle.git] / competency / classes / course_module_competency.php
blob405b495f18c6559afe96b7d856d76ff84be903de
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,
77 /**
78 * Hook to execute before validate.
80 * @return void
82 protected function before_validate() {
83 if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
84 $this->set('sortorder', $this->count_records(array('cmid' => $this->get('cmid'))));
88 /**
89 * Return a list of rules.
91 * @return array Indexed by outcome value.
93 public static function get_ruleoutcome_list() {
94 static $list = null;
96 if ($list === null) {
97 $list = array(
98 self::OUTCOME_NONE => self::get_ruleoutcome_name(self::OUTCOME_NONE),
99 self::OUTCOME_EVIDENCE => self::get_ruleoutcome_name(self::OUTCOME_EVIDENCE),
100 self::OUTCOME_RECOMMEND => self::get_ruleoutcome_name(self::OUTCOME_RECOMMEND),
101 self::OUTCOME_COMPLETE => self::get_ruleoutcome_name(self::OUTCOME_COMPLETE));
104 return $list;
108 * Human readable rule name.
110 * @param int $ruleoutcome The value of ruleoutcome.
111 * @return lang_string
113 public static function get_ruleoutcome_name($ruleoutcome) {
115 switch ($ruleoutcome) {
116 case self::OUTCOME_NONE:
117 $strname = 'none';
118 break;
119 case self::OUTCOME_EVIDENCE:
120 $strname = 'evidence';
121 break;
122 case self::OUTCOME_RECOMMEND:
123 $strname = 'recommend';
124 break;
125 case self::OUTCOME_COMPLETE:
126 $strname = 'complete';
127 break;
128 default:
129 throw new \moodle_exception('errorcompetencyrule', 'core_competency', '', $ruleoutcome);
130 break;
133 return new lang_string('coursemodulecompetencyoutcome_' . $strname, 'core_competency');
137 * Validate cmid ID.
139 * @param int $data The CM ID.
140 * @return true|lang_string
142 protected function validate_cmid($data) {
143 global $DB;
144 if (!$DB->record_exists('course_modules', array('id' => $data))) {
145 return new lang_string('invalidmodule', 'error');
147 return true;
151 * Validate competency ID.
153 * @param int $data The competency ID.
154 * @return true|lang_string
156 protected function validate_competencyid($data) {
157 if (!competency::record_exists($data)) {
158 return new lang_string('invaliddata', 'error');
160 return true;
164 * Return the module IDs and visible flags that include this competency in a single course.
166 * @param int $competencyid The competency id
167 * @param int $courseid The course ID.
168 * @return array of ints (cmids)
170 public static function list_course_modules($competencyid, $courseid) {
171 global $DB;
173 $results = $DB->get_records_sql('SELECT coursemodules.id as id
174 FROM {' . self::TABLE . '} modcomp
175 JOIN {course_modules} coursemodules
176 ON modcomp.cmid = coursemodules.id
177 WHERE modcomp.competencyid = ? AND coursemodules.course = ?',
178 array($competencyid, $courseid));
180 return array_keys($results);
184 * Count the competencies in this course module.
186 * @param int $cmid The course module id.
187 * @return int
189 public static function count_competencies($cmid) {
190 global $DB;
192 $sql = 'SELECT COUNT(comp.id)
193 FROM {' . self::TABLE . '} coursemodulecomp
194 JOIN {' . competency::TABLE . '} comp
195 ON coursecomp.competencyid = comp.id
196 WHERE coursecomp.cmid = ? ';
197 $params = array($cmid);
199 $results = $DB->count_records_sql($sql, $params);
201 return $results;
205 * List the competencies in this course module.
207 * @param int $cmid The course module id
208 * @return competency[] Indexed by competency ID.
210 public static function list_competencies($cmid) {
211 global $DB;
213 $sql = 'SELECT comp.*
214 FROM {' . competency::TABLE . '} comp
215 JOIN {' . self::TABLE . '} coursemodulecomp
216 ON coursemodulecomp.competencyid = comp.id
217 WHERE coursemodulecomp.cmid = ?
218 ORDER BY coursemodulecomp.sortorder ASC';
219 $params = array($cmid);
221 $results = $DB->get_recordset_sql($sql, $params);
222 $instances = array();
223 foreach ($results as $result) {
224 $comp = new competency(0, $result);
225 $instances[$comp->get('id')] = $comp;
227 $results->close();
229 return $instances;
233 * Get a single competency from the course module (only if it is really in the course module).
235 * @param int $cmid The course module id
236 * @param int $competencyid The competency id
237 * @return competency
239 public static function get_competency($cmid, $competencyid) {
240 global $DB;
242 $sql = 'SELECT comp.*
243 FROM {' . competency::TABLE . '} comp
244 JOIN {' . self::TABLE . '} crsmodcomp
245 ON crsmodcomp.competencyid = comp.id
246 WHERE crsmodcomp.cmid = ? AND crsmodcomp.competencyid = ?';
247 $params = array($cmid, $competencyid);
249 $result = $DB->get_record_sql($sql, $params);
250 if (!$result) {
251 throw new \coding_exception('The competency does not belong to this course module: ' . $competencyid . ', ' . $cmid);
254 return new competency(0, $result);
258 * Hook to execute after delete.
260 * @param bool $result Whether or not the delete was successful.
261 * @return void
263 protected function after_delete($result) {
264 global $DB;
265 if (!$result) {
266 return;
269 $table = '{' . self::TABLE . '}';
270 $sql = "UPDATE $table SET sortorder = sortorder -1 WHERE cmid = ? AND sortorder > ?";
271 $DB->execute($sql, array($this->get('cmid'), $this->get('sortorder')));
275 * List the course_module_competencies in this course module.
277 * @param int $cmid The course module id
278 * @return course_module_competency[]
280 public static function list_course_module_competencies($cmid) {
281 global $DB;
283 $sql = 'SELECT coursemodcomp.*
284 FROM {' . self::TABLE . '} coursemodcomp
285 JOIN {' . competency::TABLE . '} comp
286 ON coursemodcomp.competencyid = comp.id
287 WHERE coursemodcomp.cmid = ?
288 ORDER BY coursemodcomp.sortorder ASC';
289 $params = array($cmid);
291 $results = $DB->get_recordset_sql($sql, $params);
292 $instances = array();
293 foreach ($results as $result) {
294 array_push($instances, new course_module_competency(0, $result));
296 $results->close();
298 return $instances;
302 * List the relationship objects for a competency in a course.
304 * @param int $competencyid The competency ID.
305 * @param int $courseid The course ID.
306 * @return course_module_competency[]
308 public static function get_records_by_competencyid_in_course($competencyid, $courseid) {
309 global $DB;
311 $sql = 'SELECT cmc.*
312 FROM {' . self::TABLE . '} cmc
313 JOIN {course_modules} cm
314 ON cm.course = ?
315 AND cmc.cmid = cm.id
316 WHERE cmc.competencyid = ?
317 ORDER BY cmc.sortorder ASC';
318 $params = array($courseid, $competencyid);
320 $results = $DB->get_recordset_sql($sql, $params);
321 $instances = array();
322 foreach ($results as $result) {
323 $instances[$result->id] = new course_module_competency(0, $result);
325 $results->close();
327 return $instances;