MDL-54941 webservice: Refactor file handling in WS

This commit reduces boilerplate code.
It also forces WS returning files to always return the following
fields: filename, filepath, mimetype, filesize, timemodified and
fileurl.
This commit is contained in:
Juan Leyva 2016-07-12 16:38:38 +01:00
parent d1a3ea62ef
commit 145900708c
11 changed files with 46 additions and 160 deletions

View File

@ -2267,7 +2267,10 @@ class core_course_external extends external_api {
$files[] = array(
'filename' => $file->get_filename(),
'fileurl' => $fileurl,
'filesize' => $file->get_filesize()
'filesize' => $file->get_filesize(),
'filepath' => $file->get_filepath(),
'mimetype' => $file->get_mimetype(),
'timemodified' => $file->get_timemodified(),
);
}
@ -2337,16 +2340,7 @@ class core_course_external extends external_api {
'categoryname' => new external_value(PARAM_TEXT, 'category name'),
'summary' => new external_value(PARAM_RAW, 'summary'),
'summaryformat' => new external_format_value('summary'),
'overviewfiles' => new external_multiple_structure(
new external_single_structure(
array(
'filename' => new external_value(PARAM_FILE, 'overview file name'),
'fileurl' => new external_value(PARAM_URL, 'overview file url'),
'filesize' => new external_value(PARAM_INT, 'overview file size'),
)
),
'additional overview files attached to this course'
),
'overviewfiles' => new external_files('additional overview files attached to this course'),
'contacts' => new external_multiple_structure(
new external_single_structure(
array(

View File

@ -4,4 +4,7 @@ information provided here is intended especially for developers.
=== 3.2 ===
* External function core_course_external::get_course_contents now returns the section's number in the course (new section field).
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.

View File

@ -455,22 +455,8 @@ class mod_assign_external extends external_api {
$assignment['introfiles'] = external_util::get_area_files($context->id, 'mod_assign', 'intro', false,
false);
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
0, 'timemodified', false)) {
$assignment['introattachments'] = array();
foreach ($files as $file) {
$filename = $file->get_filename();
$assignment['introattachments'][] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => moodle_url::make_webservice_pluginfile_url(
$context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0, '/', $filename)->out(false)
);
}
}
$assignment['introattachments'] = external_util::get_area_files($context->id, 'mod_assign',
ASSIGN_INTROATTACHMENT_FILEAREA, 0);
}
if ($module->requiresubmissionstatement) {
@ -541,15 +527,7 @@ class mod_assign_external extends external_api {
'assignment intro, not allways returned because it deppends on the activity configuration', VALUE_OPTIONAL),
'introformat' => new external_format_value('intro', VALUE_OPTIONAL),
'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL),
'introattachments' => new external_multiple_structure(
new external_single_structure(
array (
'filename' => new external_value(PARAM_FILE, 'file name'),
'mimetype' => new external_value(PARAM_RAW, 'mime type'),
'fileurl' => new external_value(PARAM_URL, 'file download url')
)
), 'intro attachments files', VALUE_OPTIONAL
)
'introattachments' => new external_files('intro attachments files', VALUE_OPTIONAL),
), 'assignment information object');
}
@ -637,24 +615,14 @@ class mod_assign_external extends external_api {
$fileareas = $assignplugin->get_file_areas();
foreach ($fileareas as $filearea => $name) {
$fileareainfo = array('area' => $filearea);
$files = $fs->get_area_files(
$fileareainfo['files'] = external_util::get_area_files(
$assign->get_context()->id,
$component,
$filearea,
$item->id,
"timemodified",
false
$item->id
);
foreach ($files as $file) {
$filepath = $file->get_filepath().$file->get_filename();
$fileurl = file_encode_url($CFG->wwwroot . '/webservice/pluginfile.php', '/' . $assign->get_context()->id .
'/' . $component. '/'. $filearea . '/' . $item->id . $filepath);
$fileinfo = array(
'filepath' => $filepath,
'fileurl' => $fileurl
);
$fileareainfo['files'][] = $fileinfo;
}
$plugin['fileareas'][] = $fileareainfo;
}
@ -822,15 +790,7 @@ class mod_assign_external extends external_api {
new external_single_structure(
array (
'area' => new external_value (PARAM_TEXT, 'file area'),
'files' => new external_multiple_structure(
new external_single_structure(
array (
'filepath' => new external_value (PARAM_TEXT, 'file path'),
'fileurl' => new external_value (PARAM_URL, 'file download url',
VALUE_OPTIONAL)
)
), 'files', VALUE_OPTIONAL
)
'files' => new external_files('files', VALUE_OPTIONAL),
)
), 'fileareas', VALUE_OPTIONAL
),

View File

@ -1945,7 +1945,8 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
$submissionplugins[$plugin['type']] = $plugin;
}
$this->assertEquals('Submission text', $submissionplugins['onlinetext']['editorfields'][0]['text']);
$this->assertEquals('/t.txt', $submissionplugins['file']['fileareas'][0]['files'][0]['filepath']);
$this->assertEquals('/', $submissionplugins['file']['fileareas'][0]['files'][0]['filepath']);
$this->assertEquals('t.txt', $submissionplugins['file']['fileareas'][0]['files'][0]['filename']);
}
/**
@ -2087,7 +2088,8 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
$submissionplugins[$plugin['type']] = $plugin;
}
$this->assertEquals('Submission text', $submissionplugins['onlinetext']['editorfields'][0]['text']);
$this->assertEquals('/t.txt', $submissionplugins['file']['fileareas'][0]['files'][0]['filepath']);
$this->assertEquals('/', $submissionplugins['file']['fileareas'][0]['files'][0]['filepath']);
$this->assertEquals('t.txt', $submissionplugins['file']['fileareas'][0]['files'][0]['filename']);
}
/**

View File

@ -7,6 +7,10 @@ This files describes API changes in the assign code.
* Proper checking for empty submissions
* Submission modification time checking - this will help students working in groups not clobber each others'
submissions
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.
Please, note that previously the filename was part of the filepath field, now they are separated.
=== 3.1 ===
* The feedback plugins now need to implement the is_feedback_modified() method. The default is to return true

View File

@ -283,22 +283,7 @@ class mod_forum_external extends external_api {
// List attachments.
if (!empty($post->attachment)) {
$post->attachments = array();
$fs = get_file_storage();
if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment', $post->id, "filename", false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$fileurl = moodle_url::make_webservice_pluginfile_url(
$modcontext->id, 'mod_forum', 'attachment', $post->id, '/', $filename);
$post->attachments[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => $fileurl->out(false)
);
}
}
$post->attachments = external_util::get_area_files($modcontext->id, 'mod_forum', 'attachment', $post->id);
}
$posts[] = $post;
@ -334,15 +319,7 @@ class mod_forum_external extends external_api {
'messageformat' => new external_format_value('message'),
'messagetrust' => new external_value(PARAM_INT, 'Can we trust?'),
'attachment' => new external_value(PARAM_RAW, 'Has attachments?'),
'attachments' => new external_multiple_structure(
new external_single_structure(
array (
'filename' => new external_value(PARAM_FILE, 'file name'),
'mimetype' => new external_value(PARAM_RAW, 'mime type'),
'fileurl' => new external_value(PARAM_URL, 'file download url')
)
), 'attachments', VALUE_OPTIONAL
),
'attachments' => new external_files('attachments', VALUE_OPTIONAL),
'totalscore' => new external_value(PARAM_INT, 'The post message total score'),
'mailnow' => new external_value(PARAM_INT, 'Mail now?'),
'children' => new external_multiple_structure(new external_value(PARAM_INT, 'children post id')),
@ -518,22 +495,8 @@ class mod_forum_external extends external_api {
// List attachments.
if (!empty($discussion->attachment)) {
$discussion->attachments = array();
$fs = get_file_storage();
if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment',
$discussion->id, "filename", false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$discussion->attachments[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => file_encode_url($CFG->wwwroot.'/webservice/pluginfile.php',
'/'.$modcontext->id.'/mod_forum/attachment/'.$discussion->id.'/'.$filename)
);
}
}
$discussion->attachments = external_util::get_area_files($modcontext->id, 'mod_forum', 'attachment',
$discussion->id);
}
$discussions[] = $discussion;
@ -577,15 +540,7 @@ class mod_forum_external extends external_api {
'messageformat' => new external_format_value('message'),
'messagetrust' => new external_value(PARAM_INT, 'Can we trust?'),
'attachment' => new external_value(PARAM_RAW, 'Has attachments?'),
'attachments' => new external_multiple_structure(
new external_single_structure(
array (
'filename' => new external_value(PARAM_FILE, 'file name'),
'mimetype' => new external_value(PARAM_RAW, 'mime type'),
'fileurl' => new external_value(PARAM_URL, 'file download url')
)
), 'attachments', VALUE_OPTIONAL
),
'attachments' => new external_files('attachments', VALUE_OPTIONAL),
'totalscore' => new external_value(PARAM_INT, 'The post message total score'),
'mailnow' => new external_value(PARAM_INT, 'Mail now?'),
'userfullname' => new external_value(PARAM_TEXT, 'Post author full name'),

View File

@ -30,6 +30,9 @@ information provided here is intended especially for developers.
- forum_get_subscribed_forums
- forum_get_optional_subscribed_forums
- forum_get_potential_subscribers
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.
=== 3.1 ===
* The inteface to forum_get_email_message_id() has changed and no longer needs the $host argument.

View File

@ -91,13 +91,7 @@ class mod_glossary_external extends external_api {
'definitionformat' => new external_format_value('definition'),
'definitiontrust' => new external_value(PARAM_BOOL, 'The definition trust flag'),
'attachment' => new external_value(PARAM_BOOL, 'Whether or not the entry has attachments'),
'attachments' => new external_multiple_structure(
new external_single_structure(array(
'filename' => new external_value(PARAM_FILE, 'File name'),
'mimetype' => new external_value(PARAM_RAW, 'Mime type'),
'fileurl' => new external_value(PARAM_URL, 'File download URL')
)), 'attachments', VALUE_OPTIONAL
),
'attachments' => new external_files('attachments', VALUE_OPTIONAL),
'timecreated' => new external_value(PARAM_INT, 'Time created'),
'timemodified' => new external_value(PARAM_INT, 'Time modified'),
'teacherentry' => new external_value(PARAM_BOOL, 'The entry was created by a teacher, or equivalent.'),
@ -148,19 +142,7 @@ class mod_glossary_external extends external_api {
$entry->attachment = !empty($entry->attachment) ? 1 : 0;
$entry->attachments = array();
if ($entry->attachment) {
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id, 'filename', false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$fileurl = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_glossary', 'attachment',
$entry->id, '/', $filename);
$entry->attachments[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => $fileurl->out(false)
);
}
}
$files = external_util::get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
}
}

View File

@ -1,6 +1,11 @@
This files describes API changes in /mod/glossary/*,
information provided here is intended especially for developers.
=== 3.2 ===
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.
=== 2.8 ===
* The glossary_print_entry_attachment function no longer takes an `align`
or `insidetable` property. Instead the attachments are printed within a

View File

@ -728,23 +728,7 @@ class mod_wiki_external extends external_api {
throw new moodle_exception('cannotviewfiles', 'wiki');
} else if ($subwiki->id != -1) {
// The subwiki exists, let's get the files.
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_wiki', 'attachments', $subwiki->id, 'filename', false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$fileurl = moodle_url::make_webservice_pluginfile_url(
$context->id, 'mod_wiki', 'attachments', $subwiki->id, '/', $filename);
$returnedfiles[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => $fileurl->out(false),
'filepath' => $file->get_filepath(),
'filesize' => $file->get_filesize(),
'timemodified' => $file->get_timemodified()
);
}
}
$returnedfiles = external_util::get_area_files($context->id, 'mod_wiki', 'attachments', $subwiki->id);
}
$result = array();
@ -763,18 +747,7 @@ class mod_wiki_external extends external_api {
return new external_single_structure(
array(
'files' => new external_multiple_structure(
new external_single_structure(
array(
'filename' => new external_value(PARAM_FILE, 'File name.'),
'filepath' => new external_value(PARAM_PATH, 'File path.'),
'filesize' => new external_value(PARAM_INT, 'File size.'),
'fileurl' => new external_value(PARAM_URL, 'Downloadable file url.'),
'timemodified' => new external_value(PARAM_INT, 'Time modified.'),
'mimetype' => new external_value(PARAM_RAW, 'File mime type.'),
), 'Files'
)
),
'files' => new external_files('Files'),
'warnings' => new external_warnings(),
)
);

View File

@ -1,5 +1,10 @@
This files describes API changes in /mod/wiki/*,
information provided here is intended especially for developers.
=== 3.2 ===
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.
=== 3.1 ===
* Added a new param $sort to wiki_get_page_list function. Default value behaves exactly like before (sort by title ASC).