original_string: protected static function apply_competency_rules_from_usercompetency(user_competency $usercompetency,
                                                                           competency $competency = null) {
  
          // Perform some basic checks.
          if (!$usercompetency->get('proficiency')) {
              throw new coding_exception('The user competency passed is not completed.');
          }
          if ($competency === null) {
              $competency = $usercompetency->get_competency();
          }
          if ($competency->get('id') != $usercompetency->get('competencyid')) {
              throw new coding_exception('Mismatch between user competency and competency.');
          }
  
          // Fetch the parent.
          $parent = $competency->get_parent();
          if ($parent === null) {
              return;
          }
  
          // The parent should have a rule, and a meaningful outcome.
          $ruleoutcome = $parent->get('ruleoutcome');
          if ($ruleoutcome == competency::OUTCOME_NONE) {
              return;
          }
          $rule = $parent->get_rule_object();
          if ($rule === null) {
              return;
          }
  
          // Fetch or create the user competency for the parent.
          $userid = $usercompetency->get('userid');
          $parentuc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $parent->get('id')));
          if (!$parentuc) {
              $parentuc = user_competency::create_relation($userid, $parent->get('id'));
              $parentuc->create();
          }
  
          // Does the rule match?
          if (!$rule->matches($parentuc)) {
              return;
          }
  
          // Figuring out what to do.
          $recommend = false;
          if ($ruleoutcome == competency::OUTCOME_EVIDENCE) {
              $action = evidence::ACTION_LOG;
  
          } else if ($ruleoutcome == competency::OUTCOME_RECOMMEND) {
              $action = evidence::ACTION_LOG;
              $recommend = true;
  
          } else if ($ruleoutcome == competency::OUTCOME_COMPLETE) {
              $action = evidence::ACTION_COMPLETE;
  
          } else {
              throw new moodle_exception('Unexpected rule outcome: ' + $ruleoutcome);
          }
  
          // Finally add an evidence.
          static::add_evidence(
              $userid,
              $parent,
              $parent->get_context()->id,
              $action,
              'evidence_competencyrule',
              'core_competency',
              null,
              $recommend
          );
      }
code: protected static function apply_competency_rules_from_usercompetency(user_competency $usercompetency,
                                                                           competency $competency = null) {
  
          // Perform some basic checks.
          if (!$usercompetency->get('proficiency')) {
              throw new coding_exception('The user competency passed is not completed.');
          }
          if ($competency === null) {
              $competency = $usercompetency->get_competency();
          }
          if ($competency->get('id') != $usercompetency->get('competencyid')) {
              throw new coding_exception('Mismatch between user competency and competency.');
          }
  
          // Fetch the parent.
          $parent = $competency->get_parent();
          if ($parent === null) {
              return;
          }
  
          // The parent should have a rule, and a meaningful outcome.
          $ruleoutcome = $parent->get('ruleoutcome');
          if ($ruleoutcome == competency::OUTCOME_NONE) {
              return;
          }
          $rule = $parent->get_rule_object();
          if ($rule === null) {
              return;
          }
  
          // Fetch or create the user competency for the parent.
          $userid = $usercompetency->get('userid');
          $parentuc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $parent->get('id')));
          if (!$parentuc) {
              $parentuc = user_competency::create_relation($userid, $parent->get('id'));
              $parentuc->create();
          }
  
          // Does the rule match?
          if (!$rule->matches($parentuc)) {
              return;
          }
  
          // Figuring out what to do.
          $recommend = false;
          if ($ruleoutcome == competency::OUTCOME_EVIDENCE) {
              $action = evidence::ACTION_LOG;
  
          } else if ($ruleoutcome == competency::OUTCOME_RECOMMEND) {
              $action = evidence::ACTION_LOG;
              $recommend = true;
  
          } else if ($ruleoutcome == competency::OUTCOME_COMPLETE) {
              $action = evidence::ACTION_COMPLETE;
  
          } else {
              throw new moodle_exception('Unexpected rule outcome: ' + $ruleoutcome);
          }
  
          // Finally add an evidence.
          static::add_evidence(
              $userid,
              $parent,
              $parent->get_context()->id,
              $action,
              'evidence_competencyrule',
              'core_competency',
              null,
              $recommend
          );
      }
code_tokens: protected static function apply_competency_rules_from_usercompetency ( user_competency $ usercompetency , competency $ competency = null ) { // Perform some basic checks. if ( ! $ usercompetency -> get ( 'proficiency' ) ) { throw new coding_exception ( 'The user competency passed is not completed.' ) ; } if ( $ competency === null ) { $ competency = $ usercompetency -> get_competency ( ) ; } if ( $ competency -> get ( 'id' ) != $ usercompetency -> get ( 'competencyid' ) ) { throw new coding_exception ( 'Mismatch between user competency and competency.' ) ; } // Fetch the parent. $ parent = $ competency -> get_parent ( ) ; if ( $ parent === null ) { return ; } // The parent should have a rule, and a meaningful outcome. $ ruleoutcome = $ parent -> get ( 'ruleoutcome' ) ; if ( $ ruleoutcome == competency :: OUTCOME_NONE ) { return ; } $ rule = $ parent -> get_rule_object ( ) ; if ( $ rule === null ) { return ; } // Fetch or create the user competency for the parent. $ userid = $ usercompetency -> get ( 'userid' ) ; $ parentuc = user_competency :: get_record ( array ( 'userid' => $ userid , 'competencyid' => $ parent -> get ( 'id' ) ) ) ; if ( ! $ parentuc ) { $ parentuc = user_competency :: create_relation ( $ userid , $ parent -> get ( 'id' ) ) ; $ parentuc -> create ( ) ; } // Does the rule match? if ( ! $ rule -> matches ( $ parentuc ) ) { return ; } // Figuring out what to do. $ recommend = false ; if ( $ ruleoutcome == competency :: OUTCOME_EVIDENCE ) { $ action = evidence :: ACTION_LOG ; } else if ( $ ruleoutcome == competency :: OUTCOME_RECOMMEND ) { $ action = evidence :: ACTION_LOG ; $ recommend = true ; } else if ( $ ruleoutcome == competency :: OUTCOME_COMPLETE ) { $ action = evidence :: ACTION_COMPLETE ; } else { throw new moodle_exception ( 'Unexpected rule outcome: ' + $ ruleoutcome ) ; } // Finally add an evidence. static :: add_evidence ( $ userid , $ parent , $ parent -> get_context ( ) -> id , $ action , 'evidence_competencyrule' , 'core_competency' , null , $ recommend ) ; }
docstring: Apply the competency rules from a user competency.
  
  The user competency passed should be one that was recently marked as complete.
  A user competency is considered 'complete' when it's proficiency value is true.
  
  This method will check if the parent of this usercompetency's competency has any
  rules and if so will see if they match. When matched it will take the required
  step to add evidence and trigger completion, etc...
  
  @param  user_competency $usercompetency The user competency recently completed.
  @param  competency|null $competency     The competency of the user competency, useful to avoid unnecessary read.
  @return void
docstring_tokens: Apply the competency rules from a user competency .