Skip to content

Commit

Permalink
MDL-58399 webservice: Return additional file fields for repositories
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jleyva committed Mar 28, 2017
1 parent 216ea39 commit 1104a9f
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ public static function get_course_contents_returns() {
'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'),
Expand Down
18 changes: 18 additions & 0 deletions lib/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,10 @@ public static function get_area_files($contextid, $component, $filearea, $itemid
$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);
Expand Down Expand Up @@ -1345,6 +1349,8 @@ public function __construct($desc = 'List of files.', $required = VALUE_REQUIRED
'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.'
),
Expand Down Expand Up @@ -1397,6 +1403,18 @@ public static function get_properties_for_exporter() {
'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,
),
];
}
}
16 changes: 16 additions & 0 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,22 @@ public function get_repository_id() {
}
}

/**
* 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
Expand Down
4 changes: 3 additions & 1 deletion lib/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ public function test_external_util_get_area_files() {
'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);
Expand All @@ -529,7 +530,8 @@ public function test_external_files() {
$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);
Expand Down
5 changes: 5 additions & 0 deletions mod/book/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
5 changes: 5 additions & 0 deletions mod/folder/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ public function test_mod_forum_get_forum_discussion_posts() {
$discussion1reply1->id, '/', $filename),
'timemodified' => $timepost,
'mimetype' => 'image/jpeg',
'isexternalfile' => false,
)
),
'totalscore' => $discussion1reply1->totalscore,
Expand Down
5 changes: 5 additions & 0 deletions mod/imscp/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions mod/page/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions mod/resource/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions mod/wiki/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ public function test_get_subwiki_files_collaborative_no_groups() {
'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'],
Expand Down Expand Up @@ -1128,6 +1129,7 @@ public function test_get_subwiki_files_visible_groups_individual() {
'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'],
Expand Down

0 comments on commit 1104a9f

Please sign in to comment.