ILIAS  Release_5_0_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 
20 abstract class ilParticipants
21 {
22  protected $component = '';
23 
24  protected $obj_id = 0;
25  protected $type = '';
26  protected $ref_id = 0;
27 
28  protected $roles = array();
29  protected $role_data = array();
30 
31  protected $participants = array();
32  protected $participants_status = array();
33  protected $members = array();
34  protected $tutors = array();
35  protected $admins = array();
36 
37  protected $subscribers = array();
38 
39  protected $ilDB;
40  protected $lng;
41 
42 
50  public function __construct($a_component_name, $a_obj_id)
51  {
52  global $ilDB,$lng;
53 
54  $this->ilDB = $ilDB;
55  $this->lng = $lng;
56 
57  $this->component = $a_component_name;
58 
59  $this->obj_id = $a_obj_id;
60  $this->type = ilObject::_lookupType($a_obj_id);
61  $ref_ids = ilObject::_getAllReferences($this->obj_id);
62  $this->ref_id = current($ref_ids);
63 
64  $this->readParticipants();
65  $this->readParticipantsStatus();
66  }
67 
75  public static function getInstanceByObjId($a_obj_id)
76  {
77  $type = ilObject::_lookupType($a_obj_id);
78  switch($type)
79  {
80  case 'crs':
81  include_once './Modules/Course/classes/class.ilCourseParticipants.php';
83 
84  case 'grp':
85  include_once './Modules/Group/classes/class.ilGroupParticipants.php';
87 
88  case 'sess':
89  include_once './Modules/Session/classes/class.ilSessionParticipants.php';
91 
92  default:
93  $GLOBALS['ilLog']->logStack();
94  $GLOBALS['ilLog']->write(__METHOD__.': Invalid obj_id given: '.$a_obj_id);
95  throw new InvalidArgumentException('Invalid obj id given');
96  }
97  }
98 
103  protected function getComponent()
104  {
105  return $this->component;
106  }
107 
108 
109 
115  public static function hasParticipantListAccess($a_obj_id, $a_usr_id = null)
116  {
117  if(!$a_usr_id)
118  {
119  $a_usr_id = $GLOBALS['ilUser']->getId();
120  }
121 
122  // if write access granted => return true
123  $refs = ilObject::_getAllReferences($a_obj_id);
124  $ref_id = end($refs);
125 
126  if($GLOBALS['ilAccess']->checkAccess('write','',$ref_id))
127  {
128  return true;
129  }
130  $part = self::getInstanceByObjId($a_obj_id);
131  if($part->isAssigned($a_usr_id))
132  {
133  if($part->getType() == 'crs')
134  {
135  // Check for show_members
136  include_once './Modules/Course/classes/class.ilObjCourse.php';
138  {
139  return false;
140  }
141  }
142  return true;
143  }
144  // User is not assigned to course/group => no read access
145  return false;
146  }
147 
159  public static function _getMembershipByType($a_usr_id,$a_type,$a_only_member_role = false)
160  {
161  global $ilDB;
162 
163  // this will also dismiss local roles!
164  if ($a_only_member_role)
165  {
166  $j2 = "JOIN object_data obd2 ON (ua.rol_id = obd2.obj_id) ";
167  $a2 = "AND obd2.title LIKE 'il_".$a_type."_mem%' ";
168  }
169 
170  // #14290 - no role folder anymore
171  $query = "SELECT DISTINCT obd.obj_id,obr.ref_id FROM rbac_ua ua ".
172  "JOIN rbac_fa fa ON ua.rol_id = fa.rol_id ".
173  "JOIN object_reference obr ON fa.parent = obr.ref_id ".
174  "JOIN object_data obd ON obr.obj_id = obd.obj_id ".
175  $j2.
176  "WHERE obd.type = ".$ilDB->quote($a_type,'text')." ".
177  "AND fa.assign = 'y' ".
178  "AND ua.usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
179  $a2;
180 
181  $res = $ilDB->query($query);
182  while($row = $ilDB->fetchObject($res))
183  {
184  $ref_ids[] = $row->obj_id;
185  }
186 
187  return $ref_ids ? $ref_ids : array();
188  }
189 
190 
191 
200  public static function _isParticipant($a_ref_id,$a_usr_id)
201  {
202  global $rbacreview,$ilObjDataCache,$ilDB,$ilLog;
203 
204  $local_roles = $rbacreview->getRolesOfRoleFolder($a_ref_id,false);
205 
206  return $rbacreview->isAssignedToAtLeastOneGivenRole($a_usr_id, $local_roles);
207  }
208 
216  public static function lookupNumberOfParticipants($a_ref_id)
217  {
218  global $rbacreview;
219 
220  $lroles = $rbacreview->getRolesOfRoleFolder($a_ref_id,false);
221  return $rbacreview->getNumberOfAssignedUsers($lroles);
222  }
223 
231  public static function lookupNumberOfMembers($a_ref_id)
232  {
233  global $rbacreview, $ilObjDataCache;
234 
235  $has_policies = $rbacreview->getLocalPolicies($a_ref_id);
236 
237  if(!$has_policies)
238  {
239  return 0;
240  }
241  $lroles = $rbacreview->getRolesOfRoleFolder($a_ref_id,false);
242 
243  $memberRoles = array();
244  foreach($lroles as $role_id)
245  {
246  $title = $ilObjDataCache->lookupTitle($role_id);
247  switch(substr($title,0,8))
248  {
249  case 'il_crs_a':
250  case 'il_crs_t':
251  case 'il_grp_a':
252  break;
253 
254  default:
255  $memberRoles[] = $role_id;
256  break;
257  }
258  }
259  return $rbacreview->getNumberOfAssignedUsers($memberRoles);
260  }
261 
262 
272  public static function _isBlocked($a_obj_id,$a_usr_id)
273  {
274  global $ilDB;
275 
276  $query = "SELECT * FROM obj_members ".
277  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
278  "AND usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
279  "AND blocked = ".$ilDB->quote(1,'integer');
280  $res = $ilDB->query($query);
281  return $res->numRows() ? true : false;
282  }
283 
293  public static function _hasPassed($a_obj_id,$a_usr_id)
294  {
295  global $ilDB;
296 
297  $query = "SELECT * FROM obj_members ".
298  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
299  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
300  "AND passed = '1'";
301  $res = $ilDB->query($query);
302  return $res->numRows() ? true : false;
303  }
304 
314  public static function _deleteAllEntries($a_obj_id)
315  {
316  global $ilDB;
317 
318  $query = "DELETE FROM obj_members ".
319  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
320  $res = $ilDB->manipulate($query);
321 
322  $query = "DELETE FROM il_subscribers ".
323  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
324  $res = $ilDB->manipulate($query);
325 
326  $query = 'DELETE FROM crs_waiting_list '.
327  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
328  $ilDB->manipulate($query);
329 
330  return true;
331  }
332 
341  public static function _deleteUser($a_usr_id)
342  {
343  global $ilDB;
344 
345  $query = "DELETE FROM obj_members WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
346  $res = $ilDB->manipulate($query);
347 
348  $query = "DELETE FROM il_subscribers WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
349  $res = $ilDB->manipulate($query);
350 
351  include_once './Modules/Course/classes/class.ilCourseWaitingList.php';
353  }
354 
355  public static function getDefaultMemberRole($a_ref_id)
356  {
357  global $ilCtrl;
358 
359  $obj_id = ilObject::_lookupObjId($a_ref_id);
361 
362  if(!in_array($type,array('crs','grp')))
363  {
364  return 0;
365  }
366 
367  global $rbacreview;
368 
369 
370  $roles = $rbacreview->getRolesOfRoleFolder($a_ref_id,false);
371 
372  foreach($roles as $role)
373  {
374  $title = ilObject::_lookupTitle($role);
375  if(substr($title, 0, 13) == ('il_'.$type.'_member'))
376  {
377  return $role;
378  }
379  }
380  return 0;
381  }
382 
387  public function getObjId()
388  {
389  return $this->obj_id;
390  }
391 
396  public function getType()
397  {
398  return $this->type;
399  }
400 
407  public function getNotificationRecipients()
408  {
409  global $ilDB;
410 
411  $query = "SELECT * FROM obj_members ".
412  "WHERE notification = 1 ".
413  "AND obj_id = ".$ilDB->quote($this->obj_id)." ";
414  $res = $ilDB->query($query);
415  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
416  {
417  if($this->isAdmin($row->usr_id) or $this->isTutor($row->usr_id))
418  {
419  $recp[] = $row->usr_id;
420  }
421  }
422  return $recp ? $recp : array();
423  }
424 
431  public function getCountMembers()
432  {
433  return count($this->members);
434  }
435 
442  public function getCountParticipants()
443  {
444  return count($this->participants);
445  }
446 
447 
448 
449 
456  public function getParticipants()
457  {
458  return $this->participants ? $this->participants : array();
459  }
460 
468  public function getMembers()
469  {
470  return $this->members ? $this->members : array();
471  }
478  public function getAdmins()
479  {
480  return $this->admins ? $this->admins : array();
481  }
482 
487  public function getCountAdmins()
488  {
489  return count($this->getAdmins());
490  }
491 
492 
499  public function getTutors()
500  {
501  return $this->tutors ? $this->tutors : array();
502  }
503 
511  public function isAdmin($a_usr_id)
512  {
513  return in_array($a_usr_id,$this->admins) ? true : false;
514  }
515 
523  public function isTutor($a_usr_id)
524  {
525  return in_array($a_usr_id,$this->tutors) ? true : false;
526  }
527 
535  public function isMember($a_usr_id)
536  {
537  return in_array($a_usr_id,$this->members) ? true : false;
538  }
539 
540 
541 
542 
550  public function isAssigned($a_usr_id)
551  {
552  return in_array($a_usr_id,$this->participants);
553  }
554 
560  public function isLastAdmin($a_usr_id)
561  {
562  return in_array($a_usr_id,$this->getAdmins()) and count($this->getAdmins()) == 1;
563  }
564 
565 
573  public function getRoles()
574  {
575  return $this->roles ? $this->roles : array();
576  }
577 
585  public function getAssignedRoles($a_usr_id)
586  {
587  global $rbacreview;
588 
589  foreach($this->roles as $role)
590  {
591  if($rbacreview->isAssigned($a_usr_id,$role))
592  {
593  $assigned[] = $role;
594  }
595  }
596  return $assigned ? $assigned : array();
597  }
598 
607  public function updateRoleAssignments($a_usr_id,$a_roles)
608  {
609  global $rbacreview,$rbacadmin;
610 
611  $roles = $a_roles ? $a_roles : array();
612 
613  foreach($this->getRoles() as $role_id)
614  {
615  if($rbacreview->isAssigned($a_usr_id,$role_id))
616  {
617  if(!in_array($role_id,$roles))
618  {
619  $rbacadmin->deassignUser($role_id,$a_usr_id);
620  }
621  }
622  else
623  {
624  if(in_array($role_id,$roles))
625  {
626  $rbacadmin->assignUser($role_id,$a_usr_id);
627  }
628  }
629  }
630  $this->readParticipants();
631  $this->readParticipantsStatus();
632  }
633 
641  public function checkLastAdmin($a_usr_ids)
642  {
643  foreach($this->getAdmins() as $admin_id)
644  {
645  if(!in_array($admin_id,$a_usr_ids))
646  {
647  return true;
648  }
649  }
650  return false;
651  }
652 
660  public function isBlocked($a_usr_id)
661  {
662  if(isset($this->participants_status[$a_usr_id]))
663  {
664  return $this->participants_status[$a_usr_id]['blocked'] ? true : false;
665  }
666  return false;
667  }
668 
676  public function hasPassed($a_usr_id)
677  {
678  if(isset($this->participants_status[$a_usr_id]))
679  {
680  return $this->participants_status[$a_usr_id]['passed'] ? true : false;
681  }
682  return false;
683  }
684 
692  public function delete($a_usr_id)
693  {
694  global $rbacadmin,$ilDB;
695 
696  $this->dropDesktopItem($a_usr_id);
697  foreach($this->roles as $role_id)
698  {
699  $rbacadmin->deassignUser($role_id,$a_usr_id);
700  }
701 
702  $query = "DELETE FROM obj_members ".
703  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
704  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer');
705  $res = $ilDB->manipulate($query);
706 
707  $this->readParticipants();
708  $this->readParticipantsStatus();
709 
710  $GLOBALS['ilAppEventHandler']->raise(
711  $this->getComponent(),
712  "deleteParticipant",
713  array(
714  'obj_id' => $this->obj_id,
715  'usr_id' => $a_usr_id)
716  );
717 
718  return true;
719  }
720 
729  public function updateBlocked($a_usr_id,$a_blocked)
730  {
731  global $ilDB;
732 
733  $this->participants_status[$a_usr_id]['blocked'] = (int) $a_blocked;
734 
735  $query = "SELECT * FROM obj_members ".
736  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
737  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
738  $res = $ilDB->query($query);
739  if($res->numRows())
740  {
741  $query = "UPDATE obj_members SET ".
742  "blocked = ".$ilDB->quote((int) $a_blocked ,'integer')." ".
743  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
744  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
745  }
746  else
747  {
748  $query = "INSERT INTO obj_members (blocked,obj_id,usr_id,notification,passed) ".
749  "VALUES ( ".
750  $ilDB->quote((int) $a_blocked ,'integer').", ".
751  $ilDB->quote($this->obj_id ,'integer').", ".
752  $ilDB->quote($a_usr_id ,'integer').", ".
753  $ilDB->quote(0,'integer').", ".
754  $ilDB->quote(0,'integer').
755  ")";
756 
757  }
758  $res = $ilDB->manipulate($query);
759  return true;
760  }
761 
770  public function updateNotification($a_usr_id,$a_notification)
771  {
772  global $ilDB;
773 
774  $this->participants_status[$a_usr_id]['notification'] = (int) $a_notification;
775 
776  $query = "SELECT * FROM obj_members ".
777  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
778  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
779  $res = $ilDB->query($query);
780  if($res->numRows())
781  {
782  $query = "UPDATE obj_members SET ".
783  "notification = ".$ilDB->quote((int) $a_notification ,'integer')." ".
784  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
785  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
786  }
787  else
788  {
789  $query = "INSERT INTO obj_members (notification,obj_id,usr_id,passed,blocked) ".
790  "VALUES ( ".
791  $ilDB->quote((int) $a_notification ,'integer').", ".
792  $ilDB->quote($this->obj_id ,'integer').", ".
793  $ilDB->quote($a_usr_id ,'integer').", ".
794  $ilDB->quote(0,'integer').", ".
795  $ilDB->quote(0,'integer').
796  ")";
797 
798  }
799  $res = $ilDB->manipulate($query);
800  return true;
801  }
802 
803 
804 
805 
814  public function add($a_usr_id,$a_role)
815  {
816  global $rbacadmin,$ilLog,$ilAppEventHandler;
817 
818  if($this->isAssigned($a_usr_id))
819  {
820  return false;
821  }
822 
823  switch($a_role)
824  {
825  case IL_CRS_ADMIN:
826  $this->admins[] = $a_usr_id;
827  break;
828 
829  case IL_CRS_TUTOR:
830  $this->tutors[] = $a_usr_id;
831  break;
832 
833  case IL_CRS_MEMBER:
834  $this->members[] = $a_usr_id;
835  break;
836 
837  case IL_GRP_ADMIN:
838  $this->admins[] = $a_usr_id;
839  break;
840 
841  case IL_GRP_MEMBER:
842  $this->members[] = $a_usr_id;
843  break;
844  }
845 
846  $this->participants[] = $a_usr_id;
847  $rbacadmin->assignUser($this->role_data[$a_role],$a_usr_id);
848  $this->addDesktopItem($a_usr_id);
849 
850  // Delete subscription request
851  $this->deleteSubscriber($a_usr_id);
852 
853  include_once './Services/Membership/classes/class.ilWaitingList.php';
854  ilWaitingList::deleteUserEntry($a_usr_id,$this->obj_id);
855 
856  $ilLog->write(__METHOD__.': Raise new event: Modules/Course|Group addParticipant');
857  $ilAppEventHandler->raise(
858  $this->getComponent(),
859  "addParticipant",
860  array(
861  'obj_id' => $this->obj_id,
862  'usr_id' => $a_usr_id,
863  'role_id' => $a_role)
864  );
865  return true;
866  }
867 
868 
876  public function deleteParticipants($a_user_ids)
877  {
878  foreach($a_user_ids as $user_id)
879  {
880  $this->delete($user_id);
881  }
882  return true;
883  }
884 
892  public function addDesktopItem($a_usr_id)
893  {
894  if(!ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
895  {
896  ilObjUser::_addDesktopItem($a_usr_id, $this->ref_id,$this->type);
897  }
898  return true;
899  }
900 
908  function dropDesktopItem($a_usr_id)
909  {
910  if(ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
911  {
912  ilObjUser::_dropDesktopItem($a_usr_id, $this->ref_id,$this->type);
913  }
914 
915  return true;
916  }
917 
918 
919 
927  public function isNotificationEnabled($a_usr_id)
928  {
929  if(isset($this->participants_status[$a_usr_id]))
930  {
931  return $this->participants_status[$a_usr_id]['notification'] ? true : false;
932  }
933  return false;
934  }
935 
936 
944  protected function readParticipants()
945  {
946  global $rbacreview,$ilObjDataCache,$ilLog;
947 
948  $this->roles = $rbacreview->getRolesOfRoleFolder($this->ref_id,false);
949 
950  $users = array();
951  $this->participants = array();
952  $this->members = $this->admins = $this->tutors = array();
953 
954  foreach($this->roles as $role_id)
955  {
956  $title = $ilObjDataCache->lookupTitle($role_id);
957  switch(substr($title,0,8))
958  {
959  case 'il_crs_m':
960  $this->role_data[IL_CRS_MEMBER] = $role_id;
961  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
962  $this->members = array_unique(array_merge($assigned,$this->members));
963  break;
964 
965  case 'il_crs_a':
966  $this->role_data[IL_CRS_ADMIN] = $role_id;
967  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
968  $this->admins = $rbacreview->assignedUsers($role_id);
969  break;
970 
971  case 'il_crs_t':
972  $this->role_data[IL_CRS_TUTOR] = $role_id;
973  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
974  $this->tutors = $rbacreview->assignedUsers($role_id);
975  break;
976 
977  case 'il_grp_a':
978  $this->role_data[IL_GRP_ADMIN] = $role_id;
979  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
980  $this->admins = $rbacreview->assignedUsers($role_id);
981  break;
982 
983  case 'il_grp_m':
984  $this->role_data[IL_GRP_MEMBER] = $role_id;
985  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
986  $this->members = $rbacreview->assignedUsers($role_id);
987  break;
988 
989  default:
990  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
991  $this->members = array_unique(array_merge($assigned,$this->members));
992  break;
993  }
994  }
995  }
996 
1004  protected function readParticipantsStatus()
1005  {
1006  global $ilDB;
1007 
1008  $query = "SELECT * FROM obj_members ".
1009  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1010  $res = $ilDB->query($query);
1011  $this->participants_status = array();
1012  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1013  {
1014  $this->participants_status[$row->usr_id]['blocked'] = $row->blocked;
1015  $this->participants_status[$row->usr_id]['notification'] = $row->notification;
1016  $this->participants_status[$row->usr_id]['passed'] = $row->passed;
1017  }
1018  }
1019 
1027  public function isGroupingMember($a_usr_id,$a_field = '')
1028  {
1029  global $rbacreview,$ilObjDataCache,$ilDB;
1030 
1031  // Used for membership limitations -> check membership by given field
1032  if($a_field)
1033  {
1034  include_once './Services/User/classes/class.ilObjUser.php';
1035 
1036  $tmp_user =& ilObjectFactory::getInstanceByObjId($a_usr_id);
1037  switch($a_field)
1038  {
1039  case 'login':
1040  $and = "AND login = ".$ilDB->quote($tmp_user->getLogin(),'text')." ";
1041  break;
1042  case 'email':
1043  $and = "AND email = ".$ilDB->quote($tmp_user->getEmail(),'text')." ";
1044  break;
1045  case 'matriculation':
1046  $and = "AND matriculation = ".$ilDB->quote($tmp_user->getMatriculation(),'text')." ";
1047  break;
1048 
1049  default:
1050  $and = "AND usr_id = ".$ilDB->quote($a_usr_id,'integer'). " ";
1051  break;
1052  }
1053 
1054  if(!$this->getParticipants())
1055  {
1056  return false;
1057  }
1058 
1059  $query = "SELECT * FROM usr_data ud ".
1060  "WHERE ".$ilDB->in('usr_id',$this->getParticipants(),false,'integer')." ".
1061  $and;
1062 
1063  $res = $ilDB->query($query);
1064  return $res->numRows() ? true : false;
1065  }
1066  }
1067 
1068  public static function lookupSubscribers($a_obj_id)
1069  {
1070  global $ilDB;
1071 
1072  $subscribers = array();
1073  $query = "SELECT usr_id FROM il_subscribers ".
1074  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
1075  "ORDER BY sub_time ";
1076 
1077  $res = $ilDB->query($query);
1078  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1079  {
1080  $subscribers[] = $row->usr_id;
1081  }
1082  return $subscribers;
1083  }
1084 
1090  public function getSubscribers()
1091  {
1092  $this->readSubscribers();
1093 
1094  return $this->subscribers;
1095  }
1096 
1097 
1103  public function getCountSubscribers()
1104  {
1105  return count($this->getSubscribers());
1106  }
1107 
1113  public function getSubscriberData($a_usr_id)
1114  {
1115  return $this->readSubscriberData($a_usr_id);
1116  }
1117 
1118 
1119 
1125  public function assignSubscribers($a_usr_ids)
1126  {
1127  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1128  {
1129  return false;
1130  }
1131  foreach($a_usr_ids as $id)
1132  {
1133  if(!$this->assignSubscriber($id))
1134  {
1135  return false;
1136  }
1137  }
1138  return true;
1139  }
1140 
1146  public function assignSubscriber($a_usr_id)
1147  {
1148  global $ilErr;
1149 
1150  $ilErr->setMessage("");
1151  if(!$this->isSubscriber($a_usr_id))
1152  {
1153  $ilErr->appendMessage($this->lng->txt("crs_user_notsubscribed"));
1154 
1155  return false;
1156  }
1157  if($this->isAssigned($a_usr_id))
1158  {
1159  $tmp_obj = ilObjectFactory::getInstanceByObjId($a_usr_id);
1160  $ilErr->appendMessage($tmp_obj->getLogin().": ".$this->lng->txt("crs_user_already_assigned"));
1161 
1162  return false;
1163  }
1164 
1165  if(!$tmp_obj =& ilObjectFactory::getInstanceByObjId($a_usr_id))
1166  {
1167  $ilErr->appendMessage($this->lng->txt("crs_user_not_exists"));
1168 
1169  return false;
1170  }
1171 
1172  // TODO: must be group or course member role
1173  $this->add($tmp_obj->getId(),IL_CRS_MEMBER);
1174  $this->deleteSubscriber($a_usr_id);
1175 
1176  return true;
1177  }
1178 
1184  public function autoFillSubscribers()
1185  {
1186  $this->readSubscribers();
1187 
1188  $counter = 0;
1189  foreach($this->subscribers as $subscriber)
1190  {
1191  if(!$this->assignSubscriber($subscriber))
1192  {
1193  continue;
1194  }
1195  else
1196  {
1197  // TODO: notification
1198  #$this->sendNotification($this->NOTIFY_ACCEPT_SUBSCRIBER,$subscriber);
1199  }
1200  ++$counter;
1201  }
1202 
1203  return $counter;
1204  }
1205 
1211  public function addSubscriber($a_usr_id)
1212  {
1213  global $ilDB;
1214 
1215  $query = "INSERT INTO il_subscribers (usr_id,obj_id,subject,sub_time) ".
1216  " VALUES (".
1217  $ilDB->quote($a_usr_id ,'integer').",".
1218  $ilDB->quote($this->obj_id ,'integer').", ".
1219  $ilDB->quote('','text').", ".
1220  $ilDB->quote(time() ,'integer').
1221  ")";
1222  $res = $ilDB->manipulate($query);
1223 
1224  return true;
1225  }
1226 
1227 
1233  public function updateSubscriptionTime($a_usr_id,$a_subtime)
1234  {
1235  global $ilDB;
1236 
1237  $query = "UPDATE il_subscribers ".
1238  "SET sub_time = ".$ilDB->quote($a_subtime ,'integer')." ".
1239  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1240  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1241  $res = $ilDB->manipulate($query);
1242 
1243  return true;
1244  }
1245 
1253  public function updateSubject($a_usr_id,$a_subject)
1254  {
1255  global $ilDB;
1256 
1257  $query = "UPDATE il_subscribers ".
1258  "SET subject = ".$ilDB->quote($a_subject ,'text')." ".
1259  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1260  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1261  $res = $ilDB->manipulate($query);
1262  return true;
1263  }
1264 
1265 
1271  public function deleteSubscriber($a_usr_id)
1272  {
1273  global $ilDB;
1274 
1275  $query = "DELETE FROM il_subscribers ".
1276  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1277  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1278  $res = $ilDB->manipulate($query);
1279 
1280  return true;
1281  }
1282 
1283 
1289  public function deleteSubscribers($a_usr_ids)
1290  {
1291  global $ilErr,$ilDB;
1292 
1293  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1294  {
1295  $ilErr->setMessage('');
1296  $ilErr->appendMessage($this->lng->txt("no_usr_ids_given"));
1297 
1298  return false;
1299  }
1300  $query = "DELETE FROM il_subscribers ".
1301  "WHERE ".$ilDB->in('usr_id',(array) $a_usr_ids,false,'integer')." ".
1302  "AND obj_id = ".$ilDB->quote($this->obj_id,'integer');
1303  $res = $ilDB->query($query);
1304  return true;
1305  }
1306 
1307 
1313  public function isSubscriber($a_usr_id)
1314  {
1315  global $ilDB;
1316 
1317  $query = "SELECT * FROM il_subscribers ".
1318  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1319  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')."";
1320 
1321  $res = $ilDB->query($query);
1322  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1323  {
1324  return true;
1325  }
1326  return false;
1327  }
1328 
1335  public static function _isSubscriber($a_obj_id,$a_usr_id)
1336  {
1337  global $ilDB;
1338 
1339  $query = "SELECT * FROM il_subscribers ".
1340  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1341  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
1342 
1343  $res = $ilDB->query($query);
1344  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1345  {
1346  return true;
1347  }
1348  return false;
1349  }
1350 
1356  protected function readSubscribers()
1357  {
1358  global $ilDB;
1359 
1360  $this->subscribers = array();
1361 
1362  $query = "SELECT usr_id FROM il_subscribers ".
1363  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1364  "ORDER BY sub_time ";
1365 
1366  $res = $this->ilDB->query($query);
1367  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1368  {
1369  // DELETE SUBSCRIPTION IF USER HAS BEEN DELETED
1370  if(!ilObjectFactory::getInstanceByObjId($row->usr_id,false))
1371  {
1372  $this->deleteSubscriber($row->usr_id);
1373  }
1374  $this->subscribers[] = $row->usr_id;
1375  }
1376  return true;
1377  }
1378 
1384  protected function readSubscriberData($a_usr_id)
1385  {
1386  global $ilDB;
1387 
1388  $query = "SELECT * FROM il_subscribers ".
1389  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1390  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
1391 
1392  $res = $this->ilDB->query($query);
1393  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1394  {
1395  $data["time"] = $row->sub_time;
1396  $data["usr_id"] = $row->usr_id;
1397  $data['subject'] = $row->subject;
1398  }
1399  return $data ? $data : array();
1400  }
1401 
1402  public static function lookupSubscribersData($a_obj_id)
1403  {
1404  global $ilDB;
1405 
1406  $query = 'SELECT * FROM il_subscribers '.
1407  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
1408  $res = $ilDB->query($query);
1409 
1410  $data = array();
1411  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1412  {
1413  $data[$row->usr_id]['time'] = $row->sub_time;
1414  $data[$row->usr_id]['usr_id'] = $row->usr_id;
1415  $data[$row->usr_id]['subject'] = $row->subject;
1416  }
1417  return $data;
1418  }
1419 }
1420 ?>