/** * Test that context fetching returns the appropriate context. */ public function test_forum_get_context() { global $DB, $PAGE; $this->resetAfterTest(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $coursecontext = \context_course::instance($course->id); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $forum = $this->getDataGenerator()->create_module('forum', $options); $forumcm = get_coursemodule_from_instance('forum', $forum->id); $forumcontext = \context_module::instance($forumcm->id); // First check that specifying the context results in the correct context being returned. // Do this before we set up the page object and we should return from the coursemodule record. // There should be no DB queries here because the context type was correct. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id, $forumcontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(0, $aftercount - $startcount); // And a context which is not the correct type. // This tests will result in a DB query to fetch the course_module. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(1, $aftercount - $startcount); // Now do not specify a context at all. // This tests will result in a DB query to fetch the course_module. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(1, $aftercount - $startcount); // Set up the default page event to use the forum. $PAGE = new moodle_page(); $PAGE->set_context($forumcontext); $PAGE->set_cm($forumcm, $course, $forum); // Now specify a context which is not a context_module. // There should be no DB queries here because we use the PAGE. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(0, $aftercount - $startcount); // Now do not specify a context at all. // There should be no DB queries here because we use the PAGE. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(0, $aftercount - $startcount); // Now specify the page context of the course instead.. $PAGE = new moodle_page(); $PAGE->set_context($coursecontext); // Now specify a context which is not a context_module. // This tests will result in a DB query to fetch the course_module. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(1, $aftercount - $startcount); // Now do not specify a context at all. // This tests will result in a DB query to fetch the course_module. $startcount = $DB->perf_get_reads(); $result = forum_get_context($forum->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($forumcontext, $result); $this->assertEquals(1, $aftercount - $startcount); }
/** * Unsubscribes the user from the specified discussion. * * @param int $userid The userid of the user being unsubscribed * @param \stdClass $discussion The discussion to unsubscribe from * @param \context_module|null $context Module context, may be omitted if not known or if called for the current * module set in page. * @return boolean Whether a change was made */ public static function unsubscribe_user_from_discussion($userid, $discussion, $context = null) { global $DB; // First check whether the user's subscription preference for this discussion. $subscription = $DB->get_record('forum_discussion_subs', array('userid' => $userid, 'discussion' => $discussion->id)); if ($subscription) { if ($subscription->preference == self::FORUM_DISCUSSION_UNSUBSCRIBED) { // The user is already unsubscribed from the discussion. Ignore. return false; } } // No discussion-level preference. Check for a forum level subscription. if (!$DB->record_exists('forum_subscriptions', array('userid' => $userid, 'forum' => $discussion->forum))) { if ($subscription && $subscription->preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) { // The user is not subscribed to the forum, but subscribed from the discussion, delete the discussion subscription. $DB->delete_records('forum_discussion_subs', array('id' => $subscription->id)); unset(self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id]); } else { // The user is not subscribed from the forum. Ignore. return false; } } else { if ($subscription) { $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED; $DB->update_record('forum_discussion_subs', $subscription); } else { $subscription = new \stdClass(); $subscription->userid = $userid; $subscription->forum = $discussion->forum; $subscription->discussion = $discussion->id; $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED; $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription); } self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id] = $subscription->preference; } $context = forum_get_context($discussion->forum, $context); $params = array('context' => $context, 'objectid' => $subscription->id, 'relateduserid' => $userid, 'other' => array('forumid' => $discussion->forum, 'discussion' => $discussion->id)); $event = event\discussion_subscription_deleted::create($params); $event->trigger(); return true; }