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 311 and 402] [Versions 400 and 402] [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  /**
  18   * Report plugins helper class
  19   *
  20   * @package core
  21   * @subpackage report
  22   * @copyright 2021 Sujith Haridasan
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  use moodle_url;
  28  
  29  /**
  30   * A helper class with static methods to help report plugins
  31   *
  32   * @package core
  33   * @copyright 2021 Sujith Haridasan
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class report_helper {
  37      /**
  38       * Print the selector dropdown
  39       *
  40       * @param string $pluginname The report plugin where the header is modified
  41       * @return void
  42       */
  43      public static function print_report_selector(string $pluginname):void {
  44          global $OUTPUT, $PAGE;
  45  
  46          if ($reportnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER)) {
  47  
  48              $menuarray = \core\navigation\views\secondary::create_menu_element([$reportnode]);
  49              if (empty($menuarray)) {
  50                  return;
  51              }
  52  
  53              $coursereports = get_string('reports');
  54              $activeurl = '';
  55              if (isset($menuarray[0])) {
  56                  // Remove the reports entry.
  57                  $result = array_search($coursereports, $menuarray[0][$coursereports]);
  58                  unset($menuarray[0][$coursereports][$result]);
  59  
  60                  // Find the active node.
  61                  foreach ($menuarray[0] as $key => $value) {
  62                      $check = array_search($pluginname, $value);
  63                      if ($check !== false) {
  64                          $activeurl = $check;
  65                      }
  66                  }
  67              } else {
  68                  $result = array_search($coursereports, $menuarray);
  69                  unset($menuarray[$result]);
  70  
  71                  $check = array_search($pluginname, $menuarray);
  72                  if ($check !== false) {
  73                      $activeurl = $check;
  74                  }
  75  
  76              }
  77              $selectmenu = new \core\output\select_menu('reporttype', $menuarray, $activeurl);
  78              $selectmenu->set_label(get_string('reporttype'), ['class' => 'sr-only']);
  79              $options = \html_writer::tag(
  80                  'div',
  81                  $OUTPUT->render_from_template('core/tertiary_navigation_selector', $selectmenu->export_for_template($OUTPUT)),
  82                  ['class' => 'row pb-3']
  83              );
  84              echo \html_writer::tag(
  85                  'div',
  86                  $options,
  87                  ['class' => 'tertiary-navigation full-width-bottom-border ml-0', 'id' => 'tertiary-navigation']);
  88          } else {
  89              echo $OUTPUT->heading($pluginname, 2, 'mb-3');
  90          }
  91      }
  92  
  93      /**
  94       * Save the last selected report in the session
  95       *
  96       * @deprecated since Moodle 4.0
  97       * @param int $id The course id
  98       * @param moodle_url $url The moodle url
  99       * @return void
 100       */
 101      public static function save_selected_report(int $id, moodle_url $url):void {
 102          global $USER;
 103  
 104          debugging('save_selected_report() has been deprecated because it is no longer used and will be '.
 105              'removed in future versions of Moodle', DEBUG_DEVELOPER);
 106  
 107          // Last selected report.
 108          if (!isset($USER->course_last_report)) {
 109              $USER->course_last_report = [];
 110          }
 111          $USER->course_last_report[$id] = $url;
 112      }
 113  }