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