aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ProfileModel.php
blob: 152bb195005c6f74bda5d4c42c70f81bb580890c (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
<?php
/**
 * ProfileModel
 *
 * 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      David Siegfried <david.siegfried@uni-oldenburg.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       2.4
 */

class ProfileModel
{
    protected $perm;
    /**
     * Internal current selected user id
     * @var String
     */
    protected $current_user;

    /**
     * Internal current logged in user id
     * @var String
     */
    protected $user;

    /**
     * Internal user homepage visbilities
     * @var array
     */
    protected $visibilities;

    /**
     * Get informations on depending selected user
     * @param String $current_user
     * @param String $user
     */
    public function __construct($current_user, $user)
    {
        $this->current_user = User::find($current_user);
        $this->user         = User::find($user);
        $this->visibilities = $this->getHomepageVisibilities();
        $this->perm         = $GLOBALS['perm'];
    }

    /**
     * Get the homepagevisibilities
     *
     * @return array
     */
    public function getHomepageVisibilities()
    {
        $visibilities = get_local_visibility_by_id($this->current_user->user_id, 'homepage');
        if (is_array(json_decode($visibilities, true))) {
            return json_decode($visibilities, true);
        }
        return [];
    }

    /**
     * Returns the visibility value
     *
     * @return String
     */
    public function getVisibilityValue($param, $visibility = '')
    {
        if (Visibility::verify($visibility ?: $param, $this->current_user->user_id)) {
            return $this->current_user->$param;
        }
        return false;
    }

    /**
     * Returns a specific value of the visibilies
     * @param String $param
     * @return String
     */

    public function getSpecificVisibilityValue($param)
    {
        if (!empty($this->visibilities[$param])) {
            return $this->visibilities[$param];
        }
        return false;
    }

    /**
     * Creates an array with all seminars
     *
     * @return array
     */
    public function getDozentSeminars()
    {
        $courses = [];
        $semester = array_reverse(Semester::getAll());
        $field = 'name';
        if (Config::get()->IMPORTANT_SEMNUMBER) {
            $field = "veranstaltungsnummer,{$field}";
        }
        $allcourses = new SimpleCollection(Course::findBySQL("INNER JOIN seminar_user USING(Seminar_id) WHERE user_id=? AND seminar_user.status='dozent' AND seminare.visible=1", [$this->current_user->id]));
        foreach (array_filter($semester) as $one) {
            $courses[(string) $one->name] = $allcourses->filter(function ($c) use ($one) {
                if (Config::get()->HIDE_STUDYGROUPS_FROM_PROFILE && $c->isStudygroup()) {
                    return false;
                }
                if (!$c->isOpenEnded()) {
                    return $c->isInSemester($one);
                } elseif ($one->isCurrent()) {
                    return $c;
                }
                return false;
            })->orderBy($field);

            if (!$courses[(string) $one->name]->count()) {
                unset($courses[(string) $one->name]);
            }
        }
        return $courses;
    }

    /**
     * Collect user datafield informations
     *
     * @return array
     */
    public function getDatafields()
    {
        // generische Datenfelder aufsammeln
        $short_datafields = [];
        $long_datafields  = [];
        foreach (DataFieldEntry::getDataFieldEntries($this->current_user->user_id, 'user') as $entry) {
            if ($entry->isVisible() && $entry->getDisplayValue()
                && Visibility::verify($entry->getID(), $this->current_user->user_id))
            {
                if ($entry instanceof DataFieldTextareaEntry) {
                    $long_datafields[] = $entry;
                } else {
                    $short_datafields[] = $entry;
                }
            }
        }

        return [
            'long'  => $long_datafields,
            'short' => $short_datafields,
        ];
    }

    /**
     * Filter long datafiels from the datafields
     *
     * @return array
     */
    public function getLongDatafields()
    {
        $datafields = $this->getDatafields();
        $array      = [];

        if (empty($datafields)) {
            return null;
        }
        foreach ($datafields['long'] as $entry) {
            $array[(string) $entry->getName()] = [
                'content' => $entry->getDisplayValue(),
                'visible' => '(' . $entry->getPermsDescription() . ')',
            ];
        }

        return $array;
    }

    /**
     * Filter short datafiels from the datafields
     *
     * @return array
     */
    public function getShortDatafields()
    {
        $shortDatafields = $this->getDatafields();
        $array = [];

        if (empty($shortDatafields)) {
            return null;
        }

        foreach ($shortDatafields['short'] as $entry) {
            $array[(string) $entry->getName()] = [
                'content' => $entry->getDisplayValue(),
                'visible' => '(' . $entry->getPermsDescription() . ')',
            ];
        }
        return $array;
    }
}