aboutsummaryrefslogtreecommitdiff
path: root/lib/modules/ConsultationModule.php
blob: 03ee93fc004df6faac568227741d596d5c4347bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 */
class ConsultationModule extends CorePlugin implements
    StudipModuleExtended,
    SystemPlugin,
    PrivacyPlugin,
    HomepagePlugin
{
    use IconNavigationTrait;

    public function __construct()
    {
        parent::__construct();

        // Update consultation events for the course when a member changes
        foreach (['UserDidEnterCourse', 'UserDidLeaveCourse'] as $event) {
            NotificationCenter::on($event, function ($event, $course_id) {
                $course = Course::find($course_id);
                if ($course) {
                    ConsultationSlot::findEachBySQL(
                        function (ConsultationSlot $slot) {
                            $slot->updateEvents();
                        },
                        "JOIN consultation_blocks USING (block_id)
                         WHERE range_id = ? AND range_type = 'course'",
                        [$course_id]
                    );
                }
            });
        }

        // Update consultation events for the course when a member changes
        foreach (['InstituteMemberDidCreate', 'InstituteMemberDidDelete'] as $event) {
            NotificationCenter::on($event, function ($event, InstituteMember $member) {
                ConsultationSlot::findEachBySQL(
                    function (ConsultationSlot $slot) {
                        $slot->updateEvents();
                    },
                    "JOIN consultation_blocks USING (block_id)
                      WHERE range_id = ? AND range_type = 'institute'",
                    [$member->institut_id]
                );
            });
        }

        NotificationCenter::on('UserDidLeaveCourse', function ($event, $course_id) {
            // Delete consultation events for the user and course
            $course = Course::find($course_id);
            if ($course) {
                ConsultationSlot::findEachBySQL(
                    function (ConsultationSlot $slot) {
                        $slot->updateEvents();
                    },
                    "JOIN consultation_blocks USING (block_id)
                     WHERE range_id = ? AND range_type = 'course'",
                    [$course_id]
                );
            }
        });

        NotificationCenter::on('UserDidDelete', function ($event, $user) {
            // Delete consultation bookings and slots
            ConsultationBooking::deleteByUser_id($user->id);
            ConsultationBlock::deleteBySQL("range_id = ? AND range_type = 'user'", [$user->id]);
        });
        NotificationCenter::on('CourseDidDelete', function ($event, $course) {
            // Delete consultation blocks
            ConsultationBlock::deleteBySQL("range_id = ? AND range_type = 'course'", [$course->id]);
        });
        NotificationCenter::on('InstituteDidDelete', function ($event, $institute) {
            // Delete consultation blocks
            ConsultationBlock::deleteBySQL("range_id = ? AND range_type = 'institute'", [$institute->id]);
        });
    }

    /**
     * {@inheritdoc}
     */
    public function isActivatableForContext(Range $context)
    {
        // Consultations globally disabled?
        if (!Config::get()->CONSULTATION_ENABLED) {
            return false;
        }

        // Context is user and current user has required permission?
        if ($context instanceof User) {
            return $GLOBALS['perm']->have_perm(Config::get()->CONSULTATION_REQUIRED_PERMISSION, $context->getRangeId());
        }

        return true;
    }

    public function getManyIconNavigation(array $course_ids, ?string $user_id = null): array
    {
        // TODO

        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getTabNavigation($course_id)
    {
        if ($GLOBALS['user']->id !== 'nobody') {
            $navigation = new ConsultationNavigation(RangeFactory::find($course_id));
            $navigation->setImage(Icon::create('consultation', Icon::ROLE_INFO_ALT));
            return ['consultation' => $navigation];
        }

        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getInfoTemplate($course_id)
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function getHomepageTemplate($user_id)
    {
        return null;
    }


    /**
     * {@inheritdoc}
     */
    public function getMetadata()
    {
        return [
            'summary'     => _('Generische Terminvergabe'),
            'description' => _('Über die generische Terminvergabe können jegliche Formen von Terminen ' .
                               'angeboten werden, zu denen sich Personen oder auch Gruppen von Personen ' .
                               'anmelden können.'),
            'category'    => _('Kommunikation und Zusammenarbeit'),
            'keywords'    => _('Terminvergabe, Sprechstunden'),
            'displayname' => _('Terminvergabe'),
            'icon'        => Icon::create('consultation', Icon::ROLE_INFO),
            'icon_clickable' => Icon::create('consultation'),
            'screenshots' => [
                'path'     => 'assets/images/plus/screenshots/Terminvergabe',
                'pictures' => [
                    0 => ['source' => 'Uebersicht.jpg', 'title'  => _('Übersicht der erstellten Termine')],
                    1 => ['source' => 'Anlegen.jpg','title'  => _('Erstellen neuer Termine')]
                ]
             ]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function exportUserData(StoredUserData $storage)
    {
        $storage->addTabularData(
            _('Terminblöcke'),
            'consultation_blocks',
            ConsultationBlock::findAndMapBySQL(
                function ($block) {
                    return $block->toRawArray();
                },
                "range_id = :user_id AND range_type = 'user'",
                [':user_id' => $storage->user_id]
            )
        );
        $storage->addTabularData(
            _('Terminbuchungen'),
            'consultation_bookings',
            ConsultationBooking::findAndMapBySQL(
                function ($booking) {
                    return $booking->toRawArray();
                },
                'user_id = :user_id',
                [':user_id' => $storage->user_id]
            )
        );
        $storage->addTabularData(
            _('Terminverantwortlichkeiten'),
            'consultation_responsibilities',
            ConsultationResponsibility::findAndMapBySQL(
                function ($responsibility) {
                    return $responsibility->toRawArray();
                },
                "range_id = :user_id AND range_type = 'user'",
                [':user_id' => $storage->user_id]
            )
        );
    }
}