aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ConsultationResponsibility.php
blob: 9634d415394c8d0ec0e7c59ce35a00164a1fcf94 (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
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 5.1
 *
 * @property int block_id database column
 * @property int id alias column for block_id
 * @property string range_id database column
 * @property string range_type database column
 * @property int mkdate database column
 */
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',
        ];

        parent::configure($config);
    }

    /**
     * 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');
    }
}