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