diff --git a/course/externallib.php b/course/externallib.php index e868841af4c..627a7dd1afb 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -2499,4 +2499,104 @@ class core_course_external extends external_api { return self::get_course_module_returns(); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.2 + */ + public static function get_activities_overview_parameters() { + return new external_function_parameters( + array( + 'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'Course id.')), + ) + ); + } + + /** + * Return activities overview for the given courses. + * + * @param array $courseids a list of course ids + * @return array of warnings and the activities overview + * @since Moodle 3.2 + * @throws moodle_exception + */ + public static function get_activities_overview($courseids) { + global $USER; + + // Parameter validation. + $params = self::validate_parameters(self::get_activities_overview_parameters(), array('courseids' => $courseids)); + $courseoverviews = array(); + + list($courses, $warnings) = external_util::validate_courses($params['courseids']); + + if (!empty($courses)) { + // Add lastaccess to each course (required by print_overview function). + // We need the complete user data, the ws server does not load a complete one. + $user = get_complete_user_data('id', $USER->id); + foreach ($courses as $course) { + if (isset($user->lastcourseaccess[$course->id])) { + $course->lastaccess = $user->lastcourseaccess[$course->id]; + } else { + $course->lastaccess = 0; + } + } + + $overviews = array(); + if ($modules = get_plugin_list_with_function('mod', 'print_overview')) { + foreach ($modules as $fname) { + $fname($courses, $overviews); + } + } + + // Format output. + foreach ($overviews as $courseid => $modules) { + $courseoverviews[$courseid]['id'] = $courseid; + $courseoverviews[$courseid]['overviews'] = array(); + + foreach ($modules as $modname => $overviewtext) { + $courseoverviews[$courseid]['overviews'][] = array( + 'module' => $modname, + 'overviewtext' => $overviewtext // This text doesn't need formatting. + ); + } + } + } + + $result = array( + 'courses' => $courseoverviews, + 'warnings' => $warnings + ); + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.2 + */ + public static function get_activities_overview_returns() { + return new external_single_structure( + array( + 'courses' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'Course id'), + 'overviews' => new external_multiple_structure( + new external_single_structure( + array( + 'module' => new external_value(PARAM_PLUGIN, 'Module name'), + 'overviewtext' => new external_value(PARAM_RAW, 'Overview text'), + ) + ) + ) + ) + ), 'List of courses' + ), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 549b0545e6f..cdf7565d4d4 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -1670,4 +1670,47 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { } } + + /** + * Test get_activities_overview + */ + public function test_get_activities_overview() { + global $USER; + + $this->resetAfterTest(); + $course1 = self::getDataGenerator()->create_course(); + $course2 = self::getDataGenerator()->create_course(); + + // Create a viewer user. + $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1)); + $this->getDataGenerator()->enrol_user($viewer->id, $course1->id); + $this->getDataGenerator()->enrol_user($viewer->id, $course2->id); + + // Create two forums - one in each course. + $record = new stdClass(); + $record->course = $course1->id; + $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id)); + $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id)); + + $this->setAdminUser(); + // A standard post in the forum. + $record = new stdClass(); + $record->course = $course1->id; + $record->userid = $USER->id; + $record->forum = $forum1->id; + $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); + + $this->setUser($viewer->id); + $courses = array($course1->id , $course2->id); + + $result = core_course_external::get_activities_overview($courses); + $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result); + + // There should be one entry for course1, and no others. + $this->assertCount(1, $result['courses']); + $this->assertEquals($course1->id, $result['courses'][0]['id']); + // Check expected overview data for the module. + $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']); + $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']); + } } diff --git a/lib/db/services.php b/lib/db/services.php index 6428bec3182..94b2d35c474 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -293,6 +293,14 @@ $functions = array( 'type' => 'write', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_course_get_activities_overview' => array( + 'classname' => 'core_course_external', + 'methodname' => 'get_activities_overview', + 'classpath' => 'course/externallib.php', + 'description' => 'Return activities overview for the given courses.', + 'type' => 'read', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), 'core_enrol_get_course_enrolment_methods' => array( 'classname' => 'core_enrol_external', 'methodname' => 'get_course_enrolment_methods', diff --git a/version.php b/version.php index 2d80898b366..c68af99da45 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2016070100.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2016070100.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.