ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  public $type;
30 
38  public $items;
39 
45  public $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  return $shuffler->shuffle($this->items);
98  }
99 
100  return $this->items;
101  }
102 
112  public function getItemsRaw()
113  {
114  return $this->items;
115  }
116 
126  public function getItemCount()
127  {
128  return count($this->items);
129  }
130 
140  public function addItem($a_item)
141  {
142  $order = $a_item->getOrder();
143  if (array_key_exists($order, $this->items)) {
144  $newitems = array();
145  for ($i = 0; $i < $order; $i++) {
146  array_push($newitems, $this->items[$i]);
147  }
148  array_push($newitems, $a_item);
149  for ($i = $order; $i < count($this->items); $i++) {
150  array_push($newitems, $this->items[$i]);
151  }
152  $i = 0;
153  foreach ($newitems as $idx => $item) {
154  $newitems[$idx]->setOrder($i);
155  $i++;
156  }
157  $this->items = $newitems;
158  } else {
159  array_push($this->items, $a_item);
160  }
161  }
162 
173  public function setItemPoints($order, $points)
174  {
175  foreach ($this->items as $key => $item) {
176  if ($item->getOrder() == $order) {
177  $item->setPoints($points);
178  }
179  }
180  }
181 
191  public function deleteItem($order)
192  {
193  if (array_key_exists($order, $this->items)) {
194  unset($this->items[$order]);
195  $order = 0;
196  foreach ($this->items as $key => $item) {
197  $this->items[$key]->setOrder($order);
198  $order++;
199  }
200  }
201  }
202 
213  public function setItemLowerBound($order, $bound)
214  {
215  foreach ($this->items as $key => $item) {
216  if ($item->getOrder() == $order) {
217  $item->setLowerBound($bound);
218  }
219  }
220  }
221 
232  public function setItemUpperBound($order, $bound)
233  {
234  foreach ($this->items as $key => $item) {
235  if ($item->getOrder() == $order) {
236  $item->setUpperBound($bound);
237  }
238  }
239  }
240 
250  public function getItem($a_index)
251  {
252  if (array_key_exists($a_index, $this->items)) {
253  return $this->items[$a_index];
254  } else {
255  return null;
256  }
257  }
258 
267  public function clearItems()
268  {
269  $this->items = array();
270  }
271 
279  public function setShuffle($a_shuffle = true)
280  {
281  $this->shuffle = (bool) $a_shuffle;
282  }
283 
289  public function getShuffle()
290  {
291  return $this->shuffle;
292  }
293 
302  public function getMaxWidth()
303  {
304  $maxwidth = 0;
305  foreach ($this->items as $item) {
306  if (strlen($item->getAnswertext()) > $maxwidth) {
307  $maxwidth = strlen($item->getAnswertext());
308  }
309  }
310  return $maxwidth;
311  }
312 
321  public function getBestSolutionIndexes()
322  {
323  $maxpoints = 0;
324  foreach ($this->items as $key => $item) {
325  if ($item->getPoints() > $maxpoints) {
326  $maxpoints = $item->getPoints();
327  }
328  }
329  $keys = array();
330  foreach ($this->items as $key => $item) {
331  if ($item->getPoints() == $maxpoints) {
332  array_push($keys, $key);
333  }
334  }
335  return $keys;
336  }
337 
343  public function getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations = null)
344  {
345  global $lng;
346  switch ($this->getType()) {
347  case CLOZE_TEXT:
348  case CLOZE_SELECT:
349  $best_solutions = array();
350  if ($combinations !== null && $combinations['best_solution'] == 1) {
351  $best_solutions[$combinations['points']] = array();
352  array_push($best_solutions[$combinations['points']], $combinations['answer']);
353  } else {
354  foreach ($this->getItems($shuffler) as $answer) {
355  if (isset($best_solutions[$answer->getPoints()]) && is_array($best_solutions[$answer->getPoints()])) {
356  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
357  } else {
358  $best_solutions[$answer->getPoints()] = array();
359  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
360  }
361  }
362  }
363 
364  krsort($best_solutions, SORT_NUMERIC);
365  reset($best_solutions);
366  $found = current($best_solutions);
367  return join(" " . $lng->txt("or") . " ", $found);
368  break;
369  case CLOZE_NUMERIC:
370  $maxpoints = 0;
371  $foundvalue = "";
372  foreach ($this->getItems($shuffler) as $answer) {
373  if ($answer->getPoints() >= $maxpoints) {
374  $maxpoints = $answer->getPoints();
375  $foundvalue = $answer->getAnswertext();
376  }
377  }
378  return $foundvalue;
379  break;
380  default:
381  return "";
382  }
383  }
384 
388  public function setGapSize($gap_size)
389  {
390  $this->gap_size = $gap_size;
391  }
392 
396  public function getGapSize()
397  {
398  return $this->gap_size;
399  }
400 }
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.
setItemPoints($order, $points)
Sets the points for a given item.
$keys
$a_type
Definition: workflow.php:92
getItemsRaw()
Gets the items of a cloze gap.
const CLOZE_SELECT
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
$i
Definition: disco.tpl.php:19
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.
$key
Definition: croninfo.php:18
__construct($a_type)
assClozeGap constructor