Passed
Push — translations ( 6cd971...bddd68 )
by Yannick
35:39 queued 27:44
created

Agenda::move_event()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 5
nop 3
dl 0
loc 36
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Course;
6
use Chamilo\CoreBundle\Framework\Container;
7
use Chamilo\CourseBundle\Entity\CCalendarEvent;
8
use Chamilo\CourseBundle\Entity\CCalendarEventAttachment;
9
use Chamilo\CourseBundle\Entity\CGroup;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
12
use Chamilo\CoreBundle\Component\Utils\ObjectIcon;
13
14
15
/**
16
 * Class Agenda.
17
 *
18
 * @author: Julio Montoya <[email protected]>
19
 */
20
class Agenda
21
{
22
    public $events = [];
23
    /** @var string Current type */
24
    public $type = 'personal';
25
    public $types = ['personal', 'admin', 'course'];
26
    public $sessionId = 0;
27
    public $senderId;
28
    /** @var array */
29
    public $course;
30
    /** @var string */
31
    public $comment;
32
    public $eventStudentPublicationColor;
33
    /** @var array */
34
    private $sessionInfo;
35
    /** @var bool */
36
    private $isAllowedToEdit;
37
38
    private $tbl_global_agenda;
39
    private $tbl_personal_agenda;
40
    private $tbl_course_agenda;
41
    private $table_repeat;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param string $type
47
     * @param int    $senderId  Optional The user sender ID
48
     * @param int    $courseId  Optional. The course ID
49
     * @param int    $sessionId Optional The session ID
50
     */
51
    public function __construct(
52
        $type,
53
        $senderId = 0,
54
        $courseId = 0,
55
        $sessionId = 0
56
    ) {
57
        // Table definitions
58
        $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
59
        $this->table_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
60
61
        $this->setType($type);
62
        $this->setSenderId($senderId ?: api_get_user_id());
63
        $isAllowToEdit = false;
64
65
        switch ($type) {
66
            case 'course':
67
                $sessionId = $sessionId ?: api_get_session_id();
68
                $sessionInfo = api_get_session_info($sessionId);
69
                $this->setSessionId($sessionId);
70
                $this->setSessionInfo($sessionInfo);
71
72
                // Setting the course object if we are in a course
73
                $courseInfo = api_get_course_info_by_id($courseId);
74
                if (!empty($courseInfo)) {
75
                    $this->set_course($courseInfo);
76
                }
77
78
                // Check if teacher/admin rights.
79
                $isAllowToEdit = api_is_allowed_to_edit(false, true);
80
                // Check course setting.
81
                if ('1' === api_get_course_setting('allow_user_edit_agenda')
82
                    && api_is_allowed_in_course()
83
                ) {
84
                    $isAllowToEdit = true;
85
                }
86
87
                $group = api_get_group_entity();
88
                if (!empty($group)) {
89
                    $userHasAccess = GroupManager::userHasAccess(
90
                        api_get_user_id(),
91
                        $group,
92
                        GroupManager::GROUP_TOOL_CALENDAR
93
                    );
94
                    $isTutor = GroupManager::isTutorOfGroup(
95
                        api_get_user_id(),
96
                        $group
97
                    );
98
99
                    $isGroupAccess = $userHasAccess || $isTutor;
100
                    $isAllowToEdit = false;
101
                    if ($isGroupAccess) {
102
                        $isAllowToEdit = true;
103
                    }
104
                }
105
106
                if (!empty($sessionId)) {
107
                    $allowDhrToEdit = ('true' === api_get_setting('agenda.allow_agenda_edit_for_hrm'));
108
                    if ($allowDhrToEdit) {
109
                        $isHrm = SessionManager::isUserSubscribedAsHRM($sessionId, api_get_user_id());
110
                        if ($isHrm) {
111
                            $isAllowToEdit = true;
112
                        }
113
                    }
114
                }
115
                break;
116
            case 'admin':
117
                $isAllowToEdit = api_is_platform_admin();
118
                break;
119
            case 'personal':
120
                $isAllowToEdit = !api_is_anonymous();
121
                break;
122
        }
123
124
        $this->setIsAllowedToEdit($isAllowToEdit);
125
        $this->events = [];
126
        $agendaColors = [
127
                'platform' => 'red', //red
128
                'course' => '#458B00', //green
129
                'group' => '#A0522D', //siena
130
                'session' => '#00496D', // kind of green
131
                'other_session' => '#999', // kind of green
132
                'personal' => 'steel blue', //steel blue
133
                'student_publication' => '#FF8C00', //DarkOrange
134
            ];
135
        $settingAgendaColors = api_get_setting('agenda.agenda_colors', true);
136
        if (is_array($settingAgendaColors)) {
137
            $agendaColors = array_merge($agendaColors, $settingAgendaColors);
138
        }
139
140
        // Event colors
141
        $this->event_platform_color = $agendaColors['platform'];
142
        $this->event_course_color = $agendaColors['course'];
143
        $this->event_group_color = $agendaColors['group'];
144
        $this->event_session_color = $agendaColors['session'];
145
        $this->eventOtherSessionColor = $agendaColors['other_session'];
146
        $this->event_personal_color = $agendaColors['personal'];
147
        $this->eventStudentPublicationColor = $agendaColors['student_publication'];
148
    }
149
150
    /**
151
     * @param int $senderId
152
     */
153
    public function setSenderId($senderId)
154
    {
155
        $this->senderId = (int) $senderId;
156
    }
157
158
    /**
159
     * @return int
160
     */
161
    public function getSenderId()
162
    {
163
        return $this->senderId;
164
    }
165
166
    /**
167
     * @param string $type can be 'personal', 'admin'  or  'course'
168
     */
169
    public function setType($type)
170
    {
171
        $type = trim($type);
172
        $typeList = $this->getTypes();
173
        if (in_array($type, $typeList, true)) {
174
            $this->type = $type;
175
        }
176
    }
177
178
    /**
179
     * Returns the type previously set (and filtered) through setType
180
     * If setType() was not called, then type defaults to "personal" as
181
     * set in the class definition.
182
     */
183
    public function getType()
184
    {
185
        if (isset($this->type)) {
186
            return $this->type;
187
        }
188
    }
189
190
    /**
191
     * @param int $id
192
     */
193
    public function setSessionId($id)
194
    {
195
        $this->sessionId = (int) $id;
196
    }
197
198
    /**
199
     * @param array $sessionInfo
200
     */
201
    public function setSessionInfo($sessionInfo)
202
    {
203
        $this->sessionInfo = $sessionInfo;
204
    }
205
206
    /**
207
     * @return int $id
208
     */
209
    public function getSessionId()
210
    {
211
        return $this->sessionId;
212
    }
213
214
    /**
215
     * @param array $courseInfo
216
     */
217
    public function set_course($courseInfo)
218
    {
219
        $this->course = $courseInfo;
220
    }
221
222
    /**
223
     * @return array
224
     */
225
    public function getTypes()
226
    {
227
        return $this->types;
228
    }
229
230
    /**
231
     * Adds an event to the calendar.
232
     *
233
     * @param string         $start                 datetime format: 2012-06-14 09:00:00 in local time
234
     * @param string         $end                   datetime format: 2012-06-14 09:00:00 in local time
235
     * @param string         $allDay                (true, false)
236
     * @param string         $title
237
     * @param string         $content
238
     * @param array          $usersToSend           array('everyone') or a list of user/group ids
239
     * @param bool           $addAsAnnouncement     event as a *course* announcement
240
     * @param int            $parentEventId
241
     * @param UploadedFile[] $attachmentArray       array of $_FILES['']
242
     * @param array          $attachmentCommentList
243
     * @param string         $eventComment
244
     * @param string         $color
245
     *
246
     * @return int
247
     */
248
    public function addEvent(
249
        $start,
250
        $end,
251
        $allDay,
252
        $title,
253
        $content,
254
        $usersToSend = [],
255
        $addAsAnnouncement = false,
256
        $parentEventId = 0,
257
        $attachmentArray = [],
258
        $attachmentCommentList = [],
259
        $eventComment = '',
260
        $color = ''
261
    ) {
262
        $start = api_get_utc_datetime($start, false, true);
263
        $end = api_get_utc_datetime($end, false, true);
264
        $allDay = isset($allDay) && 'true' === $allDay ? 1 : 0;
265
        $id = null;
266
267
        $em = Database::getManager();
268
        switch ($this->type) {
269
            case 'personal':
270
                /*$event = new PersonalAgenda();
271
                $event
272
                    ->setTitle($title)
273
                    ->setText($content)
274
                    ->setDate($start)
275
                    ->setEndDate($end)
276
                    ->setAllDay($allDay)
277
                    ->setColor($color)
278
                    ->setUser(api_get_user_entity())
279
                ;
280
                $em->persist($event);
281
                $em->flush();
282
                $id = $event->getId();*/
283
                break;
284
            case 'course':
285
                $sessionId = $this->getSessionId();
286
                $sessionEntity = api_get_session_entity($sessionId);
287
                $courseEntity = api_get_course_entity($this->course['real_id']);
288
                $groupEntity = api_get_group_entity(api_get_group_id());
289
290
                $event = new CCalendarEvent();
291
                $event
292
                    ->setTitle($title)
293
                    ->setContent($content)
294
                    ->setStartDate($start)
295
                    ->setEndDate($end)
296
                    ->setAllDay($allDay)
297
                    ->setColor($color)
298
                    ->setComment($eventComment)
299
                ;
300
301
                if (!empty($parentEventId)) {
302
                    $repo = Container::getCalendarEventRepository();
303
                    $parentEvent = $repo->find($parentEventId);
304
                    $event->setParentEvent($parentEvent);
305
                }
306
307
                $event->setParent($courseEntity);
308
309
                if (!empty($usersToSend)) {
310
                    $sendTo = $this->parseSendToArray($usersToSend);
311
                    if ($sendTo['everyone']) {
312
                        $event->addCourseLink($courseEntity, $sessionEntity, $groupEntity);
313
                    } else {
314
                        // Storing the selected groups
315
                        if (!empty($sendTo['groups'])) {
316
                            foreach ($sendTo['groups'] as $group) {
317
                                $groupInfo = null;
318
                                if ($group) {
319
                                    $groupInfo = api_get_group_entity($group);
320
                                    $event->addCourseLink($courseEntity, $sessionEntity, $groupInfo);
321
                                }
322
                            }
323
                        }
324
325
                        // storing the selected users
326
                        if (!empty($sendTo['users'])) {
327
                            foreach ($sendTo['users'] as $userId) {
328
                                $event->addUserLink(
329
                                    api_get_user_entity($userId),
330
                                    $courseEntity,
331
                                    $sessionEntity,
332
                                    $groupEntity
333
                                );
334
                            }
335
                        }
336
                    }
337
                }
338
339
                $em->persist($event);
340
                $em->flush();
341
                $id = $event->getIid();
342
343
                if ($id) {
344
                    // Add announcement.
345
                    if ($addAsAnnouncement) {
346
                        $this->storeAgendaEventAsAnnouncement($event, $usersToSend);
347
                    }
348
349
                    // Add attachment.
350
                    if (!empty($attachmentArray)) {
351
                        $counter = 0;
352
                        foreach ($attachmentArray as $attachmentItem) {
353
                            $this->addAttachment(
354
                                $event,
355
                                $attachmentItem,
356
                                $attachmentCommentList[$counter],
357
                                $this->course
358
                            );
359
                            $counter++;
360
                        }
361
                    }
362
                }
363
                break;
364
        }
365
366
        return $id;
367
    }
368
369
    /**
370
     * @param string $type
371
     * @param string $startEvent      in UTC
372
     * @param string $endEvent        in UTC
373
     * @param string $repeatUntilDate in UTC
374
     *
375
     * @throws Exception
376
     *
377
     * @return array
378
     */
379
    public function generateDatesByType($type, $startEvent, $endEvent, $repeatUntilDate)
380
    {
381
        $continue = true;
382
        $repeatUntilDate = new DateTime($repeatUntilDate, new DateTimeZone('UTC'));
383
        $loopMax = 365;
384
        $counter = 0;
385
        $list = [];
386
387
        switch ($type) {
388
            case 'daily':
389
                $interval = 'P1D';
390
                break;
391
            case 'weekly':
392
                $interval = 'P1W';
393
                break;
394
            case 'monthlyByDate':
395
                $interval = 'P1M';
396
                break;
397
            case 'monthlyByDay':
398
            case 'monthlyByDayR':
399
                // not yet implemented
400
                break;
401
            case 'yearly':
402
                $interval = 'P1Y';
403
                break;
404
        }
405
406
        if (empty($interval)) {
407
            return [];
408
        }
409
        $timeZone = api_get_timezone();
410
411
        while ($continue) {
412
            $startDate = new DateTime($startEvent, new DateTimeZone('UTC'));
413
            $endDate = new DateTime($endEvent, new DateTimeZone('UTC'));
414
415
            $startDate->add(new DateInterval($interval));
416
            $endDate->add(new DateInterval($interval));
417
418
            $newStartDate = $startDate->format('Y-m-d H:i:s');
419
            $newEndDate = $endDate->format('Y-m-d H:i:s');
420
421
            $startEvent = $newStartDate;
422
            $endEvent = $newEndDate;
423
424
            if ($endDate > $repeatUntilDate) {
425
                break;
426
            }
427
428
            // @todo remove comment code
429
            $startDateInLocal = new DateTime($newStartDate, new DateTimeZone($timeZone));
430
            if (0 == $startDateInLocal->format('I')) {
431
                // Is saving time? Then fix UTC time to add time
432
                $seconds = $startDateInLocal->getOffset();
433
                $startDate->add(new DateInterval("PT".$seconds."S"));
434
                $startDateFixed = $startDate->format('Y-m-d H:i:s');
435
                $startDateInLocalFixed = new DateTime($startDateFixed, new DateTimeZone($timeZone));
436
                $newStartDate = $startDateInLocalFixed->format('Y-m-d H:i:s');
437
            }
438
439
            $endDateInLocal = new DateTime($newEndDate, new DateTimeZone($timeZone));
440
            if (0 == $endDateInLocal->format('I')) {
441
                // Is saving time? Then fix UTC time to add time
442
                $seconds = $endDateInLocal->getOffset();
443
                $endDate->add(new DateInterval("PT".$seconds."S"));
444
                $endDateFixed = $endDate->format('Y-m-d H:i:s');
445
                $endDateInLocalFixed = new DateTime($endDateFixed, new DateTimeZone($timeZone));
446
                $newEndDate = $endDateInLocalFixed->format('Y-m-d H:i:s');
447
            }
448
            $list[] = ['start' => $newStartDate, 'end' => $newEndDate, 'i' => $startDateInLocal->format('I')];
449
            $counter++;
450
451
            // just in case stop if more than $loopMax
452
            if ($counter > $loopMax) {
453
                break;
454
            }
455
        }
456
457
        return $list;
458
    }
459
460
    /**
461
     * @param int    $eventId
462
     * @param string $type
463
     * @param string $end     in UTC
464
     * @param array  $sentTo
465
     *
466
     * @return bool
467
     */
468
    public function addRepeatedItem($eventId, $type, $end, $sentTo = [])
469
    {
470
        $t_agenda = Database::get_course_table(TABLE_AGENDA);
471
        $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
472
473
        if (empty($this->course)) {
474
            return false;
475
        }
476
477
        $eventId = (int) $eventId;
478
479
        $sql = "SELECT title, content, start_date, end_date, all_day
480
                FROM $t_agenda
481
                WHERE iid = $eventId";
482
        $res = Database::query($sql);
483
484
        if (1 !== Database::num_rows($res)) {
485
            return false;
486
        }
487
488
        $typeList = [
489
            'daily',
490
            'weekly',
491
            'monthlyByDate',
492
            'monthlyByDay',
493
            'monthlyByDayR',
494
            'yearly',
495
        ];
496
497
        if (!in_array($type, $typeList, true)) {
498
            return false;
499
        }
500
501
        $now = time();
502
        $endTimeStamp = api_strtotime($end, 'UTC');
503
        // The event has to repeat *in the future*. We don't allow repeated
504
        // events in the past
505
        if ($endTimeStamp < $now) {
506
            return false;
507
        }
508
509
        $row = Database::fetch_array($res);
510
511
        $title = $row['title'];
512
        $content = $row['content'];
513
        $allDay = $row['all_day'];
514
515
        $type = Database::escape_string($type);
516
        $end = Database::escape_string($end);
517
        $sql = "INSERT INTO $t_agenda_r (cal_id, cal_type, cal_end)
518
                VALUES ('$eventId', '$type', '$endTimeStamp')";
519
        Database::query($sql);
520
521
        $generatedDates = $this->generateDatesByType($type, $row['start_date'], $row['end_date'], $end);
522
523
        if (empty($generatedDates)) {
524
            return false;
525
        }
526
527
        foreach ($generatedDates as $dateInfo) {
528
            $start = api_get_local_time($dateInfo['start']);
529
            $end = api_get_local_time($dateInfo['end']);
530
            $this->addEvent(
531
                $start,
532
                $end,
533
                $allDay,
534
                $title,
535
                $content,
536
                $sentTo,
537
                false,
538
                $eventId
539
            );
540
        }
541
542
        return true;
543
    }
544
545
    public function storeAgendaEventAsAnnouncement(CCalendarEvent $event, array $sentTo = [])
546
    {
547
        // Sending announcement
548
        if (!empty($sentTo)) {
549
            $id = AnnouncementManager::add_announcement(
550
                api_get_course_info(),
551
                api_get_session_id(),
552
                $event->getTitle(),
553
                $event->getContent(),
554
                $sentTo,
555
                null,
556
                null,
557
                $event->getEndDate()->format('Y-m-d H:i:s')
558
            );
559
560
            AnnouncementManager::sendEmail(
561
                api_get_course_info(),
562
                api_get_session_id(),
563
                $id
564
            );
565
566
            return true;
567
        }
568
569
        return false;
570
    }
571
572
    /**
573
     * Edits an event.
574
     *
575
     * @param int    $id
576
     * @param string $start                 datetime format: 2012-06-14 09:00:00
577
     * @param string $end                   datetime format: 2012-06-14 09:00:00
578
     * @param int    $allDay                is all day 'true' or 'false'
579
     * @param string $title
580
     * @param string $content
581
     * @param array  $usersToSend
582
     * @param array  $attachmentArray
583
     * @param array  $attachmentCommentList
584
     * @param string $comment
585
     * @param string $color
586
     * @param bool   $addAnnouncement
587
     * @param bool   $updateContent
588
     * @param int    $authorId
589
     *
590
     * @return bool
591
     */
592
    public function editEvent(
593
        $id,
594
        $start,
595
        $end,
596
        $allDay,
597
        $title,
598
        $content,
599
        $usersToSend = [],
600
        $attachmentArray = [],
601
        $attachmentCommentList = [],
602
        $comment = '',
603
        $color = '',
604
        $addAnnouncement = false,
605
        $updateContent = true,
606
        $authorId = 0
607
    ) {
608
        $startObject = api_get_utc_datetime($start, true, true);
609
        $endObject = api_get_utc_datetime($end, true, true);
610
        $start = api_get_utc_datetime($start);
611
        $end = api_get_utc_datetime($end);
612
        $allDay = isset($allDay) && 'true' == $allDay ? 1 : 0;
613
        $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
614
615
        switch ($this->type) {
616
            case 'personal':
617
                /*$eventInfo = $this->get_event($id);
618
                if ($eventInfo['user'] != api_get_user_id()) {
619
                    break;
620
                }
621
                $attributes = [
622
                    'title' => $title,
623
                    'date' => $start,
624
                    'enddate' => $end,
625
                    'all_day' => $allDay,
626
                ];
627
628
                if ($updateContent) {
629
                    $attributes['text'] = $content;
630
                }
631
632
                if (!empty($color)) {
633
                    $attributes['color'] = $color;
634
                }
635
636
                Database::update(
637
                    $this->tbl_personal_agenda,
638
                    $attributes,
639
                    ['id = ?' => $id]
640
                );*/
641
                break;
642
            case 'course':
643
                $repo = Container::getCalendarEventRepository();
644
                $em = Database::getManager();
645
                /** @var CCalendarEvent $event */
646
                $event = $repo->find($id);
647
648
                if (empty($event)) {
649
                    return false;
650
                }
651
652
                $sentToEvent = $event->getUsersAndGroupSubscribedToResource();
653
                $courseId = $this->course['real_id'];
654
655
                if (empty($courseId)) {
656
                    return false;
657
                }
658
659
                $courseEntity = api_get_course_entity($courseId);
660
                $sessionEntity = api_get_session_entity($this->sessionId);
661
                $groupEntity = api_get_group_entity(api_get_group_id());
662
663
                if ($this->getIsAllowedToEdit()) {
664
                    $event
665
                        ->setTitle($title)
666
                        ->setStartDate($startObject)
667
                        ->setEndDate($endObject)
668
                        ->setAllDay($allDay)
669
                        ->setComment($comment)
670
                    ;
671
672
                    if ($updateContent) {
673
                        $event->setContent($content);
674
                    }
675
676
                    if (!empty($color)) {
677
                        $event->setColor($color);
678
                    }
679
680
                    if (!empty($usersToSend)) {
681
                        $sendTo = $this->parseSendToArray($usersToSend);
682
683
                        $usersToDelete = array_diff($sentToEvent['users'], $sendTo['users']);
684
                        $usersToAdd = array_diff($sendTo['users'], $sentToEvent['users']);
685
                        $groupsToDelete = array_diff($sentToEvent['groups'], $sendTo['groups']);
686
                        $groupToAdd = array_diff($sendTo['groups'], $sentToEvent['groups']);
687
688
                        //var_dump($sendTo['everyone'], $usersToDelete, $usersToAdd, $groupsToDelete, $groupToAdd);exit;
689
                        $links = $event->getResourceNode()->getResourceLinks();
690
691
                        if ($sendTo['everyone']) {
692
                            // Delete all from group
693
                            if (isset($sentToEvent['groups']) && !empty($sentToEvent['groups'])) {
694
                                foreach ($sentToEvent['groups'] as $group) {
695
                                    foreach ($links as $link) {
696
                                        if ($link->hasGroup() && $link->getGroup()->getIid() === $group) {
697
                                            $em->remove($link);
698
                                        }
699
                                    }
700
                                }
701
                            }
702
703
                            // Storing the selected users.
704
                            if (isset($sentToEvent['users']) && !empty($sentToEvent['users'])) {
705
                                foreach ($sentToEvent['users'] as $userId) {
706
                                    foreach ($links as $link) {
707
                                        if ($link->hasUser() && $link->getUser()->getId() === $userId) {
708
                                            $em->remove($link);
709
                                        }
710
                                    }
711
                                }
712
                            }
713
                        } else {
714
                            foreach ($links as $link) {
715
                                $em->remove($link);
716
                            }
717
718
                            // Add groups
719
                            if (!empty($groupToAdd)) {
720
                                foreach ($groupToAdd as $group) {
721
                                    $group = api_get_group_entity($group);
722
                                    $event->addCourseLink($courseEntity, $sessionEntity, $group);
723
                                }
724
                            }
725
726
                            // Delete groups.
727
                            if (!empty($groupsToDelete)) {
728
                                foreach ($groupsToDelete as $group) {
729
                                    foreach ($links as $link) {
730
                                        if ($link->hasGroup() && $link->getGroup()->getIid() === $group) {
731
                                            $em->remove($link);
732
                                        }
733
                                    }
734
                                }
735
                            }
736
737
                            // Add users.
738
                            if (!empty($usersToAdd)) {
739
                                foreach ($usersToAdd as $userId) {
740
                                    $event = $event->addUserLink(
741
                                        api_get_user_entity($userId),
742
                                        $courseEntity,
743
                                        $sessionEntity,
744
                                        $groupEntity
745
                                    );
746
                                }
747
                            }
748
749
                            // Delete users.
750
                            if (!empty($usersToDelete)) {
751
                                foreach ($usersToDelete as $userId) {
752
                                    foreach ($links as $link) {
753
                                        if ($link->hasUser() && $link->getUser()->getId() === $userId) {
754
                                            $em->remove($link);
755
                                        }
756
                                    }
757
                                }
758
                            }
759
                        }
760
                    }
761
762
                    $em->persist($event);
763
                    $em->flush($event);
764
765
                    // Add announcement.
766
                    if (isset($addAnnouncement) && !empty($addAnnouncement)) {
767
                        $this->storeAgendaEventAsAnnouncement($event, $usersToSend);
768
                    }
769
770
                    // Add attachment.
771
                    if (isset($attachmentArray) && !empty($attachmentArray)) {
772
                        $counter = 0;
773
                        foreach ($attachmentArray as $attachmentItem) {
774
                            if (isset($attachmentItem['id'])) {
775
                                $this->updateAttachment(
776
                                    $attachmentItem['id'],
777
                                    $id,
778
                                    $attachmentItem,
779
                                    $attachmentCommentList[$counter],
780
                                    $this->course
781
                                );
782
                                $counter++;
783
                            }
784
                        }
785
                    }
786
787
                    return true;
788
                }
789
790
                return false;
791
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
792
        }
793
    }
794
795
    /**
796
     * @param int  $id
797
     * @param bool $deleteAllItemsFromSerie
798
     */
799
    public function deleteEvent($id, $deleteAllItemsFromSerie = false)
800
    {
801
        switch ($this->type) {
802
            case 'personal':
803
                /*$eventInfo = $this->get_event($id);
804
                if ($eventInfo['user'] == api_get_user_id()) {
805
                    Database::delete(
806
                        $this->tbl_personal_agenda,
807
                        ['id = ?' => $id]
808
                    );
809
                }*/
810
                break;
811
            case 'course':
812
                $courseId = api_get_course_int_id();
813
                $isAllowToEdit = $this->getIsAllowedToEdit();
814
815
                if (!empty($courseId) && $isAllowToEdit) {
816
                    // Delete
817
                    $eventInfo = $this->get_event($id);
818
819
                    if ($deleteAllItemsFromSerie) {
820
                        /* This is one of the children.
821
                           Getting siblings and delete 'Em all + the father! */
822
                        if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
823
                            // Removing items.
824
                            $events = $this->getAllRepeatEvents($eventInfo['parent_event_id']);
825
                            if (!empty($events)) {
826
                                foreach ($events as $event) {
827
                                    $this->deleteEvent($event['id']);
828
                                }
829
                            }
830
                            // Removing parent.
831
                            $this->deleteEvent($eventInfo['parent_event_id']);
832
                        } else {
833
                            // This is the father looking for the children.
834
                            $events = $this->getAllRepeatEvents($id);
835
                            if (!empty($events)) {
836
                                foreach ($events as $event) {
837
                                    $this->deleteEvent($event['id']);
838
                                }
839
                            }
840
                        }
841
                    }
842
843
                    $repo = Container::getCalendarEventRepository();
844
                    $event = $repo->find($id);
845
846
                    if ($event) {
847
                        Database::getManager()->remove($event);
848
                        Database::getManager()->flush();
849
850
                        // Removing from events.
851
                        /*Database::delete(
852
                            $this->tbl_course_agenda,
853
                            ['id = ? AND c_id = ?' => [$id, $courseId]]
854
                        );*/
855
856
                        /*api_item_property_update(
857
                            $this->course,
858
                            TOOL_CALENDAR_EVENT,
859
                            $id,
860
                            'delete',
861
                            api_get_user_id()
862
                        );*/
863
864
                        // Removing from series.
865
                        Database::delete(
866
                            $this->table_repeat,
867
                            [
868
                                'cal_id = ?' => [
869
                                    $id,
870
                                ],
871
                            ]
872
                        );
873
                        // Attachments are already deleted using the doctrine remove() function.
874
                        /*if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
875
                            foreach ($eventInfo['attachment'] as $attachment) {
876
                                self::deleteAttachmentFile(
877
                                    $attachment['id'],
878
                                    $this->course
879
                                );
880
                            }
881
                        }*/
882
                        echo 1;
883
                    }
884
                }
885
                break;
886
        }
887
    }
