-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
262 lines (231 loc) · 9.01 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
// This file is a part of Kasetsart Moodle Kit - https://github.com/Pongkemon01/moodle-local_enrolrefresh
//
// Kasetsart Moodle Kit 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.
//
// Kasetsart Moodle Kit 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/>.
/**
* Enrollment refresh is use to synchronize student enrollment with registration office.
* As the registration office use internal custom database scheme, we can refresh
* student enrollment statuses through CSV file only.
*
* @package quizaccess_studentident
* @author Akrapong Patchararungruang
* @copyright 2014 Kasetsart University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/csvlib.class.php');
require_once("$CFG->libdir/accesslib.php");
require_once("$CFG->libdir/enrollib.php");
require_once("$CFG->libdir/grouplib.php");
require_once("$CFG->dirroot/group/lib.php");
/**
* Validation callback function - verified the column line of csv file.
* Converts standard column names to lowercase.
* @param csv_import_reader $cir
* @param moodle_url $returnurl return url in case of any error
* @return array list of field names
*/
function validate_user_upload_columns(csv_import_reader $cir, moodle_url $returnurl) {
$columns = $cir->get_columns(); // Get the first line (field names)
if (empty($columns)) {
$cir->close();
$cir->cleanup();
print_error('cannotreadtmpfile', 'error', $returnurl);
}
if (count($columns) != 2) {
$cir->close();
$cir->cleanup();
print_error('csvwrongcolumns', 'error', $returnurl);
}
// test columns
$processed = array();
$allowfield = array('idnumber','username','group');
foreach ($columns as $key=>$field) {
$lcfield = core_text::strtolower($field);
if (!in_array($lcfield, $allowfield)) {
$cir->close();
$cir->cleanup();
print_error('invalidfieldname', 'error', $returnurl, $field);
}
if (in_array($lcfield, $processed)) {
$cir->close();
$cir->cleanup();
print_error('duplicatefieldname', 'error', $returnurl, $newfield);
}
$processed[$key] = $lcfield;
}
if (! in_array('group', $processed)) {
$cir->close();
$cir->cleanup();
print_error('csvnogroup', 'error', $returnurl);
}
return $processed;
}
/**
* Parse csv file.
* @param csv_import_reader $cir
* @param array $csv_keys the first line of input file (header line)
* @return associative array of indexed array of enrolled groups. The array is indexed by id field in user table.
*/
function parse_csv(csv_import_reader $cir, $csv_keys) {
global $DB;
$data = array();
// init csv import helper
$cir->init();
// Get name of the key field
if (in_array ('username', $csv_keys)) {
$keyname = 'username';
} else {
$keyname = 'idnumber';
}
// Iterate from line 2 (Skip the header line)
while ($line = $cir->next()) {
// add fields to user object
foreach ($line as $keynum => $value) {
if (!isset($csv_keys[$keynum])) {
// this should not happen
continue;
}
if($csv_keys[$keynum] == 'group') {
$gid = $value;
} else {
// csv_keys can have only 'group' or 'username' or 'idnumber'
$key = $value;
}
}
// Get user id
$user = $DB->get_record('user', array($keyname=>$key));
if (empty($user))
{
// User have not sign-up yet.
continue;
}
$uid = $user->id;
// Store id
if (array_key_exists($uid, $data)) {
// This $uid already exists, append
$data[$uid]->group[] = $gid;
} else {
// First for this $uid, create new
$data[$uid] = new stdClass();
$data[$uid]->id = $uid;
$data[$uid]->key = $key;
$data[$uid]->groups = array();
$data[$uid]->groups[] = $gid;
}
}
return (count($data)>0) ? $data : false;
}
/**
* Perform enrollment action.
* @param array $csvdata
* @param class $manual_enrol_instance - The instance of 'manual' enrollment plugin in class-context.
* @param integer $role_id
* @param string $missing_act - action for users missing from the imported data.
* @return none.
*/
function enroll_action ($csvdata, $manual_enrol_instance, $role_id, $missing_act) {
global $DB, $COURSE;
$coursecontext = context_course::instance($COURSE->id);
$system_manual_enroll = enrol_get_plugin('manual');
// Enroll new users
if ($role_id > 0) {
foreach ($csvdata as $uid=>$unused) {
if (!is_enrolled($coursecontext, $uid)) {
$system_manual_enroll->enrol_user($manual_enrol_instance, $uid, $role_id);
}
}
}
// Withdraw/Suspend unlisted users
if ($missing_act != 'nothing') {
// Get 'student' roleid
$student_roleid = $DB->get_record('role', array('shortname'=>'student'))->id;
$enrolled_users = get_enrolled_users($coursecontext);
foreach ($enrolled_users as $enrolled_user) {
if (!array_key_exists($enrolled_user->id, $csvdata)) {
if ($DB->count_records('role_assignments', array('userid'=>$enrolled_user->id,
'roleid'=>$student_roleid, 'contextid'=>$coursecontext->id)) == 0) {
continue; // Skip non-student users
}
if ($missing_act == 'suspend') {
$system_manual_enroll->update_user_enrol($manual_enrol_instance, $enrolled_user->id, 1);
} else {
$system_manual_enroll->unenrol_user($manual_enrol_instance, $enrolled_user->id);
}
}
}
}
}
/**
* Perform enrollment action.
* @param array $csvdata
* @param integer $autogroupcreate - Whether new group should be created if required?
* @param integer $autogroupwithdraw - Whether users should be withdrawn from the group not specified in imported data?
* @return none.
*/
function group_action($csvdata, $autogroupcreate, $autogroupwithdraw) {
global $DB, $COURSE;
$coursecontext = context_course::instance($COURSE->id);
// Get 'student' roleid
$student_roleid = $DB->get_record('role', array('shortname'=>'student'))->id;
foreach ($csvdata as $uid=>$userdata) {
if (!is_enrolled($coursecontext, $uid)) {
// skip unenrolled users as enrol_action should finish all enrollment
continue;
}
// Add to group
foreach ($userdata->groups as $group) {
// Prepare course group
if (! ($DB->record_exists('groups', array('name'=>$group)))) {
// If group not exists, should we create it?
if ($autogroupcreate) {
// Create a group
$newgroupdata = new stdClass();
$newgroupdata->name = $group;
$newgroupdata->courseid = $COURSE->id;
$newgroupdata->description = '';
$gid = groups_create_group($newgroupdata);
if (!$gid) {
// Fail to create group
continue; // Skip
}
} else {
continue; // Skip this group
}
} else {
$gid = groups_get_group_by_name($COURSE->id, $group);
}
// Add user to group
if (! groups_is_member($gid, $uid)) {
groups_add_member($gid, $uid);
}
}
// Withdraw from unlisted groups if required
if ($autogroupwithdraw && ($DB->count_records('role_assignments', array('userid'=>$uid,
'roleid'=>$student_roleid)) > 0)) {
// Get current groups that the user already in
$sql = "SELECT g.id, g.name
FROM {groups} g JOIN {groups_members} gm ON gm.groupid = g.id
WHERE gm.userid = ? AND g.courseid = ?";
$params = array($uid, $COURSE->id);
$currentgroups = $DB->get_records_sql($sql, $params);
// Iterate
foreach ($currentgroups as $gid=>$currentgroup) {
if (! in_array($currentgroup->name, $userdata->groups)) {
groups_remove_member($gid, $uid);
}
}
}
}
}