ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assClozeGap.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
38 {
47  var $type;
48 
56  var $items;
57 
63  var $shuffle;
64 
75  function assClozeGap($a_type)
76  {
77  $this->type = $a_type;
78  $this->items = array();
79  $this->shuffle = FALSE;
80  }
81 
91  function getType()
92  {
93  return $this->type;
94  }
95 
105  function setType($a_type = 0)
106  {
107  $this->type = $a_type;
108  }
109 
119  function getItems()
120  {
121  if ($this->shuffle)
122  {
123  return $this->arrayShuffle($this->items);
124  }
125  else
126  {
127  return $this->items;
128  }
129  }
130 
140  function getItemsRaw()
141  {
142  return $this->items;
143  }
144 
154  function getItemCount()
155  {
156  return count($this->items);
157  }
158 
168  function addItem($a_item)
169  {
170  array_push($this->items, $a_item);
171  }
172 
183  function setItemPoints($order, $points)
184  {
185  foreach ($this->items as $key => $item)
186  {
187  if ($item->getOrder() == $order)
188  {
189  $item->setPoints($points);
190  }
191  }
192  }
193 
203  function deleteItem($order)
204  {
205  if (array_key_exists($order, $this->items))
206  {
207  unset($this->items[$order]);
208  $order = 0;
209  foreach ($this->items as $key => $item)
210  {
211  $this->items[$key]->setOrder($order);
212  $order++;
213  }
214  }
215  }
216 
227  function setItemLowerBound($order, $bound)
228  {
229  foreach ($this->items as $key => $item)
230  {
231  if ($item->getOrder() == $order)
232  {
233  $item->setLowerBound($bound);
234  }
235  }
236  }
237 
248  function setItemUpperBound($order, $bound)
249  {
250  foreach ($this->items as $key => $item)
251  {
252  if ($item->getOrder() == $order)
253  {
254  $item->setUpperBound($bound);
255  }
256  }
257  }
258 
268  function getItem($a_index)
269  {
270  if (array_key_exists($a_index, $this->items))
271  {
272  return $this->items[$a_index];
273  }
274  else
275  {
276  return NULL;
277  }
278  }
279 
288  function clearItems()
289  {
290  $this->items = array();
291  }
292 
302  function setShuffle($a_shuffle = TRUE)
303  {
304  $this->shuffle = $a_shuffle ? TRUE : FALSE;
305  }
306 
316  function getShuffle()
317  {
318  return $this->shuffle;
319  }
320 
329  function arrayShuffle($array)
330  {
331  mt_srand((double)microtime()*1000000);
332  $i = count($array);
333  if ($i > 0)
334  {
335  while(--$i)
336  {
337  $j = mt_rand(0, $i);
338  if ($i != $j)
339  {
340  // swap elements
341  $tmp = $array[$j];
342  $array[$j] = $array[$i];
343  $array[$i] = $tmp;
344  }
345  }
346  }
347  return $array;
348  }
349 
358  function getMaxWidth()
359  {
360  $maxwidth = 0;
361  foreach ($this->items as $item)
362  {
363  if (strlen($item->getAnswerText()) > $maxwidth)
364  {
365  $maxwidth = strlen($item->getAnswerText());
366  }
367  }
368  return $maxwidth;
369  }
370 
380  {
381  $maxpoints = 0;
382  foreach ($this->items as $key => $item)
383  {
384  if ($item->getPoints() > $maxpoints)
385  {
386  $maxpoints = $item->getPoints();
387  }
388  }
389  $keys = array();
390  foreach ($this->items as $key => $item)
391  {
392  if ($item->getPoints() == $maxpoints)
393  {
394  array_push($keys, $key);
395  }
396  }
397  return $keys;
398  }
399 
401  {
402  global $lng;
403  switch ($this->getType())
404  {
405  case CLOZE_TEXT:
406  case CLOZE_SELECT:
407  $best_solutions = array();
408  foreach ($this->getItems() as $answer)
409  {
410  if (is_array($best_solutions[$answer->getPoints()]))
411  {
412  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
413  }
414  else
415  {
416  $best_solutions[$answer->getPoints()] = array();
417  array_push($best_solutions[$answer->getPoints()], $answer->getAnswertext());
418  }
419  }
420  krsort($best_solutions, SORT_NUMERIC);
421  reset($best_solutions);
422  $found = current($best_solutions);
423  return join(" " . $lng->txt("or") . " ", $found);
424  break;
425  case CLOZE_NUMERIC:
426  $maxpoints = 0;
427  $foundvalue = "";
428  foreach ($this->getItems() as $answer)
429  {
430  if ($answer->getPoints() >= $maxpoints)
431  {
432  $maxpoints = $answer->getPoints();
433  $foundvalue = $answer->getAnswertext();
434  }
435  }
436  return $foundvalue;
437  break;
438  default:
439  return "";
440  }
441  }
442 }
443 
444 ?>