ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCombinationInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 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 $items = array();
34  protected $labels;
35  protected $comparison;
36 
39 
47  function addCombinationItem($id, $item, $label = "")
48  {
49  $this->items[$id] = $item;
50  if($label)
51  {
52  $this->labels[$id] = $label;
53  }
54  }
55 
62  function getCombinationItem($id)
63  {
64  if(isset($this->items[$id]))
65  {
66  return $this->items[$id];
67  }
68  }
69 
75  function removeCombinationItem($id)
76  {
77  if(isset($this->items[$id]))
78  {
79  unset($this->items[$id]);
80  }
81  }
82 
89  function __call($method, $param)
90  {
91  $result = array();
92  foreach($this->items as $id => $obj)
93  {
94  if(method_exists($obj, $method))
95  {
96  $result[$id] = call_user_func_array(array($obj, $method), $param);
97  }
98  }
99  return $result;
100  }
101 
105  function serializeData()
106  {
107  $result = array();
108  foreach($this->items as $id => $obj)
109  {
110  $result[$id] = $obj->serializeData();
111  }
112  return serialize($result);
113  }
114 
118  function unserializeData($a_data)
119  {
120  $data = unserialize($a_data);
121 
122  if ($data)
123  {
124  foreach($this->items as $id => $obj)
125  {
126  $obj->unserializeData($data[$id]);
127  }
128  }
129  else
130  {
131  foreach($this->items as $id => $obj)
132  {
133  if(method_exists($obj, "setValue"))
134  {
135  $this->setValue(false);
136  }
137  }
138  }
139  }
140 
147  function setComparisonMode($mode)
148  {
149  if(in_array($mode, array(self::COMPARISON_ASCENDING, self::COMPARISON_DESCENDING)))
150  {
151  foreach($this->items as $obj)
152  {
153  if(!method_exists($obj, "getPostValueForComparison"))
154  {
155  return false;
156  }
157  }
158  $this->comparison_mode = $mode;
159  return true;
160  }
161  }
162 
168  function setValue($a_value)
169  {
170  if(is_array($a_value))
171  {
172  foreach($a_value as $id => $value)
173  {
174  if(isset($this->items[$id]))
175  {
176  $this->items[$id]->setValue($value);
177  }
178  }
179  }
180  else if($a_value === NULL)
181  {
182  foreach($this->items as $item)
183  {
184  if(method_exists($item, "setValue"))
185  {
186  $item->setValue(NULL);
187  }
188  // datetime
189  else if(method_exists($item, "setDate"))
190  {
191  $item->setDate();
192  }
193  // duration
194  else if(method_exists($item, "setMonths"))
195  {
196  $item->setMonths(0);
197  $item->setDays(0);
198  $item->setHours(0);
199  $item->setMinutes(0);
200  $item->setSeconds(0);
201  }
202  }
203  }
204  }
205 
211  function getValue()
212  {
213  $result = array();
214  foreach($this->items as $id => $obj)
215  {
216  $result[$id] = $obj->getValue();
217  }
218  return $result;
219  }
220 
226  function setValueByArray($a_values)
227  {
228  foreach($this->items as $id => $obj)
229  {
230  $obj->setValueByArray($a_values);
231  }
232  }
233 
239  function checkInput()
240  {
241  if(sizeof($this->items))
242  {
243  foreach($this->items as $id => $obj)
244  {
245  if(!$obj->checkInput())
246  {
247  return false;
248  }
249  }
250 
251  if($this->comparison_mode)
252  {
253  $prev = NULL;
254  foreach($this->items as $id => $obj)
255  {
256  $value = $obj->getPostValueForComparison();
257  if($value != "")
258  {
259  if($prev !== NULL)
260  {
261  if($this->comparison_mode == self::COMPARISON_ASCENDING)
262  {
263  if($value < $prev)
264  {
265  return false;
266  }
267  }
268  else
269  {
270  if($value > $prev)
271  {
272  return false;
273  }
274  }
275  }
276  $prev = $value;
277  }
278  }
279  }
280  }
281 
282  return $this->checkSubItemsInput();
283  }
284 
290  function insert(&$a_tpl)
291  {
292  $html = $this->render();
293 
294  $a_tpl->setCurrentBlock("prop_generic");
295  $a_tpl->setVariable("PROP_GENERIC", $html);
296  $a_tpl->parseCurrentBlock();
297  }
298 
302  function render()
303  {
304  global $lng;
305 
306  $tpl = new ilTemplate("tpl.prop_combination.html", true, true, "Services/Form");
307 
308  if(sizeof($this->items))
309  {
310  foreach($this->items as $id => $obj)
311  {
312  // label
313  if(isset($this->labels[$id]))
314  {
315  $tpl->setCurrentBlock("prop_combination_label");
316  $tpl->setVariable("LABEL", $this->labels[$id]);
317  $tpl->parseCurrentBlock();
318  }
319 
320  $tpl->setCurrentBlock("prop_combination");
321  $tpl->setVariable("FIELD", $obj->render());
322  $tpl->parseCurrentBlock();
323  }
324  }
325 
326  return $tpl->get();
327  }
328 
333  {
334  $html = $this->render();
335  return $html;
336  }
337 }