Пример #1
0
 /**
  * Move the plan competency up or down in the display list.
  *
  * Requires moodle/competency:planmanage capability at the system context.
  *
  * @param int $planid The plan  id
  * @param int $competencyidfrom The id of the competency we are moving.
  * @param int $competencyidto The id of the competency we are moving to.
  * @return boolean
  */
 public static function reorder_plan_competency($planid, $competencyidfrom, $competencyidto)
 {
     static::require_enabled();
     $plan = new plan($planid);
     // First we do a permissions check.
     if (!$plan->can_manage()) {
         $context = context_user::instance($plan->get_userid());
         throw new required_capability_exception($context, 'moodle/competency:planmanage', 'nopermissions', '');
     } else {
         if ($plan->is_based_on_template()) {
             throw new coding_exception('A competency can not be reordered in a learning plan based on a template');
         }
     }
     if (!$plan->can_be_edited()) {
         throw new coding_exception('A competency can not be reordered in a learning plan completed');
     }
     $down = true;
     $matches = plan_competency::get_records(array('planid' => $planid, 'competencyid' => $competencyidfrom));
     if (count($matches) == 0) {
         throw new coding_exception('The link does not exist');
     }
     $competencyfrom = array_pop($matches);
     $matches = plan_competency::get_records(array('planid' => $planid, 'competencyid' => $competencyidto));
     if (count($matches) == 0) {
         throw new coding_exception('The link does not exist');
     }
     $competencyto = array_pop($matches);
     $all = plan_competency::get_records(array('planid' => $planid), 'sortorder', 'ASC', 0, 0);
     if ($competencyfrom->get_sortorder() > $competencyto->get_sortorder()) {
         // We are moving up, so put it before the "to" item.
         $down = false;
     }
     foreach ($all as $id => $plancompetency) {
         $sort = $plancompetency->get_sortorder();
         if ($down && $sort > $competencyfrom->get_sortorder() && $sort <= $competencyto->get_sortorder()) {
             $plancompetency->set_sortorder($plancompetency->get_sortorder() - 1);
             $plancompetency->update();
         } else {
             if (!$down && $sort >= $competencyto->get_sortorder() && $sort < $competencyfrom->get_sortorder()) {
                 $plancompetency->set_sortorder($plancompetency->get_sortorder() + 1);
                 $plancompetency->update();
             }
         }
     }
     $competencyfrom->set_sortorder($competencyto->get_sortorder());
     return $competencyfrom->update();
 }