aboutsummaryrefslogtreecommitdiff
path: root/lib/models/StatusgruppeUser.php
blob: a36dcdc6a0b0475ce84e157630e07b56ccb5d3a6 (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
<?php

/**
 * StatusgruppeUser.php
 * model class for statusgroupusers.
 *
 * This model should be joined to an user object if nessecary
 *
 * 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      Florian Bieringer <florian.bieringer@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 *
 * @property array $id alias for pk
 * @property string $statusgruppe_id database column
 * @property string $user_id database column
 * @property int $position database column
 * @property int $visible database column
 * @property int $inherit database column
 * @property int|null $mkdate database column
 * @property SimpleORMapCollection|DatafieldEntryModel[] $datafields has_many DatafieldEntryModel
 * @property Statusgruppen $group belongs_to Statusgruppen
 * @property User $user belongs_to User
 * @property mixed $vorname additional field
 * @property mixed $nachname additional field
 * @property mixed $username additional field
 * @property mixed $email additional field
 * @property mixed $title_front additional field
 * @property mixed $title_rear additional field
 */
class StatusgruppeUser extends SimpleORMap implements PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'statusgruppe_user';
        $config['belongs_to']['group'] = [
            'class_name' => Statusgruppen::class,
            'foreign_key' => 'statusgruppe_id',
        ];
        $config['belongs_to']['user'] = [
            'class_name' => User::class,
            'foreign_key' => 'user_id',
        ];

        $config['has_many']['datafields'] = [
            'class_name' => DatafieldEntryModel::class,
            'foreign_key' => function($group_member) {
                return [$group_member];
            },
            'assoc_foreign_key' => function($model, $params) {
                $model->setValue('range_id', $params[0]->user_id);
                $model->setValue('sec_range_id', $params[0]->statusgruppe_id);
            },
            'assoc_func' => 'findByModel',
            'on_delete' => 'delete',
            'on_store'  => 'store',
        ];

        $config['additional_fields']['vorname']     = ['user', 'vorname'];
        $config['additional_fields']['nachname']    = ['user', 'nachname'];
        $config['additional_fields']['username']    = ['user', 'username'];
        $config['additional_fields']['email']       = ['user', 'email'];
        $config['additional_fields']['title_front'] = ['user', 'title_front'];
        $config['additional_fields']['title_rear']  = ['user', 'title_rear'];

        parent::configure($config);
    }

    /**
     * find and return all contactgroup entries
     *
     * @param Contact $contact
     * @return StatusgruppeUser[]
     */
    public static function findByContact(Contact $contact)
    {
        return self::findBySQL("INNER JOIN `statusgruppen` USING(`statusgruppe_id`) WHERE `statusgruppen`.`range_id` = ? AND `statusgruppe_user`.`user_id` = ?", [$contact->owner_id, $contact->user_id]);
    }

    /**
     * Prevents invisible users from being displayed
     *
     * @return string Fullname if visible else string for invisible user
     */
    public function name($format = 'full_rev')
    {
        return $this->user->getFullName($format);
    }

    public function getUserFullname($format = "full")
    {
        return User::build(array_merge(['motto' => ''], $this->toArray('vorname nachname username title_front title_rear')))->getFullName($format);
    }

    /**
     * Prevents the avatar of invisible users from being displayed
     *
     * @return mixed Useravatar if visible else dummyavatar
     */
    public function avatar()
    {
        return Avatar::getAvatar($this->user_id, $this->user->username)->getImageTag(Avatar::SMALL, ['title' => $this->name()]);
    }

    /**
     * {@inheritdoc }
     */
    public function store()
    {
        if ($this->isNew()) {
            $sql = "SELECT MAX(position)+1 FROM statusgruppe_user WHERE statusgruppe_id = ?";
            $stmt = DBManager::get()->prepare($sql);
            $stmt->execute([$this->statusgruppe_id]);
            $this->position = $stmt->fetchColumn() ?: 0;

            StudipLog::log(
                "STATUSGROUP_ADD_USER",
                $this['user_id'],
                $this['statusgruppe_id'],
                "Statusgruppe ".$this->group->name
            );
        }
        return parent::store();
    }

    /**
     * {@inheritdoc }
     */
    public function delete()
    {
        // Resort members
        $query = "UPDATE statusgruppe_user SET position = position - 1 WHERE statusgruppe_id = ? AND position > ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([$this->statusgruppe_id, $this->position]);

        StudipLog::log(
            "STATUSGROUP_REMOVE_USER",
            $this['user_id'],
            $this['statusgruppe_id'],
            "Statusgruppe ".$this->group->name
        );

        return parent::delete();
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $sorm = self::findBySQL("user_id = ?", [$storage->user_id]);
        if ($sorm) {
            $field_data = [];
            foreach ($sorm as $row) {
                $field_data[] = $row->toRawArray();
            }
            if ($field_data) {
                $storage->addTabularData(_('StatusgruppeUser'), 'statusgruppe_user', $field_data);
            }
        }
    }

}