From b2dd94ab8b8f5d7be2f63162c70fb55010842fcf Mon Sep 17 00:00:00 2001 From: David Matamoros Date: Mon, 27 Sep 2021 12:27:09 +0200 Subject: [PATCH] MDL-72588 reportbuilder: define schema/models for report audiences. --- lang/en/reportbuilder.php | 4 + lib/db/install.xml | 20 ++++- lib/db/upgrade.php | 30 +++++++ .../classes/local/models/audience.php | 86 +++++++++++++++++++ reportbuilder/classes/local/models/report.php | 5 ++ reportbuilder/classes/privacy/provider.php | 7 ++ reportbuilder/tests/privacy/provider_test.php | 8 +- version.php | 2 +- 8 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 reportbuilder/classes/local/models/audience.php diff --git a/lang/en/reportbuilder.php b/lang/en/reportbuilder.php index b70818bb92a..b96d0ae9b28 100644 --- a/lang/en/reportbuilder.php +++ b/lang/en/reportbuilder.php @@ -124,6 +124,10 @@ $string['newreport'] = 'New report'; $string['noconditions'] = 'There are no conditions selected'; $string['nofilters'] = 'There are no filters selected'; $string['nosortablecolumns'] = 'There are no sortable columns'; +$string['privacy:metadata:audience'] = 'Report audience definitions'; +$string['privacy:metadata:audience:classname'] = 'Class used by the audience'; +$string['privacy:metadata:audience:usercreated'] = 'The ID of the user who created the audience'; +$string['privacy:metadata:audience:usermodified'] = 'The ID of the user who last modified the audience'; $string['privacy:metadata:column'] = 'Report column definitions'; $string['privacy:metadata:column:uniqueidentifier'] = 'Unique identifier of the column'; $string['privacy:metadata:column:usercreated'] = 'The ID of the user who created the column'; diff --git a/lib/db/install.xml b/lib/db/install.xml index 67d944175f8..d59520dde26 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -4445,5 +4445,23 @@ + + + + + + + + + + + + + + + + + +
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 9039e9d2163..88b7b4062e9 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3010,5 +3010,35 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021101900.01); } + if ($oldversion < 2021102600.01) { + + // Define table reportbuilder_audience to be created. + $table = new xmldb_table('reportbuilder_audience'); + + // Adding fields to table reportbuilder_audience. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('reportid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('configdata', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + + // Adding keys to table reportbuilder_audience. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('reportid', XMLDB_KEY_FOREIGN, ['reportid'], 'reportbuilder_report', ['id']); + $table->add_key('usercreated', XMLDB_KEY_FOREIGN, ['usercreated'], 'user', ['id']); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']); + + // Conditionally launch create table for reportbuilder_audience. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021102600.01); + } + return true; } diff --git a/reportbuilder/classes/local/models/audience.php b/reportbuilder/classes/local/models/audience.php new file mode 100644 index 00000000000..dbdc87a9c26 --- /dev/null +++ b/reportbuilder/classes/local/models/audience.php @@ -0,0 +1,86 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\local\models; + +use lang_string; +use core\persistent; + +/** + * Persistent class to represent a report audience + * + * @package core_reportbuilder + * @copyright 2021 David Matamoros + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class audience extends persistent { + + /** @var string Table name */ + public const TABLE = 'reportbuilder_audience'; + + /** + * Return the definition of the properties of this model. + * + * @return array + */ + protected static function define_properties() : array { + return [ + 'reportid' => [ + 'type' => PARAM_INT, + ], + 'classname' => [ + 'type' => PARAM_TEXT, + ], + 'configdata' => [ + 'type' => PARAM_RAW, + 'default' => '{}', + ], + 'usercreated' => [ + 'type' => PARAM_INT, + 'default' => static function(): int { + global $USER; + + return (int) $USER->id; + }, + ], + ]; + } + + /** + * Validate reportid property + * + * @param int $reportid + * @return bool|lang_string + */ + protected function validate_reportid(int $reportid) { + if (!report::record_exists($reportid)) { + return new lang_string('invaliddata', 'error'); + } + + return true; + } + + /** + * Return the report this audience belongs to + * + * @return report + */ + public function get_report(): report { + return new report($this->get('reportid')); + } +} diff --git a/reportbuilder/classes/local/models/report.php b/reportbuilder/classes/local/models/report.php index 1cc209ffb45..2499952b40f 100644 --- a/reportbuilder/classes/local/models/report.php +++ b/reportbuilder/classes/local/models/report.php @@ -118,6 +118,11 @@ class report extends persistent { foreach (filter::get_records($reportparams) as $filter) { $filter->delete(); } + + // Audiences. + foreach (audience::get_records($reportparams) as $audience) { + $audience->delete(); + } } /** diff --git a/reportbuilder/classes/privacy/provider.php b/reportbuilder/classes/privacy/provider.php index 2e49bcd0c23..cbd84226d00 100644 --- a/reportbuilder/classes/privacy/provider.php +++ b/reportbuilder/classes/privacy/provider.php @@ -21,6 +21,7 @@ namespace core_reportbuilder\privacy; use core_privacy\local\metadata\collection; use core_privacy\local\request\writer; use core_reportbuilder\local\helpers\user_filter_manager; +use core_reportbuilder\local\models\audience; use core_reportbuilder\local\models\column; use core_reportbuilder\local\models\report; use core_reportbuilder\local\models\filter; @@ -61,6 +62,12 @@ class provider implements 'usermodified' => 'privacy:metadata:filter:usermodified', ], 'privacy:metadata:filter'); + $collection->add_database_table(audience::TABLE, [ + 'classname' => 'privacy:metadata:audience:classname', + 'usercreated' => 'privacy:metadata:audience:usercreated', + 'usermodified' => 'privacy:metadata:audience:usermodified', + ], 'privacy:metadata:audience'); + $collection->add_user_preference('core_reportbuilder', 'privacy:metadata:preference:reportfilter'); return $collection; diff --git a/reportbuilder/tests/privacy/provider_test.php b/reportbuilder/tests/privacy/provider_test.php index bf18dd73ebe..dd881d2dfca 100644 --- a/reportbuilder/tests/privacy/provider_test.php +++ b/reportbuilder/tests/privacy/provider_test.php @@ -26,6 +26,7 @@ use core_privacy\local\request\writer; use core_privacy\tests\provider_testcase; use core_reportbuilder\manager; use core_reportbuilder\local\helpers\user_filter_manager; +use core_reportbuilder\local\models\audience; use core_reportbuilder\local\models\column; use core_reportbuilder\local\models\filter; use core_reportbuilder\local\models\report; @@ -47,7 +48,7 @@ class provider_test extends provider_testcase { $collection = new collection('core_reportbuilder'); $metadata = provider::get_metadata($collection)->get_collection(); - $this->assertCount(4, $metadata); + $this->assertCount(5, $metadata); $this->assertInstanceOf(database_table::class, $metadata[0]); $this->assertEquals(report::TABLE, $metadata[0]->get_name()); @@ -58,7 +59,10 @@ class provider_test extends provider_testcase { $this->assertInstanceOf(database_table::class, $metadata[2]); $this->assertEquals(filter::TABLE, $metadata[2]->get_name()); - $this->assertInstanceOf(user_preference::class, $metadata[3]); + $this->assertInstanceOf(database_table::class, $metadata[3]); + $this->assertEquals(audience::TABLE, $metadata[3]->get_name()); + + $this->assertInstanceOf(user_preference::class, $metadata[4]); } /** diff --git a/version.php b/version.php index 34c347017fc..ef1f2da6da2 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021102600.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021102600.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev+ (Build: 20211026)'; // Human-friendly version name