/** * Test that after toggling the twf subscription as another user, * the discussion subscription functionality works as expected. */ public function test_twf_subscribe_toggle_as_other_repeat_subscriptions() { global $DB; $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $twf = $this->getDataGenerator()->create_module('twf', $options); // Create a user enrolled in the course as a student. list($user) = $this->helper_create_users($course, 1); // Post a discussion to the twf. list($discussion, $post) = $this->helper_post_to_twf($twf, $user); // Confirm that the user is currently not subscribed to the twf. $this->assertFalse(\mod_twf\subscriptions::is_subscribed($user->id, $twf)); // Confirm that the user is unsubscribed from the discussion too. $this->assertFalse(\mod_twf\subscriptions::is_subscribed($user->id, $twf, $discussion->id)); // Confirm that we have no records in either of the subscription tables. $this->assertEquals(0, $DB->count_records('twf_subscriptions', array('userid' => $user->id, 'twf' => $twf->id))); $this->assertEquals(0, $DB->count_records('twf_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); // Subscribing to the twf should create a record in the subscriptions table, but not the twf discussion // subscriptions table. \mod_twf\subscriptions::subscribe_user($user->id, $twf); $this->assertEquals(1, $DB->count_records('twf_subscriptions', array('userid' => $user->id, 'twf' => $twf->id))); $this->assertEquals(0, $DB->count_records('twf_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); // Now unsubscribe from the discussion. This should return true. $this->assertTrue(\mod_twf\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion)); // Attempting to unsubscribe again should return false because no change was made. $this->assertFalse(\mod_twf\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_twf\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); // Attempting to subscribe again should return false because no change was made. $this->assertFalse(\mod_twf\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); // Now unsubscribe from the discussion. This should return true once more. $this->assertTrue(\mod_twf\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion)); // And unsubscribing from the twf but not as a request from the user should maintain their preference. \mod_twf\subscriptions::unsubscribe_user($user->id, $twf); $this->assertEquals(0, $DB->count_records('twf_subscriptions', array('userid' => $user->id, 'twf' => $twf->id))); $this->assertEquals(1, $DB->count_records('twf_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); // Subscribing to the discussion should return truthfully because a change was made. $this->assertTrue(\mod_twf\subscriptions::subscribe_user_to_discussion($user->id, $discussion)); $this->assertEquals(0, $DB->count_records('twf_subscriptions', array('userid' => $user->id, 'twf' => $twf->id))); $this->assertEquals(1, $DB->count_records('twf_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id))); }
// Ensure they are subscribed to the discussion still. $subscriptionchanges[$userid] = $subscriptiontime; } } } $DB->set_field('twf_discussions', 'twf', $twfto->id, array('id' => $discussion->id)); $DB->set_field('twf_read', 'twfid', $twfto->id, array('discussionid' => $discussion->id)); // Delete the existing per-discussion subscriptions and replace them with the newly calculated ones. $DB->delete_records('twf_discussion_subs', array('discussion' => $discussion->id)); $newdiscussion = clone $discussion; $newdiscussion->twf = $twfto->id; foreach ($subscriptionchanges as $userid => $preference) { if ($preference != \mod_twf\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED) { // Users must have viewdiscussion to a discussion. if (has_capability('mod/twf:viewdiscussion', $destinationctx, $userid)) { \mod_twf\subscriptions::subscribe_user_to_discussion($userid, $newdiscussion, $destinationctx); } } else { \mod_twf\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx); } } $params = array('context' => $destinationctx, 'objectid' => $discussion->id, 'other' => array('fromtwfid' => $twf->id, 'totwfid' => $twfto->id)); $event = \mod_twf\event\discussion_moved::create($params); $event->add_record_snapshot('twf_discussions', $discussion); $event->add_record_snapshot('twf', $twf); $event->add_record_snapshot('twf', $twfto); $event->trigger(); // Delete the RSS files for the 2 twfs to force regeneration of the feeds require_once $CFG->dirroot . '/mod/twf/rsslib.php'; twf_rss_delete_file($twf); twf_rss_delete_file($twfto);
$includetext = optional_param('includetext', false, PARAM_BOOL); $twf = $DB->get_record('twf', array('id' => $twfid), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST); $discussion = $DB->get_record('twf_discussions', array('id' => $discussionid), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST); $context = context_module::instance($cm->id); require_sesskey(); require_login($course, false, $cm); require_capability('mod/twf:viewdiscussion', $context); $return = new stdClass(); if (is_guest($context, $USER)) { // is_guest should be used here as this also checks whether the user is a guest in the current course. // Guests and visitors cannot subscribe - only enrolled users. throw new moodle_exception('noguestsubscribe', 'mod_twf'); } if (!\mod_twf\subscriptions::is_subscribable($twf)) { // Nothing to do. We won't actually output any content here though. echo json_encode($return); die; } if (\mod_twf\subscriptions::is_subscribed($USER->id, $twf, $discussion->id, $cm)) { // The user is subscribed, unsubscribe them. \mod_twf\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context); } else { // The user is unsubscribed, subscribe them. \mod_twf\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context); } // Now return the updated subscription icon. $return->icon = twf_get_discussion_subscription_icon($twf, $discussion->id, null, $includetext); echo json_encode($return); die;
/** * Test that a user unsubscribed from a twf who has subscribed to a discussion, only receives posts made after * they subscribed to the discussion. */ public function test_twf_discussion_subscription_twf_unsubscribed_discussion_subscribed_after_post() { $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $twf = $this->getDataGenerator()->create_module('twf', $options); $expectedmessages = array(); // Create a user enrolled in the course as a student. list($author) = $this->helper_create_users($course, 1); // Post a discussion to the twf. list($discussion, $post) = $this->helper_post_to_twf($twf, $author); $this->helper_update_post_time($post, -90); $expectedmessages[] = array('id' => $post->id, 'subject' => $post->subject, 'count' => 0); // Then subscribe the user to the discussion. $this->assertTrue(\mod_twf\subscriptions::subscribe_user_to_discussion($author->id, $discussion)); $this->helper_update_subscription_time($author, $discussion, -60); // Then post a reply to the first discussion. $reply = $this->helper_post_to_discussion($twf, $discussion, $author); $this->helper_update_post_time($reply, -30); $expectedmessages[] = array('id' => $reply->id, 'subject' => $reply->subject, 'count' => 1); $expectedcount = 1; // Run cron and check that the expected number of users received the notification. $messages = $this->helper_run_cron_check_counts($expectedmessages, $expectedcount); }
/** * Test that the correct context is used in the events when subscribing * users. */ public function test_twf_subscription_page_context_valid() { global $CFG, $PAGE; require_once $CFG->dirroot . '/mod/twf/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); $twf = $this->getDataGenerator()->create_module('twf', $options); $quiz = $this->getDataGenerator()->create_module('quiz', $options); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['twf'] = $twf->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record); // Add a post. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_post($record); // Set up the default page event to use this twf. $PAGE = new moodle_page(); $cm = get_coursemodule_from_instance('twf', $discussion->twf); $context = \context_module::instance($cm->id); $PAGE->set_context($context); $PAGE->set_cm($cm, $course, $twf); // Trigger and capturing the event. $sink = $this->redirectEvents(); // Trigger the event by subscribing the user to the twf. \mod_twf\subscriptions::subscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the twf. \mod_twf\subscriptions::unsubscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_twf\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_twf\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_twf\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_twf\\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 twf. \mod_twf\subscriptions::subscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the twf. \mod_twf\subscriptions::unsubscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_twf\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_twf\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_twf\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_twf\\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 twf. \mod_twf\subscriptions::subscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user to the twf. \mod_twf\subscriptions::unsubscribe_user($user->id, $twf); $events = $sink->get_events(); $sink->clear(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by subscribing the user to the discussion. \mod_twf\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_twf\\event\\discussion_subscription_created', $event); $this->assertEquals($context, $event->get_context()); // Trigger the event by unsubscribing the user from the discussion. \mod_twf\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_twf\\event\\discussion_subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); }
/** * Given a new post, subscribes or unsubscribes as appropriate. * Returns some text which describes what happened. * * @param object $fromform The submitted form * @param stdClass $twf The twf record * @param stdClass $discussion The twf discussion record * @return string */ function twf_post_subscription($fromform, $twf, $discussion) { global $USER; if (\mod_twf\subscriptions::is_forcesubscribed($twf)) { return ""; } else { if (\mod_twf\subscriptions::subscription_disabled($twf)) { $subscribed = \mod_twf\subscriptions::is_subscribed($USER->id, $twf); if ($subscribed && !has_capability('moodle/course:manageactivities', context_course::instance($twf->course), $USER->id)) { // This user should not be subscribed to the twf. \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf); } return ""; } } $info = new stdClass(); $info->name = fullname($USER); $info->discussion = format_string($discussion->name); $info->twf = format_string($twf->name); if (isset($fromform->discussionsubscribe) && $fromform->discussionsubscribe) { if ($result = \mod_twf\subscriptions::subscribe_user_to_discussion($USER->id, $discussion)) { return html_writer::tag('p', get_string('discussionnowsubscribed', 'twf', $info)); } } else { if ($result = \mod_twf\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion)) { return html_writer::tag('p', get_string('discussionnownotsubscribed', 'twf', $info)); } } return ''; }