ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveyMultipleChoiceQuestionGUI.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.SurveyQuestionGUI.php";
25 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
26 
39 {
40 
50  $id = -1
51  )
52 
53  {
54  $this->SurveyQuestionGUI();
55  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyMultipleChoiceQuestion.php";
56  $this->object = new SurveyMultipleChoiceQuestion();
57  if ($id >= 0)
58  {
59  $this->object->loadFromDb($id);
60  }
61  }
62 
69  function writePostData($always = false)
70  {
71  $hasErrors = (!$always) ? $this->editQuestion(true) : false;
72  if (!$hasErrors)
73  {
74  $this->object->setTitle($_POST["title"]);
75  $this->object->setAuthor($_POST["author"]);
76  $this->object->setDescription($_POST["description"]);
77  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
78  $questiontext = $_POST["question"];
79  $this->object->setQuestiontext($questiontext);
80  $this->object->setObligatory(($_POST["obligatory"]) ? 1 : 0);
81  $this->object->setOrientation($_POST["orientation"]);
82 
83  $this->object->categories->flushCategories();
84 
85  foreach ($_POST['answers']['answer'] as $key => $value)
86  {
87  $this->object->getCategories()->addCategory($value);
88  }
89  return 0;
90  }
91  else
92  {
93  return 1;
94  }
95  }
96 
102  public function editQuestion($checkonly = FALSE)
103  {
104  $save = ((strcmp($this->ctrl->getCmd(), "save") == 0) || (strcmp($this->ctrl->getCmd(), "saveEdit") == 0)) ? TRUE : FALSE;
105 
106  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
107  $form = new ilPropertyFormGUI();
108  $form->setFormAction($this->ctrl->getFormAction($this));
109  $form->setTitle($this->lng->txt($this->getQuestionType()));
110  $form->setMultipart(FALSE);
111  $form->setTableWidth("100%");
112  $form->setId("multiplechoice");
113 
114  // title
115  $title = new ilTextInputGUI($this->lng->txt("title"), "title");
116  $title->setValue($this->object->getTitle());
117  $title->setRequired(TRUE);
118  $form->addItem($title);
119 
120  // author
121  $author = new ilTextInputGUI($this->lng->txt("author"), "author");
122  $author->setValue($this->object->getAuthor());
123  $author->setRequired(TRUE);
124  $form->addItem($author);
125 
126  // description
127  $description = new ilTextInputGUI($this->lng->txt("description"), "description");
128  $description->setValue($this->object->getDescription());
129  $description->setRequired(FALSE);
130  $form->addItem($description);
131 
132  // questiontext
133  $question = new ilTextAreaInputGUI($this->lng->txt("question"), "question");
134  $question->setValue($this->object->prepareTextareaOutput($this->object->getQuestiontext()));
135  $question->setRequired(TRUE);
136  $question->setRows(10);
137  $question->setCols(80);
138  $question->setUseRte(TRUE);
139  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
140  $question->setRteTags(ilObjAdvancedEditing::_getUsedHTMLTags("survey"));
141  $question->addPlugin("latex");
142  $question->addButton("latex");
143  $question->addButton("pastelatex");
144  $question->removePlugin("ibrowser");
145  $question->setRTESupport($this->object->getId(), "spl", "survey");
146  $form->addItem($question);
147 
148  // obligatory
149  $shuffle = new ilCheckboxInputGUI($this->lng->txt("obligatory"), "obligatory");
150  $shuffle->setValue(1);
151  $shuffle->setChecked($this->object->getObligatory());
152  $shuffle->setRequired(FALSE);
153  $form->addItem($shuffle);
154 
155  // orientation
156  $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
157  $orientation->setRequired(false);
158  $orientation->setValue($this->object->getOrientation());
159  $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
160  $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
161  $form->addItem($orientation);
162 
163  // Answers
164  include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
165  $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
166  $answers->setRequired(true);
167  $answers->setAllowMove(true);
168  if (!$this->object->getCategories()->getCategoryCount())
169  {
170  $this->object->getCategories()->addCategory("");
171  }
172  $answers->setValues($this->object->getCategories());
173  $form->addItem($answers);
174 
175  $form->addCommandButton("save", $this->lng->txt("save"));
176 
177  $errors = false;
178 
179  if ($save)
180  {
181  $form->setValuesByPost();
182  $errors = !$form->checkInput();
183  $form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
184  if ($errors) $checkonly = false;
185  }
186 
187  if (!$checkonly) $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
188  return $errors;
189  }
190 
194  public function addanswers()
195  {
196  $this->writePostData(true);
197  $position = key($_POST['cmd']['addanswers']);
198  $this->object->getCategories()->addCategoryAtPosition("", $position+1);
199  $this->editQuestion();
200  }
201 
205  public function removeanswers()
206  {
207  $this->writePostData(true);
208  $position = key($_POST['cmd']['removeanswers']);
209  $this->object->getCategories()->removeCategory($position);
210  $this->editQuestion();
211  }
212 
216  public function upanswers()
217  {
218  $this->writePostData(true);
219  $position = key($_POST['cmd']['upanswers']);
220  $this->object->getCategories()->moveCategoryUp($position);
221  $this->editQuestion();
222  }
223 
227  public function downanswers()
228  {
229  $this->writePostData(true);
230  $position = key($_POST['cmd']['downanswers']);
231  $this->object->getCategories()->moveCategoryDown($position);
232  $this->editQuestion();
233  }
234 
240  function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
241  {
242  $template = new ilTemplate("tpl.il_svy_out_mc.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
243  $template->setCurrentBlock("material");
244  $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
245  $template->parseCurrentBlock();
246  switch ($this->object->getOrientation())
247  {
248  case 0:
249  // vertical orientation
250  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
251  $category = $this->object->categories->getCategory($i);
252  $template->setCurrentBlock("mc_row");
253  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($category));
254  $template->setVariable("VALUE_MC", $i);
255  $template->setVariable("QUESTION_ID", $this->object->getId());
256  if (is_array($working_data))
257  {
258  foreach ($working_data as $value)
259  {
260  if (strlen($value["value"]))
261  {
262  if ($value["value"] == $i)
263  {
264  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
265  }
266  }
267  }
268  }
269  $template->parseCurrentBlock();
270  }
271  break;
272  case 1:
273  // horizontal orientation
274  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
275  {
276  $category = $this->object->categories->getCategory($i);
277  $template->setCurrentBlock("checkbox_col");
278  $template->setVariable("VALUE_MC", $i);
279  $template->setVariable("QUESTION_ID", $this->object->getId());
280  if (is_array($working_data))
281  {
282  foreach ($working_data as $value)
283  {
284  if (strlen($value["value"]))
285  {
286  if ($value["value"] == $i)
287  {
288  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
289  }
290  }
291  }
292  }
293  $template->parseCurrentBlock();
294  }
295  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
296  {
297  $category = $this->object->categories->getCategory($i);
298  $template->setCurrentBlock("text_col");
299  $template->setVariable("VALUE_MC", $i);
300  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($category));
301  $template->setVariable("QUESTION_ID", $this->object->getId());
302  $template->parseCurrentBlock();
303  }
304  break;
305  }
306 
307  $template->setCurrentBlock("question_data");
308  if (strcmp($error_message, "") != 0)
309  {
310  $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
311  }
312  if ($show_questiontext)
313  {
314  $questiontext = $this->object->getQuestiontext();
315  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
316  }
317  if (!$this->object->getObligatory($survey_id))
318  {
319  $template->setVariable("OBLIGATORY_TEXT", $this->lng->txt("survey_question_optional"));
320  }
321  if ($question_title)
322  {
323  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
324  }
325  $template->parseCurrentBlock();
326  return $template->get();
327  }
328 
334  function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null)
335  {
336  $template = new ilTemplate("tpl.il_svy_qpl_mc_printview.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
337  switch ($this->object->getOrientation())
338  {
339  case 0:
340  // vertical orientation
341  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
342  $category = $this->object->categories->getCategory($i);
343  $template->setCurrentBlock("mc_row");
344  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.gif")));
345  $template->setVariable("ALT_CHECKBOX", $this->lng->txt("unchecked"));
346  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt("unchecked"));
347  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($category));
348  $template->parseCurrentBlock();
349  }
350  break;
351  case 1:
352  // horizontal orientation
353  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
354  {
355  $category = $this->object->categories->getCategory($i);
356  $template->setCurrentBlock("checkbox_col");
357  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.gif")));
358  $template->setVariable("ALT_CHECKBOX", $this->lng->txt("unchecked"));
359  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt("unchecked"));
360  $template->parseCurrentBlock();
361  }
362  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
363  {
364  $category = $this->object->categories->getCategory($i);
365  $template->setCurrentBlock("text_col");
366  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($category));
367  $template->parseCurrentBlock();
368  }
369  break;
370  }
371 
372  if ($show_questiontext)
373  {
374  $questiontext = $this->object->getQuestiontext();
375  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
376  }
377  if (! $this->object->getObligatory($survey_id))
378  {
379  $template->setVariable("OBLIGATORY_TEXT", $this->lng->txt("survey_question_optional"));
380  }
381  if ($question_title)
382  {
383  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
384  }
385  $template->parseCurrentBlock();
386  return $template->get();
387  }
388 
394  function preview()
395  {
396  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_qpl_preview.html", "Modules/SurveyQuestionPool");
397  $question_output = $this->getWorkingForm();
398  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
399  }
400 
401  function setQuestionTabs()
402  {
403  $this->setQuestionTabsForClass("surveymultiplechoicequestiongui");
404  }
405 
414  function getCumulatedResultsDetails($survey_id, $counter)
415  {
416  if (count($this->cumulated) == 0)
417  {
418  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
419  $nr_of_users = ilObjSurvey::_getNrOfParticipants($survey_id);
420  $this->cumulated =& $this->object->getCumulatedResults($survey_id, $nr_of_users);
421  }
422 
423  $output = "";
424  include_once "./classes/class.ilTemplate.php";
425  $template = new ilTemplate("tpl.il_svy_svy_cumulated_results_detail.html", TRUE, TRUE, "Modules/Survey");
426 
427  $template->setCurrentBlock("detail_row");
428  $template->setVariable("TEXT_OPTION", $this->lng->txt("question"));
429  $questiontext = $this->object->getQuestiontext();
430  $template->setVariable("TEXT_OPTION_VALUE", $this->object->prepareTextareaOutput($questiontext, TRUE));
431  $template->parseCurrentBlock();
432  $template->setCurrentBlock("detail_row");
433  $template->setVariable("TEXT_OPTION", $this->lng->txt("question_type"));
434  $template->setVariable("TEXT_OPTION_VALUE", $this->lng->txt($this->getQuestionType()));
435  $template->parseCurrentBlock();
436  $template->setCurrentBlock("detail_row");
437  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_answered"));
438  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_ANSWERED"]);
439  $template->parseCurrentBlock();
440  $template->setCurrentBlock("detail_row");
441  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_skipped"));
442  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_SKIPPED"]);
443  $template->parseCurrentBlock();
444  $template->setCurrentBlock("detail_row");
445  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode"));
446  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE"]);
447  $template->parseCurrentBlock();
448  $template->setCurrentBlock("detail_row");
449  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode_nr_of_selections"));
450  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE_NR_OF_SELECTIONS"]);
451  $template->parseCurrentBlock();
452 
453  $template->setCurrentBlock("detail_row");
454  $template->setVariable("TEXT_OPTION", $this->lng->txt("categories"));
455  $categories = "";
456  if (is_array($this->cumulated["variables"]))
457  {
458  foreach ($this->cumulated["variables"] as $key => $value)
459  {
460  $categories .= "<li>" . $this->lng->txt("title") . ":" . "<span class=\"bold\">" . $value["title"] . "</span><br />" .
461  $this->lng->txt("category_nr_selected") . ": " . "<span class=\"bold\">" . $value["selected"] . "</span><br />" .
462  $this->lng->txt("percentage_of_selections") . ": " . "<span class=\"bold\">" . sprintf("%.2f", 100*$value["percentage"]) . "</span></li>";
463  }
464  }
465  $categories = "<ol>$categories</ol>";
466  $template->setVariable("TEXT_OPTION_VALUE", $categories);
467  $template->parseCurrentBlock();
468 
469  // display chart for multiple choice question for array $eval["variables"]
470  $template->setCurrentBlock("chart");
471  $template->setVariable("TEXT_CHART", $this->lng->txt("chart"));
472  $template->setVariable("ALT_CHART", $data["title"] . "( " . $this->lng->txt("chart") . ")");
473 
474  $charturl = "";
475  include_once "./Services/Administration/classes/class.ilSetting.php";
476  $surveySetting = new ilSetting("survey");
477  if ($surveySetting->get("googlechart") == 1)
478  {
479  $chartcolors = array("2A4BD7", "9DAFFF", "1D6914", "81C57A", "814A19", "E9DEBB", "8126C0", "AD2323", "29D0D0", "FFEE33", "FF9233", "FFCDF3", "A0A0A0", "575757", "000000");
480  $selections = array();
481  $values = array();
482  $maxselection = 0;
483  foreach ($this->cumulated["variables"] as $val)
484  {
485  if ($val["selected"] > $maxselection) $maxselection = $val["selected"];
486  array_push($selections, $val["selected"]);
487  array_push($values, str_replace(" ", "+", $val["title"]));
488  }
489  $chartwidth = 800;
490  if ($maxselection % 2 == 0)
491  {
492  $selectionlabels = "0|" . ($maxselection / 2) . "|$maxselection";
493  }
494  else
495  {
496  $selectionlabels = "0|$maxselection";
497  }
498  $charturl = "http://chart.apis.google.com/chart?chco=" . implode("|", array_slice($chartcolors, 0, count($values))). "&cht=bvs&chs=" . $chartwidth . "x250&chd=t:" . implode(",", $selections) . "&chds=0,$maxselection&chxt=y,y&chxl=0:|".$selectionlabels."|1:||".str_replace(" ", "+", $this->lng->txt("mode_nr_of_selections"))."|" . "&chxr=1,0,$maxselection&chtt=" . str_replace(" ", "+", $this->object->getTitle()) . "&chbh=20," . (round($chartwidth/count($values))-25) . "&chdl=" . implode("|", $values) . "&chdlp=b";
499  }
500  else
501  {
502  $this->ctrl->setParameterByClass("ilsurveyevaluationgui", "survey", $survey_id);
503  $this->ctrl->setParameterByClass("ilsurveyevaluationgui", "question", $this->object->getId());
504  $charturl = $this->ctrl->getLinkTargetByClass("ilsurveyevaluationgui", "outChart");
505  }
506  $template->setVariable("CHART", $charturl);
507  $template->parseCurrentBlock();
508 
509  $template->setVariable("QUESTION_TITLE", "$counter. ".$this->object->getTitle());
510  return $template->get();
511  }
512 
513 
514 }
515 ?>