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

namespace UserFilterFields\MassMail;

use UserFilterFields\SubjectCondition;

class MassMailSubjectFilter extends SubjectCondition
{
    /**
     * @see \UserFilterField::getTargets()
     */
    public static function getTargets()
    {
        return ['students'];
    }

    public function __construct($fieldId = '')
    {
        parent::__construct($fieldId);

        if (!\MassMail\MassMailPermission::has(\User::findCurrent()->id, true)) {
            $this->validValues = [];

            $permission = \MassMail\MassMailPermission::getForUser(\User::findCurrent(), true);

            foreach ($permission['allowed_subjects'] as [$id, $name]) {
                $this->validValues[$id] = (string) $name;
            }
        }
    }

    public function getUsers($restrictions = [])
    {
        $users = [];

        if (\MassMail\MassMailPermission::has(\User::findCurrent()->id, true)) {
            $users = parent::getUsers($restrictions);
        } else if (count($this->validValues) > 0) {
            // Standard query getting the values without respecting other values.
            $select = "SELECT DISTINCT `" . $this->userDataDbTable . "`.`user_id` ";
            $from = "FROM `" . $this->userDataDbTable . "` ";
            $where = "WHERE `" . $this->userDataDbTable . "`.`" . $this->userDataDbField .
                "`" . $this->compareOperator . "?";
            $parameters = [$this->value];
            $joinedTables = [
                $this->userDataDbTable => true
            ];
            // Check if there are restrictions given.
            foreach ($restrictions as $otherField => $restriction) {
                // We only take the value into consideration if it represents a valid restriction.
                if ($this->relations[$otherField]) {
                    // Do we need to join in another table?
                    if (!$joinedTables[$restriction['table']]) {
                        $joinedTables[$restriction['table']] = true;
                        $from .= " INNER JOIN `" . $restriction['table'] . "` ON (`" .
                            $this->userDataDbTable . "`.`" .
                            $this->relations[$otherField]['local_field'] . "`=`" .
                            $restriction['table'] . "`.`" .
                            $this->relations[$otherField]['foreign_field'] . "`)";
                    }
                    // Expand WHERE statement with the value from restriction.
                    $where .= " AND `" . $restriction['table'] . "`.`" .
                        $restriction['field'] . "`" . $restriction['compare'] . "?";
                    $parameters[] = $restriction['value'];
                }
            }

            $where .= " AND `" . $this->userDataDbTable . "`.`" . $this->userDataDbField . "` IN (?)";
            $parameters[] = array_keys($this->validValues);

            // Get all the users that fulfill the condition.
            $users = \DBManager::get()->fetchFirst($select . $from . $where, $parameters);
        }

        return $users;
    }

    /**
     * Gets the value for the given user that is relevant for this
     * condition field. Here, this method looks up the study degree(s)
     * for the user. These can then be compared with the required degrees
     * whether they fit.
     *
     * @param  String $userId User to check.
     * @param  array $additional conditions that are required for check.
     * @return array The value(s) for this user.
     */
    public function getUserValues($userId, $additional = null)
    {
        if (\MassMail\MassMailPermission::has(\User::findCurrent()->id, true)) {
            $result = parent::getUserValues($userId, $additional);
        } else {
            $result = [];
            $query = "SELECT DISTINCT `" . $this->userDataDbField . "` " .
                "FROM `" . $this->userDataDbTable . "` " .
                "WHERE `user_id`=?";
            $parameters = [$userId];
            // Additional requirements given...
            if (is_array($additional)) {

                // Don't use the same database field twice as this can only get ugly.
                $usedFields = [$this->userDataDbField];

                foreach ($additional as $a_condition) {
                    if (
                        $a_condition->id != $this->id
                        && $this->userDataDbTable === $a_condition->userDataDbTable
                        && !in_array($a_condition->userDataDbField, $usedFields)
                    ) {
                        $query .= " AND `" . $a_condition->userDataDbField . "` " . $a_condition->compareOperator . "?";
                        $parameters[] = $a_condition->value;
                    }
                }
            }
            // Get semester of study for user.
            $stmt = \DBManager::get()->prepare($query);
            $stmt->execute($parameters);
            while ($current = $stmt->fetch(\PDO::FETCH_ASSOC)) {
                $result[] = $current[$this->userDataDbField];
            }
        }
        return $result;
    }

}