888
889
    /**
890
     * Get agenda events.
891
     *
892
     * @param int    $start
893
     * @param int    $end
894
     * @param int    $courseId
895
     * @param int    $groupId
896
     * @param int    $user_id
897
     * @param string $format
898
     *
899
     * @return array|string
900
     */
901
    public function getEvents(
902
        $start,
903
        $end,
904
        $courseId = null,
905
        $groupId = null,
906
        $user_id = 0,
907
        $format = 'json'
908
    ) {
909
        switch ($this->type) {
910
            case 'course':
911
                $course = api_get_course_entity($courseId);
912
913
                // Session coach can see all events inside a session.
914
                if (api_is_coach()) {
915
                    // Own course
916
                    $this->getCourseEvents(
917
                        $start,
918
                        $end,
919
                        $course,
920
                        $groupId,
921
                        $this->sessionId,
922
                        $user_id
923
                    );
924
925
                    // Others
926
                    $this->getSessionEvents(
927
                        $start,
928
                        $end,
929
                        $this->sessionId,
930
                        $user_id,
931
                        $this->eventOtherSessionColor
932
                    );
933
                } else {
934
                    $this->getCourseEvents(
935
                        $start,
936
                        $end,
937
                        $course,
938
                        $groupId,
939
                        $this->sessionId,
940
                        $user_id
941
                    );
942
                }
943
                break;
944
            case 'personal':
945
            default:
946
                $sessionFilterActive = false;
947
                if (!empty($this->sessionId)) {
948
                    $sessionFilterActive = true;
949
                }
950
951
                $ignoreVisibility = ('true' === api_get_setting('agenda.personal_agenda_show_all_session_events'));
952
953
                // Getting course events
954
                $my_course_list = [];
955
                if (!api_is_anonymous()) {
956
                    $session_list = SessionManager::get_sessions_by_user(
957
                        api_get_user_id(),
958
                        $ignoreVisibility
959
                    );
960
                    $my_course_list = CourseManager::get_courses_list_by_user_id(
961
                        api_get_user_id(),
962
                        false
963
                    );
964
                }
965
966
                if (api_is_drh() && api_drh_can_access_all_session_content()) {
967
                    $session_list = [];
968
                    $sessionList = SessionManager::get_sessions_followed_by_drh(
969
                        api_get_user_id(),
970
                        null,
971
                        null,
972
                        null,
973
                        true,
974
                        false
975
                    );
976
977
                    if (!empty($sessionList)) {
978
                        foreach ($sessionList as $sessionItem) {
979
                            $sessionId = $sessionItem['id'];
980
                            $courses = SessionManager::get_course_list_by_session_id(
981
                                $sessionId
982
                            );
983
                            $sessionInfo = [
984
                                'session_id' => $sessionId,
985
                                'courses' => $courses,
986
                            ];
987
                            $session_list[] = $sessionInfo;
988
                        }
989
                    }
990
                }
991
992
                if (!empty($session_list)) {
993
                    foreach ($session_list as $session_item) {
994
                        if ($sessionFilterActive) {
995
                            if ($this->sessionId != $session_item['session_id']) {
996
                                continue;
997
                            }
998
                        }
999
1000
                        $my_courses = $session_item['courses'];
1001
                        $my_session_id = $session_item['session_id'];
1002
1003
                        if (!empty($my_courses)) {
1004
                            foreach ($my_courses as $course_item) {
1005
                                $course = api_get_course_entity($course_item['real_id']);
1006
                                $this->getCourseEvents(
1007
                                    $start,
1008
                                    $end,
1009
                                    $course,
1010
                                    0,
1011
                                    $my_session_id
1012
                                );
1013
                            }
1014
                        }
1015
                    }
1016
                }
1017
1018
                if (!empty($my_course_list) && false == $sessionFilterActive) {
1019
                    foreach ($my_course_list as $courseInfoItem) {
1020
                        $course = api_get_course_entity($courseInfoItem['real_id']);
1021
                        if (isset($courseId) && !empty($courseId)) {
1022
                            if ($course->getId() == $courseId) {
1023
                                $this->getCourseEvents(
1024
                                    $start,
1025
                                    $end,
1026
                                    $course,
1027
                                    0,
1028
                                    0,
1029
                                    $user_id
1030
                                );
1031
                            }
1032
                        } else {
1033
                            $this->getCourseEvents(
1034
                                $start,
1035
                                $end,
1036
                                $course,
1037
                                0,
1038
                                0,
1039
                                $user_id
1040
                            );
1041
                        }
1042
                    }
1043
                }
1044
                break;
1045
        }
1046
1047
        $this->cleanEvents();
1048
1049
        switch ($format) {
1050
            case 'json':
1051
                if (empty($this->events)) {
1052
                    return '[]';
1053
                }
1054
1055
                return json_encode($this->events);
1056
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
1057
            case 'array':
1058
                if (empty($this->events)) {
1059
                    return [];
1060
                }
1061
1062
                return $this->events;
1063
                break;
1064
        }
1065
    }
