ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestRandomQuestionSetBuilderWithAmountPerPool.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.ilTestRandomQuestionSetBuilder.php';
5 
13 {
14  public function checkBuildable()
15  {
16  $questionStage = $this->getQuestionStageForSourcePoolDefinitionList($this->sourcePoolDefinitionList);
17  return $this->isQuestionSetFetchable($this->sourcePoolDefinitionList, $questionStage);
18  }
19 
21  {
22  $requiredAmount = $this->getRequiredQuestionAmountForDefinitionList($sourcePoolDefinitionList);
23  $possibleAmount = count($questionStage);
24 
25  return ( $possibleAmount >= $requiredAmount );
26  }
27 
28  public function performBuild(ilTestSession $testSession)
29  {
30  $questionSet = array();
31 
32  foreach($this->sourcePoolDefinitionList as $definition)
33  {
36  $requiredQuestionAmount = $definition->getQuestionAmount();
37 
38  $potentialQuestionStage = $this->getQuestionStageForSourcePoolDefinition($definition);
39 
41  $potentialQuestionStage, $questionSet
42  );
43 
44  if( $this->questionCollectionGreaterThanRequiredAmount($actualQuestionStage, $requiredQuestionAmount) )
45  {
46  $questions = $this->fetchQuestionsFromStageRandomly($actualQuestionStage, $requiredQuestionAmount);
47  }
48  else
49  {
50  $questions = $actualQuestionStage;
51  }
52 
53  $questionSet = $this->mergeQuestionCollections($questionSet, $questions);
54  }
55 
56  $requiredQuestionAmount = self::getRequiredQuestionAmountForDefinitionList($this->sourcePoolDefinitionList);
57 
58  if( $this->questionCollectionSmallerThanRequiredAmount($questionSet, $requiredQuestionAmount) )
59  {
60  $missingQuestionCount = $this->getMissingQuestionCount($questionSet, $requiredQuestionAmount);
61  $questionStage = $this->getQuestionStageForSourcePoolDefinitionList($this->sourcePoolDefinitionList);
62  $questions = $this->fetchQuestionsFromStageRandomly($questionStage, $missingQuestionCount);
63 
64  $questionSet = $this->mergeQuestionCollections($questionSet, $questions);
65  }
66 
67  $questionSet = $this->handleQuestionOrdering($questionSet);
68 
69  $this->storeQuestionSet($testSession, $questionSet);
70  }
71 
72  private function getUniqueQuestionCollectionFromPotentialQuestionCollection($potentialQuestionCollection, $otherQuestionCollection)
73  {
74  $uniqueQuestionCollection = array_diff($potentialQuestionCollection, $otherQuestionCollection);
75  return $uniqueQuestionCollection;
76  }
77 
78  private function questionCollectionGreaterThanRequiredAmount($questionCollection, $requiredAmount)
79  {
80  return count($questionCollection) > $requiredAmount;
81  }
82 
83  private function questionCollectionSmallerThanRequiredAmount($questionCollection, $requiredAmount)
84  {
85  return count($questionCollection) < $requiredAmount;
86  }
87 
88  private function mergeQuestionCollections($questionSet, $questions)
89  {
90  return array_merge($questionSet, $questions);
91  }
92 
93  private function getMissingQuestionCount($questionSet, $requiredQuestionAmount)
94  {
95  return ( $requiredQuestionAmount - count($questionSet) );
96  }
97 
98  public static function getRequiredQuestionAmountForDefinitionList(ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList)
99  {
100  $requiredQuestionAmountPerTest = 0;
101 
102  foreach($sourcePoolDefinitionList as $definition)
103  {
105  $requiredQuestionAmountPerTest += $definition->getQuestionAmount();
106  }
107 
108  return $requiredQuestionAmountPerTest;
109  }
110 }