Пример #1
0
 /**
  * Reopen a plan.
  *
  * @param int|plan $planorid The plan, or its ID.
  * @return bool
  */
 public static function reopen_plan($planorid)
 {
     global $DB;
     static::require_enabled();
     $plan = $planorid;
     if (!is_object($planorid)) {
         $plan = new plan($planorid);
     }
     // Validate that the plan as it is can be managed.
     if (!$plan->can_manage()) {
         $context = context_user::instance($plan->get_userid());
         throw new required_capability_exception($context, 'moodle/competency:planmanage', 'nopermissions', '');
     }
     $beforestatus = $plan->get_status();
     $plan->set_status(plan::STATUS_ACTIVE);
     // Validate if status can be changed.
     if (!$plan->can_manage()) {
         $context = context_user::instance($plan->get_userid());
         throw new required_capability_exception($context, 'moodle/competency:planmanage', 'nopermissions', '');
     }
     // Wrap the updates in a DB transaction.
     $transaction = $DB->start_delegated_transaction();
     // Delete archived user competencies if the status of the plan is changed from complete to another status.
     $mustremovearchivedcompetencies = $beforestatus == plan::STATUS_COMPLETE && $plan->get_status() != plan::STATUS_COMPLETE;
     if ($mustremovearchivedcompetencies) {
         self::remove_archived_user_competencies_in_plan($plan);
     }
     // If duedate less than or equal to duedate_threshold unset it.
     if ($plan->get_duedate() <= time() + plan::DUEDATE_THRESHOLD) {
         $plan->set_duedate(0);
     }
     $success = $plan->update();
     if (!$success) {
         $transaction->rollback(new moodle_exception('The plan could not be updated.'));
         return $success;
     }
     $transaction->allow_commit();
     // Trigger reopened event.
     \core\event\competency_plan_reopened::create_from_plan($plan)->trigger();
     return $success;
 }