コード例 #1
0
 /**
  * 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('twf_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 twf level subscription.
     if (!$DB->record_exists('twf_subscriptions', array('userid' => $userid, 'twf' => $discussion->twf))) {
         if ($subscription && $subscription->preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) {
             // The user is not subscribed to the twf, but subscribed from the discussion, delete the discussion subscription.
             $DB->delete_records('twf_discussion_subs', array('id' => $subscription->id));
             unset(self::$twfdiscussioncache[$userid][$discussion->twf][$discussion->id]);
         } else {
             // The user is not subscribed from the twf. Ignore.
             return false;
         }
     } else {
         if ($subscription) {
             $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED;
             $DB->update_record('twf_discussion_subs', $subscription);
         } else {
             $subscription = new \stdClass();
             $subscription->userid = $userid;
             $subscription->twf = $discussion->twf;
             $subscription->discussion = $discussion->id;
             $subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED;
             $subscription->id = $DB->insert_record('twf_discussion_subs', $subscription);
         }
         self::$twfdiscussioncache[$userid][$discussion->twf][$discussion->id] = $subscription->preference;
     }
     $context = twf_get_context($discussion->twf, $context);
     $params = array('context' => $context, 'objectid' => $subscription->id, 'relateduserid' => $userid, 'other' => array('twfid' => $discussion->twf, 'discussion' => $discussion->id));
     $event = event\discussion_subscription_deleted::create($params);
     $event->trigger();
     return true;
 }
コード例 #2
0
ファイル: lib_test.php プロジェクト: Gavinthisisit/Moodle
 /**
  * Test that context fetching returns the appropriate context.
  */
 public function test_twf_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);
     $twf = $this->getDataGenerator()->create_module('twf', $options);
     $twfcm = get_coursemodule_from_instance('twf', $twf->id);
     $twfcontext = \context_module::instance($twfcm->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 = twf_get_context($twf->id, $twfcontext);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $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 = twf_get_context($twf->id, $coursecontext);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $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 = twf_get_context($twf->id);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $result);
     $this->assertEquals(1, $aftercount - $startcount);
     // Set up the default page event to use the twf.
     $PAGE = new moodle_page();
     $PAGE->set_context($twfcontext);
     $PAGE->set_cm($twfcm, $course, $twf);
     // 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 = twf_get_context($twf->id, $coursecontext);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $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 = twf_get_context($twf->id);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $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 = twf_get_context($twf->id, $coursecontext);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $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 = twf_get_context($twf->id);
     $aftercount = $DB->perf_get_reads();
     $this->assertEquals($twfcontext, $result);
     $this->assertEquals(1, $aftercount - $startcount);
 }