forked from lushonline/moodle2-skillsoft-activity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
1410 lines (1248 loc) · 44 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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* @package mod-skillsoft
* @author $Author$
* @version SVN: $Header$
* @copyright 2009-2014 Martin Holden
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->dirroot.'/mod/skillsoft/lib.php');
require_once($CFG->dirroot.'/mod/skillsoft/aiccmodel.php');
require_once($CFG->dirroot.'/mod/skillsoft/aicclib.php');
defined('MOODLE_INTERNAL') || die();
/// Constants and settings for module skillsoft
define('TRACK_TO_LMS', '0');
define('TRACK_TO_OLSA', '1');
define('TRACK_TO_OLSA_CUSTOMREPORT', '2');
/// Constants and settings for module skillsoft
define('IDENTIFIER_USERID', 'id');
define('IDENTIFIER_USERNAME', 'username');
/// Constants and settings for module skillsoft
/// SSO actiontype for assets
define('SSO_ASSET_ACTIONTYPE_LAUNCH', 'launch');
define('SSO_ASSET_ACTIONTYPE_SUMMARY', 'summary');
define('SKILLSOFT_EVENT_ACTIVITY_VIEWED',0);
define('SKILLSOFT_EVENT_ACTIVITY_MANAGEMENT_VIEWED',1);
define('SKILLSOFT_EVENT_REPORT_VIEWED',2);
/**
* Returns an array of the array of what grade options
*
* @return array an array of OLSA Tracking Options
*/
function skillsoft_get_tracking_method_array(){
return array (TRACK_TO_LMS => get_string('skillsoft_tracktolms', 'skillsoft'),
TRACK_TO_OLSA => get_string('skillsoft_tracktoolsa', 'skillsoft'),
TRACK_TO_OLSA_CUSTOMREPORT => get_string('skillsoft_tracktoolsacustomreport', 'skillsoft'),
);
}
/**
* Returns an array
*
* @return array an array of fileds to choose for tracking
*/
function skillsoft_get_user_identifier_array(){
return array (IDENTIFIER_USERID => get_string('skillsoft_userid_identifier', 'skillsoft'),
IDENTIFIER_USERNAME => get_string('skillsoft_username_identifier', 'skillsoft'),
);
}
/**
* Returns an array
*
* @return array an array of fileds to choose for sso asset action type
*/
function skillsoft_get_sso_asset_actiontype_array(){
return array (SSO_ASSET_ACTIONTYPE_LAUNCH => get_string('skillsoft_sso_actiontype_launch', 'skillsoft'),
SSO_ASSET_ACTIONTYPE_SUMMARY => get_string('skillsoft_sso_actiontype_summary', 'skillsoft'),
);
}
/**
* Allows re-processing of processed assets during the next cycle
* by setting skillsoft_report_results processed filed to 0
* @param string $assetid the skillsoft assetid
* @return boolean
*/
function skillsoft_reset_processed($assetid) {
global $DB;
$conditions = array("assetid"=>$assetid, "processed"=>1);
return $DB->set_field('skillsoft_report_results', 'processed', 0, $conditions);
}
/**
* Creates a new sessionid key.
* @param int $userid
* @param int $skillsoftid
* @return string access key value
*/
function skillsoft_create_sessionid($userid, $skillsoftid) {
global $DB;
$key = new stdClass();
$key->skillsoftid = $skillsoftid;
$key->userid = $userid;
$key->timecreated = time();
$key->sessionid = md5($skillsoftid.'_'.$userid.'_'.$key->timecreated.random_string(40)); // something long and unique
$conditions = array('sessionid'=>$key->sessionid);
while ($DB->record_exists('skillsoft_session_track', $conditions)) {
// must be unique
$key->sessionid = md5($skillsoftid.'_'.$userid.'_'.$key->timecreated.random_string(40));
}
if (!$DB->insert_record('skillsoft_session_track', $key)) {
error('Can not insert new sessionid');
}
return $key->sessionid;
}
/**
* Checks a sessionid key.
* @param string $sessionid the skillsoft session_id
* @return object $key
*/
function skillsoft_check_sessionid($sessionid) {
global $DB;
$conditions = array('sessionid'=>$sessionid);
$key = $DB->get_record('skillsoft_session_track', $conditions);
return $key;
}
/**
* Given an skillsoft object this will return
* the HTML snippet for displaying the Launch Button
* or output the HTML based on value of $return
* @param object $skillsoft
* @param boolean $return
* @return string $output or null
*/
function skillsoft_view_display($skillsoft, $user, $return=false) {
global $CFG;
if (stripos($skillsoft->launch,'?') !== false) {
$connector = '&';
} else {
$connector = '?';
}
$element = "";
/* We need logic here that if SSO url defined we use this */
if (!$CFG->skillsoft_usesso && strtolower($skillsoft->assetid) != 'sso') {
//skillsoft_ssourl is not defined so do AICC
$newkey = skillsoft_create_sessionid($user->id, $skillsoft->id);
$launcher = $skillsoft->launch;
//Section 508 Enhancement - add x508 value of $user->screenreader
if (isset($user->screenreader) && $user->screenreader == 1) {
$launcher .= $connector.'x508=1';
}
$launcher .= $connector.'aicc_sid='.$newkey.'&aicc_url='.$CFG->wwwroot.'/mod/skillsoft/aicchandler.php';
//$options = "'".$CFG->skillsoft_aiccwindowsettings."'";
$options = "'".$skillsoft->aiccwindowsettings."'";
/*
* TODO: WE NEED LOGIC HERE TO HANDLE ADDING A NEW ATTEMPT WHEN USING TRACK TO LMS
*/
if ($CFG->skillsoft_trackingmode == TRACK_TO_LMS) {
//Get last attempt
if ($lastsession = skillsoft_get_tracks($skillsoft->id, $user->id)) {
//Add the last $attempt as a hidden field
$element.= "<input type=\"hidden\" name=\"attempt\" id=\"attempt\" value=\"".$lastsession->attempt."\" >";
//Get completed status of lastattempt
$completed = isset($lastsession->{'[SUMMARY]completed'}) ? userdate($lastsession->{'[SUMMARY]completed'}):null;
if (!is_null($completed)) {
//Overwrite the $element taht was hidden input with a checkbox option
$element.= "<div id=\"restart\" name=\"restart\"><input type=\"checkbox\" name=\"startover\" id=\"startover\" value=\"".($lastsession->attempt+1)."\" >".get_string('skillsoft_newattempt','skillsoft')."<br/></div>";
}
} else {
$element.= "<input type=\"hidden\" name=\"attempt\" id=\"attempt\" value=\"1\" >";
}
} else {
$element.= "<input type=\"hidden\" name=\"attempt\" id=\"attempt\" value=\"1\" >";
}
} else {
//we have skillsoft_ssourl so we replace {0} with $skillsoft->id
//$launcher = sprintf($CFG->skillsoft_ssourl,$skillsoft->assetid);
$launcher = sprintf($CFG->skillsoft_ssourl,$skillsoft->id);
$options = "''";
$element.= "<input type=\"hidden\" name=\"attempt\" id=\"attempt\" value=\"1\" >";
}
//Should look at making this call a JavaScript, that we include in the page
$element.= "<input type=\"button\" value=\"". get_string('skillsoft_enter','skillsoft') ."\" onclick=\"return openAICCWindow('$launcher', 'courseWindow',$options, false);\" />";
if ($return) {
return $element;
} else {
echo $element;
}
}
/**
* Insert values into the skillsoft_au_track table
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $element
* @param $value
* @return bool true if succesful
*/
function skillsoft_insert_track($userid,$skillsoftid,$attempt,$element,$value) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
/* 13-SEP-2013
* Added error trap to convert $value=NULL to null string ""
*/
if ($value===NULL) {
$value = "";
}
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
$track->value = $value;
$track->timemodified = time();
$id = $DB->update_record('skillsoft_au_track',$track);
} else {
$track = new stdClass();
$track->userid = $userid;
$track->skillsoftid = $skillsoftid;
$track->attempt = $attempt;
$track->element = $element;
//$track->value = addslashes($value);
$track->value = $value;
$track->timemodified = time();
$id = $DB->insert_record('skillsoft_au_track',$track);
}
//if we have a best score OR we have passed/completed status then update the gradebook
if ( strstr($element, ']bestscore') ||
(strstr($element,']lesson_status') && (substr($track->value,0,1) == 'c' || substr($track->value,0,1) == 'p'))
) {
$conditions = array('id'=> $skillsoftid);
$skillsoft = $DB->get_record('skillsoft', $conditions);
include_once('lib.php');
skillsoft_update_grades($skillsoft, $userid);
}
//print_object($track);
return $id;
}
/**
* setFirstAccessDate
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $time
* @return bool true if succesful
*/
function skillsoft_setFirstAccessDate($userid,$skillsoftid,$attempt,$time) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]firstaccess';
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
//We have value so do nothing
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, '[SUMMARY]firstaccess', $time);
}
return $id;
}
/**
* setLastAccessDate
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $time
* @return bool true if succesful
*/
function skillsoft_setLastAccessDate($userid,$skillsoftid,$attempt,$time) {
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, '[SUMMARY]lastaccess', $time);
return $id;
}
/**
* setCompletedDate
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $time
* @return bool true if succesful
*/
function skillsoft_setCompletedDate($userid,$skillsoftid,$attempt,$time) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]completed';
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
//We have value so do nothing
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, '[SUMMARY]completed', $time);
}
return $id;
}
/**
* setAccessCount
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @return bool true if succesful
*/
function skillsoft_setAccessCount($userid,$skillsoftid,$attempt,$value=0) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]accesscount';
if ($value == 0 ) {
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
//We have value so increment it
$accesscount = $track->value;
$accesscount++;
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $accesscount);
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, 1);
}
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $value);
}
return $id;
}
/**
* setFirstScore
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $score
* @return bool true if succesful
*/
function skillsoft_setFirstScore($userid,$skillsoftid,$attempt,$score) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]firstscore';
if ($score != 0) {
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
//We have value so do nothing
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $score);
}
}
return $id;
}
/**
* setCurrentScore
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $score
* @return bool true if succesful
*/
function skillsoft_setCurrentScore($userid,$skillsoftid,$attempt,$score) {
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]currentscore';
if ($score != 0) {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $score);
}
return $id;
}
/**
* setBestScore
*
* @param $userid
* @param $skillsoftid
* @param $attempt
* @param $score
* @return bool true if succesful
*/
function skillsoft_setBestScore($userid,$skillsoftid,$attempt,$score) {
global $DB;
$id = null;
//Work to support multiple attempts
//$attempt = 1;
$element = '[SUMMARY]bestscore';
if ($score != 0) {
$params = array($userid,$skillsoftid,$attempt,$element);
if ($track = $DB->get_record_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=? AND element=?",$params)) {
//We this score is higher
$currentscore = $track->value;
if ($score > $currentscore) {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $score);
}
} else {
$id = skillsoft_insert_track($userid, $skillsoftid, $attempt, $element, $score);
}
}
return $id;
}
/**
* @param $skillsoftid
* @param $userid
* @return value representing last attempt by user for asset
*/
function skillsoft_get_last_attempt($skillsoftid, $userid) {
global $DB;
/// Find the last attempt number for the given user id and scorm id
$conditions = array('userid'=> $userid, 'skillsoftid'=> $skillsoftid);
if ($lastattempt = $DB->get_record('skillsoft_au_track', $conditions, 'max(attempt) as a')) {
if (empty($lastattempt->a)) {
return '0';
} else {
return $lastattempt->a;
}
}
}
/**
* @param $skillsoftid
* @param $userid
* @param $attempt
* @return object representing all values for user and skillsoft activity in skillsoft_au_track
*/
function skillsoft_get_tracks($skillsoftid,$userid,$attempt='') {
/// Gets all tracks of specified sco and user
global $CFG,$DB;
//Work to support multiple attempts
//$attempt = 1;
if (empty($attempt)) {
$attempt = skillsoft_get_last_attempt($skillsoftid,$userid);
if ($attempt == 0) {
$attempt = 1;
}
}
$params=array($userid,$skillsoftid,$attempt);
if ($tracks = $DB->get_records_select('skillsoft_au_track',"userid=? AND skillsoftid=? AND attempt=?",$params,'element ASC')) {
$usertrack = new stdClass();
$usertrack->userid = $userid;
$usertrack->skillsoftid = $skillsoftid;
$usertrack->score_raw = '';
$usertrack->status = '';
$usertrack->total_time = '00:00:00';
$usertrack->session_time = '00:00:00';
$usertrack->timemodified = 0;
$usertrack->attempt = $attempt;
foreach ($tracks as $track) {
$element = $track->element;
$usertrack->{$element} = $track->value;
if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
$usertrack->timemodified = $track->timemodified;
}
}
if (is_array($usertrack)) {
ksort($usertrack);
}
return $usertrack;
} else {
return false;
}
}
/**
* @param object $skillsoft
* @param int $userid
* @param int $attempt
* @return object
*/
function skillsoft_grade_user($skillsoft, $userid, $attempt='') {
$result = new stdClass();
$result->score = 0;
$result->time = 0;
//We need to get last attempt to grade
if (empty($attempt)) {
$attempt = skillsoft_get_last_attempt($skillsoft->id,$userid);
if ($attempt == 0) {
$attempt = 1;
}
}
if ($userdata = skillsoft_get_tracks($skillsoft->id, $userid, $attempt)) {
if (isset($userdata->{'[SUMMARY]bestscore'})) {
$result->score = $userdata->{'[SUMMARY]bestscore'};
$result->time = $userdata->{'[SUMMARY]lastaccess'};
} else {
$result = NULL;
}
}
return $result;
}
/*************************************************************
* ODC Functions
*/
/**
* Insert raw tdr into the skillsoft_tdr
*
* @param $tdr
* @return bool true if succesful
*/
function skillsoft_insert_tdr($rawtdr) {
global $CFG, $DB;
//We get a raw SkillSoft TDR which we need to manipluate to fit into
//Moodle database limits
$tdr = new stdClass();
//Set TDRID
$tdr->tdrid = $rawtdr->id;
//Convert TimeStamp
//Define variables that are passed to sscanf
$year;
$month;
$day;
$hour;
$min;
$sec;
sscanf($rawtdr->timestamp,"%u-%u-%uT%u:%u:%uZ",$year,$month,$day,$hour,$min,$sec);
$tdr->timestamp = mktime($hour,$min,$sec,$month,$day,$year);
//20110114-Use the new skillsoft_getusername_from_loginname() function
//This allows us to centralise the "translation" from SkillPort Username
//to Moodle USERID
$tdr->userid = skillsoft_getusername_from_loginname($rawtdr->userid);
$tdr->username = $rawtdr->userid;
$tdr->assetid = $rawtdr->assetid;
$tdr->reset = $rawtdr->reset;
//Addslashes
$tdr->format = $rawtdr->format;
$tdr->data = $rawtdr->data;
$tdr->context = $rawtdr->context;
$params = array($tdr->tdrid);
if ($updatetdr = $DB->get_record_select('skillsoft_tdr','tdrid=?',$params)) {
$id = $DB->update_record('skillsoft_tdr',$tdr);
} else {
$id = $DB->insert_record('skillsoft_tdr',$tdr);
}
return $id;
}
/**
* Processes all the TDRs in the datbase updating skillsoft_au_track and gradebook
*
* @param $trace false default, flag to indicate if mtrace messages should be sent
* @return unknown_type
*/
function skillsoft_process_received_tdrs($trace=false) {
global $CFG,$DB;
if ($trace) {
mtrace(get_string('skillsoft_odcprocessinginit','skillsoft'));
}
if ($unmatchedtdrs = $DB->get_records_select('skillsoft_tdr','userid=0',null,'tdrid ASC')) {
foreach ($unmatchedtdrs as $tdr) {
$tdr->userid = skillsoft_getusername_from_loginname($tdr->username);
if ($tdr->userid != 0)
{
$id = update_record('skillsoft_tdr',$tdr);
}
}
}
//Select all the unprocessed TDR's
//We do it this way so that if we create a new Moodle SkillSoft activity for an asset we
//have TDR's for already we can "catch up"
$sql = "SELECT t.id as id, s.id AS skillsoftid, u.id AS userid, t.tdrid, t.timestamp, t.reset, t.format, t.data, t.context, t.processed ";
$sql .= "FROM {skillsoft_tdr} t INNER JOIN {user} u ON u.id = t.userid INNER JOIN {skillsoft} s ON t.assetid = s.assetid ";
$sql .= "WHERE t.processed=0 ";
$sql .= "ORDER BY s.id,u.id,t.tdrid ";
$attempt=1;
$lasttdr = new stdClass();
$lasttdr->skillsoftid = NULL;
$lasttdr->userid = NULL;
$rs = $DB->get_recordset_sql($sql);
if ($rs->valid()) {
foreach ($rs as $processedtdr) {
if ($trace) {
mtrace(get_string('skillsoft_odcprocessretrievedtdr','skillsoft',$processedtdr));
}
if ($processedtdr->skillsoftid != $lasttdr->skillsoftid || $processedtdr->userid != $lasttdr->userid) {
$conditions = array('id'=> $processedtdr->skillsoftid);
$skillsoft = $DB->get_record('skillsoft',$conditions);
$conditions2 = array('id'=> $processedtdr->userid);
$user = $DB->get_record('user',$conditions2);
$handler = new aicchandler($user,$skillsoft,$attempt,$CFG->skillsoft_strictaiccstudentid);
}
//Process the TDR as AICC Data
if ($skillsoft->completable) {
$handler->processtdr($processedtdr, $attempt);
} else {
$handler->processtdr($processedtdr, 1);
}
$processedtdr->processed = 1;
$id = $DB->update_record('skillsoft_tdr',$processedtdr);
$lasttdr = $processedtdr;
}
}
$rs->close();
if ($trace) {
mtrace(get_string('skillsoft_odcprocessingend','skillsoft'));
}
}
/**
* This function is is the key to importing usage data from SkillPort
* It will attempt to convert the SkillPort username to the equivalent
* Moodle $user->id, if it fails the is is returned as 0
*
* @param $skillport_loginname
* @return $moodle_userid
*/
function skillsoft_getusername_from_loginname($skillport_loginname) {
global $CFG, $DB;
//If the PREFIX is configured we strip this from the skillport loginname
//Before we attempt to match it
if ($CFG->skillsoft_accountprefix != '') {
//Check we have the prefix in the username
$pos = stripos($skillport_loginname, $CFG->skillsoft_accountprefix);
if ($pos !== false && $pos == 0) {
$skillport_loginname = substr($skillport_loginname,strlen($CFG->skillsoft_accountprefix));
}
}
//We check if we are using the IDENTIFIER_USERID that the
//SkillPort loginname is numeric before we attempt to match it
if ($CFG->skillsoft_useridentifier == IDENTIFIER_USERID) {
if (!is_numeric($skillport_loginname) ) {
return 0;
break;
}
}
//Now we attempt to get the Moodle userid by looking up the user
//We return the Moodle USERID or 0 if no match
$conditions = array($CFG->skillsoft_useridentifier=>$skillport_loginname);
if ($user = $DB->get_record('user',$conditions)) {
return $user->id;
} else {
return 0;
}
}
/*
* CUSTOM REPORT HANDLING FUNCTIONS
*/
/**
* This function will convert numeric byte to KB, MB etc
*
* @param int $bytes - The numeric bytes
* @return string Formatted String representation
*/
function byte_convert($bytes)
{
$symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$exp = 0;
$converted_value = 0;
if( $bytes > 0 )
{
$exp = floor( log($bytes)/log(1024) );
$converted_value = ( $bytes/pow(1024,floor($exp)) );
}
return sprintf( '%.2f '.$symbol[$exp], $converted_value );
}
/**
* Insert values into the skillsoft_report_track table
*
* @return bool true if succesful
*/
function skillsoft_insert_customreport_requested($handle,$startdate='',$enddate='') {
global $DB;
$id = null;
$report = new stdClass();
$report->handle = $handle;
$report->startdate = $startdate;
$report->enddate = $enddate;
$report->timerequested = time();
$id = $DB->insert_record('skillsoft_report_track',$report);
return $id;
}
/**
* Record report ready
*
* @return bool true if succesful
*/
function skillsoft_update_customreport_polled($handle,$url) {
global $DB;
$id = null;
$params = array($handle);
if ($report = $DB->get_record_select('skillsoft_report_track','handle=?',$params)) {
$report->url = $url;
$report->polled = true;
$report->timepolled = time();
$id = $DB->update_record('skillsoft_report_track',$report);
}
return $id;
}
/**
* Record report downloaded
*
* @return bool true if succesful
*/
function skillsoft_update_customreport_downloaded($handle,$localpath) {
global $DB;
$id = null;
$params = array($handle);
if ($report = $DB->get_record_select('skillsoft_report_track','handle=?',$params)) {
$report->localpath = $localpath;
$report->downloaded = true;
$report->timedownloaded = time();
$id = $DB->update_record('skillsoft_report_track',$report);
}
return $id;
}
/**
* Record report imported
*
* @return bool true if succesful
*/
function skillsoft_update_customreport_imported($handle) {
global $DB;
$id = null;
$params = array($handle);
if ($report = $DB->get_record_select('skillsoft_report_track','handle=?',$params)) {
$report->imported = true;
$report->timeimported = time();
$id = $DB->update_record('skillsoft_report_track',$report);
}
return $id;
}
/**
* Record report processed
*
* @return bool true if succesful
*/
function skillsoft_update_customreport_processed($handle) {
global $DB;
$id = null;
$params = array($handle);
if ($report = $DB->get_record_select('skillsoft_report_track','handle=?',$params)) {
$report->processed = true;
$report->timeprocessed = time();
$id = $DB->update_record('skillsoft_report_track',$report);
}
return $id;
}
/**
* Delete the entry in skillsoft_report_track table
*
* @return bool true if succesful
*/
function skillsoft_delete_customreport($handle) {
global $DB;
$id = null;
$conditions = array('handle'=>$handle);
$id = $DB->delete_records('skillsoft_report_track',$conditions);
return $id;
}
/**
* Converts the CSV data from a row in the custom report which is in
* array format into an object for easy insert into database
*
* It also performs any necessary conversions and validations
* such as dates to timestamps
*
* @param $arraykey Array of key names
* @param $arrayvalue Array of the values
* @return $object
*/
function ConvertCSVRowToReportResults($arraykey, $arrayvalue) {
$object = new stdClass();
//Need to consider if using column position is better rather than heading
$count = count($arraykey);
for ($i = 0; $i < $count; $i++) {
$cleankey = trim(strtolower($arraykey[$i]));
//Do we have a value or is it null
if ($arrayvalue[$i])
{
$hour=0;
$min=0;
$sec=0;
$month=0;
$day=0;
$year=0;
switch ($cleankey) {
case 'duration';
if ( $arrayvalue[$i] > 0 ) {
$object->duration = $arrayvalue[$i];
} else {
$object->duration = 0;
}
break;
case 'courseid';
$object->assetid = $arrayvalue[$i];
break;
case 'firstaccessdate';
case 'lastaccessdate';
//Convert TimeStamp 2010-01-29 18:09:18
sscanf($arrayvalue[$i],"%u-%u-%u %u:%u:%u",$year,$month,$day,$hour,$min,$sec);
$object->$cleankey = mktime($hour,$min,$sec,$month,$day,$year);
break;
case 'completiondate';
//Convert TimeStamp 2010-01-29 18:09:18
sscanf($arrayvalue[$i],"%u-%u-%u %u:%u:%u",$year,$month,$day,$hour,$min,$sec);
$object->completeddate = mktime($hour,$min,$sec,$month,$day,$year);
break;
case 'timesaccessed';
$object->accesscount = $arrayvalue[$i];
break;
case 'overallpreassess';
$object->firstscore = $arrayvalue[$i];
break;
case 'overallhigh';
$object->bestscore = $arrayvalue[$i];
break;
case 'overallcurrent';
$object->currentscore = $arrayvalue[$i];
break;
case 'coursestatus';
switch (strtolower($arrayvalue[$i]))
{
case 'started';
$object->lessonstatus = 'incomplete';
break;
case 'completed';
$object->lessonstatus = 'completed';
break;
}
break;
default:
//Here we apply custom logic
$object->$cleankey = $arrayvalue[$i];
}
}
}
return $object;
}
/**
* Insert report_results into the skillsoft_report_results
*
* @param $report_results
* @return bool true if succesful
*/
function skillsoft_insert_report_results($report_results) {
global $CFG, $DB;
$success = null;
//Need to determine the moodle userid based on loginname
//$report_results->userid = skillsoft_getusername_from_loginname($report_results->loginname);
$report_results->userid = 0;
//Update to insert unique records BY loginname, assetid and firstaccessdate to handle multiple completions
//if ($update_results = get_record_select('skillsoft_report_results',"loginname='$report_results->loginname' and assetid='$report_results->assetid'")) {
$params=array($report_results->loginname,$report_results->assetid,$report_results->firstaccessdate);
if ($update_results = $DB->get_record_select('skillsoft_report_results','loginname=? and assetid=? and firstaccessdate=?',$params, 'id')) {
$report_results->id = $update_results->id;
$report_results->processed = 0;
$success = $DB->update_record('skillsoft_report_results',$report_results);
} else {
$success = $DB->insert_record('skillsoft_report_results',$report_results);
}
return $success;
}
/*
* This function will use OLSA to run a custom report
*
* @param bool $trace - Do we output tracing info.
* @param string $prefix - The string to prefix all mtrace reports with
* @param string $includetoday - Include todays report, used as a debugging aid
* @return string $handle - The report handle
*/
function skillsoft_run_customreport($trace=false, $prefix=' ', $includetoday=false) {
global $CFG;
$mprefix = is_null($prefix) ? " " : $prefix;
$starttime = microtime(true);
$handle = '';
if ($trace){
mtrace($mprefix.get_string('skillsoft_customreport_run_start','skillsoft'));
}
$startdate=$CFG->skillsoft_reportstartdate;
if ($startdate == '' || $startdate==strtotime(date("d-M-Y"))) {
$startdate = "01-Jan-2000";
set_config('skillsoft_reportstartdate', $startdate);
}
$startdateticks = strtotime($startdate);
if ($includetoday) {
//End date is "today"
$enddateticks = strtotime(date("d-M-Y"));
} else {
//End date is "yesterday"
$enddateticks = strtotime(date("d-M-Y") . " -1 day");
}
$enddate = date("d-M-Y",$enddateticks);
mtrace($mprefix.get_string('skillsoft_customreport_run_startdate','skillsoft', date("c",$startdateticks)));
mtrace($mprefix.get_string('skillsoft_customreport_run_enddate','skillsoft', date("c",$enddateticks)));
if (($startdateticks == $enddateticks) && !$includetoday) {
//The enddate has already been retrieved so do nothing
mtrace($mprefix.get_string('skillsoft_customreport_run_alreadyrun','skillsoft'));
} else {
//$initresponse = UD_InitiateCustomReportByUserGroups('skillsoft',$startdate,$enddate);
$initresponse = UD_InitiateCustomReportByUsers('',$startdate,$enddate);
if ($initresponse->success) {
$handle = $initresponse->result->handle;
$id=skillsoft_insert_customreport_requested($handle,$startdate,$enddate);
mtrace($mprefix.get_string('skillsoft_customreport_run_response','skillsoft',$initresponse->result->handle));
} else {
mtrace($mprefix.get_string('skillsoft_customreport_run_initerror','skillsoft',$initresponse->errormessage));
}
}
$endtime = microtime(true);
$duration = $endtime - $starttime;
if ($trace){
mtrace($mprefix.get_string('skillsoft_customreport_run_end','skillsoft').' (took '.$duration.' seconds)');
}
return $handle;
}
/*
* This function will use OLSA to poll if the report is ready
*
* @param string $handle - The report handle to poll for
* @param bool $trace - Do we output tracing info.
* @param string $prefix - The string to prefix all mtrace reports with
* @return string The URL of the report or ""
*/
function skillsoft_poll_customreport($handle, $trace=false, $prefix=' ') {
global $CFG;
$starttime = microtime(true);