ILIAS  Release_4_0_x_branch Revision 61816
 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->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  }
126  }
127 
134  function isComplete()
135  {
136  if ($this->title and $this->author and $this->questiontext)
137  {
138  return 1;
139  }
140  else
141  {
142  return 0;
143  }
144  }
145 
151  function setMaxChars($maxchars = 0)
152  {
153  $this->maxchars = $maxchars;
154  }
155 
161  function getMaxChars()
162  {
163  return ($this->maxchars) ? $this->maxchars : NULL;
164  }
165 
171  function saveToDb($original_id = "")
172  {
173  global $ilDB;
174 
175  $affectedRows = parent::saveToDb($original_id);
176  if ($affectedRows == 1)
177  {
178  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
179  array('integer'),
180  array($this->getId())
181  );
182  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
183  array('integer', 'integer', 'integer', 'integer'),
184  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
185  );
186 
187  $this->saveMaterial();
188  }
189  }
190 
197  function toXML($a_include_header = TRUE, $obligatory_state = "")
198  {
199  include_once("./classes/class.ilXmlWriter.php");
200  $a_xml_writer = new ilXmlWriter;
201  $a_xml_writer->xmlHeader();
202  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
203  $xml = $a_xml_writer->xmlDumpMem(FALSE);
204  if (!$a_include_header)
205  {
206  $pos = strpos($xml, "?>");
207  $xml = substr($xml, $pos + 2);
208  }
209  return $xml;
210  }
211 
220  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
221  {
222  $attrs = array(
223  "id" => $this->getId(),
224  "title" => $this->getTitle(),
225  "type" => $this->getQuestiontype(),
226  "obligatory" => $this->getObligatory()
227  );
228  $a_xml_writer->xmlStartTag("question", $attrs);
229 
230  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
231  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
232  $a_xml_writer->xmlStartTag("questiontext");
233  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
234  $a_xml_writer->xmlEndTag("questiontext");
235 
236  $a_xml_writer->xmlStartTag("responses");
237  $attrs = array(
238  "id" => "0",
239  "rows" => $this->getTextHeight(),
240  "columns" => $this->getTextWidth()
241  );
242  if ($this->getMaxChars() > 0)
243  {
244  $attrs["maxlength"] = $this->getMaxChars();
245  }
246  $a_xml_writer->xmlElement("response_text", $attrs);
247  $a_xml_writer->xmlEndTag("responses");
248 
249  if (count($this->material))
250  {
251  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
252  {
253  $attrs = array(
254  "label" => $this->material["title"]
255  );
256  $a_xml_writer->xmlStartTag("material", $attrs);
257  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
258  if (strcmp($matches[1], "") != 0)
259  {
260  $intlink = $this->material["internal_link"];
261  }
262  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
263  $a_xml_writer->xmlEndTag("material");
264  }
265  }
266 
267  $a_xml_writer->xmlEndTag("question");
268  }
269 
276  function _getMaxChars($question_id)
277  {
278  global $ilDB;
279  $result = $ilDB->queryF("SELECT maxchars FROM svy_question WHERE question_id = %s",
280  array('integer'),
281  array($question_id)
282  );
283  if ($result->numRows())
284  {
285  $row = $ilDB->fetchAssoc($result);
286  return $row["maxchars"];
287  }
288  return 0;
289  }
290 
297  function getQuestionType()
298  {
299  return "SurveyTextQuestion";
300  }
301 
309  {
310  return "svy_qst_text";
311  }
312 
319  function &getWorkingDataFromUserInput($post_data)
320  {
321  $entered_value = $post_data[$this->getId() . "_text_question"];
322  $data = array();
323  if (strlen($entered_value))
324  {
325  array_push($data, array("textanswer" => $entered_value));
326  }
327  return $data;
328  }
329 
339  function checkUserInput($post_data, $survey_id)
340  {
341  $entered_value = $post_data[$this->getId() . "_text_question"];
342 
343  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
344 
345  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
346 
347  return "";
348  }
349 
350  function randomText($length)
351  {
352  $random= "";
353  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
354  $char_list .= "abcdefghijklmnopqrstuvwxyz";
355  $char_list .= "1234567890";
356  for($i = 0; $i < $length; $i++)
357  {
358  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
359  if (!rand(0,5)) $random .= ' ';
360  }
361  return $random;
362  }
363 
369  public function saveRandomData($active_id)
370  {
371  global $ilDB;
372  // single response
373  $randomtext = $this->randomText(rand(25,100));
374  $next_id = $ilDB->nextId('svy_answer');
375  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
376  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
377  array($next_id, $this->getId(), $active_id, NULL, $randomtext, time())
378  );
379  }
380 
381  function saveUserInput($post_data, $active_id)
382  {
383  global $ilDB;
384 
385  include_once "./Services/Utilities/classes/class.ilUtil.php";
386  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
387  $maxchars = $this->getMaxChars();
388  if ($maxchars > 0)
389  {
390  $entered_value = substr($entered_value, 0, $maxchars);
391  }
392  if (strlen($entered_value) == 0) return;
393  $next_id = $ilDB->nextId('svy_answer');
394  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
395  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
396  array($next_id, $this->getId(), $active_id, NULL, (strlen($entered_value)) ? $entered_value : NULL, time())
397  );
398  }
399 
400  function &getCumulatedResults($survey_id, $nr_of_users)
401  {
402  global $ilDB;
403 
404  $question_id = $this->getId();
405 
406  $result_array = array();
407  $cumulated = array();
408  $textvalues = array();
409 
410  $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",
411  array('integer','integer'),
412  array($question_id, $survey_id)
413  );
414 
415  while ($row = $ilDB->fetchAssoc($result))
416  {
417  $cumulated[$row["value"]]++;
418  array_push($textvalues, $row["textanswer"]);
419  }
420  asort($cumulated, SORT_NUMERIC);
421  end($cumulated);
422  $numrows = $result->numRows();
423  $result_array["USERS_ANSWERED"] = $result->numRows();
424  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
425  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
426  $result_array["textvalues"] = $textvalues;
427  return $result_array;
428  }
429 
439  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
440  {
441  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
442  $worksheet =& $workbook->addWorksheet();
443  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
444  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
445  $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
446  $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
447  $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
448  $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
449  $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
450  $worksheet->write(3, 1, $eval_data["USERS_ANSWERED"]);
451  $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
452  $worksheet->write(4, 1, $eval_data["USERS_SKIPPED"]);
453  $rowcounter = 5;
454 
455  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
456  $textvalues = "";
457  if (is_array($eval_data["textvalues"]))
458  {
459  foreach ($eval_data["textvalues"] as $textvalue)
460  {
461  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
462  }
463  }
464  }
465 
473  function addUserSpecificResultsData(&$a_array, &$resultset)
474  {
475  if (count($resultset["answers"][$this->getId()]))
476  {
477  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
478  {
479  array_push($a_array, $answer["textanswer"]);
480  }
481  }
482  else
483  {
484  array_push($a_array, $this->lng->txt("skipped"));
485  }
486  }
487 
496  {
497  global $ilDB;
498 
499  $answers = array();
500 
501  $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",
502  array('integer','integer'),
503  array($survey_id, $this->getId())
504  );
505  while ($row = $ilDB->fetchAssoc($result))
506  {
507  $answers[$row["active_fi"]] = $row["textanswer"];
508  }
509  return $answers;
510  }
511 
518  function importResponses($a_data)
519  {
520  foreach ($a_data as $id => $data)
521  {
522  if ($data["maxlength"] > 0)
523  {
524  $this->setMaxChars($data["maxlength"]);
525  }
526  if ($data["rows"] > 0)
527  {
528  $this->setTextHeight($data["rows"]);
529  }
530  if ($data["columns"] > 0)
531  {
532  $this->setTextWidth($data["columns"]);
533  }
534  }
535  }
536 
544  {
545  return FALSE;
546  }
547 
554  function getTextWidth()
555  {
556  return ($this->textwidth) ? $this->textwidth : NULL;
557  }
558 
565  function getTextHeight()
566  {
567  return ($this->textheight) ? $this->textheight : NULL;
568  }
569 
576  function setTextWidth($a_textwidth)
577  {
578  if ($a_textwidth < 1)
579  {
580  $this->textwidth = 50;
581  }
582  else
583  {
584  $this->textwidth = $a_textwidth;
585  }
586  }
587 
594  function setTextHeight($a_textheight)
595  {
596  if ($a_textheight < 1)
597  {
598  $this->textheight = 5;
599  }
600  else
601  {
602  $this->textheight = $a_textheight;
603  }
604  }
605 }
606 ?>