ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjEmployeeTalkSeriesGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
48 {
49  private \ILIAS\DI\Container $container;
53  private int $userId = -1;
54  private string $link_to_parent;
55 
56  public function __construct()
57  {
58  global $DIC;
59 
60  $this->container = $DIC;
61 
62  $refId = $this->container
63  ->http()
64  ->wrapper()
65  ->query()
66  ->retrieve("ref_id", $this->container->refinery()->kindlyTo()->int());
67 
68  parent::__construct([], $refId, true, false);
69  $this->container->language()->loadLanguageModule('mst');
70  $this->container->language()->loadLanguageModule('trac');
71  $this->container->language()->loadLanguageModule('etal');
72  $this->container->language()->loadLanguageModule('dateplaner');
73 
74  $this->type = ilObjEmployeeTalkSeries::TYPE;
75 
76  $wrapper = $this->container->http()->wrapper()->query();
77 
78  if ($wrapper->has('usr_id')) {
79  $this->userId = $wrapper->retrieve('usr_id', $this->container->refinery()->kindlyTo()->int());
80  }
81 
82  $this->md_handler = new MetadataHandler();
83  $this->notif_handler = new NotificationHandler(new VCalendarGenerator($this->container->language()));
84 
85  $this->omitLocator();
86  }
87 
88  private function checkAccessOrFail(): void
89  {
90  $talkAccess = new ilObjEmployeeTalkAccess();
91  if (!$talkAccess->canCreate()) {
92  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
93  $this->ctrl->redirectByClass(ilDashboardGUI::class, "");
94  }
95  }
96 
97  public function setLinkToParentGUI(string $link): void
98  {
99  $this->link_to_parent = $link;
100  }
101 
102  public function redirectToParentGUI(): void
103  {
104  if (isset($this->link_to_parent)) {
105  $this->ctrl->redirectToURL($this->link_to_parent);
106  }
107  $this->ctrl->redirectByClass(strtolower(ilEmployeeTalkMyStaffListGUI::class));
108  }
109 
110  public function executeCommand(): void
111  {
112  $this->checkAccessOrFail();
113 
114  // determine next class in the call structure
115  $next_class = $this->container->ctrl()->getNextClass($this);
116 
117  switch ($next_class) {
118  case strtolower(ilRepositorySearchGUI::class):
119  $repo = new ilRepositorySearchGUI();
120  $repo->addUserAccessFilterCallable(function ($user_ids) {
136  return array_filter(
137  $user_ids,
138  function (int $id) use ($access) {
139  return $access->canCreate(new ilObjUser($id));
140  }
141  );
142  });
143  $this->container->ctrl()->forwardCommand($repo);
144  break;
145  default:
146  parent::executeCommand();
147  }
148  }
149 
154  protected function setTitleAndDescription(): void
155  {
156  $this->tabs_gui->clearTargets();
157  $this->tpl->resetHeaderBlock();
158  }
159 
164  protected function checkPermissionBool(string $perm, string $cmd = "", string $type = "", ?int $ref_id = null): bool
165  {
166  if ($perm === 'create') {
167  return true;
168  }
169  return false;
170  }
171 
172  public function cancelObject(): void
173  {
174  $this->redirectToParentGUI();
175  }
176 
182  protected function afterSave(ilObject $new_object): void
183  {
187  $newObject = $new_object;
188 
189  // Create clones of the first one
190  $event = $this->loadRecurrenceSettings();
191  $this->copyTemplateValues($newObject);
192  $this->createRecurringTalks($newObject, $event);
193 
194  $this->tpl->setOnScreenMessage('success', $this->lng->txt("object_added"), true);
195  $this->redirectToParentGUI();
196  }
197 
198  public function saveObject(): void
199  {
200  $this->ctrl->setParameter($this, "new_type", $this->requested_new_type);
201 
202  $form = $this->initCreateForm($this->requested_new_type);
203  if ($form->checkInput()) {
204  $userName = (string) $form->getInput('etal_employee');
205  $userId = (int) ilObjUser::_lookupId($userName);
206  $talkAccess = new ilObjEmployeeTalkAccess();
207  if (
208  !ilObjUser::_loginExists($userName) ||
209  !$talkAccess->canCreate(new ilObjUser($userId))
210  ) {
211  $form->getItemByPostVar('etal_employee')
212  ->setAlert($this->lng->txt('etal_invalid_user'));
213  $this->tpl->setOnScreenMessage(
214  'failure',
215  $this->lng->txt('form_input_not_valid')
216  );
217  $form->setValuesByPost();
218  $this->tpl->setContent($form->getHTML());
219  return;
220  }
221 
222  $this->ctrl->setParameter($this, "new_type", "");
223 
224  $class_name = "ilObj" . $this->obj_definition->getClassName($this->requested_new_type);
225  $newObj = new $class_name();
226  $newObj->setType($this->requested_new_type);
227  $newObj->setTitle($form->getInput("title"));
228  $newObj->setDescription($form->getInput("desc"));
229  $newObj->create();
230 
231  $this->putObjectInTree($newObj);
232 
233  $this->afterSave($newObj);
234  }
235 
236  $form->setValuesByPost();
237  $this->tpl->setContent($form->getHTML());
238  }
239 
240  protected function initCreateForm(string $new_type): ilPropertyFormGUI
241  {
242  $form = new ilPropertyFormGUI();
243  $form->setTarget("_top");
244  $form->setFormAction($this->ctrl->getFormAction($this, "save") . '&template=' . $this->getTemplateRefId());
245  $form->setTitle($this->lng->txt($new_type . "_new"));
246 
247  // title
248  $ti = new ilTextInputGUI($this->lng->txt("title"), "title");
249  $ti->setSize(min(40, ilObject::TITLE_LENGTH));
250  $ti->setMaxLength(ilObject::TITLE_LENGTH);
251  $ti->setRequired(true);
252  $form->addItem($ti);
253 
254  // description
255  $ta = new ilTextAreaInputGUI($this->lng->txt("description"), "desc");
256  $ta->setCols(40);
257  $ta->setRows(2);
258  $form->addItem($ta);
259 
260  // Start & End Date
261  $dur = new ilDateDurationInputGUI($this->lng->txt('cal_fullday'), 'etal_event');
262  $dur->setRequired(true);
263  $dur->enableToggleFullTime(
264  $this->lng->txt('cal_fullday_title'),
265  false
266  );
267  $dur->setShowTime(true);
268  $form->addItem($dur);
269 
270  // Recurrence
271  $cal = new ilRecurrenceInputGUI("Calender", "frequence");
272  $event = new ilCalendarRecurrence();
273  //$event->setRecurrence(ilEventRecurrence::REC_EXCLUSION);
274  //$event->setFrequenceType(ilEventRecurrence::FREQ_DAILY);
275  $cal->allowUnlimitedRecurrences(false);
276  $cal->setRecurrence($event);
277  $form->addItem($cal);
278 
279  //Location
280  $location = new ilTextInputGUI("Location", "etal_location");
281  $location->setMaxLength(200);
282  $form->addItem($location);
283 
284  //Employee
285  $login = new ilTextInputGUI($this->lng->txt("employee"), "etal_employee");
286  $login->setRequired(true);
287  $login->setDataSource($this->ctrl->getLinkTargetByClass([
288  strtolower(self::class),
289  strtolower(ilRepositorySearchGUI::class)
290  ], 'doUserAutoComplete', '', true));
291 
292  if ($this->userId !== -1) {
293  $user = new ilObjUser($this->userId);
294  $login->setValue($user->getLogin());
295  }
296 
297  $form->addItem($login);
298 
299  $form = $this->initDidacticTemplate($form);
300 
301  $form->addCommandButton("save", $this->lng->txt($new_type . "_add"));
302  $form->addCommandButton("cancel", $this->lng->txt("cancel"));
303 
304  $this->form = $form;
305 
306  return $form;
307  }
308 
309  protected function initCreationForms($new_type): array
310  {
311  return [
312  self::CFORM_NEW => $this->initCreateForm($new_type)
313  ];
314  }
315 
316  public function viewObject(): void
317  {
318  self::_goto((string) $this->ref_id);
319  }
320 
321  public function getTabs(): void
322  {
323  }
324 
325  public function getAdminTabs(): void
326  {
327  }
328 
329  private function sendNotification(ilObjEmployeeTalk ...$talks): void
330  {
331  $this->notif_handler->send(NotificationType::INVITATION, ...$talks);
332  }
333 
341  {
342  $rec = new ilCalendarRecurrence();
343 
344  switch ($this->form->getInput('frequence')) {
346  $rec->setFrequenceType($this->form->getInput('frequence'));
347  $rec->setInterval((int) $this->form->getInput('count_DAILY'));
348  break;
349 
351  $rec->setFrequenceType($this->form->getInput('frequence'));
352  $rec->setInterval((int) $this->form->getInput('count_WEEKLY'));
353  if (is_array($this->form->getInput('byday_WEEKLY'))) {
354  $rec->setBYDAY(ilUtil::stripSlashes(implode(',', $this->form->getInput('byday_WEEKLY'))));
355  }
356  break;
357 
359  $rec->setFrequenceType($this->form->getInput('frequence'));
360  $rec->setInterval((int) $this->form->getInput('count_MONTHLY'));
361  switch ((int) $this->form->getInput('subtype_MONTHLY')) {
362  case 0:
363  // nothing to do;
364  break;
365 
366  case 1:
367  switch ((int) $this->form->getInput('monthly_byday_day')) {
368  case 8:
369  // Weekday
370  $rec->setBYSETPOS($this->form->getInput('monthly_byday_num'));
371  $rec->setBYDAY('MO,TU,WE,TH,FR');
372  break;
373 
374  case 9:
375  // Day of month
376  $rec->setBYMONTHDAY($this->form->getInput('monthly_byday_num'));
377  break;
378 
379  default:
380  $rec->setBYDAY(($this->form->getInput('monthly_byday_num') . $this->form->getInput('monthly_byday_day')));
381  break;
382  }
383  break;
384 
385  case 2:
386  $rec->setBYMONTHDAY($this->form->getInput('monthly_bymonthday'));
387  break;
388  }
389  break;
390 
392  $rec->setFrequenceType($this->form->getInput('frequence'));
393  $rec->setInterval((int) $this->form->getInput('count_YEARLY'));
394  switch ((int) $this->form->getInput('subtype_YEARLY')) {
395  case 0:
396  // nothing to do;
397  break;
398 
399  case 1:
400  $rec->setBYMONTH($this->form->getInput('yearly_bymonth_byday'));
401  $rec->setBYDAY(($this->form->getInput('yearly_byday_num') . $this->form->getInput('yearly_byday')));
402  break;
403 
404  case 2:
405  $rec->setBYMONTH($this->form->getInput('yearly_bymonth_by_monthday'));
406  $rec->setBYMONTHDAY($this->form->getInput('yearly_bymonthday'));
407  break;
408  }
409  break;
410  }
411 
412  // UNTIL
413  switch ((int) $this->form->getInput('until_type')) {
414  case 1:
415  $rec->setFrequenceUntilDate(null);
416  // nothing to do
417  break;
418 
419  case 2:
420  $rec->setFrequenceUntilDate(null);
421  $rec->setFrequenceUntilCount((int) $this->form->getInput('count'));
422  break;
423 
424  case 3:
425  $frequence = $this->form->getItemByPostVar('frequence');
426  $end = $frequence->getRecurrence()->getFrequenceUntilDate();
427  $rec->setFrequenceUntilCount(0);
428  $rec->setFrequenceUntilDate($end);
429  break;
430  }
431 
432  return $rec;
433  }
434 
435 
436 
437  private function loadEtalkData(): EmployeeTalk
438  {
439  $location = $this->form->getInput('etal_location');
440  $employee = $this->form->getInput('etal_employee');
441  ['fullday' => $tgl] = $this->form->getInput('etal_event');
442 
446  $dateTimeInput = $this->form->getItemByPostVar('etal_event');
447  ['start' => $start, 'end' => $end] = $dateTimeInput->getValue();
448  if (intval($tgl)) {
449  $start_date = new ilDate($start, IL_CAL_UNIX);
450  $end_date = new ilDate($end, IL_CAL_UNIX);
451  } else {
452  $start_date = new ilDateTime($start, IL_CAL_UNIX, ilTimeZone::UTC);
453  $end_date = new ilDateTime($end, IL_CAL_UNIX, ilTimeZone::UTC);
454  }
455 
456  return new EmployeeTalk(
457  -1,
458  $start_date,
459  $end_date,
460  boolval(intval($tgl)),
461  '',
462  $location ?? '',
463  ilObjUser::getUserIdByLogin($employee),
464  false,
465  false,
467  );
468  }
469 
476  {
477  $template = new ilObjTalkTemplate($this->getTemplateRefId(), true);
478  $talk->setDescription($template->getTitle());
479  $talk->update();
480 
481  $this->md_handler->copyValues(
482  $template->getType(),
483  $template->getId(),
484  $talk->getType(),
485  $talk->getId(),
487  );
488  }
489 
497  private function createRecurringTalks(ilObjEmployeeTalkSeries $talk, ilCalendarRecurrence $recurrence): bool
498  {
499  $data = $this->loadEtalkData();
500 
501  $firstAppointment = new EmployeeTalkPeriod(
502  $data->getStartDate(),
503  $data->getEndDate(),
504  $data->isAllDay()
505  );
506  $calc = new ilCalendarRecurrenceCalculator($firstAppointment, $recurrence);
507 
508  $periodStart = clone $data->getStartDate();
509 
510  $periodEnd = clone $data->getStartDate();
511  $periodEnd->increment(IL_CAL_YEAR, 5);
512  $dateIterator = $calc->calculateDateList($periodStart, $periodEnd);
513 
514  $periodDiff = $data->getEndDate()->get(IL_CAL_UNIX) -
515  $data->getStartDate()->get(IL_CAL_UNIX);
516 
517  $talkSession = new ilObjEmployeeTalk();
518  $talkSession->setTitle($this->form->getInput('title'));
519  $talkSession->setDescription($this->form->getInput('desc'));
520  $talkSession->setType(ilObjEmployeeTalk::TYPE);
521  $talkSession->create();
522 
523  $talkSession->createReference();
524  $talkSession->putInTree($talk->getRefId());
525 
526  $data->setObjectId($talkSession->getId());
527  $talkSession->setData($data);
528  $talkSession->update();
529  $talks = [];
530  $talks[] = $talkSession;
531 
532  if (!$recurrence->getFrequenceType()) {
533  $this->sendNotification(...$talks);
534  return true;
535  }
536 
537  // Remove start date
538  $dateIterator->removeByDAY($periodStart);
539  $dateIterator->rewind();
540 
544  foreach ($dateIterator as $date) {
545  $cloneObject = $talkSession->cloneObject($talk->getRefId());
546  $cloneData = $cloneObject->getData();
547 
548  $cloneData->setStartDate($date);
549  $endDate = $date->get(IL_CAL_UNIX) + $periodDiff;
550  if ($cloneData->isAllDay()) {
551  $cloneData->setEndDate(new ilDate($endDate, IL_CAL_UNIX));
552  } else {
553  $cloneData->setEndDate(new ilDateTime($endDate, IL_CAL_UNIX, ilTimeZone::UTC));
554  }
555  $cloneObject->setData($cloneData);
556  $cloneObject->update();
557  $talks[] = $cloneObject;
558  }
559 
560  $this->sendNotification(...$talks);
561 
562  return true;
563  }
564 
565  public static function _goto(string $refId): void
566  {
567  global $DIC;
568 
569  $children = $DIC->repositoryTree()->getChildIds((int) $refId);
570 
571  /*
572  * If the series contains talks, redirect to first talk,
573  * if not (which should only happen if someone messes with
574  * the URL) redirect to dashboard.
575  */
576  if (empty($children)) {
577  $DIC->ui()->mainTemplate()->setOnScreenMessage(
578  'failure',
579  $DIC->language()->txt("permission_denied"),
580  true
581  );
582  $DIC->ctrl()->redirectByClass(ilDashboardGUI::class, "");
583  }
584  ilObjEmployeeTalkGUI::_goto((string) $children[0]);
585  }
586 
587  private function getTemplateRefId(): int
588  {
589  $refId = 0;
590  if ($this->container->http()->wrapper()->query()->has('template')) {
591  $refId = $this->container->http()->wrapper()->query()->retrieve(
592  'template',
593  $this->container->refinery()->kindlyTo()->int()
594  );
595  }
596  if (
599  ) {
600  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('etal_create_invalid_template_ref'), true);
601  $this->redirectToParentGUI();
602  }
603 
604  return $refId;
605  }
606 }
omitLocator(bool $omit=true)
getItemByPostVar(string $a_post_var)
const TITLE_LENGTH
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTarget(string $a_target)
static _lookupId($a_user_str)
checkPermissionBool(string $perm, string $cmd="", string $type="", ?int $ref_id=null)
Talk Series does not use RBAC and therefore does not require the usual permission checks...
$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-...
static lookupOfflineStatus(int $obj_id)
Lookup offline status using objectDataCache.
getFrequenceType()
Get Frequence type of recurrence.
global $DIC
Definition: feed.php:28
static getUserIdByLogin(string $a_login)
__construct(VocabulariesInterface $vocabularies)
static _exists(int $id, bool $reference=false, ?string $type=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFormAction(string $a_formaction)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initDidacticTemplate(ilPropertyFormGUI $form)
static _loginExists(string $a_login, int $a_user_id=0)
check if a login name already exists You may exclude a user from the check by giving his user id as 2...
loadRecurrenceSettings()
load recurrence settings
sendNotification(ilObjEmployeeTalk ... $talks)
static _lookupObjectId(int $ref_id)
afterSave(ilObject $new_object)
Post (successful) object creation hook.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
putObjectInTree(ilObject $obj, int $parent_node_id=null)
Add object to tree at given position.
form( $class_path, string $cmd, string $submit_caption="")
NotificationHandlerInterface $notif_handler
This class represents a text area property in a property form.
ilAccessHandler $access
Class ilContainerGUI This is a base GUI class for all container objects in ILIAS: root folder...
setTitleAndDescription()
This GUI is only called when creating a talk (series).
setDescription(string $description)
copyTemplateValues(ilObjEmployeeTalkSeries $talk)
Copy the template values, into the talk series object.
const IL_CAL_YEAR