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 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();
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';
41 * Return the definition of the properties of this model.
45 protected static function define_properties() {
50 'competencyid' => array(
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) {
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,
76 $params = array($planid);
78 // TODO MDL-52229 Handle hidden competencies.
79 $results = $DB->get_records_sql($sql, $params);
82 foreach ($results as $result) {
83 array_push($instances, new competency(0, $result));
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
96 public static function get_competency($planid, $competencyid) {
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);
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.
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');
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');
152 * Hook to execute after delete.
154 * @param bool $result Whether or not the delete was successful.
157 protected function after_delete($result) {
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
174 public static function has_records_for_competencies($competencyids) {
176 list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED
);
177 return self
::record_exists_select("competencyid $insql", $params);