Merge branch 'MDL-80072-main' of https://github.com/andrewnicols/moodle
[moodle.git] / competency / classes / template_competency.php
blob8c6df0b273078c6052b3fd6b940a19dacea6fe96
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;
29 /**
30 * Class for loading/storing template_competencies from the DB.
32 * @copyright 2015 Damyon Wiese
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class template_competency extends persistent {
37 const TABLE = 'competency_templatecomp';
39 /**
40 * Return the definition of the properties of this model.
42 * @return array
44 protected static function define_properties() {
45 return array(
46 'templateid' => array(
47 'type' => PARAM_INT,
48 'default' => 0,
50 'competencyid' => array(
51 'type' => PARAM_INT,
52 'default' => 0,
54 'sortorder' => array(
55 'type' => PARAM_INT,
56 'default' => 0,
61 /**
62 * Count the templates using a competency.
64 * @param int $competencyid The competency id
65 * @param bool $onlyvisible If true, only count visible templates using this competency.
66 * @return int
68 public static function count_templates($competencyid, $onlyvisible) {
69 global $DB;
71 $sql = 'SELECT COUNT(tpl.id)
72 FROM {' . self::TABLE . '} tplcomp
73 JOIN {' . template::TABLE . '} tpl
74 ON tplcomp.templateid = tpl.id
75 WHERE tplcomp.competencyid = ? ';
76 $params = array($competencyid);
78 if ($onlyvisible) {
79 $sql .= ' AND tpl.visible = ?';
80 $params[] = 1;
83 $results = $DB->count_records_sql($sql, $params);
85 return $results;
88 /**
89 * List the templates using a competency.
91 * @param int $competencyid The competency id
92 * @param bool $onlyvisible If true, only count visible templates using this competency.
93 * @return array[competency]
95 public static function list_templates($competencyid, $onlyvisible) {
96 global $DB;
98 $sql = 'SELECT tpl.*
99 FROM {' . template::TABLE . '} tpl
100 JOIN {' . self::TABLE . '} tplcomp
101 ON tplcomp.templateid = tpl.id
102 WHERE tplcomp.competencyid = ? ';
103 $params = array($competencyid);
105 if ($onlyvisible) {
106 $sql .= ' AND tpl.visible = ?';
107 $params[] = 1;
110 $sql .= ' ORDER BY tpl.id ASC';
112 $results = $DB->get_records_sql($sql, $params);
114 $instances = array();
115 foreach ($results as $result) {
116 array_push($instances, new template(0, $result));
119 return $instances;
123 * Count the competencies in a template.
125 * @param int $templateid The template id
126 * @return int
128 public static function count_competencies($templateid) {
129 global $DB;
131 $sql = 'SELECT COUNT(comp.id)
132 FROM {' . self::TABLE . '} tplcomp
133 JOIN {' . competency::TABLE . '} comp
134 ON tplcomp.competencyid = comp.id
135 WHERE tplcomp.templateid = ? ';
136 $params = array($templateid);
138 $results = $DB->count_records_sql($sql, $params);
140 return $results;
144 * Count the competencies in a template with no links to courses.
146 * @param int $templateid The template id
147 * @return int
149 public static function count_competencies_with_no_courses($templateid) {
150 global $DB;
152 $sql = 'SELECT COUNT(comp.id)
153 FROM {' . self::TABLE . '} tplcomp
154 JOIN {' . competency::TABLE . '} comp
155 ON tplcomp.competencyid = comp.id
156 LEFT JOIN {' . course_competency::TABLE . '} crscomp
157 ON crscomp.competencyid = comp.id
158 WHERE tplcomp.templateid = ? AND crscomp.id IS NULL';
159 $params = array($templateid);
161 $results = $DB->count_records_sql($sql, $params);
163 return $results;
167 * Get a single competency from the template (only if it is really in the template).
169 * @param int $templateid The template id
170 * @param int $competencyid The competency id
171 * @return competency
173 public static function get_competency($templateid, $competencyid) {
174 global $DB;
176 $sql = 'SELECT comp.*
177 FROM {' . competency::TABLE . '} comp
178 JOIN {' . self::TABLE . '} tplcomp
179 ON tplcomp.competencyid = comp.id
180 WHERE tplcomp.templateid = ? AND tplcomp.competencyid = ?';
181 $params = array($templateid, $competencyid);
183 $result = $DB->get_record_sql($sql, $params);
184 if (!$result) {
185 throw new \coding_exception('The competency does not belong to this template: ' . $competencyid . ', ' . $templateid);
188 return new competency(0, $result);
192 * List the competencies in this template.
194 * @param int $templateid The template id
195 * @return array[competency]
197 public static function list_competencies($templateid) {
198 global $DB;
200 $sql = 'SELECT comp.*
201 FROM {' . competency::TABLE . '} comp
202 JOIN {' . self::TABLE . '} tplcomp
203 ON tplcomp.competencyid = comp.id
204 WHERE tplcomp.templateid = ?
205 ORDER BY tplcomp.sortorder ASC,
206 tplcomp.id ASC';
207 $params = array($templateid);
209 $results = $DB->get_records_sql($sql, $params);
211 $instances = array();
212 foreach ($results as $result) {
213 array_push($instances, new competency(0, $result));
216 return $instances;
220 * Remove the competencies in this template.
222 * @param int $templateid The template id
223 * @return boolen
225 public static function delete_by_templateid($templateid) {
226 global $DB;
228 return $DB->delete_records(self::TABLE, array('templateid' => $templateid));
232 * Hook to execute before validate.
234 * @return void
236 protected function before_validate() {
237 if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
238 $this->set('sortorder', $this->count_records(array('templateid' => $this->get('templateid'))));
243 * Validate competencyid.
245 * @param int $value ID.
246 * @return true|lang_string
248 protected function validate_competencyid($value) {
249 if (!competency::record_exists($value)) {
250 return new \lang_string('invaliddata', 'error');
252 return true;
256 * Validate templateid.
258 * @param int $value ID.
259 * @return true|lang_string
261 protected function validate_templateid($value) {
262 if (!template::record_exists($value)) {
263 return new \lang_string('invaliddata', 'error');
265 return true;
269 * Hook to execute after delete.
271 * @param bool $result Whether or not the delete was successful.
272 * @return void
274 protected function after_delete($result) {
275 global $DB;
276 if (!$result) {
277 return;
280 $table = '{' . self::TABLE . '}';
281 $sql = "UPDATE $table SET sortorder = sortorder -1 WHERE templateid = ? AND sortorder > ?";
282 $DB->execute($sql, array($this->get('templateid'), $this->get('sortorder')));
286 * Check if template competency has records for competencies.
288 * @param array $competencyids Array of competencies ids.
289 * @return boolean Return true if competencies were found in template_competency.
291 public static function has_records_for_competencies($competencyids) {
292 global $DB;
293 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
294 return self::record_exists_select("competencyid $insql", $params);