1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
5/**
6 * @author		Björn Heyser <bheyser@databay.de>
7 * @version		$Id$
8 *
9 * @package     Modules/TestQuestionPool
10 */
11class ilAssIncompleteQuestionPurger
12{
13    /**
14     * @var ilDBInterface
15     */
16    protected $db;
17
18    protected $ownerId;
19
20    private $ignoredContainerObjectTypes;
21
22    public function __construct(ilDBInterface $db)
23    {
24        $this->db = $db;
25
26        $this->ignoredContainerObjectTypes = array('lm');
27    }
28
29    public function getOwnerId()
30    {
31        return $this->ownerId;
32    }
33
34    public function setOwnerId($ownerId)
35    {
36        $this->ownerId = $ownerId;
37    }
38
39    public function purge()
40    {
41        $questionIds = $this->getPurgableQuestionIds();
42        $this->purgeQuestionIds($questionIds);
43    }
44
45    private function getPurgableQuestionIds()
46    {
47        $INtypes = $this->db->in('object_data.type', $this->getIgnoredContainerObjectTypes(), true, 'text');
48
49        $query = "
50			SELECT qpl_questions.question_id
51			FROM qpl_questions
52			INNER JOIN object_data
53			ON object_data.obj_id = qpl_questions.obj_fi
54			AND $INtypes
55			WHERE qpl_questions.owner = %s
56			AND qpl_questions.tstamp = %s
57		";
58
59        $res = $this->db->queryF($query, array('integer', 'integer'), array($this->getOwnerId(), 0));
60
61        $questionIds = array();
62
63        while ($row = $this->db->fetchAssoc($res)) {
64            $questionIds[] = $row['question_id'];
65        }
66
67        return $questionIds;
68    }
69
70    private function purgeQuestionIds($questionIds)
71    {
72        require_once 'Modules/TestQuestionPool/classes/class.assQuestion.php';
73
74        foreach ($questionIds as $questionId) {
75            $question = assQuestion::_instantiateQuestion($questionId);
76            $question->delete($questionId);
77        }
78    }
79
80    protected function setIgnoredContainerObjectTypes($ignoredContainerObjectTypes)
81    {
82        $this->ignoredContainerObjectTypes = $ignoredContainerObjectTypes;
83    }
84
85    protected function getIgnoredContainerObjectTypes()
86    {
87        return $this->ignoredContainerObjectTypes;
88    }
89}
90