ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveyMultipleChoiceQuestion.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
25 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
26 
39 {
46 
57  $title = "",
58  $description = "",
59  $author = "",
60  $questiontext = "",
61  $owner = -1,
62  $orientation = 0
63  )
64  {
66  $this->orientation = $orientation;
67  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
68  $this->categories = new SurveyCategories();
69  }
70 
79  {
80  global $ilDB;
81 
82  $result = $ilDB->queryF("SELECT svy_question.*, " . $this->getAdditionalTableName() . ".* FROM svy_question, " . $this->getAdditionalTableName() . " WHERE svy_question.question_id = %s AND svy_question.question_id = " . $this->getAdditionalTableName() . ".question_fi",
83  array('integer'),
84  array($id)
85  );
86  if ($result->numRows() == 1)
87  {
88  return $ilDB->fetchAssoc($result);
89  }
90  else
91  {
92  return array();
93  }
94  }
95 
102  function loadFromDb($id)
103  {
104  global $ilDB;
105 
106  $result = $ilDB->queryF("SELECT svy_question.*, " . $this->getAdditionalTableName() . ".* FROM svy_question LEFT JOIN " . $this->getAdditionalTableName() . " ON " . $this->getAdditionalTableName() . ".question_fi = svy_question.question_id WHERE svy_question.question_id = %s",
107  array('integer'),
108  array($id)
109  );
110  if ($result->numRows() == 1)
111  {
112  $data = $ilDB->fetchAssoc($result);
113  $this->setId($data["question_id"]);
114  $this->setTitle($data["title"]);
115  $this->label = $data['label'];
116  $this->setDescription($data["description"]);
117  $this->setObjId($data["obj_fi"]);
118  $this->setAuthor($data["author"]);
119  $this->setOwner($data["owner_fi"]);
120  include_once("./Services/RTE/classes/class.ilRTE.php");
121  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
122  $this->setObligatory($data["obligatory"]);
123  $this->setComplete($data["complete"]);
124  $this->setOriginalId($data["original_id"]);
125  $this->setOrientation($data["orientation"]);
126  $this->use_min_answers = ($data['use_min_answers']) ? true : false;
127  $this->nr_min_answers = $data['nr_min_answers'];
128  $this->nr_max_answers = $data['nr_max_answers'];
129 
130  $this->categories->flushCategories();
131  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title, svy_category.neutral FROM svy_variable, svy_category WHERE svy_variable.question_fi = %s AND svy_variable.category_fi = svy_category.category_id ORDER BY sequence ASC",
132  array('integer'),
133  array($id)
134  );
135  if ($result->numRows() > 0)
136  {
137  while ($data = $ilDB->fetchAssoc($result))
138  {
139  $this->categories->addCategory($data["title"], $data["other"], $data["neutral"], null, ($data['scale']) ? $data['scale'] : ($data['sequence'] + 1));
140  }
141  }
142  }
144  }
145 
152  function isComplete()
153  {
154  if (strlen($this->title) && strlen($this->author) && strlen($this->questiontext) && $this->categories->getCategoryCount())
155  {
156  return 1;
157  }
158  else
159  {
160  return 0;
161  }
162  }
163 
169  function saveToDb($original_id = "")
170  {
171  global $ilDB;
172 
173  $affectedRows = parent::saveToDb($original_id);
174  if ($affectedRows == 1)
175  {
176  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
177  array('integer'),
178  array($this->getId())
179  );
180  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, orientation, use_min_answers, nr_min_answers, nr_max_answers) VALUES (%s, %s, %s, %s, %s)",
181  array('integer', 'text', 'integer', 'integer', 'integer'),
182  array(
183  $this->getId(),
184  $this->getOrientation(),
185  ($this->use_min_answers) ? 1 : 0,
186  ($this->nr_min_answers > 0) ? $this->nr_min_answers : null,
187  ($this->nr_max_answers > 0) ? $this->nr_max_answers : null
188  )
189  );
190 
191  // saving material uris in the database
192  $this->saveMaterial();
193  $this->saveCategoriesToDb();
194  }
195  }
196 
198  {
199  global $ilDB;
200 
201  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_variable WHERE question_fi = %s",
202  array('integer'),
203  array($this->getId())
204  );
205 
206  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
207  {
208  $cat = $this->categories->getCategory($i);
209  $category_id = $this->saveCategoryToDb($cat->title, $cat->neutral);
210  $next_id = $ilDB->nextId('svy_variable');
211  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, other, sequence, scale, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
212  array('integer','integer','integer','float','integer','integer', 'integer','integer'),
213  array($next_id, $category_id, $this->getId(), ($i + 1), $cat->other, $i, ($cat->scale > 0) ? $cat->scale : null, time())
214  );
215  }
216  $this->saveCompletionStatus();
217  }
218 
225  function toXML($a_include_header = TRUE, $obligatory_state = "")
226  {
227  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
228  $a_xml_writer = new ilXmlWriter;
229  $a_xml_writer->xmlHeader();
230  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
231  $xml = $a_xml_writer->xmlDumpMem(FALSE);
232  if (!$a_include_header)
233  {
234  $pos = strpos($xml, "?>");
235  $xml = substr($xml, $pos + 2);
236  }
237  return $xml;
238  }
239 
248  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
249  {
250  $attrs = array(
251  "id" => $this->getId(),
252  "title" => $this->getTitle(),
253  "type" => $this->getQuestiontype(),
254  "obligatory" => $this->getObligatory()
255  );
256  $a_xml_writer->xmlStartTag("question", $attrs);
257 
258  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
259  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
260  if (strlen($this->label))
261  {
262  $attrs = array(
263  "label" => $this->label,
264  );
265  }
266  else
267  {
268  $attrs = array();
269  }
270  $a_xml_writer->xmlStartTag("questiontext", $attrs);
271  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
272  $a_xml_writer->xmlEndTag("questiontext");
273 
274  $a_xml_writer->xmlStartTag("responses");
275 
276  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
277  {
278  $attrs = array(
279  "id" => $i
280  );
281  if (strlen($this->categories->getCategory($i)->other)) $attrs['other'] = $this->categories->getCategory($i)->other;
282  if (strlen($this->categories->getCategory($i)->neutral)) $attrs['neutral'] = $this->categories->getCategory($i)->neutral;
283  if (strlen($this->categories->getCategory($i)->label)) $attrs['label'] = $this->categories->getCategory($i)->label;
284  if (strlen($this->categories->getCategory($i)->scale)) $attrs['scale'] = $this->categories->getCategory($i)->scale;
285  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
286  $this->addMaterialTag($a_xml_writer, $this->categories->getCategory($i)->title);
287  $a_xml_writer->xmlEndTag("response_multiple");
288  }
289 
290  $a_xml_writer->xmlEndTag("responses");
291 
292  if (count($this->material))
293  {
294  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
295  {
296  $attrs = array(
297  "label" => $this->material["title"]
298  );
299  $a_xml_writer->xmlStartTag("material", $attrs);
300  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
301  if (strcmp($matches[1], "") != 0)
302  {
303  $intlink = $this->material["internal_link"];
304  }
305  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
306  $a_xml_writer->xmlEndTag("material");
307  }
308  }
309 
310  $a_xml_writer->xmlStartTag("metadata");
311  $a_xml_writer->xmlStartTag("metadatafield");
312  $a_xml_writer->xmlElement("fieldlabel", NULL, "orientation");
313  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getOrientation());
314  $a_xml_writer->xmlEndTag("metadatafield");
315  $a_xml_writer->xmlStartTag("metadatafield");
316  $a_xml_writer->xmlElement("fieldlabel", NULL, "use_min_answers");
317  $a_xml_writer->xmlElement("fieldentry", NULL, $this->use_min_answers);
318  $a_xml_writer->xmlEndTag("metadatafield");
319  $a_xml_writer->xmlStartTag("metadatafield");
320  $a_xml_writer->xmlElement("fieldlabel", NULL, "nr_min_answers");
321  $a_xml_writer->xmlElement("fieldentry", NULL, $this->nr_min_answers);
322  $a_xml_writer->xmlEndTag("metadatafield");
323  $a_xml_writer->xmlStartTag("metadatafield");
324  $a_xml_writer->xmlElement("fieldlabel", NULL, "nr_max_answers");
325  $a_xml_writer->xmlElement("fieldentry", NULL, $this->nr_max_answers);
326  $a_xml_writer->xmlEndTag("metadatafield");
327  $a_xml_writer->xmlEndTag("metadata");
328 
329  $a_xml_writer->xmlEndTag("question");
330  }
331 
338  function getQuestionType()
339  {
340  return "SurveyMultipleChoiceQuestion";
341  }
342 
350  {
351  return "svy_qst_mc";
352  }
353 
360  function &getWorkingDataFromUserInput($post_data)
361  {
362  $entered_value = $post_data[$this->getId() . "_value"];
363  $data = array();
364  if (is_array($entered_value))
365  {
366  foreach ($entered_value as $idx => $value)
367  {
368  array_push($data, array("value" => $value, "textanswer" => $post_data[$this->getId() . '_' . $value . '_other']));
369  }
370  }
371  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
372  {
373  $cat = $this->categories->getCategory($i);
374  if ($cat->other)
375  {
376  if (!in_array($i, $entered_value))
377  {
378  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
379  {
380  array_push($data, array("value" => $i, "textanswer" => $post_data[$this->getId() . '_' . $i . '_other'], "uncheck" => true));
381  }
382  }
383  }
384  }
385  return $data;
386  }
387 
397  function checkUserInput($post_data, $survey_id)
398  {
399  $entered_value = $post_data[$this->getId() . "_value"];
400  if (!$this->getObligatory($survey_id) && count($entered_value) == 0) return "";
401 
402  if ($this->use_min_answers && $this->nr_min_answers > 0 && $this->nr_max_answers > 0 && $this->nr_min_answers == $this->nr_max_answers && count($entered_value) != $this->nr_max_answers)
403  {
404  return sprintf($this->lng->txt("err_no_exact_answers"), $this->nr_min_answers);
405  }
406  if ($this->use_min_answers && $this->nr_min_answers > 0 && count($entered_value) < $this->nr_min_answers)
407  {
408  return sprintf($this->lng->txt("err_no_min_answers"), $this->nr_min_answers);
409  }
410  if ($this->use_min_answers && $this->nr_max_answers > 0 && count($entered_value) > $this->nr_max_answers)
411  {
412  return sprintf($this->lng->txt("err_no_max_answers"), $this->nr_max_answers);
413  }
414  if (!is_array($entered_value))
415  {
416  return $this->lng->txt("question_mr_not_checked");
417  }
418  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
419  {
420  $cat = $this->categories->getCategory($i);
421  if ($cat->other)
422  {
423  if (in_array($i, $entered_value))
424  {
425  if (array_key_exists($this->getId() . "_" . $i . "_other", $post_data) && !strlen($post_data[$this->getId() . "_" . $i . "_other"]))
426  {
427  return $this->lng->txt("question_mr_no_other_answer");
428  }
429  }
430  else
431  {
432  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
433  {
434  return $this->lng->txt("question_mr_no_other_answer_checked");
435  }
436  }
437  }
438  }
439  return "";
440  }
441 
447  public function saveRandomData($active_id)
448  {
449  global $ilDB;
450  // multiple responses
451  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
452  {
453  if (rand(0,1))
454  {
455  $cat = $this->categories->getCategory($i);
456  $next_id = $ilDB->nextId('svy_answer');
457  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
458  array('integer','integer','integer','float','text','integer'),
459  array($next_id, $this->getId(), $active_id, $i, ($cat->other) ? "Random Data" : null, time())
460  );
461  }
462  }
463  }
464 
465  function saveUserInput($post_data, $active_id)
466  {
467  global $ilDB;
468 
469  if (is_array($post_data[$this->getId() . "_value"]))
470  {
471  foreach ($post_data[$this->getId() . "_value"] as $entered_value)
472  {
473  if (strlen($entered_value) > 0)
474  {
475  $next_id = $ilDB->nextId('svy_answer');
476  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
477  array('integer','integer','integer','float','text','integer'),
478  array($next_id, $this->getId(), $active_id, (strlen($entered_value)) ? $entered_value : NULL, ($post_data[$this->getId() . "_" . $entered_value . "_other"]) ? $post_data[$this->getId() . "_" . $entered_value . "_other"] : null, time())
479  );
480  }
481  }
482  }
483  }
484 
485  function &getCumulatedResults($survey_id, $nr_of_users)
486  {
487  global $ilDB;
488 
489  $question_id = $this->getId();
490 
491  $result_array = array();
492  $cumulated = array();
493 
494  $result = $ilDB->queryF("SELECT svy_answer.* FROM svy_answer, svy_finished WHERE svy_answer.question_fi = %s AND svy_finished.survey_fi = %s AND svy_finished.finished_id = svy_answer.active_fi",
495  array('integer', 'integer'),
496  array($question_id, $survey_id)
497  );
498  $numrows = $result->numRows();
499 
500  // count the answers for every answer value
501  while ($row = $ilDB->fetchAssoc($result))
502  {
503  $cumulated[$row["value"]]++;
504 
505  // add text value to result array
506  if ($row["textanswer"])
507  {
508  $result_array["textanswers"][$row["value"]][] = $row["textanswer"];
509  }
510  }
511  // sort textanswers by value
512  if (is_array($result_array["textanswers"]))
513  {
514  ksort($result_array["textanswers"], SORT_NUMERIC);
515  }
516  asort($cumulated, SORT_NUMERIC);
517  end($cumulated);
518 
519  $mcmr_result = $ilDB->queryF("SELECT svy_answer.answer_id, svy_answer.question_fi, svy_answer.active_fi FROM svy_answer, svy_finished WHERE svy_answer.question_fi = %s AND svy_finished.survey_fi = %s AND svy_finished.finished_id = svy_answer.active_fi",
520  array('integer', 'integer'),
521  array($question_id, $survey_id)
522  );
523  $found = array();
524  while ($row = $ilDB->fetchAssoc($mcmr_result))
525  {
526  $found[$row["question_fi"] . "_" . $row["active_fi"]] = 1;
527  }
528  $result_array["USERS_ANSWERED"] = count($found);
529  $result_array["USERS_SKIPPED"] = $nr_of_users - count($found);
530  $numrows = count($found);
531 
532  $result_array["MEDIAN"] = "";
533  $result_array["ARITHMETIC_MEAN"] = "";
534  $prefix = "";
535  if (strcmp(key($cumulated), "") != 0)
536  {
537  $prefix = (key($cumulated)+1) . " - ";
538  }
539  $category = $this->categories->getCategoryForScale(key($cumulated)+1);
540  $result_array["MODE"] = $prefix . $category->title;
541  $result_array["MODE_VALUE"] = key($cumulated)+1;
542  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
543  $result_array["QUESTION_TYPE"] = "SurveyMultipleChoiceQuestion";
544  $maxvalues = 0;
545  for ($key = 0; $key < $this->categories->getCategoryCount(); $key++)
546  {
547  $cat = $this->categories->getCategory($key);
548  $maxvalues += $cumulated[$cat->scale-1];
549  }
550  for ($key = 0; $key < $this->categories->getCategoryCount(); $key++)
551  {
552  $cat = $this->categories->getCategory($key);
553  $percentage = 0;
554  if ($numrows > 0)
555  {
556  if ($maxvalues > 0)
557  {
558  $percentage = ($maxvalues > 0) ? (float)((int)$cumulated[$cat->scale-1]/$maxvalues) : 0;
559  }
560  }
561  $result_array["variables"][$key] = array("title" => $cat->title, "selected" => (int)$cumulated[$cat->scale-1], "percentage" => $percentage);
562  }
563  return $result_array;
564  }
565 
573  function setExportDetailsXLS(&$adapter, &$eval_data, $export_label)
574  {
575  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
576  $adapter->addWorksheet($this->getTitle());
577  $adapter->setActiveWorksheet($adapter->getWorksheetCount()-1);
578  $rowcounter = 0;
579  switch ($export_label)
580  {
581  case 'label_only':
582  $adapter->setCellValue(0, 0, $this->lng->txt("label"), CELL_FORMAT_BOLD);
583  $adapter->setCellValue(0, 1, $this->label);
584  break;
585  case 'title_only':
586  $adapter->setCellValue(0, 0, $this->lng->txt("title"), CELL_FORMAT_BOLD);
587  $adapter->setCellValue(0, 1, $this->getTitle());
588  break;
589  default:
590  $adapter->setCellValue(0, 0, $this->lng->txt("title"), CELL_FORMAT_BOLD);
591  $adapter->setCellValue(0, 1, $this->getTitle());
592  $rowcounter++;
593  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("label"), CELL_FORMAT_BOLD);
594  $adapter->setCellValue($rowcounter, 1, $this->label);
595  break;
596  }
597  $rowcounter++;
598  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("question"), CELL_FORMAT_BOLD);
599  $adapter->setCellValue($rowcounter, 1, $this->getQuestiontext());
600  $rowcounter++;
601  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("question_type"), CELL_FORMAT_BOLD);
602  $adapter->setCellValue($rowcounter, 1, $this->lng->txt($this->getQuestionType()));
603  $rowcounter++;
604  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("users_answered"), CELL_FORMAT_BOLD);
605  $adapter->setCellValue($rowcounter, 1, $eval_data["TOTAL"]["USERS_ANSWERED"]);
606  $rowcounter++;
607  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("users_skipped"), CELL_FORMAT_BOLD);
608  $adapter->setCellValue($rowcounter, 1, $eval_data["TOTAL"]["USERS_SKIPPED"]);
609  $rowcounter++;
610 
611  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("mode"), CELL_FORMAT_BOLD);
612  $adapter->setCellValue($rowcounter++, 1, $eval_data["MODE_VALUE"]);
613  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("mode_text"), CELL_FORMAT_BOLD);
614  $adapter->setCellValue($rowcounter++, 1, $eval_data["MODE"]);
615  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("mode_nr_of_selections"), CELL_FORMAT_BOLD);
616  $adapter->setCellValue($rowcounter++, 1, $eval_data["MODE_NR_OF_SELECTIONS"]);
617  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("categories"), CELL_FORMAT_BOLD);
618  $adapter->setCellValue($rowcounter, 1, $this->lng->txt("title"), CELL_FORMAT_TITLE);
619  $adapter->setCellValue($rowcounter, 2, $this->lng->txt("value"), CELL_FORMAT_TITLE);
620  $adapter->setCellValue($rowcounter, 3, $this->lng->txt("category_nr_selected"), CELL_FORMAT_TITLE);
621  $adapter->setCellValue($rowcounter++, 4, $this->lng->txt("percentage_of_selections"), CELL_FORMAT_TITLE);
622 
623  foreach ($eval_data["variables"] as $key => $value)
624  {
625  $adapter->setCellValue($rowcounter, 1, $value["title"]);
626  $category = $this->categories->getCategory($key);
627  $adapter->setCellValue($rowcounter, 2, $category->scale);
628  $adapter->setCellValue($rowcounter, 3, $value["selected"]);
629  $adapter->setCellValue($rowcounter++, 4, $value["percentage"], CELL_FORMAT_PERCENT);
630  }
631 
632  // add text answers to detailed results
633  if (is_array($eval_data["textanswers"]))
634  {
635  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("freetext_answers"), CELL_FORMAT_BOLD);
636  $adapter->setCellValue($rowcounter, 1, $this->lng->txt("title"), CELL_FORMAT_TITLE);
637  $adapter->setCellValue($rowcounter++, 2, $this->lng->txt("answer"), CELL_FORMAT_TITLE);
638 
639  foreach ($eval_data["textanswers"] as $key => $answers)
640  {
641  $title = $eval_data["variables"][$key]["title"];
642  foreach ($answers as $answer)
643  {
644  $adapter->setCellValue($rowcounter, 1, $title);
645  $adapter->setCellValue($rowcounter++, 2, $answer);
646  }
647  }
648  }
649  }
650 
657  function addUserSpecificResultsExportTitles(&$a_array, $a_export_label = "")
658  {
659  parent::addUserSpecificResultsExportTitles($a_array, $a_export_label);
660 
661  for ($index = 0; $index < $this->categories->getCategoryCount(); $index++)
662  {
663  $category = $this->categories->getCategory($index);
664  $title = $category->title;
665  array_push($a_array, $title);
666 
667  // optionally add headers for text answers
668  if ($category->other)
669  {
670  array_push($a_array, $title . " - ". $this->lng->txt("other"));
671  }
672  }
673  }
674 
682  function addUserSpecificResultsData(&$a_array, &$resultset)
683  {
684  if (count($resultset["answers"][$this->getId()]))
685  {
686  array_push($a_array, "");
687  for ($index = 0; $index < $this->categories->getCategoryCount(); $index++)
688  {
689  $category = $this->categories->getCategory($index);
690  $incoming_value = $category->scale ? $category->scale-1 : $index;
691 
692  $found = 0;
693  $textanswer = "";
694  foreach ($resultset["answers"][$this->getId()] as $answerdata)
695  {
696  if (strcmp($incoming_value, $answerdata["value"]) == 0)
697  {
698  $found = $answerdata["value"]+1;
699  $textanswer = $answerdata["textanswer"];
700  }
701  }
702  if ($found)
703  {
704  array_push($a_array, $found);
705  }
706  else
707  {
708  array_push($a_array, "0");
709  }
710  if ($category->other)
711  {
712  array_push($a_array, $textanswer);
713  }
714  }
715  }
716  else
717  {
718  array_push($a_array, $this->lng->txt("skipped"));
719  for ($index = 0; $index < $this->categories->getCategoryCount(); $index++)
720  {
721  array_push($a_array, "");
722 
723  // add empty text answers for skipped question
724  $category = $this->categories->getCategory($index);
725  if ($category->other)
726  {
727  array_push($a_array, "");
728  }
729  }
730  }
731  }
732 
741  {
742  global $ilDB;
743 
744  $answers = array();
745 
746  $result = $ilDB->queryF("SELECT svy_answer.* FROM svy_answer, svy_finished WHERE svy_finished.survey_fi = %s AND svy_answer.question_fi = %s AND svy_finished.finished_id = svy_answer.active_fi",
747  array('integer', 'integer'),
748  array($survey_id, $this->getId())
749  );
750  while ($row = $ilDB->fetchAssoc($result))
751  {
752  $category = $this->categories->getCategoryForScale($row["value"]+1);
753  if (!is_array($answers[$row["active_fi"]]))
754  {
755  $answers[$row["active_fi"]] = array();
756  }
757  $title = $row["value"] + 1 . " - " . $category->title;
758  if ($category->other) $title .= ": " . $row["textanswer"];
759  $catindex = $this->categories->getIndex($category);
760  if ($catindex !== null)
761  {
762  $answers[$row["active_fi"]][$catindex] = $title;
763  }
764  else
765  {
766  array_push($answers[$row["active_fi"]], $title);
767  }
768  ksort($answers[$row["active_fi"]], SORT_NUMERIC);
769  }
770  return $answers;
771  }
772 
781  function importAdditionalMetadata($a_meta)
782  {
783  foreach ($a_meta as $key => $value)
784  {
785  switch ($value["label"])
786  {
787  case "orientation":
788  $this->setOrientation($value["entry"]);
789  break;
790  case "use_min_answers":
791  $this->use_min_answers = $value["entry"];
792  break;
793  case "nr_min_answers":
794  $this->nr_min_answers = $value["entry"];
795  break;
796  case "nr_max_answers":
797  $this->nr_max_answers = $value["entry"];
798  break;
799  }
800  }
801  }
802 
809  function importResponses($a_data)
810  {
811  foreach ($a_data as $id => $data)
812  {
813  $categorytext = "";
814  foreach ($data["material"] as $material)
815  {
816  $categorytext .= $material["text"];
817  }
818  $this->categories->addCategory(
819  $categorytext,
820  strlen($data['other']) ? $data['other'] : 0,
821  strlen($data['neutral']) ? $data['neutral'] : 0,
822  strlen($data['label']) ? $data['label'] : null,
823  strlen($data['scale']) ? $data['scale'] : null
824  );
825  }
826  }
827 
835  {
836  return TRUE;
837  }
838 
846  {
847  return array("=", "<>");
848  }
849 
855  public function getPreconditionOptions()
856  {
857  global $lng;
858 
859  $options = array();
860  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
861  {
862  $category = $this->categories->getCategory($i);
863  $options[$i] = ($i+1) . " - " . $category->title;
864  }
865  return $options;
866  }
867 
874  public function getPreconditionSelectValue($default = "", $title, $variable)
875  {
876  include_once "./Services/Form/classes/class.ilSelectInputGUI.php";
877  $step3 = new ilSelectInputGUI($title, $variable);
878  $options = $this->getPreconditionOptions();
879  $step3->setOptions($options);
880  $step3->setValue($default);
881  return $step3;
882  }
883 
891  function getPreconditionValueOutput($value)
892  {
893  $category = $this->categories->getCategory($value);
894  return ($value + 1) . " - " . ((strlen($category->title)) ? $category->title : $this->lng->txt('other_answer'));
895  }
896 
897 
906  function outChart($survey_id, $type = "")
907  {
908  global $ilLog;
909 
910  if (count($this->cumulated) == 0)
911  {
912  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
914  $this->cumulated =& $this->getCumulatedResults($survey_id, $nr_of_users);
915  }
916 
917  foreach ($this->cumulated["variables"] as $key => $value)
918  {
919  foreach ($value as $key2 => $value2)
920  {
921  $this->cumulated["variables"][$key][$key2] = utf8_decode($value2);
922  }
923  }
924  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyChart.php";
925  $b1 = new SurveyChart("bars",400,250,utf8_decode($this->getTitle()),utf8_decode($this->lng->txt("answers")),utf8_decode($this->lng->txt("users_answered")),$this->cumulated["variables"]);
926  }
927 
928  public function getCategories()
929  {
930  return $this->categories;
931  }
932 
933 }
934 ?>