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 401 and 402] [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\external;
  20  
  21  use core_collator;
  22  use core_component;
  23  use core_plugin_manager;
  24  use renderer_base;
  25  use core_reportbuilder\local\audiences\base;
  26  
  27  /**
  28   * Custom report audience cards exporter class
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2022 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class custom_report_audience_cards_exporter extends custom_report_menu_cards_exporter {
  35  
  36      /**
  37       * Get the additional values to inject while exporting
  38       *
  39       * @param renderer_base $output
  40       * @return array
  41       */
  42      protected function get_other_values(renderer_base $output): array {
  43          $menucards = [];
  44  
  45          // Iterate over all audience types.
  46          $audiences = core_component::get_component_classes_in_namespace(null, 'reportbuilder\\audience');
  47          $audiencekeyindex = 0;
  48          foreach ($audiences as $class => $path) {
  49              if (is_subclass_of($class, base::class)) {
  50                  $audience = $class::instance();
  51                  if (!$audience->user_can_add()) {
  52                      continue;
  53                  }
  54  
  55                  // The name of each card will be the component the audience belongs to.
  56                  [$component] = explode('\\', $class);
  57                  if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) {
  58                      $componentname = $plugininfo->displayname;
  59                  } else {
  60                      $componentname = get_string('site');
  61                  }
  62  
  63                  // New menu card per component.
  64                  if (!array_key_exists($componentname, $menucards)) {
  65                      $menucards[$componentname] = [
  66                          'name' => $componentname,
  67                          'key' => 'index' . ++$audiencekeyindex,
  68                          'items' => [],
  69                      ];
  70                  }
  71  
  72                  // Append menu card item per audience.
  73                  $menucards[$componentname]['items'][] = [
  74                      'name' => $audience->get_name(),
  75                      'identifier' => get_class($audience),
  76                      'title' => get_string('addaudience', 'core_reportbuilder', $audience->get_name()),
  77                      'action' => 'add-audience',
  78                      'disabled' => !$audience->is_available(),
  79                  ];
  80              }
  81          }
  82  
  83          // Order items in each menu card alphabetically.
  84          array_walk($menucards, static function(array &$menucard): void {
  85              core_collator::asort_array_of_arrays_by_key($menucard['items'], 'name');
  86              $menucard['items'] = array_values($menucard['items']);
  87          });
  88  
  89          return [
  90              'menucards' => array_values($menucards),
  91          ];
  92      }
  93  }