Commit eab5f3cf authored by Andrew Nicols's avatar Andrew Nicols Committed by jun
Browse files

Merge branch 'MDL-73069' of https://github.com/paulholden/moodle

parents be78cb02 881dccaa
......@@ -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);
}
}
......@@ -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';
......@@ -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;
}
......
<?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 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 <paulh@moodle.com>
* @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];
}
}
<?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 user report filter
*
* @package core_reportbuilder
* @covers \core_reportbuilder\local\filters\base
* @covers \core_reportbuilder\local\filters\user
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @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);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment