Ejemplo n.º 1
0
/**
 * Removes user from the subscriber list
 *
 * @param int $userid
 * @param int $quoraid
 * @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_quora\subscriptions::unsubscribe_user() instead
 */
function quora_unsubscribe($userid, $quoraid, $context = null, $userrequest = false)
{
    global $DB;
    debugging("quora_unsubscribe() has been deprecated, please use \\mod_quora\\subscriptions::unsubscribe_user() instead.", DEBUG_DEVELOPER);
    // Note: The new function does not take an integer form of quora.
    $quora = $DB->get_record('quora', array('id' => $quoraid));
    \mod_quora\subscriptions::unsubscribe_user($userid, $quora, $context, $userrequest);
}
Ejemplo n.º 2
0
 public function test_automatic_with_subscribed_discussion_in_unsubscribed_quora()
 {
     $this->resetAfterTest(true);
     // Create a course, with a quora.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
     $quora = $this->getDataGenerator()->create_module('quora', $options);
     // Create two users enrolled in the course as students.
     list($author, $recipient) = $this->helper_create_users($course, 2);
     // Post a discussion to the quora.
     list($discussion, $post) = $this->helper_post_to_quora($quora, $author);
     $this->helper_update_post_time($post, -90);
     // Unsubscribe the 'author' user from the quora.
     \mod_quora\subscriptions::unsubscribe_user($author->id, $quora);
     // Then re-subscribe them to the discussion.
     \mod_quora\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
     $this->helper_update_subscription_time($author, $discussion, -60);
     // We expect just the user subscribed to the quora 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($quora, $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);
 }
