diff --git a/lib/db/services.php b/lib/db/services.php index d66a827cbdb..50acf7c272c 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1035,6 +1035,7 @@ $services = array( 'core_completion_update_activity_completion_status_manually', 'mod_data_get_databases_by_courses', 'core_comment_get_comments', + 'mod_forum_view_forum', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/forum/db/services.php b/mod/forum/db/services.php index 48da4b8d632..66b5a76ea06 100644 --- a/mod/forum/db/services.php +++ b/mod/forum/db/services.php @@ -62,4 +62,13 @@ $functions = array( 'type' => 'read', 'capabilities' => 'mod/forum:viewdiscussion, mod/forum:viewqandawithoutposting' ), + + 'mod_forum_view_forum' => array( + 'classname' => 'mod_forum_external', + 'methodname' => 'view_forum', + 'classpath' => 'mod/forum/externallib.php', + 'description' => 'Simulate the view.php web interface page: trigger events, completion, etc...', + 'type' => 'write', + 'capabilities' => 'mod/forum:viewdiscussion' + ), ); diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 322bbcc0b81..b275d027c7c 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -809,4 +809,67 @@ class mod_forum_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 2.9 + */ + public static function view_forum_parameters() { + return new external_function_parameters( + array( + 'forumid' => new external_value(PARAM_INT, 'forum instance id') + ) + ); + } + + /** + * Simulate the forum/view.php web interface page: trigger events, completion, etc... + * + * @param int $forumid the forum instance id + * @return array of warnings and status result + * @since Moodle 2.9 + * @throws moodle_exception + */ + public static function view_forum($forumid) { + global $DB, $CFG; + require_once($CFG->dirroot . "/mod/forum/lib.php"); + + $params = self::validate_parameters(self::view_forum_parameters(), + array( + 'forumid' => $forumid + )); + $warnings = array(); + + // Request and permission validation. + $forum = $DB->get_record('forum', array('id' => $params['forumid']), 'id', MUST_EXIST); + list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + // Call the forum/lib API. + forum_view($forum, $course, $cm, $context); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 2.9 + */ + public static function view_forum_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c0cb23c9707..613aa741ac9 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -7763,3 +7763,33 @@ function forum_get_context($forumid, $context = null) { return $context; } + +/** + * Mark the activity completed (if required) and trigger the course_module_viewed event. + * + * @param stdClass $forum forum object + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + * @since Moodle 2.9 + */ +function forum_view($forum, $course, $cm, $context) { + + // Completion. + $completion = new completion_info($course); + $completion->set_module_viewed($cm); + + // Trigger course_module_viewed event. + + $params = array( + 'context' => $context, + 'objectid' => $forum->id + ); + + $event = \mod_forum\event\course_module_viewed::create($params); + $event->add_record_snapshot('course_modules', $cm); + $event->add_record_snapshot('course', $course); + $event->add_record_snapshot('forum', $forum); + $event->trigger(); + +} diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 53c1716d92f..27d23627dcf 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -1256,6 +1256,45 @@ class mod_forum_lib_testcase extends advanced_testcase { } } + public function test_forum_view() { + global $CFG; + + $CFG->enablecompletion = 1; + $this->resetAfterTest(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), + array('completion' => 2, 'completionview' => 1)); + $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_view($forum, $course, $cm, $context); + + $events = $sink->get_events(); + // 2 additional events thanks to completion. + $this->assertCount(3, $events); + $event = array_pop($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id)); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + // Check completion status. + $completion = new completion_info($course); + $completiondata = $completion->get_data($cm); + $this->assertEquals(1, $completiondata->completionstate); + + } + /** * Create a new course, forum, and user with a number of discussions and replies. * diff --git a/mod/forum/version.php b/mod/forum/version.php index 55b80971f9f..9315fab4712 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015031200; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015031201; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2014110400; // Requires this Moodle version $plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics) diff --git a/mod/forum/view.php b/mod/forum/view.php index 6522d9f1bdc..af2c8d7ffb0 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -100,18 +100,12 @@ rss_add_http_header($context, 'mod_forum', $forum, $rsstitle); } - // Mark viewed if required - $completion = new completion_info($course); - $completion->set_module_viewed($cm); - /// Print header. $PAGE->set_title($forum->name); $PAGE->add_body_class('forumtype-'.$forum->type); $PAGE->set_heading($course->fullname); - echo $OUTPUT->header(); - /// Some capability checks. if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) { notice(get_string("activityiscurrentlyhidden")); @@ -121,6 +115,11 @@ notice(get_string('noviewdiscussionspermission', 'forum')); } + // Mark viewed and trigger the course_module_viewed event. + forum_view($forum, $course, $cm, $context); + + echo $OUTPUT->header(); + echo $OUTPUT->heading(format_string($forum->name), 2); if (!empty($forum->intro) && $forum->type != 'single' && $forum->type != 'teacher') { echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro'); @@ -129,16 +128,6 @@ /// find out current groups mode groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forum/view.php?id=' . $cm->id); - $params = array( - 'context' => $context, - 'objectid' => $forum->id - ); - $event = \mod_forum\event\course_module_viewed::create($params); - $event->add_record_snapshot('course_modules', $cm); - $event->add_record_snapshot('course', $course); - $event->add_record_snapshot('forum', $forum); - $event->trigger(); - $SESSION->fromdiscussion = qualified_me(); // Return here if we post or set subscription etc