diff --git a/course/dnduploadlib.php b/course/dnduploadlib.php index 5d146235ce6..16d51dc8dfd 100644 --- a/course/dnduploadlib.php +++ b/course/dnduploadlib.php @@ -648,16 +648,7 @@ class dndupload_ajax_processor { $mod = $info->get_cm($this->cm->id); // Trigger course module created event. - $event = \core\event\course_module_created::create(array( - 'courseid' => $this->course->id, - 'context' => context_module::instance($mod->id), - 'objectid' => $mod->id, - 'other' => array( - 'modulename' => $mod->modname, - 'name' => $mod->name, - 'instanceid' => $instanceid - ) - )); + $event = \core\event\course_module_created::create_from_cm($mod); $event->trigger(); $this->send_response($mod); diff --git a/course/modlib.php b/course/modlib.php index 8ece656e681..5fd80e2a002 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -148,16 +148,11 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) { $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section); // Trigger event based on the action we did. - $event = \core\event\course_module_created::create(array( - 'courseid' => $course->id, - 'context' => $modcontext, - 'objectid' => $moduleinfo->coursemodule, - 'other' => array( - 'modulename' => $moduleinfo->modulename, - 'name' => $moduleinfo->name, - 'instanceid' => $moduleinfo->instance - ) - )); + // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it. + $eventdata = clone $moduleinfo; + $eventdata->modname = $eventdata->modulename; + $eventdata->id = $eventdata->coursemodule; + $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext); $event->trigger(); $moduleinfo = edit_module_post_actions($moduleinfo, $course); diff --git a/lib/classes/event/course_module_created.php b/lib/classes/event/course_module_created.php index c8eb8581432..e481ec1de6f 100644 --- a/lib/classes/event/course_module_created.php +++ b/lib/classes/event/course_module_created.php @@ -54,6 +54,35 @@ class course_module_created extends base { $this->data['edulevel'] = self::LEVEL_TEACHING; } + /** + * Api to Create new event from course module. + * + * @since Moodle 2.6.4, 2.7.1 + * @param \cm_info|\stdClass $cm course module instance, as returned by {@link get_coursemodule_from_id} + * or {@link get_coursemodule_from_instance}. + * @param \context_module $modcontext module context instance + * + * @return \core\event\base returns instance of new event + */ + public static final function create_from_cm($cm, $modcontext = null) { + // If not set, get the module context. + if (empty($modcontext)) { + $modcontext = \context_module::instance($cm->id); + } + + // Create event object for course module update action. + $event = static::create(array( + 'context' => $modcontext, + 'objectid' => $cm->id, + 'other' => array( + 'modulename' => $cm->modname, + 'instanceid' => $cm->instance, + 'name' => $cm->name, + ) + )); + return $event; + } + /** * Returns localised general event name. *