ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.SurveySingleChoiceQuestionGUI.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
24include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestionGUI.php";
25
38{
39 protected function initObject()
40 {
41 include_once "./Modules/SurveyQuestionPool/classes/class.SurveySingleChoiceQuestion.php";
42 $this->object = new SurveySingleChoiceQuestion();
43 }
44
45
46 //
47 // EDITOR
48 //
49
50 public function setQuestionTabs()
51 {
52 $this->setQuestionTabsForClass("surveysinglechoicequestiongui");
53 }
54
55 protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
56 {
57 // orientation
58 $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
59 $orientation->setRequired(false);
60 $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
61 $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
62 $orientation->addOption(new ilRadioOption($this->lng->txt('combobox'), 2));
63 $a_form->addItem($orientation);
64
65 // Answers
66 include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
67 $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
68 $answers->setRequired(false);
69 $answers->setAllowMove(true);
70 $answers->setShowWizard(true);
71 $answers->setShowSavePhrase(true);
72 $answers->setUseOtherAnswer(true);
73 $answers->setShowNeutralCategory(true);
74 $answers->setNeutralCategoryTitle($this->lng->txt('svy_neutral_answer'));
75 $answers->setDisabledScale(false);
76 $a_form->addItem($answers);
77
78 // values
79 $orientation->setValue($this->object->getOrientation());
80 if (!$this->object->getCategories()->getCategoryCount()) {
81 $this->object->getCategories()->addCategory("");
82 }
83 $answers->setValues($this->object->getCategories());
84 }
85
86 protected function importEditFormValues(ilPropertyFormGUI $a_form)
87 {
88 $this->log->debug("importing edit values");
89
90 $this->object->setOrientation($a_form->getInput("orientation"));
91
92 $this->object->categories->flushCategories();
93 foreach ($_POST['answers']['answer'] as $key => $value) {
94 if (strlen($value)) {
95 $this->object->getCategories()->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
96 }
97 }
98 if (strlen($_POST['answers']['neutral'])) {
99 $this->object->getCategories()->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
100 }
101 }
102
103 public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
104 {
105 if (is_array($a_working_data)) {
106 $user_answer = $a_working_data[0];
107 }
108
109 $options = array();
110 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
111 $cat = $this->object->categories->getCategory($i);
112 $value = ($cat->scale) ? ($cat->scale - 1) : $i;
113
114 $checked = "unchecked";
115 $text = null;
116 if (is_array($a_working_data) &&
117 is_array($user_answer)) {
118 if ($value == $user_answer["value"]) {
119 $checked = "checked";
120 if ($user_answer["textanswer"]) {
121 $text = $user_answer["textanswer"];
122 }
123 }
124 }
125
126 // "other" options have to be last or horizontal will be screwed
127 $idx = $cat->other . "_" . $value;
128
129 if (!$a_only_user_anwers || $checked == "checked") {
130 $options[$idx] = array(
131 "value" => $value
132 ,"title" => trim($cat->title)
133 ,"other" => (bool) $cat->other
134 ,"checked" => $checked
135 ,"textanswer" => $text
136 );
137 }
138
139 ksort($options);
140 }
141
142 return array_values($options);
143 }
144
150 public function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
151 {
152 $options = $this->getParsedAnswers($a_working_data);
153
154 // rendering
155
156 $template = new ilTemplate("tpl.il_svy_qpl_sc_printview.html", true, true, "Modules/SurveyQuestionPool");
157 switch ($this->object->orientation) {
158 case 0:
159 // vertical orientation
160 foreach ($options as $option) {
161 if ($option["other"]) {
162 $template->setCurrentBlock("other_row");
163 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
164 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
165 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
166 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
167 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
168 ? ilUtil::prepareFormOutput($option["textanswer"])
169 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
170 $template->parseCurrentBlock();
171 } else {
172 $template->setCurrentBlock("row");
173 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
174 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
175 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
176 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
177 $template->parseCurrentBlock();
178 }
179 }
180 break;
181 case 1:
182 // horizontal orientation
183 foreach ($options as $option) {
184 $template->setCurrentBlock("radio_col");
185 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_" . $option["checked"] . ".png")));
186 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
187 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
188 $template->parseCurrentBlock();
189 }
190 foreach ($options as $option) {
191 if ($option["other"]) {
192 $template->setCurrentBlock("other_text_col");
193 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
194 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
195 ? ilUtil::prepareFormOutput($option["textanswer"])
196 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
197 $template->parseCurrentBlock();
198 } else {
199 $template->setCurrentBlock("text_col");
200 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
201 $template->parseCurrentBlock();
202 }
203 }
204 break;
205 case 2:
206 foreach ($options as $option) {
207 $template->setCurrentBlock("comborow");
208 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
209 $template->setVariable("VALUE_SC", $option["value"]);
210 if ($option["checked"] == "checked") {
211 $template->setVariable("SELECTED_SC", ' selected="selected"');
212 }
213 $template->parseCurrentBlock();
214 }
215 $template->setCurrentBlock("combooutput");
216 $template->setVariable("QUESTION_ID", $this->object->getId());
217 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
218 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
219 $template->parseCurrentBlock();
220 break;
221 }
222 if ($question_title) {
223 $template->setVariable("QUESTION_TITLE", $this->getPrintViewQuestionTitle($question_title));
224 }
225 if ($show_questiontext) {
227 }
228 $template->parseCurrentBlock();
229 return $template->get();
230 }
231
232
233 //
234 // EXECUTION
235 //
236
242 public function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
243 {
244 $template = new ilTemplate("tpl.il_svy_out_sc.html", true, true, "Modules/SurveyQuestionPool");
245 $template->setCurrentBlock("material");
246 $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
247 $template->parseCurrentBlock();
248 switch ($this->object->orientation) {
249 case 0:
250 // vertical orientation
251 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
252 $cat = $this->object->categories->getCategory($i);
253
254 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
255 $this->log->debug("Vertical orientation - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
256
257 if ($cat->other) {
258 $template->setCurrentBlock("other_row");
259 if (strlen($cat->title)) {
260 $template->setVariable("OTHER_LABEL", $cat->title);
261 }
262 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
263 $template->setVariable("QUESTION_ID", $this->object->getId());
264 if (is_array($working_data)) {
265 foreach ($working_data as $value) {
266 if (strlen($value["value"])) {
267 if ($value["value"] == $cat->scale - 1) {
268 if (strlen($value['textanswer'])) {
269 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
270 }
271 if (!$value['uncheck']) {
272 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
273 }
274 }
275 }
276 }
277 }
278 $template->parseCurrentBlock();
279 } else {
280 $template->setCurrentBlock("row");
281 if ($cat->neutral) {
282 $template->setVariable('ROWCLASS', ' class="neutral"');
283 }
284 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
285 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
286 $template->setVariable("QUESTION_ID", $this->object->getId());
287 if (is_array($working_data)) {
288 foreach ($working_data as $value) {
289 if (strcmp($value["value"], "") != 0) {
290 if ($value["value"] == $cat->scale - 1) {
291 if (!$value['uncheck']) {
292 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
293 }
294 }
295 }
296 }
297 }
298 $template->parseCurrentBlock();
299 }
300 $template->touchBlock('outer_row');
301 }
302 break;
303 case 1:
304 // horizontal orientation
305 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
306 $cat = $this->object->categories->getCategory($i);
307
308 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
309 $this->log->debug("Horizontal orientation - Original NEUTRAL scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
310
311 $template->setCurrentBlock("radio_col");
312 if ($cat->neutral) {
313 $template->setVariable('COLCLASS', ' neutral');
314 }
315 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
316 $template->setVariable("QUESTION_ID", $this->object->getId());
317 if (is_array($working_data)) {
318 foreach ($working_data as $value) {
319 if (strcmp($value["value"], "") != 0) {
320 if ($value["value"] == $cat->scale - 1) {
321 if (!$value['uncheck']) {
322 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
323 }
324 }
325 }
326 }
327 }
328 $template->parseCurrentBlock();
329 }
330 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
331 $cat = $this->object->categories->getCategory($i);
332
333 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
334 $this->log->debug("Horizontal orientation - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
335
336 if ($cat->other) {
337 $template->setCurrentBlock("text_other_col");
338 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
339 $template->setVariable("QUESTION_ID", $this->object->getId());
340 if (strlen($cat->title)) {
341 $template->setVariable("OTHER_LABEL", $cat->title);
342 }
343 if (is_array($working_data)) {
344 foreach ($working_data as $value) {
345 if (strlen($value["value"])) {
346 if ($value["value"] == $cat->scale - 1 && strlen($value['textanswer'])) {
347 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
348 }
349 }
350 }
351 }
352 $template->parseCurrentBlock();
353 } else {
354 $template->setCurrentBlock("text_col");
355 if ($cat->neutral) {
356 $template->setVariable('COLCLASS', ' neutral');
357 }
358 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
359 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
360 $template->setVariable("QUESTION_ID", $this->object->getId());
361 $template->parseCurrentBlock();
362 }
363 $template->touchBlock('text_outer_col');
364 }
365 break;
366 case 2:
367 // combobox output
368 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++) {
369 $cat = $this->object->categories->getCategory($i);
370
371 $debug_scale = ($cat->scale) ? ($cat->scale - 1) : $i;
372 $this->log->debug("Combobox - Original scale = " . $cat->scale . " If(scale) scale -1 else i. The new scale value is = " . $debug_scale);
373
374 $template->setCurrentBlock("comborow");
375 $template->setVariable("TEXT_SC", $cat->title);
376 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
377 if (is_array($working_data)) {
378 if (strcmp($working_data[0]["value"], "") != 0) {
379 if ($working_data[0]["value"] == $cat->scale - 1) {
380 $template->setVariable("SELECTED_SC", " selected=\"selected\"");
381 }
382 }
383 }
384 $template->parseCurrentBlock();
385 }
386 $template->setCurrentBlock("combooutput");
387 $template->setVariable("QUESTION_ID", $this->object->getId());
388 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
389 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
390 $template->parseCurrentBlock();
391 break;
392 }
393 if ($question_title) {
394 $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
395 }
396 $template->setCurrentBlock("question_data");
397 if (strcmp($error_message, "") != 0) {
398 $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
399 }
400 if ($show_questiontext) {
402 }
403 $template->parseCurrentBlock();
404 return $template->get();
405 }
406}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
Basic class for all survey question types.
getPrintViewQuestionTitle($question_title=1)
getMaterialOutput()
Creates the HTML output of the question material(s)
SingleChoice survey question GUI representation.
getParsedAnswers(array $a_working_data=null, $a_only_user_anwers=false)
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.
This class represents a survey question category wizard 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.
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 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
$template
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
$text
Definition: errorreport.php:18