-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuenrol.php
328 lines (278 loc) · 10.7 KB
/
kuenrol.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
// This file is a part of Kasetsart Moodle Kit - https://github.com/Pongkemon01/moodle-local_kuenrol
//
// 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.
*
* @package local_kuenrol
* @author Akrapong Patchararungruang
* @copyright 2015 Kasetsart University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once("$CFG->dirroot/local/kuenrol/locallib.php");
require_once('kuenrol_form.php');
/********************* Step 1: Preprocessing **********************/
// Ensure user privillege
// Fetch the course id from query string
$course_id = required_param('id', PARAM_INT);
// No anonymous access for this page, and this will
// handle bogus course id values as well
require_login($course_id);
// $PAGE, $USER, $COURSE, and other globals now set
// Want this for subsequent print_error() calls
$sCourseURL = new moodle_url("{$CFG->wwwroot}/course/view.php", array('id' => $COURSE->id));
$sGroupsURL = new moodle_url("{$CFG->wwwroot}/group/index.php", array('id' => $COURSE->id));
// up, check the capabilities (we need Manual Enrollment module)
require_capability('enrol/manual:enrol', $PAGE->context);
// Iterate the list of active enrol plugins looking for
// the manual course plugin.
// The enrollment retrieved here is the one that can be seen by this course.
// Don't confuse with the enrollment plugins enabled in system level.
$nDefaultRoleID = 0;
$xManualEnrollInstance = null;
$xSelfEnrollInstance = null;
$aEnrolsEnabled = enrol_get_instances($COURSE->id, true);
foreach( $aEnrolsEnabled as $enrol ) {
if( ( $enrol->enrol == 'manual' ) && ( $xManualEnrollInstance == null ) ) {
$xManualEnrollInstance = $enrol;
$nDefaultRoleID = $enrol->roleid;
}
if( ( $enrol->enrol == 'self' ) && ( $xSelfEnrollInstance == null ) ) {
$xSelfEnrollInstance = $enrol;
}
}
// Init for KURegis instance
$xKURegis = null;
// Extract course information from tags
if( isset( $COURSE->idnumber ) ) {
$sCourseTag = $COURSE->idnumber;
} else {
$sCourseTag = '';
}
if( strlen( $sCourseTag ) > 0 ) {
$aTag = explode_tag( $sCourseTag );
} else {
$aTag = array();
}
// Check whether we are at stage 1 or 2
$sCookie = optional_param( 'cookies', '', PARAM_RAW );
$sCourseID = optional_param( 'courseid', '00000000', PARAM_RAW );
$sSem = optional_param( 'sem', '1', PARAM_RAW );
$sYear = optional_param( 'year', '57', PARAM_RAW );
$sCampus = optional_param( 'campus', 'B', PARAM_RAW );
// Set page environment
$sPageHeadTitle = get_string( 'pluginname', local_kuenrol_form1::$pluginname ) . ' : ' . $COURSE->shortname;
$PAGE->set_title($sPageHeadTitle);
$PAGE->set_heading($sPageHeadTitle);
$PAGE->set_pagelayout('incourse');
$PAGE->set_url($CFG->wwwroot . '/local/kuenrol/kuenrol.php?id=' . $COURSE->id);
$PAGE->set_cacheable(false);
/*----------------------------------------------------------------*/
/***************** Step 2: Collect KU Regis data ******************/
if( strlen( $sCookie ) == 0 ) {
$xForm1 = new local_kuenrol_form1( null,
array( 'tag' => $aTag, 'id' => $COURSE->id ) );
// Form processing and displaying is done here
if ( $xForm1->is_cancelled() ) {
//Handle form cancel operation, if cancel button is present on form
redirect($sCourseURL);
} else if ( $xForm1Data = $xForm1->get_data() ) {
// In this case you process validated data. $xForm1->get_data()
// returns data posted in form.
// First, check session spoofing
require_sesskey();
$sCourseID = $xForm1Data->sCourseId;
$sYear = $xForm1Data->sYear;
$sSem = strval( $xForm1Data->nSem );
$sCampus = $xForm1Data->sCampus;
// Cleaning all numeric field because Moodle always trim the leading zero
if( strlen( $sCourseID ) < 8 ) {
do {
$sCourseID = '0'. $sCourseID;
}while( strlen( $sCourseID ) < 8 );
}
if( strlen( $sYear ) < 2 ) {
do {
$sYear= '0'. $sYear;
}while( strlen( $sYear ) < 2 );
}
// Create KU Regis instance based on campus
switch( $sCampus )
{
case 'B':
$xKURegis = new CentralRegis($sCookie);
break;
case 'K':
$xKURegis = new CentralRegis($sCookie);
break;
case 'S':
$xKURegis = new SakonRegis($sCookie);
break;
case 'R':
$xKURegis = new SrirachaRegis($sCookie);
break;
default:
$xKURegis = new CentralRegis($sCookie);
break;
}
// Login to KU Regis
$sCookie = $xKURegis->login( $xForm1Data->sAccount, $xForm1Data->sPassword, $xForm1Data->sCampus );
if( strlen( $sCookie ) == 0 ) {
// Cannot login. Display error and redirect
print_error( 'Fail to login to KU Regis', '', $sCourseURL );
}
// Get section list in the course
$xKURegis->set_active_course( $sCourseID, $sYear, $sSem );
$aRegisSections = $xKURegis->get_sec_list();
if( count( $aRegisSections ) <= 0 ) {
// No section available
print_error( 'This course is not available in the selected semester', '', $sCourseURL );
}
// All are good. Continue to the next step below this if-else
} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
echo $OUTPUT->header();
//displays the form
$xForm1->display();
echo $OUTPUT->footer();
die; // Just display the form and don't go any further
}
} else {
// We just come back so we should retrieve group list again from cookies data
// sent via POST
// Cleaning all numeric field because Moodle always trim the leading zero
if( strlen( $sCourseID ) < 8 ) {
do {
$sCourseID = '0'. $sCourseID;
}while( strlen( $sCourseID ) < 8 );
}
if( strlen( $sYear ) < 2 ) {
do {
$sYear= '0'. $sYear;
}while( strlen( $sYear ) < 2 );
}
// Create KU Regis instance based on campus and previously set cookies
switch( $sCampus )
{
case 'B':
$xKURegis = new CentralRegis($sCookie);
break;
case 'K':
$xKURegis = new CentralRegis($sCookie);
break;
case 'S':
$xKURegis = new SakonRegis($sCookie);
break;
case 'R':
$xKURegis = new SrirachaRegis($sCookie);
break;
default:
$xKURegis = new CentralRegis($sCookie);
break;
}
// Get section list in the course
$xKURegis->set_active_course( $sCourseID, $sYear, $sSem );
$aRegisSections = $xKURegis->get_sec_list();
if( count( $aRegisSections ) <= 0 ) {
// No section available
print_error( 'This course is not available in the selected semester', '', $sCourseURL );
}
}
/*----------------------------------------------------------------*/
/************** Step 3: Prepare data for next step ****************/
// We require manual enrollment actions
$bCanAdd = false;
$bCanDrop = false;
if( enrol_is_enabled( 'manual' ) ) {
$bCanDrop = true;
$bCanAdd = true;
}
// We require ldap authentication
$aEnabledAuths = get_enabled_auth_plugins();
if( in_array( 'ldap', $aEnabledAuths ) ) {
$bCanCreate = true;
} else {
$bCanCreate = false;
}
/*----------------------------------------------------------------*/
/**************** Step 4: Collect desired actions *****************/
$xForm2 = new local_kuenrol_form2( null,
array( 'tag' => $aTag, 'regisgrp' => $aRegisSections,
'canadd' => $bCanAdd, 'candrop' => $bCanDrop,
'cancreate' => $bCanCreate,
'default_roleid' => $nDefaultRoleID,
'courseid' => $sCourseID, 'sem' => $sSem, 'year' => $sYear,
'campus' => $sCampus,
'cookies' => $sCookie, 'id' => $COURSE->id ) );
// Form processing and displaying is done here
if ( $xForm2->is_cancelled() ) {
//Handle form cancel operation, if cancel button is present on form
redirect($sCourseURL);
} else if ( $xForm2Data = $xForm2->get_data() ) {
// In this case you process validated data. $xForm2->get_data()
// returns data posted in form.
// First, check session spoofing
require_sesskey();
if( isset( $xForm2Data->aGroups ) && is_array( $xForm2Data->aGroups ) ) {
$aGroups = $xForm2Data->aGroups;
} else {
$aGroups = array();
}
if( isset( $xForm2Data->sDropAction ) ) {
$sDropAction = trim( $xForm2Data->sDropAction );
} else {
$sDropAction = 'nothing';
}
$nRoleID = intval( $xForm2Data->nRoleID ); // Role-id = 0 means no enrollment
$bAutoGroup = $xForm2Data->bAutoGroup;
$bAutoRevoke = $xForm2Data->bAutoRevoke;
$bIncludeOld = $xForm2Data->bIncludeOld;
// Preparation completed. Continue to process the data
} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
echo $OUTPUT->header();
//displays the form
$xForm2->display();
echo $OUTPUT->footer();
die; // Just display the form and don't go any further
}
/*----------------------------------------------------------------*/
/********************** Step 5: Processing ************************/
// Process based on section
$axStudents = array();
$axStudents = get_students( $xKURegis, $sCourseID, $sYear, $sSem, $aGroups, $sCampus, $axStudents );
if( $bIncludeOld ) {
$sCourseID{0} = '9';
$axStudents = get_students( $xKURegis, $sCourseID, $sYear, $sSem, $aGroups, $sCampus, $axStudents );
}
// We don't need KU Regis now. Log out.
$xKURegis->logout();
// Look up moodle user id for each student. The second parameter means
// to get the boolean value of the comparison
find_students_userid( $axStudents, ( $nRoleID > 0 ) );
// Retrieve unselected groups in the course. These groups will be untouched.
$aUnlistedGroupIDs = get_unlist_groups( $aGroups, $sCampus, $bIncludeOld );
// Perform enrollment level action
if (($xManualEnrollInstance != null || $xSelfEnrollInstance != null) && ($nRoleID > 0 || $sDropAction != 'nothing')) {
enroll_action($axStudents, $aUnlistedGroupIDs, $xManualEnrollInstance, $xSelfEnrollInstance, $nRoleID, $sDropAction);
}
group_action($axStudents, $aUnlistedGroupIDs, $bAutoGroup, $bAutoRevoke);
// Typically you finish up by redirecting to somewhere where the user
// can see what they did.
redirect( $sCourseURL );
/*----------------------------------------------------------------*/