Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 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          // Get all the audiences types to populate the left menu.
  46          $menucardsexporter = new custom_report_audience_cards_exporter(null);
  47          $menucards = (array) $menucardsexporter->export($output);
  48  
  49          // Get all current audiences instances for this report.
  50          $audienceinstances = $this->get_all_report_audiences();
  51  
  52          $data = [
  53              'tabheading' => get_string('audience', 'core_reportbuilder'),
  54              'reportid' => $this->data['reportid'],
  55              'contextid' => (new report((int)$this->data['reportid']))->get('contextid'),
  56              'sidebarmenucards' => $menucards,
  57              'instances' => $audienceinstances,
  58              'hasinstances' => !empty($audienceinstances),
  59          ];
  60  
  61          return $data;
  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       * @return array
  96       */
  97      private function get_all_report_audiences(): array {
  98          global $PAGE;
  99  
 100          $renderer = $PAGE->get_renderer('core');
 101  
 102          $audienceinstances = [];
 103          $reportaudiences = audience_helper::get_base_records((int)$this->data['reportid']);
 104          $showormessage = false;
 105          foreach ($reportaudiences as $reportaudience) {
 106              $persistent = $reportaudience->get_persistent();
 107              $canedit = $reportaudience->user_can_edit();
 108  
 109              $editable = new audience_heading_editable(0, $persistent);
 110  
 111              $params = [
 112                  'instanceid' => $persistent->get('id'),
 113                  'description' => $reportaudience->get_description(),
 114                  'heading' => $reportaudience->get_name(),
 115                  'headingeditable' => $editable->render($renderer),
 116                  'canedit' => $canedit,
 117                  'candelete' => $canedit,
 118                  'showormessage' => $showormessage,
 119              ];
 120              $audienceinstances[] = $params;
 121              $showormessage = true;
 122          }
 123  
 124          return $audienceinstances;
 125      }
 126  }