-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlocallib.php
216 lines (189 loc) · 6.78 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local library functions for the roster report.
*
* @package report_roster
* @copyright 2013 Lafayette College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('ROSTER_MODE_DISPLAY', 'display');
define('ROSTER_MODE_PRINT', 'print');
/**
* Retrieves the groups for the course and formats them for use in a drop-down
* selector.
*
* @param int $id The course id
* @return array The course groups indexed by group id
*/
function report_roster_get_options_group($id) {
$groupsfromdb = groups_get_all_groups($id);
$groups = array(0 => get_string('allusers', 'report_roster'));
foreach ($groupsfromdb as $key => $value) {
$groups[$key] = $value->name;
}
return $groups;
}
/**
* Retrieves the roles for the course and formats them for use in a drop-down
* selector.
*
* @param int $id The course id
* @return array The course roles indexed by role id
*/
function report_roster_get_options_role($id) {
global $USER;
$context = context_course::instance($id);
$rolesfromdb = get_roles_used_in_context($context);
$viewableroles = get_viewable_roles($context, $USER->id);
$roles = array(0 => get_string('allusers', 'report_roster'));
foreach ($rolesfromdb as $role) {
$rolename = $viewableroles[$role->id];
if ($rolename) {
$roles[$role->id] = $rolename;
}
}
return $roles;
}
/**
* Retrieves the size options and formats them for use in a drop-down
* selector.
*
* @return array The user image size options
*/
function report_roster_get_options_size() {
$sizes = array();
foreach (array('small', 'medium', 'large') as $size) {
$pixels = (int) get_config('report_roster', "size_$size");
$label = get_string("size:$size", 'report_roster');
if ($pixels > 0) {
$sizes[$pixels] = $label;
}
}
return $sizes;
}
/**
* Creates the action buttons (learning mode and groups) used on the report page.
*
* @param int $id The course id
* @param moodle_url $url The current page URL
* @param array $params Current parameters values as an associative array (group, role, size, mode)
* @return string The generated HTML
*/
function report_roster_output_action_buttons($id, $url, $params) {
global $OUTPUT;
$options = array();
$options['mode'] = array(
ROSTER_MODE_DISPLAY => get_string('webmode', 'report_roster'),
ROSTER_MODE_PRINT => get_string('printmode', 'report_roster'));
$options['group'] = report_roster_get_options_group($id);
$options['role'] = report_roster_get_options_role($id);
$options['size'] = report_roster_get_options_size($id);
// If there's only one size, don't bother displaying the select.
if (count($options['size']) <= 1) {
$options['size'] = array();
}
$selects = array();
foreach ($params as $key => $val) {
if (array_key_exists($key, $options) && !empty($options[$key])) {
$myurl = clone $url;
$myparams = $params;
unset($myparams[$key]);
$myurl->params($myparams);
$myselect = new single_select($myurl, $key, $options[$key], $val, null);
$myselect->label = get_string_manager()->string_exists("param:$key", 'report_roster')
? get_string("param:$key", 'report_roster')
: get_string($key);
$selects[$key] = $myselect;
}
}
$html = html_writer::start_tag('div');
foreach ($selects as $select) {
$html .= $OUTPUT->render($select);
}
$html .= html_writer::end_tag('div');
return $html;
}
/**
* Returns the value of the given field for the given user. Returns false
* if the field does not exist on the user object.
*
* @param string $field the user field name
* @param stdClass $user the user object
*
* @return string the value of the field
*/
function report_roster_process_field($field, $user) {
$field = trim($field);
if ($field == 'fullname') {
return fullname($user);
} else if (strpos($field, 'currenttime') === 0) {
$format = trim(str_replace('currenttime', '', $field));
return userdate(time(), $format, $user->timezone);
} else if (property_exists($user, $field) && !empty($user->{$field}) && is_string($user->{$field})) {
return $user->{$field};
}
return false;
}
/**
* Resolves which size display when no query param has been passed.
*
* @return string The generated HTML
*/
function report_roster_resolve_auto_size() {
$defaultselector = get_config('report_roster', 'size_default');
$defaultsize = get_config('report_roster', 'size_' . $defaultselector);
if ($defaultsize != 0) {
// If the default size config is valid, return that.
return (int) $defaultsize;
} else {
// Otherwise, check the other size options and return the first non-zero one.
foreach (array('small', 'medium', 'large') as $selector) {
$size = get_config('report_roster', 'size_' . $selector);
if ($size != 0) {
return $size;
}
}
// And finally, if none of that worked, hard default to 100.
return 100;
}
}
/**
* Build the fields to retrieve from the user profile.
*
* @return string SQL-query-like string of fields to fetch, for use in get_enrolled_users / get_role_users
*/
function report_roster_profile_fields_query() {
global $DB, $USER;
// Set extra fields to retrieve.
$extrafields = ['username', 'timezone'];
$fieldsconfig = explode("\n", get_config('report_roster', 'fields'));
foreach ($fieldsconfig as $field) {
$field = trim($field);
// Explicitly exclude fullname from the database query.
if ($field === 'fullname') {
continue;
}
if ( property_exists($USER, $field)) {
$extrafields[] = $field;
}
}
$fields = \core_user\fields::for_userpic();
$fields->including(...$extrafields);
$selects = $fields->get_sql('u', false, '', 'id', false)->selects;
$selects = str_replace(', ', ',', $selects);
return $selects;
}