MDL-72598 reportbuilder: schema and model updates for schedules.

This commit is contained in:
Paul Holden 2021-11-04 09:21:30 +00:00
parent 58a729f088
commit eb460d93aa
13 changed files with 340 additions and 4 deletions

View File

@ -1273,6 +1273,7 @@ $string['messageprovider:infected'] = 'Antivirus failure notifications.';
$string['messageprovider:insights'] = 'Insights generated by prediction models';
$string['messageprovider:instantmessage'] = 'Personal messages between users';
$string['messageprovider:instantmessage_help'] = 'This section configures what happens to messages that are sent to you directly from other users on this site.';
$string['messageprovider:reportbuilderschedule'] = 'Custom report builder schedules';
$string['messageselect'] = 'Select this user as a message recipient';
$string['messageselectadd'] = 'Send a message';
$string['middlename'] = 'Middle name';

View File

@ -168,6 +168,11 @@ $string['privacy:metadata:report'] = 'Report definitions';
$string['privacy:metadata:report:name'] = 'The name of the report';
$string['privacy:metadata:report:usercreated'] = 'The ID of the user who created the report';
$string['privacy:metadata:report:usermodified'] = 'The ID of the user who last modified the report';
$string['privacy:metadata:schedule'] = 'Report schedule definitions';
$string['privacy:metadata:schedule:name'] = 'The name of the schedule';
$string['privacy:metadata:schedule:usercreated'] = 'The ID of the user who created the schedule';
$string['privacy:metadata:schedule:usermodified'] = 'The ID of the user who last modified the schedule';
$string['privacy:metadata:schedule:userviewas'] = 'The ID of the user who the schedule will be viewed as';
$string['renamecolumn'] = 'Rename column \'{$a}\'';
$string['renamefilter'] = 'Rename filter \'{$a}\'';
$string['reportbuilder'] = 'Report builder';

View File

@ -373,6 +373,7 @@ $string['rating:viewany'] = 'View total ratings that anyone received';
$string['rating:viewall'] = 'View all raw ratings given by individuals';
$string['reportbuilder:edit'] = 'Edit your own custom reports';
$string['reportbuilder:editall'] = 'Edit all custom reports';
$string['reportbuilder:scheduleviewas'] = 'Schedule reports to be viewed as other users';
$string['reportbuilder:view'] = 'View custom reports';
$string['resetrole'] = 'Reset';
$string['resettingrole'] = 'Resetting role \'{$a}\'';

View File

@ -2654,4 +2654,12 @@ $capabilities = array(
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => [],
],
// Allow users to schedule reports as other users.
'moodle/reportbuilder:scheduleviewas' => [
'captype' => 'read',
'riskbitmap' => RISK_PERSONAL,
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => [],
],
);

View File

@ -4452,6 +4452,7 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="reportid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="heading" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="classname" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="configdata" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="usercreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -4466,5 +4467,35 @@
<KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="reportbuilder_schedule" COMMENT="Table to represent a report schedule">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="reportid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="audiences" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="format" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="subject" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="message" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="messageformat" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="userviewas" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timescheduled" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="recurrence" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="reportempty" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timelastsent" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timenextsend" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="usercreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="reportid" TYPE="foreign" FIELDS="reportid" REFTABLE="reportbuilder_report" REFFIELDS="id"/>
<KEY NAME="userviewas" TYPE="foreign" FIELDS="userviewas" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="usercreated" TYPE="foreign" FIELDS="usercreated" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

View File

@ -166,4 +166,11 @@ $messageproviders = array (
'infected' => array(
'capability' => 'moodle/site:config',
),
// Report builder schedules.
'reportbuilderschedule' => [
'defaults' => [
'email' => MESSAGE_FORCED,
],
],
);

View File

