From da1205ccc2cda1634114d6c9ed43466a54f2373d Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 3 Jan 2017 10:34:04 +0100 Subject: [PATCH] MDL-50545 mod_page: New WS mod_page_get_pages_by_courses --- mod/page/classes/external.php | 110 +++++++++++++++++++++++++ mod/page/db/services.php | 9 ++ mod/page/tests/externallib_test.php | 122 ++++++++++++++++++++++++++++ mod/page/version.php | 2 +- 4 files changed, 242 insertions(+), 1 deletion(-) diff --git a/mod/page/classes/external.php b/mod/page/classes/external.php index 417d8d63684ce..9ef73369ea430 100644 --- a/mod/page/classes/external.php +++ b/mod/page/classes/external.php @@ -104,4 +104,114 @@ public static function view_page_returns() { ); } + /** + * Describes the parameters for get_pages_by_courses. + * + * @return external_function_parameters + * @since Moodle 3.3 + */ + public static function get_pages_by_courses_parameters() { + return new external_function_parameters ( + array( + 'courseids' => new external_multiple_structure( + new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array() + ), + ) + ); + } + + /** + * Returns a list of pages in a provided list of courses. + * If no list is provided all pages that the user can view will be returned. + * + * @param array $courseids course ids + * @return array of warnings and pages + * @since Moodle 3.3 + */ + public static function get_pages_by_courses($courseids = array()) { + + $warnings = array(); + $returnedpages = array(); + + $params = array( + 'courseids' => $courseids, + ); + $params = self::validate_parameters(self::get_pages_by_courses_parameters(), $params); + + $mycourses = array(); + if (empty($params['courseids'])) { + $mycourses = enrol_get_my_courses(); + $params['courseids'] = array_keys($mycourses); + } + + // Ensure there are courseids to loop through. + if (!empty($params['courseids'])) { + + list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses); + + // Get the pages in this course, this function checks users visibility permissions. + // We can avoid then additional validate_context calls. + $pages = get_all_instances_in_courses("page", $courses); + foreach ($pages as $page) { + $context = context_module::instance($page->coursemodule); + // Entry to return. + $page->name = external_format_string($page->name, $context->id); + + list($page->intro, $page->introformat) = external_format_text($page->intro, + $page->introformat, $context->id, 'mod_page', 'intro', null); + $page->introfiles = external_util::get_area_files($context->id, 'mod_page', 'intro', false, false); + + list($page->content, $page->contentformat) = external_format_text($page->content, $page->contentformat, + $context->id, 'mod_page', 'content', $page->revision); + $page->contentfiles = external_util::get_area_files($context->id, 'mod_page', 'content'); + + $returnedpages[] = $page; + } + } + + $result = array( + 'pages' => $returnedpages, + 'warnings' => $warnings + ); + return $result; + } + + /** + * Describes the get_pages_by_courses return value. + * + * @return external_single_structure + * @since Moodle 3.3 + */ + public static function get_pages_by_courses_returns() { + return new external_single_structure( + array( + 'pages' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'Module id'), + 'course' => new external_value(PARAM_INT, 'Course id'), + 'name' => new external_value(PARAM_RAW, 'Page name'), + 'intro' => new external_value(PARAM_RAW, 'Summary'), + 'introformat' => new external_format_value('intro', 'Summary format'), + 'introfiles' => new external_files('Files in the introduction text'), + 'content' => new external_value(PARAM_RAW, 'Page content'), + 'contentformat' => new external_format_value('content', 'Content format'), + 'contentfiles' => new external_files('Files in the content'), + 'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'), + 'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'), + 'display' => new external_value(PARAM_INT, 'How to display the page'), + 'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'), + 'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'), + 'timemodified' => new external_value(PARAM_INT, 'Last time the page was modified'), + 'section' => new external_value(PARAM_INT, 'Course section id'), + 'visible' => new external_value(PARAM_INT, 'Module visibility'), + 'groupmode' => new external_value(PARAM_INT, 'Group mode'), + 'groupingid' => new external_value(PARAM_INT, 'Grouping id'), + ) + ) + ), + 'warnings' => new external_warnings(), + ) + ); + } } diff --git a/mod/page/db/services.php b/mod/page/db/services.php index 60800690048f0..087019d86aef8 100644 --- a/mod/page/db/services.php +++ b/mod/page/db/services.php @@ -37,4 +37,13 @@ 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) ), + 'mod_page_get_pages_by_courses' => array( + 'classname' => 'mod_page_external', + 'methodname' => 'get_pages_by_courses', + 'description' => 'Returns a list of pages in a provided list of courses, if no list is provided all pages that the user + can view will be returned.', + 'type' => 'read', + 'capabilities' => 'mod/page:view', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), ); diff --git a/mod/page/tests/externallib_test.php b/mod/page/tests/externallib_test.php index 075af6e99712a..e30506fc35aa2 100644 --- a/mod/page/tests/externallib_test.php +++ b/mod/page/tests/externallib_test.php @@ -110,4 +110,126 @@ public function test_view_page() { } } + + /** + * Test test_mod_page_get_pages_by_courses + */ + public function test_mod_page_get_pages_by_courses() { + global $DB; + + $this->resetAfterTest(true); + + $course1 = self::getDataGenerator()->create_course(); + $course2 = self::getDataGenerator()->create_course(); + + $student = self::getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); + + // First page. + $record = new stdClass(); + $record->course = $course1->id; + $page1 = self::getDataGenerator()->create_module('page', $record); + + // Second page. + $record = new stdClass(); + $record->course = $course2->id; + $page2 = self::getDataGenerator()->create_module('page', $record); + + // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. + $enrol = enrol_get_plugin('manual'); + $enrolinstances = enrol_get_instances($course2->id, true); + foreach ($enrolinstances as $courseenrolinstance) { + if ($courseenrolinstance->enrol == "manual") { + $instance2 = $courseenrolinstance; + break; + } + } + $enrol->enrol_user($instance2, $student->id, $studentrole->id); + + self::setUser($student); + + $returndescription = mod_page_external::get_pages_by_courses_returns(); + + // Create what we expect to be returned when querying the two courses. + $expectedfields = array('id', 'course', 'name', 'intro', 'introformat', 'introfiles', + 'content', 'contentformat', 'contentfiles', 'legacyfiles', 'legacyfileslast', 'display', + 'displayoptions', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid'); + + // Add expected coursemodule and data. + $page1->coursemodule = $page1->cmid; + $page1->introformat = 1; + $page1->contentformat = 1; + $page1->section = 0; + $page1->visible = true; + $page1->groupmode = 0; + $page1->groupingid = 0; + $page1->introfiles = []; + $page1->contentfiles = []; + + $page2->coursemodule = $page2->cmid; + $page2->introformat = 1; + $page2->contentformat = 1; + $page2->section = 0; + $page2->visible = true; + $page2->groupmode = 0; + $page2->groupingid = 0; + $page2->introfiles = []; + $page2->contentfiles = []; + + foreach ($expectedfields as $field) { + $expected1[$field] = $page1->{$field}; + $expected2[$field] = $page2->{$field}; + } + + $expectedpages = array($expected2, $expected1); + + // Call the external function passing course ids. + $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); + $result = external_api::clean_returnvalue($returndescription, $result); + + $this->assertEquals($expectedpages, $result['pages']); + $this->assertCount(0, $result['warnings']); + + // Call the external function without passing course id. + $result = mod_page_external::get_pages_by_courses(); + $result = external_api::clean_returnvalue($returndescription, $result); + $this->assertEquals($expectedpages, $result['pages']); + $this->assertCount(0, $result['warnings']); + + // Add a file to the intro. + $filename = "file.txt"; + $filerecordinline = array( + 'contextid' => context_module::instance($page2->cmid)->id, + 'component' => 'mod_page', + 'filearea' => 'intro', + 'itemid' => 0, + 'filepath' => '/', + 'filename' => $filename, + ); + $fs = get_file_storage(); + $timepost = time(); + $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); + + $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); + $result = external_api::clean_returnvalue($returndescription, $result); + + $this->assertCount(1, $result['pages'][0]['introfiles']); + $this->assertEquals($filename, $result['pages'][0]['introfiles'][0]['filename']); + + // Unenrol user from second course. + $enrol->unenrol_user($instance2, $student->id); + array_shift($expectedpages); + + // Call the external function without passing course id. + $result = mod_page_external::get_pages_by_courses(); + $result = external_api::clean_returnvalue($returndescription, $result); + $this->assertEquals($expectedpages, $result['pages']); + + // Call for the second course we unenrolled the user from, expected warning. + $result = mod_page_external::get_pages_by_courses(array($course2->id)); + $this->assertCount(1, $result['warnings']); + $this->assertEquals('1', $result['warnings'][0]['warningcode']); + $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); + } } diff --git a/mod/page/version.php b/mod/page/version.php index de35e1a873021..28784e7e4060e 100644 --- a/mod/page/version.php +++ b/mod/page/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2016112900; // Requires this Moodle version $plugin->component = 'mod_page'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0;