echo $OUTPUT->header(); $viewurl = new moodle_url('/mod/twf/view.php', array('f' => $id)); if ($discussionid) { $a = new stdClass(); $a->twf = format_string($twf->name); $a->discussion = format_string($discussion->name); echo $OUTPUT->confirm(get_string('confirmunsubscribediscussion', 'twf', $a), $PAGE->url, $viewurl); } else { echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'twf', format_string($twf->name)), $PAGE->url, $viewurl); } echo $OUTPUT->footer(); exit; } require_sesskey(); if ($discussionid === null) { if (\mod_twf\subscriptions::unsubscribe_user($user->id, $twf, $context, true)) { redirect($returnto, get_string("nownotsubscribed", "twf", $info), 1); } else { print_error('cannotunsubscribe', 'twf', get_local_referer(false)); } } else { if (\mod_twf\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) { $info->discussion = $discussion->name; redirect($returnto, get_string("discussionnownotsubscribed", "twf", $info), 1); } else { print_error('cannotunsubscribe', 'twf', get_local_referer(false)); } } } else { // subscribe if (\mod_twf\subscriptions::subscription_disabled($twf) && !has_capability('mod/twf:managesubscriptions', $context)) {
/** * 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))); }
$PAGE->set_context(context_user::instance($USER->id)); $return = $CFG->wwwroot . '/'; if (isguestuser()) { redirect($return); } $strunsubscribeall = get_string('unsubscribeall', 'twf'); $PAGE->navbar->add(get_string('modulename', 'twf')); $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()) { $twfs = \mod_twf\subscriptions::get_unsubscribable_twfs(); foreach ($twfs as $twf) { \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf, context_module::instance($twf->cm), true); } $DB->delete_records('twf_discussion_subs', array('userid' => $USER->id)); $DB->set_field('user', 'autosubscribe', 0, array('id' => $USER->id)); echo $OUTPUT->box(get_string('unsubscribealldone', 'twf')); echo $OUTPUT->continue_button($return); echo $OUTPUT->footer(); die; } else { $count = new stdClass(); $count->twfs = count(\mod_twf\subscriptions::get_unsubscribable_twfs()); $count->discussions = $DB->count_records('twf_discussion_subs', array('userid' => $USER->id)); if ($count->twfs || $count->discussions) { if ($count->twfs && $count->discussions) { $msg = get_string('unsubscribeallconfirm', 'twf', $count); } else {
public function test_automatic_with_subscribed_discussion_in_unsubscribed_twf() { $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); $twf = $this->getDataGenerator()->create_module('twf', $options); // Create two users enrolled in the course as students. list($author, $recipient) = $this->helper_create_users($course, 2); // Post a discussion to the twf. list($discussion, $post) = $this->helper_post_to_twf($twf, $author); $this->helper_update_post_time($post, -90); // Unsubscribe the 'author' user from the twf. \mod_twf\subscriptions::unsubscribe_user($author->id, $twf); // Then re-subscribe them to the discussion. \mod_twf\subscriptions::subscribe_user_to_discussion($author->id, $discussion); $this->helper_update_subscription_time($author, $discussion, -60); // We expect just the user subscribed to the twf 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($twf, $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); }
/** * Removes user from the subscriber list * * @param int $userid * @param int $twfid * @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_twf\subscriptions::unsubscribe_user() instead */ function twf_unsubscribe($userid, $twfid, $context = null, $userrequest = false) { global $DB; debugging("twf_unsubscribe() has been deprecated, please use \\mod_twf\\subscriptions::unsubscribe_user() instead.", DEBUG_DEVELOPER); // Note: The new function does not take an integer form of twf. $twf = $DB->get_record('twf', array('id' => $twfid)); \mod_twf\subscriptions::unsubscribe_user($userid, $twf, $context, $userrequest); }
/** 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_twf\subscriptions::subscribe_user($user->id, $twf)) { print_error('cannotaddsubscriber', 'twf', '', $user->id); } } } else { if ($unsubscribe) { $users = $existingselector->get_selected_users(); foreach ($users as $user) { if (!\mod_twf\subscriptions::unsubscribe_user($user->id, $twf)) { print_error('cannotremovesubscriber', 'twf', '', $user->id); } } } } $subscriberselector->invalidate_selected_users(); $existingselector->invalidate_selected_users(); $subscriberselector->set_existing_subscribers($existingselector->find_users('')); } $strsubscribers = get_string("subscribers", "twf"); $PAGE->navbar->add($strsubscribers); $PAGE->set_title($strsubscribers); $PAGE->set_heading($COURSE->fullname); if (has_capability('mod/twf:managesubscriptions', $context) && \mod_twf\subscriptions::is_forcesubscribed($twf) === false) { if ($edit != -1) {
/** * 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()); }
$modcontext = context_module::instance($cm->id); $cansub = false; if (has_capability('mod/twf:viewdiscussion', $modcontext)) { $cansub = true; } if ($cansub && $cm->visible == 0 && !has_capability('mod/twf:managesubscriptions', $modcontext)) { $cansub = false; } if (!\mod_twf\subscriptions::is_forcesubscribed($twf)) { $subscribed = \mod_twf\subscriptions::is_subscribed($USER->id, $twf, null, $cm); $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id); if (($canmanageactivities || \mod_twf\subscriptions::is_subscribable($twf)) && $subscribe && !$subscribed && $cansub) { \mod_twf\subscriptions::subscribe_user($USER->id, $twf, $modcontext, true); } else { if (!$subscribe && $subscribed) { \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf, $modcontext, true); } } } } $returnto = twf_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', 'twf', $shortname), 1); } else { redirect($returnto, get_string('nowallunsubscribed', 'twf', $shortname), 1); } } /// First, let's process the general twfs and build up a display if ($generaltwfs) { foreach ($generaltwfs as $twf) {
/** * 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 ''; }