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
+176
View File
@@ -0,0 +1,176 @@
<?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\form;
use context;
use core_form\dynamic_form;
use core_reportbuilder\local\audiences\base;
use core_reportbuilder\output\audience_heading_editable;
use core_reportbuilder\permission;
use moodle_url;
use stdClass;
/**
* Dynamic audience form
*
* @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 dynamic_form {
/**
* Audience we work with
*
* @return base
*/
protected function get_audience(): base {
$id = $this->optional_param('id', 0, PARAM_INT);
$record = new stdClass();
if (!$id) {
// New instance, pre-define report id and classname.
$record->reportid = $this->optional_param('reportid', null, PARAM_INT);
$record->classname = $this->optional_param('classname', null, PARAM_RAW_TRIMMED);
}
return base::instance($id, $record);
}
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'reportid');
$mform->setType('reportid', PARAM_INT);
$mform->addElement('hidden', 'classname');
$mform->setType('classname', PARAM_RAW_TRIMMED);
// Embed form defined in audience class.
$audience = $this->get_audience();
$audience->get_config_form($mform);
$this->add_action_buttons();
}
/**
* Form validation.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
$audience = $this->get_audience();
return $audience->validate_config_form($data);
}
/**
* Returns context where this form is used
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return $this->get_audience()->get_persistent()->get_report()->get_context();
}
/**
* Ensure current user is able to use this form
*
* A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
*/
protected function check_access_for_dynamic_submission(): void {
$audience = $this->get_audience();
$report = $audience->get_persistent()->get_report();
permission::require_can_edit_report($report);
// Check whether we are able to add/edit the current audience.
$audience->get_persistent()->get('id') === 0
? $audience->require_user_can_add()
: $audience->require_user_can_edit();
}
/**
* Process the form submission, used if form was submitted via AJAX
*/
public function process_dynamic_submission() {
global $PAGE;
$formdata = $this->get_data();
$audience = $this->get_audience();
$configdata = $audience::retrieve_configdata($formdata);
if (!$formdata->id) {
// New audience.
$audience = $audience::create($formdata->reportid, $configdata);
} else {
// Editing audience.
$audience->update_configdata($configdata);
}
$persistent = $audience->get_persistent();
$editable = new audience_heading_editable(0, $persistent);
return [
'instanceid' => $persistent->get('id'),
'heading' => $editable->render($PAGE->get_renderer('core')),
'description' => $audience->get_description(),
];
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
$audience = $this->get_audience();
$persistent = $audience->get_persistent();
// Populate form data based on whether we are editing/creating an audience.
if ($persistent->get('id') !== 0) {
$formdata = [
'id' => $persistent->get('id'),
'reportid' => $persistent->get('reportid'),
'classname' => $persistent->get('classname'),
] + $audience->get_configdata();
} else {
$formdata = [
'reportid' => $this->optional_param('reportid', null, PARAM_INT),
'classname' => $this->optional_param('classname', null, PARAM_RAW_TRIMMED),
];
}
$this->set_data($formdata);
}
/**
* Page url
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->optional_param('reportid', 0, PARAM_INT)]);
}
}
+130
View File
@@ -0,0 +1,130 @@
<?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\form;
use context;
use moodle_url;
use core_form\dynamic_form;
use core_reportbuilder\manager;
use core_reportbuilder\permission;
use core_reportbuilder\local\report\base;
use core_reportbuilder\local\models\report;
/**
* Card view dynamic form
*
* @package core_reportbuilder
* @copyright 2021 Mikel Martín <mikel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class card_view extends dynamic_form {
/**
* Return instance of the report using the card view form
*
* @return base
*/
private function get_report(): base {
$report = new report($this->optional_param('reportid', 0, PARAM_INT));
$parameters = (array) json_decode($this->optional_param('parameters', '', PARAM_RAW));
return manager::get_report_from_persistent($report, $parameters);
}
/**
* Returns context where this form is used
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return $this->get_report()->get_context();
}
/**
* Check if current user has access to this form, otherwise throw exception
*/
public function check_access_for_dynamic_submission(): void {
permission::require_can_edit_report($this->get_report()->get_report_persistent());
}
/**
* Store the conditions values and operators
*
* @return bool
*/
public function process_dynamic_submission(): bool {
$values = $this->get_data();
$settings = [
'cardview_showfirsttitle' => (int)$values->showfirsttitle,
// Minimum value for 'cardview_visiblecolumns' should be 1.
'cardview_visiblecolumns' => max((int)$values->visiblecolumns, 1)
];
return $this->get_report()->set_settings_values($settings);
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
$report = $this->get_report();
$settings = $report->get_settings_values();
$defaults = [
// Maximum value for 'cardview_visiblecolumns' should be the report total number of columns.
'visiblecolumns' => min($settings['cardview_visiblecolumns'] ?? 1, count($report->get_active_columns())),
'showfirsttitle' => $settings['cardview_showfirsttitle'] ?? 0,
];
$this->set_data(array_merge($defaults, $this->_ajaxformdata));
}
/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/reportbuilder/edit.php');
}
/**
* Card view form definition
*/
public function definition(): void {
$report = $this->get_report();
$mform = $this->_form;
$mform->addElement('hidden', 'reportid');
$mform->setType('reportid', PARAM_INT);
// Generate select options from 1 to report total number of columns.
$visiblecolumns = range(1, max(count($report->get_active_columns()), 1));
$mform->addElement('select', 'visiblecolumns', get_string('cardviewvisiblecolumns', 'core_reportbuilder'),
array_combine($visiblecolumns, $visiblecolumns));
$mform->setType('visiblecolumns', PARAM_INT);
$mform->addElement('selectyesno', 'showfirsttitle', get_string('cardviewfirstcolumntitle', 'core_reportbuilder'));
$mform->setType('showfirsttitle', PARAM_BOOL);
$mform->disable_form_change_checker();
$this->add_action_buttons(false);
}
}
+157
View File
@@ -0,0 +1,157 @@
<?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\form;
use context;
use moodle_url;
use core_form\dynamic_form;
use core_reportbuilder\manager;
use core_reportbuilder\permission;
use core_reportbuilder\local\report\base;
use core_reportbuilder\local\models\report;
/**
* Dynamic condition form
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition extends dynamic_form {
/**
* Return instance of the report using the condition form
*
* @return base
*/
private function get_report(): base {
$report = new report($this->optional_param('reportid', 0, PARAM_INT));
$parameters = (array) json_decode($this->optional_param('parameters', '', PARAM_RAW));
return manager::get_report_from_persistent($report, $parameters);
}
/**
* Return the context for the form, it should be that of the system report itself
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return $this->get_report()->get_context();
}
/**
* Ensure current user is able to use this form
*
* A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
*/
protected function check_access_for_dynamic_submission(): void {
permission::require_can_edit_report($this->get_report()->get_report_persistent());
}
/**
* Process the form submission
*
* @return bool
*/
public function process_dynamic_submission() {
$values = $this->get_data();
// Remove some unneeded fields.
unset($values->reportid, $values->parameters);
return $this->get_report()->set_condition_values((array) $values);
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
$defaults = [
'reportid' => $this->optional_param('reportid', 0, PARAM_INT),
'parameters' => $this->optional_param('parameters', 0, PARAM_RAW),
];
$this->set_data(array_merge($defaults, $this->get_report()->get_condition_values()));
}
/**
* URL of the page using this form
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/');
}
/**
* Condition form definition
*/
protected function definition() {
global $OUTPUT;
$mform = $this->_form;
$mform->addElement('hidden', 'reportid');
$mform->setType('reportid', PARAM_INT);
$mform->addElement('hidden', 'parameters');
$mform->setType('parameters', PARAM_RAW);
// Wrap the form elements inside an outer container, as drag/drop requires draggable elements to be immediate
// descendants of said container. Note this is identified by it's data-region property in the editor module.
$mform->addElement('html', '<div class="list-group mt-2" data-region="active-conditions">');
// Allow each condition instance to add itself to this form, wrapping each inside custom header/footer template.
$conditioninstances = $this->get_report()->get_condition_instances();
foreach ($conditioninstances as $conditioninstance) {
$persistent = $conditioninstance->get_filter_persistent();
$entityname = $conditioninstance->get_entity_name();
$displayvalue = $conditioninstance->get_header();
$mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/conditions/header', [
'id' => $persistent->get('id'),
'entityname' => $this->get_report()->get_entity_title($entityname),
'heading' => $displayvalue,
'sortorder' => $persistent->get('filterorder'),
'movetitle' => get_string('movecondition', 'core_reportbuilder', $displayvalue),
]));
$conditioninstance->setup_form($mform);
$mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/conditions/footer', []));
}
$mform->addElement('html', '</div>');
$this->set_display_vertical();
// We'll add a second submit button to the form that will be used to reset current report conditions.
$mform->registerNoSubmitButton('resetconditions');
$buttons = [];
$buttons[] = $mform->createElement('submit', 'submitbutton', get_string('apply', 'core_reportbuilder'));
$buttons[] = $mform->createElement('submit', 'resetconditions', get_string('resetall', 'core_reportbuilder'),
null, null, ['customclassoverride' => 'btn-link ml-1']);
$mform->addGroup($buttons, 'buttonar', get_string('formactions', 'core_form'), '', false)
->setHiddenLabel(true);
$mform->closeHeaderBefore('buttonar');
$mform->disable_form_change_checker();
}
}
+160
View File
@@ -0,0 +1,160 @@
<?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\form;
use context;
use core_reportbuilder\local\report\base;
use core_reportbuilder\permission;
use moodle_url;
use core_form\dynamic_form;
use core_reportbuilder\manager;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\models\filter as filter_model;
/**
* Dynamic filter form
*
* @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 dynamic_form {
/**
* Return instance of the system report using the filter form
*
* @return base
*/
private function get_report(): base {
$reportpersistent = new report($this->optional_param('reportid', 0, PARAM_INT));
$parameters = (array) json_decode($this->optional_param('parameters', '', PARAM_RAW));
return manager::get_report_from_persistent($reportpersistent, $parameters);
}
/**
* Return the context for the form, it should be that of the system report itself
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return ($this->get_report())->get_context();
}
/**
* Ensure current user is able to use this form
*
* A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
*/
protected function check_access_for_dynamic_submission(): void {
$reportpersistent = $this->get_report()->get_report_persistent();
if ($reportpersistent->get('type') === base::TYPE_CUSTOM_REPORT) {
permission::require_can_view_report($reportpersistent);
} else {
$this->get_report()->require_can_view();
}
}
/**
* Process the form submission
*
* @return int Number of applied filter instances
*/
public function process_dynamic_submission() {
$values = $this->get_data();
// Remove some unneeded fields, apply filters.
unset($values->reportid, $values->parameters);
$this->get_report()->set_filter_values((array) $values);
return $this->get_report()->get_applied_filter_count();
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
$defaults = [
'reportid' => $this->optional_param('reportid', 0, PARAM_INT),
'parameters' => $this->optional_param('parameters', 0, PARAM_RAW),
];
$this->set_data(array_merge($defaults, $this->get_report()->get_filter_values()));
}
/**
* URL of the page using this form
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/');
}
/**
* Filter form definition. It should provide necessary field itself, then allow all report filters to add their own elements
*/
protected function definition() {
global $OUTPUT;
$mform = $this->_form;
$mform->addElement('hidden', 'reportid');
$mform->setType('reportid', PARAM_INT);
$mform->addElement('hidden', 'parameters');
$mform->setType('parameters', PARAM_RAW);
// Allow each filter instance to add itself to this form, wrapping each inside custom header/footer template.
$filterinstances = $this->get_report()->get_filter_instances();
foreach ($filterinstances as $filterinstance) {
$header = $filterinstance->get_header();
// Check if filter has a custom header set.
if ($persistent = $filterinstance->get_filter_persistent()) {
if ('' !== (string) $persistent->get('heading')) {
$header = $persistent->get_formatted_heading($this->get_report()->get_context());
}
}
$mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/filters/header', [
'name' => $header,
]));
$filterinstance->setup_form($mform);
$mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/filters/footer', []));
}
$this->set_display_vertical();
// We'll add a second submit button to the form that will be used to reset current report filters.
$mform->registerNoSubmitButton('resetfilters');
$buttons = [];
$buttons[] = $mform->createElement('submit', 'submitbutton', get_string('apply', 'core_reportbuilder'));
$buttons[] = $mform->createElement('submit', 'resetfilters', get_string('resetall', 'core_reportbuilder'),
null, null, ['customclassoverride' => 'btn-link ml-1']);
$mform->addGroup($buttons, 'buttonar', get_string('formactions', 'core_form'), '', false)
->setHiddenLabel(true);
$mform->closeHeaderBefore('buttonar');
$mform->disable_form_change_checker();
}
}
+176
View File
@@ -0,0 +1,176 @@
<?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\form;
use context;
use context_system;
use core_reportbuilder\permission;
use moodle_url;
use core_form\dynamic_form;
use core_reportbuilder\datasource;
use core_reportbuilder\manager;
use core_reportbuilder\local\helpers\report as reporthelper;
use core_tag_tag;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("$CFG->libdir/formslib.php");
/**
* Report details form
*
* @package core_reportbuilder
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report extends dynamic_form {
/**
* Return instance of the custom report we are editing, or null when creating a new report
*
* @return datasource|null
*/
protected function get_custom_report(): ?datasource {
if ($reportid = $this->optional_param('id', 0, PARAM_INT)) {
/** @var datasource $customreport */
$customreport = manager::get_report_from_id($reportid);
return $customreport;
}
return null;
}
/**
* Return the context for the form, it should be that of the custom report itself, or system when creating a new report
*
* @return context
*/
public function get_context_for_dynamic_submission(): context {
if ($report = $this->get_custom_report()) {
return $report->get_context();
} else {
return context_system::instance();
}
}
/**
* Ensure current user is able to use this form
*
* A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
*/
protected function check_access_for_dynamic_submission(): void {
$report = $this->get_custom_report();
if ($report) {
permission::require_can_edit_report($report->get_report_persistent());
} else {
permission::require_can_create_report();
}
}
/**
* Form definition
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('text', 'name', get_string('name'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
// Allow user to select report source if creating a new report.
if (!$this->get_custom_report()) {
$default = ['' => ['' => get_string('selectareportsource', 'core_reportbuilder')]];
$mform->addElement('selectgroups', 'source', get_string('reportsource', 'core_reportbuilder'),
array_merge($default, manager::get_report_datasources()));
$mform->addRule('source', null, 'required', null, 'client');
$mform->addHelpButton('source', 'reportsource', 'core_reportbuilder');
$mform->addElement('advcheckbox', 'includedefaultsetup', get_string('includedefaultsetup', 'core_reportbuilder'));
$mform->setDefault('includedefaultsetup', 1);
$mform->addHelpButton('includedefaultsetup', 'includedefaultsetup', 'core_reportbuilder');
}
$mform->addElement('advcheckbox', 'uniquerows', get_string('uniquerows', 'core_reportbuilder'));
$mform->addHelpButton('uniquerows', 'uniquerows', 'core_reportbuilder');
$mform->addElement('tags', 'tags', get_string('tags'), [
'component' => 'core_reportbuilder', 'itemtype' => 'reportbuilder_report',
]);
}
/**
* Process the form submission
*
* @return string The URL to advance to upon completion
*/
public function process_dynamic_submission() {
$data = $this->get_data();
if ($data->id) {
$reportpersistent = reporthelper::update_report($data);
} else {
$reportpersistent = reporthelper::create_report($data, (bool)$data->includedefaultsetup);
}
return (new moodle_url('/reportbuilder/edit.php', ['id' => $reportpersistent->get('id')]))->out(false);
}
/**
* Load in existing data as form defaults
*/
public function set_data_for_dynamic_submission(): void {
if ($persistent = $this->get_custom_report()?->get_report_persistent()) {
$tags = core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $persistent->get('id'));
$this->set_data(array_merge((array) $persistent->to_record(), ['tags' => $tags]));
}
}
/**
* URL of the page using this form
*
* @return moodle_url
*/
public function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/reportbuilder/index.php');
}
/**
* Perform some extra moodle validation
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files): array {
$errors = [];
if (trim($data['name']) === '') {
$errors['name'] = get_string('required');
}
return $errors;
}
}
+259
View File
@@ -0,0 +1,259 @@
<?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\form;
use context;
use core_user;
use html_writer;
use moodle_url;
use core\output\notification;
use core_form\dynamic_form;
use core_reportbuilder\manager;
use core_reportbuilder\permission;
use core_reportbuilder\local\helpers\audience;
use core_reportbuilder\local\helpers\schedule as helper;
use core_reportbuilder\local\models\schedule as model;
use core_reportbuilder\local\report\base;
/**
* Schedule form
*
* @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 dynamic_form {
/**
* Return instance of the system report using the filter form
*
* @return base
*/
private function get_report(): base {
$reportid = $this->optional_param('reportid', 0, PARAM_INT);
return manager::get_report_from_id($reportid);
}
/**
* Return the context for the form, it should be that of the report itself
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
return $this->get_report()->get_context();
}
/**
* Ensure current user is able to use this form
*
* A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
*/
protected function check_access_for_dynamic_submission(): void {
$persistent = $this->get_report()->get_report_persistent();
permission::require_can_edit_report($persistent);
}
/**
* Form definition
*/
protected function definition() {
global $OUTPUT;
$mform = $this->_form;
$mform->addElement('hidden', 'reportid');
$mform->setType('reportid', PARAM_INT);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
// General fields.
$mform->addElement('header', 'headergeneral', get_string('general'));
$mform->addElement('text', 'name', get_string('name'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->addElement('select', 'format', get_string('format'), helper::get_format_options());
$mform->setType('format', PARAM_PLUGIN);
$mform->addElement('date_time_selector', 'timescheduled', get_string('startingfrom'), ['optional' => false]);
$mform->setType('timescheduled', PARAM_INT);
$mform->addElement('select', 'recurrence', get_string('recurrence', 'core_reportbuilder'),
helper::get_recurrence_options());
$mform->setType('recurrence', PARAM_INT);
// View report data as.
$context = $this->get_context_for_dynamic_submission();
if (has_capability('moodle/reportbuilder:scheduleviewas', $context)) {
$mform->addElement('select', 'userviewas', get_string('scheduleviewas', 'core_reportbuilder'),
helper::get_viewas_options());
$mform->setType('userviewas', PARAM_INT);
$options = [
'ajax' => 'core_user/form_user_selector',
'multiple' => false,
'valuehtmlcallback' => function($userid) use ($context): string {
$user = core_user::get_user($userid);
return fullname($user, has_capability('moodle/site:viewfullnames', $context));
}
];
$mform->addElement('autocomplete', 'user', get_string('user'), [], $options)->setHiddenLabel(true);
$mform->hideIf('user', 'userviewas', 'neq', model::REPORT_VIEWAS_USER);
}
// Audience fields.
$mform->addElement('header', 'headeraudience', get_string('audience', 'core_reportbuilder'));
$mform->setExpanded('headeraudience', true);
$audiences = audience::get_base_records($this->optional_param('reportid', 0, PARAM_INT));
if (empty($audiences)) {
$notification = new notification(get_string('noaudiences', 'core_reportbuilder'), notification::NOTIFY_INFO, false);
$mform->addElement('static', 'noaudiences', '', $OUTPUT->render($notification));
}
$audiencecheckboxes = [];
foreach ($audiences as $audience) {
$persistent = $audience->get_persistent();
// Check for a custom name, otherwise fall back to default.
if ('' === $audiencelabel = $persistent->get_formatted_heading($context)) {
$audiencelabel = get_string('audiencelabel', 'core_reportbuilder', (object) [
'name' => $audience->get_name(),
'description' => $audience->get_description(),
]);
}
$audiencecheckboxes[] = $mform->createElement('checkbox', $persistent->get('id'), $audiencelabel);
}
$mform->addElement('group', 'audiences', '', $audiencecheckboxes, html_writer::div('', 'w-100 mb-2'));
// Message fields.
$mform->addElement('header', 'headermessage', get_string('messagecontent', 'core_reportbuilder'));
$mform->addElement('text', 'subject', get_string('messagesubject', 'core_reportbuilder'));
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', null, 'required', null, 'client');
$mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255);
$mform->addElement('editor', 'message', get_string('messagebody', 'core_reportbuilder'), null, ['autosave' => false]);
$mform->setType('message', PARAM_RAW);
$mform->addRule('message', null, 'required', null, 'client');
// Advanced.
$mform->addElement('header', 'headeradvanced', get_string('advanced'));
$mform->addElement('select', 'reportempty', get_string('scheduleempty', 'core_reportbuilder'),
helper::get_report_empty_options());
$mform->setType('reportempty', PARAM_INT);
}
/**
* Load form data if we are editing an existing schedule
*/
public function set_data_for_dynamic_submission(): void {
$reportid = $this->optional_param('reportid', 0, PARAM_INT);
$scheduleid = $this->optional_param('id', 0, PARAM_INT);
if ($scheduleid > 0) {
$schedule = model::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
$data = (array) $schedule->to_record();
// Pre-process some of the form fields.
if (!in_array($data['userviewas'], [model::REPORT_VIEWAS_CREATOR, model::REPORT_VIEWAS_RECIPIENT])) {
$data['user'] = $data['userviewas'];
$data['userviewas'] = model::REPORT_VIEWAS_USER;
}
$audiences = json_decode($data['audiences']);
$data['audiences'] = array_fill_keys($audiences, 1);
$data['message'] = [
'text' => $data['message'],
'format' => $data['messageformat'],
];
$this->set_data($data);
} else {
$this->set_data(['reportid' => $reportid]);
}
}
/**
* Form validation
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
if (trim($data['name']) === '') {
$errors['name'] = get_string('required');
}
// Make sure specific user was selected, if required.
if (array_key_exists('userviewas', $data) &&
(int) $data['userviewas'] === model::REPORT_VIEWAS_USER && empty($data['user'])) {
$errors['user'] = get_string('required');
}
if (empty($data['audiences'])) {
$errors['audiences'] = get_string('required');
}
return $errors;
}
/**
* Process form submission
*/
public function process_dynamic_submission(): void {
$data = $this->get_data();
// Pre-process some of the form fields.
if (property_exists($data, 'userviewas') && (int) $data->userviewas === model::REPORT_VIEWAS_USER) {
$data->userviewas = (int) $data->user;
}
$data->audiences = json_encode(array_keys($data->audiences));
['text' => $data->message, 'format' => $data->messageformat] = $data->message;
if ($data->id) {
helper::update_schedule($data);
} else {
helper::create_schedule($data);
}
}
/**
* URL of the page using this form
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/reportbuilder/edit.php', ['id' => $this->optional_param('reportid', 0, PARAM_INT)], 'schedules');
}
}