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
|
<?php
/**
*
* @property string $id alias column for questionnaire_id
* @property string $questionnaire_id database column
* @property string $title database column
* @property string|null $description database column
* @property string $user_id 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 int $editanswers database column
* @property int $copyable 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 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'
];
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") 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())
|| ($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;
}
}
|