Skip to content

Commit

Permalink
Merge branch 'MDL-39883-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 23, 2014
2 parents fbdb649 + dcbef3f commit b362c8f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
11 changes: 1 addition & 10 deletions course/dnduploadlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,7 @@ protected function finish_setup_course_module($instanceid) {
$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);
Expand Down
4 changes: 4 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3530,6 +3530,10 @@ function duplicate_module($course, $cm) {
$section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
moveto_module($newcm, $section, $cm);
moveto_module($cm, $section, $newcm);

// Trigger course module created event. We can trigger the event only if we know the newcmid.
$event = \core\event\course_module_created::create_from_cm($newcm);
$event->trigger();
}
rebuild_course_cache($cm->course);

Expand Down
15 changes: 5 additions & 10 deletions course/modlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1958,9 +1958,8 @@ public function test_course_module_created_event() {
$modinfo = $this->create_specific_module_test('assign');
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();

$cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
$cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
$mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);

// Validate event data.
Expand Down Expand Up @@ -1988,6 +1987,21 @@ public function test_course_module_created_event() {
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);

// Let us see if duplicating an activity results in a nice course module created event.
$sink->clear();
$course = get_course($mod->course);
$newcm = duplicate_module($course, $cm);
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();

// Validate event data.
$this->assertInstanceOf('\core\event\course_module_created', $event);
$this->assertEquals($newcm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($course->id, $event->courseid);
$url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
$this->assertEquals($url, $event->get_url());
}

/**
Expand Down
29 changes: 29 additions & 0 deletions lib/classes/event/course_module_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ protected function init() {
$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.
*
Expand Down

0 comments on commit b362c8f

Please sign in to comment.