ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestRandomQuestionSetQuestionCollection.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 require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetQuestion.php';
5 
13 {
14  private $questions = array();
15 
16  public function setQuestions($questions)
17  {
18  $this->questions = $questions;
19  }
20 
21  public function getQuestions()
22  {
23  return $this->questions;
24  }
25 
26  public function addQuestion(ilTestRandomQuestionSetQuestion $question)
27  {
28  $this->questions[] = $question;
29  }
30 
31  public function isGreaterThan($amount)
32  {
33  return count($this->questions) > $amount;
34  }
35 
36  public function isSmallerThan($amount)
37  {
38  return count($this->questions) < $amount;
39  }
40 
41  public function getMissingCount($amount)
42  {
43  return $amount - count($this->questions);
44  }
45 
46  public function shuffleQuestions()
47  {
48  shuffle($this->questions);
49  }
50 
51  public function mergeQuestionCollection(self $questionCollection)
52  {
53  $this->questions = array_merge( $this->questions, $questionCollection->getQuestions() );
54  }
55 
56  public function getUniqueQuestionCollection()
57  {
58  $uniqueQuestions = array();
59 
60  foreach($this->getQuestions() as $question)
61  {
62  /* @var ilTestRandomQuestionSetQuestion $question */
63 
64  if( !isset($uniqueQuestions[$question->getQuestionId()]) )
65  {
66  $uniqueQuestions[$question->getQuestionId()] = $question;
67  }
68  }
69 
70  $uniqueQuestionCollection = new self();
71  $uniqueQuestionCollection->setQuestions($uniqueQuestions);
72 
73  return $uniqueQuestionCollection;
74  }
75 
76  public function getRelativeComplementCollection(self $questionCollection)
77  {
78  $questionIds = array_flip( $questionCollection->getInvolvedQuestionIds() );
79 
80  $relativeComplementCollection = new self();
81 
82  foreach($this->getQuestions() as $question)
83  {
84  if( !isset($questionIds[$question->getQuestionId()]) )
85  {
86  $relativeComplementCollection->addQuestion($question);
87  }
88  }
89 
90  return $relativeComplementCollection;
91  }
92 
93  public function getInvolvedQuestionIds()
94  {
95  $questionIds = array();
96 
97  foreach($this->getQuestions() as $question)
98  {
99  $questionIds[] = $question->getQuestionId();
100  }
101 
102  return $questionIds;
103  }
104 
105  public function getRandomQuestionCollection($requiredAmount)
106  {
107  $randomKeys = $this->getRandomArrayKeys($this->questions, $requiredAmount);
108 
109  $randomQuestionCollection = new self();
110 
111  foreach($randomKeys as $randomKey)
112  {
113  $randomQuestionCollection->addQuestion( $this->questions[$randomKey] );
114  }
115 
116  return $randomQuestionCollection;
117  }
118 
119  private function getRandomArrayKeys($array, $numKeys)
120  {
121  if( $numKeys < 1 )
122  {
123  return array();
124  }
125 
126  if( $numKeys > 1 )
127  {
128  return array_rand($array, $numKeys);
129  }
130 
131  return array( array_rand($array, $numKeys) );
132  }
133 }