ILIAS  release_4-3 Revision
 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  {
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  return true;
692  }
693 
702  public function updateBlocked($a_usr_id,$a_blocked)
703  {
704  global $ilDB;
705 
706  $this->participants_status[$a_usr_id]['blocked'] = (int) $a_blocked;
707 
708  $query = "SELECT * FROM obj_members ".
709  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
710  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
711  $res = $ilDB->query($query);
712  if($res->numRows())
713  {
714  $query = "UPDATE obj_members SET ".
715  "blocked = ".$ilDB->quote((int) $a_blocked ,'integer')." ".
716  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
717  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
718  }
719  else
720  {
721  $query = "INSERT INTO obj_members (blocked,obj_id,usr_id,notification,passed) ".
722  "VALUES ( ".
723  $ilDB->quote((int) $a_blocked ,'integer').", ".
724  $ilDB->quote($this->obj_id ,'integer').", ".
725  $ilDB->quote($a_usr_id ,'integer').", ".
726  $ilDB->quote(0,'integer').", ".
727  $ilDB->quote(0,'integer').
728  ")";
729 
730  }
731  $res = $ilDB->manipulate($query);
732  return true;
733  }
734 
743  public function updateNotification($a_usr_id,$a_notification)
744  {
745  global $ilDB;
746 
747  $this->participants_status[$a_usr_id]['notification'] = (int) $a_notification;
748 
749  $query = "SELECT * FROM obj_members ".
750  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
751  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
752  $res = $ilDB->query($query);
753  if($res->numRows())
754  {
755  $query = "UPDATE obj_members SET ".
756  "notification = ".$ilDB->quote((int) $a_notification ,'integer')." ".
757  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
758  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
759  }
760  else
761  {
762  $query = "INSERT INTO obj_members (notification,obj_id,usr_id,passed,blocked) ".
763  "VALUES ( ".
764  $ilDB->quote((int) $a_notification ,'integer').", ".
765  $ilDB->quote($this->obj_id ,'integer').", ".
766  $ilDB->quote($a_usr_id ,'integer').", ".
767  $ilDB->quote(0,'integer').", ".
768  $ilDB->quote(0,'integer').
769  ")";
770 
771  }
772  $res = $ilDB->manipulate($query);
773  return true;
774  }
775 
776 
777 
778 
787  public function add($a_usr_id,$a_role)
788  {
789  global $rbacadmin,$ilLog,$ilAppEventHandler;
790 
791  if($this->isAssigned($a_usr_id))
792  {
793  return false;
794  }
795 
796  switch($a_role)
797  {
798  case IL_CRS_ADMIN:
799  $this->admins[] = $a_usr_id;
800  break;
801 
802  case IL_CRS_TUTOR:
803  $this->tutors[] = $a_usr_id;
804  break;
805 
806  case IL_CRS_MEMBER:
807  $this->members[] = $a_usr_id;
808  break;
809 
810  case IL_GRP_ADMIN:
811  $this->admins[] = $a_usr_id;
812  break;
813 
814  case IL_GRP_MEMBER:
815  $this->members[] = $a_usr_id;
816  break;
817  }
818 
819  $this->participants[] = $a_usr_id;
820  $rbacadmin->assignUser($this->role_data[$a_role],$a_usr_id);
821  $this->addDesktopItem($a_usr_id);
822 
823  // Delete subscription request
824  $this->deleteSubscriber($a_usr_id);
825 
826  include_once './Services/Membership/classes/class.ilWaitingList.php';
827  ilWaitingList::deleteUserEntry($a_usr_id,$this->obj_id);
828 
829  if($this->type == 'crs') {
830  // Add event: used for ecs accounts
831  $ilLog->write(__METHOD__.': Raise new event: Modules/Course addParticipant');
832  $ilAppEventHandler->raise(
833  "Modules/Course",
834  "addParticipant",
835  array(
836  'obj_id' => $this->obj_id,
837  'usr_id' => $a_usr_id,
838  'role_id' => $a_role)
839  );
840  }
841  return true;
842  }
843 
844 
852  public function deleteParticipants($a_user_ids)
853  {
854  foreach($a_user_ids as $user_id)
855  {
856  $this->delete($user_id);
857  }
858  return true;
859  }
860 
868  public function addDesktopItem($a_usr_id)
869  {
870  if(!ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
871  {
872  ilObjUser::_addDesktopItem($a_usr_id, $this->ref_id,$this->type);
873  }
874  return true;
875  }
876 
884  function dropDesktopItem($a_usr_id)
885  {
886  if(ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
887  {
888  ilObjUser::_dropDesktopItem($a_usr_id, $this->ref_id,$this->type);
889  }
890 
891  return true;
892  }
893 
894 
895 
903  public function isNotificationEnabled($a_usr_id)
904  {
905  if(isset($this->participants_status[$a_usr_id]))
906  {
907  return $this->participants_status[$a_usr_id]['notification'] ? true : false;
908  }
909  return false;
910  }
911 
912 
920  private function readParticipants()
921  {
922  global $rbacreview,$ilObjDataCache,$ilLog;
923 
924  $rolf = $rbacreview->getRoleFolderOfObject($this->ref_id);
925 
926  if(!isset($rolf['ref_id']) or !$rolf['ref_id'])
927  {
928  $title = $ilObjDataCache->lookupTitle($ilObjDataCache->lookupObjId($this->ref_id));
929  $ilLog->write(__METHOD__.': Found object without role folder. Ref_id: '.$this->ref_id.', title: '.$title);
930  $ilLog->logStack();
931  return false;
932  }
933 
934  $this->roles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
935 
936  $users = array();
937  $this->participants = array();
938  $this->members = $this->admins = $this->tutors = array();
939 
940  foreach($this->roles as $role_id)
941  {
942  $title = $ilObjDataCache->lookupTitle($role_id);
943  switch(substr($title,0,8))
944  {
945  case 'il_crs_m':
946  $this->role_data[IL_CRS_MEMBER] = $role_id;
947  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
948  $this->members = array_unique(array_merge($assigned,$this->members));
949  break;
950 
951  case 'il_crs_a':
952  $this->role_data[IL_CRS_ADMIN] = $role_id;
953  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
954  $this->admins = $rbacreview->assignedUsers($role_id);
955  break;
956 
957  case 'il_crs_t':
958  $this->role_data[IL_CRS_TUTOR] = $role_id;
959  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
960  $this->tutors = $rbacreview->assignedUsers($role_id);
961  break;
962 
963  case 'il_grp_a':
964  $this->role_data[IL_GRP_ADMIN] = $role_id;
965  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
966  $this->admins = $rbacreview->assignedUsers($role_id);
967  break;
968 
969  case 'il_grp_m':
970  $this->role_data[IL_GRP_MEMBER] = $role_id;
971  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
972  $this->members = $rbacreview->assignedUsers($role_id);
973  break;
974 
975  default:
976  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
977  $this->members = array_unique(array_merge($assigned,$this->members));
978  break;
979  }
980  }
981  }
982 
990  private function readParticipantsStatus()
991  {
992  global $ilDB;
993 
994  $query = "SELECT * FROM obj_members ".
995  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
996  $res = $ilDB->query($query);
997  $this->participants_status = array();
998  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
999  {
1000  $this->participants_status[$row->usr_id]['blocked'] = $row->blocked;
1001  $this->participants_status[$row->usr_id]['notification'] = $row->notification;
1002  $this->participants_status[$row->usr_id]['passed'] = $row->passed;
1003  }
1004  }
1005 
1013  public function isGroupingMember($a_usr_id,$a_field = '')
1014  {
1015  global $rbacreview,$ilObjDataCache,$ilDB;
1016 
1017  // Used for membership limitations -> check membership by given field
1018  if($a_field)
1019  {
1020  include_once './Services/User/classes/class.ilObjUser.php';
1021 
1022  $tmp_user =& ilObjectFactory::getInstanceByObjId($a_usr_id);
1023  switch($a_field)
1024  {
1025  case 'login':
1026  $and = "AND login = ".$ilDB->quote($tmp_user->getLogin(),'text')." ";
1027  break;
1028  case 'email':
1029  $and = "AND email = ".$ilDB->quote($tmp_user->getEmail(),'text')." ";
1030  break;
1031  case 'matriculation':
1032  $and = "AND matriculation = ".$ilDB->quote($tmp_user->getMatriculation(),'text')." ";
1033  break;
1034 
1035  default:
1036  $and = "AND usr_id = ".$ilDB->quote($a_usr_id,'integer'). " ";
1037  break;
1038  }
1039 
1040  if(!$this->getParticipants())
1041  {
1042  return false;
1043  }
1044 
1045  $query = "SELECT * FROM usr_data ud ".
1046  "WHERE ".$ilDB->in('usr_id',$this->getParticipants(),false,'integer')." ".
1047  $and;
1048 
1049  $res = $ilDB->query($query);
1050  return $res->numRows() ? true : false;
1051  }
1052  }
1053 
1054  public static function lookupSubscribers($a_obj_id)
1055  {
1056  global $ilDB;
1057 
1058  $subscribers = array();
1059  $query = "SELECT usr_id FROM il_subscribers ".
1060  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
1061  "ORDER BY sub_time ";
1062 
1063  $res = $ilDB->query($query);
1064  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1065  {
1066  $subscribers[] = $row->usr_id;
1067  }
1068  return $subscribers;
1069  }
1070 
1076  public function getSubscribers()
1077  {
1078  $this->readSubscribers();
1079 
1080  return $this->subscribers;
1081  }
1082 
1083 
1089  public function getCountSubscribers()
1090  {
1091  return count($this->getSubscribers());
1092  }
1093 
1099  public function getSubscriberData($a_usr_id)
1100  {
1101  return $this->readSubscriberData($a_usr_id);
1102  }
1103 
1104 
1105 
1111  public function assignSubscribers($a_usr_ids)
1112  {
1113  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1114  {
1115  return false;
1116  }
1117  foreach($a_usr_ids as $id)
1118  {
1119  if(!$this->assignSubscriber($id))
1120  {
1121  return false;
1122  }
1123  }
1124  return true;
1125  }
1126 
1132  public function assignSubscriber($a_usr_id)
1133  {
1134  global $ilErr;
1135 
1136  $ilErr->setMessage("");
1137  if(!$this->isSubscriber($a_usr_id))
1138  {
1139  $ilErr->appendMessage($this->lng->txt("crs_user_notsubscribed"));
1140 
1141  return false;
1142  }
1143  if($this->isAssigned($a_usr_id))
1144  {
1145  $tmp_obj = ilObjectFactory::getInstanceByObjId($a_usr_id);
1146  $ilErr->appendMessage($tmp_obj->getLogin().": ".$this->lng->txt("crs_user_already_assigned"));
1147 
1148  return false;
1149  }
1150 
1151  if(!$tmp_obj =& ilObjectFactory::getInstanceByObjId($a_usr_id))
1152  {
1153  $ilErr->appendMessage($this->lng->txt("crs_user_not_exists"));
1154 
1155  return false;
1156  }
1157 
1158  // TODO: must be group or course member role
1159  $this->add($tmp_obj->getId(),IL_CRS_MEMBER);
1160  $this->deleteSubscriber($a_usr_id);
1161 
1162  return true;
1163  }
1164 
1170  public function autoFillSubscribers()
1171  {
1172  $this->readSubscribers();
1173 
1174  $counter = 0;
1175  foreach($this->subscribers as $subscriber)
1176  {
1177  if(!$this->assignSubscriber($subscriber))
1178  {
1179  continue;
1180  }
1181  else
1182  {
1183  // TODO: notification
1184  #$this->sendNotification($this->NOTIFY_ACCEPT_SUBSCRIBER,$subscriber);
1185  }
1186  ++$counter;
1187  }
1188 
1189  return $counter;
1190  }
1191 
1197  public function addSubscriber($a_usr_id)
1198  {
1199  global $ilDB;
1200 
1201  $query = "INSERT INTO il_subscribers (usr_id,obj_id,subject,sub_time) ".
1202  " VALUES (".
1203  $ilDB->quote($a_usr_id ,'integer').",".
1204  $ilDB->quote($this->obj_id ,'integer').", ".
1205  $ilDB->quote('','text').", ".
1206  $ilDB->quote(time() ,'integer').
1207  ")";
1208  $res = $ilDB->manipulate($query);
1209 
1210  return true;
1211  }
1212 
1213 
1219  public function updateSubscriptionTime($a_usr_id,$a_subtime)
1220  {
1221  global $ilDB;
1222 
1223  $query = "UPDATE il_subscribers ".
1224  "SET sub_time = ".$ilDB->quote($a_subtime ,'integer')." ".
1225  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1226  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1227  $res = $ilDB->manipulate($query);
1228 
1229  return true;
1230  }
1231 
1239  public function updateSubject($a_usr_id,$a_subject)
1240  {
1241  global $ilDB;
1242 
1243  $query = "UPDATE il_subscribers ".
1244  "SET subject = ".$ilDB->quote($a_subject ,'text')." ".
1245  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1246  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1247  $res = $ilDB->manipulate($query);
1248  return true;
1249  }
1250 
1251 
1257  public function deleteSubscriber($a_usr_id)
1258  {
1259  global $ilDB;
1260 
1261  $query = "DELETE FROM il_subscribers ".
1262  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1263  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1264  $res = $ilDB->manipulate($query);
1265 
1266  return true;
1267  }
1268 
1269 
1275  public function deleteSubscribers($a_usr_ids)
1276  {
1277  global $ilErr,$ilDB;
1278 
1279  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1280  {
1281  $ilErr->setMessage('');
1282  $ilErr->appendMessage($this->lng->txt("no_usr_ids_given"));
1283 
1284  return false;
1285  }
1286  $query = "DELETE FROM il_subscribers ".
1287  "WHERE ".$ilDB->in('usr_id',(array) $a_usr_ids,false,'integer')." ".
1288  "AND obj_id = ".$ilDB->quote($this->obj_id,'integer');
1289  $res = $ilDB->query($query);
1290  return true;
1291  }
1292 
1293 
1299  public function isSubscriber($a_usr_id)
1300  {
1301  global $ilDB;
1302 
1303  $query = "SELECT * FROM il_subscribers ".
1304  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1305  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')."";
1306 
1307  $res = $ilDB->query($query);
1308  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1309  {
1310  return true;
1311  }
1312  return false;
1313  }
1314 
1321  public static function _isSubscriber($a_obj_id,$a_usr_id)
1322  {
1323  global $ilDB;
1324 
1325  $query = "SELECT * FROM il_subscribers ".
1326  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1327  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
1328 
1329  $res = $ilDB->query($query);
1330  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1331  {
1332  return true;
1333  }
1334  return false;
1335  }
1336 
1342  protected function readSubscribers()
1343  {
1344  global $ilDB;
1345 
1346  $this->subscribers = array();
1347 
1348  $query = "SELECT usr_id FROM il_subscribers ".
1349  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1350  "ORDER BY sub_time ";
1351 
1352  $res = $this->ilDB->query($query);
1353  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1354  {
1355  // DELETE SUBSCRIPTION IF USER HAS BEEN DELETED
1356  if(!ilObjectFactory::getInstanceByObjId($row->usr_id,false))
1357  {
1358  $this->deleteSubscriber($row->usr_id);
1359  }
1360  $this->subscribers[] = $row->usr_id;
1361  }
1362  return true;
1363  }
1364 
1370  protected function readSubscriberData($a_usr_id)
1371  {
1372  global $ilDB;
1373 
1374  $query = "SELECT * FROM il_subscribers ".
1375  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1376  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
1377 
1378  $res = $this->ilDB->query($query);
1379  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1380  {
1381  $data["time"] = $row->sub_time;
1382  $data["usr_id"] = $row->usr_id;
1383  $data['subject'] = $row->subject;
1384  }
1385  return $data ? $data : array();
1386  }
1387 
1388  public static function lookupSubscribersData($a_obj_id)
1389  {
1390  global $ilDB;
1391 
1392  $query = 'SELECT * FROM il_subscribers '.
1393  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
1394  $res = $ilDB->query($query);
1395 
1396  $data = array();
1397  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1398  {
1399  $data[$row->usr_id]['time'] = $row->sub_time;
1400  $data[$row->usr_id]['usr_id'] = $row->usr_id;
1401  $data[$row->usr_id]['subject'] = $row->subject;
1402  }
1403  return $data;
1404  }
1405 }
1406 ?>