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