ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  public function viewObject(): void
310  {
311  self::_goto((string) $this->ref_id);
312  }
313 
314  protected function getTabs(): void
315  {
316  }
317 
318  public function getAdminTabs(): void
319  {
320  }
321 
322  private function sendNotification(ilObjEmployeeTalk ...$talks): void
323  {
324  $this->notif_handler->send(NotificationType::INVITATION, ...$talks);
325  }
326 
334  {
335  $rec = new ilCalendarRecurrence();
336 
337  switch ($this->form->getInput('frequence')) {
339  $rec->setFrequenceType($this->form->getInput('frequence'));
340  $rec->setInterval((int) $this->form->getInput('count_DAILY'));
341  break;
342 
344  $rec->setFrequenceType($this->form->getInput('frequence'));
345  $rec->setInterval((int) $this->form->getInput('count_WEEKLY'));
346  if (is_array($this->form->getInput('byday_WEEKLY'))) {
347  $rec->setBYDAY(ilUtil::stripSlashes(implode(',', $this->form->getInput('byday_WEEKLY'))));
348  }
349  break;
350 
352  $rec->setFrequenceType($this->form->getInput('frequence'));
353  $rec->setInterval((int) $this->form->getInput('count_MONTHLY'));
354  switch ((int) $this->form->getInput('subtype_MONTHLY')) {
355  case 0:
356  // nothing to do;
357  break;
358 
359  case 1:
360  switch ((int) $this->form->getInput('monthly_byday_day')) {
361  case 8:
362  // Weekday
363  $rec->setBYSETPOS($this->form->getInput('monthly_byday_num'));
364  $rec->setBYDAY('MO,TU,WE,TH,FR');
365  break;
366 
367  case 9:
368  // Day of month
369  $rec->setBYMONTHDAY($this->form->getInput('monthly_byday_num'));
370  break;
371 
372  default:
373  $rec->setBYDAY(($this->form->getInput('monthly_byday_num') . $this->form->getInput('monthly_byday_day')));
374  break;
375  }
376  break;
377 
378  case 2:
379  $rec->setBYMONTHDAY($this->form->getInput('monthly_bymonthday'));
380  break;
381  }
382  break;
383 
385  $rec->setFrequenceType($this->form->getInput('frequence'));
386  $rec->setInterval((int) $this->form->getInput('count_YEARLY'));
387  switch ((int) $this->form->getInput('subtype_YEARLY')) {
388  case 0:
389  // nothing to do;
390  break;
391 
392  case 1:
393  $rec->setBYMONTH($this->form->getInput('yearly_bymonth_byday'));
394  $rec->setBYDAY(($this->form->getInput('yearly_byday_num') . $this->form->getInput('yearly_byday')));
395  break;
396 
397  case 2:
398  $rec->setBYMONTH($this->form->getInput('yearly_bymonth_by_monthday'));
399  $rec->setBYMONTHDAY($this->form->getInput('yearly_bymonthday'));
400  break;
401  }
402  break;
403  }
404 
405  // UNTIL
406  switch ((int) $this->form->getInput('until_type')) {
407  case 1:
408  $rec->setFrequenceUntilDate(null);
409  // nothing to do
410  break;
411 
412  case 2:
413  $rec->setFrequenceUntilDate(null);
414  $rec->setFrequenceUntilCount((int) $this->form->getInput('count'));
415  break;
416 
417  case 3:
418  $frequence = $this->form->getItemByPostVar('frequence');
419  $end = $frequence->getRecurrence()->getFrequenceUntilDate();
420  $rec->setFrequenceUntilCount(0);
421  $rec->setFrequenceUntilDate($end);
422  break;
423  }
424 
425  return $rec;
426  }
427 
428 
429 
430  private function loadEtalkData(): EmployeeTalk
431  {
432  $location = $this->form->getInput('etal_location');
433  $employee = $this->form->getInput('etal_employee');
434  ['fullday' => $tgl] = $this->form->getInput('etal_event');
435 
439  $dateTimeInput = $this->form->getItemByPostVar('etal_event');
440  ['start' => $start, 'end' => $end] = $dateTimeInput->getValue();
441  if (intval($tgl)) {
442  $start_date = new ilDate($start, IL_CAL_UNIX);
443  $end_date = new ilDate($end, IL_CAL_UNIX);
444  } else {
445  $start_date = new ilDateTime($start, IL_CAL_UNIX, ilTimeZone::UTC);
446  $end_date = new ilDateTime($end, IL_CAL_UNIX, ilTimeZone::UTC);
447  }
448 
449  return new EmployeeTalk(
450  -1,
451  $start_date,
452  $end_date,
453  boolval(intval($tgl)),
454  '',
455  $location ?? '',
456  ilObjUser::getUserIdByLogin($employee),
457  false,
458  false,
460  );
461  }
462 
469  {
470  $template = new ilObjTalkTemplate($this->getTemplateRefId(), true);
471  $talk->setDescription($template->getTitle());
472  $talk->update();
473 
474  $this->md_handler->copyValues(
475  $template->getType(),
476  $template->getId(),
477  $talk->getType(),
478  $talk->getId(),
480  );
481  }
482 
490  private function createRecurringTalks(ilObjEmployeeTalkSeries $talk, ilCalendarRecurrence $recurrence): void
491  {
492  $data = $this->loadEtalkData();
493 
494  $firstAppointment = new EmployeeTalkPeriod(
495  $data->getStartDate(),
496  $data->getEndDate(),
497  $data->isAllDay()
498  );
499  $calc = new ilCalendarRecurrenceCalculator($firstAppointment, $recurrence);
500 
501  $periodStart = clone $data->getStartDate();
502 
503  $periodEnd = clone $data->getStartDate();
504  $periodEnd->increment(IL_CAL_YEAR, 5);
505  $dateIterator = $calc->calculateDateList($periodStart, $periodEnd);
506 
507  $periodDiff = $data->getEndDate()->get(IL_CAL_UNIX) -
508  $data->getStartDate()->get(IL_CAL_UNIX);
509 
510  $talkSession = new ilObjEmployeeTalk();
511  $talkSession->setTitle($this->form->getInput('title'));
512  $talkSession->setDescription($this->form->getInput('desc'));
513  $talkSession->setType(ilObjEmployeeTalk::TYPE);
514  $talkSession->create();
515 
516  $talkSession->createReference();
517  $talkSession->putInTree($talk->getRefId());
518 
519  $data->setObjectId($talkSession->getId());
520  $talkSession->setData($data);
521  $talkSession->update();
522  $talks = [];
523  $talks[] = $talkSession;
524 
525  if (!$recurrence->getFrequenceType()) {
526  $this->sendNotification(...$talks);
527  return;
528  }
529 
530  // Remove start date
531  $dateIterator->removeByDAY($periodStart);
532  $dateIterator->rewind();
533 
537  foreach ($dateIterator as $date) {
538  $cloneObject = $talkSession->cloneObject($talk->getRefId());
539  $cloneData = $cloneObject->getData();
540 
541  $cloneData->setStartDate($date);
542  $endDate = $date->get(IL_CAL_UNIX) + $periodDiff;
543  if ($cloneData->isAllDay()) {
544  $cloneData->setEndDate(new ilDate($endDate, IL_CAL_UNIX));
545  } else {
546  $cloneData->setEndDate(new ilDateTime($endDate, IL_CAL_UNIX, ilTimeZone::UTC));
547  }
548  $cloneObject->setData($cloneData);
549  $cloneObject->update();
550  $talks[] = $cloneObject;
551  }
552 
553  $this->sendNotification(...$talks);
554  }
555 
556  public static function _goto(string $refId): void
557  {
558  global $DIC;
559 
560  $children = $DIC->repositoryTree()->getChildIds((int) $refId);
561 
562  /*
563  * If the series contains talks, redirect to first talk,
564  * if not (which should only happen if someone messes with
565  * the URL) redirect to dashboard.
566  */
567  if (empty($children)) {
568  $DIC->ui()->mainTemplate()->setOnScreenMessage(
569  'failure',
570  $DIC->language()->txt("permission_denied"),
571  true
572  );
573  $DIC->ctrl()->redirectByClass(ilDashboardGUI::class, "");
574  }
575  ilObjEmployeeTalkGUI::_goto((string) $children[0]);
576  }
577 
578  private function getTemplateRefId(): int
579  {
580  $refId = 0;
581  if ($this->container->http()->wrapper()->query()->has('template')) {
582  $refId = $this->container->http()->wrapper()->query()->retrieve(
583  'template',
584  $this->container->refinery()->kindlyTo()->int()
585  );
586  }
587  if (
590  ) {
591  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('etal_create_invalid_template_ref'), true);
592  $this->redirectToParentGUI();
593  }
594 
595  return $refId;
596  }
597 }
omitLocator(bool $omit=true)
getItemByPostVar(string $a_post_var)
const TITLE_LENGTH
$location
Definition: buildRTE.php:22
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.
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getUserIdByLogin(string $a_login)
static _exists(int $id, bool $reference=false, ?string $type=null)
Calculates an ilDateList for a given calendar entry and recurrence rule.
setFormAction(string $a_formaction)
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)
global $DIC
Definition: shib_login.php:22
static _lookupObjectId(int $ref_id)
afterSave(ilObject $new_object)
Post (successful) object creation hook.
putObjectInTree(ilObject $obj, ?int $parent_node_id=null)
Add object to tree at given position.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
form( $class_path, string $cmd, string $submit_caption="")
NotificationHandlerInterface $notif_handler
__construct(Container $dic, ilPlugin $plugin)
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