/** * 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('quora_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 quora level subscription. if (!$DB->record_exists('quora_subscriptions', array('userid' => $userid, 'quora' => $discussion->quora))) { if ($subscription && $subscription->preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) { // The user is not subscribed to the quora, but subscribed from the discussion, delete the discussion subscription. $DB->delete_records('quora_discussion_subs', array('id' => $subscription->id)); unset(self::$quoradiscussioncache[$userid][$discussion->quora][$discussion->id]); } else { // The user is not subscribed from the quora. Ignore. return false; } } else { if ($subscription) { $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED; $DB->update_record('quora_discussion_subs', $subscription); } else { $subscription = new \stdClass(); $subscription->userid = $userid; $subscription->quora = $discussion->quora; $subscription->discussion = $discussion->id; $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED; $subscription->id = $DB->insert_record('quora_discussion_subs', $subscription); } self::$quoradiscussioncache[$userid][$discussion->quora][$discussion->id] = $subscription->preference; } $context = quora_get_context($discussion->quora, $context); $params = array('context' => $context, 'objectid' => $subscription->id, 'relateduserid' => $userid, 'other' => array('quoraid' => $discussion->quora, 'discussion' => $discussion->id)); $event = event\discussion_subscription_deleted::create($params); $event->trigger(); return true; }
/** * Test that context fetching returns the appropriate context. */ public function test_quora_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); $quora = $this->getDataGenerator()->create_module('quora', $options); $quoracm = get_coursemodule_from_instance('quora', $quora->id); $quoracontext = \context_module::instance($quoracm->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 = quora_get_context($quora->id, $quoracontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $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 = quora_get_context($quora->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $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 = quora_get_context($quora->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $result); $this->assertEquals(1, $aftercount - $startcount); // Set up the default page event to use the quora. $PAGE = new moodle_page(); $PAGE->set_context($quoracontext); $PAGE->set_cm($quoracm, $course, $quora); // 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 = quora_get_context($quora->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $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 = quora_get_context($quora->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $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 = quora_get_context($quora->id, $coursecontext); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $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 = quora_get_context($quora->id); $aftercount = $DB->perf_get_reads(); $this->assertEquals($quoracontext, $result); $this->assertEquals(1, $aftercount - $startcount); }