ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilTestRandomQuestionSetPoolDeriver.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.ilTestRandomQuestionSetStagingPoolQuestionList.php';
5 require_once 'Modules/TestQuestionPool/classes/class.ilQuestionPoolFactory.php';
6 require_once 'Modules/TestQuestionPool/classes/class.assQuestion.php';
7 
15 {
19  protected $db;
20 
24  protected $pluginAdmin;
25 
29  protected $testOBJ;
30 
35 
39  protected $ownerId;
40 
44  protected $poolFactory;
45 
50 
52  {
53  $this->db = $ilDB;
54  $this->pluginAdmin = $pluginAdmin;
55  $this->testOBJ = $testOBJ;
56  $this->poolFactory = new ilQuestionPoolFactory();
57  }
58 
62  public function getTargetContainerRef()
63  {
65  }
66 
71  {
72  $this->targetContainerRef = $targetContainerRef;
73  }
74 
78  public function getOwnerId()
79  {
80  return $this->ownerId;
81  }
82 
86  public function setOwnerId($ownerId)
87  {
88  $this->ownerId = $ownerId;
89  }
90 
94  public function getSourcePoolDefinitionList()
95  {
97  }
98 
103  {
104  $this->sourcePoolDefinitionList = $sourcePoolDefinitionList;
105  }
106 
107  protected function getQuestionsForPool(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
108  {
110  $this->db, $this->pluginAdmin
111  );
112 
113  $questionList->setTestObjId($this->testOBJ->getId());
114  $questionList->setTestId($this->testOBJ->getTestId());
115  $questionList->setPoolId($nonAvailablePool->getId());
116 
117  $questionList->loadQuestions();
118 
119  $questions = array();
120 
121  foreach($questionList as $questionId)
122  {
123  $questions[] = assQuestion::_instantiateQuestion($questionId);
124  }
125 
126  return $questions;
127  }
128 
129  protected function createNewPool(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
130  {
131  $pool = $this->poolFactory->createNewInstance($this->getTargetContainerRef());
132 
133  if( strlen($nonAvailablePool->getTitle()) )
134  {
135  $pool->setTitle($nonAvailablePool->getTitle());
136  $pool->update();
137  }
138 
139  return $pool;
140  }
141 
142  protected function copyQuestionsToPool(ilObjQuestionPool $pool, $questions)
143  {
144  $poolQidByTestQidMap = array();
145 
146  foreach($questions as $questionOBJ)
147  {
148  /* @var assQuestion $questionOBJ */
149 
150  $testQuestionId = $questionOBJ->getId();
151  $poolQuestionId = $questionOBJ->duplicate(false, '', '', $this->getOwnerId(), $pool->getId());
152 
153  $poolQidByTestQidMap[$testQuestionId] = $poolQuestionId;
154  }
155 
156  return $poolQidByTestQidMap;
157  }
158 
159  protected function updateTestQuestionStage($poolQidByTestQidMap)
160  {
161  foreach($poolQidByTestQidMap as $testQid => $poolQid)
162  {
164  assQuestion::saveOriginalId($testQid, $poolQid);
165  }
166  }
167 
168  protected function filterForQuestionRelatedTaxonomies($taxonomyIds, $relatedQuestionIds)
169  {
170  require_once 'Services/Taxonomy/classes/class.ilTaxNodeAssignment.php';
171 
172  $filteredTaxIds = array();
173 
174  foreach($taxonomyIds as $taxonomyId)
175  {
176  $taxNodeAssignment = new ilTaxNodeAssignment(
177  $this->testOBJ->getType(), $this->testOBJ->getId(), 'quest', $taxonomyId
178  );
179 
180  foreach($relatedQuestionIds as $questionId)
181  {
182  $assignedTaxNodes = $taxNodeAssignment->getAssignmentsOfItem($questionId);
183 
184  if( count($assignedTaxNodes) )
185  {
186  $filteredTaxIds[] = $taxonomyId;
187  break;
188  }
189  }
190 
191  }
192 
193  return $filteredTaxIds;
194  }
195 
196  protected function duplicateTaxonomies($poolQidByTestQidMap, ilObjQuestionPool $pool)
197  {
198  require_once 'Modules/TestQuestionPool/classes/class.ilQuestionPoolTaxonomiesDuplicator.php';
199  $taxDuplicator = new ilQuestionPoolTaxonomiesDuplicator();
200  $taxDuplicator->setSourceObjId($this->testOBJ->getId());
201  $taxDuplicator->setSourceObjType($this->testOBJ->getType());
202  $taxDuplicator->setTargetObjId($pool->getId());
203  $taxDuplicator->setTargetObjType($pool->getType());
204  $taxDuplicator->setQuestionIdMapping($poolQidByTestQidMap);
205 
206  $taxDuplicator->duplicate($this->filterForQuestionRelatedTaxonomies(
207  $taxDuplicator->getAllTaxonomiesForSourceObject(), array_keys($poolQidByTestQidMap)
208  ));
209 
210  return $taxDuplicator->getDuplicatedTaxonomiesKeysMap();
211  }
212 
213  protected function buildOriginalTaxonomyFilterForDerivedPool(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxKeysMap, $mappedTaxonomyFilter)
214  {
215  $originalTaxonomyFilter = array();
216 
217  foreach($mappedTaxonomyFilter as $testTaxonomyId => $testTaxNodes)
218  {
219  $poolTaxonomyId = $taxKeysMap->getMappedTaxonomyId($testTaxonomyId);
220  $originalTaxonomyFilter[$poolTaxonomyId] = array();
221 
222  foreach($testTaxNodes as $testTaxNode)
223  {
224  $poolTaxNode = $taxKeysMap->getMappedTaxNodeId($testTaxNode);
225  $originalTaxonomyFilter[$poolTaxonomyId][] = $poolTaxNode;
226  }
227  }
228 
229  return $originalTaxonomyFilter;
230  }
231 
232  protected function updateRelatedSourcePoolDefinitions(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxKeysMap, $derivedPoolId, $nonAvailablePoolId)
233  {
234  foreach($this->getSourcePoolDefinitionList() as $definition)
235  {
236  if( $definition->getPoolId() != $nonAvailablePoolId )
237  {
238  continue;
239  }
240 
241  $definition->setPoolId($derivedPoolId);
242 
243  $definition->setOriginalTaxonomyFilter( $this->buildOriginalTaxonomyFilterForDerivedPool(
244  $taxKeysMap, $definition->getMappedTaxonomyFilter()
245  ));
246 
247  $definition->saveToDb();
248  }
249  }
250 
255  public function derive(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
256  {
257  $pool = $this->createNewPool($nonAvailablePool);
258  $questions = $this->getQuestionsForPool($nonAvailablePool);
259 
260  $poolQidByTestQidMap = $this->copyQuestionsToPool($pool, $questions);
261 
262  $this->updateTestQuestionStage($poolQidByTestQidMap);
263 
264  $duplicatedTaxKeysMap = $this->duplicateTaxonomies($poolQidByTestQidMap, $pool);
265 
266  $this->updateRelatedSourcePoolDefinitions($duplicatedTaxKeysMap, $pool->getId(), $nonAvailablePool->getId());
267 
268  return $pool;
269  }
270 }
static resetOriginalId($questionId)
getAssignmentsOfItem($a_item_id)
Get assignments for item.
Taxonomy node <-> item assignment.
copyQuestionsToPool(ilObjQuestionPool $pool, $questions)
derive(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
Administration class for plugins.
Interface ilDBInterface.
getId()
get object id public
duplicateTaxonomies($poolQidByTestQidMap, ilObjQuestionPool $pool)
__construct(ilDBInterface $ilDB, ilPluginAdmin $pluginAdmin, ilObjTest $testOBJ)
getQuestionsForPool(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
static saveOriginalId($questionId, $originalId)
getType()
get object type public
Create styles array
The data for the language used.
static _instantiateQuestion($question_id)
updateRelatedSourcePoolDefinitions(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxKeysMap, $derivedPoolId, $nonAvailablePoolId)
buildOriginalTaxonomyFilterForDerivedPool(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxKeysMap, $mappedTaxonomyFilter)
global $ilDB
createNewPool(ilTestRandomQuestionSetNonAvailablePool $nonAvailablePool)
filterForQuestionRelatedTaxonomies($taxonomyIds, $relatedQuestionIds)