Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * 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
 */
24
 
25
namespace tool_analytics\output;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
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 {
37
 
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
    }
50
 
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
    }
61
 
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;
72
 
73
        if (!$context) {
74
            $context = \context_system::instance();
75
        }
76
 
77
        $PAGE->set_context($context);
78
        $PAGE->set_url($url);
79
        $PAGE->set_secondary_active_tab('siteadminnode');
80
        $PAGE->set_primary_active_tab('siteadminnode');
81
 
82
        if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
83
            $PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action(),
84
                \breadcrumb_navigation_node::TYPE_SITE_ADMIN, null, 'root');
85
        }
86
 
87
        if ($analytics = $PAGE->settingsnav->find('analytics', \navigation_node::TYPE_SETTING)) {
88
            $PAGE->navbar->add($analytics->get_content(), $analytics->action());
89
        }
90
        if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
91
            $PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
92
        }
93
        $PAGE->navbar->add($title);
94
 
95
        $PAGE->set_pagelayout('report');
96
        $PAGE->set_title($title);
97
        $PAGE->set_heading($title);
98
    }
99
 
100
    /**
101
     * Resets the current page.
102
     *
103
     * Note that this function can only be used by analytics pages that work at the system context.
104
     *
105
     * @return null
106
     */
107
    public static function reset_page() {
108
        global $PAGE;
109
        $PAGE->reset_theme_and_output();
110
        $PAGE->set_context(\context_system::instance());
111
    }
112
    /**
113
     * Convert a list of contexts to an associative array where the value is the context name.
114
     *
115
     * @param  array            $contexts
116
     * @param  \moodle_url      $url
117
     * @param  \renderer_base   $output
118
     * @param  int|null         $selected
119
     * @param  bool             $includeall
120
     * @param  bool             $shortentext
121
     * @return \stdClass
122
     */
123
    public static function prediction_context_selector(array $contexts, \moodle_url $url, \renderer_base $output,
124
            ?int $selected = null, ?bool $includeall = false, ?bool $shortentext = true): \stdClass {
125
 
126
        foreach ($contexts as $contextid => $unused) {
127
            // We prepare this to be used as single_select template options.
128
            $context = \context::instance_by_id($contextid);
129
 
130
            // Special name for system level predictions as showing "System is not visually nice".
131
            if ($contextid == SYSCONTEXTID) {
132
                $contextname = get_string('allpredictions', 'tool_analytics');
133
            } else {
134
                if ($shortentext) {
135
                    $contextname = shorten_text($context->get_context_name(false, true), 40);
136
                } else {
137
                    $contextname = $context->get_context_name(false, true);
138
                }
139
            }
140
            $contexts[$contextid] = $contextname;
141
        }
142
 
143
        if ($includeall) {
144
            $contexts[0] = get_string('all');
145
            $nothing = '';
146
        } else {
147
            $nothing = array('' => 'choosedots');
148
        }
149
 
150
        \core_collator::asort($contexts);
151
 
152
        if (!$selected) {
153
            $selected = '';
154
        }
155
        $singleselect = new \single_select($url, 'contextid', $contexts, $selected, $nothing);
156
        return $singleselect->export_for_template($output);
157
    }
158
}