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