From c76d5fd63ef3c6c8a84a63245532ee1f26fa990a Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 3 Jan 2017 11:40:44 +0100 Subject: [PATCH] MDL-50549 mod_url: New WS mod_url_get_urls_by_courses --- mod/url/classes/external.php | 102 +++++++++++++++++++++++++ mod/url/db/services.php | 10 ++- mod/url/tests/externallib_test.php | 117 +++++++++++++++++++++++++++++ mod/url/version.php | 2 +- 4 files changed, 229 insertions(+), 2 deletions(-) diff --git a/mod/url/classes/external.php b/mod/url/classes/external.php index 7a09ccadfee..59e28c242ab 100644 --- a/mod/url/classes/external.php +++ b/mod/url/classes/external.php @@ -104,4 +104,106 @@ class mod_url_external extends external_api { ); } + /** + * Describes the parameters for get_urls_by_courses. + * + * @return external_function_parameters + * @since Moodle 3.3 + */ + public static function get_urls_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 urls in a provided list of courses. + * If no list is provided all urls that the user can view will be returned. + * + * @param array $courseids course ids + * @return array of warnings and urls + * @since Moodle 3.3 + */ + public static function get_urls_by_courses($courseids = array()) { + + $warnings = array(); + $returnedurls = array(); + + $params = array( + 'courseids' => $courseids, + ); + $params = self::validate_parameters(self::get_urls_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 urls in this course, this function checks users visibility permissions. + // We can avoid then additional validate_context calls. + $urls = get_all_instances_in_courses("url", $courses); + foreach ($urls as $url) { + $context = context_module::instance($url->coursemodule); + // Entry to return. + $url->name = external_format_string($url->name, $context->id); + + list($url->intro, $url->introformat) = external_format_text($url->intro, + $url->introformat, $context->id, 'mod_url', 'intro', null); + $url->introfiles = external_util::get_area_files($context->id, 'mod_url', 'intro', false, false); + + $returnedurls[] = $url; + } + } + + $result = array( + 'urls' => $returnedurls, + 'warnings' => $warnings + ); + return $result; + } + + /** + * Describes the get_urls_by_courses return value. + * + * @return external_single_structure + * @since Moodle 3.3 + */ + public static function get_urls_by_courses_returns() { + return new external_single_structure( + array( + 'urls' => 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, 'URL 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'), + 'externalurl' => new external_value(PARAM_RAW_TRIMMED, 'External URL'), + 'display' => new external_value(PARAM_INT, 'How to display the url'), + 'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'), + 'parameters' => new external_value(PARAM_RAW, 'Parameters to append to the URL'), + 'timemodified' => new external_value(PARAM_INT, 'Last time the url 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/url/db/services.php b/mod/url/db/services.php index 0481d941ab8..987533a961d 100644 --- a/mod/url/db/services.php +++ b/mod/url/db/services.php @@ -36,5 +36,13 @@ $functions = array( 'capabilities' => 'mod/url:view', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) ), - + 'mod_url_get_urls_by_courses' => array( + 'classname' => 'mod_url_external', + 'methodname' => 'get_urls_by_courses', + 'description' => 'Returns a list of urls in a provided list of courses, if no list is provided all urls that the user + can view will be returned.', + 'type' => 'read', + 'capabilities' => 'mod/url:view', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), ); diff --git a/mod/url/tests/externallib_test.php b/mod/url/tests/externallib_test.php index 686c4227466..eea0bf476f9 100644 --- a/mod/url/tests/externallib_test.php +++ b/mod/url/tests/externallib_test.php @@ -110,4 +110,121 @@ class mod_url_external_testcase extends externallib_advanced_testcase { } } + + /** + * Test test_mod_url_get_urls_by_courses + */ + public function test_mod_url_get_urls_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 url. + $record = new stdClass(); + $record->course = $course1->id; + $url1 = self::getDataGenerator()->create_module('url', $record); + + // Second url. + $record = new stdClass(); + $record->course = $course2->id; + $url2 = self::getDataGenerator()->create_module('url', $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_url_external::get_urls_by_courses_returns(); + + // Create what we expect to be returned when querying the two courses. + $expectedfields = array('id', 'course', 'name', 'intro', 'introformat', 'introfiles', 'externalurl', 'display', + 'displayoptions', 'parameters', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid'); + + // Add expected coursemodule and data. + $url1->coursemodule = $url1->cmid; + $url1->introformat = 1; + $url1->section = 0; + $url1->visible = true; + $url1->groupmode = 0; + $url1->groupingid = 0; + $url1->introfiles = []; + + $url2->coursemodule = $url2->cmid; + $url2->introformat = 1; + $url2->section = 0; + $url2->visible = true; + $url2->groupmode = 0; + $url2->groupingid = 0; + $url2->introfiles = []; + + foreach ($expectedfields as $field) { + $expected1[$field] = $url1->{$field}; + $expected2[$field] = $url2->{$field}; + } + + $expectedurls = array($expected2, $expected1); + + // Call the external function passing course ids. + $result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id)); + $result = external_api::clean_returnvalue($returndescription, $result); + + $this->assertEquals($expectedurls, $result['urls']); + $this->assertCount(0, $result['warnings']); + + // Call the external function without passing course id. + $result = mod_url_external::get_urls_by_courses(); + $result = external_api::clean_returnvalue($returndescription, $result); + $this->assertEquals($expectedurls, $result['urls']); + $this->assertCount(0, $result['warnings']); + + // Add a file to the intro. + $filename = "file.txt"; + $filerecordinline = array( + 'contextid' => context_module::instance($url2->cmid)->id, + 'component' => 'mod_url', + '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_url_external::get_urls_by_courses(array($course2->id, $course1->id)); + $result = external_api::clean_returnvalue($returndescription, $result); + + $this->assertCount(1, $result['urls'][0]['introfiles']); + $this->assertEquals($filename, $result['urls'][0]['introfiles'][0]['filename']); + + // Unenrol user from second course and alter expected urls. + $enrol->unenrol_user($instance2, $student->id); + array_shift($expectedurls); + + // Call the external function without passing course id. + $result = mod_url_external::get_urls_by_courses(); + $result = external_api::clean_returnvalue($returndescription, $result); + $this->assertEquals($expectedurls, $result['urls']); + + // Call for the second course we unenrolled the user from, expected warning. + $result = mod_url_external::get_urls_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/url/version.php b/mod/url/version.php index 64d8f3437a0..d0d7b8e4f4c 100644 --- a/mod/url/version.php +++ b/mod/url/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_url'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; -- 2.43.0