1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/TestQuestionPool/interfaces/interface.ilQuestionHeaderBlockBuilder.php';
5
6/**
7 * @author		Björn Heyser <bheyser@databay.de>
8 * @version		$Id$
9 *
10 * @package     Modules/Test
11 */
12class ilTestQuestionHeaderBlockBuilder implements ilQuestionHeaderBlockBuilder
13{
14    /**
15     * @var ilLanguage
16     */
17    protected $lng;
18
19    /**
20     * @var integer
21     */
22    protected $headerMode;
23
24    /**
25     * @var string
26     */
27    protected $questionTitle;
28
29    /**
30     * @var float
31     */
32    protected $questionPoints;
33
34    /**
35     * @var integer
36     */
37    protected $questionPosition;
38
39    /**
40     * @var integer
41     */
42    protected $questionCount;
43
44    /**
45     * @var bool
46     */
47    protected $questionPostponed;
48
49    /**
50     * @var bool
51     */
52    protected $questionObligatory;
53
54    /**
55     * @var string
56     */
57    protected $questionRelatedObjectives;
58
59    // fau: testNav - answer status variable
60    /**
61     * @var boolean | null
62     */
63    protected $questionAnswered;
64    // fau.
65
66    public function __construct(ilLanguage $lng)
67    {
68        $this->lng = $lng;
69
70        $this->headerMode = null;
71        $this->questionTitle = '';
72        $this->questionPoints = 0.0;
73        $this->questionPosition = 0;
74        $this->questionCount = 0;
75        $this->questionPostponed = false;
76        $this->questionObligatory = false;
77        $this->questionRelatedObjectives = '';
78    }
79
80    /**
81     * @return int
82     */
83    public function getHeaderMode()
84    {
85        return $this->headerMode;
86    }
87
88    /**
89     * @param int $headerMode
90     */
91    public function setHeaderMode($headerMode)
92    {
93        $this->headerMode = $headerMode;
94    }
95
96    /**
97     * @return string
98     */
99    public function getQuestionTitle()
100    {
101        return $this->questionTitle;
102    }
103
104    /**
105     * @param string $questionTitle
106     */
107    public function setQuestionTitle($questionTitle)
108    {
109        $this->questionTitle = $questionTitle;
110    }
111
112    /**
113     * @return float
114     */
115    public function getQuestionPoints()
116    {
117        return $this->questionPoints;
118    }
119
120    /**
121     * @param float $questionPoints
122     */
123    public function setQuestionPoints($questionPoints)
124    {
125        $this->questionPoints = $questionPoints;
126    }
127
128    // fau: testNav - setter for question answered
129    /**
130     * @param bool $questionAnswered
131     */
132    public function setQuestionAnswered($questionAnswered)
133    {
134        $this->questionAnswered = $questionAnswered;
135    }
136    // fau.
137    /**
138     * @return int
139     */
140    public function getQuestionPosition()
141    {
142        return $this->questionPosition;
143    }
144
145    /**
146     * @param int $questionPosition
147     */
148    public function setQuestionPosition($questionPosition)
149    {
150        $this->questionPosition = $questionPosition;
151    }
152
153    /**
154     * @return int
155     */
156    public function getQuestionCount()
157    {
158        return $this->questionCount;
159    }
160
161    /**
162     * @param int $questionCount
163     */
164    public function setQuestionCount($questionCount)
165    {
166        $this->questionCount = $questionCount;
167    }
168
169    /**
170     * @return boolean
171     */
172    public function isQuestionPostponed()
173    {
174        return $this->questionPostponed;
175    }
176
177    // fau: testNav - get question answered status
178    /**
179     * @return boolean | null
180     */
181    public function isQuestionAnswered()
182    {
183        return $this->questionAnswered;
184    }
185    // fau.
186
187    /**
188     * @param boolean $questionPostponed
189     */
190    public function setQuestionPostponed($questionPostponed)
191    {
192        $this->questionPostponed = $questionPostponed;
193    }
194
195    /**
196     * @return boolean
197     */
198    public function isQuestionObligatory()
199    {
200        return $this->questionObligatory;
201    }
202
203    /**
204     * @param boolean $questionObligatory
205     */
206    public function setQuestionObligatory($questionObligatory)
207    {
208        $this->questionObligatory = $questionObligatory;
209    }
210
211    /**
212     * @return string
213     */
214    public function getQuestionRelatedObjectives()
215    {
216        return $this->questionRelatedObjectives;
217    }
218
219    /**
220     * @param string $questionRelatedObjectives
221     */
222    public function setQuestionRelatedObjectives($questionRelatedObjectives)
223    {
224        $this->questionRelatedObjectives = $questionRelatedObjectives;
225    }
226
227    protected function buildQuestionPositionString()
228    {
229        if (!$this->getQuestionPosition()) {
230            return '';
231        }
232
233        if ($this->getQuestionCount()) {
234            return sprintf($this->lng->txt("tst_position"), $this->getQuestionPosition(), $this->getQuestionCount());
235        }
236
237        return sprintf($this->lng->txt("tst_position_without_total"), $this->getQuestionPosition());
238    }
239
240    // fau: testNav - remove HTML from building strings (is now in tpl.tst_question_info.html)
241    protected function buildQuestionPointsString()
242    {
243        if ($this->getQuestionPoints() == 1) {
244            return "{$this->getQuestionPoints()} {$this->lng->txt('point')}";
245        }
246
247        return "{$this->getQuestionPoints()} {$this->lng->txt('points')}";
248    }
249
250    protected function buildQuestionPostponedString()
251    {
252        if ($this->isQuestionPostponed()) {
253            return $this->lng->txt("postponed");
254        }
255
256        return '';
257    }
258
259    protected function buildQuestionObligatoryString()
260    {
261        if ($this->isQuestionObligatory()) {
262            return $this->lng->txt("tst_you_have_to_answer_this_question");
263        }
264
265        return '';
266    }
267
268    protected function buildQuestionRelatedObjectivesString()
269    {
270        if (strlen($this->getQuestionRelatedObjectives())) {
271            $label = $this->lng->txt('tst_res_lo_objectives_header');
272            return $label . ': ' . $this->getQuestionRelatedObjectives();
273        }
274
275        return '';
276    }
277    // fau.
278
279
280    // fau: testNav - split generation of presentation title and question info
281
282    /**
283     * Get the presentation title of the question
284     * This is shown above the title line in a test run
285     * @return	string
286     */
287    public function getPresentationTitle()
288    {
289        switch ($this->getHeaderMode()) {
290            case 2: 	// neither titles nor points => show position as title
291                return $this->buildQuestionPositionString();
292                break;
293
294            case 0:		// titles and points => show title here
295            case 1:		// only titles => show title here
296            default:
297                return $this->getQuestionTitle();
298        }
299    }
300
301
302    /**
303     * Get the additional question info and answering status
304     * This is shown below the title line in a test run
305     * @return string		html code of the info block
306     */
307    public function getQuestionInfoHTML()
308    {
309        $tpl = new ilTemplate('tpl.tst_question_info.html', true, true, 'Modules/Test');
310
311        // position and/or points
312        switch ($this->getHeaderMode()) {
313            case 1: // only titles =>  show position here
314                $text = $this->buildQuestionPositionString();
315                break;
316
317            case 2: //	neither titles nor points => position is separate title, show nothing here
318                $text = '';
319                break;
320
321            case 0: //  titles and points => show position and points here
322            default:
323                $text = $this->buildQuestionPositionString() . ' (' . $this->buildQuestionPointsString() . ')';
324        }
325        if ($this->isQuestionPostponed()) {
326            $text .= ($text ? ', ' : '') . $this->buildQuestionPostponedString();
327        }
328
329        $tpl->setVariable('TXT_POSITION_POINTS', $text);
330
331        // obligatory
332        if ($this->isQuestionObligatory() && !$this->isQuestionAnswered()) {
333            $tpl->setVariable('TXT_OBLIGATORY', $this->buildQuestionObligatoryString());
334        }
335
336        // objectives
337        if (strlen($this->getQuestionRelatedObjectives())) {
338            $tpl->setVariable('TXT_OBJECTIVES', $this->buildQuestionRelatedObjectivesString());
339        }
340
341        // answer status
342        if ($this->isQuestionAnswered()) {
343            $tpl->setVariable('HIDDEN_NOT_ANSWERED', 'hidden');
344        } else {
345            $tpl->setVariable('HIDDEN_ANSWERED', 'hidden');
346        }
347
348        $tpl->setVariable('SRC_ANSWERED', ilUtil::getImagePath('answered.svg'));
349        $tpl->setVariable('SRC_NOT_ANSWERED', ilUtil::getImagePath('answered_not.svg'));
350        $tpl->setVariable('TXT_ANSWERED', $this->lng->txt('tst_answer_status_answered'));
351        $tpl->setVariable('TXT_NOT_ANSWERED', $this->lng->txt('tst_answer_status_not_answered'));
352        $tpl->setVariable('TXT_EDITING', $this->lng->txt('tst_answer_status_editing'));
353
354        return $tpl->get();
355    }
356    // fau.
357
358    public function getHTML()
359    {
360        $headerBlock = $this->buildQuestionPositionString();
361
362        switch ($this->getHeaderMode()) {
363            case 1:
364
365                $headerBlock .= " - " . $this->getQuestionTitle();
366                $headerBlock .= $this->buildQuestionPostponedString();
367                $headerBlock .= $this->buildQuestionObligatoryString();
368                break;
369
370            case 2:
371
372                $headerBlock .= $this->buildQuestionPostponedString();
373                $headerBlock .= $this->buildQuestionObligatoryString();
374                break;
375
376            case 0:
377            default:
378
379                $headerBlock .= " - " . $this->getQuestionTitle();
380                $headerBlock .= $this->buildQuestionPostponedString();
381// fau: testNav - put the points in parentheses here, not in building the string
382                $headerBlock .= ' (' . $this->buildQuestionPointsString() . ')';
383// fau.
384                $headerBlock .= $this->buildQuestionObligatoryString();
385        }
386
387        $headerBlock .= $this->buildQuestionRelatedObjectivesString();
388
389        return $headerBlock;
390    }
391}
392