1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * @author		Björn Heyser <bheyser@databay.de>
6 * @version		$Id$
7 *
8 * @package		Modules/Test
9 */
10class ilTestRandomQuestionsSrcPoolDefinitionQuantitiesCalculation
11{
12    /**
13     * @var ilTestRandomQuestionSetSourcePoolDefinition
14     */
15    protected $sourcePoolDefinition;
16
17    /**
18     * @var ilTestRandomQuestionSetSourcePoolDefinitionList
19     */
20    protected $intersectionQuantitySharingDefinitionList;
21
22    /**
23     * @var integer
24     */
25    protected $overallQuestionAmount;
26
27    /**
28     * @var integer
29     */
30    protected $exclusiveQuestionAmount;
31
32    /**
33     * @var integer
34     */
35    protected $availableSharedQuestionAmount;
36
37    /**
38     * ilTestRandomQuestionsSrcPoolDefinitionQuantitiesCheck constructor.
39     *
40     * @param ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition
41     */
42    public function __construct(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition)
43    {
44        $this->setSourcePoolDefinition($sourcePoolDefinition);
45    }
46
47    /**
48     * @return ilTestRandomQuestionSetSourcePoolDefinition
49     */
50    public function getSourcePoolDefinition()
51    {
52        return $this->sourcePoolDefinition;
53    }
54
55    /**
56     * @param ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition
57     */
58    public function setSourcePoolDefinition($sourcePoolDefinition)
59    {
60        $this->sourcePoolDefinition = $sourcePoolDefinition;
61    }
62
63    /**
64     * @return ilTestRandomQuestionSetSourcePoolDefinitionList
65     */
66    public function getIntersectionQuantitySharingDefinitionList()
67    {
68        return $this->intersectionQuantitySharingDefinitionList;
69    }
70
71    /**
72     * @param ilTestRandomQuestionSetSourcePoolDefinitionList $intersectionQuantitySharingDefinitionList
73     */
74    public function setIntersectionQuantitySharingDefinitionList($intersectionQuantitySharingDefinitionList)
75    {
76        $this->intersectionQuantitySharingDefinitionList = $intersectionQuantitySharingDefinitionList;
77    }
78
79    /**
80     * @return int
81     */
82    public function getOverallQuestionAmount()
83    {
84        return $this->overallQuestionAmount;
85    }
86
87    /**
88     * @param int $overallQuestionAmount
89     */
90    public function setOverallQuestionAmount($overallQuestionAmount)
91    {
92        $this->overallQuestionAmount = $overallQuestionAmount;
93    }
94
95    /**
96     * @return int
97     */
98    public function getExclusiveQuestionAmount()
99    {
100        return $this->exclusiveQuestionAmount;
101    }
102
103    /**
104     * @param int $exclusiveQuestionAmount
105     */
106    public function setExclusiveQuestionAmount($exclusiveQuestionAmount)
107    {
108        $this->exclusiveQuestionAmount = $exclusiveQuestionAmount;
109    }
110
111    /**
112     * @return int
113     */
114    public function getAvailableSharedQuestionAmount()
115    {
116        return $this->availableSharedQuestionAmount;
117    }
118
119    /**
120     * @param int $availableSharedQuestionAmount
121     */
122    public function setAvailableSharedQuestionAmount($availableSharedQuestionAmount)
123    {
124        $this->availableSharedQuestionAmount = $availableSharedQuestionAmount;
125    }
126
127    /**
128     * @return int
129     */
130    protected function getReservedSharedQuestionAmount()
131    {
132        return $this->getOverallQuestionAmount() - (
133            $this->getExclusiveQuestionAmount() + $this->getAvailableSharedQuestionAmount()
134        );
135    }
136
137    /**
138     * @return integer
139     */
140    protected function getRemainingRequiredQuestionAmount()
141    {
142        $requiredQuestionAmount = $this->getSourcePoolDefinition()->getQuestionAmount();
143        $exclusiveQuestionAmount = $this->getExclusiveQuestionAmount();
144
145        return $requiredQuestionAmount - $exclusiveQuestionAmount;
146    }
147
148    /**
149     * @return bool
150     */
151    protected function isRequiredQuestionAmountSatisfiedByOverallQuestionQuantity()
152    {
153        $requiredQuestionAmount = $this->getSourcePoolDefinition()->getQuestionAmount();
154        $overallQuestionAmount = $this->getOverallQuestionAmount();
155
156        return $overallQuestionAmount >= $requiredQuestionAmount;
157    }
158
159    /**
160     * @return bool
161     */
162    protected function isRequiredQuestionAmountSatisfiedByExclusiveQuestionQuantity()
163    {
164        $requiredQuestionAmount = $this->getSourcePoolDefinition()->getQuestionAmount();
165        $exclusiveQuestionAmount = $this->getExclusiveQuestionAmount();
166
167        return $exclusiveQuestionAmount >= $requiredQuestionAmount;
168    }
169
170    /**
171     * @return bool
172     */
173    protected function isRemainingRequiredQuestionAmountSatisfiedBySharedQuestionQuantity()
174    {
175        $remainingRequiredQuestionAmount = $this->getRemainingRequiredQuestionAmount();
176        $availableSharedQuestionAmount = $this->getAvailableSharedQuestionAmount();
177
178        return $availableSharedQuestionAmount >= $remainingRequiredQuestionAmount;
179    }
180
181    /**
182     * @return bool
183     */
184    protected function sourcePoolDefinitionIntersectionsExist()
185    {
186        if ($this->getIntersectionQuantitySharingDefinitionList()->getDefinitionCount() > 0) {
187            return true;
188        }
189
190        return false;
191    }
192
193    /**
194     * @return bool
195     */
196    public function isRequiredAmountGuaranteedAvailable()
197    {
198        if ($this->isRequiredQuestionAmountSatisfiedByExclusiveQuestionQuantity()) {
199            return true;
200        }
201
202        if ($this->isRemainingRequiredQuestionAmountSatisfiedBySharedQuestionQuantity()) {
203            return true;
204        }
205
206        return false;
207    }
208
209    public function getDistributionReport(ilLanguage $lng)
210    {
211        $report = $this->getRuleSatisfactionResultMessage($lng);
212
213        if ($this->sourcePoolDefinitionIntersectionsExist()) {
214            $report .= ' ' . $this->getConcurrentRuleConflictMessage($lng);
215        }
216
217        return $report;
218    }
219
220    protected function getRuleSatisfactionResultMessage(ilLanguage $lng)
221    {
222        if ($this->isRequiredQuestionAmountSatisfiedByOverallQuestionQuantity()) {
223            return sprintf(
224                $lng->txt('tst_msg_rand_quest_set_rule_not_satisfied_reserved'),
225                $this->getSourcePoolDefinition()->getSequencePosition(),
226                $this->getSourcePoolDefinition()->getQuestionAmount(),
227                $this->getOverallQuestionAmount()
228            );
229        }
230
231        return sprintf(
232            $lng->txt('tst_msg_rand_quest_set_rule_not_satisfied_missing'),
233            $this->getSourcePoolDefinition()->getSequencePosition(),
234            $this->getSourcePoolDefinition()->getQuestionAmount(),
235            $this->getOverallQuestionAmount()
236        );
237    }
238
239    protected function getConcurrentRuleConflictMessage(ilLanguage $lng)
240    {
241        $definitionsString = '<br />' . $this->buildIntersectionQuestionSharingDefinitionsString($lng);
242
243        if ($this->isRequiredQuestionAmountSatisfiedByOverallQuestionQuantity()) {
244            return sprintf(
245                $lng->txt('tst_msg_rand_quest_set_rule_not_satisfied_reserved_shared'),
246                $this->getAvailableSharedQuestionAmount(),
247                $definitionsString
248            );
249        }
250
251        return sprintf(
252            $lng->txt('tst_msg_rand_quest_set_rule_not_satisfied_missing_shared'),
253            $this->getReservedSharedQuestionAmount(),
254            $definitionsString
255        );
256    }
257
258    /**
259     * @param ilLanguage $lng
260     * @return string
261     */
262    protected function buildIntersectionQuestionSharingDefinitionsString(ilLanguage $lng)
263    {
264        $definitionsString = array();
265
266        foreach ($this->getIntersectionQuantitySharingDefinitionList() as $definition) {
267            $definitionsString[] = sprintf(
268                $lng->txt('tst_msg_rand_quest_set_rule_label'),
269                $definition->getSequencePosition()
270            );
271        }
272
273        $definitionsString = '<ul><li>' . implode('</li><li>', $definitionsString) . '</li></ul>';
274        return $definitionsString;
275    }
276}
277