ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 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 $gap_size = 0;
51
58 public function __construct($a_type)
59 {
60 $this->type = $a_type;
61 $this->items = array();
62 $this->shuffle = true;
63 }
64
74 public function getType()
75 {
76 return $this->type;
77 }
78
86 public function setType($a_type = 0)
87 {
88 $this->type = $a_type;
89 }
90
97 public function getItems(ilArrayElementShuffler $shuffler)
98 {
99 if ($this->getShuffle()) {
100 return $shuffler->shuffle($this->items);
101 }
102
103 return $this->items;
104 }
105
115 public function getItemsRaw()
116 {
117 return $this->items;
118 }
119
129 public function getItemCount()
130 {
131 return count($this->items);
132 }
133
143 public function addItem($a_item)
144 {
145 $order = $a_item->getOrder();
146 if (array_key_exists($order, $this->items)) {
147 $newitems = array();
148 for ($i = 0; $i < $order; $i++) {
149 array_push($newitems, $this->items[$i]);
150 }
151 array_push($newitems, $a_item);
152 for ($i = $order; $i < count($this->items); $i++) {
153 array_push($newitems, $this->items[$i]);
154 }
155 $i = 0;
156 foreach ($newitems as $idx => $item) {
157 $newitems[$idx]->setOrder($i);
158 $i++;
159 }
160 $this->items = $newitems;
161 } else {
162 array_push($this->items, $a_item);
163 }
164 }
165
176 public function setItemPoints($order, $points)
177 {
178 foreach ($this->items as $key => $item) {
179 if ($item->getOrder() == $order) {
180 $item->setPoints($points);
181 }
182 }
183 }
184
194 public function deleteItem($order)
195 {
196 if (array_key_exists($order, $this->items)) {
197 unset($this->items[$order]);
198 $order = 0;
199 foreach ($this->items as $key => $item) {
200 $this->items[$key]->setOrder($order);
201 $order++;
202 }
203 }
204 }
205
216 public function setItemLowerBound($order, $bound)
217 {
218 foreach ($this->items as $key => $item) {
219 if ($item->getOrder() == $order) {
220 $item->setLowerBound($bound);
221 }
222 }
223 }
224
235 public function setItemUpperBound($order, $bound)
236 {
237 foreach ($this->items as $key => $item) {
238 if ($item->getOrder() == $order) {
239 $item->setUpperBound($bound);
240 }
241 }
242 }
243
254 public function getItem($a_index)
255 {
256 if (array_key_exists($a_index, $this->items)) {
257 return $this->items[$a_index];
258 } else {
259 return null;
260 }
261 }
262
271 public function clearItems()
272 {
273 $this->items = array();
274 }
275
283 public function setShuffle($a_shuffle = true)
284 {
285 $this->shuffle = (bool) $a_shuffle;
286 }
287
293 public function getShuffle()
294 {
295 return $this->shuffle;
296 }
297
306 public function getMaxWidth()
307 {
308 $maxwidth = 0;
309 foreach ($this->items as $item) {
310 if (strlen($item->getAnswertext()) > $maxwidth) {
311 $maxwidth = strlen($item->getAnswertext());
312 }
313 }
314 return $maxwidth;
315 }
316
325 public function getBestSolutionIndexes()
326 {
327 $maxpoints = 0;
328 foreach ($this->items as $key => $item) {
329 if ($item->getPoints() > $maxpoints) {
330 $maxpoints = $item->getPoints();
331 }
332 }
333 $keys = array();
334 foreach ($this->items as $key => $item) {
335 if ($item->getPoints() == $maxpoints) {
336 array_push($keys, $key);
337 }
338 }
339 return $keys;
340 }
341
347 public function getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations = null)
348 {
349 global $DIC;
350 $lng = $DIC['lng'];
351 switch ($this->getType()) {
352 case CLOZE_TEXT:
353 case CLOZE_SELECT:
354 $best_solutions = array();
355 if ($combinations !== null && $combinations['best_solution'] == 1) {
356 $best_solutions[$combinations['points']] = array();
357 array_push($best_solutions[$combinations['points']], $combinations['answer']);
358 } else {
359 foreach ($this->getItems($shuffler) as $answer) {
360 if (isset($best_solutions[$answer->getPoints()]) && is_array($best_solutions[$answer->getPoints()])) {
361 array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
362 } else {
363 $best_solutions[$answer->getPoints()] = array();
364 array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
365 }
366 }
367 }
368
369 krsort($best_solutions, SORT_NUMERIC);
370 reset($best_solutions);
371 $found = current($best_solutions);
372 return join(" " . $lng->txt("or") . " ", $found);
373 break;
374 case CLOZE_NUMERIC:
375 $maxpoints = 0;
376 $foundvalue = "";
377 foreach ($this->getItems($shuffler) as $answer) {
378 if ($answer->getPoints() >= $maxpoints) {
379 $maxpoints = $answer->getPoints();
380 $foundvalue = $answer->getAnswertext();
381 }
382 }
383 return $foundvalue;
384 break;
385 default:
386 return "";
387 }
388 }
389
393 public function setGapSize($gap_size)
394 {
395 $this->gap_size = $gap_size;
396 }
397
401 public function getGapSize()
402 {
403 return $this->gap_size;
404 }
405
406 public function numericRangeExists()
407 {
408 if ($this->getType() != CLOZE_NUMERIC) {
409 return false;
410 }
411
412 require_once 'Services/Math/classes/class.EvalMath.php';
413 $math = new EvalMath();
414
415 $item = $this->getItem(0);
416 $lowerBound = $math->evaluate($item->getLowerBound());
417 $upperBound = $math->evaluate($item->getUpperBound());
418 $preciseValue = $math->evaluate($item->getAnswertext());
419
420 if ($lowerBound < $preciseValue || $upperBound > $preciseValue) {
421 return true;
422 }
423
424 return false;
425 }
426}
An exception for terminatinating execution or to throw for unit testing.
Class for cloze question gaps.
getBestSolutionOutput(ilArrayElementShuffler $shuffler, $combinations=null)
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.
getItemCount()
Gets the item count.
setItemPoints($order, $points)
Sets the points for a given item.
getItems(ilArrayElementShuffler $shuffler)
Gets the items of a cloze gap.
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.
setGapSize($gap_size)
getType()
Gets the cloze gap type.
getItem($a_index)
Gets the item with a given index.
__construct($a_type)
assClozeGap constructor
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.
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
const CLOZE_NUMERIC
const CLOZE_SELECT
const CLOZE_TEXT
Cloze question constants.
$keys
global $DIC
Definition: saml.php:7
$lng
$a_type
Definition: workflow.php:92