ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilEmployeeTalkAppointmentGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
37 {
38  public const string EDIT_MODE_APPOINTMENT = 'appointment';
39  public const string EDIT_MODE_SERIES = 'series';
40  public const string EDIT_MODE = 'edit-mode';
41 
44  private ilCtrl $ctrl;
47  private ilTabsGUI $tabs;
50 
51  public function __construct(
52  ilGlobalTemplateInterface $template,
53  ilLanguage $language,
54  ilCtrl $ctrl,
55  HttpServices $http,
56  Refinery $refinery,
57  ilTabsGUI $tabs,
58  NotificationHandlerInterface $notif_handler,
59  ilObjEmployeeTalk $talk
60  ) {
61  $this->template = $template;
62  $this->language = $language;
63  $this->ctrl = $ctrl;
64  $this->http = $http;
65  $this->refinery = $refinery;
66  $this->tabs = $tabs;
67  $this->notif_handler = $notif_handler;
68  $this->talk = $talk;
69 
70  $this->language->loadLanguageModule('cal');
71  }
72 
73  public function executeCommand(): void
74  {
75  $cmd = $this->ctrl->getCmd(ControlFlowCommand::DEFAULT);
76  if ($this->http->wrapper()->query()->has('ref_id')) {
77  $ref_id = $this->http->wrapper()->query()->retrieve(
78  'ref_id',
79  $this->refinery->kindlyTo()->int()
80  );
81  } else {
83  'No ref_id found'
84  );
85  }
86 
87  $backClass = strtolower(ilObjEmployeeTalkGUI::class);
88  $this->ctrl->setParameterByClass($backClass, 'ref_id', $ref_id);
89  $this->tabs->setBackTarget(
90  $this->language->txt('back'),
91  $this->ctrl->getLinkTargetByClass(strtolower(ilObjEmployeeTalkGUI::class), ControlFlowCommand::UPDATE)
92  );
93 
94  switch ($this->editMode()) {
95  case self::EDIT_MODE_SERIES:
96  $this->executeSeriesCommand($cmd);
97  break;
98  case self::EDIT_MODE_APPOINTMENT:
99  $this->executeAppointmentCommand($cmd);
100  break;
101  default:
102  $this->ctrl->redirectByClass(strtolower(ilObjEmployeeTalkGUI::class), ControlFlowCommand::UPDATE);
103  break;
104  }
105  }
106 
107  private function executeSeriesCommand(string $cmd): void
108  {
109  $this->template->setTitle($this->language->txt('etal_date_series_edit'));
110 
111  switch ($cmd) {
112  case ControlFlowCommand::UPDATE_INDEX:
113  $this->editSeries();
114  return;
115  case ControlFlowCommand::UPDATE:
116  $this->updateSeries();
117  return;
118  }
119  }
120 
121  private function executeAppointmentCommand(string $cmd): void
122  {
123  $this->template->setTitle($this->language->txt('etal_date_appointment_edit'));
124 
125  switch ($cmd) {
126  case ControlFlowCommand::UPDATE_INDEX:
127  $this->editAppointment();
128  return;
129  case ControlFlowCommand::UPDATE:
130  $this->updateAppointment();
131  return;
132  }
133  }
134 
135  private function editSeries(): void
136  {
137  if (($dates_string = $this->getPendingTalkDates()) !== '') {
138  $message = $this->language->txt('pending_talks_warning');
139  $this->template->setOnScreenMessage('info', $message . $dates_string);
140  }
141  $form = $this->initSeriesEditForm($this->talk->getData());
142 
143  $this->template->setContent($form->getHTML());
144  }
145 
146  protected function getPendingTalkDates(): string
147  {
148  $parent = $this->talk->getParent();
149  $talks = $this->getPendingTalksInSeries($parent);
150 
151  usort($talks, function (ilObjEmployeeTalk $a, ilObjEmployeeTalk $b) {
152  $a = $a->getData()->getStartDate()->getUnixTime();
153  $b = $b->getData()->getStartDate()->getUnixTime();
154  if ($a === $b) {
155  return 0;
156  }
157  return $a < $b ? -1 : 1;
158  });
159 
160  $dates = '';
163  foreach ($talks as $talk) {
164  $dates .= "</br>" . ilDatePresentation::formatDate($talk->getData()->getStartDate());
165  }
167  return $dates;
168  }
169 
170  private function updateSeries(): void
171  {
172  $form = $this->initSeriesEditForm();
173  if ($form->checkInput()) {
174  $reoccurrence = $this->loadRecurrenceSettings($form);
175  $parent = $this->talk->getParent();
176  $old_talks = $this->getPendingTalksInSeries($parent);
177  $this->createRecurringTalks($form, $reoccurrence, $parent);
178  $this->deleteTalks($old_talks);
179 
180  $this->template->setOnScreenMessage('success', $this->language->txt('saved_successfully'), true);
181  }
182 
183  $this->ctrl->redirectToURL(
184  $this->ctrl->getLinkTargetByClass(
185  strtolower(ilEmployeeTalkMyStaffListGUI::class),
186  ControlFlowCommand::UPDATE_INDEX
188  );
189  }
190 
191  private function initTalkEditForm(?EmployeeTalk $employeeTalk = null): ilPropertyFormGUI
192  {
193  $form = new ilPropertyFormGUI();
195  $form->setFormAction($this->ctrl->getFormActionByClass(
196  strtolower(self::class)
197  ) . $editMode);
198 
199  $header = new ilFormSectionHeaderGUI();
200  $header->setTitle($this->language->txt('appointment'));
201  $form->addItem($header);
202 
203  $dur = new ilDateDurationInputGUI($this->language->txt('cal_fullday'), 'event');
204  $dur->setRequired(true);
205  $dur->setShowTime(true);
206 
207  if ($employeeTalk !== null) {
208  $dur->enableToggleFullTime(
209  $this->language->txt('cal_fullday_title'),
210  $employeeTalk->isAllDay()
211  );
212 
213  $dur->setStart($employeeTalk->getStartDate());
214  $dur->setEnd($employeeTalk->getEndDate());
215  }
216 
217  $form->addItem($dur);
218  $form->addCommandButton(ControlFlowCommand::UPDATE, $this->language->txt('save'), 'etal_date_save');
219 
220  return $form;
221  }
222 
223  private function initSeriesEditForm(?EmployeeTalk $employeeTalk = null): ilPropertyFormGUI
224  {
225  $form = new ilPropertyFormGUI();
227  $form->setFormAction($this->ctrl->getFormActionByClass(
228  strtolower(self::class)
229  ) . $editMode);
230 
231  $header = new ilFormSectionHeaderGUI();
232  $header->setTitle($this->language->txt('appointments'));
233  $form->addItem($header);
234 
235  $dur = new ilDateDurationInputGUI($this->language->txt('cal_fullday'), 'event');
236  $dur->setRequired(true);
237  $dur->setShowTime(true);
238 
239  if ($employeeTalk !== null) {
240  $dur->enableToggleFullTime(
241  $this->language->txt('cal_fullday_title'),
242  $employeeTalk->isAllDay()
243  );
244 
245  $dur->setStart($employeeTalk->getStartDate());
246  $dur->setEnd($employeeTalk->getEndDate());
247  }
248 
249  $form->addItem($dur);
250 
251  // Recurrence
252  $cal = new ilRecurrenceInputGUI($this->language->txt('cal_recurrences'), "frequence");
253  $event = new ilCalendarRecurrence();
254 
255  $cal->allowUnlimitedRecurrences(false);
256  $cal->setRecurrence($event);
257 
258  $form->addItem($cal);
259  $form->addCommandButton(ControlFlowCommand::UPDATE, $this->language->txt('save'), 'etal_series_save');
260 
261  return $form;
262  }
263 
264  private function editAppointment(): void
265  {
266  $form = $this->initTalkEditForm($this->talk->getData());
267 
268  $this->template->setContent($form->getHTML());
269  }
270 
271  private function updateAppointment(): void
272  {
273  $form = $this->initTalkEditForm();
274  $form->setValuesByPost();
275  if ($form->checkInput()) {
279  $dateTimeInput = $form->getItemByPostVar('event');
280  $tgl = $form->getInput('event')['tgl'] ?? 0;
281  ['start' => $start, 'end' => $end] = $dateTimeInput->getValue();
282  if (intval($tgl)) {
283  $start_date = new ilDate($start, IL_CAL_UNIX);
284  $end_date = new ilDate($end, IL_CAL_UNIX);
285  } else {
286  $start_date = new ilDateTime($start, IL_CAL_UNIX, ilTimeZone::UTC);
287  $end_date = new ilDateTime($end, IL_CAL_UNIX, ilTimeZone::UTC);
288  }
289 
290  $data = $this->talk->getData();
291  $data->setAllDay(boolval(intval($tgl)));
292  $data->setStartDate($start_date);
293  $data->setEndDate($end_date);
294  $data->setStandalone(true);
295 
296  $this->talk->setData($data);
297  $this->talk->update();
298 
299  $this->sendNotification($this->talk);
300 
301  $this->template->setOnScreenMessage('success', $this->language->txt('saved_successfully'), true);
302  }
303 
304 
305  $this->ctrl->redirectToURL(
306  $this->ctrl->getLinkTargetByClass(
307  strtolower(self::class),
308  ControlFlowCommand::UPDATE_INDEX
310  );
311  }
312 
313  private function sendNotification(ilObjEmployeeTalk ...$talks): void
314  {
315  $this->notif_handler->send(NotificationType::UPDATE, ...$talks);
316  }
317 
318  private function editMode(): string
319  {
320  $mode = '';
321  if ($this->http->wrapper()->query()->has(self::EDIT_MODE)) {
322  $mode = $this->http->wrapper()->query()->retrieve(
323  self::EDIT_MODE,
324  $this->refinery->kindlyTo()->string()
325  );
326  }
327  if ($mode === self::EDIT_MODE_SERIES || $mode === self::EDIT_MODE_APPOINTMENT) {
328  return $mode;
329  }
330  return 'invalid';
331  }
332 
333  private function getEditModeParameter(string $mode): string
334  {
335  return '&' . ilEmployeeTalkAppointmentGUI::EDIT_MODE . '=' . $mode;
336  }
337 
339  {
340  $rec = new ilCalendarRecurrence();
341 
342  switch ($form->getInput('frequence')) {
344  $rec->setFrequenceType($form->getInput('frequence'));
345  $rec->setInterval((int) $form->getInput('count_DAILY'));
346  break;
347 
349  $rec->setFrequenceType($form->getInput('frequence'));
350  $rec->setInterval((int) $form->getInput('count_WEEKLY'));
351  if (is_array($form->getInput('byday_WEEKLY'))) {
352  $rec->setBYDAY(ilUtil::stripSlashes(implode(',', $form->getInput('byday_WEEKLY'))));
353  }
354  break;
355 
357  $rec->setFrequenceType($form->getInput('frequence'));
358  $rec->setInterval((int) $form->getInput('count_MONTHLY'));
359  switch ((int) $form->getInput('subtype_MONTHLY')) {
360  case 0:
361  // nothing to do;
362  break;
363 
364  case 1:
365  switch ((int) $form->getInput('monthly_byday_day')) {
366  case 8:
367  // Weekday
368  $rec->setBYSETPOS($form->getInput('monthly_byday_num'));
369  $rec->setBYDAY('MO,TU,WE,TH,FR');
370  break;
371 
372  case 9:
373  // Day of month
374  $rec->setBYMONTHDAY($form->getInput('monthly_byday_num'));
375  break;
376 
377  default:
378  $rec->setBYDAY(($form->getInput('monthly_byday_num') . $form->getInput('monthly_byday_day')));
379  break;
380  }
381  break;
382 
383  case 2:
384  $rec->setBYMONTHDAY($form->getInput('monthly_bymonthday'));
385  break;
386  }
387  break;
388 
390  $rec->setFrequenceType($form->getInput('frequence'));
391  $rec->setInterval((int) $form->getInput('count_YEARLY'));
392  switch ((int) $form->getInput('subtype_YEARLY')) {
393  case 0:
394  // nothing to do;
395  break;
396 
397  case 1:
398  $rec->setBYMONTH($form->getInput('yearly_bymonth_byday'));
399  $rec->setBYDAY(($form->getInput('yearly_byday_num') . $form->getInput('yearly_byday')));
400  break;
401 
402  case 2:
403  $rec->setBYMONTH($form->getInput('yearly_bymonth_by_monthday'));
404  $rec->setBYMONTHDAY($form->getInput('yearly_bymonthday'));
405  break;
406  }
407  break;
408  }
409 
410  // UNTIL
411  switch ((int) $form->getInput('until_type')) {
412  case 1:
413  $rec->setFrequenceUntilDate(null);
414  // nothing to do
415  break;
416 
417  case 2:
418  $rec->setFrequenceUntilDate(null);
419  $rec->setFrequenceUntilCount((int) $form->getInput('count'));
420  break;
421 
422  case 3:
423  $frequence = $form->getItemByPostVar('frequence');
424  $end = $frequence->getRecurrence()->getFrequenceUntilDate();
425  $rec->setFrequenceUntilCount(0);
426  $rec->setFrequenceUntilDate($end);
427  break;
428  }
429 
430  return $rec;
431  }
432 
436  private function createRecurringTalks(ilPropertyFormGUI $form, ilCalendarRecurrence $recurrence, ilObjEmployeeTalkSeries $series): void
437  {
438  $data = $this->loadEtalkData($form);
439 
440  $firstAppointment = new EmployeeTalkPeriod(
441  $data->getStartDate(),
442  $data->getEndDate(),
443  $data->isAllDay()
444  );
445  $calc = new ilCalendarRecurrenceCalculator($firstAppointment, $recurrence);
446 
447  $periodStart = clone $data->getStartDate();
448 
449  $periodEnd = clone $data->getStartDate();
450  $periodEnd->increment(IL_CAL_YEAR, 5);
451  $dateIterator = $calc->calculateDateList($periodStart, $periodEnd);
452 
453  $periodDiff = $data->getEndDate()->get(IL_CAL_UNIX) -
454  $data->getStartDate()->get(IL_CAL_UNIX);
455 
456  $talkSession = new ilObjEmployeeTalk();
457  $talkSession->setTitle($this->talk->getTitle());
458  $talkSession->setDescription($this->talk->getLongDescription());
459  $talkSession->setType(ilObjEmployeeTalk::TYPE);
460  $talkSession->setOwner($series->getOwner());
461  $talkSession->create();
462 
463  $talkSession->createReference();
464  $talkSession->putInTree($series->getRefId());
465 
466  $data->setObjectId($talkSession->getId());
467  $talkSession->setData($data);
468  $talkSession->update();
469 
470  $talks = [];
471  $talks[] = $talkSession;
472 
473  if (!$recurrence->getFrequenceType()) {
474  $this->sendNotification(...$talks);
475  return;
476  }
477 
478  // Remove start date
479  $dateIterator->removeByDAY($periodStart);
480  $dateIterator->rewind();
481 
485  foreach ($dateIterator as $date) {
486  $cloneObject = $talkSession->cloneObject($series->getRefId());
487  $cloneData = $cloneObject->getData();
488 
489  $cloneData->setStartDate($date);
490  $endDate = $date->get(IL_CAL_UNIX) + $periodDiff;
491  if ($cloneData->isAllDay()) {
492  $cloneData->setEndDate(new ilDate($endDate, IL_CAL_UNIX));
493  } else {
494  $cloneData->setEndDate(new ilDateTime($endDate, IL_CAL_UNIX, ilTimeZone::UTC));
495  }
496  $cloneObject->setData($cloneData);
497  $cloneObject->update();
498 
499  $cloneObject->setOwner($series->getOwner());
500  $cloneObject->updateOwner();
501 
502  $talks[] = $cloneObject;
503  }
504 
505  $this->sendNotification(...$talks);
506  }
507 
511  private function getPendingTalksInSeries(ilObjEmployeeTalkSeries $series): array
512  {
513  $talks = [];
514  $subItems = $series->getSubItems()['_all'];
515 
516  foreach ($subItems as $subItem) {
517  if ($subItem['type'] === 'etal') {
518  $refId = intval($subItem['ref_id']);
519  $talk = new ilObjEmployeeTalk($refId, true);
520  $talk_data = $talk->getData();
521  if ($talk_data->isStandalone() || $talk_data->isCompleted()) {
522  continue;
523  }
524  $talks[] = $talk;
525  }
526  }
527 
528  return $talks;
529  }
530 
534  private function deleteTalks(array $talks): void
535  {
536  foreach ($talks as $talk) {
537  $talk->delete();
538  }
539  }
540 
541  private function loadEtalkData(ilPropertyFormGUI $form): EmployeeTalk
542  {
543  $data = $this->talk->getData();
544  $tgl = $form->getInput('event')['tgl'] ?? 0;
545 
549  $dateTimeInput = $form->getItemByPostVar('event');
550  ['start' => $start, 'end' => $end] = $dateTimeInput->getValue();
551  if (intval($tgl)) {
552  $start_date = new ilDate($start, IL_CAL_UNIX);
553  $end_date = new ilDate($end, IL_CAL_UNIX);
554  } else {
555  $start_date = new ilDateTime($start, IL_CAL_UNIX, ilTimeZone::UTC);
556  $end_date = new ilDateTime($end, IL_CAL_UNIX, ilTimeZone::UTC);
557  }
558 
559  return new EmployeeTalk(
560  -1,
561  $start_date,
562  $end_date,
563  boolval(intval($tgl)),
564  '',
565  $data->getLocation(),
566  $data->getEmployee(),
567  false,
568  false,
569  $data->getTemplateId()
570  );
571  }
572 }
static array static setUseRelativeDates(bool $a_status)
set use relative dates
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getItemByPostVar(string $a_post_var)
getSubItems(bool $a_admin_panel_enabled=false, bool $a_include_side_block=false, int $a_get_single=0, ?ilContainerUserFilter $container_user_filter=null)
This class represents an input GUI for recurring events/appointments (course events or calendar appoi...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
Model of calendar entry recurrcences based on iCalendar-RFC-5545.
getPendingTalksInSeries(ilObjEmployeeTalkSeries $series)
__construct(ilGlobalTemplateInterface $template, ilLanguage $language, ilCtrl $ctrl, HttpServices $http, Refinery $refinery, ilTabsGUI $tabs, NotificationHandlerInterface $notif_handler, ilObjEmployeeTalk $talk)
$refId
Definition: xapitoken.php:58
const IL_CAL_UNIX
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getFrequenceType()
Get Frequence type of recurrence.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
Calculates an ilDateList for a given calendar entry and recurrence rule.
sendNotification(ilObjEmployeeTalk ... $talks)
initSeriesEditForm(?EmployeeTalk $employeeTalk=null)
Class ilEmployeeTalkAppointmentGUI.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
initTalkEditForm(?EmployeeTalk $employeeTalk=null)
$message
Definition: xapiexit.php:31
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
const IL_CAL_YEAR