@ -3199,5 +3199,62 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021121400.01);
}
if ($oldversion < 2021121700.01) {
// Define field heading to be added to reportbuilder_audience.
$table = new xmldb_table('reportbuilder_audience');
$field = new xmldb_field('heading', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'reportid');
// Conditionally launch add field heading.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021121700.01);
}
if ($oldversion < 2021121700.02) {
// Define table reportbuilder_schedule to be created.
$table = new xmldb_table('reportbuilder_schedule');
// Adding fields to table reportbuilder_schedule.
$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, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
$table->add_field('audiences', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('format', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('subject', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('messageformat', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '1');
$table->add_field('userviewas', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timescheduled', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('recurrence', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('reportempty', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timelastsent', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timenextsend', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$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_schedule.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('reportid', XMLDB_KEY_FOREIGN, ['reportid'], 'reportbuilder_report', ['id']);
$table->add_key('userviewas', XMLDB_KEY_FOREIGN, ['userviewas'], 'user', ['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_schedule.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021121700.02);
}
return true;
}

View File

@ -18,6 +18,7 @@ declare(strict_types=1);
namespace core_reportbuilder\local\models;
use context;
use lang_string;
use core\persistent;
use core_reportbuilder\local\helpers\audience as helper;
@ -44,6 +45,11 @@ class audience extends persistent {
'reportid' => [
'type' => PARAM_INT,
],
'heading' => [
'type' => PARAM_TEXT,
'null' => NULL_ALLOWED,
'default' => null,
],
'classname' => [
'type' => PARAM_TEXT,
],
@ -113,4 +119,18 @@ class audience extends persistent {
public function get_report(): report {
return new report($this->get('reportid'));
}
/**
* Return formatted audience heading
*
* @param context|null $context If the context of the report is already known, it should be passed here
* @return string
*/
public function get_formatted_heading(?context $context = null): string {
if ($context === null) {
$context = $this->get_report()->get_context();
}
return format_string($this->raw_get('heading'), true, ['context' => $context]);
}
}

View File

@ -132,6 +132,11 @@ class report extends persistent {
foreach (audience::get_records($reportparams) as $audience) {
$audience->delete();
}
// Schedules.
foreach (schedule::get_records($reportparams) as $schedule) {
$schedule->delete();
}
}
/**

View File

@ -0,0 +1,189 @@
<?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 context;
use lang_string;
use core\persistent;
/**
* Persistent class to represent a report schedule
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class schedule extends persistent {
/** @var string Table name */
public const TABLE = 'reportbuilder_schedule';
/** @var int No recurrence */
public const RECURRENCE_NONE = 0;
/** @var int Daily recurrence */
public const RECURRENCE_DAILY = 1;
/** @var int Daily recurrence for week days only */
public const RECURRENCE_WEEKDAYS = 2;
/** @var int Weekly recurrence */
public const RECURRENCE_WEEKLY = 3;
/** @var int Monthly recurrence */
public const RECURRENCE_MONTHLY = 4;
/** @var int Annual recurrence */
public const RECURRENCE_ANNUALLY = 5;
/** @var int Send schedule with empty report */
public const REPORT_EMPTY_SEND_EMPTY = 0;
/** @var int Send schedule without report */
public const REPORT_EMPTY_SEND_WITHOUT = 1;
/** @var int Don't send schedule if report is empty */
public const REPORT_EMPTY_DONT_SEND = 2;
/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties() : array {
return [
'reportid' => [
'type' => PARAM_INT,
],
'name' => [
'type' => PARAM_TEXT,
],
'enabled' => [
'type' => PARAM_BOOL,
'default' => true,
],
'audiences' => [
'type' => PARAM_RAW,
'default' => '[]',
],
'format' => [
'type' => PARAM_PLUGIN,
],
'subject' => [
'type' => PARAM_TEXT,
],
'message' => [
'type' => PARAM_CLEANHTML,
],
'messageformat' => [
'type' => PARAM_INT,
'default' => FORMAT_HTML,
'choices' => [
FORMAT_MOODLE,
FORMAT_HTML,
FORMAT_PLAIN,
FORMAT_MARKDOWN,
],
],
'userviewas' => [
'type' => PARAM_INT,
'default' => static function(): int {
global $USER;
return (int) $USER->id;
},
],
'timescheduled' => [
'type' => PARAM_INT,
],
'recurrence' => [
'type' => PARAM_INT,
'default' => self::RECURRENCE_NONE,
'choices' => [
self::RECURRENCE_NONE,
self::RECURRENCE_DAILY,
self::RECURRENCE_WEEKDAYS,
self::RECURRENCE_WEEKLY,
self::RECURRENCE_MONTHLY,
self::RECURRENCE_ANNUALLY,
],
],
'reportempty' => [
'type' => PARAM_INT,
'default' => self::REPORT_EMPTY_SEND_EMPTY,
'choices' => [
self::REPORT_EMPTY_SEND_EMPTY,
self::REPORT_EMPTY_SEND_WITHOUT,
self::REPORT_EMPTY_DONT_SEND,
],
],
'timelastsent' => [
'type' => PARAM_INT,
'default' => 0,
],
'timenextsend' => [
'type' => PARAM_INT,
'default' => 0,
],
'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 schedule belongs to
*
* @return report
*/
public function get_report(): report {
return new report($this->get('reportid'));
}
/**
* Return formatted schedule name
*
* @param context|null $context If the context of the report is already known, it should be passed here
* @return string
*/
public function get_formatted_name(?context $context = null): string {
if ($context === null) {
$context = $this->get_report()->get_context();
}
return format_string($this->raw_get('name'), true, ['context' => $context]);
}
}

View File

@ -23,8 +23,9 @@ 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;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\models\schedule;
/**
* Privacy Subsystem for core_reportbuilder
@ -68,6 +69,13 @@ class provider implements
'usermodified' => 'privacy:metadata:audience:usermodified',
], 'privacy:metadata:audience');
$collection->add_database_table(schedule::TABLE, [
'name' => 'privacy:metadata:schedule:name',
'userviewas' => 'privacy:metadata:schedule:userviewas',
'usercreated' => 'privacy:metadata:schedule:usercreated',
'usermodified' => 'privacy:metadata:schedule:usermodified',
], 'privacy:metadata:schedule');
$collection->add_user_preference('core_reportbuilder', 'privacy:metadata:preference:reportfilter');
return $collection;

View File

@ -30,6 +30,7 @@ use core_reportbuilder\local\models\audience;
use core_reportbuilder\local\models\column;
use core_reportbuilder\local\models\filter;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\models\schedule;
/**
* Unit tests for privacy provider
@ -48,7 +49,7 @@ class provider_test extends provider_testcase {
$collection = new collection('core_reportbuilder');
$metadata = provider::get_metadata($collection)->get_collection();
$this->assertCount(5, $metadata);
$this->assertCount(6, $metadata);
$this->assertInstanceOf(database_table::class, $metadata[0]);
$this->assertEquals(report::TABLE, $metadata[0]->get_name());
@ -62,7 +63,10 @@ class provider_test extends provider_testcase {
$this->assertInstanceOf(database_table::class, $metadata[3]);
$this->assertEquals(audience::TABLE, $metadata[3]->get_name());
$this->assertInstanceOf(user_preference::class, $metadata[4]);
$this->assertInstanceOf(database_table::class, $metadata[4]);
$this->assertEquals(schedule::TABLE, $metadata[4]->get_name());
$this->assertInstanceOf(user_preference::class, $metadata[5]);
}
/**

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2021121700.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2021121700.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.0dev+ (Build: 20211217)'; // Human-friendly version name