original_string: public static function reorder_course_module_competency($cmorid, $competencyidfrom, $competencyidto) {
          static::require_enabled();
          $cm = $cmorid;
          if (!is_object($cmorid)) {
              $cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST);
          }
          // Check the user have access to the course module.
          self::validate_course_module($cm);
  
          // First we do a permissions check.
          $context = context_module::instance($cm->id);
  
          require_capability('moodle/competency:coursecompetencymanage', $context);
  
          $down = true;
          $matches = course_module_competency::get_records(array('cmid' => $cm->id, 'competencyid' => $competencyidfrom));
          if (count($matches) == 0) {
               throw new coding_exception('The link does not exist');
          }
  
          $competencyfrom = array_pop($matches);
          $matches = course_module_competency::get_records(array('cmid' => $cm->id, 'competencyid' => $competencyidto));
          if (count($matches) == 0) {
               throw new coding_exception('The link does not exist');
          }
  
          $competencyto = array_pop($matches);
  
          $all = course_module_competency::get_records(array('cmid' => $cm->id), '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 => $coursemodulecompetency) {
              $sort = $coursemodulecompetency->get('sortorder');
              if ($down && $sort > $competencyfrom->get('sortorder') && $sort <= $competencyto->get('sortorder')) {
                  $coursemodulecompetency->set('sortorder', $coursemodulecompetency->get('sortorder') - 1);
                  $coursemodulecompetency->update();
              } else if (!$down && $sort >= $competencyto->get('sortorder') && $sort < $competencyfrom->get('sortorder')) {
                  $coursemodulecompetency->set('sortorder', $coursemodulecompetency->get('sortorder') + 1);
                  $coursemodulecompetency->update();
              }
          }
          $competencyfrom->set('sortorder', $competencyto->get('sortorder'));
          return $competencyfrom->update();
      }
code: public static function reorder_course_module_competency($cmorid, $competencyidfrom, $competencyidto) {
          static::require_enabled();
          $cm = $cmorid;
          if (!is_object($cmorid)) {
              $cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST);
          }
          // Check the user have access to the course module.
          self::validate_course_module($cm);
  
          // First we do a permissions check.
          $context = context_module::instance($cm->id);
  
          require_capability('moodle/competency:coursecompetencymanage', $context);
  
          $down = true;
          $matches = course_module_competency::get_records(array('cmid' => $cm->id, 'competencyid' => $competencyidfrom));
          if (count($matches) == 0) {
               throw new coding_exception('The link does not exist');
          }
  
          $competencyfrom = array_pop($matches);
          $matches = course_module_competency::get_records(array('cmid' => $cm->id, 'competencyid' => $competencyidto));
          if (count($matches) == 0) {
               throw new coding_exception('The link does not exist');
          }
  
          $competencyto = array_pop($matches);
  
          $all = course_module_competency::get_records(array('cmid' => $cm->id), '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 => $coursemodulecompetency) {
              $sort = $coursemodulecompetency->get('sortorder');
              if ($down && $sort > $competencyfrom->get('sortorder') && $sort <= $competencyto->get('sortorder')) {
                  $coursemodulecompetency->set('sortorder', $coursemodulecompetency->get('sortorder') - 1);
                  $coursemodulecompetency->update();
              } else if (!$down && $sort >= $competencyto->get('sortorder') && $sort < $competencyfrom->get('sortorder')) {
                  $coursemodulecompetency->set('sortorder', $coursemodulecompetency->get('sortorder') + 1);
                  $coursemodulecompetency->update();
              }
          }
          $competencyfrom->set('sortorder', $competencyto->get('sortorder'));
          return $competencyfrom->update();
      }
code_tokens: public static function reorder_course_module_competency ( $ cmorid , $ competencyidfrom , $ competencyidto ) { static :: require_enabled ( ) ; $ cm = $ cmorid ; if ( ! is_object ( $ cmorid ) ) { $ cm = get_coursemodule_from_id ( '' , $ cmorid , 0 , true , MUST_EXIST ) ; } // Check the user have access to the course module. self :: validate_course_module ( $ cm ) ; // First we do a permissions check. $ context = context_module :: instance ( $ cm -> id ) ; require_capability ( 'moodle/competency:coursecompetencymanage' , $ context ) ; $ down = true ; $ matches = course_module_competency :: get_records ( array ( 'cmid' => $ cm -> id , 'competencyid' => $ competencyidfrom ) ) ; if ( count ( $ matches ) == 0 ) { throw new coding_exception ( 'The link does not exist' ) ; } $ competencyfrom = array_pop ( $ matches ) ; $ matches = course_module_competency :: get_records ( array ( 'cmid' => $ cm -> id , 'competencyid' => $ competencyidto ) ) ; if ( count ( $ matches ) == 0 ) { throw new coding_exception ( 'The link does not exist' ) ; } $ competencyto = array_pop ( $ matches ) ; $ all = course_module_competency :: get_records ( array ( 'cmid' => $ cm -> id ) , '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 => $ coursemodulecompetency ) { $ sort = $ coursemodulecompetency -> get ( 'sortorder' ) ; if ( $ down && $ sort > $ competencyfrom -> get ( 'sortorder' ) && $ sort <= $ competencyto -> get ( 'sortorder' ) ) { $ coursemodulecompetency -> set ( 'sortorder' , $ coursemodulecompetency -> get ( 'sortorder' ) - 1 ) ; $ coursemodulecompetency -> update ( ) ; } else if ( ! $ down && $ sort >= $ competencyto -> get ( 'sortorder' ) && $ sort < $ competencyfrom -> get ( 'sortorder' ) ) { $ coursemodulecompetency -> set ( 'sortorder' , $ coursemodulecompetency -> get ( 'sortorder' ) + 1 ) ; $ coursemodulecompetency -> update ( ) ; } } $ competencyfrom -> set ( 'sortorder' , $ competencyto -> get ( 'sortorder' ) ) ; return $ competencyfrom -> update ( ) ; }
docstring: Move the course module competency up or down in the display list.
  
  Requires moodle/competency:coursecompetencymanage capability at the course module context.
  
  @param mixed $cmorid The course module, or id of the course module
  @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 course module competency up or down in the display list .