original_string: 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();
      }
code: 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();
      }
code_tokens: 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 ( ) ; }
docstring: 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
docstring_tokens: Move the plan competency up or down in the display list .