MDL-33399 files: Fixed up handling of empty sort in File API methods.

The following three methods of file_storage have been patched:
* get_external_files
* get_area_files
* get_directory_files
This commit is contained in:
Sam Hemelryk
2012-05-29 16:38:47 +12:00
parent 4631e39533
commit 9f4789b842
+17 -11
View File
@@ -366,7 +366,7 @@ class file_storage {
* Returns all files belonging to given repository
*
* @param int $repositoryid
* @param string $sort
* @param string $sort A fragment of SQL to use for sorting
*/
public function get_external_files($repositoryid, $sort = 'sortorder, itemid, filepath, filename') {
global $DB;
@@ -374,8 +374,10 @@ class file_storage {
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE r.repositoryid = ?
ORDER BY $sort";
WHERE r.repositoryid = ?";
if (!empty($sort)) {
$sql .= " ORDER BY {$sort}";
}
$result = array();
$filerecords = $DB->get_records_sql($sql, array($repositoryid));
@@ -392,11 +394,11 @@ class file_storage {
* @param string $component component
* @param string $filearea file area
* @param int $itemid item ID or all files if not specified
* @param string $sort sort fields
* @param string $sort A fragment of SQL to use for sorting
* @param bool $includedirs whether or not include directories
* @return array of stored_files indexed by pathanmehash
*/
public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) {
public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "sortorder, itemid, filepath, filename", $includedirs = true) {
global $DB;
$conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
@@ -414,8 +416,10 @@ class file_storage {
WHERE f.contextid = :contextid
AND f.component = :component
AND f.filearea = :filearea
$itemidsql
ORDER BY $sort";
$itemidsql";
if (!empty($sort)) {
$sql .= " ORDER BY {$sort}";
}
$result = array();
$filerecords = $DB->get_records_sql($sql, $conditions);
@@ -489,7 +493,7 @@ class file_storage {
* @param int $filepath directory path
* @param bool $recursive include all subdirectories
* @param bool $includedirs include files and directories
* @param string $sort sort fields
* @param string $sort A fragment of SQL to use for sorting
* @return array of stored_files indexed by pathanmehash
*/
public function get_directory_files($contextid, $component, $filearea, $itemid, $filepath, $recursive = false, $includedirs = true, $sort = "filepath, filename") {
@@ -499,6 +503,8 @@ class file_storage {
return array();
}
$orderby = (!empty($sort)) ? " ORDER BY {$sort}" : '';
if ($recursive) {
$dirs = $includedirs ? "" : "AND filename <> '.'";
@@ -512,7 +518,7 @@ class file_storage {
AND ".$DB->sql_substr("f.filepath", 1, $length)." = :filepath
AND f.id <> :dirid
$dirs
ORDER BY $sort";
$orderby";
$params = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'dirid'=>$directory->get_id());
$files = array();
@@ -542,7 +548,7 @@ class file_storage {
AND f.itemid = :itemid AND f.filename = '.'
AND ".$DB->sql_substr("f.filepath", 1, $length)." = :filepath
AND f.id <> :dirid
ORDER BY $sort";
$orderby";
$reqlevel = substr_count($filepath, '/') + 1;
$filerecords = $DB->get_records_sql($sql, $params);
foreach ($filerecords as $filerecord) {
@@ -559,7 +565,7 @@ class file_storage {
ON f.referencefileid = r.id
WHERE f.contextid = :contextid AND f.component = :component AND f.filearea = :filearea AND f.itemid = :itemid
AND f.filepath = :filepath AND f.filename <> '.'
ORDER BY $sort";
$orderby";
$filerecords = $DB->get_records_sql($sql, $params);
foreach ($filerecords as $filerecord) {