/** * Set up message and mail sinks, and set up other requirements for the * cron to be tested here. */ public function setUp() { global $CFG; $this->helper = new stdClass(); // Messaging is not compatible with transactions... $this->preventResetByRollback(); // Catch all messages $this->helper->messagesink = $this->redirectMessages(); $this->helper->mailsink = $this->redirectEmails(); // Confirm that we have an empty message sink so far. $messages = $this->helper->messagesink->get_messages(); $this->assertEquals(0, count($messages)); $messages = $this->helper->mailsink->get_messages(); $this->assertEquals(0, count($messages)); // Tell Moodle that we've not sent any digest messages out recently. $CFG->digestmailtimelast = 0; // And set the digest sending time to a negative number - this has // the effect of making it 11pm the previous day. $CFG->digestmailtime = -1; // Forcibly reduce the maxeditingtime to a one second to ensure that // messages are sent out. $CFG->maxeditingtime = 1; // We must clear the subscription caches. This has to be done both before each test, and after in case of other // tests using these functions. \mod_quora\subscriptions::reset_quora_cache(); \mod_quora\subscriptions::reset_discussion_cache(); }
/** * 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 quora. if ($context->contextlevel != CONTEXT_COURSE) { return; } // Forum lib required for the constant used below. require_once $CFG->dirroot . '/mod/quora/lib.php'; $userid = $event->relateduserid; $sql = "SELECT f.id, f.course as course, cm.id AS cmid, f.forcesubscribe\n FROM {quora} f\n JOIN {course_modules} cm ON (cm.instance = f.id)\n JOIN {modules} m ON (m.id = cm.module)\n LEFT JOIN {quora_subscriptions} fs ON (fs.quora = f.id AND fs.userid = :userid)\n WHERE f.course = :courseid\n AND f.forcesubscribe = :initial\n AND m.name = 'quora'\n AND fs.id IS NULL"; $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_INITIALSUBSCRIBE); $quoras = $DB->get_records_sql($sql, $params); foreach ($quoras as $quora) { // If user doesn't have allowforcesubscribe capability then don't subscribe. $modcontext = context_module::instance($quora->cmid); if (has_capability('mod/quora:allowforcesubscribe', $modcontext, $userid)) { \mod_quora\subscriptions::subscribe_user($userid, $quora, $modcontext); } } }
/** * Get the list of potential subscribers to a quora. * * @param object $quoracontext the quora context. * @param integer $groupid the id of a group, or 0 for all groups. * @param string $fields the list of fields to return for each user. As for get_users_by_capability. * @param string $sort sort order. As for get_users_by_capability. * @return array list of users. * @deprecated since Moodle 2.8 use \mod_quora\subscriptions::get_potential_subscribers() instead */ function quora_get_potential_subscribers($quoracontext, $groupid, $fields, $sort = '') { debugging("quora_get_potential_subscribers() has been deprecated, please use \\mod_quora\\subscriptions::get_potential_subscribers() instead.", DEBUG_DEVELOPER); \mod_quora\subscriptions::get_potential_subscribers($quoracontext, $groupid, $fields, $sort); }
/** * Test that providing a context_module instance to is_subscribed does not result in additional lookups to retrieve * the context_module. */ public function test_is_subscribed_cm() { global $DB; $this->resetAfterTest(true); // Create a course, with a quora. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE); $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); // Retrieve the $cm now. $cm = get_fast_modinfo($quora->course)->instances['quora'][$quora->id]; // Reset get_fast_modinfo. get_fast_modinfo(0, 0, true); // Call is_subscribed without passing the $cmid - this should result in a lookup and filling of some of the // caches. This provides us with consistent data to start from. $this->assertTrue(\mod_quora\subscriptions::is_subscribed($user->id, $quora)); $this->assertTrue(\mod_quora\subscriptions::is_subscribed($user->id, $quora)); // Make a note of the number of DB calls. $basecount = $DB->perf_get_reads(); // Call is_subscribed - it should give return the correct result (False), and result in no additional queries. $this->assertTrue(\mod_quora\subscriptions::is_subscribed($user->id, $quora, null, $cm)); // The capability check does require some queries, so we don't test it directly. // We don't assert here because this is dependant upon linked code which could change at any time. $suppliedcmcount = $DB->perf_get_reads() - $basecount; // Call is_subscribed without passing the $cmid now - this should result in a lookup. get_fast_modinfo(0, 0, true); $basecount = $DB->perf_get_reads(); $this->assertTrue(\mod_quora\subscriptions::is_subscribed($user->id, $quora)); $calculatedcmcount = $DB->perf_get_reads() - $basecount; // There should be more queries than when we performed the same check a moment ago. $this->assertGreaterThan($suppliedcmcount, $calculatedcmcount); }
/** * Test that a user unsubscribed from a quora who has subscribed to a discussion, only receives posts made after * they subscribed to the discussion. */ public function test_quora_discussion_subscription_quora_unsubscribed_discussion_subscribed_after_post() { $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); $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 quora. list($discussion, $post) = $this->helper_post_to_quora($quora, $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_quora\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($quora, $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); }
/** * Form definition * * @return void */ function definition() { global $CFG, $OUTPUT; $mform =& $this->_form; $course = $this->_customdata['course']; $cm = $this->_customdata['cm']; $coursecontext = $this->_customdata['coursecontext']; $modcontext = $this->_customdata['modcontext']; $quora = $this->_customdata['quora']; $post = $this->_customdata['post']; $subscribe = $this->_customdata['subscribe']; $edit = $this->_customdata['edit']; $thresholdwarning = $this->_customdata['thresholdwarning']; $mform->addElement('header', 'general', ''); //fill in the data depending on page params later using set_data // If there is a warning message and we are not editing a post we need to handle the warning. if (!empty($thresholdwarning) && !$edit) { // Here we want to display a warning if they can still post but have reached the warning threshold. if ($thresholdwarning->canpost) { $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional); $mform->addElement('html', $OUTPUT->notification($message)); } } $mform->addElement('text', 'subject', get_string('subject', 'quora'), 'size="48"'); $mform->setType('subject', PARAM_TEXT); $mform->addRule('subject', get_string('required'), 'required', null, 'client'); $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); $mform->addElement('editor', 'message', get_string('message', 'quora'), null, self::editor_options($modcontext, empty($post->id) ? null : $post->id)); $mform->setType('message', PARAM_RAW); $mform->addRule('message', get_string('required'), 'required', null, 'client'); $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext); if (\mod_quora\subscriptions::is_forcesubscribed($quora)) { $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'quora')); $mform->freeze('discussionsubscribe'); $mform->setDefaults('discussionsubscribe', 0); $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'quora'); } else { if (\mod_quora\subscriptions::subscription_disabled($quora) && !$manageactivities) { $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'quora')); $mform->freeze('discussionsubscribe'); $mform->setDefaults('discussionsubscribe', 0); $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'quora'); } else { $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'quora')); $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'quora'); } } if (!empty($quora->maxattachments) && $quora->maxbytes != 1 && has_capability('mod/quora:createattachment', $modcontext)) { // 1 = No attachments at all $mform->addElement('filemanager', 'attachments', get_string('attachment', 'quora'), null, self::attachment_options($quora)); $mform->addHelpButton('attachments', 'attachment', 'quora'); } if (empty($post->id) && $manageactivities) { $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'quora')); } if (!empty($CFG->quora_enabletimedposts) && !$post->parent && has_capability('mod/quora:viewhiddentimedposts', $coursecontext)) { // hack alert $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'quora')); $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'quora'), array('optional' => true)); $mform->addHelpButton('timestart', 'displaystart', 'quora'); $mform->addElement('date_selector', 'timeend', get_string('displayend', 'quora'), array('optional' => true)); $mform->addHelpButton('timeend', 'displayend', 'quora'); } else { $mform->addElement('hidden', 'timestart'); $mform->setType('timestart', PARAM_INT); $mform->addElement('hidden', 'timeend'); $mform->setType('timeend', PARAM_INT); $mform->setConstants(array('timestart' => 0, 'timeend' => 0)); } if ($groupmode = groups_get_activity_groupmode($cm, $course)) { // hack alert $groupdata = groups_get_activity_allowed_groups($cm); $groupcount = count($groupdata); $groupinfo = array(); $modulecontext = context_module::instance($cm->id); // Check whether the user has access to all groups in this quora from the accessallgroups cap. if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $modulecontext)) { // Only allow posting to all groups if the user has access to all groups. $groupinfo = array('0' => get_string('allparticipants')); $groupcount++; } $contextcheck = has_capability('mod/quora:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1; if ($contextcheck) { if (has_capability('mod/quora:canposttomygroups', $modulecontext) && !isset($post->edit)) { $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'quora')); $mform->addHelpButton('posttomygroups', 'posttomygroups', 'quora'); $mform->disabledIf('groupinfo', 'posttomygroups', 'checked'); } foreach ($groupdata as $grouptemp) { $groupinfo[$grouptemp->id] = $grouptemp->name; } $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo); $mform->setDefault('groupinfo', $post->groupid); $mform->setType('groupinfo', PARAM_INT); } else { if (empty($post->groupid)) { $groupname = get_string('allparticipants'); } else { $groupname = format_string($groupdata[$post->groupid]->name); } $mform->addElement('static', 'groupinfo', get_string('group'), $groupname); } } //------------------------------------------------------------------------------- // buttons if (isset($post->edit)) { // hack alert $submit_string = get_string('savechanges'); } else { $submit_string = get_string('posttoquora', 'quora'); } $this->add_action_buttons(true, $submit_string); $mform->addElement('hidden', 'course'); $mform->setType('course', PARAM_INT); $mform->addElement('hidden', 'quora'); $mform->setType('quora', PARAM_INT); $mform->addElement('hidden', 'discussion'); $mform->setType('discussion', PARAM_INT); $mform->addElement('hidden', 'parent'); $mform->setType('parent', PARAM_INT); $mform->addElement('hidden', 'userid'); $mform->setType('userid', PARAM_INT); $mform->addElement('hidden', 'groupid'); $mform->setType('groupid', PARAM_INT); $mform->addElement('hidden', 'edit'); $mform->setType('edit', PARAM_INT); $mform->addElement('hidden', 'reply'); $mform->setType('reply', PARAM_INT); }
/** * Returns a list of user objects who are subscribed to this quora. * * @param stdClass $quora The quora record. * @param int $groupid The group id if restricting subscriptions to a group of users, or 0 for all. * @param context_module $context the quora context, to save re-fetching it where possible. * @param string $fields requested user fields (with "u." table prefix). * @param boolean $includediscussionsubscriptions Whether to take discussion subscriptions and unsubscriptions into consideration. * @return array list of users. */ public static function fetch_subscribed_users($quora, $groupid = 0, $context = null, $fields = null, $includediscussionsubscriptions = false) { global $CFG, $DB; if (empty($fields)) { $allnames = get_all_user_name_fields(true, 'u'); $fields = "u.id,\n u.username,\n {$allnames},\n u.maildisplay,\n u.mailformat,\n u.maildigest,\n u.imagealt,\n u.email,\n u.emailstop,\n u.city,\n u.country,\n u.lastaccess,\n u.lastlogin,\n u.picture,\n u.timezone,\n u.theme,\n u.lang,\n u.trackquoras,\n u.mnethostid"; } // Retrieve the quora context if it wasn't specified. $context = quora_get_context($quora->id, $context); if (self::is_forcesubscribed($quora)) { $results = \mod_quora\subscriptions::get_potential_subscribers($context, $groupid, $fields, "u.email ASC"); } else { // Only active enrolled users or everybody on the frontpage. list($esql, $params) = get_enrolled_sql($context, '', $groupid, true); $params['quoraid'] = $quora->id; if ($includediscussionsubscriptions) { $params['squoraid'] = $quora->id; $params['dsquoraid'] = $quora->id; $params['unsubscribed'] = self::FORUM_DISCUSSION_UNSUBSCRIBED; $sql = "SELECT {$fields}\n FROM (\n SELECT userid FROM {quora_subscriptions} s\n WHERE\n s.quora = :squoraid\n UNION\n SELECT userid FROM {quora_discussion_subs} ds\n WHERE\n ds.quora = :dsquoraid AND ds.preference <> :unsubscribed\n ) subscriptions\n JOIN {user} u ON u.id = subscriptions.userid\n JOIN ({$esql}) je ON je.id = u.id\n ORDER BY u.email ASC"; } else { $sql = "SELECT {$fields}\n FROM {user} u\n JOIN ({$esql}) je ON je.id = u.id\n JOIN {quora_subscriptions} s ON s.userid = u.id\n WHERE\n s.quora = :quoraid\n ORDER BY u.email ASC"; } $results = $DB->get_records_sql($sql, $params); } // Guest user should never be subscribed to a quora. unset($results[$CFG->siteguest]); // Apply the activity module availability resetrictions. $cm = get_coursemodule_from_instance('quora', $quora->id, $quora->course); $modinfo = get_fast_modinfo($quora->course); $info = new \core_availability\info_module($modinfo->get_cm($cm->id)); $results = $info->filter_user_list($results); return $results; }
/** * Adds module specific settings to the settings block * * @param settings_navigation $settings The settings navigation object * @param navigation_node $quoranode The node to add module settings to */ function quora_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $quoranode) { global $USER, $PAGE, $CFG, $DB, $OUTPUT; $quoraobject = $DB->get_record("quora", array("id" => $PAGE->cm->instance)); if (empty($PAGE->cm->context)) { $PAGE->cm->context = context_module::instance($PAGE->cm->instance); } $params = $PAGE->url->params(); if (!empty($params['d'])) { $discussionid = $params['d']; } // for some actions you need to be enrolled, beiing admin is not enough sometimes here $enrolled = is_enrolled($PAGE->cm->context, $USER, '', false); $activeenrolled = is_enrolled($PAGE->cm->context, $USER, '', true); $canmanage = has_capability('mod/quora:managesubscriptions', $PAGE->cm->context); $subscriptionmode = \mod_quora\subscriptions::get_subscription_mode($quoraobject); $cansubscribe = $activeenrolled && !\mod_quora\subscriptions::is_forcesubscribed($quoraobject) && (!\mod_quora\subscriptions::subscription_disabled($quoraobject) || $canmanage); if ($canmanage) { $mode = $quoranode->add(get_string('subscriptionmode', 'quora'), null, navigation_node::TYPE_CONTAINER); $allowchoice = $mode->add(get_string('subscriptionoptional', 'quora'), new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'mode' => FORUM_CHOOSESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $forceforever = $mode->add(get_string("subscriptionforced", "quora"), new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'mode' => FORUM_FORCESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $forceinitially = $mode->add(get_string("subscriptionauto", "quora"), new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'mode' => FORUM_INITIALSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $disallowchoice = $mode->add(get_string('subscriptiondisabled', 'quora'), new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'mode' => FORUM_DISALLOWSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); switch ($subscriptionmode) { case FORUM_CHOOSESUBSCRIBE: // 0 $allowchoice->action = null; $allowchoice->add_class('activesetting'); break; case FORUM_FORCESUBSCRIBE: // 1 $forceforever->action = null; $forceforever->add_class('activesetting'); break; case FORUM_INITIALSUBSCRIBE: // 2 $forceinitially->action = null; $forceinitially->add_class('activesetting'); break; case FORUM_DISALLOWSUBSCRIBE: // 3 $disallowchoice->action = null; $disallowchoice->add_class('activesetting'); break; } } else { if ($activeenrolled) { switch ($subscriptionmode) { case FORUM_CHOOSESUBSCRIBE: // 0 $notenode = $quoranode->add(get_string('subscriptionoptional', 'quora')); break; case FORUM_FORCESUBSCRIBE: // 1 $notenode = $quoranode->add(get_string('subscriptionforced', 'quora')); break; case FORUM_INITIALSUBSCRIBE: // 2 $notenode = $quoranode->add(get_string('subscriptionauto', 'quora')); break; case FORUM_DISALLOWSUBSCRIBE: // 3 $notenode = $quoranode->add(get_string('subscriptiondisabled', 'quora')); break; } } } if ($cansubscribe) { if (\mod_quora\subscriptions::is_subscribed($USER->id, $quoraobject, null, $PAGE->cm)) { $linktext = get_string('unsubscribe', 'quora'); } else { $linktext = get_string('subscribe', 'quora'); } $url = new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'sesskey' => sesskey())); $quoranode->add($linktext, $url, navigation_node::TYPE_SETTING); if (isset($discussionid)) { if (\mod_quora\subscriptions::is_subscribed($USER->id, $quoraobject, $discussionid, $PAGE->cm)) { $linktext = get_string('unsubscribediscussion', 'quora'); } else { $linktext = get_string('subscribediscussion', 'quora'); } $url = new moodle_url('/mod/quora/subscribe.php', array('id' => $quoraobject->id, 'sesskey' => sesskey(), 'd' => $discussionid, 'returnurl' => $PAGE->url->out())); $quoranode->add($linktext, $url, navigation_node::TYPE_SETTING); } } if (has_capability('mod/quora:viewsubscribers', $PAGE->cm->context)) { $url = new moodle_url('/mod/quora/subscribers.php', array('id' => $quoraobject->id)); $quoranode->add(get_string('showsubscribers', 'quora'), $url, navigation_node::TYPE_SETTING); } if ($enrolled && quora_tp_can_track_quoras($quoraobject)) { // keep tracking info for users with suspended enrolments if ($quoraobject->trackingtype == FORUM_TRACKING_OPTIONAL || !$CFG->quora_allowforcedreadtracking && $quoraobject->trackingtype == FORUM_TRACKING_FORCED) { if (quora_tp_is_tracked($quoraobject)) { $linktext = get_string('notrackquora', 'quora'); } else { $linktext = get_string('trackquora', 'quora'); } $url = new moodle_url('/mod/quora/settracking.php', array('id' => $quoraobject->id, 'sesskey' => sesskey())); $quoranode->add($linktext, $url, navigation_node::TYPE_SETTING); } } if (!isloggedin() && $PAGE->course->id == SITEID) { $userid = guest_user()->id; } else { $userid = $USER->id; } $hascourseaccess = $PAGE->course->id == SITEID || can_access_course($PAGE->course, $userid); $enablerssfeeds = !empty($CFG->enablerssfeeds) && !empty($CFG->quora_enablerssfeeds); if ($enablerssfeeds && $quoraobject->rsstype && $quoraobject->rssarticles && $hascourseaccess) { if (!function_exists('rss_get_url')) { require_once "{$CFG->libdir}/rsslib.php"; } if ($quoraobject->rsstype == 1) { $string = get_string('rsssubscriberssdiscussions', 'quora'); } else { $string = get_string('rsssubscriberssposts', 'quora'); } $url = new moodle_url(rss_get_url($PAGE->cm->context->id, $userid, "mod_quora", $quoraobject->id)); $quoranode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); } }
} if (!has_capability('mod/quora:viewdiscussion', $context)) { print_error('noviewdiscussionspermission', 'quora', get_local_referer(false)); } if (is_null($sesskey)) { // We came here via link in email. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); 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('confirmsubscribediscussion', 'quora', $a), $PAGE->url, $viewurl); } else { echo $OUTPUT->confirm(get_string('confirmsubscribe', 'quora', format_string($quora->name)), $PAGE->url, $viewurl); } echo $OUTPUT->footer(); exit; } require_sesskey(); if ($discussionid == null) { \mod_quora\subscriptions::subscribe_user($user->id, $quora, $context, true); redirect($returnto, get_string("nowsubscribed", "quora", $info), 1); } else { $info->discussion = $discussion->name; \mod_quora\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $context); redirect($returnto, get_string("discussionnowsubscribed", "quora", $info), 1); } }
$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) {
public function tearDown() { // We must clear the subscription caches. This has to be done both before each test, and after in case of other // tests using these functions. \mod_quora\subscriptions::reset_quora_cache(); }
$node->display = false; if ($node && $post->id != $discussion->firstpost) { $node->add(format_string($post->subject), $PAGE->url); } $PAGE->set_title("{$course->shortname}: " . format_string($discussion->name)); $PAGE->set_heading($course->fullname); $PAGE->set_button($searchform); $renderer = $PAGE->get_renderer('mod_quora'); echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($quora->name), 2); echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname'); // 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. if (!is_guest($modcontext, $USER) && isloggedin() && has_capability('mod/quora:viewdiscussion', $modcontext)) { // Discussion subscription. if (\mod_quora\subscriptions::is_subscribable($quora)) { echo html_writer::div(quora_get_discussion_subscription_icon($quora, $post->discussion, null, true), 'discussionsubscription'); echo quora_get_discussion_subscription_icon_preloaders(); } } /// Check to see if groups are being used in this quora /// If so, make sure the current person is allowed to see this discussion /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons $canreply = quora_user_can_post($quora, $discussion, $USER, $cm, $course, $modcontext); if (!$canreply and $quora->type !== 'news') { if (isguestuser() or !isloggedin()) { $canreply = true; } if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link // normal users with temporary guest access see this link too, they are asked to enrol instead
$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) { $USER->subscriptionsediting = $edit; } $PAGE->set_button(quora_update_subscriptions_button($course->id, $id)); } else { unset($USER->subscriptionsediting); } echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('quora', 'quora') . ' ' . $strsubscribers); if (empty($USER->subscriptionsediting)) { $subscribers = \mod_quora\subscriptions::fetch_subscribed_users($quora, $currentgroup, $context); if (\mod_quora\subscriptions::is_forcesubscribed($quora)) { $subscribers = mod_quora_filter_hidden_users($cm, $context, $subscribers); } echo $quoraoutput->subscriber_overview($subscribers, $quora, $course); } else { echo $quoraoutput->subscriber_selection_form($existingselector, $subscriberselector); } echo $OUTPUT->footer(); /** * Filters a list of users for whether they can see a given activity. * If the course module is hidden (closed-eye icon), then only users who have * the permission to view hidden activities will appear in the output list. * * @todo MDL-48625 This filtering should be handled in core libraries instead. * * @param stdClass $cm the course module record of the activity.
/** * Test subscription using disallow subscription on create. */ public function test_quora_disallow_subscribe_on_create() { global $CFG; $this->resetAfterTest(); $usercount = 5; $course = $this->getDataGenerator()->create_course(); $users = array(); for ($i = 0; $i < $usercount; $i++) { $user = $this->getDataGenerator()->create_user(); $users[] = $user; $this->getDataGenerator()->enrol_user($user->id, $course->id); } $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE); // Subscription prevented. $quora = $this->getDataGenerator()->create_module('quora', $options); $result = \mod_quora\subscriptions::fetch_subscribed_users($quora); // No subscriptions by default. $this->assertEquals(0, count($result)); foreach ($users as $user) { $this->assertFalse(\mod_quora\subscriptions::is_subscribed($user->id, $quora)); } }
/** * 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()); }
$draftid_editor = file_get_submitted_draft_itemid('message'); $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_quora', 'post', $postid, mod_quora_post_form::editor_options($modcontext, $postid), $post->message); $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext); if (\mod_quora\subscriptions::subscription_disabled($quora) && !$manageactivities) { // User does not have permission to subscribe to this discussion at all. $discussionsubscribe = false; } else { if (\mod_quora\subscriptions::is_forcesubscribed($quora)) { // User does not have permission to unsubscribe from this discussion at all. $discussionsubscribe = true; } else { if (isset($discussion) && \mod_quora\subscriptions::is_subscribed($USER->id, $quora, $discussion->id, $cm)) { // User is subscribed to the discussion - continue the subscription. $discussionsubscribe = true; } else { if (!isset($discussion) && \mod_quora\subscriptions::is_subscribed($USER->id, $quora, null, $cm)) { // Starting a new discussion, and the user is subscribed to the quora - subscribe to the discussion. $discussionsubscribe = true; } else { // User is not subscribed to either quora or discussion. Follow user preference. $discussionsubscribe = $USER->autosubscribe; } } } } $mform_post->set_data(array('attachments' => $draftitemid, 'general' => $heading, 'subject' => $post->subject, 'message' => array('text' => $currenttext, 'format' => empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat, 'itemid' => $draftid_editor), 'discussionsubscribe' => $discussionsubscribe, 'mailnow' => !empty($post->mailnow), 'userid' => $post->userid, 'parent' => $post->parent, 'discussion' => $post->discussion, 'course' => $course->id) + $page_params + (isset($post->format) ? array('format' => $post->format) : array()) + (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) + (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) + (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) + (isset($discussion->id) ? array('discussion' => $discussion->id) : array())); if ($mform_post->is_cancelled()) { if (!isset($discussion->id) || $quora->type === 'qanda') { // Q and A quoras don't have a discussion page, so treat them like a new thread.. redirect(new moodle_url('/mod/quora/view.php', array('f' => $quora->id))); } else {