From 9e533215bd7148de8fad269dc7adc441d6d3a696 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 31 Mar 2014 10:13:44 +0800 Subject: [PATCH] MDL-44627 events: Triggering event course module updated actions. --- course/lib.php | 16 ++++- course/mod.php | 5 +- course/modlib.php | 15 +---- course/rest.php | 4 ++ course/tests/courselib_test.php | 70 +++++++++++++++++++++ lib/classes/event/course_module_updated.php | 27 ++++++++ 6 files changed, 121 insertions(+), 16 deletions(-) diff --git a/course/lib.php b/course/lib.php index 914e5fe35ff..258e965ffbe 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1208,7 +1208,7 @@ function set_section_visible($courseid, $sectionnumber, $visibility) { if (!empty($section->sequence)) { $modules = explode(",", $section->sequence); foreach ($modules as $moduleid) { - if ($cm = $DB->get_record('course_modules', array('id' => $moduleid), 'visible, visibleold')) { + if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) { if ($visibility) { // As we unhide the section, we use the previously saved visibility stored in visibleold. set_coursemodule_visible($moduleid, $cm->visibleold); @@ -1217,6 +1217,7 @@ function set_section_visible($courseid, $sectionnumber, $visibility) { set_coursemodule_visible($moduleid, 0); $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid)); } + \core\event\course_module_updated::create_from_cm($cm)->trigger(); } } } @@ -1518,6 +1519,16 @@ function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod = return $section->id; // Return course_sections ID that was used. } +/** + * Change the group mode of a course module. + * + * Note: Do not forget to trigger the event \core\event\course_module_updated as it needs + * to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}. + * + * @param int $id course module ID. + * @param int $groupmode the new groupmode value. + * @return bool True if the $groupmode was updated. + */ function set_coursemodule_groupmode($id, $groupmode) { global $DB; $cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,groupmode', MUST_EXIST); @@ -1541,6 +1552,9 @@ function set_coursemodule_idnumber($id, $idnumber) { /** * Set the visibility of a module and inherent properties. * + * Note: Do not forget to trigger the event \core\event\course_module_updated as it needs + * to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}. + * * From 2.4 the parameter $prevstateoverrides has been removed, the logic it triggered * has been moved to {@link set_section_visible()} which was the only place from which * the parameter was used. diff --git a/course/mod.php b/course/mod.php index 1ea8b78cc8c..bd01eeb6a5d 100644 --- a/course/mod.php +++ b/course/mod.php @@ -227,7 +227,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { require_capability('moodle/course:activityvisibility', $modcontext); set_coursemodule_visible($cm->id, 0); - + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else if (!empty($show) and confirm_sesskey()) { @@ -245,6 +245,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { if ($module->visible and ($section->visible or (SITEID == $cm->course))) { set_coursemodule_visible($cm->id, 1); + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); } redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn))); @@ -261,7 +262,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { require_capability('moodle/course:manageactivities', $modcontext); set_coursemodule_groupmode($cm->id, $groupmode); - + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else if (!empty($copy) and confirm_sesskey()) { // value = course module diff --git a/course/modlib.php b/course/modlib.php index 454202e505f..8ece656e681 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -523,19 +523,8 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { $completion->reset_all_state($cm); } - - // Trigger event based on the action we did. - $event = \core\event\course_module_updated::create(array( - 'courseid' => $course->id, - 'context' => $modcontext, - 'objectid' => $moduleinfo->coursemodule, - 'other' => array( - 'modulename' => $moduleinfo->modulename, - 'name' => $moduleinfo->name, - 'instanceid' => $moduleinfo->instance - ) - )); - $event->trigger(); + $cm->name = $moduleinfo->name; + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); $moduleinfo = edit_module_post_actions($moduleinfo, $course); diff --git a/course/rest.php b/course/rest.php index 1aeca1578b9..1f7f8bc2e36 100644 --- a/course/rest.php +++ b/course/rest.php @@ -104,6 +104,7 @@ switch($requestmethod) { case 'visible': require_capability('moodle/course:activityvisibility', $modcontext); set_coursemodule_visible($cm->id, $value); + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); break; case 'duplicate': @@ -121,6 +122,7 @@ switch($requestmethod) { case 'groupmode': require_capability('moodle/course:manageactivities', $modcontext); set_coursemodule_groupmode($cm->id, $value); + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); break; case 'indent': @@ -173,6 +175,8 @@ switch($requestmethod) { if (!empty($module->name)) { $DB->update_record($cm->modname, $module); + $cm->name = $module->name; + \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); rebuild_course_cache($cm->course); } else { $module->name = $cm->name; diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 79dd3ef1561..0d0571dfd0c 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -955,6 +955,32 @@ class core_course_courselib_testcase extends advanced_testcase { } } + public function test_section_visibility_events() { + $this->setAdminUser(); + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true)); + $sectionnumber = 1; + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), + array('section' => $sectionnumber)); + $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), + 'course' => $course->id), array('section' => $sectionnumber)); + $sink = $this->redirectEvents(); + set_section_visible($course->id, $sectionnumber, 0); + $events = $sink->get_events(); + + // Extract the number of events related to what we are testing, other events + // such as course_section_updated could have been triggered. + $count = 0; + foreach ($events as $event) { + if ($event instanceof \core\event\course_module_updated) { + $count++; + } + } + $this->assertSame(2, $count); + $sink->close(); + } + public function test_section_visibility() { $this->setAdminUser(); $this->resetAfterTest(true); @@ -2060,6 +2086,50 @@ class core_course_courselib_testcase extends advanced_testcase { $this->assertEventContextNotUsed($event); } + /** + * Tests for create_from_cm method. + */ + public function test_course_module_create_from_cm() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create course and modules. + $course = $this->getDataGenerator()->create_course(array('numsections' => 5)); + + // Generate an assignment. + $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); + + // Get the module context. + $modcontext = context_module::instance($assign->cmid); + + // Get course module. + $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST); + + // Create an event from course module. + $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext); + + // Trigger the events. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event2 = array_pop($events); + + // Test event data. + $this->assertInstanceOf('\core\event\course_module_updated', $event); + $this->assertEquals($cm->id, $event2->objectid); + $this->assertEquals($modcontext, $event2->get_context()); + $this->assertEquals($cm->modname, $event2->other['modulename']); + $this->assertEquals($cm->instance, $event2->other['instanceid']); + $this->assertEquals($cm->name, $event2->other['name']); + $this->assertEventContextNotUsed($event2); + $this->assertSame('mod_updated', $event2->get_legacy_eventname()); + $arr = array( + array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"), + array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id) + ); + $this->assertEventLegacyLogData($arr, $event); + } + /** * Tests for event validations related to course module update. */ diff --git a/lib/classes/event/course_module_updated.php b/lib/classes/event/course_module_updated.php index 976987b51f0..7aaaa0b6f7b 100644 --- a/lib/classes/event/course_module_updated.php +++ b/lib/classes/event/course_module_updated.php @@ -136,5 +136,32 @@ class course_module_updated extends base { throw new \coding_exception("Field other['name'] cannot be empty"); } } + + /** + * Set data to create new event from course module. + * + * @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; + } }