2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Typical crappy helper class with tiny functions.
20 * @package tool_analytics
21 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_analytics\output;
27 defined('MOODLE_INTERNAL') || die();
30 * Helper class with general purpose tiny functions.
32 * @package tool_analytics
33 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * Converts a class full name to a select option key
41 * @param string $class
44 public static function class_to_option($class) {
45 // Form field is PARAM_ALPHANUMEXT and we are sending fully qualified class names
46 // as option names, but replacing the backslash for a string that is really unlikely
47 // to ever be part of a class name.
48 return str_replace('\\', '__', $class);
54 * @param string $option
57 public static function option_to_class($option) {
58 // Really unlikely but yeah, I'm a bad booyyy.
59 return str_replace('__', '\\', $option);
63 * Sets an analytics > analytics models > $title breadcrumb.
65 * @param string $title
66 * @param \moodle_url $url
67 * @param \context|null $context Defaults to context_system
70 public static function set_navbar(string $title, \moodle_url $url, ?\context $context = null) {
74 $context = \context_system::instance();
77 $PAGE->set_context($context);
80 if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
81 $PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action());
83 if ($analytics = $PAGE->settingsnav->find('analytics', \navigation_node::TYPE_SETTING)) {
84 $PAGE->navbar->add($analytics->get_content(), $analytics->action());
86 if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
87 $PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
89 $PAGE->navbar->add($title);
91 $PAGE->set_pagelayout('report');
92 $PAGE->set_title($title);
93 $PAGE->set_heading($title);
97 * Resets the current page.
99 * Note that this function can only be used by analytics pages that work at the system context.
103 public static function reset_page() {
105 $PAGE->reset_theme_and_output();
106 $PAGE->set_context(\context_system::instance());