1066
1067
    /**
1068
     * Clean events.
1069
     *
1070
     * @return bool
1071
     */
1072
    public function cleanEvents()
1073
    {
1074
        if (empty($this->events)) {
1075
            return false;
1076
        }
1077
1078
        foreach ($this->events as &$event) {
1079
            $event['description'] = Security::remove_XSS($event['description']);
1080
            $event['title'] = Security::remove_XSS($event['title']);
1081
        }
1082
1083
        return true;
1084
    }
1085
1086
    /**
1087
     * @param int $id
1088
     * @param int $minute_delta
1089
     *
1090
     * @return int
1091
     */
1092
    public function resizeEvent($id, $minute_delta)
1093
    {
1094
        $id = (int) $id;
1095
        $delta = (int) $minute_delta;
1096
        $event = $this->get_event($id);
1097
        if (!empty($event)) {
1098
            switch ($this->type) {
1099
                case 'personal':
1100
                    /*$sql = "UPDATE $this->tbl_personal_agenda SET
1101
                            enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
1102
							WHERE id = ".$id;
1103
                    Database::query($sql);*/
1104
                    break;
1105
                case 'course':
1106
                    $sql = "UPDATE $this->tbl_course_agenda SET
1107
                            end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
1108
							WHERE
1109
							    c_id = ".$this->course['real_id']." AND
1110
							    id = ".$id;
1111
                    Database::query($sql);
1112
                    break;
1113
            }
1114
        }
1115
1116
        return 1;
1117
    }
1118
1119
    /**
1120
     * @param int $id
1121
     * @param int $minute_delta minutes
1122
     * @param int $allDay
1123
     *
1124
     * @return int
1125
     */
1126
    public function move_event($id, $minute_delta, $allDay)
1127
    {
1128
        $id = (int) $id;
1129
        $event = $this->get_event($id);
1130
1131
        if (empty($event)) {
1132
            return false;
1133
        }
1134
1135
        // we convert the hour delta into minutes and add the minute delta
1136
        $delta = (int) $minute_delta;
1137
        $allDay = (int) $allDay;
1138
1139
        if (!empty($event)) {
1140
            switch ($this->type) {
1141
                case 'personal':
1142
                    /*$sql = "UPDATE $this->tbl_personal_agenda SET
1143
                            all_day = $allDay, date = DATE_ADD(date, INTERVAL $delta MINUTE),
1144
                            enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
1145
							WHERE id=".$id;
1146
                    Database::query($sql);*/
1147
                    break;
1148
                case 'course':
1149
                    $sql = "UPDATE $this->tbl_course_agenda SET
1150
                            all_day = $allDay,
1151
                            start_date = DATE_ADD(start_date, INTERVAL $delta MINUTE),
1152
                            end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
1153
							WHERE
1154
							    c_id = ".$this->course['real_id']." AND
1155
							    id=".$id;
1156
                    Database::query($sql);
1157
                    break;
1158
            }
1159
        }
1160
1161
        return 1;
1162
    }
1163
1164
    /**
1165
     * Gets a single event.
1166
     *
1167
     * @param int $id event id
1168
     *
1169
     * @return array
1170
     */
1171
    public function get_event($id)
1172
    {
1173
        // make sure events of the personal agenda can only be seen by the user himself
1174
        $id = (int) $id;
1175
        $event = null;
1176
        switch ($this->type) {
1177
            case 'personal':
1178
                /*$sql = "SELECT * FROM ".$this->tbl_personal_agenda."
1179
                        WHERE id = $id AND user = ".api_get_user_id();
1180
                $result = Database::query($sql);
1181
                if (Database::num_rows($result)) {
1182
                    $event = Database::fetch_array($result, 'ASSOC');
1183
                    $event['description'] = $event['text'];
1184
                    $event['content'] = $event['text'];
1185
                    $event['start_date'] = $event['date'];
1186
                    $event['end_date'] = $event['enddate'];
1187
                }*/
1188
                break;
1189
            case 'course':
1190
                $repo = Container::getCalendarEventRepository();
1191
                /** @var CCalendarEvent $eventEntity */
1192
                $eventEntity = $repo->find($id);
1193
1194
                if (!empty($this->course['real_id'])) {
1195
                    if ($eventEntity) {
1196
                        $event = [];
1197
                        $event['iid'] = $eventEntity->getIid();
1198
                        $event['title'] = $eventEntity->getTitle();
1199
                        $event['content'] = $eventEntity->getContent();
1200
                        $event['all_day'] = $eventEntity->isAllDay();
1201
                        $event['start_date'] = $eventEntity->getStartDate()->format('Y-m-d H:i:s');
1202
                        $event['end_date'] = $eventEntity->getEndDate()->format('Y-m-d H:i:s');
1203
                        $event['description'] = $eventEntity->getComment();
1204
1205
                        // Getting send to array
1206
                        $event['send_to'] = $eventEntity->getUsersAndGroupSubscribedToResource();
1207
1208
                        // Getting repeat info
1209
                        $event['repeat_info'] = $eventEntity->getRepeatEvents();
1210
1211
                        if (!empty($event['parent_event_id'])) {
1212
                            $event['parent_info'] = $eventEntity->getParentEvent();
1213
                        }
1214
1215
                        $event['attachment'] = $eventEntity->getAttachments();
1216
                    }
1217
                }
1218
                break;
1219
        }
1220
1221
        return $event;
1222
    }
1223
1224
    /**
1225
     * Gets personal events.
1226
     *
1227
     * @param int $start
1228
     * @param int $end
1229
     *
1230
     * @return array
1231
     */
1232
    public function getPersonalEvents($start, $end)
1233
    {
1234
        $start = (int) $start;
1235
        $end = (int) $end;
1236
        $startCondition = '';
1237
        $endCondition = '';
1238
1239
        if (0 !== $start) {
1240
            $startCondition = "AND date >= '".api_get_utc_datetime($start)."'";
1241
        }
1242
        if (0 !== $start) {
1243
            $endCondition = "AND (enddate <= '".api_get_utc_datetime($end)."' OR enddate IS NULL)";
1244
        }
1245
        $user_id = api_get_user_id();
1246
1247
        $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
1248
                WHERE user = $user_id $startCondition $endCondition";
1249
1250
        $result = Database::query($sql);
1251
        $my_events = [];
1252
        if (Database::num_rows($result)) {
1253
            while ($row = Database::fetch_array($result, 'ASSOC')) {
1254
                $event = [];
1255
                $event['id'] = 'personal_'.$row['id'];
1256
                $event['title'] = $row['title'];
1257
                $event['className'] = 'personal';
1258
                $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
1259
                $event['editable'] = true;
1260
                $event['sent_to'] = get_lang('Me');
1261
                $event['type'] = 'personal';
1262
1263
                if (!empty($row['date'])) {
1264
                    $event['start'] = $this->formatEventDate($row['date']);
1265
                    $event['start_date_localtime'] = api_get_local_time($row['date']);
1266
                }
1267
1268
                if (!empty($row['enddate'])) {
1269
                    $event['end'] = $this->formatEventDate($row['enddate']);
1270
                    $event['end_date_localtime'] = api_get_local_time($row['enddate']);
1271
                }
1272
1273
                $event['description'] = $row['text'];
1274
                $event['allDay'] = isset($row['all_day']) && 1 == $row['all_day'] ? $row['all_day'] : 0;
1275
                $event['parent_event_id'] = 0;
1276
                $event['has_children'] = 0;
1277
1278
                $my_events[] = $event;
1279
                $this->events[] = $event;
1280
            }
1281
        }
1282
1283
        // Add plugin personal events
1284
        $this->plugin = new AppPlugin();
1285
        $plugins = $this->plugin->getInstalledPluginListObject();
1286
        /** @var Plugin $plugin */
1287
        foreach ($plugins as $plugin) {
1288
            if ($plugin->hasPersonalEvents && method_exists($plugin, 'getPersonalEvents')) {
1289
                $pluginEvents = $plugin->getPersonalEvents($this, $start, $end);
1290
1291
                if (!empty($pluginEvents)) {
1292
                    $this->events = array_merge($this->events, $pluginEvents);
1293
                }
1294
            }
1295
        }
1296
1297
        return $my_events;
1298
    }
1299
1300
    /**
1301
     * @param int    $start
1302
     * @param int    $end
1303
     * @param int    $sessionId
1304
     * @param int    $userId
1305
     * @param string $color
1306
     *
1307
     * @return array
1308
     */
1309
    public function getSessionEvents(
1310
        $start,
1311
        $end,
1312
        $sessionId = 0,
1313
        $userId = 0,
1314
        $color = ''
1315
    ) {
1316
        $courses = SessionManager::get_course_list_by_session_id($sessionId);
1317
1318
        if (!empty($courses)) {
1319
            foreach ($courses as $course) {
1320
                $course = api_get_course_entity($course['real_id']);
1321
                $this->getCourseEvents(
1322
                    $start,
1323
                    $end,
1324
                    $course,
1325
                    0,
1326
                    $sessionId,
1327
                    0,
1328
                    $color
1329
                );
1330
            }
1331
        }
1332
    }
1333
1334
    /**
1335
     * @param int    $start
1336
     * @param int    $end
1337
     * @param int    $groupId
1338
     * @param int    $sessionId
1339
     * @param int    $user_id
1340
     * @param string $color
1341
     *
1342
     * @return array
1343
     */
1344
    public function getCourseEvents(
1345
        $start,
1346
        $end,
1347
        Course $course,
1348
        $groupId = 0,
1349
        $sessionId = 0,
1350
        $user_id = 0,
1351
        $color = ''
1352
    ) {
1353
        $start = (int) $start;
1354
        $end = (int) $end;
1355
1356
        /** @var string|null $start */
1357
        $start = !empty($start) ? api_get_utc_datetime($start) : null;
1358
        /** @var string|null $end */
1359
        $end = !empty($end) ? api_get_utc_datetime($end) : null;
1360
1361
        if (null === $course) {
1362
            return [];
1363
        }
1364
1365
        $courseId = $course->getId();
1366
        $userId = api_get_user_id();
1367
        $sessionId = (int) $sessionId;
1368
        $user_id = (int) $user_id;
1369
1370
        $groupList = GroupManager::get_group_list(
1371
            null,
1372
            $course,
1373
            null,
1374
            $sessionId
1375
        );
1376
1377
        if (api_is_platform_admin() || api_is_allowed_to_edit()) {
1378
            $isAllowToEdit = true;
1379
        } else {
1380
            $isAllowToEdit = CourseManager::isCourseTeacher($userId, $courseId);
1381
        }
1382
1383
        $isAllowToEditByHrm = false;
1384
        if (!empty($sessionId)) {
1385
            $allowDhrToEdit = ('true' === api_get_setting('agenda.allow_agenda_edit_for_hrm'));
1386
            if ($allowDhrToEdit) {
1387
                $isHrm = SessionManager::isUserSubscribedAsHRM($sessionId, $userId);
1388
                if ($isHrm) {
1389
                    $isAllowToEdit = $isAllowToEditByHrm = true;
1390
                }
1391
            }
1392
        }
1393
1394
        $groupMemberships = [];
1395
        if (!empty($groupId)) {
1396
            $groupMemberships = [$groupId];
1397
        } else {
1398
            if ($isAllowToEdit) {
1399
                if (!empty($groupList)) {
1400
                    $groupMemberships = array_column($groupList, 'iid');
1401
                }
1402
            } else {
1403
                // get only related groups from user
1404
                $groupMemberships = GroupManager::get_group_ids($courseId, $userId);
1405
            }
1406
        }
1407
1408
        $repo = Container::getCalendarEventRepository();
1409
        $session = api_get_session_entity($sessionId);
1410
        $qb = $repo->getResourcesByCourseOnly($course, $course->getResourceNode());
1411
        $userCondition = '';
1412
1413
        if ($isAllowToEdit) {
1414
            // No group filter was asked
1415
            if (empty($groupId)) {
1416
                if (empty($user_id)) {
1417
                    // Show all events not added in group
1418
                    $userCondition = ' (links.group IS NULL) ';
1419
                    // admin see only his stuff
1420
                    if ('personal' === $this->type) {
1421
                        $userCondition = " (links.user = ".api_get_user_id()." AND (links.group IS NULL) ";
1422
                        $userCondition .= " OR ( (links.user IS NULL)  AND (links.group IS NULL ))) ";
1423
                    }
1424
1425
                    if (!empty($groupMemberships)) {
1426
                        // Show events sent to selected groups
1427
                        $userCondition .= " OR (links.user IS NULL) AND (links.group IN (".implode(", ", $groupMemberships).")) ";
1428
                    }
1429
                } else {
1430
                    // Show events of requested user in no group
1431
                    $userCondition = " (links.user = $user_id AND links.group IS NULL) ";
1432
                    // Show events sent to selected groups
1433
                    if (!empty($groupMemberships)) {
1434
                        $userCondition .= " OR (links.user = $user_id) AND (links.group IN (".implode(", ", $groupMemberships).")) ";
1435
                    }
1436
                }
1437
            } else {
1438
                // Show only selected groups (depending of user status)
1439
                $userCondition = " (links.user is NULL) AND (links.group IN (".implode(", ", $groupMemberships).")) ";
1440
1441
                if (!empty($groupMemberships)) {
1442
                    // Show send to $user_id in selected groups
1443
                    $userCondition .= " OR (links.user = $user_id) AND (links.group IN (".implode(", ", $groupMemberships).")) ";
1444
                }
1445
            }
1446
        } else {
1447
            // No group filter was asked
1448
            if (empty($groupId)) {
1449
                // Show events sent to everyone and no group
1450
                $userCondition = ' ( (links.user is NULL) AND (links.group IS NULL) ';
1451
                // Show events sent to selected groups
1452
                if (!empty($groupMemberships)) {
1453
                    $userCondition .= " OR (links.user is NULL) AND
1454
                                        (links.group IN (".implode(", ", $groupMemberships)."))) ";
1455
                } else {
1456
                    $userCondition .= " ) ";
1457
                }
1458
                $userCondition .= " OR (links.user = ".api_get_user_id()." AND (links.group IS NULL )) ";
1459
            } else {
1460
                if (!empty($groupMemberships)) {
1461
                    // Show send to everyone - and only selected groups
1462
                    $userCondition = " (links.user is NULL) AND
1463
                                       (links.group IN (".implode(", ", $groupMemberships).")) ";
1464
                }
1465
            }
1466
1467
            // Show sent to only me and no group
1468
            if (!empty($groupMemberships)) {
1469
                $userCondition .= " OR (
1470
                                    links.user = ".api_get_user_id().") AND
1471
                                    (links.group IN (".implode(", ", $groupMemberships).")
1472
                                    ) ";
1473
            }
