Skip to content

Commit

Permalink
MDL-44627 events: Triggering event course module updated actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Apr 28, 2014
1 parent a046134 commit 9e53321
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 16 deletions.
16 changes: 15 additions & 1 deletion course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions course/mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
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()) {
Expand All @@ -245,6 +245,7 @@

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)));
Expand All @@ -261,7 +262,7 @@
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
Expand Down
15 changes: 2 additions & 13 deletions course/modlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions course/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
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':
Expand All @@ -121,6 +122,7 @@
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':
Expand Down Expand Up @@ -173,6 +175,8 @@

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;
Expand Down
70 changes: 70 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,32 @@ public function test_module_visibility() {
}
}

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);
Expand Down Expand Up @@ -2060,6 +2086,50 @@ public function test_course_module_updated_event() {
$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.
*/
Expand Down
27 changes: 27 additions & 0 deletions lib/classes/event/course_module_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,32 @@ protected function validate_data() {
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;
}
}

0 comments on commit 9e53321

Please sign in to comment.