ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.SurveyTextQuestion.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  var $maxchars;
42 
53  $title = "",
54  $description = "",
55  $author = "",
56  $questiontext = "",
57  $owner = -1
58  )
59  {
61  $this->maxchars = 0;
62  $this->textwidth = 50;
63  $this->textheight = 5;
64  }
65 
74  {
75  global $ilDB;
76  $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",
77  array('integer'),
78  array($id)
79  );
80  if ($result->numRows() == 1)
81  {
82  return $ilDB->fetchAssoc($result);
83  }
84  else
85  {
86  return array();
87  }
88  }
89 
96  function loadFromDb($id)
97  {
98  global $ilDB;
99 
100  $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",
101  array('integer'),
102  array($id)
103  );
104  if ($result->numRows() == 1)
105  {
106  $data = $ilDB->fetchAssoc($result);
107  $this->setId($data["question_id"]);
108  $this->setTitle($data["title"]);
109  $this->label = $data['label'];
110  $this->setDescription($data["description"]);
111  $this->setObjId($data["obj_fi"]);
112  $this->setAuthor($data["author"]);
113  $this->setOwner($data["owner_fi"]);
114  include_once("./Services/RTE/classes/class.ilRTE.php");
115  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
116  $this->setObligatory($data["obligatory"]);
117  $this->setComplete($data["complete"]);
118  $this->setOriginalId($data["original_id"]);
119 
120  $this->setMaxChars($data["maxchars"]);
121  $this->setTextWidth($data["width"]);
122  $this->setTextHeight($data["height"]);
123 
124  }
125  parent::loadFromDb($id);
126  }
127 
134  function isComplete()
135  {
136  if (
137  strlen($this->getTitle()) &&
138  strlen($this->getAuthor()) &&
139  strlen($this->getQuestiontext())
140  )
141  {
142  return 1;
143  }
144  else
145  {
146  return 0;
147  }
148  }
149 
155  function setMaxChars($maxchars = 0)
156  {
157  $this->maxchars = $maxchars;
158  }
159 
165  function getMaxChars()
166  {
167  return ($this->maxchars) ? $this->maxchars : NULL;
168  }
169 
175  function saveToDb($original_id = "")
176  {
177  global $ilDB;
178 
179  $affectedRows = parent::saveToDb($original_id);
180  if ($affectedRows == 1)
181  {
182  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
183  array('integer'),
184  array($this->getId())
185  );
186  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
187  array('integer', 'integer', 'integer', 'integer'),
188  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
189  );
190 
191  $this->saveMaterial();
192  }
193  }
194 
201  function toXML($a_include_header = TRUE, $obligatory_state = "")
202  {
203  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
204  $a_xml_writer = new ilXmlWriter;
205  $a_xml_writer->xmlHeader();
206  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
207  $xml = $a_xml_writer->xmlDumpMem(FALSE);
208  if (!$a_include_header)
209  {
210  $pos = strpos($xml, "?>");
211  $xml = substr($xml, $pos + 2);
212  }
213  return $xml;
214  }
215 
224  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
225  {
226  $attrs = array(
227  "id" => $this->getId(),
228  "title" => $this->getTitle(),
229  "type" => $this->getQuestiontype(),
230  "obligatory" => $this->getObligatory()
231  );
232  $a_xml_writer->xmlStartTag("question", $attrs);
233 
234  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
235  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
236  if (strlen($this->label))
237  {
238  $attrs = array(
239  "label" => $this->label,
240  );
241  }
242  else
243  {
244  $attrs = array();
245  }
246  $a_xml_writer->xmlStartTag("questiontext", $attrs);
247  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
248  $a_xml_writer->xmlEndTag("questiontext");
249 
250  $a_xml_writer->xmlStartTag("responses");
251  $attrs = array(
252  "id" => "0",
253  "rows" => $this->getTextHeight(),
254  "columns" => $this->getTextWidth()
255  );
256  if ($this->getMaxChars() > 0)
257  {
258  $attrs["maxlength"] = $this->getMaxChars();
259  }
260  $a_xml_writer->xmlElement("response_text", $attrs);
261  $a_xml_writer->xmlEndTag("responses");
262 
263  if (count($this->material))
264  {
265  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
266  {
267  $attrs = array(
268  "label" => $this->material["title"]
269  );
270  $a_xml_writer->xmlStartTag("material", $attrs);
271  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
272  if (strcmp($matches[1], "") != 0)
273  {
274  $intlink = $this->material["internal_link"];
275  }
276  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
277  $a_xml_writer->xmlEndTag("material");
278  }
279  }
280 
281  $a_xml_writer->xmlEndTag("question");
282  }
283 
290  function _getMaxChars($question_id)
291  {
292  global $ilDB;
293  $result = $ilDB->queryF("SELECT maxchars FROM svy_question WHERE question_id = %s",
294  array('integer'),
295  array($question_id)
296  );
297  if ($result->numRows())
298  {
299  $row = $ilDB->fetchAssoc($result);
300  return $row["maxchars"];
301  }
302  return 0;
303  }
304 
311  function getQuestionType()
312  {
313  return "SurveyTextQuestion";
314  }
315 
323  {
324  return "svy_qst_text";
325  }
326 
333  function &getWorkingDataFromUserInput($post_data)
334  {
335  $entered_value = $post_data[$this->getId() . "_text_question"];
336  $data = array();
337  if (strlen($entered_value))
338  {
339  array_push($data, array("textanswer" => $entered_value));
340  }
341  return $data;
342  }
343 
353  function checkUserInput($post_data, $survey_id)
354  {
355  $entered_value = $post_data[$this->getId() . "_text_question"];
356 
357  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
358 
359  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
360 
361  return "";
362  }
363 
364  function randomText($length)
365  {
366  $random= "";
367  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
368  $char_list .= "abcdefghijklmnopqrstuvwxyz";
369  $char_list .= "1234567890";
370  for($i = 0; $i < $length; $i++)
371  {
372  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
373  if (!rand(0,5)) $random .= ' ';
374  }
375  return $random;
376  }
377 
383  public function saveRandomData($active_id)
384  {
385  global $ilDB;
386  // single response
387  $randomtext = $this->randomText(rand(25,100));
388  $next_id = $ilDB->nextId('svy_answer');
389  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
390  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
391  array($next_id, $this->getId(), $active_id, NULL, $randomtext, time())
392  );
393  }
394 
395  function saveUserInput($post_data, $active_id, $a_return = false)
396  {
397  global $ilDB;
398 
399  include_once "./Services/Utilities/classes/class.ilUtil.php";
400  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
401  $maxchars = $this->getMaxChars();
402  if ($maxchars > 0)
403  {
404  $entered_value = substr($entered_value, 0, $maxchars);
405  }
406 
407  if($a_return)
408  {
409  return array(array("value"=>null, "textanswer"=>$entered_value));
410  }
411  if (strlen($entered_value) == 0) return;
412 
413  $next_id = $ilDB->nextId('svy_answer');
414  #20216
415  $fields = array();
416  $fields['answer_id'] = array("integer", $next_id);
417  $fields['question_fi'] = array("integer", $this->getId());
418  $fields['active_fi'] = array("integer", $active_id);
419  $fields['value'] = array("float", NULL);
420  $fields['textanswer'] = array("clob", (strlen($entered_value)) ? $entered_value : NULL);
421  $fields['tstamp'] = array("integer", time());
422 
423  $affectedRows = $ilDB->insert("svy_answer", $fields);
424 
425  }
426 
427  function &getCumulatedResults($survey_id, $nr_of_users, $finished_ids)
428  {
429  global $ilDB;
430 
431  $question_id = $this->getId();
432 
433  $result_array = array();
434  $cumulated = array();
435  $textvalues = array();
436 
437  $sql = "SELECT svy_answer.* FROM svy_answer".
438  " JOIN svy_finished ON (svy_finished.finished_id = svy_answer.active_fi)".
439  " WHERE svy_answer.question_fi = ".$ilDB->quote($question_id, "integer").
440  " AND svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer");
441  if($finished_ids)
442  {
443  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
444  }
445 
446  $result = $ilDB->query($sql);
447  while ($row = $ilDB->fetchAssoc($result))
448  {
449  $cumulated[$row["value"]]++;
450  array_push($textvalues, $row["textanswer"]);
451  }
452  asort($cumulated, SORT_NUMERIC);
453  end($cumulated);
454  $numrows = $result->numRows();
455  $result_array["USERS_ANSWERED"] = $result->numRows();
456  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
457  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
458  $result_array["textvalues"] = $textvalues;
459  return $result_array;
460  }
461 
471  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
472  {
473  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
474  $worksheet =& $workbook->addWorksheet();
475  $rowcounter = 0;
476  switch ($export_label)
477  {
478  case 'label_only':
479  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
480  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->label));
481  break;
482  case 'title_only':
483  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
484  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
485  break;
486  default:
487  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
488  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
489  $rowcounter++;
490  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
491  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->label));
492  break;
493  }
494  $rowcounter++;
495  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
496  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
497  $rowcounter++;
498  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
499  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
500  $rowcounter++;
501  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
502  $worksheet->write($rowcounter, 1, $eval_data["USERS_ANSWERED"]);
503  $rowcounter++;
504  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
505  $worksheet->write($rowcounter, 1, $eval_data["USERS_SKIPPED"]);
506  $rowcounter++;
507 
508  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
509  $textvalues = "";
510  if (is_array($eval_data["textvalues"]))
511  {
512  foreach ($eval_data["textvalues"] as $textvalue)
513  {
514  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
515  }
516  }
517  }
518 
526  function addUserSpecificResultsData(&$a_array, &$resultset)
527  {
528  if (count($resultset["answers"][$this->getId()]))
529  {
530  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
531  {
532  array_push($a_array, $answer["textanswer"]);
533  }
534  }
535  else
536  {
537  array_push($a_array, $this->getSkippedValue());
538  }
539  }
540 
548  function &getUserAnswers($survey_id, $finished_ids)
549  {
550  global $ilDB;
551 
552  $answers = array();
553 
554  $sql = "SELECT svy_answer.* FROM svy_answer, svy_finished".
555  " WHERE svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer").
556  " AND svy_answer.question_fi = ".$ilDB->quote($this->getId(), "integer").
557  " AND svy_finished.finished_id = svy_answer.active_fi";
558  if($finished_ids)
559  {
560  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
561  }
562  $result = $ilDB->query($sql);
563  while ($row = $ilDB->fetchAssoc($result))
564  {
565  $answers[$row["active_fi"]] = $row["textanswer"];
566  }
567  return $answers;
568  }
569 
576  function importResponses($a_data)
577  {
578  foreach ($a_data as $id => $data)
579  {
580  if ($data["maxlength"] > 0)
581  {
582  $this->setMaxChars($data["maxlength"]);
583  }
584  if ($data["rows"] > 0)
585  {
586  $this->setTextHeight($data["rows"]);
587  }
588  if ($data["columns"] > 0)
589  {
590  $this->setTextWidth($data["columns"]);
591  }
592  }
593  }
594 
602  {
603  return FALSE;
604  }
605 
612  function getTextWidth()
613  {
614  return ($this->textwidth) ? $this->textwidth : NULL;
615  }
616 
623  function getTextHeight()
624  {
625  return ($this->textheight) ? $this->textheight : NULL;
626  }
627 
634  function setTextWidth($a_textwidth)
635  {
636  if ($a_textwidth < 1)
637  {
638  $this->textwidth = 50;
639  }
640  else
641  {
642  $this->textwidth = $a_textwidth;
643  }
644  }
645 
652  function setTextHeight($a_textheight)
653  {
654  if ($a_textheight < 1)
655  {
656  $this->textheight = 5;
657  }
658  else
659  {
660  $this->textheight = $a_textheight;
661  }
662  }
663 }
664 ?>
setTextHeight($a_textheight)
Sets the height of the answer field.
getTextWidth()
Returns the width of the answer field.
insertXML(&$a_xml_writer, $a_include_header=TRUE, $obligatory_state="")
Adds the question XML to a given XMLWriter object.
getAuthor()
Gets the authors name of the SurveyQuestion object.
$cumulated
An array containing the cumulated results of the question for a given survey.
getTitle()
Gets the title string of the SurveyQuestion object.
_getQuestionDataArray($id)
Returns the question data fields from the database.
$result
getObligatory($survey_id="")
Gets the obligatory state of the question.
setObligatory($obligatory=1)
Sets the obligatory state of the question.
toXML($a_include_header=TRUE, $obligatory_state="")
Returns an xml representation of the question.
_convert_text($a_text, $a_target="has been removed")
saveToDb($original_id="")
Saves a SurveyTextQuestion object to a database.
& getCumulatedResults($survey_id, $nr_of_users, $finished_ids)
setId($id=-1)
Sets the id of the SurveyQuestion object.
saveRandomData($active_id)
Saves random answers for a given active user in the database.
isComplete()
Returns true if the question is complete for use.
XML writer class.
getQuestiontext()
Gets the questiontext of the SurveyQuestion object.
getQuestionType()
Returns the question type of the question.
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.
SurveyTextQuestion( $title="", $description="", $author="", $questiontext="", $owner=-1)
The constructor takes possible arguments an creates an instance of the SurveyTextQuestion object...
setComplete($a_complete)
Sets the complete state of the question.
setMaxChars($maxchars=0)
Sets the maximum number of allowed characters for the text answer.
setOriginalId($original_id)
_getMaxChars($question_id)
Returns the maxium number of allowed characters for the text answer.
checkUserInput($post_data, $survey_id)
Checks the input of the active user for obligatory status and entered values.
SurveyQuestion( $title="", $description="", $author="", $questiontext="", $owner=-1)
SurveyQuestion constructor The constructor takes possible arguments an creates an instance of the Sur...
setQuestiontext($questiontext="")
Sets the questiontext of the SurveyQuestion object.
addUserSpecificResultsData(&$a_array, &$resultset)
Adds the values for the user specific results export for a given user.
$data
saveUserInput($post_data, $active_id, $a_return=false)
getId()
Gets the id of the SurveyQuestion object.
importResponses($a_data)
Import response data from the question import file.
Basic class for all survey question types.
saveMaterial()
save material to db
loadFromDb($id)
Loads a SurveyTextQuestion object from the database.
xmlHeader()
Writes xml header public.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
usableForPrecondition()
Returns if the question is usable for preconditions.
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.
setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
Creates an Excel worksheet for the detailed cumulated results of this question.
setDescription($description="")
Sets the description string of the SurveyQuestion object.
global $ilDB
& getWorkingDataFromUserInput($post_data)
Creates the user data of the svy_answer table from the POST data.
getAdditionalTableName()
Returns the name of the additional question data table in the database.
Text survey question.
getDescription()
Gets the description string of the SurveyQuestion object.
setObjId($obj_id=0)
Set the reference id of the container object.
getMaxChars()
Returns the maximum number of allowed characters for the text answer.
getTextHeight()
Returns the height of the answer field.
setTextWidth($a_textwidth)
Sets the width of the answer field.
& getUserAnswers($survey_id, $finished_ids)
Returns an array containing all answers to this question in a given survey.
setTitle($title="")
Sets the title string of the SurveyQuestion object.