/** * Removes user from the subscriber list * * @global object * @param int $userid * @param int $forumid * @param context_module|null $context Module context, may be omitted if not known or if called for the current module set in page. */ function hsuforum_unsubscribe($userid, $forumid, $context = null) { global $DB, $PAGE; $DB->delete_records('hsuforum_digests', array('userid' => $userid, 'forum' => $forumid)); if ($forumsubscription = $DB->get_record('hsuforum_subscriptions', array('userid' => $userid, 'forum' => $forumid))) { $DB->delete_records('hsuforum_subscriptions', array('id' => $forumsubscription->id)); if (!$context) { // Find out forum context. First try to take current page context to save on DB query. if ($PAGE->cm && $PAGE->cm->modname === 'hsuforum' && $PAGE->cm->instance == $forumid && $PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->context->instanceid == $PAGE->cm->id) { $context = $PAGE->context; } else { $cm = get_coursemodule_from_instance('hsuforum', $forumid); $context = context_module::instance($cm->id); } } $params = array('context' => $context, 'objectid' => $forumsubscription->id, 'relateduserid' => $userid, 'other' => array('forumid' => $forumid)); $event = \mod_hsuforum\event\subscription_deleted::create($params); $event->add_record_snapshot('hsuforum_subscriptions', $forumsubscription); $event->trigger(); } return true; }
/** * Test the subscription_deleted event. */ public function test_subscription_deleted() { // Setup test data. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('hsuforum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $context = context_module::instance($forum->cmid); // Add a subscription. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $subscription = $this->getDataGenerator()->get_plugin_generator('mod_hsuforum')->create_subscription($record); $params = array('context' => $context, 'objectid' => $subscription->id, 'other' => array('forumid' => $forum->id), 'relateduserid' => $user->id); $event = \mod_hsuforum\event\subscription_deleted::create($params); // Trigger and capturing the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_hsuforum\\event\\subscription_deleted', $event); $this->assertEquals($context, $event->get_context()); $expected = array($course->id, 'hsuforum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $url = new \moodle_url('/mod/hsuforum/subscribers.php', array('id' => $forum->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }