ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilChatroom.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
14 {
15 
16  private $settings = array();
17  private static $settingsTable = 'chatroom_settings';
18  private static $historyTable = 'chatroom_history';
19  private static $userTable = 'chatroom_users';
20  private static $sessionTable = 'chatroom_sessions';
21  private static $banTable = 'chatroom_bans';
22  private static $privateRoomsTable = 'chatroom_prooms';
23  private static $privateSessionsTable = 'chatroom_psessions';
24  private static $uploadTable = 'chatroom_uploads';
25  private static $privateRoomsAccessTable = 'chatroom_proomaccess';
26 
34  private $availableSettings = array(
35  'object_id' => 'integer',
36  'allow_anonymous' => 'boolean',
37  'allow_custom_usernames' => 'boolean',
38  'enable_history' => 'boolean',
39  'restrict_history' => 'boolean',
40  'autogen_usernames' => 'string',
41  'room_type' => 'string',
42  'allow_private_rooms' => 'integer',
43  'display_past_msgs' => 'integer',
44  'private_rooms_enabled' => 'boolean'
45  );
46  private $roomId;
47 
48  private $object;
49 
50  public function getTitle()
51  {
52  if( !$this->object )
53  {
54  $this->object = ilObjectFactory::getInstanceByObjId($this->getSetting('object_id'));
55  }
56 
57  return $this->object->getTitle();
58  }
59 
67  public static function checkUserPermissions($permissions, $ref_id, $send_info = true)
68  {
69  global $rbacsystem, $lng;
70 
71  if( !is_array($permissions) )
72  {
73  $permissions = array( $permissions );
74  }
75 
76  foreach( $permissions as $permission )
77  {
78  if( !$rbacsystem->checkAccess( $permission, $ref_id ) )
79  {
80  if ($send_info) {
81  ilUtil::sendFailure( $lng->txt("permission_denied"), true );
82  }
83  return false;
84  }
85  }
86 
87  return true;
88  }
89 
101  public static function checkPermissionsOfUser($usr_id, $permissions, $ref_id)
102  {
103  global $rbacsystem, $lng;
104 
105  if( !is_array($permissions) )
106  {
107  $permissions = array( $permissions );
108  }
109 
110  foreach( $permissions as $permission )
111  {
112  if( !$rbacsystem->checkAccessOfUser($usr_id, $permission, $ref_id ) )
113  {
114  return false;
115  }
116  }
117 
118  return true;
119  }
120 
121  public function getDescription()
122  {
123  if (!$this->object)
124  {
125  $this->object = ilObjectFactory::getInstanceByObjId($this->getSetting('object_id'));
126  }
127 
128  return $this->object->getDescription();
129  }
130 
137  public function getSetting($name)
138  {
139  return $this->settings[$name];
140  }
141 
148  public function setSetting($name, $value)
149  {
150  $this->settings[$name] = $value;
151  }
152 
156  public function save()
157  {
158  $this->saveSettings( $this->settings );
159  }
160 
171  public function addHistoryEntry($message, $recipient = null, $publicMessage = true)
172  {
173  global $ilDB;
174 
175  $subRoom = 0;
176  if(is_array($message))
177  {
178  $subRoom = (int)$message['sub'];
179  }
180  else if(is_object($message))
181  {
182  $subRoom = (int)$message->sub;
183  }
184 
185  $id = $ilDB->nextId(self::$historyTable);
186  $ilDB->insert(
187  self::$historyTable,
188  array(
189  'hist_id' => array('integer', $id),
190  'room_id' => array('integer', $this->roomId),
191  'sub_room' => array('integer', $subRoom),
192  'message' => array('text', json_encode($message)),
193  'timestamp' => array('integer', time()),
194  )
195  );
196  }
197 
210  public function connectUser(ilChatroomUser $user)
211  {
215  global $ilDB;
216 
217  $userdata = array(
218  'login' => $user->getUsername(),
219  'id' => $user->getUserId()
220  );
221 
222  $query = 'SELECT user_id FROM ' . self::$userTable . ' WHERE room_id = %s AND user_id = %s';
223  $types = array('integer', 'integer');
224  $values = array($this->roomId, $user->getUserId());
225 
226  if(!$ilDB->fetchAssoc($ilDB->queryF($query, $types, $values)))
227  {
228  // Notice: Using replace instead of insert looks strange, because we actually know whether the selected data exists or not
229  // But we occasionally found some duplicate key errors although the data set should not exist when the following code is reached
230  $ilDB->replace(
231  self::$userTable,
232  array(
233  'room_id' => array('integer', $this->roomId),
234  'user_id' => array('integer', $user->getUserId())
235  ),
236  array(
237  'userdata' => array('text', json_encode($userdata)),
238  'connected' => array('integer', time()),
239  )
240  );
241 
242  return true;
243  }
244 
245  return false;
246  }
247 
257  public function getConnectedUsers()
258  {
259  global $ilDB;
260 
261  $query = 'SELECT userdata FROM ' . self::$userTable . ' WHERE room_id = %s';
262  $types = array('integer');
263  $values = array($this->roomId);
264  $rset = $ilDB->queryF( $query, $types, $values );
265  $users = array();
266 
267  while( $row = $ilDB->fetchAssoc( $rset ) )
268  {
269  $users[] = json_decode( $row['userdata'] );
270  }
271 
272  return $users;
273  }
274 
281  public function disconnectUser($user_id)
282  {
283  $this->disconnectUsers( array($user_id) );
284  }
285 
295  public function disconnectUsers(array $userIds)
296  {
297  global $ilDB;
298 
299  $query = 'SELECT * FROM ' . self::$userTable . ' WHERE room_id = %s AND ' .
300  $ilDB->in( 'user_id', $userIds, false, 'integer' );
301 
302  $types = array('integer');
303  $values = array($this->roomId);
304  $rset = $ilDB->queryF( $query, $types, $values );
305 
306  if( $row = $ilDB->fetchAssoc( $rset ) )
307  {
308  $query = 'SELECT proom_id FROM ' . self::$privateRoomsTable . ' WHERE parent_id = %s';
309  $rset_prooms = $ilDB->queryF($query, array('integer'), array($this->roomId));
310 
311  $prooms = array();
312 
313  while($row_prooms = $ilDB->fetchAssoc($rset_prooms)) {
314  $prooms[] = $row_prooms['proom_id'];
315  }
316 
317  if (true || $this->getSetting( 'enable_history' )) {
318  $query = 'UPDATE ' . self::$privateSessionsTable . ' SET disconnected = %s WHERE ' . $ilDB->in('user_id', $userIds, false, 'integer') . ' AND ' . $ilDB->in('proom_id', $prooms, false, 'integer');
319  $ilDB->manipulateF($query, array('integer'), array(time()));
320  }
321  else {
322  $query = 'DELETE FROM ' . self::$privateSessionsTable . ' WHERE ' . $ilDB->in('user_id', $userIds, false, 'integer') . ' AND ' . $ilDB->in('proom_id', $prooms, false, 'integer');
323  $ilDB->manipulate($query);
324  }
325 
326  $query = 'DELETE FROM ' . self::$userTable . ' WHERE room_id = %s AND ' .
327  $ilDB->in( 'user_id', $userIds, false, 'integer' );
328 
329  $types = array('integer');
330  $values = array($this->roomId);
331  $ilDB->manipulateF( $query, $types, $values );
332 
333  do
334  {
335  if ($this->getSetting( 'enable_history' )) {
336  $id = $ilDB->nextId(self::$sessionTable);
337  $ilDB->insert(
338  self::$sessionTable,
339  array(
340  'sess_id' => array('integer', $id),
341  'room_id' => array('integer', $this->roomId),
342  'user_id' => array('integer', $row['user_id']),
343  'userdata' => array('text', $row['userdata']),
344  'connected' => array('integer', $row['connected']),
345  'disconnected' => array('integer', time())
346  )
347  );
348  }
349  }
350  while( $row = $ilDB->fetchAssoc( $rset ) );
351  }
352 
353  }
354 
355  private function phpTypeToMDBType($type) {
356  switch($type) {
357  case 'string':
358  return 'text';
359  default:
360  return $type;
361  }
362 
363  }
364 
371  public function saveSettings(array $settings)
372  {
373  global $ilDB;
374 
375  $localSettings = array();
376 
377  foreach( $this->availableSettings as $setting => $type )
378  {
379  if( isset( $settings[$setting] ) ) {
380  if ($type == 'boolean') {
381  $settings[$setting] = (boolean)$settings[$setting];
382  }
383  $localSettings[$setting] = array($this->phpTypeToMDBType($type), $settings[$setting]);
384  }
385  }
386 
387  if (!$localSettings['room_type'][1]) {
388  $localSettings['room_type'][1] = 'repository';
389  }
390 
391  if( $this->roomId )
392  {
393  $ilDB->update(
394  self::$settingsTable,
395  $localSettings,
396  array( 'room_id' => array('integer', $this->roomId) )
397  );
398  }
399  else
400  {
401  $this->roomId = $ilDB->nextId( self::$settingsTable );
402 
403  $localSettings['room_id'] = array(
404  $this->availableSettings['room_id'], $this->roomId
405  );
406 
407  $ilDB->insert( self::$settingsTable, $localSettings );
408  }
409  }
410 
416  public function getSettings()
417  {
418  return $this->settings;
419  }
420 
428  public static function byObjectId($object_id)
429  {
430  global $ilDB;
431  $query = 'SELECT * FROM ' . self::$settingsTable . ' WHERE object_id = %s';
432  $types = array('integer');
433  $values = array($object_id);
434  $rset = $ilDB->queryF( $query, $types, $values );
435 
436  if( $row = $ilDB->fetchAssoc( $rset ) )
437  {
438  $room = new self();
439  $room->initialize( $row );
440  return $room;
441  }
442  }
443 
451  public static function byRoomId($room_id, $initObject = false)
452  {
453  global $ilDB;
454 
455  $query = 'SELECT * FROM ' . self::$settingsTable . ' WHERE room_id = %s';
456 
457  $types = array('integer');
458  $values = array($room_id);
459 
460  $rset = $ilDB->queryF( $query, $types, $values );
461 
462  if( $row = $ilDB->fetchAssoc( $rset ) )
463  {
464  $room = new self();
465  $room->initialize( $row );
466 
467  if ($initObject) {
468  $room->object = ilObjectFactory::getInstanceByObjId($row['object_id']);
469  }
470 
471  return $room;
472  }
473  }
474 
481  public function initialize(array $rowdata)
482  {
483  $this->roomId = $rowdata['room_id'];
484 
485  foreach( $this->availableSettings as $setting => $type )
486  {
487  if( isset($rowdata[$setting]) )
488  {
489  settype($rowdata[$setting], $this->availableSettings[$setting]);
490  $this->setSetting( $setting, $rowdata[$setting] );
491  }
492  }
493  }
494 
500  public function getRoomId()
501  {
502  return $this->roomId;
503  }
504 
513  public function isSubscribed($chat_userid)
514  {
515  global $ilDB;
516 
517  $query = 'SELECT count(user_id) as cnt FROM ' . self::$userTable .
518  ' WHERE room_id = %s AND user_id = %s';
519 
520  $types = array('integer', 'integer');
521  $values = array($this->roomId, $chat_userid);
522  $rset = $ilDB->queryF( $query, $types, $values );
523 
524  if( $rset && ($row = $ilDB->fetchAssoc( $rset )) && $row['cnt'] == 1 )
525  return true;
526 
527  return false;
528  }
529 
530  public function isAllowedToEnterPrivateRoom($chat_userid, $proom_id) {
531  //echo call_user_func_array('sprintf', array_merge(array($query), $values));
532  global $ilDB;
533 
534  $query = 'SELECT count(user_id) cnt FROM ' . self::$privateRoomsAccessTable .
535  ' WHERE proom_id = %s AND user_id = %s';
536 
537  $types = array('integer', 'integer');
538  $values = array($proom_id, $chat_userid);
539  $rset = $ilDB->queryF( $query, $types, $values );
540 
541  if( $rset && ($row = $ilDB->fetchAssoc( $rset )) && $row['cnt'] == 1 )
542  return true;
543 
544  $query = 'SELECT count(*) cnt FROM ' . self::$privateRoomsTable .
545  ' WHERE proom_id = %s AND owner = %s';
546 
547  $types = array('integer', 'integer');
548  $values = array($proom_id, $chat_userid);
549  $rset = $ilDB->queryF( $query, $types, $values );
550 
551  if( $rset && ($row = $ilDB->fetchAssoc( $rset )) && $row['cnt'] == 1 )
552  return true;
553 
554  return false;
555  }
556 
563  {
564  global $ilDB;
565 
566  $ilDB->manipulate( 'DELETE FROM ' . self::$userTable );
567  $ilDB->manipulate( 'UPDATE ' . self::$privateRoomsTable . ' SET closed = ' . $ilDB->quote( time() ,'integer') . ' WHERE closed = 0 OR closed IS NULL');
568  $ilDB->manipulate( 'UPDATE ' . self::$privateSessionsTable . ' SET disconnected = ' . $ilDB->quote( time() ,'integer') . ' WHERE disconnected = 0');
572  }
573 
584  public function getHistory(ilDateTime $from = null, ilDateTime $to = null, $restricted_session_userid = null, $proom_id = 0)
585  {
586  global $ilDB, $ilUser;
587 
588  $join = '';
589 
590  if ($proom_id) {
591  $join .=
592  'INNER JOIN ' . self::$privateSessionsTable . ' pSessionTable ' .
593  'ON pSessionTable.user_id = ' .$ilDB->quote( $restricted_session_userid, 'integer' ) . ' ' .
594  'AND pSessionTable.proom_id = historyTable.sub_room ' .
595  'AND timestamp >= pSessionTable.connected '.
596  'AND timestamp <= pSessionTable.disconnected ';
597  }
598 
599  $query =
600  'SELECT historyTable.* ' .
601  'FROM ' . self::$historyTable . ' historyTable ' . $join . ' ' .
602  'WHERE historyTable.room_id = ' . $this->getRoomId();
603 
604  $filter = array();
605 
606  if( $from != null )
607  {
608  $filter[] = 'timestamp >= ' . $ilDB->quote( $from->getUnixTime(), 'integer' );
609  }
610 
611  if( $to != null )
612  {
613  $filter[] = 'timestamp <= ' . $ilDB->quote( $to->getUnixTime(), 'integer' );
614  }
615 
616  if( $filter )
617  $query .= ' AND ' . join( ' AND ', $filter );
618  $query .= ' ORDER BY timestamp ASC';
619 
620  $rset = $ilDB->query( $query );
621  $result = array();
622 
623  while( $row = $ilDB->fetchAssoc( $rset ) )
624  {
625  $row['message'] = json_decode( $row['message'] );
626  $row['message']->timestamp = $row['timestamp'];
627  if ($row['message']->public !== null && !$row['message']->public && !in_array($ilUser->getId(), explode(',', $row['recipients']))) {
628  continue;
629  }
630 
631  $result[] = $row;
632  }
633  return $result;
634  }
635 
636  public function getPrivateRoomSessions(ilDateTime $from = null, ilDateTime $to = null, $user_id = 0, $room_id=0 ) {
637  global $ilDB;
638 
639  $query = 'SELECT proom_id, title FROM ' . self::$privateRoomsTable . ' WHERE proom_id IN (
640  SELECT proom_id FROM '.self::$privateSessionsTable.' WHERE connected >= %s AND disconnected <= %s AND user_id = %s
641 
642  ) AND parent_id = %s';
643 
644  $rset = $ilDB->queryF($query, array('integer','integer','integer','integer'), array($from->getUnixTime(), $to->getUnixTime(), $user_id, $room_id));
645  $result = array();
646  while( $row = $ilDB->fetchAssoc( $rset ) )
647  {
648  $result[] = $row;
649  }
650  return $result;
651  }
652 
661  public function saveFileUploadToDb($user_id, $filename, $type)
662  {
663  global $ilDB;
664 
665  $upload_id = $ilDB->nextId( self::$uploadTable );
666 
667  $ilDB->insert(
668  self::$uploadTable,
669  array(
670  'upload_id' => array('integer', $upload_id),
671  'room_id' => array('integer', $this->roomId),
672  'user_id' => array('integer', $user_id),
673  'filename' => array('text', $filename),
674  'filetype' => array('text', $type),
675  'timestamp' => array('integer', time())
676  )
677  );
678  }
679 
687  public function banUser($user_id, $comment = '')
688  {
689  global $ilDB;
690 
691  $ilDB->replace(
692  self::$banTable,
693  array(
694  'room_id' => array('integer', $this->roomId),
695  'user_id' => array('integer', $user_id)
696  ),
697  array(
698  'timestamp' => array('integer', time()),
699  'remark' => array('text', $comment)
700  )
701  );
702  }
703 
712  public function unbanUser($user_id)
713  {
714  global $ilDB;
715 
716  if(!is_array($user_id))
717  {
718  $user_id = array($user_id);
719  }
720 
721  $query = 'DELETE FROM ' . self::$banTable . ' WHERE room_id = %s AND ' . $ilDB->in('user_id', $user_id, false, 'integer');
722 
723  $types = array('integer');
724  $values = array($this->getRoomId());
725 
726  return $ilDB->manipulateF($query, $types, $values);
727  }
728 
737  public function isUserBanned($user_id)
738  {
739  global $ilDB;
740 
741  $query = 'SELECT COUNT(user_id) cnt FROM ' . self::$banTable . ' WHERE user_id = %s AND room_id = %s';
742 
743  $types = array('integer', 'integer');
744  $values = array($user_id, $this->getRoomId());
745 
746  $rset = $ilDB->queryF($query, $types, $values);
747 
748  if($rset && ($row = $ilDB->fetchAssoc($rset)) && $row['cnt'])
749  {
750  return true;
751  }
752 
753  return false;
754  }
755 
763  public function getBannedUsers()
764  {
765  global $ilDB;
766 
767  $query = 'SELECT chb.* FROM ' . self::$banTable . ' chb INNER JOIN usr_data ud ON chb.user_id = ud.usr_id WHERE chb.room_id = %s ';
768  $types = array('integer');
769  $values = array($this->getRoomId());
770  $rset = $ilDB->queryF( $query, $types, $values );
771  $result = array();
772 
773  if( $rset )
774  {
775  while( $row = $ilDB->fetchAssoc( $rset ) )
776  {
777  if( $row['user_id'] > 0 )
778  {
779  $user = new ilObjUser( $row['user_id'] );
780  $userdata = array(
781  'user_id' => $user->getId(),
782  'firstname' => $user->getFirstname(),
783  'lastname' => $user->getLastname(),
784  'login' => $user->getLogin(),
785  'remark' => $row['remark']
786  );
787 
788  $result[] = $userdata;
789  }
790  else
791  {
792  //@todo anonymous user
793  }
794  }
795  }
796 
797  return $result;
798  }
799 
810  public function getLastSession(ilChatroomUser $user)
811  {
812  global $ilDB;
813 
814  $query = 'SELECT * FROM ' . self::$sessionTable . ' WHERE user_id = ' .
815  $ilDB->quote( $user->getUserId(), 'integer' ) .
816  ' ORDER BY connected DESC';
817 
818  $ilDB->setLimit( 1 );
819  $rset = $ilDB->query( $query );
820 
821  if( $row = $ilDB->fetchAssoc( $rset ) )
822  {
823  return $row;
824  }
825  }
826 
837  public function getSessions(ilChatroomUser $user)
838  {
839  global $ilDB;
840 
841  $query = 'SELECT * FROM ' . self::$sessionTable
842  . ' WHERE room_id = '.
843  $ilDB->quote( $this->getRoomId(), 'integer' ) .
844  ' ORDER BY connected DESC';
845 
846  $rset = $ilDB->query( $query );
847 
848  $result = array();
849 
850  while( $row = $ilDB->fetchAssoc( $rset ) )
851  {
852  $result[] = $row;
853  }
854 
855  return $result;
856  }
857 
858  public function addPrivateRoom($title, ilChatroomUser $owner, $settings)
859  {
860  global $ilDB;
861 
862  $nextId = $ilDB->nextId(self::$privateRoomsTable);
863  $ilDB->insert(
864  self::$privateRoomsTable,
865  array(
866  'proom_id' => array('integer', $nextId),
867  'parent_id' => array('integer', $this->roomId),
868  'title' => array('text', $title),
869  'owner' => array('integer', $owner->getUserId()),
870  'created' => array('integer', time()),
871  'is_public' => array('integer', $settings['public']),
872  )
873  );
874 
875  return $nextId;
876  }
877 
878  public function closePrivateRoom($id)
879  {
880  global $ilDB;
881 
882  $ilDB->manipulateF(
883  'UPDATE ' . self::$privateRoomsTable . ' SET closed = %s WHERE proom_id = %s',
884  array('integer', 'integer'),
885  array(time(), $id)
886  );
887  }
888 
889  public function isOwnerOfPrivateRoom($user_id, $proom_id) {
890  global $ilDB;
891 
892  $query = 'SELECT proom_id FROM ' . self::$privateRoomsTable . ' WHERE proom_id = %s AND owner = %s';
893  $types = array('integer', 'integer');
894  $values = array($proom_id, $user_id);
895 
896  $rset = $ilDB->queryF($query, $types, $values);
897 
898  if ($rset && $ilDB->fetchAssoc($rset)) {
899  return true;
900  }
901  return false;
902  }
903 
908  public function inviteUserToPrivateRoom($user_id, $proom_id)
909  {
913  global $ilDB;
914 
915  $query = 'DELETE FROM ' . self::$privateRoomsAccessTable . ' WHERE user_id = %s AND proom_id = %s';
916  $types = array('integer', 'integer');
917  $values = array($user_id, $proom_id);
918 
919  $ilDB->manipulateF($query, $types, $values);
920 
921  $ilDB->insert(self::$privateRoomsAccessTable, array(
922  'user_id' => array('integer', $user_id),
923  'proom_id' => array('integer', $proom_id)
924  ));
925  }
926 
933  public function getChatURL($gui, $scope_id = 0)
934  {
935  include_once 'Services/Link/classes/class.ilLink.php';
936 
937  $url = '';
938 
939  if(is_object($gui))
940  {
941  if($scope_id)
942  {
943  $url = ilLink::_getStaticLink($gui->object->getRefId(), $gui->object->getType(), true, '_'.$scope_id);
944  }
945  else
946  {
947  $url = ilLink::_getStaticLink($gui->object->getRefId(), $gui->object->getType());
948  }
949  }
950 
951  return $url;
952  }
953 
962  public function sendInvitationNotification($gui, $sender, $recipient_id, $subScope = 0, $invitationLink = '')
963  {
967  global $lng;
968 
969  if($gui && !$invitationLink)
970  {
971  $invitationLink = $this->getChatURL($gui, $subScope);
972  }
973 
974  if($recipient_id > 0 && !in_array(ANONYMOUS_USER_ID, array($recipient_id)))
975  {
976  if(is_numeric($sender) && $sender > 0)
977  {
978  $sender_id = $sender;
982  $usr = ilObjectFactory::getInstanceByObjId($sender);
983  $public_name = $usr->getPublicName();
984  }
985  else if($sender instanceof ilChatroomUser)
986  {
987  if($sender->getUserId() > 0)
988  {
989  $sender_id = $sender->getUserId();
990  }
991  else
992  {
993  $sender_id = ANONYMOUS_USER_ID;
994  }
995  $public_name = $sender->getUsername();
996  }
997  else
998  {
999  throw new InvalidArgumentException('$sender must be an instance of ilChatroomUser or an id of an ilObjUser instance');
1000  }
1001 
1002  $lng->loadLanguageModule('mail');
1003 
1004  $recipient = ilObjectFactory::getInstanceByObjId($recipient_id);
1005  $bodyParams = array(
1006  'link' => $invitationLink,
1007  'inviter_name' => $public_name,
1008  'room_name' => $this->getTitle(),
1009  'salutation' => $lng->txt('mail_salutation_' . $recipient->getGender()) . ' ' . $recipient->getFullname()
1010  );
1011 
1012  if($subScope)
1013  {
1014  $bodyParams['room_name'] .= ' - ' . self::lookupPrivateRoomTitle($subScope);
1015  }
1016 
1017  require_once 'Services/Notifications/classes/class.ilNotificationConfig.php';
1018  $notification = new ilNotificationConfig('chat_invitation');
1019  $notification->setTitleVar('chat_invitation', $bodyParams, 'chatroom');
1020  $notification->setShortDescriptionVar('chat_invitation_short', $bodyParams, 'chatroom');
1021  $notification->setLongDescriptionVar('chat_invitation_long', $bodyParams, 'chatroom');
1022  $notification->setAutoDisable(false);
1023  $notification->setLink($invitationLink);
1024  $notification->setIconPath('templates/default/images/icon_chtr.svg');
1025  $notification->setValidForSeconds(ilNotificationConfig::TTL_LONG);
1026  $notification->setVisibleForSeconds(ilNotificationConfig::DEFAULT_TTS);
1027 
1028  $notification->setHandlerParam('mail.sender', $sender_id);
1029 
1030  $notification->notifyByUsers(array($recipient_id));
1031  }
1032  }
1033 
1034  public function inviteUserToPrivateRoomByLogin($login, $proom_id) {
1035  global $ilDB;
1036  $user_id = ilObjUser::_lookupId($login);
1037  $this->inviteUserToPrivateRoom($user_id, $proom_id);
1038  }
1039 
1040  public static function lookupPrivateRoomTitle($proom_id) {
1041  global $ilDB;
1042 
1043  $query = 'SELECT title FROM ' . self::$privateRoomsTable . ' WHERE proom_id = %s';
1044  $types = array('integer');
1045  $values = array($proom_id);
1046 
1047  $rset = $ilDB->queryF($query, $types, $values);
1048 
1049  if ($row = $ilDB->fetchAssoc($rset)) {
1050  return $row['title'];
1051  }
1052 
1053  return 'unkown';
1054  }
1055 
1056  public function getActivePrivateRooms($userid)
1057  {
1058  global $ilDB;
1059 
1060  $query = '
1061  SELECT roomtable.title, roomtable.proom_id, accesstable.user_id id, roomtable.owner rowner
1062  FROM ' . self::$privateRoomsTable . ' roomtable
1063  LEFT JOIN '.self::$privateRoomsAccessTable.' accesstable
1064  ON roomtable.proom_id = accesstable.proom_id
1065  AND accesstable.user_id = %s
1066  WHERE parent_id = %s
1067  AND (closed = 0 OR closed IS NULL)
1068  AND (accesstable.user_id IS NOT NULL OR roomtable.owner = %s)';
1069  $types = array('integer', 'integer', 'integer');
1070  $values = array($userid, $this->roomId, $userid);
1071  $rset = $ilDB->queryF( $query, $types, $values );
1072  $rooms = array();
1073 
1074  while( $row = $ilDB->fetchAssoc( $rset ) )
1075  {
1076  $row['active_users'] = $this->listUsersInPrivateRoom($row['id']);
1077  $row['owner'] = $row['rowner'];
1078  $rooms[$row['proom_id']] = $row;
1079  }
1080 
1081  return $rooms;
1082  }
1083 
1084  public function listUsersInPrivateRoom($private_room_id) {
1085  global $ilDB;
1086 
1087  $query = '
1088  SELECT chatroom_users.user_id FROM ' . self::$privateSessionsTable . '
1089  INNER JOIN chatroom_users ON chatroom_users.user_id = ' . self::$privateSessionsTable . '.user_id WHERE proom_id = %s AND disconnected = 0
1090  ';
1091  $types = array('integer');
1092  $values = array($private_room_id);
1093  $rset = $ilDB->queryF( $query, $types, $values );
1094 
1095  $users = array();
1096 
1097  while ($row = $ilDB->fetchAssoc($rset)) {
1098  $users[$row['user_id']] = $row['user_id'];
1099  }
1100 
1101  return array_values($users);
1102  }
1103 
1104  public function userIsInPrivateRoom($room_id, $user_id)
1105  {
1106  global $ilDB;
1107 
1108  $query = 'SELECT proom_id id FROM ' . self::$privateSessionsTable . ' WHERE user_id = %s AND proom_id = %s AND disconnected = 0';
1109  $types = array('integer', 'integer');
1110  $values = array($user_id, $room_id);
1111  $rset = $ilDB->queryF( $query, $types, $values );
1112  if ($ilDB->fetchAssoc($rset))
1113  return true;
1114  return false;
1115  }
1116 
1117  public function subscribeUserToPrivateRoom($room_id, $user_id)
1118  {
1119  global $ilDB;
1120 
1121  if(!$this->userIsInPrivateRoom($room_id, $user_id))
1122  {
1123  $id = $ilDB->nextId(self::$privateSessionsTable);
1124  $ilDB->insert(
1125  self::$privateSessionsTable,
1126  array(
1127  'psess_id' => array('integer', $id),
1128  'proom_id' => array('integer', $room_id),
1129  'user_id' => array('integer', $user_id),
1130  'connected' => array('integer', time()),
1131  'disconnected' => array('integer', 0),
1132  )
1133  );
1134  }
1135  }
1136 
1143  public function unsubscribeUserFromPrivateRoom($room_id, $user_id)
1144  {
1145  global $ilDB;
1146 
1147  $ilDB->update(
1148  self::$privateSessionsTable,
1149  array(
1150  'disconnected' => array('integer', time())
1151  ),
1152  array(
1153  'proom_id' => array('integer', $room_id),
1154  'user_id' => array('integer', $user_id)
1155  )
1156  );
1157  }
1158 
1159  public function countActiveUsers() {
1160  global $ilDB;
1161 
1162  $query = 'SELECT count(user_id) as cnt FROM ' . self::$userTable .
1163  ' WHERE room_id = %s';
1164 
1165  $types = array('integer');
1166  $values = array($this->roomId);
1167  $rset = $ilDB->queryF( $query, $types, $values );
1168 
1169  if( $rset && ($row = $ilDB->fetchAssoc( $rset )) && $row['cnt'] == 1 )
1170  return $row['cnt'];
1171 
1172  return 0;
1173  }
1174 
1175  public function getUniquePrivateRoomTitle($title) {
1176  global $ilDB;
1177 
1178  $query = 'SELECT title FROM ' . self::$privateRoomsTable . ' WHERE parent_id = %s and closed = 0';
1179  $rset = $ilDB->queryF($query, array('integer'), array($this->roomId));
1180 
1181  $titles = array();
1182 
1183  while($row = $ilDB->fetchAssoc($rset)) {
1184  $titles[] = $row['title'];
1185  }
1186 
1187  $suffix = '';
1188  $i = 0;
1189  do {
1190  if(!in_array($title . $suffix, $titles)) {
1191  $title .= $suffix;
1192  break;
1193  }
1194 
1195  ++$i;
1196 
1197  $suffix = ' (' . $i . ')';
1198  } while(true);
1199 
1200  return $title;
1201  }
1202 
1203  public static function findDeletablePrivateRooms() {
1204  global $ilDB;
1205 
1206  $query = '
1207  SELECT private_rooms.proom_id id, MIN(disconnected) min_disconnected, MAX(disconnected) max_disconnected
1208  FROM ' . self::$privateSessionsTable . ' private_sessions
1209  INNER JOIN '.self::$privateRoomsTable.' private_rooms
1210  ON private_sessions.proom_id = private_rooms.proom_id
1211  WHERE closed = 0
1212  GROUP BY private_rooms.proom_id
1213  HAVING MIN(disconnected) > 0 AND MAX(disconnected) < %s';
1214  $rset = $ilDB->queryF(
1215  $query,
1216  array('integer'),
1217  array(time() + 60 * 5)
1218  );
1219 
1220  $rooms = array();
1221 
1222  while ($row = $ilDB->fetchAssoc($rset)) {
1223  $rooms[$row['id']] = $row['id'];
1224  }
1225 
1226  $query = 'SELECT DISTINCT proom_id, room_id, object_id FROM ' . self::$privateRoomsTable
1227  . ' INNER JOIN ' . self::$settingsTable . ' ON parent_id = room_id '
1228  . ' WHERE ' . $ilDB->in('proom_id', $rooms, false, 'integer');
1229 
1230  $rset = $ilDB->query($query);
1231  $rooms = array();
1232  while($row = $ilDB->fetchAssoc($rset)) {
1233  $rooms[] = array(
1234  'proom_id' => $row['proom_id'],
1235  'room_id' => $row['room_id'],
1236  'object_id' => $row['object_id']
1237  );
1238  }
1239 
1240  return $rooms;
1241  }
1242 
1251  public function getAllRooms($user_id)
1252  {
1256  global $ilDB;
1257 
1258  $query = "
1259  SELECT room_id, od.title
1260  FROM object_data od
1261 
1262  INNER JOIN " . self::$settingsTable . "
1263  ON object_id = od.obj_id
1264 
1265  INNER JOIN " . self::$privateRoomsTable . " prt
1266  ON prt.owner = %s
1267 
1268  WHERE od.type = 'chtr'
1269  ";
1270 
1271  $types = array('integer');
1272  $values = array($user_id);
1273 
1274  $res = $ilDB->queryF($query, $types, $values);
1275 
1276  $rooms = array();
1277 
1278  while($row = $ilDB->fetchAssoc($res))
1279  {
1280  $room_id = $row['room_id'];
1281  $rooms[$room_id] = $row['title'];
1282  }
1283 
1284  return $rooms;
1285  }
1286 
1287 
1288 
1289  public function getPrivateSubRooms($parent_room, $user_id)
1290  {
1291  global $ilDB;
1292 
1293  $query = "
1294  SELECT proom_id, parent_id
1295  FROM chatroom_prooms
1296  WHERE parent_id = %s
1297  AND owner = %s
1298  AND closed = 0
1299  ";
1300 
1301  $types = array( 'integer', 'integer' );
1302  $values = array( $parent_room, $user_id );
1303 
1304  $res = $ilDB->queryF( $query, $types, $values );
1305 
1306  $priv_rooms = array();
1307 
1308  while( $row = $ilDB->fetchAssoc( $res ) )
1309  {
1310  $proom_id = $row['proom_id'];
1311  $priv_rooms[$proom_id] = $row['parent_id'];
1312  }
1313 
1314  return $priv_rooms;
1315  }
1316 
1324  public function getRefIdByRoomId($room_id)
1325  {
1326  global $ilDB;
1327 
1328  $query = "
1329  SELECT objr.ref_id
1330  FROM object_reference objr
1331 
1332  INNER JOIN chatroom_settings cs
1333  ON cs.object_id = objr.obj_id
1334 
1335  INNER JOIN object_data od
1336  ON od.obj_id = cs.object_id
1337 
1338  WHERE cs.room_id = %s
1339  ";
1340 
1341  $types = array( 'integer' );
1342  $values = array( $room_id );
1343 
1344  $res = $ilDB->queryF( $query, $types, $values );
1345 
1346  $row = $ilDB->fetchAssoc( $res );
1347 
1348  return $row['ref_id'];
1349  }
1350 
1351 public function getLastMessages($number, $chatuser = null) {
1352  global $ilDB;
1353 
1354  // There is currently no way to check if a message is private or not
1355  // by sql. So we fetch twice as much as we need and hope that there
1356  // are not more than $number private messages.
1357  $ilDB->setLimit($number * 2);
1358  $rset = $ilDB->queryF('SELECT * FROM ' . self::$historyTable . ' WHERE room_id = %s AND sub_room = 0 ORDER BY timestamp DESC', array('integer'), array($this->roomId));
1359 
1360  $result_count = 0;
1361  $results = array();
1362  while(($row = $ilDB->fetchAssoc($rset)) && $result_count < $number) {
1363  $tmp = json_decode($row['message']);
1364  if ($chatuser !== null && $tmp->public == 0 && $tmp->recipients) {
1365  if (in_array($chatuser->getUserId(), explode(',',$tmp->recipients))) {
1366  $results[] = $tmp;
1367  ++$result_count;
1368  }
1369  }
1370  else if ($tmp->public == 1) {
1371  $results[] = $tmp;
1372  ++$result_count;
1373  }
1374 
1375  }
1376  return $results;
1377  }
1378 
1379  public function getLastMessagesForChatViewer($number, $chatuser = null)
1380  {
1384  global $ilDB;
1385 
1386  $ilDB->setLimit($number);
1387  $rset = $ilDB->query(
1388  'SELECT *
1389  FROM ' . self::$historyTable . '
1390  WHERE room_id = '.$ilDB->quote($this->roomId, 'integer').'
1391  AND sub_room = 0
1392  AND (
1393  (' . $ilDB->like('message', 'text', '%"type":"message"%') . ' AND ' . $ilDB->like('message', 'text', '%"public":1%') . ' AND ' . $ilDB->like('message', 'text', '%"recipients":null%') . ')
1394  OR
1395  ' . $ilDB->like('message', 'text', '%"type":"%connected"%') . ')
1396  ORDER BY timestamp DESC'
1397  );
1398 
1399  $results = array();
1400  while(($row = $ilDB->fetchAssoc($rset)))
1401  {
1402  $tmp = json_decode($row['message']);
1403  if($tmp->type != 'message' && $row['timestamp'] && !is_numeric($tmp->timestamp))
1404  {
1405  $tmp->timestamp = $row['timestamp'] * 1000;
1406  }
1407  $results[] = $tmp;
1408  }
1409  return $results;
1410  }
1411 
1412  public function clearMessages($sub_room)
1413  {
1414  global $ilDB;
1415 
1416  $ilDB->queryF(
1417  'DELETE FROM ' . self::$historyTable . ' WHERE room_id = %s AND sub_room = %s',
1418  array('integer', 'integer'),
1419  array($this->roomId, (int)$sub_room)
1420  );
1421 
1422  if($sub_room)
1423  {
1424  $ilDB->queryF(
1425  'DELETE FROM ' . self::$privateSessionsTable . ' WHERE proom_id = %s AND disconnected < %s',
1426  array('integer', 'integer'),
1427  array($sub_room, time())
1428  );
1429  }
1430  else
1431  {
1432  $ilDB->queryF(
1433  'DELETE FROM ' . self::$sessionTable . ' WHERE room_id = %s AND disconnected < %s',
1434  array('integer', 'integer'),
1435  array($this->roomId, time())
1436  );
1437  }
1438  }
1439 
1440  public static function getUntrashedChatReferences($filter = array())
1441  {
1445  global $ilDB;
1446 
1447  // Check for parent because of an invalid parent node for the old public chat (thx @ jposselt ;-)).
1448  // We cannot find this old public chat and clean this automatically
1449  $query = '
1450  SELECT od.obj_id, od.title, ore.ref_id, od.type, odp.title parent_title
1451  FROM object_data od
1452  INNER JOIN object_reference ore ON ore.obj_id = od.obj_id
1453  INNER JOIN tree t ON t.child = ore.ref_id
1454  INNER JOIN tree p ON p.child = t.parent
1455  INNER JOIN object_reference orep ON orep.ref_id = p.child
1456  INNER JOIN object_data odp ON odp.obj_id = orep.obj_id
1457  INNER JOIN object_reference pre ON pre.ref_id = t.parent
1458  INNER JOIN object_data pod ON pod.obj_id = pre.obj_id
1459  ';
1460 
1461  if(isset($filter['last_activity']))
1462  {
1463  $threshold = $ilDB->quote($filter['last_activity'], 'integer');
1464  $query .= "
1465  INNER JOIN chatroom_settings ON chatroom_settings.object_id = od.obj_id
1466  INNER JOIN chatroom_history ON chatroom_history.room_id = chatroom_settings.room_id AND chatroom_history.timestamp > $threshold
1467  ";
1468  }
1469 
1470  $query .= '
1471  WHERE od.type = %s AND t.tree > 0 AND ore.deleted IS NULL
1472  GROUP BY od.obj_id, od.title, ore.ref_id, od.type, odp.title
1473  ORDER BY od.title
1474  ';
1475  $res = $ilDB->queryF($query, array('text'), array('chtr'));
1476 
1477  $chats = array();
1478  while($row = $ilDB->fetchAssoc($res))
1479  {
1480  $chats[] = $row;
1481  }
1482 
1483  return $chats;
1484  }
1485 }
1486 
1487 ?>
setSetting($name, $value)
Sets given name and value as setting into $this->settings array.
static $privateRoomsTable
getUserId()
Returns Ilias User ID.
getActivePrivateRooms($userid)
getUniquePrivateRoomTitle($title)
getPrivateSubRooms($parent_room, $user_id)
banUser($user_id, $comment='')
Inserts user into banTable, using given $user_id.
getRoomId()
Returns roomID from $this->roomId.
$result
getHistory(ilDateTime $from=null, ilDateTime $to=null, $restricted_session_userid=null, $proom_id=0)
Returns array containing history data selected from historyTable by given ilDateTime, $restricted_session_userid and matching roomId.
isUserBanned($user_id)
Returns true if there&#39;s an entry in banTable matching roomId and given $user_id.
static $settingsTable
getLastMessages($number, $chatuser=null)
getSettings()
Returns $this->settings array.
getLastSession(ilChatroomUser $user)
Returns last session from user.
getRefIdByRoomId($room_id)
Returns ref_id of given room_id.
getSetting($name)
Returns setting from $this->settings array by given name.
isSubscribed($chat_userid)
Returns true if entry exists in userTable matching given $chat_userid and $this->roomId.
static byRoomId($room_id, $initObject=false)
Returns ilChatroom by given $room_id.
getPrivateRoomSessions(ilDateTime $from=null, ilDateTime $to=null, $user_id=0, $room_id=0)
disconnectUsers(array $userIds)
Disconnects users by deleting userdata from userTable using given userId array.
static _lookupId($a_user_str)
Lookup id by login.
unsubscribeUserFromPrivateRoom($room_id, $user_id)
ilDB $ilDB
$url
Definition: shib_logout.php:72
clearMessages($sub_room)
userIsInPrivateRoom($room_id, $user_id)
getChatURL($gui, $scope_id=0)
ilCtrl $ilCtrl
Describes a notification and provides methods for publishing this notification.
listUsersInPrivateRoom($private_room_id)
save()
Saves settings using $this->settings.
isOwnerOfPrivateRoom($user_id, $proom_id)
static $privateSessionsTable
static checkUserPermissions($permissions, $ref_id, $send_info=true)
Checks user permissions by given array and ref_id.
getBannedUsers()
Returns an multidimensional array containing userdata from users having an entry in banTable with mat...
addPrivateRoom($title, ilChatroomUser $owner, $settings)
getSessions(ilChatroomUser $user)
Returns all session from user.
static findDeletablePrivateRooms()
addHistoryEntry($message, $recipient=null, $publicMessage=true)
Inserts entry into historyTable.
static lookupPrivateRoomTitle($proom_id)
Date and time handling
getConnectedUsers()
Returns an array of connected users.
subscribeUserToPrivateRoom($room_id, $user_id)
saveFileUploadToDb($user_id, $filename, $type)
Saves information about file uploads in DB.
getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
$results
unbanUser($user_id)
Deletes entry from banTable matching roomId and given $user_id and returns true if sucessful...
disconnectUser($user_id)
Creates userId array by given $user object and calls disconnectUsers method.
$comment
Definition: buildRTE.php:83
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$filename
Definition: buildRTE.php:89
Class ilChatroom.
static $privateRoomsAccessTable
static $historyTable
disconnectAllUsersFromAllRooms()
Deletes all entrys from userTable.
global $ilUser
Definition: imgupload.php:15
$ref_id
Definition: sahs_server.php:39
Class ilChatroomUser.
global $lng
Definition: privfeed.php:40
global $ilDB
inviteUserToPrivateRoomByLogin($login, $proom_id)
phpTypeToMDBType($type)
static byObjectId($object_id)
Returns ilChatroom object by given $object_id.
getUsername()
Returns username from Object or SESSION.
initialize(array $rowdata)
Sets $this->roomId by given array $rowdata and calls setSetting method foreach available setting in $...
static $uploadTable
static checkPermissionsOfUser($usr_id, $permissions, $ref_id)
Checks user permissions in question for a given user id in relation to a given ref_id.
static $sessionTable
saveSettings(array $settings)
Saves settings into settingsTable using given settings array.
isAllowedToEnterPrivateRoom($chat_userid, $proom_id)