ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilParticipants.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 define("IL_CRS_ADMIN",1);
33 define("IL_CRS_TUTOR",3);
34 define("IL_CRS_MEMBER",2);
35 
36 define('IL_GRP_ADMIN',4);
37 define('IL_GRP_MEMBER',5);
38 
39 
41 {
42  protected $obj_id = 0;
43  protected $type = '';
44  protected $ref_id = 0;
45 
46  protected $roles = array();
47  protected $role_data = array();
48 
49  protected $participants = array();
50  protected $participants_status = array();
51  protected $members = array();
52  protected $tutors = array();
53  protected $admins = array();
54 
55  protected $subscribers = array();
56 
57  protected $ilDB;
58  protected $lng;
59 
60 
68  public function __construct($a_obj_id)
69  {
70  global $ilDB,$lng;
71 
72  $this->ilDB = $ilDB;
73  $this->lng = $lng;
74 
75  $this->obj_id = $a_obj_id;
76  $this->type = ilObject::_lookupType($a_obj_id);
77  $ref_ids = ilObject::_getAllReferences($this->obj_id);
78  $this->ref_id = current($ref_ids);
79 
80  $this->readParticipants();
81  $this->readParticipantsStatus();
82  }
83 
95  public static function _getMembershipByType($a_usr_id,$a_type,$a_only_member_role = false)
96  {
97  global $ilDB;
98 
99  // this will also dismiss local roles!
100  if ($a_only_member_role)
101  {
102  $j2 = "JOIN object_data obd2 ON (ua.rol_id = obd2.obj_id) ";
103  $a2 = "AND obd2.title LIKE 'il_".$a_type."_mem%' ";
104  }
105 
106  $query = "SELECT DISTINCT obd.obj_id,obr.ref_id FROM rbac_ua ua ".
107  "JOIN rbac_fa fa ON ua.rol_id = fa.rol_id ".
108  "JOIN tree t1 ON t1.child = fa.parent ".
109  "JOIN object_reference obr ON t1.parent = obr.ref_id ".
110  "JOIN object_data obd ON obr.obj_id = obd.obj_id ".
111  $j2.
112  "WHERE obd.type = ".$ilDB->quote($a_type,'text')." ".
113  "AND fa.assign = 'y' ".
114  "AND ua.usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
115  $a2;
116 
117  $res = $ilDB->query($query);
118 
119  while($row = $ilDB->fetchObject($res))
120  {
121  $ref_ids[] = $row->obj_id;
122  }
123 
124  return $ref_ids ? $ref_ids : array();
125  }
126 
127 
128 
137  public static function _isParticipant($a_ref_id,$a_usr_id)
138  {
139  global $rbacreview,$ilObjDataCache,$ilDB,$ilLog;
140 
141  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
142  if(!isset($rolf['ref_id']) or !$rolf['ref_id'])
143  {
144  $title = $ilObjDataCache->lookupTitle($ilObjDataCache->lookupObjId($a_ref_id));
145  $ilLog->write(__METHOD__.': Found object without role folder. Ref_id: '.$a_ref_id.', title: '.$title);
146  $ilLog->logStack();
147 
148  return false;
149  }
150  $local_roles = $rbacreview->getRolesOfRoleFolder($rolf["ref_id"],false);
151 
152  return $rbacreview->isAssignedToAtLeastOneGivenRole($a_usr_id, $local_roles);
153  }
154 
162  public static function lookupNumberOfParticipants($a_ref_id)
163  {
164  global $rbacreview;
165 
166  $rolf = $rbacreview->getRoleFolderOfObject($a_ref_id);
167  $lroles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
168  return $rbacreview->getNumberOfAssignedUsers($lroles);
169  }
170 
178  public static function lookupNumberOfMembers($a_ref_id)
179  {
180  global $rbacreview, $ilObjDataCache;
181 
182  $rolf = $rbacreview->getRoleFolderIdOfObject($a_ref_id);
183 
184  if(!$rolf)
185  {
186  return 0;
187  }
188  $lroles = $rbacreview->getRolesOfRoleFolder($rolf,false);
189 
190  $memberRoles = array();
191  foreach($lroles as $role_id)
192  {
193  $title = $ilObjDataCache->lookupTitle($role_id);
194  switch(substr($title,0,8))
195  {
196  case 'il_crs_a':
197  case 'il_crs_t':
198  case 'il_grp_a':
199  break;
200 
201  default:
202  $memberRoles[] = $role_id;
203  break;
204  }
205  }
206  return $rbacreview->getNumberOfAssignedUsers($memberRoles);
207  }
208 
209 
219  public static function _isBlocked($a_obj_id,$a_usr_id)
220  {
221  global $ilDB;
222 
223  $query = "SELECT * FROM crs_members ".
224  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
225  "AND usr_id = ".$ilDB->quote($a_usr_id,'integer')." ".
226  "AND blocked = ".$ilDB->quote(1,'integer');
227  $res = $ilDB->query($query);
228  return $res->numRows() ? true : false;
229  }
230 
240  public static function _hasPassed($a_obj_id,$a_usr_id)
241  {
242  global $ilDB;
243 
244  $query = "SELECT * FROM crs_members ".
245  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
246  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
247  "AND passed = '1'";
248  $res = $ilDB->query($query);
249  return $res->numRows() ? true : false;
250  }
251 
261  public static function _deleteAllEntries($a_obj_id)
262  {
263  global $ilDB;
264 
265  $query = "DELETE FROM crs_members ".
266  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
267  $res = $ilDB->manipulate($query);
268 
269  $query = "DELETE FROM il_subscribers ".
270  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
271  $res = $ilDB->manipulate($query);
272 
273  $query = 'DELETE FROM crs_waiting_list '.
274  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
275  $ilDB->manipulate($query);
276 
277  return true;
278  }
279 
288  public static function _deleteUser($a_usr_id)
289  {
290  global $ilDB;
291 
292  $query = "DELETE FROM crs_members WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
293  $res = $ilDB->manipulate($query);
294 
295  $query = "DELETE FROM il_subscribers WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
296  $res = $ilDB->manipulate($query);
297 
298  include_once './Modules/Course/classes/class.ilCourseWaitingList.php';
300  }
301 
302  public static function getDefaultMemberRole($a_ref_id)
303  {
304  global $ilCtrl;
305 
306  $obj_id = ilObject::_lookupObjId($a_ref_id);
308 
309  if(!in_array($type,array('crs','grp')))
310  {
311  return 0;
312  }
313 
314  global $rbacreview;
315 
316 
317  $rolf = $rbacreview->getRoleFolderIdOfObject($a_ref_id);
318  $roles = $rbacreview->getRolesOfRoleFolder($rolf,false);
319 
320  foreach($roles as $role)
321  {
323  if(substr($title, 0, 13) == ('il_'.$type.'_member'))
324  {
325  return $role;
326  }
327  }
328  return 0;
329  }
330 
337  public function getNotificationRecipients()
338  {
339  global $ilDB;
340 
341  $query = "SELECT * FROM crs_members ".
342  "WHERE notification = 1 ".
343  "AND obj_id = ".$ilDB->quote($this->obj_id)." ";
344  $res = $ilDB->query($query);
345  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
346  {
347  if($this->isAdmin($row->usr_id) or $this->isTutor($row->usr_id))
348  {
349  $recp[] = $row->usr_id;
350  }
351  }
352  return $recp ? $recp : array();
353  }
354 
361  public function getCountMembers()
362  {
363  return count($this->members);
364  }
365 
372  public function getCountParticipants()
373  {
374  return count($this->participants);
375  }
376 
377 
378 
379 
386  public function getParticipants()
387  {
388  return $this->participants ? $this->participants : array();
389  }
390 
398  public function getMembers()
399  {
400  return $this->members ? $this->members : array();
401  }
408  public function getAdmins()
409  {
410  return $this->admins ? $this->admins : array();
411  }
412 
417  public function getCountAdmins()
418  {
419  return count($this->getAdmins());
420  }
421 
422 
429  public function getTutors()
430  {
431  return $this->tutors ? $this->tutors : array();
432  }
433 
441  public function isAdmin($a_usr_id)
442  {
443  return in_array($a_usr_id,$this->admins) ? true : false;
444  }
445 
453  public function isTutor($a_usr_id)
454  {
455  return in_array($a_usr_id,$this->tutors) ? true : false;
456  }
457 
465  public function isMember($a_usr_id)
466  {
467  return in_array($a_usr_id,$this->members) ? true : false;
468  }
469 
470 
471 
472 
480  public function isAssigned($a_usr_id)
481  {
482  return in_array($a_usr_id,$this->participants);
483  }
484 
490  public function isLastAdmin($a_usr_id)
491  {
492  return in_array($a_usr_id,$this->getAdmins()) and count($this->getAdmins()) == 1;
493  }
494 
495 
503  public function getRoles()
504  {
505  return $this->roles ? $this->roles : array();
506  }
507 
515  public function getAssignedRoles($a_usr_id)
516  {
517  global $rbacreview;
518 
519  foreach($this->roles as $role)
520  {
521  if($rbacreview->isAssigned($a_usr_id,$role))
522  {
523  $assigned[] = $role;
524  }
525  }
526  return $assigned ? $assigned : array();
527  }
528 
537  public function updateRoleAssignments($a_usr_id,$a_roles)
538  {
539  global $rbacreview,$rbacadmin;
540 
541  $roles = $a_roles ? $a_roles : array();
542 
543  foreach($this->getRoles() as $role_id)
544  {
545  if($rbacreview->isAssigned($a_usr_id,$role_id))
546  {
547  if(!in_array($role_id,$roles))
548  {
549  $rbacadmin->deassignUser($role_id,$a_usr_id);
550  }
551  }
552  else
553  {
554  if(in_array($role_id,$roles))
555  {
556  $rbacadmin->assignUser($role_id,$a_usr_id);
557  }
558  }
559  }
560  $this->readParticipants();
561  $this->readParticipantsStatus();
562  }
563 
571  public function checkLastAdmin($a_usr_ids)
572  {
573  foreach($this->getAdmins() as $admin_id)
574  {
575  if(!in_array($admin_id,$a_usr_ids))
576  {
577  return true;
578  }
579  }
580  return false;
581  }
582 
590  public function isBlocked($a_usr_id)
591  {
592  if(isset($this->participants_status[$a_usr_id]))
593  {
594  return $this->participants_status[$a_usr_id]['blocked'] ? true : false;
595  }
596  return false;
597  }
598 
606  public function hasPassed($a_usr_id)
607  {
608  if(isset($this->participants_status[$a_usr_id]))
609  {
610  return $this->participants_status[$a_usr_id]['passed'] ? true : false;
611  }
612  return false;
613  }
614 
622  public function delete($a_usr_id)
623  {
624  global $rbacadmin,$ilDB;
625 
626  $this->dropDesktopItem($a_usr_id);
627  foreach($this->roles as $role_id)
628  {
629  $rbacadmin->deassignUser($role_id,$a_usr_id);
630  }
631 
632  $query = "DELETE FROM crs_members ".
633  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
634  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer');
635  $res = $ilDB->manipulate($query);
636 
637  $this->readParticipants();
638  $this->readParticipantsStatus();
639 
640  return true;
641  }
642 
651  public function updateBlocked($a_usr_id,$a_blocked)
652  {
653  global $ilDB;
654 
655  $this->participants_status[$a_usr_id]['blocked'] = (int) $a_blocked;
656 
657  $query = "SELECT * FROM crs_members ".
658  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
659  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
660  $res = $ilDB->query($query);
661  if($res->numRows())
662  {
663  $query = "UPDATE crs_members SET ".
664  "blocked = ".$ilDB->quote((int) $a_blocked ,'integer')." ".
665  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
666  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
667  }
668  else
669  {
670  $query = "INSERT INTO crs_members (blocked,obj_id,usr_id,notification,passed) ".
671  "VALUES ( ".
672  $ilDB->quote((int) $a_blocked ,'integer').", ".
673  $ilDB->quote($this->obj_id ,'integer').", ".
674  $ilDB->quote($a_usr_id ,'integer').", ".
675  $ilDB->quote(0,'integer').", ".
676  $ilDB->quote(0,'integer').
677  ")";
678 
679  }
680  $res = $ilDB->manipulate($query);
681  return true;
682  }
683 
692  public function updateNotification($a_usr_id,$a_notification)
693  {
694  global $ilDB;
695 
696  $this->participants_status[$a_usr_id]['notification'] = (int) $a_notification;
697 
698  $query = "SELECT * FROM crs_members ".
699  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
700  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
701  $res = $ilDB->query($query);
702  if($res->numRows())
703  {
704  $query = "UPDATE crs_members SET ".
705  "notification = ".$ilDB->quote((int) $a_notification ,'integer')." ".
706  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
707  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer');
708  }
709  else
710  {
711  $query = "INSERT INTO crs_members (notification,obj_id,usr_id,passed,blocked) ".
712  "VALUES ( ".
713  $ilDB->quote((int) $a_notification ,'integer').", ".
714  $ilDB->quote($this->obj_id ,'integer').", ".
715  $ilDB->quote($a_usr_id ,'integer').", ".
716  $ilDB->quote(0,'integer').", ".
717  $ilDB->quote(0,'integer').
718  ")";
719 
720  }
721  $res = $ilDB->manipulate($query);
722  return true;
723  }
724 
725 
726 
727 
736  public function add($a_usr_id,$a_role)
737  {
738  global $rbacadmin,$ilLog,$ilAppEventHandler;
739 
740  if($this->isAssigned($a_usr_id))
741  {
742  return false;
743  }
744 
745  switch($a_role)
746  {
747  case IL_CRS_ADMIN:
748  $this->admins[] = $a_usr_id;
749  break;
750 
751  case IL_CRS_TUTOR:
752  $this->tutors[] = $a_usr_id;
753  break;
754 
755  case IL_CRS_MEMBER:
756  $this->members[] = $a_usr_id;
757  break;
758 
759  case IL_GRP_ADMIN:
760  $this->admins[] = $a_usr_id;
761  break;
762 
763  case IL_GRP_MEMBER:
764  $this->members[] = $a_usr_id;
765  break;
766  }
767 
768  $this->participants[] = $a_usr_id;
769  $rbacadmin->assignUser($this->role_data[$a_role],$a_usr_id);
770  $this->addDesktopItem($a_usr_id);
771 
772  // Delete subscription request
773  $this->deleteSubscriber($a_usr_id);
774 
775  include_once './Services/Membership/classes/class.ilWaitingList.php';
776  ilWaitingList::deleteUserEntry($a_usr_id,$this->obj_id);
777 
778  if($this->type == 'crs') {
779  // Add event: used for ecs accounts
780  $ilLog->write(__METHOD__.': Raise new event: Modules/Course addParticipant');
781  $ilAppEventHandler->raise("Modules/Course", "addParticipant", array('usr_id' => $a_usr_id,'role_id' => $a_role));
782  }
783  return true;
784  }
785 
786 
794  public function deleteParticipants($a_user_ids)
795  {
796  foreach($a_user_ids as $user_id)
797  {
798  $this->delete($user_id);
799  }
800  return true;
801  }
802 
810  public function addDesktopItem($a_usr_id)
811  {
812  if(!ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
813  {
814  ilObjUser::_addDesktopItem($a_usr_id, $this->ref_id,$this->type);
815  }
816  return true;
817  }
818 
826  function dropDesktopItem($a_usr_id)
827  {
828  if(ilObjUser::_isDesktopItem($a_usr_id, $this->ref_id,$this->type))
829  {
830  ilObjUser::_dropDesktopItem($a_usr_id, $this->ref_id,$this->type);
831  }
832 
833  return true;
834  }
835 
836 
837 
845  public function isNotificationEnabled($a_usr_id)
846  {
847  if(isset($this->participants_status[$a_usr_id]))
848  {
849  return $this->participants_status[$a_usr_id]['notification'] ? true : false;
850  }
851  return false;
852  }
853 
854 
862  private function readParticipants()
863  {
864  global $rbacreview,$ilObjDataCache,$ilLog;
865 
866  $rolf = $rbacreview->getRoleFolderOfObject($this->ref_id);
867 
868  if(!isset($rolf['ref_id']) or !$rolf['ref_id'])
869  {
870  $title = $ilObjDataCache->lookupTitle($ilObjDataCache->lookupObjId($this->ref_id));
871  $ilLog->write(__METHOD__.': Found object without role folder. Ref_id: '.$this->ref_id.', title: '.$title);
872  $ilLog->logStack();
873  return false;
874  }
875 
876  $this->roles = $rbacreview->getRolesOfRoleFolder($rolf['ref_id'],false);
877 
878  $users = array();
879  $this->participants = array();
880  $this->members = $this->admins = $this->tutors = array();
881 
882  foreach($this->roles as $role_id)
883  {
884  $title = $ilObjDataCache->lookupTitle($role_id);
885  switch(substr($title,0,8))
886  {
887  case 'il_crs_m':
888  $this->role_data[IL_CRS_MEMBER] = $role_id;
889  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
890  $this->members = array_unique(array_merge($assigned,$this->members));
891  break;
892 
893  case 'il_crs_a':
894  $this->role_data[IL_CRS_ADMIN] = $role_id;
895  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
896  $this->admins = $rbacreview->assignedUsers($role_id);
897  break;
898 
899  case 'il_crs_t':
900  $this->role_data[IL_CRS_TUTOR] = $role_id;
901  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
902  $this->tutors = $rbacreview->assignedUsers($role_id);
903  break;
904 
905  case 'il_grp_a':
906  $this->role_data[IL_GRP_ADMIN] = $role_id;
907  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
908  $this->admins = $rbacreview->assignedUsers($role_id);
909  break;
910 
911  case 'il_grp_m':
912  $this->role_data[IL_GRP_MEMBER] = $role_id;
913  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
914  $this->members = $rbacreview->assignedUsers($role_id);
915  break;
916 
917  default:
918  $this->participants = array_unique(array_merge($assigned = $rbacreview->assignedUsers($role_id),$this->participants));
919  $this->members = array_unique(array_merge($assigned,$this->members));
920  break;
921  }
922  }
923  }
924 
932  private function readParticipantsStatus()
933  {
934  global $ilDB;
935 
936  $query = "SELECT * FROM crs_members ".
937  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
938  $res = $ilDB->query($query);
939  $this->participants_status = array();
940  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
941  {
942  $this->participants_status[$row->usr_id]['blocked'] = $row->blocked;
943  $this->participants_status[$row->usr_id]['notification'] = $row->notification;
944  $this->participants_status[$row->usr_id]['passed'] = $row->passed;
945  }
946  }
947 
955  public function isGroupingMember($a_usr_id,$a_field = '')
956  {
957  global $rbacreview,$ilObjDataCache,$ilDB;
958 
959  // Used for membership limitations -> check membership by given field
960  if($a_field)
961  {
962  include_once './Services/User/classes/class.ilObjUser.php';
963 
964  $tmp_user =& ilObjectFactory::getInstanceByObjId($a_usr_id);
965  switch($a_field)
966  {
967  case 'login':
968  $and = "AND login = ".$ilDB->quote($tmp_user->getLogin(),'text')." ";
969  break;
970  case 'email':
971  $and = "AND email = ".$ilDB->quote($tmp_user->getEmail(),'text')." ";
972  break;
973  case 'matriculation':
974  $and = "AND matriculation = ".$ilDB->quote($tmp_user->getMatriculation(),'text')." ";
975  break;
976 
977  default:
978  $and = "AND usr_id = ".$ilDB->quote($a_usr_id,'integer'). " ";
979  break;
980  }
981 
982  if(!$this->getParticipants())
983  {
984  return false;
985  }
986 
987  $query = "SELECT * FROM usr_data ud ".
988  "WHERE ".$ilDB->in('usr_id',$this->getParticipants(),false,'integer')." ".
989  $and;
990 
991  $res = $ilDB->query($query);
992  return $res->numRows() ? true : false;
993  }
994  }
995 
996  public static function lookupSubscribers($a_obj_id)
997  {
998  global $ilDB;
999 
1000  $subscribers = array();
1001  $query = "SELECT usr_id FROM il_subscribers ".
1002  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
1003  "ORDER BY sub_time ";
1004 
1005  $res = $ilDB->query($query);
1006  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1007  {
1008  $subscribers[] = $row->usr_id;
1009  }
1010  return $subscribers;
1011  }
1012 
1018  public function getSubscribers()
1019  {
1020  $this->readSubscribers();
1021 
1022  return $this->subscribers;
1023  }
1024 
1025 
1031  public function getCountSubscribers()
1032  {
1033  return count($this->getSubscribers());
1034  }
1035 
1041  public function getSubscriberData($a_usr_id)
1042  {
1043  return $this->readSubscriberData($a_usr_id);
1044  }
1045 
1046 
1047 
1053  public function assignSubscribers($a_usr_ids)
1054  {
1055  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1056  {
1057  return false;
1058  }
1059  foreach($a_usr_ids as $id)
1060  {
1061  if(!$this->assignSubscriber($id))
1062  {
1063  return false;
1064  }
1065  }
1066  return true;
1067  }
1068 
1074  public function assignSubscriber($a_usr_id)
1075  {
1076  global $ilErr;
1077 
1078  $ilErr->setMessage("");
1079  if(!$this->isSubscriber($a_usr_id))
1080  {
1081  $ilErr->appendMessage($this->lng->txt("crs_user_notsubscribed"));
1082 
1083  return false;
1084  }
1085  if($this->isAssigned($a_usr_id))
1086  {
1087  $tmp_obj = ilObjectFactory::getInstanceByObjId($a_usr_id);
1088  $ilErr->appendMessage($tmp_obj->getLogin().": ".$this->lng->txt("crs_user_already_assigned"));
1089 
1090  return false;
1091  }
1092 
1093  if(!$tmp_obj =& ilObjectFactory::getInstanceByObjId($a_usr_id))
1094  {
1095  $ilErr->appendMessage($this->lng->txt("crs_user_not_exists"));
1096 
1097  return false;
1098  }
1099 
1100  // TODO: must be group or course member role
1101  $this->add($tmp_obj->getId(),IL_CRS_MEMBER);
1102  $this->deleteSubscriber($a_usr_id);
1103 
1104  return true;
1105  }
1106 
1112  public function autoFillSubscribers()
1113  {
1114  $this->readSubscribers();
1115 
1116  $counter = 0;
1117  foreach($this->subscribers as $subscriber)
1118  {
1119  if(!$this->assignSubscriber($subscriber))
1120  {
1121  continue;
1122  }
1123  else
1124  {
1125  // TODO: notification
1126  #$this->sendNotification($this->NOTIFY_ACCEPT_SUBSCRIBER,$subscriber);
1127  }
1128  ++$counter;
1129  }
1130 
1131  return $counter;
1132  }
1133 
1139  public function addSubscriber($a_usr_id)
1140  {
1141  global $ilDB;
1142 
1143  $query = "INSERT INTO il_subscribers (usr_id,obj_id,subject,sub_time) ".
1144  " VALUES (".
1145  $ilDB->quote($a_usr_id ,'integer').",".
1146  $ilDB->quote($this->obj_id ,'integer').", ".
1147  $ilDB->quote('','text').", ".
1148  $ilDB->quote(time() ,'integer').
1149  ")";
1150  $res = $ilDB->manipulate($query);
1151 
1152  return true;
1153  }
1154 
1155 
1161  public function updateSubscriptionTime($a_usr_id,$a_subtime)
1162  {
1163  global $ilDB;
1164 
1165  $query = "UPDATE il_subscribers ".
1166  "SET sub_time = ".$ilDB->quote($a_subtime ,'integer')." ".
1167  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1168  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1169  $res = $ilDB->manipulate($query);
1170 
1171  return true;
1172  }
1173 
1181  public function updateSubject($a_usr_id,$a_subject)
1182  {
1183  global $ilDB;
1184 
1185  $query = "UPDATE il_subscribers ".
1186  "SET subject = ".$ilDB->quote($a_subject ,'text')." ".
1187  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1188  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1189  $res = $ilDB->manipulate($query);
1190  return true;
1191  }
1192 
1193 
1199  public function deleteSubscriber($a_usr_id)
1200  {
1201  global $ilDB;
1202 
1203  $query = "DELETE FROM il_subscribers ".
1204  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1205  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ";
1206  $res = $ilDB->manipulate($query);
1207 
1208  return true;
1209  }
1210 
1211 
1217  public function deleteSubscribers($a_usr_ids)
1218  {
1219  global $ilErr,$ilDB;
1220 
1221  if(!is_array($a_usr_ids) or !count($a_usr_ids))
1222  {
1223  $ilErr->setMessage('');
1224  $ilErr->appendMessage($this->lng->txt("no_usr_ids_given"));
1225 
1226  return false;
1227  }
1228  $query = "DELETE FROM il_subscribers ".
1229  "WHERE ".$ilDB->in('usr_id',(array) $a_usr_ids,false,'integer')." ".
1230  "AND obj_id = ".$ilDB->quote($this->obj_id,'integer');
1231  $res = $ilDB->query($query);
1232  return true;
1233  }
1234 
1235 
1241  public function isSubscriber($a_usr_id)
1242  {
1243  global $ilDB;
1244 
1245  $query = "SELECT * FROM il_subscribers ".
1246  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1247  "AND obj_id = ".$ilDB->quote($this->obj_id ,'integer')."";
1248 
1249  $res = $ilDB->query($query);
1250  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1251  {
1252  return true;
1253  }
1254  return false;
1255  }
1256 
1263  public static function _isSubscriber($a_obj_id,$a_usr_id)
1264  {
1265  global $ilDB;
1266 
1267  $query = "SELECT * FROM il_subscribers ".
1268  "WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
1269  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')."";
1270 
1271  $res = $ilDB->query($query);
1272  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1273  {
1274  return true;
1275  }
1276  return false;
1277  }
1278 
1284  protected function readSubscribers()
1285  {
1286  global $ilDB;
1287 
1288  $this->subscribers = array();
1289 
1290  $query = "SELECT usr_id FROM il_subscribers ".
1291  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1292  "ORDER BY sub_time ";
1293 
1294  $res = $this->ilDB->query($query);
1295  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1296  {
1297  // DELETE SUBSCRIPTION IF USER HAS BEEN DELETED
1298  if(!ilObjectFactory::getInstanceByObjId($row->usr_id,false))
1299  {
1300  $this->deleteSubscriber($row->usr_id);
1301  }
1302  $this->subscribers[] = $row->usr_id;
1303  }
1304  return true;
1305  }
1306 
1312  protected function readSubscriberData($a_usr_id)
1313  {
1314  global $ilDB;
1315 
1316  $query = "SELECT * FROM il_subscribers ".
1317  "WHERE obj_id = ".$ilDB->quote($this->obj_id ,'integer')." ".
1318  "AND usr_id = ".$ilDB->quote($a_usr_id ,'integer')."";
1319 
1320  $res = $this->ilDB->query($query);
1321  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1322  {
1323  $data["time"] = $row->sub_time;
1324  $data["usr_id"] = $row->usr_id;
1325  $data['subject'] = $row->subject;
1326  }
1327  return $data ? $data : array();
1328  }
1329 
1330  public static function lookupSubscribersData($a_obj_id)
1331  {
1332  global $ilDB;
1333 
1334  $query = 'SELECT * FROM il_subscribers '.
1335  'WHERE obj_id = '.$ilDB->quote($a_obj_id,'integer');
1336  $res = $ilDB->query($query);
1337 
1338  $data = array();
1339  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1340  {
1341  $data[$row->usr_id]['time'] = $row->sub_time;
1342  $data[$row->usr_id]['usr_id'] = $row->usr_id;
1343  $data[$row->usr_id]['subject'] = $row->subject;
1344  }
1345  return $data;
1346  }
1347 }
1348 ?>