2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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;
50 * Return the definition of the properties of this model.
54 protected static function define_properties() {
59 'competencyid' => array(
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
,
74 'overridegrade' => array(
82 * Hook to execute before validate.
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'))));
93 * Return a list of rules.
95 * @return array Indexed by outcome value.
97 public static function get_ruleoutcome_list() {
100 if ($list === null) {
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
));
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
:
123 case self
::OUTCOME_EVIDENCE
:
124 $strname = 'evidence';
126 case self
::OUTCOME_RECOMMEND
:
127 $strname = 'recommend';
129 case self
::OUTCOME_COMPLETE
:
130 $strname = 'complete';
133 throw new \
moodle_exception('errorcompetencyrule', 'core_competency', '', $ruleoutcome);
137 return new lang_string('coursemodulecompetencyoutcome_' . $strname, 'core_competency');
143 * @param int $data The CM ID.
144 * @return true|lang_string
146 protected function validate_cmid($data) {
148 if (!$DB->record_exists('course_modules', array('id' => $data))) {
149 return new lang_string('invalidmodule', 'error');
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');
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) {
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.
193 public static function count_competencies($cmid) {
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);
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) {
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;
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
243 public static function get_competency($cmid, $competencyid) {
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);
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.
267 protected function after_delete($result) {
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) {
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));
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) {
316 FROM {' . self
::TABLE
. '} cmc
317 JOIN {course_modules} cm
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);