ILIAS  Release_4_0_x_branch Revision 61816
 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 
37 {
43  var $columns;
44 
51 
57  var $rows;
58 
65 
72 
80 
88 
96 
97  /*
98  * Layout of the matrix question
99  *
100  * @var array
101  */
102  var $layout;
103 
104  /*
105  * Use placeholders for the column titles
106  *
107  * @var boolean
108  */
110 
111  /*
112  * Show a legend
113  *
114  * @var boolean
115  */
116  var $legend;
117 
119 
121 
123 
124  /*
125  * Use random order for rows
126  *
127  * @var boolean
128  */
130 
132 
134 
136 
137 
152  var $subtype;
153 
165  $title = "",
166  $description = "",
167  $author = "",
168  $questiontext = "",
169  $owner = -1
170  )
171  {
173  $this->subtype = 0;
174  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
175  $this->columns = new SurveyCategories();
176  $this->rows = array();
177  $this->neutralColumn = "";
178  $this->bipolar_adjective1 = "";
179  $this->bipolar_adjective2 = "";
180  $this->rowSeparators = 0;
181  $this->columnSeparators = 0;
182  $this->neutralColumnSeparator = 1;
183  }
184 
192  function getNeutralColumn()
193  {
194  return $this->neutralColumn;
195  }
196 
205  {
206  if (strlen($this->getNeutralColumn()))
207  {
208  return $this->getColumnCount();
209  }
210  else
211  {
212  return FALSE;
213  }
214  }
215 
223  function setNeutralColumn($a_text)
224  {
225  $this->neutralColumn = $a_text;
226  }
227 
235  function getColumnCount()
236  {
237  return $this->columns->getCategoryCount();
238  }
239 
248  function addColumnAtPosition($columnname, $position)
249  {
250  $this->columns->addCategoryAtPosition($columnname, $position);
251  }
252 
261  function addColumn($columnname)
262  {
263  $this->columns->addCategory($columnname);
264  }
265 
274  {
275  $this->columns->addCategoryArray($columns);
276  }
277 
285  function removeColumn($index)
286  {
287  $this->columns->removeCategory($index);
288  }
289 
297  function removeColumns($array)
298  {
299  $this->columns->removeCategories($array);
300  }
301 
310  {
311  $this->columns->removeCategoryWithName($name);
312  }
313 
317  public function getColumns()
318  {
319  return $this->columns;
320  }
321 
330  function getColumn($index)
331  {
332  return $this->columns->getCategory($index);
333  }
334 
343  {
344  return $this->columns->getCategoryIndex($name);
345  }
346 
347 
354  function flushColumns()
355  {
356  $this->columns->flushCategories();
357  }
358 
365  function getRowCount()
366  {
367  return count($this->rows);
368  }
369 
375  function addRow($a_text)
376  {
377  array_push($this->rows, $a_text);
378  }
379 
386  function addRowAtPosition($a_text, $a_position)
387  {
388  if (array_key_exists($a_position, $this->rows))
389  {
390  $head = array_slice($this->rows, 0, $a_position);
391  $tail = array_slice($this->rows, $a_position);
392  $this->rows = array_merge($head, array($a_text), $tail);
393  }
394  else
395  {
396  array_push($this->rows, $a_text);
397  }
398  }
399 
406  function flushRows()
407  {
408  $this->rows = array();
409  }
410 
417  function getRow($a_index)
418  {
419  if (array_key_exists($a_index, $this->rows))
420  {
421  return $this->rows[$a_index];
422  }
423  return "";
424  }
425 
426  function moveRowUp($index)
427  {
428  if ($index > 0)
429  {
430  $temp = $this->rows[$index-1];
431  $this->rows[$index - 1] = $this->rows[$index];
432  $this->rows[$index] = $temp;
433  }
434  }
435 
436  function moveRowDown($index)
437  {
438  if ($index < (count($this->rows)-1))
439  {
440  $temp = $this->rows[$index+1];
441  $this->rows[$index + 1] = $this->rows[$index];
442  $this->rows[$index] = $temp;
443  }
444  }
445 
453  function removeRows($array)
454  {
455  foreach ($array as $index)
456  {
457  unset($this->rows[$index]);
458  }
459  $this->rows = array_values($this->rows);
460  }
461 
467  public function removeRow($index)
468  {
469  unset($this->rows[$index]);
470  $this->rows = array_values($this->rows);
471  }
472 
480  function getBipolarAdjective($a_index)
481  {
482  switch ($a_index)
483  {
484  case 1:
485  return (strlen($this->bipolar_adjective2)) ? $this->bipolar_adjective2 : NULL;
486  break;
487  case 0:
488  default:
489  return (strlen($this->bipolar_adjective1)) ? $this->bipolar_adjective1 : NULL;
490  break;
491  }
492  return NULL;
493  }
494 
502  function setBipolarAdjective($a_index, $a_value)
503  {
504  switch ($a_index)
505  {
506  case 1:
507  $this->bipolar_adjective2 = $a_value;
508  break;
509  case 0:
510  default:
511  $this->bipolar_adjective1 = $a_value;
512  break;
513  }
514  }
515 
522  function addPhrase($phrase_id)
523  {
524  global $ilUser;
525  global $ilDB;
526 
527  $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 = %s OR svy_category.owner_fi = %s) ORDER BY svy_phrase_cat.sequence",
528  array('integer', 'integer', 'integer'),
529  array($phrase_id, 0, $ilUser->getId())
530  );
531  while ($row = $ilDB->fetchAssoc($result))
532  {
533  if (($row["defaultvalue"] == 1) && ($row["owner_fi"] == 0))
534  {
535  $this->addColumn($this->lng->txt($row["title"]));
536  }
537  else
538  {
539  $this->addColumn($row["title"]);
540  }
541  }
542  }
543 
552  {
553  global $ilDB;
554 
555  $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",
556  array('integer'),
557  array($id)
558  );
559  if ($result->numRows() == 1)
560  {
561  return $ilDB->fetchAssoc($result);
562  }
563  else
564  {
565  return array();
566  }
567  }
568 
575  function loadFromDb($id)
576  {
577  global $ilDB;
578  $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",
579  array('integer'),
580  array($id)
581  );
582  if ($result->numRows() == 1)
583  {
584  $data = $ilDB->fetchAssoc($result);
585  $this->setId($data["question_id"]);
586  $this->setTitle($data["title"]);
587  $this->setDescription($data["description"]);
588  $this->setObjId($data["obj_fi"]);
589  $this->setAuthor($data["author"]);
590  $this->setOwner($data["owner_fi"]);
591  include_once("./Services/RTE/classes/class.ilRTE.php");
592  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
593  $this->setObligatory($data["obligatory"]);
594  $this->setComplete($data["complete"]);
595  $this->setOriginalId($data["original_id"]);
596  $this->setSubtype($data["subtype"]);
597  $this->setRowSeparators($data["row_separators"]);
598  $this->setNeutralColumnSeparator($data["neutral_column_separator"]);
599  $this->setColumnSeparators($data["column_separators"]);
600  $this->setColumnPlaceholders($data["column_placeholders"]);
601  $this->setLegend($data["legend"]);
602  $this->setSingleLineRowCaption($data["singleline_row_caption"]);
603  $this->setRepeatColumnHeader($data["repeat_column_header"]);
604  $this->setColumnHeaderPosition($data["column_header_position"]);
605  $this->setRandomRows($data["random_rows"]);
606  $this->setColumnOrder($data["column_order"]);
607  $this->setColumnImages($data["column_images"]);
608  $this->setRowImages($data["row_images"]);
609  $this->setBipolarAdjective(0, $data["bipolar_adjective1"]);
610  $this->setBipolarAdjective(1, $data["bipolar_adjective2"]);
611  $this->setLayout($data["layout"]);
612  $this->flushColumns();
613 
614  $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",
615  array('integer'),
616  array($id)
617  );
618  if ($result->numRows() > 0)
619  {
620  while ($data = $ilDB->fetchAssoc($result))
621  {
622  if ($data["neutral"] == 0)
623  {
624  $this->addColumn($data["title"]);
625  }
626  else
627  {
628  $this->setNeutralColumn($data["title"]);
629  }
630  }
631  }
632 
633  $result = $ilDB->queryF("SELECT * FROM svy_qst_matrixrows WHERE question_fi = %s ORDER BY sequence",
634  array('integer'),
635  array($id)
636  );
637  while ($row = $ilDB->fetchAssoc($result))
638  {
639  $this->addRow($row["title"]);
640  }
641  }
643  }
644 
651  function isComplete()
652  {
653  if (
654  strlen($this->getTitle()) &&
655  strlen($this->getAuthor()) &&
656  strlen($this->getQuestiontext()) &&
657  $this->getColumnCount() &&
658  $this->getRowCount()
659  )
660  {
661  return 1;
662  }
663  else
664  {
665  return 0;
666  }
667  }
668 
674  function saveToDb($original_id = NULL, $withanswers = true)
675  {
676  global $ilDB;
677 
678  $affectedRows = parent::saveToDb($original_id);
679 
680  if ($affectedRows == 1)
681  {
682  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
683  array('integer'),
684  array($this->getId())
685  );
686  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (
687  question_fi, subtype, column_separators, row_separators, neutral_column_separator,column_placeholders,
688  legend, singleline_row_caption, repeat_column_header, column_header_position, random_rows,
689  column_order, column_images, row_images, bipolar_adjective1, bipolar_adjective2, layout, tstamp)
690  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
691  array(
692  'integer', 'integer', 'text', 'text', 'text', 'integer', 'text', 'text', 'text',
693  'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'integer'
694  ),
695  array(
696  $this->getId(),
697  $this->getSubtype(),
698  $this->getColumnSeparators(),
699  $this->getRowSeparators(),
700  $this->getNeutralColumnSeparator(),
701  $this->getColumnPlaceholders(),
702  $this->getLegend(),
703  $this->getSingleLineRowCaption(),
704  $this->getRepeatColumnHeader(),
705  $this->getColumnHeaderPosition(),
706  $this->getRandomRows(),
707  $this->getColumnOrder(),
708  $this->getColumnImages(),
709  $this->getRowImages(),
710  $this->getBipolarAdjective(0),
711  $this->getBipolarAdjective(1),
712  serialize($this->getLayout()),
713  time()
714  )
715  );
716 
717  // saving material uris in the database
718  $this->saveMaterial();
719 
720  $this->saveColumnsToDb();
721  $this->saveRowsToDb();
722  }
723  }
724 
725  function saveBipolarAdjectives($adjective1, $adjective2)
726  {
727  global $ilDB;
728 
729  $affectedRows = $ilDB->manipulateF("UPDATE " . $this->getAdditionalTableName() . " SET bipolar_adjective1 = %s, bipolar_adjective2 = %s WHERE question_fi = %s",
730  array('text', 'text', 'integer'),
731  array((strlen($adjective1)) ? $adjective1 : NULL, (strlen($adjective2)) ? $adjective2 : NULL, $this->getId())
732  );
733  }
734 
743  function saveColumnToDb($columntext, $neutral = 0)
744  {
745  global $ilUser, $ilDB;
746 
747  $result = $ilDB->queryF("SELECT title, category_id FROM svy_category WHERE title = %s AND neutral = %s AND owner_fi = %s",
748  array('text', 'text', 'integer'),
749  array($columntext, $neutral, $ilUser->getId())
750  );
751  $insert = FALSE;
752  $returnvalue = "";
753  if ($result->numRows())
754  {
755  $insert = TRUE;
756  while ($row = $ilDB->fetchAssoc($result))
757  {
758  if (strcmp($row["title"], $columntext) == 0)
759  {
760  $returnvalue = $row["category_id"];
761  $insert = FALSE;
762  }
763  }
764  }
765  else
766  {
767  $insert = TRUE;
768  }
769  if ($insert)
770  {
771  $next_id = $ilDB->nextId('svy_category');
772  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, neutral, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
773  array('integer', 'text', 'text', 'integer', 'text', 'integer'),
774  array($next_id, $columntext, 0, $ilUser->getId(), $neutral, time())
775  );
776  $returnvalue = $next_id;
777  }
778  return $returnvalue;
779  }
780 
781  function saveColumnsToDb($original_id = "")
782  {
783  global $ilDB;
784 
785  // save columns
786  $question_id = $this->getId();
787  if (strlen($original_id))
788  {
789  $question_id = $original_id;
790  }
791 
792  // delete existing column relations
793  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_variable WHERE question_fi = %s",
794  array('integer'),
795  array($question_id)
796  );
797  // create new column relations
798  for ($i = 0; $i < $this->getColumnCount(); $i++)
799  {
800  $cat = $this->getColumn($i);
801  $column_id = $this->saveColumnToDb($cat);
802  $next_id = $ilDB->nextId('svy_variable');
803  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, value2, sequence, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
804  array('integer','integer','integer','float','float','integer','integer'),
805  array($next_id, $column_id, $question_id, ($i + 1), NULL, $i, time())
806  );
807  }
808  if (strlen($this->getNeutralColumn()))
809  {
810  $column_id = $this->saveColumnToDb($this->getNeutralColumn(), 1);
811  $next_id = $ilDB->nextId('svy_variable');
812  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, value2, sequence, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
813  array('integer','integer','integer','float','float','integer','integer'),
814  array($next_id, $column_id, $question_id, ($i + 1), NULL, $i, time())
815  );
816  }
817  $this->saveCompletionStatus($original_id);
818  }
819 
820  function saveRowsToDb($original_id = "")
821  {
822  global $ilDB;
823 
824  // save rows
825  $question_id = $this->getId();
826  if (strlen($original_id))
827  {
828  $question_id = $original_id;
829  }
830 
831  // delete existing rows
832  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_matrixrows WHERE question_fi = %s",
833  array('integer'),
834  array($question_id)
835  );
836  // create new rows
837  for ($i = 0; $i < $this->getRowCount(); $i++)
838  {
839  $row = $this->getRow($i);
840  $next_id = $ilDB->nextId('svy_qst_matrixrows');
841  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_matrixrows (id_svy_qst_matrixrows, title, sequence, question_fi) VALUES (%s, %s, %s, %s)",
842  array('integer','text','integer','integer'),
843  array($next_id, $row, $i, $question_id)
844  );
845  }
846  $this->saveCompletionStatus($original_id);
847  }
848 
855  function toXML($a_include_header = TRUE, $obligatory_state = "")
856  {
857  include_once("./classes/class.ilXmlWriter.php");
858  $a_xml_writer = new ilXmlWriter;
859  $a_xml_writer->xmlHeader();
860  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
861  $xml = $a_xml_writer->xmlDumpMem(FALSE);
862  if (!$a_include_header)
863  {
864  $pos = strpos($xml, "?>");
865  $xml = substr($xml, $pos + 2);
866  }
867  return $xml;
868  }
869 
878  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
879  {
880  $attrs = array(
881  "id" => $this->getId(),
882  "title" => $this->getTitle(),
883  "type" => $this->getQuestiontype(),
884  "subtype" => $this->getSubtype(),
885  "obligatory" => $this->getObligatory()
886  );
887  $a_xml_writer->xmlStartTag("question", $attrs);
888 
889  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
890  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
891  $a_xml_writer->xmlStartTag("questiontext");
892  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
893  $a_xml_writer->xmlEndTag("questiontext");
894 
895  $a_xml_writer->xmlStartTag("matrix");
896  $a_xml_writer->xmlStartTag("matrixrows");
897  for ($i = 0; $i < $this->getRowCount(); $i++)
898  {
899  $attrs = array(
900  "id" => $i
901  );
902  $a_xml_writer->xmlStartTag("matrixrow", $attrs);
903  $this->addMaterialTag($a_xml_writer, $this->getRow($i));
904  $a_xml_writer->xmlEndTag("matrixrow");
905  }
906  $a_xml_writer->xmlEndTag("matrixrows");
907 
908  $a_xml_writer->xmlStartTag("responses");
909  if (strlen($this->getBipolarAdjective(0)) && (strlen($this->getBipolarAdjective(1))))
910  {
911  $a_xml_writer->xmlStartTag("bipolar_adjectives");
912  $attribs = array(
913  "label" => "0"
914  );
915  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(0));
916  $attribs = array(
917  "label" => "1"
918  );
919  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(1));
920  $a_xml_writer->xmlEndTag("bipolar_adjectives");
921  }
922  for ($i = 0; $i < $this->getColumnCount(); $i++)
923  {
924  $attrs = array(
925  "id" => $i
926  );
927  switch ($this->getSubtype())
928  {
929  case 0:
930  $a_xml_writer->xmlStartTag("response_single", $attrs);
931  break;
932  case 1:
933  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
934  break;
935  }
936  $this->addMaterialTag($a_xml_writer, $this->getColumn($i));
937  switch ($this->getSubtype())
938  {
939  case 0:
940  $a_xml_writer->xmlEndTag("response_single");
941  break;
942  case 1:
943  $a_xml_writer->xmlEndTag("response_multiple");
944  break;
945  }
946  }
947  if (strlen($this->getNeutralColumn()))
948  {
949  $attrs = array(
950  "id" => $this->getColumnCount(),
951  "label" => "neutral"
952  );
953  switch ($this->getSubtype())
954  {
955  case 0:
956  $a_xml_writer->xmlStartTag("response_single", $attrs);
957  break;
958  case 1:
959  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
960  break;
961  }
962  $this->addMaterialTag($a_xml_writer, $this->getNeutralColumn());
963  switch ($this->getSubtype())
964  {
965  case 0:
966  $a_xml_writer->xmlEndTag("response_single");
967  break;
968  case 1:
969  $a_xml_writer->xmlEndTag("response_multiple");
970  break;
971  }
972  }
973 
974  $a_xml_writer->xmlEndTag("responses");
975  $a_xml_writer->xmlEndTag("matrix");
976 
977  if (count($this->material))
978  {
979  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
980  {
981  $attrs = array(
982  "label" => $this->material["title"]
983  );
984  $a_xml_writer->xmlStartTag("material", $attrs);
985  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
986  if (strcmp($matches[1], "") != 0)
987  {
988  $intlink = $this->material["internal_link"];
989  }
990  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
991  $a_xml_writer->xmlEndTag("material");
992  }
993  }
994 
995  $a_xml_writer->xmlStartTag("metadata");
996  $a_xml_writer->xmlStartTag("metadatafield");
997  $a_xml_writer->xmlElement("fieldlabel", NULL, "column_separators");
998  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getColumnSeparators());
999  $a_xml_writer->xmlEndTag("metadatafield");
1000 
1001  $a_xml_writer->xmlStartTag("metadatafield");
1002  $a_xml_writer->xmlElement("fieldlabel", NULL, "row_separators");
1003  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getRowSeparators());
1004  $a_xml_writer->xmlEndTag("metadatafield");
1005 
1006  $a_xml_writer->xmlStartTag("metadatafield");
1007  $a_xml_writer->xmlElement("fieldlabel", NULL, "neutral_column_separator");
1008  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getNeutralColumnSeparator());
1009  $a_xml_writer->xmlEndTag("metadatafield");
1010 
1011  $a_xml_writer->xmlStartTag("metadatafield");
1012  $a_xml_writer->xmlElement("fieldlabel", NULL, "layout");
1013  $a_xml_writer->xmlElement("fieldentry", NULL, serialize($this->getLayout()));
1014  $a_xml_writer->xmlEndTag("metadatafield");
1015 
1016  $a_xml_writer->xmlEndTag("metadata");
1017 
1018  $a_xml_writer->xmlEndTag("question");
1019  }
1020 
1021  function syncWithOriginal()
1022  {
1023  if ($this->getOriginalId())
1024  {
1026  $this->saveColumnsToDb($this->getOriginalId());
1027  $this->saveRowsToDb($this->getOriginalId());
1028  }
1029  }
1030 
1031 
1039  function addStandardNumbers($lower_limit, $upper_limit)
1040  {
1041  for ($i = $lower_limit; $i <= $upper_limit; $i++)
1042  {
1043  $this->addColumn($i);
1044  }
1045  }
1046 
1054  function savePhrase($phrases, $title)
1055  {
1056  global $ilUser;
1057  global $ilDB;
1058 
1059  $next_id = $ilDB->nextId('svy_phrase');
1060  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase (phrase_id, title, defaultvalue, owner_fi, tstamp) VALUES (%s, %s, %s, %s, %s)",
1061  array('integer','text','text','integer','integer'),
1062  array($next_id, $title, 1, $ilUser->getId(), time())
1063  );
1064  $phrase_id = $next_id;
1065 
1066  $counter = 1;
1067  foreach ($phrases as $category)
1068  {
1069  $next_id = $ilDB->nextId('svy_category');
1070  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, neutral, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
1071  array('integer','text','text','integer','text','integer'),
1072  array($next_id, $category, 1, $ilUser->getId(), 0, time())
1073  );
1074  $column_id = $next_id;
1075  $next_id = $ilDB->nextId('svy_phrase_cat');
1076  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence) VALUES (%s, %s, %s, %s)",
1077  array('integer', 'integer', 'integer', 'integer'),
1078  array($next_id, $phrase_id, $column_id, $counter)
1079  );
1080  $counter++;
1081  }
1082  }
1083 
1090  function getQuestionType()
1091  {
1092  return "SurveyMatrixQuestion";
1093  }
1094 
1102  {
1103  return "svy_qst_matrix";
1104  }
1105 
1112  function &getWorkingDataFromUserInput($post_data)
1113  {
1114  $data = array();
1115  foreach ($post_data as $key => $value)
1116  {
1117  switch ($this->getSubtype())
1118  {
1119  case 0:
1120  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1121  {
1122  if (is_array($value))
1123  {
1124  foreach ($value as $val)
1125  {
1126  array_push($data, array("value" => $val, "rowvalue" => $matches[1]));
1127  }
1128  }
1129  else
1130  {
1131  array_push($data, array("value" => $value, "rowvalue" => $matches[1]));
1132  }
1133  }
1134  break;
1135  case 1:
1136  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1137  {
1138  if (is_array($value))
1139  {
1140  foreach ($value as $val)
1141  {
1142  array_push($data, array("value" => $val, "rowvalue" => $matches[1]));
1143  }
1144  }
1145  else
1146  {
1147  array_push($data, array("value" => $value, "rowvalue" => $matches[1]));
1148  }
1149  }
1150  break;
1151  }
1152  }
1153  return $data;
1154  }
1155 
1165  function checkUserInput($post_data, $survey_id)
1166  {
1167  if (!$this->getObligatory($survey_id)) return "";
1168  switch ($this->getSubtype())
1169  {
1170  case 0:
1171  $counter = 0;
1172  foreach ($post_data as $key => $value)
1173  {
1174  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1175  {
1176  $counter++;
1177  }
1178  }
1179  if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_radio_button_not_checked");
1180  break;
1181  case 1:
1182  $counter = 0;
1183  foreach ($post_data as $key => $value)
1184  {
1185  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1186  {
1187  $counter++;
1188  if ((!is_array($value)) || (count($value) < 1))
1189  {
1190  return $this->lng->txt("matrix_question_checkbox_not_checked");
1191  }
1192  }
1193  }
1194  if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_checkbox_not_checked");
1195  break;
1196  }
1197  return "";
1198  }
1199 
1205  public function saveRandomData($active_id)
1206  {
1207  global $ilDB;
1208  $columncount = ($this->hasNeutralColumn()) ? $this->getColumnCount()+1 : $this->getColumnCount();
1209  for ($row = 0; $row < $this->getRowCount(); $row++)
1210  {
1211  if ($this->getSubType() == 1)
1212  {
1213  // multiple responses
1214  for ($i = 0; $i < $columncount; $i++)
1215  {
1216  if (rand(0,1))
1217  {
1218  $next_id = $ilDB->nextId('svy_answer');
1219  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, rowvalue, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
1220  array('integer','integer','integer','float','text','integer','integer'),
1221  array($next_id, $this->getId(), $active_id, $i, NULL, $row, time())
1222  );
1223  }
1224  }
1225  }
1226  else
1227  {
1228  // single responses
1229  $category = rand(0, $columncount-1);
1230  $next_id = $ilDB->nextId('svy_answer');
1231  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, rowvalue, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
1232  array('integer','integer','integer','float','text','integer','integer'),
1233  array($next_id, $this->getId(), $active_id, $category, NULL, $row, time())
1234  );
1235  }
1236  }
1237  }
1238 
1239  function saveUserInput($post_data, $active_id)
1240  {
1241  global $ilDB;
1242 
1243  switch ($this->getSubtype())
1244  {
1245  case 0:
1246  foreach ($post_data as $key => $value)
1247  {
1248  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1249  {
1250  $next_id = $ilDB->nextId('svy_answer');
1251  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, rowvalue, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
1252  array('integer','integer','integer','float','text','integer','integer'),
1253  array($next_id, $this->getId(), $active_id, $value, NULL, $matches[1], time())
1254  );
1255  }
1256  }
1257  break;
1258  case 1:
1259  foreach ($post_data as $key => $value)
1260  {
1261  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
1262  {
1263  foreach ($value as $checked)
1264  {
1265  if (strlen($checked))
1266  {
1267  $next_id = $ilDB->nextId('svy_answer');
1268  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, rowvalue, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
1269  array('integer','integer','integer','float','text','integer','integer'),
1270  array($next_id, $this->getId(), $active_id, $checked, NULL, $matches[1], time())
1271  );
1272  }
1273  }
1274  }
1275  }
1276  break;
1277  }
1278  }
1279 
1286  function deleteAdditionalTableData($question_id)
1287  {
1288  parent::deleteAdditionalTableData($question_id);
1289 
1290  global $ilDB;
1291  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_matrixrows WHERE question_fi = %s",
1292  array('integer'),
1293  array($question_id)
1294  );
1295  }
1296 
1305  {
1306  global $ilDB;
1307 
1308  $result = $ilDB->queryF("SELECT svy_answer.active_fi, svy_answer.question_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",
1309  array('integer', 'integer'),
1310  array($this->getId(), $survey_id)
1311  );
1312  $found = array();
1313  while ($row = $ilDB->fetchAssoc($result))
1314  {
1315  $found[$row["active_fi"].$row["question_fi"]] = 1;
1316  }
1317  return count($found);
1318  }
1319 
1328  function &getCumulatedResultsForRow($rowindex, $survey_id, $nr_of_users)
1329  {
1330  global $ilDB;
1331 
1332  $question_id = $this->getId();
1333 
1334  $result_array = array();
1335  $cumulated = array();
1336 
1337  $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_answer.rowvalue = %s AND svy_finished.finished_id = svy_answer.active_fi",
1338  array('integer', 'integer', 'integer'),
1339  array($question_id, $survey_id, $rowindex)
1340  );
1341 
1342  switch ($this->getSubtype())
1343  {
1344  case 0:
1345  case 1:
1346  while ($row = $ilDB->fetchAssoc($result))
1347  {
1348  $cumulated[$row["value"]]++;
1349  }
1350  asort($cumulated, SORT_NUMERIC);
1351  end($cumulated);
1352  break;
1353  }
1354  $numrows = $result->numRows();
1355  $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
1356  $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
1357 
1358  $prefix = "";
1359  if (strcmp(key($cumulated), "") != 0)
1360  {
1361  $prefix = (key($cumulated)+1) . " - ";
1362  }
1363  $cat = $this->getColumn(key($cumulated));
1364  $result_array["MODE"] = $prefix . $cat;
1365  $result_array["MODE_VALUE"] = key($cumulated)+1;
1366  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
1367  for ($key = 0; $key < $this->getColumnCount(); $key++)
1368  {
1369  $percentage = 0;
1370  if ($numrows > 0)
1371  {
1372  $percentage = (float)((int)$cumulated[$key]/$numrows);
1373  }
1374  $cat = $this->getColumn($key);
1375  $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
1376  }
1377  if ($this->hasNeutralColumn())
1378  {
1379  $percentage = 0;
1380  if ($numrows > 0)
1381  {
1382  $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
1383  }
1384  $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
1385  }
1386  ksort($cumulated, SORT_NUMERIC);
1387  $median = array();
1388  $total = 0;
1389  foreach ($cumulated as $value => $key)
1390  {
1391  $total += $key;
1392  for ($i = 0; $i < $key; $i++)
1393  {
1394  array_push($median, $value+1);
1395  }
1396  }
1397  if ($total > 0)
1398  {
1399  if (($total % 2) == 0)
1400  {
1401  $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
1402  if (round($median_value) != $median_value)
1403  {
1404  $cat = $this->getColumn((int)floor($median_value)-1);
1405  $cat2 = $this->getColumn((int)ceil($median_value)-1);
1406  $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
1407  }
1408  }
1409  else
1410  {
1411  $median_value = $median[(($total+1)/2)-1];
1412  }
1413  }
1414  else
1415  {
1416  $median_value = "";
1417  }
1418  $result_array["ARITHMETIC_MEAN"] = "";
1419  $result_array["MEDIAN"] = $median_value;
1420  $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
1421  $result_array["ROW"] = $this->getRow($rowindex);
1422  return $result_array;
1423  }
1424 
1432  function &getCumulatedResults($survey_id, $nr_of_users)
1433  {
1434  global $ilDB;
1435 
1436  $question_id = $this->getId();
1437 
1438  $result_array = array();
1439  $cumulated = array();
1440 
1441  $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",
1442  array('integer', 'integer'),
1443  array($question_id, $survey_id)
1444  );
1445 
1446  switch ($this->getSubtype())
1447  {
1448  case 0:
1449  case 1:
1450  while ($row = $ilDB->fetchAssoc($result))
1451  {
1452  $cumulated[$row["value"]]++;
1453  }
1454  asort($cumulated, SORT_NUMERIC);
1455  end($cumulated);
1456  break;
1457  }
1458  $numrows = $result->numRows();
1459  $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
1460  $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
1461 
1462  $prefix = "";
1463  if (strcmp(key($cumulated), "") != 0)
1464  {
1465  $prefix = (key($cumulated)+1) . " - ";
1466  }
1467  $cat = $this->getColumn(key($cumulated));
1468  $result_array["MODE"] = $prefix . $cat;
1469  $result_array["MODE_VALUE"] = key($cumulated)+1;
1470  $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
1471  for ($key = 0; $key < $this->getColumnCount(); $key++)
1472  {
1473  $percentage = 0;
1474  if ($numrows > 0)
1475  {
1476  $percentage = (float)((int)$cumulated[$key]/$numrows);
1477  }
1478  $cat = $this->getColumn($key);
1479  $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
1480  }
1481  if ($this->hasNeutralColumn())
1482  {
1483  $percentage = 0;
1484  if ($numrows > 0)
1485  {
1486  $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
1487  }
1488  $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
1489  }
1490  ksort($cumulated, SORT_NUMERIC);
1491  $median = array();
1492  $total = 0;
1493  foreach ($cumulated as $value => $key)
1494  {
1495  $total += $key;
1496  for ($i = 0; $i < $key; $i++)
1497  {
1498  array_push($median, $value+1);
1499  }
1500  }
1501  if ($total > 0)
1502  {
1503  if (($total % 2) == 0)
1504  {
1505  $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
1506  if (round($median_value) != $median_value)
1507  {
1508  $cat = $this->getColumn((int)floor($median_value)-1);
1509  $cat2 = $this->getColumn((int)ceil($median_value)-1);
1510  $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
1511  }
1512  }
1513  else
1514  {
1515  $median_value = $median[(($total+1)/2)-1];
1516  }
1517  }
1518  else
1519  {
1520  $median_value = "";
1521  }
1522  $result_array["ARITHMETIC_MEAN"] = "";
1523  $result_array["MEDIAN"] = $median_value;
1524  $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
1525 
1526  $cumulated_results = array();
1527  $cumulated_results["TOTAL"] = $result_array;
1528  for ($i = 0; $i < $this->getRowCount(); $i++)
1529  {
1530  $rowresult =& $this->getCumulatedResultsForRow($i, $survey_id, $nr_of_users);
1531  $cumulated_results[$i] = $rowresult;
1532  }
1533  return $cumulated_results;
1534  }
1535 
1547  function setExportCumulatedXLS(&$worksheet, &$format_title, &$format_bold, &$eval_data, $row)
1548  {
1549  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
1550  $worksheet->writeString($row, 0, ilExcelUtils::_convert_text($this->getTitle()));
1551  $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
1552  $worksheet->writeString($row, 2, ilExcelUtils::_convert_text($this->lng->txt($eval_data["TOTAL"]["QUESTION_TYPE"])));
1553  $worksheet->write($row, 3, $eval_data["TOTAL"]["USERS_ANSWERED"]);
1554  $worksheet->write($row, 4, $eval_data["TOTAL"]["USERS_SKIPPED"]);
1555  $worksheet->write($row, 5, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_VALUE"]));
1556  $worksheet->write($row, 6, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE"]));
1557  $worksheet->write($row, 7, $eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]);
1558  $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
1559  $worksheet->write($row, 9, $eval_data["TOTAL"]["ARITHMETIC_MEAN"]);
1560  $row++;
1561  foreach ($eval_data as $evalkey => $evalvalue)
1562  {
1563  if (is_numeric($evalkey))
1564  {
1565  $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
1566  $worksheet->write($row, 3, $evalvalue["USERS_ANSWERED"]);
1567  $worksheet->write($row, 4, $evalvalue["USERS_SKIPPED"]);
1568  $worksheet->write($row, 5, ilExcelUtils::_convert_text($evalvalue["MODE_VALUE"]));
1569  $worksheet->write($row, 6, ilExcelUtils::_convert_text($evalvalue["MODE"]));
1570  $worksheet->write($row, 7, $evalvalue["MODE_NR_OF_SELECTIONS"]);
1571  $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
1572  $worksheet->write($row, 9, $evalvalue["ARITHMETIC_MEAN"]);
1573  $row++;
1574  }
1575  }
1576  return $row;
1577  }
1578 
1590  function &setExportCumulatedCVS(&$eval_data)
1591  {
1592  $result = array();
1593  foreach ($eval_data as $evalkey => $evalvalue)
1594  {
1595  $csvrow = array();
1596  if (is_numeric($evalkey))
1597  {
1598  array_push($csvrow, "");
1599  array_push($csvrow, $evalvalue["ROW"]);
1600  array_push($csvrow, "");
1601  }
1602  else
1603  {
1604  array_push($csvrow, $this->getTitle());
1605  array_push($csvrow, $this->getQuestiontext());
1606  array_push($csvrow, $this->lng->txt($evalvalue["QUESTION_TYPE"]));
1607  }
1608  array_push($csvrow, $evalvalue["USERS_ANSWERED"]);
1609  array_push($csvrow, $evalvalue["USERS_SKIPPED"]);
1610  array_push($csvrow, $evalvalue["MODE"]);
1611  array_push($csvrow, $evalvalue["MODE_NR_OF_SELECTIONS"]);
1612  array_push($csvrow, $evalvalue["MEDIAN"]);
1613  array_push($csvrow, $evalvalue["ARITHMETIC_MEAN"]);
1614  array_push($result, $csvrow);
1615  }
1616  return $result;
1617  }
1618 
1628  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
1629  {
1630  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
1631  $worksheet =& $workbook->addWorksheet();
1632  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
1633  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
1634  $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
1635  $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
1636  $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
1637  $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
1638  $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
1639  $worksheet->write(3, 1, $eval_data["TOTAL"]["USERS_ANSWERED"]);
1640  $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
1641  $worksheet->write(4, 1, $eval_data["TOTAL"]["USERS_SKIPPED"]);
1642  $rowcounter = 5;
1643 
1644  preg_match("/(.*?)\s+-\s+(.*)/", $eval_data["TOTAL"]["MODE"], $matches);
1645  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
1646  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
1647  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
1648  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
1649  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
1650  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]));
1651  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
1652  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
1653  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
1654  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
1655  $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
1656  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
1657  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
1658 
1659  foreach ($eval_data["TOTAL"]["variables"] as $key => $value)
1660  {
1661  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
1662  $worksheet->write($rowcounter, 2, $key+1);
1663  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
1664  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
1665  }
1666 
1667  foreach ($eval_data as $evalkey => $evalvalue)
1668  {
1669  if (is_numeric($evalkey))
1670  {
1671  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("row")), $format_bold);
1672  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
1673  $worksheet->writeString($rowcounter + 1, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
1674  $worksheet->write($rowcounter + 1, 1, $evalvalue["USERS_ANSWERED"]);
1675  $worksheet->writeString($rowcounter + 2, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
1676  $worksheet->write($rowcounter + 2, 1, $evalvalue["USERS_SKIPPED"]);
1677  $rowcounter = $rowcounter + 3;
1678 
1679  preg_match("/(.*?)\s+-\s+(.*)/", $evalvalue["MODE"], $matches);
1680  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
1681  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
1682  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
1683  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
1684  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
1685  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($evalvalue["MODE_NR_OF_SELECTIONS"]));
1686  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
1687  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
1688  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
1689  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
1690  $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
1691  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
1692  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
1693 
1694  foreach ($evalvalue["variables"] as $key => $value)
1695  {
1696  $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
1697  $worksheet->write($rowcounter, 2, $key+1);
1698  $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
1699  $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
1700  }
1701  }
1702  }
1703  // out compressed 2D-Matrix
1704  $format_center =& $workbook->addFormat();
1705  $format_center->setColor('black');
1706  $format_center->setAlign('center');
1707 
1708  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("overview")), $format_bold);
1709  // title row with variables
1710  $rowcounter++;
1711  $counter = 0;
1712  $worksheet->write($rowcounter, $counter, "", $format_title);
1713  foreach ($eval_data["TOTAL"]["variables"] as $variable)
1714  {
1715  $worksheet->write($rowcounter, 1+$counter, ilExcelUtils::_convert_text($variable["title"]), $format_title);
1716  $counter++;
1717  }
1718  $rowcounter++;
1719  // rows with variable values
1720  foreach ($eval_data as $index => $data)
1721  {
1722  if (is_numeric($index))
1723  {
1724  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($data["ROW"]), $format_title);
1725  $counter = 1;
1726  foreach ($data["variables"] as $vardata)
1727  {
1728  $worksheet->write($rowcounter, $counter, $vardata["selected"], $format_center);
1729  $counter++;
1730  }
1731  $rowcounter++;
1732  }
1733  }
1734  }
1735 
1743  {
1745  for ($i = 0; $i < $this->getRowCount(); $i++)
1746  {
1747  array_push($a_array, $this->getRow($i));
1748  switch ($this->getSubtype())
1749  {
1750  case 0:
1751  break;
1752  case 1:
1753  for ($index = 0; $index < $this->getColumnCount(); $index++)
1754  {
1755  $col = $this->getColumn($index);
1756  array_push($a_array, ($index+1) . " - $col");
1757  }
1758  if (strlen($this->getNeutralColumn()))
1759  {
1760  array_push($a_array, ($this->getColumnCount()+1) . " - " . $this->getNeutralColumn());
1761  }
1762  break;
1763  }
1764  }
1765  }
1766 
1774  function addUserSpecificResultsData(&$a_array, &$resultset)
1775  {
1776  if (count($resultset["answers"][$this->getId()]))
1777  {
1778  array_push($a_array, "");
1779  switch ($this->getSubtype())
1780  {
1781  case 0:
1782  for ($i = 0; $i < $this->getRowCount(); $i++)
1783  {
1784  $checked = FALSE;
1785  foreach ($resultset["answers"][$this->getId()] as $result)
1786  {
1787  if ($result["rowvalue"] == $i)
1788  {
1789  $checked = TRUE;
1790  array_push($a_array, $result["value"] + 1);
1791  }
1792  }
1793  if (!$checked)
1794  {
1795  array_push($a_array, $this->lng->txt("skipped"));
1796  }
1797  }
1798  break;
1799  case 1:
1800  for ($i = 0; $i < $this->getRowCount(); $i++)
1801  {
1802  $checked = FALSE;
1803  $checked_values = array();
1804  foreach ($resultset["answers"][$this->getId()] as $result)
1805  {
1806  if ($result["rowvalue"] == $i)
1807  {
1808  $checked = TRUE;
1809  array_push($checked_values, $result["value"] + 1);
1810  }
1811  }
1812  if (!$checked)
1813  {
1814  array_push($a_array, $this->lng->txt("skipped"));
1815  }
1816  else
1817  {
1818  array_push($a_array, "");
1819  }
1820  for ($index = 0; $index < $this->getColumnCount(); $index++)
1821  {
1822  if (!$checked)
1823  {
1824  array_push($a_array, "");
1825  }
1826  else
1827  {
1828  if (in_array($index+1, $checked_values))
1829  {
1830  array_push($a_array, 1);
1831  }
1832  else
1833  {
1834  array_push($a_array, 0);
1835  }
1836  }
1837  }
1838  if (strlen($this->getNeutralColumn()))
1839  {
1840  if (!$checked)
1841  {
1842  array_push($a_array, "");
1843  }
1844  else
1845  {
1846  if (in_array($this->getColumnCount()+1, $checked_values))
1847  {
1848  array_push($a_array, 1);
1849  }
1850  else
1851  {
1852  array_push($a_array, 0);
1853  }
1854  }
1855  }
1856  }
1857  break;
1858  }
1859  }
1860  else
1861  {
1862  array_push($a_array, $this->lng->txt("skipped"));
1863  for ($i = 0; $i < $this->getRowCount(); $i++)
1864  {
1865  array_push($a_array, "");
1866  switch ($this->getSubtype())
1867  {
1868  case 0:
1869  break;
1870  case 1:
1871  for ($index = 0; $index < $this->getColumnCount(); $index++)
1872  {
1873  array_push($a_array, "");
1874  }
1875  if (strlen($this->getNeutralColumn()))
1876  {
1877  array_push($a_array, "");
1878  }
1879  break;
1880  }
1881  }
1882  }
1883  }
1884 
1893  {
1894  global $ilDB;
1895 
1896  $answers = array();
1897 
1898  $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 ORDER BY rowvalue, value",
1899  array('integer','integer'),
1900  array($survey_id, $this->getId())
1901  );
1902  $results = array();
1903  while ($row = $ilDB->fetchAssoc($result))
1904  {
1905  $column = $this->getColumn($row["value"]);
1906  if (!is_array($answers[$row["active_fi"]])) $answers[$row["active_fi"]] = array();
1907  array_push($answers[$row["active_fi"]], $this->getRow($row["rowvalue"]) . ": " . ($row["value"] + 1) . " - " . $column);
1908  }
1909  foreach ($answers as $key => $value)
1910  {
1911  $answers[$key] = implode("<br />", $value);
1912  }
1913  return $answers;
1914  }
1915 
1922  function getSubtype()
1923  {
1924  return $this->subtype;
1925  }
1926 
1933  function setSubtype($a_subtype = 0)
1934  {
1935  switch ($a_subtype)
1936  {
1937  case 1:
1938  case 2:
1939  case 3:
1940  case 4:
1941  case 5:
1942  case 6:
1943  $this->subtype = $a_subtype;
1944  break;
1945  case 0:
1946  default:
1947  $this->subtype = 0;
1948  break;
1949  }
1950  }
1951 
1958  function setColumnSeparators($enable = 0)
1959  {
1960  switch ($enable)
1961  {
1962  case 1:
1963  $this->columnSeparators = 1;
1964  break;
1965  case 0:
1966  default:
1967  $this->columnSeparators = 0;
1968  break;
1969  }
1970  }
1971 
1979  {
1980  return ($this->columnSeparators) ? 1 : 0;
1981  }
1982 
1989  function setRowSeparators($enable = 0)
1990  {
1991  switch ($enable)
1992  {
1993  case 1:
1994  $this->rowSeparators = 1;
1995  break;
1996  case 0:
1997  default:
1998  $this->rowSeparators = 0;
1999  break;
2000  }
2001  }
2002 
2009  function getRowSeparators()
2010  {
2011  return ($this->rowSeparators) ? 1 : 0;
2012  }
2013 
2020  function setNeutralColumnSeparator($enable = 0)
2021  {
2022  switch ($enable)
2023  {
2024  case 1:
2025  $this->neutralColumnSeparator = 1;
2026  break;
2027  case 0:
2028  default:
2029  $this->neutralColumnSeparator = 0;
2030  break;
2031  }
2032  }
2033 
2041  {
2042  return ($this->neutralColumnSeparator) ? 1 : 0;
2043  }
2044 
2053  function importAdditionalMetadata($a_meta)
2054  {
2055  foreach ($a_meta as $key => $value)
2056  {
2057  switch ($value["label"])
2058  {
2059  case "column_separators":
2060  $this->setColumnSeparators($value["entry"]);
2061  break;
2062  case "row_separators":
2063  $this->setRowSeparators($value["entry"]);
2064  break;
2065  case "layout":
2066  $this->setLayout($value["entry"]);
2067  break;
2068  case "neutral_column_separator":
2069  $this->setNeutralColumnSeparator($value["entry"]);
2070  break;
2071  }
2072  }
2073  }
2074 
2081  function importAdjectives($a_data)
2082  {
2083  $i = 0;
2084  foreach ($a_data as $adjective)
2085  {
2086  if (is_numeric($adjective["label"]))
2087  {
2088  $this->setBipolarAdjective($adjective["label"], $adjective["text"]);
2089  }
2090  else
2091  {
2092  $this->setBipolarAdjective($i, $adjective["text"]);
2093  }
2094  $i++;
2095  }
2096  }
2097 
2104  function importMatrix($a_data)
2105  {
2106  foreach ($a_data as $row)
2107  {
2108  $this->addRow($row);
2109  }
2110  }
2111 
2118  function importResponses($a_data)
2119  {
2120  foreach ($a_data as $id => $data)
2121  {
2122  $column = "";
2123  foreach ($data["material"] as $material)
2124  {
2125  $column .= $material["text"];
2126  }
2127  if (strcmp($data["label"], "neutral") == 0)
2128  {
2129  $this->setNeutralColumn($column);
2130  }
2131  else
2132  {
2133  $this->addColumn($column);
2134  }
2135  }
2136  }
2137 
2145  {
2146  return FALSE;
2147  }
2148 
2157  {
2158  return $value;
2159  }
2160 
2169  function outChart($survey_id, $type = "")
2170  {
2171  if (count($this->cumulated) == 0)
2172  {
2173  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
2175  $this->cumulated =& $this->getCumulatedResults($survey_id, $nr_of_users);
2176  }
2177  global $ilLog;
2178  if (is_numeric($type))
2179  {
2180  foreach ($this->cumulated[$type]["variables"] as $key => $value)
2181  {
2182  foreach ($value as $key2 => $value2)
2183  {
2184  $this->cumulated["variables"][$key][$key2] = utf8_decode($value2);
2185  }
2186  }
2187  $title = preg_replace("/<[^>]+?>/ims", "", $this->getRow($type));
2188  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyChart.php";
2189  $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"]);
2190  }
2191  }
2192 
2202  function saveLayout($percent_row, $percent_columns, $percent_bipolar_adjective1 = "", $percent_bipolar_adjective2 = "", $percent_neutral)
2203  {
2204  global $ilDB;
2205 
2206  $layout = array(
2207  "percent_row" => $percent_row,
2208  "percent_columns" => $percent_columns,
2209  "percent_bipolar_adjective1" => $percent_bipolar_adjective1,
2210  "percent_bipolar_adjective2" => $percent_bipolar_adjective2,
2211  "percent_neutral" => $percent_neutral
2212  );
2213  $affectedRows = $ilDB->manipulateF("UPDATE " . $this->getAdditionalTableName() . " SET layout = %s WHERE question_fi = %s",
2214  array('text', 'integer'),
2215  array(serialize($layout), $this->getId())
2216  );
2217  }
2218 
2219  function getLayout()
2220  {
2221  if (!is_array($this->layout) || count($this->layout) == 0)
2222  {
2223  if ($this->hasBipolarAdjectives() && $this->hasNeutralColumn())
2224  {
2225  $this->layout = array(
2226  "percent_row" => 30,
2227  "percent_columns" => 40,
2228  "percent_bipolar_adjective1" => 10,
2229  "percent_bipolar_adjective2" => 10,
2230  "percent_neutral" => 10
2231  );
2232  }
2233  elseif ($this->hasBipolarAdjectives())
2234  {
2235  $this->layout = array(
2236  "percent_row" => 30,
2237  "percent_columns" => 50,
2238  "percent_bipolar_adjective1" => 10,
2239  "percent_bipolar_adjective2" => 10,
2240  "percent_neutral" => 0
2241  );
2242  }
2243  elseif ($this->hasNeutralColumn())
2244  {
2245  $this->layout = array(
2246  "percent_row" => 30,
2247  "percent_columns" => 50,
2248  "percent_bipolar_adjective1" => 0,
2249  "percent_bipolar_adjective2" => 0,
2250  "percent_neutral" => 20
2251  );
2252  }
2253  else
2254  {
2255  $this->layout = array(
2256  "percent_row" => 30,
2257  "percent_columns" => 70,
2258  "percent_bipolar_adjective1" => 0,
2259  "percent_bipolar_adjective2" => 0,
2260  "percent_neutral" => 0
2261  );
2262  }
2263  }
2264  return $this->layout;
2265  }
2266 
2267  function setLayout($layout)
2268  {
2269  if (is_array($layout))
2270  {
2271  $this->layout = $layout;
2272  }
2273  else
2274  {
2275  $this->layout = unserialize($layout);
2276  }
2277  }
2278 
2285  {
2286  if ((strlen($this->getBipolarAdjective(0))) && (strlen($this->getBipolarAdjective(1))))
2287  {
2288  return TRUE;
2289  }
2290  else
2291  {
2292  return FALSE;
2293  }
2294  }
2295 
2301  function hasNeutralColumn()
2302  {
2303  if (strlen($this->getNeutralColumn()))
2304  {
2305  return TRUE;
2306  }
2307  else
2308  {
2309  return FALSE;
2310  }
2311  }
2312 
2318  function setColumnPlaceholders($a_value = 0)
2319  {
2320  $this->columnPlaceholders = ($a_value) ? 1 : 0;
2321  }
2322 
2329  {
2330  return ($this->columnPlaceholders) ? 1 : 0;
2331  }
2332 
2338  function setLegend($a_value = 0)
2339  {
2340  $this->legend = ($a_value) ? 1 : 0;
2341  }
2342 
2348  function getLegend()
2349  {
2350  return ($this->legend) ? 1 : 0;
2351  }
2352 
2353  function setSingleLineRowCaption($a_value = 0)
2354  {
2355  $this->singleLineRowCaption = ($a_value) ? 1 : 0;
2356  }
2357 
2359  {
2360  return ($this->singleLineRowCaption) ? 1 : 0;
2361  }
2362 
2363  function setRepeatColumnHeader($a_value = 0)
2364  {
2365  $this->repeatColumnHeader = ($a_value) ? 1 : 0;
2366  }
2367 
2369  {
2370  return ($this->repeatColumnHeader) ? 1 : 0;
2371  }
2372 
2373  function setColumnHeaderPosition($a_value)
2374  {
2375  $this->columnHeaderPosition = $a_value;
2376  }
2377 
2379  {
2380  return ($this->columnHeaderPosition) ? $this->columnHeaderPosition : 0;
2381  }
2382 
2383  function setRandomRows($a_value = 0)
2384  {
2385  $this->randomRows = ($a_value) ? 1 : 0;
2386  }
2387 
2388  function getRandomRows()
2389  {
2390  return ($this->randomRows) ? 1 : 0;
2391  }
2392 
2393  function setColumnOrder($a_value)
2394  {
2395  $this->columnOrder = $a_value;
2396  }
2397 
2398  function getColumnOrder()
2399  {
2400  return ($this->columnOrder) ? $this->columnOrder : 0;
2401  }
2402 
2403  function setColumnImages($a_value = 0)
2404  {
2405  $this->columnImages = ($a_value) ? 1 : 0;
2406  }
2407 
2408  function getColumnImages()
2409  {
2410  return ($this->columnImages) ? 1 : 0;
2411  }
2412 
2413  function setRowImages($a_value = 0)
2414  {
2415  $this->rowImages = ($a_value) ? 1 : 0;
2416  }
2417 
2418  function getRowImages()
2419  {
2420  return ($this->rowImages) ? 1 : 0;
2421  }
2422 
2423  public function getRows()
2424  {
2425  return $this->rows;
2426  }
2427 
2433  public function getCumulatedResultData($survey_id, $counter)
2434  {
2436  $questiontext = preg_replace("/<[^>]+?>/ims", "", $this->getQuestiontext());
2437  $maxlen = 37;
2438  if (strlen($questiontext) > $maxlen + 3)
2439  {
2440  include_once "./Services/Utilities/classes/class.ilStr.php";
2441  $questiontext = ilStr::substr($questiontext, 0, $maxlen) . "...";
2442  }
2443  $result = array();
2444  $row = array(
2445  'counter' => $counter,
2446  'title' => $this->getTitle(),
2447  'question' => $questiontext,
2448  'users_answered' => $cumulated['TOTAL']['USERS_ANSWERED'],
2449  'users_skipped' => $cumulated['TOTAL']['USERS_SKIPPED'],
2450  'question_type' => $this->lng->txt($cumulated['TOTAL']['QUESTION_TYPE']),
2451  'mode' => $cumulated['TOTAL']['MODE'],
2452  'mode_nr_of_selections' => $cumulated['TOTAL']['MODE_NR_OF_SELECTIONS'],
2453  'median' => $cumulated['TOTAL']['MEDIAN'],
2454  'arithmetic_mean' => $cumulated['TOTAL']['ARITHMETIC_MEAN']
2455  );
2456  array_push($result, $row);
2457  foreach ($cumulated as $key => $value)
2458  {
2459  if (is_numeric($key))
2460  {
2461  $row = array(
2462  'title' => '',
2463  'question' => ($key+1) . ". " . $value["ROW"],
2464  'users_answered' => $value['USERS_ANSWERED'],
2465  'users_skipped' => $value['USERS_SKIPPED'],
2466  'question_type' => '',
2467  'mode' => $value["MODE"],
2468  'mode_nr_of_selections' => $value["MODE_NR_OF_SELECTIONS"],
2469  'median' => $value["MEDIAN"],
2470  'arithmetic_mean' => $value["ARITHMETIC_MEAN"]
2471  );
2472  array_push($result, $row);
2473  }
2474  }
2475  return $result;
2476  }
2477 }
2478 ?>