aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Questionnaire.php
blob: 62b358db9b932c649077492ed505c5005bd478b5 (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
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
<?php
/**
 *
 * @property string $id alias column for questionnaire_id
 * @property string $questionnaire_id database column
 * @property string $template_id database column
 * @property string $title database column
 * @property string|null $description database column
 * @property string $user_id database column
 * @property int $is_template database column
 * @property int $template_is_enabled database column
 * @property int|null $startdate database column
 * @property int|null $stopdate database column
 * @property int $visible database column
 * @property int $anonymous database column
 * @property string $resultvisibility database column
 * @property string $result_visible_for database column
 * @property int $editanswers database column
 * @property int $copyable database column
 * @property int $minimum_responses database column
 * @property int $chdate database column
 * @property int $mkdate database column
 * @property SimpleORMapCollection<QuestionnaireQuestion> $questions has_many QuestionnaireQuestion
 * @property SimpleORMapCollection<QuestionnaireAssignment> $assignments has_many QuestionnaireAssignment
 * @property QuestionnaireEvalAssignment|null $eval_assignment has_one QuestionnaireEvalAssignment
 * @property SimpleORMapCollection<QuestionnaireAnonymousAnswer> $anonymousanswers has_many QuestionnaireAnonymousAnswer
 */
class Questionnaire extends SimpleORMap implements PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'questionnaires';

        $config['has_many']['questions'] = [
            'class_name' => QuestionnaireQuestion::class,
            'order_by' => 'ORDER BY position ASC',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_many']['assignments'] = [
            'class_name' => QuestionnaireAssignment::class,
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_many']['anonymousanswers'] = [
            'class_name' => QuestionnaireAnonymousAnswer::class,
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_one']['eval_assignment'] = [
            'class_name' => QuestionnaireEvalAssignment::class,
            'assoc_foreign_key' => 'questionnaire_id',
            'on_delete'  => 'delete'
        ];

        parent::configure($config);
    }

    public $answerable;

    public function countAnswers()
    {
        $statement = DBManager::get()->prepare("
            SELECT COUNT(DISTINCT user_id)
            FROM questionnaire_answers
                JOIN questionnaire_questions USING (question_id)
            WHERE questionnaire_id = :questionnaire_id
        ");
        $statement->execute([
            'questionnaire_id' => $this->getId()
        ]);
        $answers_total = $statement->fetch(PDO::FETCH_COLUMN, 0);

        return $answers_total;
    }

    public function isAnswered($user_id = null)
    {
        $user_id || $user_id = $GLOBALS['user']->id;
        if (!$user_id || ($user_id === "nobody")) {
            return false;
        }
        $statement = DBManager::get()->prepare("
            SELECT 1
            FROM questionnaire_answers
                INNER JOIN questionnaire_questions ON (questionnaire_answers.question_id = questionnaire_questions.question_id)
            WHERE user_id = :user_id
                AND questionnaire_id = :questionnaire_id
            UNION SELECT 1
            FROM questionnaire_anonymous_answers
            WHERE user_id = :user_id
                AND questionnaire_id = :questionnaire_id
        ");
        $statement->execute([
            'user_id' => $user_id,
            'questionnaire_id' => $this->getId()
        ]);
        return (bool) $statement->fetch(PDO::FETCH_COLUMN, 0);
    }

    public function latestAnswerTimestamp()
    {
        $statement = DBManager::get()->prepare("
            SELECT questionnaire_answers.chdate
            FROM questionnaire_answers
                INNER JOIN questionnaire_questions ON (questionnaire_answers.question_id = questionnaire_questions.question_id)
            WHERE questionnaire_questions.questionnaire_id = ?
            ORDER BY questionnaire_answers.chdate DESC
            LIMIT 1
        ");
        $statement->execute([$this->getId()]);
        return $statement->fetch(PDO::FETCH_COLUMN, 0);
    }

    public function isViewable()
    {
        if ($this->isEditable()) {
            return true;
        }
        if (!$this->isStarted()) {
            return false;
        }
        foreach ($this->assignments as $assignment) {
            if ($assignment['range_id'] === "public") {
                return true;
            } elseif (in_array($assignment['range_type'], ["static", "user", "institute"]) && $GLOBALS['perm']->have_perm("user")) {
                return true;
            } elseif ($assignment['range_type'] === "statusgruppe") {

                $statusgruppe_user = StatusgruppeUser::findOneBySQL(
                    "statusgruppe_id = ? AND user_id = ?",
                    [$assignment['range_id'], $GLOBALS['user']->id]);
                if ($statusgruppe_user) {
                    return true;
                }
            } elseif($GLOBALS['perm']->have_studip_perm("user", $assignment['range_id'])) {
                return true;
            } else {
                //now look through all plugin if this assignment is related to plugin contents:
                foreach (PluginManager::getInstance()->getPlugins(QuestionnaireAssignmentPlugin::class) as $plugin) {
                    if ($plugin->isQuestionnaireViewable($assignment)) {
                        return true;
                    }
                }
            }
        }

        if ($this->eval_assignment) {
            return User::findCurrent()
                ->hasPermissionLevel('autor', Course::find($this->eval_assignment->course_id));
        }

        return false;
    }

    public function isAnswerable()
    {
        if (!$this->isViewable() || !$this->isRunning()) {
            return false;
        }
        if ($this['anonymous'] && $this->isAnswered()) {
            return false;
        }
        if ($this->isEditable()) {
            return true;
        }
        $this->answerable = true;
        NotificationCenter::postNotification("QuestionnaireWillAllowToAnswer", $this);
        return $this->answerable;
    }

    public function isEditable()
    {
        if($this->is_template) {
            if(EvaluationHelper::isPermittedEvaluationAccess()) {
                return !QuestionnaireEvalAssignment::countBySQL("`template_id` = ?", [$this->id]);
            }
            return false;
        }

        if ($this->isNew() || ($this['user_id'] === $GLOBALS['user']->id) || $GLOBALS['perm']->have_perm("root")) {
            return true;
        } else {
            foreach ($this->assignments as $assignment) {
                if ($assignment['range_type'] === "institute" && $GLOBALS['perm']->have_studip_perm("tutor", $assignment['range_id'])) {
                    return true;
                } elseif ($assignment['range_type'] === "statusgruppe") {
                    $statusgruppe = Statusgruppen::find($assignment['range_id']);
                    if ($statusgruppe && $GLOBALS['perm']->have_studip_perm("tutor", $statusgruppe['range_id'])) {
                        return true;
                    }
                } elseif($assignment['range_type'] === "course" && $GLOBALS['perm']->have_studip_perm("tutor", $assignment['range_id'])) {
                    return true;
                } else {
                    //now look through all plugin if this assignment is related to plugin contents:
                    foreach (PluginManager::getInstance()->getPlugins(QuestionnaireAssignmentPlugin::class) as $plugin) {
                        if ($plugin->isQuestionnaireEditable($assignment)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public function isCopyable()
    {
        if ($this->is_template) {
            return EvaluationHelper::isPermittedEvaluationAccess();
        }
        return ($this->copyable && $GLOBALS['perm']->have_perm('autor') && $this->isViewable()) || $this->isEditable();
    }

    public function start()
    {
        if (!$this->isRunning()) {
            $this['startdate'] = time();
            $this['visible'] = 1;
            if ($this->isStopped()) {
                $this['stopdate'] = null;
            }
            $this->store();
            foreach ($this->questions as $question) {
                $question->onBeginning();
            }
        }
    }

    public function stop()
    {
        if (!$this->isStopped()) {
            $this['visible'] = $this['resultvisibility'] === 'never' ? 0 : 1;
            $this['stopdate'] = time();
            $this->store();
            foreach ($this->questions as $question) {
                $question->onEnding();
            }
        }
    }

    public function isStarted()
    {
        if ($this->eval_assignment) {
            return $this->eval_assignment->startdate && $this->eval_assignment->startdate <= time();
        } else {
            return $this['startdate'] && ($this['startdate'] <= time());
        }
    }

    public function isStopped()
    {
        if ($this->eval_assignment) {
            return $this->eval_assignment->stopdate && $this->eval_assignment->stopdate <= time();
        } else {
            return $this['stopdate'] && ($this['stopdate'] <= time());
        }
    }

    public function isRunning()
    {
        return $this->isStarted() && !$this->isStopped();
    }

    public function resultsVisible()
    {
        if (!$this->isViewable()) {
            return false;
        }

        if ($this->eval_assignment) {
            $user = User::findCurrent();
            if ($user->hasPermissionLevel('root') || $user->hasRole('Zentraler Evaluationsadmin')) {
                return true;
            }

            if (!$this->result_visible_for) {
                return false;
            }
            $eval_visible = $user->hasPermissionLevel($this->result_visible_for, Context::get());

            if ($this->anonymous) {
                $statement = DBManager::get()->prepare(
                    "SELECT DISTINCT count(`user_id`) AS 'amount' FROM `questionnaire_anonymous_answers`
                    WHERE `questionnaire_id` = :questionnaire_id");
            } else {
                $statement = DBManager::get()->prepare(
                    "SELECT DISTINCT count(`user_id`) AS 'amount' FROM `questionnaire_answers`
                    INNER JOIN `questionnaire_questions`
                        ON `questionnaire_questions`.`question_id` = `questionnaire_answers`.`question_id`
                    WHERE `questionnaire_id` = :questionnaire_id");
            }
            $statement->execute([
                'questionnaire_id' => $this->getId()
            ]);
            $response_amount = $statement->fetch()['amount'];

            $eval_visible = $eval_visible && ($response_amount >= $this->minimum_responses);
            return $eval_visible
                && ($this->resultvisibility === 'afterending' && $this->isStopped()
                || $this->resultvisibility === 'afterparticipation' && $this->isAnswered());
        }

        return $this['resultvisibility'] === 'always'
            || $this->isEditable()
            || ($this['resultvisibility'] === 'afterending' && $this->isStopped())
            || ($this['resultvisibility'] === 'afterparticipation' && $this->isAnswered());
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $sorm = self::findBySQL("user_id = ?", [$storage->user_id]);
        if ($sorm) {
            $field_data = [];
            foreach ($sorm as $row) {
                $field_data[] = $row->toRawArray();
            }
            if ($field_data) {
                $storage->addTabularData(_('Fragebögen'), 'questionnaires', $field_data);
            }
        }
    }

    /**
     * Returns all data as an array that could be stored as JSON.
     * @return array
     */
    public function exportAsFile()
    {
        $data = [
            'questionnaire' => [
                'title' => $this['title'],
                'anonymous' => $this['anonymous'],
                'resultvisibility' => $this['resultvisibility'],
                'editanswers' => $this['editanswers']
            ],
            'questions_data' => []
        ];
        foreach ($this->questions as $question) {
            $data['questions_data'][] = [
                'questiontype' => $question['questiontype'],
                'internal_name' => $question['internal_name'],
                'questiondata' => $question['questiondata']->getArrayCopy()
            ];
        }
        return $data;
    }
}