Skip to content

Commit

Permalink
Merge branch 'MDL-79200' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 27, 2023
2 parents e6ea755 + 72e0fcb commit c011fc1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
55 changes: 54 additions & 1 deletion files/classes/reportbuilder/local/entities/file.php
Expand Up @@ -20,9 +20,11 @@

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;
Expand Down Expand Up @@ -156,6 +158,36 @@ protected function get_all_columns(): array {
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',
Expand Down Expand Up @@ -348,6 +380,27 @@ protected function get_all_filters(): array {
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,
Expand Down
20 changes: 19 additions & 1 deletion files/tests/reportbuilder/datasource/files_test.php
Expand Up @@ -87,8 +87,9 @@ public function test_datasource_default(): void {
* 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);
Expand All @@ -115,6 +116,7 @@ public function test_datasource_non_default_columns(): void {
$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']);
Expand All @@ -137,6 +139,8 @@ public function test_datasource_non_default_columns(): void {
'Course',
$coursecontext->path,
$categorycontext->get_context_name(),
'<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
$OUTPUT->image_url('f/folder')->out() . '" />',
'/',
null,
'',
Expand All @@ -151,6 +155,8 @@ public function test_datasource_non_default_columns(): void {
'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,
'',
Expand All @@ -165,6 +171,8 @@ public function test_datasource_non_default_columns(): void {
'User',
$usercontext->path,
'System',
'<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
$OUTPUT->image_url('f/folder')->out() . '" />',
'/',
null,
'',
Expand All @@ -179,6 +187,8 @@ public function test_datasource_non_default_columns(): void {
'User',
$usercontext->path,
'System',
'<img class="icon iconsize-medium" alt="Text file" title="Text file" src="' .
$OUTPUT->image_url('f/text')->out() . '" />',
'/',
null,
'',
Expand Down Expand Up @@ -212,6 +222,14 @@ public function datasource_filters_provider(): array {
'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',
Expand Down
6 changes: 6 additions & 0 deletions theme/boost/scss/moodle/icons.scss
Expand Up @@ -41,6 +41,12 @@ $iconsizes: map-merge((
margin-right: 0;
}

&.iconsize-medium {
font-size: $icon-medium-height;
width: $icon-medium-width;
height: $icon-medium-height;
}

&.iconsize-big {
width: $icon-big-width;
height: $icon-big-height;
Expand Down
5 changes: 5 additions & 0 deletions theme/boost/style/moodle.css
Expand Up @@ -26108,6 +26108,11 @@ body.behat-site .action-menu .dropdown-subpanel-content.show {
.icon.spacer {
margin-right: 0;
}
.icon.iconsize-medium {
font-size: 24px;
width: 24px;
height: 24px;
}
.icon.iconsize-big {
width: 64px;
height: 64px;
Expand Down
5 changes: 5 additions & 0 deletions theme/classic/style/moodle.css
Expand Up @@ -26108,6 +26108,11 @@ body.behat-site .action-menu .dropdown-subpanel-content.show {
.icon.spacer {
margin-right: 0;
}
.icon.iconsize-medium {
font-size: 24px;
width: 24px;
height: 24px;
}
.icon.iconsize-big {
width: 64px;
height: 64px;
Expand Down

0 comments on commit c011fc1

Please sign in to comment.