ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatRoom.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 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 
33 {
34  var $ilias;
35  var $lng;
36 
38 
39  var $ref_id; // OF CHAT OBJECT
40  var $owner_id;
41  var $room_id;
42  var $guests;
43  var $title;
44 
45  var $user_id;
46 
53  public function __construct($a_id)
54  {
55  global $ilias,$lng,$ilUser;
56 
57  define(MAX_LINES,1000);
58 
59  $this->ilias =& $ilias;
60  $this->lng =& $lng;
61 
62  $this->obj_id = $a_id;
63  $this->owner_id = $ilUser->getId();
64  $this->user_id = $_SESSION["AccountId"];
65  }
66 
67  // SET/GET
68  public function getErrorMessage()
69  {
70  return $this->error_msg;
71  }
72 
73  public function setRoomId($a_id)
74  {
75  $this->room_id = $a_id;
76  $this->read(); // READ DATA OF ROOM
77  }
78 
79  public function getRoomId()
80  {
81  return $this->room_id;
82  }
83 
84  public function getObjId()
85  {
86  return $this->obj_id;
87  }
88 
89  public function setOwnerId($a_id)
90  {
91  $this->owner_id = $a_id;
92  }
93 
94  public function getOwnerId()
95  {
96  return $this->owner_id;
97  }
98 
99  public static function _getOwnerId($room_id)
100  {
101  global $ilDB;
102 
103  //$this->guests = array();
104 
105  $res = $ilDB->queryf('
106  SELECT owner FROM chat_rooms WHERE room_id = %s',
107  array('integer'),
108  array($room_id));
109 
110  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
111  {
112  return $row->owner;
113  }
114  return 0;
115  }
116 
117  public function getName()
118  {
119  if(!$this->getRoomId())
120  {
121  return $this->getObjId();
122  }
123  else
124  {
125  // GET NAME OF PRIVATE CHATROOM
126  }
127  }
128 
129  public function setTitle($a_title)
130  {
131  $this->title = $a_title;
132  }
133 
134  public function getTitle()
135  {
136  return $this->title;
137  }
138 
139  public function getGuests()
140  {
141  return $this->guests ? $this->guests : array();
142  }
143 
144  public function setUserId($a_id)
145  {
146  $this->user_id = $a_id;
147  }
148 
149  public function getUserId()
150  {
151  return $this->user_id;
152  }
153 
154  public function invite($a_id)
155  {
156  global $ilDB;
157 
158  $res = $ilDB->queryf('
159  SELECT * FROM chat_invitations
160  WHERE chat_id = %s
161  AND room_id = %s
162  AND guest_id = %s',
163  array('integer', 'integer', 'integer'),
164  array($this->getObjId(), $this->getRoomId(), $a_id));
165 
166  //if ($ilDB->numRows($res) > 0)
167  if($res->numRows() > 0)
168  {
169  $res = $ilDB->manipulateF('
170  UPDATE chat_invitations
171  SET invitation_time = %s,
172  guest_informed = %s
173  WHERE chat_id = %s
174  AND room_id = %s
175  AND guest_id = %s',
176  array('integer', 'integer', 'integer', 'integer', 'integer'),
177  array(time(), 0, $this->getObjId(), $this->getRoomId(), $a_id));
178  }
179  else
180  {
181  $res = $ilDB->manipulateF(
182  'INSERT INTO chat_invitations
183  ( chat_id,
184  room_id,
185  guest_id,
186  invitation_time
187  )
188  VALUES (%s, %s, %s, %s)',
189  array('integer', 'integer', 'integer', 'integer'),
190  array($this->getObjId(), $this->getRoomId(), $a_id, time()));
191 
192  }
193  }
194 
195  public function drop($a_id)
196  {
197  global $ilDB;
198 
199  $res = $ilDB->manipulateF('
200  DELETE FROM chat_invitations
201  WHERE chat_id = %s
202  AND room_id = %s
203  AND guest_id = %s',
204  array('integer', 'integer', 'integer'),
205  array($this->getObjId(), $this->getRoomId(), $a_id));
206 
207  }
208 
209  public function visited($a_id)
210  {
211  global $ilDB;
212 
213  $res = $ilDB->manipulateF('
214  UPDATE chat_invitations
215  SET guest_informed = %s
216  WHERE chat_id = %s
217  AND room_id = %s
218  AND guest_id = %s',
219  array('integer', 'integer', 'integer', 'integer'),
220  array('1', $this->getObjId(), $this->getRoomId(), $a_id));
221 
222  }
223 
224  public function checkAccess()
225  {
226  global $rbacsystem;
227 
228  if ($this->getObjId() ||
229  $this->getRoomId())
230  {
231  if(!$this->isInvited($this->getUserId()) &&
232  !$this->isOwner() &&
233  !$rbacsystem->checkAccess('moderate', $_GET['ref_id']))
234  {
235  $this->setRoomId(0);
236  return false;
237  }
238  $this->visited($this->getUserId());
239  }
240  return true;
241  }
242 
243  public static function _checkAccess($obj_id, $room_id, $ref_id, $user_id, $owner_id)
244  {
245  global $rbacsystem;
246 
247  if ($obj_id || $room_id)
248  {
249  if(!self::_isInvited($user_id) &&
250  !$owner_id &&
251  !$rbacsystem->checkAccess('moderate', $ref_id))
252  {
253  return false;
254  }
255  }
256  return true;
257  }
258 
259  public function isInvited($a_id)
260  {
261  global $ilDB;
262 
263  $res = $ilDB->queryf('
264  SELECT * FROM chat_invitations ci, chat_rooms ca
265  WHERE ci.room_id = ca.room_id
266  AND ci.chat_id = %s
267  AND ci.room_id = %s
268  AND owner = %s
269  AND ci.guest_id = %s',
270  array('integer', 'integer', 'integer','integer'),
271  array($this->getObjId(), $this->getRoomId(), $this->getOwnerId(), $a_id));
272 
273  return $res->numRows() ? true : false;
274  //return $ilDB->numRows($res) ? true : false;
275  }
276 
277  public static function _isInvited($obj_id, $room_id, $owner_id, $a_id)
278  {
279  global $ilDB;
280 
281  $res = $ilDB->queryf('
282  SELECT * FROM chat_invitations ci, chat_rooms ca
283  WHERE ci.room_id = ca.room_id
284  AND ci.chat_id = %s
285  AND ci.room_id = %s
286  AND owner = %s
287  AND ci.guest_id = %s',
288  array('integer', 'integer', 'integer','integer'),
289  array($obj_id, $room_id, $owner_id, $a_id));
290 
291  return $res->numRows() ? true : false;
292  }
293 
294  public function isOwner()
295  {
296  return $this->getOwnerId() == $this->getUserId();
297  }
298 
299  // METHODS FOR EXPORTING CHAT
300  public function appendMessageToDb($message)
301  {
302  //if($this->getCountLines() >= MAX_LINES)
303  //{
304  // $this->deleteFirstLine();
305  //}
306  $id = $this->addLine($message);
307  return $id;
308  }
309 
310  public function getAllMessages($min_timestamp = 0)
311  {
312  global $ilDB;
313  $res = $ilDB->queryf('
314  SELECT message FROM chat_room_messages
315  WHERE chat_id = %s
316  AND room_id = %s
317  AND commit_timestamp > %s
318  ORDER BY commit_timestamp',
319  array('integer', 'integer', 'integer'),
320  array($this->getObjId(), $this->getRoomId(), $min_timestamp)
321  );
322 
323  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
324  {
325  $data[] = $row->message;
326  }
327  return is_array($data) ? implode("<br />",$data) : "";
328  }
329 
330  public function getNewMessages($last_known_id, &$new_last_known_id = -1, $max_age = 0)
331  {
332  global $ilDB;
333  $res = $ilDB->queryf('
334  SELECT message, entry_id FROM chat_room_messages
335  WHERE chat_id = %s
336  AND room_id = %s
337  AND entry_id > %s
338  AND commit_timestamp > %s
339  ORDER BY commit_timestamp',
340  array('integer', 'integer', 'integer', 'integer'),
341  array($this->getObjId(), $this->getRoomId(), $last_known_id, $max_age));
342 
343  $max_id = 0;
344  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
345  {
346  $data[] = $row->message;
347  $max_id = max($max_id, $row->entry_id);
348  }
349 
350  if ($new_last_known_id !== -1) {
351  $new_last_known_id = $max_id;
352  }
353  return $data;
354  }
355 
356  public function deleteAllMessages()
357  {
358  global $ilDB;
359 
360  $res = $ilDB->manipulateF('
361  DELETE FROM chat_room_messages
362  WHERE chat_id = %s
363  AND room_id = %s',
364  array('integer', 'integer'),
365  array($this->getObjId(), $this->getRoomId()));
366 
367  return true;
368  }
369 
370  public function updateLastVisit()
371  {
372  // CHECK IF OLD DATA EXISTS
373  global $ilDB;
374  $kicked = $this->isKicked($this->getUserId());
375  $res = $ilDB->manipulateF('
376  DELETE FROM chat_user WHERE usr_id = %s',
377  array('integer'),
378  array($this->getUserId()));
379 
380  $res = $ilDB->manipulateF('
381  INSERT INTO chat_user
382  ( usr_id,
383  room_id,
384  chat_id,
385  kicked,
386  last_conn_timestamp
387  )
388  VALUES(%s, %s, %s, %s, %s)',
389  array('integer', 'integer', 'integer', 'integer', 'integer'),
390  array($this->getUserId(), $this->getRoomId(), $this->getObjId(), $kicked, time()));
391  return true;
392  }
393 
394  public function setKicked($a_usr_id)
395  {
396  global $ilDB;
397 
398  $res = $ilDB->manipulateF('
399  UPDATE chat_user
400  SET kicked = %s
401  WHERE usr_id = %s
402  AND chat_id = %s
403  AND room_id = %s',
404  array('integer', 'integer', 'integer', 'integer'),
405  array('1', $a_usr_id, $this->getObjId(), '0'));
406 
407  return true;
408  }
409 
410  function setUnkicked($a_usr_id)
411  {
412  global $ilDB;
413 
414  $res = $ilDB->manipulateF('
415  UPDATE chat_user SET kicked = %s
416  WHERE usr_id = %s
417  AND chat_id = %s
418  AND room_id = %s',
419  array('integer', 'integer', 'integer', 'integer'),
420  array('0', $a_usr_id, $this->getObjId(), '0'));
421 
422  return true;
423  }
424 
425  public function isKicked($a_usr_id)
426  {
427  global $ilDB;
428 
429  $res = $ilDB->queryf('
430  SELECT * FROM chat_user
431  WHERE kicked = %s
432  AND usr_id = %s
433  AND chat_id = %s',
434  array('integer', 'integer', 'integer'),
435  array('1', $a_usr_id, $this->getObjId()));
436 
437  //return $ilDB->numRows($res) ? true : false;
438  return $res->numRows() ? true : false;
439  }
440 
441  public static function _isKicked($a_usr_id, $chat_id)
442  {
443  global $ilDB;
444 
445  $res = $ilDB->queryf('
446  SELECT * FROM chat_user
447  WHERE kicked = %s
448  AND usr_id = %s
449  AND chat_id = %s',
450  array('integer', 'integer', 'integer'),
451  array('1', $a_usr_id, $chat_id));
452 
453  //return $ilDB->numRows($res) ? true : false;
454  return $res->numRows() ? true : false;
455  }
456 
457  public function getCountActiveUser($chat_id,$room_id)
458  {
459  global $ilDB;
460 
461  $res = $ilDB->queryf('
462  SELECT * FROM chat_user
463  WHERE chat_id = %s
464  AND room_id = %s
465  AND last_conn_timestamp > %s',
466  array('integer', 'integer', 'integer'),
467  array($chat_id, $room_id, time() - 40));
468 
469  //return $ilDB->numRows($res);
470  return $res->numRows();
471  }
472 
473  public static function _getCountActiveUsers($chat_id,$room_id = 0)
474  {
475  global $ilDB;
476 
477  $res = $ilDB->queryf('
478  SELECT * FROM chat_user
479  WHERE chat_id = %s
480  AND room_id = %s
481  AND last_conn_timestamp > %s',
482  array('integer', 'integer', 'integer'),
483  array($chat_id, $room_id, time() - 40));
484 
485  //return $ilDB->numRows($res);
486  return $res->numRows();
487  }
488 
489 
490  public function getActiveUsers()
491  {
492  global $ilDB;
493 
494  $res = $ilDB->queryf('
495  SELECT * FROM chat_user
496  WHERE chat_id = %s
497  AND room_id = %s
498  AND last_conn_timestamp > %s',
499  array('integer', 'integer', 'integer'),
500  array($this->getObjId(), $this->room_id, time() - 40));
501 
502  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
503  {
504  $usr_ids[] = $row->usr_id;
505  }
506  return $usr_ids ? $usr_ids : array();
507  }
508 
509  public static function _isActive($usr_id)
510  {
511  global $ilDB;
512 
513  $res = $ilDB->queryf('
514  SELECT * FROM chat_user
515  WHERE room_id = %s
516  AND usr_id = %s
517  AND last_conn_timestamp > %s',
518  array('integer', 'integer', 'integer'),
519  array('0', $usr_id, time() - 40));
520 
521  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
522  {
523  return $row->chat_id;
524  }
525  return false;
526  }
527 
528  public function getOnlineUsers()
529  {
530  // TODO: CHECK INVITABLE AND ALLOW MESSAGES
531  return ilUtil::getUsersOnline();
532  }
533 
534  public function validate()
535  {
536  $this->error_msg = "";
537 
538  if(!$this->getTitle())
539  {
540  $this->error_msg .= $this->lng->txt("chat_title_missing");
541  }
542  if(!$this->getOwnerId())
543  {
544  $this->ilias->raiseError("MISSING OWNER ID",$this->ilias->error_obj->FATAL);
545  }
546  return $this->error_msg ? false : true;
547  }
548 
549  public function deleteRooms($a_ids)
550  {
551  if(!is_array($a_ids))
552  {
553  $this->ilias->raiseError("ARRAY REQUIRED",$this->ilias->error_obj->FATAL);
554  }
555  foreach($a_ids as $id)
556  {
557  $this->delete($id);
558  }
559  return true;
560  }
561 
562  public function delete($a_id, $a_owner = 0)
563  {
564  // DELETE ROOM
565  global $ilDB;
566 
567  $data_values = array();
568  $data_types = array();
569 
570  $query = 'DELETE FROM chat_rooms WHERE room_id = %s';
571  array_push($data_types, 'integer');
572  array_push($data_values, $a_id);
573 
574  if ($a_owner > 0)
575  {
576  $query .=' AND owner = %s';
577  array_push($data_types, 'integer');
578  array_push($data_values,$a_owner);
579  }
580  $res = $ilDB->manipulateF($query, $data_types, $data_values);
581 
582  // DELETE INVITATIONS
583  $res = $ilDB->manipulateF('
584  DELETE FROM chat_invitations WHERE room_id = %s',
585  array('integer'), array($a_id));
586 
587  // DELETE MESSAGES
588  $res = $ilDB->manipulateF('
589  DELETE FROM chat_room_messages WHERE room_id = %s',
590  array('integer'),array($a_id));
591 
592  // DELETE USER_DATA
593  $data_types = array();
594  $data_values = array();
595 
596  $query = 'DELETE FROM chat_user WHERE room_id = %s';
597  array_push($data_types, 'integer');
598  array_push($data_values,$a_id);
599 
600  if ($a_owner > 0)
601  {
602  $query .= ' AND owner = %s';
603  array_push($data_types, 'integer');
604  array_push($data_values,$a_owner);
605  }
606  $res = $ilDB->manipulateF($query, $data_types, $data_values);
607 
608  // AND ALL RECORDINGS
609  $res = $ilDB->queryf('
610  SELECT record_id FROM chat_records WHERE room_id = %s',
611  array('integer'), array($a_id));
612 
613  if (ilDB::isDbError($res)) die("ilObjChat::delete(): " . $res->getMessage() . "<br>SQL-Statement: ".$res);
614 
615  //if (($num = $ilDB->numRows($res)) > 0)
616  if (($num = $res->numRows()) > 0)
617  {
618  for ($i = 0; $i < $num; $i++)
619  {
620  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
621  $statement_2 = $ilDB->manipulateF('
622  DELETE FROM chat_record_data WHERE record_id = %s',
623  array('integer'), array($row['record_id']));
624 
625  }
626 
627  }
628  $statement = $ilDB->manipulateF('
629  DELETE FROM chat_records WHERE room_id = %s',
630  array('integer'), array($a_id));
631 
632  return true;
633  }
634 
635  public function rename()
636  {
637  global $ilDB;
638 
639  $res = $ilDB->manipulateF('
640  UPDATE chat_rooms
641  SET title = %s
642  WHERE room_id = %s',
643  array('text', 'integer'),
644  array($this->getTitle(), $this->getRoomId()));
645 
646  return true;
647  }
648 
649  public function lookupRoomId()
650  {
651  global $ilDB;
652 
653  $res = $ilDB->queryf('
654  SELECT * FROM chat_rooms
655  WHERE title = %s
656  AND chat_id = %s
657  AND owner = %s',
658  array('text', 'integer', 'integer'),
659  array($this->getTitle(), $this->getObjId(), $this->getOwnerId()));
660 
661  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
662  {
663  return $row->room_id;
664  }
665  return false;
666  }
667 
668  public function add()
669  {
670  global $ilDB;
671 
672  $next_id = $ilDB->nextId('chat_rooms');
673  $res = $ilDB->manipulateF('
674  INSERT INTO chat_rooms
675  ( room_id,
676  title,
677  chat_id,
678  owner)
679  VALUES(%s, %s, %s, %s)',
680  array('integer', 'text', 'integer', 'integer'),
681  array($next_id, $this->getTitle(), $this->getObjId(), $this->getOwnerId()));
682 
683  //return ($id = $ilDB->getLastInsertId()) ? $id : false;
684  return $next_id;
685  }
686 
687  public function getInternalName()
688  {
689  if(!$this->getRoomId())
690  {
691  return $this->getObjId();
692  }
693  else
694  {
695  return $this->getObjId()."_".$this->getRoomId();
696  }
697  }
698 
699  public function getRooms()
700  {
701  global $tree, $ilDB, $rbacsystem;
702 
703  $data_types = array();
704  $data_values = array();
705  $query = 'SELECT DISTINCT(cr.room_id) room_id, owner, title, cr.chat_id chat_id
706  FROM chat_rooms cr LEFT JOIN chat_invitations ON cr.room_id = chat_invitations.room_id
707  WHERE (owner = %s) OR (guest_id = %s)';
708 
709  array_push($data_types, 'integer', 'integer');
710  array_push($data_values, $this->getUserId(), $this->getUserId());
711 
712  if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
713  {
714  $query .= ' OR 1=1';
715  //array_push($data_types, 'integer');
716  //array_push($data_values, '1');
717  }
718  $res = $ilDB->queryf($query, $data_types, $data_values);
719 
720  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
721  {
722  $data[$row->room_id]["room_id"] = $row->room_id;
723  $data[$row->room_id]["chat_id"] = $row->chat_id;
724  $data[$row->room_id]["owner"] = $row->owner;
725  $data[$row->room_id]["title"] = $row->title;
726  }
727  return $data ? $data : array();
728  }
729 
730  public function getRoomsOfObject($chat_id = 0, $owner_id = 0)
731  {
732  global $ilDB;
733 
734  if (!$chat_id)
735  $chat_id = $this->getObjId();
736  if (!$owner_id)
737  $owner_id = $this->getUserId();
738 
739  $res = $ilDB->queryf('
740  SELECT * FROM chat_rooms
741  WHERE chat_id = %s
742  AND owner = %s',
743  array('integer', 'integer'),
744  array($chat_id, $owner_id));
745 
746  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
747  {
748  $data[$row->room_id]["room_id"] = $row->room_id;
749  $data[$row->room_id]["owner"] = $row->owner;
750  $data[$row->room_id]["title"] = $row->title;
751  $data[$row->room_id]["owner"] = $row->owner;
752  }
753  return $data ? $data : array();
754  }
755 
756  public function getAllRoomsOfObject($chat_id = 0)
757  {
758  global $ilDB;
759 
760  if (!$chat_id)
761  $chat_id = $this->getObjId();
762 
763  $res = $ilDB->queryf('
764  SELECT * FROM chat_rooms
765  WHERE chat_id = %s',
766  array('integer'),
767  array($chat_id));
768 
769  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
770  {
771  $data[$row->room_id]["room_id"] = $row->room_id;
772  $data[$row->room_id]["owner"] = $row->owner;
773  $data[$row->room_id]["title"] = $row->title;
774  }
775  return $data ? $data : array();
776  }
777 
778  public function getAllRooms()
779  {
780  global $ilObjDataCache,$ilUser,$rbacsystem;
781 
782  $obj_ids = array();
783  $unique_chats = array();
784 
785  $pub_chat_id = ilObjChat::_getPublicChatRefId();
786  if($rbacsystem->checkAccess('read',$pub_chat_id))
787  {
788  $obj_id = $ilObjDataCache->lookupObjId($pub_chat_id);
789  if(!in_array($obj_id,$obj_ids))
790  {
791  $unique_data['child'] = $pub_chat_id;
792  $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
793  $unique_data['obj_id'] = $obj_id;
794  $unique_data['ref_id'] = $pub_chat_id;
795 
796  $unique_chats[] = $unique_data;
797  $obj_ids[] = $obj_id;
798  }
799  }
800 
801  foreach(ilUtil::_getObjectsByOperations("chat","read",$ilUser->getId(),-1) as $chat_id)
802  {
803  $obj_id = $ilObjDataCache->lookupObjId($chat_id);
804  if(!in_array($obj_id,$obj_ids))
805  {
806  $unique_data['child'] = $chat_id;
807  $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
808  $unique_data['obj_id'] = $obj_id;
809  $unique_data['ref_id'] = $chat_id;
810 
811  $unique_chats[] = $unique_data;
812  $obj_ids[] = $obj_id;
813  }
814  }
815  return $unique_chats ? $unique_chats : array();
816  }
817 
818  public function checkWriteAccess()
819  {
820  global $rbacsystem;
821 
822  if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
823  {
824  return true;
825  }
826 
827  if($this->isKicked($this->getUserId()))
828  {
829  return false;
830  }
831  if(!$this->getRoomId())
832  {
833  return true;
834  }
835  if($this->getUserId() == $this->getOwnerId())
836  {
837  return true;
838  }
839  if($this->isInvited($this->getUserId()))
840  {
841  return true;
842  }
843  return false;
844  }
845 
846  public static function _checkWriteAccess($ref_id, $room_id, $user_id)
847  {
848  global $rbacsystem;
849 
850  if($rbacsystem->checkAccess('moderate', $ref_id))
851  {
852  return true;
853  }
854 
855  if(self::_isKicked($user_id, $ref_id))
856  {
857  return false;
858  }
859  if(!$room_id)
860  {
861  return true;
862  }
863  if($user_id == self::_getOwnerId($room_id))
864  {
865  return true;
866  }
867  if(self::_isInvited($user_id))
868  {
869  return true;
870  }
871  return false;
872  }
873 
874  private function getCountLines()
875  {
876  global $ilDB;
877 
878  $res =$ilDB->queryf('
879  SELECT COUNT(entry_id) number_lines FROM chat_room_messages
880  WHERE chat_id = %s
881  AND room_id = %s',
882  array('integer', 'integer'),
883  array($this->getObjId(), $this->getRoomId()));
884 
885  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
886  {
887  return $row->number_lines;
888  }
889  return 0;
890  }
891 
892  private function deleteFirstLine()
893  {
894  global $ilDB;
895 
896 /* $res = $ilDB->queryf('
897  SELECT entry_id, MIN(commit_timestamp) last_comm FROM chat_room_messages
898  WHERE chat_id = %s
899  AND room_id = %s
900  GROUP BY null',
901  array('integer', 'integer'),
902  array($this->getObjId(), $this->getRoomId()));
903 */
904  $res = $ilDB->queryf('
905  SELECT entry_id, MIN(commit_timestamp) last_comm FROM chat_room_messages
906  WHERE chat_id = %s
907  AND room_id = %s
908  GROUP BY entry_id',
909  array('integer', 'integer'),
910  array($this->getObjId(), $this->getRoomId()));
911 
912  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
913  {
914  $entry_id = $row->entry_id;
915  }
916  if($entry_id)
917  {
918  $res = $ilDB->manipulateF('
919  DELETE FROM chat_room_messages WHERE entry_id = %s',
920  array('integer'), array($entry_id));
921  }
922  return true;
923  }
924 
925  private function addLine($message)
926  {
927  global $ilDB;
928  $next_id = $ilDB->nextId('chat_room_messages');
929  $res = $ilDB->manipulateF('
930  INSERT INTO chat_room_messages
931  ( entry_id,
932  chat_id,
933  room_id,
934  message,
935  commit_timestamp)
936  VALUES(%s, %s, %s, %s, %s)',
937  array('integer','integer', 'integer', 'text', 'integer'),
938  array($next_id, $this->getObjId(), $this->getRoomId(), $message, time()));
939 
940  $id = $ilDB->getLastInsertId();
941 
942  $this->chat_record = new ilChatRecording($this->getObjId());
943  $this->chat_record->setRoomId($this->getRoomId());
944  if ($this->chat_record->isRecording())
945  {
946  $next_id = $ilDB->nextId('chat_record_data');
947  $res = $ilDB->manipulateF('
948  INSERT INTO chat_record_data
949  ( record_data_id,
950  record_id,
951  message,
952  msg_time)
953  VALUES(%s, %s, %s, %s)',
954  array('integer','integer', 'text', 'integer'),
955  array($next_id, $this->chat_record->getRecordId(), $message, time()));
956  }
957 
958  return $next_id;
959  }
960 
961 
962  private function read()
963  {
964  global $ilDB;
965 
966  $this->guests = array();
967 
968  $res = $ilDB->queryf('
969  SELECT * FROM chat_rooms WHERE room_id = %s',
970  array('integer'),
971  array($this->getRoomId()));
972 
973  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
974  {
975  $this->setTitle($row->title);
976  $this->setOwnerId($row->owner);
977  }
978 
979  $res = $ilDB->queryf('
980  SELECT * FROM chat_invitations
981  WHERE chat_id = %s
982  AND room_id = %s',
983  array('integer', 'integer'),
984  array($this->getObjId(), $this->getRoomId()));
985 
986  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
987  {
988  $this->guests[] = $row->guest_id;
989  }
990  return true;
991  }
992 
993  static function _unkick($a_usr_id)
994  {
995  global $ilDB;
996 
997  $statement = $ilDB->manipulateF('
998  UPDATE chat_user SET kicked = %s
999  WHERE usr_id = %s',
1000  array('integer', 'integer'),
1001  array('0', $a_usr_id));
1002 
1003  return true;
1004  }
1005 
1006 
1007 } // END class.ilChatRoom
1008 ?>