From 6c161f3b99db234d0dd9beecadb5c49e96ba2a62 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 24 May 2022 15:28:29 +0100 Subject: [PATCH] MDL-74760 activity web services: refactor duplicate code --- .../helper_for_get_mods_by_courses.php | 135 ++++++++++++++++++ .../get_bigbluebuttonbns_by_courses.php | 28 +--- mod/book/classes/external.php | 43 ++---- mod/chat/classes/external.php | 39 ++--- mod/choice/classes/external.php | 42 ++---- mod/folder/classes/external.php | 32 ++--- mod/forum/externallib.php | 3 + mod/glossary/classes/external.php | 26 ++-- mod/imscp/classes/external.php | 48 ++----- mod/label/classes/external.php | 31 ++-- mod/lti/classes/external.php | 40 ++---- mod/lti/tests/externallib_test.php | 4 +- mod/page/classes/external.php | 38 ++--- mod/quiz/classes/external.php | 41 ++---- mod/resource/classes/external.php | 31 ++-- mod/scorm/classes/external.php | 39 ++--- mod/survey/classes/external.php | 54 ++----- mod/url/classes/external.php | 32 ++--- mod/wiki/classes/external.php | 36 ++--- 19 files changed, 290 insertions(+), 452 deletions(-) create mode 100644 course/classes/external/helper_for_get_mods_by_courses.php diff --git a/course/classes/external/helper_for_get_mods_by_courses.php b/course/classes/external/helper_for_get_mods_by_courses.php new file mode 100644 index 00000000000..44d497d64f2 --- /dev/null +++ b/course/classes/external/helper_for_get_mods_by_courses.php @@ -0,0 +1,135 @@ +. + +namespace core_course\external; + +use context_module; +use external_description; +use external_files; +use external_format_value; +use external_util; +use external_value; + +/** + * This class helps implement the get_..._by_courses web service that every activity should have. + * + * It has helper methods to add the standard course-module fields to the results, and the declaration of the return value. + * + * @package core_course + * @copyright 2022 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class helper_for_get_mods_by_courses { + + /** + * Add the value of all the standard fields to the results to be returned by the service. + * This is designed to be used in the implementation of the get_..._by_courses web service methods. + * + * Note that $modinstance is also updated in-place. + * + * @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}. + * @param string $component the plugin name, e.g. 'mod_book'. + * @param string $capabilityforgroups capability to check before including group/visible/section info in the results. + * @param string|null $capabilityforintro capability to check before including intro info in the results. + * null means always include (the default). + * @return array with the containing all the values declared in {@see standard_coursemodule_elements_returns()}. + */ + public static function standard_coursemodule_element_values(\stdClass $modinstance, string $component, + string $capabilityforgroups = 'moodle/course:manageactivities', string $capabilityforintro = null): array { + self::format_name_and_intro($modinstance, $component); + $context = context_module::instance($modinstance->coursemodule); + + // First, we return information that any user can see in the web interface. + $moddetails['id'] = $modinstance->id; + $moddetails['coursemodule'] = $modinstance->coursemodule; + $moddetails['course'] = $modinstance->course; + $moddetails['name'] = $modinstance->name; + if (!$capabilityforintro || has_capability($capabilityforintro, $context)) { + $moddetails['intro'] = $modinstance->intro; + $moddetails['introformat'] = $modinstance->introformat; + $moddetails['introfiles'] = $modinstance->introfiles; + } + + // Now add information only available to people who can edit. + if (has_capability($capabilityforgroups, $context)) { + $moddetails['section'] = $modinstance->section; + $moddetails['visible'] = $modinstance->visible; + $moddetails['groupmode'] = $modinstance->groupmode; + $moddetails['groupingid'] = $modinstance->groupingid; + } + + return $moddetails; + } + + /** + * Format the module name an introduction ready to be exported to a web service. + * + * Note that $modinstance is updated in-place. + * + * @param \stdClass $modinstance one of the objects returned from a call to {@see get_all_instances_in_courses()}. + * @param string $component the plugin name, e.g. 'mod_book'. + */ + public static function format_name_and_intro(\stdClass $modinstance, string $component) { + $context = context_module::instance($modinstance->coursemodule); + + $modinstance->name = external_format_string($modinstance->name, $context->id); + + [$modinstance->intro, $modinstance->introformat] = external_format_text( + $modinstance->intro, $modinstance->introformat, $context->id, + $component, 'intro', null, ['noclean' => true]); + $modinstance->introfiles = external_util::get_area_files($context->id, $component, 'intro', false, false); + } + + /** + * Get the list of standard fields, to add to the declaration of the return values. + * + * Example usage combine the fields returned here with any extra ones your activity uses: + * + * public static function execute_returns() { + * return new external_single_structure([ + * 'bigbluebuttonbns' => new external_multiple_structure( + * new external_single_structure(array_merge( + * helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + * [ + * 'meetingid' => new external_value(PARAM_RAW, 'Meeting id'), + * 'timemodified' => new external_value(PARAM_INT, 'Last time the instance was modified'), + * ] + * )) + * ), + * 'warnings' => new external_warnings(), + * ] + * ); + * } + * + * @param bool $introoptional if true, the intro fields are marked as optional. Default false. + * @return external_description[] array of standard fields, to which you can add your activity-specific ones. + */ + public static function standard_coursemodule_elements_returns(bool $introoptional = false): array { + return [ + 'id' => new external_value(PARAM_INT, 'Activity instance id'), + 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), + 'course' => new external_value(PARAM_INT, 'Course id'), + 'name' => new external_value(PARAM_RAW, 'Activity name'), + 'intro' => new external_value(PARAM_RAW, 'Activity introduction', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED), + 'introformat' => new external_format_value('intro', $introoptional ? VALUE_OPTIONAL : VALUE_REQUIRED), + 'introfiles' => new external_files('Files in the introduction', VALUE_OPTIONAL), + 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), + 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL), + 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), + 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), + ]; + } +} diff --git a/mod/bigbluebuttonbn/classes/external/get_bigbluebuttonbns_by_courses.php b/mod/bigbluebuttonbn/classes/external/get_bigbluebuttonbns_by_courses.php index 8edce2e9c42..c347a9c16cc 100644 --- a/mod/bigbluebuttonbn/classes/external/get_bigbluebuttonbns_by_courses.php +++ b/mod/bigbluebuttonbn/classes/external/get_bigbluebuttonbns_by_courses.php @@ -17,6 +17,7 @@ namespace mod_bigbluebuttonbn\external; use context_module; +use core_course\external\helper_for_get_mods_by_courses; use external_api; use external_files; use external_format_value; @@ -86,15 +87,7 @@ class get_bigbluebuttonbns_by_courses extends external_api { // We can avoid then additional validate_context calls. $bigbluebuttonbns = get_all_instances_in_courses("bigbluebuttonbn", $courses, $USER->id); foreach ($bigbluebuttonbns as $bigbluebuttonbn) { - $context = context_module::instance($bigbluebuttonbn->coursemodule); - // Entry to return. - $bigbluebuttonbn->name = external_format_string($bigbluebuttonbn->name, $context->id); - - [$bigbluebuttonbn->intro, $bigbluebuttonbn->introformat] = external_format_text($bigbluebuttonbn->intro, - $bigbluebuttonbn->introformat, $context->id, 'mod_bigbluebuttonbn', 'intro', null); - $bigbluebuttonbn->introfiles = external_util::get_area_files($context->id, - 'mod_bigbluebuttonbn', 'intro', false, false); - + helper_for_get_mods_by_courses::format_name_and_intro($bigbluebuttonbn, 'mod_bigbluebuttonbn'); $returnedbigbluebuttonbns[] = $bigbluebuttonbn; } } @@ -115,22 +108,13 @@ class get_bigbluebuttonbns_by_courses extends external_api { public static function execute_returns() { return new external_single_structure([ 'bigbluebuttonbns' => new external_multiple_structure( - new external_single_structure([ - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Name'), - 'intro' => new external_value(PARAM_RAW, 'Description'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'meetingid' => new external_value(PARAM_RAW, 'Meeting id'), - 'introformat' => new external_format_value('intro', 'Summary format'), - 'introfiles' => new external_files('Files in the introduction text'), 'timemodified' => new external_value(PARAM_INT, 'Last time the instance 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/book/classes/external.php b/mod/book/classes/external.php index 2047f5a4bfd..7a26c0b36a4 100644 --- a/mod/book/classes/external.php +++ b/mod/book/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -174,7 +176,6 @@ class mod_book_external extends external_api { * @since Moodle 3.0 */ public static function get_books_by_courses($courseids = array()) { - global $CFG; $returnedbooks = array(); $warnings = array(); @@ -196,31 +197,16 @@ class mod_book_external extends external_api { // We can avoid then additional validate_context calls. $books = get_all_instances_in_courses("book", $courses); foreach ($books as $book) { - $context = context_module::instance($book->coursemodule); // Entry to return. - $bookdetails = array(); - // First, we return information that any user can see in the web interface. - $bookdetails['id'] = $book->id; - $bookdetails['coursemodule'] = $book->coursemodule; - $bookdetails['course'] = $book->course; - $bookdetails['name'] = external_format_string($book->name, $context->id); - // Format intro. - $options = array('noclean' => true); - list($bookdetails['intro'], $bookdetails['introformat']) = - external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null, $options); - $bookdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_book', 'intro', false, false); + $bookdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($book, 'mod_book'); $bookdetails['numbering'] = $book->numbering; $bookdetails['navstyle'] = $book->navstyle; $bookdetails['customtitles'] = $book->customtitles; - if (has_capability('moodle/course:manageactivities', $context)) { + if (has_capability('moodle/course:manageactivities', context_module::instance($book->coursemodule))) { $bookdetails['revision'] = $book->revision; $bookdetails['timecreated'] = $book->timecreated; $bookdetails['timemodified'] = $book->timemodified; - $bookdetails['section'] = $book->section; - $bookdetails['visible'] = $book->visible; - $bookdetails['groupmode'] = $book->groupmode; - $bookdetails['groupingid'] = $book->groupingid; } $returnedbooks[] = $bookdetails; } @@ -241,31 +227,20 @@ class mod_book_external extends external_api { return new external_single_structure( array( 'books' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Book id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Book name'), - 'intro' => new external_value(PARAM_RAW, 'The Book intro'), - 'introformat' => new external_format_value('intro'), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'numbering' => new external_value(PARAM_INT, 'Book numbering configuration'), 'navstyle' => new external_value(PARAM_INT, 'Book navigation style configuration'), 'customtitles' => new external_value(PARAM_INT, 'Book custom titles type'), 'revision' => new external_value(PARAM_INT, 'Book revision', VALUE_OPTIONAL), 'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), - ), 'Books' - ) + ] + ), 'Books') ), 'warnings' => new external_warnings(), ) ); } - } diff --git a/mod/chat/classes/external.php b/mod/chat/classes/external.php index d4012db8b1a..14876c7044b 100644 --- a/mod/chat/classes/external.php +++ b/mod/chat/classes/external.php @@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); require_once($CFG->dirroot . '/mod/chat/lib.php'); +use core_course\external\helper_for_get_mods_by_courses; use mod_chat\external\chat_message_exporter; /** @@ -541,18 +542,8 @@ class mod_chat_external extends external_api { $chats = get_all_instances_in_courses("chat", $courses); foreach ($chats as $chat) { $chatcontext = context_module::instance($chat->coursemodule); - // Entry to return. - $chatdetails = array(); - // First, we return information that any user can see in the web interface. - $chatdetails['id'] = $chat->id; - $chatdetails['coursemodule'] = $chat->coursemodule; - $chatdetails['course'] = $chat->course; - $chatdetails['name'] = external_format_string($chat->name, $chatcontext->id); - // Format intro. - $options = array('noclean' => true); - list($chatdetails['intro'], $chatdetails['introformat']) = - external_format_text($chat->intro, $chat->introformat, $chatcontext->id, 'mod_chat', 'intro', null, $options); - $chatdetails['introfiles'] = external_util::get_area_files($chatcontext->id, 'mod_chat', 'intro', false, false); + + $chatdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($chat, 'mod_chat'); if (has_capability('mod/chat:chat', $chatcontext)) { $chatdetails['chatmethod'] = $CFG->chat_method; @@ -564,10 +555,6 @@ class mod_chat_external extends external_api { if (has_capability('moodle/course:manageactivities', $chatcontext)) { $chatdetails['timemodified'] = $chat->timemodified; - $chatdetails['section'] = $chat->section; - $chatdetails['visible'] = $chat->visible; - $chatdetails['groupmode'] = $chat->groupmode; - $chatdetails['groupingid'] = $chat->groupingid; } $returnedchats[] = $chatdetails; } @@ -588,15 +575,9 @@ class mod_chat_external extends external_api { return new external_single_structure( array( 'chats' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Chat id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Chat name'), - 'intro' => new external_value(PARAM_RAW, 'The Chat intro'), - 'introformat' => new external_format_value('intro'), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'chatmethod' => new external_value(PARAM_PLUGIN, 'chat method (sockets, ajax, header_js)', VALUE_OPTIONAL), 'keepdays' => new external_value(PARAM_INT, 'keep days', VALUE_OPTIONAL), @@ -604,12 +585,8 @@ class mod_chat_external extends external_api { 'chattime' => new external_value(PARAM_INT, 'chat time', VALUE_OPTIONAL), 'schedule' => new external_value(PARAM_INT, 'schedule type', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'time of last modification', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_BOOL, 'visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'group id', VALUE_OPTIONAL), - ), 'Chats' - ) + ] + ), 'Chats') ), 'warnings' => new external_warnings(), ) diff --git a/mod/choice/classes/external.php b/mod/choice/classes/external.php index 2fdb40d8567..1612e44dc89 100644 --- a/mod/choice/classes/external.php +++ b/mod/choice/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); require_once($CFG->dirroot . '/mod/choice/lib.php'); @@ -471,8 +473,6 @@ class mod_choice_external extends external_api { * @since Moodle 3.0 */ public static function get_choices_by_courses($courseids = array()) { - global $CFG; - $returnedchoices = array(); $warnings = array(); @@ -494,18 +494,8 @@ class mod_choice_external extends external_api { $choices = get_all_instances_in_courses("choice", $courses); foreach ($choices as $choice) { $context = context_module::instance($choice->coursemodule); - // Entry to return. - $choicedetails = array(); - // First, we return information that any user can see in the web interface. - $choicedetails['id'] = $choice->id; - $choicedetails['coursemodule'] = $choice->coursemodule; - $choicedetails['course'] = $choice->course; - $choicedetails['name'] = external_format_string($choice->name, $context->id); - // Format intro. - $options = array('noclean' => true); - list($choicedetails['intro'], $choicedetails['introformat']) = - external_format_text($choice->intro, $choice->introformat, $context->id, 'mod_choice', 'intro', null, $options); - $choicedetails['introfiles'] = external_util::get_area_files($context->id, 'mod_choice', 'intro', false, false); + + $choicedetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($choice, 'mod_choice'); if (has_capability('mod/choice:choose', $context)) { $choicedetails['publish'] = $choice->publish; @@ -525,10 +515,6 @@ class mod_choice_external extends external_api { if (has_capability('moodle/course:manageactivities', $context)) { $choicedetails['timemodified'] = $choice->timemodified; $choicedetails['completionsubmit'] = $choice->completionsubmit; - $choicedetails['section'] = $choice->section; - $choicedetails['visible'] = $choice->visible; - $choicedetails['groupmode'] = $choice->groupmode; - $choicedetails['groupingid'] = $choice->groupingid; } $returnedchoices[] = $choicedetails; } @@ -549,15 +535,9 @@ class mod_choice_external extends external_api { return new external_single_structure( array( 'choices' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Choice instance id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Choice name'), - 'intro' => new external_value(PARAM_RAW, 'The choice intro'), - 'introformat' => new external_format_value('intro'), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'publish' => new external_value(PARAM_BOOL, 'If choice is published', VALUE_OPTIONAL), 'showresults' => new external_value(PARAM_INT, '0 never, 1 after answer, 2 after close, 3 always', VALUE_OPTIONAL), @@ -573,12 +553,8 @@ class mod_choice_external extends external_api { 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), 'completionsubmit' => new external_value(PARAM_BOOL, 'Completion on user submission', VALUE_OPTIONAL), 'showavailable' => new external_value(PARAM_BOOL, 'Show available spaces', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), - ), 'Choices' - ) + ] + ), 'Choices') ), 'warnings' => new external_warnings(), ) diff --git a/mod/folder/classes/external.php b/mod/folder/classes/external.php index 46d36fb27cb..946e39cd986 100644 --- a/mod/folder/classes/external.php +++ b/mod/folder/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -153,15 +155,7 @@ class mod_folder_external extends external_api { // We can avoid then additional validate_context calls. $folders = get_all_instances_in_courses("folder", $courses); foreach ($folders as $folder) { - $context = context_module::instance($folder->coursemodule); - // Entry to return. - $folder->name = external_format_string($folder->name, $context->id); - - $options = array('noclean' => true); - list($folder->intro, $folder->introformat) = - external_format_text($folder->intro, $folder->introformat, $context->id, 'mod_folder', 'intro', null, $options); - $folder->introfiles = external_util::get_area_files($context->id, 'mod_folder', 'intro', false, false); - + helper_for_get_mods_by_courses::format_name_and_intro($folder, 'mod_folder'); $returnedfolders[] = $folder; } } @@ -183,27 +177,17 @@ class mod_folder_external extends external_api { return new external_single_structure( array( 'folders' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course 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'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'), 'timemodified' => new external_value(PARAM_INT, 'Last time the folder was modified'), 'display' => new external_value(PARAM_INT, 'Display type of folder contents on a separate page or inline'), 'showexpanded' => new external_value(PARAM_INT, '1 = expanded, 0 = collapsed for sub-folders'), 'showdownloadfolder' => new external_value(PARAM_INT, 'Whether to show the download folder button'), 'forcedownload' => new external_value(PARAM_INT, 'Whether file download is forced'), - '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/forum/externallib.php b/mod/forum/externallib.php index 95c31360415..43be7e6506a 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -121,6 +121,9 @@ class mod_forum_external extends external_api { * @since Moodle 2.5 */ public static function get_forums_by_courses_returns() { + // This should be using helper_for_get_mods_by_courses::standard_coursemodule_elements_returns, but it is so horribly + // inconsistent with all similar web serviecs in other modules that we just can't. + // Also, the return type declaration is wrong, but I am not changing it now because I don't want ot break things. return new external_multiple_structure( new external_single_structure( array( diff --git a/mod/glossary/classes/external.php b/mod/glossary/classes/external.php index 98acf593dd2..27a1ee9e064 100644 --- a/mod/glossary/classes/external.php +++ b/mod/glossary/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.1 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir . '/externallib.php'); @@ -198,6 +200,7 @@ class mod_glossary_external extends external_api { * @since Moodle 3.1 */ public static function get_glossaries_by_courses($courseids = array()) { + global $CFG; $params = self::validate_parameters(self::get_glossaries_by_courses_parameters(), array('courseids' => $courseids)); $warnings = array(); @@ -221,12 +224,7 @@ class mod_glossary_external extends external_api { $glossaries = get_all_instances_in_courses('glossary', $courses); foreach ($glossaries as $glossary) { $context = context_module::instance($glossary->coursemodule); - $glossary->name = external_format_string($glossary->name, $context->id); - $options = array('noclean' => true); - list($glossary->intro, $glossary->introformat) = - external_format_text($glossary->intro, $glossary->introformat, $context->id, 'mod_glossary', 'intro', null, - $options); - $glossary->introfiles = external_util::get_area_files($context->id, 'mod_glossary', 'intro', false, false); + helper_for_get_mods_by_courses::format_name_and_intro($glossary, 'mod_glossary'); // Make sure we have a number of entries per page. if (!$glossary->entbypage) { @@ -257,14 +255,9 @@ class mod_glossary_external extends external_api { public static function get_glossaries_by_courses_returns() { return new external_single_structure(array( 'glossaries' => new external_multiple_structure( - new external_single_structure(array( - 'id' => new external_value(PARAM_INT, 'Glossary id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Glossary name'), - 'intro' => new external_value(PARAM_RAW, 'The Glossary intro'), - 'introformat' => new external_format_value('intro'), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'allowduplicatedentries' => new external_value(PARAM_INT, 'If enabled, multiple entries can have the' . ' same concept name'), 'displayformat' => new external_value(PARAM_TEXT, 'Display format type'), @@ -299,14 +292,11 @@ class mod_glossary_external extends external_api { 'timecreated' => new external_value(PARAM_INT, 'Time created'), 'timemodified' => new external_value(PARAM_INT, 'Time modified'), 'completionentries' => new external_value(PARAM_INT, 'Number of entries to complete'), - 'section' => new external_value(PARAM_INT, 'Section'), - 'visible' => new external_value(PARAM_INT, 'Visible'), - 'groupmode' => new external_value(PARAM_INT, 'Group mode'), - 'groupingid' => new external_value(PARAM_INT, 'Grouping ID'), 'browsemodes' => new external_multiple_structure( new external_value(PARAM_ALPHA, 'Modes of browsing allowed') ), 'canaddentry' => new external_value(PARAM_INT, 'Whether the user can add a new entry', VALUE_OPTIONAL), + ] ), 'Glossaries') ), 'warnings' => new external_warnings()) diff --git a/mod/imscp/classes/external.php b/mod/imscp/classes/external.php index e8db88c9db7..d89120f7e86 100644 --- a/mod/imscp/classes/external.php +++ b/mod/imscp/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -151,34 +153,14 @@ class mod_imscp_external extends external_api { // We can avoid then additional validate_context calls. $imscps = get_all_instances_in_courses("imscp", $courses); foreach ($imscps as $imscp) { - $context = context_module::instance($imscp->coursemodule); + $imscpdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values( + $imscp, 'mod_imscp', 'moodle/course:manageactivities', 'mod/imscp:view'); - // Entry to return. - $imscpdetails = array(); - // First, we return information that any user can see in the web interface. - $imscpdetails['id'] = $imscp->id; - $imscpdetails['coursemodule'] = $imscp->coursemodule; - $imscpdetails['course'] = $imscp->course; - $imscpdetails['name'] = external_format_string($imscp->name, $context->id); - - if (has_capability('mod/imscp:view', $context)) { - // Format intro. - $options = array('noclean' => true); - list($imscpdetails['intro'], $imscpdetails['introformat']) = - external_format_text($imscp->intro, $imscp->introformat, $context->id, 'mod_imscp', 'intro', null, - $options); - $imscpdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_imscp', 'intro', false, false); - } - - if (has_capability('moodle/course:manageactivities', $context)) { + if (has_capability('moodle/course:manageactivities', context_module::instance($imscp->coursemodule))) { $imscpdetails['revision'] = $imscp->revision; $imscpdetails['keepold'] = $imscp->keepold; $imscpdetails['structure'] = $imscp->structure; $imscpdetails['timemodified'] = $imscp->timemodified; - $imscpdetails['section'] = $imscp->section; - $imscpdetails['visible'] = $imscp->visible; - $imscpdetails['groupmode'] = $imscp->groupmode; - $imscpdetails['groupingid'] = $imscp->groupingid; } $returnedimscps[] = $imscpdetails; } @@ -199,25 +181,15 @@ class mod_imscp_external extends external_api { return new external_single_structure( array( 'imscps' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'IMSCP id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Activity name'), - 'intro' => new external_value(PARAM_RAW, 'The IMSCP intro', VALUE_OPTIONAL), - 'introformat' => new external_format_value('intro', VALUE_OPTIONAL), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true), + [ 'revision' => new external_value(PARAM_INT, 'Revision', VALUE_OPTIONAL), 'keepold' => new external_value(PARAM_INT, 'Number of old IMSCP to keep', VALUE_OPTIONAL), 'structure' => new external_value(PARAM_RAW, 'IMSCP structure', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_RAW, 'Time of last modification', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_BOOL, 'If visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), - ), 'IMS content packages' - ) + ] + ), 'IMS content packages') ), 'warnings' => new external_warnings(), ) diff --git a/mod/label/classes/external.php b/mod/label/classes/external.php index 6b575129dbb..5ed11d6cdf7 100644 --- a/mod/label/classes/external.php +++ b/mod/label/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.3 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -88,14 +90,7 @@ class mod_label_external extends external_api { // We can avoid then additional validate_context calls. $labels = get_all_instances_in_courses("label", $courses); foreach ($labels as $label) { - $context = context_module::instance($label->coursemodule); - // Entry to return. - $label->name = external_format_string($label->name, $context->id); - $options = array('noclean' => true); - list($label->intro, $label->introformat) = - external_format_text($label->intro, $label->introformat, $context->id, 'mod_label', 'intro', null, $options); - $label->introfiles = external_util::get_area_files($context->id, 'mod_label', 'intro', false, false); - + helper_for_get_mods_by_courses::format_name_and_intro($label, 'mod_label'); $returnedlabels[] = $label; } } @@ -117,22 +112,12 @@ class mod_label_external extends external_api { return new external_single_structure( array( 'labels' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Label name'), - 'intro' => new external_value(PARAM_RAW, 'Label contents'), - 'introformat' => new external_format_value('intro', 'Content format'), - 'introfiles' => new external_files('Files in the introduction text'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'timemodified' => new external_value(PARAM_INT, 'Last time the label 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/lti/classes/external.php b/mod/lti/classes/external.php index dcb6613b68a..42225160764 100644 --- a/mod/lti/classes/external.php +++ b/mod/lti/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); @@ -298,33 +300,21 @@ class mod_lti_external extends external_api { $context = context_module::instance($lti->coursemodule); // Entry to return. - $module = array(); - - // First, we return information that any user can see in (or can deduce from) the web interface. - $module['id'] = $lti->id; - $module['coursemodule'] = $lti->coursemodule; - $module['course'] = $lti->course; - $module['name'] = external_format_string($lti->name, $context->id); + $module = helper_for_get_mods_by_courses::standard_coursemodule_element_values( + $lti, 'mod_lti', 'moodle/course:manageactivities', 'mod/lti:view'); $viewablefields = []; if (has_capability('mod/lti:view', $context)) { - $options = array('noclean' => true); - list($module['intro'], $module['introformat']) = - external_format_text($lti->intro, $lti->introformat, $context->id, 'mod_lti', 'intro', null, $options); - - $module['introfiles'] = external_util::get_area_files($context->id, 'mod_lti', 'intro', false, false); $viewablefields = array('launchcontainer', 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon'); } // Check additional permissions for returning optional private settings. if (has_capability('moodle/course:manageactivities', $context)) { - $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl', 'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster', 'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade', - 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid'); + 'resourcekey', 'password', 'debuglaunch', 'servicesalt'); $viewablefields = array_merge($viewablefields, $additionalfields); - } foreach ($viewablefields as $field) { @@ -352,15 +342,9 @@ class mod_lti_external extends external_api { return new external_single_structure( array( 'ltis' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'External tool id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'LTI name'), - 'intro' => new external_value(PARAM_RAW, 'The LTI intro', VALUE_OPTIONAL), - 'introformat' => new external_format_value('intro', VALUE_OPTIONAL), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true), + [ 'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), 'typeid' => new external_value(PARAM_INT, 'Type id', VALUE_OPTIONAL), @@ -388,12 +372,8 @@ class mod_lti_external extends external_api { 'servicesalt' => new external_value(PARAM_RAW, 'Service salt', VALUE_OPTIONAL), 'icon' => new external_value(PARAM_URL, 'Alternative icon URL', VALUE_OPTIONAL), 'secureicon' => new external_value(PARAM_URL, 'Secure icon URL', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_INT, 'visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'group id', VALUE_OPTIONAL), - ), 'Tool' - ) + ] + ), 'Tool') ), 'warnings' => new external_warnings(), ) diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index 17655a8198c..b9bd638a05f 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -203,6 +203,7 @@ class externallib_test extends mod_lti_testcase { $lti1->visible = true; $lti1->groupmode = 0; $lti1->groupingid = 0; + $lti1->section = 0; $lti1->introfiles = []; $lti2->coursemodule = $lti2->cmid; @@ -211,6 +212,7 @@ class externallib_test extends mod_lti_testcase { $lti2->visible = true; $lti2->groupmode = 0; $lti2->groupingid = 0; + $lti2->section = 0; $lti2->introfiles = []; foreach ($expectedfields as $field) { @@ -255,7 +257,7 @@ class externallib_test extends mod_lti_testcase { $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl', 'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster', 'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade', - 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid'); + 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid', 'section'); foreach ($additionalfields as $field) { $expectedltis[0][$field] = $lti1->{$field}; diff --git a/mod/page/classes/external.php b/mod/page/classes/external.php index 511d22285f5..01566bb0418 100644 --- a/mod/page/classes/external.php +++ b/mod/page/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -153,18 +155,12 @@ class mod_page_external extends external_api { // We can avoid then additional validate_context calls. $pages = get_all_instances_in_courses("page", $courses); foreach ($pages as $page) { + helper_for_get_mods_by_courses::format_name_and_intro($page, 'mod_page'); + $context = context_module::instance($page->coursemodule); - // Entry to return. - $page->name = external_format_string($page->name, $context->id); - - $options = array('noclean' => true); - list($page->intro, $page->introformat) = - external_format_text($page->intro, $page->introformat, $context->id, 'mod_page', 'intro', null, $options); - $page->introfiles = external_util::get_area_files($context->id, 'mod_page', 'intro', false, false); - - $options = array('noclean' => true); - list($page->content, $page->contentformat) = external_format_text($page->content, $page->contentformat, - $context->id, 'mod_page', 'content', $page->revision, $options); + list($page->content, $page->contentformat) = external_format_text( + $page->content, $page->contentformat, + $context->id, 'mod_page', 'content', $page->revision, ['noclean' => true]); $page->contentfiles = external_util::get_area_files($context->id, 'mod_page', 'content'); $returnedpages[] = $page; @@ -188,15 +184,9 @@ class mod_page_external extends external_api { return new external_single_structure( array( 'pages' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course 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'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'content' => new external_value(PARAM_RAW, 'Page content'), 'contentformat' => new external_format_value('content', 'Content format'), 'contentfiles' => new external_files('Files in the content'), @@ -206,12 +196,8 @@ class mod_page_external extends external_api { '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/quiz/classes/external.php b/mod/quiz/classes/external.php index ea64d5d1732..ea445bb2009 100644 --- a/mod/quiz/classes/external.php +++ b/mod/quiz/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.1 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); @@ -96,24 +98,15 @@ class mod_quiz_external extends external_api { $quiz = quiz_update_effective_access($quiz, $USER->id); // Entry to return. - $quizdetails = array(); - // First, we return information that any user can see in the web interface. - $quizdetails['id'] = $quiz->id; - $quizdetails['coursemodule'] = $quiz->coursemodule; - $quizdetails['course'] = $quiz->course; - $quizdetails['name'] = external_format_string($quiz->name, $context->id); + $quizdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values( + $quiz, 'mod_quiz', 'mod/quiz:view', 'mod/quiz:view'); if (has_capability('mod/quiz:view', $context)) { - // Format intro. - $options = array('noclean' => true); - list($quizdetails['intro'], $quizdetails['introformat']) = - external_format_text($quiz->intro, $quiz->introformat, $context->id, 'mod_quiz', 'intro', null, $options); - $quizdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_quiz', 'intro', false, false); - $viewablefields = array('timeopen', 'timeclose', 'grademethod', 'section', 'visible', 'groupmode', - 'groupingid', 'attempts', 'timelimit', 'grademethod', 'decimalpoints', + $viewablefields = array('timeopen', 'timeclose', 'attempts', 'timelimit', 'grademethod', 'decimalpoints', 'questiondecimalpoints', 'sumgrades', 'grade', 'preferredbehaviour'); - // Some times this function returns just empty. + + // Sometimes this function returns just empty. $hasfeedback = quiz_has_feedback($quiz); $quizdetails['hasfeedback'] = (!empty($hasfeedback)) ? 1 : 0; @@ -168,15 +161,9 @@ class mod_quiz_external extends external_api { return new external_single_structure( array( 'quizzes' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Standard Moodle primary key.'), - 'course' => new external_value(PARAM_INT, 'Foreign key reference to the course this quiz is part of.'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id.'), - 'name' => new external_value(PARAM_RAW, 'Quiz name.'), - 'intro' => new external_value(PARAM_RAW, 'Quiz introduction text.', VALUE_OPTIONAL), - 'introformat' => new external_format_value('intro', VALUE_OPTIONAL), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true), + [ 'timeopen' => new external_value(PARAM_INT, 'The time when this quiz opens. (0 = no restriction.)', VALUE_OPTIONAL), 'timeclose' => new external_value(PARAM_INT, 'The time when this quiz closes. (0 = no restriction.)', @@ -268,12 +255,8 @@ class mod_quiz_external extends external_api { 'hasfeedback' => new external_value(PARAM_INT, 'Whether the quiz has any non-blank feedback text', VALUE_OPTIONAL), 'hasquestions' => new external_value(PARAM_INT, 'Whether the quiz has questions', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_INT, 'Module visibility', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Grouping id', VALUE_OPTIONAL), - ) - ) + ] + )) ), 'warnings' => new external_warnings(), ) diff --git a/mod/resource/classes/external.php b/mod/resource/classes/external.php index 35a23fce88e..b82c8e830c6 100644 --- a/mod/resource/classes/external.php +++ b/mod/resource/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -154,13 +156,8 @@ class mod_resource_external extends external_api { $resources = get_all_instances_in_courses("resource", $courses); foreach ($resources as $resource) { $context = context_module::instance($resource->coursemodule); - // Entry to return. - $resource->name = external_format_string($resource->name, $context->id); - $options = array('noclean' => true); - list($resource->intro, $resource->introformat) = - external_format_text($resource->intro, $resource->introformat, $context->id, 'mod_resource', 'intro', null, - $options); - $resource->introfiles = external_util::get_area_files($context->id, 'mod_resource', 'intro', false, false); + + helper_for_get_mods_by_courses::format_name_and_intro($resource, 'mod_resource'); $resource->contentfiles = external_util::get_area_files($context->id, 'mod_resource', 'content'); $returnedresources[] = $resource; @@ -184,15 +181,9 @@ class mod_resource_external extends external_api { return new external_single_structure( array( 'resources' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course 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'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'contentfiles' => new external_files('Files in the content'), 'tobemigrated' => new external_value(PARAM_INT, 'Whether this resource was migrated'), 'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'), @@ -202,12 +193,8 @@ class mod_resource_external extends external_api { 'filterfiles' => new external_value(PARAM_INT, 'If filters should be applied to the resource content'), 'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'), 'timemodified' => new external_value(PARAM_INT, 'Last time the resource 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/scorm/classes/external.php b/mod/scorm/classes/external.php index ead804f8ef9..a470d7f4958 100644 --- a/mod/scorm/classes/external.php +++ b/mod/scorm/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); @@ -684,17 +686,7 @@ class mod_scorm_external extends external_api { $context = context_module::instance($scorm->coursemodule); // Entry to return. - $module = array(); - - // First, we return information that any user can see in (or can deduce from) the web interface. - $module['id'] = $scorm->id; - $module['coursemodule'] = $scorm->coursemodule; - $module['course'] = $scorm->course; - $module['name'] = external_format_string($scorm->name, $context->id); - $options = array('noclean' => true); - list($module['intro'], $module['introformat']) = - external_format_text($scorm->intro, $scorm->introformat, $context->id, 'mod_scorm', 'intro', null, $options); - $module['introfiles'] = external_util::get_area_files($context->id, 'mod_scorm', 'intro', false, false); + $module = helper_for_get_mods_by_courses::standard_coursemodule_element_values($scorm, 'mod_scorm'); // Check if the SCORM open and return warnings if so. list($open, $openwarnings) = scorm_get_availability_status($scorm, true, $context); @@ -730,12 +722,9 @@ class mod_scorm_external extends external_api { // Check additional permissions for returning optional private settings. if (has_capability('moodle/course:manageactivities', $context)) { - $additionalfields = array('updatefreq', 'options', 'completionstatusrequired', 'completionscorerequired', - 'completionstatusallscos', 'autocommit', 'timemodified', 'section', 'visible', - 'groupmode', 'groupingid'); + 'completionstatusallscos', 'autocommit', 'timemodified'); $viewablefields = array_merge($viewablefields, $additionalfields); - } foreach ($viewablefields as $field) { @@ -764,15 +753,9 @@ class mod_scorm_external extends external_api { return new external_single_structure( array( 'scorms' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'SCORM id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'SCORM name'), - 'intro' => new external_value(PARAM_RAW, 'The SCORM intro'), - 'introformat' => new external_format_value('intro'), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ 'packagesize' => new external_value(PARAM_INT, 'SCORM zip package size', VALUE_OPTIONAL), 'packageurl' => new external_value(PARAM_URL, 'SCORM zip package URL', VALUE_OPTIONAL), 'version' => new external_value(PARAM_NOTAGS, 'SCORM version (SCORM_12, SCORM_13, SCORM_AICC)', @@ -821,12 +804,8 @@ class mod_scorm_external extends external_api { 'completionstatusallscos' => new external_value(PARAM_INT, 'Require all scos to return completion status', VALUE_OPTIONAL), 'autocommit' => new external_value(PARAM_BOOL, 'Save track data automatically?', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), - ), 'SCORM' - ) + ] + ), 'SCORM') ), 'warnings' => new external_warnings(), ) diff --git a/mod/survey/classes/external.php b/mod/survey/classes/external.php index 8b361c14e7a..8fc2e1592c8 100644 --- a/mod/survey/classes/external.php +++ b/mod/survey/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); @@ -88,43 +90,25 @@ class mod_survey_external extends external_api { $surveys = get_all_instances_in_courses("survey", $courses); foreach ($surveys as $survey) { $context = context_module::instance($survey->coursemodule); + if (empty(trim($survey->intro))) { + $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template)); + $survey->intro = get_string($tempo, "survey"); + } + // Entry to return. - $surveydetails = array(); - // First, we return information that any user can see in the web interface. - $surveydetails['id'] = $survey->id; - $surveydetails['coursemodule'] = $survey->coursemodule; - $surveydetails['course'] = $survey->course; - $surveydetails['name'] = external_format_string($survey->name, $context->id); + $surveydetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values( + $survey, 'mod_survey', 'moodle/course:manageactivities', 'mod/survey:participate'); if (has_capability('mod/survey:participate', $context)) { - $trimmedintro = trim($survey->intro); - if (empty($trimmedintro)) { - $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template)); - $survey->intro = get_string($tempo, "survey"); - } - - // Format intro. - $options = array('noclean' => true); - list($surveydetails['intro'], $surveydetails['introformat']) = - external_format_text($survey->intro, $survey->introformat, $context->id, 'mod_survey', 'intro', null, - $options); - $surveydetails['introfiles'] = external_util::get_area_files($context->id, 'mod_survey', 'intro', false, - false); - $surveydetails['template'] = $survey->template; $surveydetails['days'] = $survey->days; $surveydetails['questions'] = $survey->questions; $surveydetails['surveydone'] = survey_already_done($survey->id, $USER->id) ? 1 : 0; - } if (has_capability('moodle/course:manageactivities', $context)) { $surveydetails['timecreated'] = $survey->timecreated; $surveydetails['timemodified'] = $survey->timemodified; - $surveydetails['section'] = $survey->section; - $surveydetails['visible'] = $survey->visible; - $surveydetails['groupmode'] = $survey->groupmode; - $surveydetails['groupingid'] = $survey->groupingid; } $returnedsurveys[] = $surveydetails; } @@ -145,27 +129,17 @@ class mod_survey_external extends external_api { return new external_single_structure( array( 'surveys' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Survey id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), - 'course' => new external_value(PARAM_INT, 'Course id'), - 'name' => new external_value(PARAM_RAW, 'Survey name'), - 'intro' => new external_value(PARAM_RAW, 'The Survey intro', VALUE_OPTIONAL), - 'introformat' => new external_format_value('intro', VALUE_OPTIONAL), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true), + [ 'template' => new external_value(PARAM_INT, 'Survey type', VALUE_OPTIONAL), 'days' => new external_value(PARAM_INT, 'Days', VALUE_OPTIONAL), 'questions' => new external_value(PARAM_RAW, 'Question ids', VALUE_OPTIONAL), 'surveydone' => new external_value(PARAM_INT, 'Did I finish the survey?', VALUE_OPTIONAL), 'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_INT, 'Visible', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), - ), 'Surveys' - ) + ] + ), 'Surveys') ), 'warnings' => new external_warnings(), ) diff --git a/mod/url/classes/external.php b/mod/url/classes/external.php index 458a4c8aadb..e0ff077bc31 100644 --- a/mod/url/classes/external.php +++ b/mod/url/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.0 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); @@ -153,15 +155,7 @@ class mod_url_external extends external_api { // 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); - - $options = array('noclean' => true); - list($url->intro, $url->introformat) = - external_format_text($url->intro, $url->introformat, $context->id, 'mod_url', 'intro', null, $options); - $url->introfiles = external_util::get_area_files($context->id, 'mod_url', 'intro', false, false); - + helper_for_get_mods_by_courses::format_name_and_intro($url, 'mod_url'); $returnedurls[] = $url; } } @@ -183,26 +177,16 @@ class mod_url_external extends external_api { return new external_single_structure( array( 'urls' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Module id'), - 'coursemodule' => new external_value(PARAM_INT, 'Course 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'), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(), + [ '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/wiki/classes/external.php b/mod/wiki/classes/external.php index b51d41bcaf2..9c37f49586d 100644 --- a/mod/wiki/classes/external.php +++ b/mod/wiki/classes/external.php @@ -24,6 +24,8 @@ * @since Moodle 3.1 */ +use core_course\external\helper_for_get_mods_by_courses; + defined('MOODLE_INTERNAL') || die; require_once($CFG->libdir . '/externallib.php'); @@ -92,21 +94,11 @@ class mod_wiki_external extends external_api { $context = context_module::instance($wiki->coursemodule); // Entry to return. - $module = array(); - - // First, we return information that any user can see in (or can deduce from) the web interface. - $module['id'] = $wiki->id; - $module['coursemodule'] = $wiki->coursemodule; - $module['course'] = $wiki->course; - $module['name'] = external_format_string($wiki->name, $context->id); + $module = helper_for_get_mods_by_courses::standard_coursemodule_element_values( + $wiki, 'mod_wiki', 'mod/wiki:viewpage', 'mod/wiki:viewpage'); $viewablefields = []; if (has_capability('mod/wiki:viewpage', $context)) { - $options = array('noclean' => true); - list($module['intro'], $module['introformat']) = - external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', null, $options); - $module['introfiles'] = external_util::get_area_files($context->id, 'mod_wiki', 'intro', false, false); - $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode', 'groupingid'); } @@ -145,15 +137,9 @@ class mod_wiki_external extends external_api { return new external_single_structure( array( 'wikis' => new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Wiki ID.'), - 'coursemodule' => new external_value(PARAM_INT, 'Course module ID.'), - 'course' => new external_value(PARAM_INT, 'Course ID.'), - 'name' => new external_value(PARAM_RAW, 'Wiki name.'), - 'intro' => new external_value(PARAM_RAW, 'Wiki intro.', VALUE_OPTIONAL), - 'introformat' => new external_format_value('Wiki intro format.', VALUE_OPTIONAL), - 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), + new external_single_structure(array_merge( + helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true), + [ 'timecreated' => new external_value(PARAM_INT, 'Time of creation.', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.', VALUE_OPTIONAL), 'firstpagetitle' => new external_value(PARAM_RAW, 'First page title.', VALUE_OPTIONAL), @@ -164,13 +150,9 @@ class mod_wiki_external extends external_api { VALUE_OPTIONAL), 'editbegin' => new external_value(PARAM_INT, 'Edit begin.', VALUE_OPTIONAL), 'editend' => new external_value(PARAM_INT, 'Edit end.', VALUE_OPTIONAL), - 'section' => new external_value(PARAM_INT, 'Course section ID.', VALUE_OPTIONAL), - 'visible' => new external_value(PARAM_INT, '1 if visible, 0 otherwise.', VALUE_OPTIONAL), - 'groupmode' => new external_value(PARAM_INT, 'Group mode.', VALUE_OPTIONAL), - 'groupingid' => new external_value(PARAM_INT, 'Group ID.', VALUE_OPTIONAL), 'cancreatepages' => new external_value(PARAM_BOOL, 'True if user can create pages.'), - ), 'Wikis' - ) + ] + ), 'Wikis') ), 'warnings' => new external_warnings(), )