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
26namespace core;
27use moodle_url;
28use 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 */
37class 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            if ($children = $reportnode->children) {
49                // Menu to select report pages to navigate.
50                $activeurl = '';
51                foreach ($children as $key => $node) {
52                    $name = $node->text;
53
54                    $url = $node->action()->out(false);
55                    $menu[$url] = $name;
56                    if ($name === $pluginname) {
57                        $activeurl = $url;
58                    }
59                }
60            }
61
62            if (!empty($menu)) {
63                $select = new url_select($menu, $activeurl, null, 'choosecoursereport');
64                $select->set_label(get_string('report'), ['class' => 'accesshide']);
65                $select->attributes['style'] = "margin-bottom: 1.5rem";
66                $select->class .= " mb-4";
67                echo $OUTPUT->render($select);
68            }
69        }
70    }
71
72    /**
73     * Save the last selected report in the session
74     *
75     * @param int $id The course id
76     * @param moodle_url $url The moodle url
77     * @return void
78     */
79    public static function save_selected_report(int $id, moodle_url $url):void {
80        global $USER;
81
82        // Last selected report.
83        if (!isset($USER->course_last_report)) {
84            $USER->course_last_report = [];
85        }
86        $USER->course_last_report[$id] = $url;
87    }
88}
89