| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           namespace core_reportbuilder\form;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use context;
  | 
        
        
            | 
            | 
           22 | 
           use context_system;
  | 
        
        
            | 
            | 
           23 | 
           use core_reportbuilder\permission;
  | 
        
        
            | 
            | 
           24 | 
           use moodle_url;
  | 
        
        
            | 
            | 
           25 | 
           use core_form\dynamic_form;
  | 
        
        
            | 
            | 
           26 | 
           use core_reportbuilder\datasource;
  | 
        
        
            | 
            | 
           27 | 
           use core_reportbuilder\manager;
  | 
        
        
            | 
            | 
           28 | 
           use core_reportbuilder\local\helpers\report as reporthelper;
  | 
        
        
            | 
            | 
           29 | 
           use core_tag_tag;
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           global $CFG;
  | 
        
        
            | 
            | 
           34 | 
           require_once("$CFG->libdir/formslib.php");
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           /**
  | 
        
        
            | 
            | 
           37 | 
            * Report details form
  | 
        
        
            | 
            | 
           38 | 
            *
  | 
        
        
            | 
            | 
           39 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           40 | 
            * @copyright   2021 David Matamoros <davidmc@moodle.com>
  | 
        
        
            | 
            | 
           41 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           42 | 
            */
  | 
        
        
            | 
            | 
           43 | 
           class report extends dynamic_form {
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               /**
  | 
        
        
            | 
            | 
           46 | 
                * Return instance of the custom report we are editing, or null when creating a new report
  | 
        
        
            | 
            | 
           47 | 
                *
  | 
        
        
            | 
            | 
           48 | 
                * @return datasource|null
  | 
        
        
            | 
            | 
           49 | 
                */
  | 
        
        
            | 
            | 
           50 | 
               protected function get_custom_report(): ?datasource {
  | 
        
        
            | 
            | 
           51 | 
                   if ($reportid = $this->optional_param('id', 0, PARAM_INT)) {
  | 
        
        
            | 
            | 
           52 | 
                       /** @var datasource $customreport */
  | 
        
        
            | 
            | 
           53 | 
                       $customreport = manager::get_report_from_id($reportid);
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
                       return $customreport;
  | 
        
        
            | 
            | 
           56 | 
                   }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
                   return null;
  | 
        
        
            | 
            | 
           59 | 
               }
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
               /**
  | 
        
        
            | 
            | 
           62 | 
                * Return the context for the form, it should be that of the custom report itself, or system when creating a new report
  | 
        
        
            | 
            | 
           63 | 
                *
  | 
        
        
            | 
            | 
           64 | 
                * @return context
  | 
        
        
            | 
            | 
           65 | 
                */
  | 
        
        
            | 
            | 
           66 | 
               public function get_context_for_dynamic_submission(): context {
  | 
        
        
            | 
            | 
           67 | 
                   if ($report = $this->get_custom_report()) {
  | 
        
        
            | 
            | 
           68 | 
                       return $report->get_context();
  | 
        
        
            | 
            | 
           69 | 
                   } else {
  | 
        
        
            | 
            | 
           70 | 
                       return context_system::instance();
  | 
        
        
            | 
            | 
           71 | 
                   }
  | 
        
        
            | 
            | 
           72 | 
               }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
               /**
  | 
        
        
            | 
            | 
           75 | 
                * Ensure current user is able to use this form
  | 
        
        
            | 
            | 
           76 | 
                *
  | 
        
        
            | 
            | 
           77 | 
                * A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
  | 
        
        
            | 
            | 
           78 | 
                */
  | 
        
        
            | 
            | 
           79 | 
               protected function check_access_for_dynamic_submission(): void {
  | 
        
        
            | 
            | 
           80 | 
                   $report = $this->get_custom_report();
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   if ($report) {
  | 
        
        
            | 
            | 
           83 | 
                       permission::require_can_edit_report($report->get_report_persistent());
  | 
        
        
            | 
            | 
           84 | 
                   } else {
  | 
        
        
            | 
            | 
           85 | 
                       permission::require_can_create_report();
  | 
        
        
            | 
            | 
           86 | 
                   }
  | 
        
        
            | 
            | 
           87 | 
               }
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               /**
  | 
        
        
            | 
            | 
           90 | 
                * Form definition
  | 
        
        
            | 
            | 
           91 | 
                */
  | 
        
        
            | 
            | 
           92 | 
               public function definition() {
  | 
        
        
            | 
            | 
           93 | 
                   $mform = $this->_form;
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   $mform->addElement('hidden', 'id');
  | 
        
        
            | 
            | 
           96 | 
                   $mform->setType('id', PARAM_INT);
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   $mform->addElement('text', 'name', get_string('name'));
  | 
        
        
            | 
            | 
           99 | 
                   $mform->setType('name', PARAM_TEXT);
  | 
        
        
            | 
            | 
           100 | 
                   $mform->addRule('name', null, 'required', null, 'client');
  | 
        
        
            | 
            | 
           101 | 
                   $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   // Allow user to select report source if creating a new report.
  | 
        
        
            | 
            | 
           104 | 
                   if (!$this->get_custom_report()) {
  | 
        
        
            | 
            | 
           105 | 
                       $default = ['' => ['' => get_string('selectareportsource', 'core_reportbuilder')]];
  | 
        
        
            | 
            | 
           106 | 
                       $mform->addElement('selectgroups', 'source', get_string('reportsource', 'core_reportbuilder'),
  | 
        
        
            | 
            | 
           107 | 
                           array_merge($default, manager::get_report_datasources()));
  | 
        
        
            | 
            | 
           108 | 
                       $mform->addRule('source', null, 'required', null, 'client');
  | 
        
        
            | 
            | 
           109 | 
                       $mform->addHelpButton('source', 'reportsource', 'core_reportbuilder');
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                       $mform->addElement('advcheckbox', 'includedefaultsetup', get_string('includedefaultsetup', 'core_reportbuilder'));
  | 
        
        
            | 
            | 
           112 | 
                       $mform->setDefault('includedefaultsetup', 1);
  | 
        
        
            | 
            | 
           113 | 
                       $mform->addHelpButton('includedefaultsetup', 'includedefaultsetup', 'core_reportbuilder');
  | 
        
        
            | 
            | 
           114 | 
                   }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $mform->addElement('advcheckbox', 'uniquerows', get_string('uniquerows', 'core_reportbuilder'));
  | 
        
        
            | 
            | 
           117 | 
                   $mform->addHelpButton('uniquerows', 'uniquerows', 'core_reportbuilder');
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                   $mform->addElement('tags', 'tags', get_string('tags'), [
  | 
        
        
            | 
            | 
           120 | 
                       'component' => 'core_reportbuilder', 'itemtype' => 'reportbuilder_report',
  | 
        
        
            | 
            | 
           121 | 
                   ]);
  | 
        
        
            | 
            | 
           122 | 
               }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               /**
  | 
        
        
            | 
            | 
           125 | 
                * Process the form submission
  | 
        
        
            | 
            | 
           126 | 
                *
  | 
        
        
            | 
            | 
           127 | 
                * @return string The URL to advance to upon completion
  | 
        
        
            | 
            | 
           128 | 
                */
  | 
        
        
            | 
            | 
           129 | 
               public function process_dynamic_submission() {
  | 
        
        
            | 
            | 
           130 | 
                   $data = $this->get_data();
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   if ($data->id) {
  | 
        
        
            | 
            | 
           133 | 
                       $reportpersistent = reporthelper::update_report($data);
  | 
        
        
            | 
            | 
           134 | 
                   } else {
  | 
        
        
            | 
            | 
           135 | 
                       $reportpersistent = reporthelper::create_report($data, (bool)$data->includedefaultsetup);
  | 
        
        
            | 
            | 
           136 | 
                   }
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
                   return (new moodle_url('/reportbuilder/edit.php', ['id' => $reportpersistent->get('id')]))->out(false);
  | 
        
        
            | 
            | 
           139 | 
               }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
               /**
  | 
        
        
            | 
            | 
           142 | 
                * Load in existing data as form defaults
  | 
        
        
            | 
            | 
           143 | 
                */
  | 
        
        
            | 
            | 
           144 | 
               public function set_data_for_dynamic_submission(): void {
  | 
        
        
            | 
            | 
           145 | 
                   if ($persistent = $this->get_custom_report()?->get_report_persistent()) {
  | 
        
        
            | 
            | 
           146 | 
                       $tags = core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $persistent->get('id'));
  | 
        
        
            | 
            | 
           147 | 
                       $this->set_data(array_merge((array) $persistent->to_record(), ['tags' => $tags]));
  | 
        
        
            | 
            | 
           148 | 
                   }
  | 
        
        
            | 
            | 
           149 | 
               }
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
               /**
  | 
        
        
            | 
            | 
           152 | 
                * URL of the page using this form
  | 
        
        
            | 
            | 
           153 | 
                *
  | 
        
        
            | 
            | 
           154 | 
                * @return moodle_url
  | 
        
        
            | 
            | 
           155 | 
                */
  | 
        
        
            | 
            | 
           156 | 
               public function get_page_url_for_dynamic_submission(): moodle_url {
  | 
        
        
            | 
            | 
           157 | 
                   return new moodle_url('/reportbuilder/index.php');
  | 
        
        
            | 
            | 
           158 | 
               }
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
               /**
  | 
        
        
            | 
            | 
           161 | 
                * Perform some extra moodle validation
  | 
        
        
            | 
            | 
           162 | 
                *
  | 
        
        
            | 
            | 
           163 | 
                * @param array $data
  | 
        
        
            | 
            | 
           164 | 
                * @param array $files
  | 
        
        
            | 
            | 
           165 | 
                * @return array
  | 
        
        
            | 
            | 
           166 | 
                */
  | 
        
        
            | 
            | 
           167 | 
               public function validation($data, $files): array {
  | 
        
        
            | 
            | 
           168 | 
                   $errors = [];
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
                   if (trim($data['name']) === '') {
  | 
        
        
            | 
            | 
           171 | 
                       $errors['name'] = get_string('required');
  | 
        
        
            | 
            | 
           172 | 
                   }
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   return $errors;
  | 
        
        
            | 
            | 
           175 | 
               }
  | 
        
        
            | 
            | 
           176 | 
           }
  |