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