ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
38 {
39  protected function initObject()
40  {
41  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyMultipleChoiceQuestion.php";
42  $this->object = new SurveyMultipleChoiceQuestion();
43  }
44 
45  //
46  // EDITOR
47  //
48 
49  public function setQuestionTabs()
50  {
51  $this->setQuestionTabsForClass("surveymultiplechoicequestiongui");
52  }
53 
54  protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
55  {
56  // orientation
57  $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
58  $orientation->setRequired(false);
59  $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
60  $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
61  $a_form->addItem($orientation);
62 
63  // minimum answers
64  $minanswers = new ilCheckboxInputGUI($this->lng->txt("use_min_answers"), "use_min_answers");
65  $minanswers->setValue(1);
66  $minanswers->setOptionTitle($this->lng->txt("use_min_answers_option"));
67  $minanswers->setRequired(false);
68 
69  $nranswers = new ilNumberInputGUI($this->lng->txt("nr_min_answers"), "nr_min_answers");
70  $nranswers->setSize(5);
71  $nranswers->setDecimals(0);
72  $nranswers->setRequired(false);
73  $nranswers->setMinValue(1);
74  $minanswers->addSubItem($nranswers);
75 
76  $nrmaxanswers = new ilNumberInputGUI($this->lng->txt("nr_max_answers"), "nr_max_answers");
77  $nrmaxanswers->setSize(5);
78  $nrmaxanswers->setDecimals(0);
79  $nrmaxanswers->setRequired(false);
80  $nrmaxanswers->setMinValue(1);
81  $minanswers->addSubItem($nrmaxanswers);
82 
83  $a_form->addItem($minanswers);
84 
85  // Answers
86  include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
87  $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
88  $answers->setRequired(false);
89  $answers->setAllowMove(true);
90  $answers->setShowWizard(false);
91  $answers->setShowSavePhrase(false);
92  $answers->setUseOtherAnswer(true);
93  $answers->setShowNeutralCategory(true);
94  $answers->setNeutralCategoryTitle($this->lng->txt('svy_neutral_answer'));
95  $answers->setDisabledScale(false);
96  $a_form->addItem($answers);
97 
98 
99  // values
100  $orientation->setValue($this->object->getOrientation());
101  $minanswers->setChecked($this->object->use_min_answers);
102  $nranswers->setValue($this->object->nr_min_answers);
103  $nrmaxanswers->setValue($this->object->nr_max_answers);
104  if (!$this->object->getCategories()->getCategoryCount()) {
105  $this->object->getCategories()->addCategory("");
106  }
107  $answers->setValues($this->object->getCategories());
108  }
109 
110  protected function validateEditForm(ilPropertyFormGUI $a_form)
111  {
112  if ($a_form->getInput("use_min_answers")) {
113  // #13927 - see importEditFormValues()
114  $cnt_answers = 0;
115  foreach ($_POST['answers']['answer'] as $key => $value) {
116  if (strlen($value)) {
117  $cnt_answers++;
118  }
119  }
120  if (strlen($_POST['answers']['neutral'])) {
121  $cnt_answers++;
122  }
123  /* this would be the DB-values
124  $cnt_answers = $a_form->getItemByPostVar("answers");
125  $cnt_answers = $cnt_answers->getCategoryCount();
126  */
127  $min_anwers = $a_form->getInput("nr_min_answers");
128  $max_anwers = $a_form->getInput("nr_max_answers");
129 
130  if ($min_anwers &&
131  $min_anwers > $cnt_answers) {
132  $a_form->getItemByPostVar("nr_min_answers")->setAlert($this->lng->txt('err_minvalueganswers'));
133  $errors = true;
134  }
135  if ($max_anwers > 0 &&
136  ($max_anwers > $cnt_answers || $max_anwers < $min_anwers)) {
137  $a_form->getItemByPostVar("nr_max_answers")->setAlert($this->lng->txt('err_maxvaluegeminvalue'));
138  $errors = true;
139  }
140  }
141 
142  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
143  return !$errors;
144  }
145 
146  protected function importEditFormValues(ilPropertyFormGUI $a_form)
147  {
148  $this->object->setOrientation($a_form->getInput("orientation"));
149  $this->object->use_other_answer = ($a_form->getInput('use_other_answer')) ? 1 : 0;
150  $this->object->other_answer_label = $this->object->use_other_answer ? $a_form->getInput('other_answer_label') : null;
151  $this->object->use_min_answers = ($a_form->getInput('use_min_answers')) ? true : false;
152  $this->object->nr_min_answers = ($a_form->getInput('nr_min_answers') > 0) ? $a_form->getInput('nr_min_answers') : null;
153  $this->object->nr_max_answers = ($a_form->getInput('nr_max_answers') > 0) ? $a_form->getInput('nr_max_answers') : null;
154  $this->object->label = $a_form->getInput('label');
155 
156  $this->object->categories->flushCategories();
157 
158  foreach ($_POST['answers']['answer'] as $key => $value) {
159  if (strlen($value)) {
160  $this->object->getCategories()->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
161  }
162  }
163  if (strlen($_POST['answers']['neutral'])) {
164  $this->object->getCategories()->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
165  }
166  }
167 
168  public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
169  {
170  if (is_array($a_working_data)) {
171  $user_answers = $a_working_data;
172  }
173 
174  $options = array();
175  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
176  $cat = $this->object->categories->getCategory($i);
177  $value = ($cat->scale) ? ($cat->scale - 1) : $i;
178 
179  $checked = "unchecked";
180  $text = null;
181  if (is_array($a_working_data)) {
182  foreach ($user_answers as $user_answer) {
183  if ($value == $user_answer["value"]) {
184  $checked = "checked";
185  if ($user_answer["textanswer"]) {
186  $text = $user_answer["textanswer"];
187  }
188  break;
189  }
190  }
191  }
192 
193  // "other" options have to be last or horizontal will be screwed
194  $idx = $cat->other . "_" . $value;
195 
196  if (!$a_only_user_anwers || $checked == "checked") {
197  $options[$idx] = array(
198  "value" => $value
199  ,"title" => trim($cat->title)
200  ,"other" => (bool) $cat->other
201  ,"checked" => $checked
202  ,"textanswer" => $text
203  );
204  }
205 
206  ksort($options);
207  }
208 
209  return array_values($options);
210  }
211 
217  public function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
218  {
219  $options = $this->getParsedAnswers($a_working_data);
220 
221  $template = new ilTemplate("tpl.il_svy_qpl_mc_printview.html", true, true, "Modules/SurveyQuestionPool");
222  switch ($this->object->getOrientation()) {
223  case 0:
224  // vertical orientation
225  foreach ($options as $option) {
226  if ($option["other"]) {
227  $template->setCurrentBlock("other_row");
228  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_" . $option["checked"] . ".png")));
229  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
230  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
231  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
232  $template->setVariable("OTHER_ANSWER", $option["textanswer"]
233  ? ilUtil::prepareFormOutput($option["textanswer"])
234  : "&nbsp;");
235  $template->parseCurrentBlock();
236  } else {
237  $template->setCurrentBlock("mc_row");
238  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_" . $option["checked"] . ".png")));
239  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
240  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
241  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($option["title"]));
242  $template->parseCurrentBlock();
243  }
244  }
245  break;
246  case 1:
247  // horizontal orientation
248  foreach ($options as $option) {
249  $template->setCurrentBlock("checkbox_col");
250  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_" . $option["checked"] . ".png")));
251  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
252  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
253  $template->parseCurrentBlock();
254  }
255  foreach ($options as $option) {
256  if ($option["other"]) {
257  $template->setCurrentBlock("other_text_col");
258  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
259  $template->setVariable("OTHER_ANSWER", $option["textanswer"]
260  ? ilUtil::prepareFormOutput($option["textanswer"])
261  : "&nbsp;");
262  $template->parseCurrentBlock();
263  } else {
264  $template->setCurrentBlock("text_col");
265  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($option["title"]));
266  $template->parseCurrentBlock();
267  }
268  }
269  break;
270  }
271 
272  if ($this->object->use_min_answers) {
273  $template->setCurrentBlock('min_max_msg');
274  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0) {
275  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
276  } elseif ($this->object->nr_min_answers > 0) {
277  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
278  } elseif ($this->object->nr_max_answers > 0) {
279  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
280  }
281  $template->parseCurrentBlock();
282  }
283  if ($show_questiontext) {
284  $this->outQuestionText($template);
285  }
286  if ($question_title) {
287  $template->setVariable("QUESTION_TITLE", $this->getPrintViewQuestionTitle($question_title));
288  }
289  $template->parseCurrentBlock();
290  return $template->get();
291  }
292 
293 
294  //
295  // EXECUTION
296  //
297 
303  public function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
304  {
305  $template = new ilTemplate("tpl.il_svy_out_mc.html", true, true, "Modules/SurveyQuestionPool");
306  $template->setCurrentBlock("material");
307  $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
308  $template->parseCurrentBlock();
309  switch ($this->object->getOrientation()) {
310  case 0:
311  // vertical orientation
312  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
313  $cat = $this->object->categories->getCategory($i);
314  if ($cat->other) {
315  $template->setCurrentBlock("other_row");
316  if (strlen($cat->title)) {
317  $template->setVariable("OTHER_LABEL", $cat->title);
318  }
319  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
320  $template->setVariable("QUESTION_ID", $this->object->getId());
321  if (is_array($working_data)) {
322  foreach ($working_data as $value) {
323  if (strlen($value["value"])) {
324  if ($value["value"] == $cat->scale-1) {
325  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
326  if (!$value['uncheck']) {
327  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
328  }
329  }
330  }
331  }
332  }
333  $template->parseCurrentBlock();
334  } else {
335  $template->setCurrentBlock("mc_row");
336  if ($cat->neutral) {
337  $template->setVariable('ROWCLASS', ' class="neutral"');
338  }
339  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
340  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
341  $template->setVariable("QUESTION_ID", $this->object->getId());
342  if (is_array($working_data)) {
343  foreach ($working_data as $value) {
344  if (strlen($value["value"])) {
345  if ($value["value"] == $cat->scale-1) {
346  if (!$value['uncheck']) {
347  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
348  }
349  }
350  }
351  }
352  }
353  $template->parseCurrentBlock();
354  }
355  $template->touchBlock('outer_row');
356  }
357  break;
358  case 1:
359  // horizontal orientation
360 
361  // #15477 - reverting the categorizing of answers
362  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
363  $cat = $this->object->categories->getCategory($i);
364 
365  // checkbox
366  $template->setCurrentBlock("checkbox_col");
367  if ($cat->neutral) {
368  $template->setVariable('COLCLASS', ' neutral');
369  }
370  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
371  $template->setVariable("QUESTION_ID", $this->object->getId());
372  if (is_array($working_data)) {
373  foreach ($working_data as $value) {
374  if (strlen($value["value"])) {
375  if ($value["value"] == $cat->scale-1) {
376  if (!$value['uncheck']) {
377  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
378  }
379  }
380  }
381  }
382  }
383  $template->parseCurrentBlock();
384 
385  // answer & input
386  if ($cat->other) {
387  $template->setCurrentBlock("text_other_col");
388  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
389  $template->setVariable("QUESTION_ID", $this->object->getId());
390  if (strlen($cat->title)) {
391  $template->setVariable("OTHER_LABEL", $cat->title);
392  }
393  if (is_array($working_data)) {
394  foreach ($working_data as $value) {
395  if (strlen($value["value"])) {
396  if ($value["value"] == $cat->scale-1) {
397  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
398  }
399  }
400  }
401  }
402  $template->parseCurrentBlock();
403  }
404  // answer
405  else {
406  $template->setCurrentBlock("text_col");
407  if ($cat->neutral) {
408  $template->setVariable('COLCLASS', ' neutral');
409  }
410  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
411  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
412  $template->setVariable("QUESTION_ID", $this->object->getId());
413  $template->parseCurrentBlock();
414  }
415  $template->touchBlock('text_outer_col');
416  }
417  break;
418  }
419 
420  $template->setCurrentBlock("question_data");
421  if ($this->object->use_min_answers) {
422  $template->setCurrentBlock('min_max_msg');
423  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0) {
424  if ($this->object->nr_min_answers == $this->object->nr_max_answers) {
425  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_exact_answers'), $this->object->nr_min_answers));
426  } else {
427  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
428  }
429  } elseif ($this->object->nr_min_answers > 0) {
430  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
431  } elseif ($this->object->nr_max_answers > 0) {
432  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
433  }
434  $template->parseCurrentBlock();
435  }
436  if (strcmp($error_message, "") != 0) {
437  $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
438  }
439  if ($show_questiontext) {
440  $this->outQuestionText($template);
441  }
442  if ($question_title) {
443  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
444  }
445  $template->parseCurrentBlock();
446  return $template->get();
447  }
448 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
This class represents an option in a radio group.
getMaterialOutput()
Creates the HTML output of the question material(s)
getItemByPostVar($a_post_var)
Get Item by POST variable.
$template
This class represents a property form user interface.
getWorkingForm($working_data="", $question_title=1, $show_questiontext=1, $error_message="", $survey_id=null)
Creates the question output form for the learner.
This class represents a checkbox property in a property form.
addItem($a_item)
Add Item (Property, SectionHeader).
getPrintView($question_title=1, $show_questiontext=1, $survey_id=null, array $a_working_data=null)
Creates a HTML representation of the question.
This class represents a property in a property form.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
This class represents a number property in a property form.
MultipleChoice survey question GUI representation.
setValue($a_value)
Set Value.
special template class to simplify handling of ITX/PEAR
$text
Definition: errorreport.php:18
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
static getHtmlPath($relative_path)
get url of path
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
This class represents a survey question category wizard property in a property form.
setSize($a_size)
Set Size.
$errors
Definition: index.php:6
Create new PHPExcel object
obj_idprivate
$i
Definition: disco.tpl.php:19
getPrintViewQuestionTitle($question_title=1)
getParsedAnswers(array $a_working_data=null, $a_only_user_anwers=false)
Basic class for all survey question types.
$key
Definition: croninfo.php:18
$_POST["username"]
setRequired($a_required)
Set Required.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20