aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ConsultationBooking.php
blob: 06e6967c2a52a972afb98bd659a5fb3ef2823e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * Representation of a user's booking of a consultation slots.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.3
 *
 * @property int $id alias column for booking_id
 * @property int $booking_id database column
 * @property int $slot_id database column
 * @property string $user_id database column
 * @property string|null $reason database column
 * @property string|null $student_event_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property ConsultationSlot $slot belongs_to ConsultationSlot
 * @property User $user belongs_to User
 * @property EventData|null $event has_one EventData
 */
class ConsultationBooking extends SimpleORMap implements PrivacyObject
{
    /**
     * Configures the model.
     * @param array  $config Configuration
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'consultation_bookings';

        $config['belongs_to']['slot'] = [
            'class_name'  => ConsultationSlot::class,
            'foreign_key' => 'slot_id',
        ];
        $config['belongs_to']['user'] = [
            'class_name'  => User::class,
            'foreign_key' => 'user_id',
        ];
        $config['has_one']['event'] = [
            'class_name'        => EventData::class,
            'foreign_key'       => 'student_event_id',
            'assoc_foreign_key' => 'event_id',
            'on_delete'         => 'delete',
        ];

        // Create student event
        $config['registered_callbacks']['before_create'][] = function (ConsultationBooking $booking) {
            setTempLanguage($booking->user_id);

            $event = $booking->slot->createEvent($booking->user);
            $event->category_intern = 1;
            $event->summary = sprintf(
                _('Termin bei %s'),
                $booking->slot->block->range->getFullName()
            );
            $event->description = $booking->reason;
            $event->store();

            restoreLanguage();

            $booking->student_event_id = $event->id;
        };

        $config['registered_callbacks']['after_create'][] = function (ConsultationBooking $booking) {
            ConsultationMailer::sendBookingMessageToUser($GLOBALS['user']->getAuthenticatedUser(), $booking);
            ConsultationMailer::sendBookingMessageToResponsibilities($GLOBALS['user']->getAuthenticatedUser(), $booking);
        };

        $config['registered_callbacks']['before_store'][] = function (ConsultationBooking $booking) {
            if (!$booking->isNew() && $booking->isFieldDirty('reason')) {
                if ($GLOBALS['user']->id !== $booking->user_id) {
                    ConsultationMailer::sendReasonMessage($GLOBALS['user']->getAuthenticatedUser(), $booking, $booking->user);
                }

                $responsible_persons = $booking->slot->block->responsible_persons;
                foreach ($responsible_persons as $user) {
                    if ($GLOBALS['user']->id !== $user->id) {
                        ConsultationMailer::sendReasonMessage($GLOBALS['user']->getAuthenticatedUser(), $booking, $user);
                    }
                }
            }
        };

        $config['registered_callbacks']['after_store'][] = function (ConsultationBooking $booking) {
            if ($booking->event) {
                $booking->event->description = $booking->reason;
                $booking->event->store();
            }

            $booking->slot->updateEvents();
        };

        $config['registered_callbacks']['after_delete'][] = function (ConsultationBooking $booking) {
            $booking->slot->updateEvents();
        };

        parent::configure($config);
    }

    /**
     * Returns whether a user may create a booking for the given range.
     *
     * @param User $user
     * @return bool
     */
    public static function userMayCreateBookingForRange(\Range $range, \User $user): bool
    {
        if (!($range instanceof \User)) {
            return true;
        }

        $allowed_perms = ['user', 'autor', 'tutor'];
        if (Config::get()->CONSULTATION_ALLOW_DOCENTS_RESERVING) {
            $allowed_perms[] = 'dozent';
        }

        return in_array($user->perms, $allowed_perms);
    }

    public function cancel($reason = '')
    {
        if ($GLOBALS['user']->id !== $this->user_id) {
            ConsultationMailer::sendCancelMessageToUser($GLOBALS['user']->getAuthenticatedUser(), $this, $reason);
        }

        ConsultationMailer::sendCancelMessageToResponsibilities($GLOBALS['user']->getAuthenticatedUser(), $this, $reason);

        return $this->delete() ? 1 : 0;
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $bookings = self::findByUser_id($storage->user_id);
        if ($bookings) {
            $storage->addTabularData(
                _('Terminbelegungen'),
                'consultation_bookings',
                array_map(function ($booking) {
                    return $booking->toRawArray();
                }, $bookings)
            );
        }
    }
}