ILIAS  Release_5_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 
38 {
39  var $maxchars;
42 
53  $title = "",
54  $description = "",
55  $author = "",
56  $questiontext = "",
57  $owner = -1
58  )
59  {
61  $this->maxchars = 0;
62  $this->textwidth = 50;
63  $this->textheight = 5;
64  }
65 
74  {
75  global $ilDB;
76  $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",
77  array('integer'),
78  array($id)
79  );
80  if ($result->numRows() == 1)
81  {
82  return $ilDB->fetchAssoc($result);
83  }
84  else
85  {
86  return array();
87  }
88  }
89 
96  function loadFromDb($id)
97  {
98  global $ilDB;
99 
100  $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",
101  array('integer'),
102  array($id)
103  );
104  if ($result->numRows() == 1)
105  {
106  $data = $ilDB->fetchAssoc($result);
107  $this->setId($data["question_id"]);
108  $this->setTitle($data["title"]);
109  $this->label = $data['label'];
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 (
137  strlen($this->getTitle()) &&
138  strlen($this->getAuthor()) &&
139  strlen($this->getQuestiontext())
140  )
141  {
142  return 1;
143  }
144  else
145  {
146  return 0;
147  }
148  }
149 
155  function setMaxChars($maxchars = 0)
156  {
157  $this->maxchars = $maxchars;
158  }
159 
165  function getMaxChars()
166  {
167  return ($this->maxchars) ? $this->maxchars : NULL;
168  }
169 
175  function saveToDb($original_id = "")
176  {
177  global $ilDB;
178 
179  $affectedRows = parent::saveToDb($original_id);
180  if ($affectedRows == 1)
181  {
182  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
183  array('integer'),
184  array($this->getId())
185  );
186  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
187  array('integer', 'integer', 'integer', 'integer'),
188  array($this->getId(), $this->getMaxChars(), $this->getTextWidth(), $this->getTextHeight())
189  );
190 
191  $this->saveMaterial();
192  }
193  }
194 
201  function toXML($a_include_header = TRUE, $obligatory_state = "")
202  {
203  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
204  $a_xml_writer = new ilXmlWriter;
205  $a_xml_writer->xmlHeader();
206  $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
207  $xml = $a_xml_writer->xmlDumpMem(FALSE);
208  if (!$a_include_header)
209  {
210  $pos = strpos($xml, "?>");
211  $xml = substr($xml, $pos + 2);
212  }
213  return $xml;
214  }
215 
224  function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
225  {
226  $attrs = array(
227  "id" => $this->getId(),
228  "title" => $this->getTitle(),
229  "type" => $this->getQuestiontype(),
230  "obligatory" => $this->getObligatory()
231  );
232  $a_xml_writer->xmlStartTag("question", $attrs);
233 
234  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
235  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
236  if (strlen($this->label))
237  {
238  $attrs = array(
239  "label" => $this->label,
240  );
241  }
242  else
243  {
244  $attrs = array();
245  }
246  $a_xml_writer->xmlStartTag("questiontext", $attrs);
247  $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
248  $a_xml_writer->xmlEndTag("questiontext");
249 
250  $a_xml_writer->xmlStartTag("responses");
251  $attrs = array(
252  "id" => "0",
253  "rows" => $this->getTextHeight(),
254  "columns" => $this->getTextWidth()
255  );
256  if ($this->getMaxChars() > 0)
257  {
258  $attrs["maxlength"] = $this->getMaxChars();
259  }
260  $a_xml_writer->xmlElement("response_text", $attrs);
261  $a_xml_writer->xmlEndTag("responses");
262 
263  if (count($this->material))
264  {
265  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
266  {
267  $attrs = array(
268  "label" => $this->material["title"]
269  );
270  $a_xml_writer->xmlStartTag("material", $attrs);
271  $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
272  if (strcmp($matches[1], "") != 0)
273  {
274  $intlink = $this->material["internal_link"];
275  }
276  $a_xml_writer->xmlElement("mattext", NULL, $intlink);
277  $a_xml_writer->xmlEndTag("material");
278  }
279  }
280 
281  $a_xml_writer->xmlEndTag("question");
282  }
283 
290  function _getMaxChars($question_id)
291  {
292  global $ilDB;
293  $result = $ilDB->queryF("SELECT maxchars FROM svy_question WHERE question_id = %s",
294  array('integer'),
295  array($question_id)
296  );
297  if ($result->numRows())
298  {
299  $row = $ilDB->fetchAssoc($result);
300  return $row["maxchars"];
301  }
302  return 0;
303  }
304 
311  function getQuestionType()
312  {
313  return "SurveyTextQuestion";
314  }
315 
323  {
324  return "svy_qst_text";
325  }
326 
333  function &getWorkingDataFromUserInput($post_data)
334  {
335  $entered_value = $post_data[$this->getId() . "_text_question"];
336  $data = array();
337  if (strlen($entered_value))
338  {
339  array_push($data, array("textanswer" => $entered_value));
340  }
341  return $data;
342  }
343 
353  function checkUserInput($post_data, $survey_id)
354  {
355  $entered_value = $post_data[$this->getId() . "_text_question"];
356 
357  if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
358 
359  if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
360 
361  return "";
362  }
363 
364  function randomText($length)
365  {
366  $random= "";
367  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
368  $char_list .= "abcdefghijklmnopqrstuvwxyz";
369  $char_list .= "1234567890";
370  for($i = 0; $i < $length; $i++)
371  {
372  $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
373  if (!rand(0,5)) $random .= ' ';
374  }
375  return $random;
376  }
377 
383  public function saveRandomData($active_id)
384  {
385  global $ilDB;
386  // single response
387  $randomtext = $this->randomText(rand(25,100));
388  $next_id = $ilDB->nextId('svy_answer');
389  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
390  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
391  array($next_id, $this->getId(), $active_id, NULL, $randomtext, time())
392  );
393  }
394 
395  function saveUserInput($post_data, $active_id, $a_return = false)
396  {
397  global $ilDB;
398 
399  include_once "./Services/Utilities/classes/class.ilUtil.php";
400  $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
401  $maxchars = $this->getMaxChars();
402  if ($maxchars > 0)
403  {
404  $entered_value = substr($entered_value, 0, $maxchars);
405  }
406 
407  if($a_return)
408  {
409  return array(array("value"=>null, "textanswer"=>$entered_value));
410  }
411  if (strlen($entered_value) == 0) return;
412 
413  $next_id = $ilDB->nextId('svy_answer');
414  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_answer (answer_id, question_fi, active_fi, value, textanswer, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
415  array('integer', 'integer', 'integer', 'float', 'text', 'integer'),
416  array($next_id, $this->getId(), $active_id, NULL, (strlen($entered_value)) ? $entered_value : NULL, time())
417  );
418  }
419 
420  function &getCumulatedResults($survey_id, $nr_of_users, $finished_ids)
421  {
422  global $ilDB;
423 
424  $question_id = $this->getId();
425 
426  $result_array = array();
427  $cumulated = array();
428  $textvalues = array();
429 
430  $sql = "SELECT svy_answer.* FROM svy_answer".
431  " JOIN svy_finished ON (svy_finished.finished_id = svy_answer.active_fi)".
432  " WHERE svy_answer.question_fi = ".$ilDB->quote($question_id, "integer").
433  " AND svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer");
434  if($finished_ids)
435  {
436  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
437  }
438 
439  $result = $ilDB->query($sql);
440  while ($row = $ilDB->fetchAssoc($result))
441  {
442  $cumulated[$row["value"]]++;
443  array_push($textvalues, $row["textanswer"]);
444  }
445  asort($cumulated, SORT_NUMERIC);
446  end($cumulated);
447  $numrows = $result->numRows();
448  $result_array["USERS_ANSWERED"] = $result->numRows();
449  $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
450  $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
451  $result_array["textvalues"] = $textvalues;
452  return $result_array;
453  }
454 
464  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data, $export_label)
465  {
466  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
467  $worksheet =& $workbook->addWorksheet();
468  $rowcounter = 0;
469  switch ($export_label)
470  {
471  case 'label_only':
472  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
473  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->label));
474  break;
475  case 'title_only':
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  break;
479  default:
480  $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
481  $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
482  $rowcounter++;
483  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("label")), $format_bold);
484  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->label));
485  break;
486  }
487  $rowcounter++;
488  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
489  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
490  $rowcounter++;
491  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
492  $worksheet->writeString($rowcounter, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
493  $rowcounter++;
494  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
495  $worksheet->write($rowcounter, 1, $eval_data["USERS_ANSWERED"]);
496  $rowcounter++;
497  $worksheet->writeString($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
498  $worksheet->write($rowcounter, 1, $eval_data["USERS_SKIPPED"]);
499  $rowcounter++;
500 
501  $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
502  $textvalues = "";
503  if (is_array($eval_data["textvalues"]))
504  {
505  foreach ($eval_data["textvalues"] as $textvalue)
506  {
507  $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
508  }
509  }
510  }
511 
519  function addUserSpecificResultsData(&$a_array, &$resultset)
520  {
521  if (count($resultset["answers"][$this->getId()]))
522  {
523  foreach ($resultset["answers"][$this->getId()] as $key => $answer)
524  {
525  array_push($a_array, $answer["textanswer"]);
526  }
527  }
528  else
529  {
530  array_push($a_array, $this->getSkippedValue());
531  }
532  }
533 
541  function &getUserAnswers($survey_id, $finished_ids)
542  {
543  global $ilDB;
544 
545  $answers = array();
546 
547  $sql = "SELECT svy_answer.* FROM svy_answer, svy_finished".
548  " WHERE svy_finished.survey_fi = ".$ilDB->quote($survey_id, "integer").
549  " AND svy_answer.question_fi = ".$ilDB->quote($this->getId(), "integer").
550  " AND svy_finished.finished_id = svy_answer.active_fi";
551  if($finished_ids)
552  {
553  $sql .= " AND ".$ilDB->in("svy_finished.finished_id", $finished_ids, "", "integer");
554  }
555  $result = $ilDB->query($sql);
556  while ($row = $ilDB->fetchAssoc($result))
557  {
558  $answers[$row["active_fi"]] = $row["textanswer"];
559  }
560  return $answers;
561  }
562 
569  function importResponses($a_data)
570  {
571  foreach ($a_data as $id => $data)
572  {
573  if ($data["maxlength"] > 0)
574  {
575  $this->setMaxChars($data["maxlength"]);
576  }
577  if ($data["rows"] > 0)
578  {
579  $this->setTextHeight($data["rows"]);
580  }
581  if ($data["columns"] > 0)
582  {
583  $this->setTextWidth($data["columns"]);
584  }
585  }
586  }
587 
595  {
596  return FALSE;
597  }
598 
605  function getTextWidth()
606  {
607  return ($this->textwidth) ? $this->textwidth : NULL;
608  }
609 
616  function getTextHeight()
617  {
618  return ($this->textheight) ? $this->textheight : NULL;
619  }
620 
627  function setTextWidth($a_textwidth)
628  {
629  if ($a_textwidth < 1)
630  {
631  $this->textwidth = 50;
632  }
633  else
634  {
635  $this->textwidth = $a_textwidth;
636  }
637  }
638 
645  function setTextHeight($a_textheight)
646  {
647  if ($a_textheight < 1)
648  {
649  $this->textheight = 5;
650  }
651  else
652  {
653  $this->textheight = $a_textheight;
654  }
655  }
656 }
657 ?>