1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
5
6/**
7* This class represents a single choice wizard property in a property form.
8*
9* @author Helmut Schottmüller <ilias@aurealis.de>
10* @version $Id$
11* @ingroup	ServicesForm
12*/
13class ilAnswerWizardInputGUI extends ilTextInputGUI
14{
15    protected $values = array();
16    protected $allowMove = false;
17    protected $allowAddRemove = true;
18    protected $singleline = true;
19    protected $qstObject = null;
20    protected $minvalue = false;
21    protected $minvalueShouldBeGreater = false;
22
23    /**
24    * Constructor
25    *
26    * @param	string	$a_title	Title
27    * @param	string	$a_postvar	Post Variable
28    */
29    public function __construct($a_title = "", $a_postvar = "")
30    {
31        parent::__construct($a_title, $a_postvar);
32        $this->setSize('25');
33        $this->validationRegexp = "";
34    }
35
36    /**
37    * Set Value.
38    *
39    * @param	string	$a_value	Value
40    */
41    public function setValue($a_value)
42    {
43        $this->values = array();
44        if (is_array($a_value)) {
45            if (is_array($a_value['answer'])) {
46                foreach ($a_value['answer'] as $index => $value) {
47                    include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
48                    $answer = new ASS_AnswerBinaryStateImage($value, $a_value['points'][$index], $index, 1, $a_value['imagename'][$index]);
49                    array_push($this->values, $answer);
50                }
51            }
52        }
53    }
54
55    /**
56    * Set Values
57    *
58    * @param	array	$a_value	Value
59    */
60    public function setValues($a_values)
61    {
62        $this->values = $a_values;
63    }
64
65    /**
66    * Get Values
67    *
68    * @return	array	Values
69    */
70    public function getValues()
71    {
72        return $this->values;
73    }
74
75    /**
76    * Set singleline
77    *
78    * @param	boolean	$a_value	Value
79    */
80    public function setSingleline($a_value)
81    {
82        $this->singleline = $a_value;
83    }
84
85    /**
86    * Get singleline
87    *
88    * @return	boolean	Value
89    */
90    public function getSingleline()
91    {
92        return $this->singleline;
93    }
94
95    /**
96    * Set question object
97    *
98    * @param	object	$a_value	test object
99    */
100    public function setQuestionObject($a_value)
101    {
102        $this->qstObject = &$a_value;
103    }
104
105    /**
106    * Get question object
107    *
108    * @return	object	Value
109    */
110    public function getQuestionObject()
111    {
112        return $this->qstObject;
113    }
114
115    /**
116    * Set allow move
117    *
118    * @param	boolean	$a_allow_move Allow move
119    */
120    public function setAllowMove($a_allow_move)
121    {
122        $this->allowMove = $a_allow_move;
123    }
124
125    /**
126    * Get allow move
127    *
128    * @return	boolean	Allow move
129    */
130    public function getAllowMove()
131    {
132        return $this->allowMove;
133    }
134
135    /**
136     * @return bool
137     */
138    public function isAddRemoveAllowed()
139    {
140        return $this->allowAddRemove;
141    }
142
143    /**
144     * @param bool $allowAddRemove
145     */
146    public function setAllowAddRemove($allowAddRemove)
147    {
148        $this->allowAddRemove = $allowAddRemove;
149    }
150
151    /**
152     * Set minvalueShouldBeGreater
153     *
154     * @param	boolean	$a_bool	true if the minimum value should be greater than minvalue
155     */
156    public function setMinvalueShouldBeGreater($a_bool)
157    {
158        $this->minvalueShouldBeGreater = $a_bool;
159    }
160
161    /**
162     * Get minvalueShouldBeGreater
163     *
164     * @return	boolean	true if the minimum value should be greater than minvalue
165     */
166    public function minvalueShouldBeGreater()
167    {
168        return $this->minvalueShouldBeGreater;
169    }
170    /**
171     * Set Minimum Value.
172     *
173     * @param	float	$a_minvalue	Minimum Value
174     */
175    public function setMinValue($a_minvalue)
176    {
177        $this->minvalue = $a_minvalue;
178    }
179
180    /**
181     * Get Minimum Value.
182     *
183     * @return	float	Minimum Value
184     */
185    public function getMinValue()
186    {
187        return $this->minvalue;
188    }
189    /**
190    * Check input, strip slashes etc. set alert, if input is not ok.
191    *
192    * @return	boolean		Input ok, true/false
193    */
194    public function checkInput()
195    {
196        global $DIC;
197        $lng = $DIC['lng'];
198        $this->sanitizeSuperGlobalSubmitValue();
199        $foundvalues = $_POST[$this->getPostVar()];
200        if (is_array($foundvalues)) {
201            // check answers
202            if (is_array($foundvalues['answer'])) {
203                foreach ($foundvalues['answer'] as $aidx => $answervalue) {
204                    if ((strlen($answervalue)) == 0) {
205                        $this->setAlert($lng->txt("msg_input_is_required"));
206                        return false;
207                    }
208                }
209            }
210            // check points
211            $max = 0;
212            if (is_array($foundvalues['points'])) {
213                foreach ($foundvalues['points'] as $points) {
214                    if ($points > $max) {
215                        $max = $points;
216                    }
217                    if (((strlen($points)) == 0) || (!is_numeric($points))) {
218                        $this->setAlert($lng->txt("form_msg_numeric_value_required"));
219                        return false;
220                    }
221                    if ($this->minvalueShouldBeGreater()) {
222                        if (trim($points) != "" &&
223                            $this->getMinValue() !== false &&
224                            $points <= $this->getMinValue()) {
225                            $this->setAlert($lng->txt("form_msg_value_too_low"));
226
227                            return false;
228                        }
229                    } else {
230                        if (trim($points) != "" &&
231                            $this->getMinValue() !== false &&
232                            $points < $this->getMinValue()) {
233                            $this->setAlert($lng->txt("form_msg_value_too_low"));
234
235                            return false;
236                        }
237                    }
238                }
239            }
240            if ($max == 0) {
241                $this->setAlert($lng->txt("enter_enough_positive_points"));
242                return false;
243            }
244        } else {
245            $this->setAlert($lng->txt("msg_input_is_required"));
246            return false;
247        }
248
249        return $this->checkSubItemsInput();
250    }
251
252    /**
253    * Insert property html
254    *
255    * @return	int	Size
256    */
257    public function insert($a_tpl)
258    {
259        global $DIC;
260        $lng = $DIC['lng'];
261
262        $tpl = new ilTemplate($this->getTemplate(), true, true, "Modules/TestQuestionPool");
263        $i = 0;
264        foreach ($this->values as $value) {
265            if ($this->getSingleline()) {
266                if (is_object($value)) {
267                    $tpl->setCurrentBlock("prop_text_propval");
268                    $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
269                    $tpl->parseCurrentBlock();
270                    $tpl->setCurrentBlock("prop_points_propval");
271                    $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
272                    $tpl->parseCurrentBlock();
273                }
274                $tpl->setCurrentBlock('singleline');
275                $tpl->setVariable("SIZE", $this->getSize());
276                $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
277                $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
278                $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
279                $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
280                if ($this->getDisabled()) {
281                    $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
282                }
283                $tpl->parseCurrentBlock();
284            } elseif (!$this->getSingleline()) {
285                if (is_object($value)) {
286                    $tpl->setCurrentBlock("prop_points_propval");
287                    $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
288                    $tpl->parseCurrentBlock();
289                }
290                $tpl->setCurrentBlock('multiline');
291                $tpl->setVariable("PROPERTY_VALUE", $this->qstObject->prepareTextareaOutput($value->getAnswertext()));
292                $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
293                $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
294                $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
295                if ($this->getDisabled()) {
296                    $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
297                }
298                $tpl->parseCurrentBlock();
299            }
300            if ($this->getAllowMove()) {
301                $tpl->setCurrentBlock("move");
302                $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
303                $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
304                $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
305                $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
306                $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
307                $tpl->parseCurrentBlock();
308            }
309            $tpl->setCurrentBlock("row");
310            $tpl->setVariable("POST_VAR", $this->getPostVar());
311            $tpl->setVariable("ROW_NUMBER", $i);
312            $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
313            $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
314            if ($this->isAddRemoveAllowed()) {
315                $tpl->setVariable("ADD_REMOVE_ID", $this->getPostVar() . "[$i]");
316                $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
317                $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
318                $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
319                $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
320            }
321            if ($this->getDisabled()) {
322                $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
323            }
324            $tpl->parseCurrentBlock();
325            $i++;
326        }
327
328        $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
329        $tpl->setVariable("ANSWER_TEXT", $this->getTextInputLabel($lng));
330        $tpl->setVariable("POINTS_TEXT", $this->getPointsInputLabel($lng));
331        $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
332
333        $a_tpl->setCurrentBlock("prop_generic");
334        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
335        $a_tpl->parseCurrentBlock();
336
337        global $DIC;
338        $tpl = $DIC['tpl'];
339        $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
340        $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizard.js");
341    }
342
343    /**
344     * @param $lng
345     * @return mixed
346     */
347    protected function getTextInputLabel($lng)
348    {
349        return $lng->txt('answer_text');
350    }
351
352    /**
353     * @param $lng
354     * @return mixed
355     */
356    protected function getPointsInputLabel($lng)
357    {
358        return $lng->txt('points');
359    }
360
361    /**
362     * @return string
363     */
364    protected function getTemplate()
365    {
366        return "tpl.prop_answerwizardinput.html";
367    }
368
369    protected function sanitizeSuperGlobalSubmitValue()
370    {
371        if (isset($_POST[$this->getPostVar()]) && is_array($_POST[$this->getPostVar()])) {
372            $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
373        }
374    }
375}
376