-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathgroup.class.php
More file actions
1122 lines (1015 loc) · 44.8 KB
/
Copy pathgroup.class.php
File metadata and controls
1122 lines (1015 loc) · 44.8 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 groups
*
* @package eFront
*/
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
/**
* EfrontGroupException class
*
* This class extends Exception class and is used to issue errors regarding groups
* @package eFront
* @author Antonellis Panagiotis
* @version 1.0
*/
class EfrontGroupException extends Exception
{
const NO_ERROR = 0;
const GROUP_NOT_EXISTS = 301;
const INVALID_ID = 302;
const INVALID_USER = 303;
const USER_NOT_EXISTS = 304;
const USER_ALREADY_MEMBER = 305;
const ASSIGNMENT_ERROR = 306;
const GROUPKEYEXISTS = 307;
const INVALID_TYPE = 308;
}
/**
* EfrontGroup class
*
* This class represents a group
* @package eFront
* @author Antonellis Panagiotis
* @version 1.0
*/
class EfrontGroup
{
/**
* The group array.
*
* @since 3.5.0
* @var array
* @access protected
*/
public $group = array();
/**
* The group users. Calling getUsers() initializes it; otherwise, it evaluates to false.
*
* @since 3.5.0
* @var array
* @access protected
*/
protected $users = false;
/*
* The group lessons. Lessons correlated with this group can be either directly assigned to
* all users of the group or be automatically assigned to every user joining the group
*/
protected $lessons = false;
/*
* The group courses. Lessons correlated with this group can be either directly assigned to
* all users of the group or be automatically assigned to every user joining the group
*/
protected $courses = false;
/**
* Class constructor
*
* This function is used to instantiate the class. The instatiation is done
* based on a group id. If an entry with this id is not found in the database,
* an EfrontGroupException is thrown.
* <br/>Example:
* <code>
* $group = new EfrontGroup(32); //32 is a group id
* </code>
*
* @param int $group_id The group id or array with group's info array
* @since 3.5.0
* @access public
*/
function __construct($group_id) {
if (is_array($group_id)) {
$group[0] = $group_id;
} else {
if (!eF_checkParameter($group_id, 'id')) {
throw new EfrontGroupException(_INVALIDID.": $group_id", EfrontGroupException :: INVALID_ID);
}
$group = eF_getTableData("groups", "*", "id = $group_id");
}
if (sizeof($group) == 0) {
throw new EfrontGroupException(_GROUPDOESNOTEXIST, EfrontGroupException :: GROUP_NOT_EXISTS);
} else {
$this -> group = $group[0];
}
}
/**
* Create group
*
* This function creates a new group. This involves creating
* the database instance..
* The function argument is an array with field values, corresponding to database
* fields. All fields are optional, and if absent they are filled with default values,
* but 'name' is strongly recommended to be defined
* <br/>Example:
* <code>
* $fields = array('name' => 'Test group', 'description' => 'Description of the group');
* try {
* $newGroup = EfrontGroup :: create($fields); //$newgroup is now a new group object
* } catch (Exception $e) {
* echo $e -> getMessage();
* }
* </code><br/>
*
* @param array $fields The new group characteristics
* @return EfrontGroup the new group object
* @since 3.5.0
* @access public
* @static
*/
public static function create($fields) {
$fields['name'] = trim($fields['name']);
!isset($fields['name']) ? $fields['name'] = 'Default name' : null;
$result = eF_getTableData("groups", "id", "unique_key != '' and unique_key is not null and unique_key='".$fields['unique_key']."'");
if (sizeof($result) == 0) {
$group_id = eF_insertTableData("groups", $fields); //Insert the group to the database
$newGroup = new EfrontGroup($group_id);
return $newGroup;
} else {
throw new EfrontGroupException(_GROUPKEYEXISTS, EfrontGroupException :: GROUPKEYEXISTS);
}
}
/**
* Create a dynamic group
* This function creates a dynamic group, which is an unnammed group with limited lifespan and hidden from users
* @param array $fields the fields for the group
* @return EfrontGroup The EfrontGroup object of the newly created dynamic group
* @since 3.6.6
* @access public
* @static
*/
public static function createDynamicGroup($fields) {
self :: deleteDynamicGroup();
$group = self::create($fields); //Insert the group to the database
return $group;
}
/**
* Delete a dynamic group
* This function deletes a dynamic group
* @param array $fields the fields for the group
* @since 3.6.6
* @access public
* @static
* @see EfrontGroup::createDynamicGroup
*/
public static function deleteDynamicGroup($group = false) {
if ($group) {
eF_deleteTableData("groups", "id=".$_SESSION['dynamic_group']);
eF_deleteTableData("users_to_groups", "groups_id=".$_SESSION['dynamic_group']);
unset($_SESSION['dynamic_group']);
} else {
eF_deleteTableData("groups", "dynamic=1 and created is not null and created < ".(time() - 360));
eF_deleteTableData("users_to_groups", "groups_id in (select id from groups where dynamic=1 and created is not null and created < ".(time() - 360).")");
}
}
/**
* Delete group
*
* This function is used to delete an existing group. In order to do
* this, it caclulates all the group dependendant elements, deletes them
* and finally deletes the group itself.
*
* <br/>Example:
* <code>
* try {
* $group = new EfrontGroup(32); //32 is the group id
* $group -> delete();
* } catch (Exception $e) {
* echo $e -> getMessage();
* }
* </code>
*
* @return boolean True if everything is ok
* @since 3.5.0
* @access public
*/
public function delete() {
eF_deleteTableData("lessons_to_groups", "groups_ID=".$this -> group['id']);
eF_deleteTableData("courses_to_groups", "groups_ID=".$this -> group['id']);
eF_deleteTableData("users_to_groups", "groups_ID=".$this -> group['id']);
eF_deleteTableData("groups", "id=".$this -> group['id']);
return true;
}
/**
* Get group users
*
* This function returns an array with the group users. The array
* has 2 sub-arrays, 'student' and 'professor', each of which holding
* a list of user logins. If the optional $type is specified, then only
* a one-dimensional array is returned, holding the group users of the
* specified type.
* <br/>Example:
* <code>
* try {
* $group = new EfrontGroup(32); //32 is the group id
* $all_users = $group -> getUsers();
* $professors = $group -> getUsers('professor');
* } catch (Exception $e) {
* echo $e -> getMessage();
* }
* </code><br/>
* One thing to note is that this function caches results throughout the life
* cycle of the object. That is, if some other function updates the group users,
* the function results will not be altered, unless $refresh is set to true
*
* @param string $type The group users type
* @param boolean $refresh Whether to explicitly refresh the object cached data set
* @return array A 2-dimensional array with group users per type, or a 1-dimensional array with group users of the specified type
* @since 3.5.0
* @access public
*/
public function getUsers($type = false, $refresh = false) {
if ($this -> users === false || $refresh) { //Make a database query only if the variable is not initialized, or it is explicitly asked
$this -> users = array('professor' => array(), 'student' => array());
$result = eF_getTableData("users_to_groups ug, users u", "ug.*, u.user_type", "u.login=ug.users_LOGIN and groups_ID=".$this -> group['id']);
foreach ($result as $value) {
$this -> users[$value['user_type']][] = $value['users_LOGIN'];
}
}
if ($type) {
return $this -> users[$type];
} else {
return $this -> users;
}
}
/**
* Add users to group
*
* This function is used to add users to the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> addUsers(array('jdoe', 'doej'));
* </code>
*
* @param mixed $users An array of user logins or EfrontUser objects, or a single login or EfrontUser object
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function addUsers($users, $userTypeInCourses = 'student') {
if (empty($users)) {
return true;
}
$users = EfrontUser::verifyUsersList($users);
$groupUsers = eF_getTableDataFlat("users_to_groups", "users_LOGIN", "groups_ID=".$this -> group['id']);
$errors = array();
foreach ($users as $key => $user) {
if (!in_array($user, $groupUsers['users_LOGIN'], true)) {
$fields[] = array('groups_ID' => $this -> group['id'],
'users_LOGIN' => $user);
}
}
eF_insertTableDataMultiple("users_to_groups", $fields);
foreach ($this -> getCourses(true, true) as $course) {
try {
$course -> addUsers($users, $userTypeInCourses, 1);
} catch (Exception $e) {
if ($e->getCode() == EfrontCourseException :: MAX_USERS_LIMIT) {
$max_users_errors[] = $e->getMessage();
} else {
$errors[] = $e->getMessage();
}
}
}
foreach ($this -> getLessons(true, true) as $lesson) {
try {
$lesson -> addUsers($users, $userTypeInCourses, 1);
} catch (Exception $e) {
if ($e->getCode() == EfrontCourseException :: MAX_USERS_LIMIT) {
$max_users_errors[] = $e->getMessage();
} else {
$errors[] = $e->getMessage();
}
}
}
if (!empty($errors)) {
throw new EfrontGroupException(implode("<br>", $errors), EfrontGroupException :: ASSIGNMENT_ERROR);
}
if (!empty($max_users_errors)) {
return $max_users_errors;
}
return true;
}
/**
* Remove users from group
*
* This function is used to remove users from the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> removeUsers(array('jdoe', 'doej'));
* </code>
*
* @param mixed $users An array of user logins or EfrontUser objects, or a single login or EfrontUser object
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function removeUsers($users) {
$users = EfrontUser::verifyUsersList($users);
foreach ($users as $user) {
eF_deleteTableData("users_to_groups", "users_LOGIN='".$user."' and groups_ID=".$this -> group['id']);
}
return true;
}
/**
* Remove all users from the group
*
* @since 3.6.7
* @access public
*/
public function removeAllUsers() {
eF_deleteTableData("users_to_groups", "groups_ID=".$this -> group['id']);
}
/**
* Update group users
*
* This function is used to update ALL current group users
* in terms of active/languages_NAME/user_type
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> group['active'] = 2; // deactivate users
* $group -> updateUsers();
* $group -> persist();
* </code>
*
* @param optionally a single user login might be set to update only this user's records
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function updateUsers($login = false) {
if ($login) {
$users = array($login);
} else {
$users = $this -> getUsers();
$users = array_merge($users['professor'], $users['student']);
}
//pr($users);
if (sizeof($users)) {
if ($this -> group['user_types_ID']) {
// If we have a custom user type
if ($this -> group['user_types_ID'] != 'student' && $this -> group['user_types_ID'] != 'professor') {
$basic_type = eF_getTableData("user_types", "basic_user_type", "id = '" . $this -> group['user_types_ID'] . "'");
if (sizeof($basic_type)) {
$fields["user_type"] = $basic_type[0]['basic_user_type'];
$fields["user_types_ID"] = $this -> group['user_types_ID'];
}
} else {
// basic user type
$fields["user_type"] = $this -> group['user_types_ID'];
$fields["user_types_ID"] = 0;
}
}
if ($this -> group['languages_NAME']) {
$fields["languages_NAME"] = $this -> group['languages_NAME'];
}
if ($this -> group['users_active']) {
$fields["active"] = ($this -> group['users_active'] == 1)? 1:0;
}
// If at least one value greater than zero
if (sizeof($fields)) {
return eF_updateTableData("users", $fields, "login IN ('". implode("','", $users) ."')");
}
}
return true;
}
/**
* Add a user to the group using the group key
*
* This function is used to add a user to the group, using the group's key
* The courses and lessons of the group are assigned to the user using either the group's group_usertype,
* or the user's own type if none is set
*
* @param mixed $user an EfrontUser object or a user login
* @since 3.6.7
* @access public
*/
public function useKeyForUser($user) {
if ($user instanceOf EfrontUser) {
$user = $user -> user['login'];
}
$groupUsers = $this -> getUsers();
if (in_array($user, $groupUsers['student']) || in_array($user, $groupUsers['professor'])) {
// throw new Exception(_YOUAREALREADYMEMBEROFTHISGROUP, EfrontGroupException::USER_ALREADY_MEMBER);
}
if (!$this -> group['active']) {
throw new Exception(_THISGROUPISINACTIVE, EfrontGroupException::ASSIGNMENT_ERROR);
}
if ($this -> group['key_max_usage'] && $this -> group['key_max_usage'] <= $this -> group['key_current_usage']) {
throw new Exception(_MAXIMUMKEYUSAGESREACHED, EfrontGroupException::ASSIGNMENT_ERROR);
}
$max_users_errors = $this -> addUsers($user, $this -> group['user_types_ID'] ? $this -> group['user_types_ID'] : 'student');
if ($this -> group['key_max_usage']) {
$this -> group['key_current_usage']++;
$this -> persist();
}
if (is_array($max_users_errors) && !empty($max_users_errors)) {
$_SESSION['s_message'] = _YOUWHEREADDEDTOTHEGROUPBUTSOMEERRORSOCCURED.'<br/>'.implode("<br>", $max_users_errors);
$_SESSION['s_message_type'] = 'failure';
return false;
} else {
$_SESSION['s_message'] = _YOUHAVEBEENSUCCESSFULLYADDEDTOTHEGROUP;
$_SESSION['s_message_type'] = 'success';
return true;
}
}
/**
* Get group lessons
*
* This function returns an array with the group lessons. Each record in
* the array holds the lesson id, lesson name and the type ('student' or 'professor')
* assosiated with that lesson.
*
* <br/>Example:
* <code>
* try {
* $group = new EfrontGroup(32); //32 is the group id
* $group_lessons = $group -> getLessons();
* } catch (Exception $e) {
* echo $e -> getMessage();
* }
* </code><br/>
* One thing to note is that this function caches results throughout the life
* cycle of the object. That is, if some other function updates the group users,
* the function results will not be altered, unless $refresh is set to true
*
* @return array of lessons in the form [lessons_ID] => [lessons_ID, lessons_name, user_type, active]
* @since 3.5.2
* @access public
*/
public function getLessons($returnObjects = false, $refresh = false) {
if ($this -> lessons === false || $refresh) { //Make a database query only if the variable is not initialized, or it is explicitly asked
$result = eF_getTableData("lessons_to_groups lg, lessons l", "lg.*, l.*", "l.archive=0 and lg.lessons_ID = l.id and lg.groups_ID=".$this -> group['id']);
$this -> lessons = array();
foreach ($result as $value) {
if ($returnObjects) {
$this -> lessons[$value['lessons_ID']] = new EfrontLesson($value);
} else {
$this -> lessons[$value['lessons_ID']] = array("lessons_ID" => $value['lessons_ID'], "lessons_name" => $value['name'], "user_type" => $this -> group['user_types_ID'], "active" => $value['active']);
}
}
}
return $this -> lessons;
}
/**
* Add lessons to group
*
* This function is used to add lessons to the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> addLesson(3, 'professor'); // will add lesson with id 3 and role 'professor' to the group's lessons
* </code>
*
* @param $lessons_ID and $user_type
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function addLesson($lessons_ID, $user_type = "student") {
// Check if the lesson exists in the group's list
$lessons = $this -> getLessons();
if (in_array($lessons_ID, array_keys($lessons))) {
// If the lesson is already assigned check if you need
// to update the user type for this lesson
if ($lessons[$lessons_ID]['user_type'] != $user_type) {
$ok = eF_updateTableData("lessons_to_groups", array("user_type" => $user_type), "lessons_ID = " . $lessons_ID);
$this -> lessons[$lessons_ID]['user_type'] = $user_type;
return $ok;
}
} else {
$fields = array('lessons_ID' => $lessons_ID,
'user_type' => $user_type,
'groups_ID' => $this -> group['id']);
if ($ok = eF_insertTableData("lessons_to_groups", $fields)) {
$newLesson = new EfrontLesson($lessons_ID);
$this -> lessons[$lessons_ID] = array('lessons_ID' => $lessons_ID,'lessons_name'=> $newLesson -> lesson['name'], 'user_type' => $user_type);
}
return $ok;
}
// if control flow reaches here then the lesson was already assigned and with the same user_type
return false;
}
/**
* Remove lessons from group
*
* This function is used to remove lessons from the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> removeLessons(array(21, 32)); // remove lessons with ids 21 and 32
* </code>
*
* @param mixed $users An array of lesson ids or EfrontLesson objects, or a single id or EfrontLesson object
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function removeLessons($lessons) {
if (!is_array($lessons)) {
if ($lessons instanceof EfrontLesson) {
$lessons = $lessons -> lesson['id'];
}
$lessons = array($lessons);
}
foreach ($lessons as $lesson) {
if ($lesson instanceof EfrontLesson) {
$lesson = $lesson -> lesson['id'];
}
if (eF_checkParameter($lesson, 'id')) {
eF_deleteTableData("lessons_to_groups", "lessons_ID ='".$lesson."' and groups_ID=".$this -> group['id']);
}
}
return true;
}
/**
* Get group courses
*
* This function returns an array with the group courses. Each record in
* the array holds the course id, course name and the type ('student' or 'professor')
* assosiated with that course.
*
* <br/>Example:
* <code>
* try {
* $group = new EfrontGroup(32); //32 is the group id
* $group_courses = $group -> getCourses();
* } catch (Exception $e) {
* echo $e -> getMessage();
* }
* </code><br/>
* One thing to note is that this function caches results throughout the life
* cycle of the object. That is, if some other function updates the group users,
* the function results will not be altered, unless $refresh is set to true
*
* @param boolean $returnObjects whether to return EfrontCourse objects
* @param boolean $refresh whether to refresh data
* @return array of courses in the form [courses_ID] => [courses_ID, courses_name, user_type]
* @since 3.5.2
* @access public
*/
public function getCourses($returnObjects = false, $refresh = false) {
if ($this -> courses === false || $refresh) { //Make a database query only if the variable is not initialized, or it is explicitly asked
$result = eF_getTableData("courses_to_groups cg, courses c", "cg.*, c.*", "c.archive=0 and cg.courses_ID = c.id and cg.groups_ID=".$this -> group['id']);
$this -> courses = array();
foreach ($result as $value) {
if ($returnObjects) {
$this -> courses[$value['courses_ID']] = new EfrontCourse($value);
} else {
$this -> courses[$value['courses_ID']] = array("courses_ID" => $value['courses_ID'], "courses_name" => $value['courses_name'], "user_type" => $this -> group['user_types_ID']);
}
}
}
return $this -> courses;
}
/**
* Get group users based on the specified constraints, but include unassigned users as well.
* Assigned users have the flag 'has_group' set to 1
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontUser objects or user arrays
* @since 3.6.2
* @access public
*/
public function getGroupUsers($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$where[] = "ug.users_LOGIN=u.login";
$where[] = "ug.groups_ID=".$this -> group['id'];
$result = eF_getTableData("users u, users_to_groups ug", "u.*, 1 as has_group", implode(" and ", $where), $orderby, "", $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontUser :: convertDatabaseResultToUserObjects($result);
} else {
return $result;
}
}
/**
* Count group users based on the specified constraints, including unassigned
* @param array $constraints The constraints for the query
* @return int the number of entries in the result set
* @since 3.6.3
* @access public
*/
public function countGroupUsers($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$where[] = "ug.users_LOGIN=u.login";
$where[] = "ug.groups_ID=".$this -> group['id'];
$result = eF_countTableData("users u, users_to_groups ug", "u.login, 1 as has_group", implode(" and ", $where), $orderby, "", $limit);
return $result[0]['count'];
}
/**
* Check if a user is part of the group
*
* @param mixed $userToCheck An EfrontUser object or a user login
* @return boolean true if the user is part of the group
* @since 3.6.7
* @access public
*/
public function hasUser($userToCheck) {
if ($userToCheck instanceOf EfrontUser) {
$userToCheck = $userToCheck -> user['login'];
} elseif (!eF_checkParameter($userToCheck, 'login')) {
throw new Exception(_INVALIDUSER.': '.$userToCheck, EfrontGroupException::INVALID_USER);
}
$result = eF_getTableData("users_to_groups", "users_LOGIN", "users_LOGIN='".$userToCheck."' and groups_ID=".$this -> group['id']);
if (sizeof($result) > 0) {
return true;
} else {
return false;
}
}
/**
* Get group users based on the specified constraints, but include unassigned users as well.
* Assigned users have the flag 'has_group' set to 1
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontUser objects or user arrays
* @since 3.6.2
* @access public
*/
public function getGroupUsersIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$result = eF_getTableData("users u left outer join users_to_groups ug on ug.users_LOGIN=u.login and ug.groups_ID=".$this -> group['id'], "u.login,u.active,u.user_type,u.user_types_ID,u.name,u.surname,u.email, ug.groups_ID is not null as has_group", implode(" and ", $where), $orderby, "", $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontUser :: convertDatabaseResultToUserObjects($result);
} else {
return $result;
}
}
/**
* Count group users based on the specified constraints, including unassigned
* @param array $constraints The constraints for the query
* @return int the number of entries in the result set
* @since 3.6.3
* @access public
*/
public function countGroupUsersIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$result = eF_countTableData("users u left outer join users_to_groups ug on ug.users_LOGIN=u.login and ug.groups_ID=".$this -> group['id'], "u.login, ug.groups_ID is not null as has_group", implode(" and ", $where));
return $result[0]['count'];
}
/*
* Returns the courses of the group users only
*/
public function getGroupUserCourses($constraints = array()) {
$select['main'] = 'c.*';
$select['has_instances'] = "(select count( * ) from courses l where instance_source=c.id) as has_instances";
$select['num_lessons'] = "(select count( * ) from lessons_to_courses cl, lessons l where cl.courses_ID=c.id and l.archive=0 and l.id=cl.lessons_ID) as num_lessons";
// num_students: assigned+completed
$select['num_students'] = "(select count( * ) from users_to_courses uc, users u where uc.courses_ID=c.id and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and u.user_type='student') as num_students";
$select['num_assigned'] = "(select count( * ) from users_to_courses uc, users u, users_to_groups ug where uc.courses_ID=c.id and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and u.user_type='student' and uc.completed=0 and u.login=ug.users_LOGIN and ug.groups_ID = ".$this -> group['id'].") as num_assigned";
$select['num_completed'] = "(select count( * ) from users_to_courses uc, users u, users_to_groups ug where uc.courses_ID=c.id and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and u.user_type='student' and uc.completed=1 and u.login=ug.users_LOGIN and ug.groups_ID = ". $this -> group['id'].") as num_completed";
if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE
$select['num_skills'] = "(select count( * ) from module_hcd_course_offers_skill s where courses_ID=c.id) as num_skills";
$select['location'] = "(select b.name from module_hcd_branch b, module_hcd_course_to_branch cb where cb.branches_ID=b.branch_ID and cb.courses_ID=c.id limit 1) as location";
} #cpp#endif
$select = EfrontCourse :: convertCourseConstraintsToRequiredFields($constraints, $select);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$result = eF_getTableData("courses c", implode(",", $select), implode(" and ", $where), $orderby, false, $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontCourse :: convertDatabaseResultToCourseObjects($result);
} else {
return $result;
}
}
/**
* Get group courses
*
* This function returns the list of courses that are part of this group
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontCourse objects or course arrays
* @since 3.6.2
* @access public
*/
public function getGroupCourses($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$select = "c.*, cg.user_type, 1 as has_course,
(select count( * ) from courses c1, courses_to_groups cg1 where c1.instance_source=c.id and cg1.courses_ID=c1.id and cg.groups_ID=".$this -> group['id'].")
as has_instances,
(select count( * ) from lessons_to_courses cl, lessons l where cl.courses_ID=c.id and l.archive=0 and l.id=cl.lessons_ID)
as num_lessons,
(select count( * ) from users_to_courses uc, users u where uc.courses_ID=c.id and c.archive=0 and u.active!=0 and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and uc.user_type IN ('".implode("','", array_keys(EfrontLessonUser::getStudentRoles()))."'))
as num_students";
if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE
$select .= ",(select count( * ) from module_hcd_course_offers_skill s where courses_ID=c.id)
as num_skills";
} #cpp#endif
$where[] = "c.id=cg.courses_ID and cg.groups_ID=".$this -> group['id'];
$result = eF_getTableData("courses c, courses_to_groups cg", $select, implode(" and ", $where), $orderby, $groupby, $limit);
if (!isset($constraints['return_objects']) || $constraints['return_objects'] == true) {
return EfrontCourse :: convertDatabaseResultToCourseObjects($result);
} else {
return $result;
}
}
/**
* Count group courses based on the specified constraints
* @param array $constraints The constraints for the query
* @return int the number of entries in the result set
* @since 3.6.3
* @access public
*/
public function countGroupCourses($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$where[] = "c.id=cg.courses_ID and cg.groups_ID=".$this -> group['id'];
$result = eF_countTableData("courses c, courses_to_groups cg", "c.id", implode(" and ", $where));
return $result[0]['count'];
}
/**
* Get group courses
*
* This function returns the list of courses that are part of this group, including unassigned
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontCourse objects or course arrays
* @since 3.6.2
* @access public
*/
public function getGroupCoursesIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$select = "c.*, r.courses_ID is not null as has_course,
(select count( * ) from courses l where instance_source=c.id)
as has_instances,
(select count( * ) from lessons_to_courses cl, lessons l where cl.courses_ID=c.id and l.archive=0 and l.id=cl.lessons_ID)
as num_lessons,
(select count( * ) from users_to_courses uc, users u where uc.courses_ID=c.id and c.archive=0 and u.active!=0 and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and uc.user_type IN ('".implode("','", array_keys(EfrontLessonUser::getStudentRoles()))."'))
as num_students";
if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE
$select .= ",(select count( * ) from module_hcd_course_offers_skill s where courses_ID=c.id)
as num_skills";
} #cpp#endif
$result = eF_getTableData("courses c left outer join (select courses_ID from courses_to_groups where groups_ID=".$this -> group['id'].") r on c.id=r.courses_ID", $select,
implode(" and ", $where), $orderby, $groupby, $limit);
return EfrontCourse :: convertDatabaseResultToCourseObjects($result);
}
/**
* Count group courses based on the specified constraints, including unassigned
* @param array $constraints The constraints for the query
* @return int the number of entries in the result set
* @since 3.6.3
* @access public
*/
public function countGroupCoursesIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$result = eF_countTableData("courses c left outer join (select courses_ID from courses_to_groups where groups_ID=".$this -> group['id'].") r on c.id=r.courses_ID", "c.id",
implode(" and ", $where));
return $result[0]['count'];
}
/**
* Get group courses
*
* This function returns the list of courses that are part of this group, including unassigned
*
* @param array $constraints The constraints for the query
* @return array An array of EfrontCourse objects or course arrays
* @since 3.6.2
* @access public
*/
public function getGroupCoursesAggregatingResultsIncludingUnassigned($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontCourse :: convertCourseConstraintsToSqlParameters($constraints);
$select = "c.*, (rr.courses_ID is not null OR r.courses_ID is not null) as has_course,
(select count( * ) from courses l where instance_source=c.id)
as has_instances,
(select count( * ) from lessons_to_courses cl, lessons l where cl.courses_ID=c.id and l.archive=0 and l.id=cl.lessons_ID)
as num_lessons,
(select count( * ) from users_to_courses uc, users u where uc.courses_ID=c.id and c.archive=0 and u.active!=0 and u.archive=0 and uc.archive=0 and u.login=uc.users_LOGIN and uc.user_type IN ('".implode("','", array_keys(EfrontLessonUser::getStudentRoles()))."'))
as num_students";
if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE
$select .= ",(select count( * ) from module_hcd_course_offers_skill s where courses_ID=c.id)
as num_skills";
} #cpp#endif
//debug();
$result = eF_getTableData("courses c left outer join (select courses_ID from courses_to_groups where groups_ID=".$this -> group['id'].") r on c.id=r.courses_ID left outer join (select courses_ID,name,instance_source from courses c, courses_to_groups cg where c.id=cg.courses_ID and cg.groups_ID={$this->group['id']}) rr on rr.instance_source=c.id", $select,
implode(" and ", $where), $orderby, $groupby, $limit);
//debug(false);
return EfrontCourse :: convertDatabaseResultToCourseObjects($result);
}
/**
* Add courses to group
*
* This function is used to add courses to the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> addCourse(3, 'professor'); // will add course with id 3 and role 'professor' to the group's courses
* </code>
*
* @param $courses_ID and $user_type
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function addCourse($courses_ID, $user_type = "student") {
// Check if the course exists in the group's list
$courses = $this -> getCourses();
if (in_array($courses_ID, array_keys($courses))) {
// If the course is already assigned check if you need
// to update the user type for this course
if ($courses[$courses_ID]['user_type'] != $user_type) {
$ok = eF_updateTableData("courses_to_groups", array("user_type" => $user_type), "courses_ID = " . $courses_ID);
$this -> courses[$courses_ID]['user_type'] = $user_type;
return $ok;
}
} else {
$fields = array('courses_ID' => $courses_ID,
'user_type' => $user_type,
'groups_ID' => $this -> group['id']);
if ($ok = eF_insertTableData("courses_to_groups", $fields)) {
$newCourse = new EfrontCourse($courses_ID);
$this -> courses[$courses_ID] = array('courses_ID' => $courses_ID,'courses_name'=> $newCourse -> course['name'], 'user_type' => $user_type);
}
return $ok;
}
// if control flow reaches here then the course was already assigned and with the same user_type
return false;
}
/**
* Remove courses from group
*
* This function is used to remove courses from the current group
* <br>Example:
* <code>
* $group = new EfrontGroup(2);
* $group -> removeCourses(array(21, 32)); // remove courses with ids 21 and 32
* </code>
*
* @param mixed $users An array of course ids or EfrontCourse objects, or a single id or EfrontCourse object
* @return boolean True if everything is ok
* @since 3.5.2
* @access public
*/
public function removeCourses($courses) {
if (!is_array($courses)) {
if ($courses instanceof EfrontCourse) {
$courses = $courses -> course['id'];
}
$courses = array($courses);
}
foreach ($courses as $course) {
if ($course instanceof EfrontCourse) {
$course = $course -> course['id'];
}
if (eF_checkParameter($course, 'id')) {
eF_deleteTableData("courses_to_groups", "courses_ID ='".$course."' and groups_ID=".$this -> group['id']);
}
}
return true;
}
public function setData($fields, $persist = false) {
if (sizeof($fields['name']) > 0)
$this -> group['name'] = $fields['name'];
if (sizeof($fields['description']) > 0)
$this -> group['description'] = $fields['description'];
if (sizeof($fields['active']) > 0)
$this -> group['active'] = $fields['active'];
if ($persist) {
$this -> persist();
}
}
/**
* Persist group values
*
* This function is used to persist any changes made to the current
* group.
* <br/>Example:
* <code>
* $group = new EfrontGroup(3); //Instantiate group with id 3
* $group -> group['name'] = 'new name'; //Change a group's value
* $group -> persist(); //Store changes values to the database
* </code>
*
* @return boolean True if everything is ok
* @since 3.5.0
* @access public
*/
public function persist() {
// Remove the current default group
if ($this -> group['is_default']) {
eF_updateTableData("groups", array("is_default" => 0), "1=1");
}
$ok = eF_updateTableData("groups", $this -> group, "id=".$this -> group['id']);
return $ok;