aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/vips/admin.php
blob: 92c6cc7bc13a3e1bcbe6336258a1fc9a52569b99 (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
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
<?php
/**
 * vips/admin.php - course administration 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_AdminController extends AuthenticatedController
{
    /**
     * Edit or create a block in the course.
     */
    public function edit_block_action()
    {
        Navigation::activateItem('/course/vips/sheets');
        PageLayout::setHelpKeyword('Basis.Vips');

        $block_id = Request::int('block_id');

        if ($block_id) {
            $block = VipsBlock::find($block_id);
        } else {
            $block = new VipsBlock();
            $block->range_id = Context::getId();
        }

        VipsModule::requireStatus('tutor', $block->range_id);

        $this->block = $block;
        $this->groups = Statusgruppen::findBySeminar_id($block->range_id);
    }

    /**
     * Store changes to a block.
     */
    public function store_block_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $block_id = Request::int('block_id');
        $group_id = Request::option('group_id');

        if ($block_id) {
            $block = VipsBlock::find($block_id);
        } else {
            $block = new VipsBlock();
            $block->range_id = Context::getId();
        }

        VipsModule::requireStatus('tutor', $block->range_id);

        $block->name = Request::get('block_name');
        $block->group_id = $group_id ?: null;
        $block->visible = $group_id !== '';

        if (!Request::int('block_grouped')) {
            $block->weight = null;
        } else if ($block->weight === null) {
            $block->weight = 0;

            if ($block_id) {
                // sum up individual assignment weights for total block weight
                foreach (VipsAssignment::findByBlock_id($block_id) as $assignment) {
                    $block->weight += $assignment->weight;
                }
            }
        }

        $block->store();

        PageLayout::postSuccess(sprintf(_('Der Block „%s“ wurde gespeichert.'), htmlReady($block->name)));

        $this->redirect($this->url_for('vips/sheets', ['group' => 1]));
    }

    /**
     * Delete a block from the course.
     */
    public function delete_block_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $block_id = Request::int('block_id');
        $block = VipsBlock::find($block_id);
        $block_name = $block->name;

        VipsModule::requireStatus('tutor', $block->range_id);

        if ($block->delete()) {
            PageLayout::postSuccess(sprintf(_('Der Block „%s“ wurde gelöscht.'), htmlReady($block_name)));
        }

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

    /**
     * Stores the weights of blocks, sheets and exams
     */
    public function store_weight_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $assignment_weight = Request::floatArray('assignment_weight');
        $block_weight      = Request::floatArray('block_weight');

        foreach ($assignment_weight as $assignment_id => $weight) {
            $assignment = VipsAssignment::find($assignment_id);
            VipsModule::requireEditPermission($assignment);

            $assignment->weight = $weight;
            $assignment->store();
        }

        foreach ($block_weight as $block_id => $weight) {
            $block = VipsBlock::find($block_id);
            VipsModule::requireStatus('tutor', $block->range_id);

            $block->weight = $weight;
            $block->store();
        }

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

    /**
     * Edit the grade distribution settings.
     */
    public function edit_grades_action()
    {
        Navigation::activateItem('/course/vips/solutions');
        PageLayout::setHelpKeyword('Basis.VipsErgebnisse');

        $course_id = Context::getId();
        VipsModule::requireStatus('tutor', $course_id);

        $grades = ['1,0', '1,3', '1,7', '2,0', '2,3', '2,7', '3,0', '3,3', '3,7', '4,0'];
        $percentages = array_fill(0, count($grades), '');
        $comments = array_fill(0, count($grades), '');
        $settings = CourseConfig::get($course_id);

        foreach ($settings->VIPS_COURSE_GRADES as $value) {
            $index = array_search($value['grade'], $grades);

            if ($index !== false) {
                $percentages[$index] = $value['percent'];
                $comments[$index]    = $value['comment'];
            }
        }

        $this->grades            = $grades;
        $this->grade_settings    = $settings->VIPS_COURSE_GRADES;
        $this->percentages       = $percentages;
        $this->comments          = $comments;
    }

    /**
     * Stores the distribution of grades
     */
    public function store_grades_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $course_id = Context::getId();
        VipsModule::requireStatus('tutor', $course_id);

        $grades = ['1,0', '1,3', '1,7', '2,0', '2,3', '2,7', '3,0', '3,3', '3,7', '4,0'];
        $percentages = Request::floatArray('percentage');
        $comments = Request::getArray('comment');
        $grade_settings = [];
        $percent_last = 101;
        $error = false;

        foreach ($percentages as $i => $percent) {
            if ($percent) {
                $grade_settings[] = [
                    'grade'   => $grades[$i],
                    'percent' => $percent,
                    'comment' => trim($comments[$i])
                ];

                if ($percent < 0 || $percent > 100) {
                    PageLayout::postError(_('Die Notenwerte müssen zwischen 0 und 100 liegen!'));
                    $error = true;
                } else if ($percent_last <= $percent) {
                    PageLayout::postError(sprintf(_('Die Notenwerte müssen monoton absteigen (%s > %s)!'), $percent_last, $percent));
                    $error = true;
                }

                $percent_last = $percent;
            }
        }

        if (!$error) {
            $settings = CourseConfig::get($course_id);
            $settings->store('VIPS_COURSE_GRADES', $grade_settings);

            PageLayout::postSuccess(_('Die Notenwerte wurden eingetragen.'));
        }

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