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