/** * Observer for role_assigned event. * * @param \core\event\role_assigned $event * @return void */ public static function role_assigned(\core\event\role_assigned $event) { global $CFG, $DB; $context = context::instance_by_id($event->contextid, MUST_EXIST); // If contextlevel is course then only subscribe user. Role assignment // at course level means user is enroled in course and can subscribe to forum. if ($context->contextlevel != CONTEXT_COURSE) { return; } // Forum lib required for the constant used below. require_once $CFG->dirroot . '/mod/forum/lib.php'; $userid = $event->relateduserid; $sql = "SELECT f.id, cm.id AS cmid\n FROM {forum} f\n JOIN {course_modules} cm ON (cm.instance = f.id)\n JOIN {modules} m ON (m.id = cm.module)\n LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)\n WHERE f.course = :courseid\n AND f.forcesubscribe = :initial\n AND m.name = 'forum'\n AND fs.id IS NULL"; $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_INITIALSUBSCRIBE); $forums = $DB->get_records_sql($sql, $params); foreach ($forums as $forum) { // If user doesn't have allowforcesubscribe capability then don't subscribe. if (has_capability('mod/forum:allowforcesubscribe', context_module::instance($forum->cmid), $userid)) { forum_subscribe($userid, $forum->id); } } }
/** * This function gets run whenever user is assigned role in course * * @param stdClass $cp * @return void */ function forum_user_role_assigned($cp) { global $DB; $context = context::instance_by_id($cp->contextid, MUST_EXIST); // If contextlevel is course then only subscribe user. Role assignment // at course level means user is enroled in course and can subscribe to forum. if ($context->contextlevel != CONTEXT_COURSE) { return; } $sql = "SELECT f.id, cm.id AS cmid FROM {forum} f JOIN {course_modules} cm ON (cm.instance = f.id) JOIN {modules} m ON (m.id = cm.module) LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid) WHERE f.course = :courseid AND f.forcesubscribe = :initial AND m.name = 'forum' AND fs.id IS NULL"; $params = array('courseid'=>$context->instanceid, 'userid'=>$cp->userid, 'initial'=>FORUM_INITIALSUBSCRIBE); $forums = $DB->get_records_sql($sql, $params); foreach ($forums as $forum) { // If user doesn't have allowforcesubscribe capability then don't subscribe. if (has_capability('mod/forum:allowforcesubscribe', context_module::instance($forum->cmid), $cp->userid)) { forum_subscribe($cp->userid, $forum->id); } } }
} // Can proceed now, the user is not guest and is enrolled foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) { $forum = $forums[$forumid]; $modcontext = context_module::instance($cm->id); $cansub = false; if (has_capability('mod/forum:viewdiscussion', $modcontext)) { $cansub = true; } if ($cansub && $cm->visible == 0 && !has_capability('mod/forum:managesubscriptions', $modcontext)) { $cansub = false; } if (!forum_is_forcesubscribed($forum)) { $subscribed = forum_is_subscribed($USER->id, $forum); if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) { forum_subscribe($USER->id, $forumid, $modcontext); } else { if (!$subscribe && $subscribed) { forum_unsubscribe($USER->id, $forumid, $modcontext); } } } } $returnto = forum_go_back_to("index.php?id={$course->id}"); $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); if ($subscribe) { redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1); } else { redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1); } }
/** * Add subscriptions for new users * * @global object * @uses CONTEXT_SYSTEM * @uses CONTEXT_COURSE * @uses CONTEXT_COURSECAT * @uses FORUM_INITIALSUBSCRIBE * @param int $userid * @param object $context * @return bool */ function forum_add_user_default_subscriptions($userid, $context) { global $DB; if (empty($context->contextlevel)) { return false; } switch ($context->contextlevel) { case CONTEXT_SYSTEM: // For the whole site $rs = $DB->get_recordset('course',null,'','id'); foreach ($rs as $course) { $subcontext = get_context_instance(CONTEXT_COURSE, $course->id); forum_add_user_default_subscriptions($userid, $subcontext); } $rs->close(); break; case CONTEXT_COURSECAT: // For a whole category $rs = $DB->get_recordset('course', array('category' => $context->instanceid),'','id'); foreach ($rs as $course) { $subcontext = get_context_instance(CONTEXT_COURSE, $course->id); forum_add_user_default_subscriptions($userid, $subcontext); } $rs->close(); if ($categories = $DB->get_records('course_categories', array('parent' => $context->instanceid))) { foreach ($categories as $category) { $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id); forum_add_user_default_subscriptions($userid, $subcontext); } } break; case CONTEXT_COURSE: // For a whole course if (is_enrolled($context, $userid)) { if ($course = $DB->get_record('course', array('id' => $context->instanceid))) { if ($forums = get_all_instances_in_course('forum', $course, $userid, false)) { foreach ($forums as $forum) { if ($forum->forcesubscribe != FORUM_INITIALSUBSCRIBE) { continue; } if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) { if (has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) { forum_subscribe($userid, $forum->id); } } } } } } break; case CONTEXT_MODULE: // Just one forum if (has_capability('mod/forum:initialsubscriptions', $context, $userid)) { if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) { if ($forum = $DB->get_record('forum', array('id' => $cm->instance))) { if ($forum->forcesubscribe != FORUM_INITIALSUBSCRIBE) { continue; } if (has_capability('mod/forum:viewdiscussion', $context, $userid)) { forum_subscribe($userid, $forum->id); } } } } break; } return true; }
unset($learningforums[$forum->keyreference]); } } break; } } } /// Do course wide subscribe/unsubscribe if (!is_null($subscribe) && !isguest()) { $allforums = array_merge($generalforums, $learningforums); if ($allforums) { foreach ($allforums as $forum) { if (!forum_is_forcesubscribed($forum->id)) { $subscribed = forum_is_subscribed($USER->id, $forum->id); if ($subscribe && !$subscribed) { forum_subscribe($USER->id, $forum->id); } elseif (!$subscribe && $subscribed) { forum_unsubscribe($USER->id, $forum->id); } } } } $returnto = forum_go_back_to("index.php?id={$course->id}"); if ($subscribe) { add_to_log($course->id, "forum", "subscribeall", "index.php?id={$course->id}", $course->id); redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname)), 1); } else { add_to_log($course->id, "forum", "unsubscribeall", "index.php?id={$course->id}", $course->id); redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname)), 1); } }
/** * This function gets run whenever user is enrolled into course * * @param stdClass $cp * @return void */ function forum_user_enrolled($cp) { global $DB; // NOTE: this has to be as fast as possible - we do not want to slow down enrolments! // Originally there used to be 'mod/forum:initialsubscriptions' which was // introduced because we did not have enrolment information in earlier versions... $sql = "SELECT f.id FROM {forum} f LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid) WHERE f.course = :courseid AND f.forcesubscribe = :initial AND fs.id IS NULL"; $params = array('courseid'=>$cp->courseid, 'userid'=>$cp->userid, 'initial'=>FORUM_INITIALSUBSCRIBE); $forums = $DB->get_records_sql($sql, $params); foreach ($forums as $forum) { forum_subscribe($cp->userid, $forum->id); } }
/// We are in editing mode. $strexistingsubscribers = get_string("existingsubscribers", 'forum'); $strpotentialsubscribers = get_string("potentialsubscribers", 'forum'); $straddsubscriber = get_string("addsubscriber", 'forum'); $strremovesubscriber = get_string("removesubscriber", 'forum'); $strsearch = get_string("search"); $strsearchresults = get_string("searchresults"); $strshowall = get_string("showall"); $strsubscribers = get_string("subscribers", "forum"); $strforums = get_string("forums", "forum"); $searchtext = optional_param('searchtext', '', PARAM_RAW); if ($frm = data_submitted()) { /// A form was submitted so process the input if (!empty($frm->add) and !empty($frm->addselect)) { foreach ($frm->addselect as $addsubscriber) { if (!forum_subscribe($addsubscriber, $id)) { print_error('cannotaddsubscriber', 'forum', '', $addsubscriber); } } } else { if (!empty($frm->remove) and !empty($frm->removeselect)) { foreach ($frm->removeselect as $removesubscriber) { if (!forum_unsubscribe($removesubscriber, $id)) { print_error('cannotremovesubscriber', 'forum', '', $removesubscriber); } } } else { if (!empty($frm->showall)) { $searchtext = ''; } }
forum_forcesubscribe($forum->id, 1); redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1); } } if (forum_is_forcesubscribed($forum)) { redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1); } $info->name = fullname($user); $info->forum = format_string($forum->name); if (forum_is_subscribed($user->id, $forum->id)) { if (forum_unsubscribe($user->id, $forum->id)) { add_to_log($course->id, "forum", "unsubscribe", "view.php?f={$forum->id}", $forum->id, $cm->id); redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1); } else { print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } } else { // subscribe if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !has_capability('mod/forum:managesubscriptions', $context)) { print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } if (!has_capability('mod/forum:viewdiscussion', $context)) { print_error('cannotsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } if (forum_subscribe($user->id, $forum->id)) { add_to_log($course->id, "forum", "subscribe", "view.php?f={$forum->id}", $forum->id, $cm->id); redirect($returnto, get_string("nowsubscribed", "forum", $info), 1); } else { print_error('cannotsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } }
/** * Test that toggling the forum-level subscription for a different user does not affect their discussion-level * subscriptions. */ public function test_forum_subscribe_toggle_as_other() { global $DB; $this->resetAfterTest(true); // Create a course, with a forum. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $forum = $this->getDataGenerator()->create_module('forum', $options); // Create a user enrolled in the course as a student. list($author) = $this->helper_create_users($course, 1); // Post a discussion to the forum. list($discussion, $post) = $this->helper_post_to_forum($forum, $author); // Check that the user is currently not subscribed to the forum. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum)); // Check the deprecated function too. $this->assertFalse(forum_is_subscribed($author->id, $forum)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // Check that the user is unsubscribed from the discussion too. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id)); // Check that we have no records in either of the subscription tables. $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion // subscriptions table. \mod_forum\subscriptions::subscribe_user($author->id, $forum); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum // discussion subscriptions table. \mod_forum\subscriptions::unsubscribe_user($author->id, $forum); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // The same thing should happen calling the deprecated versions of // these functions. // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion // subscriptions table. forum_subscribe($author->id, $forum->id); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum // discussion subscriptions table. forum_unsubscribe($author->id, $forum->id); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Enroling the user in the discussion should add one record to the forum discussion table without modifying the // form subscriptions. \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum // discussion subscriptions table. \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Re-subscribe to the discussion so that we can check the effect of forum-level subscriptions. \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Subscribing to the forum should have no effect on the forum discussion subscriptions table if the user did // not request the change themself. \mod_forum\subscriptions::subscribe_user($author->id, $forum); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing from the forum should have no effect on the forum discussion subscriptions table if the user // did not request the change themself. \mod_forum\subscriptions::unsubscribe_user($author->id, $forum); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Subscribing to the forum should remove the per-discussion subscription preference if the user requested the // change themself. \mod_forum\subscriptions::subscribe_user($author->id, $forum, null, true); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Now unsubscribe from the current discussion whilst being subscribed to the forum as a whole. \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing from the forum should remove the per-discussion subscription preference if the user requested the // change themself. \mod_forum\subscriptions::unsubscribe_user($author->id, $forum, null, true); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Subscribe to the discussion. \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Subscribe to the forum without removing the discussion preferences. \mod_forum\subscriptions::subscribe_user($author->id, $forum); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); // Unsubscribing from the discussion should result in a change. \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id))); }
$options = array('forumid' => $forum->id, 'currentgroup' => $currentgroup, 'context' => $context); $existingselector = new forum_existing_subscriber_selector('existingsubscribers', $options); $subscriberselector = new forum_potential_subscriber_selector('potentialsubscribers', $options); $subscriberselector->set_existing_subscribers($existingselector->find_users('')); if (data_submitted()) { require_sesskey(); $subscribe = (bool) optional_param('subscribe', false, PARAM_RAW); $unsubscribe = (bool) optional_param('unsubscribe', false, PARAM_RAW); /** It has to be one or the other, not both or neither */ if (!($subscribe xor $unsubscribe)) { print_error('invalidaction'); } if ($subscribe) { $users = $subscriberselector->get_selected_users(); foreach ($users as $user) { if (!forum_subscribe($user->id, $id)) { print_error('cannotaddsubscriber', 'forum', '', $user->id); } } } else { if ($unsubscribe) { $users = $existingselector->get_selected_users(); foreach ($users as $user) { if (!forum_unsubscribe($user->id, $id)) { print_error('cannotremovesubscriber', 'forum', '', $user->id); } } } } $subscriberselector->invalidate_selected_users(); $existingselector->invalidate_selected_users();