Differences Between: [Versions 311 and 402] [Versions 311 and 403]
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 competency frameworks 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 coding_exception; 28 use context; 29 use lang_string; 30 use stdClass; 31 32 require_once($CFG->libdir . '/grade/grade_scale.php'); 33 34 /** 35 * Class for loading/storing competency frameworks from the DB. 36 * 37 * @copyright 2015 Damyon Wiese 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class competency_framework extends persistent { 41 42 const TABLE = 'competency_framework'; 43 44 /** Taxonomy constant. */ 45 const TAXONOMY_BEHAVIOUR = 'behaviour'; 46 /** Taxonomy constant. */ 47 const TAXONOMY_COMPETENCY = 'competency'; 48 /** Taxonomy constant. */ 49 const TAXONOMY_CONCEPT = 'concept'; 50 /** Taxonomy constant. */ 51 const TAXONOMY_DOMAIN = 'domain'; 52 /** Taxonomy constant. */ 53 const TAXONOMY_INDICATOR = 'indicator'; 54 /** Taxonomy constant. */ 55 const TAXONOMY_LEVEL = 'level'; 56 /** Taxonomy constant. */ 57 const TAXONOMY_OUTCOME = 'outcome'; 58 /** Taxonomy constant. */ 59 const TAXONOMY_PRACTICE = 'practice'; 60 /** Taxonomy constant. */ 61 const TAXONOMY_PROFICIENCY = 'proficiency'; 62 /** Taxonomy constant. */ 63 const TAXONOMY_SKILL = 'skill'; 64 /** Taxonomy constant. */ 65 const TAXONOMY_VALUE = 'value'; 66 67 /** @var static The object before it was updated. */ 68 protected $beforeupdate; 69 70 /** 71 * Get the context. 72 * 73 * @return context The context 74 */ 75 public function get_context() { 76 return context::instance_by_id($this->get('contextid')); 77 } 78 79 /** 80 * Return the definition of the properties of this model. 81 * 82 * @return array 83 */ 84 protected static function define_properties() { 85 return array( 86 'shortname' => array( 87 'type' => PARAM_TEXT 88 ), 89 'idnumber' => array( 90 'type' => PARAM_RAW 91 ), 92 'description' => array( 93 'type' => PARAM_CLEANHTML, 94 'default' => '' 95 ), 96 'descriptionformat' => array( 97 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN), 98 'type' => PARAM_INT, 99 'default' => FORMAT_HTML 100 ), 101 'visible' => array( 102 'type' => PARAM_BOOL, 103 'default' => 1 104 ), 105 'scaleid' => array( 106 'type' => PARAM_INT 107 ), 108 'scaleconfiguration' => array( 109 'type' => PARAM_RAW 110 ), 111 'contextid' => array( 112 'type' => PARAM_INT 113 ), 114 'taxonomies' => array( 115 'type' => PARAM_RAW, 116 'default' => '' 117 ) 118 ); 119 } 120 121 /** 122 * Hook to execute before validate. 123 * 124 * @return void 125 */ 126 protected function before_validate() { 127 $this->beforeupdate = null; 128 129 // During update. 130 if ($this->get('id')) { 131 $this->beforeupdate = new competency_framework($this->get('id')); 132 } 133 134 } 135 136 /** 137 * Return the current depth of a competency framework. 138 * 139 * @see competency::get_framework_depth() 140 * @return int 141 */ 142 public function get_depth() { 143 return competency::get_framework_depth($this->get('id')); 144 } 145 146 /** 147 * Return the scale. 148 * 149 * @return \grade_scale 150 */ 151 public function get_scale() { 152 $scale = \grade_scale::fetch(array('id' => $this->get('scaleid'))); 153 $scale->load_items(); 154 return $scale; 155 } 156 157 /** 158 * Get the constant name for a level. 159 * 160 * @param int $level The level of the term. 161 * @return string 162 */ 163 public function get_taxonomy($level) { 164 $taxonomies = $this->get_taxonomies(); 165 166 if (empty($taxonomies[$level])) { 167 // If for some reason we cannot find the level, we fallback onto competency. 168 $constant = self::TAXONOMY_COMPETENCY; 169 } else { 170 $constant = $taxonomies[$level]; 171 } 172 173 return $constant; 174 } 175 176 /** 177 * Return the taxonomy constants indexed by level. 178 * 179 * @return array Contains the list of taxonomy constants indexed by level. 180 */ 181 protected function get_taxonomies() { 182 $taxonomies = explode(',', $this->raw_get('taxonomies')); 183 184 // Indexing first level at 1. 185 array_unshift($taxonomies, null); 186 unset($taxonomies[0]); 187 188 // Ensure that we do not return empty levels. 189 foreach ($taxonomies as $i => $taxonomy) { 190 if (empty($taxonomy)) { 191 $taxonomies[$i] = self::TAXONOMY_COMPETENCY; 192 } 193 } 194 195 return $taxonomies; 196 } 197 198 /** 199 * Returns true when some competencies of the framework have user competencies. 200 * 201 * This is useful to determine if the framework, or part of it, should be locked down. 202 * 203 * @return boolean 204 */ 205 public function has_user_competencies() { 206 return user_competency::has_records_for_framework($this->get('id')) || 207 user_competency_plan::has_records_for_framework($this->get('id')); 208 } 209 210 /** 211 * Convenience method to set taxonomies from an array or string. 212 * 213 * @param string|array $taxonomies A string, or an array where the values are the term constants. 214 */ 215 protected function set_taxonomies($taxonomies) { 216 if (is_array($taxonomies)) { 217 $taxonomies = implode(',', $taxonomies); 218 } 219 $this->raw_set('taxonomies', $taxonomies); 220 } 221 222 /** 223 * Validate the context ID. 224 * 225 * @param int $value The context ID. 226 * @return bool|lang_string 227 */ 228 protected function validate_contextid($value) { 229 global $DB; 230 231 $context = context::instance_by_id($value, IGNORE_MISSING); 232 if (!$context) { 233 return new lang_string('invalidcontext', 'error'); 234 } else if ($context->contextlevel != CONTEXT_SYSTEM && $context->contextlevel != CONTEXT_COURSECAT) { 235 return new lang_string('invalidcontext', 'error'); 236 } 237 238 // During update. 239 if ($this->get('id')) { 240 241 // The context must never change. 242 $oldcontextid = $DB->get_field(self::TABLE, 'contextid', array('id' => $this->get('id')), MUST_EXIST); 243 if ($this->get('contextid') != $oldcontextid) { 244 return new lang_string('invalidcontext', 'error'); 245 } 246 } 247 248 return true; 249 } 250 251 /** 252 * Validate the id number. 253 * 254 * @param string $value The id number. 255 * @return bool|lang_string 256 */ 257 protected function validate_idnumber($value) { 258 global $DB; 259 260 $params = array( 261 'id' => $this->get('id'), 262 'idnumber' => $value, 263 ); 264 265 if ($DB->record_exists_select(self::TABLE, 'idnumber = :idnumber AND id <> :id', $params)) { 266 return new lang_string('idnumbertaken', 'error'); 267 } 268 269 return true; 270 } 271 272 /** 273 * Validate the scale ID. 274 * 275 * @param string $value The scale ID. 276 * @return bool|lang_string 277 */ 278 protected function validate_scaleid($value) { 279 global $DB; 280 281 // Always validate that the scale exists. 282 if (!$DB->record_exists_select('scale', 'id = :id', array('id' => $value))) { 283 return new lang_string('invalidscaleid', 'error'); 284 } 285 286 // During update. 287 if ($this->get('id')) { 288 289 // Validate that we can only change the scale when it is not used yet. 290 if ($this->beforeupdate->get('scaleid') != $value) { 291 if ($this->beforeupdate->has_user_competencies()) { 292 return new lang_string('errorscalealreadyused', 'core_competency'); 293 } 294 } 295 296 } 297 298 return true; 299 } 300 301 /** 302 * Validate the scale configuration. 303 * 304 * @param string $value The scale configuration. 305 * @return bool|lang_string 306 */ 307 protected function validate_scaleconfiguration($value) { 308 $scaledefaultselected = false; 309 $proficientselected = false; 310 $scaleconfigurations = json_decode($value); 311 312 if (is_array($scaleconfigurations)) { 313 314 // The first element of the array contains the scale ID. 315 $scaleinfo = array_shift($scaleconfigurations); 316 if (empty($scaleinfo) || !isset($scaleinfo->scaleid) || $scaleinfo->scaleid != $this->get('scaleid')) { 317 // This should never happen. 318 return new lang_string('errorscaleconfiguration', 'core_competency'); 319 } 320 321 // Walk through the array to find proficient and default values. 322 foreach ($scaleconfigurations as $scaleconfiguration) { 323 if (isset($scaleconfiguration->scaledefault) && $scaleconfiguration->scaledefault) { 324 $scaledefaultselected = true; 325 } 326 if (isset($scaleconfiguration->proficient) && $scaleconfiguration->proficient) { 327 $proficientselected = true; 328 } 329 } 330 } 331 332 if (!$scaledefaultselected || !$proficientselected) { 333 return new lang_string('errorscaleconfiguration', 'core_competency'); 334 } 335 336 return true; 337 } 338 339 /** 340 * Validate taxonomies. 341 * 342 * @param mixed $value The taxonomies. 343 * @return true|lang_string 344 */ 345 protected function validate_taxonomies($value) { 346 $terms = explode(',', $value); 347 348 foreach ($terms as $term) { 349 if (!empty($term) && !array_key_exists($term, self::get_taxonomies_list())) { 350 return new lang_string('invalidtaxonomy', 'core_competency', $term); 351 } 352 } 353 354 return true; 355 } 356 357 /** 358 * Extract the default grade from a scale configuration. 359 * 360 * Returns an array where the first element is the grade, and the second 361 * is a boolean representing whether or not this grade is considered 'proficient'. 362 * 363 * @param string $config JSON encoded config. 364 * @return array(int grade, int proficient) 365 */ 366 public static function get_default_grade_from_scale_configuration($config) { 367 $config = json_decode($config); 368 if (!is_array($config)) { 369 throw new coding_exception('Unexpected scale configuration.'); 370 } 371 372 // Remove the scale ID from the config. 373 array_shift($config); 374 375 foreach ($config as $part) { 376 if ($part->scaledefault) { 377 return array((int) $part->id, (int) $part->proficient); 378 } 379 } 380 381 throw new coding_exception('Invalid scale configuration, default not found.'); 382 } 383 384 /** 385 * Extract the proficiency of a grade from a scale configuration. 386 * 387 * @param string $config JSON encoded config. 388 * @param int $grade The grade. 389 * @return int Representing a boolean 390 */ 391 public static function get_proficiency_of_grade_from_scale_configuration($config, $grade) { 392 $config = json_decode($config); 393 if (!is_array($config)) { 394 throw new coding_exception('Unexpected scale configuration.'); 395 } 396 397 // Remove the scale ID from the config. 398 array_shift($config); 399 400 foreach ($config as $part) { 401 if ($part->id == $grade) { 402 return (int) $part->proficient; 403 } 404 } 405 406 return 0; 407 } 408 409 /** 410 * Get the string of a taxonomy from a constant 411 * 412 * @param string $constant The taxonomy constant. 413 * @return lang_string 414 */ 415 public static function get_taxonomy_from_constant($constant) { 416 return self::get_taxonomies_list()[$constant]; 417 } 418 419 /** 420 * Get the list of all taxonomies. 421 * 422 * @return array Where the key is the taxonomy constant, and the value its translation. 423 */ 424 public static function get_taxonomies_list() { 425 static $list = null; 426 427 // At some point we'll have to switch to not using static cache, mainly for Unit Tests in case we 428 // decide to allow more taxonomies to be added dynamically from a CFG variable for instance. 429 if ($list === null) { 430 $list = array( 431 self::TAXONOMY_BEHAVIOUR => new lang_string('taxonomy_' . self::TAXONOMY_BEHAVIOUR, 'core_competency'), 432 self::TAXONOMY_COMPETENCY => new lang_string('taxonomy_' . self::TAXONOMY_COMPETENCY, 'core_competency'), 433 self::TAXONOMY_CONCEPT => new lang_string('taxonomy_' . self::TAXONOMY_CONCEPT, 'core_competency'), 434 self::TAXONOMY_DOMAIN => new lang_string('taxonomy_' . self::TAXONOMY_DOMAIN, 'core_competency'), 435 self::TAXONOMY_INDICATOR => new lang_string('taxonomy_' . self::TAXONOMY_INDICATOR, 'core_competency'), 436 self::TAXONOMY_LEVEL => new lang_string('taxonomy_' . self::TAXONOMY_LEVEL, 'core_competency'), 437 self::TAXONOMY_OUTCOME => new lang_string('taxonomy_' . self::TAXONOMY_OUTCOME, 'core_competency'), 438 self::TAXONOMY_PRACTICE => new lang_string('taxonomy_' . self::TAXONOMY_PRACTICE, 'core_competency'), 439 self::TAXONOMY_PROFICIENCY => new lang_string('taxonomy_' . self::TAXONOMY_PROFICIENCY, 'core_competency'), 440 self::TAXONOMY_SKILL => new lang_string('taxonomy_' . self::TAXONOMY_SKILL, 'core_competency'), 441 self::TAXONOMY_VALUE => new lang_string('taxonomy_' . self::TAXONOMY_VALUE, 'core_competency'), 442 ); 443 } 444 445 return $list; 446 } 447 448 /** 449 * Get a uniq idnumber. 450 * 451 * @param string $idnumber the framework idnumber 452 * @return string 453 */ 454 public static function get_unused_idnumber($idnumber) { 455 global $DB; 456 457 $currentidnumber = $idnumber; 458 $counter = 0; 459 // Iteratere while the idnumber exists. 460 while ($DB->record_exists_select(static::TABLE, 'idnumber = ?', array($currentidnumber))) { 461 $suffixidnumber = '_' . ++$counter; 462 $currentidnumber = substr($idnumber, 0, 100 - strlen($suffixidnumber)).$suffixidnumber; 463 } 464 465 // Return the uniq idnumber. 466 return $currentidnumber; 467 } 468 469 /** 470 * Whether or not the current user can manage the framework. 471 * 472 * @return bool 473 */ 474 public function can_manage() { 475 return self::can_manage_context($this->get_context()); 476 } 477 478 /** 479 * Whether or not the current user can manage the framework. 480 * 481 * @param context $context 482 * @return bool 483 */ 484 public static function can_manage_context($context) { 485 return has_capability('moodle/competency:competencymanage', $context); 486 } 487 488 /** 489 * Whether or not the current user can read the framework. 490 * 491 * @return bool 492 */ 493 public function can_read() { 494 return self::can_read_context($this->get_context()); 495 } 496 497 /** 498 * Whether or not the current user can read the framework. 499 * 500 * @param context $context 501 * @return bool 502 */ 503 public static function can_read_context($context) { 504 return has_capability('moodle/competency:competencyview', $context) || self::can_manage_context($context); 505 } 506 507 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body