1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Survey evaluation answers
6 *
7 * @author	Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8 * @ingroup ModulesSurvey
9 */
10class ilSurveyEvaluationResults
11{
12    /**
13     * @var ilLanguage
14     */
15    protected $lng;
16
17    protected $question; // [SurveyQuestion]
18    protected $users_answered; // [int]
19    protected $users_skipped; // [int]
20    protected $mode_value; // [int]
21    protected $mode_nr_of_selections; // [int]
22    protected $arithmetic_mean; // [float]
23    protected $median; // [string|float]
24    protected $variables = array(); // [array]
25    protected $answers = array(); // [array]
26
27    public function __construct(SurveyQuestion $a_question)
28    {
29        global $DIC;
30
31        $this->lng = $DIC->language();
32        $this->question = $a_question;
33    }
34
35    public function getQuestion()
36    {
37        return $this->question;
38    }
39
40    public function setUsersAnswered($a_value)
41    {
42        $this->users_answered = (int) $a_value;
43    }
44
45    public function getUsersAnswered()
46    {
47        return $this->users_answered;
48    }
49
50    public function setUsersSkipped($a_value)
51    {
52        $this->users_skipped = (int) $a_value;
53    }
54
55    public function getUsersSkipped()
56    {
57        return $this->users_skipped;
58    }
59
60    public function setMode($a_value, $a_nr_of_selections)
61    {
62        $this->mode_value = is_array($a_value)
63            ? $a_value
64            : trim($a_value);
65        $this->mode_nr_of_selections = (int) $a_nr_of_selections;
66    }
67
68    public function getModeValue()
69    {
70        return $this->mode_value;
71    }
72
73    public function getModeValueAsText()
74    {
75        if ($this->mode_value === null) {
76            return;
77        }
78
79        $res = array();
80
81        $mvalues = $this->mode_value;
82        if (!is_array($mvalues)) {
83            $mvalues = array($mvalues);
84        }
85        sort($mvalues, SORT_NUMERIC);
86        foreach ($mvalues as $value) {
87            $res[] = $this->getScaleText($value);
88        }
89
90        return implode(", ", $res);
91    }
92
93    public function getModeNrOfSelections()
94    {
95        return $this->mode_nr_of_selections;
96    }
97
98    public function setMean($a_mean)
99    {
100        $this->arithmetic_mean = (float) $a_mean;
101    }
102
103    public function getMean()
104    {
105        return $this->arithmetic_mean;
106    }
107
108    public function setMedian($a_value)
109    {
110        $this->median = is_array($a_value)
111            ? $a_value
112            : trim($a_value);
113    }
114
115    public function getMedian()
116    {
117        return $this->median;
118    }
119
120    public function getMedianAsText()
121    {
122        $lng = $this->lng;
123
124        if ($this->median === null) {
125            return;
126        }
127
128        if (!is_array($this->median)) {
129            return $this->getScaleText($this->median);
130        } else {
131            return $lng->txt("median_between") . " " .
132                $this->getScaleText($this->median[0]) . " " .
133                $lng->txt("and") . " " .
134                $this->getScaleText($this->median[1]);
135        }
136    }
137
138    public function addVariable(ilSurveyEvaluationResultsVariable $a_variable)
139    {
140        $this->variables[] = $a_variable;
141    }
142
143    public function getVariables()
144    {
145        if (sizeof($this->variables)) {
146            return $this->variables;
147        }
148    }
149
150    public function addAnswer(ilSurveyEvaluationResultsAnswer $a_answer)
151    {
152        $this->answers[] = $a_answer;
153    }
154
155    public function getAnswers()
156    {
157        if (sizeof($this->answers)) {
158            return $this->answers;
159        }
160        return [];
161    }
162
163    protected function getScaleText($a_value)
164    {
165        if (!sizeof($this->variables)) {
166            return $a_value;
167        } else {
168            foreach ($this->variables as $var) {
169                if ($var->cat->scale == $a_value) {
170                    return $var->cat->title . " [" . $a_value . "]";
171                }
172            }
173        }
174    }
175
176    protected function getCatTitle($a_value)
177    {
178        if (!sizeof($this->variables)) {
179            return $a_value;
180        } else {
181            foreach ($this->variables as $var) {
182                if ($var->cat->scale == $a_value) {
183                    return $var->cat->title;
184                }
185            }
186        }
187    }
188
189    public function getMappedTextAnswers()
190    {
191        $res = array();
192
193        foreach ($this->answers as $answer) {
194            if ($answer->text) {
195                $res[$this->getScaleText($answer->value)][] = $answer->text;
196            }
197        }
198
199        return $res;
200    }
201
202    public function getUserResults($a_active_id)
203    {
204        $res = array();
205
206        $answers = $this->getAnswers();
207        if ($answers) {
208            foreach ($answers as $answer) {
209                if ($answer->active_id == $a_active_id) {
210                    $res[] = array(
211                        $this->getScaleText($answer->value),
212                        $answer->text,
213                        $answer->value,
214                        $this->getCatTitle($answer->value)
215                    );
216                }
217            }
218        }
219
220        return $res;
221    }
222}
223
224class ilSurveyEvaluationResultsVariable
225{
226    public $cat; // [SurveyCategory]
227    public $abs; // [int]
228    public $perc; // [float]
229
230    public function __construct(ilSurveyCategory $a_cat, $a_abs, $a_perc)
231    {
232        $this->cat = $a_cat;
233        $this->abs = (int) $a_abs;
234        $this->perc = (float) $a_perc;
235    }
236}
237
238class ilSurveyEvaluationResultsAnswer
239{
240    public $active_id; // [int]
241    public $value; // [int|float]
242    public $text; // [string]
243
244    public function __construct($a_active_id, $a_value, $a_text)
245    {
246        $this->active_id = (int) $a_active_id;
247        $this->value = $a_value;
248        $this->text = trim($a_text);
249    }
250}
251