aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ConsultationResponsibility.php
blob: ef672519abc97286cb17f28c9227203549247b6a (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
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 5.1
 *
 * @property array $id alias for pk
 * @property int $block_id database column
 * @property string $range_id database column
 * @property string $range_type database column
 * @property int $mkdate database column
 * @property ConsultationBlock $block belongs_to ConsultationBlock
 */
class ConsultationResponsibility extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'consultation_responsibilities';

        $config['belongs_to']['block'] = [
            'class_name'  => ConsultationBlock::class,
            'foreign_key' => 'block_id',
        ];

        $config['registered_callbacks']['after_store'][] = function (ConsultationResponsibility $responsibility): void {
            $responsibility->block->updateEvents();
        };
        $config['registered_callbacks']['after_delete'][] = function (ConsultationResponsibility $responsibility): void {
            $responsibility->block->updateEvents();
        };

        parent::configure($config);
    }

    /**
     * Finds all responsibilities for a given user id.
     *
     * @param string $user_id
     * @return array
     */
    public static function findByUserId(string $user_id): array
    {
        return self::findBySQL(
            "range_id = ? AND range_type = 'user'",
            [$user_id]
        );
    }

    /**
     * Finds all responsibilities for a given institute id.
     *
     * @param string $institute_id
     * @return array
     */
    public static function findByInstituteId(string $institute_id): array
    {
        return self::findBySQL(
            "range_id = ? AND range_type = 'institute'",
            [$institute_id]
        );
    }

    /**
     * Finds all responsibilities for a given statusgroup id.
     *
     * @param string $statusgroup_id
     *
     * @return array
     */
    public static function findByStatusgroupId(string $statusgroup_id): array
    {
        return self::findBySQL(
            "range_id = ? AND range_type = 'statusgroup'",
            [$statusgroup_id]
        );
    }

    /**
     * Returns the name of the associated responsibility.
     *
     * @return string
     * @throws Exception
     */
    public function getName()
    {
        if ($this->range_type === 'user') {
            return User::find($this->range_id)->getFullName();
        }
        if ($this->range_type === 'statusgroup') {
            return Statusgruppen::find($this->range_id)->getName();
        }
        if ($this->range_type === 'institute') {
            return Institute::find($this->range_id)->getFullName();
        }
        throw new Exception('Unknown range type');
    }

    /**
     * Returns an url to the associated responsibility.
     *
     * @return string
     * @throws Exception
     */
    public function getURL()
    {
        if ($this->range_type === 'user') {
            $user = User::find($this->range_id);
            return URLHelper::getURL('dispatch.php/profile', ['username' => $user->username], true);
        }
        // TODO: Check if staff tab is activated and link to that
        if ($this->range_type === 'statusgroup') {
            $institute = Statusgruppen::find($this->range_id)->institute;
            return URLHelper::getURL('dispatch.php/institute/overview', ['auswahl' => $institute->id], true);
        }
        if ($this->range_type === 'institute') {
            return URLHelper::getURL('dispatch.php/institute/overview', ['auswahl' => $this->range_id], true);
        }
        throw new Exception('Unknown range type');
    }

    /**
     * Returns all users belonging to the associated responsibility.
     *
     * @return array
     * @throws Exception
     */
    public function getUsers()
    {
        if ($this->range_type === 'user') {
            return [User::find($this->range_id)];
        }
        if ($this->range_type === 'statusgroup') {
            $group = Statusgruppen::find($this->range_id);
            return self::getStatusgroupResponsibilities($group);
        }
        if ($this->range_type === 'institute') {
            $institute = Institute::find($this->range_id);
            return self::getInstituteResponsibilites($institute);
        }
        throw new Exception('Unknown range type');
    }

    /**
     * Returns all responsible users for a course.
     *
     * @param Course $course
     * @return array
     */
    public static function getCourseResponsibilities(Course $course)
    {
        return $course->getMembersWithStatus('tutor dozent', true)->pluck('user');
    }

    /**
     * Returns all responsible users for a status group.
     *
     * @param Statusgruppen $group
     * @return array
     */
    public static function getStatusgroupResponsibilities(Statusgruppen $group)
    {
        return $group->members->pluck('user');
    }

    /**
     * Returns all responsible users for an institute.
     *
     * @param Institute $institute
     * @return array
     */
    public static function getInstituteResponsibilites(Institute $institute)
    {
        return $institute->members->filter(function (InstituteMember $member) {
            return in_array($member->inst_perms, ['tutor', 'dozent']);
        })->pluck('user');
    }
}