aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Freetext.php
blob: 395836a5c9dd54110dc5d5c8be7837678234a5ff (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
<?php

require_once 'lib/classes/QuestionType.php';

/**
 * @license GPL2 or any later version
 *
 * @property string $id alias column for question_id
 * @property string $question_id database column
 * @property string $questionnaire_id database column
 * @property string $questiontype database column
 * @property string|null $internal_name database column
 * @property JSONArrayObject $questiondata database column
 * @property int $position database column
 * @property int $chdate database column
 * @property int $mkdate database column
 * @property SimpleORMapCollection<QuestionnaireAnswer> $answers has_many QuestionnaireAnswer
 * @property Questionnaire $questionnaire belongs_to Questionnaire
 */
class Freetext extends QuestionnaireQuestion implements QuestionType
{
    /**
     * Returns the Icon-object to this QuestionType.
     * @param bool $active: true if Icon should be clickable, false for black info-icon.
     * @return Icon : guestbook-icon.
     */
    public static function getIcon(bool $active = false) : Icon
    {
        return Icon::create(
            static::getIconShape(),
            $active ? Icon::ROLE_CLICKABLE : Icon::ROLE_INFO
        );
    }

    /**
     * Returns the shape of the icon of this QuestionType
     * @return string
     */
    public static function getIconShape()
    {
        return 'question-text';
    }

    /**
     * Returns the name of this QuestionType "Freitextfrage".
     * @return string
     */
    public static function getName()
    {
        return _('Freitextfrage');
    }

    static public function getEditingComponent()
    {
        return ['FreetextEdit', ''];
    }

    public function beforeStoringQuestiondata($questiondata)
    {
        $questiondata['description'] = \Studip\Markup::markAsHtml(
            \Studip\Markup::purifyHtml($questiondata['description'])
        );
        return $questiondata;
    }

    /**
     * Returns the template of this question to answer the question.
     *
     * @return Flexi\Template
     * @throws Flexi\TemplateNotFoundException if there is no template.
     */
    public function getDisplayTemplate()
    {
        $factory = new Flexi\Factory(realpath(__DIR__ . '/../../app/views'));
        $template = $factory->open('questionnaire/question_types/freetext/freetext_answer.php');
        $template->vote = $this;
        return $template;
    }

    /**
     * Creates an answer by the parameters of the request. Called when a user clicked to answer
     * the questionnaire.
     * @return QuestionnaireAnswer
     */
    public function createAnswer()
    {
        $answer = $this->getMyAnswer();
        $answers = Request::getArray('answers');
        $userAnswerText = $answers[$this->getId()]['answerdata']['text'];
        $answer->setData(['answerData' => ['text' => $userAnswerText]]);
        return $answer;
    }

    public function getUserIdsOfFilteredAnswer($answer_option)
    {
        return [];
    }

    /**
     * Returns the template with the answers of the question so far.
     *
     * @param null $only_user_ids : array of user_ids
     *
     * @return Flexi\Template
     * @throws Flexi\TemplateNotFoundException if there is no template.
     */
    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\Factory(realpath(__DIR__ . '/../../app/views'));
        $template = $factory->open('questionnaire/question_types/freetext/freetext_evaluation.php');
        $template->vote = $this;
        $template->set_attribute('answers', $answers);
        return $template;
    }

    /**
     * Returns an array of the answers to be put into a CSV-file.
     * @return array
     */
    public function getResultArray()
    {
        $output = [];
        $countNobodys = 0;

        $question = trim(strip_tags($this->questiondata['description']));
        foreach ($this->answers as $answer) {
            if ($answer['user_id'] && $answer['user_id'] != 'nobody') {
                $userId = $answer['user_id'];
            } else {
                $countNobodys++;
                $userId = _('unbekannt').' '.$countNobodys;
            }

            $output[$question][$userId] = $answer['answerdata']['text'];
        }

        return $output;
    }

    /**
     * Called after the questionnaire gets closed. Does nothing for this QuestionType Freetext.
     */
    public function onEnding()
    {
        //Nothing to do here.
    }
}