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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
<?php
/**
* Datafield
* model class for table datafields
*
* 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 datafield_id database column
* @property string id alias column for datafield_id
* @property string name database column
* @property string object_type database column
* @property string object_class database column
* @property string edit_perms database column
* @property string view_perms database column
* @property string institut_id database column
* @property string system database column
* @property string priority database column
* @property string mkdate database column
* @property string chdate database column
* @property string type database column
* @property string typeparam database column
* @property string is_required database column
* @property string default_value database column
* @property string is_userfilter database column
* @property string description database column
* @property SimpleORMapCollection entries has_many DatafieldEntryModel
*/
class DataField extends SimpleORMap implements PrivacyObject
{
/**
* Configures this model.
*
* @param Array $config Configuration array
*/
protected static function configure($config = [])
{
$config['db_table'] = 'datafields';
$config['has_many']['entries'] = [
'class_name' => DatafieldEntryModel::class,
'on_delete' => 'delete',
];
$config['has_many']['visibility_settings'] = [
'class_name' => User_Visibility_Settings::class,
'assoc_foreign_key' => 'identifier',
'on_delete' => 'delete',
];
$config['additional_fields']['institution'] = array(
'get' => function ($object, $field) {
$institution = Institute::find($object->institut_id);
if (!$institution) {
return false;
}
return $institution;
},
'set' => false,
);
$config['i18n_fields']['name'] = true;
parent::configure($config);
}
protected static $permission_masks = [
'user' => 1,
'autor' => 2,
'tutor' => 4,
'dozent' => 8,
'admin' => 16,
'root' => 32,
'self' => 64,
];
/**
* Returns a collection of datafields filtered by objectType,
* objectClass and/or unassigned objectClasses.
*
* @param mixed $objectType Object type
* @param String $objectClass Object class
* @param bool $includeNullClass Should the object class "null" be
* included
* @return array of DataField instances
*/
public static function getDataFields($objectType = null, $objectClass = '', $includeNullClass = false)
{
$conditions = [];
$parameters = [];
if ($objectType !== null) {
$conditions[] = 'object_type = ?';
$parameters[] = $objectType;
}
if ($objectClass) {
if (in_array($objectType, ['user', 'userinstrole', 'usersemdata', 'roleinstdata'])) {
$condition = ['object_class & ?'];
} else {
$condition = ['object_class = ?'];
}
if ($includeNullClass) {
$condition[] = 'object_class IS NULL';
}
$conditions[] = '(' . implode(' OR ', $condition) . ')';
$parameters[] = $objectClass;
}
$where = implode(' AND ', $conditions) ?: '1';
return self::findBySQL($where . " ORDER BY priority ASC, name ASC", $parameters);
}
/**
* Returns a list of all datatype classes with an id as key and a name as
* value.
*
* @return array list of all datatype classes
*/
public static function getDataClass()
{
return [
'sem' => _('Veranstaltungen'),
'inst' => _('Einrichtungen'),
'user' => _('Benutzer'),
'userinstrole' => _('Benutzerrollen in Einrichtungen'),
'usersemdata' => _('Benutzer-Zusatzangaben in VA'),
'roleinstdata' => _('Rollen in Einrichtungen'),
'moduldeskriptor' => _('Moduldeskriptoren'),
'modulteildeskriptor' => _('Modulteildeskriptoren'),
'studycourse' => _('Studiengänge')
];
}
/**
* Return the mask for the given permission
*
* @param string $perm the name of the permission
* @return integer the mask for the permission
* @static
*/
public static function permMask($perm)
{
return self::$permission_masks[$perm] ?? 0;
}
/**
* liefert String zu gegebener user_class-Maske
*
* @param integer $class the user class mask
* @return string a string consisting of a comma separated list of
* permissions
*/
public static function getReadableUserClass($class)
{
$result = [];
foreach (self::$permission_masks as $perm => $mask) {
if ($class & $mask) {
$result[] = $perm;
}
}
return implode(', ', $result);
}
/**
* Legacy handler for access via [get|set]VariableName().
*
* @param String $method Called method
* @param Array $arguments Given arguments
* @return mixed Return value of the getter/setter
* @throws BadMethodCallException when the method does not match a
* valid pattern
*/
public function __call($method, array $arguments)
{
if (mb_substr($method, 0, 3) === 'get') {
return $this->getValue(mb_substr($method, 3));
}
if (mb_substr($method, 0, 3) === 'set') {
return $this->setValue(mb_substr($method, 3), $arguments[0]);
}
throw new BadMethodCallException('Call to undefined method ' . __CLASS__ . '::' . $method);
}
/**
* Sets the type and adjusts type param as well.
*
* @param String $type Type of this datafield
*/
public function setType($type)
{
$this->content['type'] = $type;
if (!in_array($type, words('selectbox selectboxmultiple radio combo'))) {
$this->typeparam = '';
}
}
/**
* Returns whether a user may access this datafield.
*
* @param String $perm Permission of the user, optional defaults to
* current user
* @param String $watcher Current user
* @param String $user Associated user of the datafield
* @return bool indicating whether the datafield may be accessed.
*/
public function accessAllowed($perm = null, $watcher = '', $user = '')
{
if ($perm === null) {
$perm = $GLOBALS['user']->perms;
}
$user_perms = self::permMask($perm);
$required_perms = self::permMask($this->view_perms);
# permission is sufficient
if ($user_perms >= $required_perms) {
return true;
}
// user may see his own data if this either no system field
// or the user may edit the field
if ($watcher && $user && $user === $watcher &&
(!$this->system || $this->editAllowed($perm))) {
return true;
}
# nothing matched...
return false;
}
/**
* Returns whether a user may edit this datafield.
*
* @param String $userPerms Permissions of the user
* @return bool indicating whether the datafield may be edited
*/
public function editAllowed($userPerms)
{
$user_perms = self::permMask($userPerms);
$required_perms = self::permMask($this->edit_perms);
return $user_perms >= $required_perms;
}
/**
* Specialized count method that returns the number of concrete entries.
*
* @return int number of entries
*
* @todo Add int return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function count()
{
return DatafieldEntryModel::countBySQL('datafield_id = ?', [$this->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 = DataField::findThru($storage->user_id, [
'thru_table' => 'datafields_entries',
'thru_key' => 'range_id',
'thru_assoc_key' => 'datafield_id',
'assoc_foreign_key' => 'datafield_id',
]);
if ($sorm) {
$field_data = [];
foreach ($sorm as $row) {
$field_data[] = $row->toRawArray();
}
if ($field_data) {
$storage->addTabularData(_('Datenfelder'), 'datafields', $field_data);
}
}
}
}
|