ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assClozeGap.php
Go to the documentation of this file.
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 
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
38 {
47  var $type;
48 
56  var $items;
57 
63  var $shuffle;
64 
75  function assClozeGap($a_type)
76  {
77  $this->type = $a_type;
78  $this->items = array();
79  $this->shuffle = true;
80  }
81 
91  function getType()
92  {
93  return $this->type;
94  }
95 
105  function setType($a_type = 0)
106  {
107  $this->type = $a_type;
108  }
109 
119  function getItems()
120  {
121  if ($this->shuffle)
122  {
123  return $this->arrayShuffle($this->items);
124  }
125  else
126  {
127  return $this->items;
128  }
129  }
130 
140  function getItemsRaw()
141  {
142  return $this->items;
143  }
144 
154  function getItemCount()
155  {
156  return count($this->items);
157  }
158 
168  function addItem($a_item)
169  {
170  $order = $a_item->getOrder();
171  if (array_key_exists($order, $this->items))
172  {
173  $newitems = array();
174  for ($i = 0; $i < $order; $i++)
175  {
176  array_push($newitems, $this->items[$i]);
177  }
178  array_push($newitems, $a_item);
179  for ($i = $order; $i < count($this->items); $i++)
180  {
181  array_push($newitems, $this->items[$i]);
182  }
183  $i = 0;
184  foreach ($newitems as $idx => $item)
185  {
186  $newitems[$idx]->setOrder($i);
187  $i++;
188  }
189  $this->items = $newitems;
190  }
191  else
192  {
193  array_push($this->items, $a_item);
194  }
195  }
196 
207  function setItemPoints($order, $points)
208  {
209  foreach ($this->items as $key => $item)
210  {
211  if ($item->getOrder() == $order)
212  {
213  $item->setPoints($points);
214  }
215  }
216  }
217 
227  function deleteItem($order)
228  {
229  if (array_key_exists($order, $this->items))
230  {
231  unset($this->items[$order]);
232  $order = 0;
233  foreach ($this->items as $key => $item)
234  {
235  $this->items[$key]->setOrder($order);
236  $order++;
237  }
238  }
239  }
240 
251  function setItemLowerBound($order, $bound)
252  {
253  foreach ($this->items as $key => $item)
254  {
255  if ($item->getOrder() == $order)
256  {
257  $item->setLowerBound($bound);
258  }
259  }
260  }
261 
272  function setItemUpperBound($order, $bound)
273  {
274  foreach ($this->items as $key => $item)
275  {
276  if ($item->getOrder() == $order)
277  {
278  $item->setUpperBound($bound);
279  }
280  }
281  }
282 
292  function getItem($a_index)
293  {
294  if (array_key_exists($a_index, $this->items))
295  {
296  return $this->items[$a_index];
297  }
298  else
299  {
300  return NULL;
301  }
302  }
303 
312  function clearItems()
313  {
314  $this->items = array();
315  }
316 
326  function setShuffle($a_shuffle = TRUE)
327  {
328  $this->shuffle = $a_shuffle ? TRUE : FALSE;
329  }
330 
340  function getShuffle()
341  {
342  return $this->shuffle;
343  }
344 
353  function arrayShuffle($array)
354  {
355  mt_srand((double)microtime()*1000000);
356  $i = count($array);
357  if ($i > 0)
358  {
359  while(--$i)
360  {
361  $j = mt_rand(0, $i);
362  if ($i != $j)
363  {
364  // swap elements
365  $tmp = $array[$j];
366  $array[$j] = $array[$i];
367  $array[$i] = $tmp;
368  }
369  }
370  }
371  return $array;
372  }
373 
382  function getMaxWidth()
383  {
384  $maxwidth = 0;
385  foreach ($this->items as $item)
386  {
387  if (strlen($item->getAnswertext()) > $maxwidth)
388  {
389  $maxwidth = strlen($item->getAnswertext());
390  }
391  }
392  return $maxwidth;
393  }
394 
404  {
405  $maxpoints = 0;
406  foreach ($this->items as $key => $item)
407  {
408  if ($item->getPoints() > $maxpoints)
409  {
410  $maxpoints = $item->getPoints();
411  }
412  }
413  $keys = array();
414  foreach ($this->items as $key => $item)
415  {
416  if ($item->getPoints() == $maxpoints)
417  {
418  array_push($keys, $key);
419  }
420  }
421  return $keys;
422  }
423 
425  {
426  global $lng;
427  switch ($this->getType())
428  {
429  case CLOZE_TEXT:
430  case CLOZE_SELECT:
431  $best_solutions = array();
432  foreach ($this->getItems() as $answer)
433  {
434  if (is_array($best_solutions[$answer->getPoints()]))
435  {
436  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
437  }
438  else
439  {
440  $best_solutions[$answer->getPoints()] = array();
441  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
442  }
443  }
444  krsort($best_solutions, SORT_NUMERIC);
445  reset($best_solutions);
446  $found = current($best_solutions);
447  return join(" " . $lng->txt("or") . " ", $found);
448  break;
449  case CLOZE_NUMERIC:
450  $maxpoints = 0;
451  $foundvalue = "";
452  foreach ($this->getItems() as $answer)
453  {
454  if ($answer->getPoints() >= $maxpoints)
455  {
456  $maxpoints = $answer->getPoints();
457  $foundvalue = $answer->getAnswertext();
458  }
459  }
460  return $foundvalue;
461  break;
462  default:
463  return "";
464  }
465  }
466 }
467 
468 ?>