ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilChatroomHistoryGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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  $date_string = match ($time_format) {
55  (string) ilCalendarSettings::TIME_FORMAT_24 => $message_date_time->get(
57  'H:i',
58  $this->ilUser->getTimeZone()
59  ),
60  default => $message_date_time->get(IL_CAL_FKT_DATE, 'g:ia', $this->ilUser->getTimeZone()),
61  };
62 
63  $render_parts['MESSAGETIME'] = $date_string;
64  $prev_date_time_presentation = $message_date_time_presentation;
65  }
66 
67  if ($render_parts !== []) {
68  $room_tpl->setCurrentBlock('datetime_line');
69  foreach ($render_parts as $key => $value) {
70  $room_tpl->setVariable($key, $value);
71  }
72  $room_tpl->parseCurrentBlock();
73  }
74  }
75 
76  public function byDayExport(): void
77  {
78  $this->tabs->activateSubTab('byday');
79  $this->byDay(true);
80  }
81 
82  public function byDay(bool $export = false): void
83  {
84  $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
85  $this->exitIfNoRoomExists($room);
86 
87  $chat_user = new ilChatroomUser($this->ilUser, $room);
88  $formFactory = new ilChatroomFormFactory();
89 
90  $durationForm = $formFactory->getPeriodForm();
91  $durationForm->setTitle($this->ilLng->txt('history_byday_title'));
92  $durationForm->addCommandButton('history-byDayExport', $this->ilLng->txt('export'));
93  $durationForm->addCommandButton('history-byDay', $this->ilLng->txt('show'));
94  $durationForm->setFormAction($this->ilCtrl->getFormAction($this->gui, 'history-byDay'));
95 
96  $messages = [];
97  $submit_request = strtolower($this->http->request()->getServerParams()['REQUEST_METHOD']) === 'post';
98  $from = null;
99  $to = null;
100 
101  if ($submit_request) {
102  if ($durationForm->checkInput()) {
103  $period = $durationForm->getItemByPostVar('timeperiod');
104 
105  $messages = $room->getHistory(
106  $from = $period->getStart(),
107  $to = $period->getEnd(),
108  $chat_user->getUserId()
109  );
110  } else {
111  $export = false;
112  }
113 
114  $durationForm->setValuesByPost();
115  }
116 
117  $this->showMessages($messages, $durationForm, $export, $from, $to);
118  }
119 
120  private function showMessages(
121  array $messages,
122  ilPropertyFormGUI $durationForm,
123  bool $export = false,
124  ?ilDateTime $from = null,
125  ?ilDateTime $to = null
126  ): void {
127  $this->redirectIfNoPermission('read');
128 
129  $this->gui->switchToVisibleMode();
130 
131  $this->mainTpl->addCss('Modules/Chatroom/templates/default/style.css');
132 
133  // should be able to grep templates
134  if ($export) {
135  $roomTpl = new ilGlobalTemplate('tpl.history_export.html', true, true, 'Modules/Chatroom');
136  } else {
137  $roomTpl = new ilTemplate('tpl.history.html', true, true, 'Modules/Chatroom');
138  }
139 
140  if ($export) {
142  }
143 
144  $time_format = $this->ilUser->getTimeFormat();
145 
146  $num_messages_shown = 0;
147  $prev_date_time_presentation = null;
148  $prev_date_time = null;
149  foreach ($messages as $message) {
150  switch ($message['message']->type) {
151  case 'message':
152  $message_date = new ilDate($message['timestamp'], IL_CAL_UNIX);
153  $message_date_time = new ilDateTime($message['timestamp'], IL_CAL_UNIX);
154  $message_date_time_presentation = ilDatePresentation::formatDate($message_date_time);
155 
157  $roomTpl,
158  $prev_date_time,
159  $message_date_time,
160  $message_date,
161  $prev_date_time_presentation,
162  $message_date_time_presentation,
163  $time_format
164  );
165 
166  $roomTpl->setCurrentBlock('message_line');
167  $roomTpl->setVariable('MESSAGECONTENT', $message['message']->content); // oops... it is a message? ^^
168  $roomTpl->setVariable('MESSAGESENDER', $message['message']->from->username);
169  $roomTpl->parseCurrentBlock();
170 
171  $roomTpl->setCurrentBlock('row');
172  $roomTpl->parseCurrentBlock();
173 
174  ++$num_messages_shown;
175  break;
176  }
177  }
178 
179  if (!$num_messages_shown) {
180  $roomTpl->setVariable('LBL_NO_MESSAGES', $this->ilLng->txt('no_messages'));
181  }
182 
183  $scope = $this->ilLng->txt('main');
184 
185  $prevUseRelDates = ilDatePresentation::useRelativeDates();
187 
188  if ($from instanceof ilDateTime && $to instanceof ilDateTime) {
189  $unixFrom = $from->getUnixTime();
190  $unixTo = $to->getUnixTime();
191 
192  if ($unixFrom === $unixTo) {
193  $message_date = new ilDate($unixFrom, IL_CAL_UNIX);
194  $date_sub = ilDatePresentation::formatDate($message_date);
195  } else {
196  $date1 = new ilDate($unixFrom, IL_CAL_UNIX);
197  $date2 = new ilDate($unixTo, IL_CAL_UNIX);
198  $date_sub = ilDatePresentation::formatPeriod($date1, $date2);
199  }
200  ilDatePresentation::setUseRelativeDates($prevUseRelDates);
201 
202  $roomTpl->setVariable(
203  'ROOM_TITLE',
204  sprintf($this->ilLng->txt('history_title_general'), $this->gui->getObject()->getTitle()) . ' (' . $date_sub . ')'
205  );
206  }
207 
208  if ($export) {
210  $roomTpl->get(),
212  'text/html'
213  );
214  }
215 
216  $roomTpl->setVariable('PERIOD_FORM', $durationForm->getHTML());
217 
218  $this->mainTpl->setVariable('ADM_CONTENT', $roomTpl->get());
219  }
220 
221  public function bySessionExport(): void
222  {
223  $this->tabs->activateSubTab('bysession');
224  $this->bySession(true);
225  }
226 
227  public function bySession(bool $export = false): void
228  {
229  $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
230  $this->exitIfNoRoomExists($room);
231 
232  $scope = $room->getRoomId();
233 
234  $chat_user = new ilChatroomUser($this->ilUser, $room);
235 
236  $formFactory = new ilChatroomFormFactory();
237  $durationForm = $formFactory->getSessionForm($room->getSessions($chat_user));
238  $durationForm->setTitle($this->ilLng->txt('history_bysession_title'));
239  $durationForm->addCommandButton('history-bySessionExport', $this->ilLng->txt('export'));
240  $durationForm->addCommandButton('history-bySession', $this->ilLng->txt('show'));
241  $durationForm->setFormAction(
242  $this->ilCtrl->getFormAction($this->gui, 'history-bySession')
243  );
244 
245  if (strtolower($this->http->request()->getServerParams()['REQUEST_METHOD']) === 'post') {
246  $session = $this->getRequestValue('session', $this->refinery->kindlyTo()->string());
247  $durationForm->checkInput();
248  $postVals = explode(',', (string) $session);
249  $durationForm->setValuesByArray([
250  'session' => $session
251  ]);
252 
253  $messages = $room->getHistory(
254  $from = new ilDateTime($postVals[0], IL_CAL_UNIX),
255  $to = new ilDateTime($postVals[1], IL_CAL_UNIX),
256  $chat_user->getUserId()
257  );
258  } else {
259  $last_session = $room->getLastSession($chat_user);
260 
261  if ($last_session) {
262  $from = new ilDateTime($last_session['connected'], IL_CAL_UNIX);
263  $to = new ilDateTime($last_session['disconnected'], IL_CAL_UNIX);
264  } else {
265  $from = null;
266  $to = null;
267  }
268 
269  $messages = $room->getHistory(
270  $from,
271  $to,
272  $chat_user->getUserId()
273  );
274  }
275 
276  $from = new ilDateTime();
277  $to = new ilDateTime();
278 
279  $this->showMessages($messages, $durationForm, $export, $from, $to);
280  }
281 
282  public function executeDefault(string $requestedMethod): void
283  {
284  $this->byDay();
285  }
286 }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
$scope
Definition: ltiregstart.php:53
special template class to simplify handling of ITX/PEAR
redirectIfNoPermission($permission)
Checks for requested permissions and redirects if the permission check failed.
getRequestValue(string $key, Transformation $trafo, $default=null)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
exitIfNoRoomExists(?ilChatroom $room)
Checks if a ilChatroom exists.
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
const IL_CAL_UNIX
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)
static getASCIIFilename(string $a_filename)
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: xapiexit.php:22
static http()
Fetches the global http state from ILIAS.
Class ilChatroomGUIHandler.
showMessages(array $messages, ilPropertyFormGUI $durationForm, bool $export=false, ?ilDateTime $from=null, ?ilDateTime $to=null)
const IL_CAL_FKT_DATE
string $key
Consumer key/client ID value.
Definition: System.php:193
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
Class ilChatroomUser.
static byObjectId(int $object_id)
executeDefault(string $requestedMethod)
Class ilChatroom Keeps methods to prepare and display the history task.
$message
Definition: xapiexit.php:32
static setUseRelativeDates(bool $a_status)
set use relative dates