ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.assClozeGap.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
5 
19 {
20 
29  var $type;
30 
38  var $items;
39 
45  var $shuffle;
46 
47  private $gap_size = 0;
48 
55  public function __construct($a_type)
56  {
57  $this->type = $a_type;
58  $this->items = array();
59  $this->shuffle = true;
60  }
61 
71  public function getType()
72  {
73  return $this->type;
74  }
75 
83  public function setType($a_type = 0)
84  {
85  $this->type = $a_type;
86  }
87 
94  public function getItems(ilArrayElementShuffler $shuffler)
95  {
96  if ($this->getShuffle())
97  {
98  return $shuffler->shuffle($this->items);
99  }
100 
101  return $this->items;
102  }
103 
113  function getItemsRaw()
114  {
115  return $this->items;
116  }
117 
127  function getItemCount()
128  {
129  return count($this->items);
130  }
131 
141  function addItem($a_item)
142  {
143  $order = $a_item->getOrder();
144  if (array_key_exists($order, $this->items))
145  {
146  $newitems = array();
147  for ($i = 0; $i < $order; $i++)
148  {
149  array_push($newitems, $this->items[$i]);
150  }
151  array_push($newitems, $a_item);
152  for ($i = $order; $i < count($this->items); $i++)
153  {
154  array_push($newitems, $this->items[$i]);
155  }
156  $i = 0;
157  foreach ($newitems as $idx => $item)
158  {
159  $newitems[$idx]->setOrder($i);
160  $i++;
161  }
162  $this->items = $newitems;
163  }
164  else
165  {
166  array_push($this->items, $a_item);
167  }
168  }
169 
180  function setItemPoints($order, $points)
181  {
182  foreach ($this->items as $key => $item)
183  {
184  if ($item->getOrder() == $order)
185  {
186  $item->setPoints($points);
187  }
188  }
189  }
190 
200  function deleteItem($order)
201  {
202  if (array_key_exists($order, $this->items))
203  {
204  unset($this->items[$order]);
205  $order = 0;
206  foreach ($this->items as $key => $item)
207  {
208  $this->items[$key]->setOrder($order);
209  $order++;
210  }
211  }
212  }
213 
224  function setItemLowerBound($order, $bound)
225  {
226  foreach ($this->items as $key => $item)
227  {
228  if ($item->getOrder() == $order)
229  {
230  $item->setLowerBound($bound);
231  }
232  }
233  }
234 
245  function setItemUpperBound($order, $bound)
246  {
247  foreach ($this->items as $key => $item)
248  {
249  if ($item->getOrder() == $order)
250  {
251  $item->setUpperBound($bound);
252  }
253  }
254  }
255 
265  function getItem($a_index)
266  {
267  if (array_key_exists($a_index, $this->items))
268  {
269  return $this->items[$a_index];
270  }
271  else
272  {
273  return NULL;
274  }
275  }
276 
285  function clearItems()
286  {
287  $this->items = array();
288  }
289 
297  public function setShuffle($a_shuffle = true)
298  {
299  $this->shuffle = (bool) $a_shuffle;
300  }
301 
307  public function getShuffle()
308  {
309  return $this->shuffle;
310  }
311 
320  function getMaxWidth()
321  {
322  $maxwidth = 0;
323  foreach ($this->items as $item)
324  {
325  if (strlen($item->getAnswertext()) > $maxwidth)
326  {
327  $maxwidth = strlen($item->getAnswertext());
328  }
329  }
330  return $maxwidth;
331  }
332 
342  {
343  $maxpoints = 0;
344  foreach ($this->items as $key => $item)
345  {
346  if ($item->getPoints() > $maxpoints)
347  {
348  $maxpoints = $item->getPoints();
349  }
350  }
351  $keys = array();
352  foreach ($this->items as $key => $item)
353  {
354  if ($item->getPoints() == $maxpoints)
355  {
356  array_push($keys, $key);
357  }
358  }
359  return $keys;
360  }
361 
367  function getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations = null)
368  {
369  global $lng;
370  switch ($this->getType())
371  {
372  case CLOZE_TEXT:
373  case CLOZE_SELECT:
374  $best_solutions = array();
375  if($combinations !== null && $combinations['best_solution'] == 1)
376  {
377  $best_solutions[$combinations['points']] = array();
378  array_push($best_solutions[$combinations['points']], $combinations['answer']);
379  }
380  else
381  {
382  foreach ($this->getItems($shuffler) as $answer)
383  {
384  if (isset($best_solutions[$answer->getPoints()]) && is_array($best_solutions[$answer->getPoints()]))
385  {
386  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
387  }
388  else
389  {
390  $best_solutions[$answer->getPoints()] = array();
391  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
392  }
393  }
394  }
395 
396  krsort($best_solutions, SORT_NUMERIC);
397  reset($best_solutions);
398  $found = current($best_solutions);
399  return join(" " . $lng->txt("or") . " ", $found);
400  break;
401  case CLOZE_NUMERIC:
402  $maxpoints = 0;
403  $foundvalue = "";
404  foreach ($this->getItems($shuffler) as $answer)
405  {
406  if ($answer->getPoints() >= $maxpoints)
407  {
408  $maxpoints = $answer->getPoints();
409  $foundvalue = $answer->getAnswertext();
410  }
411  }
412  return $foundvalue;
413  break;
414  default:
415  return "";
416  }
417  }
418 
422  public function setGapSize( $gap_size)
423  {
424  $this->gap_size = $gap_size;
425  }
426 
430  public function getGapSize()
431  {
432  return $this->gap_size;
433  }
434 }
setItemUpperBound($order, $bound)
Sets the upper bound for a given item.
setType($a_type=0)
Sets the cloze gap type.
const CLOZE_TEXT
Cloze question constants.
deleteItem($order)
Deletes an item at a given index.
setItemLowerBound($order, $bound)
Sets the lower bound for a given item.
clearItems()
Removes all gap items.
Class for cloze question gaps.
setItemPoints($order, $points)
Sets the points for a given item.
$a_type
Definition: workflow.php:93
getItemsRaw()
Gets the items of a cloze gap.
const CLOZE_SELECT
setGapSize( $gap_size)
addItem($a_item)
Adds a gap item.
$type
Type of gap.
getBestSolutionIndexes()
Returns the indexes of the best solutions for the gap.
Create styles array
The data for the language used.
getItems(ilArrayElementShuffler $shuffler)
Gets the items of a cloze gap.
getMaxWidth()
Returns the maximum width of the gap.
getItemCount()
Gets the item count.
global $lng
Definition: privfeed.php:17
getType()
Gets the cloze gap type.
getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations=null)
getShuffle()
Gets the shuffle state of the items.
const CLOZE_NUMERIC
getItem($a_index)
Gets the item with a given index.
setShuffle($a_shuffle=true)
Sets the shuffle state of the items.
__construct($a_type)
assClozeGap constructor