• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

chat/classes/class.ilChatRoom.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00033 class ilChatRoom
00034 {
00035         var $ilias;
00036         var $lng;
00037 
00038         var $error_msg;
00039 
00040         var $ref_id; // OF CHAT OBJECT
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         // SET/GET
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                 // READ DATA OF ROOM
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                         // GET NAME OF PRIVATE CHATROOM
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         // METHODS FOR EXPORTTING CHAT
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                 // CHECK IF OLD DATA EXISTS
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->getRefId()."', ".
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->getRefId()."' ".
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->getRefId()."' ".
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->getRefId()."'";
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 getActiveUsers()
00278         {
00279                 $query = "SELECT * FROM chat_user ".
00280                         "WHERE chat_id = '".$this->ref_id."' ".
00281                         "AND room_id = '".$this->room_id."' ".
00282                         "AND last_conn_timestamp > ".time()." - 40";
00283                 $res = $this->ilias->db->query($query);
00284                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00285                 {
00286                         $usr_ids[] = $row->usr_id;
00287                 }
00288                 return $usr_ids ? $usr_ids : array();
00289         }
00290 
00291         function getOnlineUsers()
00292         {
00293                 // TODO: CHECK INVITABLE AND ALLOW MESSAGES 
00294                 return ilUtil::getUsersOnline();
00295         }
00296 
00297         function validate()
00298         {
00299                 $this->error_msg = "";
00300 
00301                 if(!$this->getTitle())
00302                 {
00303                         $this->error_msg .= $this->lng->txt("chat_title_missing");
00304                 }
00305                 if(!$this->getOwnerId())
00306                 {
00307                         $this->ilias->raiseError("MISSING OWNER ID",$this->ilias->error_obj->FATAL);
00308                 }
00309                 return $this->error_msg ? false : true;
00310         }
00311         function deleteRooms($a_ids)
00312         {
00313                 if(!is_array($a_ids))
00314                 {
00315                         $this->ilias->raiseError("ARRAY REQUIRED",$this->ilias->error_obj->FATAL);
00316                 }
00317                 foreach($a_ids as $id)
00318                 {
00319                         $this->delete($id);
00320                 }
00321                 return true;
00322         }
00323 
00324         function delete($a_id, $a_owner = 0)
00325         {
00326                 // DELETE ROOM
00327                 $query = "DELETE FROM chat_rooms WHERE ".
00328                         "room_id = '".$a_id."'";
00329                 if ($a_owner > 0)
00330                 {
00331                         " AND owner = '".$a_owner."'";
00332                 }
00333                 $res = $this->ilias->db->query($query);
00334 
00335                 // DELETE INVITATIONS
00336                 $query = "DELETE FROM chat_invitations WHERE ".
00337                         "room_id = '".$a_id."'";
00338                 $res = $this->ilias->db->query($query);
00339 
00340                 // DELETE MESSAGES
00341                 $query = "DELETE FROM chat_room_messages WHERE ".
00342                         "room_id = '".$a_id."'";
00343                 $res = $this->ilias->db->query($query);
00344 
00345                 // DELETE USER_DATA
00346                 $query = "DELETE FROM chat_user WHERE ".
00347                         "room_id = '".$a_id."'";
00348                 if ($a_owner > 0)
00349                 {
00350                         " AND owner = '".$a_owner."'";
00351                 }
00352                 $res = $this->ilias->db->query($query);
00353                         
00354                 // AND ALL RECORDINGS
00355                 $query = "SELECT record_id FROM chat_records WHERE 
00356                                         room_id = '".$a_id."'";
00357                 $res = $this->ilias->db->query($query);
00358                 if (DB::isError($res)) die("ilObjChat::delete(): " . $res->getMessage() . "<br>SQL-Statement: ".$q);
00359                 if (($num = $res->numRows()) > 0)
00360                 {
00361                         for ($i = 0; $i < $num; $i++)
00362                         {
00363                                 $data = $res->fetchRow(DB_FETCHMODE_ASSOC);
00364                                 $this->ilias->db->query("DELETE FROM chat_record_data WHERE record_id = '" . $data["record_id"] . "'");
00365                         }
00366                         
00367                 }
00368                 $query = "DELETE FROM chat_records WHERE 
00369                                         room_id = '".$a_id."'";
00370                 $res = $this->ilias->db->query($query);
00371 
00372                 return true;
00373         }
00374 
00375         function rename()
00376         {
00377                 $query = "UPDATE chat_rooms ".
00378                         "SET title = '".ilUtil::prepareDBString($this->getTitle())."' ".
00379                         "WHERE room_id = '".$this->getRoomId()."'";
00380 
00381                 $res = $this->ilias->db->query($query);
00382 
00383                 return true;
00384         }
00385 
00386         function add()
00387         {
00388                 $query = "INSERT INTO chat_rooms ".
00389                         "SET title = '".ilUtil::prepareDBString($this->getTitle())."', ".
00390                         "chat_id = '".$this->getRefId()."', ".
00391                         "owner = '".$this->getOwnerId()."'";
00392 
00393                 $res = $this->ilias->db->query($query);
00394 
00395                 return true;
00396         }
00397 
00398         function getInternalName()
00399         {
00400                 if(!$this->getRoomId())
00401                 {
00402                         return $this->getRefId();
00403                 }
00404                 else
00405                 {
00406                         return $this->getRefId()."_".$this->getRoomId();
00407                 }
00408         }
00409         function getRooms()
00410         {
00411                 global $tree;
00412 
00413                 $query = "SELECT DISTINCT(cr.room_id) as room_id,owner,title,chat_id FROM chat_rooms AS cr NATURAL LEFT JOIN chat_invitations ".
00414                         "WHERE (owner = '".$this->getUserId()."') ".
00415                         "OR (guest_id = '".$this->getUserId()."')";
00416 
00417                 $res = $this->ilias->db->query($query);
00418                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00419                 {
00420                         if(!$tree->isInTree($row->chat_id))
00421                         {
00422                                 continue;
00423                         }
00424 
00425                         $data[$row->room_id]["room_id"] = $row->room_id;
00426                         $data[$row->room_id]["chat_id"] = $row->chat_id;
00427                         $data[$row->room_id]["owner"] = $row->owner;
00428                         $data[$row->room_id]["title"] = $row->title;
00429                 }
00430                 return $data ? $data : array();
00431         }
00432                 
00433 
00434 
00435         function getRoomsOfObject()
00436         {
00437                 $query = "SELECT * FROM chat_rooms ".
00438                         "WHERE chat_id = '".$this->getRefId()."' ".
00439                         "AND owner = '".$this->getUserId()."'";
00440 
00441                 $res = $this->ilias->db->query($query);
00442                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00443                 {
00444                         $data[$row->room_id]["room_id"] = $row->room_id;
00445                         $data[$row->room_id]["owner"] = $row->owner;
00446                         $data[$row->room_id]["title"] = $row->title;
00447                         $data[$row->room_id]["owner"] = $row->owner;
00448                 }
00449                 return $data ? $data : array();
00450         }
00451         function getAllRoomsOfObject()
00452         {
00453                 $query = "SELECT * FROM chat_rooms ".
00454                         "WHERE chat_id = '".$this->getRefId()."'";
00455 
00456                 $res = $this->ilias->db->query($query);
00457                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00458                 {
00459                         $data[$row->room_id]["room_id"] = $row->room_id;
00460                         $data[$row->room_id]["owner"] = $row->owner;
00461                         $data[$row->room_id]["title"] = $row->title;
00462                         $data[$row->room_id]["owner"] = $row->owner;
00463                 }
00464                 return $data ? $data : array();
00465         }               
00466 
00467                         
00468 
00469         function getAllRooms()
00470         {
00471                 return ilUtil::getObjectsByOperations("chat","read");
00472         }
00473 
00474         function checkWriteAccess()
00475         {
00476                 if($this->isKicked($this->getUserId()))
00477                 {
00478                         return false;
00479                 }
00480                 if(!$this->getRoomId())
00481                 {
00482                         return true;
00483                 }
00484                 if($this->getUserId() == $this->getOwnerId())
00485                 {
00486                         return true;
00487                 }
00488                 if($this->isInvited($this->getUserId()))
00489                 {
00490                         return true;
00491                 }
00492                 return false;
00493         }
00494 
00495         // PRIVATE
00496         function __getCountLines()
00497         {
00498                 $query = "SELECT COUNT(entry_id) as number_lines FROM chat_room_messages ".
00499                         "WHERE chat_id = '".$this->getRefId()."' ".
00500                         "AND room_id = '".$this->getRoomId()."'";
00501 
00502                 $res = $this->ilias->db->query($query);
00503                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00504                 {
00505                         return $row->number_lines;
00506                 }
00507                 return 0;
00508         }
00509         function __deleteFirstLine()
00510         {
00511                 $query = "SELECT entry_id, MIN(commit_timestamp) as last_comm FROM chat_room_messages ".
00512                         "WHERE chat_id = '".$this->getRefId()."' ".
00513                         "AND room_id = '".$this->getRoomId()."' ".
00514                         "GROUP BY null";
00515 
00516                 $res = $this->ilias->db->query($query);
00517                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00518                 {
00519                         $entry_id = $row->entry_id;
00520                 }
00521                 if($entry_id)
00522                 {
00523                         $query = "DELETE FROM chat_room_messages ".
00524                                 "WHERE entry_id = '".$entry_id."'";
00525                         
00526                         $res = $this->ilias->db->query($query);
00527                 }
00528                 return true;
00529         }
00530         function __addLine($message)
00531         {
00532                 $query = "INSERT INTO chat_room_messages ".
00533                         "VALUES('0','".$this->getRefId()."','".$this->getRoomId()."','".addslashes($message)."',now())";
00534                 
00535                 $res = $this->ilias->db->query($query);
00536 
00537                 $this->chat_record = new ilChatRecording($this->getRefId());
00538                 $this->chat_record->setRoomId($this->getRoomId());
00539                 if ($this->chat_record->isRecording())
00540                 {
00541                         $query = "INSERT INTO chat_record_data VALUES (
00542                                                 '0', 
00543                                                 '" . $this->chat_record->getRecordId() . "', 
00544                                                 '" . addslashes($message) . "', 
00545                                                 '" . time() . "')";
00546 
00547                         $res = $this->ilias->db->query($query);
00548                 }
00549 
00550                 return true;
00551         }
00552 
00553 
00554         function __read()
00555         {
00556                 $this->guests = array();
00557 
00558                 $query = "SELECT * FROM chat_rooms ".
00559                         "WHERE room_id = '".$this->getRoomId()."'";
00560 
00561                 $res = $this->ilias->db->query($query);
00562                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00563                 {
00564                         $this->setTitle($row->title);
00565                         $this->setOwnerId($row->owner);
00566                 }
00567 
00568                 $query = "SELECT * FROM chat_invitations ".
00569                         "WHERE room_id = '".$this->getRoomId()."'";
00570 
00571                 $res = $this->ilias->db->query($query);
00572                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00573                 {
00574                         $this->guests[] = $row->guest_id;
00575                 }
00576                 return true;
00577         }
00578 
00579         function _unkick($a_usr_id)
00580         {
00581                 global $ilDB;
00582 
00583                 $ilDB->query("UPDATE chat_user SET kicked = 0 WHERE usr_id = '".$a_usr_id."'");
00584 
00585                 return true;
00586         }
00587 
00588 
00589 } // END class.ilChatRoom
00590 ?>

Generated on Fri Dec 13 2013 10:18:26 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1