Merge branch 'MDL-78173' of https://github.com/paulholden/moodle
[moodle.git] / lib / classes / report_helper.php
blobd84e0a0f144837d5e72f2d17d8b9306d7f410dc9
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.
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/>.
17 /**
18 * Report plugins helper class
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
26 namespace core;
27 use moodle_url;
29 /**
30 * A helper class with static methods to help report plugins
32 * @package core
33 * @copyright 2021 Sujith Haridasan
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class report_helper {
37 /**
38 * Print the selector dropdown
40 * @param string $pluginname The report plugin where the header is modified
41 * @return void
43 public static function print_report_selector(string $pluginname):void {
44 global $OUTPUT, $PAGE;
46 if ($reportnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER)) {
48 $menuarray = \core\navigation\views\secondary::create_menu_element([$reportnode]);
49 if (empty($menuarray)) {
50 return;
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]);
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;
67 } else {
68 $result = array_search($coursereports, $menuarray);
69 unset($menuarray[$result]);
71 $check = array_search($pluginname, $menuarray);
72 if ($check !== false) {
73 $activeurl = $check;
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']
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');
93 /**
94 * Save the last selected report in the session
96 * @deprecated since Moodle 4.0
97 * @param int $id The course id
98 * @param moodle_url $url The moodle url
99 * @return void
101 public static function save_selected_report(int $id, moodle_url $url):void {
102 global $USER;
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);
107 // Last selected report.
108 if (!isset($USER->course_last_report)) {
109 $USER->course_last_report = [];
111 $USER->course_last_report[$id] = $url;