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