| 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 | namespace block_dedication\local\systemreports;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use block_dedication\local\entities\dedication;
 | 
        
           |  |  | 20 | use core_reportbuilder\local\helpers\database;
 | 
        
           |  |  | 21 | use core_reportbuilder\system_report;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | /**
 | 
        
           |  |  | 24 |  * Dedication system level report.
 | 
        
           |  |  | 25 |  *
 | 
        
           |  |  | 26 |  * @package    block_dedication
 | 
        
           |  |  | 27 |  * @copyright  2022 Canterbury University
 | 
        
           |  |  | 28 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 29 |  */
 | 
        
           |  |  | 30 | class userreport extends system_report {
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      * Initialise report, we need to set the main table, load our entities and set columns/filters
 | 
        
           |  |  | 34 |      */
 | 
        
           |  |  | 35 |     protected function initialise(): void {
 | 
        
           |  |  | 36 |         global $PAGE, $USER;
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |         // We need to ensure page context is always set, as required by output and string formatting.
 | 
        
           |  |  | 39 |         $course = get_course($this->get_context()->instanceid);
 | 
        
           |  |  | 40 |         $PAGE->set_context($this->get_context());
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |         $userid = optional_param('userid', $USER->id, PARAM_INT);
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |         // Our main entity, it contains all of the column definitions that we need.
 | 
        
           |  |  | 45 |         $entitymain = new dedication();
 | 
        
           |  |  | 46 |         $entitymainalias = $entitymain->get_table_alias('block_dedication');
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         $this->set_main_table('block_dedication', $entitymainalias);
 | 
        
           |  |  | 49 |         $this->add_entity($entitymain);
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |         $param1 = database::generate_param_name();
 | 
        
           |  |  | 52 |         $param2 = database::generate_param_name();
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         $wheresql = "$entitymainalias.courseid = :$param1 AND $entitymainalias.userid = :$param2";
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         $this->add_base_condition_sql($wheresql,
 | 
        
           |  |  | 57 |             [$param1 => $course->id, $param2 => $userid]);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         // Now we can call our helper methods to add the content we want to include in the report.
 | 
        
           |  |  | 60 |         $this->add_columns();
 | 
        
           |  |  | 61 |         $this->add_filters();
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         // Set if report can be downloaded.
 | 
        
           |  |  | 64 |         $this->set_downloadable(true);
 | 
        
           |  |  | 65 |         $this->set_initial_sort_column('dedication:timestart', SORT_ASC);
 | 
        
           |  |  | 66 |     }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     /**
 | 
        
           |  |  | 69 |      * Validates access to view this report
 | 
        
           |  |  | 70 |      *
 | 
        
           |  |  | 71 |      * @return bool
 | 
        
           |  |  | 72 |      */
 | 
        
           |  |  | 73 |     protected function can_view(): bool {
 | 
        
           |  |  | 74 |         global $USER;
 | 
        
           |  |  | 75 |         $userid = optional_param('userid', $USER->id, PARAM_INT);
 | 
        
           |  |  | 76 |         if ($userid == $USER->id) {
 | 
        
           |  |  | 77 |             return true;
 | 
        
           |  |  | 78 |         }
 | 
        
           |  |  | 79 |         // Not viewing own report, check if can view others.
 | 
        
           |  |  | 80 |         return has_capability('block/dedication:viewreports', $this->get_context());
 | 
        
           |  |  | 81 |     }
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |     /**
 | 
        
           |  |  | 84 |      * Adds the columns we want to display in the report
 | 
        
           |  |  | 85 |      *
 | 
        
           |  |  | 86 |      * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
 | 
        
           |  |  | 87 |      * unique identifier
 | 
        
           |  |  | 88 |      */
 | 
        
           |  |  | 89 |     public function add_columns(): void {
 | 
        
           |  |  | 90 |         $columns = [
 | 
        
           |  |  | 91 |             'dedication:timestart',
 | 
        
           |  |  | 92 |             'dedication:timespent',
 | 
        
           |  |  | 93 |         ];
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         $this->add_columns_from_entities($columns);
 | 
        
           |  |  | 96 |     }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |     /**
 | 
        
           |  |  | 99 |      * Adds the filters we want to display in the report
 | 
        
           |  |  | 100 |      *
 | 
        
           |  |  | 101 |      * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
 | 
        
           |  |  | 102 |      * unique identifier
 | 
        
           |  |  | 103 |      */
 | 
        
           |  |  | 104 |     protected function add_filters(): void {
 | 
        
           |  |  | 105 |         $filters = [
 | 
        
           |  |  | 106 |             'dedication:timestart',
 | 
        
           |  |  | 107 |             'dedication:timespent',
 | 
        
           |  |  | 108 |         ];
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         $this->add_filters_from_entities($filters);
 | 
        
           |  |  | 111 |     }
 | 
        
           |  |  | 112 | }
 |