MDL-70424 auth: Avoid random changes to $CFG->auth
[moodle.git] / competency / classes / user_evidence_competency.php
blob0c3272bcf343d0274633f73243cbec9e556a1c6f
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 * User evidence competency persistent.
20 * This represent the many to many relationship between evidence of prior
21 * learning and competencies.
23 * @package core_competency
24 * @copyright 2015 Frédéric Massart - FMCorz.net
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 namespace core_competency;
28 defined('MOODLE_INTERNAL') || die();
30 use stdClass;
31 use lang_string;
33 /**
34 * User evidence competency persistent class.
36 * @package core_competency
37 * @copyright 2015 Frédéric Massart - FMCorz.net
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class user_evidence_competency extends persistent {
42 const TABLE = 'competency_userevidencecomp';
44 /**
45 * Return the definition of the properties of this model.
47 * @return array
49 protected static function define_properties() {
50 return array(
51 'userevidenceid' => array(
52 'type' => PARAM_INT
54 'competencyid' => array(
55 'type' => PARAM_INT,
60 /**
61 * Validate competency ID.
63 * @param int $value ID.
64 * @return true|lang_string
66 protected function validate_competencyid($value) {
67 if (!competency::record_exists($value)) {
68 return new lang_string('invaliddata', 'error');
70 return true;
73 /**
74 * Validate user evidence ID.
76 * @param int $value ID.
77 * @return true|lang_string
79 protected function validate_userevidenceid($value) {
80 if (!user_evidence::record_exists($value)) {
81 return new lang_string('invaliddata', 'error');
83 return true;
86 /**
87 * Get competencies by user evidence ID.
89 * @param int $userevidenceid The user evidence ID.
90 * @return competency[]
92 public static function get_competencies_by_userevidenceid($userevidenceid) {
93 global $DB;
94 $sql = "SELECT c.*
95 FROM {" . self::TABLE . "} uec
96 JOIN {" . competency::TABLE . "} c
97 ON uec.userevidenceid = ?
98 AND uec.competencyid = c.id
99 ORDER BY c.shortname";
100 $competencies = array();
101 $records = $DB->get_recordset_sql($sql, array($userevidenceid));
102 foreach ($records as $record) {
103 $competencies[] = new competency(0, $record);
105 $records->close();
106 return $competencies;
110 * Get user competencies by user evidence ID.
112 * @param int $userevidenceid The user evidence ID.
113 * @return user_competency[]
115 public static function get_user_competencies_by_userevidenceid($userevidenceid) {
116 global $DB;
118 $sql = "SELECT uc.*
119 FROM {" . user_competency::TABLE . "} uc
120 JOIN {" . self::TABLE . "} uec
121 ON uc.competencyid = uec.competencyid
122 JOIN {" . user_evidence::TABLE . "} ue
123 ON uec.userevidenceid = ue.id
124 AND uc.userid = ue.userid
125 AND ue.id = ?
126 ORDER BY uc.id ASC";
128 $usercompetencies = array();
129 $records = $DB->get_recordset_sql($sql, array($userevidenceid));
130 foreach ($records as $record) {
131 $usercompetencies[] = new user_competency(0, $record);
133 $records->close();
134 return $usercompetencies;
138 * Get a relation.
140 * This does not perform any validation on the data passed. If the relation exists in the database
141 * then it is loaded in a the model, if not then it is up to the developer to save the model.
143 * @param int $userevidenceid
144 * @param int $competencyid
145 * @return template_cohort
147 public static function get_relation($userevidenceid, $competencyid) {
148 global $DB;
150 $params = array(
151 'userevidenceid' => $userevidenceid,
152 'competencyid' => $competencyid
155 $relation = new static(null, (object) $params);
156 if ($record = $DB->get_record(static::TABLE, $params)) {
157 $relation->from_record($record);
160 return $relation;
164 * Delete evidences using competencies.
166 * @param array $competencyids Array of competencies ids.
167 * @return bool Return true if the delete was successful.
169 public static function delete_by_competencyids($competencyids) {
170 global $DB;
171 if (empty($competencyids)) {
172 return true;
174 list($insql, $params) = $DB->get_in_or_equal($competencyids);
175 return $DB->delete_records_select(self::TABLE, "competencyid $insql", $params);