ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.SurveyTextQuestion.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 {
39  public $maxchars;
40  public $textwidth;
41  public $textheight;
42 
52  public function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1)
53  {
54  global $DIC;
55 
56  $this->db = $DIC->database();
57  parent::__construct($title, $description, $author, $questiontext, $owner);
58 
59  $this->maxchars = 0;
60  $this->textwidth = 50;
61  $this->textheight = 5;
62  }
63 
71  public function getQuestionDataArray($id)
72  {
73  $ilDB = $this->db;
74  $result = $ilDB->queryF(
75  "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",
76  array('integer'),
77  array($id)
78  );
79  if ($result->numRows() == 1) {
80  return $ilDB->fetchAssoc($result);
81  } else {
82  return array();
83  }
84  }
85 
92  public function loadFromDb($id)
93  {
94  $ilDB = $this->db;
95 
96  $result = $ilDB->queryF(
97  "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",
98  array('integer'),
99  array($id)
100  );
101  if ($result->numRows() == 1) {
102  $data = $ilDB->fetchAssoc($result);
103  $this->setId($data["question_id"]);
104  $this->setTitle($data["title"]);
105  $this->label = $data['label'];
106  $this->setDescription($data["description"]);
107  $this->setObjId($data["obj_fi"]);
108  $this->setAuthor($data["author"]);
109  $this->setOwner($data["owner_fi"]);
110  include_once("./Services/RTE/classes/class.ilRTE.php");
111  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
112  $this->setObligatory($data["obligatory"]);
113  $this->setComplete($data["complete"]);
114  $this->setOriginalId($data["original_id"]);
115 
116  $this->setMaxChars($data["maxchars"]);
117  $this->setTextWidth($data["width"]);
118  $this->setTextHeight($data["height"]);
119  }
120  parent::loadFromDb($id);
121  }
122 
129  public function isComplete()
130  {
131  if (
132  strlen($this->getTitle()) &&
133  strlen($this->getAuthor()) &&
134  strlen($this->getQuestiontext())
135  ) {
136  return 1;
137  } else {
138  return 0;
139  }
140  }
141 
147  public function setMaxChars($maxchars = 0)
148  {
149  $this->maxchars = $maxchars;
150  }
151 
157  public function getMaxChars()
158  {
159  return ($this->maxchars) ? $this->maxchars : null;
160  }
161 
167  public function saveToDb($original_id = "")
168  {
169  $ilDB = $this->db;
170 
171  $affectedRows = parent::saveToDb($original_id);
172  if ($affectedRows == 1) {
173  $affectedRows = $ilDB->manipulateF(
174  "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
175  array('integer'),
176  array($this->getId())
177  );
178  $affectedRows = $ilDB->manipulateF(
179  "INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
180  array('integer', 'integer', 'integer', 'integer'),
181  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
182  );
183 
184  $this->saveMaterial();
185  }
186  }
187 
194  public function toXML($a_include_header = true, $obligatory_state = "")
195  {
196  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
197  $a_xml_writer = new ilXmlWriter;
198  $a_xml_writer->xmlHeader();
199  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
200  $xml = $a_xml_writer->xmlDumpMem(false);
201  if (!$a_include_header) {
202  $pos = strpos($xml, "?>");
203  $xml = substr($xml, $pos + 2);
204  }
205  return $xml;
206  }
207 
215  public function insertXML(&$a_xml_writer, $a_include_header = true)
216  {
217  $attrs = array(
218  "id" => $this->getId(),
219  "title" => $this->getTitle(),
220  "type" => $this->getQuestiontype(),
221  "obligatory" => $this->getObligatory()
222  );
223  $a_xml_writer->xmlStartTag("question", $attrs);
224 
225  $a_xml_writer->xmlElement("description", null, $this->getDescription());
226  $a_xml_writer->xmlElement("author", null, $this->getAuthor());
227  if (strlen($this->label)) {
228  $attrs = array(
229  "label" => $this->label,
230  );
231  } else {
232  $attrs = array();
233  }
234  $a_xml_writer->xmlStartTag("questiontext", $attrs);
235  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
236  $a_xml_writer->xmlEndTag("questiontext");
237 
238  $a_xml_writer->xmlStartTag("responses");
239  $attrs = array(
240  "id" => "0",
241  "rows" => $this->getTextHeight(),
242  "columns" => $this->getTextWidth()
243  );
244  if ($this->getMaxChars() > 0) {
245  $attrs["maxlength"] = $this->getMaxChars();
246  }
247  $a_xml_writer->xmlElement("response_text", $attrs);
248  $a_xml_writer->xmlEndTag("responses");
249 
250  if (count($this->material)) {
251  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
252  $attrs = array(
253  "label" => $this->material["title"]
254  );
255  $a_xml_writer->xmlStartTag("material", $attrs);
256  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
257  if (strcmp($matches[1], "") != 0) {
258  $intlink = $this->material["internal_link"];
259  }
260  $a_xml_writer->xmlElement("mattext", null, $intlink);
261  $a_xml_writer->xmlEndTag("material");
262  }
263  }
264 
265  $a_xml_writer->xmlEndTag("question");
266  }
267 
274  public function getQuestionType()
275  {
276  return "SurveyTextQuestion";
277  }
278 
285  public function getAdditionalTableName()
286  {
287  return "svy_qst_text";
288  }
289 
296  public function &getWorkingDataFromUserInput($post_data)
297  {
298  $entered_value = $post_data[$this->getId() . "_text_question"];
299  $data = array();
300  if (strlen($entered_value)) {
301  array_push($data, array("textanswer" => $entered_value));
302  }
303  return $data;
304  }
305 
315  public function checkUserInput($post_data, $survey_id)
316  {
317  $entered_value = $post_data[$this->getId() . "_text_question"];
318 
319  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) {
320  return "";
321  }
322 
323  if (strlen($entered_value) == 0) {
324  return $this->lng->txt("text_question_not_filled_out");
325  }
326 
327  // see bug #22648
328  include_once("./Services/Utilities/classes/class.ilStr.php");
329  if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars()) {
330  return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
331  }
332 
333  return "";
334  }
335 
336  public function saveUserInput($post_data, $active_id, $a_return = false)
337  {
338  $ilDB = $this->db;
339 
340  $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
341  $maxchars = $this->getMaxChars();
342 
343  include_once("./Services/Utilities/classes/class.ilStr.php");
344  if ($maxchars > 0) {
345  $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
346  }
347 
348  if ($a_return) {
349  return array(array("value" => null, "textanswer" => $entered_value));
350  }
351  if (strlen($entered_value) == 0) {
352  return;
353  }
354 
355  $next_id = $ilDB->nextId('svy_answer');
356  #20216
357  $fields = array();
358  $fields['answer_id'] = array("integer", $next_id);
359  $fields['question_fi'] = array("integer", $this->getId());
360  $fields['active_fi'] = array("integer", $active_id);
361  $fields['value'] = array("float", null);
362  $fields['textanswer'] = array("clob", (strlen($entered_value)) ? $entered_value : null);
363  $fields['tstamp'] = array("integer", time());
364 
365  $affectedRows = $ilDB->insert("svy_answer", $fields);
366  }
367 
374  public function importResponses($a_data)
375  {
376  foreach ($a_data as $id => $data) {
377  if ($data["maxlength"] > 0) {
378  $this->setMaxChars($data["maxlength"]);
379  }
380  if ($data["rows"] > 0) {
381  $this->setTextHeight($data["rows"]);
382  }
383  if ($data["columns"] > 0) {
384  $this->setTextWidth($data["columns"]);
385  }
386  }
387  }
388 
395  public function usableForPrecondition()
396  {
397  return false;
398  }
399 
406  public function getTextWidth()
407  {
408  return ($this->textwidth) ? $this->textwidth : null;
409  }
410 
417  public function getTextHeight()
418  {
419  return ($this->textheight) ? $this->textheight : null;
420  }
421 
428  public function setTextWidth($a_textwidth)
429  {
430  if ($a_textwidth < 1) {
431  $this->textwidth = 50;
432  } else {
433  $this->textwidth = $a_textwidth;
434  }
435  }
436 
443  public function setTextHeight($a_textheight)
444  {
445  if ($a_textheight < 1) {
446  $this->textheight = 5;
447  } else {
448  $this->textheight = $a_textheight;
449  }
450  }
451 }
setTextHeight($a_textheight)
Sets the height of the answer field.
getTextWidth()
Returns the width of the answer field.
getAuthor()
Gets the authors name of the SurveyQuestion object.
static strLen($a_string)
Definition: class.ilStr.php:78
getTitle()
Gets the title string of the SurveyQuestion object.
$result
getObligatory($survey_id="")
Gets the obligatory state of the question.
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.
saveToDb($original_id="")
Saves a SurveyTextQuestion object to a database.
setId($id=-1)
Sets the id of the SurveyQuestion object.
isComplete()
Returns true if the question is complete for use.
XML writer class.
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getQuestionType()
Returns the question type of the question.
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
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.
setMaxChars($maxchars=0)
Sets the maximum number of allowed characters for the text answer.
getQuestionDataArray($id)
Returns the question data fields from the database.
setOriginalId($original_id)
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
saveUserInput($post_data, $active_id, $a_return=false)
getId()
Gets the id of the SurveyQuestion object.
importResponses($a_data)
Import response data from the question import file.
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.
saveMaterial()
save material to db
loadFromDb($id)
Loads a SurveyTextQuestion object from the database.
xmlHeader()
Writes xml header public.
insertXML(&$a_xml_writer, $a_include_header=true)
Adds the question XML to a given XMLWriter object.
usableForPrecondition()
Returns if the question is usable for preconditions.
setAuthor($author="")
Sets the authors name of the SurveyQuestion object.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
global $ilDB
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
toXML($a_include_header=true, $obligatory_state="")
Returns an xml representation of the question.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
Text survey question.
getDescription()
Gets the description string of the SurveyQuestion object.
setObjId($obj_id=0)
Set the reference id of the container object.
getMaxChars()
Returns the maximum number of allowed characters for the text answer.
getTextHeight()
Returns the height of the answer field.
setTextWidth($a_textwidth)
Sets the width of the answer field.
__construct($title="", $description="", $author="", $questiontext="", $owner=-1)
The constructor takes possible arguments an creates an instance of the SurveyTextQuestion object...
$data
Definition: bench.php:6
setTitle($title="")
Sets the title string of the SurveyQuestion object.