MDL-79200 files: mimetype filter, plus icon column in report entity.
This commit is contained in:
@@ -20,9 +20,11 @@ namespace core_files\reportbuilder\local\entities;
|
||||
|
||||
use context;
|
||||
use context_helper;
|
||||
use core_collator;
|
||||
use core_filetypes;
|
||||
use html_writer;
|
||||
use lang_string;
|
||||
use license_manager;
|
||||
use html_writer;
|
||||
use stdClass;
|
||||
use core_reportbuilder\local\entities\base;
|
||||
use core_reportbuilder\local\helpers\format;
|
||||
@@ -156,6 +158,36 @@ class file extends base {
|
||||
return get_mimetype_description($fileinfo->mimetype);
|
||||
});
|
||||
|
||||
// Icon.
|
||||
$columns[] = (new column(
|
||||
'icon',
|
||||
new lang_string('icon'),
|
||||
$this->get_entity_name()
|
||||
))
|
||||
->add_joins($this->get_joins())
|
||||
->set_type(column::TYPE_TEXT)
|
||||
->add_field("{$filesalias}.mimetype")
|
||||
->add_field("CASE WHEN {$filesalias}.filename = '.' THEN 1 ELSE 0 END", 'directory')
|
||||
->set_disabled_aggregation_all()
|
||||
->add_callback(static function($mimetype, stdClass $fileinfo): string {
|
||||
global $CFG, $OUTPUT;
|
||||
require_once("{$CFG->libdir}/filelib.php");
|
||||
|
||||
if ($fileinfo->mimetype === null && !$fileinfo->directory) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($fileinfo->directory) {
|
||||
$icon = file_folder_icon();
|
||||
$description = get_string('directory');
|
||||
} else {
|
||||
$icon = file_file_icon($fileinfo);
|
||||
$description = get_mimetype_description($fileinfo->mimetype);
|
||||
}
|
||||
|
||||
return $OUTPUT->pix_icon($icon, $description, 'moodle', ['class' => 'iconsize-medium']);
|
||||
});
|
||||
|
||||
// Author.
|
||||
$columns[] = (new column(
|
||||
'author',
|
||||
@@ -348,6 +380,27 @@ class file extends base {
|
||||
number::RANGE,
|
||||
]);
|
||||
|
||||
// Type.
|
||||
$filters[] = (new filter(
|
||||
select::class,
|
||||
'type',
|
||||
new lang_string('type', 'core_repository'),
|
||||
$this->get_entity_name(),
|
||||
"{$filesalias}.mimetype"
|
||||
))
|
||||
->add_joins($this->get_joins())
|
||||
->set_options_callback(static function(): array {
|
||||
$mimetypenames = array_column(core_filetypes::get_types(), 'type');
|
||||
|
||||
// Convert the names into a map of name => description.
|
||||
$mimetypes = array_combine($mimetypenames, array_map(static function(string $mimetype): string {
|
||||
return get_mimetype_description($mimetype);
|
||||
}, $mimetypenames));
|
||||
|
||||
core_collator::asort($mimetypes);
|
||||
return $mimetypes;
|
||||
});
|
||||
|
||||
// License (consider null = 'unknown/license not specified' for filtering purposes).
|
||||
$filters[] = (new filter(
|
||||
select::class,
|
||||
|
||||
@@ -87,8 +87,9 @@ class files_test extends core_reportbuilder_testcase {
|
||||
* Test datasource columns that aren't added by default
|
||||
*/
|
||||
public function test_datasource_non_default_columns(): void {
|
||||
global $OUTPUT;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$category = $this->getDataGenerator()->create_category();
|
||||
$categorycontext = coursecat::instance($category->id);
|
||||
@@ -115,6 +116,7 @@ class files_test extends core_reportbuilder_testcase {
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:path']);
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:parent']);
|
||||
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:icon']);
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:path']);
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:author']);
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:license']);
|
||||
@@ -137,6 +139,8 @@ class files_test extends core_reportbuilder_testcase {
|
||||
'Course',
|
||||
$coursecontext->path,
|
||||
$categorycontext->get_context_name(),
|
||||
'<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
|
||||
$OUTPUT->image_url('f/folder')->out() . '" />',
|
||||
'/',
|
||||
null,
|
||||
'',
|
||||
@@ -151,6 +155,8 @@ class files_test extends core_reportbuilder_testcase {
|
||||
'Course',
|
||||
$coursecontext->path,
|
||||
$categorycontext->get_context_name(),
|
||||
'<img class="icon iconsize-medium" alt="Text file" title="Text file" src="' .
|
||||
$OUTPUT->image_url('f/text')->out() . '" />',
|
||||
'/',
|
||||
null,
|
||||
'',
|
||||
@@ -165,6 +171,8 @@ class files_test extends core_reportbuilder_testcase {
|
||||
'User',
|
||||
$usercontext->path,
|
||||
'System',
|
||||
'<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
|
||||
$OUTPUT->image_url('f/folder')->out() . '" />',
|
||||
'/',
|
||||
null,
|
||||
'',
|
||||
@@ -179,6 +187,8 @@ class files_test extends core_reportbuilder_testcase {
|
||||
'User',
|
||||
$usercontext->path,
|
||||
'System',
|
||||
'<img class="icon iconsize-medium" alt="Text file" title="Text file" src="' .
|
||||
$OUTPUT->image_url('f/text')->out() . '" />',
|
||||
'/',
|
||||
null,
|
||||
'',
|
||||
@@ -212,6 +222,14 @@ class files_test extends core_reportbuilder_testcase {
|
||||
'file:size_operator' => number::GREATER_THAN,
|
||||
'file:size_value1' => 2,
|
||||
], 2],
|
||||
'Filter type' => ['file:type', [
|
||||
'file:type_operator' => select::EQUAL_TO,
|
||||
'file:type_value' => 'text/plain',
|
||||
], 2],
|
||||
'Filter type (non match)' => ['file:type', [
|
||||
'file:type_operator' => select::EQUAL_TO,
|
||||
'file:type_value' => 'image/png',
|
||||
], 0],
|
||||
'Filter license' => ['file:license', [
|
||||
'file:license_operator' => select::EQUAL_TO,
|
||||
'file:license_value' => 'unknown',
|
||||
|
||||
Reference in New Issue
Block a user