ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilChatroomHistoryGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
26 private function renderDateTimeInformation(
27 $room_tpl,
28 ?ilDateTime &$prev_date_time,
29 ilDateTime $message_date_time,
30 ilDate $message_date,
31 ?string &$prev_date_time_presentation,
32 string $message_date_time_presentation,
33 string $time_format
34 ): void {
35 $render_parts = [];
36
37 if (null === $prev_date_time ||
38 date('d', $prev_date_time->get(IL_CAL_UNIX)) !== date('d', $message_date_time->get(IL_CAL_UNIX)) ||
39 date('m', $prev_date_time->get(IL_CAL_UNIX)) !== date('m', $message_date_time->get(IL_CAL_UNIX)) ||
40 date('Y', $prev_date_time->get(IL_CAL_UNIX)) !== date('Y', $message_date_time->get(IL_CAL_UNIX))
41 ) {
42 $render_parts['MESSAGEDATE'] = ilDatePresentation::formatDate($message_date);
43 $prev_date_time = $message_date_time;
44 }
45
46 if ($prev_date_time_presentation !== $message_date_time_presentation) {
47 $date_string = match ($time_format) {
48 (string) ilCalendarSettings::TIME_FORMAT_24 => $message_date_time->get(
50 'H:i',
51 $this->ilUser->getTimeZone()
52 ),
53 default => $message_date_time->get(IL_CAL_FKT_DATE, 'g:ia', $this->ilUser->getTimeZone()),
54 };
55
56 $render_parts['MESSAGETIME'] = $date_string;
57 $prev_date_time_presentation = $message_date_time_presentation;
58 }
59
60 if ($render_parts !== []) {
61 $room_tpl->setCurrentBlock('datetime_line');
62 foreach ($render_parts as $key => $value) {
63 $room_tpl->setVariable($key, $value);
64 }
65 $room_tpl->parseCurrentBlock();
66 }
67 }
68
69 public function byDayExport(): void
70 {
71 $this->tabs->activateSubTab('byday');
72 $this->byDay(true);
73 }
74
75 public function byDay(bool $export = false): void
76 {
77 $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
78 $this->exitIfNoRoomExists($room);
79
80 $chat_user = new ilChatroomUser($this->ilUser, $room);
81 $formFactory = new ilChatroomFormFactory();
82
83 $durationForm = $formFactory->getPeriodForm();
84 $durationForm->setTitle($this->ilLng->txt('history_byday_title'));
85 $durationForm->addCommandButton('history-byDayExport', $this->ilLng->txt('export'));
86 $durationForm->addCommandButton('history-byDay', $this->ilLng->txt('show'));
87 $durationForm->setFormAction($this->ilCtrl->getFormAction($this->gui, 'history-byDay'));
88
89 $messages = [];
90 $submit_request = strtolower($this->http->request()->getServerParams()['REQUEST_METHOD']) === 'post';
91 $from = null;
92 $to = null;
93
94 if ($submit_request) {
95 if ($durationForm->checkInput()) {
97 $period = $durationForm->getItemByPostVar('timeperiod');
98
99 $messages = $room->getHistory(
100 $from = $period->getStart(),
101 $to = $period->getEnd(),
102 $chat_user->getUserId()
103 );
104 } else {
105 $export = false;
106 }
107
108 $durationForm->setValuesByPost();
109 }
110
111 $this->showMessages($messages, $durationForm, $export, $from, $to, $room);
112 }
113
114 private function showMessages(
115 array $messages,
116 ilPropertyFormGUI $durationForm,
117 bool $export = false,
118 ?ilDateTime $from = null,
119 ?ilDateTime $to = null,
120 $room = null
121 ): void {
122 $this->redirectIfNoPermission('read');
123
124 $this->gui->switchToVisibleMode();
125
126 // should be able to grep templates
127 if ($export) {
128 $roomTpl = new ilGlobalTemplate('tpl.history_export.html', true, true, 'components/ILIAS/Chatroom');
129 } else {
130 $roomTpl = new ilTemplate('tpl.history.html', true, true, 'components/ILIAS/Chatroom');
131 }
132
133 if ($export) {
135 }
136
137 $time_format = $this->ilUser->getTimeFormat();
138
139 $num_messages_shown = 0;
140 $prev_date_time_presentation = null;
141 $prev_date_time = null;
142 if ($export) {
143 foreach ($messages as $message) {
144 switch ($message['message']->type) {
145 case 'message':
146 $message_date = new ilDate($message['timestamp'], IL_CAL_UNIX);
147 $message_date_time = new ilDateTime($message['timestamp'], IL_CAL_UNIX);
148 $message_date_time_presentation = ilDatePresentation::formatDate($message_date_time);
149
150 $this->renderDateTimeInformation(
151 $roomTpl,
152 $prev_date_time,
153 $message_date_time,
154 $message_date,
155 $prev_date_time_presentation,
156 $message_date_time_presentation,
157 $time_format
158 );
159
160 $roomTpl->setCurrentBlock('message_line');
161 $roomTpl->setVariable('MESSAGECONTENT', htmlspecialchars($message['message']->content, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8')); // oops... it is a message? ^^
162 $roomTpl->setVariable('MESSAGESENDER', htmlspecialchars($message['message']->from->username, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'));
163 $roomTpl->parseCurrentBlock();
164
165 $roomTpl->setCurrentBlock('row');
166 $roomTpl->parseCurrentBlock();
167
168 ++$num_messages_shown;
169 break;
170 }
171 }
172 }
173
174 if (!$num_messages_shown) {
175 $roomTpl->setVariable('LBL_NO_MESSAGES', $this->ilLng->txt('no_messages'));
176 }
177
178 $scope = $this->ilLng->txt('main');
179
180 $prevUseRelDates = ilDatePresentation::useRelativeDates();
182
183 if ($from instanceof ilDateTime && $to instanceof ilDateTime) {
184 $unixFrom = $from->getUnixTime();
185 $unixTo = $to->getUnixTime();
186
187 if ($unixFrom === $unixTo) {
188 $message_date = new ilDate($unixFrom, IL_CAL_UNIX);
189 $date_sub = ilDatePresentation::formatDate($message_date);
190 } else {
191 $date1 = new ilDate($unixFrom, IL_CAL_UNIX);
192 $date2 = new ilDate($unixTo, IL_CAL_UNIX);
193 $date_sub = ilDatePresentation::formatPeriod($date1, $date2);
194 }
196
197 $roomTpl->setVariable(
198 'ROOM_TITLE',
199 sprintf($this->ilLng->txt('history_title_general'), $this->gui->getObject()->getTitle()) . ' (' . $date_sub . ')'
200 );
201 }
202
203 if ($export) {
205 $roomTpl->get(),
207 'text/html'
208 );
209 }
210
211 $roomTpl->setVariable('PERIOD_FORM', $durationForm->getHTML());
212
213 if ($room && $messages !== []) {
214 ilLinkifyUtil::initLinkify($this->mainTpl);
215 $this->mainTpl->addJavaScript('assets/js/socket.io.min.js');
216 $this->mainTpl->addJavaScript('assets/js/Chatroom.min.js');
217 $roomTpl->setVariable('CHAT', (new ilChatroomViewGUI($this->gui))->readOnlyChatWindow($room, array_column($messages, 'message'))->get());
218 } else {
219 $roomTpl->setVariable('CHAT', '');
220 }
221 $this->mainTpl->setVariable('ADM_CONTENT', $roomTpl->get());
222 }
223
224 public function executeDefault(string $requestedMethod): void
225 {
226 $this->byDay();
227 }
228}
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
Class ilChatroomGUIHandler.
showMessages(array $messages, ilPropertyFormGUI $durationForm, bool $export=false, ?ilDateTime $from=null, ?ilDateTime $to=null, $room=null)
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)
executeDefault(string $requestedMethod)
Class ilChatroomUser.
Class ilChatroomViewGUI.
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 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, ?ilObjUser $user=null,)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
@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)
special template class to simplify handling of ITX/PEAR
static initLinkify(?ilGlobalTemplateInterface $a_tpl=null)
This class represents a property form user interface.
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:51
static http()
Fetches the global http state from ILIAS.
$messages
Definition: xapiexit.php:21
$message
Definition: xapiexit.php:31