aboutsummaryrefslogtreecommitdiff
path: root/lib/models/QuestionnaireQuestion.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /lib/models/QuestionnaireQuestion.php
current code from svn, revision 62608
Diffstat (limited to 'lib/models/QuestionnaireQuestion.php')
-rw-r--r--lib/models/QuestionnaireQuestion.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/models/QuestionnaireQuestion.php b/lib/models/QuestionnaireQuestion.php
new file mode 100644
index 0000000..5cb06eb
--- /dev/null
+++ b/lib/models/QuestionnaireQuestion.php
@@ -0,0 +1,100 @@
+<?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',
+ 'foreign_key' => 'questionnaire_id'
+ ];
+ $config['has_many']['answers'] = [
+ 'class_name' => 'QuestionnaireAnswer',
+ 'on_delete' => 'delete',
+ 'on_store' => 'store'
+ ];
+ $config['belongs_to']['etask'] = [
+ 'class_name' => '\\eTask\\Task',
+ '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()
+ {
+ }
+}