ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
24include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
25
38{
42
52 function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1)
53 {
54 parent::__construct($title, $description, $author, $questiontext, $owner);
55
56 $this->maxchars = 0;
57 $this->textwidth = 50;
58 $this->textheight = 5;
59 }
60
69 {
70 global $ilDB;
71 $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",
72 array('integer'),
73 array($id)
74 );
75 if ($result->numRows() == 1)
76 {
77 return $ilDB->fetchAssoc($result);
78 }
79 else
80 {
81 return array();
82 }
83 }
84
91 function loadFromDb($id)
92 {
93 global $ilDB;
94
95 $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",
96 array('integer'),
97 array($id)
98 );
99 if ($result->numRows() == 1)
100 {
101 $data = $ilDB->fetchAssoc($result);
102 $this->setId($data["question_id"]);
103 $this->setTitle($data["title"]);
104 $this->label = $data['label'];
105 $this->setDescription($data["description"]);
106 $this->setObjId($data["obj_fi"]);
107 $this->setAuthor($data["author"]);
108 $this->setOwner($data["owner_fi"]);
109 include_once("./Services/RTE/classes/class.ilRTE.php");
110 $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
111 $this->setObligatory($data["obligatory"]);
112 $this->setComplete($data["complete"]);
113 $this->setOriginalId($data["original_id"]);
114
115 $this->setMaxChars($data["maxchars"]);
116 $this->setTextWidth($data["width"]);
117 $this->setTextHeight($data["height"]);
118
119 }
120 parent::loadFromDb($id);
121 }
122
129 function isComplete()
130 {
131 if (
132 strlen($this->getTitle()) &&
133 strlen($this->getAuthor()) &&
134 strlen($this->getQuestiontext())
135 )
136 {
137 return 1;
138 }
139 else
140 {
141 return 0;
142 }
143 }
144
150 function setMaxChars($maxchars = 0)
151 {
152 $this->maxchars = $maxchars;
153 }
154
160 function getMaxChars()
161 {
162 return ($this->maxchars) ? $this->maxchars : NULL;
163 }
164
170 function saveToDb($original_id = "")
171 {
172 global $ilDB;
173
174 $affectedRows = parent::saveToDb($original_id);
175 if ($affectedRows == 1)
176 {
177 $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
178 array('integer'),
179 array($this->getId())
180 );
181 $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
182 array('integer', 'integer', 'integer', 'integer'),
183 array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
184 );
185
186 $this->saveMaterial();
187 }
188 }
189
196 function toXML($a_include_header = TRUE, $obligatory_state = "")
197 {
198 include_once("./Services/Xml/classes/class.ilXmlWriter.php");
199 $a_xml_writer = new ilXmlWriter;
200 $a_xml_writer->xmlHeader();
201 $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
202 $xml = $a_xml_writer->xmlDumpMem(FALSE);
203 if (!$a_include_header)
204 {
205 $pos = strpos($xml, "?>");
206 $xml = substr($xml, $pos + 2);
207 }
208 return $xml;
209 }
210
218 function insertXML(&$a_xml_writer, $a_include_header = TRUE)
219 {
220 $attrs = array(
221 "id" => $this->getId(),
222 "title" => $this->getTitle(),
223 "type" => $this->getQuestiontype(),
224 "obligatory" => $this->getObligatory()
225 );
226 $a_xml_writer->xmlStartTag("question", $attrs);
227
228 $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
229 $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
230 if (strlen($this->label))
231 {
232 $attrs = array(
233 "label" => $this->label,
234 );
235 }
236 else
237 {
238 $attrs = array();
239 }
240 $a_xml_writer->xmlStartTag("questiontext", $attrs);
241 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
242 $a_xml_writer->xmlEndTag("questiontext");
243
244 $a_xml_writer->xmlStartTag("responses");
245 $attrs = array(
246 "id" => "0",
247 "rows" => $this->getTextHeight(),
248 "columns" => $this->getTextWidth()
249 );
250 if ($this->getMaxChars() > 0)
251 {
252 $attrs["maxlength"] = $this->getMaxChars();
253 }
254 $a_xml_writer->xmlElement("response_text", $attrs);
255 $a_xml_writer->xmlEndTag("responses");
256
257 if (count($this->material))
258 {
259 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
260 {
261 $attrs = array(
262 "label" => $this->material["title"]
263 );
264 $a_xml_writer->xmlStartTag("material", $attrs);
265 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
266 if (strcmp($matches[1], "") != 0)
267 {
268 $intlink = $this->material["internal_link"];
269 }
270 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
271 $a_xml_writer->xmlEndTag("material");
272 }
273 }
274
275 $a_xml_writer->xmlEndTag("question");
276 }
277
285 {
286 return "SurveyTextQuestion";
287 }
288
296 {
297 return "svy_qst_text";
298 }
299
306 function &getWorkingDataFromUserInput($post_data)
307 {
308 $entered_value = $post_data[$this->getId() . "_text_question"];
309 $data = array();
310 if (strlen($entered_value))
311 {
312 array_push($data, array("textanswer" => $entered_value));
313 }
314 return $data;
315 }
316
326 function checkUserInput($post_data, $survey_id)
327 {
328 $entered_value = $post_data[$this->getId() . "_text_question"];
329
330 if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
331
332 if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
333
334 // see bug #22648
335 include_once("./Services/Utilities/classes/class.ilStr.php");
336 if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars())
337 {
338 return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
339 }
340
341 return "";
342 }
343
344 function saveUserInput($post_data, $active_id, $a_return = false)
345 {
346 global $ilDB;
347
348 $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
349 $maxchars = $this->getMaxChars();
350
351 include_once("./Services/Utilities/classes/class.ilStr.php");
352 if ($maxchars > 0)
353 {
354 $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
355 }
356
357 if($a_return)
358 {
359 return array(array("value"=>null, "textanswer"=>$entered_value));
360 }
361 if (strlen($entered_value) == 0) return;
362
363 $next_id = $ilDB->nextId('svy_answer');
364 #20216
365 $fields = array();
366 $fields['answer_id'] = array("integer", $next_id);
367 $fields['question_fi'] = array("integer", $this->getId());
368 $fields['active_fi'] = array("integer", $active_id);
369 $fields['value'] = array("float", NULL);
370 $fields['textanswer'] = array("clob", (strlen($entered_value)) ? $entered_value : NULL);
371 $fields['tstamp'] = array("integer", time());
372
373 $affectedRows = $ilDB->insert("svy_answer", $fields);
374
375 }
376
383 function importResponses($a_data)
384 {
385 foreach ($a_data as $id => $data)
386 {
387 if ($data["maxlength"] > 0)
388 {
389 $this->setMaxChars($data["maxlength"]);
390 }
391 if ($data["rows"] > 0)
392 {
393 $this->setTextHeight($data["rows"]);
394 }
395 if ($data["columns"] > 0)
396 {
397 $this->setTextWidth($data["columns"]);
398 }
399 }
400 }
401
409 {
410 return FALSE;
411 }
412
419 function getTextWidth()
420 {
421 return ($this->textwidth) ? $this->textwidth : NULL;
422 }
423
430 function getTextHeight()
431 {
432 return ($this->textheight) ? $this->textheight : NULL;
433 }
434
441 function setTextWidth($a_textwidth)
442 {
443 if ($a_textwidth < 1)
444 {
445 $this->textwidth = 50;
446 }
447 else
448 {
449 $this->textwidth = $a_textwidth;
450 }
451 }
452
459 function setTextHeight($a_textheight)
460 {
461 if ($a_textheight < 1)
462 {
463 $this->textheight = 5;
464 }
465 else
466 {
467 $this->textheight = $a_textheight;
468 }
469 }
470}
471?>
$result
An exception for terminatinating execution or to throw for unit testing.
Basic class for all survey question types.
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
setId($id=-1)
Sets the id of the SurveyQuestion object.
setAuthor($author="")
Sets the authors name of the SurveyQuestion object.
stripSlashesAddSpaceFallback($a_str)
Strip slashes with add space fallback, see https://mantis.ilias.de/view.php?id=19727 and https://mant...
getDescription()
Gets the description string of the SurveyQuestion object.
getId()
Gets the id of the SurveyQuestion object.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
setObjId($obj_id=0)
Set the reference id of the container object.
getAuthor()
Gets the authors name of the SurveyQuestion object.
setOriginalId($original_id)
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getObligatory($survey_id="")
Gets the obligatory state of the question.
getTitle()
Gets the title string of the SurveyQuestion object.
setComplete($a_complete)
Sets the complete state of the question.
saveMaterial()
save material to db
setOwner($owner="")
Sets the creator/owner ID of the SurveyQuestion object.
setTitle($title="")
Sets the title string of the SurveyQuestion object.
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.
setObligatory($obligatory=1)
Sets the obligatory state of the question.
Text survey question.
getTextWidth()
Returns the width of the answer field.
insertXML(&$a_xml_writer, $a_include_header=TRUE)
Adds the question XML to a given XMLWriter object.
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
saveUserInput($post_data, $active_id, $a_return=false)
setMaxChars($maxchars=0)
Sets the maximum number of allowed characters for the text answer.
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
isComplete()
Returns true if the question is complete for use.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
saveToDb($original_id="")
Saves a SurveyTextQuestion object to a database.
importResponses($a_data)
Import response data from the question import file.
getMaxChars()
Returns the maximum number of allowed characters for the text answer.
setTextWidth($a_textwidth)
Sets the width of the answer field.
usableForPrecondition()
Returns if the question is usable for preconditions.
toXML($a_include_header=TRUE, $obligatory_state="")
Returns an xml representation of the question.
getQuestionType()
Returns the question type of the question.
__construct($title="", $description="", $author="", $questiontext="", $owner=-1)
The constructor takes possible arguments an creates an instance of the SurveyTextQuestion object.
getTextHeight()
Returns the height of the answer field.
loadFromDb($id)
Loads a SurveyTextQuestion object from the database.
setTextHeight($a_textheight)
Sets the height of the answer field.
getQuestionDataArray($id)
Returns the question data fields from the 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...
static strLen($a_string)
Definition: class.ilStr.php:91
static subStr($a_str, $a_start, $a_length=NULL)
Definition: class.ilStr.php:15
XML writer class.
xmlHeader()
Writes xml header @access public.
global $ilDB