1474
        }
1475
1476
        if (!empty($userCondition)) {
1477
            $qb->andWhere($userCondition);
1478
        }
1479
1480
        if (!empty($start) && !empty($end)) {
1481
            $qb->andWhere(
1482
                $qb->expr()->orX(
1483
                    'resource.startDate BETWEEN :start AND :end',
1484
                    'resource.endDate BETWEEN :start AND :end',
1485
                    $qb->expr()->orX(
1486
                        'resource.startDate IS NOT NULL AND resource.endDate IS NOT NULL AND
1487
                            YEAR(resource.startDate) = YEAR(resource.endDate) AND
1488
                            MONTH(:start) BETWEEN MONTH(resource.startDate) AND MONTH(resource.endDate)
1489
                        '
1490
                    )
1491
                )
1492
            )
1493
            ->setParameter('start', $start)
1494
            ->setParameter('end', $end);
1495
        }
1496
1497
        /*
1498
        if (empty($sessionId)) {
1499
            $sessionCondition = "
1500
            (
1501
                agenda.session_id = 0 AND (ip.session_id IS NULL OR ip.session_id = 0)
1502
            ) ";
1503
        } else {
1504
            $sessionCondition = "
1505
            (
1506
                agenda.session_id = $sessionId AND
1507
                ip.session_id = $sessionId
1508
            ) ";
1509
        }
1510
1511
        if (api_is_allowed_to_edit()) {
1512
            $visibilityCondition = " (ip.visibility IN ('1', '0'))  ";
1513
        } else {
1514
            $visibilityCondition = " (ip.visibility = '1') ";
1515
        }
1516
1517
        $sql = "SELECT DISTINCT
1518
                    agenda.*,
1519
                    ip.visibility,
1520
                    ip.to_group_id,
1521
                    ip.insert_user_id,
1522
                    ip.ref,
1523
                    to_user_id
1524
                FROM $tlb_course_agenda agenda
1525
                INNER JOIN $tbl_property ip
1526
                ON (
1527
                    agenda.id = ip.ref AND
1528
                    agenda.c_id = ip.c_id AND
1529
                    ip.tool = '".TOOL_CALENDAR_EVENT."'
1530
                )
1531
                WHERE
1532
                    $sessionCondition AND
1533
                    ($userCondition) AND
1534
                    $visibilityCondition AND
1535
                    agenda.c_id = $courseId
1536
        ";
1537
        $dateCondition = '';
1538
        if (!empty($start) && !empty($end)) {
1539
            $dateCondition .= "AND (
1540
                 agenda.start_date BETWEEN '".$start."' AND '".$end."' OR
1541
                 agenda.end_date BETWEEN '".$start."' AND '".$end."' OR
1542
                 (
1543
                     agenda.start_date IS NOT NULL AND agenda.end_date IS NOT NULL AND
1544
                     YEAR(agenda.start_date) = YEAR(agenda.end_date) AND
1545
                     MONTH('$start') BETWEEN MONTH(agenda.start_date) AND MONTH(agenda.end_date)
1546
                 )
1547
            )";
1548
        }
1549
1550
        $sql .= $dateCondition;
1551
        $result = Database::query($sql);*/
1552
        $coachCanEdit = false;
1553
        if (!empty($sessionId)) {
1554
            $coachCanEdit = api_is_coach($sessionId, $courseId) || api_is_platform_admin();
1555
        }
1556
        $events = $qb->getQuery()->getResult();
1557
1558
        $repo = Container::getCalendarEventAttachmentRepository();
1559
1560
        /** @var CCalendarEvent $row */
1561
        foreach ($events as $row) {
1562
            $eventId = $row->getIid();
1563
            $event = [];
1564
            $event['id'] = 'course_'.$eventId;
1565
            $event['unique_id'] = $eventId;
1566
1567
            $eventsAdded[] = $eventId;
1568
            $attachmentList = $row->getAttachments();
1569
            $event['attachment'] = '';
1570
            if (!empty($attachmentList)) {
1571
                $icon = Display::getMdiIcon(
1572
                    ObjectIcon::ATTACHMENT,
1573
                    'ch-tool-icon',
1574
                    null,
1575
                    ICON_SIZE_SMALL
1576
                );
1577
                /** @var CCalendarEventAttachment $attachment */
1578
                foreach ($attachmentList as $attachment) {
1579
                    $url = $repo->getResourceFileDownloadUrl($attachment).'?'.api_get_cidreq();
1580
                    $event['attachment'] .= $icon.
1581
                        Display::url(
1582
                            $attachment->getFilename(),
1583
                            $url
1584
                        ).'<br />';
1585
                }
1586
            }
1587
1588
            $event['title'] = $row->getTitle();
1589
            $event['className'] = 'course';
1590
            $event['allDay'] = 'false';
1591
            $event['course_id'] = $courseId;
1592
            $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
1593
1594
            $sessionInfo = [];
1595
            /*if (!empty($row->getSessionId())) {
1596
                $sessionInfo = api_get_session_info($row->getSessionId());
1597
                $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
1598
            }*/
1599
1600
            $event['session_name'] = $sessionInfo['name'] ?? '';
1601
            $event['course_name'] = $course->getTitle();
1602
1603
            /*if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
1604
                $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
1605
            }*/
1606
1607
            if (!empty($color)) {
1608
                $event['borderColor'] = $event['backgroundColor'] = $color;
1609
            }
1610
1611
            if ($row->getColor()) {
1612
                $event['borderColor'] = $event['backgroundColor'] = $row->getColor();
1613
            }
1614
1615
            $event['resourceEditable'] = false;
1616
            if ($this->getIsAllowedToEdit() && 'course' === $this->type) {
1617
                $event['resourceEditable'] = true;
1618
                if (!empty($sessionId)) {
1619
                    if (false == $coachCanEdit) {
1620
                        $event['resourceEditable'] = false;
1621
                    }
1622
                    if ($isAllowToEditByHrm) {
1623
                        $event['resourceEditable'] = true;
1624
                    }
1625
                }
1626
                // if user is author then he can edit the item
1627
                if (api_get_user_id() == $row->getResourceNode()->getCreator()->getId()) {
1628
                    $event['resourceEditable'] = true;
1629
                }
1630
            }
1631
1632
            if (!empty($row->getStartDate())) {
1633
                $event['start'] = $this->formatEventDate($row->getStartDate()->format('Y-m-d H:i:s'));
1634
                $event['start_date_localtime'] = api_get_local_time($row->getStartDate()->format('Y-m-d H:i:s'));
1635
            }
1636
            if (!empty($row->getEndDate())) {
1637
                $event['end'] = $this->formatEventDate($row->getEndDate()->format('Y-m-d H:i:s'));
1638
                $event['end_date_localtime'] = api_get_local_time($row->getEndDate()->format('Y-m-d H:i:s'));
1639
            }
1640
1641
            $event['sent_to'] = '';
1642
            $event['type'] = 'course';
1643
            /*if (0 != $row->getSessionId()) {
1644
                $event['type'] = 'session';
1645
            }*/
1646
1647
            $everyone = false;
1648
            $links = $row->getResourceNode()->getResourceLinks();
1649
            $sentTo = [];
1650
            foreach ($links as $link) {
1651
                if ($link->getUser()) {
1652
                    $sentTo[] = $link->getUser()->getFirstname();
1653
                }
1654
                if ($link->getCourse()) {
1655
                    $sentTo[] = $link->getCourse()->getTitle();
1656
                }
1657
                if ($link->getSession()) {
1658
                    $sentTo[] = $link->getSession()->getTitle();
1659
                }
1660
                if ($link->getGroup()) {
1661
                    $sentTo[] = $link->getGroup()->getTitle();
1662
                }
1663
            }
1664
1665
            // Event Sent to a group?
1666
            /*if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
1667
                $sent_to = [];
1668
                if (!empty($group_to_array)) {
1669
                    foreach ($group_to_array as $group_item) {
1670
                        $sent_to[] = $groupNameList[$group_item];
1671
                    }
1672
                }
1673
                $sent_to = implode('@@', $sent_to);
1674
                $sent_to = str_replace(
1675
                    '@@',
1676
                    '</div><div class="label_tag notice">',
1677
                    $sent_to
1678
                );
1679
                $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
1680
                $event['type'] = 'group';
1681
            }*/
1682
1683
            // Event sent to a user?
1684
            /*if (isset($row['to_user_id'])) {
1685
                $sent_to = [];
1686
                if (!empty($user_to_array)) {
1687
                    foreach ($user_to_array as $item) {
1688
                        $user_info = api_get_user_info($item);
1689
                        // Add username as tooltip for $event['sent_to'] - ref #4226
1690
                        $username = api_htmlentities(
1691
                            sprintf(
1692
                                get_lang('Login: %s'),
1693
                                $user_info['username']
1694
                            ),
1695
                            ENT_QUOTES
1696
                        );
1697
                        $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
1698
                    }
1699
                }
1700
                $sent_to = implode('@@', $sent_to);
1701
                $sent_to = str_replace(
1702
                    '@@',
1703
                    '</div><div class="label_tag notice">',
1704
                    $sent_to
1705
                );
1706
                $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
1707
            }*/
1708
1709
            //Event sent to everyone!
1710
            /*if (empty($event['sent_to'])) {
1711
                $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
1712
            }*/
1713
            $event['sent_to'] = implode('<br />', $sentTo);
1714
            $event['description'] = $row->getContent();
1715
            $event['visibility'] = $row->isVisible($course, $session) ? 1 : 0;
1716
            $event['real_id'] = $eventId;
1717
            $event['allDay'] = $row->isAllDay();
1718
            $event['parent_event_id'] = $row->getParentEvent() ? $row->getParentEvent()->getIid() : null;
1719
            $event['has_children'] = $row->getChildren()->count() > 0;
1720
            $event['comment'] = $row->getComment();
1721
            $this->events[] = $event;
1722
        }
1723
1724
        return $this->events;
1725
    }
1726
1727
    /**
1728
     * @param int $start tms
1729
     * @param int $end   tms
1730
     *
1731
     * @return array
1732
     */
1733
    public function getPlatformEvents($start, $end)
1734
    {
1735
        $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
1736
        $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
1737
        $dateCondition = '';
1738
1739
        if (!empty($start) && !empty($end)) {
1740
            $dateCondition .= "AND (
1741
                 start_date BETWEEN '".$start."' AND '".$end."' OR
1742
                 end_date BETWEEN '".$start."' AND '".$end."' OR
1743
                 (
1744
                     start_date IS NOT NULL AND end_date IS NOT NULL AND
1745
                     YEAR(start_date) = YEAR(end_date) AND
1746
                     MONTH('$start') BETWEEN MONTH(start_date) AND MONTH(end_date)
1747
                 )
1748
            )";
1749
        }
1750
1751
        $access_url_id = api_get_current_access_url_id();
1752
1753
        $sql = "SELECT *
1754
                FROM ".$this->tbl_global_agenda."
1755
                WHERE access_url_id = $access_url_id
1756
                $dateCondition";
1757
        $result = Database::query($sql);
1758
        $my_events = [];
1759
        if (Database::num_rows($result)) {
1760
            while ($row = Database::fetch_array($result, 'ASSOC')) {
1761
                $event = [];
1762
                $event['id'] = 'platform_'.$row['id'];
1763
                $event['title'] = $row['title'];
1764
                $event['className'] = 'platform';
1765
                $event['allDay'] = 'false';
1766
                $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
1767
                $event['editable'] = false;
1768
                $event['type'] = 'admin';
1769
1770
                if (api_is_platform_admin() && 'admin' === $this->type) {
1771
                    $event['editable'] = true;
1772
                }
1773
1774
                if (!empty($row['start_date'])) {
1775
                    $event['start'] = $this->formatEventDate($row['start_date']);
1776
                    $event['start_date_localtime'] = api_get_local_time($row['start_date']);
1777
                }
1778
1779
                if (!empty($row['end_date'])) {
1780
                    $event['end'] = $this->formatEventDate($row['end_date']);
1781
                    $event['end_date_localtime'] = api_get_local_time($row['end_date']);
1782
                }
1783
                $event['allDay'] = isset($row['all_day']) && 1 == $row['all_day'] ? $row['all_day'] : 0;
1784
                $event['parent_event_id'] = 0;
1785
                $event['has_children'] = 0;
1786
                $event['description'] = $row['content'];
1787
1788
                $my_events[] = $event;
1789
                $this->events[] = $event;
1790
            }
1791
        }
1792
1793
        return $my_events;
1794
    }
1795
1796
    /**
1797
     * @param CGroup[] $groupList
1798
     * @param array    $userList
1799
     * @param array    $sendTo               array('users' => [1, 2], 'groups' => [3, 4])
1800
     * @param array    $attributes
1801
     * @param bool     $addOnlyItemsInSendTo
1802
     * @param bool     $required
1803
     */
1804
    public function setSendToSelect(
1805
        FormValidator $form,
1806
        $groupList = [],
1807
        $userList = [],
1808
        $sendTo = [],
1809
        $attributes = [],
1810
        $addOnlyItemsInSendTo = false,
1811
        $required = false
1812
    ) {
1813
        $params = [
1814
            'id' => 'users_to_send_id',
1815
            'data-placeholder' => get_lang('Select'),
1816
            'multiple' => 'multiple',
1817
            'class' => 'multiple-select',
1818
        ];
1819
1820
        if (!empty($attributes)) {
1821
            $params = array_merge($params, $attributes);
1822
            if (empty($params['multiple'])) {
1823
                unset($params['multiple']);
1824
            }
1825
        }
1826
1827
        $sendToGroups = $sendTo['groups'] ?? [];
1828
        $sendToUsers = $sendTo['users'] ?? [];
1829
1830
        $select = $form->addSelect(
1831
            'users_to_send',
1832
            get_lang('To'),
1833
            [],
1834
            $params
1835
        );
1836
1837
        if ($required) {
1838
            $form->setRequired($select);
1839
        }
1840
1841
        $selectedEveryoneOptions = [];
1842
        if (isset($sendTo['everyone']) && $sendTo['everyone']) {
1843
            $selectedEveryoneOptions = ['selected'];
1844
            $sendToUsers = [];
1845
        }
1846
1847
        $select->addOption(
1848
            get_lang('Everyone'),
1849
            'everyone',
1850
            $selectedEveryoneOptions
1851
        );
1852
1853
        $options = [];
1854
        if (is_array($groupList)) {
1855
            foreach ($groupList as $group) {
1856
                $groupId = $group->getIid();
1857
                $count = $group->getMembers()->count();
1858
                $countUsers = " &ndash; $count ".get_lang('Users');
1859
                $option = [
1860
                    'text' => $group->getTitle().$countUsers,
1861
                    'value' => "GROUP:".$groupId,
1862
                ];
1863
1864
                $selected = in_array(
1865
                    $groupId,
1866
                    $sendToGroups
1867
                ) ? true : false;
1868
                if ($selected) {
1869
                    $option['selected'] = 'selected';
1870
                }
1871
1872
                if ($addOnlyItemsInSendTo) {
1873
                    if ($selected) {
1874
                        $options[] = $option;
1875
                    }
1876
                } else {
1877
                    $options[] = $option;
1878
                }
1879
            }
1880
            $select->addOptGroup($options, get_lang('Groups'));
1881
        }
1882
1883
        // adding the individual users to the select form
1884
        if (is_array($userList)) {
1885
            $options = [];
1886
            foreach ($userList as $user) {
1887
                if (ANONYMOUS == $user['status']) {
1888
                    continue;
1889
                }
1890
                $option = [
1891
                    'text' => api_get_person_name(
1892
                            $user['firstname'],
1893
                            $user['lastname']
1894
                        ).' ('.$user['username'].')',
1895
                    'value' => "USER:".$user['user_id'],
1896
                ];
1897
1898
                $selected = in_array(
1899
                    $user['user_id'],
1900
                    $sendToUsers
1901
                ) ? true : false;
1902
1903
                if ($selected) {
1904
                    $option['selected'] = 'selected';
1905
                }
1906
1907
                if ($addOnlyItemsInSendTo) {
1908
                    if ($selected) {
1909
                        $options[] = $option;
1910
                    }
1911
                } else {
1912
                    $options[] = $option;
1913
                }
1914
            }
1915
1916
            $select->addOptGroup($options, get_lang('Users'));
1917
        }
1918
    }
