aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Vote.php
blob: 6060f0beb24e99b4a2d944a6be04f587f56e62a3 (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
<?php
require_once 'lib/classes/QuestionType.interface.php';

use eTask\Task;

class Vote extends QuestionnaireQuestion implements QuestionType
{
    public static function getIcon($active = false, $add = false)
    {
        return Icon::create('vote', $active ? 'clickable' : 'info');
    }

    public static function getName()
    {
        return _('Auswahlfrage');
    }

    public function getEditingTemplate()
    {
        $tf = new Flexi_TemplateFactory(realpath(__DIR__.'/../../app/views'));
        $template = $tf->open('questionnaire/question_types/vote/vote_edit');
        $template->set_attribute('vote', $this);
        return $template;
    }

    public function createDataFromRequest()
    {
        $questions = Request::getArray('questions');
        $data = $questions[$this->getId()];

        // create a new eTask if this is a new question
        if (!$this->etask) {
            $this->etask = Task::create(
                [
                    'type' => 'multiple-choice',
                    'user_id' => $GLOBALS['user']->id,
                ]
            );
        }

        // update description
        $this->etask->description = Studip\Markup::purifyHtml($data['description']);

        // update task's type (single|multiple)
        $task = [
            'type' => $data['task']['type'] === 'multiple' ? 'multiple' : 'single',
            'answers' => []
        ];

        // update task's answers
        foreach ($data['task']['answers'] as $index => $text) {
            $trimmedText = trim($text);
            if ($trimmedText === '') {
                continue;
            }

            $task['answers'][] = [
                'text' => $trimmedText,
                'score' => 0,
                'feedback' => ''
            ];
        }

        $this->etask->task = $task;

        // update randomize option
        if (isset($data['options']['randomize'])) {
            $options = $this->etask->options;
            $options['randomize'] = (bool) $data['options']['randomize'];
            $this->etask->options = $options;
        }
        // update mandatory option
        if (isset($data['options']['mandatory'])) {
            $options = $this->etask->options;
            $options['mandatory'] = (bool) $data['options']['mandatory'];
            $this->etask->options = $options;
        }

        // store the eTask instance
        $this->etask->store();
    }

    public function getDisplayTemplate()
    {
        $factory = new Flexi_TemplateFactory(realpath(__DIR__.'/../../app/views'));
        $template = $factory->open('questionnaire/question_types/vote/vote_answer');
        $template->set_attribute('vote', $this);
        return $template;
    }

    public function createAnswer()
    {
        $answer = $this->getMyAnswer();

        $answers = Request::getArray('answers');
        if (array_key_exists($this->getId(), $answers)) {
            $userAnswer = $answers[$this->getId()]['answerdata']['answers'];
            if (is_array($userAnswer)) {
                $userAnswer = array_map('intval', $userAnswer);
            }
            else {
                $userAnswer = (int) $userAnswer;
            }
        }
        $answer->setData(['answerData' => ['answers' => $userAnswer ] ]);
        return $answer;
    }

    public function getResultTemplate($only_user_ids = null)
    {
        $answers = $this->answers;
        if ($only_user_ids !== null) {
            foreach ($answers as $key => $answer) {
                if (!in_array($answer['user_id'], $only_user_ids)) {
                    unset($answers[$key]);
                }
            }
        }
        $factory = new Flexi_TemplateFactory(realpath(__DIR__.'/../../app/views'));
        $template = $factory->open('questionnaire/question_types/vote/vote_evaluation');
        $template->set_attribute('vote', $this);
        $template->set_attribute('answers', $answers);
        return $template;
    }

    public function getResultArray()
    {
        $output = [];

        $taskAnswers = $this->etask->task['answers'];

        foreach ($taskAnswers as $key => $option) {
            $answerOption = [];
            $countNobodys = 0;

            foreach ($this->answers as $answer) {
                $answerData = $answer['answerdata']->getArrayCopy();

                if ($answer['user_id'] && $answer['user_id'] != 'nobody') {
                    $userId = $answer['user_id'];
                } else {
                    $countNobodys++;
                    $userId = _('unbekannt').' '.$countNobodys;
                }

                if (in_array($key, (array) $answerData['answers'])) {
                    $answerOption[$userId] = 1;
                } else {
                    $answerOption[$userId] = 0;
                }
            }
            $output[$option['text']] = $answerOption;
        }
        return $output;
    }
}