aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Questionnaire.php
blob: 7483483df59b35c4963af70daf30dedee260b4fd (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
<?php

class Questionnaire extends SimpleORMap implements PrivacyObject
{

    public $answerable;

    protected static function configure($config = [])
    {
        $config['db_table'] = 'questionnaires';
        $config['has_many']['questions'] = [
            'class_name' => 'QuestionnaireQuestion',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_many']['assignments'] = [
            'class_name' => 'QuestionnaireAssignment',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_many']['anonymousanswers'] = [
            'class_name' => 'QuestionnaireAnonymousAnswer',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        parent::configure($config);
    }

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

        return count($this->questions) ? $answers_total / count($this->questions) : 0;
    }

    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") as $plugin) {
                    if ($plugin->isQuestionnaireViewable($assignment)) {
                        return true;
                    }
                }
            }
        }
        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->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") as $plugin) {
                        if ($plugin->isQuestionnaireEditable($assignment)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public function isCopyable()
    {
        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()
    {
        return $this['startdate'] && ($this['startdate'] <= time());
    }

    public function isStopped()
    {
        return $this['stopdate'] && ($this['stopdate'] <= time());
    }

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

    public function resultsVisible()
    {
        if (!$this->isViewable()) {
            return false;
        }
        return $this['resultvisibility'] === "always"
            || $this->isEditable()
            || ($this['resultvisibility'] === "afterending" && $this->isStopped());
    }

    /**
     * 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);
            }
        }
    }
}