Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402]

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