MDL-66609: core_h5p: Add <component>_get_path_from_pluginfile method

This method has been added to all the components having some exceptions
with the way they treat the itemid in the pluginfile paths.
This commit is contained in:
Sara Arjona 2019-10-17 22:20:13 +02:00 committed by Andrew Nicols
parent 9e67f5e366
commit 810d7a3d2e
8 changed files with 177 additions and 0 deletions

View file

@ -109,3 +109,27 @@ function block_html_global_db_replace($search, $replace) {
}
$instances->close();
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function block_html_get_path_from_pluginfile(string $filearea, array $args) : array {
// This block never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -72,6 +72,10 @@ validation against and defaults to null (so, no user needed) if not provided.
* Attempting to use xsendfile via the 3rd param of readstring_accel() is now ignored.
* New H5P libraries have been added to Moodle core in /lib/h5p.
* New H5P core subsystem have been added.
* Introduced new callback for plugin developers '<component>_get_path_from_pluginfile($filearea, $args)': This will return
the itemid and filepath for the filearea and path defined in $args. It has been added in order to get the correct itemid and
filepath because some components, such as mod_page or mod_resource, add the revision to the URL where the itemid should be placed
(to prevent caching problems), but then they don't store it in database.
=== 3.7 ===

View file

@ -1753,3 +1753,27 @@ function mod_assign_user_preferences() {
return $preferences;
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_assign_get_path_from_pluginfile(string $filearea, array $args) : array {
// Assign never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -818,3 +818,27 @@ function mod_folder_core_calendar_provide_event_action(calendar_event $event,
true
);
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_folder_get_path_from_pluginfile(string $filearea, array $args) : array {
// Folder never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -571,3 +571,27 @@ function mod_page_core_calendar_provide_event_action(calendar_event $event,
true
);
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_page_get_path_from_pluginfile(string $filearea, array $args) : array {
// Page never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -584,3 +584,28 @@ function mod_resource_core_calendar_provide_event_action(calendar_event $event,
true
);
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_resource_get_path_from_pluginfile(string $filearea, array $args) : array {
// Resource never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -1887,3 +1887,27 @@ function mod_scorm_core_calendar_get_valid_event_timestart_range(\calendar_event
return [$mindate, $maxdate];
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_scorm_get_path_from_pluginfile(string $filearea, array $args) : array {
// SCORM never has an itemid (the number represents the revision but it's not stored in database).
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}

View file

@ -2177,3 +2177,31 @@ function workshop_check_updates_since(cm_info $cm, $from, $filter = array()) {
}
return $updates;
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array|null The itemid and the filepath inside the $args path, for the defined filearea.
*/
function mod_workshop_get_path_from_pluginfile(string $filearea, array $args) : ?array {
if ($filearea !== 'instructauthors' && $filearea !== 'instructreviewers' && $filearea !== 'conclusion') {
return null;
}
// Workshop only has empty itemid for some of the fileareas.
array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => 0,
'filepath' => $filepath,
];
}