Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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\output\dynamictabs;
  20  
  21  use core\output\dynamic_tabs\base;
  22  use core_reportbuilder\external\custom_report_audience_cards_exporter;
  23  use core_reportbuilder\local\helpers\audience as audience_helper;
  24  use core_reportbuilder\local\models\report;
  25  use core_reportbuilder\output\audience_heading_editable;
  26  use core_reportbuilder\permission;
  27  use renderer_base;
  28  
  29  /**
  30   * Audience dynamic tab
  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 audience extends base {
  37  
  38      /**
  39       * Export this for use in a mustache template context.
  40       *
  41       * @param renderer_base $output
  42       * @return array
  43       */
  44      public function export_for_template(renderer_base $output): array {
  45          $reportid = (int) $this->data['reportid'];
  46  
  47          // Get all the audiences types to populate the left menu.
  48          $menucardsexporter = new custom_report_audience_cards_exporter(null);
  49          $menucards = (array) $menucardsexporter->export($output);
  50  
  51          // Get all current audiences instances for this report.
  52          $audienceinstances = $this->get_all_report_audiences($reportid);
  53  
  54          return [
  55              'tabheading' => get_string('audience', 'core_reportbuilder'),
  56              'reportid' => $reportid,
  57              'contextid' => (new report($reportid))->get('contextid'),
  58              'sidebarmenucards' => $menucards,
  59              'instances' => $audienceinstances,
  60              'hasinstances' => !empty($audienceinstances),
  61          ];
  62      }
  63  
  64      /**
  65       * The label to be displayed on the tab
  66       *
  67       * @return string
  68       */
  69      public function get_tab_label(): string {
  70          return get_string('audience', 'core_reportbuilder');
  71      }
  72  
  73      /**
  74       * Check permission of the current user to access this tab
  75       *
  76       * @return bool
  77       */
  78      public function is_available(): bool {
  79          $reportpersistent = new report((int)$this->data['reportid']);
  80          return permission::can_edit_report($reportpersistent);
  81      }
  82  
  83      /**
  84       * Template to use to display tab contents
  85       *
  86       * @return string
  87       */
  88      public function get_template(): string {
  89          return 'core_reportbuilder/local/dynamictabs/audience';
  90      }
  91  
  92      /**
  93       * Get all current audiences instances for this report.
  94       *
  95       * @param int $reportid
  96       * @return array
  97       */
  98      private function get_all_report_audiences(int $reportid): array {
  99          global $PAGE;
 100  
 101          $renderer = $PAGE->get_renderer('core');
 102  
 103          $audienceinstances = [];
 104          $showormessage = false;
 105  
 106          // Retrieve list of audiences that are used in report schedules, to warn user when editing.
 107          $scheduleaudiences = audience_helper::get_audiences_for_report_schedules($reportid);
 108  
 109          $reportaudiences = audience_helper::get_base_records($reportid);
 110          foreach ($reportaudiences as $reportaudience) {
 111              $persistent = $reportaudience->get_persistent();
 112              $canedit = $reportaudience->user_can_edit();
 113  
 114              $editable = new audience_heading_editable(0, $persistent);
 115  
 116              $audienceinstances[] = [
 117                  'instanceid' => $persistent->get('id'),
 118                  'description' => $reportaudience->get_description(),
 119                  'heading' => $reportaudience->get_name(),
 120                  'headingeditable' => $editable->render($renderer),
 121                  'editwarning' => in_array($persistent->get('id'), $scheduleaudiences) ?
 122                      get_string('audienceusedbyschedule', 'core_reportbuilder') : '',
 123                  'canedit' => $canedit,
 124                  'candelete' => $canedit,
 125                  'showormessage' => $showormessage,
 126              ];
 127  
 128              $showormessage = true;
 129          }
 130  
 131          return $audienceinstances;
 132      }
 133  }