aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/UserFilterFields/MassMail/MassMailDomainFilter.php
blob: 7d7a9225fff57f79ae640d0eafdff1ed05df15f1 (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
<?php

/**
 * DomainCondition.php
 *
 * All conditions concerning the user domain in Stud.IP can be specified here.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Thomas Hackl <thomas.hackl@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */
namespace UserFilterFields\MassMail;

use MassMail\MassMailPermission;
use UserFilterFields\DomainCondition;
use DBManager;
use User;

class MassMailDomainFilter extends DomainCondition
{

    public string $target = '';

    /**
     * Gets all users belonging to given domain.
     *
     * @return array All users that are affected by the current condition
     * field.
     */
    public function getUsers($restrictions = [])
    {
        $users = [];
        if (MassMailPermission::has(User::findCurrent()->id, true)) {
            $users = parent::getUsers($restrictions);
        } else if (MassMailPermission::has(User::findCurrent()->id)) {

            $allowed = MassMailPermission::getForUser(User::findCurrent());

            switch ($this->target) {
                case 'employees':
                    $sql = "SELECT DISTINCT `" . $this->userDataDbTable . "`.`user_id` FROM `" . $this->userDataDbTable
                        . "`JOIN `user_inst` USING (`user_id`) ";
                    $where = "WHERE `" . $this->userDataDbTable . "`.`" . $this->userDataDbField . "`" . $this->compareOperator
                        . ":value AND `user_inst`.`Institut_id` IN (:institutes) AND `user_inst`.`inst_perms` IN (:perms)";
                    $parameters = [
                        'value' => $this->value,
                        'institutes' => $allowed['allowed_institutes'],
                        'perms' => ['autor', 'tutor', 'dozent', 'admin']
                    ];
                    break;
                case 'students':
                default:
                    $sql = "SELECT DISTINCT `" . $this->userDataDbTable . "`.`user_id` FROM `" . $this->userDataDbTable
                        . "`JOIN `user_studiengang` USING (`user_id`) ";
                    $where = "WHERE `" . $this->userDataDbTable . "`.`" . $this->userDataDbField . "`" . $this->compareOperator
                        . ":value AND `user_studiengang`.`abschluss_id` IN (:degrees)
                        AND `user_studiengang`.`fach_id` IN (:subjects)";
                    $parameters = [
                        'value' => $this->value,
                        'degrees' => $allowed['allowed_degrees'],
                        'subjects' => $allowed['allowed_subjects']
                    ];
                    break;
            }
            $users = DBManager::get()->fetchFirst($sql . $where, $parameters);
        }

        return $users;
    }
}