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
26/**
27* Text survey question GUI representation
28*
29* The SurveyTextQuestionGUI class encapsulates the GUI representation
30* for text survey question types.
31*
32* @author		Helmut Schottmüller <helmut.schottmueller@mac.com>
33* @version	$Id$
34* @extends SurveyQuestionGUI
35* @ingroup ModulesSurveyQuestionPool
36*/
37class SurveyTextQuestionGUI extends SurveyQuestionGUI
38{
39    protected function initObject()
40    {
41        include_once "./Modules/SurveyQuestionPool/classes/class.SurveyTextQuestion.php";
42        $this->object = new SurveyTextQuestion();
43    }
44
45
46    //
47    // EDITOR
48    //
49
50    public function setQuestionTabs()
51    {
52        $this->setQuestionTabsForClass("surveytextquestiongui");
53    }
54
55    protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
56    {
57        // maximum number of characters
58        $maxchars = new ilNumberInputGUI($this->lng->txt("maxchars"), "maxchars");
59        $maxchars->setRequired(false);
60        $maxchars->setSize(5);
61        $maxchars->setDecimals(0);
62        $a_form->addItem($maxchars);
63
64        // textwidth
65        $textwidth = new ilNumberInputGUI($this->lng->txt("width"), "textwidth");
66        $textwidth->setRequired(true);
67        $textwidth->setSize(3);
68        $textwidth->setDecimals(0);
69        $textwidth->setMinValue(10, true);
70        $a_form->addItem($textwidth);
71
72        // textheight
73        $textheight = new ilNumberInputGUI($this->lng->txt("height"), "textheight");
74        $textheight->setRequired(true);
75        $textheight->setSize(3);
76
77        $textheight->setDecimals(0);
78        $textheight->setMinValue(1);
79        $a_form->addItem($textheight);
80
81        // values
82        if ($this->object->getMaxChars() > 0) {
83            $maxchars->setValue($this->object->getMaxChars());
84        }
85        $textwidth->setValue($this->object->getTextWidth());
86        $textheight->setValue($this->object->getTextHeight());
87    }
88
89    protected function importEditFormValues(ilPropertyFormGUI $a_form)
90    {
91        $max = $a_form->getInput("maxchars");
92        $this->object->setMaxChars(strlen($max) ? $max : null);
93        $this->object->setTextWidth($a_form->getInput("textwidth"));
94        $this->object->setTextHeight($a_form->getInput("textheight"));
95    }
96
97    public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
98    {
99        $res = array();
100
101        if (is_array($a_working_data)) {
102            $res[] = array("textanswer" => trim($a_working_data[0]["textanswer"]));
103        }
104
105        return $res;
106    }
107
108    public function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
109    {
110        $user_answer = null;
111        if ($a_working_data) {
112            $user_answer = $this->getParsedAnswers($a_working_data);
113            $user_answer = $user_answer[0]["textanswer"];
114        }
115
116        $template = new ilTemplate("tpl.il_svy_qpl_text_printview.html", true, true, "Modules/SurveyQuestionPool");
117        if ($show_questiontext) {
118            $this->outQuestionText($template);
119        }
120        if ($question_title) {
121            $template->setVariable("QUESTION_TITLE", $this->getPrintViewQuestionTitle($question_title));
122        }
123        $template->setVariable("QUESTION_ID", $this->object->getId());
124        $template->setVariable("TEXT_ANSWER", $this->lng->txt("answer"));
125        if (is_array($a_working_data) && trim($user_answer)) {
126            $template->setVariable("TEXT", nl2br($user_answer));
127        } else {
128            $template->setVariable("TEXTBOX_IMAGE", ilUtil::getHtmlPath(ilUtil::getImagePath("textbox.png")));
129            $template->setVariable("TEXTBOX", $this->lng->txt("textbox"));
130            $template->setVariable("TEXTBOX_WIDTH", $this->object->getTextWidth() * 16);
131            $template->setVariable("TEXTBOX_HEIGHT", $this->object->getTextHeight() * 16);
132        }
133        if ($this->object->getMaxChars()) {
134            $template->setVariable("TEXT_MAXCHARS", sprintf($this->lng->txt("text_maximum_chars_allowed"), $this->object->getMaxChars()));
135        }
136        return $template->get();
137    }
138
139
140    //
141    // EXECUTION
142    //
143
144    /**
145    * Creates the question output form for the learner
146    */
147    public function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
148    {
149        $template = new ilTemplate("tpl.il_svy_out_text.html", true, true, "Modules/SurveyQuestionPool");
150        $template->setCurrentBlock("material_text");
151        $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
152        $template->parseCurrentBlock();
153
154        if ($this->object->getTextHeight() == 1) {
155            $template->setCurrentBlock("textinput");
156            if (is_array($working_data)) {
157                if (strlen($working_data[0]["textanswer"])) {
158                    $template->setVariable("VALUE_ANSWER", " value=\"" . ilUtil::prepareFormOutput($working_data[0]["textanswer"]) . "\"");
159                }
160            }
161            $template->setVariable("QUESTION_ID", $this->object->getId());
162            $template->setVariable("WIDTH", $this->object->getTextWidth());
163            if ($this->object->getMaxChars()) {
164                $template->setVariable("MAXLENGTH", " maxlength=\"" . $this->object->getMaxChars() . "\"");
165            }
166            $template->parseCurrentBlock();
167        } else {
168            $template->setCurrentBlock("textarea");
169            if (is_array($working_data)) {
170                $template->setVariable("VALUE_ANSWER", ilUtil::prepareFormOutput($working_data[0]["textanswer"]));
171            }
172            $template->setVariable("QUESTION_ID", $this->object->getId());
173            $template->setVariable("WIDTH", $this->object->getTextWidth());
174            $template->setVariable("HEIGHT", $this->object->getTextHeight());
175            $template->parseCurrentBlock();
176        }
177        $template->setCurrentBlock("question_data_text");
178        if ($show_questiontext) {
179            $this->outQuestionText($template);
180        }
181        if ($question_title) {
182            $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
183        }
184        $template->setVariable("TEXT_ANSWER", $this->lng->txt("answer"));
185        $template->setVariable("LABEL_QUESTION_ID", $this->object->getId());
186        if (strcmp($error_message, "") != 0) {
187            $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
188        }
189        if ($this->object->getMaxChars()) {
190            $template->setVariable("TEXT_MAXCHARS", sprintf($this->lng->txt("text_maximum_chars_allowed"), $this->object->getMaxChars()));
191        }
192        $template->parseCurrentBlock();
193        return $template->get();
194    }
195}
196