1919
1920
    /**
1921
     * Separates the users and groups array
1922
     * users have a value USER:XXX (with XXX the user id
1923
     * groups have a value GROUP:YYY (with YYY the group id)
1924
     * use the 'everyone' key.
1925
     *
1926
     * @author Julio Montoya based in separate_users_groups in agenda.inc.php
1927
     *
1928
     * @param array $to
1929
     *
1930
     * @return array
1931
     */
1932
    public function parseSendToArray($to)
1933
    {
1934
        $groupList = [];
1935
        $userList = [];
1936
        $sendTo = null;
1937
1938
        $sendTo['everyone'] = false;
1939
        if (is_array($to) && count($to) > 0) {
1940
            foreach ($to as $item) {
1941
                if ('everyone' == $item) {
1942
                    $sendTo['everyone'] = true;
1943
                } else {
1944
                    [$type, $id] = explode(':', $item);
1945
                    switch ($type) {
1946
                        case 'GROUP':
1947
                            $groupList[] = $id;
1948
                            break;
1949
                        case 'USER':
1950
                            $userList[] = $id;
1951
                            break;
1952
                    }
1953
                }
1954
            }
1955
            $sendTo['groups'] = $groupList;
1956
            $sendTo['users'] = $userList;
1957
        }
1958
1959
        return $sendTo;
1960
    }
1961
1962
    /**
1963
     * @param array $params
1964
     *
1965
     * @return FormValidator
1966
     */
1967
    public function getForm($params = [])
1968
    {
1969
        $action = isset($params['action']) ? Security::remove_XSS($params['action']) : null;
1970
        $id = isset($params['id']) ? (int) $params['id'] : 0;
1971
1972
        $url = api_get_self().'?action='.$action.'&id='.$id.'&type='.$this->type;
1973
        if ('course' === $this->type) {
1974
            $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&id='.$id.'&type='.$this->type;
1975
        }
1976
1977
        $form = new FormValidator(
1978
            'add_event',
1979
            'post',
1980
            $url,
1981
            null,
1982
            ['enctype' => 'multipart/form-data']
1983
        );
1984
1985
        $idAttach = isset($params['id_attach']) ? (int) $params['id_attach'] : null;
1986
        $groupId = api_get_group_id();
1987
        $form_Title = get_lang('Add event to agenda');
1988
        if (!empty($id)) {
1989
            $form_Title = get_lang('Edit event');
1990
        }
1991
1992
        $form->addHeader($form_Title);
1993
        $form->addHidden('id', $id);
1994
        $form->addHidden('action', $action);
1995
        $form->addHidden('id_attach', $idAttach);
1996
1997
        $isSubEventEdition = false;
1998
        $isParentFromSerie = false;
1999
        $showAttachmentForm = true;
2000
2001
        if ('course' === $this->type) {
2002
            // Edition mode.
2003
            if (!empty($id)) {
2004
                $showAttachmentForm = false;
2005
                if (isset($params['parent_event_id']) && !empty($params['parent_event_id'])) {
2006
                    $isSubEventEdition = true;
2007
                }
2008
                if (!empty($params['repeat_info'])) {
2009
                    $isParentFromSerie = true;
2010
                }
2011
            }
2012
        }
2013
2014
        if ($isSubEventEdition) {
2015
            $form->addElement(
2016
                'label',
2017
                null,
2018
                Display::return_message(
2019
                    get_lang('Editing this event will remove it from the serie of events it is currently part of'),
2020
                    'warning'
2021
                )
2022
            );
2023
        }
2024
2025
        $form->addElement('text', 'title', get_lang('Event name'));
2026
2027
        if (isset($groupId) && !empty($groupId)) {
2028
            $form->addElement(
2029
                'hidden',
2030
                'users_to_send[]',
2031
                "GROUP:$groupId"
2032
            );
2033
            $form->addElement('hidden', 'to', 'true');
2034
        } else {
2035
            $sendTo = isset($params['send_to']) ? $params['send_to'] : ['everyone' => true];
2036
            if ('course' === $this->type) {
2037
                $this->showToForm($form, $sendTo, [], false, true);
2038
            }
2039
        }
2040
2041
        $form->addDateRangePicker(
2042
            'date_range',
2043
            get_lang('Date range'),
2044
            false,
2045
            ['id' => 'date_range']
2046
        );
2047
        $form->addElement('checkbox', 'all_day', null, get_lang('All day'));
2048
2049
        if ('course' === $this->type) {
2050
            $repeat = $form->addElement(
2051
                'checkbox',
2052
                'repeat',
2053
                null,
2054
                get_lang('Repeat event'),
2055
                ['onclick' => 'return plus_repeated_event();']
2056
            );
2057
            $form->addElement(
2058
                'html',
2059
                '<div id="options2" style="display:none">'
2060
            );
2061
            $form->addSelect(
2062
                'repeat_type',
2063
                get_lang('Repeat type'),
2064
                self::getRepeatTypes()
2065
            );
2066
            $form->addElement(
2067
                'date_picker',
2068
                'repeat_end_day',
2069
                get_lang('Repeat end date'),
2070
                ['id' => 'repeat_end_date_form']
2071
            );
2072
2073
            if ($isSubEventEdition || $isParentFromSerie) {
2074
                $repeatInfo = $params['repeat_info'];
2075
                if ($isSubEventEdition) {
2076
                    $parentEvent = $params['parent_info'];
2077
                    $repeatInfo = $parentEvent['repeat_info'];
2078
                }
2079
                $params['repeat'] = 1;
2080
                $params['repeat_type'] = $repeatInfo['cal_type'];
2081
                $params['repeat_end_day'] = substr(
2082
                    api_get_local_time($repeatInfo['cal_end']),
2083
                    0,
2084
                    10
2085
                );
2086
2087
                $form->freeze(['repeat_type', 'repeat_end_day']);
2088
                $repeat->_attributes['disabled'] = 'disabled';
2089
            }
2090
            $form->addElement('html', '</div>');
2091
        }
2092
2093
        if (!empty($id)) {
2094
            if (empty($params['end_date'])) {
2095
                $params['date_range'] = $params['end_date'];
2096
            }
2097
2098
            $params['date_range'] =
2099
                substr(api_get_local_time($params['start_date']), 0, 16).' / '.
2100
                substr(api_get_local_time($params['end_date']), 0, 16);
2101
        }
2102
2103
        $toolbar = 'Agenda';
2104
        if (!api_is_allowed_to_edit(null, true)) {
2105
            $toolbar = 'AgendaStudent';
2106
        }
2107
2108
        $form->addHtmlEditor(
2109
            'content',
2110
            get_lang('Description'),
2111
            null,
2112
            [
2113
                'ToolbarSet' => $toolbar,
2114
                'Width' => '100%',
2115
                'Height' => '200',
2116
            ]
2117
        );
2118
2119
        if ('course' === $this->type) {
2120
            $form->addTextarea('comment', get_lang('Comment'));
2121
            $form->addLabel(
2122
                get_lang('Files attachments'),
2123
                '<div id="filepaths" class="file-upload-event">
2124
2125
                        <div id="filepath_1">
2126
                            <input type="file" name="attach_1"/>
2127
2128
                            <label>'.get_lang('Description').'</label>
2129
                            <input class="form-control" type="text" name="legend[]" />
2130
                        </div>
2131
2132
                    </div>'
2133
            );
2134
2135
            $form->addLabel(
2136
                '',
2137
                '<span id="link-more-attach">
2138
                    <a href="javascript://" onclick="return add_image_form()">'.
2139
                get_lang('Add one more file').'</a>
2140
                 </span>&nbsp;('.sprintf(
2141
                    get_lang('Maximun file size: %s'),
2142
                    format_file_size(
2143
                        api_get_setting('message_max_upload_filesize')
2144
                    )
2145
                ).')'
2146
            );
2147
2148
            if (isset($params['attachment']) && !empty($params['attachment'])) {
2149
                $attachmentList = $params['attachment'];
2150
                /** @var CCalendarEventAttachment $attachment */
2151
                foreach ($attachmentList as $attachment) {
2152
                    $form->addElement(
2153
                        'checkbox',
2154
                        'delete_attachment['.$attachment->getIid().']',
2155
                        null,
2156
                        get_lang('Delete attachment').': '.$attachment->getFilename()
2157
                    );
2158
                }
2159
            }
2160
2161
            $form->addTextarea(
2162
                'file_comment',
2163
                get_lang('File comment')
2164
            );
2165
        }
2166
2167
        if (empty($id)) {
2168
            $form->addElement(
2169
                'checkbox',
2170
                'add_announcement',
2171
                null,
2172
                get_lang('Add an announcement').'&nbsp('.get_lang('Send mail').')'
2173
            );
2174
        }
2175
2176
        if ($id) {
2177
            $form->addButtonUpdate(get_lang('Edit event'));
2178
        } else {
2179
            $form->addButtonSave(get_lang('Add event'));
2180
        }
2181
2182
        $form->setDefaults($params);
2183
        $form->addRule(
2184
            'date_range',
2185
            get_lang('Required field'),
2186
            'required'
2187
        );
2188
        $form->addRule('title', get_lang('Required field'), 'required');
2189
2190
        return $form;
2191
    }
2192
2193
    /**
2194
     * @param FormValidator $form
2195
     * @param array         $sendTo               array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
2196
     * @param array         $attributes
2197
     * @param bool          $addOnlyItemsInSendTo
2198
     * @param bool          $required
2199
     *
2200
     * @return bool
2201
     */
2202
    public function showToForm(
2203
        $form,
2204
        $sendTo = [],
2205
        $attributes = [],
2206
        $addOnlyItemsInSendTo = false,
2207
        $required = false
2208
    ) {
2209
        if ('course' !== $this->type) {
2210
            return false;
2211
        }
2212
2213
        $order = 'lastname';
2214
        if (api_is_western_name_order()) {
2215
            $order = 'firstname';
2216
        }
2217
2218
        $userList = CourseManager::get_user_list_from_course_code(
2219
            api_get_course_id(),
2220
            $this->sessionId,
2221
            null,
2222
            $order
2223
        );
2224
2225
        $groupList = CourseManager::get_group_list_of_course(
2226
            api_get_course_id(),
2227
            $this->sessionId
2228
        );
2229
2230
        $this->setSendToSelect(
2231
            $form,
2232
            $groupList,
2233
            $userList,
2234
            $sendTo,
2235
            $attributes,
2236
            $addOnlyItemsInSendTo,
2237
            $required
2238
        );
2239
2240
        return true;
2241
    }
2242
2243
    /**
2244
     * @param int   $id
2245
     * @param int   $visibility 0= invisible, 1 visible
2246
     * @param array $courseInfo
2247
     * @param int   $userId
2248
     */
2249
    public static function changeVisibility(
2250
        $id,
2251
        $visibility,
2252
        $courseInfo,
2253
        $userId = null
2254
    ) {
2255
        $id = (int) $id;
2256
2257
        $repo = Container::getCalendarEventRepository();
2258
        /** @var CCalendarEvent $event */
2259
        $event = $repo->find($id);
2260
        $visibility = (int) $visibility;
2261
2262
        if ($event) {
2263
            if (0 === $visibility) {
2264
                $repo->setVisibilityDraft($event);
2265
            } else {
2266
                $repo->setVisibilityPublished($event);
2267
            }
2268
        }
2269
2270
        return true;
2271
    }
2272
2273
    /**
2274
     * Get repeat types.
2275
     */
2276
    public static function getRepeatTypes(): array
2277
    {
2278
        return [
2279
            'daily' => get_lang('Daily'),
2280
            'weekly' => get_lang('Weekly'),
2281
            'monthlyByDate' => get_lang('Monthly, by date'),
2282
            //monthlyByDay"> get_lang('Monthly, by day');
2283
            //monthlyByDayR' => get_lang('Monthly, by dayR'),
2284
            'yearly' => get_lang('Yearly'),
2285
        ];
2286
    }
2287
2288
    /**
2289
     * Show a list with all the attachments according to the post's id.
2290
     *
2291
     * @param int   $attachmentId
2292
     * @param int   $eventId
2293
     * @param array $courseInfo
2294
     *
2295
     * @return array with the post info
2296
     */
2297
    public function getAttachment($attachmentId, $eventId, $courseInfo)
2298
    {
2299
        if (empty($courseInfo) || empty($attachmentId) || empty($eventId)) {
2300
            return [];
2301
        }
2302
2303
        $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
2304
        $courseId = (int) $courseInfo['real_id'];
2305
        $eventId = (int) $eventId;
2306
        $attachmentId = (int) $attachmentId;
2307
2308
        $row = [];
2309
        $sql = "SELECT iid, path, filename, comment
2310
                FROM $tableAttachment
2311
                WHERE
2312
                    c_id = $courseId AND
2313
                    agenda_id = $eventId AND
2314
                    iid = $attachmentId
2315
                ";
2316
        $result = Database::query($sql);
2317
        if (0 != Database::num_rows($result)) {
2318
            $row = Database::fetch_array($result, 'ASSOC');
2319
        }
2320
2321
        return $row;
2322
    }
2323
2324
    /**
2325
     * Add an attachment file into agenda.
2326
     *
2327
     * @param CCalendarEvent $event
2328
     * @param UploadedFile   $file
2329
     * @param string         $comment
2330
     * @param array          $courseInfo
2331
     *
2332
     * @return string
2333
     */
2334
    public function addAttachment(
2335
        $event,
2336
        $file,
2337
        $comment,
2338
        $courseInfo
2339
    ) {
2340
        // Storing the attachments
2341
        $valid = false;
2342
        if ($file) {
2343
            $valid = process_uploaded_file($file);
2344
        }
2345
2346
        if ($valid) {
2347
            // user's file name
2348
            $fileName = $file->getClientOriginalName();
2349
            $em = Database::getManager();
2350
            $attachment = (new CCalendarEventAttachment())
2351
                ->setFilename($fileName)
2352
                ->setComment($comment)
2353
                ->setEvent($event)
2354
                ->setParent($event)
2355
                ->addCourseLink(
2356
                    api_get_course_entity(),
2357
                    api_get_session_entity(),
2358
                    api_get_group_entity()
2359
                )
2360
            ;
2361
2362
            $repo = Container::getCalendarEventAttachmentRepository();
2363
            $em->persist($attachment);
2364
            $em->flush();
2365
2366
            $repo->addFile($attachment, $file);
2367
            $em->persist($attachment);
2368
            $em->flush();
2369
        }
2370
    }
