ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveySingleChoiceQuestion.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 
38 {
45 
58  $title = "",
59  $description = "",
60  $author = "",
61  $questiontext = "",
62  $owner = -1,
63  $orientation = 1
64  )
65  {
67  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
68  $this->orientation = $orientation;
69  $this->categories = new SurveyCategories();
70  }
71 
79  function &getCategoriesForPhrase($phrase_id)
80  {
81  global $ilDB;
82  $categories = array();
83  $result = $ilDB->queryF("SELECT svy_category.* FROM svy_category, svy_phrase_cat WHERE svy_phrase_cat.category_fi = svy_category.category_id AND svy_phrase_cat.phrase_fi = %s ORDER BY svy_phrase_cat.sequence",
84  array('integer'),
85  array($phrase_id)
86  );
87  while ($row = $ilDB->fetchAssoc($result))
88  {
89  if (($row["defaultvalue"] == 1) and ($row["owner_fi"] == 0))
90  {
91  $categories[$row["category_id"]] = $this->lng->txt($row["title"]);
92  }
93  else
94  {
95  $categories[$row["category_id"]] = $row["title"];
96  }
97  }
98  return $categories;
99  }
100 
107  function addPhrase($phrase_id)
108  {
109  global $ilUser;
110  global $ilDB;
111 
112  $result = $ilDB->queryF("SELECT svy_category.* FROM svy_category, svy_phrase_cat WHERE svy_phrase_cat.category_fi = svy_category.category_id AND svy_phrase_cat.phrase_fi = %s AND (svy_category.owner_fi = 0 OR svy_category.owner_fi = %s) ORDER BY svy_phrase_cat.sequence",
113  array('integer', 'integer'),
114  array($phrase_id, $ilUser->getId())
115  );
116  while ($row = $ilDB->fetchAssoc($result))
117  {
118  $neutral = $row["neutral"];
119  if (($row["defaultvalue"] == 1) and ($row["owner_fi"] == 0))
120  {
121  $this->categories->addCategory($this->lng->txt($row["title"]), 0, $neutral);
122  }
123  else
124  {
125  $this->categories->addCategory($row["title"], 0, $neutral);
126  }
127  }
128  }
129 
138  {
139  global $ilDB;
140 
141  $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",
142  array('integer'),
143  array($id)
144  );
145  if ($result->numRows() == 1)
146  {
147  return $ilDB->fetchAssoc($result);
148  }
149  else
150  {
151  return array();
152  }
153  }
154 
161  function loadFromDb($id)
162  {
163  global $ilDB;
164 
165  $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",
166  array('integer'),
167  array($id)
168  );
169  if ($result->numRows() == 1)
170  {
171  $data = $ilDB->fetchAssoc($result);
172  $this->setId($data["question_id"]);
173  $this->setTitle($data["title"]);
174  $this->label = $data['label'];
175  $this->setDescription($data["description"]);
176  $this->setObjId($data["obj_fi"]);
177  $this->setAuthor($data["author"]);
178  $this->setOwner($data["owner_fi"]);
179  include_once("./Services/RTE/classes/class.ilRTE.php");
180  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
181  $this->setObligatory($data["obligatory"]);
182  $this->setComplete($data["complete"]);
183  $this->setOriginalId($data["original_id"]);
184  $this->setOrientation($data["orientation"]);
185 
186  $this->categories->flushCategories();
187  $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",
188  array('integer'),
189  array($id)
190  );
191  if ($result->numRows() > 0)
192  {
193  while ($data = $ilDB->fetchAssoc($result))
194  {
195  $this->categories->addCategory($data["title"], $data["other"], $data["neutral"], null, ($data['scale']) ? $data['scale'] : ($data['sequence'] + 1));
196  }
197  }
198  }
200  }
201 
208  function isComplete()
209  {
210  if (
211  strlen($this->getTitle()) &&
212  strlen($this->getAuthor()) &&
213  strlen($this->getQuestiontext()) &&
214  $this->categories->getCategoryCount()
215  )
216  {
217  return 1;
218  }
219  else
220  {
221  return 0;
222  }
223  }
224 
230  function saveToDb($original_id = "")
231  {
232  global $ilDB;
233 
234  $affectedRows = parent::saveToDb($original_id);
235  if ($affectedRows == 1)
236  {
237  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
238  array('integer'),
239  array($this->getId())
240  );
241  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, orientation) VALUES (%s, %s)",
242  array('integer', 'text'),
243  array(
244  $this->getId(),
245  $this->getOrientation()
246  )
247  );
248 
249  $this->saveMaterial();
250  $this->saveCategoriesToDb();
251  }
252  }
253 
255  {
256  global $ilDB;
257 
258  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_variable WHERE question_fi = %s",
259  array('integer'),
260  array($this->getId())
261  );
262 
263  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
264  {
265  $cat = $this->categories->getCategory($i);
266  $category_id = $this->saveCategoryToDb($cat->title, $cat->neutral);
267  $next_id = $ilDB->nextId('svy_variable');
268  $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)",
269  array('integer','integer','integer','float','integer','integer', 'integer','integer'),
270  array($next_id, $category_id, $this->getId(), ($i + 1), $cat->other, $i, ($cat->scale > 0) ? $cat->scale : null, time())
271  );
272  }
273  $this->saveCompletionStatus();
274  }
275 
282  function toXML($a_include_header = TRUE, $obligatory_state = "")
283  {
284  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
285  $a_xml_writer = new ilXmlWriter;
286  $a_xml_writer->xmlHeader();
287  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
288  $xml = $a_xml_writer->xmlDumpMem(FALSE);
289  if (!$a_include_header)
290  {
291  $pos = strpos($xml, "?>");
292  $xml = substr($xml, $pos + 2);
293  }
294  return $xml;
295  }
296 
305  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
306  {
307  $attrs = array(
308  "id" => $this->getId(),
309  "title" => $this->getTitle(),
310  "type" => $this->getQuestiontype(),
311  "obligatory" => $this->getObligatory()
312  );
313  $a_xml_writer->xmlStartTag("question", $attrs);
314 
315  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
316  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
317  if (strlen($this->label))
318  {
319  $attrs = array(
320  "label" => $this->label,
321  );
322  }
323  else
324  {
325  $attrs = array();
326  }
327  $a_xml_writer->xmlStartTag("questiontext", $attrs);
328  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
329  $a_xml_writer->xmlEndTag("questiontext");
330 
331  $a_xml_writer->xmlStartTag("responses");
332 
333  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
334  {
335  $attrs = array(
336  "id" => $i
337  );
338  if (strlen($this->categories->getCategory($i)->other)) $attrs['other'] = $this->categories->getCategory($i)->other;
339  if (strlen($this->categories->getCategory($i)->neutral)) $attrs['neutral'] = $this->categories->getCategory($i)->neutral;
340  if (strlen($this->categories->getCategory($i)->label)) $attrs['label'] = $this->categories->getCategory($i)->label;
341  if (strlen($this->categories->getCategory($i)->scale)) $attrs['scale'] = $this->categories->getCategory($i)->scale;
342  $a_xml_writer->xmlStartTag("response_single", $attrs);
343  $this->addMaterialTag($a_xml_writer, $this->categories->getCategory($i)->title);
344  $a_xml_writer->xmlEndTag("response_single");
345  }
346 
347  $a_xml_writer->xmlEndTag("responses");
348 
349  if (count($this->material))
350  {
351  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
352  {
353  $attrs = array(
354  "label" => $this->material["title"]
355  );
356  $a_xml_writer->xmlStartTag("material", $attrs);
357  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
358  if (strcmp($matches[1], "") != 0)
359  {
360  $intlink = $this->material["internal_link"];
361  }
362  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
363  $a_xml_writer->xmlEndTag("material");
364  }
365  }
366 
367  $a_xml_writer->xmlStartTag("metadata");
368  $a_xml_writer->xmlStartTag("metadatafield");
369  $a_xml_writer->xmlElement("fieldlabel", NULL, "orientation");
370  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getOrientation());
371  $a_xml_writer->xmlEndTag("metadatafield");
372  $a_xml_writer->xmlEndTag("metadata");
373 
374  $a_xml_writer->xmlEndTag("question");
375  }
376 
385  function importAdditionalMetadata($a_meta)
386  {
387  foreach ($a_meta as $key => $value)
388  {
389  switch ($value["label"])
390  {
391  case "orientation":
392  $this->setOrientation($value["entry"]);
393  break;
394  }
395  }
396  }
397 
405  function addStandardNumbers($lower_limit, $upper_limit)
406  {
407  for ($i = $lower_limit; $i <= $upper_limit; $i++)
408  {
409  $this->categories->addCategory($i);
410  }
411  }
412 
420  function savePhrase($title)
421  {
422  global $ilUser;
423  global $ilDB;
424 
425  $next_id = $ilDB->nextId('svy_phrase');
426  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase (phrase_id, title, defaultvalue, owner_fi, tstamp) VALUES (%s, %s, %s, %s, %s)",
427  array('integer','text','text','integer','integer'),
428  array($next_id, $title, 1, $ilUser->getId(), time())
429  );
430  $phrase_id = $next_id;
431 
432  $counter = 1;
433  foreach ($_SESSION['save_phrase_data'] as $data)
434  {
435  $next_id = $ilDB->nextId('svy_category');
436  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, tstamp, neutral) VALUES (%s, %s, %s, %s, %s, %s)",
437  array('integer','text','text','integer','integer','text'),
438  array($next_id, $data['answer'], 1, $ilUser->getId(), time(), $data['neutral'])
439  );
440  $category_id = $next_id;
441  $next_id = $ilDB->nextId('svy_phrase_cat');
442  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence, other, scale) VALUES (%s, %s, %s, %s, %s, %s)",
443  array('integer', 'integer', 'integer','integer', 'integer', 'integer'),
444  array($next_id, $phrase_id, $category_id, $counter, ($data['other']) ? 1 : 0, $data['scale'])
445  );
446  $counter++;
447  }
448  }
449 
456  function getQuestionType()
457  {
458  return "SurveySingleChoiceQuestion";
459  }
460 
468  {
469  return "svy_qst_sc";
470  }
471 
478  function &getWorkingDataFromUserInput($post_data)
479  {
480  $entered_value = $post_data[$this->getId() . "_value"];
481  $data = array();
482  if (strlen($entered_value))
483  {
484  array_push($data, array("value" => $entered_value, "textanswer" => $post_data[$this->getId() . '_' . $entered_value . '_other']));
485  }
486  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
487  {
488  $cat = $this->categories->getCategory($i);
489  if ($cat->other)
490  {
491  if ($i != $entered_value)
492  {
493  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
494  {
495  array_push($data, array("value" => $i, "textanswer" => $post_data[$this->getId() . '_' . $i . '_other'], "uncheck" => true));
496  }
497  }
498  }
499  }
500  return $data;
501  }
502 
512  function checkUserInput($post_data, $survey_id)
513  {
514  $entered_value = $post_data[$this->getId() . "_value"];
515 
516  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
517 
518  if (strlen($entered_value) == 0) return $this->lng->txt("question_not_checked");
519 
520  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
521  {
522  $cat = $this->categories->getCategory($i);
523  if ($cat->other)
524  {
525  if ($i == $entered_value)
526  {
527  if (array_key_exists($this->getId() . "_" . $entered_value . "_other", $post_data) && !strlen($post_data[$this->getId() . "_" . $entered_value . "_other"]))
528  {
529  return $this->lng->txt("question_mr_no_other_answer");
530  }
531  }
532  else
533  {
534  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
535  {
536  return $this->lng->txt("question_sr_no_other_answer_checked");
537  }
538  }
539  }
540  }
541 
542  return "";
543  }
544 
550  public function saveRandomData($active_id)
551  {
552  global $ilDB;
553  // single response
554  $category = rand(0, $this->categories->getCategoryCount()-1);
555  $cat = $this->categories->getCategory($category);
556  $next_id = $ilDB->nextId('svy_answer');
557  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
558  array('integer','integer','integer','float','text','integer'),
559  array($next_id, $this->getId(), $active_id, $category, ($cat->other) ? "Random Data" : null, time())
560  );
561  }
562 
563  function saveUserInput($post_data, $active_id, $a_return = false)
564  {
565  global $ilDB;
566 
567  $entered_value = $post_data[$this->getId() . "_value"];
568 
569  if($a_return)
570  {
571  return array(array("value"=>$entered_value,
572  "textanswer"=>$post_data[$this->getId() . "_" . $entered_value . "_other"]));
573  }
574  if (strlen($entered_value) == 0) return;
575 
576  $next_id = $ilDB->nextId('svy_answer');
577  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
578  array('integer','integer','integer','float','text','integer'),
579  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())
580  );
581  }
582 
583  function &getCumulatedResults($survey_id, $nr_of_users, $finished_ids)
584  {
585  global $ilDB;
586 
587  $question_id = $this->getId();
588 
589  $result_array = array();
590  $cumulated = array();
591 
592  $sql = "SELECT svy_answer.* FROM svy_answer".
593  " JOIN svy_finished ON (svy_finished.finished_id = svy_answer.active_fi)".
594  " WHERE svy_answer.question_fi = ".$ilDB->quote($question_id, "integer").
595  " AND svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer");
596  if($finished_ids)
597  {
598  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
599  }
600 
601  $result = $ilDB->query($sql);
602  while ($row = $ilDB->fetchAssoc($result))
603  {
604  $cumulated[$row["value"]]++;
605 
606  // add text value to result array
607  if ($row["textanswer"])
608  {
609  $result_array["textanswers"][$row["value"]][] = $row["textanswer"];
610  }
611  }
612  // sort textanswers by value
613  if (is_array($result_array["textanswers"]))
614  {
615  ksort($result_array["textanswers"], SORT_NUMERIC);
616  }
617  asort($cumulated, SORT_NUMERIC);
618  end($cumulated);
619  $numrows = $result->numRows();
620  $result_array["USERS_ANSWERED"] = $result->numRows();
621  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
622 
623  if(sizeof($cumulated))
624  {
625  $prefix = "";
626  if (strcmp(key($cumulated), "") != 0)
627  {
628  $prefix = (key($cumulated)+1) . " - ";
629  }
630  $category = $this->categories->getCategoryForScale(key($cumulated)+1);
631  $result_array["MODE"] = $prefix . $category->title;
632  $result_array["MODE_VALUE"] = key($cumulated)+1;
633  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
634  }
635  for ($key = 0; $key < $this->categories->getCategoryCount(); $key++)
636  {
637  $cat = $this->categories->getCategory($key);
638  $percentage = 0;
639  if ($numrows > 0)
640  {
641  $percentage = (float)((int)$cumulated[$cat->scale-1]/$numrows);
642  }
643  if ($cat->other)
644  {
645  $result_array["variables"][$key] = array("title" => (strlen($cat->title)) ? $cat->title : $this->lng->txt('other_answer'), "selected" => (int)$cumulated[$cat->scale-1], "percentage" => $percentage);
646  }
647  else
648  {
649  $result_array["variables"][$key] = array("title" => $cat->title, "selected" => (int)$cumulated[$cat->scale-1], "percentage" => $percentage);
650  }
651  }
652  ksort($cumulated, SORT_NUMERIC);
653  $median = array();
654  $total = 0;
655  foreach ($cumulated as $value => $key)
656  {
657  $total += $key;
658  for ($i = 0; $i < $key; $i++)
659  {
660  array_push($median, $value+1);
661  }
662  }
663  if ($total > 0)
664  {
665  if (($total % 2) == 0)
666  {
667  $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
668  if (round($median_value) != $median_value)
669  {
670  $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $this->categories->getCategory((int)floor($median_value)-1)->title . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $this->categories->getCategory((int)ceil($median_value)-1)->title . ")";
671  }
672  }
673  else
674  {
675  $median_value = $median[(($total+1)/2)-1];
676  }
677  }
678  else
679  {
680  $median_value = "";
681  }
682  $result_array["ARITHMETIC_MEAN"] = "";
683  $result_array["MEDIAN"] = $median_value;
684  $result_array["QUESTION_TYPE"] = "SurveySingleChoiceQuestion";
685  return $result_array;
686  }
687 
697  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
698  {
699  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
700  $worksheet =& $workbook->addWorksheet();
701  $rowcounter = 0;
702  switch ($export_label)
703  {
704  case 'label_only':
705  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
706  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->label));
707  break;
708  case 'title_only':
709  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
710  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
711  break;
712  default:
713  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
714  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
715  $rowcounter++;
716  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
717  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->label));
718  break;
719  }
720  $rowcounter++;
721  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
722  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
723  $rowcounter++;
724  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
725  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
726  $rowcounter++;
727  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
728  $worksheet->write($rowcounter, 1, $eval_data["USERS_ANSWERED"]);
729  $rowcounter++;
730  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
731  $worksheet->write($rowcounter, 1, $eval_data["USERS_SKIPPED"]);
732  $rowcounter++;
733 
734  preg_match("/(.*?)\s+-\s+(.*)/", $eval_data["MODE"], $matches);
735  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
736  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
737  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
738  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
739  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
740  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($eval_data["MODE_NR_OF_SELECTIONS"]));
741  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
742  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["MEDIAN"])));
743  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
744  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
745  $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
746  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
747  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
748 
749  foreach ($eval_data["variables"] as $key => $value)
750  {
751  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
752  $category = $this->categories->getCategory($key);
753  $worksheet->write($rowcounter, 2, $category->scale);
754  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
755  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
756  }
757 
758  // add text answers to detailed results
759  if (is_array($eval_data["textanswers"]))
760  {
761  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("freetext_answers")), $format_bold);
762  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
763  $worksheet->write($rowcounter++, 2, ilExcelUtils::_convert_text($this->lng->txt("answer")), $format_title);
764 
765  foreach ($eval_data["textanswers"] as $key => $answers)
766  {
767  $title = $eval_data["variables"][$key]["title"];
768  foreach ($answers as $answer)
769  {
770  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($title));
771  $worksheet->write($rowcounter++, 2, ilExcelUtils::_convert_text($answer));
772  }
773  }
774  }
775  }
776 
785  function addUserSpecificResultsExportTitles(&$a_array, $a_use_label = false, $a_substitute = true)
786  {
787  $title = parent::addUserSpecificResultsExportTitles($a_array, $a_use_label, $a_substitute);
788 
789  // optionally add header for text answer
790  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
791  {
792  $cat = $this->categories->getCategory($i);
793  if ($cat->other)
794  {
795  if(!$a_use_label || $a_substitute)
796  {
797  array_push($a_array, $title. ' - '. $this->lng->txt('other'));
798  }
799  else
800  {
801  array_push($a_array, "");
802  }
803  break;
804  }
805  }
806  }
807 
815  function addUserSpecificResultsData(&$a_array, &$resultset)
816  {
817  // check if text answer column is needed
818  $other = false;
819  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
820  {
821  $cat = $this->categories->getCategory($i);
822  if ($cat->other)
823  {
824  $other = true;
825  break;
826  }
827  }
828 
829  if (count($resultset["answers"][$this->getId()]))
830  {
831  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
832  {
833  array_push($a_array, $answer["value"]+1);
834 
835  // add the text answer from the selected option
836  if ($other)
837  {
838  array_push($a_array, $answer["textanswer"]);
839  }
840  }
841  }
842  else
843  {
844  array_push($a_array, $this->getSkippedValue());
845 
846  if ($other)
847  {
848  array_push($a_array, "");
849  }
850  }
851  }
852 
860  function &getUserAnswers($survey_id, $finished_ids)
861  {
862  global $ilDB;
863 
864  $answers = array();
865 
866  $sql = "SELECT svy_answer.* FROM svy_answer".
867  " JOIN svy_finished ON (svy_finished.finished_id = svy_answer.active_fi)".
868  " WHERE svy_answer.question_fi = ".$ilDB->quote($this->getId(), "integer").
869  " AND svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer");
870  if($finished_ids)
871  {
872  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
873  }
874 
875  $result = $ilDB->query($sql);
876  while ($row = $ilDB->fetchAssoc($result))
877  {
878  $category = $this->categories->getCategoryForScale($row["value"]+1);
879  $title = $row["value"] + 1 . " - " . $category->title;
880  if ($category->other) $title .= ": " . $row["textanswer"];
881  $answers[$row["active_fi"]] = $title;
882  }
883  return $answers;
884  }
885 
892  function importResponses($a_data)
893  {
894  foreach ($a_data as $id => $data)
895  {
896  $categorytext = "";
897  foreach ($data["material"] as $material)
898  {
899  $categorytext .= $material["text"];
900  }
901  $this->categories->addCategory(
902  $categorytext,
903  strlen($data['other']) ? $data['other'] : 0,
904  strlen($data['neutral']) ? $data['neutral'] : 0,
905  strlen($data['label']) ? $data['label'] : null,
906  strlen($data['scale']) ? $data['scale'] : null
907  );
908  }
909  }
910 
918  {
919  return TRUE;
920  }
921 
929  {
930  return array("<", "<=", "=", "<>", ">=", ">");
931  }
932 
938  public function getPreconditionOptions()
939  {
940  global $lng;
941 
942  $options = array();
943  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
944  {
945  $category = $this->categories->getCategory($i);
946  $options[$category->scale-1] = $category->scale . " - " . $category->title;
947  }
948  return $options;
949  }
950 
957  public function getPreconditionSelectValue($default = "", $title, $variable)
958  {
959  include_once "./Services/Form/classes/class.ilSelectInputGUI.php";
960  $step3 = new ilSelectInputGUI($title, $variable);
961  $options = $this->getPreconditionOptions();
962  $step3->setOptions($options);
963  $step3->setValue($default);
964  return $step3;
965  }
966 
974  function getPreconditionValueOutput($value)
975  {
976  // #18136
977  $category = $this->categories->getCategoryForScale($value+1);
978 
979  // #17895 - see getPreconditionOptions()
980  return $category->scale .
981  " - " .
982  ((strlen($category->title)) ? $category->title : $this->lng->txt('other_answer'));
983  }
984 
985  public function getCategories()
986  {
987  return $this->categories;
988  }
989 
990 }
991 ?>