MDL-77807 files: normalise file entity size/type column fields.

The `filename` field was only used by each to determine whether the
file was itself a directory, and it's presence meant that aggregation
of each column wasn't working properly.
This commit is contained in:
Paul Holden 2023-03-30 20:40:24 +01:00
parent 449a8b0b31
commit fbb6dc4ca3
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164

View File

@ -109,11 +109,12 @@ class file extends base {
))
->add_joins($this->get_joins())
->set_type(column::TYPE_INTEGER)
->add_fields("{$filesalias}.filesize, {$filesalias}.filename")
->add_field("{$filesalias}.filesize")
->add_field("CASE WHEN {$filesalias}.filename = '.' THEN 1 ELSE 0 END", 'directory')
->set_is_sortable(true)
->add_callback(static function($filesize, stdClass $fileinfo): string {
// Absent file size and/or directory should not return output.
if ($fileinfo->filesize === null || $fileinfo->filename === '.') {
if ($fileinfo->filesize === null || $fileinfo->directory) {
return '';
}
return display_size($fileinfo->filesize);
@ -138,20 +139,21 @@ class file extends base {
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$filesalias}.mimetype, {$filesalias}.filename")
->add_field("{$filesalias}.mimetype")
->add_field("CASE WHEN {$filesalias}.filename = '.' THEN 1 ELSE 0 END", 'directory')
->set_is_sortable(true)
->add_callback(static function($mimetype, stdClass $fileinfo): string {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");
// Absent mime type and/or directory has pre-determined output.
if ($fileinfo->filename === '.') {
return get_string('directory');
} else if ($fileinfo->mimetype === null) {
if ($fileinfo->mimetype === null && !$fileinfo->directory) {
return '';
} else if ($fileinfo->directory) {
return get_string('directory');
}
return get_mimetype_description($fileinfo, true);
return get_mimetype_description($fileinfo->mimetype);
});
// Author.