Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032 class ilChatRoom
00033 {
00034 var $ilias;
00035 var $lng;
00036
00037 var $error_msg;
00038
00039 var $ref_id;
00040 var $owner_id;
00041 var $room_id;
00042 var $guests;
00043 var $title;
00044
00045 var $user_id;
00046
00053 function ilChatRoom($a_id)
00054 {
00055 global $ilias,$lng,$ilUser;
00056
00057 define(MAX_LINES,1000);
00058
00059 $this->ilias =& $ilias;
00060 $this->lng =& $lng;
00061
00062 $this->obj_id = $a_id;
00063 $this->owner_id = $ilUser->getId();
00064 $this->user_id = $_SESSION["AccountId"];
00065 }
00066
00067
00068 function getErrorMessage()
00069 {
00070 return $this->error_msg;
00071 }
00072
00073 function setRoomId($a_id)
00074 {
00075 $this->room_id = $a_id;
00076
00077
00078 $this->__read();
00079 }
00080 function getRoomId()
00081 {
00082 return $this->room_id;
00083 }
00084 function getObjId()
00085 {
00086 return $this->obj_id;
00087 }
00088 function setOwnerId($a_id)
00089 {
00090 $this->owner_id = $a_id;
00091 }
00092 function getOwnerId()
00093 {
00094 return $this->owner_id;
00095 }
00096
00097 function getName()
00098 {
00099 if(!$this->getRoomId())
00100 {
00101 return $this->getObjId();
00102 }
00103 else
00104 {
00105
00106 }
00107 }
00108 function setTitle($a_title)
00109 {
00110 $this->title = $a_title;
00111 }
00112 function getTitle()
00113 {
00114 return $this->title;
00115 }
00116 function getGuests()
00117 {
00118 return $this->guests ? $this->guests : array();
00119 }
00120 function setUserId($a_id)
00121 {
00122 $this->user_id = $a_id;
00123 }
00124 function getUserId()
00125 {
00126 return $this->user_id;
00127 }
00128
00129 function invite($a_id)
00130 {
00131 global $ilDB;
00132
00133 $query = "REPLACE INTO chat_invitations ".
00134 "SET chat_id = ".$ilDB->quote( $this->getObjId() ).", ".
00135 "room_id = ".$ilDB->quote( $this->getRoomId() ).", ".
00136 "guest_id = ".$ilDB->quote( $a_id ).", ".
00137 "invitation_time = ".time();
00138
00139 $res = $this->ilias->db->query($query);
00140 }
00141 function drop($a_id)
00142 {
00143 global $ilDB;
00144
00145 $query = "DELETE FROM chat_invitations ".
00146 "WHERE chat_id = ".$ilDB->quote( $this->getObjId() )." ".
00147 "AND room_id = ".$ilDB->quote( $this->getRoomId() )." ".
00148 "AND guest_id = ".$ilDB->quote( $a_id )."";
00149
00150 $res = $this->ilias->db->query($query);
00151 }
00152
00153 function visited($a_id)
00154 {
00155 global $ilDB;
00156
00157 $query = "UPDATE chat_invitations SET guest_informed = 1 ".
00158 "WHERE chat_id = ".$ilDB->quote( $this->getObjId() )." ".
00159 "AND room_id = ".$ilDB->quote( $this->getRoomId() )." ".
00160 "AND guest_id = ".$ilDB->quote( $a_id )."";
00161
00162 $res = $this->ilias->db->query($query);
00163 }
00164
00165 function checkAccess()
00166 {
00167 global $rbacsystem;
00168
00169 if ($this->getObjId() ||
00170 $this->getRoomId())
00171 {
00172 if(!$this->isInvited($this->getUserId()) &&
00173 !$this->isOwner() &&
00174 !$rbacsystem->checkAccess('moderate', $_GET['ref_id']))
00175 {
00176 $this->setRoomId(0);
00177 return false;
00178 }
00179
00180 $this->visited($this->getUserId());
00181 }
00182 return true;
00183 }
00184
00185 function isInvited($a_id)
00186 {
00187 global $ilDB;
00188
00189 $query = "SELECT * FROM chat_invitations AS ci JOIN chat_rooms AS ca ".
00190 "WHERE ci.room_id = ca.room_id ".
00191 "AND ci.chat_id = ".$ilDB->quote($this->getObjId())." ".
00192 "AND ci.room_id = ".$ilDB->quote($this->getRoomId())." ".
00193 "AND owner = ".$ilDB->quote($this->getOwnerId())." ".
00194 "AND ci.guest_id = ".$ilDB->quote($a_id)."";
00195
00196 $res = $this->ilias->db->query($query);
00197
00198 return $res->numRows() ? true : false;
00199 }
00200 function isOwner()
00201 {
00202 return $this->getOwnerId() == $this->getUserId();
00203 }
00204
00205
00206 function appendMessageToDb($message)
00207 {
00208 if($this->__getCountLines() >= MAX_LINES)
00209 {
00210 $this->__deleteFirstLine();
00211 }
00212 $this->__addLine($message);
00213
00214 return true;
00215 }
00216 function getAllMessages()
00217 {
00218 global $ilDB;
00219
00220 $query = "SELECT message FROM chat_room_messages ".
00221 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
00222 "AND room_id = ".$ilDB->quote($this->getRoomId())." ".
00223 "ORDER BY commit_timestamp ";
00224
00225 $res = $this->ilias->db->query($query);
00226 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00227 {
00228 $data[] = $row->message;
00229 }
00230 return is_array($data) ? implode("<br />",$data) : "";
00231 }
00232 function deleteAllMessages()
00233 {
00234 global $ilDB;
00235
00236 $query = "DELETE FROM chat_room_messages ".
00237 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
00238 "AND room_id = ".$ilDB->quote($this->getRoomId())."";
00239
00240 $res = $this->ilias->db->query($query);
00241
00242 return true;
00243 }
00244
00245 function updateLastVisit()
00246 {
00247
00248 global $ilDB;
00249
00250 $query = "DELETE FROM chat_user ".
00251 "WHERE usr_id = ".$ilDB->quote($this->getUserId())."";
00252 $res = $this->ilias->db->query($query);
00253
00254 $query = "INSERT INTO chat_user ".
00255 "SET usr_id = ".$ilDB->quote($this->getUserId()).", ".
00256 "room_id = ".$ilDB->quote($this->getRoomId()).", ".
00257 "chat_id = ".$ilDB->quote($this->getObjId()).", ".
00258 "last_conn_timestamp = '".time()."'";
00259 $res = $this->ilias->db->query($query);
00260 return true;
00261 }
00262
00263 function setKicked($a_usr_id)
00264 {
00265 global $ilDB;
00266
00267 $query = "UPDATE chat_user SET kicked = '1' ".
00268 "WHERE usr_id = ".$ilDB->quote($a_usr_id)." ".
00269 "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
00270 "AND room_id = '0'";
00271
00272 $this->ilias->db->query($query);
00273
00274 return true;
00275 }
00276
00277 function setUnkicked($a_usr_id)
00278 {
00279 global $ilDB;
00280
00281 $query = "UPDATE chat_user SET kicked = '0' ".
00282 "WHERE usr_id = ".$ilDB->quote($a_usr_id)." ".
00283 "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
00284 "AND room_id = '0'";
00285
00286 $this->ilias->db->query($query);
00287
00288 return true;
00289 }
00290
00291 function isKicked($a_usr_id)
00292 {
00293 global $ilDB;
00294
00295 $query = "SELECT * FROM chat_user ".
00296 "WHERE kicked = 1 ".
00297 "AND usr_id = ".$ilDB->quote($a_usr_id)." ".
00298 "AND chat_id = ".$ilDB->quote($this->getObjId())."";
00299
00300 $res = $this->ilias->db->query($query);
00301
00302 return $res->numRows() ? true : false;
00303 }
00304
00305 function getCountActiveUser($chat_id,$room_id)
00306 {
00307 global $ilDB;
00308
00309 $query = "SELECT * FROM chat_user ".
00310 "WHERE chat_id = ".$ilDB->quote($chat_id)." ".
00311 "AND room_id = ".$ilDB->quote($room_id)." ".
00312 "AND last_conn_timestamp > ".time()." - 40";
00313 $res = $this->ilias->db->query($query);
00314
00315 return $res->numRows();
00316 }
00317
00318 function _getCountActiveUsers($chat_id,$room_id = 0)
00319 {
00320 global $ilDB;
00321
00322 $query = "SELECT * FROM chat_user ".
00323 "WHERE chat_id = ".$ilDB->quote($chat_id)." ".
00324 "AND room_id = ".$ilDB->quote($room_id)." ".
00325 "AND last_conn_timestamp > ".time()." - 40";
00326 $res = $ilDB->query($query);
00327
00328 return $res->numRows();
00329 }
00330
00331
00332 function getActiveUsers()
00333 {
00334 global $ilDB;
00335
00336 $query = "SELECT * FROM chat_user ".
00337 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
00338 "AND room_id = ".$ilDB->quote($this->room_id)." ".
00339 "AND last_conn_timestamp > ".time()." - 40";
00340 $res = $this->ilias->db->query($query);
00341 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00342 {
00343 $usr_ids[] = $row->usr_id;
00344 }
00345 return $usr_ids ? $usr_ids : array();
00346 }
00347
00348
00349 function _isActive($usr_id)
00350 {
00351 global $ilDB;
00352
00353 $query = "SELECT * FROM chat_user ".
00354 "WHERE room_id = 0 ".
00355 "AND usr_id = ".$ilDB->quote((int) $usr_id)." ".
00356 "AND last_conn_timestamp > ".time()." - 40";
00357
00358 $res = $ilDB->query($query);
00359 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00360 {
00361 return $row->chat_id;
00362 }
00363 return false;
00364 }
00365
00366 function getOnlineUsers()
00367 {
00368
00369 return ilUtil::getUsersOnline();
00370 }
00371
00372 function validate()
00373 {
00374 $this->error_msg = "";
00375
00376 if(!$this->getTitle())
00377 {
00378 $this->error_msg .= $this->lng->txt("chat_title_missing");
00379 }
00380 if(!$this->getOwnerId())
00381 {
00382 $this->ilias->raiseError("MISSING OWNER ID",$this->ilias->error_obj->FATAL);
00383 }
00384 return $this->error_msg ? false : true;
00385 }
00386 function deleteRooms($a_ids)
00387 {
00388 if(!is_array($a_ids))
00389 {
00390 $this->ilias->raiseError("ARRAY REQUIRED",$this->ilias->error_obj->FATAL);
00391 }
00392 foreach($a_ids as $id)
00393 {
00394 $this->delete($id);
00395 }
00396 return true;
00397 }
00398
00399 function delete($a_id, $a_owner = 0)
00400 {
00401
00402 global $ilDB;
00403
00404 $query = "DELETE FROM chat_rooms WHERE ".
00405 "room_id = ".$ilDB->quote($a_id)."";
00406 if ($a_owner > 0)
00407 {
00408 " AND owner = ".$ilDB->quote($a_owner)."";
00409 }
00410 $res = $this->ilias->db->query($query);
00411
00412
00413 $query = "DELETE FROM chat_invitations WHERE ".
00414 "room_id = ".$ilDB->quote($a_id)."";
00415 $res = $this->ilias->db->query($query);
00416
00417
00418 $query = "DELETE FROM chat_room_messages WHERE ".
00419 "room_id = ".$ilDB->quote($a_id)."";
00420 $res = $this->ilias->db->query($query);
00421
00422
00423 $query = "DELETE FROM chat_user WHERE ".
00424 "room_id = ".$ilDB->quote($a_id)."";
00425 if ($a_owner > 0)
00426 {
00427 " AND owner = ".$ilDB->quote($a_owner)."";
00428 }
00429 $res = $this->ilias->db->query($query);
00430
00431
00432 $query = "SELECT record_id FROM chat_records WHERE
00433 room_id = ".$ilDB->quote($a_id)."";
00434 $res = $this->ilias->db->query($query);
00435 if (DB::isError($res)) die("ilObjChat::delete(): " . $res->getMessage() . "<br>SQL-Statement: ".$query);
00436 if (($num = $res->numRows()) > 0)
00437 {
00438 for ($i = 0; $i < $num; $i++)
00439 {
00440 $data = $res->fetchRow(DB_FETCHMODE_ASSOC);
00441 $this->ilias->db->query("DELETE FROM chat_record_data WHERE record_id = ".$ilDB->quote($data["record_id"])."");
00442 }
00443
00444 }
00445 $query = "DELETE FROM chat_records WHERE
00446 room_id = ".$ilDB->quote($a_id)."";
00447 $res = $this->ilias->db->query($query);
00448
00449 return true;
00450 }
00451
00452 function rename()
00453 {
00454 global $ilDB;
00455
00456 $query = "UPDATE chat_rooms ".
00457 "SET title = ".$ilDB->quote($this->getTitle())." ".
00458 "WHERE room_id = ".$ilDB->quote($this->getRoomId())."";
00459
00460 $res = $this->ilias->db->query($query);
00461
00462 return true;
00463 }
00464
00465 function lookupRoomId()
00466 {
00467 global $ilDB;
00468
00469 $query = "SELECT * FROM chat_rooms ".
00470 "WHERE title = ".$ilDB->quote($this->getTitle())." ".
00471 "AND chat_id = ".$ilDB->quote($this->getObjId())." ".
00472 "AND owner = ".$ilDB->quote($this->getOwnerId())."";
00473
00474 $res = $this->ilias->db->query($query);
00475 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00476 {
00477 return $row->room_id;
00478 }
00479 return false;
00480 }
00481
00482 function add()
00483 {
00484 global $ilDB;
00485
00486 $query = "INSERT INTO chat_rooms ".
00487 "SET title = ".$ilDB->quote($this->getTitle()).", ".
00488 "chat_id = ".$ilDB->quote($this->getObjId()).", ".
00489 "owner = ".$ilDB->quote($this->getOwnerId())."";
00490
00491 $res = $this->ilias->db->query($query);
00492
00493
00494 return ($id = $this->ilias->db->getLastInsertId()) ? $id : false;
00495 }
00496
00497 function getInternalName()
00498 {
00499 if(!$this->getRoomId())
00500 {
00501 return $this->getObjId();
00502 }
00503 else
00504 {
00505 return $this->getObjId()."_".$this->getRoomId();
00506 }
00507 }
00508
00509 function getRooms()
00510 {
00511 global $ilUser, $tree, $ilDB, $rbacsystem;
00512
00513 $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 ".
00514 "WHERE (owner = ".$ilDB->quote($this->getUserId()).") ".
00515 "OR (guest_id = ".$ilDB->quote($this->getUserId()).") ";
00516
00517 if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
00518 {
00519 $query .= " OR (1) ";
00520 }
00521
00522 $res = $this->ilias->db->query($query);
00523 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00524 {
00525 $data[$row->room_id]["room_id"] = $row->room_id;
00526 $data[$row->room_id]["chat_id"] = $row->chat_id;
00527 $data[$row->room_id]["owner"] = $row->owner;
00528 $data[$row->room_id]["title"] = $row->title;
00529 }
00530 return $data ? $data : array();
00531 }
00532
00533 function getRoomsOfObject()
00534 {
00535 global $ilDB;
00536
00537 $query = "SELECT * FROM chat_rooms ".
00538 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
00539 "AND owner = ".$ilDB->quote($this->getUserId())."";
00540
00541 $res = $this->ilias->db->query($query);
00542 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00543 {
00544 $data[$row->room_id]["room_id"] = $row->room_id;
00545 $data[$row->room_id]["owner"] = $row->owner;
00546 $data[$row->room_id]["title"] = $row->title;
00547 $data[$row->room_id]["owner"] = $row->owner;
00548 }
00549 return $data ? $data : array();
00550 }
00551
00552 function getAllRoomsOfObject()
00553 {
00554 global $ilDB;
00555
00556 $query = "SELECT * FROM chat_rooms ".
00557 "WHERE chat_id = ".$ilDB->quote($this->getObjId())."";
00558
00559 $res = $this->ilias->db->query($query);
00560 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00561 {
00562 $data[$row->room_id]["room_id"] = $row->room_id;
00563 $data[$row->room_id]["owner"] = $row->owner;
00564 $data[$row->room_id]["title"] = $row->title;
00565 $data[$row->room_id]["owner"] = $row->owner;
00566 }
00567 return $data ? $data : array();
00568 }
00569
00570 function getAllRooms()
00571 {
00572 global $ilObjDataCache,$ilUser,$rbacsystem;
00573
00574 $obj_ids = array();
00575 $unique_chats = array();
00576
00577 $pub_chat_id = ilObjChat::_getPublicChatRefId();
00578 if($rbacsystem->checkAccess('read',$pub_chat_id))
00579 {
00580 $obj_id = $ilObjDataCache->lookupObjId($pub_chat_id);
00581 if(!in_array($obj_id,$obj_ids))
00582 {
00583 $unique_data['child'] = $pub_chat_id;
00584 $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
00585 $unique_data['obj_id'] = $obj_id;
00586 $unique_data['ref_id'] = $pub_chat_id;
00587
00588 $unique_chats[] = $unique_data;
00589 $obj_ids[] = $obj_id;
00590 }
00591 }
00592
00593 foreach(ilUtil::_getObjectsByOperations("chat","read",$ilUser->getId(),-1) as $chat_id)
00594 {
00595 $obj_id = $ilObjDataCache->lookupObjId($chat_id);
00596 if(!in_array($obj_id,$obj_ids))
00597 {
00598 $unique_data['child'] = $chat_id;
00599 $unique_data['title'] = $ilObjDataCache->lookupTitle($obj_id);
00600 $unique_data['obj_id'] = $obj_id;
00601 $unique_data['ref_id'] = $chat_id;
00602
00603 $unique_chats[] = $unique_data;
00604 $obj_ids[] = $obj_id;
00605 }
00606 }
00607 return $unique_chats ? $unique_chats : array();
00608 }
00609
00610 function checkWriteAccess()
00611 {
00612 global $rbacsystem;
00613
00614 if($rbacsystem->checkAccess('moderate', $_GET['ref_id']))
00615 {
00616 return true;
00617 }
00618
00619 if($this->isKicked($this->getUserId()))
00620 {
00621 return false;
00622 }
00623 if(!$this->getRoomId())
00624 {
00625 return true;
00626 }
00627 if($this->getUserId() == $this->getOwnerId())
00628 {
00629 return true;
00630 }
00631 if($this->isInvited($this->getUserId()))
00632 {
00633 return true;
00634 }
00635 return false;
00636 }
00637
00638
00639 function __getCountLines()
00640 {
00641 global $ilDB;
00642
00643 $query = "SELECT COUNT(entry_id) as number_lines FROM chat_room_messages ".
00644 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ".
00645 "AND room_id = ".$ilDB->quote($this->getRoomId())."";
00646
00647 $res = $this->ilias->db->query($query);
00648 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00649 {
00650 return $row->number_lines;
00651 }
00652 return 0;
00653 }
00654
00655 function __deleteFirstLine()
00656 {
00657 global $ilDB;
00658
00659 $query = "SELECT entry_id, MIN(commit_timestamp) as last_comm FROM chat_room_messages ".
00660 "WHERE chat_id = ".$ilDB->quote($this->getObjId()). " ".
00661 "AND room_id = ".$ilDB->quote($this->getRoomId()). " ".
00662 "GROUP BY null";
00663
00664 $res = $this->ilias->db->query($query);
00665 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00666 {
00667 $entry_id = $row->entry_id;
00668 }
00669 if($entry_id)
00670 {
00671 $query = "DELETE FROM chat_room_messages ".
00672 "WHERE entry_id = ".$ilDB->quote($entry_id)."";
00673
00674 $res = $this->ilias->db->query($query);
00675 }
00676 return true;
00677 }
00678
00679 function __addLine($message)
00680 {
00681 global $ilDB;
00682
00683 $query = "INSERT INTO chat_room_messages ".
00684 "VALUES('0',".$ilDB->quote($this->getObjId()).",".$ilDB->quote($this->getRoomId()).",".$ilDB->quote($message).",NOW())";
00685
00686 $res = $this->ilias->db->query($query);
00687
00688 $this->chat_record = new ilChatRecording($this->getObjId());
00689 $this->chat_record->setRoomId($this->getRoomId());
00690 if ($this->chat_record->isRecording())
00691 {
00692 $query = "INSERT INTO chat_record_data VALUES (
00693 '0',
00694 ".$ilDB->quote($this->chat_record->getRecordId()).",
00695 ".$ilDB->quote($message).",
00696 '" . time() . "')";
00697
00698 $res = $this->ilias->db->query($query);
00699 }
00700
00701 return true;
00702 }
00703
00704
00705 function __read()
00706 {
00707 global $ilDB;
00708
00709 $this->guests = array();
00710
00711 $query = "SELECT * FROM chat_rooms ".
00712 "WHERE room_id = ".$ilDB->quote($this->getRoomId())."";
00713
00714 $res = $this->ilias->db->query($query);
00715 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00716 {
00717 $this->setTitle($row->title);
00718 $this->setOwnerId($row->owner);
00719 }
00720
00721 $query = "SELECT * FROM chat_invitations ".
00722 "WHERE chat_id = ".$ilDB->quote($this->getObjId())." ";
00723 "AND room_id = ".$ilDB->quote($this->getRoomId())."";
00724
00725 $res = $this->ilias->db->query($query);
00726 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00727 {
00728 $this->guests[] = $row->guest_id;
00729 }
00730 return true;
00731 }
00732
00733 function _unkick($a_usr_id)
00734 {
00735 global $ilDB;
00736
00737 $ilDB->query("UPDATE chat_user SET kicked = 0 WHERE usr_id = ".$ilDB->quote($a_usr_id)."");
00738
00739 return true;
00740 }
00741
00742
00743 }
00744 ?>