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. 13 // 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/>. 16 17 /** 18 * Class for loading/storing related competencies from the DB. 19 * 20 * @package core_competency 21 * @copyright 2015 David Monllao 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace core_competency; 25 defined('MOODLE_INTERNAL') || die(); 26 27 use lang_string; 28 use stdClass; 29 30 /** 31 * Class for loading/storing related_competencies from the DB. 32 * 33 * @package core_competency 34 * @copyright 2015 David Monllao 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class related_competency extends persistent { 38 39 const TABLE = 'competency_relatedcomp'; 40 41 /** 42 * Return the definition of the properties of this model. 43 * 44 * @return array 45 */ 46 protected static function define_properties() { 47 return array( 48 'competencyid' => array( 49 'type' => PARAM_INT 50 ), 51 'relatedcompetencyid' => array( 52 'type' => PARAM_INT 53 ), 54 ); 55 } 56 57 /** 58 * Validate competency ID. 59 * 60 * @param int $data The competency ID. 61 * @return true|lang_string 62 */ 63 protected function validate_competencyid($data) { 64 if (!competency::record_exists($data)) { 65 return new lang_string('invaliddata', 'error'); 66 } 67 return true; 68 } 69 70 /** 71 * Validate related competency ID. 72 * 73 * @param int $data The related competency ID. 74 * @return true|lang_string 75 */ 76 protected function validate_relatedcompetencyid($data) { 77 78 if ($this->get('competencyid') == $data) { 79 // A competency cannot be related to itself. 80 return new lang_string('invaliddata', 'error'); 81 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'); 85 86 } else if (!competency::record_exists($data)) { 87 return new lang_string('invaliddata', 'error'); 88 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'); 92 } 93 94 return true; 95 } 96 97 /** 98 * Get relation specifying both competencies. 99 * 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. 102 * 103 * @param int $competencyid 104 * @param int $relatedcompetencyid 105 * @return related_competency 106 */ 107 public static function get_relation($competencyid, $relatedcompetencyid) { 108 global $DB; 109 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); 118 } 119 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') 124 ); 125 if ($record = $DB->get_record(self::TABLE, $params)) { 126 $relation->from_record($record); 127 } 128 129 return $relation; 130 } 131 132 /** 133 * Get the competencies related to a competency. 134 * 135 * @param int $competencyid The competency ID. 136 * @return competency[] 137 */ 138 public static function get_related_competencies($competencyid) { 139 global $DB; 140 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"; 154 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_')); 160 } 161 $records->close(); 162 163 return $competencies; 164 } 165 166 /** 167 * Get the related competencies from competency ids. 168 * 169 * @param int[] $competencyids Array of competency ids. 170 * @return related_competency[] 171 */ 172 public static function get_multiple_relations($competencyids) { 173 global $DB; 174 175 if (empty($competencyids)) { 176 return array(); 177 } 178 179 list($insql, $params) = $DB->get_in_or_equal($competencyids); 180 181 $records = $DB->get_records_select(self::TABLE, 182 "competencyid $insql OR relatedcompetencyid $insql", 183 array_merge($params, $params) 184 ); 185 186 $relatedcompetencies = array(); 187 foreach ($records as $record) { 188 unset($record->id); 189 $relatedcompetencies[] = new related_competency(null, $record); 190 } 191 return $relatedcompetencies; 192 } 193 194 /** 195 * Delete relations using competencies. 196 * 197 * @param array $competencyids Array of competencies ids. 198 * @return bool True if relations were deleted successfully. 199 */ 200 public static function delete_multiple_relations($competencyids) { 201 global $DB; 202 if (empty($competencyids)) { 203 return true; 204 } 205 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) 210 ); 211 } 212 213 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body