00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
00025 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
00026
00038 class SurveyMatrixQuestion extends SurveyQuestion
00039 {
00047 var $columns;
00048
00056 var $neutralColumn;
00057
00065 var $rows;
00066
00074 var $bipolar_adjective1;
00075
00083 var $bipolar_adjective2;
00084
00092 var $columnSeparators;
00093
00101 var $rowSeparators;
00102
00110 var $neutralColumnSeparator;
00111
00112
00113
00114
00115
00116
00117 var $layout;
00118
00119
00120
00121
00122
00123
00124 var $columnPlaceholders;
00125
00126
00127
00128
00129
00130
00131 var $legend;
00132
00133 var $singleLineRowCaption;
00134
00135 var $repeatColumnHeader;
00136
00137 var $columnHeaderPosition;
00138
00139
00140
00141
00142
00143
00144 var $randomRows;
00145
00146 var $columnOrder;
00147
00148 var $columnImages;
00149
00150 var $rowImages;
00151
00152
00167 var $subtype;
00168
00180 function SurveyMatrixQuestion(
00181 $title = "",
00182 $description = "",
00183 $author = "",
00184 $questiontext = "",
00185 $owner = -1
00186 )
00187
00188 {
00189 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00190 $this->subtype = 0;
00191 $this->columns = array();
00192 $this->rows = array();
00193 $this->neutralColumn = "";
00194 $this->bipolar_adjective1 = "";
00195 $this->bipolar_adjective2 = "";
00196 $this->rowSeparators = 0;
00197 $this->columnSeparators = 0;
00198 $this->neutralColumnSeparator = 1;
00199 }
00200
00210 function getNeutralColumn()
00211 {
00212 return $this->neutralColumn;
00213 }
00214
00224 function getNeutralColumnIndex()
00225 {
00226 if (strlen($this->getNeutralColumn()))
00227 {
00228 return $this->getColumnCount();
00229 }
00230 else
00231 {
00232 return FALSE;
00233 }
00234 }
00235
00245 function setNeutralColumn($a_text)
00246 {
00247 $this->neutralColumn = $a_text;
00248 }
00249
00259 function getColumnCount()
00260 {
00261 return count($this->columns);
00262 }
00263
00274 function addColumnAtPosition($columnname, $position)
00275 {
00276 if (array_key_exists($position, $this->columns))
00277 {
00278 $head = array_slice($this->columns, 0, $position);
00279 $tail = array_slice($this->columns, $position);
00280 $this->columns = array_merge($head, array($columnname), $tail);
00281 }
00282 else
00283 {
00284 array_push($this->columns, $columnname);
00285 }
00286 }
00287
00298 function addColumn($columnname)
00299 {
00300 array_push($this->columns, $columnname);
00301 }
00302
00312 function addColumnArray($columns)
00313 {
00314 $this->columns = array_merge($this->columns, $columns);
00315 }
00316
00326 function removeColumn($index)
00327 {
00328 unset($this->columns[$index]);
00329 $this->columns = array_values($this->columns);
00330 }
00331
00341 function removeColumns($array)
00342 {
00343 foreach ($array as $index)
00344 {
00345 unset($this->columns[$index]);
00346 }
00347 $this->columns = array_values($this->columns);
00348 }
00349
00359 function removeColumnWithName($name)
00360 {
00361 foreach ($this->columns as $index => $column)
00362 {
00363 if (strcmp($column, $name) == 0)
00364 {
00365 return $this->removeColumn($index);
00366 }
00367 }
00368 }
00369
00380 function getColumn($index)
00381 {
00382 if (array_key_exists($index, $this->columns))
00383 {
00384 return $this->columns[$index];
00385 }
00386 else
00387 {
00388 if (($index = $this->getColumnCount()) && (strlen($this->getNeutralColumn())))
00389 {
00390 return $this->getNeutralColumn();
00391 }
00392 else
00393 {
00394 return "";
00395 }
00396 }
00397 }
00398
00408 function getColumnIndex($name)
00409 {
00410 foreach ($this->columns as $index => $column)
00411 {
00412 if (strcmp($column, $name) == 0)
00413 {
00414 return $index;
00415 }
00416 }
00417 return -1;
00418 }
00419
00420
00429 function flushColumns()
00430 {
00431 $this->columns = array();
00432 }
00433
00442 function getRowCount()
00443 {
00444 return count($this->rows);
00445 }
00446
00455 function addRow($a_text)
00456 {
00457 array_push($this->rows, $a_text);
00458 }
00459
00468 function flushRows()
00469 {
00470 $this->rows = array();
00471 }
00472
00481 function getRow($a_index)
00482 {
00483 if (array_key_exists($a_index, $this->rows))
00484 {
00485 return $this->rows[$a_index];
00486 }
00487 return "";
00488 }
00489
00499 function removeRows($array)
00500 {
00501 foreach ($array as $index)
00502 {
00503 unset($this->rows[$index]);
00504 }
00505 $this->rows = array_values($this->rows);
00506 }
00507
00517 function getBipolarAdjective($a_index)
00518 {
00519 switch ($a_index)
00520 {
00521 case 1:
00522 return $this->bipolar_adjective2;
00523 break;
00524 case 0:
00525 default:
00526 return $this->bipolar_adjective1;
00527 break;
00528 }
00529 }
00530
00540 function setBipolarAdjective($a_index, $a_value)
00541 {
00542 switch ($a_index)
00543 {
00544 case 1:
00545 $this->bipolar_adjective2 = $a_value;
00546 break;
00547 case 0:
00548 default:
00549 $this->bipolar_adjective1 = $a_value;
00550 break;
00551 }
00552 }
00553
00562 function addPhrase($phrase_id)
00563 {
00564 global $ilUser;
00565 global $ilDB;
00566
00567 $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",
00568 $ilDB->quote($phrase_id),
00569 $ilDB->quote($ilUser->id)
00570 );
00571 $result = $ilDB->query($query);
00572 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00573 {
00574 if (($row->defaultvalue == 1) and ($row->owner_fi == 0))
00575 {
00576 $this->addColumn($this->lng->txt($row->title));
00577 }
00578 else
00579 {
00580 $this->addColumn($row->title);
00581 }
00582 }
00583 }
00584
00594 function _getQuestionDataArray($id)
00595 {
00596 global $ilDB;
00597
00598 $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",
00599 $ilDB->quote($id)
00600 );
00601 $result = $ilDB->query($query);
00602 if ($result->numRows() == 1)
00603 {
00604 return $result->fetchRow(DB_FETCHMODE_ASSOC);
00605 }
00606 else
00607 {
00608 return array();
00609 }
00610 }
00611
00620 function loadFromDb($id)
00621 {
00622 global $ilDB;
00623 $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",
00624 $ilDB->quote($id)
00625 );
00626 $result = $ilDB->query($query);
00627 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00628 {
00629 if ($result->numRows() == 1)
00630 {
00631 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00632 $this->id = $data->question_id;
00633 $this->title = $data->title;
00634 $this->description = $data->description;
00635 $this->obj_id = $data->obj_fi;
00636 $this->author = $data->author;
00637 $this->owner = $data->owner_fi;
00638 include_once("./Services/RTE/classes/class.ilRTE.php");
00639 $this->questiontext = ilRTE::_replaceMediaObjectImageSrc($data->questiontext, 1);
00640 $this->obligatory = $data->obligatory;
00641 $this->complete = $data->complete;
00642 $this->original_id = $data->original_id;
00643 $this->setSubtype($data->subtype);
00644 $this->setRowSeparators($data->row_separators);
00645 $this->setNeutralColumnSeparator($data->neutral_column_separator);
00646 $this->setColumnSeparators($data->column_separators);
00647 $this->setColumnPlaceholders($data->column_placeholders);
00648 $this->setLegend($data->legend);
00649 $this->getSingleLineRowCaption($data->singleline_row_caption);
00650 $this->getRepeatColumnHeader($data->repeat_column_header);
00651 $this->getColumnHeaderPosition($data->column_header_position);
00652 $this->getRandomRows($data->random_rows);
00653 $this->getColumnOrder($data->column_order);
00654 $this->getColumnImages($data->column_images);
00655 $this->getRowImages($data->row_images);
00656 $this->setBipolarAdjective(0, $data->bipolar_adjective1);
00657 $this->setBipolarAdjective(1, $data->bipolar_adjective2);
00658 $this->setLayout($data->layout);
00659 }
00660
00661 $this->loadMaterialFromDb($id);
00662
00663 $this->flushColumns();
00664
00665 $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",
00666 $ilDB->quote($id)
00667 );
00668 $result = $ilDB->query($query);
00669 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00670 {
00671 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00672 {
00673 if ($data->neutral == 0)
00674 {
00675 $this->addColumn($data->title);
00676 }
00677 else
00678 {
00679 $this->setNeutralColumn($data->title);
00680 }
00681 }
00682 }
00683
00684 $query = sprintf("SELECT * FROM survey_question_matrix_rows WHERE question_fi = %s ORDER BY sequence",
00685 $ilDB->quote($id . "")
00686 );
00687 $result = $ilDB->query($query);
00688 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00689 {
00690 $this->addRow($row["title"]);
00691 }
00692 }
00693 parent::loadFromDb($id);
00694 }
00695
00704 function isComplete()
00705 {
00706 if (
00707 strlen($this->getTitle()) &&
00708 strlen($this->getAuthor()) &&
00709 strlen($this->getQuestiontext()) &&
00710 $this->getColumnCount() &&
00711 $this->getRowCount()
00712 )
00713 {
00714 return 1;
00715 }
00716 else
00717 {
00718 return 0;
00719 }
00720 }
00721
00729 function saveToDb($original_id = "", $withanswers = true)
00730 {
00731 global $ilDB;
00732 $complete = 0;
00733 if ($this->isComplete())
00734 {
00735 $complete = 1;
00736 }
00737 if ($original_id)
00738 {
00739 $original_id = $ilDB->quote($original_id);
00740 }
00741 else
00742 {
00743 $original_id = "NULL";
00744 }
00745
00746
00747 include_once("./Services/RTE/classes/class.ilRTE.php");
00748 ilRTE::_cleanupMediaObjectUsage($this->questiontext, "spl:html",
00749 $this->getId());
00750
00751 if ($this->id == -1)
00752 {
00753
00754 $now = getdate();
00755 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00756 $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)",
00757 $ilDB->quote($this->getQuestionTypeID()),
00758 $ilDB->quote($this->obj_id),
00759 $ilDB->quote($this->owner),
00760 $ilDB->quote($this->title),
00761 $ilDB->quote($this->description),
00762 $ilDB->quote($this->author),
00763 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00764 $ilDB->quote(sprintf("%d", $this->obligatory)),
00765 $ilDB->quote("$complete"),
00766 $ilDB->quote($created),
00767 $original_id
00768 );
00769 $result = $ilDB->query($query);
00770 if ($result == DB_OK)
00771 {
00772 $this->id = $ilDB->getLastInsertId();
00773 $query = sprintf("INSERT INTO survey_question_matrix (
00774 question_fi,
00775 subtype,
00776 column_separators,
00777 row_separators,
00778 neutral_column_separator,
00779 column_placeholders,
00780 legend,
00781 singleline_row_caption,
00782 repeat_column_header,
00783 column_header_position,
00784 random_rows,
00785 column_order,
00786 column_images,
00787 row_images,
00788 bipolar_adjective1,
00789 bipolar_adjective2,
00790 layout
00791 )
00792 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
00793 $ilDB->quote($this->id . ""),
00794 $ilDB->quote(sprintf("%d", $this->getSubtype())),
00795 $ilDB->quote($this->getColumnSeparators() . ""),
00796 $ilDB->quote($this->getRowSeparators() . ""),
00797 $ilDB->quote($this->getNeutralColumnSeparator() . ""),
00798 $ilDB->quote($this->getColumnPlaceholders() . ""),
00799 $ilDB->quote($this->getLegend() . ""),
00800 $ilDB->quote($this->getSingleLineRowCaption() . ""),
00801 $ilDB->quote($this->getRepeatColumnHeader() . ""),
00802 $ilDB->quote($this->getColumnHeaderPosition() . ""),
00803 $ilDB->quote($this->getRandomRows() . ""),
00804 $ilDB->quote($this->getColumnOrder() . ""),
00805 $ilDB->quote($this->getColumnImages() . ""),
00806 $ilDB->quote($this->getRowImages() . ""),
00807 $ilDB->quote($this->getBipolarAdjective(0) . ""),
00808 $ilDB->quote($this->getBipolarAdjective(1) . ""),
00809 $ilDB->quote(serialize($this->getLayout()) . "")
00810 );
00811 $ilDB->query($query);
00812 }
00813 }
00814 else
00815 {
00816
00817 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00818 $ilDB->quote($this->title),
00819 $ilDB->quote($this->description),
00820 $ilDB->quote($this->author),
00821 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00822 $ilDB->quote(sprintf("%d", $this->obligatory)),
00823 $ilDB->quote("$complete"),
00824 $ilDB->quote($this->id)
00825 );
00826 $result = $ilDB->query($query);
00827 $query = sprintf("UPDATE survey_question_matrix SET
00828 subtype = %s,
00829 column_separators = %s,
00830 row_separators = %s,
00831 neutral_column_separator = %s,
00832 column_placeholders = %s,
00833 legend = %s,
00834 singleline_row_caption = %s,
00835 repeat_column_header = %s,
00836 column_header_position = %s,
00837 random_rows = %s,
00838 column_order = %s,
00839 column_images = %s,
00840 row_images = %s,
00841 bipolar_adjective1 = %s,
00842 bipolar_adjective2 = %s,
00843 layout = %s
00844 WHERE question_fi = %s",
00845 $ilDB->quote(sprintf("%d", $this->getSubtype())),
00846 $ilDB->quote($this->getColumnSeparators() . ""),
00847 $ilDB->quote($this->getRowSeparators() . ""),
00848 $ilDB->quote($this->getNeutralColumnSeparator() . ""),
00849 $ilDB->quote($this->getColumnPlaceholders() . ""),
00850 $ilDB->quote($this->getLegend() . ""),
00851 $ilDB->quote($this->getSingleLineRowCaption() . ""),
00852 $ilDB->quote($this->getRepeatColumnHeader() . ""),
00853 $ilDB->quote($this->getColumnHeaderPosition() . ""),
00854 $ilDB->quote($this->getRandomRows() . ""),
00855 $ilDB->quote($this->getColumnOrder() . ""),
00856 $ilDB->quote($this->getColumnImages() . ""),
00857 $ilDB->quote($this->getRowImages() . ""),
00858 $ilDB->quote($this->getBipolarAdjective(0) . ""),
00859 $ilDB->quote($this->getBipolarAdjective(1) . ""),
00860 $ilDB->quote(serialize($this->getLayout()) . ""),
00861 $ilDB->quote($this->id . "")
00862 );
00863 $result = $ilDB->query($query);
00864 }
00865 if ($result == DB_OK)
00866 {
00867
00868 $this->saveMaterialsToDb();
00869 if ($withanswers)
00870 {
00871 $this->saveColumnsToDb();
00872 $this->saveRowsToDb();
00873 }
00874 }
00875 parent::saveToDb($original_id);
00876 }
00877
00878 function saveBipolarAdjectives($adjective1, $adjective2)
00879 {
00880 global $ilDB;
00881
00882 $query = sprintf("UPDATE survey_question_matrix SET bipolar_adjective1 = %s, bipolar_adjective2 = %s WHERE question_fi = %s",
00883 $ilDB->quote($adjective1 . ""),
00884 $ilDB->quote($adjective2 . ""),
00885 $ilDB->quote($this->getId() . "")
00886 );
00887 $result = $ilDB->query($query);
00888 }
00889
00900 function saveColumnToDb($columntext, $neutral = 0)
00901 {
00902 global $ilUser, $ilDB;
00903
00904 $query = sprintf("SELECT title, category_id FROM survey_category WHERE title = %s AND neutral = %s AND owner_fi = %s",
00905 $ilDB->quote($columntext . ""),
00906 $ilDB->quote($neutral . ""),
00907 $ilDB->quote($ilUser->getId() . "")
00908 );
00909 $result = $ilDB->query($query);
00910 $insert = FALSE;
00911 $returnvalue = "";
00912 if ($result->numRows())
00913 {
00914 $insert = TRUE;
00915 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00916 {
00917 if (strcmp($row->title, $columntext) == 0)
00918 {
00919 $returnvalue = $row->category_id;
00920 $insert = FALSE;
00921 }
00922 }
00923 }
00924 else
00925 {
00926 $insert = TRUE;
00927 }
00928 if ($insert)
00929 {
00930 $query = sprintf("INSERT INTO survey_category (category_id, title, neutral, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00931 $ilDB->quote($columntext . ""),
00932 $ilDB->quote($neutral . ""),
00933 $ilDB->quote($ilUser->getId() . "")
00934 );
00935 $result = $ilDB->query($query);
00936 $returnvalue = $ilDB->getLastInsertId();
00937 }
00938 return $returnvalue;
00939 }
00940
00941
00942 function saveColumnsToDb($original_id = "")
00943 {
00944 global $ilDB;
00945
00946
00947 $question_id = $this->getId();
00948 if (strlen($original_id))
00949 {
00950 $question_id = $original_id;
00951 }
00952
00953
00954 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00955 $ilDB->quote($question_id)
00956 );
00957 $result = $ilDB->query($query);
00958
00959 for ($i = 0; $i < $this->getColumnCount(); $i++)
00960 {
00961 $cat = $this->getColumn($i);
00962 $column_id = $this->saveColumnToDb($cat);
00963 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00964 $ilDB->quote($column_id . ""),
00965 $ilDB->quote($question_id . ""),
00966 $ilDB->quote(($i + 1) . ""),
00967 $ilDB->quote($i . "")
00968 );
00969 $answer_result = $ilDB->query($query);
00970 }
00971 if (strlen($this->getNeutralColumn()))
00972 {
00973 $column_id = $this->saveColumnToDb($this->getNeutralColumn(), 1);
00974 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00975 $ilDB->quote($column_id . ""),
00976 $ilDB->quote($question_id . ""),
00977 $ilDB->quote(($i + 1) . ""),
00978 $ilDB->quote($i . "")
00979 );
00980 $answer_result = $ilDB->query($query);
00981 }
00982 $this->saveCompletionStatus($original_id);
00983 }
00984
00985 function saveRowsToDb($original_id = "")
00986 {
00987 global $ilDB;
00988
00989
00990 $question_id = $this->getId();
00991 if (strlen($original_id))
00992 {
00993 $question_id = $original_id;
00994 }
00995
00996
00997 $query = sprintf("DELETE FROM survey_question_matrix_rows WHERE question_fi = %s",
00998 $ilDB->quote($question_id . "")
00999 );
01000 $result = $ilDB->query($query);
01001
01002 for ($i = 0; $i < $this->getRowCount(); $i++)
01003 {
01004 $row = $this->getRow($i);
01005 $query = sprintf("INSERT INTO survey_question_matrix_rows (id_survey_question_matrix_rows, title, sequence, question_fi) VALUES (NULL, %s, %s, %s)",
01006 $ilDB->quote($row . ""),
01007 $ilDB->quote($i . ""),
01008 $ilDB->quote($question_id . "")
01009 );
01010 $answer_result = $ilDB->query($query);
01011 }
01012 $this->saveCompletionStatus($original_id);
01013 }
01014
01023 function toXML($a_include_header = TRUE, $obligatory_state = "")
01024 {
01025 include_once("./classes/class.ilXmlWriter.php");
01026 $a_xml_writer = new ilXmlWriter;
01027 $a_xml_writer->xmlHeader();
01028 $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
01029 $xml = $a_xml_writer->xmlDumpMem(FALSE);
01030 if (!$a_include_header)
01031 {
01032 $pos = strpos($xml, "?>");
01033 $xml = substr($xml, $pos + 2);
01034 }
01035 return $xml;
01036 }
01037
01048 function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
01049 {
01050 $attrs = array(
01051 "id" => $this->getId(),
01052 "title" => $this->getTitle(),
01053 "type" => $this->getQuestiontype(),
01054 "subtype" => $this->getSubtype(),
01055 "obligatory" => $this->getObligatory()
01056 );
01057 $a_xml_writer->xmlStartTag("question", $attrs);
01058
01059 $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
01060 $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
01061 $a_xml_writer->xmlStartTag("questiontext");
01062 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
01063 $a_xml_writer->xmlEndTag("questiontext");
01064
01065 $a_xml_writer->xmlStartTag("matrix");
01066 $a_xml_writer->xmlStartTag("matrixrows");
01067 for ($i = 0; $i < $this->getRowCount(); $i++)
01068 {
01069 $attrs = array(
01070 "id" => $i
01071 );
01072 $a_xml_writer->xmlStartTag("matrixrow", $attrs);
01073 $this->addMaterialTag($a_xml_writer, $this->getRow($i));
01074 $a_xml_writer->xmlEndTag("matrixrow");
01075 }
01076 $a_xml_writer->xmlEndTag("matrixrows");
01077
01078 $a_xml_writer->xmlStartTag("responses");
01079 if (strlen($this->getBipolarAdjective(0)) && (strlen($this->getBipolarAdjective(1))))
01080 {
01081 $a_xml_writer->xmlStartTag("bipolar_adjectives");
01082 $attribs = array(
01083 "label" => "0"
01084 );
01085 $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(0));
01086 $attribs = array(
01087 "label" => "1"
01088 );
01089 $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(1));
01090 $a_xml_writer->xmlEndTag("bipolar_adjectives");
01091 }
01092 for ($i = 0; $i < $this->getColumnCount(); $i++)
01093 {
01094 $attrs = array(
01095 "id" => $i
01096 );
01097 switch ($this->getSubtype())
01098 {
01099 case 0:
01100 $a_xml_writer->xmlStartTag("response_single", $attrs);
01101 break;
01102 case 1:
01103 $a_xml_writer->xmlStartTag("response_multiple", $attrs);
01104 break;
01105 }
01106 $this->addMaterialTag($a_xml_writer, $this->getColumn($i));
01107 switch ($this->getSubtype())
01108 {
01109 case 0:
01110 $a_xml_writer->xmlEndTag("response_single");
01111 break;
01112 case 1:
01113 $a_xml_writer->xmlEndTag("response_multiple");
01114 break;
01115 }
01116 }
01117 if (strlen($this->getNeutralColumn()))
01118 {
01119 $attrs = array(
01120 "id" => $this->getColumnCount(),
01121 "label" => "neutral"
01122 );
01123 switch ($this->getSubtype())
01124 {
01125 case 0:
01126 $a_xml_writer->xmlStartTag("response_single", $attrs);
01127 break;
01128 case 1:
01129 $a_xml_writer->xmlStartTag("response_multiple", $attrs);
01130 break;
01131 }
01132 $this->addMaterialTag($a_xml_writer, $this->getNeutralColumn());
01133 switch ($this->getSubtype())
01134 {
01135 case 0:
01136 $a_xml_writer->xmlEndTag("response_single");
01137 break;
01138 case 1:
01139 $a_xml_writer->xmlEndTag("response_multiple");
01140 break;
01141 }
01142 }
01143
01144 $a_xml_writer->xmlEndTag("responses");
01145 $a_xml_writer->xmlEndTag("matrix");
01146
01147 if (count($this->material))
01148 {
01149 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
01150 {
01151 $attrs = array(
01152 "label" => $this->material["title"]
01153 );
01154 $a_xml_writer->xmlStartTag("material", $attrs);
01155 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
01156 if (strcmp($matches[1], "") != 0)
01157 {
01158 $intlink = $this->material["internal_link"];
01159 }
01160 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
01161 $a_xml_writer->xmlEndTag("material");
01162 }
01163 }
01164
01165 $a_xml_writer->xmlStartTag("metadata");
01166 $a_xml_writer->xmlStartTag("metadatafield");
01167 $a_xml_writer->xmlElement("fieldlabel", NULL, "column_separators");
01168 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getColumnSeparators());
01169 $a_xml_writer->xmlEndTag("metadatafield");
01170
01171 $a_xml_writer->xmlStartTag("metadatafield");
01172 $a_xml_writer->xmlElement("fieldlabel", NULL, "row_separators");
01173 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getRowSeparators());
01174 $a_xml_writer->xmlEndTag("metadatafield");
01175
01176 $a_xml_writer->xmlStartTag("metadatafield");
01177 $a_xml_writer->xmlElement("fieldlabel", NULL, "neutral_column_separator");
01178 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getNeutralColumnSeparator());
01179 $a_xml_writer->xmlEndTag("metadatafield");
01180
01181 $a_xml_writer->xmlStartTag("metadatafield");
01182 $a_xml_writer->xmlElement("fieldlabel", NULL, "layout");
01183 $a_xml_writer->xmlElement("fieldentry", NULL, serialize($this->getLayout()));
01184 $a_xml_writer->xmlEndTag("metadatafield");
01185
01186 $a_xml_writer->xmlEndTag("metadata");
01187
01188 $a_xml_writer->xmlEndTag("question");
01189 }
01190
01191 function syncWithOriginal()
01192 {
01193 global $ilDB;
01194 if ($this->original_id)
01195 {
01196 $complete = 0;
01197 if ($this->isComplete())
01198 {
01199 $complete = 1;
01200 }
01201 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
01202 $ilDB->quote($this->title . ""),
01203 $ilDB->quote($this->description . ""),
01204 $ilDB->quote($this->author . ""),
01205 $ilDB->quote($this->questiontext . ""),
01206 $ilDB->quote(sprintf("%d", $this->obligatory) . ""),
01207 $ilDB->quote($complete . ""),
01208 $ilDB->quote($this->original_id . "")
01209 );
01210 $result = $ilDB->query($query);
01211 $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",
01212 $ilDB->quote($this->getRowSeparators() . ""),
01213 $ilDB->quote($this->getColumnSeparators() . ""),
01214 $ilDB->quote($this->getNeutralColumnSeparator() . ""),
01215 $ilDB->quote($this->getBipolarAdjective(0) . ""),
01216 $ilDB->quote($this->getBipolarAdjective(1) . ""),
01217 $ilDB->quote($this->original_id . "")
01218 );
01219 $result = $ilDB->query($query);
01220 if ($result == DB_OK)
01221 {
01222
01223 $this->saveColumnsToDb($this->original_id);
01224
01225 $this->saveRowsToDb($this->original_id);
01226 }
01227 }
01228 parent::syncWithOriginal();
01229 }
01230
01240 function addStandardNumbers($lower_limit, $upper_limit)
01241 {
01242 for ($i = $lower_limit; $i <= $upper_limit; $i++)
01243 {
01244 $this->addColumn($i);
01245 }
01246 }
01247
01257 function savePhrase($phrases, $title)
01258 {
01259 global $ilUser;
01260 global $ilDB;
01261
01262 $query = sprintf("INSERT INTO survey_phrase (phrase_id, title, defaultvalue, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
01263 $ilDB->quote($title . ""),
01264 $ilDB->quote("1"),
01265 $ilDB->quote($ilUser->id . "")
01266 );
01267 $result = $ilDB->query($query);
01268 $phrase_id = $ilDB->getLastInsertId();
01269
01270 $counter = 1;
01271 foreach ($phrases as $column_index)
01272 {
01273 $column = $this->getColumn($column_index);
01274 $query = sprintf("INSERT INTO survey_category (category_id, title, neutral, defaultvalue, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
01275 $ilDB->quote($column . ""),
01276 $ilDB->quote("0"),
01277 $ilDB->quote("1"),
01278 $ilDB->quote($ilUser->getId() . "")
01279 );
01280 $result = $ilDB->query($query);
01281 $column_id = $ilDB->getLastInsertId();
01282 $query = sprintf("INSERT INTO survey_phrase_category (phrase_category_id, phrase_fi, category_fi, sequence) VALUES (NULL, %s, %s, %s)",
01283 $ilDB->quote($phrase_id . ""),
01284 $ilDB->quote($column_id . ""),
01285 $ilDB->quote($counter . "")
01286 );
01287 $result = $ilDB->query($query);
01288 $counter++;
01289 }
01290 }
01291
01300 function getQuestionType()
01301 {
01302 return "SurveyMatrixQuestion";
01303 }
01304
01313 function getAdditionalTableName()
01314 {
01315 return "survey_question_matrix";
01316 }
01317
01326 function &getWorkingDataFromUserInput($post_data)
01327 {
01328 $data = array();
01329 foreach ($post_data as $key => $value)
01330 {
01331 switch ($this->getSubtype())
01332 {
01333 case 0:
01334 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01335 {
01336 if (is_array($value))
01337 {
01338 foreach ($value as $val)
01339 {
01340 array_push($data, array("value" => $val, "row" => $matches[1]));
01341 }
01342 }
01343 else
01344 {
01345 array_push($data, array("value" => $value, "row" => $matches[1]));
01346 }
01347 }
01348 break;
01349 case 1:
01350 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01351 {
01352 if (is_array($value))
01353 {
01354 foreach ($value as $val)
01355 {
01356 array_push($data, array("value" => $val, "row" => $matches[1]));
01357 }
01358 }
01359 else
01360 {
01361 array_push($data, array("value" => $value, "row" => $matches[1]));
01362 }
01363 }
01364 break;
01365 }
01366 }
01367 return $data;
01368 }
01369
01382 function checkUserInput($post_data, $survey_id)
01383 {
01384 if (!$this->getObligatory($survey_id)) return "";
01385 switch ($this->getSubtype())
01386 {
01387 case 0:
01388 $counter = 0;
01389 foreach ($post_data as $key => $value)
01390 {
01391 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01392 {
01393 $counter++;
01394 }
01395 }
01396 if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_radio_button_not_checked");
01397 break;
01398 case 1:
01399 $counter = 0;
01400 foreach ($post_data as $key => $value)
01401 {
01402 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01403 {
01404 $counter++;
01405 if ((!is_array($value)) || (count($value) < 1))
01406 {
01407 return $this->lng->txt("matrix_question_checkbox_not_checked");
01408 }
01409 }
01410 }
01411 if ($counter != $this->getRowCount()) return $this->lng->txt("matrix_question_checkbox_not_checked");
01412 break;
01413 }
01414 return "";
01415 }
01416
01417 function saveUserInput($post_data, $active_id)
01418 {
01419 global $ilDB;
01420
01421 switch ($this->getSubtype())
01422 {
01423 case 0:
01424 foreach ($post_data as $key => $value)
01425 {
01426 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01427 {
01428 $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
01429 $ilDB->quote($this->getId() . ""),
01430 $ilDB->quote($active_id . ""),
01431 $ilDB->quote($value . ""),
01432 "NULL",
01433 $ilDB->quote($matches[1] . "")
01434 );
01435 $result = $ilDB->query($query);
01436 }
01437 }
01438 break;
01439 case 1:
01440 foreach ($post_data as $key => $value)
01441 {
01442 if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches))
01443 {
01444 foreach ($value as $checked)
01445 {
01446 if (strlen($checked))
01447 {
01448 $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, row, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
01449 $ilDB->quote($this->getId() . ""),
01450 $ilDB->quote($active_id . ""),
01451 $ilDB->quote($checked . ""),
01452 "NULL",
01453 $ilDB->quote($matches[1] . "")
01454 );
01455 $result = $ilDB->query($query);
01456 }
01457 }
01458 }
01459 }
01460 break;
01461 }
01462 }
01463
01472 function deleteAdditionalTableData($question_id)
01473 {
01474 parent::deleteAdditionalTableData($question_id);
01475
01476 global $ilDB;
01477 $query = sprintf("DELETE FROM survey_question_matrix_rows WHERE question_fi = %s",
01478 $ilDB->quote($question_id . "")
01479 );
01480 $result = $ilDB->query($query);
01481 }
01482
01492 function getNrOfUsersAnswered($survey_id)
01493 {
01494 global $ilDB;
01495
01496 $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",
01497 $ilDB->quote($this->getId() . ""),
01498 $ilDB->quote($survey_id . "")
01499 );
01500 $result = $ilDB->query($query);
01501 return $result->numRows();
01502 }
01503
01514 function &getCumulatedResultsForRow($rowindex, $survey_id, $nr_of_users)
01515 {
01516 global $ilDB;
01517
01518 $question_id = $this->getId();
01519
01520 $result_array = array();
01521 $cumulated = array();
01522
01523 $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",
01524 $ilDB->quote($question_id . ""),
01525 $ilDB->quote($survey_id . ""),
01526 $ilDB->quote($rowindex . "")
01527 );
01528 $result = $ilDB->query($query);
01529
01530 switch ($this->getSubtype())
01531 {
01532 case 0:
01533 case 1:
01534 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01535 {
01536 $cumulated[$row->value]++;
01537 }
01538 asort($cumulated, SORT_NUMERIC);
01539 end($cumulated);
01540 break;
01541 }
01542 $numrows = $result->numRows();
01543 $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
01544 $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
01545
01546 $prefix = "";
01547 if (strcmp(key($cumulated), "") != 0)
01548 {
01549 $prefix = (key($cumulated)+1) . " - ";
01550 }
01551 $cat = $this->getColumn(key($cumulated));
01552 $result_array["MODE"] = $prefix . $cat;
01553 $result_array["MODE_VALUE"] = key($cumulated)+1;
01554 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
01555 for ($key = 0; $key < $this->getColumnCount(); $key++)
01556 {
01557 $percentage = 0;
01558 if ($numrows > 0)
01559 {
01560 $percentage = (float)((int)$cumulated[$key]/$numrows);
01561 }
01562 $cat = $this->getColumn($key);
01563 $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
01564 }
01565 if ($this->hasNeutralColumn())
01566 {
01567 $percentage = 0;
01568 if ($numrows > 0)
01569 {
01570 $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
01571 }
01572 $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
01573 }
01574 ksort($cumulated, SORT_NUMERIC);
01575 $median = array();
01576 $total = 0;
01577 foreach ($cumulated as $value => $key)
01578 {
01579 $total += $key;
01580 for ($i = 0; $i < $key; $i++)
01581 {
01582 array_push($median, $value+1);
01583 }
01584 }
01585 if ($total > 0)
01586 {
01587 if (($total % 2) == 0)
01588 {
01589 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
01590 if (round($median_value) != $median_value)
01591 {
01592 $cat = $this->getColumn((int)floor($median_value)-1);
01593 $cat2 = $this->getColumn((int)ceil($median_value)-1);
01594 $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
01595 }
01596 }
01597 else
01598 {
01599 $median_value = $median[(($total+1)/2)-1];
01600 }
01601 }
01602 else
01603 {
01604 $median_value = "";
01605 }
01606 $result_array["ARITHMETIC_MEAN"] = "";
01607 $result_array["MEDIAN"] = $median_value;
01608 $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
01609 $result_array["ROW"] = $this->getRow($rowindex);
01610 return $result_array;
01611 }
01612
01622 function &getCumulatedResults($survey_id, $nr_of_users)
01623 {
01624 global $ilDB;
01625
01626 $question_id = $this->getId();
01627
01628 $result_array = array();
01629 $cumulated = array();
01630
01631 $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",
01632 $ilDB->quote($question_id),
01633 $ilDB->quote($survey_id)
01634 );
01635 $result = $ilDB->query($query);
01636
01637 switch ($this->getSubtype())
01638 {
01639 case 0:
01640 case 1:
01641 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01642 {
01643 $cumulated[$row->value]++;
01644 }
01645 asort($cumulated, SORT_NUMERIC);
01646 end($cumulated);
01647 break;
01648 }
01649 $numrows = $result->numRows();
01650 $result_array["USERS_ANSWERED"] = $this->getNrOfUsersAnswered($survey_id);
01651 $result_array["USERS_SKIPPED"] = $nr_of_users - $this->getNrOfUsersAnswered($survey_id);
01652
01653 $prefix = "";
01654 if (strcmp(key($cumulated), "") != 0)
01655 {
01656 $prefix = (key($cumulated)+1) . " - ";
01657 }
01658 $cat = $this->getColumn(key($cumulated));
01659 $result_array["MODE"] = $prefix . $cat;
01660 $result_array["MODE_VALUE"] = key($cumulated)+1;
01661 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
01662 for ($key = 0; $key < $this->getColumnCount(); $key++)
01663 {
01664 $percentage = 0;
01665 if ($numrows > 0)
01666 {
01667 $percentage = (float)((int)$cumulated[$key]/$numrows);
01668 }
01669 $cat = $this->getColumn($key);
01670 $result_array["variables"][$key] = array("title" => $cat, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
01671 }
01672 if ($this->hasNeutralColumn())
01673 {
01674 $percentage = 0;
01675 if ($numrows > 0)
01676 {
01677 $percentage = (float)((int)$cumulated[$this->getNeutralColumnIndex()]/$numrows);
01678 }
01679 $result_array["variables"][$this->getNeutralColumnIndex()] = array("title" => $this->getNeutralColumn(), "selected" => (int)$cumulated[$this->getNeutralColumnIndex()], "percentage" => $percentage);
01680 }
01681 ksort($cumulated, SORT_NUMERIC);
01682 $median = array();
01683 $total = 0;
01684 foreach ($cumulated as $value => $key)
01685 {
01686 $total += $key;
01687 for ($i = 0; $i < $key; $i++)
01688 {
01689 array_push($median, $value+1);
01690 }
01691 }
01692 if ($total > 0)
01693 {
01694 if (($total % 2) == 0)
01695 {
01696 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
01697 if (round($median_value) != $median_value)
01698 {
01699 $cat = $this->getColumn((int)floor($median_value)-1);
01700 $cat2 = $this->getColumn((int)ceil($median_value)-1);
01701 $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $cat . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $cat2 . ")";
01702 }
01703 }
01704 else
01705 {
01706 $median_value = $median[(($total+1)/2)-1];
01707 }
01708 }
01709 else
01710 {
01711 $median_value = "";
01712 }
01713 $result_array["ARITHMETIC_MEAN"] = "";
01714 $result_array["MEDIAN"] = $median_value;
01715 $result_array["QUESTION_TYPE"] = "SurveyMatrixQuestion";
01716
01717 $cumulated_results = array();
01718 $cumulated_results["TOTAL"] = $result_array;
01719 for ($i = 0; $i < $this->getRowCount(); $i++)
01720 {
01721 $rowresult =& $this->getCumulatedResultsForRow($i, $survey_id, $nr_of_users);
01722 $cumulated_results[$i] = $rowresult;
01723 }
01724 return $cumulated_results;
01725 }
01726
01740 function setExportCumulatedXLS(&$worksheet, &$format_title, &$format_bold, &$eval_data, $row)
01741 {
01742 include_once ("./classes/class.ilExcelUtils.php");
01743 $worksheet->writeString($row, 0, ilExcelUtils::_convert_text($this->getTitle()));
01744 $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
01745 $worksheet->writeString($row, 2, ilExcelUtils::_convert_text($this->lng->txt($eval_data["TOTAL"]["QUESTION_TYPE"])));
01746 $worksheet->write($row, 3, $eval_data["TOTAL"]["USERS_ANSWERED"]);
01747 $worksheet->write($row, 4, $eval_data["TOTAL"]["USERS_SKIPPED"]);
01748 $worksheet->write($row, 5, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_VALUE"]));
01749 $worksheet->write($row, 6, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE"]));
01750 $worksheet->write($row, 7, $eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]);
01751 $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
01752 $worksheet->write($row, 9, $eval_data["TOTAL"]["ARITHMETIC_MEAN"]);
01753 $row++;
01754 foreach ($eval_data as $evalkey => $evalvalue)
01755 {
01756 if (is_numeric($evalkey))
01757 {
01758 $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
01759 $worksheet->write($row, 3, $evalvalue["USERS_ANSWERED"]);
01760 $worksheet->write($row, 4, $evalvalue["USERS_SKIPPED"]);
01761 $worksheet->write($row, 5, ilExcelUtils::_convert_text($evalvalue["MODE_VALUE"]));
01762 $worksheet->write($row, 6, ilExcelUtils::_convert_text($evalvalue["MODE"]));
01763 $worksheet->write($row, 7, $evalvalue["MODE_NR_OF_SELECTIONS"]);
01764 $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
01765 $worksheet->write($row, 9, $evalvalue["ARITHMETIC_MEAN"]);
01766 $row++;
01767 }
01768 }
01769 return $row;
01770 }
01771
01785 function &setExportCumulatedCVS(&$eval_data)
01786 {
01787 $result = array();
01788 foreach ($eval_data as $evalkey => $evalvalue)
01789 {
01790 $csvrow = array();
01791 if (is_numeric($evalkey))
01792 {
01793 array_push($csvrow, "");
01794 array_push($csvrow, $evalvalue["ROW"]);
01795 array_push($csvrow, "");
01796 }
01797 else
01798 {
01799 array_push($csvrow, $this->getTitle());
01800 array_push($csvrow, $this->getQuestiontext());
01801 array_push($csvrow, $this->lng->txt($evalvalue["QUESTION_TYPE"]));
01802 }
01803 array_push($csvrow, $evalvalue["USERS_ANSWERED"]);
01804 array_push($csvrow, $evalvalue["USERS_SKIPPED"]);
01805 array_push($csvrow, $evalvalue["MODE"]);
01806 array_push($csvrow, $evalvalue["MODE_NR_OF_SELECTIONS"]);
01807 array_push($csvrow, $evalvalue["MEDIAN"]);
01808 array_push($csvrow, $evalvalue["ARITHMETIC_MEAN"]);
01809 array_push($result, $csvrow);
01810 }
01811 return $result;
01812 }
01813
01825 function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
01826 {
01827 include_once ("./classes/class.ilExcelUtils.php");
01828 $worksheet =& $workbook->addWorksheet();
01829 $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
01830 $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
01831 $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
01832 $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
01833 $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
01834 $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
01835 $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
01836 $worksheet->write(3, 1, $eval_data["TOTAL"]["USERS_ANSWERED"]);
01837 $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
01838 $worksheet->write(4, 1, $eval_data["TOTAL"]["USERS_SKIPPED"]);
01839 $rowcounter = 5;
01840
01841 preg_match("/(.*?)\s+-\s+(.*)/", $eval_data["TOTAL"]["MODE"], $matches);
01842 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
01843 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
01844 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
01845 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
01846 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
01847 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($eval_data["TOTAL"]["MODE_NR_OF_SELECTIONS"]));
01848 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
01849 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["TOTAL"]["MEDIAN"])));
01850 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
01851 $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
01852 $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
01853 $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
01854 $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
01855
01856 foreach ($eval_data["TOTAL"]["variables"] as $key => $value)
01857 {
01858 $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
01859 $worksheet->write($rowcounter, 2, $key+1);
01860 $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
01861 $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
01862 }
01863
01864 foreach ($eval_data as $evalkey => $evalvalue)
01865 {
01866 if (is_numeric($evalkey))
01867 {
01868 $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("row")), $format_bold);
01869 $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($evalvalue["ROW"]));
01870 $worksheet->writeString($rowcounter + 1, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
01871 $worksheet->write($rowcounter + 1, 1, $evalvalue["USERS_ANSWERED"]);
01872 $worksheet->writeString($rowcounter + 2, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
01873 $worksheet->write($rowcounter + 2, 1, $evalvalue["USERS_SKIPPED"]);
01874 $rowcounter = $rowcounter + 3;
01875
01876 preg_match("/(.*?)\s+-\s+(.*)/", $evalvalue["MODE"], $matches);
01877 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode")), $format_bold);
01878 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[1]));
01879 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_text")), $format_bold);
01880 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($matches[2]));
01881 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("mode_nr_of_selections")), $format_bold);
01882 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($evalvalue["MODE_NR_OF_SELECTIONS"]));
01883 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("median")), $format_bold);
01884 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text(str_replace("<br />", " ", $evalvalue["MEDIAN"])));
01885 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("categories")), $format_bold);
01886 $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_title);
01887 $worksheet->write($rowcounter, 2, ilExcelUtils::_convert_text($this->lng->txt("value")), $format_title);
01888 $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($this->lng->txt("category_nr_selected")), $format_title);
01889 $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($this->lng->txt("percentage_of_selections")), $format_title);
01890
01891 foreach ($evalvalue["variables"] as $key => $value)
01892 {
01893 $worksheet->write($rowcounter, 1, ilExcelUtils::_convert_text($value["title"]));
01894 $worksheet->write($rowcounter, 2, $key+1);
01895 $worksheet->write($rowcounter, 3, ilExcelUtils::_convert_text($value["selected"]));
01896 $worksheet->write($rowcounter++, 4, ilExcelUtils::_convert_text($value["percentage"]), $format_percent);
01897 }
01898 }
01899 }
01900
01901 $format_center =& $workbook->addFormat();
01902 $format_center->setColor('black');
01903 $format_center->setAlign('center');
01904
01905 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("overview")), $format_bold);
01906
01907 $rowcounter++;
01908 $counter = 0;
01909 $worksheet->write($rowcounter, $counter, "", $format_title);
01910 foreach ($eval_data["TOTAL"]["variables"] as $variable)
01911 {
01912 $worksheet->write($rowcounter, 1+$counter, ilExcelUtils::_convert_text($variable["title"]), $format_title);
01913 $counter++;
01914 }
01915 $rowcounter++;
01916
01917 foreach ($eval_data as $index => $data)
01918 {
01919 if (is_numeric($index))
01920 {
01921 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($data["ROW"]), $format_title);
01922 $counter = 1;
01923 foreach ($data["variables"] as $vardata)
01924 {
01925 $worksheet->write($rowcounter, $counter, $vardata["selected"], $format_center);
01926 $counter++;
01927 }
01928 $rowcounter++;
01929 }
01930 }
01931 }
01932
01941 function addUserSpecificResultsExportTitles(&$a_array)
01942 {
01943 parent::addUserSpecificResultsExportTitles($a_array);
01944 for ($i = 0; $i < $this->getRowCount(); $i++)
01945 {
01946 array_push($a_array, $this->getRow($i));
01947 switch ($this->getSubtype())
01948 {
01949 case 0:
01950 break;
01951 case 1:
01952 for ($index = 0; $index < $this->getColumnCount(); $index++)
01953 {
01954 $col = $this->getColumn($index);
01955 array_push($a_array, ($index+1) . " - $col");
01956 }
01957 if (strlen($this->getNeutralColumn()))
01958 {
01959 array_push($a_array, ($this->getColumnCount()+1) . " - " . $this->getNeutralColumn());
01960 }
01961 break;
01962 }
01963 }
01964 }
01965
01975 function addUserSpecificResultsData(&$a_array, &$resultset)
01976 {
01977 if (count($resultset["answers"][$this->getId()]))
01978 {
01979 array_push($a_array, "");
01980 switch ($this->getSubtype())
01981 {
01982 case 0:
01983 for ($i = 0; $i < $this->getRowCount(); $i++)
01984 {
01985 $checked = FALSE;
01986 foreach ($resultset["answers"][$this->getId()] as $result)
01987 {
01988 if ($result["row"] == $i)
01989 {
01990 $checked = TRUE;
01991 array_push($a_array, $result["value"] + 1);
01992 }
01993 }
01994 if (!$checked)
01995 {
01996 array_push($a_array, $this->lng->txt("skipped"));
01997 }
01998 }
01999 break;
02000 case 1:
02001 for ($i = 0; $i < $this->getRowCount(); $i++)
02002 {
02003 $checked = FALSE;
02004 $checked_values = array();
02005 foreach ($resultset["answers"][$this->getId()] as $result)
02006 {
02007 if ($result["row"] == $i)
02008 {
02009 $checked = TRUE;
02010 array_push($checked_values, $result["value"] + 1);
02011 }
02012 }
02013 if (!$checked)
02014 {
02015 array_push($a_array, $this->lng->txt("skipped"));
02016 }
02017 else
02018 {
02019 array_push($a_array, "");
02020 }
02021 for ($index = 0; $index < $this->getColumnCount(); $index++)
02022 {
02023 if (!$checked)
02024 {
02025 array_push($a_array, "");
02026 }
02027 else
02028 {
02029 if (in_array($index+1, $checked_values))
02030 {
02031 array_push($a_array, 1);
02032 }
02033 else
02034 {
02035 array_push($a_array, 0);
02036 }
02037 }
02038 }
02039 if (strlen($this->getNeutralColumn()))
02040 {
02041 if (!$checked)
02042 {
02043 array_push($a_array, "");
02044 }
02045 else
02046 {
02047 if (in_array($this->getColumnCount()+1, $checked_values))
02048 {
02049 array_push($a_array, 1);
02050 }
02051 else
02052 {
02053 array_push($a_array, 0);
02054 }
02055 }
02056 }
02057 }
02058 break;
02059 }
02060 }
02061 else
02062 {
02063 array_push($a_array, $this->lng->txt("skipped"));
02064 for ($i = 0; $i < $this->getRowCount(); $i++)
02065 {
02066 array_push($a_array, "");
02067 switch ($this->getSubtype())
02068 {
02069 case 0:
02070 break;
02071 case 1:
02072 for ($index = 0; $index < $this->getColumnCount(); $index++)
02073 {
02074 array_push($a_array, "");
02075 }
02076 if (strlen($this->getNeutralColumn()))
02077 {
02078 array_push($a_array, "");
02079 }
02080 break;
02081 }
02082 }
02083 }
02084 }
02085
02095 function &getUserAnswers($survey_id)
02096 {
02097 global $ilDB;
02098
02099 $answers = array();
02100
02101 $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",
02102 $ilDB->quote($survey_id),
02103 $ilDB->quote($this->getId())
02104 );
02105 $result = $ilDB->query($query);
02106 $results = array();
02107 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02108 {
02109 $column = $this->getColumn($row["value"]);
02110 if (!is_array($answers[$row["active_fi"]])) $answers[$row["active_fi"]] = array();
02111 array_push($answers[$row["active_fi"]], $this->getRow($row["row"]) . ": " . ($row["value"] + 1) . " - " . $column);
02112 }
02113 foreach ($answers as $key => $value)
02114 {
02115 $answers[$key] = implode("<br />", $value);
02116 }
02117 return $answers;
02118 }
02119
02128 function getSubtype()
02129 {
02130 return $this->subtype;
02131 }
02132
02141 function setSubtype($a_subtype = 0)
02142 {
02143 switch ($a_subtype)
02144 {
02145 case 1:
02146 case 2:
02147 case 3:
02148 case 4:
02149 case 5:
02150 case 6:
02151 $this->subtype = $a_subtype;
02152 break;
02153 case 0:
02154 default:
02155 $this->subtype = 0;
02156 break;
02157 }
02158 }
02159
02168 function setColumnSeparators($enable = 0)
02169 {
02170 switch ($enable)
02171 {
02172 case 1:
02173 $this->columnSeparators = 1;
02174 break;
02175 case 0:
02176 default:
02177 $this->columnSeparators = 0;
02178 break;
02179 }
02180 }
02181
02190 function getColumnSeparators()
02191 {
02192 return $this->columnSeparators;
02193 }
02194
02203 function setRowSeparators($enable = 0)
02204 {
02205 switch ($enable)
02206 {
02207 case 1:
02208 $this->rowSeparators = 1;
02209 break;
02210 case 0:
02211 default:
02212 $this->rowSeparators = 0;
02213 break;
02214 }
02215 }
02216
02225 function getRowSeparators()
02226 {
02227 return $this->rowSeparators;
02228 }
02229
02238 function setNeutralColumnSeparator($enable = 0)
02239 {
02240 switch ($enable)
02241 {
02242 case 1:
02243 $this->neutralColumnSeparator = 1;
02244 break;
02245 case 0:
02246 default:
02247 $this->neutralColumnSeparator = 0;
02248 break;
02249 }
02250 }
02251
02260 function getNeutralColumnSeparator()
02261 {
02262 return $this->neutralColumnSeparator;
02263 }
02264
02275 function importAdditionalMetadata($a_meta)
02276 {
02277 foreach ($a_meta as $key => $value)
02278 {
02279 switch ($value["label"])
02280 {
02281 case "column_separators":
02282 $this->setColumnSeparators($value["entry"]);
02283 break;
02284 case "row_separators":
02285 $this->setRowSeparators($value["entry"]);
02286 break;
02287 case "layout":
02288 $this->setLayout($value["entry"]);
02289 break;
02290 case "neutral_column_separator":
02291 $this->setNeutralColumnSeparator($value["entry"]);
02292 break;
02293 }
02294 }
02295 }
02296
02305 function importAdjectives($a_data)
02306 {
02307 $i = 0;
02308 foreach ($a_data as $adjective)
02309 {
02310 if (is_numeric($adjective["label"]))
02311 {
02312 $this->setBipolarAdjective($adjective["label"], $adjective["text"]);
02313 }
02314 else
02315 {
02316 $this->setBipolarAdjective($i, $adjective["text"]);
02317 }
02318 $i++;
02319 }
02320 }
02321
02330 function importMatrix($a_data)
02331 {
02332 foreach ($a_data as $row)
02333 {
02334 $this->addRow($row);
02335 }
02336 }
02337
02346 function importResponses($a_data)
02347 {
02348 foreach ($a_data as $id => $data)
02349 {
02350 $column = "";
02351 foreach ($data["material"] as $material)
02352 {
02353 $column .= $material["text"];
02354 }
02355 if (strcmp($data["label"], "neutral") == 0)
02356 {
02357 $this->setNeutralColumn($column);
02358 }
02359 else
02360 {
02361 $this->addColumn($column);
02362 }
02363 }
02364 }
02365
02374 function usableForPrecondition()
02375 {
02376 return FALSE;
02377 }
02378
02388 function getPreconditionValueOutput($value)
02389 {
02390 return $value;
02391 }
02392
02403 function outChart($survey_id, $type = "")
02404 {
02405 if (count($this->cumulated) == 0)
02406 {
02407 include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
02408 $nr_of_users = ilObjSurvey::_getNrOfParticipants($survey_id);
02409 $this->cumulated =& $this->getCumulatedResults($survey_id, $nr_of_users);
02410 }
02411 global $ilLog;
02412 if (is_numeric($type))
02413 {
02414 foreach ($this->cumulated[$type]["variables"] as $key => $value)
02415 {
02416 foreach ($value as $key2 => $value2)
02417 {
02418 $this->cumulated["variables"][$key][$key2] = utf8_decode($value2);
02419 }
02420 }
02421 $title = preg_replace("/<[^>]+?>/ims", "", $this->getRow($type));
02422 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyChart.php";
02423 $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"]);
02424 }
02425 }
02426
02438 function saveLayout($percent_row, $percent_columns, $percent_bipolar_adjective1 = "", $percent_bipolar_adjective2 = "", $percent_neutral)
02439 {
02440 global $ilDB;
02441
02442 $layout = array(
02443 "percent_row" => $percent_row,
02444 "percent_columns" => $percent_columns,
02445 "percent_bipolar_adjective1" => $percent_bipolar_adjective1,
02446 "percent_bipolar_adjective2" => $percent_bipolar_adjective2,
02447 "percent_neutral" => $percent_neutral
02448 );
02449 $query = sprintf("UPDATE survey_question_matrix SET layout = %s WHERE question_fi = %s",
02450 $ilDB->quote(serialize($layout) . ""),
02451 $ilDB->quote($this->getId() . "")
02452 );
02453 $ilDB->query($query);
02454 }
02455
02456 function getLayout()
02457 {
02458 if (!is_array($this->layout) || count($this->layout) == 0)
02459 {
02460 if ($this->hasBipolarAdjectives() && $this->hasNeutralColumn())
02461 {
02462 $this->layout = array(
02463 "percent_row" => 30,
02464 "percent_columns" => 40,
02465 "percent_bipolar_adjective1" => 10,
02466 "percent_bipolar_adjective2" => 10,
02467 "percent_neutral" => 10
02468 );
02469 }
02470 elseif ($this->hasBipolarAdjectives())
02471 {
02472 $this->layout = array(
02473 "percent_row" => 30,
02474 "percent_columns" => 50,
02475 "percent_bipolar_adjective1" => 10,
02476 "percent_bipolar_adjective2" => 10,
02477 "percent_neutral" => 0
02478 );
02479 }
02480 elseif ($this->hasNeutralColumn())
02481 {
02482 $this->layout = array(
02483 "percent_row" => 30,
02484 "percent_columns" => 50,
02485 "percent_bipolar_adjective1" => 0,
02486 "percent_bipolar_adjective2" => 0,
02487 "percent_neutral" => 20
02488 );
02489 }
02490 else
02491 {
02492 $this->layout = array(
02493 "percent_row" => 30,
02494 "percent_columns" => 70,
02495 "percent_bipolar_adjective1" => 0,
02496 "percent_bipolar_adjective2" => 0,
02497 "percent_neutral" => 0
02498 );
02499 }
02500 }
02501 return $this->layout;
02502 }
02503
02504 function setLayout($layout)
02505 {
02506 if (is_array($layout))
02507 {
02508 $this->layout = $layout;
02509 }
02510 else
02511 {
02512 $this->layout = unserialize($layout);
02513 }
02514 }
02515
02523 function hasBipolarAdjectives()
02524 {
02525 if ((strlen($this->getBipolarAdjective(0))) && (strlen($this->getBipolarAdjective(1))))
02526 {
02527 return TRUE;
02528 }
02529 else
02530 {
02531 return FALSE;
02532 }
02533 }
02534
02542 function hasNeutralColumn()
02543 {
02544 if (strlen($this->getNeutralColumn()))
02545 {
02546 return TRUE;
02547 }
02548 else
02549 {
02550 return FALSE;
02551 }
02552 }
02553
02554 function getColumnPlaceholders()
02555 {
02556 return $this->columnPlaceholders;
02557 }
02558
02559 function setColumnPlaceholders($columnplaceholders)
02560 {
02561 $this->columnPlaceholders = $columnplaceholders;
02562 }
02563
02564 function getLegend()
02565 {
02566 return $this->legend;
02567 }
02568
02569 function setLegend($legend)
02570 {
02571 $this->legend = $legend;
02572 }
02573
02574 function getSingleLineRowCaption()
02575 {
02576 return $this->singleLineRowCaption;
02577 }
02578
02579 function setSingleLineRowCaption($singleLineRowCaption)
02580 {
02581 $this->singleLineRowCaption = $singleLineRowCaption;
02582 }
02583
02584 function getRepeatColumnHeader()
02585 {
02586 return $this->repeatColumnHeader;
02587 }
02588
02589 function setRepeatColumnHeader($repeatColumnHeader)
02590 {
02591 $this->repeatColumnHeader = $repeatColumnHeader;
02592 }
02593
02594 function getColumnHeaderPosition()
02595 {
02596 return $this->columnHeaderPosition;
02597 }
02598
02599 function setColumnHeaderPosition($columnHeaderPosition)
02600 {
02601 $this->columnHeaderPosition = $columnHeaderPosition;
02602 }
02603
02604 function getRandomRows()
02605 {
02606 return $this->randomRows;
02607 }
02608
02609 function setRandomRows($randomRows)
02610 {
02611 $this->randomRows = $randomRows;
02612 }
02613
02614 function getColumnOrder()
02615 {
02616 return $this->columnOrder;
02617 }
02618
02619 function setColumnOrder($columnOrder)
02620 {
02621 $this->columnOrder = $columnOrder;
02622 }
02623
02624 function getColumnImages()
02625 {
02626 return $this->columnImages;
02627 }
02628
02629 function setColumnImages($columnImages)
02630 {
02631 $this->columnImages = $columnImages;
02632 }
02633
02634 function getRowImages()
02635 {
02636 return $this->rowImages;
02637 }
02638
02639 function setRowImages($rowImages)
02640 {
02641 $this->rowImages = $rowImages;
02642 }
02643 }
02644 ?>