ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
24 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
25 
38 {
39  const SUBTYPE_NON_RATIO = 3;
42 
50  var $subtype;
51 
57  var $minimum;
58 
64  var $maximum;
65 
77  function __construct($title = "", $description = "", $author = "", $questiontext = "", $owner = -1, $subtype = self::SUBTYPE_NON_RATIO)
78  {
79  parent::__construct($title, $description, $author, $questiontext, $owner);
80 
81  $this->subtype = $subtype;
82  $this->minimum = "";
83  $this->maximum = "";
84  }
85 
93  function setSubtype($subtype = self::SUBTYPE_NON_RATIO)
94  {
95  $this->subtype = $subtype;
96  }
97 
105  function setMinimum($minimum = 0)
106  {
107  if($minimum !== NULL)
108  {
109  $minimum = (float)$minimum;
110  }
111  if(!$minimum)
112  {
113  $minimum = NULL;
114  }
115  $this->minimum = $minimum;
116  }
117 
125  function setMaximum($maximum = "")
126  {
127  if($maximum !== NULL)
128  {
129  $maximum = (float)$maximum;
130  }
131  if(!$maximum)
132  {
133  $maximum = NULL;
134  }
135  $this->maximum = $maximum;
136  }
137 
145  function getSubtype()
146  {
147  return $this->subtype;
148  }
149 
157  function getMinimum()
158  {
159  if ((strlen($this->minimum) == 0) && ($this->getSubtype() > 3))
160  {
161  $this->minimum = 0;
162  }
163  return (strlen($this->minimum)) ? $this->minimum : NULL;
164  }
165 
173  function getMaximum()
174  {
175  return (strlen($this->maximum)) ? $this->maximum : NULL;
176  }
177 
186  {
187  global $ilDB;
188 
189  $result = $ilDB->queryF("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",
190  array('integer'),
191  array($id)
192  );
193  if ($result->numRows() == 1)
194  {
195  return $ilDB->fetchAssoc($result);
196  }
197  else
198  {
199  return array();
200  }
201  }
202 
209  function loadFromDb($id)
210  {
211  global $ilDB;
212 
213  $result = $ilDB->queryF("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",
214  array('integer'),
215  array($id)
216  );
217  if ($result->numRows() == 1)
218  {
219  $data = $ilDB->fetchAssoc($result);
220  $this->setId($data["question_id"]);
221  $this->setTitle($data["title"]);
222  $this->setDescription($data["description"]);
223  $this->setObjId($data["obj_fi"]);
224  $this->setAuthor($data["author"]);
225  $this->setOwner($data["owner_fi"]);
226  $this->label = $data['label'];
227  include_once("./Services/RTE/classes/class.ilRTE.php");
228  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
229  $this->setObligatory($data["obligatory"]);
230  $this->setComplete($data["complete"]);
231  $this->setOriginalId($data["original_id"]);
232  $this->setSubtype($data["subtype"]);
233 
234  $result = $ilDB->queryF("SELECT svy_variable.* FROM svy_variable WHERE svy_variable.question_fi = %s",
235  array('integer'),
236  array($id)
237  );
238  if ($result->numRows() > 0)
239  {
240  if ($data = $ilDB->fetchAssoc($result))
241  {
242  $this->minimum = $data["value1"];
243  if (($data["value2"] < 0) or (strcmp($data["value2"], "") == 0))
244  {
245  $this->maximum = "";
246  }
247  else
248  {
249  $this->maximum = $data["value2"];
250  }
251  }
252  }
253  }
254  parent::loadFromDb($id);
255  }
256 
263  function isComplete()
264  {
265  if (
266  strlen($this->getTitle()) &&
267  strlen($this->getAuthor()) &&
268  strlen($this->getQuestiontext())
269  )
270  {
271  return 1;
272  }
273  else
274  {
275  return 0;
276  }
277  }
278 
284  function saveToDb($original_id = "")
285  {
286  global $ilDB;
287 
288  $affectedRows = parent::saveToDb($original_id);
289  if ($affectedRows == 1)
290  {
291  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
292  array('integer'),
293  array($this->getId())
294  );
295  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, subtype) VALUES (%s, %s)",
296  array('integer', 'text'),
297  array($this->getId(), $this->getSubType())
298  );
299 
300  // saving material uris in the database
301  $this->saveMaterial();
302 
303  // save categories
304  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_variable WHERE question_fi = %s",
305  array('integer'),
306  array($this->getId())
307  );
308 
309  if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "&infin;") == 0))
310  {
311  $max = -1;
312  }
313  else
314  {
315  $max = $this->getMaximum();
316  }
317  $next_id = $ilDB->nextId('svy_variable');
318  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_variable (variable_id, category_fi, question_fi, value1, value2, sequence, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
319  array('integer','integer','integer','float','float','integer','integer'),
320  array($next_id, 0, $this->getId(), $this->getMinimum(), $max, 0, time())
321  );
322  }
323  }
324 
331  function toXML($a_include_header = TRUE, $obligatory_state = "")
332  {
333  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
334  $a_xml_writer = new ilXmlWriter;
335  $a_xml_writer->xmlHeader();
336  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
337  $xml = $a_xml_writer->xmlDumpMem(FALSE);
338  if (!$a_include_header)
339  {
340  $pos = strpos($xml, "?>");
341  $xml = substr($xml, $pos + 2);
342  }
343  return $xml;
344  }
345 
353  function insertXML(&$a_xml_writer, $a_include_header = TRUE)
354  {
355  $attrs = array(
356  "id" => $this->getId(),
357  "title" => $this->getTitle(),
358  "type" => $this->getQuestiontype(),
359  "subtype" => $this->getSubtype(),
360  "obligatory" => $this->getObligatory()
361  );
362  $a_xml_writer->xmlStartTag("question", $attrs);
363 
364  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
365  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
366  $a_xml_writer->xmlStartTag("questiontext");
367  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
368  $a_xml_writer->xmlEndTag("questiontext");
369 
370  $a_xml_writer->xmlStartTag("responses");
371  switch ($this->getSubtype())
372  {
373  case 3:
374  $attrs = array(
375  "id" => "0",
376  "format" => "double"
377  );
378  if (strlen($this->getMinimum()))
379  {
380  $attrs["min"] = $this->getMinimum();
381  }
382  if (strlen($this->getMaximum()))
383  {
384  $attrs["max"] = $this->getMaximum();
385  }
386  break;
387  case 4:
388  $attrs = array(
389  "id" => "0",
390  "format" => "double"
391  );
392  if (strlen($this->getMinimum()))
393  {
394  $attrs["min"] = $this->getMinimum();
395  }
396  if (strlen($this->getMaximum()))
397  {
398  $attrs["max"] = $this->getMaximum();
399  }
400  break;
401  case 5:
402  $attrs = array(
403  "id" => "0",
404  "format" => "integer"
405  );
406  if (strlen($this->getMinimum()))
407  {
408  $attrs["min"] = $this->getMinimum();
409  }
410  if (strlen($this->getMaximum()))
411  {
412  $attrs["max"] = $this->getMaximum();
413  }
414  break;
415  }
416  $a_xml_writer->xmlStartTag("response_num", $attrs);
417  $a_xml_writer->xmlEndTag("response_num");
418 
419  $a_xml_writer->xmlEndTag("responses");
420 
421  if (count($this->material))
422  {
423  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
424  {
425  $attrs = array(
426  "label" => $this->material["title"]
427  );
428  $a_xml_writer->xmlStartTag("material", $attrs);
429  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
430  if (strcmp($matches[1], "") != 0)
431  {
432  $intlink = $this->material["internal_link"];
433  }
434  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
435  $a_xml_writer->xmlEndTag("material");
436  }
437  }
438 
439  $a_xml_writer->xmlEndTag("question");
440  }
441 
448  function getQuestionTypeID()
449  {
450  global $ilDB;
451  $result = $ilDB->queryF("SELECT questiontype_id FROM svy_qtype WHERE type_tag = %s",
452  array('text'),
453  array($this->getQuestionType())
454  );
455  $row = $ilDB->fetchAssoc($result);
456  return $row["questiontype_id"];
457  }
458 
465  function getQuestionType()
466  {
467  return "SurveyMetricQuestion";
468  }
469 
477  {
478  return "svy_qst_metric";
479  }
480 
487  function &getWorkingDataFromUserInput($post_data)
488  {
489  $entered_value = $post_data[$this->getId() . "_metric_question"];
490  $data = array();
491  if (strlen($entered_value))
492  {
493  array_push($data, array("value" => $entered_value));
494  }
495  return $data;
496  }
497 
507  function checkUserInput($post_data, $survey_id)
508  {
509  $entered_value = $post_data[$this->getId() . "_metric_question"];
510  // replace german notation with international notation
511  $entered_value = str_replace(",", ".", $entered_value);
512 
513  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
514 
515  if (strlen($entered_value) == 0) return $this->lng->txt("survey_question_obligatory");
516 
517  if (strlen($this->getMinimum()))
518  {
519  if ($entered_value < $this->getMinimum())
520  {
521  return $this->lng->txt("metric_question_out_of_bounds");
522  }
523  }
524 
525  if (strlen($this->getMaximum()))
526  {
527  if (($this->getMaximum() == 1) && ($this->getMaximum() < $this->getMinimum()))
528  {
529  // old &infty; values as maximum
530  }
531  else
532  {
533  if ($entered_value > $this->getMaximum())
534  {
535  return $this->lng->txt("metric_question_out_of_bounds");
536  }
537  }
538  }
539 
540  if (!is_numeric($entered_value))
541  {
542  return $this->lng->txt("metric_question_not_a_value");
543  }
544 
545  if (($this->getSubType() == self::SUBTYPE_RATIO_ABSOLUTE) && (intval($entered_value) != doubleval($entered_value)))
546  {
547  return $this->lng->txt("metric_question_floating_point");
548  }
549  return "";
550  }
551 
552  function saveUserInput($post_data, $active_id, $a_return = false)
553  {
554  global $ilDB;
555 
556  $entered_value = $post_data[$this->getId() . "_metric_question"];
557 
558  // replace german notation with international notation
559  $entered_value = str_replace(",", ".", $entered_value);
560 
561  if($a_return)
562  {
563  return array(array("value"=>$entered_value, "textanswer"=>null));
564  }
565  if (strlen($entered_value) == 0) return;
566 
567  $next_id = $ilDB->nextId('svy_answer');
568  #20216
569  $fields = array();
570  $fields['answer_id'] = array("integer", $next_id);
571  $fields['question_fi'] = array("integer", $this->getId());
572  $fields['active_fi'] = array("integer", $active_id);
573  $fields['value'] = array("float", (strlen($entered_value)) ? $entered_value : NULL);
574  $fields['textanswer'] = array("clob", NULL);
575  $fields['tstamp'] = array("integer", time());
576 
577  $affectedRows = $ilDB->insert("svy_answer", $fields);
578  }
579 
586  function importResponses($a_data)
587  {
588  foreach ($a_data as $id => $data)
589  {
590  $this->setMinimum($data["min"]);
591  $this->setMaximum($data["max"]);
592  }
593  }
594 
602  {
603  return TRUE;
604  }
605 
613  {
614  return array("<", "<=", "=", "<>", ">=", ">");
615  }
616 
623  function outPreconditionSelectValue(&$template)
624  {
625  $template->setCurrentBlock("textfield");
626  $template->setVariable("TEXTFIELD_VALUE", "");
627  $template->parseCurrentBlock();
628  }
629 
636  public function getPreconditionSelectValue($default = "", $title, $variable)
637  {
638  include_once "./Services/Form/classes/class.ilNumberInputGUI.php";
639  $step3 = new ilNumberInputGUI($title, $variable);
640  $step3->setValue($default);
641  return $step3;
642  }
643 
650  function getMinMaxText()
651  {
652  $min = $this->getMinimum();
653  $max = $this->getMaximum();
654  if (strlen($min) && strlen($max))
655  {
656  return "(" . $min . " " . strtolower($this->lng->txt("to")) . " " . $max . ")";
657  }
658  else if (strlen($min))
659  {
660  return "(&gt;= " . $min . ")";
661  }
662  else if (strlen($max))
663  {
664  return "(&lt;= " . $max . ")";
665  }
666  else
667  {
668  return "";
669  }
670  }
671 }
672 ?>
getQuestionTypeID()
Returns the question type ID of the question.
outPreconditionSelectValue(&$template)
Creates a value selection for preconditions.
getAuthor()
Gets the authors name of the SurveyQuestion object.
getTitle()
Gets the title string of the SurveyQuestion object.
$result
getObligatory($survey_id="")
Gets the obligatory state of the question.
setSubtype($subtype=self::SUBTYPE_NON_RATIO)
Sets the question subtype.
getMaximum()
Returns the maximum value of the question.
setObligatory($obligatory=1)
Sets the obligatory state of the question.
setId($id=-1)
Sets the id of the SurveyQuestion object.
getAvailableRelations()
Returns the available relations for the question.
XML writer class.
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getSubtype()
Gets the question subtype.
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...
setOwner($owner="")
Sets the creator/owner ID of the SurveyQuestion object.
isComplete()
Returns true if the question is complete for use.
setComplete($a_complete)
Sets the complete state of the question.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
setMinimum($minimum=0)
Sets the minimum value.
loadFromDb($id)
Loads a SurveyMetricQuestion object from the database.
setOriginalId($original_id)
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
getId()
Gets the id of the SurveyQuestion object.
getMinimum()
Returns the minimum value of the question.
saveToDb($original_id="")
Saves a SurveyMetricQuestion object to a database.
This class represents a number property in a property form.
importResponses($a_data)
Import response data from the question import file.
Basic class for all survey question types.
saveMaterial()
save material to db
xmlHeader()
Writes xml header public.
toXML($a_include_header=TRUE, $obligatory_state="")
Returns an xml representation of the question.
usableForPrecondition()
Returns if the question is usable for preconditions.
getQuestionType()
Returns the question type of the question.
Create styles array
The data for the language used.
saveUserInput($post_data, $active_id, $a_return=false)
setAuthor($author="")
Sets the authors name 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.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
global $ilDB
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.
getMinMaxText()
Creates a text for the input range of the metric question.
getPreconditionSelectValue($default="", $title, $variable)
Creates a form property for the precondition value.
__construct($title="", $description="", $author="", $questiontext="", $owner=-1, $subtype=self::SUBTYPE_NON_RATIO)
SurveyMetricQuestion constructor.
getDescription()
Gets the description string of the SurveyQuestion object.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
setObjId($obj_id=0)
Set the reference id of the container object.
Metric survey question.
getQuestionDataArray($id)
Returns the question data fields from the database.
setMaximum($maximum="")
Sets the maximum value.
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
setTitle($title="")
Sets the title string of the SurveyQuestion object.