ILIAS  eassessment Revision 61809
 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 
448  function setExportDetailsXLS(&$adapter, &$eval_data, $export_label)
449  {
450  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
451  $adapter->addWorksheet($this->getTitle());
452  $adapter->setActiveWorksheet($adapter->getWorksheetCount()-1);
453  $rowcounter = 0;
454  switch ($export_label)
455  {
456  case 'label_only':
457  $adapter->setCellValue(0, 0, $this->lng->txt("label"), CELL_FORMAT_BOLD);
458  $adapter->setCellValue(0, 1, $this->label);
459  break;
460  case 'title_only':
461  $adapter->setCellValue(0, 0, $this->lng->txt("title"), CELL_FORMAT_BOLD);
462  $adapter->setCellValue(0, 1, $this->getTitle());
463  break;
464  default:
465  $adapter->setCellValue(0, 0, $this->lng->txt("title"), CELL_FORMAT_BOLD);
466  $adapter->setCellValue(0, 1, $this->getTitle());
467  $rowcounter++;
468  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("label"), CELL_FORMAT_BOLD);
469  $adapter->setCellValue($rowcounter, 1, $this->label);
470  break;
471  }
472  $rowcounter++;
473  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("question"), CELL_FORMAT_BOLD);
474  $adapter->setCellValue($rowcounter, 1, $this->getQuestiontext());
475  $rowcounter++;
476  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("question_type"), CELL_FORMAT_BOLD);
477  $adapter->setCellValue($rowcounter, 1, $this->lng->txt($this->getQuestionType()));
478  $rowcounter++;
479  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("users_answered"), CELL_FORMAT_BOLD);
480  $adapter->setCellValue($rowcounter, 1, $eval_data["TOTAL"]["USERS_ANSWERED"]);
481  $rowcounter++;
482  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("users_skipped"), CELL_FORMAT_BOLD);
483  $adapter->setCellValue($rowcounter, 1, $eval_data["TOTAL"]["USERS_SKIPPED"]);
484  $rowcounter++;
485 
486  $adapter->setCellValue($rowcounter, 0, $this->lng->txt("given_answers"), CELL_FORMAT_BOLD);
487  $textvalues = "";
488  if (is_array($eval_data["textvalues"]))
489  {
490  foreach ($eval_data["textvalues"] as $textvalue)
491  {
492  $adapter->setCellValue($rowcounter++, 1, $textvalue);
493  }
494  }
495  }
496 
504  function addUserSpecificResultsData(&$a_array, &$resultset)
505  {
506  if (count($resultset["answers"][$this->getId()]))
507  {
508  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
509  {
510  array_push($a_array, $answer["textanswer"]);
511  }
512  }
513  else
514  {
515  array_push($a_array, $this->lng->txt("skipped"));
516  }
517  }
518 
527  {
528  global $ilDB;
529 
530  $answers = array();
531 
532  $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",
533  array('integer','integer'),
534  array($survey_id, $this->getId())
535  );
536  while ($row = $ilDB->fetchAssoc($result))
537  {
538  $answers[$row["active_fi"]] = $row["textanswer"];
539  }
540  return $answers;
541  }
542 
549  function importResponses($a_data)
550  {
551  foreach ($a_data as $id => $data)
552  {
553  if ($data["maxlength"] > 0)
554  {
555  $this->setMaxChars($data["maxlength"]);
556  }
557  if ($data["rows"] > 0)
558  {
559  $this->setTextHeight($data["rows"]);
560  }
561  if ($data["columns"] > 0)
562  {
563  $this->setTextWidth($data["columns"]);
564  }
565  }
566  }
567 
575  {
576  return FALSE;
577  }
578 
585  function getTextWidth()
586  {
587  return ($this->textwidth) ? $this->textwidth : NULL;
588  }
589 
596  function getTextHeight()
597  {
598  return ($this->textheight) ? $this->textheight : NULL;
599  }
600 
607  function setTextWidth($a_textwidth)
608  {
609  if ($a_textwidth < 1)
610  {
611  $this->textwidth = 50;
612  }
613  else
614  {
615  $this->textwidth = $a_textwidth;
616  }
617  }
618 
625  function setTextHeight($a_textheight)
626  {
627  if ($a_textheight < 1)
628  {
629  $this->textheight = 5;
630  }
631  else
632  {
633  $this->textheight = $a_textheight;
634  }
635  }
636 }
637 ?>