This commit is contained in:
Sara Arjona 2022-08-31 10:34:08 +02:00
commit 5bf3b5078e
3 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,95 @@
<?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\reportbuilder\audience;
use MoodleQuickForm;
use core_reportbuilder\local\audiences\base;
use core_reportbuilder\local\helpers\database;
/**
* Administrators audience type
*
* @package core_reportbuilder
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admins extends base {
/**
* Add audience elements to the current form
*
* @param MoodleQuickForm $mform
*/
public function get_config_form(MoodleQuickForm $mform): void {
$mform->addElement('static', 'admins', get_string('siteadministrators', 'core_role'));
}
/**
* Return SQL to retrieve users that match this audience
*
* @param string $usertablealias
* @return array [$join, $select, $params]
*/
public function get_sql(string $usertablealias): array {
global $CFG, $DB;
$siteadmins = array_map('intval', explode(',', $CFG->siteadmins));
[$select, $params] = $DB->get_in_or_equal($siteadmins, SQL_PARAMS_NAMED, database::generate_param_name() . '_');
return ['', "{$usertablealias}.id {$select}", $params];
}
/**
* Return name of this audience
*
* @return string
*/
public function get_name(): string {
return get_string('siteadministrators', 'core_role');
}
/**
* Return description of this audience.
*
* @return string
*/
public function get_description(): string {
$siteadmins = array_map('fullname', get_admins());
return $this->format_description_for_multiselect($siteadmins);
}
/**
* Whether the current user is able to add this audience
*
* @return bool
*/
public function user_can_add(): bool {
return is_siteadmin();
}
/**
* Whether the current user is able to edit this audience
*
* @return bool
*/
public function user_can_edit(): bool {
return $this->user_can_add();
}
}

View File

@ -46,6 +46,18 @@ Feature: Configure access to reports based on intended audience
And I click on "My report" "link" in the "My report" "table_row"
And I should see "User 1" in the "reportbuilder-table" "table"
Scenario: Configure report audience with administrator audience type
Given I am on the "My report" "reportbuilder > Editor" page logged in as "admin"
And I click on the "Audience" dynamic tab
When I click on "Add audience 'Site administrators'" "link"
And I press "Save changes"
Then I should see "Audience saved"
And I click on the "Access" dynamic tab
And I should see "Admin User" in the "reportbuilder-table" "table"
And I should not see "User 1" in the "reportbuilder-table" "table"
And I should not see "User 2" in the "reportbuilder-table" "table"
And I should not see "User 3" in the "reportbuilder-table" "table"
Scenario: Configure report audience with has system role audience type
Given the following "roles" exist:
| shortname | name | archetype |

View File

@ -0,0 +1,119 @@
<?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\reportbuilder\audience;
use advanced_testcase;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for the administrators audience type
*
* @package core_reportbuilder
* @covers \core_reportbuilder\reportbuilder\audience\admins
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admins_test extends advanced_testcase {
/**
* Test whether user can add this audience
*/
public function test_user_can_add(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$audience = admins::create($report->get('id'), []);
$this->assertFalse($audience->user_can_add());
// Switch to privileged user.
$this->setAdminUser();
$this->assertTrue($audience->user_can_add());
}
/**
* Test whether user can edit this audience
*/
public function test_user_can_edit(): void {
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$audience = admins::create($report->get('id'), []);
$this->assertFalse($audience->user_can_edit());
// Switch to privileged user.
$this->setAdminUser();
$this->assertTrue($audience->user_can_edit());
}
/**
* Test retrieving audience SQL for matching users
*/
public function test_get_sql(): void {
global $CFG, $DB;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
// Create three users, set only the initial two as admins.
$userone = $this->getDataGenerator()->create_user();
$usertwo = $this->getDataGenerator()->create_user();
$userthree = $this->getDataGenerator()->create_user();
$CFG->siteadmins = "{$userone->id},{$usertwo->id}";
$audience = admins::create($report->get('id'), []);
[$join, $select, $params] = $audience->get_sql('u');
$users = $DB->get_fieldset_sql("SELECT u.id FROM {user} u {$join} WHERE {$select}", $params);
$this->assertEqualsCanonicalizing([$userone->id, $usertwo->id], $users);
}
/**
* Test showing audience description
*/
public function test_get_description(): void {
global $CFG;
$this->resetAfterTest();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
// Create three users, set only the initial two as admins.
$userone = $this->getDataGenerator()->create_user();
$usertwo = $this->getDataGenerator()->create_user();
$userthree = $this->getDataGenerator()->create_user();
$CFG->siteadmins = "{$userone->id},{$usertwo->id}";
$audience = admins::create($report->get('id'), []);
$this->assertEquals(fullname($userone) . ', ' . fullname($usertwo), $audience->get_description());
}
}