ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
26 
39 {
40  var $maxchars;
43 
54  $title = "",
55  $description = "",
56  $author = "",
57  $questiontext = "",
58  $owner = -1
59  )
60  {
62  $this->maxchars = 0;
63  $this->textwidth = 50;
64  $this->textheight = 5;
65  }
66 
75  {
76  global $ilDB;
77  $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",
78  array('integer'),
79  array($id)
80  );
81  if ($result->numRows() == 1)
82  {
83  return $ilDB->fetchAssoc($result);
84  }
85  else
86  {
87  return array();
88  }
89  }
90 
97  function loadFromDb($id)
98  {
99  global $ilDB;
100 
101  $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",
102  array('integer'),
103  array($id)
104  );
105  if ($result->numRows() == 1)
106  {
107  $data = $ilDB->fetchAssoc($result);
108  $this->setId($data["question_id"]);
109  $this->setTitle($data["title"]);
110  $this->label = $data['label'];
111  $this->setDescription($data["description"]);
112  $this->setObjId($data["obj_fi"]);
113  $this->setAuthor($data["author"]);
114  $this->setOwner($data["owner_fi"]);
115  include_once("./Services/RTE/classes/class.ilRTE.php");
116  $this->setQuestiontext(ilRTE::_replaceMediaObjectImageSrc($data["questiontext"], 1));
117  $this->setObligatory($data["obligatory"]);
118  $this->setComplete($data["complete"]);
119  $this->setOriginalId($data["original_id"]);
120 
121  $this->setMaxChars($data["maxchars"]);
122  $this->setTextWidth($data["width"]);
123  $this->setTextHeight($data["height"]);
124 
125  }
127  }
128 
135  function isComplete()
136  {
137  if (
138  strlen($this->getTitle()) &&
139  strlen($this->getAuthor()) &&
140  strlen($this->getQuestiontext())
141  )
142  {
143  return 1;
144  }
145  else
146  {
147  return 0;
148  }
149  }
150 
156  function setMaxChars($maxchars = 0)
157  {
158  $this->maxchars = $maxchars;
159  }
160 
166  function getMaxChars()
167  {
168  return ($this->maxchars) ? $this->maxchars : NULL;
169  }
170 
176  function saveToDb($original_id = "")
177  {
178  global $ilDB;
179 
180  $affectedRows = parent::saveToDb($original_id);
181  if ($affectedRows == 1)
182  {
183  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
184  array('integer'),
185  array($this->getId())
186  );
187  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
188  array('integer', 'integer', 'integer', 'integer'),
189  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
190  );
191 
192  $this->saveMaterial();
193  }
194  }
195 
202  function toXML($a_include_header = TRUE, $obligatory_state = "")
203  {
204  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
205  $a_xml_writer = new ilXmlWriter;
206  $a_xml_writer->xmlHeader();
207  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
208  $xml = $a_xml_writer->xmlDumpMem(FALSE);
209  if (!$a_include_header)
210  {
211  $pos = strpos($xml, "?>");
212  $xml = substr($xml, $pos + 2);
213  }
214  return $xml;
215  }
216 
225  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
226  {
227  $attrs = array(
228  "id" => $this->getId(),
229  "title" => $this->getTitle(),
230  "type" => $this->getQuestiontype(),
231  "obligatory" => $this->getObligatory()
232  );
233  $a_xml_writer->xmlStartTag("question", $attrs);
234 
235  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
236  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
237  if (strlen($this->label))
238  {
239  $attrs = array(
240  "label" => $this->label,
241  );
242  }
243  else
244  {
245  $attrs = array();
246  }
247  $a_xml_writer->xmlStartTag("questiontext", $attrs);
248  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
249  $a_xml_writer->xmlEndTag("questiontext");
250 
251  $a_xml_writer->xmlStartTag("responses");
252  $attrs = array(
253  "id" => "0",
254  "rows" => $this->getTextHeight(),
255  "columns" => $this->getTextWidth()
256  );
257  if ($this->getMaxChars() > 0)
258  {
259  $attrs["maxlength"] = $this->getMaxChars();
260  }
261  $a_xml_writer->xmlElement("response_text", $attrs);
262  $a_xml_writer->xmlEndTag("responses");
263 
264  if (count($this->material))
265  {
266  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
267  {
268  $attrs = array(
269  "label" => $this->material["title"]
270  );
271  $a_xml_writer->xmlStartTag("material", $attrs);
272  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
273  if (strcmp($matches[1], "") != 0)
274  {
275  $intlink = $this->material["internal_link"];
276  }
277  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
278  $a_xml_writer->xmlEndTag("material");
279  }
280  }
281 
282  $a_xml_writer->xmlEndTag("question");
283  }
284 
291  function _getMaxChars($question_id)
292  {
293  global $ilDB;
294  $result = $ilDB->queryF("SELECT maxchars FROM svy_question WHERE question_id = %s",
295  array('integer'),
296  array($question_id)
297  );
298  if ($result->numRows())
299  {
300  $row = $ilDB->fetchAssoc($result);
301  return $row["maxchars"];
302  }
303  return 0;
304  }
305 
312  function getQuestionType()
313  {
314  return "SurveyTextQuestion";
315  }
316 
324  {
325  return "svy_qst_text";
326  }
327 
334  function &getWorkingDataFromUserInput($post_data)
335  {
336  $entered_value = $post_data[$this->getId() . "_text_question"];
337  $data = array();
338  if (strlen($entered_value))
339  {
340  array_push($data, array("textanswer" => $entered_value));
341  }
342  return $data;
343  }
344 
354  function checkUserInput($post_data, $survey_id)
355  {
356  $entered_value = $post_data[$this->getId() . "_text_question"];
357 
358  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
359 
360  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
361 
362  return "";
363  }
364 
365  function randomText($length)
366  {
367  $random= "";
368  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
369  $char_list .= "abcdefghijklmnopqrstuvwxyz";
370  $char_list .= "1234567890";
371  for($i = 0; $i < $length; $i++)
372  {
373  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
374  if (!rand(0,5)) $random .= ' ';
375  }
376  return $random;
377  }
378 
384  public function saveRandomData($active_id)
385  {
386  global $ilDB;
387  // single response
388  $randomtext = $this->randomText(rand(25,100));
389  $next_id = $ilDB->nextId('svy_answer');
390  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
391  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
392  array($next_id, $this->getId(), $active_id, NULL, $randomtext, time())
393  );
394  }
395 
396  function saveUserInput($post_data, $active_id, $a_return = false)
397  {
398  global $ilDB;
399 
400  include_once "./Services/Utilities/classes/class.ilUtil.php";
401  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
402  $maxchars = $this->getMaxChars();
403  if ($maxchars > 0)
404  {
405  $entered_value = substr($entered_value, 0, $maxchars);
406  }
407 
408  if($a_return)
409  {
410  return array(array("value"=>null, "textanswer"=>$entered_value));
411  }
412  if (strlen($entered_value) == 0) return;
413 
414  $next_id = $ilDB->nextId('svy_answer');
415  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
416  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
417  array($next_id, $this->getId(), $active_id, NULL, (strlen($entered_value)) ? $entered_value : NULL, time())
418  );
419  }
420 
421  function &getCumulatedResults($survey_id, $nr_of_users)
422  {
423  global $ilDB;
424 
425  $question_id = $this->getId();
426 
427  $result_array = array();
428  $cumulated = array();
429  $textvalues = array();
430 
431  $result = $ilDB->queryF("SELECT svy_answer.* FROM svy_answer, svy_finished WHERE svy_answer.question_fi = %s AND svy_finished.survey_fi = %s AND svy_finished.finished_id = svy_answer.active_fi",
432  array('integer','integer'),
433  array($question_id, $survey_id)
434  );
435 
436  while ($row = $ilDB->fetchAssoc($result))
437  {
438  $cumulated[$row["value"]]++;
439  array_push($textvalues, $row["textanswer"]);
440  }
441  asort($cumulated, SORT_NUMERIC);
442  end($cumulated);
443  $numrows = $result->numRows();
444  $result_array["USERS_ANSWERED"] = $result->numRows();
445  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
446  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
447  $result_array["textvalues"] = $textvalues;
448  return $result_array;
449  }
450 
460  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
461  {
462  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
463  $worksheet =& $workbook->addWorksheet();
464  $rowcounter = 0;
465  switch ($export_label)
466  {
467  case 'label_only':
468  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
469  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->label));
470  break;
471  case 'title_only':
472  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
473  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
474  break;
475  default:
476  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
477  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
478  $rowcounter++;
479  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
480  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->label));
481  break;
482  }
483  $rowcounter++;
484  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
485  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
486  $rowcounter++;
487  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
488  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
489  $rowcounter++;
490  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
491  $worksheet->write($rowcounter, 1, $eval_data["USERS_ANSWERED"]);
492  $rowcounter++;
493  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
494  $worksheet->write($rowcounter, 1, $eval_data["USERS_SKIPPED"]);
495  $rowcounter++;
496 
497  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
498  $textvalues = "";
499  if (is_array($eval_data["textvalues"]))
500  {
501  foreach ($eval_data["textvalues"] as $textvalue)
502  {
503  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
504  }
505  }
506  }
507 
515  function addUserSpecificResultsData(&$a_array, &$resultset)
516  {
517  if (count($resultset["answers"][$this->getId()]))
518  {
519  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
520  {
521  array_push($a_array, $answer["textanswer"]);
522  }
523  }
524  else
525  {
526  array_push($a_array, $this->lng->txt("skipped"));
527  }
528  }
529 
538  {
539  global $ilDB;
540 
541  $answers = array();
542 
543  $result = $ilDB->queryF("SELECT svy_answer.* FROM svy_answer, svy_finished WHERE svy_finished.survey_fi = %s AND svy_answer.question_fi = %s AND svy_finished.finished_id = svy_answer.active_fi",
544  array('integer','integer'),
545  array($survey_id, $this->getId())
546  );
547  while ($row = $ilDB->fetchAssoc($result))
548  {
549  $answers[$row["active_fi"]] = $row["textanswer"];
550  }
551  return $answers;
552  }
553 
560  function importResponses($a_data)
561  {
562  foreach ($a_data as $id => $data)
563  {
564  if ($data["maxlength"] > 0)
565  {
566  $this->setMaxChars($data["maxlength"]);
567  }
568  if ($data["rows"] > 0)
569  {
570  $this->setTextHeight($data["rows"]);
571  }
572  if ($data["columns"] > 0)
573  {
574  $this->setTextWidth($data["columns"]);
575  }
576  }
577  }
578 
586  {
587  return FALSE;
588  }
589 
596  function getTextWidth()
597  {
598  return ($this->textwidth) ? $this->textwidth : NULL;
599  }
600 
607  function getTextHeight()
608  {
609  return ($this->textheight) ? $this->textheight : NULL;
610  }
611 
618  function setTextWidth($a_textwidth)
619  {
620  if ($a_textwidth < 1)
621  {
622  $this->textwidth = 50;
623  }
624  else
625  {
626  $this->textwidth = $a_textwidth;
627  }
628  }
629 
636  function setTextHeight($a_textheight)
637  {
638  if ($a_textheight < 1)
639  {
640  $this->textheight = 5;
641  }
642  else
643  {
644  $this->textheight = $a_textheight;
645  }
646  }
647 }
648 ?>