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
|
<?php
/**
* CourseMember.class.php
* model class for table seminar_user
*
* 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 André Noack <noack@data-quest.de>
* @copyright 2012 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*
* @property string seminar_id database column
* @property string user_id database column
* @property string status database column
* @property string position database column
* @property string gruppe database column
* @property string notification database column
* @property string mkdate database column
* @property string comment database column
* @property string visible database column
* @property string label database column
* @property string bind_calendar database column
* @property string vorname computed column read/write
* @property string nachname computed column read/write
* @property string username computed column read/write
* @property string email computed column read/write
* @property string title_front computed column read/write
* @property string title_rear computed column read/write
* @property string course_name computed column read/write
* @property string id computed column read/write
* @property SimpleORMapCollection datafields has_many DatafieldEntryModel
* @property User user belongs_to User
* @property Course course belongs_to Course
*/
class CourseMember extends SimpleORMap implements PrivacyObject
{
protected static function configure($config = [])
{
$config['db_table'] = 'seminar_user';
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id',
];
$config['belongs_to']['course'] = [
'class_name' => Course::class,
'foreign_key' => 'seminar_id',
];
$config['has_many']['datafields'] = [
'class_name' => DatafieldEntryModel::class,
'assoc_foreign_key' =>
function($model, $params) {
list($sec_range_id, $range_id) = (array)$params[0]->getId();
$model->setValue('range_id', $range_id);
$model->setValue('sec_range_id', $sec_range_id);
},
'assoc_func' => 'findByModel',
'on_delete' => 'delete',
'on_store' => 'store',
'foreign_key' =>
function($course_member) {
return [$course_member];
}
];
$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'];
$config['additional_fields']['course_name'] = [];
$config['registered_callbacks']['after_delete'][] = 'cbRemoveNotifications';
parent::configure($config);
}
public static function findByCourse($course_id)
{
$query = "SELECT seminar_user.*, aum.Vorname, aum.Nachname, aum.Email,
aum.username, ui.title_front, ui.title_rear
FROM seminar_user
LEFT JOIN auth_user_md5 aum USING (user_id)
LEFT JOIN user_info ui USING (user_id)
WHERE seminar_id = ?
ORDER BY position, Nachname, Vorname";
return DBManager::get()->fetchAll(
$query,
[$course_id],
__CLASS__ . '::buildExisting'
);
}
public static function findByCourseAndStatus($course_id, $status)
{
$query = "SELECT seminar_user.*, aum.Vorname, aum.Nachname, aum.Email,
aum.username, ui.title_front, ui.title_rear
FROM seminar_user
LEFT JOIN auth_user_md5 aum USING (user_id)
LEFT JOIN user_info ui USING (user_id)
WHERE seminar_id = ?
AND seminar_user.status IN (?)
ORDER BY status, position, Nachname, Vorname";
return DBManager::get()->fetchAll(
$query,
[$course_id, is_array($status) ? $status : words($status)],
__CLASS__ . '::buildExisting'
);
}
public static function findByUser($user_id)
{
$query = "SELECT seminar_user.*, seminare.Name AS course_name
FROM seminar_user
LEFT JOIN seminare USING (seminar_id)
WHERE user_id = ?
ORDER BY seminare.Name";
return DBManager::get()->fetchAll(
$query,
[$user_id],
__CLASS__ . '::buildExisting'
);
}
/**
* Retrieves the number of all members of a status
*
* @param String|Array $status the status to filter with
*
* @return int the number of all those members.
*/
public static function countByCourseAndStatus($course_id, $status)
{
return self::countBySql(
'seminar_id = ? AND status IN(?)',
[$course_id, is_array($status) ? $status : words($status)]
);
}
public function getUserFullname($format = 'full')
{
return User::build(array_merge(
['motto' => ''],
$this->toArray('vorname nachname username title_front title_rear')
))->getFullname($format);
}
public function cbRemoveNotifications()
{
CourseMemberNotification::deleteBySQL('user_id = ?', [$this->user_id]);
}
/**
* 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(_('SeminareUser'), 'seminar_user', $field_data);
}
}
}
}
|