ILIAS  release_8 Revision v8.24
class.ilChatroomHistoryGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
29{
33 private function renderDateTimeInformation(
34 $room_tpl,
35 ?ilDateTime &$prev_date_time,
36 ilDateTime $message_date_time,
37 ilDate $message_date,
38 ?string &$prev_date_time_presentation,
39 string $message_date_time_presentation,
40 string $time_format
41 ): void {
42 $render_parts = [];
43
44 if (null === $prev_date_time ||
45 date('d', $prev_date_time->get(IL_CAL_UNIX)) !== date('d', $message_date_time->get(IL_CAL_UNIX)) ||
46 date('m', $prev_date_time->get(IL_CAL_UNIX)) !== date('m', $message_date_time->get(IL_CAL_UNIX)) ||
47 date('Y', $prev_date_time->get(IL_CAL_UNIX)) !== date('Y', $message_date_time->get(IL_CAL_UNIX))
48 ) {
49 $render_parts['MESSAGEDATE'] = ilDatePresentation::formatDate($message_date);
50 $prev_date_time = $message_date_time;
51 }
52
53 if ($prev_date_time_presentation !== $message_date_time_presentation) {
54 switch ($time_format) {
56 $date_string = $message_date_time->get(IL_CAL_FKT_DATE, 'H:i', $this->ilUser->getTimeZone());
57 break;
59 default:
60 $date_string = $message_date_time->get(IL_CAL_FKT_DATE, 'g:ia', $this->ilUser->getTimeZone());
61 break;
62 }
63
64 $render_parts['MESSAGETIME'] = $date_string;
65 $prev_date_time_presentation = $message_date_time_presentation;
66 }
67
68 if ($render_parts !== []) {
69 $room_tpl->setCurrentBlock('datetime_line');
70 foreach ($render_parts as $key => $value) {
71 $room_tpl->setVariable($key, $value);
72 }
73 $room_tpl->parseCurrentBlock();
74 }
75 }
76
77 public function byDayExport(): void
78 {
79 $this->tabs->activateSubTab('byday');
80 $this->byDay(true);
81 }
82
83 public function byDay(bool $export = false): void
84 {
85 $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
86 $this->exitIfNoRoomExists($room);
87
88 $scope = $room->getRoomId();
89
90 $chat_user = new ilChatroomUser($this->ilUser, $room);
91 $formFactory = new ilChatroomFormFactory();
92
93 $durationForm = $formFactory->getPeriodForm();
94 $durationForm->setTitle($this->ilLng->txt('history_byday_title'));
95 $durationForm->addCommandButton('history-byDayExport', $this->ilLng->txt('export'));
96 $durationForm->addCommandButton('history-byDay', $this->ilLng->txt('show'));
97 $durationForm->setFormAction($this->ilCtrl->getFormAction($this->gui, 'history-byDay'));
98
99 $messages = [];
100 $psessions = [];
101 $submit_request = strtolower($this->http->request()->getServerParams()['REQUEST_METHOD']) === 'post';
102 $from = null;
103 $to = null;
104
105 if ($submit_request) {
106 if ($durationForm->checkInput()) {
107 $period = $durationForm->getItemByPostVar('timeperiod');
108
109 $messages = $room->getHistory(
110 $from = $period->getStart(),
111 $to = $period->getEnd(),
112 $chat_user->getUserId(),
113 $this->getRequestValue('scope', $this->refinery->kindlyTo()->int(), 0)
114 );
115
116 $psessions = $room->getPrivateRoomSessions(
117 $from,
118 $to,
119 $chat_user->getUserId(),
120 $scope
121 );
122 } else {
123 $export = false;
124 }
125
126 $durationForm->setValuesByPost();
127 }
128
129 $this->showMessages($messages, $durationForm, $export, $psessions, $from, $to);
130 }
131
132 private function showMessages(
133 array $messages,
134 ilPropertyFormGUI $durationForm,
135 bool $export = false,
136 array $psessions = [],
137 ?ilDateTime $from = null,
138 ?ilDateTime $to = null
139 ): void {
140 $this->redirectIfNoPermission('read');
141
142 $this->gui->switchToVisibleMode();
143
144 $this->mainTpl->addCss('Modules/Chatroom/templates/default/style.css');
145
146 // should be able to grep templates
147 if ($export) {
148 $roomTpl = new ilGlobalTemplate('tpl.history_export.html', true, true, 'Modules/Chatroom');
149 } else {
150 $roomTpl = new ilTemplate('tpl.history.html', true, true, 'Modules/Chatroom');
151 }
152
153 $scopes = [];
154 $isScopeRequest = $this->hasRequestValue('scope');
155 $requestScope = $this->getRequestValue('scope', $this->refinery->kindlyTo()->int(), 0);
156
157 if ($export) {
159 }
160
161 $time_format = $this->ilUser->getTimeFormat();
162
163 $messagesShown = 0;
164 $prev_date_time_presentation = null;
165 $prev_date_time = null;
166 foreach ($messages as $message) {
167 switch ($message['message']->type) {
168 case 'message':
169 if (
170 !$message['message']->subRoomId ||
171 (
172 $isScopeRequest &&
173 (int) $message['message']->subRoomId === $requestScope
174 )
175 ) {
176 $date = new ilDate($message['timestamp'], IL_CAL_UNIX);
177 $dateTime = new ilDateTime($message['timestamp'], IL_CAL_UNIX);
178 $currentDate = ilDatePresentation::formatDate($dateTime);
179
180 $this->renderDateTimeInformation(
181 $roomTpl,
182 $prev_date_time,
183 $dateTime,
184 $date,
185 $prev_date_time_presentation,
186 $currentDate,
187 $time_format
188 );
189
190 $roomTpl->setCurrentBlock('message_line');
191 $roomTpl->setVariable('MESSAGECONTENT', $message['message']->content); // oops... it is a message? ^^
192 $roomTpl->setVariable('MESSAGESENDER', $message['message']->from->username);
193 $roomTpl->parseCurrentBlock();
194
195 $roomTpl->setCurrentBlock('row');
196 $roomTpl->parseCurrentBlock();
197
198 ++$messagesShown;
199 }
200 break;
201 }
202 }
203
204 foreach ($psessions as $session) {
205 $scopes[$session['proom_id']] = $session['title'];
206 }
207
208 if (isset($scopes[''])) {
209 unset($scopes['']);
210 }
211
212 if (!$messagesShown) {
213 $roomTpl->setVariable('LBL_NO_MESSAGES', $this->ilLng->txt('no_messages'));
214 }
215
216 asort($scopes, SORT_STRING);
217
218 $scopes = [$this->ilLng->txt('main')] + $scopes;
219
220 if (count($scopes) > 1) {
221 $select = new ilSelectInputGUI($this->ilLng->txt('scope'), 'scope');
222 $select->setOptions($scopes);
223
224 if ($isScopeRequest) {
225 $select->setValue($requestScope);
226 }
227
228 $durationForm->addItem($select);
229 }
230
231 $prevUseRelDates = ilDatePresentation::useRelativeDates();
233
234 if ($from instanceof ilDateTime && $to instanceof ilDateTime) {
235 $unixFrom = $from->getUnixTime();
236 $unixTo = $to->getUnixTime();
237
238 if ($unixFrom === $unixTo) {
239 $date = new ilDate($unixFrom, IL_CAL_UNIX);
240 $date_sub = ilDatePresentation::formatDate($date);
241 } else {
242 $date1 = new ilDate($unixFrom, IL_CAL_UNIX);
243 $date2 = new ilDate($unixTo, IL_CAL_UNIX);
244 $date_sub = ilDatePresentation::formatPeriod($date1, $date2);
245 }
247
248 $isPrivateRoom = $isScopeRequest;
249 if ($isPrivateRoom) {
250 $roomTpl->setVariable(
251 'ROOM_TITLE',
252 sprintf(
253 $this->ilLng->txt('history_title_private_room'),
254 $scopes[$requestScope]
255 ) . ' (' . $date_sub . ')'
256 );
257 } else {
258 $roomTpl->setVariable(
259 'ROOM_TITLE',
260 sprintf($this->ilLng->txt('history_title_general'), $this->gui->getObject()->getTitle()) . ' (' . $date_sub . ')'
261 );
262 }
263 }
264
265 if ($export) {
267 $roomTpl->get(),
268 ilFileUtils::getASCIIFilename($scopes[$requestScope] . '.html'),
269 'text/html'
270 );
271 }
272
273 $roomTpl->setVariable('PERIOD_FORM', $durationForm->getHTML());
274
275 $this->mainTpl->setVariable('ADM_CONTENT', $roomTpl->get());
276 }
277
278 public function bySessionExport(): void
279 {
280 $this->tabs->activateSubTab('bysession');
281 $this->bySession(true);
282 }
283
284 public function bySession(bool $export = false): void
285 {
286 $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
287 $this->exitIfNoRoomExists($room);
288
289 $scope = $room->getRoomId();
290
291 $chat_user = new ilChatroomUser($this->ilUser, $room);
292
293 $formFactory = new ilChatroomFormFactory();
294 $durationForm = $formFactory->getSessionForm($room->getSessions($chat_user));
295 $durationForm->setTitle($this->ilLng->txt('history_bysession_title'));
296 $durationForm->addCommandButton('history-bySessionExport', $this->ilLng->txt('export'));
297 $durationForm->addCommandButton('history-bySession', $this->ilLng->txt('show'));
298 $durationForm->setFormAction(
299 $this->ilCtrl->getFormAction($this->gui, 'history-bySession')
300 );
301
302 if (strtolower($this->http->request()->getServerParams()['REQUEST_METHOD']) === 'post') {
303 $session = $this->getRequestValue('session', $this->refinery->kindlyTo()->string());
304 $durationForm->checkInput();
305 $postVals = explode(',', $session);
306 $durationForm->setValuesByArray([
307 'session' => $session
308 ]);
309
310 $messages = $room->getHistory(
311 $from = new ilDateTime($postVals[0], IL_CAL_UNIX),
312 $to = new ilDateTime($postVals[1], IL_CAL_UNIX),
313 $chat_user->getUserId(),
314 $this->getRequestValue('scope', $this->refinery->kindlyTo()->int(), 0)
315 );
316 } else {
317 $last_session = $room->getLastSession($chat_user);
318
319 if ($last_session) {
320 $from = new ilDateTime($last_session['connected'], IL_CAL_UNIX);
321 $to = new ilDateTime($last_session['disconnected'], IL_CAL_UNIX);
322 } else {
323 $from = null;
324 $to = null;
325 }
326
327 $messages = $room->getHistory(
328 $from,
329 $to,
330 $chat_user->getUserId(),
331 $this->getRequestValue('scope', $this->refinery->kindlyTo()->int(), 0)
332 );
333 }
334
335 $from = new ilDateTime();
336 $to = new ilDateTime();
337 $psessions = [];
338 if ($from && $to) {
339 $psessions = $room->getPrivateRoomSessions(
340 $from,
341 $to,
342 $chat_user->getUserId(),
343 $scope
344 );
345 }
346
347 $this->showMessages($messages, $durationForm, $export, $psessions, $from, $to);
348 }
349
350 public function executeDefault(string $requestedMethod): void
351 {
352 $this->byDay();
353 }
354}
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
Class ilChatroomFormFactory.
Class ilChatroomGUIHandler.
Class ilChatroom Keeps methods to prepare and display the history task.
renderDateTimeInformation( $room_tpl, ?ilDateTime &$prev_date_time, ilDateTime $message_date_time, ilDate $message_date, ?string &$prev_date_time_presentation, string $message_date_time_presentation, string $time_format)
showMessages(array $messages, ilPropertyFormGUI $durationForm, bool $export=false, array $psessions=[], ?ilDateTime $from=null, ?ilDateTime $to=null)
executeDefault(string $requestedMethod)
Class ilChatroomUser.
static byObjectId(int $object_id)
Class ilCtrl provides processing control methods.
getFormAction(object $a_gui_obj, string $a_fallback_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
Class for single dates.
static getASCIIFilename(string $a_filename)
setFormAction(string $a_formaction)
special template class to simplify handling of ITX/PEAR
This class represents a property form user interface.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
This class represents a selection list property in a property form.
special template class to simplify handling of ITX/PEAR
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
$scope
Definition: ltiregstart.php:53
$scopes
Definition: ltitoken.php:99
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
$session
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: xapiexit.php:22
$message
Definition: xapiexit.php:32