ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilParticipants.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 define("IL_CRS_ADMIN",1);
13 define("IL_CRS_TUTOR",3);
14 define("IL_CRS_MEMBER",2);
15 
16 define('IL_GRP_ADMIN',4);
17 define('IL_GRP_MEMBER',5);
18 
19 
21 {
22  protected $obj_id = 0;
23  protected $type = '';
24  protected $ref_id = 0;
25 
26  protected $roles = array();
27  protected $role_data = array();
28 
29  protected $participants = array();
30  protected $participants_status = array();
31  protected $members = array();
32  protected $tutors = array();
33  protected $admins = array();
34 
35  protected $subscribers = array();
36 
37  protected $ilDB;
38  protected $lng;
39 
40 
48  public function __construct($a_obj_id)
49  {
50  global $ilDB,$lng;
51 
52  $this->ilDB = $ilDB;
53  $this->lng = $lng;
54 
55  $this->obj_id = $a_obj_id;
56  $this->type = ilObject::_lookupType($a_obj_id);
57  $ref_ids = ilObject::_getAllReferences($this->obj_id);
58  $this->ref_id = current($ref_ids);
59 
60  $this->readParticipants();
61  $this->readParticipantsStatus();
62  }
63 
70  public static function getInstanceByObjId($a_obj_id)
71  {
72  $type = ilObject::_lookupType($a_obj_id);
73  switch($type)
74  {
75  case 'crs':
76  include_once './Modules/Course/classes/class.ilCourseParticipants.php';
78 
79  case 'grp':
80  include_once './Modules/Group/classes/class.ilGroupParticipants.php';
82  }
83  // @todo proper error handling
84  return null;
85  }
86 
87 
93  public static function hasParticipantListAccess($a_obj_id, $a_usr_id = null)
94  {
95  if(!$a_usr_id)
96  {
97  $a_usr_id = $GLOBALS['ilUser']->getId();
98  }
99 
100  // if write access granted => return true
101  $refs = ilObject::_getAllReferences($a_obj_id);
102  $ref_id = end($refs);
103 
104  if($GLOBALS['ilAccess']->checkAccess('write','',$ref_id))
105  {
106  return true;
107  }
108  $part = self::getInstanceByObjId($a_obj_id);
109  if($part->isAssigned($a_usr_id))
110  {
111  if($part->getType() == 'crs')
112  {
113  // Check for show_members
114  include_once './Modules/Course/classes/class.ilObjCourse.php';
116  {
117  return false;
118  }
119  }
120  return true;
121  }
122  // User is not assigned to course/group => no read access
123  return false;
124  }
125 
137  public static function _getMembershipByType($a_usr_id,$a_type,$a_only_member_role = false)
138  {
139  global $ilDB;
140 
141  // this will also dismiss local roles!
142  if ($a_only_member_role)
143  {
144  $j2 = "JOIN object_data obd2 ON (ua.rol_id = obd2.obj_id) ";
145  $a2 = "AND obd2.title LIKE 'il_".$a_type."_mem%' ";
146  }
147 
148  $query = "SELECT DISTINCT obd.obj_id,obr.ref_id FROM rbac_ua ua ".
149  "JOIN rbac_fa fa ON ua.rol_id = fa.rol_id ".
150  "JOIN tree t1 ON t1.child = fa.parent ".
151  "JOIN object_reference obr ON t1.parent = obr.ref_id ".
152  "JOIN object_data obd ON obr.obj_id = obd.obj_id ".
153  $j2.
154  "WHERE obd.type = ".$ilDB->quote($a_type,'text')." ".
155  "AND fa.assign = 'y' ".
156  "AND ua.usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
157  $a2;
158 
159  $res = $ilDB->query($query);
160 
161  while($row = $ilDB->fetchObject($res))
162  {
163  $ref_ids[] = $row->obj_id;
164  }
165 
166  return $ref_ids ? $ref_ids : array();
167  }
168 
169 
170 
179  public static function _isParticipant($a_ref_id,$a_usr_id)
180  {
181  global $rbacreview,$ilObjDataCache,$ilDB,$ilLog;
182 
183  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
184  if(!isset($rolf['ref_id']) or !$rolf['ref_id'])
185  {
186  $title = $ilObjDataCache->lookupTitle($ilObjDataCache->lookupObjId($a_ref_id));
187  $ilLog->write(__METHOD__.': Found object without role folder. Ref_id: '.$a_ref_id.', title: '.$title);
188  $ilLog->logStack();
189 
190  return false;
191  }
192  $local_roles = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"],false);
193 
194  return $rbacreview->isAssignedToAtLeastOneGivenRole($a_usr_id, $local_roles);
195  }
196 
204  public static function lookupNumberOfParticipants($a_ref_id)
205  {
206  global $rbacreview;
207 
208  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
209  $lroles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
210  return $rbacreview->getNumberOfAssignedUsers($lroles);
211  }
212 
220  public static function lookupNumberOfMembers($a_ref_id)
221  {
222  global $rbacreview, $ilObjDataCache;
223 
224  $rolf = $rbacreview->getRoleFolderIdOfObject($a_ref_id);
225 
226  if(!$rolf)
227  {
228  return 0;
229  }
230  $lroles = $rbacreview->getRolesOfRoleFolder($rolf,false);
231 
232  $memberRoles = array();
233  foreach($lroles as $role_id)
234  {
235  $title = $ilObjDataCache->lookupTitle($role_id);
236  switch(substr($title,0,8))
237  {
238  case 'il_crs_a':
239  case 'il_crs_t':
240  case 'il_grp_a':
241  break;
242 
243  default:
244  $memberRoles[] = $role_id;
245  break;
246  }
247  }
248  return $rbacreview->getNumberOfAssignedUsers($memberRoles);
249  }
250 
251 
261  public static function _isBlocked($a_obj_id,$a_usr_id)
262  {
263  global $ilDB;
264 
265  $query = "SELECT * FROM obj_members ".
266  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
267  "AND usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
268  "AND blocked = ".$ilDB->quote(1,'integer');
269  $res = $ilDB->query($query);
270  return $res->numRows() ? true : false;
271  }
272 
282  public static function _hasPassed($a_obj_id,$a_usr_id)
283  {
284  global $ilDB;
285 
286  $query = "SELECT * FROM obj_members ".
287  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
288  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
289  "AND passed = '1'";
290  $res = $ilDB->query($query);
291  return $res->numRows() ? true : false;
292  }
293 
303  public static function _deleteAllEntries($a_obj_id)
304  {
305  global $ilDB;
306 
307  $query = "DELETE FROM obj_members ".
308  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
309  $res = $ilDB->manipulate($query);
310 
311  $query = "DELETE FROM il_subscribers ".
312  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
313  $res = $ilDB->manipulate($query);
314 
315  $query = 'DELETE FROM crs_waiting_list '.
316  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
317  $ilDB->manipulate($query);
318 
319  return true;
320  }
321 
330  public static function _deleteUser($a_usr_id)
331  {
332  global $ilDB;
333 
334  $query = "DELETE FROM obj_members WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
335  $res = $ilDB->manipulate($query);
336 
337  $query = "DELETE FROM il_subscribers WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
338  $res = $ilDB->manipulate($query);
339 
340  include_once './Modules/Course/classes/class.ilCourseWaitingList.php';
342  }
343 
344  public static function getDefaultMemberRole($a_ref_id)
345  {
346  global $ilCtrl;
347 
348  $obj_id = ilObject::_lookupObjId($a_ref_id);
350 
351  if(!in_array($type,array('crs','grp')))
352  {
353  return 0;
354  }
355 
356  global $rbacreview;
357 
358 
359  $rolf = $rbacreview->getRoleFolderIdOfObject($a_ref_id);
360  $roles = $rbacreview->getRolesOfRoleFolder($rolf,false);
361 
362  foreach($roles as $role)
363  {
364  $title = ilObject::_lookupTitle($role);
365  if(substr($title, 0, 13) == ('il_'.$type.'_member'))
366  {
367  return $role;
368  }
369  }
370  return 0;
371  }
372 
377  public function getType()
378  {
379  return $this->type;
380  }
381 
388  public function getNotificationRecipients()
389  {
390  global $ilDB;
391 
392  $query = "SELECT * FROM obj_members ".
393  "WHERE notification = 1 ".
394  "AND obj_id = ".$ilDB->quote($this->obj_id)." ";
395  $res = $ilDB->query($query);
396  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
397  {
398  if($this->isAdmin($row->usr_id) or $this->isTutor($row->usr_id))
399  {
400  $recp[] = $row->usr_id;
401  }
402  }
403  return $recp ? $recp : array();
404  }
405 
412  public function getCountMembers()
413  {
414  return count($this->members);
415  }
416 
423  public function getCountParticipants()
424  {
425  return count($this->participants);
426  }
427 
428 
429 
430 
437  public function getParticipants()
438  {
439  return $this->participants ? $this->participants : array();
440  }
441 
449  public function getMembers()
450  {
451  return $this->members ? $this->members : array();
452  }
459  public function getAdmins()
460  {
461  return $this->admins ? $this->admins : array();
462  }
463 
468  public function getCountAdmins()
469  {
470  return count($this->getAdmins());
471  }
472 
473 
480  public function getTutors()
481  {
482  return $this->tutors ? $this->tutors : array();
483  }
484 
492  public function isAdmin($a_usr_id)
493  {
494  return in_array($a_usr_id,$this->admins) ? true : false;
495  }
496 
504  public function isTutor($a_usr_id)
505  {
506  return in_array($a_usr_id,$this->tutors) ? true : false;
507  }
508 
516  public function isMember($a_usr_id)
517  {
518  return in_array($a_usr_id,$this->members) ? true : false;
519  }
520 
521 
522 
523 
531  public function isAssigned($a_usr_id)
532  {
533  return in_array($a_usr_id,$this->participants);
534  }
535 
541  public function isLastAdmin($a_usr_id)
542  {
543  return in_array($a_usr_id,$this->getAdmins()) and count($this->getAdmins()) == 1;
544  }
545 
546 
554  public function getRoles()
555  {
556  return $this->roles ? $this->roles : array();
557  }
558 
566  public function getAssignedRoles($a_usr_id)
567  {
568  global $rbacreview;
569 
570  foreach($this->roles as $role)
571  {
572  if($rbacreview->isAssigned($a_usr_id,$role))
573  {
574  $assigned[] = $role;
575  }
576  }
577  return $assigned ? $assigned : array();
578  }
579 
588  public function updateRoleAssignments($a_usr_id,$a_roles)
589  {
590  global $rbacreview,$rbacadmin;
591 
592  $roles = $a_roles ? $a_roles : array();
593 
594  foreach($this->getRoles() as $role_id)
595  {
596  if($rbacreview->isAssigned($a_usr_id,$role_id))
597  {
598  if(!in_array($role_id,$roles))
599  {
600  $rbacadmin->deassignUser($role_id,$a_usr_id);
601  }
602  }
603  else
604  {
605  if(in_array($role_id,$roles))
606  {
607  $rbacadmin->assignUser($role_id,$a_usr_id);
608  }
609  }
610  }
611  $this->readParticipants();
612  $this->readParticipantsStatus();
613  }
614 
622  public function checkLastAdmin($a_usr_ids)
623  {
624  foreach($this->getAdmins() as $admin_id)
625  {
626  if(!in_array($admin_id,$a_usr_ids))
627  {
628  return true;
629  }
630  }
631  return false;
632  }
633 
641  public function isBlocked($a_usr_id)
642  {
643  if(isset($this->participants_status[$a_usr_id]))
644  {
645  return $this->participants_status[$a_usr_id]['blocked'] ? true : false;
646  }
647  return false;
648  }
649 
657  public function hasPassed($a_usr_id)
658  {
659  if(isset($this->participants_status[$a_usr_id]))
660  {
661  return $this->participants_status[$a_usr_id]['passed'] ? true : false;
662  }
663  return false;
664  }
665 
673  public function delete($a_usr_id)
674  {
675  global $rbacadmin,$ilDB;
676 
677  $this->dropDesktopItem($a_usr_id);
678  foreach($this->roles as $role_id)
679  {
680  $rbacadmin->deassignUser($role_id,$a_usr_id);
681  }
682 
683  $query = "DELETE FROM obj_members ".
684  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
685  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer');
686  $res = $ilDB->manipulate($query);
687 
688  $this->readParticipants();
689  $this->readParticipantsStatus();
690 
691  if($this->type == 'crs')
692  {
693  // Add event: used for ecs accounts
694  $GLOBALS['ilAppEventHandler']->raise("Modules/Course", "deleteParticipant", array('obj_id' => $this->obj_id, 'usr_id' => $a_usr_id));
695  }
696 
697 
698  return true;
699  }
700 
709  public function updateBlocked($a_usr_id,$a_blocked)
710  {
711  global $ilDB;
712 
713  $this->participants_status[$a_usr_id]['blocked'] = (int) $a_blocked;
714 
715  $query = "SELECT * FROM obj_members ".
716  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
717  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
718  $res = $ilDB->query($query);
719  if($res->numRows())
720  {
721  $query = "UPDATE obj_members SET ".
722  "blocked = ".$ilDB->quote((int) $a_blocked ,'integer')." ".
723  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
724  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
725  }
726  else
727  {
728  $query = "INSERT INTO obj_members (blocked,obj_id,usr_id,notification,passed) ".
729  "VALUES ( ".
730  $ilDB->quote((int) $a_blocked ,'integer').", ".
731  $ilDB->quote($this->obj_id ,'integer').", ".
732  $ilDB->quote($a_usr_id ,'integer').", ".
733  $ilDB->quote(0,'integer').", ".
734  $ilDB->quote(0,'integer').
735  ")";
736 
737  }
738  $res = $ilDB->manipulate($query);
739  return true;
740  }
741 
750  public function updateNotification($a_usr_id,$a_notification)
751  {
752  global $ilDB;
753 
754  $this->participants_status[$a_usr_id]['notification'] = (int) $a_notification;
755 
756  $query = "SELECT * FROM obj_members ".
757  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
758  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
759  $res = $ilDB->query($query);
760  if($res->numRows())
761  {
762  $query = "UPDATE obj_members SET ".
763  "notification = ".$ilDB->quote((int) $a_notification ,'integer')." ".
764  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
765  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
766  }
767  else
768  {
769  $query = "INSERT INTO obj_members (notification,obj_id,usr_id,passed,blocked) ".
770  "VALUES ( ".
771  $ilDB->quote((int) $a_notification ,'integer').", ".
772  $ilDB->quote($this->obj_id ,'integer').", ".
773  $ilDB->quote($a_usr_id ,'integer').", ".
774  $ilDB->quote(0,'integer').", ".
775  $ilDB->quote(0,'integer').
776  ")";
777 
778  }
779  $res = $ilDB->manipulate($query);
780  return true;
781  }
782 
783 
784 
785 
794  public function add($a_usr_id,$a_role)
795  {
796  global $rbacadmin,$ilLog,$ilAppEventHandler;
797 
798  if($this->isAssigned($a_usr_id))
799  {
800  return false;
801  }
802 
803  switch($a_role)
804  {
805  case IL_CRS_ADMIN:
806  $this->admins[] = $a_usr_id;
807  break;
808 
809  case IL_CRS_TUTOR:
810  $this->tutors[] = $a_usr_id;
811  break;
812 
813  case IL_CRS_MEMBER:
814  $this->members[] = $a_usr_id;
815  break;
816 
817  case IL_GRP_ADMIN:
818  $this->admins[] = $a_usr_id;
819  break;
820 
821  case IL_GRP_MEMBER:
822  $this->members[] = $a_usr_id;
823  break;
824  }
825 
826  $this->participants[] = $a_usr_id;
827  $rbacadmin->assignUser($this->role_data[$a_role],$a_usr_id);
828  $this->addDesktopItem($a_usr_id);
829 
830  // Delete subscription request
831  $this->deleteSubscriber($a_usr_id);
832 
833  include_once './Services/Membership/classes/class.ilWaitingList.php';
834  ilWaitingList::deleteUserEntry($a_usr_id,$this->obj_id);
835 
836  if($this->type == 'crs') {
837  // Add event: used for ecs accounts
838  $ilLog->write(__METHOD__.': Raise new event: Modules/Course addParticipant');
839  $ilAppEventHandler->raise(
840  "Modules/Course",
841  "addParticipant",
842  array(
843  'obj_id' => $this->obj_id,
844  'usr_id' => $a_usr_id,
845  'role_id' => $a_role)
846  );
847  }
848  return true;
849  }
850 
851 
859  public function deleteParticipants($a_user_ids)
860  {
861  foreach($a_user_ids as $user_id)
862  {
863  $this->delete($user_id);
864  }
865  return true;
866  }
867 
875  public function addDesktopItem($a_usr_id)
876  {
877  if(!ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
878  {
879  ilObjUser::_addDesktopItem($a_usr_id, $this->ref_id,$this->type);
880  }
881  return true;
882  }
883 
891  function dropDesktopItem($a_usr_id)
892  {
893  if(ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
894  {
895  ilObjUser::_dropDesktopItem($a_usr_id, $this->ref_id,$this->type);
896  }
897 
898  return true;
899  }
900 
901 
902 
910  public function isNotificationEnabled($a_usr_id)
911  {
912  if(isset($this->participants_status[$a_usr_id]))
913  {
914  return $this->participants_status[$a_usr_id]['notification'] ? true : false;
915  }
916  return false;
917  }
918 
919 
927  private function readParticipants()
928  {
929  global $rbacreview,$ilObjDataCache,$ilLog;
930 
931  $rolf = $rbacreview->getRoleFolderOfObject($this->ref_id);
932 
933  if(!isset($rolf['ref_id']) or !$rolf['ref_id'])
934  {
935  $title = $ilObjDataCache->lookupTitle($ilObjDataCache->lookupObjId($this->ref_id));
936  $ilLog->write(__METHOD__.': Found object without role folder. Ref_id: '.$this->ref_id.', title: '.$title);
937  $ilLog->logStack();
938  return false;
939  }
940 
941  $this->roles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
942 
943  $users = array();
944  $this->participants = array();
945  $this->members = $this->admins = $this->tutors = array();
946 
947  foreach($this->roles as $role_id)
948  {
949  $title = $ilObjDataCache->lookupTitle($role_id);
950  switch(substr($title,0,8))
951  {
952  case 'il_crs_m':
953  $this->role_data[IL_CRS_MEMBER] = $role_id;
954  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
955  $this->members = array_unique(array_merge($assigned,$this->members));
956  break;
957 
958  case 'il_crs_a':
959  $this->role_data[IL_CRS_ADMIN] = $role_id;
960  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
961  $this->admins = $rbacreview->assignedUsers($role_id);
962  break;
963 
964  case 'il_crs_t':
965  $this->role_data[IL_CRS_TUTOR] = $role_id;
966  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
967  $this->tutors = $rbacreview->assignedUsers($role_id);
968  break;
969 
970  case 'il_grp_a':
971  $this->role_data[IL_GRP_ADMIN] = $role_id;
972  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
973  $this->admins = $rbacreview->assignedUsers($role_id);
974  break;
975 
976  case 'il_grp_m':
977  $this->role_data[IL_GRP_MEMBER] = $role_id;
978  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
979  $this->members = $rbacreview->assignedUsers($role_id);
980  break;
981 
982  default:
983  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
984  $this->members = array_unique(array_merge($assigned,$this->members));
985  break;
986  }
987  }
988  }
989 
997  private function readParticipantsStatus()
998  {
999  global $ilDB;
1000 
1001  $query = "SELECT * FROM obj_members ".
1002  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1003  $res = $ilDB->query($query);
1004  $this->participants_status = array();
1005  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1006  {
1007  $this->participants_status[$row->usr_id]['blocked'] = $row->blocked;
1008  $this->participants_status[$row->usr_id]['notification'] = $row->notification;
1009  $this->participants_status[$row->usr_id]['passed'] = $row->passed;
1010  }
1011  }
1012 
1020  public function isGroupingMember($a_usr_id,$a_field = '')
1021  {
1022  global $rbacreview,$ilObjDataCache,$ilDB;
1023 
1024  // Used for membership limitations -> check membership by given field
1025  if($a_field)
1026  {
1027  include_once './Services/User/classes/class.ilObjUser.php';
1028 
1029  $tmp_user =& ilObjectFactory::getInstanceByObjId($a_usr_id);
1030  switch($a_field)
1031  {
1032  case 'login':
1033  $and = "AND login = ".$ilDB->quote($tmp_user->getLogin(),'text')." ";
1034  break;
1035  case 'email':
1036  $and = "AND email = ".$ilDB->quote($tmp_user->getEmail(),'text')." ";
1037  break;
1038  case 'matriculation':
1039  $and = "AND matriculation = ".$ilDB->quote($tmp_user->getMatriculation(),'text')." ";
1040  break;
1041 
1042  default:
1043  $and = "AND usr_id = ".$ilDB->quote($a_usr_id,'integer'). " ";
1044  break;
1045  }
1046 
1047  if(!$this->getParticipants())
1048  {
1049  return false;
1050  }
1051 
1052  $query = "SELECT * FROM usr_data ud ".
1053  "WHERE ".$ilDB->in('usr_id',$this->getParticipants(),false,'integer')." ".
1054  $and;
1055 
1056  $res = $ilDB->query($query);
1057  return $res->numRows() ? true : false;
1058  }
1059  }
1060 
1061  public static function lookupSubscribers($a_obj_id)
1062  {
1063  global $ilDB;
1064 
1065  $subscribers = array();
1066  $query = "SELECT usr_id FROM il_subscribers ".
1067  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
1068  "ORDER BY sub_time ";
1069 
1070  $res = $ilDB->query($query);
1071  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1072  {
1073  $subscribers[] = $row->usr_id;
1074  }
1075  return $subscribers;
1076  }
1077 
1083  public function getSubscribers()
1084  {
1085  $this->readSubscribers();
1086 
1087  return $this->subscribers;
1088  }
1089 
1090 
1096  public function getCountSubscribers()
1097  {
1098  return count($this->getSubscribers());
1099  }
1100 
1106  public function getSubscriberData($a_usr_id)
1107  {
1108  return $this->readSubscriberData($a_usr_id);
1109  }
1110 
1111 
1112 
1118  public function assignSubscribers($a_usr_ids)
1119  {
1120  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1121  {
1122  return false;
1123  }
1124  foreach($a_usr_ids as $id)
1125  {
1126  if(!$this->assignSubscriber($id))
1127  {
1128  return false;
1129  }
1130  }
1131  return true;
1132  }
1133 
1139  public function assignSubscriber($a_usr_id)
1140  {
1141  global $ilErr;
1142 
1143  $ilErr->setMessage("");
1144  if(!$this->isSubscriber($a_usr_id))
1145  {
1146  $ilErr->appendMessage($this->lng->txt("crs_user_notsubscribed"));
1147 
1148  return false;
1149  }
1150  if($this->isAssigned($a_usr_id))
1151  {
1152  $tmp_obj = ilObjectFactory::getInstanceByObjId($a_usr_id);
1153  $ilErr->appendMessage($tmp_obj->getLogin().": ".$this->lng->txt("crs_user_already_assigned"));
1154 
1155  return false;
1156  }
1157 
1158  if(!$tmp_obj =& ilObjectFactory::getInstanceByObjId($a_usr_id))
1159  {
1160  $ilErr->appendMessage($this->lng->txt("crs_user_not_exists"));
1161 
1162  return false;
1163  }
1164 
1165  // TODO: must be group or course member role
1166  $this->add($tmp_obj->getId(),IL_CRS_MEMBER);
1167  $this->deleteSubscriber($a_usr_id);
1168 
1169  return true;
1170  }
1171 
1177  public function autoFillSubscribers()
1178  {
1179  $this->readSubscribers();
1180 
1181  $counter = 0;
1182  foreach($this->subscribers as $subscriber)
1183  {
1184  if(!$this->assignSubscriber($subscriber))
1185  {
1186  continue;
1187  }
1188  else
1189  {
1190  // TODO: notification
1191  #$this->sendNotification($this->NOTIFY_ACCEPT_SUBSCRIBER,$subscriber);
1192  }
1193  ++$counter;
1194  }
1195 
1196  return $counter;
1197  }
1198 
1204  public function addSubscriber($a_usr_id)
1205  {
1206  global $ilDB;
1207 
1208  $query = "INSERT INTO il_subscribers (usr_id,obj_id,subject,sub_time) ".
1209  " VALUES (".
1210  $ilDB->quote($a_usr_id ,'integer').",".
1211  $ilDB->quote($this->obj_id ,'integer').", ".
1212  $ilDB->quote('','text').", ".
1213  $ilDB->quote(time() ,'integer').
1214  ")";
1215  $res = $ilDB->manipulate($query);
1216 
1217  return true;
1218  }
1219 
1220 
1226  public function updateSubscriptionTime($a_usr_id,$a_subtime)
1227  {
1228  global $ilDB;
1229 
1230  $query = "UPDATE il_subscribers ".
1231  "SET sub_time = ".$ilDB->quote($a_subtime ,'integer')." ".
1232  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1233  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1234  $res = $ilDB->manipulate($query);
1235 
1236  return true;
1237  }
1238 
1246  public function updateSubject($a_usr_id,$a_subject)
1247  {
1248  global $ilDB;
1249 
1250  $query = "UPDATE il_subscribers ".
1251  "SET subject = ".$ilDB->quote($a_subject ,'text')." ".
1252  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1253  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1254  $res = $ilDB->manipulate($query);
1255  return true;
1256  }
1257 
1258 
1264  public function deleteSubscriber($a_usr_id)
1265  {
1266  global $ilDB;
1267 
1268  $query = "DELETE FROM il_subscribers ".
1269  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1270  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1271  $res = $ilDB->manipulate($query);
1272 
1273  return true;
1274  }
1275 
1276 
1282  public function deleteSubscribers($a_usr_ids)
1283  {
1284  global $ilErr,$ilDB;
1285 
1286  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1287  {
1288  $ilErr->setMessage('');
1289  $ilErr->appendMessage($this->lng->txt("no_usr_ids_given"));
1290 
1291  return false;
1292  }
1293  $query = "DELETE FROM il_subscribers ".
1294  "WHERE ".$ilDB->in('usr_id',(array) $a_usr_ids,false,'integer')." ".
1295  "AND obj_id = ".$ilDB->quote($this->obj_id,'integer');
1296  $res = $ilDB->query($query);
1297  return true;
1298  }
1299 
1300 
1306  public function isSubscriber($a_usr_id)
1307  {
1308  global $ilDB;
1309 
1310  $query = "SELECT * FROM il_subscribers ".
1311  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1312  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')."";
1313 
1314  $res = $ilDB->query($query);
1315  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1316  {
1317  return true;
1318  }
1319  return false;
1320  }
1321 
1328  public static function _isSubscriber($a_obj_id,$a_usr_id)
1329  {
1330  global $ilDB;
1331 
1332  $query = "SELECT * FROM il_subscribers ".
1333  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1334  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
1335 
1336  $res = $ilDB->query($query);
1337  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1338  {
1339  return true;
1340  }
1341  return false;
1342  }
1343 
1349  protected function readSubscribers()
1350  {
1351  global $ilDB;
1352 
1353  $this->subscribers = array();
1354 
1355  $query = "SELECT usr_id FROM il_subscribers ".
1356  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1357  "ORDER BY sub_time ";
1358 
1359  $res = $this->ilDB->query($query);
1360  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1361  {
1362  // DELETE SUBSCRIPTION IF USER HAS BEEN DELETED
1363  if(!ilObjectFactory::getInstanceByObjId($row->usr_id,false))
1364  {
1365  $this->deleteSubscriber($row->usr_id);
1366  }
1367  $this->subscribers[] = $row->usr_id;
1368  }
1369  return true;
1370  }
1371 
1377  protected function readSubscriberData($a_usr_id)
1378  {
1379  global $ilDB;
1380 
1381  $query = "SELECT * FROM il_subscribers ".
1382  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1383  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
1384 
1385  $res = $this->ilDB->query($query);
1386  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1387  {
1388  $data["time"] = $row->sub_time;
1389  $data["usr_id"] = $row->usr_id;
1390  $data['subject'] = $row->subject;
1391  }
1392  return $data ? $data : array();
1393  }
1394 
1395  public static function lookupSubscribersData($a_obj_id)
1396  {
1397  global $ilDB;
1398 
1399  $query = 'SELECT * FROM il_subscribers '.
1400  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
1401  $res = $ilDB->query($query);
1402 
1403  $data = array();
1404  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1405  {
1406  $data[$row->usr_id]['time'] = $row->sub_time;
1407  $data[$row->usr_id]['usr_id'] = $row->usr_id;
1408  $data[$row->usr_id]['subject'] = $row->subject;
1409  }
1410  return $data;
1411  }
1412 }
1413 ?>