Exemple #1
0
 /**
  * Archive user competencies in a plan.
  *
  * @param int $plan The plan object.
  * @return void
  */
 protected static function archive_user_competencies_in_plan($plan)
 {
     // Check if the plan was already completed.
     if ($plan->get_status() == plan::STATUS_COMPLETE) {
         throw new coding_exception('The plan is already completed.');
     }
     $competencies = $plan->get_competencies();
     $usercompetencies = user_competency::get_multiple($plan->get_userid(), $competencies);
     $i = 0;
     foreach ($competencies as $competency) {
         $found = false;
         foreach ($usercompetencies as $uckey => $uc) {
             if ($uc->get_competencyid() == $competency->get_id()) {
                 $found = true;
                 $ucprecord = $uc->to_record();
                 $ucprecord->planid = $plan->get_id();
                 $ucprecord->sortorder = $i;
                 unset($ucprecord->id);
                 unset($ucprecord->status);
                 unset($ucprecord->reviewerid);
                 $usercompetencyplan = new user_competency_plan(0, $ucprecord);
                 $usercompetencyplan->create();
                 unset($usercompetencies[$uckey]);
                 break;
             }
         }
         // If the user competency doesn't exist, we create a new relation in user_competency_plan.
         if (!$found) {
             $usercompetencyplan = user_competency_plan::create_relation($plan->get_userid(), $competency->get_id(), $plan->get_id());
             $usercompetencyplan->set_sortorder($i);
             $usercompetencyplan->create();
         }
         $i++;
     }
 }