MDL-72757 reportbuilder: Events for CRUD operations on report models

This commit is contained in:
David Matamoros
2022-01-05 09:55:36 +01:00
parent 541d999d27
commit 39b90208e1
17 changed files with 1294 additions and 0 deletions
+4
View File
@@ -43,11 +43,14 @@ $string['allusers'] = 'All users';
$string['apply'] = 'Apply';
$string['audience'] = 'Audience';
$string['audienceadded'] = 'Added audience \'{$a}\'';
$string['audiencecreated'] = 'Audience created';
$string['audiencedeleted'] = 'Deleted audience \'{$a}\'';
$string['audiencedeletedevent'] = 'Audience deleted';
$string['audiencelabel'] = '{$a->name}: {$a->description}';
$string['audiencemultiselectpostfix'] = '{$a->elements} plus {$a->morecount} more';
$string['audiencenotsaved'] = 'Audience not saved';
$string['audiencesaved'] = 'Audience saved';
$string['audienceupdated'] = 'Audience updated';
$string['cardview'] = 'Card view';
$string['cardview_help'] = 'Card view allows you to define the layout of your report when viewed on narrow devices. Columns will collapse beyond the limit set here, with a toggle to expand the card to view all report data.';
$string['cardviewfirstcolumntitle'] = 'First column title';
@@ -197,6 +200,7 @@ $string['reportdeleted'] = 'Report deleted';
$string['reportsource'] = 'Report source';
$string['reportsource_help'] = 'The report source defines where the data for the report will come from.';
$string['reportupdated'] = 'Report updated';
$string['reportviewed'] = 'Report viewed';
$string['resetall'] = 'Reset all';
$string['resetconditions'] = 'Reset conditions';
$string['resetconditionsconfirm'] = 'Are you sure you want to reset all conditions for this report?';
@@ -0,0 +1,111 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\audience;
use moodle_url;
/**
* Report builder custom report audience created event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class audience_created extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = audience::TABLE;
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report audience object
*
* @param audience $audience
* @return self
*/
public static function create_from_object(audience $audience): self {
$eventparams = [
'context' => $audience->get_report()->get_context(),
'objectid' => $audience->get('id'),
'other' => [
'reportid' => $audience->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $audience->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('audiencecreated', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' created an audience in the custom report with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'audience');
}
}
@@ -0,0 +1,112 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\audience;
use moodle_url;
/**
* Report builder custom report audience deleted event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class audience_deleted extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = audience::TABLE;
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report audience object
*
* @param audience $audience
* @return self
*/
public static function create_from_object(audience $audience): self {
$eventparams = [
'context' => $audience->get_report()->get_context(),
'objectid' => $audience->get('id'),
'other' => [
'reportid' => $audience->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $audience->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('audiencedeletedevent', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' deleted the audience with id '$this->objectid' in the custom report" .
" with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'audience');
}
}
@@ -0,0 +1,112 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\audience;
use moodle_url;
/**
* Report builder custom report audience created event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class audience_updated extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = audience::TABLE;
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report audience object
*
* @param audience $audience
* @return self
*/
public static function create_from_object(audience $audience): self {
$eventparams = [
'context' => $audience->get_report()->get_context(),
'objectid' => $audience->get('id'),
'other' => [
'reportid' => $audience->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $audience->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('audienceupdated', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' updated the audience with id '$this->objectid' in the custom report" .
" with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'audience');
}
}
@@ -18,6 +18,7 @@ declare(strict_types=1);
namespace core_reportbuilder\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\report;
use moodle_url;
@@ -85,6 +86,18 @@ class report_created extends base {
return "The user with id '$this->userid' created the custom report with id '$this->objectid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
}
/**
* Returns relevant URL.
*
@@ -18,6 +18,7 @@ declare(strict_types=1);
namespace core_reportbuilder\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\report;
@@ -83,4 +84,16 @@ class report_deleted extends base {
public function get_description() {
return "The user with id '$this->userid' deleted the custom report with id '$this->objectid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
}
}
@@ -18,6 +18,7 @@ declare(strict_types=1);
namespace core_reportbuilder\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\report;
use moodle_url;
@@ -85,6 +86,18 @@ class report_updated extends base {
return "The user with id '$this->userid' updated the custom report with id '$this->objectid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
}
/**
* Returns relevant URL.
*
@@ -0,0 +1,109 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\report;
use moodle_url;
/**
* Report builder custom report viewed event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - string name: The name of the report
* - string source: The report source class
* }
*/
class report_viewed extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = report::TABLE;
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report object
*
* @param report $report
* @return self
*/
public static function create_from_object(report $report): self {
$eventparams = [
'context' => $report->get_context(),
'objectid' => $report->get('id'),
'other' => [
'name' => $report->get('name'),
'source' => $report->get('source'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $report->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('reportviewed', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the custom report with id '$this->objectid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/view.php', ['id' => $this->objectid]);
}
}
@@ -0,0 +1,111 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\schedule;
use moodle_url;
/**
* Report builder custom report schedule created event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class schedule_created extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = schedule::TABLE;
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report schedule object
*
* @param schedule $schedule
* @return self
*/
public static function create_from_object(schedule $schedule): self {
$eventparams = [
'context' => $schedule->get_report()->get_context(),
'objectid' => $schedule->get('id'),
'other' => [
'reportid' => $schedule->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $schedule->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('schedulecreated', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' created a schedule in the custom report with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'schedules');
}
}
@@ -0,0 +1,112 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\schedule;
use moodle_url;
/**
* Report builder custom report schedule created event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class schedule_deleted extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = schedule::TABLE;
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report schedule object
*
* @param schedule $schedule
* @return self
*/
public static function create_from_object(schedule $schedule): self {
$eventparams = [
'context' => $schedule->get_report()->get_context(),
'objectid' => $schedule->get('id'),
'other' => [
'reportid' => $schedule->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $schedule->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('scheduledeleted', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' deleted the schedule with id '$this->objectid' in the custom report" .
" with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'schedules');
}
}
@@ -0,0 +1,112 @@
<?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\event;
use coding_exception;
use core\event\base;
use core_reportbuilder\local\models\schedule;
use moodle_url;
/**
* Report builder custom report schedule updated event class.
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property-read array $other {
* Extra information about the event.
*
* - int reportid: The id of the report
* }
*/
class schedule_updated extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = schedule::TABLE;
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Creates an instance from a report schedule object
*
* @param schedule $schedule
* @return self
*/
public static function create_from_object(schedule $schedule): self {
$eventparams = [
'context' => $schedule->get_report()->get_context(),
'objectid' => $schedule->get('id'),
'other' => [
'reportid' => $schedule->get('reportid'),
]
];
$event = self::create($eventparams);
$event->add_record_snapshot($event->objecttable, $schedule->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('scheduleupdated', 'core_reportbuilder');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
$reportid = $this->other['reportid'];
return "The user with id '$this->userid' updated the schedule with id '$this->objectid' in the custom report" .
" with id '$reportid'.";
}
/**
* Custom validations.
*
* @throws coding_exception
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->objectid)) {
throw new coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['reportid'])) {
throw new coding_exception('The \'reportid\' must be set in other.');
}
}
/**
* Returns relevant URL.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'schedules');
}
}
@@ -19,6 +19,9 @@ declare(strict_types=1);
namespace core_reportbuilder\local\models;
use context;
use core_reportbuilder\event\audience_created;
use core_reportbuilder\event\audience_deleted;
use core_reportbuilder\event\audience_updated;
use lang_string;
use core\persistent;
use core_reportbuilder\local\helpers\audience as helper;
@@ -86,6 +89,7 @@ class audience extends persistent {
* Hook to execute after creation
*/
protected function after_create(): void {
audience_created::create_from_object($this)->trigger();
helper::purge_caches();
}
@@ -96,6 +100,7 @@ class audience extends persistent {
*/
protected function after_update($result): void {
if ($result) {
audience_updated::create_from_object($this)->trigger();
helper::purge_caches();
}
}
@@ -107,6 +112,7 @@ class audience extends persistent {
*/
protected function after_delete($result): void {
if ($result) {
audience_deleted::create_from_object($this)->trigger();
helper::purge_caches();
}
}
@@ -19,6 +19,9 @@ declare(strict_types=1);
namespace core_reportbuilder\local\models;
use context;
use core_reportbuilder\event\schedule_created;
use core_reportbuilder\event\schedule_deleted;
use core_reportbuilder\event\schedule_updated;
use lang_string;
use core\persistent;
@@ -192,4 +195,33 @@ class schedule extends persistent {
return format_string($this->raw_get('name'), true, ['context' => $context]);
}
/**
* Hook to execute after creation
*/
protected function after_create(): void {
schedule_created::create_from_object($this)->trigger();
}
/**
* Hook to execute after update
*
* @param bool $result
*/
protected function after_update($result): void {
if ($result) {
schedule_updated::create_from_object($this)->trigger();
}
}
/**
* Hook to execute after deletion
*
* @param bool $result
*/
protected function after_delete($result): void {
if ($result) {
schedule_deleted::create_from_object($this)->trigger();
}
}
}
@@ -0,0 +1,150 @@
<?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\models;
use advanced_testcase;
use core_reportbuilder\event\audience_created;
use core_reportbuilder\event\audience_deleted;
use core_reportbuilder\event\audience_updated;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for the audience model
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class audience_test extends advanced_testcase {
/**
* Tests for audience_created event
*
* @covers \core_reportbuilder\event\audience_created
*/
public function test_audience_created_event(): 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,
'default' => false,
]);
// Catch the events.
$sink = $this->redirectEvents();
$audience = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(audience_created::class, $event);
$this->assertEquals(audience::TABLE, $event->objecttable);
$this->assertEquals($audience->get_persistent()->get('id'), $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
/**
* Tests for audience_deleted event
*
* @covers \core_reportbuilder\event\audience_deleted
*/
public function test_audience_deleted_event(): 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,
'default' => false,
]);
$audience = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
$audienceid = $audience->get_persistent()->get('id');
// Catch the events.
$sink = $this->redirectEvents();
$audience->get_persistent()->delete();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(audience_deleted::class, $event);
$this->assertEquals(audience::TABLE, $event->objecttable);
$this->assertEquals($audienceid, $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
/**
* Tests for audience_updated event
*
* @covers \core_reportbuilder\event\audience_updated
*/
public function test_audience_updated_event(): 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,
'default' => false,
]);
$audience = $generator->create_audience([
'reportid' => $report->get('id'),
'configdata' => [],
]);
// Catch the events.
$sink = $this->redirectEvents();
$audience->get_persistent()->set('heading', 'Hello');
$audience->get_persistent()->update();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(audience_updated::class, $event);
$this->assertEquals(audience::TABLE, $event->objecttable);
$this->assertEquals($audience->get_persistent()->get('id'), $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
}
@@ -0,0 +1,139 @@
<?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\models;
use advanced_testcase;
use core_reportbuilder\event\report_created;
use core_reportbuilder\event\report_deleted;
use core_reportbuilder\event\report_updated;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for the report model
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_test extends advanced_testcase {
/**
* Tests for report_created event
*
* @covers \core_reportbuilder\event\report_created
*/
public function test_report_created_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Catch the events.
$sink = $this->redirectEvents();
$report = $generator->create_report([
'name' => 'My report',
'source' => users::class,
'default' => false,
]);
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(report_created::class, $event);
$this->assertEquals(report::TABLE, $event->objecttable);
$this->assertEquals($report->get('id'), $event->objectid);
$this->assertEquals('My report', $event->other['name']);
$this->assertEquals(users::class, $event->other['source']);
}
/**
* Tests for report_deleted event
*
* @covers \core_reportbuilder\event\report_deleted
*/
public function test_report_deleted_event(): 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,
'default' => false,
]);
$reportid = $report->get('id');
// Catch the events.
$sink = $this->redirectEvents();
$report->delete();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(report_deleted::class, $event);
$this->assertEquals(report::TABLE, $event->objecttable);
$this->assertEquals($reportid, $event->objectid);
$this->assertEquals('My report', $event->other['name']);
$this->assertEquals(users::class, $event->other['source']);
}
/**
* Tests for report_updated event
*
* @covers \core_reportbuilder\event\report_updated
*/
public function test_report_updated_event(): 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,
'default' => false,
]);
// Catch the events.
$sink = $this->redirectEvents();
$report->set('name', 'New report name');
$report->update();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(report_updated::class, $event);
$this->assertEquals(report::TABLE, $event->objecttable);
$this->assertEquals($report->get('id'), $event->objectid);
$this->assertEquals('New report name', $event->other['name']);
$this->assertEquals(users::class, $event->other['source']);
}
}
@@ -0,0 +1,141 @@
<?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\models;
use advanced_testcase;
use core_reportbuilder\event\schedule_created;
use core_reportbuilder\event\schedule_deleted;
use core_reportbuilder\event\schedule_updated;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
/**
* Unit tests for the schedule model
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class schedule_test extends advanced_testcase {
/**
* Tests for schedule_created event
*
* @covers \core_reportbuilder\event\schedule_created
*/
public function test_schedule_created_event(): 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,
'default' => false,
]);
// Catch the events.
$sink = $this->redirectEvents();
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(schedule_created::class, $event);
$this->assertEquals(schedule::TABLE, $event->objecttable);
$this->assertEquals($schedule->get('id'), $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
/**
* Tests for schedule_deleted event
*
* @covers \core_reportbuilder\event\schedule_deleted
*/
public function test_schedule_deleted_event(): 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,
'default' => false,
]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
$scheduleid = $schedule->get('id');
// Catch the events.
$sink = $this->redirectEvents();
$schedule->delete();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(schedule_deleted::class, $event);
$this->assertEquals(schedule::TABLE, $event->objecttable);
$this->assertEquals($scheduleid, $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
/**
* Tests for schedule_updated event
*
* @covers \core_reportbuilder\event\schedule_updated
*/
public function test_schedule_updated_event(): 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,
'default' => false,
]);
$schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
// Catch the events.
$sink = $this->redirectEvents();
$schedule->set('name', 'My new schedule');
$schedule->update();
$events = $sink->get_events();
$sink->close();
// Validate the event.
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf(schedule_updated::class, $event);
$this->assertEquals(schedule::TABLE, $event->objecttable);
$this->assertEquals($schedule->get('id'), $event->objectid);
$this->assertEquals($report->get('id'), $event->other['reportid']);
}
}
+4
View File
@@ -24,6 +24,7 @@
declare(strict_types=1);
use core_reportbuilder\event\report_viewed;
use core_reportbuilder\manager;
use core_reportbuilder\output\custom_report;
use core_reportbuilder\permission;
@@ -55,3 +56,6 @@ $export = (new custom_report($report->get_report_persistent(), false))->export_f
echo $renderer->render_from_template('core_reportbuilder/report', $export);
echo $OUTPUT->footer();
// Trigger report viewed event.
report_viewed::create_from_object($report->get_report_persistent())->trigger();