From 881dccaa7d8f7c6c8d00f852f753cbc70106661c Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 12 Nov 2021 10:50:26 +0000 Subject: [PATCH] MDL-73069 reportbuilder: filter/condition for selecting user. This allows a report creator to create personalised reports according to the user who is viewing the report. --- cohort/tests/datasource_test.php | 63 +++++++++++- lang/en/reportbuilder.php | 3 + reportbuilder/classes/local/entities/user.php | 11 +++ reportbuilder/classes/local/filters/user.php | 96 +++++++++++++++++++ .../tests/local/filters/user_test.php | 82 ++++++++++++++++ 5 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 reportbuilder/classes/local/filters/user.php create mode 100644 reportbuilder/tests/local/filters/user_test.php diff --git a/cohort/tests/datasource_test.php b/cohort/tests/datasource_test.php index eb6533d533c..0d47358b27f 100644 --- a/cohort/tests/datasource_test.php +++ b/cohort/tests/datasource_test.php @@ -18,8 +18,11 @@ declare(strict_types=1); namespace core_cohort\reportbuilder\datasource; -use core_reportbuilder_testcase; use core_reportbuilder_generator; +use core_reportbuilder_testcase; +use core_reportbuilder\manager; +use core_reportbuilder\local\filters\user; +use core_user; defined('MOODLE_INTERNAL') || die(); @@ -71,4 +74,62 @@ class datasource_test extends core_reportbuilder_testcase { 'Lionel Richards', // User. ], $contentrow); } + + /** + * Data provider for {@see test_cohorts_datasource_user_select} + * + * @return array[] + */ + public function cohorts_datasource_user_select_provider(): array { + return [ + ['user01', 'Cohort01'], + ['user02', 'Cohort02'], + ]; + } + + /** + * Test cohorts datasource, while adding the user select condition + * + * @param string $username + * @param string $expectedcohort + * + * @dataProvider cohorts_datasource_user_select_provider + */ + public function test_cohorts_datasource_user_select(string $username, string $expectedcohort): void { + $this->resetAfterTest(); + + // First cohort/user member. + $cohort01 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort01']); + $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']); + cohort_add_member($cohort01->id, $user01->id); + + // Second cohort/user member. + $cohort02 = $this->getDataGenerator()->create_cohort(['name' => 'Cohort02']); + $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']); + cohort_add_member($cohort02->id, $user02->id); + + /** @var core_reportbuilder_generator $generator */ + $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); + $report = $generator->create_report(['name' => 'User cohorts', 'source' => cohorts::class, 'default' => 0]); + + // Add cohort name and user fullname columns. + $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']); + $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']); + + // Add condition to limit report data to current user. + $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:userselect']); + manager::get_report_from_persistent($report)->set_condition_values([ + $condition->get('uniqueidentifier') . '_operator' => user::USER_CURRENT, + ]); + + // Switch user, request report. + $currentuser = core_user::get_user_by_username($username); + $this->setUser($currentuser); + + $content = $this->get_custom_report_content($report->get('id')); + $this->assertCount(1, $content); + + $contentrow = array_values(reset($content)); + $this->assertEquals([$expectedcohort, $username], $contentrow); + } } diff --git a/lang/en/reportbuilder.php b/lang/en/reportbuilder.php index 7689942c182..97527ed85b5 100644 --- a/lang/en/reportbuilder.php +++ b/lang/en/reportbuilder.php @@ -191,9 +191,12 @@ $string['timecreated'] = 'Time created'; $string['timemodified'] = 'Time modified'; $string['uniquerows'] = 'Show unique rows'; $string['uniquerows_help'] = 'Show only unique rows in the report. Note this setting has no effect if any report columns are being aggregated'; +$string['userany'] = 'Any user'; +$string['usercurrent'] = 'Current user'; $string['userfullnamewithlink'] = 'Full name with link'; $string['userfullnamewithpicture'] = 'Full name with picture'; $string['userfullnamewithpicturelink'] = 'Full name with picture and link'; $string['usermodified'] = 'Modified by'; $string['userpicture'] = 'User picture'; +$string['userselect'] = 'Select user'; $string['viewreport'] = 'View report'; diff --git a/reportbuilder/classes/local/entities/user.php b/reportbuilder/classes/local/entities/user.php index 3e8c1f7ea56..14feaa3b5e5 100644 --- a/reportbuilder/classes/local/entities/user.php +++ b/reportbuilder/classes/local/entities/user.php @@ -28,6 +28,7 @@ use core_reportbuilder\local\filters\boolean_select; use core_reportbuilder\local\filters\date; use core_reportbuilder\local\filters\select; use core_reportbuilder\local\filters\text; +use core_reportbuilder\local\filters\user as user_filter; use core_reportbuilder\local\helpers\user_profile_fields; use core_reportbuilder\local\helpers\format; use core_reportbuilder\local\report\column; @@ -411,6 +412,16 @@ class user extends base { $filters[] = $filter; } + // User select filter. + $filters[] = (new filter( + user_filter::class, + 'userselect', + new lang_string('userselect', 'core_reportbuilder'), + $this->get_entity_name(), + "{$tablealias}.id" + )) + ->add_joins($this->get_joins()); + return $filters; } diff --git a/reportbuilder/classes/local/filters/user.php b/reportbuilder/classes/local/filters/user.php new file mode 100644 index 00000000000..b794bbd72b2 --- /dev/null +++ b/reportbuilder/classes/local/filters/user.php @@ -0,0 +1,96 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\local\filters; + +use lang_string; +use MoodleQuickForm; +use core_reportbuilder\local\helpers\database; + +/** + * User report filter + * + * This filter expects field SQL referring to a user ID (e.g. "{$tableuser}.id") + * + * @package core_reportbuilder + * @copyright 2021 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user extends base { + + /** @var int Filter for any user */ + public const USER_ANY = 0; + + /** @var int Filter for current user */ + public const USER_CURRENT = 1; + + /** + * Return an array of operators available for this filter + * + * @return lang_string[] + */ + private function get_operators(): array { + $operators = [ + self::USER_ANY => new lang_string('userany', 'core_reportbuilder'), + self::USER_CURRENT => new lang_string('usercurrent', 'core_reportbuilder'), + ]; + + return $this->filter->restrict_limited_operators($operators); + } + + /** + * Setup form + * + * @param MoodleQuickForm $mform + */ + public function setup_form(MoodleQuickForm $mform): void { + $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()); + $mform->addElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators()) + ->setHiddenLabel(true); + + $mform->setType("{$this->name}_operator", PARAM_INT); + $mform->setDefault("{$this->name}_operator", self::USER_ANY); + } + + /** + * Return filter SQL + * + * @param array $values + * @return array + */ + public function get_sql_filter(array $values): array { + global $USER; + + $fieldsql = $this->filter->get_field_sql(); + $params = $this->filter->get_field_params(); + + $operator = $values["{$this->name}_operator"] ?? self::USER_ANY; + switch ($operator) { + case self::USER_CURRENT: + $paramuserid = database::generate_param_name(); + $sql = "{$fieldsql} = :{$paramuserid}"; + $params[$paramuserid] = $USER->id; + break; + default: + // Invalid or inactive filter. + return ['', []]; + } + + return [$sql, $params]; + } +} diff --git a/reportbuilder/tests/local/filters/user_test.php b/reportbuilder/tests/local/filters/user_test.php new file mode 100644 index 00000000000..17dc3442656 --- /dev/null +++ b/reportbuilder/tests/local/filters/user_test.php @@ -0,0 +1,82 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\local\filters; + +use advanced_testcase; +use lang_string; +use core_reportbuilder\local\report\filter; + +/** + * Unit tests for user report filter + * + * @package core_reportbuilder + * @covers \core_reportbuilder\local\filters\base + * @covers \core_reportbuilder\local\filters\user + * @copyright 2021 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_test extends advanced_testcase { + + /** + * Data provider for {@see test_get_sql_filter} + * + * @return array + */ + public function get_sql_filter_simple(): array { + return [ + [user::USER_ANY, ['admin', 'guest', 'user01', 'user02']], + [user::USER_CURRENT, ['user01']], + ]; + } + + /** + * Test getting filter SQL + * + * @param int $operator + * @param string[] $expectedusernames + * + * @dataProvider get_sql_filter_simple + */ + public function test_get_sql_filter(int $operator, array $expectedusernames): void { + global $DB; + + $this->resetAfterTest(); + + $user01 = $this->getDataGenerator()->create_user(['username' => 'user01']); + $user02 = $this->getDataGenerator()->create_user(['username' => 'user02']); + + $this->setUser($user01); + + $filter = new filter( + user::class, + 'test', + new lang_string('yes'), + 'testentity', + 'id' + ); + + // Create instance of our filter, passing given operator. + [$select, $params] = user::create($filter)->get_sql_filter([ + $filter->get_unique_identifier() . '_operator' => $operator, + ]); + + $usernames = $DB->get_fieldset_select('user', 'username', $select, $params); + $this->assertEqualsCanonicalizing($expectedusernames, $usernames); + } +}