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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
<?php
use MassWidget\MassWidget;
use MassWidget\MassWidgetFilter;
use Studip\Forms\Form;
/**
* start.php - start page controller
*
*
* 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é Klaßen <klassen@elan-ev.de>
* @author Nadine Werner <nadine.werner@uni-osnabrueck.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 3.1
*/
class StartController extends AuthenticatedController
{
/**
* Callback function being called before an action is executed.
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
Navigation::activateItem('/start');
PageLayout::setTabNavigation(NULL); // disable display of tabs
PageLayout::setHelpKeyword('Basis.Startseite'); // set keyword for new help
PageLayout::setTitle(_('Startseite'));
}
/**
* Entry point of the controller that displays the start page of Stud.IP
*
* @param string $action
* @param string $widgetId
*
* @return void
*/
public function index_action($action = false, $widgetId = null)
{
$this->syncMassWidgets();
$plugin_manager = PluginManager::getInstance();
$widgets = WidgetUser::getWidgets($GLOBALS['user']->id);
$this->columns = [[], []];
foreach ($widgets as $col => $list) {
foreach ($list as $plugin_id) {
$plugin = $plugin_manager->getPluginById($plugin_id);
if ($plugin) {
$this->columns[$col][] = $plugin;
}
}
}
$this->widget_layout = $this->get_template_factory()->open('start/_widget.php');
$sidebar = Sidebar::get();
$nav = $sidebar->addWidget(new NavigationWidget());
$nav->setTitle(_('Sprungmarken'));
$nav->setId('navigation-layer-3');
foreach (array_merge(...$this->columns) as $widget) {
$nav->addLink(
$widget->getPluginName(),
$this->url_for("start#widget-" . $widget->getPluginId())
);
}
// Show action to add widget only if not all widgets have already been added.
$actions = $sidebar->addWidget(new ActionsWidget());
if ($this->getAvailableWidgets($GLOBALS['user']->id)) {
$actions->addLink(
_('Widgets hinzufügen'),
$this->url_for('start/add'),
Icon::create('add')
)->asDialog();
}
$actions->addLink(
_('Standard wiederherstellen'),
$this->url_for('start/reset'),
Icon::create('accept'),
['data-confirm' => _('Sollen wirklich alle Widgets auf den Standard zurückgesetzt werden?')]
);
// Root may set initial positions
if ($GLOBALS['perm']->have_perm('root')) {
$massWidgetActions = $sidebar->addWidget(new ActionsWidget());
$massWidgetActions->setTitle(_('Widgets für Zielgruppen'));
$massWidgetActions->addLink(
_('Regel hinzufügen'),
$this->url_for('start/masswidget_edit'),
Icon::create('add')
)->asDialog();
$massWidgetActions->addLink(
_('Regel-Übersicht'),
$this->url_for('start/masswidget_index'),
Icon::create('view-wall')
)->asDialog();
$settings = $sidebar->addWidget(new ActionsWidget());
$settings->setTitle(_('Einstellungen'));
$settings->addElement(new WidgetElement(_('Standard-Startseite bearbeiten:')));
foreach ($GLOBALS['perm']->permissions as $permission => $useless) {
$settings->addLink(
ucfirst($permission),
$this->url_for("start/edit_defaults/{$permission}"),
Icon::create('link-intern')
)->asDialog();
}
}
$this->messages = [];
if (User::findCurrent()->perms == 'user') {
$this->messages[] = MessageBox::info(
_('Sie haben noch nicht auf Ihre Bestätigungsmail geantwortet.'),
[
_('Bitte holen Sie dies nach, um Stud.IP Funktionen wie das Belegen von Veranstaltungen nutzen zu können.'),
sprintf(_('Bei Problemen wenden Sie sich an: %s'), '<a href="mailto:'.$GLOBALS['UNI_CONTACT'].'">'.$GLOBALS['UNI_CONTACT'].'</a>')
]
);
$details = Studip\LinkButton::create(
_('Bestätigungsmail erneut verschicken'),
$this->resend_validation_mailURL()
);
if (
!StudipAuthAbstract::CheckField('auth_user_md5.Email', User::findCurrent()->auth_plugin)
&& !LockRules::check(User::findCurrent()->id, 'email')
) {
$details .= ' ';
$details .= Studip\LinkButton::create(
_('E-Mail-Adresse ändern'),
$this->edit_mail_addressURL(),
[
'data-dialog' => 'size=auto',
'title' => _('E-Mail-Adresse')
]
);
}
$this->messages[] = MessageBox::info(
sprintf(
_('Haben Sie die Bestätigungsmail an Ihre Adresse "%s" nicht erhalten?'),
htmlReady(User::findCurrent()->email)
),
[$details]
);
}
}
/**
* Fetches all widgets that are not already in use.
*
* @param string $user_id the user to check
*
* @return array available widgets
*/
private function getAvailableWidgets($user_id)
{
$all_widgets = PluginEngine::getPlugins(PortalPlugin::class);
$user_widgets = WidgetUser::getWidgets($user_id);
$used_widgets = array_merge(...$user_widgets);
$available = [];
foreach ($all_widgets as $widget) {
if (!in_array($widget->getPluginId(), $used_widgets)) {
$available[] = $widget;
}
}
return $available;
}
/**
* This action adds one or more new widgets to the start page
*
* @return void
*/
public function add_action()
{
PageLayout::setTitle(_('Widgets hinzufügen'));
if (Request::isPost()) {
$ticket = Request::get('studip_ticket');
$widgets = Request::intArray('widget_id');
$position = Request::int('position');
$post_url = '';
if (check_ticket($ticket)) {
foreach ($widgets as $widget) {
WidgetUser::addWidget($GLOBALS['user']->id, $widget);
if (!$post_url) {
$post_url = '#widget-' . $widget;
}
}
}
$this->redirect('start' . $post_url);
}
$this->widgets = $this->getAvailableWidgets($GLOBALS['user']->id);
}
/**
* Edit the default startpage configuration for users by permissions
*
* @param string $permission
*
* @throws InvalidArgumentException
*/
public function edit_defaults_action($permission)
{
if (!in_array($permission, array_keys($GLOBALS['perm']->permissions))) {
throw new InvalidArgumentException('There is no such permission!');
}
PageLayout::setTitle(sprintf(_('Standard-Startseite für "%s" bearbeiten'), ucfirst($permission)));
$this->widgets = PluginEngine::getPlugins(PortalPlugin::class);
$this->initial_widgets = WidgetDefault::getWidgets($permission);
$this->permission = $permission;
}
/**
* Store the edited default startpage configuration for users by permissions
*
* @param string $permission
*
* @throws InvalidArgumentException
*/
public function update_defaults_action($permission)
{
$GLOBALS['perm']->check('root');
if (!in_array($permission, array_keys($GLOBALS['perm']->permissions))) {
throw new InvalidArgumentException('There is no such permission!');
}
$widgets = [Request::getArray('left'), Request::getArray('right')];
WidgetDefault::deleteBySQL('perm = ?', [$permission]);
foreach ($widgets as $col => $list) {
foreach ($list as $plugin_id => $position) {
WidgetDefault::create([
'pluginid' => $plugin_id,
'col' => $col,
'position' => $position,
'perm' => $permission
]);
}
}
$this->render_nothing();
}
/**
* This actions removes a new widget from the start page
*
* @param string $widgetId
* @param string $approveDelete
* @param string $studipticket
*/
public function delete_action($id)
{
$plugin_manager = PluginManager::getInstance();
$plugin_info = $plugin_manager->getPluginById($id);
$name = $plugin_info->getPluginName();
if (Request::isPost()) {
if (Request::submitted('yes')) {
if (WidgetUser::removeWidget($GLOBALS['user']->id, $id)) {
$message = sprintf(
_('Widget "%s" wurde entfernt.'),
htmlReady($name)
);
PageLayout::postSuccess($message);
} else {
$message = sprintf(
_('Widget "%s" konnte nicht entfernt werden.'),
htmlReady($name)
);
PageLayout::postError($message);
}
}
} else {
PageLayout::postQuestion(
sprintf(
_('Sind Sie sicher, dass Sie das Widget "%s" von der Startseite entfernen möchten?'),
htmlReady($name)
),
$this->url_for("start/delete/{$id}")
);
}
$this->redirect('start');
}
/**
* Resets widget to initial default state.
*/
public function reset_action()
{
WidgetUser::deleteBySQL('range_id = ?', [$GLOBALS['user']->id]);
$message = _('Die Widgets wurden auf die Standardkonfiguration zurückgesetzt.');
PageLayout::postSuccess($message);
$this->redirect('start');
}
/**
* Action to store the widget placements
*/
public function storeNewOrder_action(): void
{
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
$lanes = Request::getArray('lanes');
WidgetUser::setInitialWidgets($GLOBALS['user']->id);
foreach ($lanes as $column => $list) {
foreach ($list as $position => $plugin_id) {
$widget = WidgetUser::findOneBySQL('pluginid = ? AND range_id = ?', [$plugin_id, $GLOBALS['user']->id]);
$widget->position = $position;
$widget->col = $column;
$widget->store();
}
}
$this->render_nothing();
}
/**
* Resend the validation mail for the current user
*
* @return void
*/
public function resend_validation_mail_action()
{
if ($GLOBALS['perm']->get_perm() === 'user') {
auth()->sendValidationMail();
PageLayout::postSuccess(
_('Die Bestätigungsmail wurde erneut verschickt.')
);
}
$this->redirect('start');
}
/**
* Show form to change the mail-address for the validation mail
*
* @return void
*/
public function edit_mail_address_action()
{
// only allow editing of mail-address here if user has not yet validated
if ($GLOBALS['perm']->get_perm() !== 'user') {
$this->redirect('start');
return;
}
$this->restricted = StudipAuthAbstract::CheckField('auth_user_md5.Email', $GLOBALS['user']->auth_plugin)
&& LockRules::check($GLOBALS['user']->id, 'email');
$this->email = $GLOBALS['user']->Email;
}
/**
* Change the mail-address and resend validation mail
*
* @return void
*/
public function change_mail_address_action()
{
$email1 = Request::get('email1');
$email2 = Request::get('email2');
if ($GLOBALS['perm']->get_perm() == 'user') {
if($email1 != $email2) {
PageLayout::postError(_('Die Wiederholung der E-Mail-Adresse stimmt nicht mit Ihrer Eingabe überein.'));
$this->redirect('start/edit_mail_address');
return;
}
$user = \User::findCurrent();
$user->Email = $email1;
$user->store();
auth()->sendValidationMail($user);
PageLayout::postMessage(MessageBox::success(
_('Ihre Mailadresse wurde geändert und die Bestätigungsmail erneut verschickt.')
));
}
$this->relocate('start');
}
public function masswidget_index_action()
{
$GLOBALS['perm']->check('root');
$this->massWidgets = MassWidget::findBySQL('1');
}
public function masswidget_edit_action(?MassWidget $massWidget = null)
{
$GLOBALS['perm']->check('root');
PageLayout::setTitle(_('Widget hinzufügen'));
if (Request::isPost() && !$massWidget->isNew()) {
CSRFProtection::verifyUnsafeRequest();
if (
Request::get('target') !== $massWidget->target
|| Request::int('plugin_id') !== $massWidget->plugin_id
) {
$massWidget->deleteUserWidgets();
}
}
// SearchType needed for course selection
$courseSearch = new StandardSearch('Seminar_id');
$widgets = PluginEngine::getPlugins(PortalPlugin::class);
$availableWidgets = [];
foreach ($widgets as $widget) {
$availableWidgets[$widget->getPluginId()] = htmlReady($widget->getPluginName());
}
$semesters = [];
foreach (array_reverse(Semester::getAll()) as $one) {
$semesters[$one->id] = $one->name;
}
$form = Form::fromSORM(
$massWidget,
[
'legend' => _('Zielgruppe'),
'collapsed' => false,
'collapsable' => false,
'fields' => [
'target' => [
'type' => 'select',
'required' => true,
'label' => _('Zielgruppe'),
'value' => $massWidget->target ?? 'all',
'options' => MassWidget::getTargets()
],
'student_filters' => [
'type' => 'userFilter',
'label' => _('Filterauswahl'),
'if' => 'target === "students"',
'context' => '',
'target' => 'students',
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'students') {
$input->getContextObject()->filters = $this->buildMassWidgetFilters($value);
}
}
],
'employee_filters' => [
'type' => 'userFilter',
'label' => _('Filterauswahl'),
'if' => 'target === "employees"',
'context' => '',
'target' => 'employees',
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'employees') {
$input->getContextObject()->filters = $this->buildMassWidgetFilters($value);
}
}
],
'semester' => [
'type' => 'select',
'label' => _('Semester'),
'value' => $massWidget->settings['semester'] ?? \Semester::findDefault()->id,
'if' => 'target === "lecturers"',
'options' => $semesters,
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'lecturers') {
$input->getContextObject()->settings = ['semester' => $value];
}
}
],
'courses' => [
'type' => 'quicksearchList',
'label' => _('Veranstaltungen'),
'value' => json_encode($massWidget->settings?->getArrayCopy()['courses'] ?? []),
'if' => 'target === "courses"',
'searchtype' => $courseSearch,
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'courses') {
$input->getContextObject()->settings = [];
$input->getContextObject()->settings['courses'] = \Course::findAndMapMany(
function ($course) {
return ['id' => $course->id, 'name' => $course->getFullname()];
},
json_decode($value, true)
);
}
}
],
'course_perm' => [
'type' => 'select',
'label' => _('Rechtestufe'),
'value' => $massWidget->settings['perm'] ?? 'autor',
'if' => 'target === "courses"',
'options' => [
'dozent' => get_title_for_status('dozent', 2, 1),
'tutor' => get_title_for_status('tutor', 2, 1),
'autor' => get_title_for_status('autor', 2, 1),
'user' => get_title_for_status('user', 2, 1),
],
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'courses') {
$input->getContextObject()->settings['perm'] = $value;
}
}
],
'manual_usernames' => [
'type' => 'textarea',
'label' => _('Liste von Benutzernamen (durch Zeilenumbruch getrennt)'),
'if' => 'target === "usernames"',
'value' => $massWidget->settings['usernames'] ?? '',
'store' => function($value, $input) {
if ($input->getContextObject()->target === 'usernames') {
$input->getContextObject()->settings = [];
$input->getContextObject()->settings['usernames'] = $value;
}
}
],
]
],
$this->url_for('start')
)->addSORM($massWidget, [
'legend' => _('Widget'),
'collapsable' => false,
'collapsed' => false,
'fields' => [
'author_id' => [
'type' => 'hidden',
'value' => User::findCurrent()->id
],
'name' => [
'type' => 'text',
'required' => true,
'label' => _('Name'),
'value' => $massWidget->name
],
'plugin_id' => [
'type' => 'select',
'required' => true,
'label' => _('Widget'),
'value' => $massWidget->plugin_id ?? '0',
'options' => $availableWidgets
],
'col' => [
'if' => 'plugin_id > 0',
'value' => $massWidget->col ?? '0',
'label' => _('Spalte'),
'type' => 'radio',
'orientation' => 'vertical',
'options' => [
'0' => _('Links'),
'1' => _('Rechts'),
],
],
'row' => [
'if' => 'plugin_id > 0',
'value' => $massWidget->row ?? '0',
'type' => 'radio',
'label' => _('Zeile'),
'orientation' => 'vertical',
'default' => '0',
'options' => [
'0' => _('Oben'),
'1' => _('Unten'),
],
]
]
])
->setSuccessMessage(_('Die Regel für das Widget wurde gespeichert.'))
->autoStore();
$this->render_form($form);
}
public function masswidget_delete_action(MassWidget $massWidget)
{
CSRFProtection::verifyUnsafeRequest();
$GLOBALS['perm']->check('root');
$massWidget->deleteUserWidgets()->delete();
PageLayout::postSuccess(_('Die Regel für das Widget wurde gelöscht.'));
$this->redirect($this->url_for('start'));
}
public function syncMassWidgets(): void
{
$massWidgets = MassWidget::findBySQL('1');
if (count($massWidgets) > 0) {
WidgetUser::setInitialWidgets(User::findCurrent()->id);
}
foreach ($massWidgets as $massWidget) {
$recipientIds = $massWidget->getTargetUserIds();
if (in_array(User::findCurrent()->id, $recipientIds)) {
$userWidget = WidgetUser::findOneBySQL(
'pluginid = :plugin_id AND range_id = :user_id',
['plugin_id' => $massWidget->plugin_id, 'user_id' => User::findCurrent()->id]
);
if (!$userWidget) {
$userWidget = new WidgetUser();
$userWidget->range_id = User::findCurrent()->id;
$userWidget->pluginid = $massWidget->plugin_id;
}
if ($userWidget->isNew() || $massWidget->chdate > $userWidget->chdate) {
if ((int) $massWidget->row === 0) {
$minRow = DBManager::get()->fetchColumn(
"SELECT MIN(`position`) - 1 FROM `widget_user` WHERE `range_id` = ?",
[User::findCurrent()->id]
);
$userWidget->position = $minRow;
} else {
$maxRow = DBManager::get()->fetchColumn(
"SELECT MAX(`position`) + 1 FROM `widget_user` WHERE `range_id` = ?",
[User::findCurrent()->id]
);
$userWidget->position = $maxRow;
}
$userWidget->is_active = 1;
$userWidget->col = $massWidget->col ?? 0;
$userWidget->store();
}
}
}
}
private function buildMassWidgetFilters(array $value): array
{
$filters = [];
foreach ($value as $one) {
$filter = new UserFilter($one['id'] ?? '');
$filter->fields = [];
foreach ($one['attributes']['fields'] as $field) {
$className = $field['attributes']['type'];
if (!is_a($className, UserFilterField::class, true)) {
throw new InvalidArgumentException('Only user filters allowed');
}
$f = new $className();
if (!empty($field['id'])) {
$f->setId($field['id']);
}
$f->setCompareOperator($field['attributes']['compare-operator']);
$f->setValue($field['attributes']['value']);
$filter->addField($f);
}
$filter->store();
$connection = new MassWidgetFilter();
$connection->filter_id = $filter->getId();
$filters[] = $connection;
}
return $filters;
}
}
|