aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/evaluation/profiles.php
blob: c394677950b76d0f11dbfa2558952a2c4e52ed19 (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
<?php
class Evaluation_ProfilesController extends AuthenticatedController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);
        if (!EvaluationHelper::isPermittedEvaluationAccess()) {
            throw new AccessDeniedException();
        }
    }

    public function index_action(): void
    {
        Navigation::activateItem('/evaluation/profiles');
        $this->profiles = QuestionnaireEvalCentralProfile::findBySQL(
            "INNER JOIN `semester_data`
            USING (`semester_id`)
            ORDER BY `semester_data`.`beginn` DESC"
        );
    }

    public function preedit_action()
    {
        $this->semesters = $this->getAvailableSemesters(true);
        $this->total_sem_count = Semester::countBySql();
        $this->render_template('evaluation/profiles/preedit');
    }

    public function edit_action(string $id = null): void
    {
        $sem_preselect = Request::get('sem_select');
        if ($id) {
            $profile = QuestionnaireEvalCentralProfile::find($id);
            $semesters = [$profile->semester_id => $profile->semester->name];
        } else {
            $profile_preselect = QuestionnaireEvalCentralProfile::find($sem_preselect);
            $profile = QuestionnaireEvalCentralProfile::build($profile_preselect);
            $semesters = $this->getAvailableSemesters();
        }
        $is_fill = $sem_preselect || $id;

        $template_array = [];
        Questionnaire::findEachBySQL(
            function($row) use (&$template_array) {
                $template_array[$row['questionnaire_id']] = $row['title'];
            },
            "`is_template` = 1"
        );

        $form = \Studip\Forms\Form::fromSORM($profile, [
            'legend' => $id ? htmlReady($profile->semester->name) . _(' bearbeiten') : _('Profil anlegen'),
            'fields' => [
                'semester_id' => [
                    'label'    => _('Semester'),
                    'required' => true,
                    'type'     => 'select',
                    'options'  => $semesters,
                    'value'    => $profile->semester_id ?? null
                ],
                'template_id' => [
                    'label'    => _('Vorlage'),
                    'required' => true,
                    'type'     => 'select',
                    'options'  => $template_array,
                    'value'    => $profile->template_id ?? null
                ],
                'optional_templates' => [
                    'label'   => _('Alternative Vorlagen'),
                    'type'    => 'multiselect',
                    'options' => $template_array,
                    'mapper'  => function($value) {
                        return implode(',', $value);
                    },
                    'value'   => $is_fill && $profile->optional_templates ?
                        explode(',', $profile->optional_templates) : null
                ],
                'startdate' => [
                    'label'    => _('Start'),
                    'name'     => 'startdate',
                    'required' => true,
                    'type'     => 'datetimepicker',
                    'value'    => $is_fill ? $profile->startdate : time() //TODO sem
                ],
                'stopdate' => [
                    'label'    => _('Ende'),
                    'required' => true,
                    'type'     => 'datetimepicker',
                    'value'    => $is_fill ? $profile->stopdate : time(), //TODO sem
                    'mindate'  => 'startdate'
                ],
                'info' => [
                  'label' => _('Information'),
                  'type'  => 'info',
                  'value' => _('Antworten können nicht revidiert werden, wenn sie anonym sind.')
                ],
                'anonymous' => [
                    'label' => _('Anonyme Teilnahme'),
                    'type'  => 'checkbox',
                    'value' => $is_fill ? $profile->anonymous : true
                ],
                'editanswers' => [
                    'label' => _('Antworten revidierbar'),
                    'type'  => 'checkbox',
                    'value' => $is_fill ? $profile->editanswers : false
                ],
                'resultvisibility' => [
                    'label'   => _('Zeitpunkt der Ergebnis-Einsicht'),
                    'type'    => 'select',
                    'options' => QuestionnaireEvalCentralProfile::getTranslatedVisibilityOptions(),
                    'value'   => $is_fill ? $profile->resultvisibility : 'never'
                ],
                'result_visible_for' => [
                    'label'   => _('Ergebnis-Einsicht für (Evaluations-Admins immer)'),
                    'type'    => 'select',
                    'options' => QuestionnaireEvalCentralProfile::getTranslatedVisibilityOptions(true),
                    'value'   => $is_fill ? $profile->result_visible_for : null
                ],
                'minimum_responses' => [
                    'label' => _('Mindestrücklauf'),
                    'type'  => 'number',
                    'value' => $is_fill ? $profile->minimum_responses : 8,
                    'min'   => 0
                ]
            ]
        ], $this->url_for('evaluation/profiles')
        )
        ->setSuccessMessage(_('Erfolgreich gespeichert.'))
        ->addStoreCallback(function ($form) {
            /** @var QuestionnaireEvalCentralProfile $profile */
            $profile = $form->getLastPart()->getContextObject();
            $optional_array = explode(',', $profile->optional_templates);
            $key = array_search($profile->template_id, $optional_array);
            if ($key !== false) {
                unset($optional_array[$key]);
                $profile->optional_templates = implode(',', $optional_array);
                $profile->store();
            }
            if ($profile->anonymous && $profile->editanswers) {
                $profile->editanswers = false;
                $profile->store();
            }
        })
        ->autoStore();

        PageLayout::setTitle(
            $id ? _('Profil bearbeiten: ') . htmlReady($profile->semester->name) : _('Neues Profil'));
        $this->render_form($form);
    }

    public function getAvailableSemesters(bool $reverse = false): array
    {
        $semesters = [];
        Semester::findEachBySQL(
            function($row) use (&$semesters) {
                $semesters[$row['semester_id']] = $row['name'];
            },
            $reverse ?
                "`semester_id` IN (SELECT `semester_id` FROM `questionnaire_eval_central_profiles`)
                ORDER BY `beginn` desc" :
                "`semester_id` NOT IN (SELECT `semester_id` FROM `questionnaire_eval_central_profiles`)
                    ORDER BY `beginn` desc"
        );
        return $semesters;
    }

    public function bulkdelete_action(): void
    {
        CSRFProtection::verifyUnsafeRequest();
        $profiles = QuestionnaireEvalCentralProfile::findMany(Request::optionArray('profiles'));
        foreach ($profiles as $profile) {
            $profile->delete();
        }
        $this->redirect('evaluation/profiles');
    }
}