$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 (!\mod_forum\subscriptions::is_forcesubscribed($forum)) { $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm); $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id); if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) { \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true); } else { if (!$subscribe && $subscribed) { \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true); } } } } $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); } } /// First, let's process the general forums and build up a display if ($generalforums) { foreach ($generalforums as $forum) {
/** * Given a new post, subscribes or unsubscribes as appropriate. * Returns some text which describes what happened. * * @param object $fromform The submitted form * @param stdClass $forum The forum record * @param stdClass $discussion The forum discussion record * @return string */ function forum_post_subscription($fromform, $forum, $discussion) { global $USER; if (\mod_forum\subscriptions::is_forcesubscribed($forum)) { return ""; } else { if (\mod_forum\subscriptions::subscription_disabled($forum)) { $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum); if ($subscribed && !has_capability('moodle/course:manageactivities', context_course::instance($forum->course), $USER->id)) { // This user should not be subscribed to the forum. \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum); } return ""; } } $info = new stdClass(); $info->name = fullname($USER); $info->forum = format_string($forum->name); if ($fromform->discussionsubscribe) { if ($result = \mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion)) { return html_writer::tag('p', get_string('nowsubscribed', 'forum', $info)); } } else { if ($result = \mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion)) { return html_writer::tag('p', get_string('nownotsubscribed', 'forum', $info)); } } return ''; }
/** * Test that the correct context is used in the events when subscribing * users. */ public function test_forum_subscription_page_context_valid() { global $CFG, $PAGE; require_once $CFG->dirroot . '/mod/forum/lib.php'; // Setup test data. $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $forum = $this->getDataGenerator()->create_module('forum', $options); $quiz = $this->getDataGenerator()->create_module('quiz', $options); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add a post. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // Set up the default page event to use this forum. $PAGE = new moodle_page(); $cm = get_coursemodule_from_instance('forum', $discussion->forum); $context = \context_module::instance($cm->id); $PAGE->set_context($context); $PAGE->set_cm($cm, $course, $forum); // Trigger and capturing the event. $sink = $this->redirectEvents(); // Trigger the event by subscribing the user to the forum. \mod_forum\subscriptions::subscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the forum. \mod_forum\subscriptions::unsubscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Now try with the context for a different module (quiz). $PAGE = new moodle_page(); $cm = get_coursemodule_from_instance('quiz', $quiz->id); $quizcontext = \context_module::instance($cm->id); $PAGE->set_context($quizcontext); $PAGE->set_cm($cm, $course, $quiz); // Trigger and capturing the event. $sink = $this->redirectEvents(); // Trigger the event by subscribing the user to the forum. \mod_forum\subscriptions::subscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the forum. \mod_forum\subscriptions::unsubscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Now try with the course context - the module context should still be used. $PAGE = new moodle_page(); $coursecontext = \context_course::instance($course->id); $PAGE->set_context($coursecontext); // Trigger the event by subscribing the user to the forum. \mod_forum\subscriptions::subscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the forum. \mod_forum\subscriptions::unsubscribe_user($user->id, $forum); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); }
public function test_automatic_with_subscribed_discussion_in_unsubscribed_forum() { $this->resetAfterTest(true); // Create a course, with a forum. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); $forum = $this->getDataGenerator()->create_module('forum', $options); // Create two users enrolled in the course as students. list($author, $recipient) = $this->helper_create_users($course, 2); // Post a discussion to the forum. list($discussion, $post) = $this->helper_post_to_forum($forum, $author); $this->helper_update_post_time($post, -90); // Unsubscribe the 'author' user from the forum. \mod_forum\subscriptions::unsubscribe_user($author->id, $forum); // Then re-subscribe them to the discussion. \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion); $this->helper_update_subscription_time($author, $discussion, -60); // We expect just the user subscribed to the forum to receive this post at the moment as the discussion // subscription time is after the post time. $expected = 1; // Run cron and check that the expected number of users received the notification. $messages = $this->helper_run_cron_check_count($post, $expected); $seenauthor = false; $seenrecipient = false; foreach ($messages as $message) { // They should both be from our user. $this->assertEquals($author->id, $message->useridfrom); if ($message->useridto == $author->id) { $seenauthor = true; } else { if ($message->useridto = $recipient->id) { $seenrecipient = true; } } } // Check we only saw one user. $this->assertFalse($seenauthor); $this->assertTrue($seenrecipient); // Now post a reply to the original post. $reply = $this->helper_post_to_discussion($forum, $discussion, $author); $this->helper_update_post_time($reply, -30); // We expect two users to receive this post. $expected = 2; // Run cron and check that the expected number of users received the notification. $messages = $this->helper_run_cron_check_count($reply, $expected); $seenauthor = false; $seenrecipient = false; foreach ($messages as $message) { // They should both be from our user. $this->assertEquals($author->id, $message->useridfrom); if ($message->useridto == $author->id) { $seenauthor = true; } else { if ($message->useridto = $recipient->id) { $seenrecipient = true; } } } // Check we saw both users. $this->assertTrue($seenauthor); $this->assertTrue($seenrecipient); }
/** * Test that after toggling the forum subscription as another user, * the discussion subscription functionality works as expected. */ public function test_forum_subscribe_toggle_as_other_repeat_subscriptions() { 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($user) = $this->helper_create_users($course, 1); // Post a discussion to the forum. list($discussion, $post) = $this->helper_post_to_forum($forum, $user); // Confirm that the user is currently not subscribed to the forum. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum)); // Confirm that the user is unsubscribed from the discussion too. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id)); // Confirm that we have no records in either of the subscription tables. $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $user->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($user->id, $forum); $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id))); $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); // Now unsubscribe from the discussion. This should return true. $this->assertTrue(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion)); // Attempting to unsubscribe again should return false because no change was made. $this->assertFalse(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion)); // Subscribing to the discussion again should return truthfully as the subscription preference was removed. $this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); // Attempting to subscribe again should return false because no change was made. $this->assertFalse(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); // Now unsubscribe from the discussion. This should return true once more. $this->assertTrue(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion)); // And unsubscribing from the forum but not as a request from the user should maintain their preference. \mod_forum\subscriptions::unsubscribe_user($user->id, $forum); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); // Subscribing to the discussion should return truthfully because a change was made. $this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id))); $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); }
/** 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 (!\mod_forum\subscriptions::subscribe_user($user->id, $forum)) { print_error('cannotaddsubscriber', 'forum', '', $user->id); } } } else { if ($unsubscribe) { $users = $existingselector->get_selected_users(); foreach ($users as $user) { if (!\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) { print_error('cannotremovesubscriber', 'forum', '', $user->id); } } } } $subscriberselector->invalidate_selected_users(); $existingselector->invalidate_selected_users(); $subscriberselector->set_existing_subscribers($existingselector->find_users('')); } $strsubscribers = get_string("subscribers", "forum"); $PAGE->navbar->add($strsubscribers); $PAGE->set_title($strsubscribers); $PAGE->set_heading($COURSE->fullname); if (has_capability('mod/forum:managesubscriptions', $context)) { $PAGE->set_button(forum_update_subscriptions_button($course->id, $id));
/** * Removes user from the subscriber list * * @param int $userid * @param int $forumid * @param context_module|null $context Module context, may be omitted if not known or if called for the current module set in page. * @param boolean $userrequest Whether the user requested this change themselves. This has an effect on whether * discussion subscriptions are removed too. * @deprecated since Moodle 2.8 use \mod_forum\subscriptions::unsubscribe_user() instead */ function forum_unsubscribe($userid, $forumid, $context = null, $userrequest = false) { global $DB; debugging("forum_unsubscribe() has been deprecated, please use \\mod_forum\\subscriptions::unsubscribe_user() instead.", DEBUG_DEVELOPER); // Note: The new function does not take an integer form of forum. $forum = $DB->get_record('forum', array('id' => $forumid)); \mod_forum\subscriptions::unsubscribe_user($userid, $forum, $context, $userrequest); }
$info = new stdClass(); $info->name = fullname($user); $info->forum = format_string($forum->name); if ($issubscribed) { if (is_null($sesskey)) { // we came here via link in email $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'forum', format_string($forum->name)), new moodle_url($PAGE->url, array('sesskey' => sesskey())), new moodle_url('/mod/forum/view.php', array('f' => $id))); echo $OUTPUT->footer(); exit; } require_sesskey(); if ($discussionid === null) { if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum, $context, true)) { redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1); } else { print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } } else { $discussion = $DB->get_record('forum_discussions', array('id' => $discussionid)); if (\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) { $info->discussion = $discussion->name; redirect($returnto, get_string("discussionnownotsubscribed", "forum", $info), 1); } else { print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); } } } else { // subscribe
$PAGE->set_context(context_user::instance($USER->id)); $return = $CFG->wwwroot . '/'; if (isguestuser()) { redirect($return); } $strunsubscribeall = get_string('unsubscribeall', 'forum'); $PAGE->navbar->add(get_string('modulename', 'forum')); $PAGE->navbar->add($strunsubscribeall); $PAGE->set_title($strunsubscribeall); $PAGE->set_heading($COURSE->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading($strunsubscribeall); if (data_submitted() and $confirm and confirm_sesskey()) { $forums = \mod_forum\subscriptions::get_unsubscribable_forums(); foreach ($forums as $forum) { \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true); } $DB->set_field('user', 'autosubscribe', 0, array('id' => $USER->id)); echo $OUTPUT->box(get_string('unsubscribealldone', 'forum')); echo $OUTPUT->continue_button($return); echo $OUTPUT->footer(); die; } else { $a = count(\mod_forum\subscriptions::get_unsubscribable_forums()); if ($a) { $msg = get_string('unsubscribeallconfirm', 'forum', $a); echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm' => 1)), $return); echo $OUTPUT->footer(); die; } else { echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));