Skip to content

Commit

Permalink
MDL-79216 files: new report filter type for filesize data.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Dec 1, 2023
1 parent abba174 commit 0d9b79f
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 11 deletions.
12 changes: 3 additions & 9 deletions files/classes/reportbuilder/local/entities/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\helpers\format;
use core_reportbuilder\local\filters\{boolean_select, date, number, select, text};
use core_reportbuilder\local\filters\{boolean_select, date, filesize, select, text};
use core_reportbuilder\local\report\{column, filter};

/**
Expand Down Expand Up @@ -366,19 +366,13 @@ protected function get_all_filters(): array {

// Size.
$filters[] = (new filter(
number::class,
filesize::class,
'size',
new lang_string('size'),
$this->get_entity_name(),
"{$filesalias}.filesize"
))
->add_joins($this->get_joins())
->set_limited_operators([
number::ANY_VALUE,
number::LESS_THAN,
number::GREATER_THAN,
number::RANGE,
]);
->add_joins($this->get_joins());

// Type.
$filters[] = (new filter(
Expand Down
5 changes: 3 additions & 2 deletions files/tests/reportbuilder/datasource/files_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use core\context\{course, coursecat, user};
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, number, select, text};
use core_reportbuilder\local\filters\{boolean_select, date, filesize, select, text};

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -219,8 +219,9 @@ public function datasource_filters_provider(): array {
'file:name_value' => 'Hello.txt',
], 2],
'Filter size' => ['file:size', [
'file:size_operator' => number::GREATER_THAN,
'file:size_operator' => filesize::GREATER_THAN,
'file:size_value1' => 2,
'file:size_unit' => filesize::SIZE_UNIT_BYTE,
], 2],
'Filter type' => ['file:type', [
'file:type_operator' => select::EQUAL_TO,
Expand Down
1 change: 1 addition & 0 deletions lang/en/reportbuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
$string['filterequalorgreaterthan'] = 'Greater than or equal';
$string['filterequalorlessthan'] = 'Less than or equal';
$string['filterfieldoperator'] = '{$a} operator';
$string['filterfieldunit'] = '{$a} unit';
$string['filterfieldvalue'] = '{$a} value';
$string['filtergreaterthan'] = 'Greater than';
$string['filterinvalid'] = 'Invalid filter';
Expand Down
151 changes: 151 additions & 0 deletions reportbuilder/classes/local/filters/filesize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_reportbuilder\local\filters;

use core_reportbuilder\local\helpers\database;
use lang_string;
use MoodleQuickForm;

/**
* Filesize report filter
*
* @package core_reportbuilder
* @copyright 2023 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filesize extends base {

/** @var int Any value */
public const ANY_VALUE = 0;

/** @var int Less than */
public const LESS_THAN = 3;

/** @var int Greater than */
public const GREATER_THAN = 4;

/** @var int Bytes */
public const SIZE_UNIT_BYTE = 1;

/** @var int Kilobytes */
public const SIZE_UNIT_KILOBYTE = 1024;

/** @var int Megabytes */
public const SIZE_UNIT_MEGABYTE = self::SIZE_UNIT_KILOBYTE * 1024;

/** @var int Gigabytes */
public const SIZE_UNIT_GIGABYTE = self::SIZE_UNIT_MEGABYTE * 1024;

/**
* Return an array of operators available for this filter
*
* @return lang_string[]
*/
private function get_operators(): array {
$operators = [
self::ANY_VALUE => new lang_string('filterisanyvalue', 'core_reportbuilder'),
self::LESS_THAN => new lang_string('filterlessthan', 'core_reportbuilder'),
self::GREATER_THAN => new lang_string('filtergreaterthan', 'core_reportbuilder'),
];

return $this->filter->restrict_limited_operators($operators);
}

/**
* Setup form
*
* @param MoodleQuickForm $mform
*/
public function setup_form(MoodleQuickForm $mform): void {
// Operator selector.
$elements[] = $mform->createElement('select', "{$this->name}_operator",
get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()), $this->get_operators());
$mform->setType("{$this->name}_operator", PARAM_INT);
$mform->setDefault("{$this->name}_operator", self::ANY_VALUE);

// Value selector.
$elements[] = $mform->createElement('text', "{$this->name}_value1",
get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()), ['size' => 4]);
$mform->setType("{$this->name}_value1", PARAM_FLOAT);
$mform->setDefault("{$this->name}_value1", 1);
$mform->hideIf("{$this->name}_value1", "{$this->name}_operator", 'eq', self::ANY_VALUE);

// Unit selector.
$units = [
self::SIZE_UNIT_BYTE => new lang_string('sizeb'),
self::SIZE_UNIT_KILOBYTE => new lang_string('sizekb'),
self::SIZE_UNIT_MEGABYTE => new lang_string('sizemb'),
self::SIZE_UNIT_GIGABYTE => new lang_string('sizegb'),
];

