ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomViewTask.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5 require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
6 
14 {
18  private $gui;
19 
23  public function __construct(ilDBayObjectGUI $gui)
24  {
25  $this->gui = $gui;
26  }
27 
32  private function cancelJoin($message)
33  {
34  ilUtil::sendFailure($message);
35  }
36 
42  private function showRoom(ilChatroom $room, ilChatroomUser $chat_user)
43  {
52  global $tpl, $ilUser, $ilCtrl, $rbacsystem, $lng, $ilNavigationHistory;
53 
54  if(!ilChatroom::checkUserPermissions('read', $this->gui->ref_id))
55  {
56  $ilCtrl->setParameterByClass('ilrepositorygui', 'ref_id', ROOT_FOLDER_ID);
57  $ilCtrl->redirectByClass('ilrepositorygui', '');
58  }
59 
60  $user_id = $chat_user->getUserId($ilUser);
61 
62  $ilNavigationHistory->addItem($_GET['ref_id'], $ilCtrl->getLinkTargetByClass('ilrepositorygui', 'view'), 'chtr');
63 
64  if($room->isUserBanned($user_id))
65  {
66  $this->cancelJoin($lng->txt('banned'));
67  return;
68  }
69 
70  $scope = $room->getRoomId();
71  $connector = $this->gui->getConnector();
72  $response = @$connector->connect($scope, $user_id);
73 
74  if(!$response)
75  {
76  ilUtil::sendFailure($lng->txt('unable_to_connect'), true);
77  $ilCtrl->redirectByClass('ilinfoscreengui', 'info');
78  }
79 
80  if(!$room->isSubscribed($chat_user->getUserId()) && $room->connectUser($chat_user))
81  {
82  $messageObject = array(
83  'type' => 'connected',
84  'users' => array(
85  array(
86  'login' => $chat_user->getUsername(),
87  'id' => $user_id,
88  ),
89  ),
90  'timestamp' => time() * 1000
91  );
92  $message = json_encode($messageObject);
93  $connector->sendMessage(
94  $scope,
95  $message
96  );
97 
98  $room->addHistoryEntry($messageObject);
99  }
100 
101  $connection_info = json_decode($response);
102  $settings = $connector->getSettings();
103  $known_private_room = $room->getActivePrivateRooms($ilUser->getId());
104 
105  $initial = new stdClass();
106  $initial->users = $room->getConnectedUsers();
107  $initial->private_rooms = array_values($known_private_room);
108  $initial->redirect_url = $ilCtrl->getLinkTarget($this->gui, 'view-lostConnection', '', false, false);
109  $initial->private_rooms_enabled = (boolean)$room->getSetting('private_rooms_enabled');
110 
111  $initial->userinfo = array(
112  'moderator' => $rbacsystem->checkAccess('moderate', (int)$_GET['ref_id']),
113  'userid' => $chat_user->getUserId()
114  );
115 
116  $smileys = array();
117 
118  include_once('Modules/Chatroom/classes/class.ilChatroomSmilies.php');
119 
120  if($settings->getSmiliesEnabled())
121  {
122  $smileys_array = ilChatroomSmilies::_getSmilies();
123  foreach($smileys_array as $smiley_array)
124  {
125  $new_keys = array();
126  $new_val = '';
127  foreach($smiley_array as $key => $value)
128  {
129  if($key == 'smiley_keywords')
130  {
131  $new_keys = explode("\n", $value);
132  }
133 
134  if($key == 'smiley_fullpath')
135  {
136  $new_val = $value;
137  }
138  }
139 
140  if(!$new_keys || !$new_val)
141  {
142  continue;
143  }
144 
145  foreach($new_keys as $new_key)
146  {
147  $smileys[$new_key] = $new_val;
148  }
149  }
150 
151  $initial->smileys = $smileys;
152  }
153  else
154  {
155  $initial->smileys = '{}';
156  }
157 
158  $initial->messages = array();
159 
160  if(isset($_REQUEST['sub']))
161  {
162  if($known_private_room[$_REQUEST['sub']])
163  {
164  if(!$room->isAllowedToEnterPrivateRoom($chat_user->getUserId(), $_REQUEST['sub']))
165  {
166  $initial->messages[] = array(
167  'type' => 'error',
168  'message' => $lng->txt('not_allowed_to_enter'),
169  );
170  }
171  else
172  {
173  $scope = $room->getRoomId();
174  $params = array();
175  $params['user'] = $chat_user->getUserId();
176  $params['sub'] = $_REQUEST['sub'];
177 
178  $params['message'] = json_encode(
179  array(
180  'type' => 'private_room_entered',
181  'user' => $user_id
182  )
183  );
184 
185  $query = http_build_query($params);
186  $connector = $this->gui->getConnector();
187  $response = $connector->enterPrivateRoom($scope, $query);
188 
189  $responseObject = json_decode($response);
190 
191  if($responseObject->success == true)
192  {
193  $room->subscribeUserToPrivateRoom($params['sub'], $params['user']);
194  }
195 
196  $message = json_encode(array(
197  'type' => 'private_room_entered',
198  'user' => $params['user'],
199  'sub' => $params['sub']
200  ));
201 
202  $connector->sendMessage($room->getRoomId(), $message, array('public' => 1, 'sub' => $params['sub']));
203 
204  $initial->enter_room = $_REQUEST['sub'];
205  $initial->messages[] = array(
206  'type' => 'notice',
207  'user' => $params['user'],
208  'sub' => $params['sub'],
209  'entersub' => 1
210  );
211  }
212 
213  if($_SESSION['show_invitation_message'])
214  {
215  $initial->messages[] = array(
216  'type' => 'notice',
217  'message' => $lng->txt('user_invited'),
218  'sub' => $_REQUEST['sub']
219  );
220  unset($_SESSION['show_invitation_message']);
221  }
222  }
223  else
224  {
225  $initial->messages[] = array(
226  'type' => 'error',
227  'message' => $lng->txt('user_invited'),
228  );
229  }
230  }
231 
232  if((int)$room->getSetting('display_past_msgs'))
233  {
234  $initial->messages = array_merge($initial->messages, array_reverse($room->getLastMessages($room->getSetting('display_past_msgs'), $chat_user)));
235  }
236 
237  $roomTpl = new ilTemplate('tpl.chatroom.html', true, true, 'Modules/Chatroom');
238  $roomTpl->setVariable('SESSION_ID', $connection_info->{'session-id'});
239  $roomTpl->setVariable('BASEURL', $settings->getBaseURL());
240  $roomTpl->setVariable('INSTANCE', $settings->getInstance());
241  $roomTpl->setVariable('SCOPE', $scope);
242  $roomTpl->setVariable('MY_ID', $user_id);
243  $roomTpl->setVariable('INITIAL_DATA', json_encode($initial));
244  $roomTpl->setVariable('POSTURL', $ilCtrl->getLinkTarget($this->gui, 'postMessage', '', true, true));
245 
246  $roomTpl->setVariable('ACTIONS', $lng->txt('actions'));
247  $roomTpl->setVariable('LBL_CREATE_PRIVATE_ROOM', $lng->txt('create_private_room_label'));
248  $roomTpl->setVariable('LBL_USER', $lng->txt('user'));
249  $roomTpl->setVariable('LBL_USER_TEXT', $lng->txt('invite_username'));
250  $roomTpl->setVariable('LBL_AUTO_SCROLL', $lng->txt('auto_scroll'));
251 
252  $roomTpl->setVariable('INITIAL_USERS', json_encode($room->getConnectedUsers()));
253 
254  $this->renderFontSettings($roomTpl, array());
255  $this->renderFileUploadForm($roomTpl);
256  $this->renderSendMessageBox($roomTpl);
257  $this->renderRightUsersDock($roomTpl);
258 
259  $tpl->setVariable('ADM_CONTENT', $roomTpl->get());
260  }
261 
265  protected function renderRightUsersDock(ilTemplate $roomTpl)
266  {
270  global $lng;
271 
272  $js_translations = array(
273  'LBL_MAINROOM' => 'chat_mainroom',
274  'LBL_LEAVE_PRIVATE_ROOM' => 'leave_private_room',
275  'LBL_JOIN' => 'chat_join',
276  'LBL_DELETE_PRIVATE_ROOM' => 'delete_private_room',
277  'LBL_INVITE_TO_PRIVATE_ROOM' => 'invite_to_private_room',
278  'LBL_KICK' => 'chat_kick',
279  'LBL_BAN' => 'chat_ban',
280  'LBL_KICK_QUESTION' => 'kick_question',
281  'LBL_BAN_QUESTION' => 'ban_question',
282  'LBL_ADDRESS' => 'chat_address',
283  'LBL_WHISPER' => 'chat_whisper',
284  'LBL_CONNECT' => 'chat_connection_established',
285  'LBL_DISCONNECT' => 'chat_connection_disconnected',
286  'LBL_TO_MAINROOM' => 'chat_to_mainroom',
287  'LBL_CREATE_PRIVATE_ROOM_JS' => 'chat_create_private_room_button',
288  'LBL_WELCOME_TO_CHAT' => 'welcome_to_chat',
289  'LBL_USER_INVITED' => 'user_invited',
290  'LBL_USER_KICKED' => 'user_kicked',
291  'LBL_USER_INVITED_SELF' => 'user_invited_self',
292  'LBL_PRIVATE_ROOM_CLOSED' => 'private_room_closed',
293  'LBL_PRIVATE_ROOM_ENTERED' => 'private_room_entered',
294  'LBL_PRIVATE_ROOM_LEFT' => 'private_room_left',
295  'LBL_PRIVATE_ROOM_ENTERED_USER' => 'private_room_entered_user',
296  'LBL_KICKED_FROM_PRIVATE_ROOM' => 'kicked_from_private_room',
297  'LBL_OK' => 'ok',
298  'LBL_CANCEL' => 'cancel',
299  'LBL_WHISPER_TO' => 'whisper_to',
300  'LBL_SPEAK_TO' => 'speak_to',
301  'LBL_HISTORY_CLEARED' => 'history_cleared',
302  'LBL_CLEAR_ROOM_HISTORY' => 'clear_room_history',
303  'LBL_CLEAR_ROOM_HISTORY_QUESTION' => 'clear_room_history_question',
304  'LBL_END_WHISPER' => 'end_whisper',
305  'LBL_SHOW_SETTINGS_JS' => 'show_settings',
306  'LBL_HIDE_SETTINGS' => 'hide_settings',
307  'LBL_TIMEFORMAT' => 'lang_timeformat_no_sec',
308  'LBL_DATEFORMAT' => 'lang_dateformat'
309  );
310  foreach($js_translations as $placeholder => $lng_variable)
311  {
312  $roomTpl->setVariable($placeholder, json_encode($lng->txt($lng_variable)));
313  }
314 
315  $roomTpl->setVariable('LBL_CREATE_PRIVATE_ROOM', $lng->txt('chat_create_private_room_button'));
316  $roomTpl->setVariable('LBL_CREATE_PRIVATE_ROOM_TEXT', $lng->txt('create_private_room_text'));
317  $roomTpl->setVariable('LBL_USERS', $lng->txt('users'));
318  $roomTpl->setVariable('LBL_LAYOUT', $lng->txt('layout'));
319  $roomTpl->setVariable('LBL_SHOW_SETTINGS', $lng->txt('show_settings'));
320  $roomTpl->setVariable('LBL_NO_FURTHER_USERS', $lng->txt('no_further_users'));
321  $roomTpl->setVariable('LBL_USER_IN_ROOM', $lng->txt('user_in_room'));
322  $roomTpl->setVariable('LBL_USER_IN_ILIAS', $lng->txt('user_in_ilias'));
323  }
324 
328  protected function renderSendMessageBox(ilTemplate $roomTpl)
329  {
333  global $lng;
334 
335  $roomTpl->setVariable('LBL_MESSAGE', $lng->txt('chat_message'));
336  $roomTpl->setVariable('LBL_TOALL', $lng->txt('chat_message_to_all'));
337  $roomTpl->setVariable('LBL_OPTIONS', $lng->txt('chat_message_options'));
338  $roomTpl->setVariable('LBL_DISPLAY', $lng->txt('chat_message_display'));
339  $roomTpl->setVariable('LBL_SEND', $lng->txt('send'));
340  }
341 
348  private function showNameSelection(ilChatroomUser $chat_user)
349  {
355  global $lng, $ilCtrl, $tpl;
356 
357  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
358 
359  $name_options = $chat_user->getChatNameSuggestions();
360  $formFactory = new ilChatroomFormFactory();
361  $selectionForm = $formFactory->getUserChatNameSelectionForm($name_options);
362 
363  $ilCtrl->saveParameter($this->gui, 'sub');
364 
365  $selectionForm->addCommandButton('view-joinWithCustomName', $lng->txt('enter'));
366  $selectionForm->setFormAction(
367  $ilCtrl->getFormAction($this->gui, 'view-joinWithCustomName')
368  );
369 
370  $tpl->setVariable('ADM_CONTENT', $selectionForm->getHtml());
371  }
372 
376  private function setupTemplate()
377  {
381  global $tpl;
382 
383  $tpl->addJavaScript('Modules/Chatroom/js/colorpicker/jquery.colorPicker.js');
384  $tpl->addJavaScript('Modules/Chatroom/js/chat.js');
385  $tpl->addJavaScript('Modules/Chatroom/js/iliaschat.jquery.js');
386  $tpl->addJavaScript('Services/jQuery/js/jquery.outside.events.min.js');
387  $tpl->addJavaScript('Modules/Chatroom/js/json2.js');
388 
389  $tpl->addJavaScript('./Services/UIComponent/AdvancedSelectionList/js/AdvancedSelectionList.js');
390 
391  $tpl->addCSS('Modules/Chatroom/js/colorpicker/colorPicker.css');
392  $tpl->addCSS('Modules/Chatroom/templates/default/style.css');
393  }
394 
401  public function joinWithCustomName()
402  {
407  global $ilUser, $lng;
408 
409  $this->gui->switchToVisibleMode();
410  $this->setupTemplate();
411  $room = ilChatroom::byObjectId($this->gui->object->getId());
412  $chat_user = new ilChatroomUser($ilUser, $room);
413  $failure = false;
414  $username = '';
415 
416  if($_REQUEST['custom_username_radio'] == 'custom_username')
417  {
418  $username = $_REQUEST['custom_username_text'];
419  }
420  elseif(method_exists($chat_user, 'build' . $_REQUEST['custom_username_radio']))
421  {
422  $username = $chat_user->{'build' . $_REQUEST['custom_username_radio']}();
423  }
424  else
425  {
426  $failure = true;
427  }
428 
429  if(!$failure && trim($username) != '')
430  {
431  $chat_user->setUsername($username);
432  $this->showRoom($room, $chat_user);
433  }
434  else
435  {
436  ilUtil::sendFailure($lng->txt('no_username_given'));
437  $this->showNameSelection($chat_user);
438  }
439  }
440 
447  public function executeDefault($method)
448  {
454  global $ilUser, $lng, $ilCtrl;
455 
456  include_once 'Modules/Chatroom/classes/class.ilChatroom.php';
457 
458  ilChatroom::checkUserPermissions('read', $this->gui->ref_id);
459 
460  $this->gui->switchToVisibleMode();
461  $this->setupTemplate();
462 
463  $chatSettings = new ilSetting('chatroom');
464  if(!$chatSettings->get('chat_enabled'))
465  {
466  $ilCtrl->redirect($this->gui, 'settings-general');
467  exit;
468  }
469 
470  $room = ilChatroom::byObjectId($this->gui->object->getId());
471 
472  if(!$room->getSetting('allow_anonymous') && $ilUser->isAnonymous())
473  {
474  $this->cancelJoin($lng->txt('anonymous_not_allowed'));
475  return;
476  }
477 
478  $chat_user = new ilChatroomUser($ilUser, $room);
479 
480  if($room->getSetting('allow_custom_usernames'))
481  {
482  if($room->isSubscribed($chat_user->getUserId()))
483  {
484  $chat_user->setUsername($chat_user->getUsername());
485  $this->showRoom($room, $chat_user);
486  }
487  else
488  {
489  $this->showNameSelection($chat_user);
490  }
491  }
492  else
493  {
494  $chat_user->setUsername($ilUser->getLogin());
495  $this->showRoom($room, $chat_user);
496  }
497  }
498 
502  public function invitePD()
503  {
508  global $ilUser, $ilCtrl;
509 
510  $chatSettings = new ilSetting('chatroom');
511  if(!$chatSettings->get('chat_enabled'))
512  {
513  $ilCtrl->redirect($this->gui, 'settings-general');
514  }
515 
516  $room = ilChatroom::byObjectId($this->gui->object->getId());
517  $chat_user = new ilChatroomUser($ilUser, $room);
518  $user_id = $_REQUEST['usr_id'];
519  $connector = $this->gui->getConnector();
520  $title = $room->getUniquePrivateRoomTitle($chat_user->getUsername());
521  $response = $connector->createPrivateRoom($room, $title, $chat_user);
522  $connector->inviteToPrivateRoom($room, $response->id, $ilUser, $user_id);
523  $room->sendInvitationNotification($this->gui, $chat_user, $user_id, $response->id);
524 
525  $_REQUEST['sub'] = $response->id;
526 
527  $_SESSION['show_invitation_message'] = $user_id;
528 
529  $ilCtrl->setParameter($this->gui, 'sub', $response->id);
530  $ilCtrl->redirect($this->gui, 'view');
531  }
532 
539  private function renderFontSettings(ilTemplate $roomTpl, array $defaultSettings)
540  {
545  global $lng, $ilCtrl;
546 
547  $font_family = array(
548  'sans' => 'Sans Serif',
549  'times' => 'Times',
550  'monospace' => 'Monospace',
551  );
552 
553  $font_style = array(
554  'italic' => $lng->txt('italic'),
555  'bold' => $lng->txt('bold'),
556  'normal' => $lng->txt('normal'),
557  'underlined' => $lng->txt('underlined'),
558  );
559 
560  $font_size = array(
561  'small' => $lng->txt('small'),
562  'normal' => $lng->txt('normal'),
563  'large' => $lng->txt('large')
564  );
565 
566  $default_font_color = '#000000';
567 
568  $default_font_family = (
569  isset($defaultSettings['font_family']) &&
570  isset($font_family[$defaultSettings['font_family']]) ?
571  $defaultSettings['font_family'] : 'sans'
572  );
573 
574  $default_font_style = (
575  isset($defaultSettings['font_style']) &&
576  isset($font_family[$defaultSettings['font_style']]) ?
577  $defaultSettings['font_style'] : 'normal'
578  );
579 
580  $default_font_size = (
581  isset($defaultSettings['font_size']) &&
582  isset($font_family[$defaultSettings['font_size']]) ?
583  $defaultSettings['font_size'] : 'normal'
584  );
585 
586  $roomTpl->setVariable('VAL_FONTCOLOR', $default_font_color);
587 
588  foreach($font_family as $font => $label)
589  {
590  $roomTpl->setCurrentBlock('chat_fontfamily');
591  $roomTpl->setVariable('VAL_FONTFAMILY', $font);
592  $roomTpl->setVariable('LBL_FONTFAMILY', $label);
593  $roomTpl->setVariable(
594  'SELECTED_FONTFAMILY', $font == $default_font_family ?
595  'selected="selected"' : ''
596  );
597  $roomTpl->parseCurrentBlock();
598  }
599 
600  foreach($font_style as $font => $label)
601  {
602  $roomTpl->setCurrentBlock('chat_fontstyle');
603  $roomTpl->setVariable('VAL_FONTSTYLE', $font);
604  $roomTpl->setVariable('LBL_FONTSTYLE', $label);
605  $roomTpl->setVariable(
606  'SELECTED_FONTSTYLE', $font == $default_font_style ?
607  'selected="selected"' : ''
608  );
609  $roomTpl->parseCurrentBlock();
610  }
611 
612  foreach($font_size as $font => $label)
613  {
614  $roomTpl->setCurrentBlock('chat_fontsize');
615  $roomTpl->setVariable('VAL_FONTSIZE', $font);
616  $roomTpl->setVariable('LBL_FONTSIZE', $label);
617  $roomTpl->setVariable(
618  'SELECTED_FONTSIZE',
619  $font == $default_font_size ? 'selected="selected"' : ''
620  );
621  $roomTpl->parseCurrentBlock();
622  }
623 
624  $roomTpl->setVariable('LBL_FONTCOLOR', $lng->txt('fontcolor'));
625  $roomTpl->setVariable('LBL_FONTFAMILY', $lng->txt('fontfamily'));
626  $roomTpl->setVariable('LBL_FONTSTYLE', $lng->txt('fontstyle'));
627  $roomTpl->setVariable('LBL_FONTSIZE', $lng->txt('fontsize'));
628 
629  $logoutLink = $ilCtrl->getLinkTarget($this->gui, 'view-logout');
630  $roomTpl->setVariable('LOGOUT_LINK', $logoutLink);
631  }
632 
637  public function renderFileUploadForm(ilTemplate $roomTpl)
638  {
639  // @todo: Not implemented yet
640  return;
641 
642  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
643  $formFactory = new ilChatroomFormFactory();
644  $file_upload = $formFactory->getFileUploadForm();
645  //$file_upload->setFormAction( $ilCtrl->getFormAction($this->gui, 'UploadFile-uploadFile') );
646  $roomTpl->setVariable('FILE_UPLOAD', $file_upload->getHTML());
647  }
648 
652  public function logout()
653  {
658  global $tree, $ilCtrl;
659 
663  $pid = $tree->getParentId($this->gui->getRefId());
664  $ilCtrl->setParameterByClass('ilrepositorygui', 'ref_id', $pid);
665  $ilCtrl->redirectByClass('ilrepositorygui', '');
666  }
667 
671  public function lostConnection()
672  {
677  global $lng, $ilCtrl;
678 
679  ilUtil::sendFailure($lng->txt('lost_connection'), true);
680  $ilCtrl->redirectByClass('ilinfoscreengui', 'info');
681  }
682 }