-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlib.php
More file actions
221 lines (191 loc) · 7.5 KB
/
Copy pathlib.php
File metadata and controls
221 lines (191 loc) · 7.5 KB
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
217
218
219
220
221
<?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/>.
/**
* @package local_loginas
* @copyright 2015 Itamar Tzadok {@link http://substantialmethods.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') or die;
/**
* Adds module specific settings to the settings block.
*
* @param settings_navigation $settings The settings navigation object
* @param stdClass $context The node context
*/
function local_loginas_extend_settings_navigation(settings_navigation $settings, $context) {
global $DB, $CFG, $PAGE, $USER;
// Course id and context.
$courseid = !empty($PAGE->course->id) ? $PAGE->course->id : SITEID;
$coursecontext = context_course::instance($courseid);
// Must have the loginas capability.
if (!has_capability('moodle/user:loginas', $coursecontext)) {
return;
}
// Set the settings category.
$loginas = $settings->add(get_string('loginas'));
// Login as list by admin setting.
if (is_siteadmin($USER)) {
// Admin settings page.
$url = new moodle_url('/admin/settings.php', array('section' => 'localsettingloginas'));
$loginas->add(get_string('settings'), $url, $settings::TYPE_SETTING);
// Users list.
$loginasusers = array();
// Since 2.6, use all the required fields.
$ufields = 'id, ' . get_all_user_name_fields(true);
// Get users by id.
if ($configuserids = get_config('local_loginas', 'loginasusers')) {
$userids = explode(',', $configuserids);
if ($users = $DB->get_records_list('user', 'id', $userids, '', $ufields)) {
$loginasusers = $users;
}
}
// Get users by username.
if ($configusernames = get_config('local_loginas', 'loginasusernames')) {
$usernames = explode(',', $configusernames);
if ($users = $DB->get_records_list('user', 'username', $usernames, '', $ufields)) {
$loginasusers = $loginasusers + $users;
}
}
// Add action links for specified users.
if ($loginasusers) {
$params = array('id' => $courseid, 'sesskey' => sesskey());
foreach ($loginasusers as $userid => $lauser) {
$url = new moodle_url('/course/loginas.php', $params);
$url->param('user', $userid);
$loginas->add(fullname($lauser, true), $url, $settings::TYPE_SETTING);
}
}
}
// Course users login as.
if (!$configcourseusers = get_config('local_loginas', 'courseusers')) {
return;
}
$loggedinas = \core\session\manager::is_loggedinas();
if (!$loggedinas) {
// Ajax link.
$node = $loginas->add(get_string('courseusers', 'local_loginas'), 'javascript:void();', $settings::TYPE_SETTING);
$node->add_class('local_loginas_setting_link');
local_loginas_require_js($PAGE);
}
}
/**
* Sets required javascript.
*/
function local_loginas_require_js($page) {
$modules = array('moodle-local_loginas-quickloginas', 'moodle-local_loginas-quickloginas-skin');
$arguments = array(
'courseid' => $page->course->id,
'ajaxurl' => '/local/loginas/ajax.php',
'url' => $page->url->out(false),
);
$function = 'M.local_loginas.quickloginas.init';
$page->requires->yui_module($modules, $function, array($arguments));
$page->requires->strings_for_js(array(
'ajaxoneuserfound',
'ajaxxusersfound',
'ajaxnext25',
'ajaxprev25',
'loginasuser',
'errajaxsearch'), 'local_loginas');
$page->requires->strings_for_js(array('none', 'search'), 'moodle');
}
/**
* Searches non-admin non-guest users in the context and returns paginated results.
*
* @global moodle_database $DB
* @param string $search
* @param bool $searchanywhere
* @param int $page Starting at 0
* @param int $perpage
* @return array
*/
function local_loginas_get_users($contextid, $search='', $searchanywhere=false, $page=0, $perpage=25) {
global $DB, $CFG, $USER, $COURSE;
// Add some additional sensible conditions.
$tests = array(
"u.id <> :guestid",
"u.id <> :cuserid",
'u.deleted = 0',
'u.confirmed = 1'
);
$params = array(
'guestid' => $CFG->siteguest,
'cuserid' => $USER->id,
);
// Omit admin condition.
$admins = explode(',', $CFG->siteadmins);
list($notinids, $aparams) = $DB->get_in_or_equal($admins, SQL_PARAMS_NAMED, 'admin', false);
$tests[] = "u.id $notinids";
$params = array_merge($params, $aparams);
// Search condition.
if (!empty($search)) {
$conditions = array('u.firstname', 'u.lastname');
if ($searchanywhere) {
$searchparam = '%' . $search . '%';
} else {
$searchparam = $search . '%';
}
$i = 0;
foreach ($conditions as $key => $condition) {
$conditions[$key] = $DB->sql_like($condition, ":con{$i}00", false);
$params["con{$i}00"] = $searchparam;
$i++;
}
$tests[] = '(' . implode(' OR ', $conditions) . ')';
}
// Groups condition.
$joingroupmembers = '';
$context = context_course::instance($COURSE->id);
if (groups_get_course_groupmode($COURSE) != NOGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
// User course groups.
$groups = groups_get_user_groups($COURSE->id);
$groupids = reset($groups);
// No groups, no users to login as.
if (empty($groupids)) {
return array('totalusers' => 0, 'users' => array());
}
// Some groups, add condition.
$joingroupmembers = " JOIN {groups_members} gm ON gm.userid = u.id ";
list($ingroupids, $gparams) = $DB->get_in_or_equal($groupids, SQL_PARAMS_NAMED, 'group');
$tests[] = "gm.groupid $ingroupids";
$params = array_merge($params, $gparams);
}
// Require enrollment if not on front page.
$joinroleassignments = '';
if ($COURSE->id != SITEID) {
$joinroleassignments = 'JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid = :contextid)';
}
// Get the users.
$wherecondition = implode(' AND ', $tests);
$fields = 'SELECT DISTINCT '. user_picture::fields('u', array('username', 'lastaccess'));
$countfields = 'SELECT COUNT(u.id)';
$sql = "
FROM
{user} u
$joinroleassignments
$joingroupmembers
WHERE
$wherecondition
";
$order = ' ORDER BY lastname ASC, firstname ASC';
$params['contextid'] = $contextid;
if ($totalusers = $DB->count_records_sql($countfields . $sql, $params)) {
$availableusers = $DB->get_records_sql($fields. $sql. $order, $params, $page * $perpage, $perpage);
} else {
$availableusers = null;
}
return array('totalusers' => $totalusers, 'users' => $availableusers);
}