aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/vips/config.php
blob: d6d4e4857db9ce63150c96e6c93a70bbe0a53159 (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
<?php
/**
 * vips/config.php - global configuration 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      Elmar Ludwig
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 */

class Vips_ConfigController extends AuthenticatedController
{
    /**
     * Callback function being called before an action is executed. If this
     * function does not return FALSE, the action will be called, otherwise
     * an error will be generated and processing will be aborted. If this function
     * already #rendered or #redirected, further processing of the action is
     * withheld.
     *
     * @param string  Name of the action to perform.
     * @param array   An array of arguments to the action.
     *
     * @return bool|void
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        $GLOBALS['perm']->check('root');

        Navigation::activateItem('/admin/config/vips');
        PageLayout::setHelpKeyword('Basis.VipsEinstellungen');
        PageLayout::setTitle(_('Einstellungen für Aufgaben'));
    }

    public function index_action()
    {
        $this->fields = DataField::getDataFields('user');
        $this->config = Config::get();

        $widget = new ActionsWidget();
        $widget->addLink(
            _('Anstehende Klausuren anzeigen'),
            $this->pending_assignmentsURL(),
            Icon::create('doctoral_cap')
        )->asDialog('size=big');
        Sidebar::get()->addWidget($widget);
    }

    public function save_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $exam_mode = Request::int('exam_mode', 0);
        $exam_terms = trim(Request::get('exam_terms'));
        $exam_terms = Studip\Markup::purifyHtml($exam_terms);

        $config = Config::get();
        $config->store('VIPS_EXAM_RESTRICTIONS', $exam_mode);
        $config->store('VIPS_EXAM_TERMS', $exam_terms);

        $room = Request::getArray('room');
        $ip_range = Request::getArray('ip_range');
        $ip_ranges = [];

        foreach ($room as $i => $name) {
            $name = preg_replace('/[ ,]+/', '_', trim($name));

            if ($name !== '') {
                $ip_ranges[$name] = trim($ip_range[$i]);
            }
        }

        if ($ip_ranges) {
            ksort($ip_ranges);
            $config->store('VIPS_EXAM_ROOMS', $ip_ranges);
        }

        PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));

        $this->redirect('vips/config');
    }

    public function pending_assignments_action()
    {
        $this->assignments = VipsAssignment::findBySQL(
            "range_type = 'course' AND type = 'exam' AND
             start BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY) AND UNIX_TIMESTAMP(NOW() + INTERVAL 14 DAY) AND end > UNIX_TIMESTAMP()
             ORDER BY start"
        );
    }
}