ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.SurveySingleChoiceQuestion.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
25 
38 {
45 
57  function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1, $orientation = 1)
58  {
59  parent::__construct($title, $description, $author, $questiontext, $owner);
60 
61  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
62  $this->orientation = $orientation;
63  $this->categories = new SurveyCategories();
64  }
65 
73  function &getCategoriesForPhrase($phrase_id)
74  {
75  global $ilDB;
76  $categories = array();
77  $result = $ilDB->queryF("SELECT svy_category.* FROM svy_category, svy_phrase_cat WHERE svy_phrase_cat.category_fi = svy_category.category_id AND svy_phrase_cat.phrase_fi = %s ORDER BY svy_phrase_cat.sequence",
78  array('integer'),
79  array($phrase_id)
80  );
81  while ($row = $ilDB->fetchAssoc($result))
82  {
83  if (($row["defaultvalue"] == 1) and ($row["owner_fi"] == 0))
84  {
85  $categories[$row["category_id"]] = $this->lng->txt($row["title"]);
86  }
87  else
88  {
89  $categories[$row["category_id"]] = $row["title"];
90  }
91  }
92  return $categories;
93  }
94 
101  function addPhrase($phrase_id)
102  {
103  global $ilUser;
104  global $ilDB;
105 
106  $result = $ilDB->queryF("SELECT svy_category.* FROM svy_category, svy_phrase_cat WHERE svy_phrase_cat.category_fi = svy_category.category_id AND svy_phrase_cat.phrase_fi = %s AND (svy_category.owner_fi = 0 OR svy_category.owner_fi = %s) ORDER BY svy_phrase_cat.sequence",
107  array('integer', 'integer'),
108  array($phrase_id, $ilUser->getId())
109  );
110  while ($row = $ilDB->fetchAssoc($result))
111  {
112  $neutral = $row["neutral"];
113  if (($row["defaultvalue"] == 1) and ($row["owner_fi"] == 0))
114  {
115  $this->categories->addCategory($this->lng->txt($row["title"]), 0, $neutral);
116  }
117  else
118  {
119  $this->categories->addCategory($row["title"], 0, $neutral);
120  }
121  }
122  }
123 
132  {
133  global $ilDB;
134 
135  $result = $ilDB->queryF("SELECT svy_question.*, " . $this->getAdditionalTableName() . ".* FROM svy_question, " . $this->getAdditionalTableName() . " WHERE svy_question.question_id = %s AND svy_question.question_id = " . $this->getAdditionalTableName() . ".question_fi",
136  array('integer'),
137  array($id)
138  );
139  if ($result->numRows() == 1)
140  {
141  return $ilDB->fetchAssoc($result);
142  }
143  else
144  {
145  return array();
146  }
147  }
148 
155  function loadFromDb($id)
156  {
157  global $ilDB;
158 
159  $result = $ilDB->queryF("SELECT svy_question.*, " . $this->getAdditionalTableName() . ".* FROM svy_question LEFT JOIN " . $this->getAdditionalTableName() . " ON " . $this->getAdditionalTableName() . ".question_fi = svy_question.question_id WHERE svy_question.question_id = %s",
160  array('integer'),
161  array($id)
162  );
163  if ($result->numRows() == 1)
164  {
165  $data = $ilDB->fetchAssoc($result);
166  $this->setId($data["question_id"]);
167  $this->setTitle($data["title"]);
168  $this->label = $data['label'];
169  $this->setDescription($data["description"]);
170  $this->setObjId($data["obj_fi"]);
171  $this->setAuthor($data["author"]);
172  $this->setOwner($data["owner_fi"]);
173  include_once("./Services/RTE/classes/class.ilRTE.php");
174  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
175  $this->setObligatory($data["obligatory"]);
176  $this->setComplete($data["complete"]);
177  $this->setOriginalId($data["original_id"]);
178  $this->setOrientation($data["orientation"]);
179 
180  $this->categories->flushCategories();
181  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title, svy_category.neutral FROM svy_variable, svy_category WHERE svy_variable.question_fi = %s AND svy_variable.category_fi = svy_category.category_id ORDER BY sequence ASC",
182  array('integer'),
183  array($id)
184  );
185  if ($result->numRows() > 0)
186  {
187  while ($data = $ilDB->fetchAssoc($result))
188  {
189  $this->categories->addCategory($data["title"], $data["other"], $data["neutral"], null, ($data['scale']) ? $data['scale'] : ($data['sequence'] + 1));
190  }
191  }
192  }
193  parent::loadFromDb($id);
194  }
195 
202  function isComplete()
203  {
204  if (
205  strlen($this->getTitle()) &&
206  strlen($this->getAuthor()) &&
207  strlen($this->getQuestiontext()) &&
208  $this->categories->getCategoryCount()
209  )
210  {
211  return 1;
212  }
213  else
214  {
215  return 0;
216  }
217  }
218 
224  function saveToDb($original_id = "")
225  {
226  global $ilDB;
227 
228  $affectedRows = parent::saveToDb($original_id);
229  if ($affectedRows == 1)
230  {
231  $this->log->debug("Before save Category-> DELETE from svy_qst_sc WHERE question_fi = ".$this->getId()." AND INSERT again the same id and orientation in svy_qst_sc");
232  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
233  array('integer'),
234  array($this->getId())
235  );
236  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, orientation) VALUES (%s, %s)",
237  array('integer', 'text'),
238  array(
239  $this->getId(),
240  $this->getOrientation()
241  )
242  );
243 
244  $this->saveMaterial();
245  $this->saveCategoriesToDb();
246  }
247  }
248 
250  {
251  global $ilDB;
252 
253  $this->log->debug("DELETE from svy_variable before the INSERT into svy_variable. if scale > 0 we get scale value else we get null");
254 
255  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_variable WHERE question_fi = %s",
256  array('integer'),
257  array($this->getId())
258  );
259 
260  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
261  {
262  $cat = $this->categories->getCategory($i);
263  $category_id = $this->saveCategoryToDb($cat->title, $cat->neutral);
264  $next_id = $ilDB->nextId('svy_variable');
265  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, other, sequence, scale, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
266  array('integer','integer','integer','float','integer','integer', 'integer','integer'),
267  array($next_id, $category_id, $this->getId(), ($i + 1), $cat->other, $i, ($cat->scale > 0) ? $cat->scale : null, time())
268  );
269 
270  $debug_scale = ($cat->scale > 0) ? $cat->scale : null;
271  $this->log->debug("INSERT INTO svy_variable category_fi= ".$category_id." question_fi= ".$this->getId()." value1= ".($i + 1)." other= ".$cat->other." sequence= ".$i." scale =".$debug_scale);
272  }
273  $this->saveCompletionStatus();
274  }
275 
282  function toXML($a_include_header = TRUE, $obligatory_state = "")
283  {
284  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
285  $a_xml_writer = new ilXmlWriter;
286  $a_xml_writer->xmlHeader();
287  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
288  $xml = $a_xml_writer->xmlDumpMem(FALSE);
289  if (!$a_include_header)
290  {
291  $pos = strpos($xml, "?>");
292  $xml = substr($xml, $pos + 2);
293  }
294  return $xml;
295  }
296 
304  function insertXML(&$a_xml_writer, $a_include_header = TRUE)
305  {
306  $attrs = array(
307  "id" => $this->getId(),
308  "title" => $this->getTitle(),
309  "type" => $this->getQuestiontype(),
310  "obligatory" => $this->getObligatory()
311  );
312  $a_xml_writer->xmlStartTag("question", $attrs);
313 
314  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
315  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
316  if (strlen($this->label))
317  {
318  $attrs = array(
319  "label" => $this->label,
320  );
321  }
322  else
323  {
324  $attrs = array();
325  }
326  $a_xml_writer->xmlStartTag("questiontext", $attrs);
327  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
328  $a_xml_writer->xmlEndTag("questiontext");
329 
330  $a_xml_writer->xmlStartTag("responses");
331 
332  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
333  {
334  $attrs = array(
335  "id" => $i
336  );
337  if (strlen($this->categories->getCategory($i)->other)) $attrs['other'] = $this->categories->getCategory($i)->other;
338  if (strlen($this->categories->getCategory($i)->neutral)) $attrs['neutral'] = $this->categories->getCategory($i)->neutral;
339  if (strlen($this->categories->getCategory($i)->label)) $attrs['label'] = $this->categories->getCategory($i)->label;
340  if (strlen($this->categories->getCategory($i)->scale)) $attrs['scale'] = $this->categories->getCategory($i)->scale;
341  $a_xml_writer->xmlStartTag("response_single", $attrs);
342  $this->addMaterialTag($a_xml_writer, $this->categories->getCategory($i)->title);
343  $a_xml_writer->xmlEndTag("response_single");
344  }
345 
346  $a_xml_writer->xmlEndTag("responses");
347 
348  if (count($this->material))
349  {
350  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
351  {
352  $attrs = array(
353  "label" => $this->material["title"]
354  );
355  $a_xml_writer->xmlStartTag("material", $attrs);
356  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
357  if (strcmp($matches[1], "") != 0)
358  {
359  $intlink = $this->material["internal_link"];
360  }
361  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
362  $a_xml_writer->xmlEndTag("material");
363  }
364  }
365 
366  $a_xml_writer->xmlStartTag("metadata");
367  $a_xml_writer->xmlStartTag("metadatafield");
368  $a_xml_writer->xmlElement("fieldlabel", NULL, "orientation");
369  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getOrientation());
370  $a_xml_writer->xmlEndTag("metadatafield");
371  $a_xml_writer->xmlEndTag("metadata");
372 
373  $a_xml_writer->xmlEndTag("question");
374  }
375 
384  function importAdditionalMetadata($a_meta)
385  {
386  foreach ($a_meta as $key => $value)
387  {
388  switch ($value["label"])
389  {
390  case "orientation":
391  $this->setOrientation($value["entry"]);
392  break;
393  }
394  }
395  }
396 
404  function addStandardNumbers($lower_limit, $upper_limit)
405  {
406  for ($i = $lower_limit; $i <= $upper_limit; $i++)
407  {
408  $this->categories->addCategory($i);
409  }
410  }
411 
419  function savePhrase($title)
420  {
421  global $ilUser;
422  global $ilDB;
423 
424  $next_id = $ilDB->nextId('svy_phrase');
425  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase (phrase_id, title, defaultvalue, owner_fi, tstamp) VALUES (%s, %s, %s, %s, %s)",
426  array('integer','text','text','integer','integer'),
427  array($next_id, $title, 1, $ilUser->getId(), time())
428  );
429  $phrase_id = $next_id;
430 
431  $counter = 1;
432  foreach ($_SESSION['save_phrase_data'] as $data)
433  {
434  $next_id = $ilDB->nextId('svy_category');
435  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_category (category_id, title, defaultvalue, owner_fi, tstamp, neutral) VALUES (%s, %s, %s, %s, %s, %s)",
436  array('integer','text','text','integer','integer','text'),
437  array($next_id, $data['answer'], 1, $ilUser->getId(), time(), $data['neutral'])
438  );
439  $category_id = $next_id;
440  $next_id = $ilDB->nextId('svy_phrase_cat');
441  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_phrase_cat (phrase_category_id, phrase_fi, category_fi, sequence, other, scale) VALUES (%s, %s, %s, %s, %s, %s)",
442  array('integer', 'integer', 'integer','integer', 'integer', 'integer'),
443  array($next_id, $phrase_id, $category_id, $counter, ($data['other']) ? 1 : 0, $data['scale'])
444  );
445  $counter++;
446  }
447  }
448 
455  function getQuestionType()
456  {
457  return "SurveySingleChoiceQuestion";
458  }
459 
467  {
468  return "svy_qst_sc";
469  }
470 
477  function &getWorkingDataFromUserInput($post_data)
478  {
479  $entered_value = $post_data[$this->getId() . "_value"];
480  $data = array();
481  if (strlen($entered_value))
482  {
483  array_push($data, array("value" => $entered_value, "textanswer" => $post_data[$this->getId() . '_' . $entered_value . '_other']));
484  }
485  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
486  {
487  $cat = $this->categories->getCategory($i);
488  if ($cat->other)
489  {
490  if ($i != $entered_value)
491  {
492  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
493  {
494  array_push($data, array("value" => $i, "textanswer" => $post_data[$this->getId() . '_' . $i . '_other'], "uncheck" => true));
495  }
496  }
497  }
498  }
499  return $data;
500  }
501 
511  function checkUserInput($post_data, $survey_id)
512  {
513  $entered_value = $post_data[$this->getId() . "_value"];
514 
515  $this->log->debug("Entered value = ".$entered_value);
516 
517  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
518 
519  if (strlen($entered_value) == 0) return $this->lng->txt("question_not_checked");
520 
521  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
522  {
523  $cat = $this->categories->getCategory($i);
524  if ($cat->other)
525  {
526  if ($i == $entered_value)
527  {
528  if (array_key_exists($this->getId() . "_" . $entered_value . "_other", $post_data) && !strlen($post_data[$this->getId() . "_" . $entered_value . "_other"]))
529  {
530  return $this->lng->txt("question_mr_no_other_answer");
531  }
532  }
533  else
534  {
535  if (strlen($post_data[$this->getId() . "_" . $i . "_other"]))
536  {
537  return $this->lng->txt("question_sr_no_other_answer_checked");
538  }
539  }
540  }
541  }
542 
543  return "";
544  }
545 
546  function saveUserInput($post_data, $active_id, $a_return = false)
547  {
548  global $ilDB;
549 
550  $entered_value = $post_data[$this->getId() . "_value"];
551 
552  if($a_return)
553  {
554  return array(array("value"=>$entered_value,
555  "textanswer"=>$post_data[$this->getId() . "_" . $entered_value . "_other"]));
556  }
557  if (strlen($entered_value) == 0) return;
558 
559  $next_id = $ilDB->nextId('svy_answer');
560  #20216
561  $fields = array();
562  $fields['answer_id'] = array("integer", $next_id);
563  $fields['question_fi'] = array("integer", $this->getId());
564  $fields['active_fi'] = array("integer", $active_id);
565  $fields['value'] = array("float", (strlen($entered_value)) ? $entered_value : NULL);
566  $fields['textanswer'] = array("clob", ($post_data[$this->getId() . "_" . $entered_value . "_other"]) ? $post_data[$this->getId() . "_" . $entered_value . "_other"] : null);
567  $fields['tstamp'] = array("integer", time());
568 
569  $affectedRows = $ilDB->insert("svy_answer", $fields);
570 
571  $debug_value = (strlen($entered_value)) ? $entered_value : "NULL";
572  $debug_answer = ($post_data[$this->getId() . "_" . $entered_value . "_other"]) ? $post_data[$this->getId() . "_" . $entered_value . "_other"] : "NULL";
573  $this->log->debug("INSERT svy_answer answer_id=".$next_id." question_fi=".$this->getId()." active_fi=".$active_id." value=".$debug_value. " textanswer=".$debug_answer);
574  }
575 
582  function importResponses($a_data)
583  {
584  foreach ($a_data as $id => $data)
585  {
586  $categorytext = "";
587  foreach ($data["material"] as $material)
588  {
589  $categorytext .= $material["text"];
590  }
591  $this->categories->addCategory(
592  $categorytext,
593  strlen($data['other']) ? $data['other'] : 0,
594  strlen($data['neutral']) ? $data['neutral'] : 0,
595  strlen($data['label']) ? $data['label'] : null,
596  strlen($data['scale']) ? $data['scale'] : null
597  );
598  }
599  }
600 
608  {
609  return TRUE;
610  }
611 
619  {
620  return array("<", "<=", "=", "<>", ">=", ">");
621  }
622 
628  public function getPreconditionOptions()
629  {
630  global $lng;
631 
632  $options = array();
633  for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
634  {
635  $category = $this->categories->getCategory($i);
636  $options[$category->scale-1] = $category->scale . " - " . $category->title;
637  }
638  return $options;
639  }
640 
647  public function getPreconditionSelectValue($default = "", $title, $variable)
648  {
649  include_once "./Services/Form/classes/class.ilSelectInputGUI.php";
650  $step3 = new ilSelectInputGUI($title, $variable);
651  $options = $this->getPreconditionOptions();
652  $step3->setOptions($options);
653  $step3->setValue($default);
654  return $step3;
655  }
656 
664  function getPreconditionValueOutput($value)
665  {
666  // #18136
667  $category = $this->categories->getCategoryForScale($value+1);
668 
669  // #17895 - see getPreconditionOptions()
670  return $category->scale .
671  " - " .
672  ((strlen($category->title)) ? $category->title : $this->lng->txt('other_answer'));
673  }
674 
675  public function getCategories()
676  {
677  return $this->categories;
678  }
679 
680 }
681 ?>
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
saveUserInput($post_data, $active_id, $a_return=false)
saveCategoryToDb($categorytext, $neutral=0)
Saves a category to the database.
saveCompletionStatus($original_id="")
Saves the complete flag to the database.
getPreconditionValueOutput($value)
Returns the output for a precondition value.
getAuthor()
Gets the authors name of the SurveyQuestion object.
getTitle()
Gets the title string of the SurveyQuestion object.
usableForPrecondition()
Returns if the question is usable for preconditions.
$_SESSION["AccountId"]
This class represents a selection list property in a property form.
$result
getObligatory($survey_id="")
Gets the obligatory state of the question.
setObligatory($obligatory=1)
Sets the obligatory state of the question.
setOrientation($orientation=0)
Sets the orientation of the question output.
insertXML(&$a_xml_writer, $a_include_header=TRUE)
Adds the question XML to a given XMLWriter object.
importAdditionalMetadata($a_meta)
Import additional meta data from the question import file.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
setId($id=-1)
Sets the id of the SurveyQuestion object.
isComplete()
Returns true if the question is complete for use.
savePhrase($title)
Saves a set of categories to a default phrase.
XML writer class.
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getOrientation()
Gets the orientation of the question output.
addStandardNumbers($lower_limit, $upper_limit)
Adds standard numbers as categories.
importResponses($a_data)
Import response data from the question import file.
saveToDb($original_id="")
Saves a SurveySingleChoiceQuestion object to a database.
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.
setComplete($a_complete)
Sets the complete state of the question.
$counter
setOriginalId($original_id)
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
Class SurveyCategories.
getPreconditionOptions()
Returns the options for preconditions.
if(!is_array($argv)) $options
getId()
Gets the id of the SurveyQuestion object.
Basic class for all survey question types.
saveMaterial()
save material to db
$ilUser
Definition: imgupload.php:18
xmlHeader()
Writes xml header public.
& getCategoriesForPhrase($phrase_id)
Gets the available categories for a given phrase.
getQuestionDataArray($id)
Returns the question data fields from the database.
Create styles array
The data for the language used.
toXML($a_include_header=TRUE, $obligatory_state="")
Returns an xml representation of the question.
setAuthor($author="")
Sets the authors name of the SurveyQuestion object.
loadFromDb($id)
Loads a SurveySingleChoiceQuestion object from the database.
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.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
global $ilDB
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
addPhrase($phrase_id)
Adds a phrase to the question.
getQuestionType()
Returns the question type of the question.
getDescription()
Gets the description string of the SurveyQuestion object.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
setObjId($obj_id=0)
Set the reference id of the container object.
__construct($title="", $description="", $author="", $questiontext="", $owner=-1, $orientation=1)
SurveySingleChoiceQuestion constructor.
getPreconditionSelectValue($default="", $title, $variable)
Creates a form property for the precondition value.
getAvailableRelations()
Returns the available relations for the question.
setTitle($title="")
Sets the title string of the SurveyQuestion object.