ILIAS  Release_4_1_x_branch Revision 61804
 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 ($this->title and $this->author and $this->questiontext)
138  {
139  return 1;
140  }
141  else
142  {
143  return 0;
144  }
145  }
146 
152  function setMaxChars($maxchars = 0)
153  {
154  $this->maxchars = $maxchars;
155  }
156 
162  function getMaxChars()
163  {
164  return ($this->maxchars) ? $this->maxchars : NULL;
165  }
166 
172  function saveToDb($original_id = "")
173  {
174  global $ilDB;
175 
176  $affectedRows = parent::saveToDb($original_id);
177  if ($affectedRows == 1)
178  {
179  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
180  array('integer'),
181  array($this->getId())
182  );
183  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
184  array('integer', 'integer', 'integer', 'integer'),
185  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
186  );
187 
188  $this->saveMaterial();
189  }
190  }
191 
198  function toXML($a_include_header = TRUE, $obligatory_state = "")
199  {
200  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
201  $a_xml_writer = new ilXmlWriter;
202  $a_xml_writer->xmlHeader();
203  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
204  $xml = $a_xml_writer->xmlDumpMem(FALSE);
205  if (!$a_include_header)
206  {
207  $pos = strpos($xml, "?>");
208  $xml = substr($xml, $pos + 2);
209  }
210  return $xml;
211  }
212 
221  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
222  {
223  $attrs = array(
224  "id" => $this->getId(),
225  "title" => $this->getTitle(),
226  "type" => $this->getQuestiontype(),
227  "obligatory" => $this->getObligatory()
228  );
229  $a_xml_writer->xmlStartTag("question", $attrs);
230 
231  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
232  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
233  if (strlen($this->label))
234  {
235  $attrs = array(
236  "label" => $this->label,
237  );
238  }
239  else
240  {
241  $attrs = array();
242  }
243  $a_xml_writer->xmlStartTag("questiontext", $attrs);
244  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
245  $a_xml_writer->xmlEndTag("questiontext");
246 
247  $a_xml_writer->xmlStartTag("responses");
248  $attrs = array(
249  "id" => "0",
250  "rows" => $this->getTextHeight(),
251  "columns" => $this->getTextWidth()
252  );
253  if ($this->getMaxChars() > 0)
254  {
255  $attrs["maxlength"] = $this->getMaxChars();
256  }
257  $a_xml_writer->xmlElement("response_text", $attrs);
258  $a_xml_writer->xmlEndTag("responses");
259 
260  if (count($this->material))
261  {
262  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
263  {
264  $attrs = array(
265  "label" => $this->material["title"]
266  );
267  $a_xml_writer->xmlStartTag("material", $attrs);
268  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
269  if (strcmp($matches[1], "") != 0)
270  {
271  $intlink = $this->material["internal_link"];
272  }
273  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
274  $a_xml_writer->xmlEndTag("material");
275  }
276  }
277 
278  $a_xml_writer->xmlEndTag("question");
279  }
280 
287  function _getMaxChars($question_id)
288  {
289  global $ilDB;
290  $result = $ilDB->queryF("SELECT maxchars FROM svy_question WHERE question_id = %s",
291  array('integer'),
292  array($question_id)
293  );
294  if ($result->numRows())
295  {
296  $row = $ilDB->fetchAssoc($result);
297  return $row["maxchars"];
298  }
299  return 0;
300  }
301 
308  function getQuestionType()
309  {
310  return "SurveyTextQuestion";
311  }
312 
320  {
321  return "svy_qst_text";
322  }
323 
330  function &getWorkingDataFromUserInput($post_data)
331  {
332  $entered_value = $post_data[$this->getId() . "_text_question"];
333  $data = array();
334  if (strlen($entered_value))
335  {
336  array_push($data, array("textanswer" => $entered_value));
337  }
338  return $data;
339  }
340 
350  function checkUserInput($post_data, $survey_id)
351  {
352  $entered_value = $post_data[$this->getId() . "_text_question"];
353 
354  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
355 
356  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
357 
358  return "";
359  }
360 
361  function randomText($length)
362  {
363  $random= "";
364  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
365  $char_list .= "abcdefghijklmnopqrstuvwxyz";
366  $char_list .= "1234567890";
367  for($i = 0; $i < $length; $i++)
368  {
369  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
370  if (!rand(0,5)) $random .= ' ';
371  }
372  return $random;
373  }
374 
380  public function saveRandomData($active_id)
381  {
382  global $ilDB;
383  // single response
384  $randomtext = $this->randomText(rand(25,100));
385  $next_id = $ilDB->nextId('svy_answer');
386  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
387  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
388  array($next_id, $this->getId(), $active_id, NULL, $randomtext, time())
389  );
390  }
391 
392  function saveUserInput($post_data, $active_id)
393  {
394  global $ilDB;
395 
396  include_once "./Services/Utilities/classes/class.ilUtil.php";
397  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
398  $maxchars = $this->getMaxChars();
399  if ($maxchars > 0)
400  {
401  $entered_value = substr($entered_value, 0, $maxchars);
402  }
403  if (strlen($entered_value) == 0) return;
404  $next_id = $ilDB->nextId('svy_answer');
405  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
406  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
407  array($next_id, $this->getId(), $active_id, NULL, (strlen($entered_value)) ? $entered_value : NULL, time())
408  );
409  }
410 
411  function &getCumulatedResults($survey_id, $nr_of_users)
412  {
413  global $ilDB;
414 
415  $question_id = $this->getId();
416 
417  $result_array = array();
418  $cumulated = array();
419  $textvalues = array();
420 
421  $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",
422  array('integer','integer'),
423  array($question_id, $survey_id)
424  );
425 
426  while ($row = $ilDB->fetchAssoc($result))
427  {
428  $cumulated[$row["value"]]++;
429  array_push($textvalues, $row["textanswer"]);
430  }
431  asort($cumulated, SORT_NUMERIC);
432  end($cumulated);
433  $numrows = $result->numRows();
434  $result_array["USERS_ANSWERED"] = $result->numRows();
435  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
436  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
437  $result_array["textvalues"] = $textvalues;
438  return $result_array;
439  }
440 
450  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
451  {
452  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
453  $worksheet =& $workbook->addWorksheet();
454  $rowcounter = 0;
455  switch ($export_label)
456  {
457  case 'label_only':
458  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
459  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->label));
460  break;
461  case 'title_only':
462  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
463  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
464  break;
465  default:
466  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
467  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
468  $rowcounter++;
469  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
470  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->label));
471  break;
472  }
473  $rowcounter++;
474  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
475  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
476  $rowcounter++;
477  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
478  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
479  $rowcounter++;
480  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
481  $worksheet->write($rowcounter, 1, $eval_data["USERS_ANSWERED"]);
482  $rowcounter++;
483  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
484  $worksheet->write($rowcounter, 1, $eval_data["USERS_SKIPPED"]);
485  $rowcounter++;
486 
487  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
488  $textvalues = "";
489  if (is_array($eval_data["textvalues"]))
490  {
491  foreach ($eval_data["textvalues"] as $textvalue)
492  {
493  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
494  }
495  }
496  }
497 
505  function addUserSpecificResultsData(&$a_array, &$resultset)
506  {
507  if (count($resultset["answers"][$this->getId()]))
508  {
509  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
510  {
511  array_push($a_array, $answer["textanswer"]);
512  }
513  }
514  else
515  {
516  array_push($a_array, $this->lng->txt("skipped"));
517  }
518  }
519 
528  {
529  global $ilDB;
530 
531  $answers = array();
532 
533  $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",
534  array('integer','integer'),
535  array($survey_id, $this->getId())
536  );
537  while ($row = $ilDB->fetchAssoc($result))
538  {
539  $answers[$row["active_fi"]] = $row["textanswer"];
540  }
541  return $answers;
542  }
543 
550  function importResponses($a_data)
551  {
552  foreach ($a_data as $id => $data)
553  {
554  if ($data["maxlength"] > 0)
555  {
556  $this->setMaxChars($data["maxlength"]);
557  }
558  if ($data["rows"] > 0)
559  {
560  $this->setTextHeight($data["rows"]);
561  }
562  if ($data["columns"] > 0)
563  {
564  $this->setTextWidth($data["columns"]);
565  }
566  }
567  }
568 
576  {
577  return FALSE;
578  }
579 
586  function getTextWidth()
587  {
588  return ($this->textwidth) ? $this->textwidth : NULL;
589  }
590 
597  function getTextHeight()
598  {
599  return ($this->textheight) ? $this->textheight : NULL;
600  }
601 
608  function setTextWidth($a_textwidth)
609  {
610  if ($a_textwidth < 1)
611  {
612  $this->textwidth = 50;
613  }
614  else
615  {
616  $this->textwidth = $a_textwidth;
617  }
618  }
619 
626  function setTextHeight($a_textheight)
627  {
628  if ($a_textheight < 1)
629  {
630  $this->textheight = 5;
631  }
632  else
633  {
634  $this->textheight = $a_textheight;
635  }
636  }
637 }
638 ?>