Example #1
0
 /**
  * Recursively duplicate competencies from a tree, we start duplicating from parents to children to have a correct path.
  * This method does not copy the related competencies.
  *
  * @param int $frameworkid - framework id
  * @param competency[] $tree - array of competencies object
  * @param int $oldparent - old parent id
  * @param int $newparent - new parent id
  * @return competency[] $matchids - List of old competencies ids matched with new competencies object.
  */
 protected static function duplicate_competency_tree($frameworkid, $tree, $oldparent = 0, $newparent = 0)
 {
     $matchids = array();
     foreach ($tree as $node) {
         if ($node->competency->get_parentid() == $oldparent) {
             $parentid = $node->competency->get_id();
             // Create the competency.
             $competency = new competency(0, $node->competency->to_record());
             $competency->set_competencyframeworkid($frameworkid);
             $competency->set_parentid($newparent);
             $competency->set_path('');
             $competency->set_id(0);
             $competency->reset_rule();
             $competency->create();
             // Trigger the created event competency.
             \core\event\competency_created::create_from_competency($competency)->trigger();
             // Match the old id with the new one.
             $matchids[$parentid] = $competency;
             if (!empty($node->children)) {
                 // Duplicate children competency.
                 $childrenids = self::duplicate_competency_tree($frameworkid, $node->children, $parentid, $competency->get_id());
                 // Array_merge does not keep keys when merging so we use the + operator.
                 $matchids = $matchids + $childrenids;
             }
         }
     }
     return $matchids;
 }
Example #2
0
 /**
  * Create a new competency.
  *
  * @param array|stdClass $record
  * @return competency
  */
 public function create_competency($record = null)
 {
     $this->frameworkcount++;
     $i = $this->frameworkcount;
     $record = (object) $record;
     if (!isset($record->competencyframeworkid)) {
         throw new coding_exception('The competencyframeworkid value is required.');
     }
     if (!isset($record->shortname)) {
         $record->shortname = "Competency shortname {$i}";
     }
     if (!isset($record->idnumber)) {
         $record->idnumber = "cmp{$i}";
     }
     if (!isset($record->description)) {
         $record->description = "Competency {$i} description ";
     }
     if (!isset($record->descriptionformat)) {
         $record->descriptionformat = FORMAT_HTML;
     }
     if (isset($record->scaleconfiguration) && (is_array($record->scaleconfiguration) || is_object($record->scaleconfiguration))) {
         // Conveniently encode the config.
         $record->scaleconfiguration = json_encode($record->scaleconfiguration);
     }
     $competency = new competency(0, $record);
     $competency->create();
     return $competency;
 }