1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once './Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php';
5require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
6
7/**
8 * This class represents a single choice wizard property in a property form.
9 *
10 * @author Helmut Schottmüller <ilias@aurealis.de>
11 * @author Maximilian Becker <mbecker@databay.de>
12 *
13 * @version $Id$
14 *
15 * @ingroup ModulesTestQuestionPool
16 */
17class ilMatchingWizardInputGUI extends ilTextInputGUI
18{
19    protected $text_name = '';
20    protected $image_name = '';
21    protected $values = array();
22    protected $allowMove = false;
23    protected $qstObject = null;
24    protected $suffixes = array();
25    protected $hideImages = false;
26
27    /**
28     * Constructor
29     *
30     * @param	string	$a_title	Title
31     * @param	string	$a_postvar	Post Variable
32     *
33     * @return \ilMatchingWizardInputGUI
34     */
35    public function __construct($a_title = "", $a_postvar = "")
36    {
37        global $DIC;
38        $lng = $DIC['lng'];
39
40        parent::__construct($a_title, $a_postvar);
41        $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
42        $this->setSize('40');
43        $this->text_name = $lng->txt('answer_text');
44        $this->image_name = $lng->txt('answer_image');
45    }
46
47    /**
48    * Set Accepted Suffixes.
49    *
50    * @param	array	$a_suffixes	Accepted Suffixes
51    */
52    public function setSuffixes($a_suffixes)
53    {
54        $this->suffixes = $a_suffixes;
55    }
56
57    /**
58    * Get Accepted Suffixes.
59    *
60    * @return	array	Accepted Suffixes
61    */
62    public function getSuffixes()
63    {
64        return $this->suffixes;
65    }
66
67    /**
68    * Set hide images.
69    *
70    * @param	bool	$a_hide	Hide images
71    */
72    public function setHideImages($a_hide)
73    {
74        $this->hideImages = $a_hide;
75    }
76
77    /**
78    * Set Values
79    *
80    * @param	array	$a_value	Value
81    */
82    public function setValues($a_values)
83    {
84        $this->values = $a_values;
85    }
86
87    /**
88    * Get Values
89    *
90    * @return	array	Values
91    */
92    public function getValues()
93    {
94        return $this->values;
95    }
96
97    public function setTextName($a_value)
98    {
99        $this->text_name = $a_value;
100    }
101
102    public function setImageName($a_value)
103    {
104        $this->image_name = $a_value;
105    }
106
107    /**
108    * Set question object
109    *
110    * @param	object	$a_value	test object
111    */
112    public function setQuestionObject($a_value)
113    {
114        $this->qstObject = &$a_value;
115    }
116
117    /**
118    * Get question object
119    *
120    * @return	object	Value
121    */
122    public function getQuestionObject()
123    {
124        return $this->qstObject;
125    }
126
127    /**
128    * Set allow move
129    *
130    * @param	boolean	$a_allow_move Allow move
131    */
132    public function setAllowMove($a_allow_move)
133    {
134        $this->allowMove = $a_allow_move;
135    }
136
137    /**
138    * Get allow move
139    *
140    * @return	boolean	Allow move
141    */
142    public function getAllowMove()
143    {
144        return $this->allowMove;
145    }
146
147    /**
148    * Set Value.
149    *
150    * @param	string	$a_value	Value
151    */
152    public function setValue($a_value)
153    {
154        $this->values = array();
155        if (is_array($a_value)) {
156            if (is_array($a_value['answer'])) {
157                foreach ($a_value['answer'] as $index => $value) {
158                    include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
159                    $answer = new assAnswerMatchingTerm($value, $a_value['imagename'][$index], $a_value['identifier'][$index]);
160                    array_push($this->values, $answer);
161                }
162            }
163        }
164    }
165
166    /**
167    * Check input, strip slashes etc. set alert, if input is not ok.
168    *
169    * @return	boolean		Input ok, true/false
170    */
171    public function checkInput()
172    {
173        global $DIC;
174        $lng = $DIC['lng'];
175        include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
176        if (is_array($_POST[$this->getPostVar()])) {
177            $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], true, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
178        }
179        $foundvalues = $_POST[$this->getPostVar()];
180        if (is_array($foundvalues)) {
181            // check answers
182            if (is_array($foundvalues['answer'])) {
183                foreach ($foundvalues['answer'] as $aidx => $answervalue) {
184                    if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0)) {
185                        $this->setAlert($lng->txt("msg_input_is_required"));
186                        return false;
187                    }
188                }
189            }
190            if (!$this->hideImages) {
191                if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
192                    foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
193                        // error handling
194                        if ($error > 0) {
195                            switch ($error) {
196                                case UPLOAD_ERR_INI_SIZE:
197                                    $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
198                                    return false;
199                                    break;
200
201                                case UPLOAD_ERR_FORM_SIZE:
202                                    $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
203                                    return false;
204                                    break;
205
206                                case UPLOAD_ERR_PARTIAL:
207                                    $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
208                                    return false;
209                                    break;
210
211                                case UPLOAD_ERR_NO_FILE:
212                                    if ($this->getRequired()) {
213                                        if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
214                                            $this->setAlert($lng->txt("form_msg_file_no_upload"));
215                                            return false;
216                                        }
217                                    }
218                                    break;
219
220                                case UPLOAD_ERR_NO_TMP_DIR:
221                                    $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
222                                    return false;
223                                    break;
224
225                                case UPLOAD_ERR_CANT_WRITE:
226                                    $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
227                                    return false;
228                                    break;
229
230                                case UPLOAD_ERR_EXTENSION:
231                                    $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
232                                    return false;
233                                    break;
234                            }
235                        }
236                    }
237                }
238
239                if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
240                    foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
241                        $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
242                        $filename_arr = pathinfo($filename);
243                        $suffix = $filename_arr["extension"];
244
245                        // check suffixes
246                        if (strlen($tmpname) && is_array($this->getSuffixes())) {
247                            $vir = ilUtil::virusHandling($tmpname, $filename);
248                            if ($vir[0] == false) {
249                                $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
250                                return false;
251                            }
252
253                            if (!in_array(strtolower($suffix), $this->getSuffixes())) {
254                                $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
255                                return false;
256                            }
257                        }
258                    }
259                }
260            }
261        }
262        return $this->checkSubItemsInput();
263    }
264
265    /**
266    * Insert property html
267    *
268    * @return	int	Size
269    */
270    public function insert($a_tpl)
271    {
272        global $DIC;
273        $lng = $DIC['lng'];
274
275        $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "Modules/TestQuestionPool");
276        $i = 0;
277        foreach ($this->values as $value) {
278            if (!$this->hideImages) {
279                if (strlen($value->picture)) {
280                    $imagename = $this->qstObject->getImagePathWeb() . $value->picture;
281                    if ($this->qstObject->getThumbSize()) {
282                        if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->picture)) {
283                            $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->picture;
284                        }
285                    }
286                    $tpl->setCurrentBlock('image');
287                    $tpl->setVariable('SRC_IMAGE', $imagename);
288                    $tpl->setVariable('IMAGE_NAME', $value->picture);
289                    $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->text));
290                    $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
291                    $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
292                    $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
293                    $tpl->parseCurrentBlock();
294                }
295                $tpl->setCurrentBlock('addimage');
296                $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
297                $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
298                $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
299                $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
300                $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
301                $tpl->parseCurrentBlock();
302            }
303
304            if (is_object($value)) {
305                $tpl->setCurrentBlock("prop_text_propval");
306                $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->text));
307                $tpl->parseCurrentBlock();
308            }
309            // this block does not exist in the template
310            //			$tpl->setCurrentBlock('singleline');
311            $tpl->setVariable("SIZE", $this->getSize());
312            $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
313            $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
314            $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
315            $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
316            if ($this->getDisabled()) {
317                $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
318            }
319            $tpl->parseCurrentBlock();
320            if ($this->getAllowMove()) {
321                $tpl->setCurrentBlock("move");
322                $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
323                $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
324                $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
325                $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
326                $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
327                $tpl->parseCurrentBlock();
328            }
329            $tpl->setCurrentBlock("row");
330            $tpl->setVariable("POST_VAR", $this->getPostVar());
331            $tpl->setVariable("ROW_NUMBER", $i + 1);
332            $tpl->setVariable("ROW_IDENTIFIER", $value->identifier);
333            $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
334            $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
335            $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
336            $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
337            $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
338            $tpl->parseCurrentBlock();
339            $i++;
340        }
341
342        if (!$this->hideImages) {
343            if (is_array($this->getSuffixes())) {
344                $suff_str = $delim = "";
345                foreach ($this->getSuffixes() as $suffix) {
346                    $suff_str .= $delim . "." . $suffix;
347                    $delim = ", ";
348                }
349                $tpl->setCurrentBlock('allowed_image_suffixes');
350                $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
351                $tpl->parseCurrentBlock();
352            }
353            $tpl->setCurrentBlock("image_heading");
354            $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
355            $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
356            $tpl->parseCurrentBlock();
357        }
358
359        $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
360        $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
361        $tpl->setVariable("TEXT_NO", $lng->txt('no'));
362        $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
363        $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
364        $tpl->setVariable("ANSWER_TEXT", $this->text_name);
365        $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
366        $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
367
368        $a_tpl->setCurrentBlock("prop_generic");
369        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
370        $a_tpl->parseCurrentBlock();
371    }
372}
373