Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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_menu_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          $menucardexporter = new custom_report_menu_cards_exporter(null, [
  47              'menucards' => audience_helper::get_all_audiences_menu_types()
  48          ]);
  49          $menucards = (array) $menucardexporter->export($output);
  50  
  51          // Get all current audiences instances for this report.
  52          $audienceinstances = $this->get_all_report_audiences();
  53  
  54          $data = [
  55              'tabheading' => get_string('audience', 'core_reportbuilder'),
  56              'reportid' => $this->data['reportid'],
  57              'contextid' => (new report((int)$this->data['reportid']))->get('contextid'),
  58              'sidebarmenucards' => $menucards,
  59              'instances' => $audienceinstances,
  60              'hasinstances' => !empty($audienceinstances),
  61          ];
  62  
  63          return $data;
  64      }
  65  
  66      /**
  67       * The label to be displayed on the tab
  68       *
  69       * @return string
  70       */
  71      public function get_tab_label(): string {
  72          return get_string('audience', 'core_reportbuilder');
  73      }
  74  
  75      /**
  76       * Check permission of the current user to access this tab
  77       *
  78       * @return bool
  79       */
  80      public function is_available(): bool {
  81          $reportpersistent = new report((int)$this->data['reportid']);
  82          return permission::can_edit_report($reportpersistent);
  83      }
  84  
  85      /**
  86       * Template to use to display tab contents
  87       *
  88       * @return string
  89       */
  90      public function get_template(): string {
  91          return 'core_reportbuilder/local/dynamictabs/audience';
  92      }
  93  
  94      /**
  95       * Get all current audiences instances for this report.
  96       *
  97       * @return array
  98       */
  99      private function get_all_report_audiences(): array {
 100          global $PAGE;
 101  
 102          $renderer = $PAGE->get_renderer('core');
 103  
 104          $audienceinstances = [];
 105          $reportaudiences = audience_helper::get_base_records((int)$this->data['reportid']);
 106          $showormessage = false;
 107          foreach ($reportaudiences as $reportaudience) {
 108              $persistent = $reportaudience->get_persistent();
 109              $canedit = $reportaudience->user_can_edit();
 110  
 111              $editable = new audience_heading_editable($persistent->get('id'));
 112  
 113              $params = [
 114                  'instanceid' => $persistent->get('id'),
 115                  'description' => $reportaudience->get_description(),
 116                  'heading' => $reportaudience->get_name(),
 117                  'headingeditable' => $editable->render($renderer),
 118                  'canedit' => $canedit,
 119                  'candelete' => $canedit,
 120                  'showormessage' => $showormessage,
 121              ];
 122              $audienceinstances[] = $params;
 123              $showormessage = true;
 124          }
 125  
 126          return $audienceinstances;
 127      }
 128  }