ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCalendarViewGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 
34 {
35  public const CAL_PRESENTATION_UNDEFINED = 0;
36  public const CAL_PRESENTATION_DAY = 1;
37  public const CAL_PRESENTATION_WEEK = 2;
38  public const CAL_PRESENTATION_MONTH = 3;
39  public const CAL_PRESENTATION_AGENDA_LIST = 9;
40 
41  protected int $presentation_type = self::CAL_PRESENTATION_UNDEFINED;
42  protected bool $view_with_appointments = false;
43  protected ilDate $seed;
44  protected int $ch_user_id = 0;
45  protected ?string $period_end_day = null;
46 
47  protected Factory $ui_factory;
51  protected ilLogger $logger;
52  protected \ILIAS\DI\UIServices $ui;
53  protected ilLanguage $lng;
54  protected ilObjUser $user;
55  protected ?ilTemplate $tpl;
58  protected ilTabsGUI $tabs_gui;
60  protected HttpServices $http;
61 
62  public function __construct(ilDate $seed, int $presentation_type)
63  {
64  $this->seed = $seed;
65  $this->initialize($presentation_type);
66  }
67 
68  public function setConsulationHoursUserId(int $a_user_id): void
69  {
70  $this->ch_user_id = $a_user_id;
71  }
72 
73  public function getConsultationHoursUserId(): int
74  {
75  return $this->ch_user_id;
76  }
77 
78  public function initialize(int $a_calendar_presentation_type): void
79  {
80  global $DIC;
81 
82  $this->component_factory = $DIC['component.factory'];
83  $this->ui_factory = $DIC->ui()->factory();
84  $this->ui_renderer = $DIC->ui()->renderer();
85  $this->ui = $DIC->ui();
86  $this->ctrl = $DIC->ctrl();
87  $this->lng = $DIC->language();
88  $this->user = $DIC->user();
89  $this->tabs_gui = $DIC->tabs();
90  $this->toolbar = $DIC->toolbar();
91  $this->presentation_type = $a_calendar_presentation_type;
92  $this->logger = $DIC->logger()->cal();
93  $this->view_with_appointments = false;
94  $this->main_tpl = $DIC->ui()->mainTemplate();
95  if ($this->presentation_type == self::CAL_PRESENTATION_DAY ||
96  $this->presentation_type == self::CAL_PRESENTATION_WEEK) {
97  iljQueryUtil::initjQuery($this->main_tpl);
98  $this->main_tpl->addJavaScript('assets/js/calendar_appointment.js');
99  }
100  $this->http = $DIC->http();
101  $this->refinery = $DIC->refinery();
102  }
103 
104  protected function initAppointmentIdFromQuery(): int
105  {
106  if ($this->http->wrapper()->query()->has('app_id')) {
107  return $this->http->wrapper()->query()->retrieve(
108  'app_id',
109  $this->refinery->kindlyTo()->int()
110  );
111  }
112  return 0;
113  }
114 
115  protected function initInitialDateFromQuery(): string
116  {
117  if ($this->http->wrapper()->query()->has('idate')) {
118  return $this->http->wrapper()->query()->retrieve(
119  'idate',
120  $this->refinery->kindlyTo()->string()
121  );
122  }
123  return '';
124  }
125 
126  protected function initInitialDateTimeFromQuery(): int
127  {
128  if ($this->http->wrapper()->query()->has('dt')) {
129  return $this->http->wrapper()->query()->retrieve(
130  'dt',
131  $this->refinery->kindlyTo()->int()
132  );
133  }
134  return 0;
135  }
136 
137  protected function initBookingUserFromQuery(): int
138  {
139  if ($this->http->wrapper()->query()->has('bkid')) {
140  return $this->http->wrapper()->query()->retrieve(
141  'bkid',
142  $this->refinery->kindlyTo()->int()
143  );
144  }
145  return 0;
146  }
147 
148  public function getCurrentApp(): ?array
149  {
150  // @todo: this needs optimization
151  $events = $this->getEvents();
152  foreach ($events as $item) {
153  if ($item["event"]->getEntryId() == $this->initAppointmentIdFromQuery()) {
154  return $item;
155  }
156  }
157  return null;
158  }
159 
160  public function getEvents(): array
161  {
162  $user = $this->user->getId();
163  $schedule = null;
164  switch ($this->presentation_type) {
165  case self::CAL_PRESENTATION_AGENDA_LIST:
166  $end_date = clone $this->seed;
169  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_DAY, $user, true);
170  $end_date->increment(IL_CAL_DAY, 1);
171  break;
172 
174  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
175  $end_date->increment(IL_CAL_WEEK, 1);
176  break;
177 
179  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_MONTH, $user, true);
180  $end_date->increment(IL_CAL_MONTH, 1);
181  break;
182 
184  $schedule = new ilCalendarSchedule(
185  $this->seed,
187  $user,
188  true
189  );
190  $end_date->increment(IL_CAL_MONTH, 6);
191  break;
192  default:
193  // default is week ?!
194  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
195  $end_date->increment(IL_CAL_WEEK, 1);
196  break;
197  }
198  $this->period_end_day = $end_date->get(IL_CAL_DATE);
199  $schedule->setPeriod($this->seed, $end_date);
200 
201  //}
202  /*else
203  {
204  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_PD_UPCOMING);
205  }*/
206 
207  break;
208  case self::CAL_PRESENTATION_DAY:
209  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_DAY, $user, true);
210  break;
211  case self::CAL_PRESENTATION_WEEK:
212  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
213  break;
214  case self::CAL_PRESENTATION_MONTH:
215  // if we put the user and true in the call method we will get only events and
216  // files from the current month. Not from 6 days before and after.
217  $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_MONTH);
218  break;
219  }
220 
221  $schedule->addSubitemCalendars(true);
222  $schedule->calculate();
223  return $schedule->getScheduledEvents();
224  }
225 
231  public function getDatesForItem($item): array
232  {
233  $start = $item["dstart"];
234  $end = $item["dend"];
235  if ($item["fullday"]) {
236  $start = new ilDate($start, IL_CAL_UNIX);
237  $end = new ilDate($end, IL_CAL_UNIX);
238  } else {
239  $start = new ilDateTime($start, IL_CAL_UNIX);
240  $end = new ilDateTime($end, IL_CAL_UNIX);
241  }
242  return array("start" => $start, "end" => $end);
243  }
244 
249  public function getModalForApp(): void
250  {
251  // set return class
252  $this->ctrl->setReturn($this, '');
253 
254  // @todo: this needs optimization
255  $events = $this->getEvents();
256 
257  //item => array containing ilcalendary object, dstart of the event , dend etc.
258  foreach ($events as $item) {
259  if ($item["event"]->getEntryId() == $this->initAppointmentIdFromQuery() && $item['dstart'] == $this->initInitialDateTimeFromQuery()) {
260  $dates = $this->getDatesForItem($item);
261  // content of modal
262  $next_gui = ilCalendarAppointmentPresentationGUI::_getInstance($this->seed, $item);
263  $content = $this->ctrl->getHTML($next_gui);
264 
265  //plugins can change the modal title.
266  $modal_title = ilDatePresentation::formatPeriod($dates["start"], $dates["end"]);
267  $modal_title = $this->getModalTitleByPlugins($modal_title);
268  $modal = $this->ui_factory->modal()->roundtrip(
269  $modal_title,
270  $this->ui_factory->legacy($content)
271  )->withCancelButtonLabel($this->lng->txt("close"));
272  echo $this->ui_renderer->renderAsync($modal);
273  }
274  }
275  exit();
276  }
277 
284  public function getAppointmentShyButton(
285  ilCalendarEntry $a_calendar_entry,
286  string $a_dstart,
287  string $a_title_forced = ""
288  ): string {
289  $this->ctrl->setParameter($this, "app_id", $a_calendar_entry->getEntryId());
290 
291  if ($this->getConsultationHoursUserId()) {
292  $this->ctrl->setParameter($this, 'chuid', $this->getConsultationHoursUserId());
293  }
294  $this->ctrl->setParameter($this, 'dt', $a_dstart);
295  $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
296  $url = $this->ctrl->getLinkTarget($this, "getModalForApp", "", true, false);
297  $this->ctrl->setParameter($this, "app_id", $this->initAppointmentIdFromQuery());
298  $this->ctrl->setParameter($this, "dt", $this->initInitialDateTimeFromQuery());
299  $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
300 
301  $modal = $this->ui_factory->modal()->roundtrip('', [])->withAsyncRenderUrl($url);
302 
303  //Day view presents the titles with the full length.(agenda:class.ilCalendarAgendaListGUI.php)
304  if ($this->presentation_type == self::CAL_PRESENTATION_DAY) {
305  $title = ($a_title_forced == "") ? $a_calendar_entry->getPresentationTitle(false) : $a_title_forced;
306  } else {
307  $title = ($a_title_forced == "") ? $a_calendar_entry->getPresentationTitle() : $a_title_forced;
308  }
309 
310  // aria label including start time
311  $start_time = '';
312  switch ($this->user_settings->getTimeFormat()) {
314  $start_time = $a_calendar_entry->getStart()->get(IL_CAL_FKT_DATE, 'H:i', $this->timezone);
315  break;
316 
318  $start_time = $a_calendar_entry->getStart()->get(IL_CAL_FKT_DATE, 'h:ia', $this->timezone);
319  break;
320  }
321  $aria_label = $start_time . ' - ' . $title;
322 
323  $comps = [$this->ui_factory->button()->shy($title, "#")->withAriaLabel($aria_label)->withOnClick($modal->getShowSignal()), $modal];
324  return $this->ui_renderer->render($comps);
325  }
326 
331  public function getActivePlugins(string $a_slot_id): Iterator
332  {
333  return $this->component_factory->getActivePluginsInSlot($a_slot_id);
334  }
335 
336  public function getModalTitleByPlugins(string $a_current_title): string
337  {
338  $modal_title = $a_current_title;
339  //demo of plugin execution.
340  //"capm" is the plugin slot id for Appointment presentations (modals)
341  foreach ($this->getActivePlugins("capm") as $plugin) {
342  $modal_title = ($new_title = $plugin->editModalTitle($a_current_title)) ? $new_title : $a_current_title;
343  }
344  return $modal_title;
345  }
346 
354  public function getContentByPlugins(
355  ilCalendarEntry $a_cal_entry,
356  int $a_start_date,
357  string $a_content,
358  ilTemplate $a_tpl
359  ): string {
360  $content = $a_content;
361 
362  //"capg" is the plugin slot id for AppointmentCustomGrid
363  foreach ($this->getActivePlugins("capg") as $plugin) {
364  $plugin->setAppointment($a_cal_entry, new ilDateTime($a_start_date, IL_CAL_UNIX));
365 
366  if ($new_title = $plugin->editShyButtonTitle()) {
367  $a_tpl->setVariable(
368  'EVENT_CONTENT',
369  $this->getAppointmentShyButton($a_cal_entry, (string) $a_start_date, $new_title)
370  );
371  }
372 
373  if ($glyph = $plugin->addGlyph()) {
374  $a_tpl->setVariable('EXTRA_GLYPH_BY_PLUGIN', $glyph);
375  }
376 
377  if ($more_content = $plugin->addExtraContent()) {
378  $a_tpl->setVariable('EXTRA_CONTENT_BY_PLUGIN', $more_content);
379  }
380 
381  $a_tpl->parseCurrentBlock();
382  $html_content = $a_tpl->get();
383 
384  if ($new_content = $plugin->replaceContent($html_content)) {
385  $content = $new_content;
386  }
387  }
388  if ($content == $a_content) {
389  return '';
390  }
391  return $content;
392  }
393 
397  public function addToolbarFileDownload(): void
398  {
399  $settings = ilCalendarSettings::_getInstance();
400 
401  if ($settings->isBatchFileDownloadsEnabled()) {
402  $num_events = 0;
403  if ($this->presentation_type == self::CAL_PRESENTATION_AGENDA_LIST) {
404  $num_events = $this->countEventsInView();
405  }
406  if ($this->view_with_appointments || $num_events) {
407  $toolbar = $this->toolbar;
409  $lng = $this->lng;
410  $ctrl = $this->ctrl;
411 
412  // file download
413  $add_button = $f->button()->standard(
414  $this->lng->txt("cal_download_files"),
415  $ctrl->getLinkTarget($this, "downloadFiles")
416  );
417  $toolbar->addSeparator();
418  $toolbar->addComponent($add_button);
419  }
420  }
421  }
422 
426  public function downloadFiles(): void
427  {
428  $download_job = new ilDownloadFilesBackgroundTask($this->user->getId());
429  $download_job->setBucketTitle($this->getBucketTitle());
430  $download_job->setEvents($this->getEvents());
431  if ($download_job->run()) {
432  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('cal_download_files_started'), true);
433  }
434  $this->ctrl->redirect($this);
435  }
436 
440  public function getBucketTitle(): string
441  {
442  //definition of bucket titles here: 21365
443  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
444  $bucket_title = $this->lng->txt("cal_calendar_download");
445 
446  switch ($this->presentation_type) {
447  case self::CAL_PRESENTATION_DAY:
448  $bucket_title .= " " . $this->seed->get(IL_CAL_DATE);
449  break;
450  case self::CAL_PRESENTATION_WEEK:
451  $weekday_list = ilCalendarUtil::_buildWeekDayList($this->seed, $user_settings->getWeekStart())->get();
452  $start = current($weekday_list);
453  $char = strtolower(mb_substr($this->lng->txt("week"), 0, 1));
454  $bucket_title .= " " . $start->get(IL_CAL_DATE) . " 1$char";
455  break;
456  case self::CAL_PRESENTATION_MONTH:
457  $year_month = $this->seed->get(IL_CAL_FKT_DATE, 'Y-m', 'UTC');
458  $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
459  $bucket_title .= " " . $year_month . " 1" . $char;
460  break;
461  case self::CAL_PRESENTATION_AGENDA_LIST:
462  $bucket_title .= " " . $this->seed->get(IL_CAL_DATE);
463  $get_list_option = intval($this->user->getPref('cal_list_view'));
464  switch ($get_list_option) {
466  break;
468  $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
469  $bucket_title .= " 1$char";
470  break;
472  $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
473  $bucket_title .= " 6$char";
474  break;
476  default:
477  $char = strtolower(mb_substr($this->lng->txt("week"), 0, 1));
478  $bucket_title .= " 1$char";
479  break;
480  }
481  }
482  return $bucket_title;
483  }
484 
489  public function countEventsInView(): int
490  {
491  $start = $this->seed;
492  $end = clone $start;
493  $get_list_option = ilCalendarAgendaListGUI::getPeriod();
494  switch ($get_list_option) {
496  break;
498  $end->increment(IL_CAL_MONTH, 1);
499  break;
501  $end->increment(IL_CAL_MONTH, 6);
502  break;
504  default:
505  $end->increment(IL_CAL_DAY, 7);
506  break;
507  }
508  $events = $this->getEvents();
509  $num_events = 0;
510  foreach ($events as $event) {
511  $event_start = $event['event']->getStart()->get(IL_CAL_DATE);
512 
513  if ($event_start >= $start->get(IL_CAL_DATE) && $event_start <= $end->get(IL_CAL_DATE)) {
514  $num_events++;
515  }
516  }
517  return $num_events;
518  }
519 }
addToolbarFileDownload()
Add download link to toolbar.
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static _getInstance(ilDate $seed, array $a_app)
get singleton instance
getLinkTarget(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
Returns a link target for the given information.
getModalForApp()
Get modal for appointment (see similar code in ilCalendarBlockGUI)
getActivePlugins(string $a_slot_id)
get(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilDate $seed, int $presentation_type)
getBucketTitle()
get proper label to add in the background task popover
getContentByPlugins(ilCalendarEntry $a_cal_entry, int $a_start_date, string $a_content, ilTemplate $a_tpl)
addComponent(\ILIAS\UI\Component\Component $a_comp)
static _buildWeekDayList(ilDate $a_day, int $a_weekstart)
build week day list public
const IL_CAL_MONTH
$url
Definition: shib_logout.php:63
setConsulationHoursUserId(int $a_user_id)
const IL_CAL_UNIX
const IL_CAL_WEEK
countEventsInView()
get the events starting between 2 dates based in seed + view options.
static _getInstanceByUserId(int $a_user_id)
getPresentationTitle(bool $a_shorten=true)
const IL_CAL_DAY
static getPeriod()
needed in CalendarInboxGUI to get events using a proper period.
static http()
Fetches the global http state from ILIAS.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
downloadFiles()
Download files related to the appointments showed in the current calendar view (day,week,month,list).
ilGlobalTemplateInterface $main_tpl
This is how the factory for UI elements looks.
Definition: Factory.php:37
ILIAS DI UIServices $ui
global $DIC
Definition: shib_login.php:25
const IL_CAL_FKT_DATE
getModalTitleByPlugins(string $a_current_title)
ilComponentFactory $component_factory
const IL_CAL_DATE
getStart()
Get start of date period.
static initjQuery(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ilObjUser $user=null)
Format a period of two dates Shows: 14.
initialize(int $a_calendar_presentation_type)
getAppointmentShyButton(ilCalendarEntry $a_calendar_entry, string $a_dstart, string $a_title_forced="")
Represents a list of calendar appointments (including recurring events) for a specific user in a give...