Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 
11 efrain 17
namespace core;
1 efrain 18
 
19
use context_course;
20
use moodle_url;
21
use stdClass;
22
 
23
/**
24
 * A helper class with static methods to help report plugins
25
 *
26
 * @package core
11 efrain 27
 * @subpackage report
1 efrain 28
 * @copyright 2021 Sujith Haridasan
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class report_helper {
11 efrain 32
 
1 efrain 33
    /**
34
     * Print the selector dropdown
35
     *
36
     * @param string $pluginname The report plugin where the header is modified
11 efrain 37
     * @param string $additional Additional content to display aligned with the selector
1 efrain 38
     */
11 efrain 39
    public static function print_report_selector(string $pluginname, string $additional = ''): void {
1 efrain 40
        global $OUTPUT, $PAGE;
41
 
42
        if ($reportnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER)) {
43
 
44
            $menuarray = \core\navigation\views\secondary::create_menu_element([$reportnode]);
45
            if (empty($menuarray)) {
46
                return;
47
            }
48
 
49
            $coursereports = get_string('reports');
50
            $activeurl = '';
51
            if (isset($menuarray[0])) {
52
                // Remove the reports entry.
53
                $result = array_search($coursereports, $menuarray[0][$coursereports]);
54
                unset($menuarray[0][$coursereports][$result]);
55
 
56
                // Find the active node.
57
                foreach ($menuarray[0] as $key => $value) {
58
                    $check = array_search($pluginname, $value);
59
                    if ($check !== false) {
60
                        $activeurl = $check;
61
                    }
62
                }
63
            } else {
64
                $result = array_search($coursereports, $menuarray);
65
                unset($menuarray[$result]);
66
 
67
                $check = array_search($pluginname, $menuarray);
68
                if ($check !== false) {
69
                    $activeurl = $check;
70
                }
71
 
72
            }
73
            $selectmenu = new \core\output\select_menu('reporttype', $menuarray, $activeurl);
74
            $selectmenu->set_label(get_string('reporttype'), ['class' => 'sr-only']);
75
            $options = \html_writer::tag(
76
                'div',
77
                $OUTPUT->render_from_template('core/tertiary_navigation_selector', $selectmenu->export_for_template($OUTPUT)),
11 efrain 78
                ['class' => 'navitem']
1 efrain 79
            );
11 efrain 80
 
81
            if ($additional) {
82
                $options .= \html_writer::div('', 'navitem-divider') .
83
                    \html_writer::div($additional, 'navitem');
84
            }
85
 
1 efrain 86
            echo \html_writer::tag(
87
                'div',
88
                $options,
11 efrain 89
                ['class' => 'tertiary-navigation full-width-bottom-border ml-0 d-flex', 'id' => 'tertiary-navigation']);
1 efrain 90
        } else {
91
            echo $OUTPUT->heading($pluginname, 2, 'mb-3');
92
        }
93
    }
94
 
95
    /**
96
     * Save the last selected report in the session
97
     *
98
     * @deprecated since Moodle 4.0
99
     * @param int $id The course id
100
     * @param moodle_url $url The moodle url
101
     * @return void
102
     */
103
    public static function save_selected_report(int $id, moodle_url $url): void {
104
        global $USER;
105
 
106
        debugging('save_selected_report() has been deprecated because it is no longer used and will be '.
107
            'removed in future versions of Moodle', DEBUG_DEVELOPER);
108
 
109
        // Last selected report.
110
        if (!isset($USER->course_last_report)) {
111
            $USER->course_last_report = [];
112
        }
113
        $USER->course_last_report[$id] = $url;
114
    }
115
 
116
    /**
117
     * Retrieve the right SQL / params for the group filter depending on the filterparams, course and group settings.
118
     *
119
     * Addionnaly, it will return the list of users visible by the current user so
120
     * it can be used to filter out records that are not visible. This is mainly
121
     * because we cannot use joins as the log tables can be in two different databases.
122
     *
123
     * @param stdClass $filterparams
124
     * @return array
125
     */
126
    public static function get_group_filter(stdClass $filterparams): array {
127
        global $DB, $USER;
128
        $useridfilter = null;
129
        // First and just in case we are in separate group, just set the $useridfilter to the list
130
        // of users visible by this user.
131
        $courseid = $filterparams->courseid ?? SITEID;
132
        $courseid = $courseid ?: SITEID; // Make sure that if courseid is set to 0 we use SITEID.
133
        $course = get_course($courseid);
134
        $groupmode = groups_get_course_groupmode($course);
135
        $groupid = $filterparams->groupid ?? 0;
136
        if ($groupmode == SEPARATEGROUPS || $groupid) {
137
            $context = context_course::instance($courseid);
138
            if ($groupid) {
139
                $cgroups = [(int) $groupid];
140
            } else {
141
                $cgroups = groups_get_all_groups(
142
                    $courseid,
143
                    has_capability('moodle/site:accessallgroups', $context) ? 0 : $USER->id
144
                );
145
                $cgroups = array_keys($cgroups);
146
                // If that's the case, limit the users to be in the groups only, defined by the filter.
147
                if (has_capability('moodle/site:accessallgroups', $context) || empty($cgroups)) {
148
                    $cgroups[] = USERSWITHOUTGROUP;
149
                }
150
            }
151
            // If that's the case, limit the users to be in the groups only, defined by the filter.
152
            [$groupmembersql, $groupmemberparams] = groups_get_members_ids_sql($cgroups, $context);
153
            $groupusers = $DB->get_fieldset_sql($groupmembersql, $groupmemberparams);
154
            $useridfilter = array_fill_keys($groupusers, true);
155
        }
156
        $joins = [];
157
        $params = [];
158
        if (empty($filterparams->userid)) {
159
            if ($groupid) {
160
                if ($thisgroupusers = groups_get_members($groupid)) {
161
                    [$sql, $sqlfilterparams] = $DB->get_in_or_equal(
162
                        array_keys($thisgroupusers),
163
                        SQL_PARAMS_NAMED,
164
                    );
165
                    $joins[] = "userid {$sql}";
166
                    $params = $sqlfilterparams;
167
                } else {
168
                    $joins[] = 'userid = 0'; // No users in groups, so we want something that will always be false.
169
                }
170
            }
171
        } else {
172
            $joins[] = "userid = :userid";
173
            $params['userid'] = $filterparams->userid;
174
        }
175
 
176
        return [
177
            'joins' => $joins,
178
            'params' => $params,
179
            'useridfilter' => $useridfilter,
180
        ];
181
    }
182
}