2371
2372
    /**
2373
     * @param int    $attachmentId
2374
     * @param int    $eventId
2375
     * @param array  $fileUserUpload
2376
     * @param string $comment
2377
     * @param array  $courseInfo
2378
     */
2379
    public function updateAttachment(
2380
        $attachmentId,
2381
        $eventId,
2382
        $fileUserUpload,
2383
        $comment,
2384
        $courseInfo
2385
    ) {
2386
        $attachment = $this->getAttachment(
2387
            $attachmentId,
2388
            $eventId,
2389
            $courseInfo
2390
        );
2391
        if (!empty($attachment)) {
2392
            $this->deleteAttachmentFile($attachmentId);
2393
        }
2394
        $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
2395
    }
2396
2397
    /**
2398
     * This function delete a attachment file by id.
2399
     *
2400
     * @param int $attachmentId
2401
     *
2402
     * @return string
2403
     */
2404
    public function deleteAttachmentFile($attachmentId)
2405
    {
2406
        $repo = Container::getCalendarEventAttachmentRepository();
2407
        /** @var CCalendarEventAttachment $attachment */
2408
        $attachment = $repo->find($attachmentId);
2409
        $em = Database::getManager();
2410
        if (empty($attachment)) {
2411
            return false;
2412
        }
2413
2414
        $em->remove($attachment);
2415
        $em->flush();
2416
2417
        return Display::return_message(
2418
            get_lang("The attached file has been deleted"),
2419
            'confirmation'
2420
        );
2421
    }
2422
2423
    /**
2424
     * @param int $eventId
2425
     *
2426
     * @return array
2427
     */
2428
    public function getAllRepeatEvents($eventId)
2429
    {
2430
        $events = [];
2431
        $eventId = (int) $eventId;
2432
2433
        switch ($this->type) {
2434
            case 'personal':
2435
                break;
2436
            case 'course':
2437
                if (!empty($this->course['real_id'])) {
2438
                    $sql = "SELECT * FROM ".$this->tbl_course_agenda."
2439
                            WHERE
2440
                                c_id = ".$this->course['real_id']." AND
2441
                                parent_event_id = ".$eventId;
2442
                    $result = Database::query($sql);
2443
                    if (Database::num_rows($result)) {
2444
                        while ($row = Database::fetch_array($result, 'ASSOC')) {
2445
                            $events[] = $row;
2446
                        }
2447
                    }
2448
                }
2449
                break;
2450
        }
2451
2452
        return $events;
2453
    }
2454
2455
    /**
2456
     * @param int    $filter
2457
     * @param string $view
2458
     *
2459
     * @return string
2460
     */
2461
    public function displayActions($view, $filter = 0)
