ILIAS  release_8 Revision v8.24
class.SurveyTextQuestion.php
Go to the documentation of this file.
1<?php
2
28{
29 protected ?int $maxchars = null;
30 protected ?int $textwidth = null;
31 protected ?int $textheight = null;
32
33 public function __construct(
34 string $title = "",
35 string $description = "",
36 string $author = "",
37 string $questiontext = "",
38 int $owner = -1
39 ) {
40 global $DIC;
41
42 $this->db = $DIC->database();
44
45 $this->maxchars = 0;
46 $this->textwidth = 50;
47 $this->textheight = 5;
48 }
49
50 public function getQuestionDataArray(int $id): array
51 {
53 $result = $ilDB->queryF(
54 "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",
55 array('integer'),
56 array($id)
57 );
58 if ($result->numRows() === 1) {
59 return $ilDB->fetchAssoc($result);
60 } else {
61 return array();
62 }
63 }
64
65 public function loadFromDb(int $question_id): void
66 {
68
69 $result = $ilDB->queryF(
70 "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",
71 array('integer'),
72 array($question_id)
73 );
74 if ($result->numRows() === 1) {
75 $data = $ilDB->fetchAssoc($result);
76 $this->setId((int) $data["question_id"]);
77 $this->setTitle((string) $data["title"]);
78 $this->label = (string) $data['label'];
79 $this->setDescription((string) $data["description"]);
80 $this->setObjId((int) $data["obj_fi"]);
81 $this->setAuthor((string) $data["author"]);
82 $this->setOwner((int) $data["owner_fi"]);
83 $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc((string) $data["questiontext"], 1));
84 $this->setObligatory((bool) $data["obligatory"]);
85 $this->setComplete((bool) $data["complete"]);
86 $this->setOriginalId((int) $data["original_id"]);
87
88 $this->setMaxChars((int) $data["maxchars"]);
89 $this->setTextWidth($data["width"] ? (int) $data["width"] : null);
90 $this->setTextHeight($data["height"] ? (int) $data["height"] : null);
91 }
92 parent::loadFromDb($question_id);
93 }
94
95 public function isComplete(): bool
96 {
97 if (
98 strlen($this->getTitle()) &&
99 strlen($this->getAuthor()) &&
100 strlen($this->getQuestiontext())
101 ) {
102 return true;
103 } else {
104 return false;
105 }
106 }
107
108 public function setMaxChars(int $maxchars = 0): void
109 {
110 $this->maxchars = $maxchars;
111 }
112
113 public function getMaxChars(): int
114 {
115 return $this->maxchars;
116 }
117
118 public function saveToDb(int $original_id = 0): int
119 {
121
122 $affectedRows = parent::saveToDb($original_id);
123 if ($affectedRows === 1) {
124 $ilDB->manipulateF(
125 "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
126 array('integer'),
127 array($this->getId())
128 );
129 $ilDB->manipulateF(
130 "INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
131 array('integer', 'integer', 'integer', 'integer'),
132 array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
133 );
134
135 $this->saveMaterial();
136 }
137 return $affectedRows;
138 }
139
140 public function toXML(
141 bool $a_include_header = true,
142 bool $obligatory_state = false
143 ): string {
144 $a_xml_writer = new ilXmlWriter();
145 $a_xml_writer->xmlHeader();
146 $this->insertXML($a_xml_writer, $a_include_header);
147 $xml = $a_xml_writer->xmlDumpMem(false);
148 if (!$a_include_header) {
149 $pos = strpos($xml, "?>");
150 $xml = substr($xml, $pos + 2);
151 }
152 return $xml;
153 }
154
155 public function insertXML(
156 ilXmlWriter $a_xml_writer,
157 $a_include_header = true
158 ): void {
159 $attrs = array(
160 "id" => $this->getId(),
161 "title" => $this->getTitle(),
162 "type" => $this->getQuestionType(),
163 "obligatory" => $this->getObligatory()
164 );
165 $a_xml_writer->xmlStartTag("question", $attrs);
166
167 $a_xml_writer->xmlElement("description", null, $this->getDescription());
168 $a_xml_writer->xmlElement("author", null, $this->getAuthor());
169 if (strlen($this->label)) {
170 $attrs = array(
171 "label" => $this->label,
172 );
173 } else {
174 $attrs = array();
175 }
176 $a_xml_writer->xmlStartTag("questiontext", $attrs);
177 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
178 $a_xml_writer->xmlEndTag("questiontext");
179
180 $a_xml_writer->xmlStartTag("responses");
181 $attrs = array(
182 "id" => "0",
183 "rows" => $this->getTextHeight(),
184 "columns" => $this->getTextWidth()
185 );
186 if ($this->getMaxChars() > 0) {
187 $attrs["maxlength"] = $this->getMaxChars();
188 }
189 $a_xml_writer->xmlElement("response_text", $attrs);
190 $a_xml_writer->xmlEndTag("responses");
191
192 if (count($this->material)) {
193 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
194 $attrs = array(
195 "label" => $this->material["title"]
196 );
197 $a_xml_writer->xmlStartTag("material", $attrs);
198 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
199 if (strcmp($matches[1], "") != 0) {
200 $intlink = $this->material["internal_link"];
201 }
202 $a_xml_writer->xmlElement("mattext", null, $intlink);
203 $a_xml_writer->xmlEndTag("material");
204 }
205 }
206
207 $a_xml_writer->xmlEndTag("question");
208 }
209
210 public function getQuestionType(): string
211 {
212 return "SurveyTextQuestion";
213 }
214
215 public function getAdditionalTableName(): string
216 {
217 return "svy_qst_text";
218 }
219
221 array $post_data
222 ): array {
223 $entered_value = $post_data[$this->getId() . "_text_question"] ?? "";
224 $data = array();
225 if (strlen($entered_value)) {
226 $data[] = array("textanswer" => $entered_value);
227 }
228 return $data;
229 }
230
235 public function checkUserInput(
236 array $post_data,
237 int $survey_id
238 ): string {
239 $entered_value = $post_data[$this->getId() . "_text_question"];
240
241 if ((!$this->getObligatory()) && (strlen($entered_value) == 0)) {
242 return "";
243 }
244
245 if (strlen($entered_value) == 0) {
246 return $this->lng->txt("text_question_not_filled_out");
247 }
248
249 // see bug #22648
250 if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars()) {
251 return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
252 }
253
254 return "";
255 }
256
257 public function saveUserInput(
258 array $post_data,
259 int $active_id,
260 bool $a_return = false
261 ): ?array {
262 $ilDB = $this->db;
263
264 $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
265 $maxchars = $this->getMaxChars();
266
267 if ($maxchars > 0) {
268 $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
269 }
270
271 if ($a_return) {
272 return array(array("value" => null, "textanswer" => $entered_value));
273 }
274 if (strlen($entered_value) == 0) {
275 return null;
276 }
277
278 $next_id = $ilDB->nextId('svy_answer');
279 #20216
280 $fields = array();
281 $fields['answer_id'] = array("integer", $next_id);
282 $fields['question_fi'] = array("integer", $this->getId());
283 $fields['active_fi'] = array("integer", $active_id);
284 $fields['value'] = array("float", null);
285 $fields['textanswer'] = array("clob", (strlen($entered_value)) ? $entered_value : null);
286 $fields['tstamp'] = array("integer", time());
287
288 $ilDB->insert("svy_answer", $fields);
289
290 return null;
291 }
292
293 public function importResponses(array $a_data): void
294 {
295 foreach ($a_data as $id => $data) {
296 if ($data["maxlength"] > 0) {
297 $this->setMaxChars($data["maxlength"]);
298 }
299 if ($data["rows"] > 0) {
300 $this->setTextHeight($data["rows"]);
301 }
302 if ($data["columns"] > 0) {
303 $this->setTextWidth($data["columns"]);
304 }
305 }
306 }
307
308 public function usableForPrecondition(): bool
309 {
310 return false;
311 }
312
313 public function getTextWidth(): ?int
314 {
315 return $this->textwidth;
316 }
317
318 public function getTextHeight(): ?int
319 {
320 return $this->textheight;
321 }
322
323 public function setTextWidth(?int $a_textwidth = null): void
324 {
325 if ($a_textwidth < 1) {
326 $this->textwidth = 50;
327 } else {
328 $this->textwidth = $a_textwidth;
329 }
330 }
331
332 public function setTextHeight(?int $a_textheight = null): void
333 {
334 if ($a_textheight < 1) {
335 $this->textheight = 5;
336 } else {
337 $this->textheight = $a_textheight;
338 }
339 }
340}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTitle(string $title="")
setQuestiontext(string $questiontext="")
setDescription(string $description="")
setObjId(int $obj_id=0)
Set the reference(?) id of the container object.
setOwner(int $owner=0)
setComplete(bool $a_complete)
setOriginalId(?int $original_id)
setObligatory(bool $obligatory=true)
setAuthor(string $author="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
importResponses(array $a_data)
Import response data from the question import file.
toXML(bool $a_include_header=true, bool $obligatory_state=false)
setTextHeight(?int $a_textheight=null)
saveToDb(int $original_id=0)
Saves a SurveyQuestion object to a database.
usableForPrecondition()
Returns if the question is usable for preconditions.
checkUserInput(array $post_data, int $survey_id)
Checks the input of the active user for obligatory status and entered values.
getQuestionDataArray(int $id)
Returns the question data.
insertXML(ilXmlWriter $a_xml_writer, $a_include_header=true)
saveUserInput(array $post_data, int $active_id, bool $a_return=false)
__construct(string $title="", string $description="", string $author="", string $questiontext="", int $owner=-1)
loadFromDb(int $question_id)
load question data into object note: this base implementation only loads the material data
setTextWidth(?int $a_textwidth=null)
getWorkingDataFromUserInput(array $post_data)
Creates the user data of the svy_answer table from the POST data.
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
static strLen(string $a_string)
Definition: class.ilStr.php:63
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$xml
Definition: metadata.php:351
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc