aboutsummaryrefslogtreecommitdiff
path: root/lib/statusgruppe.inc.php
blob: 719cdce69500c8ac9010422d05e51b53c673fe6f (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
# Lifter001: DONE
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TEST
# Lifter010: TODO
/**
* helper functions for handling statusgruppen
*
* helper functions for handling statusgruppen
*
* @author               Ralf Stockmann <rstockm@gwdg.de>
* @access               public
* @package          studip_core
* @modulegroup  library
* @module               statusgruppe.inc.php
*/
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// statusgruppe.inc.php
// Copyright (c) 2002 Ralf Stockmann <rstockm@gwdg.de>
// +---------------------------------------------------------------------------+
// 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 any later version.
// +---------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// +---------------------------------------------------------------------------+

/**
 * Returns all statusgruppen for the given range.
 *
 * If there is no statusgruppe for the given range, it returns FALSE.
 * @deprecated
 * @param    string  $range_id
 * @param    string  $user_id
 * @return   array   (structure statusgruppe_id => name)
 */
function GetAllStatusgruppen($parent, $check_user = null, $exclude = false) {
    $query = "SELECT * FROM statusgruppen WHERE range_id = ? ORDER BY position";
    $statement = DBManager::get()->prepare($query);
    $statement->execute([$parent]);
    $groups = $statement->fetchAll(PDO::FETCH_ASSOC);

    if (!$groups) {
        return false;
    }

    $query = "SELECT visible FROM statusgruppe_user WHERE user_id = ? AND statusgruppe_id = ?";
    $presence = DBManager::get()->prepare($query);

    $childs = [];
    foreach ($groups as $group) {
        $user_there = $visible = $user_in_child = false;

        $kids = GetAllStatusgruppen($group['statusgruppe_id'], $check_user, $exclude);

        if ($check_user) {
            $presence->execute([$check_user, $group['statusgruppe_id']]);
            $present = $presence->fetchColumn();
            $presence->closeCursor();

            if ($user_there = ($present !== false)) {
                $visible = $present;
            }

            if (is_array($kids)) {
                foreach ($kids as $kid) {
                    if ($kid['user_there'] || $kid['user_in_child']) {
                        $user_in_child = true;
                    }
                }
            }
        }

        if (!$check_user || !$exclude || $user_in_child || $user_there) {
            $childs[$group['statusgruppe_id']] = [
                'role'          => Statusgruppen::build($group),
                'visible'       => $visible,
                'user_there'    => $user_there,
                'user_in_child' => $user_in_child,
                'child'         => $kids
            ];
        }
    }

    return $childs ?: false;
}


/**
 * @deprecated
 * @param $roles
 * @param int $level
 * @param string $pred
 * @param bool $all
 * @return array|null
 */
function GetRoleNames($roles, $level = 0, $pred = '', $all = false) {
    $out = [];

    if (is_array($roles))
    foreach ($roles as $role_id => $role) {
        if (!$role['name']) $role['name'] = $role['role']->getName();

        if ($pred != '') {
            $new_pred = $pred.' > '.$role['name'];
        } else {
            $new_pred = $role['name'];
        }

        if ($role['user_there'] || $all) {
            $out[$role_id] = $new_pred;
        }

        if ($role['child']) {
            $out = array_merge((array)$out, (array)GetRoleNames($role['child'], $level+1, $new_pred, $all));
        }
    }

    return (sizeof($out) > 0 ? $out : null);
}

/**
 * @deprecated
 * @param $roles
 * @param $user_or_id
 * @param $default_entries
 * @param int $level
 * @param string $pred
 * @return array
 */
function get_role_data_recursive($roles, $user_or_id, $default_entries, $level = 0, $pred = '') {
    if (!is_array($roles)) {
        return '';
    }

    $user = $user_or_id instanceof User
          ? $user_or_id
          : User::find($user_or_id);

    $out = '';

    foreach ($roles as $role_id => $role) {
        $role['name'] = $role['role']->getGenderedName($user);

        if ($pred != '') {
            $new_pred = "{$pred} > {$role['name']}";
        } else {
            $new_pred = $role['name'];
        }

        if ($role['user_there'] && $role['visible']) {
            $out .= '<tr><td>'
                 . Icon::create('arr_1right', Icon::ROLE_INFO)
                 . '</td><td colspan="2"><b>'. htmlReady($new_pred) .'</b></td></tr>';

            $entries = DataFieldEntry::getDataFieldEntries([$user->id, $role_id]);
            foreach ($entries as $id => $entry) {
                if ($entry->getValue() == 'default_value') {
                    $value = $default_entries[$id]->getDisplayValue();
                    $default = true;
                } else {
                    $value = $entry->getDisplayValue();
                    $default = false;
                }

                $name = $entry->getName();

                if ($entry->isVisible()) {
                    if (trim($value) && !$default) {
                        $out .= '<tr><td></td><td>'. htmlReady($name) .':</td><td>'.trim($value);
                        $out .= '</td></tr>';
                    }
                }
            }
        }

        if ($role['child'] && $role['user_in_child']) {
            $out .= get_role_data_recursive($role['child'], $user, $default_entries, $level + 1, $new_pred);
        }
    }

    return $out;
}

/**
 * @deprecated
 * @param $roles
 * @param int $level
 * @param bool $parent_name
 * @return array
 */
function getFlattenedRoles($roles, $level = 0, $parent_name = false) {
    if (!is_array($roles)) {
        return [];
    }

    $ret = [];
    foreach ($roles as $id => $role) {
        if (!isset($role['name'])) {
            $role['name'] = $role['role']->getName();
        }
        $spaces = '';
        for ($i = 0; $i < $level; $i++) {
            $spaces .= '&nbsp;&nbsp;';
        }

        // generate an indented version of the role-name
        $role['name'] = $spaces . $role['name'];

        // generate a name with all parent-roles in the name
        if ($parent_name) {
            $role['name_long'] = $parent_name . ' > ' . $role['role']->getName();
        } else {
            $role['name_long'] = $role['role']->getName();
        }

        $ret[$id] = $role;

        if ($role['child']) {
            $ret = array_merge($ret, getFlattenedRoles($role['child'], $level + 1, $role['name_long']));
        }

    }

    return $ret;
}