コード例 #1
0
ファイル: observer.php プロジェクト: evltuma/moodle
 /**
  * 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);
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #2
0
 /**
  * Updates completion data for a particular coursemodule and user (user is
  * determined from $data).
  *
  * (Internal function. Not private, so we can unit-test it.)
  *
  * @param stdClass|cm_info $cm Activity
  * @param stdClass $data Data about completion for that user
  */
 public function internal_set_data($cm, $data)
 {
     global $USER, $SESSION, $DB;
     $transaction = $DB->start_delegated_transaction();
     if (!$data->id) {
         // Check there isn't really a row
         $data->id = $DB->get_field('course_modules_completion', 'id', array('coursemoduleid' => $data->coursemoduleid, 'userid' => $data->userid));
     }
     if (!$data->id) {
         // Didn't exist before, needs creating
         $data->id = $DB->insert_record('course_modules_completion', $data);
     } else {
         // Has real (nonzero) id meaning that a database row exists, update
         $DB->update_record('course_modules_completion', $data);
     }
     $transaction->allow_commit();
     $cmcontext = context_module::instance($data->coursemoduleid, MUST_EXIST);
     $coursecontext = $cmcontext->get_parent_context();
     // Trigger an event for course module completion changed.
     $event = \core\event\course_module_completion_updated::create(array('objectid' => $data->id, 'context' => $cmcontext, 'relateduserid' => $data->userid, 'other' => array('relateduserid' => $data->userid)));
     $event->add_record_snapshot('course_modules_completion', $data);
     $event->trigger();
     if ($data->userid == $USER->id) {
         $SESSION->completioncache[$cm->course][$cm->id] = $data;
         // reset modinfo for user (no need to call rebuild_course_cache())
         get_fast_modinfo($cm->course, 0, true);
     }
 }
コード例 #3
0
ファイル: api.php プロジェクト: gabrielrosset/moodle
 /**
  * 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);
         }
     }
 }