mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-74957 reportbuilder: external method for triggering report view.
This commit is contained in:
parent
41b805c1ba
commit
637e3a7c88
@ -2812,6 +2812,12 @@ $functions = array(
|
||||
'type' => 'read',
|
||||
'ajax' => true,
|
||||
],
|
||||
'core_reportbuilder_view_report' => [
|
||||
'classname' => 'core_reportbuilder\external\reports\view',
|
||||
'description' => 'Trigger custom report viewed',
|
||||
'type' => 'write',
|
||||
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
|
||||
],
|
||||
'core_reportbuilder_columns_add' => [
|
||||
'classname' => 'core_reportbuilder\external\columns\add',
|
||||
'description' => 'Add column to report',
|
||||
|
93
reportbuilder/classes/external/reports/view.php
vendored
Normal file
93
reportbuilder/classes/external/reports/view.php
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
<?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\external\reports;
|
||||
|
||||
use external_api;
|
||||
use external_function_parameters;
|
||||
use external_single_structure;
|
||||
use external_value;
|
||||
use external_warnings;
|
||||
use core_reportbuilder\manager;
|
||||
use core_reportbuilder\permission;
|
||||
use core_reportbuilder\event\report_viewed;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once("{$CFG->libdir}/externallib.php");
|
||||
|
||||
/**
|
||||
* External method to record the viewing of a report
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @copyright 2022 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class view extends external_api {
|
||||
|
||||
/**
|
||||
* External method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function execute_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'reportid' => new external_value(PARAM_INT, 'Report ID'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* External method execution
|
||||
*
|
||||
* @param int $reportid
|
||||
* @return array
|
||||
*/
|
||||
public static function execute(int $reportid): array {
|
||||
[
|
||||
'reportid' => $reportid,
|
||||
] = self::validate_parameters(self::execute_parameters(), [
|
||||
'reportid' => $reportid,
|
||||
]);
|
||||
|
||||
$report = manager::get_report_from_id($reportid);
|
||||
self::validate_context($report->get_context());
|
||||
|
||||
$persistent = $report->get_report_persistent();
|
||||
permission::require_can_view_report($persistent);
|
||||
|
||||
// Trigger the report viewed event.
|
||||
report_viewed::create_from_object($persistent)->trigger();
|
||||
|
||||
return [
|
||||
'status' => true,
|
||||
'warnings' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* External method return value
|
||||
*
|
||||
* @return external_single_structure
|
||||
*/
|
||||
public static function execute_returns(): external_single_structure {
|
||||
return new external_single_structure([
|
||||
'status' => new external_value(PARAM_BOOL, 'Success'),
|
||||
'warnings' => new external_warnings(),
|
||||
]);
|
||||
}
|
||||
}
|
98
reportbuilder/tests/external/reports/view_test.php
vendored
Normal file
98
reportbuilder/tests/external/reports/view_test.php
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
<?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\external\reports;
|
||||
|
||||
use core_reportbuilder_generator;
|
||||
use external_api;
|
||||
use externallib_advanced_testcase;
|
||||
use core_reportbuilder\event\report_viewed;
|
||||
use core_reportbuilder\report_access_exception;
|
||||
use core_reportbuilder\local\models\report;
|
||||
use core_user\reportbuilder\datasource\users;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
|
||||
|
||||
/**
|
||||
* Unit tests of external class for viewing reports
|
||||
*
|
||||
* @package core_reportbuilder
|
||||
* @covers \core_reportbuilder\external\reports\view
|
||||
* @copyright 2022 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class view_test extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Text execute method
|
||||
*/
|
||||
public function test_execute(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
||||
|
||||
// Catch the events.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
$result = view::execute($report->get('id'));
|
||||
$result = external_api::clean_returnvalue(view::execute_returns(), $result);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$sink->close();
|
||||
|
||||
$this->assertValidKeys($result, ['status', 'warnings']);
|
||||
$this->assertTrue($result['status']);
|
||||
$this->assertEmpty($result['warnings']);
|
||||
|
||||
// Validate the event.
|
||||
$this->assertCount(1, $events);
|
||||
|
||||
$event = reset($events);
|
||||
$this->assertInstanceOf(report_viewed::class, $event);
|
||||
$this->assertEquals(report::TABLE, $event->objecttable);
|
||||
$this->assertEquals($report->get('id'), $event->objectid);
|
||||
$this->assertEquals($report->get('name'), $event->other['name']);
|
||||
$this->assertEquals($report->get('source'), $event->other['source']);
|
||||
$this->assertEquals($report->get_context()->id, $event->contextid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test execute method for a user without permission to view reports
|
||||
*/
|
||||
public function test_execute_access_exception(): 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]);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->setUser($user);
|
||||
|
||||
$this->expectException(report_access_exception::class);
|
||||
$this->expectExceptionMessage('You cannot view this report');
|
||||
view::execute($report->get('id'));
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@ Information provided here is intended especially for developers.
|
||||
export for calling from Javascript modules)
|
||||
* New method `set_filter_form_default` in base system report class, to override whether the default filters form
|
||||
should be shown for a report
|
||||
* New external methods for retrieving custom report data:
|
||||
- `core_reportbuilder_view_report`
|
||||
* 'set_default_per_page' and 'get_default_per_page' methods have been added to \local\report\base class
|
||||
to manage the default displayed rows per page.
|
||||
* Added two new methods in the datasource class:
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2022072900.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2022072900.02; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
$release = '4.1dev (Build: 20220729)'; // Human-friendly version name
|
||||
|
Loading…
x
Reference in New Issue
Block a user