ILIAS  release_8 Revision v8.24
class.ilCalendarViewGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
25use ILIAS\Refinery\Factory as RefineryFactory;
26use ILIAS\HTTP\Services as HttpServices;
27
34{
36 public const CAL_PRESENTATION_DAY = 1;
37 public const CAL_PRESENTATION_WEEK = 2;
38 public const CAL_PRESENTATION_MONTH = 3;
40
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
51 protected ilLogger $logger;
52 protected \ILIAS\DI\UIServices $ui;
53 protected ilLanguage $lng;
54 protected ilObjUser $user;
55 protected ?ilTemplate $tpl;
59 protected RefineryFactory $refinery;
60 protected HttpServices $http;
61
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
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('./Services/Calendar/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) {
166
167 //if($this->period_end_day == "")
168 //{
169 $this->period = ilCalendarAgendaListGUI::getPeriod();
170
171 $end_date = clone $this->seed;
172 switch ($this->period) {
174 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_DAY, $user, true);
175 $end_date->increment(IL_CAL_DAY, 1);
176 break;
177
179 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
180 $end_date->increment(IL_CAL_WEEK, 1);
181 break;
182
184 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_MONTH, $user, true);
185 $end_date->increment(IL_CAL_MONTH, 1);
186 break;
187
189 $schedule = new ilCalendarSchedule(
190 $this->seed,
192 $user,
193 true
194 );
195 $end_date->increment(IL_CAL_MONTH, 6);
196 break;
197 default:
198 // default is week ?!
199 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
200 $end_date->increment(IL_CAL_WEEK, 1);
201 break;
202 }
203 $this->period_end_day = $end_date->get(IL_CAL_DATE);
204 $schedule->setPeriod($this->seed, $end_date);
205
206 //}
207 /*else
208 {
209 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_PD_UPCOMING);
210 }*/
211
212 break;
214 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_DAY, $user, true);
215 break;
217 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_WEEK, $user, true);
218 break;
220 // if we put the user and true in the call method we will get only events and
221 // files from the current month. Not from 6 days before and after.
222 $schedule = new ilCalendarSchedule($this->seed, ilCalendarSchedule::TYPE_MONTH);
223 break;
224 }
225
226 $schedule->addSubitemCalendars(true);
227 $schedule->calculate();
228 return $schedule->getScheduledEvents();
229 }
230
236 public function getDatesForItem($item): array
237 {
238 $start = $item["dstart"];
239 $end = $item["dend"];
240 if ($item["fullday"]) {
241 $start = new ilDate($start, IL_CAL_UNIX);
242 $end = new ilDate($end, IL_CAL_UNIX);
243 } else {
244 $start = new ilDateTime($start, IL_CAL_UNIX);
245 $end = new ilDateTime($end, IL_CAL_UNIX);
246 }
247 return array("start" => $start, "end" => $end);
248 }
249
254 public function getModalForApp(): void
255 {
256 // set return class
257 $this->ctrl->setReturn($this, '');
258
259 // @todo: this needs optimization
260 $events = $this->getEvents();
261
262 //item => array containing ilcalendary object, dstart of the event , dend etc.
263 foreach ($events as $item) {
264 if ($item["event"]->getEntryId() == $this->initAppointmentIdFromQuery() && $item['dstart'] == $this->initInitialDateTimeFromQuery()) {
265 $dates = $this->getDatesForItem($item);
266 // content of modal
267 $next_gui = ilCalendarAppointmentPresentationGUI::_getInstance($this->seed, $item);
268 $content = $this->ctrl->getHTML($next_gui);
269
270 //plugins can change the modal title.
271 $modal_title = ilDatePresentation::formatPeriod($dates["start"], $dates["end"]);
272 $modal_title = $this->getModalTitleByPlugins($modal_title);
273 $modal = $this->ui_factory->modal()->roundtrip(
274 $modal_title,
275 $this->ui_factory->legacy($content)
276 )->withCancelButtonLabel("close");
277 echo $this->ui_renderer->renderAsync($modal);
278 }
279 }
280 exit();
281 }
282
289 public function getAppointmentShyButton(
290 ilCalendarEntry $a_calendar_entry,
291 string $a_dstart,
292 string $a_title_forced = ""
293 ): string {
294 $this->ctrl->setParameter($this, "app_id", $a_calendar_entry->getEntryId());
295
296 if ($this->getConsultationHoursUserId()) {
297 $this->ctrl->setParameter($this, 'chuid', $this->getConsultationHoursUserId());
298 }
299 $this->ctrl->setParameter($this, 'dt', $a_dstart);
300 $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
301 $url = $this->ctrl->getLinkTarget($this, "getModalForApp", "", true, false);
302 $this->ctrl->setParameter($this, "app_id", $this->initAppointmentIdFromQuery());
303 $this->ctrl->setParameter($this, "dt", $this->initInitialDateTimeFromQuery());
304 $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
305
306 $modal = $this->ui_factory->modal()->roundtrip('', [])->withAsyncRenderUrl($url);
307
308 //Day view presents the titles with the full length.(agenda:class.ilCalendarAgendaListGUI.php)
309 if ($this->presentation_type == self::CAL_PRESENTATION_DAY) {
310 $title = ($a_title_forced == "") ? $a_calendar_entry->getPresentationTitle(false) : $a_title_forced;
311 } else {
312 $title = ($a_title_forced == "") ? $a_calendar_entry->getPresentationTitle() : $a_title_forced;
313 }
314 $comps = [$this->ui_factory->button()->shy($title, "#")->withOnClick($modal->getShowSignal()), $modal];
315 return $this->ui_renderer->render($comps);
316 }
317
322 public function getActivePlugins(string $a_slot_id): Iterator
323 {
324 return $this->component_factory->getActivePluginsInSlot($a_slot_id);
325 }
326
327 public function getModalTitleByPlugins(string $a_current_title): string
328 {
329 $modal_title = $a_current_title;
330 //demo of plugin execution.
331 //"capm" is the plugin slot id for Appointment presentations (modals)
332 foreach ($this->getActivePlugins("capm") as $plugin) {
333 $modal_title = ($new_title = $plugin->editModalTitle($a_current_title)) ? $new_title : $a_current_title;
334 }
335 return $modal_title;
336 }
337
345 public function getContentByPlugins(
346 ilCalendarEntry $a_cal_entry,
347 int $a_start_date,
348 string $a_content,
349 ilTemplate $a_tpl
350 ): string {
351 $content = $a_content;
352
353 //"capg" is the plugin slot id for AppointmentCustomGrid
354 foreach ($this->getActivePlugins("capg") as $plugin) {
355 $plugin->setAppointment($a_cal_entry, new ilDateTime($a_start_date, IL_CAL_UNIX));
356
357 if ($new_title = $plugin->editShyButtonTitle()) {
358 $a_tpl->setVariable(
359 'EVENT_CONTENT',
360 $this->getAppointmentShyButton($a_cal_entry, (string) $a_start_date, $new_title)
361 );
362 }
363
364 if ($glyph = $plugin->addGlyph()) {
365 $a_tpl->setVariable('EXTRA_GLYPH_BY_PLUGIN', $glyph);
366 }
367
368 if ($more_content = $plugin->addExtraContent()) {
369 $a_tpl->setVariable('EXTRA_CONTENT_BY_PLUGIN', $more_content);
370 }
371
372 $a_tpl->parseCurrentBlock();
373 $html_content = $a_tpl->get();
374
375 if ($new_content = $plugin->replaceContent($html_content)) {
376 $content = $new_content;
377 }
378 }
379 if ($content == $a_content) {
380 return '';
381 }
382 return $content;
383 }
384
388 public function addToolbarFileDownload(): void
389 {
391
392 if ($settings->isBatchFileDownloadsEnabled()) {
393 $num_events = 0;
394 if ($this->presentation_type == self::CAL_PRESENTATION_AGENDA_LIST) {
395 $num_events = $this->countEventsInView();
396 }
397 if ($this->view_with_appointments || $num_events) {
398 $toolbar = $this->toolbar;
399 $f = $this->ui_factory;
401 $ctrl = $this->ctrl;
402
403 // file download
404 $add_button = $f->button()->standard(
405 $this->lng->txt("cal_download_files"),
406 $ctrl->getLinkTarget($this, "downloadFiles")
407 );
408 $toolbar->addSeparator();
409 $toolbar->addComponent($add_button);
410 }
411 }
412 }
413
417 public function downloadFiles(): void
418 {
419 $download_job = new ilDownloadFilesBackgroundTask($this->user->getId());
420 $download_job->setBucketTitle($this->getBucketTitle());
421 $download_job->setEvents($this->getEvents());
422 if ($download_job->run()) {
423 $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('cal_download_files_started'), true);
424 }
425 $this->ctrl->redirect($this);
426 }
427
431 public function getBucketTitle(): string
432 {
433 //definition of bucket titles here: 21365
434 $user_settings = ilCalendarUserSettings::_getInstanceByUserId($this->user->getId());
435 $bucket_title = $this->lng->txt("cal_calendar_download");
436
437 switch ($this->presentation_type) {
438 case self::CAL_PRESENTATION_DAY:
439 $bucket_title .= " " . $this->seed->get(IL_CAL_DATE);
440 break;
441 case self::CAL_PRESENTATION_WEEK:
442 $weekday_list = ilCalendarUtil::_buildWeekDayList($this->seed, $user_settings->getWeekStart())->get();
443 $start = current($weekday_list);
444 $char = strtolower(mb_substr($this->lng->txt("week"), 0, 1));
445 $bucket_title .= " " . $start->get(IL_CAL_DATE) . " 1$char";
446 break;
447 case self::CAL_PRESENTATION_MONTH:
448 $year_month = $this->seed->get(IL_CAL_FKT_DATE, 'Y-m', 'UTC');
449 $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
450 $bucket_title .= " " . $year_month . " 1" . $char;
451 break;
452 case self::CAL_PRESENTATION_AGENDA_LIST:
453 $bucket_title .= " " . $this->seed->get(IL_CAL_DATE);
454 $get_list_option = intval($this->user->getPref('cal_list_view'));
455 switch ($get_list_option) {
457 break;
459 $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
460 $bucket_title .= " 1$char";
461 break;
463 $char = strtolower(mb_substr($this->lng->txt("month"), 0, 1));
464 $bucket_title .= " 6$char";
465 break;
467 default:
468 $char = strtolower(mb_substr($this->lng->txt("week"), 0, 1));
469 $bucket_title .= " 1$char";
470 break;
471 }
472 }
473 return $bucket_title;
474 }
475
480 public function countEventsInView(): int
481 {
482 $start = $this->seed;
483 $end = clone $start;
484 $get_list_option = ilCalendarAgendaListGUI::getPeriod();
485 switch ($get_list_option) {
487 break;
489 $end->increment(IL_CAL_MONTH, 1);
490 break;
492 $end->increment(IL_CAL_MONTH, 6);
493 break;
495 default:
496 $end->increment(IL_CAL_DAY, 7);
497 break;
498 }
499 $events = $this->getEvents();
500 $num_events = 0;
501 foreach ($events as $event) {
502 $event_start = $event['event']->getStart()->get(IL_CAL_DATE);
503
504 if ($event_start >= $start->get(IL_CAL_DATE) && $event_start <= $end->get(IL_CAL_DATE)) {
505 $num_events++;
506 }
507 }
508 return $num_events;
509 }
510}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
Provides fluid interface to RBAC services.
Definition: UIServices.php:24
Builds data types.
Definition: Factory.php:21
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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)
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
global $DIC
Definition: feed.php:28
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
exit
Definition: login.php:28
static http()
Fetches the global http state from ILIAS.
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
$url
$lng