ILIAS  Release_3_10_x_branch Revision 61812
 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-2001 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  function ilChatRoom($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  function getErrorMessage()
69  {
70  return $this->error_msg;
71  }
72 
73  function setRoomId($a_id)
74  {
75  $this->room_id = $a_id;
76 
77  // READ DATA OF ROOM
78  $this->__read();
79  }
80  function getRoomId()
81  {
82  return $this->room_id;
83  }
84  function getObjId()
85  {
86  return $this->obj_id;
87  }
88  function setOwnerId($a_id)
89  {
90  $this->owner_id = $a_id;
91  }
92  function getOwnerId()
93  {
94  return $this->owner_id;
95  }
96 
97  function getName()
98  {
99  if(!$this->getRoomId())
100  {
101  return $this->getObjId();
102  }
103  else
104  {
105  // GET NAME OF PRIVATE CHATROOM
106  }
107  }
108  function setTitle($a_title)
109  {
110  $this->title = $a_title;
111  }
112  function getTitle()
113  {
114  return $this->title;
115  }
116  function getGuests()
117  {
118  return $this->guests ? $this->guests : array();
119  }
120  function setUserId($a_id)
121  {
122  $this->user_id = $a_id;
123  }
124  function getUserId()
125  {
126  return $this->user_id;
127  }
128 
129  function invite($a_id)
130  {
131  global $ilDB;
132 
133  $statement = $ilDB->prepare('
134  SELECT * FROM chat_invitations
135  WHERE chat_id = ?
136  AND room_id = ?
137  AND guest_id = ?',
138  array('integer', 'integer', 'integer')
139  );
140 
141  $data = array($this->getObjId(), $this->getRoomId(), $a_id);
142  $res = $ilDB->execute($statement, $data);
143 
144  if($res->numRows() > 0)
145  {
146  $statement = $ilDB->prepareManip('
147  UPDATE chat_invitations
148  SET invitation_time = ?,
149  guest_informed = ?
150  WHERE chat_id = ?
151  AND room_id = ?
152  AND guest_id = ?',
153  array('integer', 'integer', 'integer', 'integer', 'integer')
154  );
155 
156  $data = array(time(), 0, $this->getObjId(), $this->getRoomId(), $a_id);
157  $res = $ilDB->execute($statement, $data);
158  }
159  else
160  {
161  $statement = $ilDB->prepareManip(
162  'INSERT INTO chat_invitations (chat_id, room_id, guest_id, invitation_time) '.
163  'VALUES(?, ?, ?, ?)',
164  array('integer', 'integer', 'integer', 'integer')
165  );
166 
167  $data = array($this->getObjId(), $this->getRoomId(), $a_id, time());
168  $res = $ilDB->execute($statement, $data);
169  }
170  }
171  function drop($a_id)
172  {
173  global $ilDB;
174 
175  $query = "DELETE FROM chat_invitations ".
176  "WHERE chat_id = ".$ilDB->quote( $this->getObjId() )." ".
177  "AND room_id = ".$ilDB->quote( $this->getRoomId() )." ".
178  "AND guest_id = ".$ilDB->quote( $a_id )."";
179 
180  $res = $this->ilias->db->query($query);
181  }
182 
183  function visited($a_id)
184  {
185  global $ilDB;
186 
187  $query = "UPDATE chat_invitations SET guest_informed = 1 ".
188  "WHERE chat_id = ".$ilDB->quote( $this->getObjId() )." ".
189  "AND room_id = ".$ilDB->quote( $this->getRoomId() )." ".
190  "AND guest_id = ".$ilDB->quote( $a_id )."";
191 
192  $res = $this->ilias->db->query($query);
193  }
194 
195  function checkAccess()
196  {
197  global $rbacsystem;
198 
199  if ($this->getObjId() ||
200  $this->getRoomId())
201  {
202  if(!$this->isInvited($this->getUserId()) &&
203  !$this->isOwner() &&
204  !$rbacsystem->checkAccess('moderate', $_GET['ref_id']))
205  {
206  $this->setRoomId(0);
207  return false;
208  }
209 
210  $this->visited($this->getUserId());
211  }
212  return true;
213  }
214 
215  function isInvited($a_id)
216  {
217  global $ilDB;
218 
219  $query = "SELECT * FROM chat_invitations AS ci JOIN chat_rooms AS ca ".
220  "WHERE ci.room_id = ca.room_id ".
221  "AND ci.chat_id = ".$ilDB->quote($this->getObjId())." ".
222  "AND ci.room_id = ".$ilDB->quote($this->getRoomId())." ".
223  "AND owner = ".$ilDB->quote($this->getOwnerId())." ".
224  "AND ci.guest_id = ".$ilDB->quote($a_id)."";
225 
226  $res = $this->ilias->db->query($query);
227 
228  return $res->numRows() ? true : false;
229  }
230  function isOwner()
231  {
232  return $this->getOwnerId() == $this->getUserId();
233  }
234 
235  // METHODS FOR EXPORTTING CHAT
236  function appendMessageToDb($message)
237  {
238  if($this->__getCountLines() >= MAX_LINES)
239  {
240  $this->__deleteFirstLine();
241  }
242  $this->__addLine($message);
243 
244  return true;
245  }
246  function getAllMessages()
247  {
248  global $ilDB;
249 
250  $query = "SELECT message FROM chat_room_messages ".
251  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
252  "AND room_id = ".$ilDB->quote($this->getRoomId())." ".
253  "ORDER BY commit_timestamp ";
254 
255  $res = $this->ilias->db->query($query);
256  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
257  {
258  $data[] = $row->message;
259  }
260  return is_array($data) ? implode("<br />",$data) : "";
261  }
262  function deleteAllMessages()
263  {
264  global $ilDB;
265 
266  $query = "DELETE FROM chat_room_messages ".
267  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
268  "AND room_id = ".$ilDB->quote($this->getRoomId())."";
269 
270  $res = $this->ilias->db->query($query);
271 
272  return true;
273  }
274 
275  function updateLastVisit()
276  {
277  // CHECK IF OLD DATA EXISTS
278  global $ilDB;
279 
280  $query = "DELETE FROM chat_user ".
281  "WHERE usr_id = ".$ilDB->quote($this->getUserId())."";
282  $res = $this->ilias->db->query($query);
283 
284  $query = "INSERT INTO chat_user ".
285  "SET usr_id = ".$ilDB->quote($this->getUserId()).", ".
286  "room_id = ".$ilDB->quote($this->getRoomId()).", ".
287  "chat_id = ".$ilDB->quote($this->getObjId()).", ".
288  "last_conn_timestamp = '".time()."'";
289  $res = $this->ilias->db->query($query);
290  return true;
291  }
292 
293  function setKicked($a_usr_id)
294  {
295  global $ilDB;
296 
297  $query = "UPDATE chat_user SET kicked = '1' ".
298  "WHERE usr_id = ".$ilDB->quote($a_usr_id)." ".
299  "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
300  "AND room_id = '0'";
301 
302  $this->ilias->db->query($query);
303 
304  return true;
305  }
306 
307  function setUnkicked($a_usr_id)
308  {
309  global $ilDB;
310 
311  $query = "UPDATE chat_user SET kicked = '0' ".
312  "WHERE usr_id = ".$ilDB->quote($a_usr_id)." ".
313  "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
314  "AND room_id = '0'";
315 
316  $this->ilias->db->query($query);
317 
318  return true;
319  }
320 
321  function isKicked($a_usr_id)
322  {
323  global $ilDB;
324 
325  $query = "SELECT * FROM chat_user ".
326  "WHERE kicked = 1 ".
327  "AND usr_id = ".$ilDB->quote($a_usr_id)." ".
328  "AND chat_id = ".$ilDB->quote($this->getObjId())."";
329 
330  $res = $this->ilias->db->query($query);
331 
332  return $res->numRows() ? true : false;
333  }
334 
335  function getCountActiveUser($chat_id,$room_id)
336  {
337  global $ilDB;
338 
339  $query = "SELECT * FROM chat_user ".
340  "WHERE chat_id = ".$ilDB->quote($chat_id)." ".
341  "AND room_id = ".$ilDB->quote($room_id)." ".
342  "AND last_conn_timestamp > ".time()." - 40";
343  $res = $this->ilias->db->query($query);
344 
345  return $res->numRows();
346  }
347 
348  function _getCountActiveUsers($chat_id,$room_id = 0)
349  {
350  global $ilDB;
351 
352  $query = "SELECT * FROM chat_user ".
353  "WHERE chat_id = ".$ilDB->quote($chat_id)." ".
354  "AND room_id = ".$ilDB->quote($room_id)." ".
355  "AND last_conn_timestamp > ".time()." - 40";
356  $res = $ilDB->query($query);
357 
358  return $res->numRows();
359  }
360 
361 
362  function getActiveUsers()
363  {
364  global $ilDB;
365 
366  $query = "SELECT * FROM chat_user ".
367  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
368  "AND room_id = ".$ilDB->quote($this->room_id)." ".
369  "AND last_conn_timestamp > ".time()." - 40";
370  $res = $this->ilias->db->query($query);
371  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
372  {
373  $usr_ids[] = $row->usr_id;
374  }
375  return $usr_ids ? $usr_ids : array();
376  }
377 
378  // Static
379  function _isActive($usr_id)
380  {
381  global $ilDB;
382 
383  $query = "SELECT * FROM chat_user ".
384  "WHERE room_id = 0 ".
385  "AND usr_id = ".$ilDB->quote((int) $usr_id)." ".
386  "AND last_conn_timestamp > ".time()." - 40";
387 
388  $res = $ilDB->query($query);
389  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
390  {
391  return $row->chat_id;
392  }
393  return false;
394  }
395 
396  function getOnlineUsers()
397  {
398  // TODO: CHECK INVITABLE AND ALLOW MESSAGES
399  return ilUtil::getUsersOnline();
400  }
401 
402  function validate()
403  {
404  $this->error_msg = "";
405 
406  if(!$this->getTitle())
407  {
408  $this->error_msg .= $this->lng->txt("chat_title_missing");
409  }
410  if(!$this->getOwnerId())
411  {
412  $this->ilias->raiseError("MISSING OWNER ID",$this->ilias->error_obj->FATAL);
413  }
414  return $this->error_msg ? false : true;
415  }
416  function deleteRooms($a_ids)
417  {
418  if(!is_array($a_ids))
419  {
420  $this->ilias->raiseError("ARRAY REQUIRED",$this->ilias->error_obj->FATAL);
421  }
422  foreach($a_ids as $id)
423  {
424  $this->delete($id);
425  }
426  return true;
427  }
428 
429  function delete($a_id, $a_owner = 0)
430  {
431  // DELETE ROOM
432  global $ilDB;
433 
434  $query = "DELETE FROM chat_rooms WHERE ".
435  "room_id = ".$ilDB->quote($a_id)."";
436  if ($a_owner > 0)
437  {
438  " AND owner = ".$ilDB->quote($a_owner)."";
439  }
440  $res = $this->ilias->db->query($query);
441 
442  // DELETE INVITATIONS
443  $query = "DELETE FROM chat_invitations WHERE ".
444  "room_id = ".$ilDB->quote($a_id)."";
445  $res = $this->ilias->db->query($query);
446 
447  // DELETE MESSAGES
448  $query = "DELETE FROM chat_room_messages WHERE ".
449  "room_id = ".$ilDB->quote($a_id)."";
450  $res = $this->ilias->db->query($query);
451 
452  // DELETE USER_DATA
453  $query = "DELETE FROM chat_user WHERE ".
454  "room_id = ".$ilDB->quote($a_id)."";
455  if ($a_owner > 0)
456  {
457  " AND owner = ".$ilDB->quote($a_owner)."";
458  }
459  $res = $this->ilias->db->query($query);
460 
461  // AND ALL RECORDINGS
462  $query = "SELECT record_id FROM chat_records WHERE
463  room_id = ".$ilDB->quote($a_id)."";
464  $res = $this->ilias->db->query($query);
465  if (ilDBx::isDbError($res)) die("ilObjChat::delete(): " . $res->getMessage() . "<br>SQL-Statement: ".$query);
466  if (($num = $res->numRows()) > 0)
467  {
468  for ($i = 0; $i < $num; $i++)
469  {
470  $data = $res->fetchRow(DB_FETCHMODE_ASSOC);
471  $this->ilias->db->query("DELETE FROM chat_record_data WHERE record_id = ".$ilDB->quote($data["record_id"])."");
472  }
473 
474  }
475  $query = "DELETE FROM chat_records WHERE
476  room_id = ".$ilDB->quote($a_id)."";
477  $res = $this->ilias->db->query($query);
478 
479  return true;
480  }
481 
482  function rename()
483  {
484  global $ilDB;
485 
486  $query = "UPDATE chat_rooms ".
487  "SET title = ".$ilDB->quote($this->getTitle())." ".
488  "WHERE room_id = ".$ilDB->quote($this->getRoomId())."";
489 
490  $res = $this->ilias->db->query($query);
491 
492  return true;
493  }
494 
495  function lookupRoomId()
496  {
497  global $ilDB;
498 
499  $query = "SELECT * FROM chat_rooms ".
500  "WHERE title = ".$ilDB->quote($this->getTitle())." ".
501  "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
502  "AND owner = ".$ilDB->quote($this->getOwnerId())."";
503 
504  $res = $this->ilias->db->query($query);
505  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
506  {
507  return $row->room_id;
508  }
509  return false;
510  }
511 
512  function add()
513  {
514  global $ilDB;
515 
516  $query = "INSERT INTO chat_rooms ".
517  "SET title = ".$ilDB->quote($this->getTitle()).", ".
518  "chat_id = ".$ilDB->quote($this->getObjId()).", ".
519  "owner = ".$ilDB->quote($this->getOwnerId())."";
520 
521  $res = $this->ilias->db->query($query);
522 
523 
524  return ($id = $this->ilias->db->getLastInsertId()) ? $id : false;
525  }
526 
527  function getInternalName()
528  {
529  if(!$this->getRoomId())
530  {
531  return $this->getObjId();
532  }
533  else
534  {
535  return $this->getObjId()."_".$this->getRoomId();
536  }
537  }
538 
539  function getRooms()
540  {
541  global $tree, $ilDB, $rbacsystem;
542 
543  $query = "SELECT DISTINCT(cr.room_id) as room_id,owner,title,cr.chat_id as chat_id FROM chat_rooms AS cr NATURAL LEFT JOIN chat_invitations ".
544  "WHERE (owner = ".$ilDB->quote($this->getUserId()).") ".
545  "OR (guest_id = ".$ilDB->quote($this->getUserId()).")";
546 
547  if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
548  {
549  $query .= " OR (1) ";
550  }
551 
552  $res = $this->ilias->db->query($query);
553  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
554  {
555  $data[$row->room_id]["room_id"] = $row->room_id;
556  $data[$row->room_id]["chat_id"] = $row->chat_id;
557  $data[$row->room_id]["owner"] = $row->owner;
558  $data[$row->room_id]["title"] = $row->title;
559  }
560  return $data ? $data : array();
561  }
562 
563  function getRoomsOfObject()
564  {
565  global $ilDB;
566 
567  $query = "SELECT * FROM chat_rooms ".
568  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
569  "AND owner = ".$ilDB->quote($this->getUserId())."";
570 
571  $res = $this->ilias->db->query($query);
572  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
573  {
574  $data[$row->room_id]["room_id"] = $row->room_id;
575  $data[$row->room_id]["owner"] = $row->owner;
576  $data[$row->room_id]["title"] = $row->title;
577  $data[$row->room_id]["owner"] = $row->owner;
578  }
579  return $data ? $data : array();
580  }
581 
583  {
584  global $ilDB;
585 
586  $query = "SELECT * FROM chat_rooms ".
587  "WHERE chat_id = ".$ilDB->quote($this->getObjId())."";
588 
589  $res = $this->ilias->db->query($query);
590  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
591  {
592  $data[$row->room_id]["room_id"] = $row->room_id;
593  $data[$row->room_id]["owner"] = $row->owner;
594  $data[$row->room_id]["title"] = $row->title;
595  $data[$row->room_id]["owner"] = $row->owner;
596  }
597  return $data ? $data : array();
598  }
599 
600  function getAllRooms()
601  {
602  global $ilObjDataCache,$ilUser,$rbacsystem;
603 
604  $obj_ids = array();
605  $unique_chats = array();
606 
607  $pub_chat_id = ilObjChat::_getPublicChatRefId();
608  if($rbacsystem->checkAccess('read',$pub_chat_id))
609  {
610  $obj_id = $ilObjDataCache->lookupObjId($pub_chat_id);
611  if(!in_array($obj_id,$obj_ids))
612  {
613  $unique_data['child'] = $pub_chat_id;
614  $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
615  $unique_data['obj_id'] = $obj_id;
616  $unique_data['ref_id'] = $pub_chat_id;
617 
618  $unique_chats[] = $unique_data;
619  $obj_ids[] = $obj_id;
620  }
621  }
622 
623  foreach(ilUtil::_getObjectsByOperations("chat","read",$ilUser->getId(),-1) as $chat_id)
624  {
625  $obj_id = $ilObjDataCache->lookupObjId($chat_id);
626  if(!in_array($obj_id,$obj_ids))
627  {
628  $unique_data['child'] = $chat_id;
629  $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
630  $unique_data['obj_id'] = $obj_id;
631  $unique_data['ref_id'] = $chat_id;
632 
633  $unique_chats[] = $unique_data;
634  $obj_ids[] = $obj_id;
635  }
636  }
637  return $unique_chats ? $unique_chats : array();
638  }
639 
640  function checkWriteAccess()
641  {
642  global $rbacsystem;
643 
644  if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
645  {
646  return true;
647  }
648 
649  if($this->isKicked($this->getUserId()))
650  {
651  return false;
652  }
653  if(!$this->getRoomId())
654  {
655  return true;
656  }
657  if($this->getUserId() == $this->getOwnerId())
658  {
659  return true;
660  }
661  if($this->isInvited($this->getUserId()))
662  {
663  return true;
664  }
665  return false;
666  }
667 
668  // PRIVATE
669  function __getCountLines()
670  {
671  global $ilDB;
672 
673  $query = "SELECT COUNT(entry_id) as number_lines FROM chat_room_messages ".
674  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
675  "AND room_id = ".$ilDB->quote($this->getRoomId())."";
676 
677  $res = $this->ilias->db->query($query);
678  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
679  {
680  return $row->number_lines;
681  }
682  return 0;
683  }
684 
685  function __deleteFirstLine()
686  {
687  global $ilDB;
688 
689  $query = "SELECT entry_id, MIN(commit_timestamp) as last_comm FROM chat_room_messages ".
690  "WHERE chat_id = ".$ilDB->quote($this->getObjId()). " ".
691  "AND room_id = ".$ilDB->quote($this->getRoomId()). " ".
692  "GROUP BY null";
693 
694  $res = $this->ilias->db->query($query);
695  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
696  {
697  $entry_id = $row->entry_id;
698  }
699  if($entry_id)
700  {
701  $query = "DELETE FROM chat_room_messages ".
702  "WHERE entry_id = ".$ilDB->quote($entry_id)."";
703 
704  $res = $this->ilias->db->query($query);
705  }
706  return true;
707  }
708 
709  function __addLine($message)
710  {
711  global $ilDB;
712 
713  $query = "INSERT INTO chat_room_messages ".
714  "VALUES('0',".$ilDB->quote($this->getObjId()).",".$ilDB->quote($this->getRoomId()).",".$ilDB->quote($message).",NOW())";
715 
716  $res = $this->ilias->db->query($query);
717 
718  $this->chat_record = new ilChatRecording($this->getObjId());
719  $this->chat_record->setRoomId($this->getRoomId());
720  if ($this->chat_record->isRecording())
721  {
722  $query = "INSERT INTO chat_record_data VALUES (
723  '0',
724  ".$ilDB->quote($this->chat_record->getRecordId()).",
725  ".$ilDB->quote($message).",
726  '" . time() . "')";
727 
728  $res = $this->ilias->db->query($query);
729  }
730 
731  return true;
732  }
733 
734 
735  function __read()
736  {
737  global $ilDB;
738 
739  $this->guests = array();
740 
741  $query = "SELECT * FROM chat_rooms ".
742  "WHERE room_id = ".$ilDB->quote($this->getRoomId())."";
743 
744  $res = $this->ilias->db->query($query);
745  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
746  {
747  $this->setTitle($row->title);
748  $this->setOwnerId($row->owner);
749  }
750 
751  $query = "SELECT * FROM chat_invitations ".
752  "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ";
753  "AND room_id = ".$ilDB->quote($this->getRoomId())."";
754 
755  $res = $this->ilias->db->query($query);
756  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
757  {
758  $this->guests[] = $row->guest_id;
759  }
760  return true;
761  }
762 
763  function _unkick($a_usr_id)
764  {
765  global $ilDB;
766 
767  $ilDB->query("UPDATE chat_user SET kicked = 0 WHERE usr_id = ".$ilDB->quote($a_usr_id)."");
768 
769  return true;
770  }
771 
772 
773 } // END class.ilChatRoom
774 ?>