first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,142 @@
<?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 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;
/**
* Persistent class to represent a report audience
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @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,
],
'heading' => [
'type' => PARAM_TEXT,
'null' => NULL_ALLOWED,
'default' => null,
],
'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;
}
/**
* Hook to execute after creation
*/
protected function after_create(): void {
audience_created::create_from_object($this)->trigger();
helper::purge_caches();
}
/**
* Hook to execute after update
*
* @param bool $result
*/
protected function after_update($result): void {
if ($result) {
audience_updated::create_from_object($this)->trigger();
helper::purge_caches();
}
}
/**
* Hook to execute after deletion
*
* @param bool $result
*/
protected function after_delete($result): void {
if ($result) {
audience_deleted::create_from_object($this)->trigger();
helper::purge_caches();
}
}
/**
* Return the report this audience belongs to
*
* @return report
*/
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]);
}
}
@@ -0,0 +1,167 @@
<?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;
use core_reportbuilder\datasource;
/**
* Persistent class to represent a report column
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class column extends persistent {
/** @var string The table name. */
public const TABLE = 'reportbuilder_column';
/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties(): array {
return [
'reportid' => [
'type' => PARAM_INT,
],
'uniqueidentifier' => [
'type' => PARAM_RAW,
],
'aggregation' => [
'type' => PARAM_ALPHANUMEXT,
'default' => null,
'null' => NULL_ALLOWED,
],
'heading' => [
'type' => PARAM_TEXT,
'default' => null,
'null' => NULL_ALLOWED,
],
'columnorder' => [
'type' => PARAM_INT,
],
'sortenabled' => [
'type' => PARAM_BOOL,
'default' => false,
],
'sortdirection' => [
'type' => PARAM_INT,
'choices' => [SORT_ASC, SORT_DESC],
'default' => SORT_ASC,
],
'sortorder' => [
'type' => PARAM_INT,
'default' => null,
'null' => NULL_ALLOWED,
],
'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 column belongs to
*
* @return report
*/
public function get_report(): report {
return new report($this->get('reportid'));
}
/**
* Ensure report source is notified of new column
*/
protected function after_create(): void {
datasource::report_elements_modified($this->get('reportid'));
}
/**
* Ensure report source is notified of updated column
*
* @param bool $result
*/
protected function after_update($result): void {
if ($result) {
datasource::report_elements_modified($this->get('reportid'));
}
}
/**
* Ensure report source is notified of deleted column
*
* @param bool $result
*/
protected function after_delete($result): void {
if ($result) {
datasource::report_elements_modified($this->get('reportid'));
}
}
/**
* Helper method to return the current maximum column order value for a report
*
* @param int $reportid
* @param string $columnname
* @return int
*/
public static function get_max_columnorder(int $reportid, string $columnname): int {
global $DB;
return (int) $DB->get_field(static::TABLE, "MAX({$columnname})", ['reportid' => $reportid], MUST_EXIST);
}
/**
* Return formatted column 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_heading(?context $context = null): string {
if ($context === null) {
$context = $this->get_report()->get_context();
}
return format_string($this->raw_get('heading'), true, ['context' => $context]);
}
}
@@ -0,0 +1,200 @@
<?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;
use core_reportbuilder\datasource;
/**
* Persistent class to represent a report filter/condition
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter extends persistent {
/** @var string The table name. */
public const TABLE = 'reportbuilder_filter';
/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties(): array {
return [
'reportid' => [
'type' => PARAM_INT,
],
'uniqueidentifier' => [
'type' => PARAM_RAW,
],
'heading' => [
'type' => PARAM_TEXT,
'null' => NULL_ALLOWED,
'default' => null,
],
'iscondition' => [
'type' => PARAM_BOOL,
'default' => false,
],
'filterorder' => [
'type' => PARAM_INT,
],
'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;
}
/**
* Ensure report source is notified of new filter
*/
protected function after_create(): void {
datasource::report_elements_modified($this->get('reportid'));
}
/**
* Ensure report source is notified of updated filter
*
* @param bool $result
*/
protected function after_update($result): void {
if ($result) {
datasource::report_elements_modified($this->get('reportid'));
}
}
/**
* Ensure report source is notified of deleted filter
*
* @param bool $result
*/
protected function after_delete($result): void {
if ($result) {
datasource::report_elements_modified($this->get('reportid'));
}
}
/**
* Return the report this filter belongs to
*
* @return report
*/
public function get_report(): report {
return new report($this->get('reportid'));
}
/**
* Return filter record
*
* @param int $reportid
* @param int $filterid
* @return false|static
*/
public static function get_filter_record(int $reportid, int $filterid) {
return self::get_record(['id' => $filterid, 'reportid' => $reportid, 'iscondition' => 0]);
}
/**
* Return filter records for report
*
* @param int $reportid
* @param string $sort
* @param string $order
* @return static[]
*/
public static function get_filter_records(int $reportid, string $sort = '', string $order = 'ASC'): array {
return self::get_records(['reportid' => $reportid, 'iscondition' => 0], $sort, $order);
}
/**
* Return condition record
*
* @param int $reportid
* @param int $conditionid
* @return false|static
*/
public static function get_condition_record(int $reportid, int $conditionid) {
return self::get_record(['id' => $conditionid, 'reportid' => $reportid, 'iscondition' => 1]);
}
/**
* Return condition records for report
*
* @param int $reportid
* @param string $sort
* @param string $order
* @return static[]
*/
public static function get_condition_records(int $reportid, string $sort = '', string $order = 'ASC'): array {
return self::get_records(['reportid' => $reportid, 'iscondition' => 1], $sort, $order);
}
/**
* Helper method to return the current maximum filter order value for a report
*
* @param int $reportid
* @param bool $iscondition
* @return int
*/
public static function get_max_filterorder(int $reportid, bool $iscondition = false): int {
global $DB;
$params = ['reportid' => $reportid, 'iscondition' => (int) $iscondition];
return (int) $DB->get_field(static::TABLE, "MAX(filterorder)", $params, MUST_EXIST);
}
/**
* Return formatted filter 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]);
}
}
@@ -0,0 +1,183 @@
<?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 context_system;
use core\persistent;
use core_reportbuilder\event\report_created;
use core_reportbuilder\event\report_deleted;
use core_reportbuilder\event\report_updated;
use core_reportbuilder\local\report\base;
/**
* Persistent class to represent a report
*
* @package core_reportbuilder
* @copyright 2018 Alberto Lara Hernández <albertolara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report extends persistent {
/** @var string The table name. */
public const TABLE = 'reportbuilder_report';
/**
* Return the definition of the properties of this model
*
* @return array
*/
protected static function define_properties(): array {
return [
'name' => [
'type' => PARAM_TEXT,
'null' => NULL_ALLOWED,
'default' => null,
],
'source' => [
'type' => PARAM_RAW,
],
'type' => [
'type' => PARAM_INT,
'choices' => [
base::TYPE_CUSTOM_REPORT,
base::TYPE_SYSTEM_REPORT,
],
],
'uniquerows' => [
'type' => PARAM_BOOL,
'default' => false,
],
'conditiondata' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'default' => null,
],
'settingsdata' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'default' => null,
],
'contextid' => [
'type' => PARAM_INT,
'default' => static function(): int {
return context_system::instance()->id;
}
],
'component' => [
'type' => PARAM_COMPONENT,
'default' => '',
],
'area' => [
'type' => PARAM_AREA,
'default' => '',
],
'itemid' => [
'type' => PARAM_INT,
'default' => 0,
],
'usercreated' => [
'type' => PARAM_INT,
'default' => static function(): int {
global $USER;
return (int) $USER->id;
},
],
];
}
/**
* Trigger report created event when persistent is created
*/
protected function after_create(): void {
if ($this->get('type') === base::TYPE_CUSTOM_REPORT) {
report_created::create_from_object($this)->trigger();
}
}
/**
* Cascade report deletion, first deleting any linked persistents
*/
protected function before_delete(): void {
$reportparams = ['reportid' => $this->get('id')];
// Columns.
foreach (column::get_records($reportparams) as $column) {
$column->delete();
}
// Filters.
foreach (filter::get_records($reportparams) as $filter) {
$filter->delete();
}
// Audiences.
foreach (audience::get_records($reportparams) as $audience) {
$audience->delete();
}
// Schedules.
foreach (schedule::get_records($reportparams) as $schedule) {
$schedule->delete();
}
}
/**
* Throw report deleted event when persistent is deleted
*
* @param bool $result
*/
protected function after_delete($result): void {
if (!$result || $this->get('type') === base::TYPE_SYSTEM_REPORT) {
return;
}
report_deleted::create_from_object($this)->trigger();
}
/**
* Throw report updated event when persistent is updated
*
* @param bool $result
*/
protected function after_update($result): void {
if (!$result || $this->get('type') === base::TYPE_SYSTEM_REPORT) {
return;
}
report_updated::create_from_object($this)->trigger();
}
/**
* Return report context, used by exporters
*
* @return context
*/
public function get_context(): context {
return context::instance_by_id($this->raw_get('contextid'));
}
/**
* Return formatted report name
*
* @return string
*/
public function get_formatted_name(): string {
return format_string($this->raw_get('name'), true, ['context' => $this->get_context(), 'escape' => true]);
}
}
@@ -0,0 +1,227 @@
<?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 core_reportbuilder\event\schedule_created;
use core_reportbuilder\event\schedule_deleted;
use core_reportbuilder\event\schedule_updated;
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 Send report schedule as viewed by recipient */
public const REPORT_VIEWAS_RECIPIENT = -1;
/** @var int Send report schedule as viewed by creator */
public const REPORT_VIEWAS_CREATOR = 0;
/** @var int Send report schedule as viewed by specific user */
public const REPORT_VIEWAS_USER = 1;
/** @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' => self::REPORT_VIEWAS_CREATOR,
],
'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]);
}
/**
* 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();
}
}
}