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();
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';
40 * Return the definition of the properties of this model.
44 protected static function define_properties() {
46 'templateid' => array(
50 'competencyid' => array(
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.
68 public static function count_templates($competencyid, $onlyvisible) {
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);
79 $sql .= ' AND tpl.visible = ?';
83 $results = $DB->count_records_sql($sql, $params);
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) {
99 FROM {' . template
::TABLE
. '} tpl
100 JOIN {' . self
::TABLE
. '} tplcomp
101 ON tplcomp.templateid = tpl.id
102 WHERE tplcomp.competencyid = ? ';
103 $params = array($competencyid);
106 $sql .= ' AND tpl.visible = ?';
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));
123 * Count the competencies in a template.
125 * @param int $templateid The template id
128 public static function count_competencies($templateid) {
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);
144 * Count the competencies in a template with no links to courses.
146 * @param int $templateid The template id
149 public static function count_competencies_with_no_courses($templateid) {
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);
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
173 public static function get_competency($templateid, $competencyid) {
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);
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) {
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,
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));
220 * Remove the competencies in this template.
222 * @param int $templateid The template id
225 public static function delete_by_templateid($templateid) {
228 return $DB->delete_records(self
::TABLE
, array('templateid' => $templateid));
232 * Hook to execute before validate.
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');
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');
269 * Hook to execute after delete.
271 * @param bool $result Whether or not the delete was successful.
274 protected function after_delete($result) {
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) {
293 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED
);
294 return self
::record_exists_select("competencyid $insql", $params);