| 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\event;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | use coding_exception;
 | 
        
           |  |  | 22 | use core\event\base;
 | 
        
           |  |  | 23 | use core_reportbuilder\local\models\schedule;
 | 
        
           |  |  | 24 | use moodle_url;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | /**
 | 
        
           |  |  | 27 |  * Report builder custom report schedule created event class.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package     core_reportbuilder
 | 
        
           |  |  | 30 |  * @copyright   2021 David Matamoros <davidmc@moodle.com>
 | 
        
           |  |  | 31 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @property-read array $other {
 | 
        
           |  |  | 34 |  *      Extra information about the event.
 | 
        
           |  |  | 35 |  *
 | 
        
           |  |  | 36 |  *      - int    reportid:      The id of the report
 | 
        
           |  |  | 37 |  * }
 | 
        
           |  |  | 38 |  */
 | 
        
           |  |  | 39 | class schedule_created extends base {
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * Initialise the event data.
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     protected function init() {
 | 
        
           |  |  | 45 |         $this->data['objecttable'] = schedule::TABLE;
 | 
        
           |  |  | 46 |         $this->data['crud'] = 'c';
 | 
        
           |  |  | 47 |         $this->data['edulevel'] = self::LEVEL_OTHER;
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           |  |  | 51 |      * Creates an instance from a report schedule object
 | 
        
           |  |  | 52 |      *
 | 
        
           |  |  | 53 |      * @param schedule $schedule
 | 
        
           |  |  | 54 |      * @return self
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     public static function create_from_object(schedule $schedule): self {
 | 
        
           |  |  | 57 |         $eventparams = [
 | 
        
           |  |  | 58 |             'context'  => $schedule->get_report()->get_context(),
 | 
        
           |  |  | 59 |             'objectid' => $schedule->get('id'),
 | 
        
           |  |  | 60 |             'other' => [
 | 
        
           |  |  | 61 |                 'reportid' => $schedule->get('reportid'),
 | 
        
           |  |  | 62 |             ]
 | 
        
           |  |  | 63 |         ];
 | 
        
           |  |  | 64 |         $event = self::create($eventparams);
 | 
        
           |  |  | 65 |         $event->add_record_snapshot($event->objecttable, $schedule->to_record());
 | 
        
           |  |  | 66 |         return $event;
 | 
        
           |  |  | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |     /**
 | 
        
           |  |  | 70 |      * Returns localised general event name.
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @return string
 | 
        
           |  |  | 73 |      */
 | 
        
           |  |  | 74 |     public static function get_name() {
 | 
        
           |  |  | 75 |         return get_string('schedulecreated', 'core_reportbuilder');
 | 
        
           |  |  | 76 |     }
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |     /**
 | 
        
           |  |  | 79 |      * Returns non-localised description of what happened.
 | 
        
           |  |  | 80 |      *
 | 
        
           |  |  | 81 |      * @return string
 | 
        
           |  |  | 82 |      */
 | 
        
           |  |  | 83 |     public function get_description() {
 | 
        
           |  |  | 84 |         $reportid = $this->other['reportid'];
 | 
        
           |  |  | 85 |         return "The user with id '$this->userid' created a schedule in the custom report with id '$reportid'.";
 | 
        
           |  |  | 86 |     }
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |     /**
 | 
        
           |  |  | 89 |      * Custom validations.
 | 
        
           |  |  | 90 |      *
 | 
        
           |  |  | 91 |      * @throws coding_exception
 | 
        
           |  |  | 92 |      */
 | 
        
           |  |  | 93 |     protected function validate_data(): void {
 | 
        
           |  |  | 94 |         parent::validate_data();
 | 
        
           |  |  | 95 |         if (!isset($this->objectid)) {
 | 
        
           |  |  | 96 |             throw new coding_exception('The \'objectid\' must be set.');
 | 
        
           |  |  | 97 |         }
 | 
        
           |  |  | 98 |         if (!isset($this->other['reportid'])) {
 | 
        
           |  |  | 99 |             throw new coding_exception('The \'reportid\' must be set in other.');
 | 
        
           |  |  | 100 |         }
 | 
        
           |  |  | 101 |     }
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |     /**
 | 
        
           |  |  | 104 |      * Returns relevant URL.
 | 
        
           |  |  | 105 |      *
 | 
        
           |  |  | 106 |      * @return moodle_url
 | 
        
           |  |  | 107 |      */
 | 
        
           |  |  | 108 |     public function get_url(): moodle_url {
 | 
        
           |  |  | 109 |         return new moodle_url('/reportbuilder/edit.php', ['id' => $this->other['reportid']], 'schedules');
 | 
        
           |  |  | 110 |     }
 | 
        
           |  |  | 111 | }
 |