ILIAS  release_7 Revision v7.30-3-g800a261c036
class.SurveyMatrixQuestionEvaluation.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
12 //
13 // RESULTS
14 //
15
16 public function getResults()
17 {
18 $results = array();
19
20 $answers = $this->getAnswerData();
21
22 // parse rows
23 for ($r = 0; $r < $this->question->getRowCount(); $r++) {
24 $row_results = new ilSurveyEvaluationResults($this->question);
25
26 $this->parseResults(
27 $row_results,
28 (array) $answers[$r],
29 $this->question->getColumns()
30 );
31
32 $results[] = array(
33 $this->question->getRow($r)->title,
34 $row_results
35 );
36 }
37
38 return $results;
39 }
40
41
42 //
43 // DETAILS
44 //
45
46
47 public function getGrid($a_results, $a_abs = true, $a_perc = true)
48 {
50
51 $res = array(
52 "cols" => array(),
53 "rows" => array()
54 );
55
56 $tmp = $a_results;
57 $tmp = array_shift($tmp);
58 $vars = $tmp[1]->getVariables();
59 if ($vars) {
60 foreach ($vars as $var) {
61 $res["cols"][] = $var->cat->title;
62 }
63 }
64 $q_counter = 0;
65 foreach ($a_results as $results_row) {
66 #20363
67 $parsed_row = array(
68 ++$q_counter . ". " . $results_row[0]
69 );
70
71 $vars = $results_row[1]->getVariables();
72 if ($vars) {
73 foreach ($vars as $var) {
74 $perc = $var->perc
75 ? sprintf("%.2f", $var->perc * 100) . "%"
76 : "0%";
77
78 if ((bool) $a_abs && (bool) $a_perc) {
79 $parsed_row[] = $var->abs . " / " . $perc;
80 } elseif ((bool) $a_abs) {
81 $parsed_row[] = $var->abs;
82 } else {
83 $parsed_row[] = $perc;
84 }
85 }
86 }
87
88 $res["rows"][] = $parsed_row;
89 }
90 return $res;
91 }
92
93 public function getTextAnswers($a_results)
94 {
95 $res = array();
96
97 foreach ($a_results as $results_row) {
98 $texts = $results_row[1]->getMappedTextAnswers();
99 if ($texts) {
100 $idx = $results_row[0];
101 foreach ($texts as $answers) {
102 foreach ($answers as $answer) {
103 $res[$idx][] = $answer;
104 }
105 }
106 }
107 }
108
109 return $res;
110 }
111
112 public function getChart($a_results)
113 {
115
116 $chart = ilChart::getInstanceByType(ilChart::TYPE_GRID, $a_results[0][1]->getQuestion()->getId());
117 $chart->setXAxisToInteger(true);
118 $chart->setStacked(true);
119
120 $colors = $this->getChartColors();
121 $chart->setColors($colors);
122
123 // :TODO:
124 //$chart->setsize($this->chart_width, $this->chart_height);
125
126 $data = $labels = $legend = array();
127
128 $row_idx = sizeof($a_results);
129
130 $row_counter = 0;
131 $text_shortened = false;
132 foreach ($a_results as $row) {
133 $row_idx--;
134
135 $row_title = $row[0];
136 $row_results = $row[1];
137
138 #20363
139 $row_title = ++$row_counter . ". " . $row_title;
140 $labels[$row_idx] = ilUtil::shortenText($row_title, 50, true);
141 if ($labels[$row_idx] != $row_title) {
142 $text_shortened = true;
143 }
144 //$labels[$row_idx] = wordwrap(ilUtil::shortenText($row_title, 50, true), 30, "<br />");
145
146 $vars = $row_results->getVariables();
147 if ($vars) {
148 foreach ($vars as $idx => $var) {
149 if (!array_key_exists($idx, $data)) {
150 $data[$idx] = $chart->getDataInstance(ilChartGrid::DATA_BARS);
151 $data[$idx]->setLabel($var->cat->title);
152 $data[$idx]->setBarOptions(0.5, "center", true);
153 $data[$idx]->setFill(1);
154
155 $legend[] = array(
156 $var->cat->title,
157 $colors[$idx]
158 );
159 }
160
161 $data[$idx]->addPoint($var->abs, $row_idx);
162 }
163 }
164 }
165
166 //Chart height depending on the number of questions. Not fixed anymore.
167 $this->chart_height = count($a_results) * 40;
168 //Chart width 500px if one or + question string are longer than 60 char. Otherwise the default width still aplied.
169 if ($text_shortened) {
170 $this->chart_width = 500;
171 }
172 $chart->setSize($this->chart_width, $this->chart_height);
173
174 foreach ($data as $var) {
175 $chart->addData($var);
176 }
177
178 $chart->setTicks(false, $labels, true);
179
180 return array(
181 $chart->getHTML(),
182 $legend
183 );
184 }
185
186
187
188 //
189 // EXPORT
190 //
191
192 public function exportResults($a_results, $a_do_title, $a_do_label)
193 {
194 $question = $a_results[0][1]->getQuestion();
195
196 $rows = array();
197 $row = array();
198
199 if ($a_do_title) {
200 $row[] = $question->getTitle();
201 }
202 if ($a_do_label) {
203 $row[] = $question->label;
204 }
205
206 $row[] = $question->getQuestiontext();
207 $row[] = SurveyQuestion::_getQuestionTypeName($question->getQuestionType());
208
209 $row[] = (int) $a_results[0][1]->getUsersAnswered();
210 $row[] = (int) $a_results[0][1]->getUsersSkipped();
211 $row[] = null;
212 $row[] = null;
213 $row[] = null;
214 $row[] = null;
215 $row[] = null;
216
217 $rows[] = $row;
218
219 foreach ($a_results as $row_result) {
220 $row_title = $row_result[0];
221 $row_res = $row_result[1];
222
223 $row = array();
224
225 if ($a_do_title) {
226 $row[] = null;
227 }
228 if ($a_do_label) {
229 $row[] = null;
230 }
231
232 $row[] = $row_title;
233 $row[] = null;
234
235 $row[] = null;
236 $row[] = null;
237
238 // :TODO:
239 $row[] = is_array($row_res->getModeValue())
240 ? implode(", ", $row_res->getModeValue())
241 : $row_res->getModeValue();
242
243 $row[] = $row_res->getModeValueAsText();
244 $row[] = (int) $row_res->getModeNrOfSelections();
245
246 // :TODO:
247 $row[] = $row_res->getMedianAsText();
248
249 $row[] = $row_res->getMean();
250
251 $rows[] = $row;
252 }
253 return $rows;
254 }
255
256 public function getUserSpecificVariableTitles(array &$a_title_row, array &$a_title_row2, $a_do_title, $a_do_label)
257 {
259
260 for ($i = 0; $i < $this->question->getRowCount(); $i++) {
261 // create row title according label, add 'other column'
262 $row = $this->question->getRow($i);
263
264 if ($a_do_title && $a_do_label) {
265 $a_title_row[] = $row->title;
266 $a_title_row2[] = $row->label;
267
268 if ($this->question->getSubtype() == 0) {
269 $a_title_row[] = $row->title; // see #20646
270 $a_title_row2[] = $row->label; // see #20646
271 }
272
273 if ($row->other) {
274 $a_title_row[] = $row->title;
275 $a_title_row2[] = $lng->txt('other');
276 }
277 } elseif ($a_do_title) {
278 $a_title_row[] = $row->title;
279 $a_title_row2[] = "";
280
281 if ($this->question->getSubtype() == 0) {
282 $a_title_row[] = $row->title; // see #20646
283 $a_title_row2[] = ""; // see #20646
284 }
285
286 if ($row->other) {
287 $a_title_row[] = $row->title;
288 $a_title_row2[] = $lng->txt('other');
289 }
290 } else {
291 $a_title_row[] = $row->label;
292 $a_title_row2[] = "";
293
294 if ($this->question->getSubtype() == 0) {
295 $a_title_row[] = $row->label; // see #20646
296 $a_title_row2[] = ""; // see #20646
297 }
298
299 if ($row->other) {
300 $a_title_row[] = $row->label;
301 $a_title_row2[] = $lng->txt('other');
302 }
303 }
304
305 // mc
306 if ($this->question->getSubtype() == 1) {
307 for ($index = 0; $index < $this->question->getColumnCount(); $index++) {
308 $col = $this->question->getColumn($index);
309
310 $a_title_row[] = $col->title . " [" . $col->scale . "]";
311 $a_title_row2[] = "";
312 }
313 }
314 }
315 }
316
317 public function addUserSpecificResults(array &$a_row, $a_user_id, $a_results)
318 {
319 $answer_map = array();
320 foreach ($a_results as $row_results) {
321 $row_title = $row_results[0];
322 $row_result = $row_results[1];
323
324 $answers = $row_result->getUserResults($a_user_id);
325 if ($answers !== null) {
326 foreach ($answers as $answer) {
327 // mc
328 if ($this->question->getSubtype() == 1) {
329 $answer_map[$row_title . "|" . $answer[2]] = $answer[2];
330 } else {
331 $answer_map[$row_title] = $answer[3];
332 $answer_map[$row_title . "|scale"] = $answer[2]; // see #20646
333 }
334 if ($answer[1]) {
335 $answer_map[$row_title . "|txt"] = $answer[1];
336 }
337 }
338 }
339 }
340
341 if (!sizeof($answer_map)) {
342 $a_row[] = $this->getSkippedValue();
343 } else {
344 $a_row[] = "";
345 }
346
347 for ($i = 0; $i < $this->question->getRowCount(); $i++) {
348 $row = $this->question->getRow($i);
349 $row_title = $row->title;
350
351 $a_row[] = $answer_map[$row_title];
352 if ($this->question->getSubtype() == 0) {
353 $a_row[] = $answer_map[$row_title . "|scale"]; // see #20646
354 }
355
356 if ($row->other) {
357 $a_row[] = $answer_map[$row_title . "|txt"];
358 }
359
360 // mc
361 if ($this->question->getSubtype() == 1) {
362 for ($index = 0; $index < $this->question->getColumnCount(); $index++) {
363 $col = $this->question->getColumn($index);
364 $a_row[] = $answer_map[$row_title . "|" . $col->scale];
365 }
366 }
367 }
368 }
369
373 protected function supportsSumScore() : bool
374 {
375 return true;
376 }
377
381 protected function isSumScoreValid(int $nr_answer_records) : bool
382 {
383 if ($nr_answer_records == $this->question->getRowCount()) {
384 return true;
385 }
386 return false;
387 }
388}
An exception for terminatinating execution or to throw for unit testing.
isSumScoreValid(int $nr_answer_records)
@inheritDoc
getGrid($a_results, $a_abs=true, $a_perc=true)
Get grid data.
exportResults($a_results, $a_do_title, $a_do_label)
addUserSpecificResults(array &$a_row, $a_user_id, $a_results)
getUserSpecificVariableTitles(array &$a_title_row, array &$a_title_row2, $a_do_title, $a_do_label)
Get title columns for user-specific export.
getSkippedValue()
Get caption for skipped value.
parseResults(ilSurveyEvaluationResults $a_results, array $a_answers, SurveyCategories $a_categories=null)
Parse answer data into results instance.
static _getQuestionTypeName($type_tag)
Return the translation for a given question type tag.
static getInstanceByType($a_type, $a_id)
Get type instance.
const TYPE_GRID
static shortenText( $a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
$index
Definition: metadata.php:128
$i
Definition: metadata.php:24
$results
foreach($_POST as $key=> $value) $res
$data
Definition: storeScorm.php:23
$rows
Definition: xhr_table.php:10