-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcourse.class.php
More file actions
4473 lines (3988 loc) · 172 KB
/
Copy pathcourse.class.php
File metadata and controls
4473 lines (3988 loc) · 172 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
/**
* File for courses
*
* @package eFront
*/
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
/**
* Course exceptions
*
* This class extends Exception to provide the exceptions related to courses
* @package eFront
* @since 3.5.0
*
*/
class EfrontCourseException extends Exception
{
const COURSE_NOT_EXISTS = 251;
const INVALID_ID = 252;
const MAX_USERS_LIMIT = 253;
const DATABASE_ERROR = 254;
const INVALID_PARAMETER = 255;
const PARTIAL_IMPORT = 256;
const INVALID_USER_TYPE = 257;
const COURSE_NOT_EMPTY = 258;
const GENERAL_ERROR = 299;
const INVALID_LOGIN = 300;
}
/**
* This class represents a course in eFront
*
* @package eFront
* @since 3.5.0
*/
class EfrontCourse
{
/**
* The maximum length of a course name
*
* @var string
* @since 3.6.1
*/
const MAX_NAME_LENGTH = 150;
/**
* The limit for a mass operation, such as adding users to a course
*
* @var int
* @since 3.6.3
*/
const MAX_MASS_OPERATION_SIZE = 50;
/**
* The course variable
*
* @since 3.5.0
* @var array
* @access public
*/
public $course = array();
/**
* The course users
*
* @since 3.5.0
* @var array
* @access public
*/
public $users = false;
/**
* The course rules
*
* @since 3.5.0
* @var array
* @access public
*/
public $rules = array();
/**
* Default course options
*
* @since 3.5.2
* @var array
* @access public
*/
public $options = array(
'recurring' => 0,
'recurring_duration' => 0,
'auto_complete' => 1,
'auto_certificate' => 0,
'certificate' => '',
'certificate_tpl_id' => 0,
'certificate_tpl_id_rtf' => 0,
'certificate_export_method' => 'xml',
//'course_code' => '',
'duration' => 0,
'training_hours' => '',
'start_date' => '',
'end_date' => ''
);
/**
* Initialize course
*
* This function creates the course instance based on the
* given course id.
* <br/>Example:
* <code>
* $course = new EfrontCourse(5); //create object for course with id 5
* </code>
*
* @param mixed $course The course id or the course array
* @since 3.5.0
* @access public
*/
function __construct($source) {
$this -> initializeDataFromSource($source);
$this -> initializeRules();
$this -> initializeOptions();
$this -> buildPriceString();
}
/**
* Initialize course data based on the passed parameter. If the parameter is an id, then
* a db query takes place in order to retrive course values. Otherwise, if it's an array
* it is used for the course values.
*
* @param mixed $course A course id or an array with course values
* @since 3.6.1
* @access private
*/
private function initializeDataFromSource($source) {
if (is_array($source)) {
$this -> course = $source;
} elseif (!$this -> validateId($source)) {
throw new EfrontCourseException(_INVALIDID, EfrontCourseException :: INVALID_ID);
} else {
$source = eF_getTableData("courses", "*", "id = $source");
if (empty($source)) {
throw new EfrontCourseException(_COURSEDOESNOTEXIST, EfrontCourseException :: COURSE_NOT_EXISTS);
}
$this -> course = $source[0];
}
if (!$this -> course['directions_ID']) {
$this -> setCategoryId();
}
}
/**
* Set a category (direction) id, in case it's missing.
* If this course is an instance, set it to be the same as the originating course. Otherwise,
* set it to be the first active category
*
* @since 3.6.3
* @access private
*/
private function setCategoryId() {
if ($this -> course['instance_source']) {
$parentCourse = new EfrontCourse($this -> course['instance_source']);
if ($parentCourse -> course['directions_ID']) {
$this -> course['directions_ID'] = $parentCourse -> course['directions_ID'];
$this -> persist();
}
} else {
$result = eF_getTableData("directions", "id", "active=1", "", "", 1);
if (!empty($result)) {
$this -> course['directions_ID'] = $result[0]['id'];
$this -> persist();
}
}
}
/**
* Initialize course rules, by unserializing the stored rules array
*
* @since 3.6.1
* @access private
*/
private function initializeRules() {
$this -> validateSerializedArray($this -> course['rules']) OR $this -> course['rules'] = $this -> sanitizeSerialized($this -> course['rules']);
$this -> rules = unserialize($this -> course['rules']);
}
/**
* Initialize course options, by unserializing the stored options array
*
* @since 3.6.1
* @access private
*/
private function initializeOptions() {
$this -> validateSerializedArray($this -> course['options']) OR $this -> course['options'] = $this -> sanitizeSerialized($this -> course['options']);
$options = unserialize($this -> course['options']);
$newOptions = array_diff_key($this -> options, $options); //$newOptions are course options that were added to the EfrontCourse object AFTER the lesson options serialization took place
$this -> options = $options + $newOptions; //Set course options
}
/**
* Build the price string. This function takes the course price and
* creates a human-readable version, based on whether it is a one-time
* or a recurring price
*
* @since 3.6.1
* @access private
*/
private function buildPriceString() {
if ($this -> validateFloat($this -> course['price'])) { //Create the string representing the course price
$this -> options['recurring'] ? $recurring = array($this -> options['recurring'], $this -> options['recurring_duration']) : $recurring = false;
$this -> course['price_string'] = formatPrice($this -> course['price'], $recurring);
} else {
$this -> course['price_string'] = formatPrice(0);
}
}
/**
* Check whether the course should display in the catalog
*
* This function returns false if this course should not appear in the catalog at all.
* Otherwise, it returns the id to which the catalog entry should point
*
* @return mixed Either false or the id of the course that the catalog entry points to
* @since 3.6.3
* @access public
*/
public function shouldDisplayInCatalog() {
if ($this -> course['show_catalog']) {
return $this -> course['id'];
} else {
$instances = $this -> getInstances();
if (!empty($instances)) {
foreach ($instances as $instance) {
if ($instance -> course['show_catalog']) {
return $instance -> course['id'];
}
}
} else {
return false;
}
}
}
/**
* Return an array of EfrontLesson objects that belong to this course, based
* on the specified constraints
*
* @param array $constraints Database constraints
* @return array The course lessons
* @since 3.6.3
* @access public
*/
public function getCourseLessons($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertLessonConstraintsToSqlParameters($constraints);
$from = "lessons_to_courses lc, lessons l";
$where[] = "l.archive = 0 and l.course_only=1 and l.id=lc.lessons_ID and courses_ID=".$this -> course['id'];
$result = eF_getTableData($from, "lc.start_date, lc.end_date, lc.start_period, lc.end_period, lc.previous_lessons_ID, l.*",
implode(" and ", $where), $orderby, false, $limit);
$result = $this -> sortLessons($result);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontCourse :: convertDatabaseResultToLessonObjects($result);
} else {
return EfrontCourse :: convertDatabaseResultToLessonArray($result);
}
}
/**
* Count the number of lessons in the course, based on the specified constraints
*
* @param array $constraints Database constraints
* @return int The total course lessons
* @since 3.6.3
* @access public
*/
public function countCourseLessons($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertLessonConstraintsToSqlParameters($constraints);
$from = "lessons_to_courses lc, lessons l";
$where[] = "l.archive = 0 and l.course_only=1 and l.id=lc.lessons_ID and courses_ID=".$this -> course['id'];
$result = eF_countTableData($from, "l.id",
implode(" and ", $where));
return $result[0]['count'];
}
/**
* Experimental addition based on sorted table
*/
public function addCourseLessons($constraints = array()) {
$lessons = $this -> getCourseLessonsIncludingUnassigned($constraints);
$this -> addLessons($lessons);
}
/**
* Experimental removal based on sorted table
*/
public function removeCourseLessons($constraints = array()) {
$lessons = $this -> getCourseLessons($constraints);
$this -> removeLessons($lessons);
}
/**
* Return an array of EfrontLesson objects, based on the specified constraints. If any of the lessons
* is part of the course, then it has an extra field 'has_lesson' set to 1
*
* @param array $constraints Database constraints
* @return array All the lessons, with course lessons having has_lesson=1
* @since 3.6.3
* @access public
*/
public function getCourseLessonsIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertLessonConstraintsToSqlParameters($constraints);
$from = "lessons l left outer join (select lessons_ID from lessons_to_courses where courses_ID='".$this -> course['id']."') r on l.id=r.lessons_ID ";
$select = "l.*, r.lessons_ID is not null as has_lesson";
$where[] = "l.course_only=1";
$result = eF_getTableData($from, $select, implode(" and ", $where), $orderby, false, $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontCourse :: convertDatabaseResultToLessonObjects($result);
} else {
return EfrontCourse :: convertDatabaseResultToLessonArray($result);
}
}
/**
* Count the number of lessons in the course, based on the specified constraints, including unassigned
*
* @param array $constraints Database constraints
* @return int The total course lessons
* @since 3.6.3
* @access public
*/
public function countCourseLessonsIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertLessonConstraintsToSqlParameters($constraints);
$from = "lessons l left outer join (select lessons_ID from lessons_to_courses where courses_ID='".$this -> course['id']."') r on l.id=r.lessons_ID ";
$select = "l.id";
$where[] = "l.course_only=1";
$result = eF_countTableData($from, $select, implode(" and ", $where));
return $result[0]['count'];
}
/**
* Get the schedule for this lesson, in this course
*
* @param mixed $lesson The lesson to get the schedule for
* @return array The lesson's schedule in the course
* @since 3.6.3
* @access public
*/
public function getLessonScheduleInCourse($lesson) {
$lesson = EfrontLesson::convertArgumentToLessonObject($lesson);
$result = eF_getTableData("lessons_to_courses", "start_date, end_date, start_period, end_period", "courses_ID=".$this -> course['id']." and lessons_ID=".$lesson -> lesson['id']);
return $result[0];
}
/**
* Set the schedule for this lesson, in this course.
*
* @param mixed $lesson The lesson to set schedule for
* @param int $fromTimestamp A timestamp indicating when the lesson starts
* @param int $toTimestamp A timestamp indicating when the lesson ends
* @since 3.6.3
* @access public
*/
public function setLessonScheduleInCourse($lesson, $fromTimestamp, $toTimestamp, $startPeriod, $endPeriod) {
$lesson = EfrontLesson::convertArgumentToLessonObject($lesson);
if ($startPeriod && $endPeriod && !$fromTimestamp && !$toTimestamp) {
$fields = array("start_date" => NULL, "end_date" => NULL, "start_period" => $startPeriod, "end_period" => $endPeriod);
} else {
$fields = array("start_date" => $fromTimestamp, "end_date" => $toTimestamp, "start_period" => NULL, "end_period" => NULL);
}
$where = "courses_ID=".$this -> course['id']." and lessons_ID=".$lesson -> lesson['id'];
self::persistCourseLessons($fields, $where);
}
/** Unset any schedule set for this lesson, in this course
*
* @param mixed $lesson The lesson to get the schedule for
* @return array The lesson's schedule in the course
* @since 3.6.3
* @access public
*/
public function unsetLessonScheduleInCourse($lesson) {
$lesson = EfrontLesson::convertArgumentToLessonObject($lesson);
$fields = array("start_date" => null, "end_date" => null, "start_period" => null, "end_period" => null);
$where = "courses_ID=".$this -> course['id']." and lessons_ID=".$lesson -> lesson['id'];
self::persistCourseLessons($fields, $where);
}
/**
* Sort course lessons, based on the succession built-in the database, if any
*
* @param array $result The course lessons array, as retrieved from the database
* @return array The course lessons array, sorted accordingly and with lesson ids as keys
* @since 3.6.1
* @access private
*/
private function sortLessons($result) {
$previous = 0; //Previous is only used when no previous_lessons_ID is set
$courseLessons = $previousValues = array();
foreach ($result as $value) {
$courseLessons[$value['id']] = $value;
$previousValues[$value['id']] = $value['previous_lessons_ID'];
$value['previous_lessons_ID'] !== false ? $previousLessons[$value['previous_lessons_ID']] = $value : $previousLessons[$previous] = $value;
$previous = $value['id'];
}
if (array_sum($previousValues)) { //The special case where all previous values are 0, which is checked by array_sum, means that there is no specific ordering
//Sorting algorithm, based on previous_lessons_ID. The algorithm is copied from EfrontContentTree :: reset() and is the same with the one applied for content. It is also used in questions order
$node = $count = 0;
$nodes = array(); //$count is used to prevent infinite loops
while (sizeof($previousLessons) > 0 && isset($previousLessons[$node]) && $count++ < 1000) {
$nodes[$previousLessons[$node]['id']] = $previousLessons[$node];
$newNode = $previousLessons[$node]['id'];
unset($previousLessons[$node]);
$node = $newNode;
}
if (sizeof($nodes) != sizeof($courseLessons)) { //If the ordering is messed up for some reason.
$nodes = $courseLessons;
$fields = array("previous_lessons_ID" => 0);
$where = "courses_ID=".$this -> course['id'];
self::persistCourseLessons($fields, $where);
}
} else {
$nodes = $courseLessons;
}
return $nodes;
}
/**
* Add lessons to course
*
* This function is used to add lessons to the current course
* <br/>Example:
* <code>
* $course -> addLessons(4); //Add lesson with id 4
* $course -> addLessons(array(4,5,6)); //Add lessons with ids 4,5,6
* </code>
*
* @param mixed $lessons Either a single lesson id, or an array of ids
* @return array The new list of course lessons
* @since 3.5.0
* @access public
*/
public function addLessons($lessons) {
$lessonObjects = $this -> verifyLessonsList($lessons);
$lastLessonId = $this -> getCourseLastLesson();
$courseUsers = $this -> getUsers();
$result = eF_getTableDataFlat("lessons_to_courses", "lessons_ID", "courses_ID=".$this -> course['id']); //We don't call getCourseLessons() because we need all and every lesson, so this is faster
foreach ($lessonObjects as $key => $lesson) {
if (!in_array($key, $result['lessons_ID'])) {
eF_insertTableData("lessons_to_courses", array('courses_ID' => $this -> course['id'],
'lessons_ID' => $key,
'previous_lessons_ID' => $lastLessonId));
$this -> addCourseUsersToLesson($lesson, $courseUsers);
if (G_VERSIONTYPE == 'educational') { #cpp#ifdef EDUCATIONAL
$this -> addLessonQuestionsToCourseSkill($lesson);
} #cpp#endif
$lastLessonId = $key;
}
}
return EfrontCourse::convertLessonObjectsToArrays($this -> getCourseLessons());
}
/**
* Remove lessons from course
*
* This function is used to reove lessons from the current course
* <br/>Example:
* <code>
* $course -> removeLessons(4); //Remove lesson with id 4
* $course -> removeLessons(array(4,5,6)); //Remove lessons with ids 4,5,6
* </code>
*
* @param mixed $lessons Either a single lesson id, or an array of ids
* @return array The new list of course lessons
* @since 3.5.0
* @access public
*/
public function removeLessons($lessons) {
$lessonObjects = $this -> verifyLessonsList($lessons);
$previousLessons = $this -> getPreviousLessonsInCourse();
$lessonsToCourses = $this -> countLessonsOccurencesInCourses();
foreach ($lessonObjects as $key => $lesson) {
$this -> removeLessonFromCourseRules($lesson);
if ($lessonsToCourses[$key] == 1) { //Meaning that this lesson was only related to this course
$lesson -> archiveLessonUsers(array_keys($this -> getUsers()));
}
if (G_VERSIONTYPE == 'educational') { #cpp#ifdef EDUCATIONAL
$this -> removeLessonQuestionsFromCourseSkill($lesson);
} #cpp#endif
eF_deleteTableData("lessons_to_courses", "courses_ID=".$this -> course['id']." and lessons_ID=".$key);
$fields = array("previous_lessons_ID" => $previousLessons[$key]);
$where = "courses_ID=".$this -> course['id']." and previous_lessons_ID=$key";
self::persistCourseLessons($fields, $where);
}
return EfrontCourse::convertLessonObjectsToArrays($this -> getCourseLessons());
}
/**
* This function removes a lesson from the course's rules, which are rather complicated
*
* @param mixed $lesson The lesson to remove from course rules
* @since 3.6.3
* @access private
* @todo: Simplify course rules implementation
*/
private function removeLessonFromCourseRules($lesson) {
$lesson = EfrontLesson::convertArgumentToLessonObject($lesson);
unset($this -> rules[$lesson]); //Unset rules that have this lesson as source
foreach ($this -> rules as $id => $rule) {
foreach ($rule['lesson'] as $key => $value) {
if ($value == $lesson) {
unset($rule['lesson'][$key]);
unset($rule['condition'][$key+1]);
}
if (sizeof($rule['lesson']) == 0) {
unset($this -> rules[$id]);
} else {
if (sizeof($rule['condition']) == 0) {
unset($rule['condition']);
}
$this -> rules[$id] = $rule;
}
}
if ($this -> rules[$id]) {
$this -> rules[$id]['lesson'] = array_values($this -> rules[$id]['lesson']);
array_unshift($this -> rules[$id]['lesson'], 0);
unset($this -> rules[$id]['lesson'][0]);
if ($this -> rules[$id]['condition']) {
$this -> rules[$id]['condition'] = array_values($this -> rules[$id]['condition']);
array_unshift($this -> rules[$id]['condition'], 0);
array_unshift($this -> rules[$id]['condition'], 0);
unset($this -> rules[$id]['condition'][0]);
unset($this -> rules[$id]['condition'][1]);
}
}
}
$this -> persist();
}
/**
* For each of the course's lesson, get its previous
*
* @return array The id of the previous lesson, for each lesson in the course
* @since 3.6.1
* @access private
*/
private function getPreviousLessonsInCourse() {
$courseLessons = $this -> getCourseLessons();
foreach ($courseLessons as $id => $lesson) {
$previousLessons[$id] = $lesson->lesson['previous_lessons_ID'];
}
return $previousLessons;
}
/**
* Count how many occurences each lesson has in courses
*
* @return array The number of occurences in courses for each lesson
* @since 3.6.1
* @access private
*/
private function countLessonsOccurencesInCourses() {
$result = eF_getTableDataFlat("lessons_to_courses lc", "lc.lessons_ID, count(lc.lessons_ID)", "", "", "lc.lessons_ID");
$lessonsToCourses = array_combine($result['lessons_ID'], $result['count(lc.lessons_ID)']);
return $lessonsToCourses;
}
/**
* Get the id of the last lesson in the course
*
* @return int The last lesson id
* @since 3.6.1
* @access private
*/
private function getCourseLastLesson() {
$lastLesson = end($this -> getCourseLessons());
if ($lastLesson) {
$lastLessonId = $lastLesson->lesson['id'];
} else {
$lastLessonId = 0;
}
return $lastLessonId;
}
/**
* Verify the integrity of the lessons list and convert each one to
* an object, if it isn't already. The returned array has the lesson ids
* as keys.
*
* @param mixed $lessons An array of lesson objects or lesson ids, or a single lesson, or a single lesson object
* @return array The array of lesson objects, where keys are ids
* @since 3.6.1
* @access private
*/
private function verifyLessonsList($lessonsList) {
is_array($lessonsList) OR $lessonsList = array($lessonsList);
$newLessonsList = array();
foreach ($lessonsList as $lesson) {
($lesson instanceof EfrontLesson) OR $lesson = new EfrontLesson($lesson);
$newLessonsList[$lesson -> lesson['id']] = $lesson;
}
return $newLessonsList;
}
/**
* Verify the integrity of the course list and convert each one to
* an object, if it isn't already. The returned array has the courses ids
* as keys.
*
* @param mixed $courses An array of course objects, course ids, or arrays with course values (or single versions of all these)
* @return array The array of course objects, where keys are ids
* @since 3.6.1
* @access public
* @static
*/
public static function verifyCoursesList($coursesList) {
is_array($coursesList) OR $coursesList = array($coursesList);
$newCoursesList = array();
foreach ($coursesList as $course) {
($course instanceof EfrontCourse) OR $course = new EfrontCourse($course);
$newCoursesList[$course -> course['id']] = $course;
}
return $newCoursesList;
}
/**
* Add this course's users to the specified lesson
*
* @param EfrontLesson $lesson The lesson to add users to
* @since 3.6.1
* @access private
*/
private function addCourseUsersToLesson($lesson, $usersToAdd = false, $confirmed = true) {
if (!$usersToAdd) {
$usersToAdd = $this -> getUsers();
}
$users = $roles = array();
foreach ($usersToAdd as $login => $user) {
if ($user['user_type'] != 'administrator') {
$users[] = $login;
$roles[] = $user['role'];
}
}
$lesson -> addUsers($users, $roles, $confirmed);
}
/**
* Insert the skill corresponding to this course: Every course is mapped to a skill like "Knowledge of that course"
* This insertion takes place when a course is changed from course_only to regular course
*
* <br/>Example:
* <code>
* $course -> insertCourseSkill();
* </code>
*
* @return the id of the newly created record in the module_hcd_course_offers_skill table or false if something went wrong
* @since 3.6.2
* @access public
*/
public function insertCourseSkill() {
// If insertion of a self-contained course add the corresponding skill
// Insert the corresponding course skill to the skill and course_offers_skill tables
$courseSkillId = eF_insertTableData("module_hcd_skills", array("description" => _KNOWLEDGEOFCOURSE . " ". $this -> course['name'], "categories_ID" => -1));
// Insert question to course skill records for all course questions
$questions = eF_getTableData("questions", "id", "lessons_ID in ('". implode("','", array_keys($this->getCourseLessons())) . "')");
$insert_string = "";
foreach ($questions as $question) {
if ($insert_string != "") {
$insert_string .= ",('" . $question['id']. "','" . $courseSkillId . "',2)";
} else {
$insert_string .= "('".$question['id']."','".$courseSkillId."',2)";
}
}
if ($insert_string != "") {
eF_executeNew("INSERT INTO questions_to_skills VALUES " . $insert_string);
}
return eF_insertTableData("module_hcd_course_offers_skill", array("courses_ID" => $this -> course['id'], "skill_ID" => $courseSkillId));
}
/**
* Get the skill corresponding to this course: Every course is mapped to a skill like "Knowledge of that course"
*
* <br/>Example:
* <code>
* $course_skill = $course -> getcourseSkill();
* </code>
*
* @return An array of the form [skill_ID] => [courses_ID, description, specification,skill_ID,categories_ID]
* @since 3.5.2
* @access public
*/
public function getCourseSkill() {
if (G_VERSIONTYPE == 'educational') { #cpp#ifdef EDUCATIONAL
$skills = $this -> getSkills();
foreach ($skills as $skid=>$skill) {
if ($skill['courses_ID'] == $this -> course['id'] && $skill['categories_ID'] == -1) {
return $skill;
}
}
// The default lesson skill was not found
return $this -> insertCourseSkill();
} #cpp#endif
return false;
}
/**
* Add the lesson's questions to the course's skill
*
* @param EfrontLesson $lesson The lesson to retrieve questions for
* @since 3.6.1
* @access private
*/
private function addLessonQuestionsToCourseSkill($lesson) {
$lessonQuestions = eF_getTableDataFlat("questions", "id", "lessons_ID = ". $lesson ->lesson['id']);
$courseSkill = $this -> getCourseSkill();
// Get course specific skill
foreach ($lessonQuestions['id'] as $questionId) {
$fields[] = array("questions_id" => $questionId,
"skills_ID" => $courseSkill['courses_ID'],
"relevance" => 2);
}
eF_insertTableDataMultiple("questions_to_skills", $fields);
}
/**
* Remove the lesson's questions from the course's skill
*
* @param EfrontLesson $lesson The lesson to retrieve questions for
* @since 3.6.1
* @access private
*/
private function removeLessonQuestionsFromCourseSkill($lesson) {
$lessonQuestions = eF_getTableDataFlat("questions", "id", "lessons_ID = ". $lesson ->lesson['id']);
$courseSkill = $this -> getCourseSkill();
if (!empty($lessonQuestions['id'])) {
eF_deleteTableData("questions_to_skills", "questions_id IN ('".implode("','",$lessonQuestions['id'])."') AND skills_ID = ".$courseSkill['skill_ID']);
}
}
/**
* Get course users
*
* This function is used to retrieve a list with the users
* that have this course, along with their declared type
* <br/>Example:
* <code>
* $course -> getUsers();
* </code>
*
* @param boolean $returnObjects Whether to return EfrontUser Objects
* @return array An array of users or EfrontUser objects
* @since 3.5.0
* @access public
* @todo: Replace with getCourseUsersXXX()
*/
public function getUsers($returnObjects = false, $constraints = array()) {
if ($this -> users === false) {
$this -> initializeUsers($constraints);
}
if ($returnObjects) {
$users = array();
if (is_array($this -> users)) {
foreach ($this -> users as $key => $user) {
$users[$key] = EfrontUserFactory :: factory($key);
$users[$key] -> user = array_merge($users[$key]->user, $user); }
}
return $users;
} else {
return $this -> users;
}
}
/**
* Get the course users that are students
*
* This function returns all the course users that their role is a student role
*
* @param boolean $returnObjects Whether to return objects
* @return mixed An array of users or EfrontUser objects
* @since 3.6.1
* @access public
* @todo: Replace with getCourseUsersXXX()
*/
public function getStudentUsers($returnObjects = false, $constraints = array()) {
$courseUsers = $this -> getUsers($returnObjects, $constraints) OR $courseUsers = array();
foreach ($courseUsers as $key => $value) {
if ($value instanceOf EfrontUser) {
$value = $value -> user;
}
if (!EfrontUser::isStudentRole($value['role'])) {
unset($courseUsers[$key]);
}
}
return $courseUsers;
}
/**
* Get the course users that are professors
*
* This function returns all the course users that their role is a professor role
*
* @param boolean $returnObjects Whether to return objects
* @return mixed An array of users or EfrontUser objects
* @since 3.6.1
* @access public
* @todo: Replace with getCourseUsersXXX()
*/
public function getProfessorUsers($returnObjects = false, $constraints = array()) {
$courseUsers = $this -> getUsers($returnObjects, $constraints) OR $courseUsers = array();
foreach ($courseUsers as $key => $value) {
if ($value instanceOf EfrontUser) {
$value = $value -> user;
}
if (!EfrontUser::isProfessorRole($value['role'])) {
unset($courseUsers[$key]);
}
}
return $courseUsers;
}
/**
* Check if the specified user has a 'student' role in the course
*
* @param mixed $user a login or an EfrontUser object
* @return boolean True if the user's role in the course is 'student'
* @since 3.6.1
* @access public
* @todo: Replace with getCourseUsersXXX()
*/
public function isStudentInCourse($user) {
if ($user instanceOf EfrontUser) {
$user = $user -> user['login'];
}
$roles = $this -> getPossibleCourseRoles();
$courseUsers = $this -> getUsers();
if (in_array($user, array_keys($courseUsers)) && $roles[$courseUsers[$user]['role']] == 'student') {
return true;
} else {
return false;
}
}
/**
* Check if the specified user has a 'professor' role in the course
*
* @param mixed $user a login or an EfrontUser object
* @return boolean True if the user's role in the course is 'professor'
* @since 3.6.1
* @access public
* @todo Implement using getCourseUsersXXX()
*/
public function isProfessorInCourse($user) {
if ($user instanceOf EfrontUser) {
$user = $user -> user['login'];
}
$roles = $this -> getPossibleCourseRoles();
$courseUsers = $this -> getUsers();
if (in_array($user, array_keys($courseUsers)) && $roles[$courseUsers[$user]['role']] == 'professor') {
return true;
} else {
return false;
}
}
/**
* Get course users based on the specified constraints, but display results for the mother course only, in case the course has instances
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontUser objects
* @since 3.6.2
* @access public
*/
public function getCourseUsersAggregatingResults($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$from = "(users u, (select uc.user_type as role,uc.score,uc.completed,uc.users_LOGIN,uc.to_timestamp, uc.from_timestamp as active_in_course, uc.from_timestamp as enrolled_on from courses c left outer join users_to_courses uc on uc.courses_ID=c.id where (c.id=".$this -> course['id']." or c.instance_source=".$this -> course['id'].") and uc.archive=0) r)";
$from = EfrontCourse :: appendTableFiltersUserConstraints($from, $constraints);
$where[] = "u.login=r.users_LOGIN";
$select = "u.*, max(score) as score, max(completed) as completed, max(to_timestamp) as to_timestamp, max(role) as role, 1 as has_course, max(active_in_course) as active_in_course, max(enrolled_on) as enrolled_on";
$groupby = "r.users_LOGIN";
/*
if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE
$from .= " left outer join module_hcd_employees e on e.users_LOGIN=u.login";
//$where[] = "e.users_LOGIN=u.login";
$select .= ",e.*, e.users_LOGIN as has_hcd";
} #cpp#endif
*/
$result = eF_getTableData($from, $select, implode(" and ", $where), $orderby, $groupby, $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontUser :: convertDatabaseResultToUserObjects($result);
} else {
return EfrontUser :: convertDatabaseResultToUserArray($result);
}
}
/**
* Count course users based on the specified constraints, but display results for the mother course only, in case the course has instances
*
* @param array $constraints The constraints for the query
* @return int Total entries
* @since 3.6.2
* @access public
*/
public function countCourseUsersAggregatingResults($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$from = "(users u, (select uc.score,uc.completed,uc.users_LOGIN,uc.to_timestamp, uc.from_timestamp as active_in_course from courses c left outer join users_to_courses uc on uc.courses_ID=c.id where (c.id=".$this -> course['id']." or c.instance_source=".$this -> course['id'].") and uc.archive=0) r)";
$from = EfrontCourse :: appendTableFiltersUserConstraints($from, $constraints);
$where[] = "u.login=r.users_LOGIN";
$select = "u.login";
$groupby = "r.users_LOGIN";
$result = eF_countTableData($from, $select, implode(" and ", $where), false, $groupby);
return $result[0]['count'];
}
/**
* Get course users based on the specified constraints
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontUser objects
* @since 3.6.2
* @access public
*/
public function getCourseUsers($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$select = "u.*, uc.courses_ID,uc.completed,uc.score,uc.user_type as role,uc.from_timestamp as active_in_course, uc.to_timestamp, uc.comments, uc.issued_certificate, 1 as has_course";
$where[] = "u.login=uc.users_LOGIN and uc.courses_ID='".$this -> course['id']."' and uc.archive=0";
$from = "users_to_courses uc,users u";
$from = EfrontCourse :: appendTableFiltersUserConstraints($from, $constraints);
$result = eF_getTableData($from, $select, implode(" and ", $where), $orderby, false, $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {