Merge branch 'MDL-80342' of https://github.com/paulholden/moodle
This commit is contained in:
@@ -105,7 +105,7 @@ class users extends datasource {
|
||||
$this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
|
||||
$this->add_all_from_entity($courseentity->get_entity_name());
|
||||
$this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
|
||||
['name', 'idnumber', 'customfield*'], ['name', 'idnumber', 'customfield*']);
|
||||
['cohortselect', 'name', 'idnumber', 'customfield*'], ['cohortselect', 'name', 'idnumber', 'customfield*']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,7 @@ use stdClass;
|
||||
use theme_config;
|
||||
use core_reportbuilder\local\entities\base;
|
||||
use core_reportbuilder\local\filters\boolean_select;
|
||||
use core_reportbuilder\local\filters\cohort as cohort_filter;
|
||||
use core_reportbuilder\local\filters\date;
|
||||
use core_reportbuilder\local\filters\select;
|
||||
use core_reportbuilder\local\filters\text;
|
||||
@@ -268,6 +269,16 @@ class cohort extends base {
|
||||
|
||||
$tablealias = $this->get_table_alias('cohort');
|
||||
|
||||
// Cohort select filter.
|
||||
$filters[] = (new filter(
|
||||
cohort_filter::class,
|
||||
'cohortselect',
|
||||
new lang_string('selectcohort', 'core_cohort'),
|
||||
$this->get_entity_name(),
|
||||
"{$tablealias}.id"
|
||||
))
|
||||
->add_joins($this->get_joins());
|
||||
|
||||
// Context filter.
|
||||
$filters[] = (new filter(
|
||||
select::class,
|
||||
|
||||
@@ -148,6 +148,9 @@ class cohorts_test extends core_reportbuilder_testcase {
|
||||
public function datasource_filters_provider(): array {
|
||||
return [
|
||||
// Cohort.
|
||||
'Filter cohort' => ['cohort:cohortselect', [
|
||||
'cohort:cohortselect_values' => [-1],
|
||||
], false],
|
||||
'Filter context' => ['cohort:context', [
|
||||
'cohort:context_operator' => select::EQUAL_TO,
|
||||
'cohort:context_value' => context_system::instance()->id,
|
||||
|
||||
@@ -78,6 +78,7 @@ $string['privacy:metadata:cohort_members:cohortid'] = 'The ID of the cohort';
|
||||
$string['privacy:metadata:cohort_members:timeadded'] = 'The timestamp indicating when the user was added to the cohort';
|
||||
$string['privacy:metadata:cohort_members:userid'] = 'The ID of the user which is associated to the cohort';
|
||||
$string['removeuserwarning'] = 'Removing users from a cohort may result in unenrolling of users from multiple courses which includes deleting of user settings, grades, group membership and other user information from affected courses.';
|
||||
$string['selectcohort'] = 'Select cohort';
|
||||
$string['selectfromcohort'] = 'Select members from cohort';
|
||||
$string['systemcohorts'] = 'System cohorts';
|
||||
$string['unknowncohort'] = 'Unknown cohort ({$a})!';
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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 MoodleQuickForm;
|
||||
use core_reportbuilder\local\helpers\database;
|
||||
|
||||
/**
|
||||
* Cohort selector filter class implementation
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @copyright 2024 Paul Holden <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cohort extends base {
|
||||
|
||||
/**
|
||||
* Setup form
|
||||
*
|
||||
* @param MoodleQuickForm $mform
|
||||
*/
|
||||
public function setup_form(MoodleQuickForm $mform): void {
|
||||
$mform->addElement(
|
||||
'cohort',
|
||||
"{$this->name}_values",
|
||||
get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()),
|
||||
[
|
||||
'multiple' => true,
|
||||
],
|
||||
)->setHiddenLabel(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return filter SQL
|
||||
*
|
||||
* @param array $values
|
||||
* @return array
|
||||
*/
|
||||
public function get_sql_filter(array $values): array {
|
||||
global $DB;
|
||||
|
||||
$fieldsql = $this->filter->get_field_sql();
|
||||
$params = $this->filter->get_field_params();
|
||||
|
||||
$cohortids = $values["{$this->name}_values"] ?? [];
|
||||
if (empty($cohortids)) {
|
||||
return ['', []];
|
||||
}
|
||||
|
||||
[$cohortselect, $cohortparams] = $DB->get_in_or_equal(
|
||||
$cohortids,
|
||||
SQL_PARAMS_NAMED,
|
||||
database::generate_param_name('_'),
|
||||
);
|
||||
|
||||
return ["{$fieldsql} $cohortselect", array_merge($params, $cohortparams)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sample filter values
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_sample_values(): array {
|
||||
return [
|
||||
"{$this->name}_values" => [1],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?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 lang_string;
|
||||
use core_reportbuilder\local\report\filter;
|
||||
|
||||
/**
|
||||
* Unit tests for cohort report filter
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @covers \core_reportbuilder\local\filters\cohort
|
||||
* @copyright 2024 Paul Holden <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cohort_test extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Data provider for {@see test_get_sql_filter}
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public static function get_sql_filter_provider(): array {
|
||||
return [
|
||||
'Empty' => [
|
||||
[],
|
||||
['C1', 'C2', 'C3'],
|
||||
],
|
||||
'Non-existing' => [
|
||||
[-1],
|
||||
[],
|
||||
],
|
||||
'Single cohort' => [
|
||||
['C1'],
|
||||
['C1'],
|
||||
],
|
||||
'Multiple cohorts' => [
|
||||
['C1', 'C2'],
|
||||
['C1', 'C2'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting filter SQL
|
||||
*
|
||||
* @param int[]|string[] $values
|
||||
* @param string[] $expectcohorts
|
||||
*
|
||||
* @dataProvider get_sql_filter_provider
|
||||
*/
|
||||
public function test_get_sql_filter(array $values, array $expectcohorts): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->getDataGenerator()->create_cohort(['name' => 'C1']);
|
||||
$this->getDataGenerator()->create_cohort(['name' => 'C2']);
|
||||
$this->getDataGenerator()->create_cohort(['name' => 'C3']);
|
||||
|
||||
// Create cohort lookup for convenience, transform values that refer to cohorts by name, to their ID.
|
||||
$cohortmap = $DB->get_records_menu(table: 'cohort', fields: 'name, id');
|
||||
$values = array_map(static function(int|string $value) use ($cohortmap): int {
|
||||
if (is_numeric($value)) {
|
||||
return $value;
|
||||
}
|
||||
return (int) $cohortmap[$value];
|
||||
}, $values);
|
||||
|
||||
$filter = new filter(
|
||||
cohort::class,
|
||||
'test',
|
||||
new lang_string('yes'),
|
||||
'testentity',
|
||||
'id'
|
||||
);
|
||||
|
||||
// Create instance of our filter, passing given values.
|
||||
[$select, $params] = cohort::create($filter)->get_sql_filter([
|
||||
$filter->get_unique_identifier() . '_values' => $values,
|
||||
]);
|
||||
|
||||
$cohorts = $DB->get_fieldset_select('cohort', 'name', $select, $params);
|
||||
$this->assertEqualsCanonicalizing($expectcohorts, $cohorts);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ Information provided here is intended especially for developers.
|
||||
are added. The `add_[columns|filters|conditions]_from_entity` class methods also now support wildcard matching in both `$include`
|
||||
and `$exclude` parameters
|
||||
* New report filter types:
|
||||
- `cohort` for reports containing cohort data
|
||||
- `courserole` for reports showing course enrolments
|
||||
- `filesize` for reports containing filesize data
|
||||
|
||||
=== 4.3 ===
|
||||
|
||||
@@ -81,7 +81,7 @@ class users extends datasource {
|
||||
$this->add_all_from_entity($userentity->get_entity_name());
|
||||
$this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
|
||||
$this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
|
||||
['name', 'idnumber', 'customfield*'], ['name', 'idnumber', 'customfield*']);
|
||||
['cohortselect', 'name', 'idnumber', 'customfield*'], ['cohortselect', 'name', 'idnumber', 'customfield*']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user