NOBUG: Fixed SVG browser compatibility
[moodle.git] / competency / classes / related_competency.php
blob3ef7cedee1e2af35f96b640508573bcf82dcfe6d
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 related competencies from the DB.
20 * @package core_competency
21 * @copyright 2015 David Monllao
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 lang_string;
28 use stdClass;
30 /**
31 * Class for loading/storing related_competencies from the DB.
33 * @package core_competency
34 * @copyright 2015 David Monllao
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class related_competency extends persistent {
39 const TABLE = 'competency_relatedcomp';
41 /**
42 * Return the definition of the properties of this model.
44 * @return array
46 protected static function define_properties() {
47 return array(
48 'competencyid' => array(
49 'type' => PARAM_INT
51 'relatedcompetencyid' => array(
52 'type' => PARAM_INT
57 /**
58 * Validate competency ID.
60 * @param int $data The competency ID.
61 * @return true|lang_string
63 protected function validate_competencyid($data) {
64 if (!competency::record_exists($data)) {
65 return new lang_string('invaliddata', 'error');
67 return true;
70 /**
71 * Validate related competency ID.
73 * @param int $data The related competency ID.
74 * @return true|lang_string
76 protected function validate_relatedcompetencyid($data) {
78 if ($this->get('competencyid') == $data) {
79 // A competency cannot be related to itself.
80 return new lang_string('invaliddata', 'error');
82 } if ($this->get('competencyid') > $data) {
83 // The competency ID must be lower than the related competency ID.
84 return new lang_string('invaliddata', 'error');
86 } else if (!competency::record_exists($data)) {
87 return new lang_string('invaliddata', 'error');
89 } else if (!competency::share_same_framework(array($data, $this->get('competencyid')))) {
90 // The competencies must belong to the same framework.
91 return new lang_string('invaliddata', 'error');
94 return true;
97 /**
98 * Get relation specifying both competencies.
100 * This does not perform any validation on the data passed. If the relation exists in the database
101 * then it is loaded in a the model, if not then it is up to the developer to save the model.
103 * @param int $competencyid
104 * @param int $relatedcompetencyid
105 * @return related_competency
107 public static function get_relation($competencyid, $relatedcompetencyid) {
108 global $DB;
110 // Lower id always as competencyid so we know which one is competencyid and which one relatedcompetencyid.
111 $relation = new static();
112 if ($competencyid > $relatedcompetencyid) {
113 $relation->set('competencyid', $relatedcompetencyid);
114 $relation->set('relatedcompetencyid', $competencyid);
115 } else {
116 $relation->set('competencyid', $competencyid);
117 $relation->set('relatedcompetencyid', $relatedcompetencyid);
120 // We can do it because we have bidirectional relations in the DB.
121 $params = array(
122 'competencyid' => $relation->get('competencyid'),
123 'relatedcompetencyid' => $relation->get('relatedcompetencyid')
125 if ($record = $DB->get_record(self::TABLE, $params)) {
126 $relation->from_record($record);
129 return $relation;
133 * Get the competencies related to a competency.
135 * @param int $competencyid The competency ID.
136 * @return competency[]
138 public static function get_related_competencies($competencyid) {
139 global $DB;
141 $fields = competency::get_sql_fields('c', 'c_');
142 $sql = "(SELECT $fields, " . $DB->sql_concat('rc.relatedcompetencyid', "'_'", 'rc.competencyid') . " AS rid
143 FROM {" . self::TABLE . "} rc
144 JOIN {" . competency::TABLE . "} c
145 ON c.id = rc.relatedcompetencyid
146 WHERE rc.competencyid = :cid)
147 UNION ALL
148 (SELECT $fields, " . $DB->sql_concat('rc.competencyid', "'_'", 'rc.relatedcompetencyid') . " AS rid
149 FROM {" . self::TABLE . "} rc
150 JOIN {" . competency::TABLE . "} c
151 ON c.id = rc.competencyid
152 WHERE rc.relatedcompetencyid = :cid2)
153 ORDER BY c_path ASC, c_sortorder ASC";
155 $competencies = array();
156 $records = $DB->get_recordset_sql($sql, array('cid' => $competencyid, 'cid2' => $competencyid));
157 foreach ($records as $record) {
158 unset($record->rid);
159 $competencies[$record->c_id] = new competency(null, competency::extract_record($record, 'c_'));
161 $records->close();
163 return $competencies;
167 * Get the related competencies from competency ids.
169 * @param int[] $competencyids Array of competency ids.
170 * @return related_competency[]
172 public static function get_multiple_relations($competencyids) {
173 global $DB;
175 if (empty($competencyids)) {
176 return array();
179 list($insql, $params) = $DB->get_in_or_equal($competencyids);
181 $records = $DB->get_records_select(self::TABLE,
182 "competencyid $insql OR relatedcompetencyid $insql",
183 array_merge($params, $params)
186 $relatedcompetencies = array();
187 foreach ($records as $record) {
188 unset($record->id);
189 $relatedcompetencies[] = new related_competency(null, $record);
191 return $relatedcompetencies;
195 * Delete relations using competencies.
197 * @param array $competencyids Array of competencies ids.
198 * @return bool True if relations were deleted successfully.
200 public static function delete_multiple_relations($competencyids) {
201 global $DB;
202 if (empty($competencyids)) {
203 return true;
206 list($insql, $params) = $DB->get_in_or_equal($competencyids);
207 return $DB->delete_records_select(self::TABLE,
208 "competencyid $insql OR relatedcompetencyid $insql",
209 array_merge($params, $params)