[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 coding_exception; 28 use context_system; 29 use lang_string; 30 use stdClass; 31 32 require_once($CFG->libdir . '/grade/grade_scale.php'); 33 34 /** 35 * Class for loading/storing competencies 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 extends persistent { 41 42 const TABLE = 'competency'; 43 44 /** Outcome none. */ 45 const OUTCOME_NONE = 0; 46 /** Outcome evidence. */ 47 const OUTCOME_EVIDENCE = 1; 48 /** Outcome complete. */ 49 const OUTCOME_COMPLETE = 2; 50 /** Outcome recommend. */ 51 const OUTCOME_RECOMMEND = 3; 52 53 /** @var competency Object before update. */ 54 protected $beforeupdate = null; 55 56 /** 57 * Return the definition of the properties of this model. 58 * 59 * @return array 60 */ 61 protected static function define_properties() { 62 return array( 63 'shortname' => array( 64 'type' => PARAM_TEXT 65 ), 66 'idnumber' => array( 67 'type' => PARAM_RAW 68 ), 69 'description' => array( 70 'default' => '', 71 'type' => PARAM_RAW 72 ), 73 'descriptionformat' => array( 74 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN), 75 'type' => PARAM_INT, 76 'default' => FORMAT_HTML 77 ), 78 'sortorder' => array( 79 'default' => null, 80 'type' => PARAM_INT 81 ), 82 'parentid' => array( 83 'default' => 0, 84 'type' => PARAM_INT 85 ), 86 'path' => array( 87 'default' => '/0/', 88 'type' => PARAM_RAW 89 ), 90 'ruleoutcome' => array( 91 'choices' => array(self::OUTCOME_NONE, self::OUTCOME_EVIDENCE, self::OUTCOME_COMPLETE, self::OUTCOME_RECOMMEND), 92 'default' => self::OUTCOME_NONE, 93 'type' => PARAM_INT 94 ), 95 'ruletype' => array( 96 'type' => PARAM_RAW, 97 'default' => null, 98 'null' => NULL_ALLOWED 99 ), 100 'ruleconfig' => array( 101 'default' => null, 102 'type' => PARAM_RAW, 103 'null' => NULL_ALLOWED 104 ), 105 'scaleid' => array( 106 'default' => null, 107 'type' => PARAM_INT, 108 'null' => NULL_ALLOWED 109 ), 110 'scaleconfiguration' => array( 111 'default' => null, 112 'type' => PARAM_RAW, 113 'null' => NULL_ALLOWED 114 ), 115 'competencyframeworkid' => array( 116 'default' => 0, 117 'type' => PARAM_INT 118 ), 119 ); 120 } 121 122 /** 123 * Hook to execute before validate. 124 * 125 * @return void 126 */ 127 protected function before_validate() { 128 $this->beforeupdate = null; 129 $this->newparent = null; 130 131 // During update. 132 if ($this->get_id()) { 133 $this->beforeupdate = new competency($this->get_id()); 134 135 // The parent ID has changed. 136 if ($this->beforeupdate->get_parentid() != $this->get_parentid()) { 137 $this->newparent = $this->get_parent(); 138 139 // Update path and sortorder. 140 $this->set_new_path($this->newparent); 141 $this->set_new_sortorder(); 142 } 143 144 } else { 145 // During create. 146 147 $this->set_new_path(); 148 // Always generate new sortorder when we create new competency. 149 $this->set_new_sortorder(); 150 151 } 152 } 153 154 /** 155 * Hook to execute after an update. 156 * 157 * @param bool $result Whether or not the update was successful. 158 * @return void 159 */ 160 protected function after_update($result) { 161 global $DB; 162 163 if (!$result) { 164 $this->beforeupdate = null; 165 return; 166 } 167 168 // The parent ID has changed, we need to fix all the paths of the children. 169 if ($this->beforeupdate->get_parentid() != $this->get_parentid()) { 170 $beforepath = $this->beforeupdate->get_path() . $this->get_id() . '/'; 171 172 $like = $DB->sql_like('path', '?'); 173 $likesearch = $DB->sql_like_escape($beforepath) . '%'; 174 175 $table = '{' . self::TABLE . '}'; 176 $sql = "UPDATE $table SET path = REPLACE(path, ?, ?) WHERE " . $like; 177 $DB->execute($sql, array( 178 $beforepath, 179 $this->get_path() . $this->get_id() . '/', 180 $likesearch 181 )); 182 183 // Resolving sortorder holes left after changing parent. 184 $table = '{' . self::TABLE . '}'; 185 $sql = "UPDATE $table SET sortorder = sortorder -1 " 186 . " WHERE competencyframeworkid = ? AND parentid = ? AND sortorder > ?"; 187 $DB->execute($sql, array($this->get_competencyframeworkid(), 188 $this->beforeupdate->get_parentid(), 189 $this->beforeupdate->get_sortorder() 190 )); 191 } 192 193 $this->beforeupdate = null; 194 } 195 196 197 /** 198 * Hook to execute after a delete. 199 * 200 * @param bool $result Whether or not the delete was successful. 201 * @return void 202 */ 203 protected function after_delete($result) { 204 global $DB; 205 if (!$result) { 206 return; 207 } 208 209 // Resolving sortorder holes left after delete. 210 $table = '{' . self::TABLE . '}'; 211 $sql = "UPDATE $table SET sortorder = sortorder -1 WHERE competencyframeworkid = ? AND parentid = ? AND sortorder > ?"; 212 $DB->execute($sql, array($this->get_competencyframeworkid(), $this->get_parentid(), $this->get_sortorder())); 213 } 214 215 /** 216 * Extracts the default grade from the scale configuration. 217 * 218 * Returns an array where the first element is the grade, and the second 219 * is a boolean representing whether or not this grade is considered 'proficient'. 220 * 221 * @return array(int grade, bool proficient) 222 */ 223 public function get_default_grade() { 224 $scaleid = $this->get_scaleid(); 225 $scaleconfig = $this->get_scaleconfiguration(); 226 if ($scaleid === null) { 227 $scaleconfig = $this->get_framework()->get_scaleconfiguration(); 228 } 229 return competency_framework::get_default_grade_from_scale_configuration($scaleconfig); 230 } 231 232 /** 233 * Get the competency framework. 234 * 235 * @return competency_framework 236 */ 237 public function get_framework() { 238 return new competency_framework($this->get_competencyframeworkid()); 239 } 240 241 /** 242 * Get the competency level. 243 * 244 * @return int 245 */ 246 public function get_level() { 247 $path = $this->get_path(); 248 $path = trim($path, '/'); 249 return substr_count($path, '/') + 1; 250 } 251 252 /** 253 * Return the parent competency. 254 * 255 * @return null|competency 256 */ 257 public function get_parent() { 258 $parentid = $this->get_parentid(); 259 if (!$parentid) { 260 return null; 261 } 262 return new competency($parentid); 263 } 264 265 /** 266 * Extracts the proficiency of a grade from the scale configuration. 267 * 268 * @param int $grade The grade (scale item ID). 269 * @return array(int grade, bool proficient) 270 */ 271 public function get_proficiency_of_grade($grade) { 272 $scaleid = $this->get_scaleid(); 273 $scaleconfig = $this->get_scaleconfiguration(); 274 if ($scaleid === null) { 275 $scaleconfig = $this->get_framework()->get_scaleconfiguration(); 276 } 277 return competency_framework::get_proficiency_of_grade_from_scale_configuration($scaleconfig, $grade); 278 } 279 280 /** 281 * Return the related competencies. 282 * 283 * @return competency[] 284 */ 285 public function get_related_competencies() { 286 return related_competency::get_related_competencies($this->get_id()); 287 } 288 289 /** 290 * Get the rule object. 291 * 292 * @return null|competency_rule 293 */ 294 public function get_rule_object() { 295 $rule = $this->get_ruletype(); 296 297 if (!$rule || !is_subclass_of($rule, 'core_competency\\competency_rule')) { 298 // Double check that the rule is extending the right class to avoid bad surprises. 299 return null; 300 } 301 302 return new $rule($this); 303 } 304 305 /** 306 * Return the scale. 307 * 308 * @return \grade_scale 309 */ 310 public function get_scale() { 311 $scaleid = $this->get_scaleid(); 312 if ($scaleid === null) { 313 return $this->get_framework()->get_scale(); 314 } 315 $scale = \grade_scale::fetch(array('id' => $scaleid)); 316 $scale->load_items(); 317 return $scale; 318 } 319 320 /** 321 * Returns true when the competency has user competencies. 322 * 323 * This is useful to determine if the competency, or part of it, should be locked down. 324 * 325 * @return boolean 326 */ 327 public function has_user_competencies() { 328 return user_competency::has_records_for_competency($this->get_id()) || 329 user_competency_plan::has_records_for_competency($this->get_id()); 330 } 331 332 /** 333 * Check if the competency is the parent of passed competencies. 334 * 335 * @param array $ids IDs of supposedly direct children. 336 * @return boolean 337 */ 338 public function is_parent_of(array $ids) { 339 global $DB; 340 341 list($insql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); 342 $params['parentid'] = $this->get_id(); 343 344 return $DB->count_records_select(self::TABLE, "id $insql AND parentid = :parentid", $params) == count($ids); 345 } 346 347 /** 348 * Reset the rule. 349 * 350 * @return void 351 */ 352 public function reset_rule() { 353 $this->set_ruleoutcome(static::OUTCOME_NONE); 354 $this->set_ruletype(null); 355 $this->set_ruleconfig(null); 356 } 357 358 /** 359 * Helper method to set the path. 360 * 361 * @param competency $parent The parent competency object. 362 * @return void 363 */ 364 protected function set_new_path(competency $parent = null) { 365 $path = '/0/'; 366 if ($this->get_parentid()) { 367 $parent = $parent !== null ? $parent : $this->get_parent(); 368 $path = $parent->get_path() . $this->get_parentid() . '/'; 369 } 370 $this->set('path', $path); 371 } 372 373 /** 374 * Helper method to set the sortorder. 375 * 376 * @return void 377 */ 378 protected function set_new_sortorder() { 379 $search = array('parentid' => $this->get_parentid(), 'competencyframeworkid' => $this->get_competencyframeworkid()); 380 $this->set('sortorder', $this->count_records($search)); 381 } 382 383 /** 384 * This does a specialised search that finds all nodes in the tree with matching text on any text like field, 385 * and returns this node and all its parents in a displayable sort order. 386 * 387 * @param string $searchtext The text to search for. 388 * @param int $competencyframeworkid The competency framework to limit the search. 389 * @return persistent[] 390 */ 391 public static function search($searchtext, $competencyframeworkid) { 392 global $DB; 393 394 $like1 = $DB->sql_like('shortname', ':like1', false); 395 $like2 = $DB->sql_like('idnumber', ':like2', false); 396 $like3 = $DB->sql_like('description', ':like3', false); 397 398 $params = array( 399 'like1' => '%' . $DB->sql_like_escape($searchtext) . '%', 400 'like2' => '%' . $DB->sql_like_escape($searchtext) . '%', 401 'like3' => '%' . $DB->sql_like_escape($searchtext) . '%', 402 'frameworkid' => $competencyframeworkid 403 ); 404 405 $sql = 'competencyframeworkid = :frameworkid AND ((' . $like1 . ') OR (' . $like2 . ') OR (' . $like3 . '))'; 406 $records = $DB->get_records_select(self::TABLE, $sql, $params, 'path, sortorder ASC', '*'); 407 408 // Now get all the parents. 409 $parents = array(); 410 foreach ($records as $record) { 411 $split = explode('/', trim($record->path, '/')); 412 foreach ($split as $parent) { 413 $parents[intval($parent)] = true; 414 } 415 } 416 $parents = array_keys($parents); 417 418 // Skip ones we already fetched. 419 foreach ($parents as $idx => $parent) { 420 if ($parent == 0 || isset($records[$parent])) { 421 unset($parents[$idx]); 422 } 423 } 424 425 if (count($parents)) { 426 list($parentsql, $parentparams) = $DB->get_in_or_equal($parents, SQL_PARAMS_NAMED); 427 428 $parentrecords = $DB->get_records_select(self::TABLE, 'id ' . $parentsql, 429 $parentparams, 'path, sortorder ASC', '*'); 430 431 foreach ($parentrecords as $id => $record) { 432 $records[$id] = $record; 433 } 434 } 435 436 $instances = array(); 437 // Convert to instances of this class. 438 foreach ($records as $record) { 439 $newrecord = new static(0, $record); 440 $instances[$newrecord->get_id()] = $newrecord; 441 } 442 return $instances; 443 } 444 445 /** 446 * Validate the competency framework ID. 447 * 448 * @param int $value The framework ID. 449 * @return true|lang_string 450 */ 451 protected function validate_competencyframeworkid($value) { 452 453 // During update. 454 if ($this->get_id()) { 455 456 // Ensure that we are not trying to move the competency across frameworks. 457 if ($this->beforeupdate->get_competencyframeworkid() != $value) { 458 return new lang_string('invaliddata', 'error'); 459 } 460 461 } else { 462 // During create. 463 464 // Check that the framework exists. 465 if (!competency_framework::record_exists($value)) { 466 return new lang_string('invaliddata', 'error'); 467 } 468 } 469 470 return true; 471 } 472 473 /** 474 * Validate the ID number. 475 * 476 * @param string $value The ID number. 477 * @return true|lang_string 478 */ 479 protected function validate_idnumber($value) { 480 global $DB; 481 $sql = 'idnumber = :idnumber AND competencyframeworkid = :competencyframeworkid AND id <> :id'; 482 $params = array( 483 'id' => $this->get_id(), 484 'idnumber' => $value, 485 'competencyframeworkid' => $this->get_competencyframeworkid() 486 ); 487 if ($DB->record_exists_select(self::TABLE, $sql, $params)) { 488 return new lang_string('idnumbertaken', 'error'); 489 } 490 return true; 491 } 492 493 /** 494 * Validate the path. 495 * 496 * @param string $value The path. 497 * @return true|lang_string 498 */ 499 protected function validate_path($value) { 500 501 // The last item should be the parent ID. 502 $id = $this->get_parentid(); 503 if (substr($value, -(strlen($id) + 2)) != '/' . $id . '/') { 504 return new lang_string('invaliddata', 'error'); 505 506 } else if (!preg_match('@/([0-9]+/)+@', $value)) { 507 // The format of the path is not correct. 508 return new lang_string('invaliddata', 'error'); 509 } 510 511 return true; 512 } 513 514 /** 515 * Validate the parent ID. 516 * 517 * @param string $value The ID. 518 * @return true|lang_string 519 */ 520 protected function validate_parentid($value) { 521 522 // Check that the parent exists. But only if we don't have it already, and we actually have a parent. 523 if (!empty($value) && !$this->newparent && !self::record_exists($value)) { 524 return new lang_string('invaliddata', 'error'); 525 } 526 527 // During update. 528 if ($this->get_id()) { 529 530 // If there is a new parent. 531 if ($this->beforeupdate->get_parentid() != $value && $this->newparent) { 532 533 // Check that the new parent belongs to the same framework. 534 if ($this->newparent->get_competencyframeworkid() != $this->get_competencyframeworkid()) { 535 return new lang_string('invaliddata', 'error'); 536 } 537 } 538 } 539 540 return true; 541 } 542 543 /** 544 * Validate the rule. 545 * 546 * @param string $value The ID. 547 * @return true|lang_string 548 */ 549 protected function validate_ruletype($value) { 550 if ($value === null) { 551 return true; 552 } 553 554 if (!class_exists($value) || !is_subclass_of($value, 'core_competency\\competency_rule')) { 555 return new lang_string('invaliddata', 'error'); 556 } 557 558 return true; 559 } 560 561 /** 562 * Validate the rule config. 563 * 564 * @param string $value The ID. 565 * @return true|lang_string 566 */ 567 protected function validate_ruleconfig($value) { 568 $rule = $this->get_rule_object(); 569 570 // We don't have a rule. 571 if (empty($rule)) { 572 if ($value === null) { 573 // No config, perfect. 574 return true; 575 } 576 // Config but no rules, whoops! 577 return new lang_string('invaliddata', 'error'); 578 } 579 580 $valid = $rule->validate_config($value); 581 if ($valid !== true) { 582 // Whoops! 583 return new lang_string('invaliddata', 'error'); 584 } 585 586 return true; 587 } 588 589 /** 590 * Validate the scale ID. 591 * 592 * Note that the value for a scale can never be 0, null has to be used when 593 * the framework's scale has to be used. 594 * 595 * @param int $value 596 * @return true|lang_string 597 */ 598 protected function validate_scaleid($value) { 599 global $DB; 600 601 if ($value === null) { 602 return true; 603 } 604 605 // Always validate that the scale exists. 606 if (!$DB->record_exists_select('scale', 'id = :id', array('id' => $value))) { 607 return new lang_string('invalidscaleid', 'error'); 608 } 609 610 // During update. 611 if ($this->get_id()) { 612 613 // Validate that we can only change the scale when it is not used yet. 614 if ($this->beforeupdate->get_scaleid() != $value) { 615 if ($this->has_user_competencies()) { 616 return new lang_string('errorscalealreadyused', 'core_competency'); 617 } 618 } 619 620 } 621 622 return true; 623 } 624 625 /** 626 * Validate the scale configuration. 627 * 628 * This logic is adapted from {@link \core_competency\competency_framework::validate_scaleconfiguration()}. 629 * 630 * @param string $value The scale configuration. 631 * @return bool|lang_string 632 */ 633 protected function validate_scaleconfiguration($value) { 634 $scaleid = $this->get('scaleid'); 635 if ($scaleid === null && $value === null) { 636 return true; 637 } 638 639 $scaledefaultselected = false; 640 $proficientselected = false; 641 $scaleconfigurations = json_decode($value); 642 643 if (is_array($scaleconfigurations)) { 644 645 // The first element of the array contains the scale ID. 646 $scaleinfo = array_shift($scaleconfigurations); 647 if (empty($scaleinfo) || !isset($scaleinfo->scaleid) || $scaleinfo->scaleid != $scaleid) { 648 // This should never happen. 649 return new lang_string('errorscaleconfiguration', 'core_competency'); 650 } 651 652 // Walk through the array to find proficient and default values. 653 foreach ($scaleconfigurations as $scaleconfiguration) { 654 if (isset($scaleconfiguration->scaledefault) && $scaleconfiguration->scaledefault) { 655 $scaledefaultselected = true; 656 } 657 if (isset($scaleconfiguration->proficient) && $scaleconfiguration->proficient) { 658 $proficientselected = true; 659 } 660 } 661 } 662 663 if (!$scaledefaultselected || !$proficientselected) { 664 return new lang_string('errorscaleconfiguration', 'core_competency'); 665 } 666 667 return true; 668 } 669 670 /** 671 * Return whether or not the competency IDs share the same framework. 672 * 673 * @param array $ids Competency IDs 674 * @return bool 675 */ 676 public static function share_same_framework(array $ids) { 677 global $DB; 678 list($insql, $params) = $DB->get_in_or_equal($ids); 679 return $DB->count_records_select(self::TABLE, "id $insql", $params, "COUNT(DISTINCT(competencyframeworkid))") == 1; 680 } 681 682 /** 683 * Get the available rules. 684 * 685 * @return array Keys are the class names, values are the name of the rule. 686 */ 687 public static function get_available_rules() { 688 // Fully qualified class names without leading slashes because get_class() does not add them either. 689 $rules = array( 690 'core_competency\\competency_rule_all' => competency_rule_all::get_name(), 691 'core_competency\\competency_rule_points' => competency_rule_points::get_name(), 692 ); 693 return $rules; 694 } 695 696 /** 697 * Return the current depth of a competency framework. 698 * 699 * @param int $frameworkid The framework ID. 700 * @return int 701 */ 702 public static function get_framework_depth($frameworkid) { 703 global $DB; 704 $totallength = $DB->sql_length('path'); 705 $trimmedlength = $DB->sql_length("REPLACE(path, '/', '')"); 706 $sql = "SELECT ($totallength - $trimmedlength - 1) AS depth 707 FROM {" . self::TABLE . "} 708 WHERE competencyframeworkid = :id 709 ORDER BY depth DESC"; 710 $record = $DB->get_record_sql($sql, array('id' => $frameworkid), IGNORE_MULTIPLE); 711 if (!$record) { 712 $depth = 0; 713 } else { 714 $depth = $record->depth; 715 } 716 return $depth; 717 } 718 719 /** 720 * Build a framework tree with competency nodes. 721 * 722 * @param int $frameworkid the framework id 723 * @return node[] tree of framework competency nodes 724 */ 725 public static function get_framework_tree($frameworkid) { 726 $competencies = self::search('', $frameworkid); 727 return self::build_tree($competencies, 0); 728 } 729 730 /** 731 * Get the context from the framework. 732 * 733 * @return context 734 */ 735 public function get_context() { 736 return $this->get_framework()->get_context(); 737 } 738 739 /** 740 * Recursively build up the tree of nodes. 741 * 742 * @param array $all - List of all competency classes. 743 * @param int $parentid - The current parent ID. Pass 0 to build the tree from the top. 744 * @return node[] $tree tree of nodes 745 */ 746 protected static function build_tree($all, $parentid) { 747 $tree = array(); 748 foreach ($all as $one) { 749 if ($one->get_parentid() == $parentid) { 750 $node = new stdClass(); 751 $node->competency = $one; 752 $node->children = self::build_tree($all, $one->get_id()); 753 $tree[] = $node; 754 } 755 } 756 return $tree; 757 } 758 759 /** 760 * Check if we can delete competencies safely. 761 * 762 * This moethod does not check any capablities. 763 * Check if competency is used in a plan and user competency. 764 * Check if competency is used in a template. 765 * Check if competency is linked to a course. 766 * 767 * @param array $ids Array of competencies ids. 768 * @return bool True if we can delete the competencies. 769 */ 770 public static function can_all_be_deleted($ids) { 771 if (empty($ids)) { 772 return true; 773 } 774 // Check if competency is used in template. 775 if (template_competency::has_records_for_competencies($ids)) { 776 return false; 777 } 778 // Check if competency is used in plan. 779 if (plan_competency::has_records_for_competencies($ids)) { 780 return false; 781 } 782 // Check if competency is used in course. 783 if (course_competency::has_records_for_competencies($ids)) { 784 return false; 785 } 786 // Check if competency is used in user_competency. 787 if (user_competency::has_records_for_competencies($ids)) { 788 return false; 789 } 790 // Check if competency is used in user_competency_plan. 791 if (user_competency_plan::has_records_for_competencies($ids)) { 792 return false; 793 } 794 return true; 795 } 796 797 /** 798 * Delete the competencies. 799 * 800 * This method is reserved to core usage. 801 * This method does not trigger the after_delete event. 802 * This method does not delete related objects such as related competencies and evidences. 803 * 804 * @param array $ids The competencies ids. 805 * @return bool True if the competencies were deleted successfully. 806 */ 807 public static function delete_multiple($ids) { 808 global $DB; 809 list($insql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); 810 return $DB->delete_records_select(self::TABLE, "id $insql", $params); 811 } 812 813 /** 814 * Get descendant ids. 815 * 816 * @param competency $competency The competency. 817 * @return array Array of competencies ids. 818 */ 819 public static function get_descendants_ids($competency) { 820 global $DB; 821 822 $path = $DB->sql_like_escape($competency->get_path() . $competency->get_id() . '/') . '%'; 823 $like = $DB->sql_like('path', ':likepath'); 824 return $DB->get_fieldset_select(self::TABLE, 'id', $like, array('likepath' => $path)); 825 } 826 827 /** 828 * Get competencyids by frameworkid. 829 * 830 * @param int $frameworkid The competency framework ID. 831 * @return array Array of competency ids. 832 */ 833 public static function get_ids_by_frameworkid($frameworkid) { 834 global $DB; 835 836 return $DB->get_fieldset_select(self::TABLE, 'id', 'competencyframeworkid = :frmid', array('frmid' => $frameworkid)); 837 } 838 839 /** 840 * Delete competencies by framework ID. 841 * 842 * This method is reserved to core usage. 843 * This method does not trigger the after_delete event. 844 * This method does not delete related objects such as related competencies and evidences. 845 * 846 * @param int $id the framework ID 847 * @return bool Return true if delete was successful. 848 */ 849 public static function delete_by_frameworkid($id) { 850 global $DB; 851 return $DB->delete_records(self::TABLE, array('competencyframeworkid' => $id)); 852 } 853 854 /** 855 * Get competency ancestors. 856 * 857 * @return competency[] Return array of ancestors. 858 */ 859 public function get_ancestors() { 860 global $DB; 861 $ancestors = array(); 862 $ancestorsids = explode('/', trim($this->get_path(), '/')); 863 // Drop the root item from the array /0/. 864 array_shift($ancestorsids); 865 if (!empty($ancestorsids)) { 866 list($insql, $params) = $DB->get_in_or_equal($ancestorsids, SQL_PARAMS_NAMED); 867 $ancestors = self::get_records_select("id $insql", $params); 868 } 869 return $ancestors; 870 } 871 872 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |