1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
5include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
6
7/**
8* Class ilLOEditorGUI
9*
10* @author Stefan Meyer <smeyer.ilias@gmx.de>
11* $Id$
12*
13*
14*/
15class ilLORandomTestQuestionPools
16{
17    protected $container_id = 0;
18    protected $objective_id = 0;
19    protected $test_type = 0;
20    protected $test_id = 0;
21    protected $qpl_seq = 0;
22    protected $limit = 50;
23
24
25    /**
26     * Constructor
27     * @param type $a_container_id
28     * @param type $a_objective_id
29     */
30    public function __construct($a_container_id, $a_objective_id, $a_test_type, $a_qpl_sequence)
31    {
32        $this->container_id = $a_container_id;
33        $this->objective_id = $a_objective_id;
34        $this->test_type = $a_test_type;
35        $this->qpl_seq = $a_qpl_sequence;
36
37        $this->read();
38    }
39
40    /**
41     * lookup limit
42     * @global type $ilDB
43     * @param int $a_container_id
44     * @param int $a_objective_id
45     * @param int $a_test_type
46     * @return int
47     */
48    public static function lookupLimit($a_container_id, $a_objective_id, $a_test_type)
49    {
50        global $DIC;
51
52        $ilDB = $DIC['ilDB'];
53
54        $query = 'SELECT * FROM loc_rnd_qpl ' .
55                'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
56                'AND objective_id = ' . $ilDB->quote($a_objective_id, 'integer') . ' ' .
57                'AND tst_type = ' . $ilDB->quote($a_test_type, 'integer');
58        $res = $ilDB->query($query);
59        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
60            return $row->percentage;
61        }
62        return 0;
63    }
64
65    /**
66     * Lookup sequence ids
67     * @global type $ilDB
68     * @param int $a_container_id
69     * @param int $a_objective_id
70     * @param int $a_test_id
71     * @return int[]
72     */
73    public static function lookupSequences($a_container_id, $a_objective_id, $a_test_id)
74    {
75        global $DIC;
76
77        $ilDB = $DIC['ilDB'];
78
79        $query = 'SELECT * FROM loc_rnd_qpl ' .
80                'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
81                'AND objective_id = ' . $ilDB->quote($a_objective_id, 'integer') . ' ' .
82                'AND tst_id = ' . $ilDB->quote($a_test_id, 'integer');
83
84        $res = $ilDB->query($query);
85        $sequences = [];
86        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
87            $sequences[] = $row->qp_seq;
88        }
89        return (array) $sequences;
90    }
91
92    /**
93     * Lookup sequence ids
94     * @global type $ilDB
95     * @param int $a_container_id
96     * @param int $a_objective_id
97     * @param int $a_test_id
98     * @param int $a_test_type
99     * @return int[]
100     */
101    public static function lookupSequencesByType($a_container_id, $a_objective_id, $a_test_id, $a_test_type)
102    {
103        global $DIC;
104
105        $ilDB = $DIC['ilDB'];
106
107        $query = 'SELECT * FROM loc_rnd_qpl ' .
108            'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
109            'AND objective_id = ' . $ilDB->quote($a_objective_id, 'integer') . ' ' .
110            'AND tst_id = ' . $ilDB->quote($a_test_id, 'integer') . ' ' .
111            'AND tst_type = ' . $ilDB->quote($a_test_type, 'integer');
112
113        $res = $ilDB->query($query);
114        $sequences = [];
115        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
116            $sequences[] = $row->qp_seq;
117        }
118        return (array) $sequences;
119    }
120
121
122    /**
123     * Lookup objective ids by sequence_id
124     * @global type $ilDB
125     * @param int $a_container_id
126     * @param int $a_seq_id
127     * @return int[]
128     */
129    public static function lookupObjectiveIdsBySequence($a_container_id, $a_seq_id)
130    {
131        global $DIC;
132
133        $ilDB = $DIC['ilDB'];
134
135        $query = 'SELECT objective_id FROM loc_rnd_qpl ' .
136                'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
137                'AND qp_seq = ' . $ilDB->quote($a_seq_id, 'integer');
138        $res = $ilDB->query($query);
139        $objectiveIds = array();
140        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
141            $objectiveIds[] = $row->objective_id;
142        }
143        return $objectiveIds;
144    }
145
146
147    public function setContainerId($a_id)
148    {
149        $this->container_id = $a_id;
150    }
151
152    public function getContainerId()
153    {
154        return $this->container_id;
155    }
156
157    public function setObjectiveId($a_id)
158    {
159        $this->objective_id = $a_id;
160    }
161
162    public function getObjectiveId()
163    {
164        return $this->objective_id;
165    }
166
167    public function setTestType($a_type)
168    {
169        $this->test_type = $a_type;
170    }
171
172    public function getTestType()
173    {
174        return $this->test_type;
175    }
176
177    public function setTestId($a_id)
178    {
179        $this->test_id = $a_id;
180    }
181
182    public function getTestId()
183    {
184        return $this->test_id;
185    }
186
187    public function setQplSequence($a_id)
188    {
189        $this->qpl_seq = $a_id;
190    }
191
192    public function getQplSequence()
193    {
194        return $this->qpl_seq;
195    }
196
197    public function setLimit($a_id)
198    {
199        $this->limit = $a_id;
200    }
201
202    public function getLimit()
203    {
204        return $this->limit;
205    }
206
207    /**
208     * Copy assignment
209     * @param type $a_copy_id
210     * @param type $a_new_objective_id
211     */
212    public function copy($a_copy_id, $a_new_course_id, $a_new_objective_id)
213    {
214        include_once './Services/CopyWizard/classes/class.ilCopyWizardOptions.php';
215        $options = ilCopyWizardOptions::_getInstance($a_copy_id);
216        $mappings = $options->getMappings();
217
218        foreach (self::lookupSequences($this->getContainerId(), $this->getContainerId(), $this->getTestId()) as $sequence) {
219            // not nice
220            $this->setQplSequence($sequence);
221            $this->read();
222
223
224            $mapped_id = 0;
225            $test_ref_id = 0;
226            foreach ((array) ilObject::_getAllReferences($this->getTestId()) as $tmp => $ref_id) {
227                $test_ref_id = $ref_id;
228                $mapped_id = $mappings[$ref_id];
229                if ($mapped_id) {
230                    continue;
231                }
232            }
233            if (!$mapped_id) {
234                ilLoggerFactory::getLogger('crs')->debug('No test mapping found for random question pool assignment: ' . $this->getTestId() . ' ' . $sequence);
235                continue;
236            }
237
238            // Mapping for sequence
239            $new_question_info = $mappings[$test_ref_id . '_rndSelDef_' . $this->getQplSequence()];
240            $new_question_arr = explode('_', $new_question_info);
241            if (!isset($new_question_arr[2]) or !$new_question_arr[2]) {
242                //ilLoggerFactory::getLogger('crs')->debug(print_r($mappings,TRUE));
243                ilLoggerFactory::getLogger('crs')->debug('Found invalid or no mapping format of random question id mapping: ' . print_r($new_question_arr, true));
244                continue;
245            }
246
247            $new_ass = new self(
248                $a_new_course_id,
249                $a_new_objective_id,
250                $this->getTestType(),
251                $new_question_arr[2]
252            );
253            $new_ass->setTestId($mapped_id);
254            $new_ass->setLimit($this->getLimit());
255            $new_ass->create();
256        }
257    }
258
259
260    /**
261     * read settings
262     * @global type $ilDB
263     * @return boolean
264     */
265    public function read()
266    {
267        global $DIC;
268
269        $ilDB = $DIC['ilDB'];
270
271        $query = 'SELECT * FROM loc_rnd_qpl ' .
272            'WHERE container_id = ' . $ilDB->quote($this->getContainerId(), 'integer') . ' ' .
273            'AND objective_id = ' . $ilDB->quote($this->getObjectiveId(), 'integer') . ' ' .
274            'AND tst_type = ' . $ilDB->quote($this->getTestType(), 'integer') . ' ' .
275            'AND qp_seq = ' . $ilDB->quote($this->getQplSequence(), 'integer');
276
277        $res = $ilDB->query($query);
278        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
279            $this->setLimit($row->percentage);
280            $this->setTestId($row->tst_id);
281        }
282        return true;
283    }
284
285    public function delete()
286    {
287        global $DIC;
288
289        $ilDB = $DIC['ilDB'];
290
291        $query = 'DELETE FROM loc_rnd_qpl ' .
292            'WHERE container_id = ' . $ilDB->quote($this->getContainerId(), 'integer') . ' ' .
293            'AND objective_id = ' . $ilDB->quote($this->getObjectiveId(), 'integer') . ' ' .
294            'AND tst_type = ' . $ilDB->quote($this->getTestType(), 'integer') . ' ' .
295            'AND qp_seq = ' . $ilDB->quote($this->getQplSequence(), 'integer');
296        $ilDB->manipulate($query);
297    }
298
299    /**
300     * Delete assignment for objective id and test type
301     * @param int $a_course_id
302     * @param int $a_objective_id
303     * @param int $a_tst_type
304     *
305     */
306    public static function deleteForObjectiveAndTestType($a_course_id, $a_objective_id, $a_tst_type)
307    {
308        $db = $GLOBALS['DIC']->database();
309
310        $query = 'DELETE FROM loc_rnd_qpl ' .
311                'WHERE container_id = ' . $db->quote($a_course_id, 'integer') . ' ' .
312                'AND objective_id = ' . $db->quote($a_objective_id, 'integer') . ' ' .
313                'AND tst_type = ' . $db->quote($a_tst_type, 'integer');
314        $db->manipulate($query);
315    }
316
317    public function create()
318    {
319        global $DIC;
320
321        $ilDB = $DIC['ilDB'];
322
323        $query = 'INSERT INTO loc_rnd_qpl ' .
324                '(container_id, objective_id, tst_type, tst_id, qp_seq, percentage) ' .
325                'VALUES ( ' .
326                $ilDB->quote($this->getContainerId(), 'integer') . ', ' .
327                $ilDB->quote($this->getObjectiveId(), 'integer') . ', ' .
328                $ilDB->quote($this->getTestType(), 'integer') . ', ' .
329                $ilDB->quote($this->getTestId(), 'integer') . ', ' .
330                $ilDB->quote($this->getQplSequence(), 'integer') . ', ' .
331                $ilDB->quote($this->getLimit()) . ' ' .
332                ')';
333        $ilDB->manipulate($query);
334    }
335
336    // begin-patch optes_lok_export
337    public static function toXml(ilXmlWriter $writer, $a_objective_id)
338    {
339        global $DIC;
340
341        $ilDB = $DIC['ilDB'];
342
343        $query = 'SELECT * FROM loc_rnd_qpl ' .
344            'WHERE objective_id = ' . $ilDB->quote($a_objective_id, 'integer');
345        $res = $ilDB->query($query);
346        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
347            include_once './Modules/Course/classes/Objectives/class.ilLOXmlWriter.php';
348            $writer->xmlElement(
349                'Test',
350                array(
351                    'type' => ilLOXmlWriter::TYPE_TST_RND,
352                    'objId' => $row->tst_id,
353                    'testType' => $row->tst_type,
354                    'limit' => $row->percentage,
355                    'poolId' => $row->qp_seq
356                )
357            );
358        }
359    }
360    // end-patch optes_lok_export
361}
362