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->ref_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 getRefId()
00086 {
00087 return $this->ref_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->getRefId();
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->getRefId()."' ".
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->getRefId()."' ".
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 = "SELECT * FROM chat_user ".
00217 "WHERE usr_id = '".$this->getUserId()."' ".
00218 "AND chat_id = '".$this->getRefId()."' ".
00219 "AND room_id = '".$this->getRoomId()."'";
00220
00221 $res = $this->ilias->db->query($query);
00222 if($res->numRows())
00223 {
00224 $query = "UPDATE chat_user ".
00225 "SET last_conn_timestamp = '".time()."' ".
00226 "WHERE usr_id = '".$this->getUserId()."' ".
00227 "AND room_id = '".$this->getRoomId()."' ".
00228 "AND chat_id = '".$this->getRefId()."'";
00229 }
00230 else
00231 {
00232 $query = "INSERT INTO chat_user ".
00233 "SET usr_id = '".$this->getUserId()."', ".
00234 "room_id = '".$this->getRoomId()."', ".
00235 "chat_id = '".$this->getRefId()."', ".
00236 "last_conn_timestamp = '".time()."'";
00237 }
00238 $res = $this->ilias->db->query($query);
00239 return true;
00240 }
00241 function getCountActiveUser($chat_id,$room_id)
00242 {
00243 $query = "SELECT * FROM chat_user ".
00244 "WHERE chat_id = '".$chat_id."' ".
00245 "AND room_id = '".$room_id."' ".
00246 "AND last_conn_timestamp > ".time()." - 30";
00247 $res = $this->ilias->db->query($query);
00248
00249 return $res->numRows();
00250 }
00251
00252 function getActiveUsers()
00253 {
00254 $query = "SELECT * FROM chat_user ".
00255 "WHERE chat_id = '".$this->ref_id."' ".
00256 "AND room_id = '".$this->room_id."' ".
00257 "AND last_conn_timestamp > ".time()." - 30";
00258 $res = $this->ilias->db->query($query);
00259 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00260 {
00261 $usr_ids[] = $row->usr_id;
00262 }
00263 return $usr_ids ? $usr_ids : array();
00264 }
00265
00266 function getOnlineUsers()
00267 {
00268
00269 return ilUtil::getUsersOnline();
00270 }
00271
00272 function validate()
00273 {
00274 $this->error_msg = "";
00275
00276 if(!$this->getTitle())
00277 {
00278 $this->error_msg .= $this->lng->txt("chat_title_missing");
00279 }
00280 if(!$this->getOwnerId())
00281 {
00282 $this->ilias->raiseError("MISSING OWNER ID",$this->ilias->error_obj->FATAL);
00283 }
00284 return $this->error_msg ? false : true;
00285 }
00286 function deleteRooms($a_ids)
00287 {
00288 if(!is_array($a_ids))
00289 {
00290 $this->ilias->raiseError("ARRAY REQUIRED",$this->ilias->error_obj->FATAL);
00291 }
00292 foreach($a_ids as $id)
00293 {
00294 $this->delete($id);
00295 }
00296 return true;
00297 }
00298
00299 function delete($a_id)
00300 {
00301
00302 $query = "DELETE FROM chat_rooms WHERE ".
00303 "room_id = '".$a_id."' ".
00304 "AND chat_id = '".$this->getRefId()."'";
00305 $res = $this->ilias->db->query($query);
00306
00307
00308 $query = "DELETE FROM chat_invitations WHERE ".
00309 "room_id = '".$a_id."'";
00310 $res = $this->ilias->db->query($query);
00311
00312
00313 $query = "DELETE FROM chat_room_messages WHERE ".
00314 "room_id = '".$a_id."'";
00315 $res = $this->ilias->db->query($query);
00316
00317
00318 $query = "DELETE FROM chat_user WHERE ".
00319 "room_id = '".$a_id."' ".
00320 "AND chat_id = '".$this->getRefId()."'";
00321 $res = $this->ilias->db->query($query);
00322
00323 return true;
00324 }
00325
00326 function rename()
00327 {
00328 $query = "UPDATE chat_rooms ".
00329 "SET title = '".ilUtil::prepareDBString($this->getTitle())."' ".
00330 "WHERE room_id = '".$this->getRoomId()."'";
00331
00332 $res = $this->ilias->db->query($query);
00333
00334 return true;
00335 }
00336
00337 function add()
00338 {
00339 $query = "INSERT INTO chat_rooms ".
00340 "SET title = '".ilUtil::prepareDBString($this->getTitle())."', ".
00341 "chat_id = '".$this->getRefId()."', ".
00342 "owner = '".$this->getOwnerId()."'";
00343
00344 $res = $this->ilias->db->query($query);
00345
00346 return true;
00347 }
00348
00349 function getInternalName()
00350 {
00351 if(!$this->getRoomId())
00352 {
00353 return $this->getRefId();
00354 }
00355 else
00356 {
00357 return $this->getRefId()."_".$this->getRoomId();
00358 }
00359 }
00360 function getRooms()
00361 {
00362 global $tree;
00363
00364 $query = "SELECT DISTINCT(cr.room_id) as room_id,owner,title,chat_id FROM chat_rooms AS cr NATURAL LEFT JOIN chat_invitations ".
00365 "WHERE (owner = '".$this->getUserId()."') ".
00366 "OR (guest_id = '".$this->getUserId()."')";
00367
00368 $res = $this->ilias->db->query($query);
00369 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00370 {
00371 if(!$tree->isInTree($row->chat_id))
00372 {
00373 continue;
00374 }
00375
00376 $data[$row->room_id]["room_id"] = $row->room_id;
00377 $data[$row->room_id]["chat_id"] = $row->chat_id;
00378 $data[$row->room_id]["owner"] = $row->owner;
00379 $data[$row->room_id]["title"] = $row->title;
00380 }
00381 return $data ? $data : array();
00382 }
00383
00384
00385
00386 function getRoomsOfObject()
00387 {
00388 $query = "SELECT * FROM chat_rooms ".
00389 "WHERE chat_id = '".$this->getRefId()."' ".
00390 "AND owner = '".$this->getUserId()."'";
00391
00392 $res = $this->ilias->db->query($query);
00393 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00394 {
00395 $data[$row->room_id]["room_id"] = $row->room_id;
00396 $data[$row->room_id]["owner"] = $row->owner;
00397 $data[$row->room_id]["title"] = $row->title;
00398 $data[$row->room_id]["owner"] = $row->owner;
00399 }
00400 return $data ? $data : array();
00401 }
00402 function getAllRoomsOfObject()
00403 {
00404 $query = "SELECT * FROM chat_rooms ".
00405 "WHERE chat_id = '".$this->getRefId()."'";
00406
00407 $res = $this->ilias->db->query($query);
00408 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00409 {
00410 $data[] = $row->room_id;
00411 }
00412 return $data ? $data : array();
00413 }
00414
00415
00416
00417 function getAllRooms()
00418 {
00419 return ilUtil::getObjectsByOperations("chat","read");
00420 }
00421
00422 function checkWriteAccess()
00423 {
00424 if(!$this->getRoomId())
00425 {
00426 return true;
00427 }
00428 if($this->getUserId() == $this->getOwnerId())
00429 {
00430 return true;
00431 }
00432 if($this->isInvited($this->getUserId()))
00433 {
00434 return true;
00435 }
00436 return false;
00437 }
00438
00439
00440 function __getCountLines()
00441 {
00442 $query = "SELECT COUNT(entry_id) as number_lines FROM chat_room_messages ".
00443 "WHERE chat_id = '".$this->getRefId()."' ".
00444 "AND room_id = '".$this->getRoomId()."'";
00445
00446 $res = $this->ilias->db->query($query);
00447 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00448 {
00449 return $row->number_lines;
00450 }
00451 return 0;
00452 }
00453 function __deleteFirstLine()
00454 {
00455 $query = "SELECT entry_id, MIN(commit_timestamp) as last_comm FROM chat_room_messages ".
00456 "WHERE chat_id = '".$this->getRefId()."' ".
00457 "AND room_id = '".$this->getRoomId()."' ".
00458 "GROUP BY null";
00459
00460 $res = $this->ilias->db->query($query);
00461 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00462 {
00463 $entry_id = $row->entry_id;
00464 }
00465 if($entry_id)
00466 {
00467 $query = "DELETE FROM chat_room_messages ".
00468 "WHERE entry_id = '".$entry_id."'";
00469
00470 $res = $this->ilias->db->query($query);
00471 }
00472 return true;
00473 }
00474 function __addLine($message)
00475 {
00476 $query = "INSERT INTO chat_room_messages ".
00477 "VALUES('0','".$this->getRefId()."','".$this->getRoomId()."','".addslashes($message)."',now())";
00478
00479 $res = $this->ilias->db->query($query);
00480
00481 return true;
00482 }
00483
00484
00485 function __read()
00486 {
00487 $this->guests = array();
00488
00489 $query = "SELECT * FROM chat_rooms ".
00490 "WHERE room_id = '".$this->getRoomId()."'";
00491
00492 $res = $this->ilias->db->query($query);
00493 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00494 {
00495 $this->setTitle($row->title);
00496 $this->setOwnerId($row->owner);
00497 }
00498
00499 $query = "SELECT * FROM chat_invitations ".
00500 "WHERE room_id = '".$this->getRoomId()."'";
00501
00502 $res = $this->ilias->db->query($query);
00503 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00504 {
00505 $this->guests[] = $row->guest_id;
00506 }
00507 return true;
00508 }
00509
00510 }
00511 ?>