ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
4include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
5
19{
20
29 var $type;
30
38 var $items;
39
46
47 private $gap_size = 0;
48
55 public function assClozeGap($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
97 function getItems()
98 {
99 if ($this->shuffle)
100 {
101 return $this->arrayShuffle($this->items);
102 }
103 else
104 {
105 return $this->items;
106 }
107 }
108
118 function getItemsRaw()
119 {
120 return $this->items;
121 }
122
132 function getItemCount()
133 {
134 return count($this->items);
135 }
136
146 function addItem($a_item)
147 {
148 $order = $a_item->getOrder();
149 if (array_key_exists($order, $this->items))
150 {
151 $newitems = array();
152 for ($i = 0; $i < $order; $i++)
153 {
154 array_push($newitems, $this->items[$i]);
155 }
156 array_push($newitems, $a_item);
157 for ($i = $order; $i < count($this->items); $i++)
158 {
159 array_push($newitems, $this->items[$i]);
160 }
161 $i = 0;
162 foreach ($newitems as $idx => $item)
163 {
164 $newitems[$idx]->setOrder($i);
165 $i++;
166 }
167 $this->items = $newitems;
168 }
169 else
170 {
171 array_push($this->items, $a_item);
172 }
173 }
174
185 function setItemPoints($order, $points)
186 {
187 foreach ($this->items as $key => $item)
188 {
189 if ($item->getOrder() == $order)
190 {
191 $item->setPoints($points);
192 }
193 }
194 }
195
205 function deleteItem($order)
206 {
207 if (array_key_exists($order, $this->items))
208 {
209 unset($this->items[$order]);
210 $order = 0;
211 foreach ($this->items as $key => $item)
212 {
213 $this->items[$key]->setOrder($order);
214 $order++;
215 }
216 }
217 }
218
229 function setItemLowerBound($order, $bound)
230 {
231 foreach ($this->items as $key => $item)
232 {
233 if ($item->getOrder() == $order)
234 {
235 $item->setLowerBound($bound);
236 }
237 }
238 }
239
250 function setItemUpperBound($order, $bound)
251 {
252 foreach ($this->items as $key => $item)
253 {
254 if ($item->getOrder() == $order)
255 {
256 $item->setUpperBound($bound);
257 }
258 }
259 }
260
270 function getItem($a_index)
271 {
272 if (array_key_exists($a_index, $this->items))
273 {
274 return $this->items[$a_index];
275 }
276 else
277 {
278 return NULL;
279 }
280 }
281
290 function clearItems()
291 {
292 $this->items = array();
293 }
294
302 public function setShuffle($a_shuffle = true)
303 {
304 $this->shuffle = (bool) $a_shuffle;
305 }
306
312 public function getShuffle()
313 {
314 return $this->shuffle;
315 }
316
331 function arrayShuffle($array)
332 {
333 mt_srand((double)microtime()*1000000);
334 $i = count($array);
335 if ($i > 0)
336 {
337 while(--$i)
338 {
339 $j = mt_rand(0, $i);
340 if ($i != $j)
341 {
342 // swap elements
343 $tmp = $array[$j];
344 $array[$j] = $array[$i];
345 $array[$i] = $tmp;
346 }
347 }
348 }
349 return $array;
350 }
351
360 function getMaxWidth()
361 {
362 $maxwidth = 0;
363 foreach ($this->items as $item)
364 {
365 if (strlen($item->getAnswertext()) > $maxwidth)
366 {
367 $maxwidth = strlen($item->getAnswertext());
368 }
369 }
370 return $maxwidth;
371 }
372
382 {
383 $maxpoints = 0;
384 foreach ($this->items as $key => $item)
385 {
386 if ($item->getPoints() > $maxpoints)
387 {
388 $maxpoints = $item->getPoints();
389 }
390 }
391 $keys = array();
392 foreach ($this->items as $key => $item)
393 {
394 if ($item->getPoints() == $maxpoints)
395 {
396 array_push($keys, $key);
397 }
398 }
399 return $keys;
400 }
401
403 {
404 global $lng;
405 switch ($this->getType())
406 {
407 case CLOZE_TEXT:
408 case CLOZE_SELECT:
409 $best_solutions = array();
410 foreach ($this->getItems() as $answer)
411 {
412 if (is_array($best_solutions[$answer->getPoints()]))
413 {
414 array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
415 }
416 else
417 {
418 $best_solutions[$answer->getPoints()] = array();
419 array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
420 }
421 }
422 krsort($best_solutions, SORT_NUMERIC);
423 reset($best_solutions);
424 $found = current($best_solutions);
425 return join(" " . $lng->txt("or") . " ", $found);
426 break;
427 case CLOZE_NUMERIC:
428 $maxpoints = 0;
429 $foundvalue = "";
430 foreach ($this->getItems() as $answer)
431 {
432 if ($answer->getPoints() >= $maxpoints)
433 {
434 $maxpoints = $answer->getPoints();
435 $foundvalue = $answer->getAnswertext();
436 }
437 }
438 return $foundvalue;
439 break;
440 default:
441 return "";
442 }
443 }
444
448 public function setGapSize( $gap_size)
449 {
450 $this->gap_size = $gap_size;
451 }
452
456 public function getGapSize()
457 {
458 return $this->gap_size;
459 }
460}
Class for cloze question gaps.
setGapSize( $gap_size)
setItemLowerBound($order, $bound)
Sets the lower bound for a given item.
deleteItem($order)
Deletes an item at a given index.
getBestSolutionIndexes()
Returns the indexes of the best solutions for the gap.
$type
Type of gap.
getItems()
Gets the items of a cloze gap.
assClozeGap($a_type)
assClozeGap constructor
getItemCount()
Gets the item count.
arrayShuffle($array)
Shuffles the values of a given array.
setItemPoints($order, $points)
Sets the points for a given item.
getShuffle()
Gets the shuffle state of the items.
clearItems()
Removes all gap items.
getMaxWidth()
Returns the maximum width of the gap.
setShuffle($a_shuffle=true)
Sets the shuffle state of the items.
getType()
Gets the cloze gap type.
getItem($a_index)
Gets the item with a given index.
getItemsRaw()
Gets the items of a cloze gap.
addItem($a_item)
Adds a gap item.
setItemUpperBound($order, $bound)
Sets the upper bound for a given item.
setType($a_type=0)
Sets the cloze gap type.
const CLOZE_NUMERIC
const CLOZE_SELECT
const CLOZE_TEXT
Cloze question constants.
global $lng
Definition: privfeed.php:40