original_string: public static function add_competency_to_course_module($cmorid, $competencyid) {
          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);
  
          // Check that the competency belongs to the course.
          $exists = course_competency::get_records(array('courseid' => $cm->course, 'competencyid' => $competencyid));
          if (!$exists) {
              throw new coding_exception('Cannot add a competency to a module if it does not belong to the course');
          }
  
          $record = new stdClass();
          $record->cmid = $cm->id;
          $record->competencyid = $competencyid;
  
          $coursemodulecompetency = new course_module_competency();
          $exists = $coursemodulecompetency->get_records(array('cmid' => $cm->id, 'competencyid' => $competencyid));
          if (!$exists) {
              $coursemodulecompetency->from_record($record);
              if ($coursemodulecompetency->create()) {
                  return true;
              }
          }
          return false;
      }
code: public static function add_competency_to_course_module($cmorid, $competencyid) {
          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);
  
          // Check that the competency belongs to the course.
          $exists = course_competency::get_records(array('courseid' => $cm->course, 'competencyid' => $competencyid));
          if (!$exists) {
              throw new coding_exception('Cannot add a competency to a module if it does not belong to the course');
          }
  
          $record = new stdClass();
          $record->cmid = $cm->id;
          $record->competencyid = $competencyid;
  
          $coursemodulecompetency = new course_module_competency();
          $exists = $coursemodulecompetency->get_records(array('cmid' => $cm->id, 'competencyid' => $competencyid));
          if (!$exists) {
              $coursemodulecompetency->from_record($record);
              if ($coursemodulecompetency->create()) {
                  return true;
              }
          }
          return false;
      }
code_tokens: public static function add_competency_to_course_module ( $ cmorid , $ competencyid ) { 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 ) ; // Check that the competency belongs to the course. $ exists = course_competency :: get_records ( array ( 'courseid' => $ cm -> course , 'competencyid' => $ competencyid ) ) ; if ( ! $ exists ) { throw new coding_exception ( 'Cannot add a competency to a module if it does not belong to the course' ) ; } $ record = new stdClass ( ) ; $ record -> cmid = $ cm -> id ; $ record -> competencyid = $ competencyid ; $ coursemodulecompetency = new course_module_competency ( ) ; $ exists = $ coursemodulecompetency -> get_records ( array ( 'cmid' => $ cm -> id , 'competencyid' => $ competencyid ) ) ; if ( ! $ exists ) { $ coursemodulecompetency -> from_record ( $ record ) ; if ( $ coursemodulecompetency -> create ( ) ) { return true ; } } return false ; }
docstring: Add a competency to this course module.
  
  @param mixed $cmorid The course module, or id of the course module
  @param int $competencyid The id of the competency
  @return bool
docstring_tokens: Add a competency to this course module .