ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  {
52  $ilDB = $this->db;
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  {
67  $ilDB = $this->db;
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  {
120  $ilDB = $this->db;
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  return $affectedRows;
136  }
137 
138  public function toXML(
139  bool $a_include_header = true,
140  bool $obligatory_state = false
141  ): string {
142  $a_xml_writer = new ilXmlWriter();
143  $a_xml_writer->xmlHeader();
144  $this->insertXML($a_xml_writer, $a_include_header);
145  $xml = $a_xml_writer->xmlDumpMem(false);
146  if (!$a_include_header) {
147  $pos = strpos($xml, "?>");
148  $xml = substr($xml, $pos + 2);
149  }
150  return $xml;
151  }
152 
153  public function insertXML(
154  ilXmlWriter $a_xml_writer,
155  $a_include_header = true
156  ): void {
157  $attrs = array(
158  "id" => $this->getId(),
159  "title" => $this->getTitle(),
160  "type" => $this->getQuestionType(),
161  "obligatory" => $this->getObligatory()
162  );
163  $a_xml_writer->xmlStartTag("question", $attrs);
164 
165  $a_xml_writer->xmlElement("description", null, $this->getDescription());
166  $a_xml_writer->xmlElement("author", null, $this->getAuthor());
167  if (strlen($this->label ?? "")) {
168  $attrs = array(
169  "label" => $this->label,
170  );
171  } else {
172  $attrs = array();
173  }
174  $a_xml_writer->xmlStartTag("questiontext", $attrs);
175  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
176  $a_xml_writer->xmlEndTag("questiontext");
177 
178  $a_xml_writer->xmlStartTag("responses");
179  $attrs = array(
180  "id" => "0",
181  "rows" => $this->getTextHeight(),
182  "columns" => $this->getTextWidth()
183  );
184  if ($this->getMaxChars() > 0) {
185  $attrs["maxlength"] = $this->getMaxChars();
186  }
187  $a_xml_writer->xmlElement("response_text", $attrs);
188  $a_xml_writer->xmlEndTag("responses");
189 
190  if (count($this->material)) {
191  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
192  $attrs = array(
193  "label" => $this->material["title"]
194  );
195  $a_xml_writer->xmlStartTag("material", $attrs);
196  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
197  if (strcmp($matches[1], "") != 0) {
198  $intlink = $this->material["internal_link"];
199  }
200  $a_xml_writer->xmlElement("mattext", null, $intlink);
201  $a_xml_writer->xmlEndTag("material");
202  }
203  }
204 
205  $a_xml_writer->xmlEndTag("question");
206  }
207 
208  public function getQuestionType(): string
209  {
210  return "SurveyTextQuestion";
211  }
212 
213  public function getAdditionalTableName(): string
214  {
215  return "svy_qst_text";
216  }
217 
219  array $post_data
220  ): array {
221  $entered_value = $post_data[$this->getId() . "_text_question"] ?? "";
222  $data = array();
223  if (strlen($entered_value ?? "")) {
224  $data[] = array("textanswer" => $entered_value);
225  }
226  return $data;
227  }
228 
233  public function checkUserInput(
234  array $post_data,
235  int $survey_id
236  ): string {
237  $entered_value = $post_data[$this->getId() . "_text_question"];
238 
239  if ((!$this->getObligatory()) && (strlen($entered_value ?? "") == 0)) {
240  return "";
241  }
242 
243  if (strlen($entered_value ?? "") == 0) {
244  return $this->lng->txt("text_question_not_filled_out");
245  }
246 
247  // see bug #22648
248  if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars()) {
249  return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
250  }
251 
252  return "";
253  }
254 
255  public function saveUserInput(
256  array $post_data,
257  int $active_id,
258  bool $a_return = false
259  ): ?array {
260  $ilDB = $this->db;
261 
262  $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
263  $maxchars = $this->getMaxChars();
264 
265  if ($maxchars > 0) {
266  $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
267  }
268 
269  if ($a_return) {
270  return array(array("value" => null, "textanswer" => $entered_value));
271  }
272  if (strlen($entered_value ?? "") == 0) {
273  return null;
274  }
275 
276  $next_id = $ilDB->nextId('svy_answer');
277  #20216
278  $fields = array();
279  $fields['answer_id'] = array("integer", $next_id);
280  $fields['question_fi'] = array("integer", $this->getId());
281  $fields['active_fi'] = array("integer", $active_id);
282  $fields['value'] = array("float", null);
283  $fields['textanswer'] = array("clob", (strlen($entered_value ?? "")) ? $entered_value : null);
284  $fields['tstamp'] = array("integer", time());
285 
286  $ilDB->insert("svy_answer", $fields);
287 
288  return null;
289  }
290 
291  public function importResponses(array $a_data): void
292  {
293  foreach ($a_data as $id => $data) {
294  if ($data["maxlength"] > 0) {
295  $this->setMaxChars($data["maxlength"]);
296  }
297  if ($data["rows"] > 0) {
298  $this->setTextHeight($data["rows"]);
299  }
300  if ($data["columns"] > 0) {
301  $this->setTextWidth($data["columns"]);
302  }
303  }
304  }
305 
306  public function usableForPrecondition(): bool
307  {
308  return false;
309  }
310 
311  public function getTextWidth(): ?int
312  {
313  return $this->textwidth;
314  }
315 
316  public function getTextHeight(): ?int
317  {
318  return $this->textheight;
319  }
320 
321  public function setTextWidth(?int $a_textwidth = null): void
322  {
323  if ($a_textwidth < 1) {
324  $this->textwidth = 50;
325  } else {
326  $this->textwidth = $a_textwidth;
327  }
328  }
329 
330  public function setTextHeight(?int $a_textheight = null): void
331  {
332  if ($a_textheight < 1) {
333  $this->textheight = 5;
334  } else {
335  $this->textheight = $a_textheight;
336  }
337  }
338 }
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...
setQuestiontext(string $questiontext="")
loadFromDb(int $question_id)
__construct(string $title="", string $description="", string $author="", string $questiontext="", int $owner=-1)
setTextHeight(?int $a_textheight=null)
const IL_INST_ID
Definition: constants.php:40
setObligatory(bool $obligatory=true)
setOriginalId(?int $original_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setComplete(bool $a_complete)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
toXML(bool $a_include_header=true, bool $obligatory_state=false)
stripSlashesAddSpaceFallback(string $a_str)
Strip slashes with add space fallback, see https://mantis.ilias.de/view.php?id=19727 and https://mant...
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
static strLen(string $a_string)
Definition: class.ilStr.php:63
__construct(VocabulariesInterface $vocabularies)
setAuthor(string $author="")
addMaterialTag(ilXmlWriter $a_xml_writer, string $a_material, bool $close_material_tag=true, bool $add_mobs=true, ?array $a_attrs=null)
Creates an XML material tag from a plain text or xhtml text.
checkUserInput(array $post_data, int $survey_id)
Checks the input of the active user for obligatory status and entered values.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insertXML(ilXmlWriter $a_xml_writer, $a_include_header=true)
setTitle(string $title="")
setTextWidth(?int $a_textwidth=null)
saveUserInput(array $post_data, int $active_id, bool $a_return=false)
saveToDb(int $original_id=0)
setOwner(int $owner=0)
getWorkingDataFromUserInput(array $post_data)
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
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)
setDescription(string $description="")
setObjId(int $obj_id=0)
Set the reference(?) id of the container object.