ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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  const TYPE_TEXT = 0;
21  const TYPE_SELECT = 1;
22  const TYPE_NUMERIC = 2;
23 
32  public $type;
33 
41  public $items;
42 
48  public $shuffle;
49 
50  private $shuffler;
51 
52  private $gap_size = 0;
53 
60  public function __construct($a_type)
61  {
62  $this->type = (int) $a_type;
63  $this->items = array();
64  $this->shuffle = true;
65  }
66 
76  public function getType()
77  {
78  return $this->type;
79  }
80 
81  public function isTextGap() : bool
82  {
83  return $this->type == self::TYPE_TEXT;
84  }
85 
86  public function isSelectGap() : bool
87  {
88  return $this->type == self::TYPE_SELECT;
89  }
90 
91  public function isNumericGap() : bool
92  {
93  return $this->type == self::TYPE_NUMERIC;
94  }
95 
103  public function setType($a_type = 0)
104  {
105  $this->type = $a_type;
106  }
107 
108  public function getItems(ilArrayElementShuffler $shuffler, ?int $gap_index = null) : array
109  {
110  if (!$this->getShuffle()) {
111  return $this->items;
112  }
113 
114  if ($gap_index === null) {
115  return $shuffler->shuffle($this->items);
116  }
117 
119  for ($i = -2; $i < $gap_index; $i++) {
120  $items = $shuffler->shuffle($items);
121  }
122 
123  return $items;
124  }
125 
135  public function getItemsRaw()
136  {
137  return $this->items;
138  }
139 
149  public function getItemCount()
150  {
151  return count($this->items);
152  }
153 
163  public function addItem($a_item)
164  {
165  $order = $a_item->getOrder();
166  if (array_key_exists($order, $this->items)) {
167  $newitems = array();
168  for ($i = 0; $i < $order; $i++) {
169  array_push($newitems, $this->items[$i]);
170  }
171  array_push($newitems, $a_item);
172  for ($i = $order; $i < count($this->items); $i++) {
173  array_push($newitems, $this->items[$i]);
174  }
175  $i = 0;
176  foreach ($newitems as $idx => $item) {
177  $newitems[$idx]->setOrder($i);
178  $i++;
179  }
180  $this->items = $newitems;
181  } else {
182  array_push($this->items, $a_item);
183  }
184  }
185 
196  public function setItemPoints($order, $points)
197  {
198  foreach ($this->items as $key => $item) {
199  if ($item->getOrder() == $order) {
200  $item->setPoints($points);
201  }
202  }
203  }
204 
214  public function deleteItem($order)
215  {
216  if (array_key_exists($order, $this->items)) {
217  unset($this->items[$order]);
218  $order = 0;
219  foreach ($this->items as $key => $item) {
220  $this->items[$key]->setOrder($order);
221  $order++;
222  }
223  }
224  }
225 
234  public function setItemLowerBound($order, $bound)
235  {
236  foreach ($this->items as $key => $item) {
237  if ($item->getOrder() == $order) {
238  $item->setLowerBound($bound);
239  }
240  }
241  }
242 
253  public function setItemUpperBound($order, $bound)
254  {
255  foreach ($this->items as $key => $item) {
256  if ($item->getOrder() == $order) {
257  $item->setUpperBound($bound);
258  }
259  }
260  }
261 
272  public function getItem($a_index)
273  {
274  if (array_key_exists($a_index, $this->items)) {
275  return $this->items[$a_index];
276  } else {
277  return null;
278  }
279  }
280 
289  public function clearItems()
290  {
291  $this->items = array();
292  }
293 
301  public function setShuffle($a_shuffle = true)
302  {
303  $this->shuffle = (bool) $a_shuffle;
304  }
305 
311  public function getShuffle()
312  {
313  return $this->shuffle;
314  }
315 
320  {
321  if ($shuffler == null) {
322  require_once 'Services/Randomization/classes/class.ilArrayElementShuffler.php';
324  $seed = $shuffler->buildRandomSeed();
325  $shuffler->setSeed($seed);
326  }
327  $this->shuffler = $shuffler;
328  }
329 
334  {
335  if ($this->shuffler == null) {
336  $this->setShuffler();
337  }
338  return $this->shuffler;
339  }
340 
341 
342 
351  public function getMaxWidth()
352  {
353  $maxwidth = 0;
354  foreach ($this->items as $item) {
355  if (strlen($item->getAnswertext()) > $maxwidth) {
356  $maxwidth = strlen($item->getAnswertext());
357  }
358  }
359  return $maxwidth;
360  }
361 
370  public function getBestSolutionIndexes()
371  {
372  $maxpoints = 0;
373  foreach ($this->items as $key => $item) {
374  if ($item->getPoints() > $maxpoints) {
375  $maxpoints = $item->getPoints();
376  }
377  }
378  $keys = array();
379  foreach ($this->items as $key => $item) {
380  if ($item->getPoints() == $maxpoints) {
381  array_push($keys, $key);
382  }
383  }
384  return $keys;
385  }
386 
392  public function getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations = null)
393  {
394  global $DIC;
395  $lng = $DIC['lng'];
396  switch ($this->getType()) {
397  case CLOZE_TEXT:
398  case CLOZE_SELECT:
399  $best_solutions = array();
400  if ($combinations !== null && $combinations['best_solution'] == 1) {
401  $best_solutions[$combinations['points']] = array();
402  array_push($best_solutions[$combinations['points']], $combinations['answer']);
403  } else {
404  foreach ($this->getItems($shuffler) as $answer) {
405  if (isset($best_solutions[$answer->getPoints()]) && is_array($best_solutions[$answer->getPoints()])) {
406  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
407  } else {
408  $best_solutions[$answer->getPoints()] = array();
409  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
410  }
411  }
412  }
413 
414  krsort($best_solutions, SORT_NUMERIC);
415  reset($best_solutions);
416  $found = current($best_solutions);
417  return join(" " . $lng->txt("or") . " ", $found);
418  break;
419  case CLOZE_NUMERIC:
420  $maxpoints = 0;
421  $foundvalue = "";
422  foreach ($this->getItems($shuffler) as $answer) {
423  if ($answer->getPoints() >= $maxpoints) {
424  $maxpoints = $answer->getPoints();
425  $foundvalue = $answer->getAnswertext();
426  }
427  }
428  return $foundvalue;
429  break;
430  default:
431  return "";
432  }
433  }
434 
438  public function setGapSize($gap_size)
439  {
440  $this->gap_size = $gap_size;
441  }
442 
446  public function getGapSize()
447  {
448  return $this->gap_size;
449  }
450 
451  public function numericRangeExists()
452  {
453  if ($this->getType() != CLOZE_NUMERIC) {
454  return false;
455  }
456 
457  require_once 'Services/Math/classes/class.EvalMath.php';
458  $math = new EvalMath();
459 
460  $item = $this->getItem(0);
461  $lowerBound = $math->evaluate($item->getLowerBound());
462  $upperBound = $math->evaluate($item->getUpperBound());
463  $preciseValue = $math->evaluate($item->getAnswertext());
464 
465  if ($lowerBound < $preciseValue || $upperBound > $preciseValue) {
466  return true;
467  }
468 
469  return false;
470  }
471 }
setGapSize($gap_size)
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.
getItems(ilArrayElementShuffler $shuffler, ?int $gap_index=null)
setItemPoints($order, $points)
Sets the points for a given item.
setShuffler(ilArrayElementShuffler $shuffler=null)
getItemsRaw()
Gets the items of a cloze gap.
const CLOZE_SELECT
$keys
Definition: metadata.php:187
$lng
addItem($a_item)
Adds a gap item.
global $DIC
Definition: goto.php:24
$type
Type of gap.
getBestSolutionIndexes()
Returns the indexes of the best solutions for the gap.
getMaxWidth()
Returns the maximum width of the gap.
getItemCount()
Gets the item count.
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.
$i
Definition: metadata.php:24
__construct($a_type)
assClozeGap constructor