Example #1
1
 /**
  * Triggered when 'course_module_completion_updated' event is triggered.
  *
  * @param \core\event\course_module_completion_updated $event
  */
 public static function course_module_criteria_review(\core\event\course_module_completion_updated $event)
 {
     global $DB, $CFG;
     if (!empty($CFG->enablebadges)) {
         require_once $CFG->dirroot . '/lib/badgeslib.php';
         $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
         $userid = $event->relateduserid;
         $mod = $event->contextinstanceid;
         if ($eventdata->completionstate == COMPLETION_COMPLETE || $eventdata->completionstate == COMPLETION_COMPLETE_PASS || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) {
             // Need to take into account that there can be more than one badge with the same activity in its criteria.
             if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) {
                 foreach ($rs as $r) {
                     $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST);
                     $badge = new badge($bid);
                     if (!$badge->is_active() || $badge->is_issued($userid)) {
                         continue;
                     }
                     if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) {
                         $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid);
                         if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
                             $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
                             $badge->issue($userid);
                         }
                     }
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Observe when a course module is marked as completed.
  *
  * Note that the user being logged in while this happens may be anyone.
  * Do not rely on capability checks here!
  *
  * @param  \core\event\course_module_completion_updated $event
  * @return void
  */
 public static function observe_course_module_completion_updated(\core\event\course_module_completion_updated $event)
 {
     if (!static::is_enabled()) {
         return;
     }
     $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
     if ($eventdata->completionstate == COMPLETION_COMPLETE || $eventdata->completionstate == COMPLETION_COMPLETE_PASS) {
         $coursemodulecompetencies = course_module_competency::list_course_module_competencies($eventdata->coursemoduleid);
         $cm = get_coursemodule_from_id(null, $eventdata->coursemoduleid);
         $fastmodinfo = get_fast_modinfo($cm->course)->cms[$cm->id];
         $cmname = $fastmodinfo->name;
         $url = $fastmodinfo->url;
         foreach ($coursemodulecompetencies as $coursemodulecompetency) {
             $outcome = $coursemodulecompetency->get_ruleoutcome();
             $action = null;
             $recommend = false;
             $strdesc = 'evidence_coursemodulecompleted';
             if ($outcome == course_module_competency::OUTCOME_EVIDENCE) {
                 $action = evidence::ACTION_LOG;
             } else {
                 if ($outcome == course_module_competency::OUTCOME_RECOMMEND) {
                     $action = evidence::ACTION_LOG;
                     $recommend = true;
                 } else {
                     if ($outcome == course_module_competency::OUTCOME_COMPLETE) {
                         $action = evidence::ACTION_COMPLETE;
                     } else {
                         throw new moodle_exception('Unexpected rule outcome: ' + $outcome);
                     }
                 }
             }
             static::add_evidence($event->relateduserid, $coursemodulecompetency->get_competencyid(), $event->contextid, $action, $strdesc, 'core_competency', $cmname, $recommend, $url);
         }
     }
 }