ILIAS  Release_5_0_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 
4 require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetQuestionCollection.php';
5 
13 {
17  protected $db = null;
18 
22  protected $testOBJ = null;
23 
27  protected $questionSetConfig = null;
28 
32  protected $sourcePoolDefinitionList = null;
33 
37  protected $stagingPoolQuestionList = null;
38 
46  protected function __construct(
47  ilDB $db,
52  )
53  {
54  $this->db = $db;
55  $this->testOBJ = $testOBJ;
56  $this->questionSetConfig = $questionSetConfig;
57  $this->sourcePoolDefinitionList = $sourcePoolDefinitionList;
58  $this->stagingPoolQuestionList = $stagingPoolQuestionList;
59  }
60 
61  abstract public function checkBuildable();
62 
63  abstract public function performBuild(ilTestSession $testSession);
64 
65 
66  protected function getQuestionStageForSourcePoolDefinitionList(ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList)
67  {
68  $questionStage = new ilTestRandomQuestionSetQuestionCollection();
69 
70  foreach($sourcePoolDefinitionList as $definition)
71  {
74  $questions = $this->getQuestionStageForSourcePoolDefinition($definition);
75  $questionStage->mergeQuestionCollection($questions);
76  }
77 
78  return $questionStage->getUniqueQuestionCollection();
79  }
80 
82  {
83  $questionIds = $this->getQuestionIdsForSourcePoolDefinitionIds($definition);
84  $questionStage = $this->buildSetQuestionCollection($definition, $questionIds);
85 
86  return $questionStage;
87  }
88 
90  {
91  $this->stagingPoolQuestionList->resetQuestionList();
92 
93  $this->stagingPoolQuestionList->setTestObjId( $this->testOBJ->getId() );
94  $this->stagingPoolQuestionList->setTestId( $this->testOBJ->getTestId() );
95  $this->stagingPoolQuestionList->setPoolId( $definition->getPoolId() );
96 
97  if( $this->hasTaxonomyFilter($definition) )
98  {
99  $this->stagingPoolQuestionList->addTaxonomyFilter(
100  $definition->getMappedFilterTaxId(), array($definition->getMappedFilterTaxNodeId())
101  );
102  }
103 
104  $this->stagingPoolQuestionList->loadQuestions();
105 
106  return $this->stagingPoolQuestionList->getQuestions();
107  }
108 
110  {
111  $setQuestionCollection = new ilTestRandomQuestionSetQuestionCollection();
112 
113  foreach($questionIds as $questionId)
114  {
115  $setQuestion = new ilTestRandomQuestionSetQuestion();
116 
117  $setQuestion->setQuestionId($questionId);
118  $setQuestion->setSourcePoolDefinitionId($definition->getId());
119 
120  $setQuestionCollection->addQuestion($setQuestion);
121  }
122 
123  return $setQuestionCollection;
124  }
125 
127  {
128  if( !(int)$definition->getMappedFilterTaxId() )
129  {
130  return false;
131  }
132 
133  if( !(int)$definition->getMappedFilterTaxNodeId() )
134  {
135  return false;
136  }
137 
138  return true;
139  }
140 
141  protected function storeQuestionSet(ilTestSession $testSession, $questionSet)
142  {
143  $position = 0;
144 
145  foreach($questionSet->getQuestions() as $setQuestion)
146  {
147  /* @var ilTestRandomQuestionSetQuestion $setQuestion */
148 
149  $setQuestion->setSequencePosition($position++);
150 
151  $this->storeQuestion($testSession, $setQuestion);
152  }
153  }
154 
155  private function storeQuestion(ilTestSession $testSession, ilTestRandomQuestionSetQuestion $setQuestion)
156  {
157  $nextId = $this->db->nextId('tst_test_rnd_qst');
158 
159  $this->db->insert('tst_test_rnd_qst', array(
160  'test_random_question_id' => array('integer', $nextId),
161  'active_fi' => array('integer', $testSession->getActiveId()),
162  'question_fi' => array('integer', $setQuestion->getQuestionId()),
163  'sequence' => array('integer', $setQuestion->getSequencePosition()),
164  'pass' => array('integer', $testSession->getPass()),
165  'tstamp' => array('integer', time()),
166  'src_pool_def_fi' => array('integer', $setQuestion->getSourcePoolDefinitionId())
167  ));
168  }
169 
170  protected function fetchQuestionsFromStageRandomly(ilTestRandomQuestionSetQuestionCollection $questionStage, $requiredQuestionAmount)
171  {
172  $questionSet = $questionStage->getRandomQuestionCollection($requiredQuestionAmount);
173 
174  return $questionSet;
175  }
176 
178  {
179  if( $this->testOBJ->getShuffleQuestions() )
180  {
181  $questionSet->shuffleQuestions();
182  }
183  }
184 
185  // =================================================================================================================
186 
187  final static public function getInstance(
189  ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList,
191  )
192  {
193  if( $questionSetConfig->isQuestionAmountConfigurationModePerPool() )
194  {
195  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetBuilderWithAmountPerPool.php';
196 
198  $db, $testOBJ, $questionSetConfig, $sourcePoolDefinitionList, $stagingPoolQuestionList
199  );
200  }
201 
202  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetBuilderWithAmountPerTest.php';
203 
205  $db, $testOBJ, $questionSetConfig, $sourcePoolDefinitionList, $stagingPoolQuestionList
206  );
207  }
208 }