forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-79216 files: new report filter type for filesize data.
- Loading branch information
1 parent
abba174
commit 0d9b79f
Showing
6 changed files
with
271 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters