-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathstatistics.class.php
More file actions
2406 lines (2173 loc) · 121 KB
/
Copy pathstatistics.class.php
File metadata and controls
2406 lines (2173 loc) · 121 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
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
/**
* EfrontStats Class file
*
* @package eFront
* @version 3.5.0
*/
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
/**
* Statistics exceptions
*
* @package eFront
*/
class EfrontStatsException extends Exception
{
const INVALID_ID = 1001;
const INVALID_PARAMETER = 1002;
}
/**
* This class is used to handle statistics
*
* @package eFront
*/
class EfrontStats
{
/**
* Get user seen content in lesson
*
* This function calulates the content done by the specified student(s) in the
* specified lesson. If $users is not specified, then information on all users and/or lessons
* is calculated.
* <br/>Example:
* <code>
* EfrontStats :: getStudentsSeenContent(3, 'jdoe'); //Get statistics for user jdoe in lesson 3
* EfrontStats :: getStudentsSeenContent(3, array('jdoe', 'george')); //Get statistics for users george and jdoe in lesson 3
* EfrontStats :: getStudentsSeenContent(3); //Get statistics for all users in lesson 3
* EfrontStats :: getStudentsSeenContent(); //Get statistics for all users in all lessons
* </code>
* The resulting array is of the form array(lesson id => array(login => array(done content))):
* <code>
* Array
* (
* [32] => Array
* (
* [jdoe] => Array
* (
* [1415] =>
* [1417] =>
* [1416] => 50
* [1412] =>
* [1411] =>
* [1413] =>
* [1420] => 100
* )
* [george] => Array
* (
* [1415] =>
* [1417] =>
* [1416] => 66
* [1412] =>
* [1408] =>
* [1409] =>
* [1410] =>
* [1420] => 30
* )
* )
* )
* </code>
*
* This means that: very lesson is an array, where every student has a corresponding array, in which the keys represent the units he has
* completed. For content units, the array values are empty. For test units, the array values contain the
* users's score.
*
* @param mixed $lessons An array of lesson ids or EfrontLesson objects. If false, all lessons are considered
* @param mixed $users One or more optional user logins
* @return array The seen content per user login
* @since 3.5.0
* @access public
* @static
*/
public static function getStudentsSeenContent($lessons = false, $users = false, $options = array()) {
if ($lessons == false) {
$lessons = eF_getTableDataFlat("lessons", "id");
$lessons = $lessons['id'];
} else if (!is_array($lessons)) {
$lessons = array($lessons);
}
foreach ($lessons as $key => $lesson) {
if ($lesson instanceof EfrontLesson) {
$lessons[$key] = $lesson -> lesson['id'];
} else if (!eF_checkParameter($lesson, 'id')) {
throw new EfrontLessonException(_INVALIDID, EfrontLessonException :: INVALID_ID);
} else {
$lessons[$key] = $lesson;
}
}
if ($users != false) {
!is_array($users) ? $users = array($users) : null; //Convert single login to array
} else {
$users = eF_getTableDataFlat("users", "login");
$users = $users['login'];
}
foreach ($users as $key => $user) {
if ($user instanceof EfrontUser) {
$users[$key] = $user -> user['login'];
} else if (!eF_checkParameter($user, 'login')) {
throw new EfrontLessonException(_INVALIDLOGIN, EfrontUserException :: INVALID_LOGIN);
} else {
$users[$key] = $user;
}
}
if (sizeof($lessons) > 0 && sizeof($users) > 0) {
$doneTests = array();
//debug_print_backtrace();
if (!isset($options['notests']) || !$options['notests']) {
if (sizeof($users) <= 100) {
$result = eF_getTableData("completed_tests ct, tests t", "ct.archive, ct.status, ct.score, ct.users_LOGIN, t.lessons_ID, t.content_ID, t.keep_best", "ct.status != 'deleted' and ct.status != 'incomplete' and t.id=ct.tests_ID and t.lessons_ID in (".implode(",", $lessons).") and users_LOGIN in ('".implode("','", $users)."')");
} else {
$result = eF_getTableData("completed_tests ct, tests t", "ct.archive, ct.status, ct.score, ct.users_LOGIN, t.lessons_ID, t.content_ID, t.keep_best", "ct.status != 'deleted' and ct.status != 'incomplete' and t.id=ct.tests_ID and t.lessons_ID in (".implode(",", $lessons).")");
}
foreach ($result as $value) {
if ($value['keep_best'] && $value['status'] == 'passed') {
$doneTests[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']] = $value['score'];
} elseif ($value['status'] != 'failed' && !$value['archive']) {
$doneTests[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']] = $value['score'];
}
}
//
}
$temp = eF_getTableData("user_types", "*");
if (sizeof($temp) == 0) {
$result = eF_getTableData("users u, users_to_lessons ul", "u.login, ul.lessons_ID, ul.done_content", "ul.archive=0 and ul.user_type = 'student' and u.login = ul.users_LOGIN and u.login in ('".implode("','", $users)."') and ul.lessons_ID in (".implode(",", $lessons).")") ;
} else {
$result = eF_getTableData("users u, users_to_lessons ul, user_types as ut", "u.login, ul.lessons_ID, ul.done_content", "ul.archive=0 and (ul.user_type = 'student' OR (ul.user_type=ut.id AND ut.basic_user_type = 'student')) and u.login = ul.users_LOGIN and u.login in ('".implode("','", $users)."') and ul.lessons_ID in (".implode(",", $lessons).")");
}
//$result = eF_getTableData("users u, users_to_lessons ul, user_types as ut", "u.login, ul.lessons_ID, ul.done_content", "(ul.user_type = 'student' OR (ul.user_type=ut.id AND ut.basic_user_type = 'student')) and u.login = ul.users_LOGIN");
//$result = eF_getTableData("users u, users_to_lessons ul", "u.login, ul.lessons_ID, ul.done_content", "ul.user_type = 'student' and u.login = ul.users_LOGIN");
$usersDoneContent = array();
foreach ($result as $value) {
$usersDoneContent[$value['lessons_ID']][$value['login']] = unserialize($value['done_content']);
}
//Get lessons content, in case a done unit is not part of a lesson anymore or is inactive
$result = eF_getTableData("content c", "id, lessons_ID", "lessons_ID in (".implode(",", $lessons).") and active=1");
$lessonContent = array();
foreach ($result as $value) {
$lessonContent[$value['lessons_ID']][] = $value['id'];
}
$resultScorm = eF_getTableData("scorm_data sd, content c", "c.ctg_type, c.lessons_ID, content_ID, users_LOGIN, lesson_status, score, minscore, maxscore, masteryscore", "c.id=sd.content_ID and c.lessons_ID in (".implode(",", $lessons).") and sd.users_LOGIN in ('".implode("','", $users)."')");
foreach ($resultScorm as $key => $value) {
if ($value['lesson_status'] == 'passed' || $value['lesson_status'] == 'completed') {
if ($value['ctg_type'] == 'scorm') {
$scormDoneContent[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']] = '';
} elseif ($value['ctg_type'] == 'scorm_test') {
if (is_numeric($value['minscore']) && is_numeric($value['maxscore'])) {
$value['score'] = 100 * $value['score'] / ($value['minscore'] + $value['maxscore']);
} else {
$value['score'] = $value['score'];
}
$scormDoneContent[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']] = $value['score'];
}
} else {
//Remove this unit from the seen contents unit, since it is failed
if (isset($usersDoneContent[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']])) {
unset($usersDoneContent[$value['lessons_ID']][$value['users_LOGIN']][$value['content_ID']]);
}
}
}
}
foreach ($lessons as $lessonId) {
!isset($usersDoneContent[$lessonId]) ? $usersDoneContent[$lessonId] = array() : null;
foreach ($usersDoneContent[$lessonId] as $key => $value) { //Unserialize and preprocess values. This way, only the array keys contain the content id, while array values contain test scores (when the unit is a test). This way we may use array_sum to calculate the mean score at once)
//if ($value) {
!isset($usersDoneContent[$lessonId][$key]) || !is_array(($usersDoneContent[$lessonId][$key])) ? $usersDoneContent[$lessonId][$key] = array() : null;
foreach ($usersDoneContent[$lessonId][$key] as $k => $id) {
if (!in_array($id, $lessonContent[$lessonId])) {
unset($usersDoneContent[$lessonId][$key][$k]);
} else {
$usersDoneContent[$lessonId][$key][$k] = '';
}
}
if (isset($doneTests[$lessonId][$key])) {
is_array($usersDoneContent[$lessonId][$key]) ? $usersDoneContent[$lessonId][$key] = ($doneTests[$lessonId][$key] + $usersDoneContent[$lessonId][$key]) : $usersDoneContent[$lessonId][$key] = $doneTests[$lessonId][$key]; //We cannot use + for arrays, if one of them is not set.
}
if (isset($scormDoneContent[$lessonId][$key])) {
is_array($usersDoneContent[$lessonId][$key]) ? $usersDoneContent[$lessonId][$key] = ($scormDoneContent[$lessonId][$key] + $usersDoneContent[$lessonId][$key]) : $usersDoneContent[$lessonId][$key] = $scormDoneContent[$lessonId][$key];
}
}
foreach ($usersDoneContent[$lessonId] as $key => $value) {
if (!$value || ($users != false && !in_array($key, $users))) { //Filter out empty results or results not specified within $users array
unset($usersDoneContent[$lessonId][$key]);
}
}
if (empty($usersDoneContent[$lessonId])) {
unset($usersDoneContent[$lessonId]);
}
}
return $usersDoneContent;
}
/**
* Get user done tests in lesson
*
* This function finds the done tests of the specified users.
* If $users is not specified, then information on all users and/or lessons
* is calculated.
* <br/>Example:
* <code>
* EfrontStats :: getStudentsDoneTests(3, 'jdoe'); //Get statistics for user jdoe in lesson 3
* EfrontStats :: getStudentsDoneTests(3, array('jdoe', 'george')); //Get statistics for users george and jdoe in lesson 3
* EfrontStats :: getStudentsDoneTests(3); //Get statistics for all users in lesson 3
* </code>
* The resulting array is of the form array(login => array(content id => array(results))):
* <code>
* Array
* (
* [jdoe] => Array
* (
* [30] => Array
* (
* [lessons_ID] => 78
* [name] => Maya History Test
* [content_ID] => 30
* [done_tests_ID] => 1
* [tests_ID] => 2
* [score] => 1
* [comments] =>
* [users_LOGIN] => jdoe
* )
* [2] => Array
* (
* [lessons_ID] => 77
* [name] => General concepts test
* [content_ID] => 2
* [done_tests_ID] => 2
* [tests_ID] => 1
* [score] => 0.333333
* [comments] =>
* [users_LOGIN] => jdoe
* )
* )
* [george] => Array
* (
* [2] => Array
* (
* [lessons_ID] => 77
* [name] => General concepts test
* [content_ID] => 2
* [done_tests_ID] => 3
* [tests_ID] => 1
* [score] => 1
* [comments] =>
* [users_LOGIN] => george
* )
* )
* )
* </code>
*
* @param mixed $lessons Either the lesson id or an EfrontLesson object, or an array of such
* @param mixed $users A single user login, an array of user logins or nothing for all users
* @return array The done tests per user
* @since 3.5.0
* @access public
* @static
*/
public static function getStudentsDoneTests($lessons = false, $users = false) {
if (!$users) {
$users = eF_getTableDataFlat("users", "login");
$users = $users['login'];
} elseif (!(is_array($users))) {
$users = array($users);
}
if ($lessons !== false) {
if (!is_array($lessons)) {
$lessons = array($lessons);
}
foreach ($lessons as $key => $lesson) {
$lesson instanceOf EfrontLesson ? $lessons[$key] = $lesson -> lesson['id'] : null;
}
$lessonId = implode(",", $lessons);
}
/*
$usersDoneTests = eF_getTableData("tests t, content c, done_tests dt", "c.lessons_ID, c.name, c.active, t.content_ID, dt.id as done_tests_ID, dt.tests_ID, dt.score, dt.comments, dt.users_LOGIN, dt.timestamp", "dt.tests_ID = t.id and t.content_ID = c.id".($lessonId ? " and c.lessons_ID in ($lessonId)" : ""));
$doneTests = array();
foreach ($usersDoneTests as $doneTest) {
if (!$users || in_array($doneTest['users_LOGIN'], $users)) {
$doneTests[$doneTest['users_LOGIN']][$doneTest['content_ID']] = $doneTest;
}
}
*/
$doneTests = array();
//We create a 10-fold loop for memory efficiency
for ($i = 0; $i < sizeof($users); $i += 20) {
$temp = EfrontStats :: getDoneTestsPerUser(array_slice($users, $i, 10, true));
}
//@todo: This is for compatibility with previous version and should be removed in the future
foreach ($temp as $user => $value) {
foreach ($value as $testId => $testData) {
if (is_array($testData)) {
foreach ($testData as $done_tests_ID => $test) {
if (is_numeric($done_tests_ID) && $done_tests_ID == $testData['active_test_id']) {
//$unit = $test -> getUnit();
$stats = array('lessons_ID' => $test['lessons_ID'],
'name' => $test['name'],
'active' => $test['active'],
'content_ID' => $test['content_ID'],
'done_tests_ID' => $done_tests_ID,
'tests_ID' => $test['tests_ID'],
'score' => $test['score'],
'active_score' => $testData['active_score'],
'active_test_id' => $testData['active_test_id'],
'status' => $test['status'],
//'comments' => $test -> completedTest['feedback'],
'users_LOGIN'=> $user,
'timestamp' => $test['time_end']);
if ($dt['archive'] == 0 && $test['status'] != 'incomplete' && $test['status'] != '' && ($lessons === false || in_array($stats['lessons_ID'], $lessons)) && in_array($stats['users_LOGIN'], $users)) {
$doneTests[$user][$test['content_ID']] = $stats;
}
}
}
}
}
}
$usersDoneScormTests = eF_getTableData("content c, scorm_data sd", "c.lessons_ID, c.name, c.active, sd.masteryscore, sd.lesson_status, sd.content_ID, sd.score, sd.minscore, sd.maxscore, sd.users_LOGIN, sd.timestamp", "sd.lesson_status != 'incomplete' and sd.content_ID = c.id and c.ctg_type = 'scorm_test' and sd.users_LOGIN != ''".($lessonId ? " and c.lessons_ID in ($lessonId)" : ""));
foreach ($usersDoneScormTests as $doneScormTest) {
if (!$users || in_array($doneScormTest['users_LOGIN'], $users)) {
if (is_numeric($doneScormTest['minscore']) && is_numeric($doneScormTest['maxscore'])) {
$doneScormTest['score'] = 100 * $doneScormTest['score'] / ($doneScormTest['minscore'] + $doneScormTest['maxscore']);
} else {
$doneScormTest['score'] = $doneScormTest['score'];
}
$doneScormTest['active_score'] = $doneScormTest['score'];
$doneScormTest['status'] = $doneScormTest['lesson_status'];
$doneScormTest['scorm'] = true;
$doneTests[$doneScormTest['users_LOGIN']][$doneScormTest['content_ID']] = $doneScormTest;
}
}
return $doneTests;
}
/**
* Get users' done tests
*
* This function is used to get the users' done tests.
* <br/>Example:
* <code>
* $doneTests = EfrontStats :: getDoneTestsPerUser(); //Get done instances of all tests for all users
* $doneTests = EfrontStats :: getDoneTestsPerUser('jdoe'); //Get done instances of all tests for user 'jdoe'
* $doneTests = EfrontStats :: getDoneTestsPerUser(false, 23); //Get done instances of test with id 23 for all users
* $doneTests = EfrontStats :: getDoneTestsPerUser('jdoe', 23); //Get done instances of test with id 23 for user 'jdoe'
* //$doneTests now contains an array of the form:
* Array
* (
* [jdoe] => Array
* (
* [23] => Array
* (
* [1] => Array
* (
* //Completed test with id 1...
* )
* [2] => Array
* (
* //Completed test with id 2...
* )
* )
* )
* )
* </code>
*
* @param mixed $user The user to get tests for, or all if false. Can be either the user login, or an EfrontUser object
* @param mixed $test The test to get done tests for, or all if false. Can be either the test id, or an EfrontTest object
* @return array The list of done tests, in a user => test id => tests form
* @since 3.5.2
* @access public
* @static
*/
public static function getDoneTestsPerUser($users = false, $test = false, $lesson = false) {
if ($users !== false) {
if (is_array($users)) {
foreach ($users as $key => $user) {
if (!eF_checkParameter($user, 'login')) {
unset($users[$key]);
}
}
if (sizeof($users) == 0) {
throw new EfrontUserException(_INVALIDLOGIN.': '.implode(",", $users), EfrontUserException :: INVALID_LOGIN);
}
} else if ($users instanceof EfrontUser) {
$users = array($users -> user['login']);
} else if (!eF_checkParameter($users, 'login')) {
throw new EfrontUserException(_INVALIDLOGIN.': '.$users, EfrontUserException :: INVALID_LOGIN);
} else {
$users = array($users);
}
$user = implode("','", $users);
}
if ($test !== false) {
if ($test instanceof EfrontTest) {
$test = $test -> test['id'];
} else if (!eF_checkParameter($test, 'id')) {
throw new EfrontTestException(_INVALIDID.': '.$test, EfrontTestException :: INVALID_ID);
}
}
$sql = '';
if ($lesson) {
$sql = ' and t.lessons_ID='.$lesson;
}
if ($user && $test) {
$result = eF_getTableData("completed_tests ct, tests t", "ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending, t.content_ID, t.lessons_ID, t.name, t.active, t.keep_best", "ct.status != 'deleted' and ct.tests_ID=t.id and ct.tests_ID=$test and ct.users_LOGIN in ('$user') $sql");
} else if ($user) {
$result = eF_getTableData("completed_tests ct, tests t", "ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending, t.content_ID, t.lessons_ID, t.name, t.active, t.keep_best", "ct.status != 'deleted' and ct.tests_ID=t.id and ct.users_LOGIN in ('$user') $sql");
} else if ($test) {
$result = eF_getTableData("completed_tests ct, tests t", "ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending, t.content_ID, t.lessons_ID, t.name, t.active, t.keep_best", "ct.status != 'deleted' and ct.tests_ID=t.id and ct.tests_ID=$test $sql");
} else {
$result = eF_getTableData("completed_tests ct, tests t", "ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending, t.content_ID, t.lessons_ID, t.name, t.active, t.keep_best", "ct.status != 'deleted' and ct.tests_ID=t.id $sql");
}
$testResults = array();
foreach ($result as $value) {
//$value['test'] = unserialize($value['test']);
$testResults[$value['users_LOGIN']][$value['tests_ID']][$value['id']] = $value;
}
//Loop through objects, so that a per lesson/per test array can be constructed, with statistics for each
foreach ($testResults as $user => $tests) {
$averageScores = array();
foreach ($tests as $testId => $doneTests) {
foreach ($doneTests as $doneTestId => $doneTest) {
$testResults[$user][$testId]['scores'][$doneTest['id']] = $doneTest['score'];
$doneTest['archive'] == 0 ? $testResults[$user][$testId]['last_test_id'] = $doneTest['id'] : null;
$testResults[$user][$testId][$doneTestId] = $doneTest;
}
if (!isset($testResults[$user][$testId]['last_test_id'])){
end($testResults[$user][$testId]['scores']);
$testResults[$user][$testId]['last_test_id'] = key($testResults[$user][$testId]['scores']);
}
$testResults[$user][$testId]['average_score'] = round(array_sum($testResults[$user][$testId]['scores']) / sizeof($doneTests), 2);
$testResults[$user][$testId]['max_score'] = max($testResults[$user][$testId]['scores']);
$testResults[$user][$testId]['min_score'] = min($testResults[$user][$testId]['scores']);
$testResults[$user][$testId]['times_done'] = sizeof($doneTests);
if ($doneTest['keep_best']) {
$testResults[$user][$testId]['active_score'] = $testResults[$user][$testId]['max_score'];
$maxScoreId = array_search($testResults[$user][$testId]['max_score'], $testResults[$user][$testId]['scores']);
$testResults[$user][$testId]['active_test_id'] = $maxScoreId;
} else {
$testResults[$user][$testId]['active_score'] = $testResults[$user][$testId]['scores'][$testResults[$user][$testId]['last_test_id']];
$testResults[$user][$testId]['active_test_id'] = $testResults[$user][$testId]['last_test_id'];
}
$averageScores[] = $testResults[$user][$testId]['average_score'];
}
$testResults[$user]['average_score'] = round(array_sum($averageScores) / sizeof($averageScores), 2);
}
//pr($testResults);
//exit;
return $testResults;
}
/**
* Get users' done tests per test
*
* This function is used to get the users' done tests, on a per-test basis.
* <br/>Example:
* <code>
* $doneTests = EfrontStats :: getDoneTests(); //Get done instances of all tests for all users
* $doneTests = EfrontStats :: getDoneTests('jdoe'); //Get done instances of all tests for user 'jdoe'
* $doneTests = EfrontStats :: getDoneTests(false, 23); //Get done instances of test with id 23 for all users
* $doneTests = EfrontStats :: getDoneTests('jdoe', 23); //Get done instances of test with id 23 for user 'jdoe'
* //$doneTests now contains an array of the form:
* Array
* (
* [23] => Array
* (
* [jdoe] => Array
* (
* [1] => Array
* (
* //Completed test with id 1...
* )
* [2] => Array
* (
* //Completed test with id 2...
* )
* )
* )
* )
* </code>
*
* @param mixed $user The user to get tests for, or all if false. Can be either the user login, or an EfrontUser object
* @param mixed $test The test to get done tests for, or all if false. Can be either the test id, or an EfrontTest object
* @return array The list of done tests, in a user => test id => tests form
* @since 3.5.2
* @access public
* @static
*/
public static function getDoneTestsPerTest($users = false, $test = false, $from = false, $to = false, $lesson = false) {
if ($from !== false && $to !== false) {
$timeString = " and ct.timestamp between $from and $to ";
}
if ($users !== false) {
if (is_array($users)) {
foreach ($users as $key => $user) {
if (!eF_checkParameter($user, 'login')) {
unset($users[$key]);
}
}
if (sizeof($users) == 0) {
throw new EfrontUserException(_INVALIDLOGIN.': '.implode(",", $users), EfrontUserException :: INVALID_LOGIN);
}
} else if ($users instanceof EfrontUser) {
$users = array($users -> user['login']);
} else if (!eF_checkParameter($users, 'login')) {
throw new EfrontUserException(_INVALIDLOGIN.': '.$users, EfrontUserException :: INVALID_LOGIN);
}
$user = implode("','", $users);
}
if ($test !== false) {
if ($test instanceof EfrontTest) {
$test = $test -> test['id'];
} else if (!eF_checkParameter($test, 'id')) {
throw new EfrontTestException(_INVALIDID.': '.$test, EfrontTestException :: INVALID_ID);
}
}
if ($lesson) {
$sql = ' and t.lessons_ID='.$lesson;
}
if ($user && $test) {
$result = eF_getTableData("completed_tests ct, tests t", "t.keep_best, t.mastery_score, ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending", "ct.status != 'deleted' and ct.tests_ID = t.id and ct.status != '' and ct.status != 'incomplete'".$timeString." and ct.tests_ID=$test and ct.users_LOGIN in ('$user') $sql");
} else if ($user) {
$result = eF_getTableData("completed_tests ct, tests t", "t.keep_best, t.mastery_score, ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending", "ct.status != 'deleted' and ct.tests_ID = t.id and ct.status != '' and ct.status != 'incomplete'".$timeString." and ct.users_LOGIN in ('$user') $sql");
//$result = eF_getTableData("completed_tests ct, tests t", "ct.users_LOGIN, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending", "ct.status != 'deleted' and ct.tests_ID = t.id and ct.status != '' and ct.status != 'incomplete'".$timeString." and ct.users_LOGIN in ('$user') $sql");
} else if ($test) {
$result = eF_getTableData("completed_tests ct, tests t", "t.keep_best, t.mastery_score, ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending", "ct.status != 'deleted' and ct.tests_ID = t.id and ct.status != '' and ct.status != 'incomplete'".$timeString." and ct.tests_ID=$test $sql");
} else {
$result = eF_getTableData("completed_tests ct, tests t", "t.keep_best, t.mastery_score, ct.id, ct.users_LOGIN, ct.tests_ID, ct.status, ct.timestamp, ct.archive, ct.time_start, ct.time_end, ct.time_spent, ct.score, ct.pending", "ct.status != 'deleted' and ct.tests_ID = t.id and ct.status != '' and ct.status != 'incomplete'".$timeString." $sql");
}
//Unserialize EfrontCompletedTest objects
$testResults = array();
foreach ($result as $value) {
//$value['test'] = unserialize($value['test']);
$testResults[$value['tests_ID']][$value['users_LOGIN']][$value['id']] = $value;
}
//Loop through objects, so that a per lesson/per test array can be constructed, with statistics for each
foreach ($testResults as $testId => $logins) {
$averageScores = array();
foreach ($logins as $user => $doneTests) {
foreach ($doneTests as $doneTestId => $doneTest) {
$testResults[$testId][$user]['scores'][$doneTest['id']] = $doneTest['score'] ? $doneTest['score'] : 0;
$doneTest['archive'] == 0 ? $testResults[$testId][$user]['last_test_id'] = $doneTest['id'] : null;
//$doneTest['test'] = serialize($doneTest['test']);
$testResults[$testId][$user][$doneTestId] = $doneTest;
}
if (!isset($testResults[$testId][$user]['last_test_id'])){
end($testResults[$testId][$user]['scores']);
$testResults[$testId][$user]['last_test_id'] = key($testResults[$testId][$user]['scores']);
}
$testResults[$testId][$user]['average_score'] = round(array_sum($testResults[$testId][$user]['scores']) / sizeof($doneTests), 2);
$testResults[$testId][$user]['max_score'] = max($testResults[$testId][$user]['scores']) ? max($testResults[$testId][$user]['scores']) : 0;
$testResults[$testId][$user]['min_score'] = min($testResults[$testId][$user]['scores']) ? min($testResults[$testId][$user]['scores']) : 0;
$testResults[$testId][$user]['times_done'] = sizeof($doneTests);
if ($doneTest['keep_best']) {
$testResults[$testId][$user]['active_score'] = $testResults[$testId][$user]['max_score'];
$maxScoreId = array_search($testResults[$testId][$user]['max_score'], $testResults[$testId][$user]['scores']);
$testResults[$testId][$user]['active_test_id'] = $maxScoreId;
} else {
$testResults[$testId][$user]['active_score'] = $testResults[$testId][$user]['scores'][$testResults[$testId][$user]['last_test_id']];
$testResults[$testId][$user]['active_test_id'] = $testResults[$testId][$user]['last_test_id'];
}
$averageScores[] = $testResults[$testId][$user]['average_score'];
}
$testResults[$testId]['average_score'] = round(array_sum($averageScores) / sizeof($averageScores), 2);
}
return $testResults;
}
/**
* Get user login time in lessons
*
* This function calculates the total time each student spent in any lesson
* <br/>Example:
* <code>
* EfrontStats :: getUsersTimeAll(); //Get statistics for all users in all lessons
* EfrontStats :: getUsersTimeAll(1259135220, 1259394420); //Get statistics for all users in all lessons from 1259135220 to 1259394420
* </code>
* The results are of the form:
* <code>
* Array
* (
* [3] => Array
* {
* [jdoe] => Array
* (
* [minutes] => 0
* [seconds] => 42
* [hours] => 0
* [total_seconds] => 42
* [accesses] => 5
* )
* }
* )
* </code>
* Accesses are the times the user accessed content on the lesson. total_seconds is the sum of time spent in the lesson in seconds
*
* @param int $fromTimestamp The time to calculate statistics from
* @param int $toTimestamp The time to calculate statistics to
* @param array $lessons The lessons to retrieve statistics for
* @param array $users The users to retrieve statistics for
* @return array The total time per lesson/user
* @since 3.6.0
* @access public
* @static
*/
public static function getUsersTimeAll($fromTimestamp = false, $toTimestamp = false, $lessons = false, $users = false) {
//pr($users);exit;
!$fromTimestamp ? $fromTimestamp = mktime(0, 0, 0, 1, 1, 2000) : null;
!$toTimestamp ? $toTimestamp = time() : null;
!$users || empty($users) ? $usersSql = '' : $usersSql = "users_LOGIN in (\"".implode('","', $users)."\") and";
$result = eF_getTableData("logs", "*", "$usersSql timestamp between $fromTimestamp and $toTimestamp order by timestamp");
foreach ($result as $value) {
$logResults[$value['users_LOGIN']][] = $value;
$resultLessons[$value['lessons_ID']] = $value['lessons_ID'];
}
unset($resultLessons[0]);
if (!$lessons) {
$lessons = $resultLessons;
}
$result = eF_getTableData("logs", "lessons_ID, users_LOGIN, count(id) as accesses", "$usersSql timestamp between $fromTimestamp and $toTimestamp group by lessons_ID, users_LOGIN order by users_LOGIN");
foreach ($result as $value) {
$accessResults[$value['lessons_ID']][$value['users_LOGIN']]= $value['accesses'];
}
$userTimes = array();
foreach ($lessons as $lessonId) {
$users = array_keys($logResults);
foreach ($users as $login) {
$totalTime = array('minutes' => 0, 'seconds' => 0, 'hours' => 0, 'total_seconds' => 0);
$lessonStart = 0;
$inLesson = 0;
if (isset($logResults[$login])) {
foreach ($logResults[$login] as $value) {
if ($inLesson) {
if ($value['timestamp'] - $lessonStart >= 0) {
$interval = eF_convertIntervalToTime($value['timestamp'] - $lessonStart);
} else {
$interval = eF_convertIntervalToTime(0); //This is to avoid negative times
}
if ($interval['hours'] == 0 && $interval['minutes'] <= 30) { //not with $GLOBALS['configuration']['autologout_time'] because it may be changed
$totalTime['minutes'] += $interval['minutes'];
$totalTime['seconds'] += $interval['seconds'];
}
if ($value['lessons_ID'] != $lessonId) {
$inLesson = 0;
} else {
$lessonStart = $value['timestamp'];
}
} else if ($value['lessons_ID'] == $lessonId) {
$inLesson = 1;
$lessonStart = $value['timestamp'];
}
}
}
$sec = $totalTime['seconds'];
if ($sec >= 60) {
$totalTime['seconds'] = $sec % 60;;
$totalTime['minutes'] += floor($sec / 60);;
}
if ($totalTime['minutes'] >= 60) {
$totalTime['hours'] = floor($totalTime['minutes']/60);;
$totalTime['minutes'] = $totalTime['minutes'] % 60;;
}
$totalTime['total_seconds'] = $totalTime['hours'] * 3600 + $totalTime['minutes'] * 60 + $totalTime['seconds'];
$userTimes[$lessonId][$login] = $totalTime;
isset($accessResults[$lessonId][$login]) ? $userTimes[$lessonId][$login]['accesses'] = $accessResults[$lessonId][$login] : $userTimes[$lessonId][$login]['accesses'] = 0;
}
}
return $userTimes;
}
/**
* Get user login time in a lesson
*
* This function calculates the total time each student spent in the lesson
* If $users is not specified, then information on all users is calculated.
* <br/>Example:
* <code>
* EfrontStats :: getUsersTime(3, 'jdoe'); //Get statistics for user jdoe in lesson 3
* EfrontStats :: getUsersTime(3, array('jdoe', 'george')); //Get statistics for users george and jdoe in lesson 3
* EfrontStats :: getUsersTime(3); //Get statistics for all users in lesson 3
* </code>
* The results are of the form:
* <code>
* Array
* (
* [jdoe] => Array
* (
* [minutes] => 0
* [seconds] => 42
* [hours] => 0
* [total_seconds] => 42
* [accesses] => 5
* )
* )
* </code>
* Accesses are the times the user accessed content on the lesson. total_seconds is the sum of time spent in the lesson in seconds
*
* @param int $lesson Either the lesson id or the equivalent EfrontLesson object
* @param mixed $users One or more optional user logins
* @return array The total time per user
* @since 3.5.0
* @access public
* @static
*/
public static function getUsersTime($lesson, $users = false, $fromTimestamp = false, $toTimestamp = false) {
if (!($lesson instanceof EfrontLesson)) {
$lesson = new EfrontLesson($lesson);
}
$lessonId = $lesson -> lesson['id'];
if (!$users) {
$users = array_keys($lesson -> getUsers());
} else if (!is_array($users)) {
$users = array($users);
}
!$fromTimestamp ? $fromTimestamp = mktime(0, 0, 0, 1, 1, 1970) : null;
!$toTimestamp ? $toTimestamp = time() : null;
$result = eF_getTableData("logs", "id, timestamp, lessons_ID, users_LOGIN", "users_LOGIN in (\"".implode('","', $users)."\") and timestamp between $fromTimestamp and $toTimestamp order by timestamp");
foreach ($result as $value) {
$logResults[$value['users_LOGIN']][] = $value;
}
$result = eF_getTableData("logs", "users_LOGIN, count(id) as accesses", "users_LOGIN in (\"".implode('","', $users)."\") and lessons_ID = $lessonId and timestamp between $fromTimestamp and $toTimestamp group by users_LOGIN order by users_LOGIN");
foreach ($result as $value) {
$accessResults[$value['users_LOGIN']]= $value['accesses'];
}
$userTimes = array();
foreach ($users as $login) {
$totalTime = array('minutes' => 0, 'seconds' => 0, 'hours' => 0, 'total_seconds' => 0);
$lessonStart = 0;
$inLesson = 0;
if (isset($logResults[$login])) {
foreach ($logResults[$login] as $value) {
if ($inLesson) {
if ($value['timestamp'] - $lessonStart >= 0) {
$interval = eF_convertIntervalToTime($value['timestamp'] - $lessonStart);
} else {
$interval = eF_convertIntervalToTime(0); //This is to avoid negative times
}
if ($interval['hours'] == 0 && $interval['minutes'] <= 30) { //not with $GLOBALS['configuration']['autologout_time'] because it may be changed
$totalTime['minutes'] += $interval['minutes'];
$totalTime['seconds'] += $interval['seconds'];
}
if ($value['lessons_ID'] != $lessonId) {
$inLesson = 0;
} else {
$lessonStart = $value['timestamp'];
}
} else if ($value['lessons_ID'] == $lessonId) {
$inLesson = 1;
$lessonStart = $value['timestamp'];
}
}
}
$sec = $totalTime['seconds'];
if ($sec >= 60) {
$totalTime['seconds'] = $sec % 60;;
$totalTime['minutes'] += floor($sec / 60);;
}
if ($totalTime['minutes'] >= 60) {
$totalTime['hours'] = floor($totalTime['minutes']/60);;
$totalTime['minutes'] = $totalTime['minutes'] % 60;;
}
$totalTime['total_seconds'] = $totalTime['hours'] * 3600 + $totalTime['minutes'] * 60 + $totalTime['seconds'];
$userTimes[$login] = $totalTime;
isset($accessResults[$login]) ? $userTimes[$login]['accesses'] = $accessResults[$login] : $userTimes[$login]['accesses'] = 0;
}
return $userTimes;
}
/**
* Get user assigned projects in lesson
*
* This function finds the assigned projects to the specified users.
* If $users is not specified, then information on all users
* is calculated. If the project is done, done information is included also.
* <br/>Example:
* <code>
* EfrontStats :: getStudentsAssignedProjects(3, 'jdoe'); //Get statistics for user jdoe in lesson 3
* EfrontStats :: getStudentsAssignedProjects(3, array('jdoe', 'george')); //Get statistics for users george and jdoe in lesson 3
* EfrontStats :: getStudentsAssignedProjects(3); //Get statistics for all users in lesson 3
* </code>
*
* @param mixed $lessons Either a single lesson id or ana array of lesson ids (or EfrontLesson objects)
* @param mixed $users One or more optional user logins
* @return array The assigned projects per user login
* @since 3.5.0
* @access public
* @static
*/
public static function getStudentsAssignedProjects($lessons = false, $users = false) {
if (!$users) {
$users = eF_getTableDataFlat("users", "login");
$users = $users['login'];
}
if (!(is_array($users))) {
$users = array($users);
}
if ($lessons === false) {
$usersAssignedProjects = eF_getTableData("projects p, users_to_projects up", "p.title, p.lessons_ID, up.projects_ID, up.grade, up.upload_timestamp, up.users_LOGIN as login, up.comments", "up.projects_ID = p.id");
} else {
if (!is_array($lessons)) {
$lessons = array($lessons);
}
foreach ($lessons as $key => $lesson) {
$lesson instanceOf EfrontLesson ? $lessons[$key] = $lesson -> lesson['id'] : null;
}
if (sizeof($lessons) > 0) {
$usersAssignedProjects = eF_getTableData("projects p, users_to_projects up", "p.title, p.lessons_ID, up.projects_ID, up.grade, up.upload_timestamp, up.users_LOGIN as login, up.comments", "up.projects_ID = p.id and p.lessons_ID in (".implode(",", $lessons).")");
} else {
$usersAssignedProjects = array();
}
}
$asignedProjects = array();
foreach ($usersAssignedProjects as $project) {
$login = $project['login'];
if (!$users || in_array($login, $users)) {
$asignedProjects[$login][$project['projects_ID']] = $project;
}
}
return $asignedProjects;
}
public static function getUsersForumPosts($lessonId, $users = false) {
//pr($lessonId);
$total_posts = array();
$result = eF_getTableData("f_messages fm, f_topics ft, f_forums ff", "fm.users_LOGIN as login, count(*) as cnt", "fm.f_topics_ID = ft.id and ft.f_forums_ID = ff.id and ff.lessons_ID = ".$lessonId. " group by fm.users_LOGIN");
//pr($resul);
foreach ($result as $data) {
$total_posts[$data['login']] = $data['cnt'];
}
foreach ($users as $login) {
if (!isset($total_posts[$login])) {
$total_posts[$login] = 0;
}
}
return $total_posts;
}
public static function getUsersComments($lessonId, $users = false) {
$total_comments = array();
$result = eF_getTableData("comments cm, content c", "cm.users_LOGIN as login, count(*) as cnt", "cm.content_id = c.id and c.lessons_ID = ".$lessonId. " group by cm.users_LOGIN");
foreach ($result as $data) {
$total_comments[$data['login']] = $data['cnt'];
}
foreach ($users as $login) {
if (!isset($total_comments[$login])) {
$total_comments[$login] = 0;
}
}
return $total_comments;
}
/**
* Get lesson(s) status for user(s)
*
* This function is used to calculate the specified lessons status for the specified users
* It calculates the progress, percentages, scores etc that describe the users' status
* to the lesson.
* <br/>Example:
* <code>
* $status = EfrontStats :: getUsersLessonStatus(34, 'jdoe'); //Get the status for user jdoe in lesson with id 34
* $status = EfrontStats :: getUsersLessonStatus(false, 'jdoe'); //Get the status for user jdoe in all lessons
* $status = EfrontStats :: getUsersLessonStatus(34); //Get the status for all users in lesson with id 34
* $status = EfrontStats :: getUsersLessonStatus(); //Get the status for all users in all lessons
* </code>
* Note: This function is designed so that there is never the need to call it inside a loop
* Since it is database-intensive, make sure that it is NEVER called inside a loop!
*
* @param array $lessons an array of lesson ids or EfrontLesson objects
* @param array $users An array of user logins
* @return array The lesson status
* @since 3.5.0
* @access public
*/
public static function getUsersLessonStatus($lessons = false, $users = false, $options = array()) {
if ($lessons === false) {
$result = eF_getTableDataFlat("lessons", "id");
$lessons = $result['id'];
} else if (!is_array($lessons)) {
$lessons = array($lessons);
}
if ($users != false) {
!is_array($users) ? $users = array($users) : null; //Convert single login to array
} else {
$users = eF_getTableDataFlat("users", "login", "user_type != 'administrator'");
$users = $users['login'];
}
foreach ($lessons as $lesson) {
foreach ($users as $user) {
if ($lesson instanceOf EfrontLesson) {
$lessonId = $lesson -> lesson['id'];
} else {
$lessonId = $lesson;
}
$lessonStatus[$lessonId][$user] = self :: getUserLessonStatus($lesson, $user, $options);
}
}
return $lessonStatus;
}
/**
* Get user(s) status in course(s)
*
* This function is used to calculate the user's status in the course, ie the score, completed
* etc. It also calculates statistics for all lessons inside the course
* <br>Example:
* <code>
* $status = EfrontStats :: getUsersCourseStatus(34, 'jdoe'); //Get the status for user jdoe in course with id 34
* $status = EfrontStats :: getUsersCourseStatus(false, 'jdoe'); //Get the status for user jdoe in all courses
* $status = EfrontStats :: getUsersCourseStatus(34); //Get the status for all users in course with id 34
* $status = EfrontStats :: getUsersCourseStatus(); //Get the status for all users in all courses
*</code>
* Note: This function is designed so that there is never the need to call it inside a loop
* Since it is database-intensive, make sure that it is NEVER called inside a loop!
*
* @param mixed $courses an array of course ids or EfrontCourse objects
* @param mixed $users an array of users logins
* @return array The user status in courses
* @since 3.5.0
* @access public
*/