ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestRandomQuestionSetBuilder.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
15  protected $db = null;
16 
20  protected $testOBJ = null;
21 
25  protected $questionSetConfig = null;
26 
30  protected $sourcePoolDefinitionList = null;
31 
35  protected $stagingPoolQuestionList = null;
36 
44  protected function __construct(
45  ilDB $db,
50  )
51  {
52  $this->db = $db;
53  $this->testOBJ = $testOBJ;
54  $this->questionSetConfig = $questionSetConfig;
55  $this->sourcePoolDefinitionList = $sourcePoolDefinitionList;
56  $this->stagingPoolQuestionList = $stagingPoolQuestionList;
57  }
58 
59  abstract public function checkBuildable();
60 
61  abstract public function performBuild(ilTestSession $testSession);
62 
63 
64  protected function getQuestionStageForSourcePoolDefinitionList(ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList)
65  {
66  $questionStage = array();
67 
68  foreach($sourcePoolDefinitionList as $definition)
69  {
72  $questions = $this->getQuestionStageForSourcePoolDefinition($definition);
73  $questionStage = array_merge($questionStage, $questions);
74  }
75 
76  return array_unique($questionStage);
77  }
78 
80  {
81  $this->stagingPoolQuestionList->resetQuestionList();
82 
83  $this->stagingPoolQuestionList->setTestObjId( $this->testOBJ->getId() );
84  $this->stagingPoolQuestionList->setTestId( $this->testOBJ->getTestId() );
85  $this->stagingPoolQuestionList->setPoolId( $definition->getPoolId() );
86 
87  if( $this->hasTaxonomyFilter($definition) )
88  {
89  $this->stagingPoolQuestionList->addTaxonomyFilter(
90  $definition->getMappedFilterTaxId(), array($definition->getMappedFilterTaxNodeId())
91  );
92  }
93 
94  $this->stagingPoolQuestionList->loadQuestions();
95 
96  return $this->stagingPoolQuestionList->getQuestions();
97  }
98 
100  {
101  if( !(int)$definition->getMappedFilterTaxId() )
102  {
103  return false;
104  }
105 
106  if( !(int)$definition->getMappedFilterTaxNodeId() )
107  {
108  return false;
109  }
110 
111  return true;
112  }
113 
114  protected function storeQuestionSet(ilTestSession $testSession, $questionSet)
115  {
116  foreach($questionSet as $sequencePosition => $questionId)
117  {
118  $this->storeQuestion($testSession, $questionId, $sequencePosition);
119  }
120  }
121 
122  private function storeQuestion(ilTestSession $testSession, $questionId, $sequencePosition)
123  {
124  $nextId = $this->db->nextId('tst_test_rnd_qst');
125 
126  $this->db->insert('tst_test_rnd_qst', array(
127  'test_random_question_id' => array('integer', $nextId),
128  'active_fi' => array('integer', $testSession->getActiveId()),
129  'question_fi' => array('integer', $questionId),
130  'sequence' => array('integer', $sequencePosition),
131  'pass' => array('integer', $testSession->getPass()),
132  'tstamp' => array('integer', time())
133  ));
134  }
135 
136  protected function fetchQuestionsFromStageRandomly($questionStage, $requiredQuestionAmount)
137  {
138  $randomKeys = $this->getRandomArrayKeys($questionStage, $requiredQuestionAmount);
139 
140  $questionSet = array();
141 
142  foreach($randomKeys as $randomKey)
143  {
144  $questionSet[] = $questionStage[$randomKey];
145  }
146 
147  return $questionSet;
148  }
149 
150  private function getRandomArrayKeys($array, $numKeys)
151  {
152  if( $numKeys < 1 )
153  {
154  return array();
155  }
156 
157  if( $numKeys > 1 )
158  {
159  return array_rand($array, $numKeys);
160  }
161 
162  return array( array_rand($array, $numKeys) );
163  }
164 
165  protected function handleQuestionOrdering($questionSet)
166  {
167  if( $this->testOBJ->getShuffleQuestions() )
168  {
169  shuffle($questionSet);
170  }
171 
172  return $questionSet;
173  }
174 
175  // =================================================================================================================
176 
177  final static public function getInstance(
179  ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList,
181  )
182  {
183  if( $questionSetConfig->isQuestionAmountConfigurationModePerPool() )
184  {
185  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetBuilderWithAmountPerPool.php';
186 
188  $db, $testOBJ, $questionSetConfig, $sourcePoolDefinitionList, $stagingPoolQuestionList
189  );
190  }
191 
192  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetBuilderWithAmountPerTest.php';
193 
195  $db, $testOBJ, $questionSetConfig, $sourcePoolDefinitionList, $stagingPoolQuestionList
196  );
197  }
198 }