-
Notifications
You must be signed in to change notification settings - Fork 1
/
locallib.php
executable file
·543 lines (438 loc) · 15.7 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
// 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/>.
/**
* auth_nsdc
*
*
* @package auth
* @subpackage nsdc
* @copyright 2020 Saylor Academy
* @author John Azinheira
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_nsdc;
use core_completion\progress;
use stdClass;
use DateTime;
use DateTimeZone;
use core;
use moodle_database;
use moodle_exception;
defined('MOODLE_INTERNAL') || die;
/**
* Decrypt and verify the incoming payload.
*
* @param type $string payload
* @return obj decrypted payload
*/
function decrypt_payload($encryptedpayload) {
// Get config and decrypt.
$pluginconfig = get_config('auth_nsdc');
if (empty($pluginconfig->key)) {
throw new \moodle_exception('nsdc_no_key', 'auth_nsdc');
}
if (empty($pluginconfig->iv)) {
throw new \moodle_exception('nsdc_no_iv', 'auth_nsdc');
}
$key = base64_decode($pluginconfig->key);
$iv = base64_decode($pluginconfig->iv);
$decryptedpayload = openssl_decrypt($encryptedpayload, 'AES-256-CBC', $key, 0, $iv);
// Convert to object.
$data = json_decode($decryptedpayload);
// Verify. Check if the timestamp is less than 30 seconds old.
if ((time() - strtotime($data->time_stamp)) > 30 ) {
// Time stamp verification has failed. Possible replay attack.
throw new moodle_exception('timeverificationfailed', 'auth_nsdc');
}
return $data;
}
/**
* Check if the user already has an account.
*
* @param type $int candidate_id
* @return bool has_account
*/
function has_account($candidate_id) {
global $DB;
$linkedlogin = $DB->get_record('auth_nsdc_linked_login', array('nsdcid' => $candidate_id), '*', IGNORE_MISSING);
if ($linkedlogin == false) {
// User does not have an account through this plugin.
$has_account = false;
}
else {
$has_account = true;
}
return $has_account;
}
/**
* Create a fake email for user's who do not have an email address
* using the baseemail and their candidate_id. Format is:
*
* @param type $int candidate_id
* @return string newemail
*/
function generate_email($candidate_id) {
$pluginconfig = get_config('auth_nsdc');
$baseemail = $pluginconfig->baseemail;
$basedomain = strstr($baseemail, '@');
$baseuser = strstr($baseemail, '@', true);
return $baseuser . '+' . $candidate_id . $basedomain;
}
/**
* Get user info for the linked login.
*
* @param type $obj data
* @return bool success
*/
function create_account($data) {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
$user = new stdClass();
$user->username = 'nsdc_'.$data->candidate_id;
$user->auth = 'nsdc';
$user->mnethostid = $CFG->mnet_localhost_id;
$user->lastname = $data->last_name ?? '';
$user->firstname = $data->candidate_name ?? '';
$user->url = '';
$user->alternatename = '';
$user->secret = random_string(15);
// Users may or may not have an email address.
// Generate an email address if they do not have one.
$user->email = $data->candidate_email ?? generate_email($data->candidate_id);
$user->password = '';
// This user is confirmed.
$user->confirmed = 1;
// Do a final check to make sure that there is not already a user.
// Check email.
$emailcheck = $DB->get_record('user', array('email' => $user->email), '*', IGNORE_MISSING);
if ($emailcheck !== false) {
// Someone already has this email address.
throw new moodle_exception('could_not_create_account_exists', 'auth_nsdc');
}
$user->id = user_create_user($user, false, true);
// Create a linked login in the linked login table.
$success = link_login($user, $data);
return $success;
}
/**
* Create a linked login record.
* This is to have a record linking the candidate_id to a user.
*
* @param obj $user
* @param obj $data User data from NSDC
* @return bool success
*/
function link_login($user, $data) {
global $DB, $USER;
// Double check that this user doesn't already exist in the table.
$linkedlogin = $DB->get_record('auth_nsdc_linked_login', array('userid' => $user->id), '*', IGNORE_MISSING);
if ($linkedlogin == false) {
// Linked login is not already in db, create linked login record.
$record = new stdClass();
$record->timecreated = $user->timecreated;
$record->timemodified = $user->timemodified;
$record->userid = $user->id;
$record->nsdcid = $data->candidate_id;
$record->username = $user->username;
$record->email = $user->email;
// If the candidate_email is not set, it is a phone only user.
if (!isset($data->candidate_email)) {
$record->phoneonly = true;
}
else {
$record->phoneonly = false;
}
// Insert the record.
$linkedlogin = $DB->insert_record('auth_nsdc_linked_login', $record, false);
}
return $linkedlogin;
}
/**
* Get the user.
*
* @param obj $data User data from NSDC
* @return obj user
*/
function get_user($data) {
global $CFG, $DB;
require_once($CFG->dirroot.'/lib/moodlelib.php');
// Get the user account from the linked login.
$linkedlogin = $DB->get_record('auth_nsdc_linked_login', array('nsdcid' => $data->candidate_id), '*', IGNORE_MISSING);
$user = \get_complete_user_data('id', $linkedlogin->userid);
return $user;
}
/**
* Log in the user.
*
* @param obj $user
* @return bool success
*/
function complete_login($user) {
global $CFG, $DB;
require_once($CFG->dirroot.'/lib/moodlelib.php');
// Log in the user.
\complete_user_login($user);
return true;
}
/**
* Enroll the user in the specified course.
*
* @param obj $user
* @param int $courseid
* @return bool success
*/
function enrol_nsdc_user($user, $courseid) {
global $CFG, $DB;
require_once($CFG->dirroot.'/lib/enrollib.php');
// Grab the student role id from the DB.
$roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
// Enroll the user.
// I'm using manual enrollment for now. If we want to use self
// enrollment in the future (so students can unenroll from courses,
// try enrol_user().
if (!\enrol_try_internal_enrol($courseid, $user->id, $roleid, time())) {
// There's a problem.
throw new moodle_exception('enrolerror', 'auth_nsdc');
}
return true;
}
/**
* Create the request to update course status at NSDC.
*
* @param obj $data The data payload supplied by NSDC.
* @param obj $user The moodle user data object.
* @param bool $production Whether we are sending status updates to the NSDC production environment.
* @return bool Return true on success.
*/
function course_status_update($data, $user, $production = false) {
global $CFG, $DB;
// Get the url to send to.
$pluginconfig = get_config('auth_nsdc');
$domain = 'http://test.nsdccindia.org';
$endpoint = '/API/user_update';
if ($production) {
$domain = 'https://eskillindia.org';
}
// Hardcoding this for now.
$certificationtype = "completion";
$candidatedata = new stdClass();
$candidatedata->candidate_id = $data->candidate_id;
$candidatedata->course_id = $data->course_id;
$candidatedata->course_status = get_nsdc_course_status($user->id, $data->kp_course_id);
$candidatedata->course_completion_pencentage = get_course_progress_percentage($user->id, $data->kp_course_id);
$candidatedata->course_enrollment_date = get_course_enrollment_date($user->id, $data->kp_course_id);
$candidatedata->course_last_date = get_course_lastaccess_date($user->id, $data->kp_course_id);
$candidatedata->certification_status = get_certification_status($user->id, $data->kp_course_id);
if ($candidatedata->certification_status == 1) {
// If the user has completed the course, grab date and grade.
$candidatedata->certification_issue_date = get_certification_date($user->id, $data->kp_course_id);
$candidatedata->certification_type = $certificationtype;
$candidatedata->certification_percentage = get_certification_percentage($user->id, $data->kp_course_id);
// Also, set the course completion to 100%.
$candidatedata->course_completion_pencentage = 100;
}
// Format the data to ready for json encoding.
$params['candidate_data'] = array($candidatedata);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $domain.$endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'x-api-key: '.$pluginconfig->apikey,
'Content-Type: application/json'
));
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($params));
// So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
// Collect info.
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// Execute.
$response = json_decode(curl_exec($ch));
$information = curl_getinfo($ch);
$statuscode = $information['http_code'];
if ($statuscode !== 200) {
// NSDC reports an error.
// As of mid-2020 ALL call respond with 422 for some reason.
// Actual errors appear to response with an error object instead of message.
if (isset($response->error)) {
$error = ' ('.$statuscode.') '.$response->error;
throw new moodle_exception('nsdc_api_returned_error', 'auth_nsdc', '', $error);
}
}
return true;
}
/**
* Get the user's course completion status and return a NSDC status.
*
* @param int $userid
* @param int $courseid
* @return int $status The NSDC course status code.
*/
function get_nsdc_course_status($userid, $courseid) {
global $CFG;
// NSDC status codes:
// -1 = Quit
// 0 = Enrolled
// 1 = In Progress
// 2 = Assessment Complete
// 3 = Certification Complete
// 4 = Course Complete
require_once($CFG->dirroot.'/lib/completionlib.php');
$status = 0; // Basically, they have to be enrolled in the course at this point.
// Create a course object and create a course completion object.
$course = new stdClass();
$course->id = $courseid;
$completioninfo = new \completion_info($course);
if ($completioninfo->is_course_complete($userid)) {
// The user has completed the course.
$status = 4;
}
else {
// Course completion will be false.
// TODO: If we want to be more granular, we can set to
// In Progress only once the user's progress is more than 0%.
$status = 1;
}
return $status;
}
/**
* Get the user's course completion progress.
*
* @param int $userid
* @param int $courseid
* @return int $percentage
*/
function get_course_progress_percentage($userid, $courseid) {
$course = new stdClass();
$course->id = $courseid;
$percentage = intval(progress::get_course_progress_percentage($course, $userid));
if ($percentage === 0) {
$percentage = (int) 1;
}
return $percentage;
}
/**
* Get the user's enrollment time.
*
* @param int $userid
* @param int $courseid
* @return string $enrolldate
*/
function get_course_enrollment_date($userid, $courseid) {
global $DB;
// Get the enrol id for the enrollment instance.
$userenrollments = $DB->get_records('user_enrolments', array('userid' => $userid), '', '*');
// Iterate through the enrollments until the enrollment for the course is found.
foreach ($userenrollments as $userenrollment) {
// Get the course id from the corresponding mdl_enrol enrollment.
$enrollmentcourse = $DB->get_field('enrol', 'courseid', array('id' => $userenrollment->enrolid));
if ($enrollmentcourse == $courseid) {
// This is the enrollment record for the course.
$enrolltimestamp = $userenrollment->timecreated;
break;
}
}
// Convert to DateTime.
$enrolldate = new DateTime();
$enrolldate->setTimestamp($enrolltimestamp);
// Set the timezone to IST (NSDC is in India).
$timezone = new DateTimeZone('Asia/Kolkata');
$enrolldate->setTimezone($timezone);
// Return in DD-MM-YYYY format.
return $enrolldate->format("d-m-Y");
}
/**
* Get the user's last access time.
*
* @param int $userid
* @param int $courseid
* @return string $lastaccess
*/
function get_course_lastaccess_date($userid, $courseid) {
global $DB;
// Get the last access time for the course. This is a UNIX timestamp.
$lastaccesstimestamp = $DB->get_field('user_lastaccess', 'timeaccess', array('userid' => $userid, 'courseid' => $courseid));
// If null, the user hasn't gone into the course yet. Most likely logging in for the first time.
// Spoof the data with today's date.
if ($lastaccesstimestamp == null) {
$lastaccesstimestamp = time();
}
// Convert to DateTime.
$lastaccess = new DateTime();
$lastaccess->setTimestamp($lastaccesstimestamp);
// Set the timezone to IST (NSDC is in India).
$timezone = new DateTimeZone('Asia/Kolkata');
$lastaccess->setTimezone($timezone);
// Return in DD-MM-YYYY format.
return $lastaccess->format("d-m-Y");
}
/**
* Get the user's certification status.
*
* @param int $userid
* @param int $courseid
* @return int $status 1 if certificate issued (course is completed), 0 if not.
*/
function get_certification_status($userid, $courseid) {
global $CFG;
require_once($CFG->dirroot.'/lib/completionlib.php');
// Create a course object and create a course completion object.
$course = new stdClass();
$course->id = $courseid;
$completioninfo = new \completion_info($course);
$status = 0;
if ($completioninfo->is_course_complete($userid)) {
// The user has completed the course.
$status = 1;
}
return $status;
}
/**
* Get the date that a user got a certification - the date they completed a course.
*
* @param int $userid
* @param int $courseid
* @return string $completiondate Date of completion in DD-MM-YYYY format.
*/
function get_certification_date($userid, $courseid) {
global $CFG, $DB;
// Get the user's completion info for the course
$completioninfo = $DB->get_record('course_completions', array('userid' => $userid, 'course' => $courseid), '*', IGNORE_MISSING);
$completiontimestamp = $completioninfo->timecompleted;
// Convert to DateTime.
$completiondate = new DateTime();
$completiondate->setTimestamp($completiontimestamp);
// Set the timezone to IST (NSDC is in India).
$timezone = new DateTimeZone('Asia/Kolkata');
$completiondate->setTimezone($timezone);
// Return in DD-MM-YYYY format.
return $completiondate->format("d-m-Y");
}
/**
* Get the grade of a completed course for a user.
*
* @param int $userid
* @param int $courseid
* @return int $grade
*/
function get_certification_percentage($userid, $courseid) {
global $CFG;
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once($CFG->dirroot.'/grade/querylib.php');
$gradeitem = \grade_get_course_grades($courseid, $userid);
// Format the grade.
$grade = intval($gradeitem->grades[$userid]->grade);
return $grade;
}