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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
<?php
/**
* statusgroups.php - trails-controller for managing statusgroups
*
* 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>
* @author Sebastian Hobert <sebastian.hobert@uni-goettingen.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class Admin_StatusgroupsController extends AuthenticatedController
{
/**
* Initializes the controller
*
* @param String $action Action to execute
* @param Array $args Arguments
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if (Request::submitted('abort')) {
$this->redirect('admin/statusgroups/index');
}
$this->user_id = $GLOBALS['user']->user_id;
// Set pagelayout
PageLayout::addStyleSheet('studip-statusgroups.css');
PageLayout::addScript('studip-statusgroups.js');
PageLayout::setHelpKeyword("Basis.Allgemeines");
PageLayout::setTitle(_("Verwaltung von Funktionen und Gruppen"));
Navigation::activateItem('/admin/institute/groups');
// Change header_line if open object
if ($header_line = Context::getHeaderLine()) {
PageLayout::setTitle($header_line." - ".PageLayout::getTitle());
}
// Include url for ajax moving of members in group to page header
PageLayout::addHeadElement('meta', [
'name' => 'statusgroups-ajax-movable-endpoint',
'content' => $this->url_for('admin/statusgroups/move'),
]);
$this->setType();
// Check if the viewing user should get the admin interface
$this->tutor = $this->type['edit']($this->user_id);
}
/**
* Basic display of the groups
*/
public function index_action()
{
$lockrule = LockRules::getObjectRule(Context::getId());
$this->is_locked = LockRules::Check(Context::getId(), 'groups');
if ($lockrule && $lockrule->description && $this->is_locked) {
PageLayout::postMessage(MessageBox::info(formatLinks($lockrule->description)));
}
// Setup sidebar.
$sidebar = Sidebar::get();
if ($this->tutor) {
$widget = new ActionsWidget();
$widget->addLink(_('Neue Gruppe anlegen'),
$this->url_for('admin/statusgroups/editGroup'),
Icon::create('add', 'clickable'))
->asDialog('size=auto');
$widget->addLink(_('Gruppenreihenfolge ändern'),
$this->url_for('admin/statusgroups/sortGroups'),
Icon::create('arr_2down', 'clickable'))
->asDialog();
$sidebar->addWidget($widget);
}
// Collect all groups
$this->loadGroups();
$institute = Institute::find(Context::getId());
$this->membersOfInstitute = $institute->members->orderBy('nachname')->pluck('user_id');
$assigned = array_unique(array_flatten($institute->all_status_groups->map(function ($group) {
return $group->members->pluck('user_id');
})));
$this->not_assigned = array_diff(
$this->membersOfInstitute,
$assigned
);
// Create multiperson search type
$query = "SELECT auth_user_md5.user_id, CONCAT({$GLOBALS['_fullname_sql']['full']}, ' (', auth_user_md5.username, ')') as fullname
FROM auth_user_md5
LEFT JOIN user_info ON (user_info.user_id = auth_user_md5.user_id)
WHERE (
CONCAT(auth_user_md5.Vorname, ' ', auth_user_md5.Nachname) LIKE :input
OR CONCAT(auth_user_md5.Nachname, ' ', auth_user_md5.Vorname) LIKE :input
OR auth_user_md5.username LIKE :input
)
AND auth_user_md5.perms IN ('autor', 'tutor', 'dozent')
AND auth_user_md5.visible <> 'never'
ORDER BY Vorname, Nachname";
$this->searchType = new SQLSearch($query, _('Teilnehmende/n suchen'), 'username');
}
/**
* Interface to edit a group or create a new one.
*
* @param string group id
*/
public function editGroup_action($group_id = null)
{
$this->check('edit');
if (Request::isPost()) {
$group = new Statusgruppen($group_id);
if ($group->isNew()) {
$group->range_id = Context::getId();
}
$group->name = Request::i18n('name');
$group->name_w = Request::i18n('name_w');
$group->name_m = Request::i18n('name_m');
$group->size = Request::int('size', 0);
$group->range_id = Request::option('range_id', $group->range_id);
$group->position = Request::int('position', $group->position);
$group->selfassign = Request::int('selfassign', 0);
$group->store();
$group->setDatafields(Request::getArray('datafields'));
$message = $group->isNew()
? _('Die Gruppe wurde angelegt.')
: _('Die Gruppe wurde gespeichert');
PageLayout::postSuccess($message);
$this->redirect('admin/statusgroups');
return;
}
$this->group = new Statusgruppen($group_id);
if ($this->group->isNew()) {
$this->group->range_id = Context::getId();
}
$this->loadGroups();
}
/**
* Interface to sort groups
*/
public function sortGroups_action()
{
$this->check('edit');
if (Request::isPost()) {
$newOrder = json_decode(Request::get('ordering'));
$this->updateRecoursive($newOrder, Context::getId());
PageLayout::postMessage(MessageBox::success(_('Die Gruppenreihenfolge wurde gespeichert.')));
$this->redirect('admin/statusgroups');
return;
}
$this->loadGroups();
}
/**
* Action to add multiple members to a group.
*
* @param string group id
*/
public function memberAdd_action($group_id)
{
$this->check('edit');
$mp = MultiPersonSearch::load("add_statusgroup" . $group_id);
$this->group = new Statusgruppen($group_id);
$countAdded = 0;
foreach ($mp->getAddedUsers() as $a) {
if ($this->group->addUser($a)) {
$this->type['after_user_add']($a);
$countAdded++;
}
}
if ($countAdded > 0) {
$message = sprintf(ngettext('Es wurde eine Person hinzugefügt.',
'Es wurden %u Personen hinzugefügt.',
$countAdded),
$countAdded);
PageLayout::postMessage(MessageBox::success($message));
}
$this->redirect('admin/statusgroups#group-' . $group_id);
}
/**
* Ajax action to move a user
*/
public function move_action()
{
$this->check('edit');
$GLOBALS['perm']->check('tutor');
$group = Request::get('group');
$user_id = Request::get('user');
$pos = Request::get('pos');
$this->group = new Statusgruppen($group);
$this->group->moveUser($user_id, $pos);
$this->type['after_user_move']($user_id);
$this->users = $this->group->members;
$this->renderGroupOrRedirect($group);
}
/**
* Ajaxaction to delete a user
*/
public function delete_action($group_id, $user_id)
{
$this->check('edit');
$this->group = new Statusgruppen($group_id);
$this->user = new User($user_id);
if (Request::submitted('confirm')) {
$this->group->removeUser($user_id);
$this->type['after_user_delete']($user_id);
$this->renderGroupOrRedirect($group_id);
}
}
/**
* Delete a group
*/
public function deleteGroup_action($group_id)
{
$this->check('edit');
$this->group = new Statusgruppen($group_id);
if (Request::submitted('confirm')) {
CSRFProtection::verifyUnsafeRequest();
// move all subgroups to the parent
$children = SimpleORMapCollection::createFromArray($this->group->children);
$children->setValue('range_id', $this->group->range_id);
$children->store();
//remove users
$this->group->removeAllUsers();
//remove datafields
DataFieldEntry::removeAll(['', $this->group->statusgruppe_id]);
//goodbye group
$this->group->delete();
$this->redirect('admin/statusgroups/index');
}
}
/**
* Sort a group
*/
public function sortAlphabetic_action($group_id)
{
$this->check('edit');
$this->group = new Statusgruppen($group_id);
if (Request::submitted('confirm')) {
CSRFProtection::verifyUnsafeRequest();
$this->group->sortMembersAlphabetic();
$this->redirect('admin/statusgroups/index#group-' . $group_id);
}
}
/**
* Sort list of subgroups
*/
public function sortGroupsAlphabetical_action($group_id)
{
$this->check('edit');
$this->group = new Statusgruppen($group_id);
$this->group->sortSubGroupsAlphabetic();
$this->redirect('admin/statusgroups/index#group-' . $group_id);
}
/**
* Action to select institute. This should be put somewhere else since we
* have to do this on EVERY institute page
*/
public function selectInstitute_action()
{
}
/* *********************************
* ***** PRIVATE HELP FUNCTIONS ****
* *********************************/
/*
* Loads groups from the database.
*/
private function loadGroups()
{
$this->groups = Institute::findCurrent()->status_groups;
}
/*
* Updates groups recursivly.
*/
private function updateRecoursive($obj, $parent)
{
$i = 0;
if ($obj) {
foreach ($obj as $group) {
$statusgroup = new Statusgruppen($group->id);
$statusgroup->range_id = $parent;
$statusgroup->position = $i;
$statusgroup->store();
$this->updateRecoursive($group->children, $group->id);
$i++;
}
}
}
/*
* Renders an action (ajax) or redirects to the statusgroup index page (no ajax).
*/
private function renderGroupOrRedirect($group_id)
{
if (Request::isXhr()) {
$this->render_action('_members');
} else {
$this->redirect('admin/statusgroups#group-' . $group_id);
}
}
/*
* Checks if the current user has the specific $rights
*/
private function check($rights)
{
if (!$this->type[$rights]($this->user_id)) {
throw new AccessDeniedException();
}
}
/*
* This sets the type of statusgroup. By now it only supports
* Inst statusgroup but could be extended
*/
private function setType()
{
$type = Context::isInstitute() ? 'inst' : null;
$types = $this->types();
if (!$type || Request::submitted('type') && $type !== Request::get('type')) {
$types[Request::get('type', 'inst')]['redirect']();
} else {
$this->type = $types[$type];
}
}
/*
* This is the rest of the idea we could use statusgroups on other pages.
* navigation and redirect to selection page must move here if the
* statusgroupspage is reused
*
* @return type
*/
private function types()
{
return [
'inst' => [
'name' => _('Institut'),
'after_user_add' => function ($user_id) {
$newInstUser = InstituteMember::findByUserAndInstitute($user_id, Context::getId()) ?:
InstituteMember::build(['user_id' => $user_id, 'institut_id' => Context::getId()]);
if ($newInstUser->isNew() || $newInstUser->inst_perms == 'user') {
$user = new User($user_id);
$newInstUser->inst_perms = $user->perms;
if ($newInstUser->store()) {
InstituteMember::ensureDefaultInstituteForUser($user->id);
StudipLog::INST_USER_ADD(Context::getId(), $user->id, $user->perms);
}
}
},
'after_user_delete' => function ($user_id) {
null;
},
'after_user_move' => function ($user_id) {
null;
},
'view' => function ($user_id) {
return true;
},
'needs_size' => false,
'needs_self_assign' => false,
'edit' => function ($user_id) {
return $GLOBALS['perm']->have_studip_perm('admin', Context::getId()) && !LockRules::Check(Context::getId(), 'groups');
},
'redirect' => function () {
require_once 'lib/admin_search.inc.php';
die(); //must not return
},
'groups' => [
'members' => [
'name' => _('Mitglieder'),
]
]
]
];
}
}
|