ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.SurveyMetricQuestion.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 protected $db;
43
47
55 public $subtype;
56
62 public $minimum;
63
69 public $maximum;
70
82 public function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1, $subtype = self::SUBTYPE_NON_RATIO)
83 {
84 global $DIC;
85
86 $this->db = $DIC->database();
87 parent::__construct($title, $description, $author, $questiontext, $owner);
88
89 $this->subtype = $subtype;
90 $this->minimum = "";
91 $this->maximum = "";
92 }
93
101 public function setSubtype($subtype = self::SUBTYPE_NON_RATIO)
102 {
103 $this->subtype = $subtype;
104 }
105
113 public function setMinimum($minimum = 0)
114 {
115 if ($minimum !== null) {
116 $minimum = (float) $minimum;
117 }
118 if (!$minimum) {
119 $minimum = null;
120 }
121 $this->minimum = $minimum;
122 }
123
131 public function setMaximum($maximum = "")
132 {
133 if ($maximum !== null) {
134 $maximum = (float) $maximum;
135 }
136 if (!$maximum) {
137 $maximum = null;
138 }
139 $this->maximum = $maximum;
140 }
141
149 public function getSubtype()
150 {
151 return $this->subtype;
152 }
153
161 public function getMinimum()
162 {
163 if ((strlen($this->minimum) == 0) && ($this->getSubtype() > 3)) {
164 $this->minimum = 0;
165 }
166 return (strlen($this->minimum)) ? $this->minimum : null;
167 }
168
176 public function getMaximum()
177 {
178 return (strlen($this->maximum)) ? $this->maximum : null;
179 }
180
188 public function getQuestionDataArray($id)
189 {
191
192 $result = $ilDB->queryF(
193 "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",
194 array('integer'),
195 array($id)
196 );
197 if ($result->numRows() == 1) {
198 return $ilDB->fetchAssoc($result);
199 } else {
200 return array();
201 }
202 }
203
210 public function loadFromDb($id)
211 {
213
214 $result = $ilDB->queryF(
215 "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",
216 array('integer'),
217 array($id)
218 );
219 if ($result->numRows() == 1) {
220 $data = $ilDB->fetchAssoc($result);
221 $this->setId($data["question_id"]);
222 $this->setTitle($data["title"]);
223 $this->setDescription($data["description"]);
224 $this->setObjId($data["obj_fi"]);
225 $this->setAuthor($data["author"]);
226 $this->setOwner($data["owner_fi"]);
227 $this->label = $data['label'];
228 include_once("./Services/RTE/classes/class.ilRTE.php");
229 $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
230 $this->setObligatory($data["obligatory"]);
231 $this->setComplete($data["complete"]);
232 $this->setOriginalId($data["original_id"]);
233 $this->setSubtype($data["subtype"]);
234
235 $result = $ilDB->queryF(
236 "SELECT svy_variable.* FROM svy_variable WHERE svy_variable.question_fi = %s",
237 array('integer'),
238 array($id)
239 );
240 if ($result->numRows() > 0) {
241 if ($data = $ilDB->fetchAssoc($result)) {
242 $this->minimum = $data["value1"];
243 if (($data["value2"] < 0) or (strcmp($data["value2"], "") == 0)) {
244 $this->maximum = "";
245 } else {
246 $this->maximum = $data["value2"];
247 }
248 }
249 }
250 }
251 parent::loadFromDb($id);
252 }
253
260 public function isComplete()
261 {
262 if (
263 strlen($this->getTitle()) &&
264 strlen($this->getAuthor()) &&
265 strlen($this->getQuestiontext())
266 ) {
267 return 1;
268 } else {
269 return 0;
270 }
271 }
272
278 public function saveToDb($original_id = "")
279 {
281
282 $affectedRows = parent::saveToDb($original_id);
283 if ($affectedRows == 1) {
284 $affectedRows = $ilDB->manipulateF(
285 "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
286 array('integer'),
287 array($this->getId())
288 );
289 $affectedRows = $ilDB->manipulateF(
290 "INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, subtype) VALUES (%s, %s)",
291 array('integer', 'text'),
292 array($this->getId(), $this->getSubType())
293 );
294
295 // saving material uris in the database
296 $this->saveMaterial();
297
298 // save categories
299 $affectedRows = $ilDB->manipulateF(
300 "DELETE FROM svy_variable WHERE question_fi = %s",
301 array('integer'),
302 array($this->getId())
303 );
304
305 if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "&infin;") == 0)) {
306 $max = -1;
307 } else {
308 $max = $this->getMaximum();
309 }
310 $next_id = $ilDB->nextId('svy_variable');
311 $affectedRows = $ilDB->manipulateF(
312 "INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, value2, sequence, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
313 array('integer','integer','integer','float','float','integer','integer'),
314 array($next_id, 0, $this->getId(), $this->getMinimum(), $max, 0, time())
315 );
316 }
317 }
318
325 public function toXML($a_include_header = true, $obligatory_state = "")
326 {
327 include_once("./Services/Xml/classes/class.ilXmlWriter.php");
328 $a_xml_writer = new ilXmlWriter;
329 $a_xml_writer->xmlHeader();
330 $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
331 $xml = $a_xml_writer->xmlDumpMem(false);
332 if (!$a_include_header) {
333 $pos = strpos($xml, "?>");
334 $xml = substr($xml, $pos + 2);
335 }
336 return $xml;
337 }
338
346 public function insertXML(&$a_xml_writer, $a_include_header = true)
347 {
348 $attrs = array(
349 "id" => $this->getId(),
350 "title" => $this->getTitle(),
351 "type" => $this->getQuestiontype(),
352 "subtype" => $this->getSubtype(),
353 "obligatory" => $this->getObligatory()
354 );
355 $a_xml_writer->xmlStartTag("question", $attrs);
356
357 $a_xml_writer->xmlElement("description", null, $this->getDescription());
358 $a_xml_writer->xmlElement("author", null, $this->getAuthor());
359 $a_xml_writer->xmlStartTag("questiontext");
360 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
361 $a_xml_writer->xmlEndTag("questiontext");
362
363 $a_xml_writer->xmlStartTag("responses");
364 switch ($this->getSubtype()) {
365 case 3:
366 $attrs = array(
367 "id" => "0",
368 "format" => "double"
369 );
370 if (strlen($this->getMinimum())) {
371 $attrs["min"] = $this->getMinimum();
372 }
373 if (strlen($this->getMaximum())) {
374 $attrs["max"] = $this->getMaximum();
375 }
376 break;
377 case 4:
378 $attrs = array(
379 "id" => "0",
380 "format" => "double"
381 );
382 if (strlen($this->getMinimum())) {
383 $attrs["min"] = $this->getMinimum();
384 }
385 if (strlen($this->getMaximum())) {
386 $attrs["max"] = $this->getMaximum();
387 }
388 break;
389 case 5:
390 $attrs = array(
391 "id" => "0",
392 "format" => "integer"
393 );
394 if (strlen($this->getMinimum())) {
395 $attrs["min"] = $this->getMinimum();
396 }
397 if (strlen($this->getMaximum())) {
398 $attrs["max"] = $this->getMaximum();
399 }
400 break;
401 }
402 $a_xml_writer->xmlStartTag("response_num", $attrs);
403 $a_xml_writer->xmlEndTag("response_num");
404
405 $a_xml_writer->xmlEndTag("responses");
406
407 if (count($this->material)) {
408 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches)) {
409 $attrs = array(
410 "label" => $this->material["title"]
411 );
412 $a_xml_writer->xmlStartTag("material", $attrs);
413 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
414 if (strcmp($matches[1], "") != 0) {
415 $intlink = $this->material["internal_link"];
416 }
417 $a_xml_writer->xmlElement("mattext", null, $intlink);
418 $a_xml_writer->xmlEndTag("material");
419 }
420 }
421
422 $a_xml_writer->xmlEndTag("question");
423 }
424
431 public function getQuestionTypeID()
432 {
434 $result = $ilDB->queryF(
435 "SELECT questiontype_id FROM svy_qtype WHERE type_tag = %s",
436 array('text'),
437 array($this->getQuestionType())
438 );
439 $row = $ilDB->fetchAssoc($result);
440 return $row["questiontype_id"];
441 }
442
449 public function getQuestionType()
450 {
451 return "SurveyMetricQuestion";
452 }
453
460 public function getAdditionalTableName()
461 {
462 return "svy_qst_metric";
463 }
464
471 public function &getWorkingDataFromUserInput($post_data)
472 {
473 $entered_value = $post_data[$this->getId() . "_metric_question"];
474 $data = array();
475 if (strlen($entered_value)) {
476 array_push($data, array("value" => $entered_value));
477 }
478 return $data;
479 }
480
490 public function checkUserInput($post_data, $survey_id)
491 {
492 $entered_value = $post_data[$this->getId() . "_metric_question"];
493 // replace german notation with international notation
494 $entered_value = str_replace(",", ".", $entered_value);
495
496 if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) {
497 return "";
498 }
499
500 if (strlen($entered_value) == 0) {
501 return $this->lng->txt("survey_question_obligatory");
502 }
503
504 if (strlen($this->getMinimum())) {
505 if ($entered_value < $this->getMinimum()) {
506 return $this->lng->txt("metric_question_out_of_bounds");
507 }
508 }
509
510 if (strlen($this->getMaximum())) {
511 if (($this->getMaximum() == 1) && ($this->getMaximum() < $this->getMinimum())) {
512 // old &infty; values as maximum
513 } else {
514 if ($entered_value > $this->getMaximum()) {
515 return $this->lng->txt("metric_question_out_of_bounds");
516 }
517 }
518 }
519
520 if (!is_numeric($entered_value)) {
521 return $this->lng->txt("metric_question_not_a_value");
522 }
523
524 if (($this->getSubType() == self::SUBTYPE_RATIO_ABSOLUTE) && (intval($entered_value) != doubleval($entered_value))) {
525 return $this->lng->txt("metric_question_floating_point");
526 }
527 return "";
528 }
529
530 public function saveUserInput($post_data, $active_id, $a_return = false)
531 {
533
534 $entered_value = $post_data[$this->getId() . "_metric_question"];
535
536 // replace german notation with international notation
537 $entered_value = str_replace(",", ".", $entered_value);
538
539 if ($a_return) {
540 return array(array("value"=>$entered_value, "textanswer"=>null));
541 }
542 if (strlen($entered_value) == 0) {
543 return;
544 }
545
546 $next_id = $ilDB->nextId('svy_answer');
547 #20216
548 $fields = array();
549 $fields['answer_id'] = array("integer", $next_id);
550 $fields['question_fi'] = array("integer", $this->getId());
551 $fields['active_fi'] = array("integer", $active_id);
552 $fields['value'] = array("float", (strlen($entered_value)) ? $entered_value : null);
553 $fields['textanswer'] = array("clob", null);
554 $fields['tstamp'] = array("integer", time());
555
556 $affectedRows = $ilDB->insert("svy_answer", $fields);
557 }
558
565 public function importResponses($a_data)
566 {
567 foreach ($a_data as $id => $data) {
568 $this->setMinimum($data["min"]);
569 $this->setMaximum($data["max"]);
570 }
571 }
572
579 public function usableForPrecondition()
580 {
581 return true;
582 }
583
590 public function getAvailableRelations()
591 {
592 return array("<", "<=", "=", "<>", ">=", ">");
593 }
594
602 {
603 $template->setCurrentBlock("textfield");
604 $template->setVariable("TEXTFIELD_VALUE", "");
605 $template->parseCurrentBlock();
606 }
607
614 public function getPreconditionSelectValue($default = "", $title, $variable)
615 {
616 include_once "./Services/Form/classes/class.ilNumberInputGUI.php";
617 $step3 = new ilNumberInputGUI($title, $variable);
618 $step3->setValue($default);
619 return $step3;
620 }
621
628 public function getMinMaxText()
629 {
630 $min = $this->getMinimum();
631 $max = $this->getMaximum();
632 if (strlen($min) && strlen($max)) {
633 return "(" . $min . " " . strtolower($this->lng->txt("to")) . " " . $max . ")";
634 } elseif (strlen($min)) {
635 return "(&gt;= " . $min . ")";
636 } elseif (strlen($max)) {
637 return "(&lt;= " . $max . ")";
638 } else {
639 return "";
640 }
641 }
642}
$result
An exception for terminatinating execution or to throw for unit testing.
getQuestionType()
Returns the question type of the question.
saveToDb($original_id="")
Saves a SurveyMetricQuestion object to a database.
getQuestionTypeID()
Returns the question type ID of the question.
getQuestionDataArray($id)
Returns the question data fields from the database.
getSubtype()
Gets the question subtype.
setMaximum($maximum="")
Sets the maximum value.
outPreconditionSelectValue(&$template)
Creates a value selection for preconditions.
getPreconditionSelectValue($default="", $title, $variable)
Creates a form property for the precondition value.
getAvailableRelations()
Returns the available relations for the question.
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
getMinMaxText()
Creates a text for the input range of the metric question.
__construct($title="", $description="", $author="", $questiontext="", $owner=-1, $subtype=self::SUBTYPE_NON_RATIO)
SurveyMetricQuestion constructor.
getMinimum()
Returns the minimum value of the question.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
saveUserInput($post_data, $active_id, $a_return=false)
setSubtype($subtype=self::SUBTYPE_NON_RATIO)
Sets the question subtype.
isComplete()
Returns true if the question is complete for use.
getMaximum()
Returns the maximum value of the question.
loadFromDb($id)
Loads a SurveyMetricQuestion object from the database.
insertXML(&$a_xml_writer, $a_include_header=true)
Adds the question XML to a given XMLWriter object.
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
toXML($a_include_header=true, $obligatory_state="")
Returns an xml representation of the question.
importResponses($a_data)
Import response data from the question import file.
setMinimum($minimum=0)
Sets the minimum value.
usableForPrecondition()
Returns if the question is usable for preconditions.
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.
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.
This class represents a number property in a property form.
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...
XML writer class.
xmlHeader()
Writes xml header @access public.
$template
$xml
Definition: metadata.php:240
global $DIC
Definition: saml.php:7
global $ilDB