/** * Simulate the forum/discuss.php web interface page: trigger events * * @param int $discussionid the discussion id * @return array of warnings and status result * @since Moodle 2.9 * @throws moodle_exception */ public static function view_forum_discussion($discussionid) { global $DB, $CFG; require_once $CFG->dirroot . "/mod/forum/lib.php"; $params = self::validate_parameters(self::view_forum_discussion_parameters(), array('discussionid' => $discussionid)); $warnings = array(); $discussion = $DB->get_record('forum_discussions', array('id' => $params['discussionid']), '*', MUST_EXIST); $forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum'); // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..). $modcontext = context_module::instance($cm->id); self::validate_context($modcontext); require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum'); // Call the forum/lib API. forum_discussion_view($modcontext, $forum, $discussion); $result = array(); $result['status'] = true; $result['warnings'] = $warnings; return $result; }
/** * Test forum_discussion_view. */ public function test_forum_discussion_view() { global $CFG, $USER; $this->resetAfterTest(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $discussion = $this->create_single_discussion_with_replies($forum, $USER, 2); $context = context_module::instance($forum->cmid); $cm = get_coursemodule_from_instance('forum', $forum->id); // Trigger and capture the event. $sink = $this->redirectEvents(); $this->setAdminUser(); forum_discussion_view($context, $forum, $discussion); $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_pop($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_viewed', $event); $this->assertEquals($context, $event->get_context()); $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }
} } $params = array('context' => $destinationctx, 'objectid' => $discussion->id, 'other' => array('fromforumid' => $forum->id, 'toforumid' => $forumto->id)); $event = \mod_forum\event\discussion_moved::create($params); $event->add_record_snapshot('forum_discussions', $discussion); $event->add_record_snapshot('forum', $forum); $event->add_record_snapshot('forum', $forumto); $event->trigger(); // Delete the RSS files for the 2 forums to force regeneration of the feeds require_once $CFG->dirroot . '/mod/forum/rsslib.php'; forum_rss_delete_file($forum); forum_rss_delete_file($forumto); redirect($return . '&moved=-1&sesskey=' . sesskey()); } // Trigger discussion viewed event. forum_discussion_view($modcontext, $forum, $discussion); unset($SESSION->fromdiscussion); if ($mode) { set_user_preference('forum_displaymode', $mode); } $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode); if ($parent) { // If flat AND parent, then force nested display this time if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) { $displaymode = FORUM_MODE_NESTED; } } else { $parent = $discussion->firstpost; } if (!($post = forum_get_post_full($parent))) { print_error("notexists", 'forum', "{$CFG->wwwroot}/mod/forum/view.php?f={$forum->id}");