Merge branch 'MDL-65307_master' of git://github.com/dmonllao/moodle
[moodle.git] / admin / tool / analytics / classes / output / helper.php
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/>.
17 /**
18  * Typical crappy helper class with tiny functions.
19  *
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
23  */
25 namespace tool_analytics\output;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Helper class with general purpose tiny functions.
31  *
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
35  */
36 class helper {
38     /**
39      * Converts a class full name to a select option key
40      *
41      * @param string $class
42      * @return string
43      */
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);
49     }
51     /**
52      * option_to_class
53      *
54      * @param string $option
55      * @return string
56      */
57     public static function option_to_class($option) {
58         // Really unlikely but yeah, I'm a bad booyyy.
59         return str_replace('__', '\\', $option);
60     }
62     /**
63      * Sets an analytics > analytics models > $title breadcrumb.
64      *
65      * @param string $title
66      * @param \moodle_url $url
67      * @param \context|null $context Defaults to context_system
68      * @return null
69      */
70     public static function set_navbar(string $title, \moodle_url $url, ?\context $context = null) {
71         global $PAGE;
73         if (!$context) {
74             $context = \context_system::instance();
75         }
77         $PAGE->set_context($context);
78         $PAGE->set_url($url);
80         if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
81             $PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action());
82         }
83         if ($analytics = $PAGE->settingsnav->find('analytics', \navigation_node::TYPE_SETTING)) {
84             $PAGE->navbar->add($analytics->get_content(), $analytics->action());
85         }
86         if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
87             $PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
88         }
89         $PAGE->navbar->add($title);
91         $PAGE->set_pagelayout('report');
92         $PAGE->set_title($title);
93         $PAGE->set_heading($title);
94     }
96     /**
97      * Resets the current page.
98      *
99      * Note that this function can only be used by analytics pages that work at the system context.
100      *
101      * @return null
102      */
103     public static function reset_page() {
104         global $PAGE;
105         $PAGE->reset_theme_and_output();
106         $PAGE->set_context(\context_system::instance());
107     }