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 ilTestRandomQuestionSetSourcePoolDefinition
11{
12    /**
13     * global $ilDB object instance
14     *
15     * @var ilDBInterface
16     */
17    protected $db = null;
18
19    /**
20     * object instance of current test
21     *
22     * @var ilObjTest
23     */
24    protected $testOBJ = null;
25
26    private $id = null;
27
28    private $poolId = null;
29
30    /** @var null|int */
31    private $poolRefId = null;
32
33    private $poolTitle = null;
34
35    private $poolPath = null;
36
37    private $poolQuestionCount = null;
38
39    // fau: taxFilter/typeFilter - new class variables
40    #private $originalFilterTaxId = null;
41
42    #private $originalFilterTaxNodeId = null;
43
44    #private $mappedFilterTaxId = null;
45
46    #private $mappedFilterTaxNodeId = null;
47
48    /**
49     * @var array taxId => [nodeId, ...]
50     */
51    private $originalTaxonomyFilter = array();
52
53    /**
54     * @var array taxId => [nodeId, ...]
55     */
56    private $mappedTaxonomyFilter = array();
57
58    /**
59     * @var array
60     */
61    private $typeFilter = array();
62    // fau.
63    // fau.
64
65    /**
66     * @var array
67     */
68    private $lifecycleFilter = array();
69
70    private $questionAmount = null;
71
72    private $sequencePosition = null;
73
74    public function __construct(ilDBInterface $db, ilObjTest $testOBJ)
75    {
76        $this->db = $db;
77        $this->testOBJ = $testOBJ;
78    }
79
80    public function setId($id)
81    {
82        $this->id = $id;
83    }
84
85    public function getId()
86    {
87        return $this->id;
88    }
89
90    public function setPoolId($poolId)
91    {
92        $this->poolId = $poolId;
93    }
94
95    public function getPoolId()
96    {
97        return $this->poolId;
98    }
99
100    public function getPoolRefId() : ?int
101    {
102        return $this->poolRefId;
103    }
104
105    public function setPoolRefId(?int $poolRefId) : void
106    {
107        $this->poolRefId = $poolRefId;
108    }
109
110    public function setPoolTitle($poolTitle)
111    {
112        $this->poolTitle = $poolTitle;
113    }
114
115    public function getPoolTitle()
116    {
117        return $this->poolTitle;
118    }
119
120    public function setPoolPath($poolPath)
121    {
122        $this->poolPath = $poolPath;
123    }
124
125    public function getPoolPath()
126    {
127        return $this->poolPath;
128    }
129
130    public function setPoolQuestionCount($poolQuestionCount)
131    {
132        $this->poolQuestionCount = $poolQuestionCount;
133    }
134
135    public function getPoolQuestionCount()
136    {
137        return $this->poolQuestionCount;
138    }
139
140    // fau: taxFilter/typeFilter - new setters/getters
141    /**
142     * get the original taxonomy filter conditions
143     * @return array	taxId => [nodeId, ...]
144     */
145    public function getOriginalTaxonomyFilter()
146    {
147        return $this->originalTaxonomyFilter;
148    }
149
150    /**
151     * set the original taxonomy filter condition
152     * @param  array taxId => [nodeId, ...]
153     */
154    public function setOriginalTaxonomyFilter($filter = array())
155    {
156        $this->originalTaxonomyFilter = $filter;
157    }
158
159    /**
160     * get the original taxonomy filter for insert into the database
161     * @return null|string		serialized taxonomy filter
162     */
163    private function getOriginalTaxonomyFilterForDbValue()
164    {
165        // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
166        return empty($this->originalTaxonomyFilter) ? null : serialize($this->originalTaxonomyFilter);
167    }
168
169    /**
170     * get the original taxonomy filter from database value
171     * @param null|string		serialized taxonomy filter
172     */
173    private function setOriginalTaxonomyFilterFromDbValue($value)
174    {
175        // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
176        $this->originalTaxonomyFilter = empty($value) ? array() : unserialize($value);
177    }
178
179    /**
180     * get the mapped taxonomy filter conditions
181     * @return 	array	taxId => [nodeId, ...]
182     */
183    public function getMappedTaxonomyFilter()
184    {
185        return $this->mappedTaxonomyFilter;
186    }
187
188    /**
189     * set the original taxonomy filter condition
190     * @param array 	taxId => [nodeId, ...]
191     */
192    public function setMappedTaxonomyFilter($filter = array())
193    {
194        $this->mappedTaxonomyFilter = $filter;
195    }
196
197    /**
198     * get the original taxonomy filter for insert into the database
199     * @return null|string		serialized taxonomy filter
200     */
201    private function getMappedTaxonomyFilterForDbValue()
202    {
203        return empty($this->mappedTaxonomyFilter) ? null : serialize($this->mappedTaxonomyFilter);
204    }
205
206    /**
207     * get the original taxonomy filter from database value
208     * @param null|string		serialized taxonomy filter
209     */
210    private function setMappedTaxonomyFilterFromDbValue($value)
211    {
212        $this->mappedTaxonomyFilter = empty($value) ? array() : unserialize($value);
213    }
214
215
216    /**
217     * set the mapped taxonomy filter from original by applying a keys map
218     * @param ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomiesKeysMap
219     */
220    public function mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomiesKeysMap)
221    {
222        $this->mappedTaxonomyFilter = array();
223        foreach ($this->originalTaxonomyFilter as $taxId => $nodeIds) {
224            $mappedNodeIds = array();
225            foreach ($nodeIds as $nodeId) {
226                $mappedNodeIds[] = $taxonomiesKeysMap->getMappedTaxNodeId($nodeId);
227            }
228            $this->mappedTaxonomyFilter[$taxonomiesKeysMap->getMappedTaxonomyId($taxId)] = $mappedNodeIds;
229        }
230    }
231
232    public function setTypeFilter($typeFilter = array())
233    {
234        $this->typeFilter = $typeFilter;
235    }
236
237    public function getTypeFilter()
238    {
239        return $this->typeFilter;
240    }
241
242    /**
243     * get the question type filter for insert into the database
244     * @return null|string		serialized type filter
245     */
246    private function getTypeFilterForDbValue()
247    {
248        return empty($this->typeFilter) ? null : serialize($this->typeFilter);
249    }
250
251    /**
252     * get the question type filter from database value
253     * @param null|string		serialized type filter
254     */
255    private function setTypeFilterFromDbValue($value)
256    {
257        $this->typeFilter = empty($value) ? array() : unserialize($value);
258    }
259
260    /**
261     * @return array
262     */
263    public function getLifecycleFilter()
264    {
265        return $this->lifecycleFilter;
266    }
267
268    /**
269     * @param array $lifecycleFilter
270     */
271    public function setLifecycleFilter($lifecycleFilter)
272    {
273        $this->lifecycleFilter = $lifecycleFilter;
274    }
275
276    /**
277     * @return null|string		serialized lifecycle filter
278     */
279    public function getLifecycleFilterForDbValue()
280    {
281        return empty($this->lifecycleFilter) ? null : serialize($this->lifecycleFilter);
282    }
283
284    /**
285     * @param null|string		serialized lifecycle filter
286     */
287    public function setLifecycleFilterFromDbValue($dbValue)
288    {
289        $this->lifecycleFilter = empty($dbValue) ? array() : unserialize($dbValue);
290    }
291
292    /*
293    public function setOriginalFilterTaxId($originalFilterTaxId)
294    {
295        $this->originalFilterTaxId = $originalFilterTaxId;
296    }
297
298    public function getOriginalFilterTaxId()
299    {
300        return $this->originalFilterTaxId;
301    }
302
303    public function setOriginalFilterTaxNodeId($originalFilterNodeId)
304    {
305        $this->originalFilterTaxNodeId = $originalFilterNodeId;
306    }
307
308    public function getOriginalFilterTaxNodeId()
309    {
310        return $this->originalFilterTaxNodeId;
311    }
312
313    public function setMappedFilterTaxId($mappedFilterTaxId)
314    {
315        $this->mappedFilterTaxId = $mappedFilterTaxId;
316    }
317
318    public function getMappedFilterTaxId()
319    {
320        return $this->mappedFilterTaxId;
321    }
322
323    public function setMappedFilterTaxNodeId($mappedFilterTaxNodeId)
324    {
325        $this->mappedFilterTaxNodeId = $mappedFilterTaxNodeId;
326    }
327
328    public function getMappedFilterTaxNodeId()
329    {
330        return $this->mappedFilterTaxNodeId;
331    }
332    */
333    // fau.
334
335    public function setQuestionAmount($questionAmount)
336    {
337        $this->questionAmount = $questionAmount;
338    }
339
340    public function getQuestionAmount()
341    {
342        return $this->questionAmount;
343    }
344
345    public function setSequencePosition($sequencePosition)
346    {
347        $this->sequencePosition = $sequencePosition;
348    }
349
350    public function getSequencePosition()
351    {
352        return $this->sequencePosition;
353    }
354
355    // -----------------------------------------------------------------------------------------------------------------
356
357    /**
358     * @param array $dataArray
359     */
360    public function initFromArray($dataArray)
361    {
362        foreach ($dataArray as $field => $value) {
363            switch ($field) {
364                case 'def_id':				$this->setId($value);						break;
365                case 'pool_fi':				$this->setPoolId($value);					break;
366                case 'pool_ref_id':         $this->setPoolRefId($value ? (int) $value : null); break;
367                case 'pool_title':			$this->setPoolTitle($value);				break;
368                case 'pool_path':			$this->setPoolPath($value);					break;
369                case 'pool_quest_count':	$this->setPoolQuestionCount($value);		break;
370                // fau: taxFilter - use new db fields
371                #case 'origin_tax_fi':		$this->setOriginalFilterTaxId($value);		break;
372                #case 'origin_node_fi':		$this->setOriginalFilterTaxNodeId($value);	break;
373                #case 'mapped_tax_fi':		$this->setMappedFilterTaxId($value);		break;
374                #case 'mapped_node_fi':		$this->setMappedFilterTaxNodeId($value);	break;
375                case 'origin_tax_filter':	$this->setOriginalTaxonomyFilterFromDbValue($value);	break;
376                case 'mapped_tax_filter':	$this->setMappedTaxonomyFilterFromDbValue($value);		break;
377                case 'type_filter':			$this->setTypeFilterFromDbValue($value);	break;
378                case 'lifecycle_filter':			$this->setLifecycleFilterFromDbValue($value);	break;
379                // fau.
380                case 'quest_amount':		$this->setQuestionAmount($value);			break;
381                case 'sequence_pos':		$this->setSequencePosition($value);			break;
382            }
383        }
384    }
385
386    /**
387     * @param integer $poolId
388     * @return boolean
389     */
390    public function loadFromDb($id)
391    {
392        $res = $this->db->queryF(
393            "SELECT * FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
394            array('integer'),
395            array($id)
396        );
397
398        while ($row = $this->db->fetchAssoc($res)) {
399            $this->initFromArray($row);
400
401            return true;
402        }
403
404        return false;
405    }
406
407    public function saveToDb()
408    {
409        if ($this->getId()) {
410            $this->updateDbRecord($this->testOBJ->getTestId());
411        } else {
412            $this->insertDbRecord($this->testOBJ->getTestId());
413        }
414    }
415
416    public function cloneToDbForTestId($testId)
417    {
418        $this->insertDbRecord($testId);
419    }
420
421    public function deleteFromDb()
422    {
423        $this->db->manipulateF(
424            "DELETE FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
425            array('integer'),
426            array($this->getId())
427        );
428    }
429
430    /**
431     * @param $testId
432     */
433    private function updateDbRecord($testId)
434    {
435        $this->db->update(
436            'tst_rnd_quest_set_qpls',
437            array(
438                'test_fi' => array('integer', $testId),
439                'pool_fi' => array('integer', $this->getPoolId()),
440                'pool_ref_id' => array('integer', $this->getPoolRefId()),
441                'pool_title' => array('text', $this->getPoolTitle()),
442                'pool_path' => array('text', $this->getPoolPath()),
443                'pool_quest_count' => array('integer', $this->getPoolQuestionCount()),
444                // fau: taxFilter/typeFilter - use new db fields
445                #'origin_tax_fi' => array('integer', $this->getOriginalFilterTaxId()),
446                #'origin_node_fi' => array('integer', $this->getOriginalFilterTaxNodeId()),
447                #'mapped_tax_fi' => array('integer', $this->getMappedFilterTaxId()),
448                #'mapped_node_fi' => array('integer', $this->getMappedFilterTaxNodeId()),
449                'origin_tax_filter' => array('text', $this->getOriginalTaxonomyFilterForDbValue()),
450                'mapped_tax_filter' => array('text', $this->getMappedTaxonomyFilterForDbValue()),
451                'type_filter' => array('text', $this->getTypeFilterForDbValue()),
452                'lifecycle_filter' => array('text', $this->getLifecycleFilterForDbValue()),
453                // fau.
454                'quest_amount' => array('integer', $this->getQuestionAmount()),
455                'sequence_pos' => array('integer', $this->getSequencePosition())
456            ),
457            array(
458                'def_id' => array('integer', $this->getId())
459            )
460        );
461    }
462
463    /**
464     * @param $testId
465     */
466    private function insertDbRecord($testId)
467    {
468        $nextId = $this->db->nextId('tst_rnd_quest_set_qpls');
469
470        $this->db->insert('tst_rnd_quest_set_qpls', array(
471                'def_id' => array('integer', $nextId),
472                'test_fi' => array('integer', $testId),
473                'pool_fi' => array('integer', $this->getPoolId()),
474                'pool_ref_id' => array('integer', $this->getPoolRefId()),
475                'pool_title' => array('text', $this->getPoolTitle()),
476                'pool_path' => array('text', $this->getPoolPath()),
477                'pool_quest_count' => array('integer', $this->getPoolQuestionCount()),
478                // fau: taxFilter/typeFilter - use new db fields
479                #'origin_tax_fi' => array('integer', $this->getOriginalFilterTaxId()),
480                #'origin_node_fi' => array('integer', $this->getOriginalFilterTaxNodeId()),
481                #'mapped_tax_fi' => array('integer', $this->getMappedFilterTaxId()),
482                #'mapped_node_fi' => array('integer', $this->getMappedFilterTaxNodeId()),
483                'origin_tax_filter' => array('text', $this->getOriginalTaxonomyFilterForDbValue()),
484                'mapped_tax_filter' => array('text', $this->getMappedTaxonomyFilterForDbValue()),
485                'type_filter' => array('text', $this->getTypeFilterForDbValue()),
486                'lifecycle_filter' => array('text', $this->getLifecycleFilterForDbValue()),
487                // fau.
488                'quest_amount' => array('integer', $this->getQuestionAmount()),
489                'sequence_pos' => array('integer', $this->getSequencePosition())
490        ));
491
492        $this->setId($nextId);
493    }
494
495    // -----------------------------------------------------------------------------------------------------------------
496
497    public function getPoolInfoLabel(ilLanguage $lng)
498    {
499        $pool_path = $this->getPoolPath();
500        if (is_int($this->getPoolRefId()) && ilObject::_lookupObjId($this->getPoolRefId())) {
501            $path = new ilPathGUI();
502            $path->enableTextOnly(true);
503            $pool_path = $path->getPath(ROOT_FOLDER_ID, $this->getPoolRefId());
504        }
505
506        $poolInfoLabel = sprintf(
507            $lng->txt('tst_dynamic_question_set_source_questionpool_summary_string'),
508            $this->getPoolTitle(),
509            $pool_path,
510            $this->getPoolQuestionCount()
511        );
512
513        return $poolInfoLabel;
514    }
515
516    // -----------------------------------------------------------------------------------------------------------------
517}
518