ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjChat.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 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 require_once "classes/class.ilObjectGUI.php";
34 require_once "Modules/Chat/classes/class.ilChatServerConfig.php";
35 require_once "Modules/Chat/classes/class.ilChatServerCommunicator.php";
36 require_once "Modules/Chat/classes/class.ilChatUser.php";
37 require_once "Modules/Chat/classes/class.ilChatRoom.php";
38 require_once "Modules/Chat/classes/class.ilFileDataChat.php";
39 
40 class ilObjChat extends ilObject
41 {
46  var $chat_recording = null;
47 
54  public function __construct($a_id = 0,$a_call_by_reference = true)
55  {
56  $this->type = "chat";
57  $this->ilObject($a_id,$a_call_by_reference);
58 
59  $this->server_conf =& new ilChatServerConfig();
60  $this->server_comm =& new ilChatServerCommunicator($this);
61  $this->chat_user =& new ilChatUser();
62  $this->chat_room =& new ilChatRoom($this->getId());
63  }
64 
73  public static function _lookupModeratorRole($a_ref_id)
74  {
75  global $ilDB;
76 
77  $mod_title = 'il_chat_moderator_'.$a_ref_id;
78 
79  $res = $ilDB->queryf('SELECT * FROM object_data WHERE title = %s',
80  array('text'), array($mod_title));
81 
82  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
83  {
84  return $row->obj_id;
85  }
86  return 0;
87  }
88 
89 
90 
99  public function cloneObject($a_target_id,$a_copy_id = 0)
100  {
101  global $ilDB,$ilUser;
102 
103  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
104  $this->cloneAutoGeneratedRoles($new_obj);
105  }
106 
114  public function cloneAutoGeneratedRoles($new_obj)
115  {
116  global $ilLog,$rbacadmin,$rbacreview;
117 
118  $moderator = ilObjChat::_lookupModeratorRole($this->getRefId());
119  $new_moderator = ilObjChat::_lookupModeratorRole($new_obj->getRefId());
120  $source_rolf = $rbacreview->getRoleFolderIdOfObject($this->getRefId());
121  $target_rolf = $rbacreview->getRoleFolderIdOfObject($new_obj->getRefId());
122 
123  if(!$moderator || !$new_moderator || !$source_rolf || !$target_rolf)
124  {
125  $ilLog->write(__METHOD__.' : Error cloning auto generated role: il_chat_moderator');
126  }
127  $rbacadmin->copyRolePermissions($moderator,$source_rolf,$target_rolf,$new_moderator,true);
128  $ilLog->write(__METHOD__.' : Finished copying of role il_chat_moderator.');
129  }
130 
131 
132  public function read()
133  {
134  // USED ilObjectFactory::getInstance...
135  parent::read();
136 
137  $this->server_conf =& new ilChatServerConfig();
138  $this->server_comm =& new ilChatServerCommunicator($this);
139  $this->chat_user =& new ilChatUser();
140  $this->chat_room =& new ilChatRoom($this->getId());
141  }
142 
148  public function initDefaultRoles()
149  {
150  global $rbacadmin;
151 
152  // create a local role folder
153  $rolf_obj =& $this->createRoleFolder();
154 
155  // create moderator role and assign role to rolefolder...
156  $role_obj = $rolf_obj->createRole("il_chat_moderator_".$this->getRefId(),"Moderator of chat obj_no.".$this->getId());
157 
158  // grant permissions: visible,read,write,chat_moderate
159  $permissions = ilRbacReview::_getOperationIdsByName(array('visible','read','moderate'));
160  $rbacadmin->grantPermission($role_obj->getId(),
161  $permissions,
162  $this->getRefId());
163 
164  unset($rolf_obj);
165 
166  return array($role_obj->getId());
167  }
168 
169  public function delete()
170  {
171  global $ilDB;
172 
173  if(!parent::delete())
174  {
175  return false;
176  }
177  $rooms = $this->chat_room->getAllRoomsOfObject();
178  foreach($rooms as $room)
179  {
180  $this->chat_room->delete($room["room_id"]);
181  }
182 
183  // FINALLY DELETE MESSAGES IN PUBLIC ROOM
184  $res = $ilDB->manipulateF('
185  DELETE FROM chat_room_messages WHERE chat_id = %s',
186  array('integer'),array($this->getRefId()));
187 
188 
189  // AND ALL USERS
190  $res = $ilDB->manipulateF('
191  DELETE FROM chat_user WHERE chat_id = %s',
192  array('integer'), array($this->getRefId()));
193 
194 
195  // AND ALL RECORDINGS
196  $res = $ilDB->queryf('
197  SELECT record_id FROM chat_records
198  WHERE chat_id = %s',
199  array('integer'), array($this->getId()));
200 
201  if (ilDB::isDbError($res)) die("ilObjChat::delete(): " . $res->getMessage() . "<br>SQL-Statement: ".$res);
202  //if (($num = $ilDB->numRows($res)) > 0)
203  if (($num = $res->numRows()) > 0)
204  {
205  for ($i = 0; $i < $num; $i++)
206  {
207  $rec_data = $res->fetchRow(DB_FETCHMODE_ASSOC);
208  $res = $ilDB->manipulateF('
209  DELETE FROM chat_record_data WHERE record_id = %s',
210  array('integer'), array($rec_data['record_id']));
211 
212  }
213 
214  }
215  $res = $ilDB->manipulateF('
216  DELETE FROM chat_records WHERE chat_id = %s',
217  array('integer'), array($this->getId()));
218 
219  return true;
220  }
221 
222  public function sendMessage($a_id)
223  {
224  include_once "Services/Mail/classes/class.ilMail.php";
225 
226  $tmp_mail_obj = new ilMail($_SESSION["AccountId"]);
227 
228  // GET USER OBJECT
229  $tmp_user = ilObjectFactory::getInstanceByObjId($a_id);
230 
231  // GET USERS LANGUAGE
232  $tmp_lang =& new ilLanguage($tmp_user->getLanguage());
233  $tmp_lang->loadLanguageModule("chat");
234 
235  $message = $tmp_mail_obj->sendMail($this->formatRecipient($tmp_user),"","",$this->formatSubject($tmp_lang),
236  $this->formatBody($tmp_user,$tmp_lang),array(),array("normal"));
237 
238  unset($tmp_mail_obj);
239  unset($tmp_lang);
240  unset($tmp_user);
241 
242  return true;
243  }
244 
245  public function sendMessageForRoom($a_id, $room)
246  {
247  include_once "Services/Mail/classes/class.ilMail.php";
248 
249  $tmp_mail_obj = new ilMail($_SESSION["AccountId"]);
250 
251  // GET USER OBJECT
252  $tmp_user = ilObjectFactory::getInstanceByObjId($a_id);
253 
254  // GET USERS LANGUAGE
255  $tmp_lang =& new ilLanguage($tmp_user->getLanguage());
256  $tmp_lang->loadLanguageModule("chat");
257 
258  $message = $tmp_mail_obj->sendMail(self::formatRecipient($tmp_user),"","",self::formatSubject($tmp_lang),
259  $this->formatBodyForRoom($tmp_user,$tmp_lang, $room),array(),array("normal"));
260 
261  unset($tmp_mail_obj);
262  unset($tmp_lang);
263  unset($tmp_user);
264 
265  return true;
266  }
267 
268  public function getHTMLDirectory()
269  {
270  $tmp_tpl =& new ilTemplate("tpl.chat_export.html",true,true);
271 
272  $this->chat_room->setRoomId(0);
273 
274  $tmp_tpl->setVariable("CHAT_NAME",$this->getTitle());
275  $tmp_tpl->setVariable("CHAT_DATE",strftime("%c",time()));
276  $tmp_tpl->setVariable("CONTENT",$this->chat_room->getAllMessages());
277 
278  $file_obj =& new ilFileDataChat($this);
279 
280  // return directory name of index.html
281  return $file_obj->addFile('index.html',$tmp_tpl->get());
282  }
283 
284  private function formatRecipient(&$user)
285  {
286  if(is_object($user))
287  {
288  return $user->getLogin();
289  }
290  return false;
291  }
292 
293  private function formatSubject(&$lang)
294  {
295  return $lang->txt("chat_invitation_subject");
296  }
297 
298  private function formatBody(&$user,&$lang)
299  {
300  global $ilClientIniFile;
301 
302  $room_id = $this->chat_room->getRoomId();
303  $room_title = $this->chat_room->getTitle();
304 
305  $body = sprintf($this->lng->txt("chat_notification_intro"), $ilClientIniFile->readVariable("client","name"), ILIAS_HTTP_PATH)."\n\n";
306  $body .= $lang->txt("chat_invitation_body")." ";
307  $body .= $this->ilias->account->getFullname();
308  $body .= "\n";
309  $body .= $lang->txt("chat_chatroom_body").' '.$this->getTitle();
310  if ($room_title != '')
311  {
312  $body .= ', '.$room_title;
313  }
314  $body .= "\n\n";
315  $body .= $lang->txt('chat_to_chat_body');
316  $body .= ': '.ILIAS_HTTP_PATH."/ilias.php?baseClass=ilChatPresentationGUI&room_id=".$room_id."&ref_id=".$this->getRefId();
317 
318  return $body;
319  }
320 
321  private function formatBodyForRoom(&$user,&$lang, $room)
322  {
323  global $ilClientIniFile, $lng, $ilias;
324 
325  $room_id = $room->getRoomId();
326  $room_title = $room->getTitle();
327 
328  $body = sprintf($lng->txt("chat_notification_intro"), $ilClientIniFile->readVariable("client","name"), ILIAS_HTTP_PATH)."\n\n";
329  $body .= $lang->txt("chat_invitation_body")." ";
330  $body .= $ilias->account->getFullname();
331  $body .= "\n";
332  $body .= $lang->txt("chat_chatroom_body").' '.$this->getTitle();
333  if ($room_title != '')
334  {
335  $body .= ', '.$room_title;
336  }
337  $body .= "\n\n";
338  $body .= $lang->txt('chat_to_chat_body');
339  $body .= ': '.ILIAS_HTTP_PATH."/ilias.php?baseClass=ilChatPresentationGUI&room_id=".$room_id."&ref_id=".$room->getObjId();
340 
341  return $body;
342  }
343 
344  // Protected
345  // must be pubic
346  public function __initChatRecording()
347  {
348  if(!is_object($this->chat_recording))
349  {
350  include_once 'Modules/Chat/classes/class.ilChatRecording.php';
351 
352  $this->chat_recording = new ilChatRecording($this->getId());
353 
354  return true;
355  }
356  return false;
357  }
358 
359  static function _getPublicChatRefId()
360  {
361  static $public_chat_ref_id = 0;
362 
363  global $tree;
364 
365  if($public_chat_ref_id)
366  {
367  return $public_chat_ref_id;
368  }
369  else
370  {
371  foreach($tree->getSubTree($tree->getNodeData(SYSTEM_FOLDER_ID)) as $node)
372  {
373  if($node['type'] == 'chat')
374  {
375  return $public_chat_ref_id = $node['child'];
376  }
377  }
378  }
379  return false;
380  }
381 
382  // SET/GET
383 } // END class.ilObjTest
384 ?>