ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
36 {
40  protected $user;
41 
45  protected $db;
46 
52  public $columns;
53 
59  public $rows;
60 
67 
74 
82 
90 
98 
99  /*
100  * Layout of the matrix question
101  *
102  * @var array
103  */
104  public $layout;
105 
106  /*
107  * Use placeholders for the column titles
108  *
109  * @var boolean
110  */
112 
113  /*
114  * Show a legend
115  *
116  * @var boolean
117  */
118  public $legend;
119 
121 
123 
125 
126  /*
127  * Use random order for rows
128  *
129  * @var boolean
130  */
131  public $randomRows;
132 
133  public $columnOrder;
134 
136 
137  public $rowImages;
138 
139  public $openRows;
140 
141 
156  public $subtype;
157 
168  public function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1)
169  {
170  global $DIC;
171 
172  $this->user = $DIC->user();
173  $this->db = $DIC->database();
174  parent::__construct($title, $description, $author, $questiontext, $owner);
175 
176  $this->subtype = 0;
177  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
178  $this->columns = new SurveyCategories();
179  $this->rows = new SurveyCategories();
180  $this->bipolar_adjective1 = "";
181  $this->bipolar_adjective2 = "";
182  $this->rowSeparators = 0;
183  $this->columnSeparators = 0;
184  $this->neutralColumnSeparator = 1;
185  }
186 
194  public function getColumnCount()
195  {
196  return $this->columns->getCategoryCount();
197  }
198 
206  public function removeColumn($index)
207  {
208  $this->columns->removeCategory($index);
209  }
210 
218  public function removeColumns($array)
219  {
220  $this->columns->removeCategories($array);
221  }
222 
230  public function removeColumnWithName($name)
231  {
232  $this->columns->removeCategoryWithName($name);
233  }
234 
238  public function getColumns()
239  {
240  return $this->columns;
241  }
242 
251  public function getColumn($index)
252  {
253  return $this->columns->getCategory($index);
254  }
255 
256  public function getColumnForScale($scale)
257  {
258  return $this->columns->getCategoryForScale($scale);
259  }
260 
268  public function getColumnIndex($name)
269  {
270  return $this->columns->getCategoryIndex($name);
271  }
272 
273 
280  public function flushColumns()
281  {
282  $this->columns->flushCategories();
283  }
284 
291  public function getRowCount()
292  {
293  return $this->rows->getCategoryCount();
294  }
295 
301  public function addRow($a_text, $a_other, $a_label)
302  {
303  $this->rows->addCategory($a_text, $a_other, 0, $a_label);
304  }
305 
312  public function addRowAtPosition($a_text, $a_other, $a_position)
313  {
314  $this->rows->addCategoryAtPosition($a_text, $a_position, $a_other);
315  }
316 
323  public function flushRows()
324  {
325  $this->rows = new SurveyCategories();
326  }
327 
334  public function getRow($a_index)
335  {
336  return $this->rows->getCategory($a_index);
337  }
338 
339  public function moveRowUp($index)
340  {
341  $this->rows->moveCategoryUp($index);
342  }
343 
344  public function moveRowDown($index)
345  {
346  $this->rows->moveCategoryDown($index);
347  }
348 
356  public function removeRows($array)
357  {
358  $this->rows->removeCategories($array);
359  }
360 
366  public function removeRow($index)
367  {
368  $this->rows->removeCategory($index);
369  }
370 
378  public function getBipolarAdjective($a_index)
379  {
380  switch ($a_index) {
381  case 1:
382  return (strlen($this->bipolar_adjective2)) ? $this->bipolar_adjective2 : null;
383  break;
384  case 0:
385  default:
386  return (strlen($this->bipolar_adjective1)) ? $this->bipolar_adjective1 : null;
387  break;
388  }
389  return null;
390  }
391 
399  public function setBipolarAdjective($a_index, $a_value)
400  {
401  switch ($a_index) {
402  case 1:
403  $this->bipolar_adjective2 = $a_value;
404  break;
405  case 0:
406  default:
407  $this->bipolar_adjective1 = $a_value;
408  break;
409  }
410  }
411 
418  public function addPhrase($phrase_id)
419  {
421  $ilDB = $this->db;
422 
423  $result = $ilDB->queryF(
424  "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",
425  array('integer', 'integer', 'integer'),
426  array($phrase_id, 0, $ilUser->getId())
427  );
428  while ($row = $ilDB->fetchAssoc($result)) {
429  $neutral = $row["neutral"];
430  if (($row["defaultvalue"] == 1) && ($row["owner_fi"] == 0)) {
431  $this->columns->addCategory($this->lng->txt($row["title"]), 0, $neutral);
432  } else {
433  $this->columns->addCategory($row["title"], 0, $neutral);
434  }
435  }
436  }
437 
445  public function getQuestionDataArray($id)
446  {
447  $ilDB = $this->db;
448 
449  $result = $ilDB->queryF(
450  "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",
451  array('integer'),
452  array($id)
453  );
454  if ($result->numRows() == 1) {
455  return $ilDB->fetchAssoc($result);
456  } else {
457  return array();
458  }
459  }
460 
467  public function loadFromDb($id)
468  {
469  $ilDB = $this->db;
470  $result = $ilDB->queryF(
471  "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",
472  array('integer'),
473  array($id)
474  );
475  if ($result->numRows() == 1) {
476  $data = $ilDB->fetchAssoc($result);
477  $this->setId($data["question_id"]);
478  $this->setTitle($data["title"]);
479  $this->label = $data['label'];
480  $this->setDescription($data["description"]);
481  $this->setObjId($data["obj_fi"]);
482  $this->setAuthor($data["author"]);
483  $this->setOwner($data["owner_fi"]);
484  include_once("./Services/RTE/classes/class.ilRTE.php");
485  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
486  $this->setObligatory($data["obligatory"]);
487  $this->setComplete($data["complete"]);
488  $this->setOriginalId($data["original_id"]);
489  $this->setSubtype($data["subtype"]);
490  $this->setRowSeparators($data["row_separators"]);
491  $this->setNeutralColumnSeparator($data["neutral_column_separator"]);
492  $this->setColumnSeparators($data["column_separators"]);
493  $this->setColumnPlaceholders($data["column_placeholders"]);
494  $this->setLegend($data["legend"]);
495  $this->setSingleLineRowCaption($data["singleline_row_caption"]);
496  $this->setRepeatColumnHeader($data["repeat_column_header"]);
497  $this->setColumnHeaderPosition($data["column_header_position"]);
498  $this->setRandomRows($data["random_rows"]);
499  $this->setColumnOrder($data["column_order"]);
500  $this->setColumnImages($data["column_images"]);
501  $this->setRowImages($data["row_images"]);
502  $this->setBipolarAdjective(0, $data["bipolar_adjective1"]);
503  $this->setBipolarAdjective(1, $data["bipolar_adjective2"]);
504  $this->setLayout($data["layout"]);
505  $this->flushColumns();
506 
507  $result = $ilDB->queryF(
508  "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",
509  array('integer'),
510  array($id)
511  );
512  if ($result->numRows() > 0) {
513  while ($data = $ilDB->fetchAssoc($result)) {
514  $this->columns->addCategory($data["title"], $data["other"], $data["neutral"], null, ($data['scale']) ? $data['scale'] : ($data['sequence'] + 1));
515  }
516  }
517 
518  $result = $ilDB->queryF(
519  "SELECT * FROM svy_qst_matrixrows WHERE question_fi = %s ORDER BY sequence",
520  array('integer'),
521  array($id)
522  );
523  while ($row = $ilDB->fetchAssoc($result)) {
524  $this->addRow($row["title"], $row['other'], $row['label']);
525  }
526  }
527  parent::loadFromDb($id);
528  }
529 
536  public function isComplete()
537  {
538  if (
539  strlen($this->getTitle()) &&
540  strlen($this->getAuthor()) &&
541  strlen($this->getQuestiontext()) &&
542  $this->getColumnCount() &&
543  $this->getRowCount()
544  ) {
545  return 1;
546  } else {
547  return 0;
548  }
549  }
550 
556  public function saveToDb($original_id = null, $withanswers = true)
557  {
558  $ilDB = $this->db;
559 
560  $affectedRows = parent::saveToDb($original_id);
561 
562  if ($affectedRows == 1) {
563  $affectedRows = $ilDB->manipulateF(
564  "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
565  array('integer'),
566  array($this->getId())
567  );
568  $affectedRows = $ilDB->manipulateF(
569  "INSERT INTO " . $this->getAdditionalTableName() . " (
570  question_fi, subtype, column_separators, row_separators, neutral_column_separator,column_placeholders,
571  legend, singleline_row_caption, repeat_column_header, column_header_position, random_rows,
572  column_order, column_images, row_images, bipolar_adjective1, bipolar_adjective2, layout, tstamp)
573  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
574  array(
575  'integer', 'integer', 'text', 'text', 'text', 'integer', 'text', 'text', 'text',
576  'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'integer'
577  ),
578  array(
579  $this->getId(),
580  $this->getSubtype(),
581  $this->getColumnSeparators(),
582  $this->getRowSeparators(),
583  $this->getNeutralColumnSeparator(),
584  $this->getColumnPlaceholders(),
585  $this->getLegend(),
586  $this->getSingleLineRowCaption(),
587  $this->getRepeatColumnHeader(),
588  $this->getColumnHeaderPosition(),
589  $this->getRandomRows(),
590  $this->getColumnOrder(),
591  $this->getColumnImages(),
592  $this->getRowImages(),
593  $this->getBipolarAdjective(0),
594  $this->getBipolarAdjective(1),
595  serialize($this->getLayout()),
596  time()
597  )
598  );
599 
600  // saving material uris in the database
601  $this->saveMaterial();
602 
603  $this->saveColumnsToDb();
604  $this->saveRowsToDb();
605  }
606  }
607 
608  public function saveBipolarAdjectives($adjective1, $adjective2)
609  {
610  $ilDB = $this->db;
611 
612  $affectedRows = $ilDB->manipulateF(
613  "UPDATE " . $this->getAdditionalTableName() . " SET bipolar_adjective1 = %s, bipolar_adjective2 = %s WHERE question_fi = %s",
614  array('text', 'text', 'integer'),
615  array((strlen($adjective1)) ? $adjective1 : null, (strlen($adjective2)) ? $adjective2 : null, $this->getId())
616  );
617  }
618 
627  public function saveColumnToDb($columntext, $neutral = 0)
628  {
630  $ilDB = $this->db;
631 
632  $result = $ilDB->queryF(
633  "SELECT title, category_id FROM svy_category WHERE title = %s AND neutral = %s AND owner_fi = %s",
634  array('text', 'text', 'integer'),
635  array($columntext, $neutral, $ilUser->getId())
636  );
637  $insert = false;
638  $returnvalue = "";
639  if ($result->numRows()) {
640  $insert = true;
641  while ($row = $ilDB->fetchAssoc($result)) {
642  if (strcmp($row["title"], $columntext) == 0) {
643  $returnvalue = $row["category_id"];
644  $insert = false;
645  }
646  }
647  } else {
648  $insert = true;
649  }
650  if ($insert) {
651  $next_id = $ilDB->nextId('svy_category');
652  $affectedRows = $ilDB->manipulateF(
653  "INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, neutral, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
654  array('integer', 'text', 'text', 'integer', 'text', 'integer'),
655  array($next_id, $columntext, 0, $ilUser->getId(), $neutral, time())
656  );
657  $returnvalue = $next_id;
658  }
659  return $returnvalue;
660  }
661 
662  public function saveColumnsToDb($original_id = "")
663  {
664  $ilDB = $this->db;
665 
666  // save columns
667  $question_id = $this->getId();
668  if (strlen($original_id)) {
669  $question_id = $original_id;
670  }
671 
672  // delete existing column relations
673  $affectedRows = $ilDB->manipulateF(
674  "DELETE FROM svy_variable WHERE question_fi = %s",
675  array('integer'),
676  array($question_id)
677  );
678  // create new column relations
679  for ($i = 0; $i < $this->getColumnCount(); $i++) {
680  $cat = $this->getColumn($i);
681  $column_id = $this->saveColumnToDb($cat->title, $cat->neutral);
682  $next_id = $ilDB->nextId('svy_variable');
683  $affectedRows = $ilDB->manipulateF(
684  "INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, other, sequence, scale, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
685  array('integer','integer','integer','float','integer','integer', 'integer','integer'),
686  array($next_id, $column_id, $question_id, ($i + 1), $cat->other, $i, ($cat->scale > 0) ? $cat->scale : null, time())
687  );
688  }
689  $this->saveCompletionStatus($original_id);
690  }
691 
692  public function saveRowsToDb($original_id = "")
693  {
694  $ilDB = $this->db;
695 
696  // save rows
697  $question_id = $this->getId();
698  if (strlen($original_id)) {
699  $question_id = $original_id;
700  }
701 
702  // delete existing rows
703  $affectedRows = $ilDB->manipulateF(
704  "DELETE FROM svy_qst_matrixrows WHERE question_fi = %s",
705  array('integer'),
706  array($question_id)
707  );
708  // create new rows
709  for ($i = 0; $i < $this->getRowCount(); $i++) {
710  $row = $this->getRow($i);
711  $next_id = $ilDB->nextId('svy_qst_matrixrows');
712  $affectedRows = $ilDB->manipulateF(
713  "INSERT INTO svy_qst_matrixrows (id_svy_qst_matrixrows, title, label, other, sequence, question_fi) VALUES (%s, %s, %s, %s, %s, %s)",
714  array('integer','text','text','integer','integer','integer'),
715  array($next_id, $row->title, $row->label, ($row->other) ? 1 : 0, $i, $question_id)
716  );
717  }
718  $this->saveCompletionStatus($original_id);
719  }
720 
727  public function toXML($a_include_header = true, $obligatory_state = "")
728  {
729  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
730  $a_xml_writer = new ilXmlWriter;
731  $a_xml_writer->xmlHeader();
732  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
733  $xml = $a_xml_writer->xmlDumpMem(false);
734  if (!$a_include_header) {
735  $pos = strpos($xml, "?>");
736  $xml = substr($xml, $pos + 2);
737  }
738  return $xml;
739  }
740 
748  public function insertXML(&$a_xml_writer, $a_include_header = true)
749  {
750  $attrs = array(
751  "id" => $this->getId(),
752  "title" => $this->getTitle(),
753  "type" => $this->getQuestiontype(),
754  "subtype" => $this->getSubtype(),
755  "obligatory" => $this->getObligatory()
756  );
757  $a_xml_writer->xmlStartTag("question", $attrs);
758 
759  $a_xml_writer->xmlElement("description", null, $this->getDescription());
760  $a_xml_writer->xmlElement("author", null, $this->getAuthor());
761  $a_xml_writer->xmlStartTag("questiontext");
762  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
763  $a_xml_writer->xmlEndTag("questiontext");
764 
765  $a_xml_writer->xmlStartTag("matrix");
766  $a_xml_writer->xmlStartTag("matrixrows");
767  for ($i = 0; $i < $this->getRowCount(); $i++) {
768  $attrs = array(
769  "id" => $i
770  );
771  if (strlen($this->getRow($i)->label)) {
772  $attrs['label'] = $this->getRow($i)->label;
773  }
774  if ($this->getRow($i)->other) {
775  $attrs['other'] = 1;
776  }
777  $a_xml_writer->xmlStartTag("matrixrow", $attrs);
778  $this->addMaterialTag($a_xml_writer, $this->getRow($i)->title);
779  $a_xml_writer->xmlEndTag("matrixrow");
780  }
781  $a_xml_writer->xmlEndTag("matrixrows");
782 
783  $a_xml_writer->xmlStartTag("responses");
784  if (strlen($this->getBipolarAdjective(0)) && (strlen($this->getBipolarAdjective(1)))) {
785  $a_xml_writer->xmlStartTag("bipolar_adjectives");
786  $attribs = array(
787  "label" => "0"
788  );
789  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(0));
790  $attribs = array(
791  "label" => "1"
792  );
793  $a_xml_writer->xmlElement("adjective", $attribs, $this->getBipolarAdjective(1));
794  $a_xml_writer->xmlEndTag("bipolar_adjectives");
795  }
796  for ($i = 0; $i < $this->getColumnCount(); $i++) {
797  $attrs = array(
798  "id" => $i
799  );
800  if ($this->getColumn($i)->neutral) {
801  $attrs['label'] = 'neutral';
802  }
803  switch ($this->getSubtype()) {
804  case 0:
805  $a_xml_writer->xmlStartTag("response_single", $attrs);
806  break;
807  case 1:
808  $a_xml_writer->xmlStartTag("response_multiple", $attrs);
809  break;
810  }
811  $this->addMaterialTag($a_xml_writer, $this->getColumn($i)->title);
812  switch ($this->getSubtype()) {
813  case 0:
814  $a_xml_writer->xmlEndTag("response_single");
815  break;
816  case 1:
817  $a_xml_writer->xmlEndTag("response_multiple");
818  break;
819  }
820  }
821 
822  $a_xml_writer->xmlEndTag("responses");
823  $a_xml_writer->xmlEndTag("matrix");
824 
825  if (count($this->material)) {
826  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
827  $attrs = array(
828  "label" => $this->material["title"]
829  );
830  $a_xml_writer->xmlStartTag("material", $attrs);
831  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
832  if (strcmp($matches[1], "") != 0) {
833  $intlink = $this->material["internal_link"];
834  }
835  $a_xml_writer->xmlElement("mattext", null, $intlink);
836  $a_xml_writer->xmlEndTag("material");
837  }
838  }
839 
840  $a_xml_writer->xmlStartTag("metadata");
841  $a_xml_writer->xmlStartTag("metadatafield");
842  $a_xml_writer->xmlElement("fieldlabel", null, "column_separators");
843  $a_xml_writer->xmlElement("fieldentry", null, $this->getColumnSeparators());
844  $a_xml_writer->xmlEndTag("metadatafield");
845 
846  $a_xml_writer->xmlStartTag("metadatafield");
847  $a_xml_writer->xmlElement("fieldlabel", null, "row_separators");
848  $a_xml_writer->xmlElement("fieldentry", null, $this->getRowSeparators());
849  $a_xml_writer->xmlEndTag("metadatafield");
850 
851  $a_xml_writer->xmlStartTag("metadatafield");
852  $a_xml_writer->xmlElement("fieldlabel", null, "neutral_column_separator");
853  $a_xml_writer->xmlElement("fieldentry", null, $this->getNeutralColumnSeparator());
854  $a_xml_writer->xmlEndTag("metadatafield");
855 
856  $a_xml_writer->xmlStartTag("metadatafield");
857  $a_xml_writer->xmlElement("fieldlabel", null, "layout");
858  $a_xml_writer->xmlElement("fieldentry", null, serialize($this->getLayout()));
859  $a_xml_writer->xmlEndTag("metadatafield");
860 
861  $a_xml_writer->xmlEndTag("metadata");
862 
863  $a_xml_writer->xmlEndTag("question");
864  }
865 
866  public function syncWithOriginal()
867  {
868  if ($this->getOriginalId()) {
869  parent::syncWithOriginal();
870  $this->saveColumnsToDb($this->getOriginalId());
871  $this->saveRowsToDb($this->getOriginalId());
872  }
873  }
874 
875 
883  public function addStandardNumbers($lower_limit, $upper_limit)
884  {
885  for ($i = $lower_limit; $i <= $upper_limit; $i++) {
886  $this->columns->addCategory($i);
887  }
888  }
889 
897  public function savePhrase($title)
898  {
900  $ilDB = $this->db;
901 
902  $next_id = $ilDB->nextId('svy_phrase');
903  $affectedRows = $ilDB->manipulateF(
904  "INSERT INTO svy_phrase (phrase_id, title, defaultvalue, owner_fi, tstamp) VALUES (%s, %s, %s, %s, %s)",
905  array('integer','text','text','integer','integer'),
906  array($next_id, $title, 1, $ilUser->getId(), time())
907  );
908  $phrase_id = $next_id;
909 
910  $counter = 1;
911  foreach ($_SESSION['save_phrase_data'] as $data) {
912  $next_id = $ilDB->nextId('svy_category');
913  $affectedRows = $ilDB->manipulateF(
914  "INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, tstamp, neutral) VALUES (%s, %s, %s, %s, %s, %s)",
915  array('integer','text','text','integer','integer','text'),
916  array($next_id, $data['answer'], 1, $ilUser->getId(), time(), $data['neutral'])
917  );
918  $category_id = $next_id;
919  $next_id = $ilDB->nextId('svy_phrase_cat');
920  $affectedRows = $ilDB->manipulateF(
921  "INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence, other, scale) VALUES (%s, %s, %s, %s, %s, %s)",
922  array('integer', 'integer', 'integer','integer', 'integer', 'integer'),
923  array($next_id, $phrase_id, $category_id, $counter, ($data['other']) ? 1 : 0, $data['scale'])
924  );
925  $counter++;
926  }
927  }
928 
935  public function getQuestionType()
936  {
937  return "SurveyMatrixQuestion";
938  }
939 
946  public function getAdditionalTableName()
947  {
948  return "svy_qst_matrix";
949  }
950 
957  public function &getWorkingDataFromUserInput($post_data)
958  {
959  $data = array();
960  foreach ($post_data as $key => $value) {
961  switch ($this->getSubtype()) {
962  case 0:
963  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
964  if (is_array($value)) {
965  foreach ($value as $val) {
966  array_push($data, array("value" => $val, "rowvalue" => $matches[1], "textanswer" => $post_data['matrix_other_' . $this->getId() . '_' . $matches[1]]));
967  }
968  } else {
969  array_push($data, array("value" => $value, "rowvalue" => $matches[1], "textanswer" => $post_data['matrix_other_' . $this->getId() . '_' . $matches[1]]));
970  }
971  }
972  break;
973  case 1:
974  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
975  if (is_array($value)) {
976  foreach ($value as $val) {
977  array_push($data, array("value" => $val, "rowvalue" => $matches[1], "textanswer" => $post_data['matrix_other_' . $this->getId() . '_' . $matches[1]]));
978  }
979  } else {
980  array_push($data, array("value" => $value, "rowvalue" => $matches[1], "textanswer" => $post_data['matrix_other_' . $this->getId() . '_' . $matches[1]]));
981  }
982  }
983  break;
984  }
985  }
986  return $data;
987  }
988 
998  public function checkUserInput($post_data, $survey_id)
999  {
1000  if (!$this->getObligatory($survey_id)) {
1001  return "";
1002  }
1003  switch ($this->getSubtype()) {
1004  case 0:
1005  $counter = 0;
1006  foreach ($post_data as $key => $value) {
1007  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
1008  if (array_key_exists('matrix_other_' . $this->getId() . "_" . $matches[1], $post_data) && strlen($post_data['matrix_other_' . $this->getId() . "_" . $matches[1]]) == 0) {
1009  return $this->lng->txt("question_mr_no_other_answer");
1010  }
1011  $counter++;
1012  }
1013  }
1014  if ($counter != $this->getRowCount()) {
1015  return $this->lng->txt("matrix_question_radio_button_not_checked");
1016  }
1017  break;
1018  case 1:
1019  $counter = 0;
1020  foreach ($post_data as $key => $value) {
1021  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
1022  if (array_key_exists('matrix_other_' . $this->getId() . "_" . $matches[1], $post_data) && strlen($post_data['matrix_other_' . $this->getId() . "_" . $matches[1]]) == 0) {
1023  return $this->lng->txt("question_mr_no_other_answer");
1024  }
1025  $counter++;
1026  if ((!is_array($value)) || (count($value) < 1)) {
1027  return $this->lng->txt("matrix_question_checkbox_not_checked");
1028  }
1029  }
1030  }
1031  if ($counter != $this->getRowCount()) {
1032  return $this->lng->txt("matrix_question_checkbox_not_checked");
1033  }
1034  break;
1035  }
1036  return "";
1037  }
1038 
1039  public function saveUserInput($post_data, $active_id, $a_return = false)
1040  {
1041  $ilDB = $this->db;
1042 
1043  $answer_data = array();
1044 
1045  // gather data
1046  switch ($this->getSubtype()) {
1047  case 0:
1048  foreach ($post_data as $key => $value) {
1049  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
1050  if (strlen($value)) {
1051  $other_value = (array_key_exists('matrix_other_' . $this->getId() . '_' . $matches[1], $post_data))
1052  ? $this->stripSlashesAddSpaceFallback($post_data['matrix_other_' . $this->getId() . '_' . $matches[1]])
1053  : null;
1054  $answer_data[] = array("value" => $value,
1055  "textanswer" => $other_value,
1056  "rowvalue" => $matches[1]);
1057  }
1058  }
1059  }
1060  break;
1061 
1062  case 1:
1063  foreach ($post_data as $key => $value) {
1064  if (preg_match("/matrix_" . $this->getId() . "_(\d+)/", $key, $matches)) {
1065  $other_value = (array_key_exists('matrix_other_' . $this->getId() . '_' . $matches[1], $post_data))
1066  ? $this->stripSlashesAddSpaceFallback($post_data['matrix_other_' . $this->getId() . '_' . $matches[1]])
1067  : null;
1068  foreach ($value as $checked) {
1069  $answer_data[] = array("value" => $checked,
1070  "textanswer" => $other_value,
1071  "rowvalue" => $matches[1]);
1072  }
1073  }
1074  }
1075  break;
1076  }
1077 
1078  if ($a_return) {
1079  return $answer_data;
1080  }
1081 
1082  // #16387 - only if any input
1083  if (sizeof($answer_data)) {
1084  // save data
1085  foreach ($answer_data as $item) {
1086  $next_id = $ilDB->nextId('svy_answer');
1087  #20216
1088  $fields = array();
1089  $fields['answer_id'] = array("integer", $next_id);
1090  $fields['question_fi'] = array("integer", $this->getId());
1091  $fields['active_fi'] = array("integer", $active_id);
1092  $fields['value'] = array("float", $item['value']);
1093  $fields['textanswer'] = array("clob", $item['textanswer']);
1094  $fields['rowvalue'] = array("integer", $item['rowvalue']);
1095  $fields['tstamp'] = array("integer", time());
1096 
1097  $affectedRows = $ilDB->insert("svy_answer", $fields);
1098  }
1099  }
1100  }
1101 
1108  public function deleteAdditionalTableData($question_id)
1109  {
1110  parent::deleteAdditionalTableData($question_id);
1111 
1112  $ilDB = $this->db;
1113  $affectedRows = $ilDB->manipulateF(
1114  "DELETE FROM svy_qst_matrixrows WHERE question_fi = %s",
1115  array('integer'),
1116  array($question_id)
1117  );
1118  }
1119 
1126  public function getSubtype()
1127  {
1128  return $this->subtype;
1129  }
1130 
1137  public function setSubtype($a_subtype = 0)
1138  {
1139  switch ($a_subtype) {
1140  case 1:
1141  case 2:
1142  case 3:
1143  case 4:
1144  case 5:
1145  case 6:
1146  $this->subtype = $a_subtype;
1147  break;
1148  case 0:
1149  default:
1150  $this->subtype = 0;
1151  break;
1152  }
1153  }
1154 
1161  public function setColumnSeparators($enable = 0)
1162  {
1163  switch ($enable) {
1164  case 1:
1165  $this->columnSeparators = 1;
1166  break;
1167  case 0:
1168  default:
1169  $this->columnSeparators = 0;
1170  break;
1171  }
1172  }
1173 
1180  public function getColumnSeparators()
1181  {
1182  return ($this->columnSeparators) ? 1 : 0;
1183  }
1184 
1191  public function setRowSeparators($enable = 0)
1192  {
1193  switch ($enable) {
1194  case 1:
1195  $this->rowSeparators = 1;
1196  break;
1197  case 0:
1198  default:
1199  $this->rowSeparators = 0;
1200  break;
1201  }
1202  }
1203 
1210  public function getRowSeparators()
1211  {
1212  return ($this->rowSeparators) ? 1 : 0;
1213  }
1214 
1221  public function setNeutralColumnSeparator($enable = 0)
1222  {
1223  switch ($enable) {
1224  case 1:
1225  $this->neutralColumnSeparator = 1;
1226  break;
1227  case 0:
1228  default:
1229  $this->neutralColumnSeparator = 0;
1230  break;
1231  }
1232  }
1233 
1240  public function getNeutralColumnSeparator()
1241  {
1242  return ($this->neutralColumnSeparator) ? 1 : 0;
1243  }
1244 
1253  public function importAdditionalMetadata($a_meta)
1254  {
1255  foreach ($a_meta as $key => $value) {
1256  switch ($value["label"]) {
1257  case "column_separators":
1258  $this->setColumnSeparators($value["entry"]);
1259  break;
1260  case "row_separators":
1261  $this->setRowSeparators($value["entry"]);
1262  break;
1263  case "layout":
1264  $this->setLayout($value["entry"]);
1265  break;
1266  case "neutral_column_separator":
1267  $this->setNeutralColumnSeparator($value["entry"]);
1268  break;
1269  }
1270  }
1271  }
1272 
1279  public function importAdjectives($a_data)
1280  {
1281  $i = 0;
1282  foreach ($a_data as $adjective) {
1283  if (is_numeric($adjective["label"])) {
1284  $this->setBipolarAdjective($adjective["label"], $adjective["text"]);
1285  } else {
1286  $this->setBipolarAdjective($i, $adjective["text"]);
1287  }
1288  $i++;
1289  }
1290  }
1291 
1298  public function importMatrix($a_data)
1299  {
1300  foreach ($a_data as $row) {
1301  $this->addRow($row['title'], $row['other'], $row['label']);
1302  }
1303  }
1304 
1311  public function importResponses($a_data)
1312  {
1313  foreach ($a_data as $id => $data) {
1314  $column = "";
1315  foreach ($data["material"] as $material) {
1316  $column .= $material["text"];
1317  }
1318  $this->columns->addCategory($column, null, (strcmp($data["label"], "neutral") == 0) ? true : false);
1319  }
1320  }
1321 
1328  public function usableForPrecondition()
1329  {
1330  return false;
1331  }
1332 
1340  public function getPreconditionValueOutput($value)
1341  {
1342  return $value;
1343  }
1344 
1351  public function getPreconditionSelectValue($default = "", $title, $variable)
1352  {
1353  include_once "./Services/Form/classes/class.ilSelectInputGUI.php";
1354  $step3 = new ilSelectInputGUI($title, $variable);
1355  $options = $this->getPreconditionOptions();
1356  $step3->setOptions($options);
1357  $step3->setValue($default);
1358  return $step3;
1359  }
1360 
1370  public function saveLayout($percent_row, $percent_columns, $percent_bipolar_adjective1 = "", $percent_bipolar_adjective2 = "", $percent_neutral)
1371  {
1372  $ilDB = $this->db;
1373 
1374  $layout = array(
1375  "percent_row" => $percent_row,
1376  "percent_columns" => $percent_columns,
1377  "percent_bipolar_adjective1" => $percent_bipolar_adjective1,
1378  "percent_bipolar_adjective2" => $percent_bipolar_adjective2,
1379  "percent_neutral" => $percent_neutral
1380  );
1381  $affectedRows = $ilDB->manipulateF(
1382  "UPDATE " . $this->getAdditionalTableName() . " SET layout = %s WHERE question_fi = %s",
1383  array('text', 'integer'),
1384  array(serialize($layout), $this->getId())
1385  );
1386  }
1387 
1388  public function getLayout()
1389  {
1390  if (!is_array($this->layout) || count($this->layout) == 0) {
1391  if ($this->hasBipolarAdjectives() && $this->hasNeutralColumn()) {
1392  $this->layout = array(
1393  "percent_row" => 30,
1394  "percent_columns" => 40,
1395  "percent_bipolar_adjective1" => 10,
1396  "percent_bipolar_adjective2" => 10,
1397  "percent_neutral" => 10
1398  );
1399  } elseif ($this->hasBipolarAdjectives()) {
1400  $this->layout = array(
1401  "percent_row" => 30,
1402  "percent_columns" => 50,
1403  "percent_bipolar_adjective1" => 10,
1404  "percent_bipolar_adjective2" => 10,
1405  "percent_neutral" => 0
1406  );
1407  } elseif ($this->hasNeutralColumn()) {
1408  $this->layout = array(
1409  "percent_row" => 30,
1410  "percent_columns" => 50,
1411  "percent_bipolar_adjective1" => 0,
1412  "percent_bipolar_adjective2" => 0,
1413  "percent_neutral" => 20
1414  );
1415  } else {
1416  $this->layout = array(
1417  "percent_row" => 30,
1418  "percent_columns" => 70,
1419  "percent_bipolar_adjective1" => 0,
1420  "percent_bipolar_adjective2" => 0,
1421  "percent_neutral" => 0
1422  );
1423  }
1424  }
1425  return $this->layout;
1426  }
1427 
1428  public function setLayout($layout)
1429  {
1430  if (is_array($layout)) {
1431  $this->layout = $layout;
1432  } else {
1433  $this->layout = unserialize($layout);
1434  }
1435  }
1436 
1442  public function hasBipolarAdjectives()
1443  {
1444  if ((strlen($this->getBipolarAdjective(0))) && (strlen($this->getBipolarAdjective(1)))) {
1445  return true;
1446  } else {
1447  return false;
1448  }
1449  }
1450 
1456  public function hasNeutralColumn()
1457  {
1458  for ($i = 0; $i < $this->getColumnCount(); $i++) {
1459  $column = $this->getColumn($i);
1460  if ($column->neutral && strlen($column->title)) {
1461  return true;
1462  }
1463  }
1464  return false;
1465  }
1466 
1472  public function setColumnPlaceholders($a_value = 0)
1473  {
1474  $this->columnPlaceholders = ($a_value) ? 1 : 0;
1475  }
1476 
1482  public function getColumnPlaceholders()
1483  {
1484  return ($this->columnPlaceholders) ? 1 : 0;
1485  }
1486 
1492  public function setLegend($a_value = 0)
1493  {
1494  $this->legend = ($a_value) ? 1 : 0;
1495  }
1496 
1502  public function getLegend()
1503  {
1504  return ($this->legend) ? 1 : 0;
1505  }
1506 
1507  public function setSingleLineRowCaption($a_value = 0)
1508  {
1509  $this->singleLineRowCaption = ($a_value) ? 1 : 0;
1510  }
1511 
1512  public function getSingleLineRowCaption()
1513  {
1514  return ($this->singleLineRowCaption) ? 1 : 0;
1515  }
1516 
1517  public function setRepeatColumnHeader($a_value = 0)
1518  {
1519  $this->repeatColumnHeader = ($a_value) ? 1 : 0;
1520  }
1521 
1522  public function getRepeatColumnHeader()
1523  {
1524  return ($this->repeatColumnHeader) ? 1 : 0;
1525  }
1526 
1527  public function setColumnHeaderPosition($a_value)
1528  {
1529  $this->columnHeaderPosition = $a_value;
1530  }
1531 
1532  public function getColumnHeaderPosition()
1533  {
1534  return ($this->columnHeaderPosition) ? $this->columnHeaderPosition : 0;
1535  }
1536 
1537  public function setRandomRows($a_value = 0)
1538  {
1539  $this->randomRows = ($a_value) ? 1 : 0;
1540  }
1541 
1542  public function getRandomRows()
1543  {
1544  return ($this->randomRows) ? 1 : 0;
1545  }
1546 
1547  public function setColumnOrder($a_value)
1548  {
1549  $this->columnOrder = $a_value;
1550  }
1551 
1552  public function getColumnOrder()
1553  {
1554  return ($this->columnOrder) ? $this->columnOrder : 0;
1555  }
1556 
1557  public function setColumnImages($a_value = 0)
1558  {
1559  $this->columnImages = ($a_value) ? 1 : 0;
1560  }
1561 
1562  public function getColumnImages()
1563  {
1564  return ($this->columnImages) ? 1 : 0;
1565  }
1566 
1567  public function setRowImages($a_value = 0)
1568  {
1569  $this->rowImages = ($a_value) ? 1 : 0;
1570  }
1571 
1572  public function getRowImages()
1573  {
1574  return ($this->rowImages) ? 1 : 0;
1575  }
1576 
1577  public function getRows()
1578  {
1579  return $this->rows;
1580  }
1581 }
toXML($a_include_header=true, $obligatory_state="")
Returns an xml representation of the question.
addRowAtPosition($a_text, $a_other, $a_position)
Adds a row at a given position.
saveCompletionStatus($original_id="")
Saves the complete flag to the database.
getAuthor()
Gets the authors name of the SurveyQuestion object.
getTitle()
Gets the title string of the SurveyQuestion object.
getBipolarAdjective($a_index)
Returns one of the bipolar adjectives.
$_SESSION["AccountId"]
This class represents a selection list property in a property form.
$result
getObligatory($survey_id="")
Gets the obligatory state of the question.
saveToDb($original_id=null, $withanswers=true)
Saves a SurveyMatrixQuestion object to a database.
getRow($a_index)
Returns a specific row.
global $DIC
Definition: saml.php:7
stripSlashesAddSpaceFallback($a_str)
Strip slashes with add space fallback, see https://mantis.ilias.de/view.php?id=19727 and https://mant...
setObligatory($obligatory=1)
Sets the obligatory state of the question.
savePhrase($title)
Saves a set of columns to a default phrase.
setColumnPlaceholders($a_value=0)
Set whether placeholders should be used for the column titles or not.
setId($id=-1)
Sets the id of the SurveyQuestion object.
hasBipolarAdjectives()
Returns TRUE if bipolar adjectives exist.
XML writer class.
removeRows($array)
Removes rows from the question.
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
saveBipolarAdjectives($adjective1, $adjective2)
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getRowSeparators()
Gets the separators enable state for the matrix rows.
$index
Definition: metadata.php:60
getColumns()
Return the columns.
getPreconditionValueOutput($value)
Returns the output for a precondition value.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
setOwner($owner="")
Sets the creator/owner ID of the SurveyQuestion object.
getColumn($index)
Returns the name of a column for a given index.
user()
Definition: user.php:4
setComplete($a_complete)
Sets the complete state of the question.
flushColumns()
Empties the columns list.
deleteAdditionalTableData($question_id)
Deletes datasets from the additional question table in the database.
getPreconditionOptions()
Returns the options for preconditions.
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
getQuestionDataArray($id)
Returns the question data fields from the database.
saveLayout($percent_row, $percent_columns, $percent_bipolar_adjective1="", $percent_bipolar_adjective2="", $percent_neutral)
Saves the layout of a matrix question.
importAdjectives($a_data)
Import bipolar adjectives from the question import file.
setOriginalId($original_id)
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
Class SurveyCategories.
hasNeutralColumn()
Returns TRUE if a neutral column exists.
setNeutralColumnSeparator($enable=0)
Enables/Disables a separator for the neutral column.
getLegend()
Get whether the legend should be shown or not.
usableForPrecondition()
Returns if the question is usable for preconditions.
getId()
Gets the id of the SurveyQuestion object.
addStandardNumbers($lower_limit, $upper_limit)
Adds standard numbers as columns.
The SurveyMatrixQuestion class defines and encapsulates basic methods and attributes for matrix quest...
addMaterialTag(&$a_xml_writer, $a_material, $close_material_tag=true, $add_mobs=true, $a_attrs=null)
Creates an XML material tag from a plain text or xhtml text.
Basic class for all survey question types.
removeRow($index)
Removes a row.
importMatrix($a_data)
Import matrix rows from the question import file.
saveMaterial()
save material to db
importResponses($a_data)
Import response data from the question import file.
loadFromDb($id)
Loads a SurveyMatrixQuestion object from the database.
setLegend($a_value=0)
Set whether the legend should be shown or not.
getPreconditionSelectValue($default="", $title, $variable)
Creates a form property for the precondition value.
$ilUser
Definition: imgupload.php:18
setColumnSeparators($enable=0)
Enables/Disables separators for the matrix columns.
xmlHeader()
Writes xml header public.
insertXML(&$a_xml_writer, $a_include_header=true)
Adds the question XML to a given XMLWriter object.
saveColumnToDb($columntext, $neutral=0)
Saves a column to the database.
setSubtype($a_subtype=0)
Sets the subtype of the matrix question.
getColumnIndex($name)
Returns the index of a column with a given name.
saveUserInput($post_data, $active_id, $a_return=false)
flushRows()
Empties the row list.
getSubtype()
Returns the subtype of the matrix question.
$default
Definition: build.php:20
$row
removeColumn($index)
Removes a column from the list of columns.
setAuthor($author="")
Sets the authors name of the SurveyQuestion object.
setRowSeparators($enable=0)
Enables/Disables separators for the matrix rows.
importAdditionalMetadata($a_meta)
Import additional meta data from the question import file.
removeColumnWithName($name)
Removes a column from the list of columns.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
addRow($a_text, $a_other, $a_label)
Adds a row to the question.
global $ilDB
$i
Definition: disco.tpl.php:19
__construct($title="", $description="", $author="", $questiontext="", $owner=-1)
SurveyMatrixQuestion constructor The constructor takes possible arguments an creates an instance of t...
getColumnPlaceholders()
Get whether placeholders should be used for the column titles or not.
getDescription()
Gets the description string of the SurveyQuestion object.
getQuestionType()
Returns the question type of the question.
getColumnCount()
Returns the number of columns.
setObjId($obj_id=0)
Set the reference id of the container object.
getColumnSeparators()
Gets the separators enable state for the matrix columns.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
setBipolarAdjective($a_index, $a_value)
Sets one of the bipolar adjectives.
$key
Definition: croninfo.php:18
getRowCount()
Returns the number of rows in the question.
getNeutralColumnSeparator()
Gets the separator enable state for the neutral column.
addPhrase($phrase_id)
Adds a phrase to the question.
removeColumns($array)
Removes many columns from the list of columns.
$data
Definition: bench.php:6
isComplete()
Returns 1 if the question is complete for use.
setTitle($title="")
Sets the title string of the SurveyQuestion object.