1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Test/classes/class.ilTestSequence.php';
5require_once 'Modules/Test/interfaces/interface.ilTestRandomQuestionSequence.php';
6
7
8/**
9 * @author		Björn Heyser <bheyser@databay.de>
10 * @version		$Id$
11 *
12 * @package     Modules/Test
13 */
14class ilTestSequenceRandomQuestionSet extends ilTestSequence implements ilTestRandomQuestionSequence
15{
16    private $responsibleSourcePoolDefinitionByQuestion = array();
17
18    public function loadQuestions(ilTestQuestionSetConfig $testQuestionSetConfig = null, $taxonomyFilterSelection = array())
19    {
20        global $DIC;
21        $ilDB = $DIC['ilDB'];
22
23        $this->questions = array();
24
25        $result = $ilDB->queryF(
26            "SELECT tst_test_rnd_qst.* FROM tst_test_rnd_qst, qpl_questions WHERE tst_test_rnd_qst.active_fi = %s AND qpl_questions.question_id = tst_test_rnd_qst.question_fi AND tst_test_rnd_qst.pass = %s ORDER BY sequence",
27            array('integer','integer'),
28            array($this->active_id, $this->pass)
29        );
30        // The following is a fix for random tests prior to ILIAS 3.8. If someone started a random test in ILIAS < 3.8, there
31        // is only one test pass (pass = 0) in tst_test_rnd_qst while with ILIAS 3.8 there are questions for every test pass.
32        // To prevent problems with tests started in an older version and continued in ILIAS 3.8, the first pass should be taken if
33        // no questions are present for a newer pass.
34        if ($result->numRows() == 0) {
35            $result = $ilDB->queryF(
36                "SELECT tst_test_rnd_qst.* FROM tst_test_rnd_qst, qpl_questions WHERE tst_test_rnd_qst.active_fi = %s AND qpl_questions.question_id = tst_test_rnd_qst.question_fi AND tst_test_rnd_qst.pass = 0 ORDER BY sequence",
37                array('integer'),
38                array($this->active_id)
39            );
40        }
41
42        $index = 1;
43
44        while ($data = $ilDB->fetchAssoc($result)) {
45            $this->questions[$index++] = $data["question_fi"];
46
47            $this->responsibleSourcePoolDefinitionByQuestion[$data['question_fi']] = $data['src_pool_def_fi'];
48        }
49    }
50
51    /**
52     * !!! LEGACY CODE !!!
53     *
54     * Checkes wheather a random test has already created questions for a given pass or not
55     *
56     * @access private
57     * @param $active_id Active id of the test
58     * @param $pass Pass of the test
59     * @return boolean TRUE if the test already contains questions, FALSE otherwise
60     */
61    public function hasRandomQuestionsForPass($active_id, $pass)
62    {
63        global $DIC;
64        $ilDB = $DIC['ilDB'];
65        $result = $ilDB->queryF(
66            "SELECT test_random_question_id FROM tst_test_rnd_qst WHERE active_fi = %s AND pass = %s",
67            array('integer','integer'),
68            array($active_id, $pass)
69        );
70        return ($result->numRows() > 0) ? true : false;
71    }
72
73    public function getResponsibleSourcePoolDefinitionId($questionId)
74    {
75        if (isset($this->responsibleSourcePoolDefinitionByQuestion[$questionId])) {
76            return $this->responsibleSourcePoolDefinitionByQuestion[$questionId];
77        }
78
79        return null;
80    }
81}
82