From 1104a9fa444149029171ebe327a0d2937c136c8f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 27 Mar 2017 14:20:37 +0200 Subject: [PATCH] MDL-58399 webservice: Return additional file fields for repositories We should be detecting when we are treating files that are linked to external repositories. For doing that we'd need to return some additional fields via Web Services: - isexternalfile - mimetype (google docs files use an special one) - repositorytype (the repository plugin name) --- course/externallib.php | 5 +++++ lib/externallib.php | 18 ++++++++++++++++++ lib/filestorage/stored_file.php | 16 ++++++++++++++++ lib/tests/externallib_test.php | 4 +++- mod/book/lib.php | 5 +++++ mod/folder/lib.php | 5 +++++ mod/forum/tests/externallib_test.php | 1 + mod/imscp/lib.php | 5 +++++ mod/page/lib.php | 5 +++++ mod/resource/lib.php | 5 +++++ mod/wiki/tests/externallib_test.php | 2 ++ 11 files changed, 70 insertions(+), 1 deletion(-) diff --git a/course/externallib.php b/course/externallib.php index 73370acc2db..26b6cc5a863 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -363,6 +363,11 @@ class core_course_external extends external_api { 'timecreated' => new external_value(PARAM_INT, 'Time created'), 'timemodified' => new external_value(PARAM_INT, 'Time modified'), 'sortorder' => new external_value(PARAM_INT, 'Content sort order'), + 'mimetype' => new external_value(PARAM_RAW, 'File mime type.', VALUE_OPTIONAL), + 'isexternalfile' => new external_value(PARAM_BOOL, 'Whether is an external file.', + VALUE_OPTIONAL), + 'repositorytype' => new external_value(PARAM_PLUGIN, 'The repository type for external files.', + VALUE_OPTIONAL), // copyright related info 'userid' => new external_value(PARAM_INT, 'User who added this content to moodle'), diff --git a/lib/externallib.php b/lib/externallib.php index 896a175e541..4114ad654f9 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -1309,6 +1309,10 @@ class external_util { $file['mimetype'] = $areafile->get_mimetype(); $file['filesize'] = $areafile->get_filesize(); $file['timemodified'] = $areafile->get_timemodified(); + $file['isexternalfile'] = $areafile->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $areafile->get_repository_type(); + } $fileitemid = $useitemidinurl ? $areafile->get_itemid() : null; $file['fileurl'] = moodle_url::make_webservice_pluginfile_url($contextid, $component, $filearea, $fileitemid, $areafile->get_filepath(), $areafile->get_filename())->out(false); @@ -1345,6 +1349,8 @@ class external_files extends external_multiple_structure { 'fileurl' => new external_value(PARAM_URL, 'Downloadable file url.', VALUE_OPTIONAL), 'timemodified' => new external_value(PARAM_INT, 'Time modified.', VALUE_OPTIONAL), 'mimetype' => new external_value(PARAM_RAW, 'File mime type.', VALUE_OPTIONAL), + 'isexternalfile' => new external_value(PARAM_BOOL, 'Whether is an external file.', VALUE_OPTIONAL), + 'repositorytype' => new external_value(PARAM_PLUGIN, 'The repository type for external files.', VALUE_OPTIONAL), ), 'File.' ), @@ -1397,6 +1403,18 @@ class external_files extends external_multiple_structure { 'optional' => true, 'null' => NULL_NOT_ALLOWED, ), + 'isexternalfile' => array( + 'type' => PARAM_BOOL, + 'description' => 'Whether is an external file.', + 'optional' => true, + 'null' => NULL_NOT_ALLOWED, + ), + 'repositorytype' => array( + 'type' => PARAM_PLUGIN, + 'description' => 'The repository type for the external files.', + 'optional' => true, + 'null' => NULL_ALLOWED, + ), ]; } } diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index 192e7b8daf2..481fa352e9b 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -806,6 +806,22 @@ class stored_file { } } + /** + * Returns repository type. + * + * @return mixed str|null the repository type or null if is not an external file + * @since Moodle 3.3 + */ + public function get_repository_type() { + + if (!empty($this->repository)) { + return $this->repository->get_typename(); + } else { + return null; + } + } + + /** * get reference file id * @return int diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index d6a922c0a00..088e40af96c 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -510,6 +510,7 @@ class core_externallib_testcase extends advanced_testcase { 'timemodified' => $timemodified, 'filesize' => $filesize, 'mimetype' => 'text/plain', + 'isexternalfile' => false, ); // Get all the files for the area. $files = external_util::get_area_files($context, $component, $filearea, false); @@ -529,7 +530,8 @@ class core_externallib_testcase extends advanced_testcase { $description = new external_files(); // First check that the expected default values and keys are returned. - $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype')); + $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype', + 'isexternalfile', 'repositorytype')); $returnedkeys = array_flip(array_keys($description->content->keys)); $this->assertEquals($expectedkeys, $returnedkeys); $this->assertEquals('List of files.', $description->desc); diff --git a/mod/book/lib.php b/mod/book/lib.php index 7f1fb35d17c..e44bc698e3d 100644 --- a/mod/book/lib.php +++ b/mod/book/lib.php @@ -584,6 +584,11 @@ function book_export_contents($cm, $baseurl) { $file['userid'] = $fileinfo->get_userid(); $file['author'] = $fileinfo->get_author(); $file['license'] = $fileinfo->get_license(); + $file['mimetype'] = $fileinfo->get_mimetype(); + $file['isexternalfile'] = $fileinfo->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $fileinfo->get_repository_type(); + } $contents[] = $file; } } diff --git a/mod/folder/lib.php b/mod/folder/lib.php index eba3377ee7d..afeb96938ef 100644 --- a/mod/folder/lib.php +++ b/mod/folder/lib.php @@ -317,6 +317,11 @@ function folder_export_contents($cm, $baseurl) { $file['userid'] = $fileinfo->get_userid(); $file['author'] = $fileinfo->get_author(); $file['license'] = $fileinfo->get_license(); + $file['mimetype'] = $fileinfo->get_mimetype(); + $file['isexternalfile'] = $fileinfo->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $fileinfo->get_repository_type(); + } $contents[] = $file; } diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index ea1ca6d0354..3f141b9a7a9 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -323,6 +323,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $discussion1reply1->id, '/', $filename), 'timemodified' => $timepost, 'mimetype' => 'image/jpeg', + 'isexternalfile' => false, ) ), 'totalscore' => $discussion1reply1->totalscore, diff --git a/mod/imscp/lib.php b/mod/imscp/lib.php index a7bdb71c715..35e6e7e0450 100644 --- a/mod/imscp/lib.php +++ b/mod/imscp/lib.php @@ -411,6 +411,11 @@ function imscp_export_contents($cm, $baseurl) { $file['userid'] = $fileinfo->get_userid(); $file['author'] = $fileinfo->get_author(); $file['license'] = $fileinfo->get_license(); + $file['mimetype'] = $fileinfo->get_mimetype(); + $file['isexternalfile'] = $fileinfo->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $fileinfo->get_repository_type(); + } $contents[] = $file; } diff --git a/mod/page/lib.php b/mod/page/lib.php index f3890103c62..34155c38f34 100644 --- a/mod/page/lib.php +++ b/mod/page/lib.php @@ -415,6 +415,11 @@ function page_export_contents($cm, $baseurl) { $file['userid'] = $fileinfo->get_userid(); $file['author'] = $fileinfo->get_author(); $file['license'] = $fileinfo->get_license(); + $file['mimetype'] = $fileinfo->get_mimetype(); + $file['isexternalfile'] = $fileinfo->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $fileinfo->get_repository_type(); + } $contents[] = $file; } diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 468e8382968..b44e5855c65 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -449,6 +449,11 @@ function resource_export_contents($cm, $baseurl) { $file['userid'] = $fileinfo->get_userid(); $file['author'] = $fileinfo->get_author(); $file['license'] = $fileinfo->get_license(); + $file['mimetype'] = $fileinfo->get_mimetype(); + $file['isexternalfile'] = $fileinfo->is_external_file(); + if ($file['isexternalfile']) { + $file['repositorytype'] = $fileinfo->get_repository_type(); + } $contents[] = $file; } diff --git a/mod/wiki/tests/externallib_test.php b/mod/wiki/tests/externallib_test.php index cbe49678316..3e3c9dceb17 100644 --- a/mod/wiki/tests/externallib_test.php +++ b/mod/wiki/tests/externallib_test.php @@ -1074,6 +1074,7 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase { 'filename' => $file['filename'], 'filepath' => $file['filepath'], 'mimetype' => 'image/jpeg', + 'isexternalfile' => false, 'filesize' => strlen($content), 'timemodified' => $file['timemodified'], 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], @@ -1128,6 +1129,7 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase { 'filename' => $file['filename'], 'filepath' => $file['filepath'], 'mimetype' => 'image/jpeg', + 'isexternalfile' => false, 'filesize' => strlen($content), 'timemodified' => $file['timemodified'], 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],