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