2462
    {
2463
        $group = api_get_group_entity();
2464
        $groupIid = null === $group ? 0 : $group->getIid();
2465
        $codePath = api_get_path(WEB_CODE_PATH);
2466
2467
        $currentUserId = api_get_user_id();
2468
        $cidReq = api_get_cidreq();
2469
2470
        $actionsLeft = '';
2471
        $actionsLeft .= Display::url(
2472
            Display::getMdiIcon(ObjectIcon::AGENDA, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Calendar')),
2473
            $codePath."calendar/agenda_js.php?type={$this->type}&$cidReq"
2474
        );
2475
        $actionsLeft .= Display::url(
2476
            Display::getMdiIcon(ObjectIcon::AGENDA_WEEK, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Agenda list')),
2477
            $codePath."calendar/agenda_list.php?type={$this->type}&$cidReq"
2478
        );
2479
2480
        $form = '';
2481
        if (api_is_allowed_to_edit(false, true) ||
2482
            ('1' == api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous()) &&
2483
            api_is_allowed_to_session_edit(false, true)
2484
            || (
2485
                GroupManager::userHasAccess($currentUserId, $group, GroupManager::GROUP_TOOL_CALENDAR)
2486
                && GroupManager::isTutorOfGroup($currentUserId, $group)
2487
            )
2488
        ) {
2489
            $actionsLeft .= Display::url(
2490
                Display::getMdiIcon(ObjectIcon::AGENDA_EVENT, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Add event')),
2491
                $codePath."calendar/agenda.php?action=add&type={$this->type}&$cidReq"
2492
            );
2493
2494
            $actionsLeft .= Display::url(
2495
                Display::getMdiIcon(ActionIcon::IMPORT_ARCHIVE, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Outlook import')),
2496
                $codePath."calendar/agenda.php?action=importical&type={$this->type}&$cidReq"
2497
            );
2498
2499
            if ('course' === $this->type) {
2500
                if (!isset($_GET['action'])) {
2501
                    $form = new FormValidator(
2502
                        'form-search',
2503
                        'post',
2504
                        '',
2505
                        '',
2506
                        [],
2507
                        FormValidator::LAYOUT_INLINE
2508
                    );
2509
                    $attributes = [
2510
                        'multiple' => false,
2511
                        'id' => 'select_form_id_search',
2512
                    ];
2513
                    $selectedValues = $this->parseAgendaFilter($filter);
2514
                    $this->showToForm($form, $selectedValues, $attributes);
2515
                    $form = $form->returnForm();
2516
                }
2517
            }
2518
        }
2519
2520
        if ('personal' === $this->type && !api_is_anonymous()) {
2521
            $actionsLeft .= Display::url(
2522
                Display::getMdiIcon(ObjectIcon::AGENDA_PLAN, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Sessions plan calendar')),
2523
                $codePath."calendar/planification.php"
2524
            );
2525
2526
            if (api_is_student_boss() || api_is_platform_admin()) {
2527
                $actionsLeft .= Display::url(
2528
                    Display::getMdiIcon(ObjectIcon::AGENDA_USER_EVENT, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('MyStudentsSchedule')),
2529
                    $codePath.'my_space/calendar_plan.php'
2530
                );
2531
            }
2532
        }
2533
2534
        if (api_is_platform_admin() ||
2535
            api_is_teacher() ||
2536
            api_is_student_boss() ||
2537
            api_is_drh() ||
2538
            api_is_session_admin() ||
2539
            api_is_coach()
2540
        ) {
2541
            if ('personal' === $this->type) {
2542
                $form = null;
2543
                if (!isset($_GET['action'])) {
2544
                    $form = new FormValidator(
2545
                        'form-search',
2546
                        'get',
2547
                        api_get_self().'?type=personal&',
2548
                        '',
2549
                        [],
2550
                        FormValidator::LAYOUT_INLINE
2551
                    );
2552
2553
                    $sessions = [];
2554
2555
                    if (api_is_drh()) {
2556
                        $sessionList = SessionManager::get_sessions_followed_by_drh($currentUserId);
2557
                        if (!empty($sessionList)) {
2558
                            foreach ($sessionList as $sessionItem) {
2559
                                $sessions[$sessionItem['id']] = strip_tags($sessionItem['name']);
2560
                            }
2561
                        }
2562
                    } else {
2563
                        $sessions = SessionManager::get_sessions_by_user($currentUserId);
2564
                        $sessions = array_column($sessions, 'session_name', 'session_id');
2565
                    }
2566
2567
                    $form->addHidden('type', 'personal');
2568
                    $sessions = ['0' => get_lang('Please select an option')] + $sessions;
2569
2570
                    $form->addSelect(
2571
                        'session_id',
2572
                        get_lang('Session'),
2573
                        $sessions,
2574
                        ['id' => 'session_id', 'onchange' => 'submit();']
2575
                    );
2576
2577
                    $form->addButtonReset(get_lang('Reset'));
2578
                    $form = $form->returnForm();
2579
                }
2580
            }
2581
        }
2582
2583
        $actionsRight = '';
2584
        if ('calendar' === $view) {
2585
            $actionsRight .= $form;
2586
        }
2587
2588
        return Display::toolbarAction(
2589
            'toolbar-agenda',
2590
            [$actionsLeft, $actionsRight]
2591
        );
2592
    }
2593
2594
    /**
2595
     * @return FormValidator
2596
     */
2597
    public function getImportCalendarForm()
2598
    {
2599
        $form = new FormValidator(
2600
            'frm_import_ical',
2601
            'post',
2602
            api_get_self().'?action=importical&type='.$this->type,
2603
            ['enctype' => 'multipart/form-data']
2604
        );
2605
        $form->addHeader(get_lang('Outlook import'));
2606
        $form->addElement('file', 'ical_import', get_lang('Outlook import'));
2607
        $form->addRule(
2608
            'ical_import',
2609
            get_lang('Required field'),
2610
            'required'
2611
        );
2612
        $form->addButtonImport(get_lang('Import'), 'ical_submit');
2613
2614
        return $form;
2615
    }
2616
2617
    /**
2618
     * @param array $courseInfo
2619
     * @param $file
2620
     *
2621
     * @return false|string
2622
     */
2623
    public function importEventFile($courseInfo, $file)
2624
    {
2625
        $charset = api_get_system_encoding();
2626
        $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
2627
        $messages = [];
2628
2629
        if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
2630
            error_log(
2631
                'Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__
2632
            );
2633
2634
            return false;
2635
        }
2636
2637
        $data = file_get_contents($filepath);
2638
2639
        $trans = [
2640
            'DAILY' => 'daily',
2641
            'WEEKLY' => 'weekly',
2642
            'MONTHLY' => 'monthlyByDate',
2643
            'YEARLY' => 'yearly',
2644
        ];
2645
        $sentTo = ['everyone' => true];
2646
        $calendar = Sabre\VObject\Reader::read($data);
2647
        $currentTimeZone = api_get_timezone();
2648
        if (!empty($calendar->VEVENT)) {
2649
            foreach ($calendar->VEVENT as $event) {
2650
                $start = $event->DTSTART->getDateTime();
2651
                $end = $event->DTEND->getDateTime();
2652
                //Sabre\VObject\DateTimeParser::parseDateTime(string $dt, \Sabre\VObject\DateTimeZone $tz)
2653
2654
                $startDateTime = api_get_local_time(
2655
                    $start->format('Y-m-d H:i:s'),
2656
                    $currentTimeZone,
2657
                    $start->format('e')
2658
                );
2659
                $endDateTime = api_get_local_time(
2660
                    $end->format('Y-m-d H:i'),
2661
                    $currentTimeZone,
2662
                    $end->format('e')
2663
                );
2664
                $title = api_convert_encoding(
2665
                    (string) $event->summary,
2666
                    $charset,
2667
                    'UTF-8'
2668
                );
2669
                $description = api_convert_encoding(
2670
                    (string) $event->description,
2671
                    $charset,
2672
                    'UTF-8'
2673
                );
2674
2675
                $id = $this->addEvent(
2676
                    $startDateTime,
2677
                    $endDateTime,
2678
                    'false',
2679
                    $title,
2680
                    $description,
2681
                    $sentTo
2682
                );
2683
2684
                $messages[] = " $title - ".$startDateTime." - ".$endDateTime;
2685
2686
                //$attendee = (string)$event->attendee;
2687
                /** @var Sabre\VObject\Property\ICalendar\Recur $repeat */
2688
                $repeat = $event->RRULE;
2689
                if ($id && !empty($repeat)) {
2690
                    $repeat = $repeat->getParts();
2691
                    $freq = $trans[$repeat['FREQ']];
2692
2693
                    if (isset($repeat['UNTIL']) && !empty($repeat['UNTIL'])) {
2694
                        // Check if datetime or just date (strlen == 8)
2695
                        if (8 == strlen($repeat['UNTIL'])) {
2696
                            // Fix the datetime format to avoid exception in the next step
2697
                            $repeat['UNTIL'] .= 'T000000';
2698
                        }
2699
                        $until = Sabre\VObject\DateTimeParser::parseDateTime(
2700
                            $repeat['UNTIL'],
2701
                            new DateTimeZone($currentTimeZone)
2702
                        );
2703
                        $until = $until->format('Y-m-d H:i:s');
2704
                        $this->addRepeatedItem(
2705
                            $id,
2706
                            $freq,
2707
                            $until,
2708
                            $sentTo
2709
                        );
2710
                    }
2711
                }
2712
            }
2713
        }
2714
2715
        if (!empty($messages)) {
2716
            $messages = implode('<br /> ', $messages);
2717
        } else {
2718
            $messages = get_lang('There are no events');
2719
        }
2720
2721
        return $messages;
2722
    }
2723
2724
    /**
2725
     * Parse filter turns USER:12 to ['users' => [12])] or G:1 ['groups' => [1]].
2726
     *
2727
     * @param int $filter
2728
     *
2729
     * @return array
2730
     */
2731
    public function parseAgendaFilter($filter)
2732
    {
2733
        $everyone = false;
2734
        $groupId = null;
2735
        $userId = null;
2736
2737
        if ('everyone' === $filter) {
2738
            $everyone = true;
2739
        } else {
2740
            if ('G' === substr($filter, 0, 1)) {
2741
                $groupId = str_replace('GROUP:', '', $filter);
2742
            } else {
2743
                $userId = str_replace('USER:', '', $filter);
2744
            }
2745
        }
2746
        if (empty($userId) && empty($groupId)) {
2747
            $everyone = true;
2748
        }
2749
2750
        return [
2751
            'everyone' => $everyone,
2752
            'users' => [$userId],
2753
            'groups' => [$groupId],
2754
        ];
2755
    }
2756
2757
    /**
2758
     * @deprecated
2759
     *    This function retrieves all the agenda items of all the courses the user is subscribed to.
2760
     */
2761
    public static function get_myagendaitems(
2762
        $user_id,
2763
        $courses_dbs,
2764
        $month,
2765
        $year
2766
    ) {
2767
        $user_id = (int) $user_id;
2768
2769
        $items = [];
2770
        $my_list = [];
2771
        throw new Exception('@todo get_myagendaitems');
2772
        /*
2773
        // get agenda-items for every course
2774
        foreach ($courses_dbs as $key => $array_course_info) {
2775
            //databases of the courses
2776
            $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
2777
            $TABLE_ITEMPROPERTY = Database::get_course_table(
2778
                TABLE_ITEM_PROPERTY
2779
            );
2780
2781
            $group_memberships = GroupManager::get_group_ids(
2782
                $array_course_info['real_id'],
2783
                $user_id
2784
            );
2785
            $course_user_status = CourseManager::getUserInCourseStatus(
2786
                $user_id,
2787
                $array_course_info['real_id']
2788
            );
2789
            // if the user is administrator of that course we show all the agenda items
2790
            if ('1' == $course_user_status) {
2791
                //echo "course admin";
2792
                $sqlquery = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
2793
                            FROM ".$TABLEAGENDA." agenda,
2794
                                 ".$TABLE_ITEMPROPERTY." ip
2795
                            WHERE agenda.id = ip.ref
2796
                            AND MONTH(agenda.start_date)='".$month."'
2797
                            AND YEAR(agenda.start_date)='".$year."'
2798
                            AND ip.tool='".TOOL_CALENDAR_EVENT."'
2799
                            AND ip.visibility='1'
2800
                            GROUP BY agenda.id
2801
                            ORDER BY start_date ";
2802
            } else {
2803
                // if the user is not an administrator of that course
2804
                if (is_array($group_memberships) && count(
2805
                        $group_memberships
2806
                    ) > 0
2807
                ) {
2808
                    $sqlquery = "SELECT	agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
2809
                                FROM ".$TABLEAGENDA." agenda,
2810
                                    ".$TABLE_ITEMPROPERTY." ip
2811
                                WHERE agenda.id = ip.ref
2812
                                AND MONTH(agenda.start_date)='".$month."'
2813
                                AND YEAR(agenda.start_date)='".$year."'
2814
                                AND ip.tool='".TOOL_CALENDAR_EVENT."'
2815
                                AND	( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
2816
                            ", ",
2817
                            $group_memberships
2818
                        ).")) )
2819
                                AND ip.visibility='1'
2820
                                ORDER BY start_date ";
2821
                } else {
2822
                    $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
2823
                                FROM ".$TABLEAGENDA." agenda,
2824
                                    ".$TABLE_ITEMPROPERTY." ip
2825
                                WHERE agenda.id = ip.ref
2826
                                AND MONTH(agenda.start_date)='".$month."'
2827
                                AND YEAR(agenda.start_date)='".$year."'
2828
                                AND ip.tool='".TOOL_CALENDAR_EVENT."'
2829
                                AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
2830
                                AND ip.visibility='1'
2831
                                ORDER BY start_date ";
2832
                }
2833
            }
2834
            $result = Database::query($sqlquery);
2835
2836
            while ($item = Database::fetch_array($result, 'ASSOC')) {
2837
                $agendaday = -1;
2838
                if (!empty($item['start_date'])) {
2839
                    $item['start_date'] = api_get_local_time(
2840
                        $item['start_date']
2841
                    );
2842
                    $item['start_date_tms'] = api_strtotime(
2843
                        $item['start_date']
2844
                    );
2845
                    $agendaday = date("j", $item['start_date_tms']);
2846
                }
2847
                if (!empty($item['end_date'])) {
2848
                    $item['end_date'] = api_get_local_time($item['end_date']);
2849
                }
2850
2851
                $url = api_get_path(
2852
                        WEB_CODE_PATH
2853
                    )."calendar/agenda.php?cidReq=".urlencode(
2854
                        $array_course_info["code"]
2855
                    )."&day=$agendaday&month=$month&year=$year#$agendaday";
2856
2857
                $item['url'] = $url;
2858
                $item['course_name'] = $array_course_info['title'];
2859
                $item['calendar_type'] = 'course';
2860
                $item['course_id'] = $array_course_info['course_id'];
2861
2862
                $my_list[$agendaday][] = $item;
2863
            }
2864
        }
2865
2866
        // sorting by hour for every day
2867
        $agendaitems = [];
2868
        foreach ($items as $agendaday => $tmpitems) {
2869
            if (!isset($agendaitems[$agendaday])) {
2870
                $agendaitems[$agendaday] = '';
2871
            }
2872
            sort($tmpitems);
2873
            foreach ($tmpitems as $val) {
2874
                $agendaitems[$agendaday] .= $val;
2875
            }
2876
        }*/
2877
2878
        return $my_list;
0 ignored issues
show
Unused Code introduced by
return $my_list is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
2879
    }
2880
2881
    /**
2882
     * This function retrieves one personal agenda item returns it.
2883
     *
2884
     * @param    array    The array containing existing events. We add to this array.
2885
     * @param    int        Day
2886
     * @param    int        Month
2887
     * @param    int        Year (4 digits)
2888
     * @param    int        Week number
2889
     * @param    string    Type of view (month_view, week_view, day_view)
2890
     *
2891
     * @return array The results of the database query, or null if not found
2892
     */
2893
    public static function get_global_agenda_items(
2894
        $agendaitems,
2895
        $day,
2896
        $month,
2897
        $year,
2898
        $week,
2899
        $type
2900
    ) {
2901
        $tbl_global_agenda = Database::get_main_table(
2902
            TABLE_MAIN_SYSTEM_CALENDAR
2903
        );
2904
        $month = intval($month);
2905
        $year = intval($year);
2906
        $week = intval($week);
2907
        $day = intval($day);
2908
        // 1. creating the SQL statement for getting the personal agenda items in MONTH view
2909
2910
        $current_access_url_id = api_get_current_access_url_id();
2911
2912
        if ("month_view" == $type or "" == $type) {
2913
            // We are in month view
2914
            $sql = "SELECT * FROM ".$tbl_global_agenda." WHERE MONTH(start_date) = ".$month." AND YEAR(start_date) = ".$year."  AND access_url_id = $current_access_url_id ORDER BY start_date ASC";
2915
        }
2916
        // 2. creating the SQL statement for getting the personal agenda items in WEEK view
2917
        if ("week_view" == $type) { // we are in week view
2918
            $start_end_day_of_week = self::calculate_start_end_of_week(
2919
                $week,
2920
                $year
2921
            );
2922
            $start_day = $start_end_day_of_week['start']['day'];
2923
            $start_month = $start_end_day_of_week['start']['month'];
2924
            $start_year = $start_end_day_of_week['start']['year'];
2925
            $end_day = $start_end_day_of_week['end']['day'];
2926
            $end_month = $start_end_day_of_week['end']['month'];
2927
            $end_year = $start_end_day_of_week['end']['year'];
2928
            // in sql statements you have to use year-month-day for date calculations
2929
            $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
2930
            $start_filter = api_get_utc_datetime($start_filter);
2931
2932
            $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
2933
            $end_filter = api_get_utc_datetime($end_filter);
2934
            $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND  access_url_id = $current_access_url_id ";
2935
        }
2936
        // 3. creating the SQL statement for getting the personal agenda items in DAY view
2937
        if ("day_view" == $type) { // we are in day view
2938
            // we could use mysql date() function but this is only available from 4.1 and higher
2939
            $start_filter = $year."-".$month."-".$day." 00:00:00";
2940
            $start_filter = api_get_utc_datetime($start_filter);
2941
2942
            $end_filter = $year."-".$month."-".$day." 23:59:59";
2943
            $end_filter = api_get_utc_datetime($end_filter);
2944
            $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."'  AND  access_url_id = $current_access_url_id";
2945
        }
2946
2947
        $result = Database::query($sql);
2948
2949
        while ($item = Database::fetch_array($result)) {
2950
            if (!empty($item['start_date'])) {
2951
                $item['start_date'] = api_get_local_time($item['start_date']);
2952
                $item['start_date_tms'] = api_strtotime($item['start_date']);
2953
            }
2954
            if (!empty($item['end_date'])) {
2955
                $item['end_date'] = api_get_local_time($item['end_date']);
2956
            }
2957
2958
            // we break the date field in the database into a date and a time part
2959
            $agenda_db_date = explode(" ", $item['start_date']);
2960
            $date = $agenda_db_date[0];
2961
            $time = $agenda_db_date[1];
2962
            // we divide the date part into a day, a month and a year
2963
            $agendadate = explode("-", $date);
2964
            $year = intval($agendadate[0]);
2965
            $month = intval($agendadate[1]);
2966
            $day = intval($agendadate[2]);
2967
            // we divide the time part into hour, minutes, seconds
2968
            $agendatime = explode(":", $time);
2969
            $hour = $agendatime[0];
2970
            $minute = $agendatime[1];
2971
            $second = $agendatime[2];
2972
2973
            if ('month_view' == $type) {
2974
                $item['calendar_type'] = 'global';
2975
                $agendaitems[$day][] = $item;
2976
                continue;
2977
            }
2978
2979
            $start_time = api_format_date(
2980
                $item['start_date'],
2981
                TIME_NO_SEC_FORMAT
2982
            );
2983
            $end_time = '';
2984
            if (!empty($item['end_date'])) {
2985
                $end_time = ' - '.api_format_date(
2986
                        $item['end_date'],
2987
                        DATE_TIME_FORMAT_LONG
2988
                    );
2989
            }
2990
2991
            // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
2992
            // if we have a day_view we use a half hour as index => key 33 = 16h30
2993
            if ("day_view" !== $type) {
2994
                // This is the array construction for the WEEK or MONTH view
2995
                //Display the Agenda global in the tab agenda (administrator)
2996
                $agendaitems[$day] .= "<i>$start_time $end_time</i>&nbsp;-&nbsp;";
2997
                $agendaitems[$day] .= "<b>".get_lang('Platform event')."</b>";
2998
                $agendaitems[$day] .= "<div>".$item['title']."</div><br>";
2999
            } else {
3000
                // this is the array construction for the DAY view
3001
                $halfhour = 2 * $agendatime['0'];
3002
                if ($agendatime['1'] >= '30') {
3003
                    $halfhour = $halfhour + 1;
3004
                }
3005
                if (!is_array($agendaitems[$halfhour])) {
3006
                    $content = $agendaitems[$halfhour];
3007
                }
3008
                $agendaitems[$halfhour] = $content."<div><i>$hour:$minute</i> <b>".get_lang(
3009
                        'Platform event'
3010
                    ).":  </b>".$item['title']."</div>";
3011
            }
3012
        }
3013
3014
        return $agendaitems;
3015
    }
3016
3017
    /**
3018
     * This function retrieves all the personal agenda items and add them to the agenda items found by the other
3019
     * functions.
3020
     */
3021
    public static function get_personal_agenda_items(
3022
        $user_id,
3023
        $agendaitems,
3024
        $day,
3025
        $month,
3026
        $year,
3027
        $week,
3028
        $type
3029
    ) {
3030
        $tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
3031
        $user_id = (int) $user_id;
3032
        $course_link = '';
3033
        // 1. creating the SQL statement for getting the personal agenda items in MONTH view
3034
        if ("month_view" === $type || "" == $type) {
3035
            // we are in month view
3036
            $sql = "SELECT * FROM ".$tbl_personal_agenda."
3037
                    WHERE user='".$user_id."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."'
3038
                    ORDER BY date ASC";
3039
        }
3040
3041
        // 2. creating the SQL statement for getting the personal agenda items in WEEK view
3042
        // we are in week view
3043
        if ("week_view" === $type) {
3044
            $start_end_day_of_week = self::calculate_start_end_of_week(
3045
                $week,
3046
                $year
3047
            );
3048
            $start_day = $start_end_day_of_week['start']['day'];
3049
            $start_month = $start_end_day_of_week['start']['month'];
3050
            $start_year = $start_end_day_of_week['start']['year'];
3051
            $end_day = $start_end_day_of_week['end']['day'];
3052
            $end_month = $start_end_day_of_week['end']['month'];
3053
            $end_year = $start_end_day_of_week['end']['year'];
3054
            // in sql statements you have to use year-month-day for date calculations
3055
            $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
3056
            $start_filter = api_get_utc_datetime($start_filter);
3057
            $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
3058
            $end_filter = api_get_utc_datetime($end_filter);
3059
            $sql = "SELECT * FROM ".$tbl_personal_agenda."
3060
                    WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
3061
        }
3062
        // 3. creating the SQL statement for getting the personal agenda items in DAY view
3063
        if ("day_view" === $type) {
3064
            // we are in day view
3065
            // we could use mysql date() function but this is only available from 4.1 and higher
3066
            $start_filter = $year."-".$month."-".$day." 00:00:00";
3067
            $start_filter = api_get_utc_datetime($start_filter);
3068
            $end_filter = $year."-".$month."-".$day." 23:59:59";
3069
            $end_filter = api_get_utc_datetime($end_filter);
3070
            $sql = "SELECT * FROM ".$tbl_personal_agenda."
3071
                    WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
3072
        }
3073
3074
        $result = Database::query($sql);
3075
        while ($item = Database::fetch_array($result, 'ASSOC')) {
3076
            $time_minute = api_convert_and_format_date(
3077
                $item['date'],
3078
                TIME_NO_SEC_FORMAT
3079
            );
3080
            $item['date'] = api_get_local_time($item['date']);
3081
            $item['start_date_tms'] = api_strtotime($item['date']);
3082
            $item['content'] = $item['text'];
3083
3084
            // we break the date field in the database into a date and a time part
3085
            $agenda_db_date = explode(" ", $item['date']);
3086
            $date = $agenda_db_date[0];
3087
            $time = $agenda_db_date[1];
3088
            // we divide the date part into a day, a month and a year
3089
            $agendadate = explode("-", $item['date']);
3090
            $year = intval($agendadate[0]);
3091
            $month = intval($agendadate[1]);
3092
            $day = intval($agendadate[2]);
3093
            // we divide the time part into hour, minutes, seconds
3094
            $agendatime = explode(":", $time);
3095
3096
            $hour = $agendatime[0];
3097
            $minute = $agendatime[1];
3098
            $second = $agendatime[2];
3099
3100
            if ('month_view' === $type) {
3101
                $item['calendar_type'] = 'personal';
3102
                $item['start_date'] = $item['date'];
3103
                $agendaitems[$day][] = $item;
3104
                continue;
3105
            }
3106
3107
            // Creating the array that will be returned.
3108
            // If we have week or month view we have an array with the date as the key
3109
            // if we have a day_view we use a half hour as index => key 33 = 16h30
3110
            if ("day_view" !== $type) {
3111
                // This is the array construction for the WEEK or MONTH view
3112
3113
                //Display events in agenda
3114
                $agendaitems[$day] .= "<div>
3115
                     <i>$time_minute</i> $course_link
3116
                     <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".
3117
                    $item['title']."</a></div><br />";
3118
            } else {
3119
                // this is the array construction for the DAY view
3120
                $halfhour = 2 * $agendatime['0'];
3121
                if ($agendatime['1'] >= '30') {
3122
                    $halfhour = $halfhour + 1;
3123
                }
3124
3125
                //Display events by list
3126
                $agendaitems[$halfhour] .= "<div>
3127
                    <i>$time_minute</i> $course_link
3128
                    <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div>";
3129
            }
3130
        }
3131
3132
        return $agendaitems;
3133
    }
3134
3135
    /**
3136
     * Show the month calendar of the given month.
3137
     *
3138
     * @param    array    Agendaitems
3139
     * @param    int    Month number
3140
     * @param    int    Year number
3141
     * @param    array    Array of strings containing long week day names (deprecated, you can send an empty array
3142
     *                          instead)
3143
     * @param    string    The month name
3144
     */
3145
    public static function display_mymonthcalendar(
3146
        $user_id,
3147
        $agendaitems,
3148
        $month,
3149
        $year,
3150
        $weekdaynames,
3151
        $monthName,
3152
        $show_content = true
3153
    ) {
3154
        global $DaysShort, $course_path;
3155
        //Handle leap year
3156
        $numberofdays = [
3157
            0,
3158
            31,
3159
            28,
3160
            31,
3161
            30,
3162
            31,
3163
            30,
3164
            31,
3165
            31,
3166
            30,
3167
            31,
3168
            30,
3169
            31,
3170
        ];
3171
        if ((0 == $year % 400) or (0 == $year % 4 and 0 != $year % 100)) {
3172
            $numberofdays[2] = 29;
3173
        }
3174
        //Get the first day of the month
3175
        $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
3176
        //Start the week on monday
3177
        $startdayofweek = 0 != $dayone['wday'] ? ($dayone['wday'] - 1) : 6;
3178
        $g_cc = (isset($_GET['courseCode']) ? $_GET['courseCode'] : '');
3179
3180
        $next_month = (1 == $month ? 12 : $month - 1);
3181
        $prev_month = (12 == $month ? 1 : $month + 1);
3182
3183
        $next_year = (1 == $month ? $year - 1 : $year);
3184
        $prev_year = (12 == $month ? $year + 1 : $year);
3185
3186
        if ($show_content) {
3187
            $back_url = Display::url(
3188
                get_lang('Previous'),
3189
                api_get_self()."?coursePath=".urlencode(
3190
                    $course_path
3191
                )."&courseCode=".Security::remove_XSS(
3192
                    $g_cc
3193
                )."&action=view&view=month&month=".$next_month."&year=".$next_year
3194
            );
3195
            $next_url = Display::url(
3196
                get_lang('Next'),
3197
                api_get_self()."?coursePath=".urlencode(
3198
                    $course_path
3199
                )."&courseCode=".Security::remove_XSS(
3200
                    $g_cc
3201
                )."&action=view&view=month&month=".$prev_month."&year=".$prev_year
3202
            );
3203
        } else {
3204
            $back_url = Display::url(
3205
                get_lang('Previous'),
3206
                '',
3207
                [
3208
                    'onclick' => "load_calendar('".$user_id."','".$next_month."', '".$next_year."'); ",
3209
                    'class' => 'btn ui-button ui-widget ui-state-default',
3210
                ]
3211
            );
3212
            $next_url = Display::url(
3213
                get_lang('Next'),
3214
                '',
3215
                [
3216
                    'onclick' => "load_calendar('".$user_id."','".$prev_month."', '".$prev_year."'); ",
3217
                    'class' => 'pull-right btn ui-button ui-widget ui-state-default',
3218
                ]
3219
            );
3220
        }
3221
        $html = '';
3222
        $html .= '<div class="actions">';
3223
        $html .= '<div class="row">';
3224
        $html .= '<div class="col-md-4">'.$back_url.'</div>';
3225
        $html .= '<div class="col-md-4"><p class="agenda-title text-center">'.$monthName." ".$year.'</p></div>';
3226
        $html .= '<div class="col-md-4">'.$next_url.'</div>';
3227
        $html .= '</div>';
3228
        $html .= '</div>';
3229
        $html .= '<table id="agenda_list2" class="table table-bordered">';
3230
        $html .= '<tr>';
3231
        for ($ii = 1; $ii < 8; $ii++) {
3232
            $html .= '<td class="weekdays">'.$DaysShort[$ii % 7].'</td>';
3233
        }
3234
        $html .= '</tr>';
3235
3236
        $curday = -1;
3237
        $today = getdate();
3238
        while ($curday <= $numberofdays[$month]) {
3239
            $html .= "<tr>";
3240
            for ($ii = 0; $ii < 7; $ii++) {
3241
                if ((-1 == $curday) && ($ii == $startdayofweek)) {
3242
                    $curday = 1;
3243
                }
3244
                if (($curday > 0) && ($curday <= $numberofdays[$month])) {
3245
                    $bgcolor = $class = 'class="days_week"';
3246
                    $dayheader = Display::div(
3247
                        strval($curday),
3248
                        ['class' => 'agenda_day']
3249
                    );
3250
                    if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
3251
                        $class = "class=\"days_today\" style=\"width:10%;\"";
3252
                    }
3253
3254
                    $html .= "<td ".$class.">".$dayheader;
3255
3256
                    if (!empty($agendaitems[$curday])) {
3257
                        $items = $agendaitems[$curday];
3258
                        $items = msort($items, 'start_date_tms');
3259
3260
                        foreach ($items as $value) {
3261
                            $value['title'] = Security::remove_XSS(
3262
                                $value['title']
3263
                            );
3264
                            $start_time = api_format_date(
3265
                                $value['start_date'],
3266
                                TIME_NO_SEC_FORMAT
3267
                            );
3268
                            $end_time = '';
3269
3270
                            if (!empty($value['end_date'])) {
3271
                                $end_time = '-&nbsp;<i>'.api_format_date(
3272
                                        $value['end_date'],
3273
                                        DATE_TIME_FORMAT_LONG
3274
                                    ).'</i>';
3275
                            }
3276
                            $complete_time = '<i>'.api_format_date(
3277
                                    $value['start_date'],
3278
                                    DATE_TIME_FORMAT_LONG
3279
                                ).'</i>&nbsp;'.$end_time;
3280
                            $time = '<i>'.$start_time.'</i>';
3281
3282
                            switch ($value['calendar_type']) {
3283
                                case 'personal':
3284
                                    $bg_color = '#D0E7F4';
3285
                                    $icon = Display::getMdiIcon(ObjectIcon::USER, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Personal agenda'));
3286
                                    break;
3287
                                case 'global':
3288
                                    $bg_color = '#FFBC89';
3289
                                    $icon = Display::getMdiIcon(ObjectIcon::AGENDA_PLATFORM_EVENT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Platform event'));
3290
                                    break;
3291
                                case 'course':
3292
                                    $bg_color = '#CAFFAA';
3293
                                    $icon_name = ObjectIcon::COURSE;
3294
                                    if (!empty($value['session_id'])) {
3295
                                        $icon_name = ObjectIcon::SESSION;
3296
                                    }
3297
                                    if ($show_content) {
3298
                                        $icon = Display::url(
3299
                                            Display::getMdiIcon(
3300
                                                $icon_name,
3301
                                                'ch-tool-icon',
3302
                                                null,
3303
                                                ICON_SIZE_SMALL,
3304
                                                $value['course_name'].' '.get_lang(
3305
                                                    'Course'
3306
                                                )
3307
                                            ),
3308
                                            $value['url']
3309
                                        );
3310
                                    } else {
3311
                                        $icon = Display::getMdiIcon(
3312
                                            $icon_name,
3313
                                            'ch-tool-icon',
3314
                                            null,
3315
                                            ICON_SIZE_SMALL,
3316
                                            $value['course_name'].' '.get_lang(
3317
                                                'Course'
3318
                                            )
3319
                                        );
3320
                                    }
3321
                                    break;
3322
                                default:
3323
                                    break;
3324
                            }
3325
3326
                            $result = '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
3327
3328
                            if ($show_content) {
3329
                                //Setting a personal event to green
3330
                                $icon = Display::div(
3331
                                    $icon,
3332
                                    ['style' => 'float:right']
3333
                                );
3334
3335
                                $link = $value['calendar_type'].'_'.$value['id'].'_'.$value['course_id'].'_'.$value['session_id'];
3336
3337
                                //Link to bubble
3338
                                $url = Display::url(
3339
                                    cut($value['title'], 40),
3340
                                    '#',
3341
                                    ['id' => $link, 'class' => 'opener']
3342
                                );
3343
                                $result .= $time.' '.$icon.' '.Display::div(
3344
                                        $url
3345
                                    );
3346
3347
                                //Hidden content
3348
                                $content = Display::div(
3349
                                    $icon.Display::tag(
3350
                                        'h2',
3351
                                        $value['course_name']
3352
                                    ).'<hr />'.Display::tag(
3353
                                        'h3',
3354
                                        $value['title']
3355
                                    ).$complete_time.'<hr />'.Security::remove_XSS(
3356
                                        $value['content']
3357
                                    )
3358
                                );
3359
3360
                                //Main div
3361
                                $result .= Display::div(
3362
                                    $content,
3363
                                    [
3364
                                        'id' => 'main_'.$link,
3365
                                        'class' => 'dialog',
3366
                                        'style' => 'display:none',
3367
                                    ]
3368
                                );
3369
                                $result .= '</div>';
3370
                                $html .= $result;
3371
                            } else {
3372
                                $html .= $result .= $icon.'</div>';
3373
                            }
3374
                        }
3375
                    }
3376
                    $html .= "</td>";
3377
                    $curday++;
3378
                } else {
3379
                    $html .= "<td></td>";
3380
                }
3381
            }
3382
            $html .= "</tr>";
3383
        }
3384
        $html .= "</table>";
3385
        echo $html;
3386
    }
3387
3388
    /**
3389
     * Get personal agenda items between two dates (=all events from all registered courses).
3390
     *
3391
     * @param int $user_id user ID of the user
3392
     * @param    string    Optional start date in datetime format (if no start date is given, uses today)
3393
     * @param    string    Optional end date in datetime format (if no date is given, uses one year from now)
3394
     *
3395
     * @return array array of events ordered by start date, in
3396
     *               [0]('datestart','dateend','title'),[1]('datestart','dateend','title','link','coursetitle') format,
3397
     *               where datestart and dateend are in yyyyMMddhhmmss format
3398
     *
3399
     * @deprecated use agenda events
3400
     */
3401
    public static function get_personal_agenda_items_between_dates($user_id, $date_start = '', $date_end = '')
3402
    {
3403
        throw new Exception('fix get_personal_agenda_items_between_dates');
3404
        /*
3405
        $items = [];
3406
        if ($user_id != strval(intval($user_id))) {
3407
            return $items;
3408
        }
3409
        if (empty($date_start)) {
3410
            $date_start = date('Y-m-d H:i:s');
3411
        }
3412
        if (empty($date_end)) {
3413
            $date_end = date(
3414
                'Y-m-d H:i:s',
3415
                mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1)
3416
            );
3417
        }
3418
        $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
3419
        if (!preg_match($expr, $date_start)) {
3420
            return $items;
3421
        }
3422
        if (!preg_match($expr, $date_end)) {
3423
            return $items;
3424
        }
3425
3426
        // get agenda-items for every course
3427
        //$courses = api_get_user_courses($user_id, false);
3428
        $courses = CourseManager::get_courses_list_by_user_id($user_id, false);
3429
        foreach ($courses as $id => $course) {
3430
            $c = api_get_course_info_by_id($course['real_id']);
3431
            $t_a = Database::get_course_table(TABLE_AGENDA, $course['db']);
3432
            $t_ip = Database::get_course_table(
3433
                TABLE_ITEM_PROPERTY,
3434
                $course['db']
3435
            );
3436
            // get the groups to which the user belong
3437
            $group_memberships = GroupManager:: get_group_ids(
3438
                $course['db'],
3439
                $user_id
3440
            );
3441
            // if the user is administrator of that course we show all the agenda items
3442
            if ('1' == $course['status']) {
3443
                //echo "course admin";
3444
                $sqlquery = "SELECT ".
3445
                    " DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
3446
                    " FROM ".$t_a." agenda, ".
3447
                    $t_ip." ip ".
3448
                    " WHERE agenda.id = ip.ref ".
3449
                    " AND agenda.start_date>='$date_start' ".
3450
                    " AND agenda.end_date<='$date_end' ".
3451
                    " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
3452
                    " AND ip.visibility='1' ".
3453
                    " GROUP BY agenda.id ".
3454
                    " ORDER BY start_date ";
3455
            } else {
3456
                // if the user is not an administrator of that course, then...
3457
                if (is_array($group_memberships) && count(
3458
                        $group_memberships
3459
                    ) > 0
3460
                ) {
3461
                    $sqlquery = "SELECT ".
3462
                        "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
3463
                        " FROM ".$t_a." agenda, ".
3464
                        $t_ip." ip ".
3465
                        " WHERE agenda.id = ip.ref ".
3466
                        " AND agenda.start_date>='$date_start' ".
3467
                        " AND agenda.end_date<='$date_end' ".
3468
                        " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
3469
                        " AND	( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
3470
                            ", ",
3471
                            $group_memberships
3472
                        ).")) ) ".
3473
                        " AND ip.visibility='1' ".
3474
                        " ORDER BY start_date ";
3475
                } else {
3476
                    $sqlquery = "SELECT ".
3477
                        "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
3478
                        " FROM ".$t_a." agenda, ".
3479
                        $t_ip." ip ".
3480
                        " WHERE agenda.id = ip.ref ".
3481
                        " AND agenda.start_date>='$date_start' ".
3482
                        " AND agenda.end_date<='$date_end' ".
3483
                        " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
3484
                        " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ".
3485
                        " AND ip.visibility='1' ".
3486
                        " ORDER BY start_date ";
3487
                }
3488
            }
3489
3490
            $result = Database::query($sqlquery);
3491
            while ($item = Database::fetch_array($result)) {
3492
                $agendaday = date("j", strtotime($item['start_date']));
3493
                $month = date("n", strtotime($item['start_date']));
3494
                $year = date("Y", strtotime($item['start_date']));
3495
                $URL = api_get_path(
3496
                        WEB_PATH
3497
                    )."main/calendar/agenda.php?cidReq=".urlencode(
3498
                        $course["code"]
3499
                    )."&day=$agendaday&month=$month&year=$year#$agendaday";
3500
                [$year, $month, $day, $hour, $min, $sec] = explode(
3501
                    '[-: ]',
3502
                    $item['start_date']
3503
                );
3504
                $start_date = $year.$month.$day.$hour.$min;
3505
                [$year, $month, $day, $hour, $min, $sec] = explode(
3506
                    '[-: ]',
3507
                    $item['end_date']
3508
                );
3509
                $end_date = $year.$month.$day.$hour.$min;
3510
3511
                $items[] = [
3512
                    'datestart' => $start_date,
3513
                    'dateend' => $end_date,
3514
                    'title' => $item['title'],
3515
                    'link' => $URL,
3516
                    'coursetitle' => $c['name'],
3517
                ];
3518
            }
3519
        }
3520
3521
        return $items;*/
3522
    }
3523
3524
    /**
3525
     * This function calculates the startdate of the week (monday)
3526
     * and the enddate of the week (sunday)
3527
     * and returns it as an array.
3528
     */
3529
    public static function calculate_start_end_of_week($week_number, $year)
3530
    {
3531
        // determine the start and end date
3532
        // step 1: we calculate a timestamp for a day in this week
3533
        $random_day_in_week = mktime(
3534
                0,
3535
                0,
3536
                0,
3537
                1,
3538
                1,
3539
                $year
3540
            ) + ($week_number) * (7 * 24 * 60 * 60); // we calculate a random day in this week
3541
        // step 2: we which day this is (0=sunday, 1=monday, ...)
3542
        $number_day_in_week = date('w', $random_day_in_week);
3543
        // step 3: we calculate the timestamp of the monday of the week we are in
3544
        $start_timestamp = $random_day_in_week - (($number_day_in_week - 1) * 24 * 60 * 60);
3545
        // step 4: we calculate the timestamp of the sunday of the week we are in
3546
        $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week + 1) * 24 * 60 * 60) - 3600;
3547
        // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
3548
        $start_day = date('j', $start_timestamp);
3549
        $start_month = date('n', $start_timestamp);
3550
        $start_year = date('Y', $start_timestamp);
3551
        $end_day = date('j', $end_timestamp);
3552
        $end_month = date('n', $end_timestamp);
3553
        $end_year = date('Y', $end_timestamp);
3554
        $start_end_array['start']['day'] = $start_day;
3555
        $start_end_array['start']['month'] = $start_month;
3556
        $start_end_array['start']['year'] = $start_year;
3557
        $start_end_array['end']['day'] = $end_day;
3558
        $start_end_array['end']['month'] = $end_month;
3559
        $start_end_array['end']['year'] = $end_year;
3560
3561
        return $start_end_array;
3562
    }
3563
3564
    /**
3565
     * @return bool
3566
     */
3567
    public function getIsAllowedToEdit()
3568
    {
3569
        return $this->isAllowedToEdit;
3570
    }
3571
3572
    /**
3573
     * @param bool $isAllowedToEdit
3574
     */
3575
    public function setIsAllowedToEdit($isAllowedToEdit)
3576
    {
3577
        $this->isAllowedToEdit = $isAllowedToEdit;
3578
    }
3579
3580
    /**
3581
     * Format needed for the Fullcalendar js lib.
3582
     *
3583
     * @param string $utcTime
3584
     *
3585
     * @return bool|string
3586
     */
3587
    public function formatEventDate($utcTime)
3588
    {
3589
        $utcTimeZone = new DateTimeZone('UTC');
3590
        $platformTimeZone = new DateTimeZone(api_get_timezone());
3591
3592
        $eventDate = new DateTime($utcTime, $utcTimeZone);
3593
        $eventDate->setTimezone($platformTimeZone);
3594
3595
        return $eventDate->format(DateTime::ISO8601);
3596
    }
3597
}
3598