$elements[] = $mform->createElement('select', "{$this->name}_unit",
get_string('filterfieldunit', 'core_reportbuilder', $this->get_header()), $units);
$mform->setType("{$this->name}_unit", PARAM_INT);
$mform->setDefault("{$this->name}_unit", self::SIZE_UNIT_BYTE);
$mform->hideIf("{$this->name}_unit", "{$this->name}_operator", 'eq', self::ANY_VALUE);

$mform->addElement('group', "{$this->name}_group", '', $elements, '', false);
}

/**
* Return filter SQL
*
* @param array $values
* @return array
*/
public function get_sql_filter(array $values): array {
$fieldsql = $this->filter->get_field_sql();
$params = $this->filter->get_field_params();

$operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);

$filesizevalue = unformat_float($values["{$this->name}_value1"] ?? 1);
$filesizeunit = (int) ($values["{$this->name}_unit"] ?? self::SIZE_UNIT_BYTE);

$paramfilesize = database::generate_param_name();
$params[$paramfilesize] = $filesizevalue * $filesizeunit;

switch ($operator) {
case self::LESS_THAN:
$sql = "{$fieldsql} < :{$paramfilesize}";
break;
case self::GREATER_THAN:
$sql = "{$fieldsql} > :{$paramfilesize}";
break;
default:
// Invalid or inactive filter.
return ['', []];
}

return [$sql, $params];
}

/**
* Return sample filter values
*
* @return array
*/
public function get_sample_values(): array {
return [
"{$this->name}_operator" => self::GREATER_THAN,
"{$this->name}_value1" => 1,
"{$this->name}_unit" => self::SIZE_UNIT_KILOBYTE,
];
}
}
111 changes: 111 additions & 0 deletions reportbuilder/tests/local/filters/filesize_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_reportbuilder\local\filters;

use advanced_testcase;
use core\context\user;
use core_reportbuilder\local\report\filter;
use lang_string;

/**
* Unit tests for filesize report filter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\local\filters\filesize
* @copyright 2023 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filesize_test extends advanced_testcase {

/**
* Data provider for {@see test_get_sql_filter}
*
* @return array
*/
public static function get_sql_filter_provider(): array {
return [
[filesize::ANY_VALUE, true],

[filesize::LESS_THAN, false, 10, filesize::SIZE_UNIT_BYTE],
[filesize::LESS_THAN, false, 10, filesize::SIZE_UNIT_KILOBYTE],
[filesize::LESS_THAN, true, 10, filesize::SIZE_UNIT_MEGABYTE],
[filesize::LESS_THAN, true, 10, filesize::SIZE_UNIT_GIGABYTE],

[filesize::GREATER_THAN, true, 10, filesize::SIZE_UNIT_BYTE],
[filesize::GREATER_THAN, true, 10, filesize::SIZE_UNIT_KILOBYTE],
[filesize::GREATER_THAN, false, 10, filesize::SIZE_UNIT_MEGABYTE],
[filesize::GREATER_THAN, false, 10, filesize::SIZE_UNIT_GIGABYTE],
];
}

/**
* Test getting filter SQL
*
* @param int $operator
* @param bool $expected
* @param float $value
* @param int $unit
*
* @dataProvider get_sql_filter_provider
*/
public function test_get_sql_filter(
int $operator,
bool $expected,
float $value = 1,
int $unit = filesize::SIZE_UNIT_BYTE,
): void {
global $DB, $USER;

$this->resetAfterTest();
$this->setAdminUser();

// Create a sample 2MB file.
$file = get_file_storage()->create_file_from_string([
'contextid' => user::instance($USER->id)->id,
'userid' => $USER->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => file_get_unused_draft_itemid(),
'filepath' => '/',
'filename' => 'Hello.txt',
], str_repeat('A', 2 * filesize::SIZE_UNIT_MEGABYTE));

$filter = new filter(
filesize::class,
'test',
new lang_string('yes'),
'testentity',
'filesize'
);

// Create instance of our filter, passing given values.
[$select, $params] = filesize::create($filter)->get_sql_filter([
$filter->get_unique_identifier() . '_operator' => $operator,
$filter->get_unique_identifier() . '_value1' => $value,
$filter->get_unique_identifier() . '_unit' => $unit,
]);

$fileids = $DB->get_fieldset_select('files', 'id', $select, $params);
if ($expected) {
$this->assertContains((string) $file->get_id(), $fileids);
} else {
$this->assertNotContains((string) $file->get_id(), $fileids);
}
}
}
2 changes: 2 additions & 0 deletions reportbuilder/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Information provided here is intended especially for developers.
* New methods `get_identity_[columns|filters]` in user entity, for retrieving all user identity field report elements
* The database helper `generate_alias[es]` and `generate_param_name[s]` methods now accept an optional `$suffix` argument for
appending additional string to the generated value
* New report filter types:
- `filesize` for reports containing filesize data

=== 4.3 ===

Expand Down

0 comments on commit 0d9b79f

Please sign in to comment.