ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarEntry.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 public const TRANSLATION_NONE = 0;
30 public const TRANSLATION_SYSTEM = 1;
31
32 protected ilLogger $log;
33 protected ilDBInterface $db;
34 protected ilLanguage $lng;
36
37 protected int $entry_id = 0;
38 protected ?ilDateTime $last_update = null;
39 protected string $title = '';
40 protected string $presentation_style = '';
41 protected string $subtitle = '';
42 protected string $description = '';
43 protected string $location = '';
44 protected string $further_informations = '';
45 protected ?ilDateTime $start = null;
46 protected bool $fullday = false;
47 protected ?ilDateTime $end = null;
48 protected bool $is_auto_generated = false;
49 protected int $context_id = 0;
50 protected string $context_info = '';
52 protected bool $notification = false;
53
54 public function __construct(int $a_id = 0)
55 {
56 global $DIC;
57
58 $this->log = $DIC->logger()->cal();
59 $this->lng = $DIC->language();
60 $this->db = $DIC->database();
61 $this->error = $DIC['ilErr'];
62 $this->entry_id = $a_id;
63 if ($this->entry_id > 0) {
64 $this->read();
65 }
66 }
67
71 public function __clone()
72 {
73 $this->entry_id = 0;
74 }
75
76 public static function _delete(int $a_entry_id): void
77 {
78 global $DIC;
79
80 $ilDB = $DIC['ilDB'];
82
83 $query = "DELETE FROM cal_entries " .
84 "WHERE cal_id = " . $ilDB->quote($a_entry_id, 'integer') . " ";
85 $res = $ilDB->manipulate($query);
86 }
87
88 public function setContextInfo(string $a_info): void
89 {
90 $this->context_info = $a_info;
91 }
92
93 public function getContextInfo(): string
94 {
96 }
97
98 public function getEntryId(): int
99 {
100 return $this->entry_id;
101 }
102
103 public function getLastUpdate(): ilDateTime
104 {
105 return $this->last_update ?: new ilDateTime(time(), IL_CAL_UNIX);
106 }
107
108 public function setLastUpdate(ilDateTime $a_date): void
109 {
110 $this->last_update = $a_date;
111 }
112
113 public function getStart(): ?ilDateTime
114 {
115 return $this->start;
116 }
117
118 public function setStart(?ilDateTime $a_start): void
119 {
120 $this->start = $a_start;
121 }
122
123 public function getEnd(): ?ilDateTime
124 {
125 return $this->end;
126 }
127
128 public function setEnd(?ilDateTime $a_end): void
129 {
130 $this->end = $a_end;
131 }
132
133 public function setTitle(string $a_title): void
134 {
135 $this->title = $a_title;
136 }
137
138 public function getTitle(): string
139 {
140 return $this->title;
141 }
142
143 public function getPresentationTitle(bool $a_shorten = true): string
144 {
146 $title = $this->getTitle();
147 } elseif (strlen($this->getSubtitle())) {
148 // parse dynamic title?
149 if (preg_match("/#([a-z]+)#/", $this->getSubtitle(), $matches)) {
150 $subtitle = $this->parseDynamicTitle($matches[1]);
151 } else {
152 $subtitle = $this->lng->txt($this->getSubtitle());
153 }
154 $title = $this->getTitle() .
155 (strlen($subtitle)
156 ? ' (' . $subtitle . ')'
157 : '');
158 } else {
159 $title = $this->lng->txt($this->getTitle());
160 }
161
162 if ($a_shorten) {
164 }
165 return $title;
166 }
167
168 protected function parseDynamicTitle(string $a_type): string
169 {
170 $title = $style = "";
171 switch ($a_type) {
172 case "consultationhour":
173 $entry = new ilBookingEntry($this->getContextId());
174 if ($entry) {
175 if ($entry->isOwner()) {
176 $max = $entry->getNumberOfBookings();
177 $current = $entry->getCurrentNumberOfBookings($this->getEntryId());
178 $free = (($max - $current) >= 0 ? ($max - $current) : 0);
179 if (!$current) {
180 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
181 $title = $this->lng->txt('cal_book_free');
182 } elseif ($current >= $max) {
183 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
184 $title = $this->lng->txt('cal_booked_out');
185 } else {
186 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: yellow';
187 $title = sprintf($this->lng->txt('cal_ch_booking_num_free_short'), $free);
188 }
189 } else {
191 $entry->getObjId(),
192 $this->getContextId(),
193 $this->getStart()
194 );
195 if ($apps === []) {
196 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
197 $title = $this->lng->txt('cal_booked_out');
198 } else {
199 $orig_event = $apps[0];
200 $max = $entry->getNumberOfBookings();
201 $current = $entry->getCurrentNumberOfBookings($this->getEntryId());
202 if ($entry->hasBooked($orig_event)) {
203 $title = $this->lng->txt('cal_date_booked');
204 } elseif ($current >= $max) {
205 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: red';
206 $title = $this->lng->txt('cal_booked_out');
207 } else {
208 $style = ';border-left-width: 5px; border-left-style: solid; border-left-color: green';
209 $title = $this->lng->txt('cal_book_free');
210 }
211 }
212 }
213 }
214 break;
215 }
216 if (strlen($style)) {
217 $this->presentation_style = $style;
218 }
219
220 return $title;
221 }
222
223 public function getPresentationStyle(): string
224 {
226 }
227
233 public function setSubtitle(string $a_subtitle): void
234 {
235 $this->subtitle = $a_subtitle;
236 }
237
238 public function getSubtitle(): string
239 {
240 return $this->subtitle;
241 }
242
243 public function setDescription(string $a_description): void
244 {
245 $this->description = $a_description;
246 }
247
248 public function getDescription(): string
249 {
250 return $this->description;
251 }
252
253 public function setLocation(string $a_location): void
254 {
255 $this->location = $a_location;
256 }
257
258 public function getLocation(): string
259 {
260 return $this->location;
261 }
262
263 public function setFurtherInformations(string $a_informations): void
264 {
265 $this->further_informations = $a_informations;
266 }
267
268 public function getFurtherInformations(): string
269 {
271 }
272
278 public function setFullday(bool $a_fullday): void
279 {
280 $this->fullday = $a_fullday;
281 }
282
283 public function isFullday(): bool
284 {
285 return $this->fullday;
286 }
287
288 public function isAutoGenerated(): bool
289 {
291 }
292
293 public function setAutoGenerated(bool $a_status): void
294 {
295 $this->is_auto_generated = $a_status;
296 }
297
298 public function setContextId(int $a_context_id): void
299 {
300 $this->context_id = $a_context_id;
301 }
302
303 public function getContextId(): int
304 {
305 return $this->context_id;
306 }
307
308 public function setTranslationType(int $a_type): void
309 {
310 $this->translation_type = $a_type;
311 }
312
313 public function getTranslationType(): int
314 {
316 }
317
318 public function enableNotification(bool $a_status): void
319 {
320 $this->notification = $a_status;
321 }
322
323 public function isNotificationEnabled(): bool
324 {
325 return $this->notification;
326 }
327
328 public function update(): void
329 {
330 $now = new ilDateTime(time(), IL_CAL_UNIX);
331 $utc_timestamp = $now->get(IL_CAL_DATETIME, '', ilTimeZone::UTC);
332 $query = "UPDATE cal_entries " .
333 /*
334 * The title needs to be truncated to fit into the table column. This is a pretty
335 * brute force method for doing so, but right now I can't find a better place for it.
336 */
337 "SET title = " . $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
338 "last_update = " . $this->db->quote($utc_timestamp, 'timestamp') . ", " .
339 "subtitle = " . $this->db->quote($this->getSubtitle(), 'text') . ", " .
340 "description = " . $this->db->quote($this->getDescription(), 'text') . ", " .
341 "location = " . $this->db->quote($this->getLocation(), 'text') . ", " .
342 "fullday = " . $this->db->quote($this->isFullday() ? 1 : 0, 'integer') . ", " .
343 "starta = " . $this->db->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
344 "enda = " . $this->db->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
345 "informations = " . $this->db->quote($this->getFurtherInformations(), 'text') . ", " .
346 "auto_generated = " . $this->db->quote($this->isAutoGenerated(), 'integer') . ", " .
347 "translation_type = " . $this->db->quote($this->getTranslationType(), 'integer') . ", " .
348 "context_id = " . $this->db->quote($this->getContextId(), 'integer') . ", " .
349 'context_info = ' . $this->db->quote($this->getContextInfo(), 'text') . ', ' .
350 'notification = ' . $this->db->quote($this->isNotificationEnabled() ? 1 : 0, 'integer') . ' ' .
351 "WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
352 $res = $this->db->manipulate($query);
353 }
354
355 public function save(): void
356 {
357 $next_id = $this->db->nextId('cal_entries');
358 $now = new ilDateTime(time(), IL_CAL_UNIX);
359 $utc_timestamp = $now->get(IL_CAL_DATETIME, '', ilTimeZone::UTC);
360
361 $query = "INSERT INTO cal_entries (cal_id,title,last_update,subtitle,description,location,fullday,starta,enda, " .
362 "informations,auto_generated,context_id,context_info,translation_type, notification) " .
363 "VALUES( " .
364 $this->db->quote($next_id, 'integer') . ", " .
365 /*
366 * The title needs to be truncated to fit into the table column. This is a pretty
367 * brute force method for doing so, but right now I can't find a better place for it.
368 */
369 $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
370 $this->db->quote($utc_timestamp, 'timestamp') . ", " .
371 $this->db->quote($this->getSubtitle(), 'text') . ", " .
372 $this->db->quote($this->getDescription(), 'text') . ", " .
373 $this->db->quote($this->getLocation(), 'text') . ", " .
374 $this->db->quote($this->isFullday() ? 1 : 0, 'integer') . ", " .
375 $this->db->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
376 $this->db->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
377 $this->db->quote($this->getFurtherInformations(), 'text') . ", " .
378 $this->db->quote($this->isAutoGenerated(), 'integer') . ", " .
379 $this->db->quote($this->getContextId(), 'integer') . ", " .
380 $this->db->quote($this->getContextInfo(), 'text') . ', ' .
381 $this->db->quote($this->getTranslationType(), 'integer') . ", " .
382 $this->db->quote($this->isNotificationEnabled() ? 1 : 0, 'integer') . ' ' .
383 ")";
384 $res = $this->db->manipulate($query);
385
386 $this->entry_id = $next_id;
387 }
388
389 public function delete(): void
390 {
392
393 $query = "DELETE FROM cal_entries " .
394 "WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
395 $res = $this->db->manipulate($query);
396
398 }
399
400 public function validate(): bool
401 {
402 $success = true;
403 $this->error->setMessage('');
404 if (!strlen($this->getTitle())) {
405 $success = false;
406 $this->error->appendMessage($this->lng->txt('err_missing_title'));
407 }
408 if (!$this->getStart() || !$this->getEnd()) {
409 $success = false;
410 } elseif (ilDateTime::_before($this->getEnd(), $this->getStart(), '')) {
411 $success = false;
412 $this->error->appendMessage($this->lng->txt('err_end_before_start'));
413 }
414 return $success;
415 }
416
417 protected function read(): void
418 {
419 $query = "SELECT * FROM cal_entries WHERE cal_id = " . $this->db->quote($this->getEntryId(), 'integer') . " ";
420 $res = $this->db->query($query);
421 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
422 $this->setLastUpdate(new ilDateTime((string) $row->last_update, IL_CAL_DATETIME, 'UTC'));
423 $this->setTitle((string) $row->title);
424 $this->setSubtitle((string) $row->subtitle);
425 $this->setDescription((string) $row->description);
426 $this->setLocation((string) $row->location);
427 $this->setFurtherInformations((string) $row->informations);
428 $this->setFullday((bool) $row->fullday);
429 $this->setAutoGenerated((bool) $row->auto_generated);
430 $this->setContextId((int) $row->context_id);
431 $this->setContextInfo((string) $row->context_info);
432 $this->setTranslationType((int) $row->translation_type);
433 $this->enableNotification((bool) $row->notification);
434
435 if ($this->isFullday()) {
436 $this->start = new ilDate((string) $row->starta, IL_CAL_DATETIME);
437 $this->end = new ilDate((string) $row->enda, IL_CAL_DATETIME);
438 } else {
439 $this->start = new ilDateTime((string) $row->starta, IL_CAL_DATETIME, 'UTC');
440 $this->end = new ilDateTime((string) $row->enda, IL_CAL_DATETIME, 'UTC');
441 }
442 }
443 }
444
445 public function appointmentToMailString(ilLanguage $lng): string
446 {
447 $body = $lng->txt('cal_details');
448 $body .= "\n\n";
449 $body .= $lng->txt('title') . ': ' . $this->getTitle() . "\n";
450
452 $body .= $lng->txt('date') . ': ' . ilDatePresentation::formatPeriod($this->getStart(), $this->getEnd()) . "\n";
454
455 if (strlen($this->getLocation())) {
456 $body .= $lng->txt('cal_where') . ': ' . $this->getLocation() . "\n";
457 }
458
459 if (strlen($this->getDescription())) {
460 $body .= $lng->txt('description') . ': ' . $this->getDescription() . "\n";
461 }
462 return $body;
463 }
464}
const IL_CAL_UNIX
const IL_CAL_DATETIME
error(string $a_errmsg)
Booking definition.
static _deleteByAppointmentId(int $a_app_id)
Delete appointment assignment.
Model for a calendar entry.
static _delete(int $a_entry_id)
setEnd(?ilDateTime $a_end)
setTranslationType(int $a_type)
setStart(?ilDateTime $a_start)
ilErrorHandling $error
setLastUpdate(ilDateTime $a_date)
setDescription(string $a_description)
setFullday(bool $a_fullday)
set fullday event Fullday events do not change their time in different timezones.
setLocation(string $a_location)
enableNotification(bool $a_status)
getEnd()
Get end of period.
setSubtitle(string $a_subtitle)
set subtitle Used for automatic generated appointments.
getStart()
Get start of date period.
isFullday()
is event a fullday period
__clone()
clone instance
appointmentToMailString(ilLanguage $lng)
parseDynamicTitle(string $a_type)
setFurtherInformations(string $a_informations)
setAutoGenerated(bool $a_status)
getPresentationTitle(bool $a_shorten=true)
setContextInfo(string $a_info)
setContextId(int $a_context_id)
setTitle(string $a_title)
static _delete(int $a_cal_id)
static getAppointmentIds(int $a_user_id, ?int $a_context_id=null, ?ilDateTime $a_start=null, ?int $a_type=null, bool $a_check_owner=true)
static setUseRelativeDates(bool $a_status)
set use relative dates
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
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
Class for single dates.
Error Handling & global info handling.
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Component logger with individual log levels by component id.
static shortenWords(string $a_str, int $a_len=30, bool $a_dots=true)
Ensure that the maximum word lenght within a text is not longer than $a_len.
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26