ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.SurveyTextQuestion.php
Go to the documentation of this file.
1 <?php
2 
28 {
29  protected ?int $maxchars = null;
30 
31  public function __construct(
32  string $title = "",
33  string $description = "",
34  string $author = "",
35  string $questiontext = "",
36  int $owner = -1
37  ) {
38  global $DIC;
39 
40  $this->db = $DIC->database();
42 
43  $this->maxchars = 0;
44  }
45 
46  public function getQuestionDataArray(int $id): array
47  {
48  $ilDB = $this->db;
49  $result = $ilDB->queryF(
50  "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",
51  array('integer'),
52  array($id)
53  );
54  if ($result->numRows() === 1) {
55  return $ilDB->fetchAssoc($result);
56  } else {
57  return array();
58  }
59  }
60 
61  public function loadFromDb(int $question_id): void
62  {
63  $ilDB = $this->db;
64 
65  $result = $ilDB->queryF(
66  "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",
67  array('integer'),
68  array($question_id)
69  );
70  if ($result->numRows() === 1) {
71  $data = $ilDB->fetchAssoc($result);
72  $this->setId((int) $data["question_id"]);
73  $this->setTitle((string) $data["title"]);
74  $this->label = (string) $data['label'];
75  $this->setDescription((string) $data["description"]);
76  $this->setObjId((int) $data["obj_fi"]);
77  $this->setAuthor((string) $data["author"]);
78  $this->setOwner((int) $data["owner_fi"]);
79  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc((string) $data["questiontext"], 1));
80  $this->setObligatory((bool) $data["obligatory"]);
81  $this->setComplete((bool) $data["complete"]);
82  $this->setOriginalId((int) $data["original_id"]);
83 
84  $this->setMaxChars((int) $data["maxchars"]);
85  }
86  parent::loadFromDb($question_id);
87  }
88 
89  public function isComplete(): bool
90  {
91  if (
92  strlen($this->getTitle() ?? "") &&
93  strlen($this->getAuthor() ?? "") &&
94  strlen($this->getQuestiontext() ?? "")
95  ) {
96  return true;
97  } else {
98  return false;
99  }
100  }
101 
102  public function setMaxChars(int $maxchars = 0): void
103  {
104  $this->maxchars = $maxchars;
105  }
106 
107  public function getMaxChars(): int
108  {
109  return $this->maxchars;
110  }
111 
112  public function saveToDb(int $original_id = 0): int
113  {
114  $ilDB = $this->db;
115 
116  $affectedRows = parent::saveToDb($original_id);
117  if ($affectedRows === 1) {
118  $ilDB->manipulateF(
119  "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
120  array('integer'),
121  array($this->getId())
122  );
123  $ilDB->manipulateF(
124  "INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars) VALUES (%s, %s)",
125  array('integer', 'integer'),
126  array($this->getId(), $this->getMaxChars())
127  );
128  }
129  return $affectedRows;
130  }
131 
132  public function toXML(
133  bool $a_include_header = true,
134  bool $obligatory_state = false
135  ): string {
136  $a_xml_writer = new ilXmlWriter();
137  $a_xml_writer->xmlHeader();
138  $this->insertXML($a_xml_writer, $a_include_header);
139  $xml = $a_xml_writer->xmlDumpMem(false);
140  if (!$a_include_header) {
141  $pos = strpos($xml, "?>");
142  $xml = substr($xml, $pos + 2);
143  }
144  return $xml;
145  }
146 
147  public function insertXML(
148  ilXmlWriter $a_xml_writer,
149  $a_include_header = true
150  ): void {
151  $attrs = array(
152  "id" => $this->getId(),
153  "title" => $this->getTitle(),
154  "type" => $this->getQuestionType(),
155  "obligatory" => $this->getObligatory()
156  );
157  $a_xml_writer->xmlStartTag("question", $attrs);
158 
159  $a_xml_writer->xmlElement("description", null, $this->getDescription());
160  $a_xml_writer->xmlElement("author", null, $this->getAuthor());
161  if (strlen($this->label ?? "")) {
162  $attrs = array(
163  "label" => $this->label,
164  );
165  } else {
166  $attrs = array();
167  }
168  $a_xml_writer->xmlStartTag("questiontext", $attrs);
169  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
170  $a_xml_writer->xmlEndTag("questiontext");
171 
172  $a_xml_writer->xmlStartTag("responses");
173  $attrs = array(
174  "id" => "0"
175  );
176  if ($this->getMaxChars() > 0) {
177  $attrs["maxlength"] = $this->getMaxChars();
178  }
179  $a_xml_writer->xmlElement("response_text", $attrs);
180  $a_xml_writer->xmlEndTag("responses");
181 
182  if (count($this->material)) {
183  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
184  $attrs = array(
185  "label" => $this->material["title"]
186  );
187  $a_xml_writer->xmlStartTag("material", $attrs);
188  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
189  if (strcmp($matches[1], "") != 0) {
190  $intlink = $this->material["internal_link"];
191  }
192  $a_xml_writer->xmlElement("mattext", null, $intlink);
193  $a_xml_writer->xmlEndTag("material");
194  }
195  }
196 
197  $a_xml_writer->xmlEndTag("question");
198  }
199 
200  public function getQuestionType(): string
201  {
202  return "SurveyTextQuestion";
203  }
204 
205  public function getAdditionalTableName(): string
206  {
207  return "svy_qst_text";
208  }
209 
211  array $post_data
212  ): array {
213  $entered_value = $post_data[$this->getId() . "_text_question"] ?? "";
214  $data = array();
215  if (strlen($entered_value ?? "")) {
216  $data[] = array("textanswer" => $entered_value);
217  }
218  return $data;
219  }
220 
225  public function checkUserInput(
226  array $post_data,
227  int $survey_id
228  ): string {
229  $entered_value = $post_data[$this->getId() . "_text_question"];
230 
231  if ((!$this->getObligatory()) && (strlen($entered_value ?? "") == 0)) {
232  return "";
233  }
234 
235  if (strlen($entered_value ?? "") == 0) {
236  return $this->lng->txt("text_question_not_filled_out");
237  }
238 
239  // see bug #22648
240  if ($this->getMaxChars() > 0 && ilStr::strLen($entered_value) > $this->getMaxChars()) {
241  return str_replace("%s", ilStr::strLen($entered_value), $this->lng->txt("svy_answer_too_long"));
242  }
243 
244  return "";
245  }
246 
247  public function saveUserInput(
248  array $post_data,
249  int $active_id,
250  bool $a_return = false
251  ): ?array {
252  $ilDB = $this->db;
253 
254  $entered_value = $this->stripSlashesAddSpaceFallback($post_data[$this->getId() . "_text_question"]);
255  $maxchars = $this->getMaxChars();
256 
257  if ($maxchars > 0) {
258  $entered_value = ilStr::subStr($entered_value, 0, $maxchars);
259  }
260 
261  if ($a_return) {
262  return array(array("value" => null, "textanswer" => $entered_value));
263  }
264  if (strlen($entered_value ?? "") == 0) {
265  return null;
266  }
267 
268  $next_id = $ilDB->nextId('svy_answer');
269  #20216
270  $fields = array();
271  $fields['answer_id'] = array("integer", $next_id);
272  $fields['question_fi'] = array("integer", $this->getId());
273  $fields['active_fi'] = array("integer", $active_id);
274  $fields['value'] = array("float", null);
275  $fields['textanswer'] = array("clob", (strlen($entered_value ?? "")) ? $entered_value : null);
276  $fields['tstamp'] = array("integer", time());
277 
278  $ilDB->insert("svy_answer", $fields);
279 
280  return null;
281  }
282 
283  public function importResponses(array $a_data): void
284  {
285  foreach ($a_data as $id => $data) {
286  if ($data["maxlength"] > 0) {
287  $this->setMaxChars($data["maxlength"]);
288  }
289  }
290  }
291 
292  public function usableForPrecondition(): bool
293  {
294  return false;
295  }
296 }
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)
const IL_INST_ID
Definition: constants.php:40
setObligatory(bool $obligatory=true)
setOriginalId(?int $original_id)
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static strLen(string $a_string)
Definition: class.ilStr.php:63
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...
global $DIC
Definition: shib_login.php:22
insertXML(ilXmlWriter $a_xml_writer, $a_include_header=true)
setTitle(string $title="")
saveUserInput(array $post_data, int $active_id, bool $a_return=false)
saveToDb(int $original_id=0)
setOwner(int $owner=0)
__construct(Container $dic, ilPlugin $plugin)
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.