| 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\local\systemreports;
 | 
        
           |  |  | 20 |   | 
        
           | 1441 | ariadna | 21 | use core\lang_string;
 | 
        
           | 1 | efrain | 22 | use core_reportbuilder\local\models\audience;
 | 
        
           |  |  | 23 | use core_reportbuilder\local\models\report;
 | 
        
           |  |  | 24 | use core_reportbuilder\permission;
 | 
        
           |  |  | 25 | use core_reportbuilder\system_report;
 | 
        
           |  |  | 26 | use core_reportbuilder\local\entities\user;
 | 
        
           | 1441 | ariadna | 27 | use core_reportbuilder\local\filters\audience as audience_filter;
 | 
        
           | 1 | efrain | 28 | use core_reportbuilder\local\helpers\audience as audience_helper;
 | 
        
           | 1441 | ariadna | 29 | use core_reportbuilder\local\report\filter;
 | 
        
           | 1 | efrain | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * Report access list
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package     core_reportbuilder
 | 
        
           |  |  | 35 |  * @copyright   2021 David Matamoros <davidmc@moodle.com>
 | 
        
           |  |  | 36 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class report_access_list extends system_report {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Initialise the report
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     protected function initialise(): void {
 | 
        
           |  |  | 44 |         $userentity = new user();
 | 
        
           |  |  | 45 |         $userentityalias = $userentity->get_table_alias('user');
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         $this->set_main_table('user', $userentityalias);
 | 
        
           |  |  | 48 |         $this->add_entity($userentity);
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         // Find users allowed to view the report thru the report audiences.
 | 
        
           |  |  | 51 |         $audiences = audience::get_records(['reportid' => $this->get_parameter('id', 0, PARAM_INT)]);
 | 
        
           |  |  | 52 |         [$wheres, $params] = audience_helper::user_audience_sql($audiences, $userentityalias);
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         if (count($wheres) > 0) {
 | 
        
           |  |  | 55 |             $select = '(' . implode(' OR ', $wheres) . ')';
 | 
        
           |  |  | 56 |         } else {
 | 
        
           |  |  | 57 |             $select = "1=0";
 | 
        
           |  |  | 58 |         }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         $this->add_base_condition_sql($select, $params);
 | 
        
           |  |  | 61 |         $this->add_base_condition_simple("{$userentityalias}.deleted", 0);
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         $this->add_columns($userentity);
 | 
        
           |  |  | 64 |         $this->add_filters($userentity);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         $this->set_downloadable(false);
 | 
        
           |  |  | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |     /**
 | 
        
           |  |  | 70 |      * Ensure we can view the report
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @return bool
 | 
        
           |  |  | 73 |      */
 | 
        
           |  |  | 74 |     protected function can_view(): bool {
 | 
        
           |  |  | 75 |         $reportid = $this->get_parameter('id', 0, PARAM_INT);
 | 
        
           |  |  | 76 |         $report = report::get_record(['id' => $reportid], MUST_EXIST);
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         return permission::can_edit_report($report);
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * Add columns to report
 | 
        
           |  |  | 83 |      *
 | 
        
           |  |  | 84 |      * @param user $userentity
 | 
        
           |  |  | 85 |      */
 | 
        
           |  |  | 86 |     protected function add_columns(user $userentity): void {
 | 
        
           |  |  | 87 |         $this->add_column($userentity->get_column('fullnamewithpicturelink'));
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         // Include all identity field columns.
 | 
        
           |  |  | 90 |         $identitycolumns = $userentity->get_identity_columns($this->get_context());
 | 
        
           |  |  | 91 |         foreach ($identitycolumns as $identitycolumn) {
 | 
        
           |  |  | 92 |             $this->add_column($identitycolumn);
 | 
        
           |  |  | 93 |         }
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         $this->set_initial_sort_column('user:fullnamewithpicturelink', SORT_ASC);
 | 
        
           |  |  | 96 |     }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |     /**
 | 
        
           |  |  | 99 |      * Add filters to report
 | 
        
           |  |  | 100 |      *
 | 
        
           |  |  | 101 |      * @param user $userentity
 | 
        
           |  |  | 102 |      */
 | 
        
           |  |  | 103 |     protected function add_filters(user $userentity): void {
 | 
        
           |  |  | 104 |         $this->add_filter($userentity->get_filter('fullname'));
 | 
        
           |  |  | 105 |   | 
        
           | 1441 | ariadna | 106 |         // Include audience filter.
 | 
        
           |  |  | 107 |         $this->add_filter((new filter(
 | 
        
           |  |  | 108 |             audience_filter::class,
 | 
        
           |  |  | 109 |             'audience',
 | 
        
           |  |  | 110 |             new lang_string('audience', 'core_reportbuilder'),
 | 
        
           |  |  | 111 |             $userentity->get_entity_name(),
 | 
        
           |  |  | 112 |             $userentity->get_table_alias('user') . '.id',
 | 
        
           |  |  | 113 |         ))
 | 
        
           |  |  | 114 |             ->set_options([
 | 
        
           |  |  | 115 |                 'reportid' => $this->get_parameter('id', 0, PARAM_INT),
 | 
        
           |  |  | 116 |             ])
 | 
        
           |  |  | 117 |         );
 | 
        
           |  |  | 118 |   | 
        
           | 1 | efrain | 119 |         // Include all identity field filters.
 | 
        
           |  |  | 120 |         $identityfilters = $userentity->get_identity_filters($this->get_context());
 | 
        
           |  |  | 121 |         foreach ($identityfilters as $identityfilter) {
 | 
        
           |  |  | 122 |             $this->add_filter($identityfilter);
 | 
        
           |  |  | 123 |         }
 | 
        
           |  |  | 124 |     }
 | 
        
           |  |  | 125 | }
 |