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