ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveyMatrixQuestion.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 {
47  var $columns;
48 
57 
65  var $rows;
66 
75 
84 
93 
102 
111 
112  /*
113  * Layout of the matrix question
114  *
115  * @var array
116  */
117  var $layout;
118 
119  /*
120  * Use placeholders for the column titles
121  *
122  * @var boolean
123  */
125 
126  /*
127  * Show a legend
128  *
129  * @var boolean
130  */
131  var $legend;
132 
134 
136 
138 
139  /*
140  * Use random order for rows
141  *
142  * @var boolean
143  */
145 
147 
149 
151 
152 
167  var $subtype;
168 
181  $title = "",
182  $description = "",
183  $author = "",
184  $questiontext = "",
185  $owner = -1
186  )
187 
188  {
190  $this->subtype = 0;
191  $this->columns = array();
192  $this->rows = array();
193  $this->neutralColumn = "";
194  $this->bipolar_adjective1 = "";
195  $this->bipolar_adjective2 = "";
196  $this->rowSeparators = 0;
197  $this->columnSeparators = 0;
198  $this->neutralColumnSeparator = 1;
199  }
200 
210  function getNeutralColumn()
211  {
212  return $this->neutralColumn;
213  }
214 
225  {
226  if (strlen($this->getNeutralColumn()))
227  {
228  return $this->getColumnCount();
229  }
230  else
231  {
232  return FALSE;
233  }
234  }
235 
245  function setNeutralColumn($a_text)
246  {
247  $this->neutralColumn = $a_text;
248  }
249 
259  function getColumnCount()
260  {
261  return count($this->columns);
262  }
263 
274  function addColumnAtPosition($columnname, $position)
275  {
276  if (array_key_exists($position, $this->columns))
277  {
278  $head = array_slice($this->columns, 0, $position);
279  $tail = array_slice($this->columns, $position);
280  $this->columns = array_merge($head, array($columnname), $tail);
281  }
282  else
283  {
284  array_push($this->columns, $columnname);
285  }
286  }
287 
298  function addColumn($columnname)
299  {
300  array_push($this->columns, $columnname);
301  }
302 
313  {
314  $this->columns = array_merge($this->columns, $columns);
315  }
316 
326  function removeColumn($index)
327  {
328  unset($this->columns[$index]);
329  $this->columns = array_values($this->columns);
330  }
331 
341  function removeColumns($array)
342  {
343  foreach ($array as $index)
344  {
345  unset($this->columns[$index]);
346  }
347  $this->columns = array_values($this->columns);
348  }
349 
359  function removeColumnWithName($name)
360  {
361  foreach ($this->columns as $index => $column)
362  {
363  if (strcmp($column, $name) == 0)
364  {
365  return $this->removeColumn($index);
366  }
367  }
368  }
369 
380  function getColumn($index)
381  {
382  if (array_key_exists($index, $this->columns))
383  {
384  return $this->columns[$index];
385  }
386  else
387  {
388  if (($index = $this->getColumnCount()) && (strlen($this->getNeutralColumn())))
389  {
390  return $this->getNeutralColumn();
391  }
392  else
393  {
394  return "";
395  }
396  }
397  }
398 
408  function getColumnIndex($name)
409  {
410  foreach ($this->columns as $index => $column)
411  {
412  if (strcmp($column, $name) == 0)
413  {
414  return $index;
415  }
416  }
417  return -1;
418  }
419 
420 
429  function flushColumns()
430  {
431  $this->columns = array();
432  }
433 
442  function getRowCount()
443  {
444  return count($this->rows);
445  }
446 
455  function addRow($a_text)
456  {
457  array_push($this->rows, $a_text);
458  }
459 
468  function flushRows()
469  {
470  $this->rows = array();
471  }
472 
481  function getRow($a_index)
482  {
483  if (array_key_exists($a_index, $this->rows))
484  {
485  return $this->rows[$a_index];
486  }
487  return "";
488  }
489 
499  function removeRows($array)
500  {
501  foreach ($array as $index)
502  {
503  unset($this->rows[$index]);
504  }
505  $this->rows = array_values($this->rows);
506  }
507 
517  function getBipolarAdjective($a_index)
518  {
519  switch ($a_index)
520  {
521  case 1:
523  break;
524  case 0:
525  default:
527  break;
528  }
529  }
530 
540  function setBipolarAdjective($a_index, $a_value)
541  {
542  switch ($a_index)
543  {
544  case 1:
545  $this->bipolar_adjective2 = $a_value;
546  break;
547  case 0:
548  default:
549  $this->bipolar_adjective1 = $a_value;
550  break;
551  }
552  }
553 
562  function addPhrase($phrase_id)
563  {
564  global $ilUser;
565  global $ilDB;
566 
567  $query = sprintf("SELECT survey_category.* FROM survey_category, survey_phrase_category WHERE survey_phrase_category.category_fi = survey_category.category_id AND survey_phrase_category.phrase_fi = %s AND (survey_category.owner_fi = 0 OR survey_category.owner_fi = %s) ORDER BY survey_phrase_category.sequence",
568  $ilDB->quote($phrase_id),
569  $ilDB->quote($ilUser->id)
570  );
571  $result = $ilDB->query($query);
572  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
573  {
574  if (($row->defaultvalue == 1) and ($row->owner_fi == 0))
575  {
576  $this->addColumn($this->lng->txt($row->title));
577  }
578  else
579  {
580  $this->addColumn($row->title);
581  }
582  }
583  }
584 
595  {
596  global $ilDB;
597 
598  $query = sprintf("SELECT survey_question.*, survey_question_matrix.* FROM survey_question, survey_question_matrix WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_matrix.question_fi",
599  $ilDB->quote($id)
600  );
601  $result = $ilDB->query($query);
602  if ($result->numRows() == 1)
603  {
604  return $result->fetchRow(MDB2_FETCHMODE_ASSOC);
605  }
606  else
607  {
608  return array();
609  }
610  }
611 
620  function loadFromDb($id)
621  {
622  global $ilDB;
623  $query = sprintf("SELECT survey_question.*, survey_question_matrix.* FROM survey_question, survey_question_matrix WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_matrix.question_fi",
624  $ilDB->quote($id)
625  );
626  $result = $ilDB->query($query);
627  if ($result->numRows() == 1)
628  {
629  $data = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
630  $this->id = $data->question_id;
631  $this->title = $data->title;
632  $this->description = $data->description;
633  $this->obj_id = $data->obj_fi;
634  $this->author = $data->author;
635  $this->owner = $data->owner_fi;
636  include_once("./Services/RTE/classes/class.ilRTE.php");
637  $this->questiontext = ilRTE::_replaceMediaObjectImageSrc($data->questiontext, 1);
638  $this->obligatory = $data->obligatory;
639  $this->complete = $data->complete;
640  $this->original_id = $data->original_id;
641  $this->setSubtype($data->subtype);
642  $this->setRowSeparators($data->row_separators);
643  $this->setNeutralColumnSeparator($data->neutral_column_separator);
644  $this->setColumnSeparators($data->column_separators);
645  $this->setColumnPlaceholders($data->column_placeholders);
646  $this->setLegend($data->legend);
647  $this->getSingleLineRowCaption($data->singleline_row_caption);
648  $this->getRepeatColumnHeader($data->repeat_column_header);
649  $this->getColumnHeaderPosition($data->column_header_position);
650  $this->getRandomRows($data->random_rows);
651  $this->getColumnOrder($data->column_order);
652  $this->getColumnImages($data->column_images);
653  $this->getRowImages($data->row_images);
654  $this->setBipolarAdjective(0, $data->bipolar_adjective1);
655  $this->setBipolarAdjective(1, $data->bipolar_adjective2);
656  $this->setLayout($data->layout);
657  // loads materials uris from database
658  $this->loadMaterialFromDb($id);
659 
660  $this->flushColumns();
661 
662  $query = sprintf("SELECT survey_variable.*, survey_category.title, survey_category.neutral FROM survey_variable, survey_category WHERE survey_variable.question_fi = %s AND survey_variable.category_fi = survey_category.category_id ORDER BY sequence ASC",
663  $ilDB->quote($id)
664  );
665  $result = $ilDB->query($query);
666  if ($result->numRows() > 0)
667  {
668  while ($data = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
669  {
670  if ($data->neutral == 0)
671  {
672  $this->addColumn($data->title);
673  }
674  else
675  {
676  $this->setNeutralColumn($data->title);
677  }
678  }
679  }
680 
681  $query = sprintf("SELECT * FROM survey_question_matrix_rows WHERE question_fi = %s ORDER BY sequence",
682  $ilDB->quote($id . "")
683  );
684  $result = $ilDB->query($query);
685  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
686  {
687  $this->addRow($row["title"]);
688  }
689  }
691  }
692 
701  function isComplete()
702  {
703  if (
704  strlen($this->getTitle()) &&
705  strlen($this->getAuthor()) &&
706  strlen($this->getQuestiontext()) &&
707  $this->getColumnCount() &&
708  $this->getRowCount()
709  )
710  {
711  return 1;
712  }
713  else
714  {
715  return 0;
716  }
717  }
718 
726  function saveToDb($original_id = "", $withanswers = true)
727  {
728  global $ilDB;
729  $complete = 0;
730  if ($this->isComplete())
731  {
732  $complete = 1;
733  }
734  if ($original_id)
735  {
736  $original_id = $ilDB->quote($original_id);
737  }
738  else
739  {
740  $original_id = "NULL";
741  }
742 
743  // cleanup RTE images which are not inserted into the question text
744  include_once("./Services/RTE/classes/class.ilRTE.php");
745  ilRTE::_cleanupMediaObjectUsage($this->questiontext, "spl:html",
746  $this->getId());
747 
748  if ($this->id == -1)
749  {
750  // Write new dataset
751  $now = getdate();
752  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
753  $query = sprintf("INSERT INTO survey_question (question_id, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
754  $ilDB->quote($this->getQuestionTypeID()),
755  $ilDB->quote($this->obj_id),
756  $ilDB->quote($this->owner),
757  $ilDB->quote($this->title),
758  $ilDB->quote($this->description),
759  $ilDB->quote($this->author),
760  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
761  $ilDB->quote(sprintf("%d", $this->obligatory)),
762  $ilDB->quote("$complete"),
763  $ilDB->quote($created),
764  $original_id
765  );
766  $result = $ilDB->query($query);
767  if (!PEAR::isError($result))
768  {
769  $this->id = $ilDB->getLastInsertId();
770  $query = sprintf("INSERT INTO survey_question_matrix (
771  question_fi,
772  subtype,
773  column_separators,
774  row_separators,
775  neutral_column_separator,
776  column_placeholders,
777  legend,
778  singleline_row_caption,
779  repeat_column_header,
780  column_header_position,
781  random_rows,
782  column_order,
783  column_images,
784  row_images,
785  bipolar_adjective1,
786  bipolar_adjective2,
787  layout
788  )
789  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
790  $ilDB->quote($this->id . ""),
791  $ilDB->quote(sprintf("%d", $this->getSubtype())),
792  $ilDB->quote($this->getColumnSeparators() . ""),
793  $ilDB->quote($this->getRowSeparators() . ""),
794  $ilDB->quote($this->getNeutralColumnSeparator() . ""),
795  $ilDB->quote($this->getColumnPlaceholders() . ""),
796  $ilDB->quote($this->getLegend() . ""),
797  $ilDB->quote($this->getSingleLineRowCaption() . ""),
798  $ilDB->quote($this->getRepeatColumnHeader() . ""),
799  $ilDB->quote($this->getColumnHeaderPosition() . ""),
800  $ilDB->quote($this->getRandomRows() . ""),
801  $ilDB->quote($this->getColumnOrder() . ""),
802  $ilDB->quote($this->getColumnImages() . ""),
803  $ilDB->quote($this->getRowImages() . ""),
804  $ilDB->quote($this->getBipolarAdjective(0) . ""),
805  $ilDB->quote($this->getBipolarAdjective(1) . ""),
806  $ilDB->quote(serialize($this->getLayout()) . "")
807  );
808  $ilDB->query($query);
809  }
810  }
811  else
812  {
813  // update existing dataset
814  $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
815  $ilDB->quote($this->title),
816  $ilDB->quote($this->description),
817  $ilDB->quote($this->author),
818  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
819  $ilDB->quote(sprintf("%d", $this->obligatory)),
820  $ilDB->quote("$complete"),
821  $ilDB->quote($this->id)
822  );
823  $result = $ilDB->query($query);
824  $query = sprintf("UPDATE survey_question_matrix SET
825  subtype = %s,
826  column_separators = %s,
827  row_separators = %s,
828  neutral_column_separator = %s,
829  column_placeholders = %s,
830  legend = %s,
831  singleline_row_caption = %s,
832  repeat_column_header = %s,
833  column_header_position = %s,
834  random_rows = %s,
835  column_order = %s,
836  column_images = %s,
837  row_images = %s,
838  bipolar_adjective1 = %s,
839  bipolar_adjective2 = %s,
840  layout = %s
841  WHERE question_fi = %s",
842  $ilDB->quote(sprintf("%d", $this->getSubtype())),
843  $ilDB->quote($this->getColumnSeparators() . ""),
844  $ilDB->quote($this->getRowSeparators() . ""),
845  $ilDB->quote($this->getNeutralColumnSeparator() . ""),
846  $ilDB->quote($this->getColumnPlaceholders() . ""),
847  $ilDB->quote($this->getLegend() . ""),
848  $ilDB->quote($this->getSingleLineRowCaption() . ""),
849  $ilDB->quote($this->getRepeatColumnHeader() . ""),
850  $ilDB->quote($this->getColumnHeaderPosition() . ""),
851  $ilDB->quote($this->getRandomRows() . ""),
852  $ilDB->quote($this->getColumnOrder() . ""),
853  $ilDB->quote($this->getColumnImages() . ""),
854  $ilDB->quote($this->getRowImages() . ""),
855  $ilDB->quote($this->getBipolarAdjective(0) . ""),
856  $ilDB->quote($this->getBipolarAdjective(1) . ""),
857  $ilDB->quote(serialize($this->getLayout()) . ""),
858  $ilDB->quote($this->id . "")
859  );
860  $result = $ilDB->query($query);
861  }
862  if (!PEAR::isError($result))
863  {
864  // saving material uris in the database
865  $this->saveMaterialsToDb();
866  if ($withanswers)
867  {
868  $this->saveColumnsToDb();
869  $this->saveRowsToDb();
870  }
871  }
872  parent::saveToDb($original_id);
873  }
874 
875  function saveBipolarAdjectives($adjective1, $adjective2)
876  {
877  global $ilDB;
878 
879  $query = sprintf("UPDATE survey_question_matrix SET bipolar_adjective1 = %s, bipolar_adjective2 = %s WHERE question_fi = %s",
880  $ilDB->quote($adjective1 . ""),
881  $ilDB->quote($adjective2 . ""),
882  $ilDB->quote($this->getId() . "")
883  );
884  $result = $ilDB->query($query);
885  }
886 
897  function saveColumnToDb($columntext, $neutral = 0)
898  {
899  global $ilUser, $ilDB;
900 
901  $query = sprintf("SELECT title, category_id FROM survey_category WHERE title = %s AND neutral = %s AND owner_fi = %s",
902  $ilDB->quote($columntext . ""),
903  $ilDB->quote($neutral . ""),
904  $ilDB->quote($ilUser->getId() . "")
905  );
906  $result = $ilDB->query($query);
907  $insert = FALSE;
908  $returnvalue = "";
909  if ($result->numRows())
910  {
911  $insert = TRUE;
912  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
913  {
914  if (strcmp($row->title, $columntext) == 0)
915  {
916  $returnvalue = $row->category_id;
917  $insert = FALSE;
918  }
919  }
920  }
921  else
922  {
923  $insert = TRUE;
924  }
925  if ($insert)
926  {
927  $query = sprintf("INSERT INTO survey_category (category_id, title, neutral, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
928  $ilDB->quote($columntext . ""),
929  $ilDB->quote($neutral . ""),
930  $ilDB->quote($ilUser->getId() . "")
931  );
932  $result = $ilDB->query($query);
933  $returnvalue = $ilDB->getLastInsertId();
934  }
935  return $returnvalue;
936  }
937 
938 
939  function saveColumnsToDb($original_id = "")
940  {
941  global $ilDB;
942 
943  // save columns
944  $question_id = $this->getId();
945  if (strlen($original_id))
946  {
947  $question_id = $original_id;
948  }
949 
950  // delete existing column relations
951  $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
952  $ilDB->quote($question_id)
953  );
954  $result = $ilDB->query($query);
955  // create new column relations
956  for ($i = 0; $i < $this->getColumnCount(); $i++)
957  {
958  $cat = $this->getColumn($i);
959  $column_id = $this->saveColumnToDb($cat);
960  $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
961  $ilDB->quote($column_id . ""),
962  $ilDB->quote($question_id . ""),
963  $ilDB->quote(($i + 1) . ""),
964  $ilDB->quote($i . "")
965  );
966  $answer_result = $ilDB->query($query);
967  }
968  if (strlen($this->getNeutralColumn()))
969  {
970  $column_id = $this->saveColumnToDb($this->getNeutralColumn(), 1);
971  $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
972  $ilDB->quote($column_id . ""),
973  $ilDB->quote($question_id . ""),
974  $ilDB->quote(($i + 1) . ""),
975  $ilDB->quote($i . "")
976  );
977  $answer_result = $ilDB->query($query);
978  }
979  $this->saveCompletionStatus($original_id);
980  }
981 
982  function saveRowsToDb($original_id = "")
983  {
984  global $ilDB;
985 
986  // save rows
987  $question_id = $this->getId();
988  if (strlen($original_id))
989  {
990  $question_id = $original_id;
991  }
992 
993  // delete existing rows
994  $query = sprintf("DELETE FROM survey_question_matrix_rows WHERE question_fi = %s",
995  $ilDB->quote($question_id . "")
996  );
997  $result = $ilDB->query($query);
998  // create new rows
999  for ($i = 0; $i < $this->getRowCount(); $i++)
1000  {
1001  $row = $this->getRow($i);
1002  $query = sprintf("INSERT INTO survey_question_matrix_rows (id_survey_question_matrix_rows, title, sequence, question_fi) VALUES (NULL, %s, %s, %s)",
1003  $ilDB->quote($row . ""),
1004  $ilDB->quote($i . ""),
1005  $ilDB->quote($question_id . "")
1006  );
1007  $answer_result = $ilDB->query($query);
1008  }
1009  $this->saveCompletionStatus($original_id);
1010  }
1011 
1020  function toXML($a_include_header = TRUE, $obligatory_state = "")
1021  {
1022  include_once("./classes/class.ilXmlWriter.php");
1023  $a_xml_writer = new ilXmlWriter;
1024  $a_xml_writer->xmlHeader();
1025  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
1026  $xml = $a_xml_writer->xmlDumpMem(FALSE);
1027  if (!$a_include_header)
1028  {
1029  $pos = strpos($xml, "?>");
1030  $xml = substr($xml, $pos + 2);
1031  }
1032  return $xml;
1033  }
1034 
1045  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
1046  {
1047  $attrs = array(
1048  "id" => $this->getId(),
1049  "title" => $this->getTitle(),
1050  "type" => $this->getQuestiontype(),
1051  "subtype" => $this->getSubtype(),
1052  "obligatory" => $this->getObligatory()
1053  );
1054  $a_xml_writer->xmlStartTag("question", $attrs);
1055 
1056  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
1057  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
1058  $a_xml_writer->xmlStartTag("questiontext");
1059  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
1060  $a_xml_writer->xmlEndTag("questiontext");
1061 
1062  $a_xml_writer->xmlStartTag("matrix");
1063  $a_xml_writer->xmlStartTag("matrixrows");
1064  for ($i = 0; $i < $this->getRowCount(); $i++)
1065  {
1066  $attrs = array(
1067  "id" => $i
1068  );
1069  $a_xml_writer->xmlStartTag("matrixrow", $attrs);
1070  $this->addMaterialTag($a_xml_writer, $this->getRow($i));
1071  $a_xml_writer->xmlEndTag("matrixrow");
1072  }
1073  $a_xml_writer->xmlEndTag("matrixrows");
1074 
1075  $a_xml_writer->xmlStartTag("responses");
1076  if (strlen($this->getBipolarAdjective(0)) && (strlen($this->getBipolarAdjective(1))))
1077  {
1078  $a_xml_writer->xmlStartTag("bipolar_adjectives");
1079  $attribs = array(
1080  "label" => "0"
1081  );
1082  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(0));
1083  $attribs = array(
1084  "label" => "1"
1085  );
1086  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(1));
1087  $a_xml_writer->xmlEndTag("bipolar_adjectives");
1088  }
1089  for ($i = 0; $i < $this->getColumnCount(); $i++)
1090  {
1091  $attrs = array(
1092  "id" => $i
1093  );
1094  switch ($this->getSubtype())
1095  {
1096  case 0:
1097  $a_xml_writer->xmlStartTag("response_single", $attrs);
1098  break;
1099  case 1:
1100  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
1101  break;
1102  }
1103  $this->addMaterialTag($a_xml_writer, $this->getColumn($i));
1104  switch ($this->getSubtype())
1105  {
1106  case 0:
1107  $a_xml_writer->xmlEndTag("response_single");
1108  break;
1109  case 1:
1110  $a_xml_writer->xmlEndTag("response_multiple");
1111  break;
1112  }
1113  }
1114  if (strlen($this->getNeutralColumn()))
1115  {
1116  $attrs = array(
1117  "id" => $this->getColumnCount(),
1118  "label" => "neutral"
1119  );
1120  switch ($this->getSubtype())
1121  {
1122  case 0:
1123  $a_xml_writer->xmlStartTag("response_single", $attrs);
1124  break;
1125  case 1:
1126  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
1127  break;
1128  }
1129  $this->addMaterialTag($a_xml_writer, $this->getNeutralColumn());
1130  switch ($this->getSubtype())
1131  {
1132  case 0:
1133  $a_xml_writer->xmlEndTag("response_single");
1134  break;
1135  case 1:
1136  $a_xml_writer->xmlEndTag("response_multiple");
1137  break;
1138  }
1139  }
1140 
1141  $a_xml_writer->xmlEndTag("responses");
1142  $a_xml_writer->xmlEndTag("matrix");
1143 
1144  if (count($this->material))
1145  {
1146  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
1147  {
1148  $attrs = array(
1149  "label" => $this->material["title"]
1150  );
1151  $a_xml_writer->xmlStartTag("material", $attrs);
1152  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
1153  if (strcmp($matches[1], "") != 0)
1154  {
1155  $intlink = $this->material["internal_link"];
1156  }
1157  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
1158  $a_xml_writer->xmlEndTag("material");
1159  }
1160  }
1161 
1162  $a_xml_writer->xmlStartTag("metadata");
1163  $a_xml_writer->xmlStartTag("metadatafield");
1164  $a_xml_writer->xmlElement("fieldlabel", NULL, "column_separators");
1165  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getColumnSeparators());
1166  $a_xml_writer->xmlEndTag("metadatafield");
1167 
1168  $a_xml_writer->xmlStartTag("metadatafield");
1169  $a_xml_writer->xmlElement("fieldlabel", NULL, "row_separators");
1170  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getRowSeparators());
1171  $a_xml_writer->xmlEndTag("metadatafield");
1172 
1173  $a_xml_writer->xmlStartTag("metadatafield");
1174  $a_xml_writer->xmlElement("fieldlabel", NULL, "neutral_column_separator");
1175  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getNeutralColumnSeparator());
1176  $a_xml_writer->xmlEndTag("metadatafield");
1177 
1178  $a_xml_writer->xmlStartTag("metadatafield");
1179  $a_xml_writer->xmlElement("fieldlabel", NULL, "layout");
1180  $a_xml_writer->xmlElement("fieldentry", NULL, serialize($this->getLayout()));
1181  $a_xml_writer->xmlEndTag("metadatafield");
1182 
1183  $a_xml_writer->xmlEndTag("metadata");
1184 
1185  $a_xml_writer->xmlEndTag("question");
1186  }
1187 
1188  function syncWithOriginal()
1189  {
1190  global $ilDB;
1191  if ($this->original_id)
1192  {
1193  $complete = 0;
1194  if ($this->isComplete())
1195  {
1196  $complete = 1;
1197  }
1198  $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
1199  $ilDB->quote($this->title . ""),
1200  $ilDB->quote($this->description . ""),
1201  $ilDB->quote($this->author . ""),
1202  $ilDB->quote($this->questiontext . ""),
1203  $ilDB->quote(sprintf("%d", $this->obligatory) . ""),
1204  $ilDB->quote($complete . ""),
1205  $ilDB->quote($this->original_id . "")
1206  );
1207  $result = $ilDB->query($query);
1208  $query = sprintf("UPDATE survey_question_matrix SET row_separators = %s, column_separators = %s, neutral_column_separator = %s, bipolar_adjective1 = %s, bipolar_adjective2 = %s WHERE question_fi = %s",
1209  $ilDB->quote($this->getRowSeparators() . ""),
1210  $ilDB->quote($this->getColumnSeparators() . ""),
1211  $ilDB->quote($this->getNeutralColumnSeparator() . ""),
1212  $ilDB->quote($this->getBipolarAdjective(0) . ""),
1213  $ilDB->quote($this->getBipolarAdjective(1) . ""),
1214  $ilDB->quote($this->original_id . "")
1215  );
1216  $result = $ilDB->query($query);
1217  if (PEAR::isError($result))
1218  {
1219  global $ilias;
1220  $ilias->raiseError($result->getMessage());
1221  }
1222  else
1223  {
1224  // sync columns
1225  $this->saveColumnsToDb($this->original_id);
1226  // sync rows
1227  $this->saveRowsToDb($this->original_id);
1228  }
1229  }
1231  }
1232 
1242  function addStandardNumbers($lower_limit, $upper_limit)
1243  {
1244  for ($i = $lower_limit; $i <= $upper_limit; $i++)
1245  {
1246  $this->addColumn($i);
1247  }
1248  }
1249 
1259  function savePhrase($phrases, $title)
1260  {
1261  global $ilUser;
1262  global $ilDB;
1263 
1264  $query = sprintf("INSERT INTO survey_phrase (phrase_id, title, defaultvalue, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
1265  $ilDB->quote($title . ""),
1266  $ilDB->quote("1"),
1267  $ilDB->quote($ilUser->id . "")
1268  );
1269  $result = $ilDB->query($query);
1270  $phrase_id = $ilDB->getLastInsertId();
1271 
1272  $counter = 1;
1273  foreach ($phrases as $column_index)
1274  {
1275  $column = $this->getColumn($column_index);
1276  $query = sprintf("INSERT INTO survey_category (category_id, title, neutral, defaultvalue, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
1277  $ilDB->quote($column . ""),
1278  $ilDB->quote("0"),
1279  $ilDB->quote("1"),
1280  $ilDB->quote($ilUser->getId() . "")
1281  );
1282  $result = $ilDB->query($query);
1283  $column_id = $ilDB->getLastInsertId();
1284  $query = sprintf("INSERT INTO survey_phrase_category (phrase_category_id, phrase_fi, category_fi, sequence) VALUES (NULL, %s, %s, %s)",
1285  $ilDB->quote($phrase_id . ""),
1286  $ilDB->quote($column_id . ""),
1287  $ilDB->quote($counter . "")
1288  );
1289  $result = $ilDB->query($query);
1290  $counter++;
1291  }
1292  }
1293 
1302  function getQuestionType()
1303  {
1304  return "SurveyMatrixQuestion";
1305  }
1306 
1316  {
1317  return "survey_question_matrix";
1318  }
1319 
1328  function &getWorkingDataFromUserInput($post_data)
1329  {
1330  $data = array();
1331  foreach ($post_data as $key => $value)
1332  {
1333  switch ($this->getSubtype())
1334  {
1335  case 0:
1336  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1337  {
1338  if (is_array($value))
1339  {
1340  foreach ($value as $val)
1341  {
1342  array_push($data, array("value" => $val, "row" => $matches[1]));
1343  }
1344  }
1345  else
1346  {
1347  array_push($data, array("value" => $value, "row" => $matches[1]));
1348  }
1349  }
1350  break;
1351  case 1:
1352  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1353  {
1354  if (is_array($value))
1355  {
1356  foreach ($value as $val)
1357  {
1358  array_push($data, array("value" => $val, "row" => $matches[1]));
1359  }
1360  }
1361  else
1362  {
1363  array_push($data, array("value" => $value, "row" => $matches[1]));
1364  }
1365  }
1366  break;
1367  }
1368  }
1369  return $data;
1370  }
1371 
1384  function checkUserInput($post_data, $survey_id)
1385  {
1386  if (!$this->getObligatory($survey_id)) return "";
1387  switch ($this->getSubtype())
1388  {
1389  case 0:
1390  $counter = 0;
1391  foreach ($post_data as $key => $value)
1392  {
1393  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1394  {
1395  $counter++;
1396  }
1397  }
1398  if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_radio_button_not_checked");
1399  break;
1400  case 1:
1401  $counter = 0;
1402  foreach ($post_data as $key => $value)
1403  {
1404  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1405  {
1406  $counter++;
1407  if ((!is_array($value)) || (count($value) < 1))
1408  {
1409  return $this->lng->txt("matrix_question_checkbox_not_checked");
1410  }
1411  }
1412  }
1413  if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_checkbox_not_checked");
1414  break;
1415  }
1416  return "";
1417  }
1418 
1424  public function saveRandomData($active_id)
1425  {
1426  global $ilDB;
1427  $columncount = ($this->hasNeutralColumn()) ? $this->getColumnCount()+1 : $this->getColumnCount();
1428  for ($row = 0; $row < $this->getRowCount(); $row++)
1429  {
1430  if ($this->getSubType() == 1)
1431  {
1432  // multiple responses
1433  for ($i = 0; $i < $columncount; $i++)
1434  {
1435  if (rand(0,1))
1436  {
1437  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL, %s, NULL)",
1438  $ilDB->quote($this->getId() . ""),
1439  $ilDB->quote($active_id . ""),
1440  $ilDB->quote($i . ""),
1441  $ilDB->quote($row . "")
1442  );
1443  $result = $ilDB->query($query);
1444  }
1445  }
1446  }
1447  else
1448  {
1449  // single responses
1450  $category = rand(0, $columncount-1);
1451  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL, %s, NULL)",
1452  $ilDB->quote($this->getId() . ""),
1453  $ilDB->quote($active_id . ""),
1454  $ilDB->quote($category . ""),
1455  $ilDB->quote($row . "")
1456  );
1457  $result = $ilDB->query($query);
1458  }
1459  }
1460  }
1461 
1462  function saveUserInput($post_data, $active_id)
1463  {
1464  global $ilDB;
1465 
1466  switch ($this->getSubtype())
1467  {
1468  case 0:
1469  foreach ($post_data as $key => $value)
1470  {
1471  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1472  {
1473  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
1474  $ilDB->quote($this->getId() . ""),
1475  $ilDB->quote($active_id . ""),
1476  $ilDB->quote($value . ""),
1477  "NULL",
1478  $ilDB->quote($matches[1] . "")
1479  );
1480  $result = $ilDB->query($query);
1481  }
1482  }
1483  break;
1484  case 1:
1485  foreach ($post_data as $key => $value)
1486  {
1487  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1488  {
1489  foreach ($value as $checked)
1490  {
1491  if (strlen($checked))
1492  {
1493  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
1494  $ilDB->quote($this->getId() . ""),
1495  $ilDB->quote($active_id . ""),
1496  $ilDB->quote($checked . ""),
1497  "NULL",
1498  $ilDB->quote($matches[1] . "")
1499  );
1500  $result = $ilDB->query($query);
1501  }
1502  }
1503  }
1504  }
1505  break;
1506  }
1507  }
1508 
1517  function deleteAdditionalTableData($question_id)
1518  {
1519  parent::deleteAdditionalTableData($question_id);
1520 
1521  global $ilDB;
1522  $query = sprintf("DELETE FROM survey_question_matrix_rows WHERE question_fi = %s",
1523  $ilDB->quote($question_id . "")
1524  );
1525  $result = $ilDB->query($query);
1526  }
1527 
1538  {
1539  global $ilDB;
1540 
1541  $query = sprintf("SELECT DISTINCT(CONCAT(survey_answer.active_fi,survey_answer.question_fi)) AS participants FROM survey_answer, survey_finished WHERE survey_answer.question_fi = %s AND survey_finished.survey_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
1542  $ilDB->quote($this->getId() . ""),
1543  $ilDB->quote($survey_id . "")
1544  );
1545  $result = $ilDB->query($query);
1546  return $result->numRows();
1547  }
1548 
1559  function &getCumulatedResultsForRow($rowindex, $survey_id, $nr_of_users)
1560  {
1561  global $ilDB;
1562 
1563  $question_id = $this->getId();
1564 
1565  $result_array = array();
1566  $cumulated = array();
1567 
1568  $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_answer.question_fi = %s AND survey_finished.survey_fi = %s AND survey_answer.row = %s AND survey_finished.finished_id = survey_answer.active_fi",
1569  $ilDB->quote($question_id . ""),
1570  $ilDB->quote($survey_id . ""),
1571  $ilDB->quote($rowindex . "")
1572  );
1573  $result = $ilDB->query($query);
1574 
1575  switch ($this->getSubtype())
1576  {
1577  case 0:
1578  case 1:
1579  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1580  {
1581  $cumulated[$row->value]++;
1582  }
1583  asort($cumulated, SORT_NUMERIC);
1584  end($cumulated);
1585  break;
1586  }
1587  $numrows = $result->numRows();
1588  $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
1589  $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
1590 
1591  $prefix = "";
1592  if (strcmp(key($cumulated), "") != 0)
1593  {
1594  $prefix = (key($cumulated)+1) . " - ";
1595  }
1596  $cat = $this->getColumn(key($cumulated));
1597  $result_array["MODE"] = $prefix . $cat;
1598  $result_array["MODE_VALUE"] = key($cumulated)+1;
1599  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
1600  for ($key = 0; $key < $this->getColumnCount(); $key++)
1601  {
1602  $percentage = 0;
1603  if ($numrows > 0)
1604  {
1605  $percentage = (float)((int)$cumulated[$key]/$numrows);
1606  }
1607  $cat = $this->getColumn($key);
1608  $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
1609  }
1610  if ($this->hasNeutralColumn())
1611  {
1612  $percentage = 0;
1613  if ($numrows > 0)
1614  {
1615  $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
1616  }
1617  $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
1618  }
1619  ksort($cumulated, SORT_NUMERIC);
1620  $median = array();
1621  $total = 0;
1622  foreach ($cumulated as $value => $key)
1623  {
1624  $total += $key;
1625  for ($i = 0; $i < $key; $i++)
1626  {
1627  array_push($median, $value+1);
1628  }
1629  }
1630  if ($total > 0)
1631  {
1632  if (($total % 2) == 0)
1633  {
1634  $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
1635  if (round($median_value) != $median_value)
1636  {
1637  $cat = $this->getColumn((int)floor($median_value)-1);
1638  $cat2 = $this->getColumn((int)ceil($median_value)-1);
1639  $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
1640  }
1641  }
1642  else
1643  {
1644  $median_value = $median[(($total+1)/2)-1];
1645  }
1646  }
1647  else
1648  {
1649  $median_value = "";
1650  }
1651  $result_array["ARITHMETIC_MEAN"] = "";
1652  $result_array["MEDIAN"] = $median_value;
1653  $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
1654  $result_array["ROW"] = $this->getRow($rowindex);
1655  return $result_array;
1656  }
1657 
1667  function &getCumulatedResults($survey_id, $nr_of_users)
1668  {
1669  global $ilDB;
1670 
1671  $question_id = $this->getId();
1672 
1673  $result_array = array();
1674  $cumulated = array();
1675 
1676  $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_answer.question_fi = %s AND survey_finished.survey_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
1677  $ilDB->quote($question_id),
1678  $ilDB->quote($survey_id)
1679  );
1680  $result = $ilDB->query($query);
1681 
1682  switch ($this->getSubtype())
1683  {
1684  case 0:
1685  case 1:
1686  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1687  {
1688  $cumulated[$row->value]++;
1689  }
1690  asort($cumulated, SORT_NUMERIC);
1691  end($cumulated);
1692  break;
1693  }
1694  $numrows = $result->numRows();
1695  $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
1696  $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
1697 
1698  $prefix = "";
1699  if (strcmp(key($cumulated), "") != 0)
1700  {
1701  $prefix = (key($cumulated)+1) . " - ";
1702  }
1703  $cat = $this->getColumn(key($cumulated));
1704  $result_array["MODE"] = $prefix . $cat;
1705  $result_array["MODE_VALUE"] = key($cumulated)+1;
1706  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
1707  for ($key = 0; $key < $this->getColumnCount(); $key++)
1708  {
1709  $percentage = 0;
1710  if ($numrows > 0)
1711  {
1712  $percentage = (float)((int)$cumulated[$key]/$numrows);
1713  }
1714  $cat = $this->getColumn($key);
1715  $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
1716  }
1717  if ($this->hasNeutralColumn())
1718  {
1719  $percentage = 0;
1720  if ($numrows > 0)
1721  {
1722  $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
1723  }
1724  $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
1725  }
1726  ksort($cumulated, SORT_NUMERIC);
1727  $median = array();
1728  $total = 0;
1729  foreach ($cumulated as $value => $key)
1730  {
1731  $total += $key;
1732  for ($i = 0; $i < $key; $i++)
1733  {
1734  array_push($median, $value+1);
1735  }
1736  }
1737  if ($total > 0)
1738  {
1739  if (($total % 2) == 0)
1740  {
1741  $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
1742  if (round($median_value) != $median_value)
1743  {
1744  $cat = $this->getColumn((int)floor($median_value)-1);
1745  $cat2 = $this->getColumn((int)ceil($median_value)-1);
1746  $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
1747  }
1748  }
1749  else
1750  {
1751  $median_value = $median[(($total+1)/2)-1];
1752  }
1753  }
1754  else
1755  {
1756  $median_value = "";
1757  }
1758  $result_array["ARITHMETIC_MEAN"] = "";
1759  $result_array["MEDIAN"] = $median_value;
1760  $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
1761 
1762  $cumulated_results = array();
1763  $cumulated_results["TOTAL"] = $result_array;
1764  for ($i = 0; $i < $this->getRowCount(); $i++)
1765  {
1766  $rowresult =& $this->getCumulatedResultsForRow($i, $survey_id, $nr_of_users);
1767  $cumulated_results[$i] = $rowresult;
1768  }
1769  return $cumulated_results;
1770  }
1771 
1785  function setExportCumulatedXLS(&$worksheet, &$format_title, &$format_bold, &$eval_data, $row)
1786  {
1787  include_once ("./classes/class.ilExcelUtils.php");
1788  $worksheet->writeString($row, 0, ilExcelUtils::_convert_text($this->getTitle()));
1789  $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
1790  $worksheet->writeString($row, 2, ilExcelUtils::_convert_text($this->lng->txt($eval_data["TOTAL"]["QUESTION_TYPE"])));
1791  $worksheet->write($row, 3, $eval_data["TOTAL"]["USERS_ANSWERED"]);
1792  $worksheet->write($row, 4, $eval_data["TOTAL"]["USERS_SKIPPED"]);
1793  $worksheet->write($row, 5, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_VALUE"]));
1794  $worksheet->write($row, 6, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE"]));
1795  $worksheet->write($row, 7, $eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]);
1796  $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
1797  $worksheet->write($row, 9, $eval_data["TOTAL"]["ARITHMETIC_MEAN"]);
1798  $row++;
1799  foreach ($eval_data as $evalkey => $evalvalue)
1800  {
1801  if (is_numeric($evalkey))
1802  {
1803  $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
1804  $worksheet->write($row, 3, $evalvalue["USERS_ANSWERED"]);
1805  $worksheet->write($row, 4, $evalvalue["USERS_SKIPPED"]);
1806  $worksheet->write($row, 5, ilExcelUtils::_convert_text($evalvalue["MODE_VALUE"]));
1807  $worksheet->write($row, 6, ilExcelUtils::_convert_text($evalvalue["MODE"]));
1808  $worksheet->write($row, 7, $evalvalue["MODE_NR_OF_SELECTIONS"]);
1809  $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
1810  $worksheet->write($row, 9, $evalvalue["ARITHMETIC_MEAN"]);
1811  $row++;
1812  }
1813  }
1814  return $row;
1815  }
1816 
1830  function &setExportCumulatedCVS(&$eval_data)
1831  {
1832  $result = array();
1833  foreach ($eval_data as $evalkey => $evalvalue)
1834  {
1835  $csvrow = array();
1836  if (is_numeric($evalkey))
1837  {
1838  array_push($csvrow, "");
1839  array_push($csvrow, $evalvalue["ROW"]);
1840  array_push($csvrow, "");
1841  }
1842  else
1843  {
1844  array_push($csvrow, $this->getTitle());
1845  array_push($csvrow, $this->getQuestiontext());
1846  array_push($csvrow, $this->lng->txt($evalvalue["QUESTION_TYPE"]));
1847  }
1848  array_push($csvrow, $evalvalue["USERS_ANSWERED"]);
1849  array_push($csvrow, $evalvalue["USERS_SKIPPED"]);
1850  array_push($csvrow, $evalvalue["MODE"]);
1851  array_push($csvrow, $evalvalue["MODE_NR_OF_SELECTIONS"]);
1852  array_push($csvrow, $evalvalue["MEDIAN"]);
1853  array_push($csvrow, $evalvalue["ARITHMETIC_MEAN"]);
1854  array_push($result, $csvrow);
1855  }
1856  return $result;
1857  }
1858 
1870  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
1871  {
1872  include_once ("./classes/class.ilExcelUtils.php");
1873  $worksheet =& $workbook->addWorksheet();
1874  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
1875  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
1876  $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
1877  $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
1878  $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
1879  $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
1880  $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
1881  $worksheet->write(3, 1, $eval_data["TOTAL"]["USERS_ANSWERED"]);
1882  $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
1883  $worksheet->write(4, 1, $eval_data["TOTAL"]["USERS_SKIPPED"]);
1884  $rowcounter = 5;
1885 
1886  preg_match("/(.*?)\s+-\s+(.*)/", $eval_data["TOTAL"]["MODE"], $matches);
1887  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
1888  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
1889  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
1890  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
1891  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
1892  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]));
1893  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
1894  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
1895  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
1896  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
1897  $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
1898  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
1899  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
1900 
1901  foreach ($eval_data["TOTAL"]["variables"] as $key => $value)
1902  {
1903  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
1904  $worksheet->write($rowcounter, 2, $key+1);
1905  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
1906  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
1907  }
1908 
1909  foreach ($eval_data as $evalkey => $evalvalue)
1910  {
1911  if (is_numeric($evalkey))
1912  {
1913  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("row")), $format_bold);
1914  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
1915  $worksheet->writeString($rowcounter + 1, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
1916  $worksheet->write($rowcounter + 1, 1, $evalvalue["USERS_ANSWERED"]);
1917  $worksheet->writeString($rowcounter + 2, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
1918  $worksheet->write($rowcounter + 2, 1, $evalvalue["USERS_SKIPPED"]);
1919  $rowcounter = $rowcounter + 3;
1920 
1921  preg_match("/(.*?)\s+-\s+(.*)/", $evalvalue["MODE"], $matches);
1922  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
1923  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
1924  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
1925  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
1926  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
1927  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($evalvalue["MODE_NR_OF_SELECTIONS"]));
1928  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
1929  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
1930  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
1931  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
1932  $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
1933  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
1934  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
1935 
1936  foreach ($evalvalue["variables"] as $key => $value)
1937  {
1938  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
1939  $worksheet->write($rowcounter, 2, $key+1);
1940  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
1941  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
1942  }
1943  }
1944  }
1945  // out compressed 2D-Matrix
1946  $format_center =& $workbook->addFormat();
1947  $format_center->setColor('black');
1948  $format_center->setAlign('center');
1949 
1950  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("overview")), $format_bold);
1951  // title row with variables
1952  $rowcounter++;
1953  $counter = 0;
1954  $worksheet->write($rowcounter, $counter, "", $format_title);
1955  foreach ($eval_data["TOTAL"]["variables"] as $variable)
1956  {
1957  $worksheet->write($rowcounter, 1+$counter, ilExcelUtils::_convert_text($variable["title"]), $format_title);
1958  $counter++;
1959  }
1960  $rowcounter++;
1961  // rows with variable values
1962  foreach ($eval_data as $index => $data)
1963  {
1964  if (is_numeric($index))
1965  {
1966  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($data["ROW"]), $format_title);
1967  $counter = 1;
1968  foreach ($data["variables"] as $vardata)
1969  {
1970  $worksheet->write($rowcounter, $counter, $vardata["selected"], $format_center);
1971  $counter++;
1972  }
1973  $rowcounter++;
1974  }
1975  }
1976  }
1977 
1987  {
1989  for ($i = 0; $i < $this->getRowCount(); $i++)
1990  {
1991  array_push($a_array, $this->getRow($i));
1992  switch ($this->getSubtype())
1993  {
1994  case 0:
1995  break;
1996  case 1:
1997  for ($index = 0; $index < $this->getColumnCount(); $index++)
1998  {
1999  $col = $this->getColumn($index);
2000  array_push($a_array, ($index+1) . " - $col");
2001  }
2002  if (strlen($this->getNeutralColumn()))
2003  {
2004  array_push($a_array, ($this->getColumnCount()+1) . " - " . $this->getNeutralColumn());
2005  }
2006  break;
2007  }
2008  }
2009  }
2010 
2020  function addUserSpecificResultsData(&$a_array, &$resultset)
2021  {
2022  if (count($resultset["answers"][$this->getId()]))
2023  {
2024  array_push($a_array, "");
2025  switch ($this->getSubtype())
2026  {
2027  case 0:
2028  for ($i = 0; $i < $this->getRowCount(); $i++)
2029  {
2030  $checked = FALSE;
2031  foreach ($resultset["answers"][$this->getId()] as $result)
2032  {
2033  if ($result["row"] == $i)
2034  {
2035  $checked = TRUE;
2036  array_push($a_array, $result["value"] + 1);
2037  }
2038  }
2039  if (!$checked)
2040  {
2041  array_push($a_array, $this->lng->txt("skipped"));
2042  }
2043  }
2044  break;
2045  case 1:
2046  for ($i = 0; $i < $this->getRowCount(); $i++)
2047  {
2048  $checked = FALSE;
2049  $checked_values = array();
2050  foreach ($resultset["answers"][$this->getId()] as $result)
2051  {
2052  if ($result["row"] == $i)
2053  {
2054  $checked = TRUE;
2055  array_push($checked_values, $result["value"] + 1);
2056  }
2057  }
2058  if (!$checked)
2059  {
2060  array_push($a_array, $this->lng->txt("skipped"));
2061  }
2062  else
2063  {
2064  array_push($a_array, "");
2065  }
2066  for ($index = 0; $index < $this->getColumnCount(); $index++)
2067  {
2068  if (!$checked)
2069  {
2070  array_push($a_array, "");
2071  }
2072  else
2073  {
2074  if (in_array($index+1, $checked_values))
2075  {
2076  array_push($a_array, 1);
2077  }
2078  else
2079  {
2080  array_push($a_array, 0);
2081  }
2082  }
2083  }
2084  if (strlen($this->getNeutralColumn()))
2085  {
2086  if (!$checked)
2087  {
2088  array_push($a_array, "");
2089  }
2090  else
2091  {
2092  if (in_array($this->getColumnCount()+1, $checked_values))
2093  {
2094  array_push($a_array, 1);
2095  }
2096  else
2097  {
2098  array_push($a_array, 0);
2099  }
2100  }
2101  }
2102  }
2103  break;
2104  }
2105  }
2106  else
2107  {
2108  array_push($a_array, $this->lng->txt("skipped"));
2109  for ($i = 0; $i < $this->getRowCount(); $i++)
2110  {
2111  array_push($a_array, "");
2112  switch ($this->getSubtype())
2113  {
2114  case 0:
2115  break;
2116  case 1:
2117  for ($index = 0; $index < $this->getColumnCount(); $index++)
2118  {
2119  array_push($a_array, "");
2120  }
2121  if (strlen($this->getNeutralColumn()))
2122  {
2123  array_push($a_array, "");
2124  }
2125  break;
2126  }
2127  }
2128  }
2129  }
2130 
2141  {
2142  global $ilDB;
2143 
2144  $answers = array();
2145 
2146  $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_finished.survey_fi = %s AND survey_answer.question_fi = %s AND survey_finished.finished_id = survey_answer.active_fi ORDER BY row, value",
2147  $ilDB->quote($survey_id),
2148  $ilDB->quote($this->getId())
2149  );
2150  $result = $ilDB->query($query);
2151  $results = array();
2152  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
2153  {
2154  $column = $this->getColumn($row["value"]);
2155  if (!is_array($answers[$row["active_fi"]])) $answers[$row["active_fi"]] = array();
2156  array_push($answers[$row["active_fi"]], $this->getRow($row["row"]) . ": " . ($row["value"] + 1) . " - " . $column);
2157  }
2158  foreach ($answers as $key => $value)
2159  {
2160  $answers[$key] = implode("<br />", $value);
2161  }
2162  return $answers;
2163  }
2164 
2173  function getSubtype()
2174  {
2175  return $this->subtype;
2176  }
2177 
2186  function setSubtype($a_subtype = 0)
2187  {
2188  switch ($a_subtype)
2189  {
2190  case 1:
2191  case 2:
2192  case 3:
2193  case 4:
2194  case 5:
2195  case 6:
2196  $this->subtype = $a_subtype;
2197  break;
2198  case 0:
2199  default:
2200  $this->subtype = 0;
2201  break;
2202  }
2203  }
2204 
2213  function setColumnSeparators($enable = 0)
2214  {
2215  switch ($enable)
2216  {
2217  case 1:
2218  $this->columnSeparators = 1;
2219  break;
2220  case 0:
2221  default:
2222  $this->columnSeparators = 0;
2223  break;
2224  }
2225  }
2226 
2236  {
2237  return $this->columnSeparators;
2238  }
2239 
2248  function setRowSeparators($enable = 0)
2249  {
2250  switch ($enable)
2251  {
2252  case 1:
2253  $this->rowSeparators = 1;
2254  break;
2255  case 0:
2256  default:
2257  $this->rowSeparators = 0;
2258  break;
2259  }
2260  }
2261 
2270  function getRowSeparators()
2271  {
2272  return $this->rowSeparators;
2273  }
2274 
2283  function setNeutralColumnSeparator($enable = 0)
2284  {
2285  switch ($enable)
2286  {
2287  case 1:
2288  $this->neutralColumnSeparator = 1;
2289  break;
2290  case 0:
2291  default:
2292  $this->neutralColumnSeparator = 0;
2293  break;
2294  }
2295  }
2296 
2306  {
2308  }
2309 
2320  function importAdditionalMetadata($a_meta)
2321  {
2322  foreach ($a_meta as $key => $value)
2323  {
2324  switch ($value["label"])
2325  {
2326  case "column_separators":
2327  $this->setColumnSeparators($value["entry"]);
2328  break;
2329  case "row_separators":
2330  $this->setRowSeparators($value["entry"]);
2331  break;
2332  case "layout":
2333  $this->setLayout($value["entry"]);
2334  break;
2335  case "neutral_column_separator":
2336  $this->setNeutralColumnSeparator($value["entry"]);
2337  break;
2338  }
2339  }
2340  }
2341 
2350  function importAdjectives($a_data)
2351  {
2352  $i = 0;
2353  foreach ($a_data as $adjective)
2354  {
2355  if (is_numeric($adjective["label"]))
2356  {
2357  $this->setBipolarAdjective($adjective["label"], $adjective["text"]);
2358  }
2359  else
2360  {
2361  $this->setBipolarAdjective($i, $adjective["text"]);
2362  }
2363  $i++;
2364  }
2365  }
2366 
2375  function importMatrix($a_data)
2376  {
2377  foreach ($a_data as $row)
2378  {
2379  $this->addRow($row);
2380  }
2381  }
2382 
2391  function importResponses($a_data)
2392  {
2393  foreach ($a_data as $id => $data)
2394  {
2395  $column = "";
2396  foreach ($data["material"] as $material)
2397  {
2398  $column .= $material["text"];
2399  }
2400  if (strcmp($data["label"], "neutral") == 0)
2401  {
2402  $this->setNeutralColumn($column);
2403  }
2404  else
2405  {
2406  $this->addColumn($column);
2407  }
2408  }
2409  }
2410 
2420  {
2421  return FALSE;
2422  }
2423 
2434  {
2435  return $value;
2436  }
2437 
2448  function outChart($survey_id, $type = "")
2449  {
2450  if (count($this->cumulated) == 0)
2451  {
2452  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
2454  $this->cumulated =& $this->getCumulatedResults($survey_id, $nr_of_users);
2455  }
2456  global $ilLog;
2457  if (is_numeric($type))
2458  {
2459  foreach ($this->cumulated[$type]["variables"] as $key => $value)
2460  {
2461  foreach ($value as $key2 => $value2)
2462  {
2463  $this->cumulated["variables"][$key][$key2] = utf8_decode($value2);
2464  }
2465  }
2466  $title = preg_replace("/<[^>]+?>/ims", "", $this->getRow($type));
2467  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyChart.php";
2468  $b1 = new SurveyChart("bars", 400, 250, utf8_decode($title),utf8_decode($this->lng->txt("answers")), utf8_decode($this->lng->txt("users_answered")), $this->cumulated[$type]["variables"]);
2469  }
2470  }
2471 
2483  function saveLayout($percent_row, $percent_columns, $percent_bipolar_adjective1 = "", $percent_bipolar_adjective2 = "", $percent_neutral)
2484  {
2485  global $ilDB;
2486 
2487  $layout = array(
2488  "percent_row" => $percent_row,
2489  "percent_columns" => $percent_columns,
2490  "percent_bipolar_adjective1" => $percent_bipolar_adjective1,
2491  "percent_bipolar_adjective2" => $percent_bipolar_adjective2,
2492  "percent_neutral" => $percent_neutral
2493  );
2494  $query = sprintf("UPDATE survey_question_matrix SET layout = %s WHERE question_fi = %s",
2495  $ilDB->quote(serialize($layout) . ""),
2496  $ilDB->quote($this->getId() . "")
2497  );
2498  $ilDB->query($query);
2499  }
2500 
2501  function getLayout()
2502  {
2503  if (!is_array($this->layout) || count($this->layout) == 0)
2504  {
2505  if ($this->hasBipolarAdjectives() && $this->hasNeutralColumn())
2506  {
2507  $this->layout = array(
2508  "percent_row" => 30,
2509  "percent_columns" => 40,
2510  "percent_bipolar_adjective1" => 10,
2511  "percent_bipolar_adjective2" => 10,
2512  "percent_neutral" => 10
2513  );
2514  }
2515  elseif ($this->hasBipolarAdjectives())
2516  {
2517  $this->layout = array(
2518  "percent_row" => 30,
2519  "percent_columns" => 50,
2520  "percent_bipolar_adjective1" => 10,
2521  "percent_bipolar_adjective2" => 10,
2522  "percent_neutral" => 0
2523  );
2524  }
2525  elseif ($this->hasNeutralColumn())
2526  {
2527  $this->layout = array(
2528  "percent_row" => 30,
2529  "percent_columns" => 50,
2530  "percent_bipolar_adjective1" => 0,
2531  "percent_bipolar_adjective2" => 0,
2532  "percent_neutral" => 20
2533  );
2534  }
2535  else
2536  {
2537  $this->layout = array(
2538  "percent_row" => 30,
2539  "percent_columns" => 70,
2540  "percent_bipolar_adjective1" => 0,
2541  "percent_bipolar_adjective2" => 0,
2542  "percent_neutral" => 0
2543  );
2544  }
2545  }
2546  return $this->layout;
2547  }
2548 
2549  function setLayout($layout)
2550  {
2551  if (is_array($layout))
2552  {
2553  $this->layout = $layout;
2554  }
2555  else
2556  {
2557  $this->layout = unserialize($layout);
2558  }
2559  }
2560 
2569  {
2570  if ((strlen($this->getBipolarAdjective(0))) && (strlen($this->getBipolarAdjective(1))))
2571  {
2572  return TRUE;
2573  }
2574  else
2575  {
2576  return FALSE;
2577  }
2578  }
2579 
2587  function hasNeutralColumn()
2588  {
2589  if (strlen($this->getNeutralColumn()))
2590  {
2591  return TRUE;
2592  }
2593  else
2594  {
2595  return FALSE;
2596  }
2597  }
2598 
2604  function setColumnPlaceholders($a_value = FALSE)
2605  {
2606  if ($a_value)
2607  {
2608  $this->columnPlaceholders = TRUE;
2609  }
2610  else
2611  {
2612  $this->columnPlaceholders = FALSE;
2613  }
2614  }
2615 
2622  {
2624  }
2625 
2631  function setLegend($a_value = FALSE)
2632  {
2633  if ($a_value)
2634  {
2635  $this->legend = TRUE;
2636  }
2637  else
2638  {
2639  $this->legend = FALSE;
2640  }
2641  }
2642 
2648  function getLegend()
2649  {
2650  return $this->legend;
2651  }
2652 
2653  function setSingleLineRowCaption($a_value = FALSE)
2654  {
2655  if ($a_value)
2656  {
2657  $this->singleLineRowCaption = TRUE;
2658  }
2659  else
2660  {
2661  $this->singleLineRowCaption = FALSE;
2662  }
2663  }
2664 
2666  {
2668  }
2669 
2670  function setRepeatColumnHeader($a_value = FALSE)
2671  {
2672  if ($a_value)
2673  {
2674  $this->repeatColumnHeader = TRUE;
2675  }
2676  else
2677  {
2678  $this->repeatColumnHeader = FALSE;
2679  }
2680  }
2681 
2683  {
2685  }
2686 
2687  function setColumnHeaderPosition($a_value)
2688  {
2689  $this->columnHeaderPosition = $a_value;
2690  }
2691 
2693  {
2695  }
2696 
2697  function setRandomRows($a_value = FALSE)
2698  {
2699  if ($a_value)
2700  {
2701  $this->randomRows = TRUE;
2702  }
2703  else
2704  {
2705  $this->randomRows = FALSE;
2706  }
2707  }
2708 
2709  function getRandomRows()
2710  {
2711  return $this->randomRows;
2712  }
2713 
2714  function setColumnOrder($a_value)
2715  {
2716  $this->columnOrder = $a_value;
2717  }
2718 
2719  function getColumnOrder()
2720  {
2721  return $this->columnOrder;
2722  }
2723 
2724  function setColumnImages($a_value = FALSE)
2725  {
2726  if ($a_value)
2727  {
2728  $this->columnImages = TRUE;
2729  }
2730  else
2731  {
2732  $this->columnImages = FALSE;
2733  }
2734  }
2735 
2736  function getColumnImages()
2737  {
2738  return $this->columnImages;
2739  }
2740 
2741  function setRowImages($a_value = FALSE)
2742  {
2743  if ($a_value)
2744  {
2745  $this->rowImages = TRUE;
2746  }
2747  else
2748  {
2749  $this->rowImages = FALSE;
2750  }
2751  }
2752 
2753  function getRowImages()
2754  {
2755  return $this->rowImages;
2756  }
2757 
2758 }
2759 ?>