| 1 | efrain | 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 competencies from the DB.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core_competency
 | 
        
           |  |  | 21 |  * @copyright  2015 Damyon Wiese
 | 
        
           |  |  | 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 stdClass;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Class for loading/storing template_competencies from the DB.
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @copyright  2015 Damyon Wiese
 | 
        
           |  |  | 33 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | class template_competency extends persistent {
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     const TABLE = 'competency_templatecomp';
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Return the definition of the properties of this model.
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @return array
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     protected static function define_properties() {
 | 
        
           |  |  | 45 |         return array(
 | 
        
           |  |  | 46 |             'templateid' => array(
 | 
        
           |  |  | 47 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 48 |                 'default' => 0,
 | 
        
           |  |  | 49 |             ),
 | 
        
           |  |  | 50 |             'competencyid' => array(
 | 
        
           |  |  | 51 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 52 |                 'default' => 0,
 | 
        
           |  |  | 53 |             ),
 | 
        
           |  |  | 54 |             'sortorder' => array(
 | 
        
           |  |  | 55 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 56 |                 'default' => 0,
 | 
        
           |  |  | 57 |             ),
 | 
        
           |  |  | 58 |         );
 | 
        
           |  |  | 59 |     }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     /**
 | 
        
           |  |  | 62 |      * Count the templates using a competency.
 | 
        
           |  |  | 63 |      *
 | 
        
           |  |  | 64 |      * @param int $competencyid The competency id
 | 
        
           |  |  | 65 |      * @param bool $onlyvisible If true, only count visible templates using this competency.
 | 
        
           |  |  | 66 |      * @return int
 | 
        
           |  |  | 67 |      */
 | 
        
           |  |  | 68 |     public static function count_templates($competencyid, $onlyvisible) {
 | 
        
           |  |  | 69 |         global $DB;
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         $sql = 'SELECT COUNT(tpl.id)
 | 
        
           |  |  | 72 |                   FROM {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 73 |                   JOIN {' . template::TABLE . '} tpl
 | 
        
           |  |  | 74 |                     ON tplcomp.templateid = tpl.id
 | 
        
           |  |  | 75 |                  WHERE tplcomp.competencyid = ? ';
 | 
        
           |  |  | 76 |         $params = array($competencyid);
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         if ($onlyvisible) {
 | 
        
           |  |  | 79 |             $sql .= ' AND tpl.visible = ?';
 | 
        
           |  |  | 80 |             $params[] = 1;
 | 
        
           |  |  | 81 |         }
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         $results = $DB->count_records_sql($sql, $params);
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |         return $results;
 | 
        
           |  |  | 86 |     }
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |     /**
 | 
        
           |  |  | 89 |      * List the templates using a competency.
 | 
        
           |  |  | 90 |      *
 | 
        
           |  |  | 91 |      * @param int $competencyid The competency id
 | 
        
           |  |  | 92 |      * @param bool $onlyvisible If true, only count visible templates using this competency.
 | 
        
           |  |  | 93 |      * @return array[competency]
 | 
        
           |  |  | 94 |      */
 | 
        
           |  |  | 95 |     public static function list_templates($competencyid, $onlyvisible) {
 | 
        
           |  |  | 96 |         global $DB;
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         $sql = 'SELECT tpl.*
 | 
        
           |  |  | 99 |                   FROM {' . template::TABLE . '} tpl
 | 
        
           |  |  | 100 |                   JOIN {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 101 |                     ON tplcomp.templateid = tpl.id
 | 
        
           |  |  | 102 |                  WHERE tplcomp.competencyid = ? ';
 | 
        
           |  |  | 103 |         $params = array($competencyid);
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |         if ($onlyvisible) {
 | 
        
           |  |  | 106 |             $sql .= ' AND tpl.visible = ?';
 | 
        
           |  |  | 107 |             $params[] = 1;
 | 
        
           |  |  | 108 |         }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         $sql .= ' ORDER BY tpl.id ASC';
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |         $results = $DB->get_records_sql($sql, $params);
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |         $instances = array();
 | 
        
           |  |  | 115 |         foreach ($results as $result) {
 | 
        
           |  |  | 116 |             array_push($instances, new template(0, $result));
 | 
        
           |  |  | 117 |         }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |         return $instances;
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     /**
 | 
        
           |  |  | 123 |      * Count the competencies in a template.
 | 
        
           |  |  | 124 |      *
 | 
        
           |  |  | 125 |      * @param int $templateid The template id
 | 
        
           |  |  | 126 |      * @return int
 | 
        
           |  |  | 127 |      */
 | 
        
           |  |  | 128 |     public static function count_competencies($templateid) {
 | 
        
           |  |  | 129 |         global $DB;
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |         $sql = 'SELECT COUNT(comp.id)
 | 
        
           |  |  | 132 |                   FROM {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 133 |                   JOIN {' . competency::TABLE . '} comp
 | 
        
           |  |  | 134 |                     ON tplcomp.competencyid = comp.id
 | 
        
           |  |  | 135 |                  WHERE tplcomp.templateid = ? ';
 | 
        
           |  |  | 136 |         $params = array($templateid);
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |         $results = $DB->count_records_sql($sql, $params);
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |         return $results;
 | 
        
           |  |  | 141 |     }
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |     /**
 | 
        
           |  |  | 144 |      * Count the competencies in a template with no links to courses.
 | 
        
           |  |  | 145 |      *
 | 
        
           |  |  | 146 |      * @param int $templateid The template id
 | 
        
           |  |  | 147 |      * @return int
 | 
        
           |  |  | 148 |      */
 | 
        
           |  |  | 149 |     public static function count_competencies_with_no_courses($templateid) {
 | 
        
           |  |  | 150 |         global $DB;
 | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 |         $sql = 'SELECT COUNT(comp.id)
 | 
        
           |  |  | 153 |                   FROM {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 154 |                   JOIN {' . competency::TABLE . '} comp
 | 
        
           |  |  | 155 |                     ON tplcomp.competencyid = comp.id
 | 
        
           |  |  | 156 |                   LEFT JOIN {' . course_competency::TABLE . '} crscomp
 | 
        
           |  |  | 157 |                     ON crscomp.competencyid = comp.id
 | 
        
           |  |  | 158 |                  WHERE tplcomp.templateid = ? AND crscomp.id IS NULL';
 | 
        
           |  |  | 159 |         $params = array($templateid);
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |         $results = $DB->count_records_sql($sql, $params);
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         return $results;
 | 
        
           |  |  | 164 |     }
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 |     /**
 | 
        
           |  |  | 167 |      * Get a single competency from the template (only if it is really in the template).
 | 
        
           |  |  | 168 |      *
 | 
        
           |  |  | 169 |      * @param int $templateid The template id
 | 
        
           |  |  | 170 |      * @param int $competencyid The competency id
 | 
        
           |  |  | 171 |      * @return competency
 | 
        
           |  |  | 172 |      */
 | 
        
           |  |  | 173 |     public static function get_competency($templateid, $competencyid) {
 | 
        
           |  |  | 174 |         global $DB;
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         $sql = 'SELECT comp.*
 | 
        
           |  |  | 177 |                   FROM {' . competency::TABLE . '} comp
 | 
        
           |  |  | 178 |                   JOIN {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 179 |                     ON tplcomp.competencyid = comp.id
 | 
        
           |  |  | 180 |                  WHERE tplcomp.templateid = ? AND tplcomp.competencyid = ?';
 | 
        
           |  |  | 181 |         $params = array($templateid, $competencyid);
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |         $result = $DB->get_record_sql($sql, $params);
 | 
        
           |  |  | 184 |         if (!$result) {
 | 
        
           |  |  | 185 |             throw new \coding_exception('The competency does not belong to this template: ' . $competencyid . ', ' . $templateid);
 | 
        
           |  |  | 186 |         }
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |         return new competency(0, $result);
 | 
        
           |  |  | 189 |     }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |     /**
 | 
        
           |  |  | 192 |      * List the competencies in this template.
 | 
        
           |  |  | 193 |      *
 | 
        
           |  |  | 194 |      * @param int $templateid The template id
 | 
        
           |  |  | 195 |      * @return array[competency]
 | 
        
           |  |  | 196 |      */
 | 
        
           |  |  | 197 |     public static function list_competencies($templateid) {
 | 
        
           |  |  | 198 |         global $DB;
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 |         $sql = 'SELECT comp.*
 | 
        
           |  |  | 201 |                   FROM {' . competency::TABLE . '} comp
 | 
        
           |  |  | 202 |                   JOIN {' . self::TABLE . '} tplcomp
 | 
        
           |  |  | 203 |                     ON tplcomp.competencyid = comp.id
 | 
        
           |  |  | 204 |                  WHERE tplcomp.templateid = ?
 | 
        
           |  |  | 205 |               ORDER BY tplcomp.sortorder ASC,
 | 
        
           |  |  | 206 |                        tplcomp.id ASC';
 | 
        
           |  |  | 207 |         $params = array($templateid);
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |         $results = $DB->get_records_sql($sql, $params);
 | 
        
           |  |  | 210 |   | 
        
           |  |  | 211 |         $instances = array();
 | 
        
           |  |  | 212 |         foreach ($results as $result) {
 | 
        
           |  |  | 213 |             array_push($instances, new competency(0, $result));
 | 
        
           |  |  | 214 |         }
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 |         return $instances;
 | 
        
           |  |  | 217 |     }
 | 
        
           |  |  | 218 |   | 
        
           |  |  | 219 |     /**
 | 
        
           |  |  | 220 |      * Remove the competencies in this template.
 | 
        
           |  |  | 221 |      *
 | 
        
           |  |  | 222 |      * @param int $templateid The template id
 | 
        
           |  |  | 223 |      * @return boolen
 | 
        
           |  |  | 224 |      */
 | 
        
           |  |  | 225 |     public static function delete_by_templateid($templateid) {
 | 
        
           |  |  | 226 |         global $DB;
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 |         return $DB->delete_records(self::TABLE, array('templateid' => $templateid));
 | 
        
           |  |  | 229 |     }
 | 
        
           |  |  | 230 |   | 
        
           |  |  | 231 |     /**
 | 
        
           |  |  | 232 |      * Hook to execute before validate.
 | 
        
           |  |  | 233 |      *
 | 
        
           |  |  | 234 |      * @return void
 | 
        
           |  |  | 235 |      */
 | 
        
           |  |  | 236 |     protected function before_validate() {
 | 
        
           |  |  | 237 |         if (($this->get('id') && $this->get('sortorder') === null) || !$this->get('id')) {
 | 
        
           |  |  | 238 |             $this->set('sortorder', $this->count_records(array('templateid' => $this->get('templateid'))));
 | 
        
           |  |  | 239 |         }
 | 
        
           |  |  | 240 |     }
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |     /**
 | 
        
           |  |  | 243 |      * Validate competencyid.
 | 
        
           |  |  | 244 |      *
 | 
        
           |  |  | 245 |      * @param  int $value ID.
 | 
        
           |  |  | 246 |      * @return true|lang_string
 | 
        
           |  |  | 247 |      */
 | 
        
           |  |  | 248 |     protected function validate_competencyid($value) {
 | 
        
           |  |  | 249 |         if (!competency::record_exists($value)) {
 | 
        
           |  |  | 250 |             return new \lang_string('invaliddata', 'error');
 | 
        
           |  |  | 251 |         }
 | 
        
           |  |  | 252 |         return true;
 | 
        
           |  |  | 253 |     }
 | 
        
           |  |  | 254 |   | 
        
           |  |  | 255 |     /**
 | 
        
           |  |  | 256 |      * Validate templateid.
 | 
        
           |  |  | 257 |      *
 | 
        
           |  |  | 258 |      * @param  int $value ID.
 | 
        
           |  |  | 259 |      * @return true|lang_string
 | 
        
           |  |  | 260 |      */
 | 
        
           |  |  | 261 |     protected function validate_templateid($value) {
 | 
        
           |  |  | 262 |         if (!template::record_exists($value)) {
 | 
        
           |  |  | 263 |             return new \lang_string('invaliddata', 'error');
 | 
        
           |  |  | 264 |         }
 | 
        
           |  |  | 265 |         return true;
 | 
        
           |  |  | 266 |     }
 | 
        
           |  |  | 267 |   | 
        
           |  |  | 268 |     /**
 | 
        
           |  |  | 269 |      * Hook to execute after delete.
 | 
        
           |  |  | 270 |      *
 | 
        
           |  |  | 271 |      * @param bool $result Whether or not the delete was successful.
 | 
        
           |  |  | 272 |      * @return void
 | 
        
           |  |  | 273 |      */
 | 
        
           |  |  | 274 |     protected function after_delete($result) {
 | 
        
           |  |  | 275 |         global $DB;
 | 
        
           |  |  | 276 |         if (!$result) {
 | 
        
           |  |  | 277 |             return;
 | 
        
           |  |  | 278 |         }
 | 
        
           |  |  | 279 |   | 
        
           |  |  | 280 |         $table = '{' . self::TABLE . '}';
 | 
        
           |  |  | 281 |         $sql = "UPDATE $table SET sortorder = sortorder -1  WHERE templateid = ? AND sortorder > ?";
 | 
        
           |  |  | 282 |         $DB->execute($sql, array($this->get('templateid'), $this->get('sortorder')));
 | 
        
           |  |  | 283 |     }
 | 
        
           |  |  | 284 |   | 
        
           |  |  | 285 |     /**
 | 
        
           |  |  | 286 |      * Check if template competency has records for competencies.
 | 
        
           |  |  | 287 |      *
 | 
        
           |  |  | 288 |      * @param array $competencyids Array of competencies ids.
 | 
        
           |  |  | 289 |      * @return boolean Return true if competencies were found in template_competency.
 | 
        
           |  |  | 290 |      */
 | 
        
           |  |  | 291 |     public static function has_records_for_competencies($competencyids) {
 | 
        
           |  |  | 292 |         global $DB;
 | 
        
           |  |  | 293 |         list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
 | 
        
           |  |  | 294 |         return self::record_exists_select("competencyid $insql", $params);
 | 
        
           |  |  | 295 |     }
 | 
        
           |  |  | 296 |   | 
        
           |  |  | 297 | }
 |