blob: 93c3279b63f38ec2d29f60c2af42bc00530b4ea6 (
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
|
<?php
use eTask\Task;
class QuestionnaireQuestion extends SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'questionnaire_questions';
$config['belongs_to']['questionnaire'] = [
'class_name' => Questionnaire::class,
'foreign_key' => 'questionnaire_id'
];
$config['has_many']['answers'] = [
'class_name' => QuestionnaireAnswer::class,
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['belongs_to']['etask'] = [
'class_name' => \eTask\Task::class,
'foreign_key' => 'etask_task_id'
];
parent::configure($config);
}
public static function findByQuestionnaire_id($questionnaire_id)
{
$statement = DBManager::get()->prepare("
SELECT *
FROM questionnaire_questions
WHERE questionnaire_id = ?
ORDER BY position ASC
");
$statement->execute([$questionnaire_id]);
$data = $statement->fetchAll();
$questions = [];
foreach ($data as $questionnaire_data) {
if (!$task = Task::find($questionnaire_data['etask_task_id'])) {
continue;
}
$class = $task->type;
if ($class === 'multiple-choice') {
$totalScore = array_reduce(
isset($task->task['answers']) ? $task->task['answers']->getArrayCopy() : [],
function ($totalScore, $answer) {
return $totalScore + intval($answer['score'] ?: 0);
},
0
);
$class = $totalScore === 0 ? 'Vote' : 'Test';
}
if (class_exists(ucfirst($class))) {
$questions[] = $class::buildExisting($questionnaire_data);
}
}
return $questions;
}
public function getMyAnswer($user_id = null)
{
$user_id || $user_id = $GLOBALS['user']->id;
if (!$user_id || $user_id === "nobody") {
$answer = new QuestionnaireAnswer();
$answer['user_id'] = $user_id;
$answer['question_id'] = $this->getId();
return $answer;
}
$statement = DBManager::get()->prepare("
SELECT *
FROM questionnaire_answers
WHERE question_id = :question_id
AND user_id = :me
");
$statement->execute([
'question_id' => $this->getId(),
'me' => $user_id
]);
$data = $statement->fetch(PDO::FETCH_ASSOC);
if ($data) {
return QuestionnaireAnswer::buildExisting($data);
} else {
$answer = new QuestionnaireAnswer();
$answer['user_id'] = $user_id;
$answer['question_id'] = $this->getId();
return $answer;
}
}
public function onBeginning()
{
}
public function onEnding()
{
}
}
|