ILIAS  Release_3_10_x_branch Revision 61812
 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 
56  $title = "",
57  $description = "",
58  $author = "",
59  $questiontext = "",
60  $owner = -1
61  )
62 
63  {
65  $this->maxchars = 0;
66  $this->textwidth = 50;
67  $this->textheight = 5;
68  }
69 
80  {
81  global $ilDB;
82 
83  $query = sprintf("SELECT survey_question.*, survey_question_text.* FROM survey_question, survey_question_text WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_text.question_fi",
84  $ilDB->quote($id)
85  );
86  $result = $ilDB->query($query);
87  if ($result->numRows() == 1)
88  {
89  return $result->fetchRow(MDB2_FETCHMODE_ASSOC);
90  }
91  else
92  {
93  return array();
94  }
95  }
96 
105  function loadFromDb($id)
106  {
107  global $ilDB;
108  $query = sprintf("SELECT survey_question.*, survey_question_text.* FROM survey_question, survey_question_text WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_text.question_fi",
109  $ilDB->quote($id)
110  );
111  $result = $ilDB->query($query);
112  if ($result->numRows() == 1)
113  {
114  $data = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
115  $this->id = $data->question_id;
116  $this->title = $data->title;
117  $this->description = $data->description;
118  $this->obj_id = $data->obj_fi;
119  $this->author = $data->author;
120  $this->obligatory = $data->obligatory;
121  $this->owner = $data->owner_fi;
122  $this->original_id = $data->original_id;
123  $this->maxchars = $data->maxchars;
124  $this->textwidth = $data->width;
125  $this->textheight = $data->height;
126  include_once("./Services/RTE/classes/class.ilRTE.php");
127  $this->questiontext = ilRTE::_replaceMediaObjectImageSrc($data->questiontext, 1);
128  $this->complete = $data->complete;
129  // loads materials uris from database
130  $this->loadMaterialFromDb($id);
131  }
133  }
134 
143  function isComplete()
144  {
145  if ($this->title and $this->author and $this->questiontext)
146  {
147  return 1;
148  }
149  else
150  {
151  return 0;
152  }
153  }
154 
162  function setMaxChars($maxchars = 0)
163  {
164  $this->maxchars = $maxchars;
165  }
166 
174  function getMaxChars()
175  {
176  return $this->maxchars;
177  }
178 
186  function saveToDb($original_id = "")
187  {
188  global $ilDB;
189  $maxchars = "NULL";
190  if ($this->maxchars)
191  {
192  $maxchars = $ilDB->quote($this->maxchars . "");
193  }
194  $complete = 0;
195  if ($this->isComplete()) {
196  $complete = 1;
197  }
198  if ($original_id)
199  {
200  $original_id = $ilDB->quote($original_id);
201  }
202  else
203  {
204  $original_id = "NULL";
205  }
206 
207  // cleanup RTE images which are not inserted into the question text
208  include_once("./Services/RTE/classes/class.ilRTE.php");
209  ilRTE::_cleanupMediaObjectUsage($this->questiontext, "spl:html",
210  $this->getId());
211 
212  if ($this->id == -1)
213  {
214  // Write new dataset
215  $now = getdate();
216  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
217  $query = sprintf("INSERT INTO survey_question (question_id, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
218  $ilDB->quote($this->getQuestionTypeID() . ""),
219  $ilDB->quote($this->obj_id),
220  $ilDB->quote($this->owner),
221  $ilDB->quote($this->title),
222  $ilDB->quote($this->description),
223  $ilDB->quote($this->author),
224  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
225  $ilDB->quote(sprintf("%d", $this->obligatory)),
226  $ilDB->quote("$complete"),
227  $ilDB->quote($created),
228  $original_id
229  );
230  $result = $ilDB->query($query);
231  if (PEAR::isError($result))
232  {
233  global $ilias;
234  $ilias->raiseError($result->getMessage());
235  }
236  else
237  {
238  $this->id = $ilDB->getLastInsertId();
239  $query = sprintf("INSERT INTO survey_question_text (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
240  $ilDB->quote($this->id . ""),
241  $maxchars,
242  $ilDB->quote($this->getTextWidth() . ""),
243  $ilDB->quote($this->getTextHeight() . ""),
244  $maxchars
245  );
246  $ilDB->query($query);
247  }
248  }
249  else
250  {
251  // update existing dataset
252  $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
253  $ilDB->quote($this->title),
254  $ilDB->quote($this->description),
255  $ilDB->quote($this->author),
256  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
257  $ilDB->quote(sprintf("%d", $this->obligatory)),
258  $ilDB->quote("$complete"),
259  $ilDB->quote($this->id)
260  );
261  $result = $ilDB->query($query);
262  $query = sprintf("UPDATE survey_question_text SET maxchars = %s, width = %s, height = %s WHERE question_fi = %s",
263  $maxchars,
264  $ilDB->quote($this->getTextWidth() . ""),
265  $ilDB->quote($this->getTextHeight() . ""),
266  $ilDB->quote($this->id . "")
267  );
268  $result = $ilDB->query($query);
269  }
270  if (PEAR::isError($result))
271  {
272  global $ilias;
273  $ilias->raiseError($result->getMessage());
274  }
275  else
276  {
277  // saving material uris in the database
278  $this->saveMaterialsToDb();
279  }
280  parent::saveToDb($original_id);
281  }
282 
291  function toXML($a_include_header = TRUE, $obligatory_state = "")
292  {
293  include_once("./classes/class.ilXmlWriter.php");
294  $a_xml_writer = new ilXmlWriter;
295  $a_xml_writer->xmlHeader();
296  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
297  $xml = $a_xml_writer->xmlDumpMem(FALSE);
298  if (!$a_include_header)
299  {
300  $pos = strpos($xml, "?>");
301  $xml = substr($xml, $pos + 2);
302  }
303  return $xml;
304  }
305 
316  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
317  {
318  $attrs = array(
319  "id" => $this->getId(),
320  "title" => $this->getTitle(),
321  "type" => $this->getQuestiontype(),
322  "obligatory" => $this->getObligatory()
323  );
324  $a_xml_writer->xmlStartTag("question", $attrs);
325 
326  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
327  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
328  $a_xml_writer->xmlStartTag("questiontext");
329  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
330  $a_xml_writer->xmlEndTag("questiontext");
331 
332  $a_xml_writer->xmlStartTag("responses");
333  $attrs = array(
334  "id" => "0",
335  "rows" => $this->getTextHeight(),
336  "columns" => $this->getTextWidth()
337  );
338  if ($this->getMaxChars() > 0)
339  {
340  $attrs["maxlength"] = $this->getMaxChars();
341  }
342  $a_xml_writer->xmlElement("response_text", $attrs);
343  $a_xml_writer->xmlEndTag("responses");
344 
345  if (count($this->material))
346  {
347  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
348  {
349  $attrs = array(
350  "label" => $this->material["title"]
351  );
352  $a_xml_writer->xmlStartTag("material", $attrs);
353  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
354  if (strcmp($matches[1], "") != 0)
355  {
356  $intlink = $this->material["internal_link"];
357  }
358  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
359  $a_xml_writer->xmlEndTag("material");
360  }
361  }
362 
363  $a_xml_writer->xmlEndTag("question");
364  }
365 
366  function syncWithOriginal()
367  {
368  global $ilDB;
369  if ($this->original_id)
370  {
371  $complete = 0;
372  if ($this->isComplete())
373  {
374  $complete = 1;
375  }
376  $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
377  $ilDB->quote($this->title . ""),
378  $ilDB->quote($this->description . ""),
379  $ilDB->quote($this->author . ""),
380  $ilDB->quote($this->questiontext . ""),
381  $ilDB->quote(sprintf("%d", $this->obligatory) . ""),
382  $ilDB->quote($complete . ""),
383  $ilDB->quote($this->original_id . "")
384  );
385  $result = $ilDB->query($query);
386  $query = sprintf("UPDATE survey_question_text SET maxchars = %s, width = %s, height = %s WHERE question_fi = %s",
387  $ilDB->quote($this->getMaxChars() . ""),
388  $ilDB->quote($this->getTextWidth() . ""),
389  $ilDB->quote($this->getTextHeight() . ""),
390  $ilDB->quote($this->original_id . "")
391  );
392  $result = $ilDB->query($query);
393  }
395  }
396 
405  function _getMaxChars($question_id)
406  {
407  global $ilDB;
408  $query = sprintf("SELECT maxchars FROM survey_question WHERE question_id = %s",
409  $ilDB->quote($question_id . "")
410  );
411  $result = $ilDB->query($query);
412  if ($result->numRows())
413  {
414  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
415  return $row["maxchars"];
416  }
417  return 0;
418  }
419 
428  function getQuestionType()
429  {
430  return "SurveyTextQuestion";
431  }
432 
442  {
443  return "survey_question_text";
444  }
445 
454  function &getWorkingDataFromUserInput($post_data)
455  {
456  $entered_value = $post_data[$this->getId() . "_text_question"];
457  $data = array();
458  if (strlen($entered_value))
459  {
460  array_push($data, array("textanswer" => $entered_value));
461  }
462  return $data;
463  }
464 
477  function checkUserInput($post_data, $survey_id)
478  {
479  $entered_value = $post_data[$this->getId() . "_text_question"];
480 
481  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
482 
483  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
484 
485  return "";
486  }
487 
488  function randomText($length)
489  {
490  $random= "";
491  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
492  $char_list .= "abcdefghijklmnopqrstuvwxyz";
493  $char_list .= "1234567890";
494  for($i = 0; $i < $length; $i++)
495  {
496  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
497  if (!rand(0,5)) $random .= ' ';
498  }
499  return $random;
500  }
501 
507  public function saveRandomData($active_id)
508  {
509  global $ilDB;
510  // single response
511  $randomtext = $this->randomText(rand(25,100));
512  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, TIMESTAMP) VALUES (NULL, %s, %s, NULL, %s, NULL)",
513  $ilDB->quote($this->getId()),
514  $ilDB->quote($active_id),
515  $ilDB->quote($randomtext)
516  );
517  $result = $ilDB->query($query);
518  }
519 
520  function saveUserInput($post_data, $active_id)
521  {
522  global $ilDB;
523 
524  include_once "./Services/Utilities/classes/class.ilUtil.php";
525  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
526  $maxchars = $this->getMaxChars();
527  if ($maxchars > 0)
528  {
529  $entered_value = substr($entered_value, 0, $maxchars);
530  }
531  if (strlen($entered_value) == 0) return;
532  $entered_value = $ilDB->quote($entered_value . "");
533  $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
534  $ilDB->quote($this->getId() . ""),
535  $ilDB->quote($active_id . ""),
536  "NULL",
537  $entered_value
538  );
539  $result = $ilDB->query($query);
540  }
541 
542  function &getCumulatedResults($survey_id, $nr_of_users)
543  {
544  global $ilDB;
545 
546  $question_id = $this->getId();
547 
548  $result_array = array();
549  $cumulated = array();
550  $textvalues = array();
551 
552  $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_answer.question_fi = %s AND survey_finished.survey_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
553  $ilDB->quote($question_id),
554  $ilDB->quote($survey_id)
555  );
556  $result = $ilDB->query($query);
557 
558  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
559  {
560  $cumulated["$row->value"]++;
561  array_push($textvalues, $row->textanswer);
562  }
563  asort($cumulated, SORT_NUMERIC);
564  end($cumulated);
565  $numrows = $result->numRows();
566  $result_array["USERS_ANSWERED"] = $result->numRows();
567  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
568  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
569  $result_array["textvalues"] = $textvalues;
570  return $result_array;
571  }
572 
584  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
585  {
586  include_once ("./classes/class.ilExcelUtils.php");
587  $worksheet =& $workbook->addWorksheet();
588  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
589  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
590  $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
591  $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
592  $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
593  $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
594  $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
595  $worksheet->write(3, 1, $eval_data["USERS_ANSWERED"]);
596  $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
597  $worksheet->write(4, 1, $eval_data["USERS_SKIPPED"]);
598  $rowcounter = 5;
599 
600  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
601  $textvalues = "";
602  if (is_array($eval_data["textvalues"]))
603  {
604  foreach ($eval_data["textvalues"] as $textvalue)
605  {
606  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
607  }
608  }
609  }
610 
620  function addUserSpecificResultsData(&$a_array, &$resultset)
621  {
622  if (count($resultset["answers"][$this->getId()]))
623  {
624  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
625  {
626  array_push($a_array, $answer["textanswer"]);
627  }
628  }
629  else
630  {
631  array_push($a_array, $this->lng->txt("skipped"));
632  }
633  }
634 
645  {
646  global $ilDB;
647 
648  $answers = array();
649 
650  $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_finished.survey_fi = %s AND survey_answer.question_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
651  $ilDB->quote($survey_id),
652  $ilDB->quote($this->getId())
653  );
654  $result = $ilDB->query($query);
655  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
656  {
657  $answers[$row["active_fi"]] = $row["textanswer"];
658  }
659  return $answers;
660  }
661 
670  function importResponses($a_data)
671  {
672  foreach ($a_data as $id => $data)
673  {
674  if ($data["maxlength"] > 0)
675  {
676  $this->setMaxChars($data["maxlength"]);
677  }
678  if ($data["rows"] > 0)
679  {
680  $this->setTextHeight($data["rows"]);
681  }
682  if ($data["columns"] > 0)
683  {
684  $this->setTextWidth($data["columns"]);
685  }
686  }
687  }
688 
698  {
699  return FALSE;
700  }
701 
710  function getTextWidth()
711  {
712  return $this->textwidth;
713  }
714 
723  function getTextHeight()
724  {
725  return $this->textheight;
726  }
727 
736  function setTextWidth($a_textwidth)
737  {
738  if ($a_textwidth < 1)
739  {
740  $this->textwidth = 50;
741  }
742  else
743  {
744  $this->textwidth = $a_textwidth;
745  }
746  }
747 
756  function setTextHeight($a_textheight)
757  {
758  if ($a_textheight < 1)
759  {
760  $this->textheight = 5;
761  }
762  else
763  {
764  $this->textheight = $a_textheight;
765  }
766  }
767 }
768 ?>