Merge branch 'MDL-81472' of https://github.com/paulholden/moodle
[moodle.git] / competency / classes / plan_competency.php
blob31cbc6f44494ab58dd852722b905f8d7afdecfc4
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 plan_competency persistence.
20 * @package core_competency
21 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
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;
29 /**
30 * Class for managing competencies in the plan (add/remove competencies for given plan).
32 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class plan_competency extends persistent {
37 /** Table name for plan_competency persistency */
38 const TABLE = 'competency_plancomp';
40 /**
41 * Return the definition of the properties of this model.
43 * @return array
45 protected static function define_properties() {
46 return array(
47 'planid' => array(
48 'type' => PARAM_INT,
50 'competencyid' => array(
51 'type' => PARAM_INT,
53 'sortorder' => array(
54 'type' => PARAM_INT,
55 'default' => null,
60 /**
61 * List the competencies in this plan.
63 * @param int $planid The plan id
64 * @return array[competency]
66 public static function list_competencies($planid) {
67 global $DB;
69 $sql = 'SELECT comp.*
70 FROM {' . competency::TABLE . '} comp
71 JOIN {' . self::TABLE . '} plancomp
72 ON plancomp.competencyid = comp.id
73 WHERE plancomp.planid = ?
74 ORDER BY plancomp.sortorder ASC,
75 plancomp.id ASC';
76 $params = array($planid);
78 // TODO MDL-52229 Handle hidden competencies.
79 $results = $DB->get_records_sql($sql, $params);
81 $instances = array();
82 foreach ($results as $result) {
83 array_push($instances, new competency(0, $result));
86 return $instances;
89 /**
90 * Get a single competency from the plan (only if it is really in the plan).
92 * @param int $planid The plan id
93 * @param int $competencyid The competency id
94 * @return competency
96 public static function get_competency($planid, $competencyid) {
97 global $DB;
99 $sql = 'SELECT comp.*
100 FROM {' . competency::TABLE . '} comp
101 JOIN {' . self::TABLE . '} plncomp
102 ON plncomp.competencyid = comp.id
103 WHERE plncomp.planid = ? AND plncomp.competencyid = ?';
104 $params = array($planid, $competencyid);
106 $result = $DB->get_record_sql($sql, $params);
107 if (!$result) {
108 throw new \coding_exception('The competency does not belong to this plan: ' . $competencyid . ', ' . $planid);
111 return new competency(0, $result);
115 * Hook to execute before validate.
117 * @return void
119 protected function before_validate() {
120 if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
121 $this->set('sortorder', $this->count_records(array('planid' => $this->get('planid'))));
126 * Validate competencyid.
128 * @param int $value ID.
129 * @return true|lang_string
131 protected function validate_competencyid($value) {
132 if (!competency::record_exists($value)) {
133 return new lang_string('invaliddata', 'error');
135 return true;
139 * Validate planid.
141 * @param int $value ID.
142 * @return true|lang_string
144 protected function validate_planid($value) {
145 if (!plan::record_exists($value)) {
146 return new lang_string('invaliddata', 'error');
148 return true;
152 * Hook to execute after delete.
154 * @param bool $result Whether or not the delete was successful.
155 * @return void
157 protected function after_delete($result) {
158 global $DB;
159 if (!$result) {
160 return;
163 $table = '{' . self::TABLE . '}';
164 $sql = "UPDATE $table SET sortorder = sortorder -1 WHERE planid = ? AND sortorder > ?";
165 $DB->execute($sql, array($this->get('planid'), $this->get('sortorder')));
169 * Check if plan competency has records for competencies.
171 * @param array $competencyids The competences IDs
172 * @return boolean
174 public static function has_records_for_competencies($competencyids) {
175 global $DB;
176 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
177 return self::record_exists_select("competencyid $insql", $params);