Ejemplo n.º 3
0
 /**
  * Test that after toggling the quora subscription as another user,
  * the discussion subscription functionality works as expected.
  */
 public function test_quora_subscribe_toggle_as_other_repeat_subscriptions()
 {
     global $DB;
     $this->resetAfterTest(true);
     // Create a course, with a quora.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $quora = $this->getDataGenerator()->create_module('quora', $options);
     // Create a user enrolled in the course as a student.
     list($user) = $this->helper_create_users($course, 1);
     // Post a discussion to the quora.
     list($discussion, $post) = $this->helper_post_to_quora($quora, $user);
     // Confirm that the user is currently not subscribed to the quora.
     $this->assertFalse(\mod_quora\subscriptions::is_subscribed($user->id, $quora));
     // Confirm that the user is unsubscribed from the discussion too.
     $this->assertFalse(\mod_quora\subscriptions::is_subscribed($user->id, $quora, $discussion->id));
     // Confirm that we have no records in either of the subscription tables.
     $this->assertEquals(0, $DB->count_records('quora_subscriptions', array('userid' => $user->id, 'quora' => $quora->id)));
     $this->assertEquals(0, $DB->count_records('quora_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Subscribing to the quora should create a record in the subscriptions table, but not the quora discussion
     // subscriptions table.
     \mod_quora\subscriptions::subscribe_user($user->id, $quora);
     $this->assertEquals(1, $DB->count_records('quora_subscriptions', array('userid' => $user->id, 'quora' => $quora->id)));
     $this->assertEquals(0, $DB->count_records('quora_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Now unsubscribe from the discussion. This should return true.
     $this->assertTrue(\mod_quora\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion));
     // Attempting to unsubscribe again should return false because no change was made.
     $this->assertFalse(\mod_quora\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_quora\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     // Attempting to subscribe again should return false because no change was made.
     $this->assertFalse(\mod_quora\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     // Now unsubscribe from the discussion. This should return true once more.
     $this->assertTrue(\mod_quora\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion));
     // And unsubscribing from the quora but not as a request from the user should maintain their preference.
     \mod_quora\subscriptions::unsubscribe_user($user->id, $quora);
     $this->assertEquals(0, $DB->count_records('quora_subscriptions', array('userid' => $user->id, 'quora' => $quora->id)));
     $this->assertEquals(1, $DB->count_records('quora_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Subscribing to the discussion should return truthfully because a change was made.
     $this->assertTrue(\mod_quora\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     $this->assertEquals(0, $DB->count_records('quora_subscriptions', array('userid' => $user->id, 'quora' => $quora->id)));
     $this->assertEquals(1, $DB->count_records('quora_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
 }
Ejemplo n.º 4
0
/**
 * Given a new post, subscribes or unsubscribes as appropriate.
 * Returns some text which describes what happened.
 *
 * @param object $fromform The submitted form
 * @param stdClass $quora The quora record
 * @param stdClass $discussion The quora discussion record
 * @return string
 */
function quora_post_subscription($fromform, $quora, $discussion)
{
    global $USER;
    if (\mod_quora\subscriptions::is_forcesubscribed($quora)) {
        return "";
    } else {
        if (\mod_quora\subscriptions::subscription_disabled($quora)) {
            $subscribed = \mod_quora\subscriptions::is_subscribed($USER->id, $quora);
            if ($subscribed && !has_capability('moodle/course:manageactivities', context_course::instance($quora->course), $USER->id)) {
                // This user should not be subscribed to the quora.
                \mod_quora\subscriptions::unsubscribe_user($USER->id, $quora);
            }
            return "";
        }
    }
    $info = new stdClass();
    $info->name = fullname($USER);
    $info->discussion = format_string($discussion->name);
    $info->quora = format_string($quora->name);
    if (isset($fromform->discussionsubscribe) && $fromform->discussionsubscribe) {
        if ($result = \mod_quora\subscriptions::subscribe_user_to_discussion($USER->id, $discussion)) {
            return html_writer::tag('p', get_string('discussionnowsubscribed', 'quora', $info));
        }
    } else {
        if ($result = \mod_quora\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion)) {
            return html_writer::tag('p', get_string('discussionnownotsubscribed', 'quora', $info));
        }
    }
    return '';
}
Ejemplo n.º 5
0
        echo $OUTPUT->header();
        $viewurl = new moodle_url('/mod/quora/view.php', array('f' => $id));
        if ($discussionid) {
            $a = new stdClass();
            $a->quora = format_string($quora->name);
            $a->discussion = format_string($discussion->name);
            echo $OUTPUT->confirm(get_string('confirmunsubscribediscussion', 'quora', $a), $PAGE->url, $viewurl);
        } else {
            echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'quora', format_string($quora->name)), $PAGE->url, $viewurl);
        }
        echo $OUTPUT->footer();
        exit;
    }
    require_sesskey();
    if ($discussionid === null) {
        if (\mod_quora\subscriptions::unsubscribe_user($user->id, $quora, $context, true)) {
            redirect($returnto, get_string("nownotsubscribed", "quora", $info), 1);
        } else {
            print_error('cannotunsubscribe', 'quora', get_local_referer(false));
        }
    } else {
        if (\mod_quora\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) {
            $info->discussion = $discussion->name;
            redirect($returnto, get_string("discussionnownotsubscribed", "quora", $info), 1);
        } else {
            print_error('cannotunsubscribe', 'quora', get_local_referer(false));
        }
    }
} else {
    // subscribe
    if (\mod_quora\subscriptions::subscription_disabled($quora) && !has_capability('mod/quora:managesubscriptions', $context)) {
Ejemplo n.º 6
0
        $modcontext = context_module::instance($cm->id);
        $cansub = false;
        if (has_capability('mod/quora:viewdiscussion', $modcontext)) {
            $cansub = true;
        }
        if ($cansub && $cm->visible == 0 && !has_capability('mod/quora:managesubscriptions', $modcontext)) {
            $cansub = false;
        }
        if (!\mod_quora\subscriptions::is_forcesubscribed($quora)) {
            $subscribed = \mod_quora\subscriptions::is_subscribed($USER->id, $quora, null, $cm);
            $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
            if (($canmanageactivities || \mod_quora\subscriptions::is_subscribable($quora)) && $subscribe && !$subscribed && $cansub) {
                \mod_quora\subscriptions::subscribe_user($USER->id, $quora, $modcontext, true);
            } else {
                if (!$subscribe && $subscribed) {
                    \mod_quora\subscriptions::unsubscribe_user($USER->id, $quora, $modcontext, true);
                }
            }
        }
    }
    $returnto = quora_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', 'quora', $shortname), 1);
    } else {
        redirect($returnto, get_string('nowallunsubscribed', 'quora', $shortname), 1);
    }
}
/// First, let's process the general quoras and build up a display
if ($generalquoras) {
    foreach ($generalquoras as $quora) {
Ejemplo n.º 7
0
    /** 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_quora\subscriptions::subscribe_user($user->id, $quora)) {
                print_error('cannotaddsubscriber', 'quora', '', $user->id);
            }
        }
    } else {
        if ($unsubscribe) {
            $users = $existingselector->get_selected_users();
            foreach ($users as $user) {
                if (!\mod_quora\subscriptions::unsubscribe_user($user->id, $quora)) {
                    print_error('cannotremovesubscriber', 'quora', '', $user->id);
                }
            }
        }
    }
    $subscriberselector->invalidate_selected_users();
    $existingselector->invalidate_selected_users();
    $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
}
$strsubscribers = get_string("subscribers", "quora");
$PAGE->navbar->add($strsubscribers);
$PAGE->set_title($strsubscribers);
$PAGE->set_heading($COURSE->fullname);
if (has_capability('mod/quora:managesubscriptions', $context) && \mod_quora\subscriptions::is_forcesubscribed($quora) === false) {
    if ($edit != -1) {
Ejemplo n.º 8
0
 /**
  * Test that the correct context is used in the events when subscribing
  * users.
  */
 public function test_quora_subscription_page_context_valid()
 {
     global $CFG, $PAGE;
     require_once $CFG->dirroot . '/mod/quora/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);
     $quora = $this->getDataGenerator()->create_module('quora', $options);
     $quiz = $this->getDataGenerator()->create_module('quiz', $options);
     // Add a discussion.
     $record = array();
     $record['course'] = $course->id;
     $record['quora'] = $quora->id;
     $record['userid'] = $user->id;
     $discussion = $this->getDataGenerator()->get_plugin_generator('mod_quora')->create_discussion($record);
     // Add a post.
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $this->getDataGenerator()->get_plugin_generator('mod_quora')->create_post($record);
     // Set up the default page event to use this quora.
     $PAGE = new moodle_page();
     $cm = get_coursemodule_from_instance('quora', $discussion->quora);
     $context = \context_module::instance($cm->id);
     $PAGE->set_context($context);
     $PAGE->set_cm($cm, $course, $quora);
     // Trigger and capturing the event.
     $sink = $this->redirectEvents();
     // Trigger the event by subscribing the user to the quora.
     \mod_quora\subscriptions::subscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the quora.
     \mod_quora\subscriptions::unsubscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_quora\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_quora\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_quora\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_quora\\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 quora.
     \mod_quora\subscriptions::subscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the quora.
     \mod_quora\subscriptions::unsubscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_quora\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_quora\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_quora\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_quora\\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 quora.
     \mod_quora\subscriptions::subscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the quora.
     \mod_quora\subscriptions::unsubscribe_user($user->id, $quora);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_quora\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_quora\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_quora\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_quora\\event\\discussion_subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
 }