aboutsummaryrefslogtreecommitdiff
path: root/lib/models/calendar/ScheduleEntry.php
blob: c9eb13c7b00d0b970269533f846814606b616397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
 * ScheduleEntry.php - Model class for regular dates
 * in the schedule view that are not bound to a course.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       6.0
 *
 * @property int $id database column
 * @property int $start_time database column
 * @property int $end_time database column
 * @property int $dow database column
 * @property string $label database column
 * @property string|null $content database column
 * @property string $user_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property int $colour_id database column
 * @property User $user belongs_to User
 */
class ScheduleEntry extends SimpleORMap implements Event
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'schedule_entries';
        $config['belongs_to']['user'] = [
            'class_name'  => User::class,
            'foreign_key' => 'user_id'
        ];
        parent::configure($config);
    }

    /**
     * A helper method to set the content of the start attribute by a formatted date
     * in the format HH:mm.
     *
     * @param string $formatted_start The formatted date in the format HH:mm.
     */
    public function setFormattedStart(string $formatted_start) : void
    {
        $this->start_time = str_replace(':', '', $formatted_start);
    }

    /**
     * A helper method to set the content of the end attribute by a formatted date
     * in the format HH:mm.
     *
     * @param string $formatted_end The formatted date in the format HH:mm.
     */
    public function setFormattedEnd(string $formatted_end) : void
    {
        $this->end_time = str_replace(':', '', $formatted_end);
    }

    /**
     * Formats the start time for human-readable output.
     *
     * @return string The start time in the format HH:mm or an empty string in case
     *      the format stored in the start attribute is not supported.
     */
    public function getFormattedStart() : string
    {
        $padded_start_time = str_pad($this->start_time, 4, '0', STR_PAD_LEFT);
        return substr($padded_start_time, 0, 2) . ':' . substr($padded_start_time, 2, 2);
    }

    /**
     * Formats the end time for human-readable output.
     *
     * @return string The end time in the format HH:mm or an empty string in case
     *     the format stored in the end attribute is not supported.
     */
    public function getFormattedEnd() : string
    {
        $padded_end_time = str_pad($this->end_time, 4, '0', STR_PAD_LEFT);
        return substr($padded_end_time, 0, 2) . ':' . substr($padded_end_time, 2, 2);
    }

    /**
     * @inheritDoc
     */
    public static function getEvents(DateTime $begin, DateTime $end, string $range_id): array
    {
        return self::findBySQL(
            "`user_id` = :range_id
            AND `start` < :end AND `end` > :start
            AND `day` >= :start_day AND day <= :end_day",
            [
                'range_id'  => $range_id,
                'start'     => $begin->format('Hi'),
                'end'       => $end->format('Hi'),
                'start_day' => $begin->format('N'),
                'end_day'   => $end->format('N')
            ]
        );
    }

    /**
     * @inheritDoc
     */
    public function getObjectId(): string
    {
        return $this->id;
    }

    /**
     * @inheritDoc
     */
    public function getPrimaryObjectID(): string
    {
        return $this->user_id;
    }

    /**
     * @inheritDoc
     */
    public function getObjectClass(): string
    {
        return self::class;
    }

    /**
     * @inheritDoc
     */
    public function getTitle(): string
    {
        return $this->label;
    }

    /**
     * @inheritDoc
     */
    public function getBegin(): DateTime
    {
        //Map the entry to the current week:
        $date = new DateTime();
        $date->setTimestamp(strtotime('midnight this week'));
        if ($this->dow > 1) {
            $days_to_add = $this->dow - 1;
            $date = $date->add(new DateInterval(sprintf('P%dD', $days_to_add)));
        }
        $time_parts = explode(':', $this->getFormattedStart());
        $date->setTime($time_parts[0], $time_parts[1]);
        return $date;
    }

    /**
     * @inheritDoc
     */
    public function getEnd(): DateTime
    {
        //Map the entry to the current week:
        $date = new DateTime();
        $date->setTimestamp(strtotime('midnight this week'));
        if ($this->dow > 1) {
            $days_to_add = $this->dow - 1;
            $date = $date->add(new DateInterval(sprintf('P%dD', $days_to_add)));
        }
        $time_parts = explode(':', $this->getFormattedEnd());
        $date->setTime($time_parts[0], $time_parts[1]);
        return $date;
    }

    /**
     * @inheritDoc
     */
    public function getDuration(): DateInterval
    {
        return $this->getEnd()->diff($this->getBegin());
    }

    /**
     * @inheritDoc
     */
    public function getLocation(): string
    {
        //No location supported.
        return '';
    }

    /**
     * @inheritDoc
     */
    public function getUniqueId(): string
    {
        return implode('_', [
            Config::get()->STUDIP_INSTALLATION_ID,
            self::class,
            $this->id,
        ]);
    }

    /**
     * @inheritDoc
     */
    public function getDescription(): string
    {
        return $this->getValue('content');
    }

    /**
     * @inheritDoc
     */
    public function getAdditionalDescriptions(): array
    {
        //No additional description supported.
        return [];
    }

    /**
     * @inheritDoc
     */
    public function isAllDayEvent(): bool
    {
        return $this->start_time === '000' && $this->end_time === '2359';
    }

    /**
     * @inheritDoc
     */
    public function isWritable(string $user_id): bool
    {
        //Only the owner and root may edit the entry:
        return $user_id === $this->user_id
            || $GLOBALS['perm']->have_perm('root', $user_id);
    }

    /**
     * @inheritDoc
     */
    public function getCreationDate(): DateTime
    {
        $date = new DateTime();
        $date->setTimestamp($this->mkdate);
        return $date;
    }

    /**
     * @inheritDoc
     */
    public function getModificationDate(): DateTime
    {
        $date = new DateTime();
        $date->setTimestamp($this->chdate);
        return $date;
    }

    /**
     * @inheritDoc
     */
    public function getImportDate(): DateTime
    {
        //The import date is not supported. Use mkdate instead.
        $date = new DateTime();
        $date->setTimestamp($this->mkdate);
        return $date;
    }

    /**
     * @inheritDoc
     */
    public function getAuthor(): ?User
    {
        return $this->user;
    }

    /**
     * @inheritDoc
     */
    public function getEditor(): ?User
    {
        return $this->user;
    }

    /**
     * @inheritDoc
     */
    public function toEventData(string $user_id): \Studip\Calendar\EventData
    {
        $title = $this->label;

        $description = $this->getDescription();
        if ($description) {
            if ($this->label) {
                $title = $this->label . ': ' . $description;
            } else {
                $title = $description;
            }
        }
        $event_classes = ['schedule-entry'];
        return new \Studip\Calendar\EventData(
            $this->getBegin(),
            $this->getEnd(),
            $title,
            $event_classes,
            $GLOBALS['PERS_TERMIN_KAT'][$this->colour_id]['fgcolor'] ?? '#000000',
            $GLOBALS['PERS_TERMIN_KAT'][$this->colour_id]['bgcolor'] ?? '#ffffff',
            $this->isWritable($user_id),
            self::class,
            $this->id,
            User::class,
            $this->user_id,
            User::class,
            $this->user_id,
            [
                'show' => URLHelper::getURL('dispatch.php/calendar/schedule/entry/' . $this->id)
            ],
            [
                'resize' => URLHelper::getURL('dispatch.php/calendar/schedule/move_entry/' . $this->id),
                'move'   => URLHelper::getURL('dispatch.php/calendar/schedule/move_entry/' . $this->id)
            ],
            '',
            $GLOBALS['PERS_TERMIN_KAT'][$this->colour_id]['border_color'] ?? '#000000',
            $this->isAllDayEvent()
        );
    }

    /**
     * Creates a string representation of the schedule entry.
     *
     * @return string A human-readable string describing the schedule entry.
     */
    public function toString() : string
    {
        return studip_interpolate(
            _('Termin jeden %{dow} von %{start_time} bis %{end_time} Uhr'),
            [
                'dow'        => getWeekday($this->dow % 7, false),
                'start_time' => $this->getFormattedStart(),
                'end_time'   => $this->getFormattedEnd()
            ]
        );
    }
}