ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCategoryWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 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 
32 {
33  protected $values = array();
34  protected $allowMove = false;
35  protected $disabled_scale = true;
36  protected $show_wizard = false;
37  protected $show_save_phrase = false;
38  protected $categorytext;
39  protected $show_neutral_category = false;
41  protected $use_other_answer;
42 
49  function __construct($a_title = "", $a_postvar = "")
50  {
51  parent::__construct($a_title, $a_postvar);
52  global $lng;
53  $this->show_wizard = false;
54  $this->show_save_phrase = false;
55  $this->categorytext = $lng->txt('answer');
56  $this->use_other_answer = false;
57  }
58 
59  public function getUseOtherAnswer()
60  {
62  }
63 
64  public function setUseOtherAnswer($a_value)
65  {
66  $this->use_other_answer = ($a_value) ? true : false;
67  }
68 
69  public function getCategoryCount()
70  {
71  if (!is_object($this->values)) return 0;
72  return $this->values->getCategoryCount();
73  }
74 
75  protected function calcNeutralCategoryScale()
76  {
77  if (is_object($this->values))
78  {
79  $scale = 0;
80  for ($i = 0; $i < $this->values->getCategoryCount(); $i++)
81  {
82  $cat = $this->values->getCategory($i);
83  if ($cat->neutral == 0) $scale += 1;
84  }
85  return $scale+1;
86  }
87  else
88  {
89  return 99;
90  }
91  }
92 
93  public function setShowNeutralCategory($a_value)
94  {
95  $this->show_neutral_category = $a_value;
96  }
97 
98  public function getShowNeutralCategory()
99  {
101  }
102 
103  public function setNeutralCategoryTitle($a_title)
104  {
105  $this->neutral_category_title = $a_title;
106  }
107 
108  public function getNeutralCategoryTitle()
109  {
111  }
112 
118  function setValue($a_value)
119  {
120  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyCategories.php";
121  $this->values = new SurveyCategories();
122  if (is_array($a_value))
123  {
124  if (is_array($a_value['answer']))
125  {
126  foreach ($a_value['answer'] as $index => $value)
127  {
128  $this->values->addCategory($value, $a_value['other'][$index], null, null, $a_value['scale'][$index]);
129  }
130  }
131  }
132  if (array_key_exists('neutral', $a_value))
133  {
134  $this->values->addCategory($a_value['neutral'], 0, 1, null, $_POST[$this->postvar . '_neutral_scale']);
135  }
136  }
137 
143  function setValues($a_values)
144  {
145  $this->values = $a_values;
146  }
147 
153  function getValues()
154  {
155  return $this->values;
156  }
157 
163  function setAllowMove($a_allow_move)
164  {
165  $this->allowMove = $a_allow_move;
166  }
167 
173  function getAllowMove()
174  {
175  return $this->allowMove;
176  }
177 
178  function setShowWizard($a_value)
179  {
180  $this->show_wizard = $a_value;
181  }
182 
183  function getShowWizard()
184  {
185  return $this->show_wizard;
186  }
187 
188  public function setCategoryText($a_text)
189  {
190  $this->categorytext = $a_text;
191  }
192 
193  public function getCategoryText()
194  {
195  return $this->categorytext;
196  }
197 
198  function setShowSavePhrase($a_value)
199  {
200  $this->show_save_phrase = $a_value;
201  }
202 
203  function getShowSavePhrase()
204  {
206  }
207 
208  function getDisabledScale()
209  {
210  return $this->disabled_scale;
211  }
212 
213  function setDisabledScale($a_value)
214  {
215  $this->disabled_scale = $a_value;
216  }
217 
223  function checkInput()
224  {
225  global $lng;
226  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
227  $foundvalues = $_POST[$this->getPostVar()];
228  if (is_array($foundvalues))
229  {
230  // check answers
231  if (is_array($foundvalues['answer']))
232  {
233  foreach ($foundvalues['answer'] as $idx => $answervalue)
234  {
235  if (((strlen($answervalue)) == 0) && ($this->getRequired() && (!$foundvalues['other'][$idx])))
236  {
237  $this->setAlert($lng->txt("msg_input_is_required"));
238  return FALSE;
239  }
240  }
241  }
242  // check neutral column
243  if (array_key_exists('neutral', $foundvalues))
244  {
245  if ((strlen($foundvalues['neutral']) == 0) && ($this->getRequired))
246  {
247  $this->setAlert($lng->txt("msg_input_is_required"));
248  return false;
249  }
250  }
251  // check scales
252  if (is_array($foundvalues['scale']))
253  {
254  foreach ($foundvalues['scale'] as $scale)
255  {
256  if ((strlen($scale)) == 0)
257  {
258  $this->setAlert($lng->txt("msg_input_is_required"));
259  return FALSE;
260  }
261  }
262  }
263  // check scales
264  if (is_array($foundvalues['scale']))
265  {
266  if (count(array_unique($foundvalues['scale'])) != count($foundvalues['scale']))
267  {
268  $this->setAlert($lng->txt("msg_duplicate_scale"));
269  return FALSE;
270  }
271  }
272  // check neutral column scale
273  if (strlen($_POST[$this->postvar . '_neutral_scale']))
274  {
275  if (is_array($foundvalues['scale']))
276  {
277  if (in_array($_POST[$this->postvar . '_neutral_scale'], $foundvalues['scale']))
278  {
279  $this->setAlert($lng->txt("msg_duplicate_scale"));
280  return FALSE;
281  }
282  }
283  }
284  }
285  else
286  {
287  $this->setAlert($lng->txt("msg_input_is_required"));
288  return FALSE;
289  }
290  return $this->checkSubItemsInput();
291  }
292 
298  function insert(&$a_tpl)
299  {
300  global $lng;
301 
302  $neutral_category = null;
303  $tpl = new ilTemplate("tpl.prop_categorywizardinput.html", true, true, "Modules/SurveyQuestionPool");
304  $i = 0;
305  if (is_object($this->values))
306  {
307  for ($i = 0; $i < $this->values->getCategoryCount(); $i++)
308  {
309  $cat = $this->values->getCategory($i);
310  if (!$cat->neutral)
311  {
312  $tpl->setCurrentBlock("prop_text_propval");
313  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($cat->title));
314  $tpl->parseCurrentBlock();
315  $tpl->setCurrentBlock("prop_scale_propval");
316  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->values->getScale($i)));
317  $tpl->parseCurrentBlock();
318 
319  if ($this->getUseOtherAnswer())
320  {
321  $tpl->setCurrentBlock("other_answer_checkbox");
322  $tpl->setVariable("POST_VAR", $this->getPostVar());
323  $tpl->setVariable("OTHER_ID", $this->getPostVar() . "[other][$i]");
324  $tpl->setVariable("ROW_NUMBER", $i);
325  if ($cat->other)
326  {
327  $tpl->setVariable("CHECKED_OTHER", ' checked="checked"');
328  }
329  $tpl->parseCurrentBlock();
330  }
331 
332  if ($this->getAllowMove())
333  {
334  $tpl->setCurrentBlock("move");
335  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
336  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
337  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
338  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
339  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
340  $tpl->parseCurrentBlock();
341  }
342  $tpl->setCurrentBlock("row");
343  $class = ($i % 2 == 0) ? "even" : "odd";
344  if ($i == 0) $class .= " first";
345  if ($i == $this->values->getCategoryCount()-1) $class .= " last";
346  $tpl->setVariable("ROW_CLASS", $class);
347  $tpl->setVariable("POST_VAR", $this->getPostVar());
348  $tpl->setVariable("ROW_NUMBER", $i);
349  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
350  $tpl->setVariable("SIZE", $this->getSize());
351  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
352  if ($this->getDisabled())
353  {
354  $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
355  }
356 
357  $tpl->setVariable("SCALE_ID", $this->getPostVar() . "[scale][$i]");
358  if ($this->getDisabledScale())
359  {
360  $tpl->setVariable("DISABLED_SCALE", " disabled=\"disabled\"");
361  }
362 
363  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
364  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
365  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
366  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
367  $tpl->parseCurrentBlock();
368  }
369  else
370  {
371  $neutral_category = $cat;
372  }
373  }
374  }
375 
376  if ($this->getShowWizard())
377  {
378  $tpl->setCurrentBlock("wizard");
379  $tpl->setVariable("CMD_WIZARD", 'cmd[wizard' . $this->getFieldId() . ']');
380  $tpl->setVariable("WIZARD_BUTTON", ilUtil::getImagePath('wizard.png'));
381  $tpl->setVariable("WIZARD_TEXT", $lng->txt('add_phrase'));
382  $tpl->parseCurrentBlock();
383  }
384 
385  if ($this->getShowSavePhrase())
386  {
387  $tpl->setCurrentBlock('savephrase');
388  $tpl->setVariable("POST_VAR", $this->getPostVar());
389  $tpl->setVariable("VALUE_SAVE_PHRASE", $lng->txt('save_phrase'));
390  $tpl->parseCurrentBlock();
391  }
392 
393  if ($this->getShowNeutralCategory())
394  {
395  if (is_object($neutral_category) && strlen($neutral_category->title))
396  {
397  $tpl->setCurrentBlock("prop_text_neutral_propval");
398  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($neutral_category->title));
399  $tpl->parseCurrentBlock();
400  }
401  if (strlen($this->getNeutralCategoryTitle()))
402  {
403  $tpl->setCurrentBlock("neutral_category_title");
404  $tpl->setVariable("CATEGORY_TITLE", ilUtil::prepareFormOutput($this->getNeutralCategoryTitle()));
405  $tpl->parseCurrentBlock();
406  }
407  $tpl->setCurrentBlock("prop_scale_neutral_propval");
408  $scale = ($neutral_category->scale > 0) ? $neutral_category->scale : $this->values->getNewScale();
409  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($scale));
410  $tpl->parseCurrentBlock();
411 
412  if ($this->getUseOtherAnswer())
413  {
414  $tpl->touchBlock('other_answer_neutral');
415  }
416 
417  $tpl->setCurrentBlock('neutral_row');
418  $tpl->setVariable("POST_VAR", $this->getPostVar());
419  $tpl->setVariable("ID", $this->getPostVar() . "_neutral");
420  $tpl->setVariable("SIZE", $this->getSize());
421  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
422  if ($this->getDisabled())
423  {
424  $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
425  }
426  $tpl->setVariable("SCALE_ID", $this->getPostVar() . "_neutral_scale");
427  if ($this->getDisabledScale())
428  {
429  $tpl->setVariable("DISABLED_SCALE", " disabled=\"disabled\"");
430  }
431  $tpl->parseCurrentBlock();
432  }
433 
434  if ($this->getUseOtherAnswer())
435  {
436  $tpl->setCurrentBlock('other_answer_title');
437  $tpl->setVariable("OTHER_TEXT", $lng->txt('use_other_answer'));
438  $tpl->parseCurrentBlock();
439  }
440 
441  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
442  $tpl->setVariable("ANSWER_TEXT", $this->getCategoryText());
443  $tpl->setVariable("SCALE_TEXT", $lng->txt('scale'));
444  $tpl->setVariable("ACTIONS_TEXT", $lng->txt('actions'));
445 
446  $a_tpl->setCurrentBlock("prop_generic");
447  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
448  $a_tpl->parseCurrentBlock();
449 
450  global $tpl;
451  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
453  $tpl->addJavascript("./Modules/SurveyQuestionPool/templates/default/categorywizard.js");
454  }
455 }