ILIAS  release_7 Revision v7.30-3-g800a261c036
class.SurveyTextQuestion.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
14{
15 public $maxchars;
16 public $textwidth;
18
28 public function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1)
29 {
30 global $DIC;
31
32 $this->db = $DIC->database();
34
35 $this->maxchars = 0;
36 $this->textwidth = 50;
37 $this->textheight = 5;
38 }
39
47 public function getQuestionDataArray($id)
48 {
50 $result = $ilDB->queryF(
51 "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",
52 array('integer'),
53 array($id)
54 );
55 if ($result->numRows() == 1) {
56 return $ilDB->fetchAssoc($result);
57 } else {
58 return array();
59 }
60 }
61
68 public function loadFromDb($id)
69 {
71
72 $result = $ilDB->queryF(
73 "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",
74 array('integer'),
75 array($id)
76 );
77 if ($result->numRows() == 1) {
78 $data = $ilDB->fetchAssoc($result);
79 $this->setId($data["question_id"]);
80 $this->setTitle($data["title"]);
81 $this->label = $data['label'];
82 $this->setDescription($data["description"]);
83 $this->setObjId($data["obj_fi"]);
84 $this->setAuthor($data["author"]);
85 $this->setOwner($data["owner_fi"]);
87 $this->setObligatory($data["obligatory"]);
88 $this->setComplete($data["complete"]);
89 $this->setOriginalId($data["original_id"]);
90
91 $this->setMaxChars($data["maxchars"]);
92 $this->setTextWidth($data["width"]);
93 $this->setTextHeight($data["height"]);
94 }
95 parent::loadFromDb($id);
96 }
97
104 public function isComplete()
105 {
106 if (
107 strlen($this->getTitle()) &&
108 strlen($this->getAuthor()) &&
109 strlen($this->getQuestiontext())
110 ) {
111 return 1;
112 } else {
113 return 0;
114 }
115 }
116
122 public function setMaxChars($maxchars = 0)
123 {
124 $this->maxchars = $maxchars;
125 }
126
132 public function getMaxChars()
133 {
134 return ($this->maxchars) ? $this->maxchars : null;
135 }
136
142 public function saveToDb($original_id = "")
143 {
145
146 $affectedRows = parent::saveToDb($original_id);
147 if ($affectedRows == 1) {
148 $affectedRows = $ilDB->manipulateF(
149 "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
150 array('integer'),
151 array($this->getId())
152 );
153 $affectedRows = $ilDB->manipulateF(
154 "INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
155 array('integer', 'integer', 'integer', 'integer'),
156 array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
157 );
158
159 $this->saveMaterial();
160 }
161 }
162
169 public function toXML($a_include_header = true, $obligatory_state = "")
170 {
171 $a_xml_writer = new ilXmlWriter;
172 $a_xml_writer->xmlHeader();
173 $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
174 $xml = $a_xml_writer->xmlDumpMem(false);
175 if (!$a_include_header) {
176 $pos = strpos($xml, "?>");
177 $xml = substr($xml, $pos + 2);
178 }
179 return $xml;
180 }
181
189 public function insertXML(&$a_xml_writer, $a_include_header = true)
190 {
191 $attrs = array(
192 "id" => $this->getId(),
193 "title" => $this->getTitle(),
194 "type" => $this->getQuestiontype(),
195 "obligatory" => $this->getObligatory()
196 );
197 $a_xml_writer->xmlStartTag("question", $attrs);
198
199 $a_xml_writer->xmlElement("description", null, $this->getDescription());
200 $a_xml_writer->xmlElement("author", null, $this->getAuthor());
201 if (strlen($this->label)) {
202 $attrs = array(
203 "label" => $this->label,
204 );
205 } else {
206 $attrs = array();
207 }
208 $a_xml_writer->xmlStartTag("questiontext", $attrs);
209 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
210 $a_xml_writer->xmlEndTag("questiontext");
211
212 $a_xml_writer->xmlStartTag("responses");
213 $attrs = array(
214 "id" => "0",
215 "rows" => $this->getTextHeight(),
216 "columns" => $this->getTextWidth()
217 );
218 if ($this->getMaxChars() > 0) {
219 $attrs["maxlength"] = $this->getMaxChars();
220 }
221 $a_xml_writer->xmlElement("response_text", $attrs);
222 $a_xml_writer->xmlEndTag("responses");
223
224 if (count($this->material)) {
225 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
226 $attrs = array(
227 "label" => $this->material["title"]
228 );
229 $a_xml_writer->xmlStartTag("material", $attrs);
230 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
231 if (strcmp($matches[1], "") != 0) {
232 $intlink = $this->material["internal_link"];
233 }
234 $a_xml_writer->xmlElement("mattext", null, $intlink);
235 $a_xml_writer->xmlEndTag("material");
236 }
237 }
238
239 $a_xml_writer->xmlEndTag("question");
240 }
241
248 public function getQuestionType()
249 {
250 return "SurveyTextQuestion";
251 }
252
259 public function getAdditionalTableName()
260 {
261 return "svy_qst_text";
262 }
263
270 public function &getWorkingDataFromUserInput($post_data)
271 {
272 $entered_value = $post_data[$this->getId() . "_text_question"];
273 $data = array();
274 if (strlen($entered_value)) {
275 array_push($data, array("textanswer" => $entered_value));
276 }
277 return $data;
278 }
279
289 public function checkUserInput($post_data, $survey_id)
290 {
291 $entered_value = $post_data[$this->getId() . "_text_question"];
292
293 if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) {
294 return "";
295 }
296
297 if (strlen($entered_value) == 0) {
298 return $this->lng->txt("text_question_not_filled_out");
299 }
300
301 // see bug #22648
302 if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars()) {
303 return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
304 }
305
306 return "";
307 }
308
309 public function saveUserInput($post_data, $active_id, $a_return = false)
310 {
312
313 $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
314 $maxchars = $this->getMaxChars();
315
316 if ($maxchars > 0) {
317 $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
318 }
319
320 if ($a_return) {
321 return array(array("value" => null, "textanswer" => $entered_value));
322 }
323 if (strlen($entered_value) == 0) {
324 return;
325 }
326
327 $next_id = $ilDB->nextId('svy_answer');
328 #20216
329 $fields = array();
330 $fields['answer_id'] = array("integer", $next_id);
331 $fields['question_fi'] = array("integer", $this->getId());
332 $fields['active_fi'] = array("integer", $active_id);
333 $fields['value'] = array("float", null);
334 $fields['textanswer'] = array("clob", (strlen($entered_value)) ? $entered_value : null);
335 $fields['tstamp'] = array("integer", time());
336
337 $affectedRows = $ilDB->insert("svy_answer", $fields);
338 }
339
346 public function importResponses($a_data)
347 {
348 foreach ($a_data as $id => $data) {
349 if ($data["maxlength"] > 0) {
350 $this->setMaxChars($data["maxlength"]);
351 }
352 if ($data["rows"] > 0) {
353 $this->setTextHeight($data["rows"]);
354 }
355 if ($data["columns"] > 0) {
356 $this->setTextWidth($data["columns"]);
357 }
358 }
359 }
360
367 public function usableForPrecondition()
368 {
369 return false;
370 }
371
378 public function getTextWidth()
379 {
380 return ($this->textwidth) ? $this->textwidth : null;
381 }
382
389 public function getTextHeight()
390 {
391 return ($this->textheight) ? $this->textheight : null;
392 }
393
400 public function setTextWidth($a_textwidth)
401 {
402 if ($a_textwidth < 1) {
403 $this->textwidth = 50;
404 } else {
405 $this->textwidth = $a_textwidth;
406 }
407 }
408
415 public function setTextHeight($a_textheight)
416 {
417 if ($a_textheight < 1) {
418 $this->textheight = 5;
419 } else {
420 $this->textheight = $a_textheight;
421 }
422 }
423}
$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.
toXML($a_include_header=true, $obligatory_state="")
Returns an xml representation of the question.
setTextWidth($a_textwidth)
Sets the width of the answer field.
usableForPrecondition()
Returns if the question is usable for preconditions.
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 subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
static strLen($a_string)
Definition: class.ilStr.php:78
XML writer class.
xmlHeader()
Writes xml header @access public.
const IL_INST_ID
Definition: constants.php:38
global $DIC
Definition: goto.php:24
$xml
Definition: metadata.php:332
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $ilDB
$data
Definition: storeScorm.php:23