/** * Hook the add/edit of the course module. * * @param stdClass $data Data from the form submission. * @param stdClass $course The course. */ function tool_lp_coursemodule_edit_post_actions($data, $course) { if (!get_config('core_competency', 'enabled')) { return $data; } // It seems like the form did not contain any of the form fields, we can return. if (!isset($data->competency_rule) && !isset($data->competencies)) { return $data; } // We bypass the API here and go direct to the persistent layer - because we don't want to do permission // checks here - we need to load the real list of existing course module competencies. $existing = \core_competency\course_module_competency::list_course_module_competencies($data->coursemodule); $existingids = array(); foreach ($existing as $cmc) { array_push($existingids, $cmc->get_competencyid()); } $newids = isset($data->competencies) ? $data->competencies : array(); $removed = array_diff($existingids, $newids); $added = array_diff($newids, $existingids); foreach ($removed as $removedid) { \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid); } foreach ($added as $addedid) { \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid); } if (isset($data->competency_rule)) { // Now update the rules for each course_module_competency. $current = \core_competency\api::list_course_module_competencies_in_course_module($data->coursemodule); foreach ($current as $coursemodulecompetency) { \core_competency\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule); } } return $data; }
/** * 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); } } }