MDL-45075 mod_forum: Move user subscription to an observer

The forum subscription function is called in some cases on forum creation
(specifically the case of an automatically subscribed forum).

Since the forum subscription requires both the course_module instance, and
the context these must both be present when subscribing users to the forum,
however these are not fully set up until after the forum has been created.

By moving the subscription to a subscriber, we are able to hook into the
module creation event after the course_module and context have been linked
to the forum instance.
This commit is contained in:
Andrew Nicols
2014-04-22 11:50:52 +08:00
parent 23a9a5632c
commit 8e6775d8f9
4 changed files with 40 additions and 8 deletions
+18
View File
@@ -91,4 +91,22 @@ class mod_forum_observer {
}
}
}
/**
* Observer for \core\event\course_module_created event.
*
* @param \core\event\course_module_created $event
* @return void
*/
public static function course_module_created(\core\event\course_module_created $event) {
global $CFG;
if ($event->other['modulename'] === 'forum') {
// Include the forum library to make use of the forum_instance_created function.
require_once($CFG->dirroot . '/mod/forum/lib.php');
$forum = $event->get_record_snapshot('forum', $event->other['instanceid']);
forum_instance_created($event->get_context(), $forum);
}
}
}
+5
View File
@@ -35,4 +35,9 @@ $observers = array(
'eventname' => '\core\event\role_assigned',
'callback' => 'mod_forum_observer::role_assigned'
),
array(
'eventname' => '\core\event\course_module_created',
'callback' => 'mod_forum_observer::course_module_created',
),
);
+16 -7
View File
@@ -126,18 +126,27 @@ function forum_add_instance($forum, $mform = null) {
}
}
if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) {
$users = forum_get_potential_subscribers($modcontext, 0, 'u.id, u.email');
foreach ($users as $user) {
forum_subscribe($user->id, $forum->id);
}
}
forum_grade_item_update($forum);
return $forum->id;
}
/**
* Handle changes following the creation of a forum instance.
* This function is typically called by the course_module_created observer.
*
* @param object $context the forum context
* @param stdClass $forum The forum object
* @return void
*/
function forum_instance_created($context, $forum) {
if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) {
$users = forum_get_potential_subscribers($context, 0, 'u.id, u.email');
foreach ($users as $user) {
forum_subscribe($user->id, $forum->id);
}
}
}
/**
* Given an object containing all the necessary data,
+1 -1
View File
@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014040400; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2014042200; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2013110500; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 60;