MDL-40038 - web services: Added an alternative to using the context id.

Context id is more of an internal Moodle identifier, so we have now included
contextlevel and instanceid in the funciton core_files_external::get_files().
This commit is contained in:
Adrian Greeve
2013-08-15 15:28:38 +08:00
parent 50ff861263
commit dbb8cd36bd
+37 -15
View File
@@ -47,13 +47,16 @@ class core_files_external extends external_api {
public static function get_files_parameters() {
return new external_function_parameters(
array(
'contextid' => new external_value(PARAM_INT, 'context id'),
'component' => new external_value(PARAM_TEXT, 'component'),
'filearea' => new external_value(PARAM_TEXT, 'file area'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'filepath' => new external_value(PARAM_PATH, 'file path'),
'filename' => new external_value(PARAM_FILE, 'file name'),
'modified' => new external_value(PARAM_INT, 'timestamp to return files changed after this time.', VALUE_DEFAULT, null)
'contextid' => new external_value(PARAM_INT, 'context id Set to -1 to use contextlevel and instanceid.'),
'component' => new external_value(PARAM_TEXT, 'component'),
'filearea' => new external_value(PARAM_TEXT, 'file area'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'filepath' => new external_value(PARAM_PATH, 'file path'),
'filename' => new external_value(PARAM_FILE, 'file name'),
'modified' => new external_value(PARAM_INT, 'timestamp to return files changed after this time.', VALUE_DEFAULT, null),
'contextlevel' => new external_value(PARAM_ALPHA, 'The context level for the file location.', VALUE_DEFAULT, null),
'instanceid' => new external_value(PARAM_INT, 'The instance id for where the file is located.', VALUE_DEFAULT, null)
)
);
}
@@ -68,22 +71,41 @@ class core_files_external extends external_api {
* @param string $filepath file path
* @param string $filename file name
* @param int $modified timestamp to return files changed after this time.
* @param string $contextlevel The context level for the file location.
* @param int $instanceid The instance id for where the file is located.
* @return array
* @since Moodle 2.2
*/
public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null) {
global $CFG, $USER, $OUTPUT;
$fileinfo = self::validate_parameters(self::get_files_parameters(), array(
'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea,
'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'modified'=>$modified));
public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null,
$contextlevel = null, $instanceid = null) {
$parameters = array(
'contextid' => $contextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $itemid,
'filepath' => $filepath,
'filename' => $filename,
'modified' => $modified,
'contextlevel' => $contextlevel,
'instanceid' => $instanceid);
$fileinfo = self::validate_parameters(self::get_files_parameters(), $parameters);
$browser = get_file_browser();
if (empty($fileinfo['contextid'])) {
$context = context_system::instance();
// We need to preserve backwards compatibility. Zero will use the system context and minus one will
// use the addtional parameters to determine the context.
// TODO MDL-40489 get_context_from_params should handle this logic.
if ($fileinfo['contextid'] == 0) {
$context = context_system::instance();
} else {
$context = context::instance_by_id($fileinfo['contextid']);
if ($fileinfo['contextid'] == -1) {
$fileinfo['contextid'] = null;
}
$context = self::get_context_from_params($fileinfo);
}
self::validate_context($context);
if (empty($fileinfo['component'])) {
$fileinfo['component'] = null;
}