1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* Test results to XML class
6*
7* @author Helmut Schottmüller <ilias@aurealis.de>
8* @version $Id$
9* @ingroup ModulesTest
10*/
11include_once './Services/Xml/classes/class.ilXmlWriter.php';
12
13class ilTestResultsToXML extends ilXmlWriter
14{
15    private $test_id = 0;
16    private $anonymized = false;
17    private $active_ids;
18
19    protected $includeRandomTestQuestionsEnabled = false;
20
21    public function __construct($test_id, $anonymized = false)
22    {
23        parent::__construct();
24        $this->test_id = $test_id;
25        $this->anonymized = $anonymized;
26    }
27
28    /**
29     * @return boolean
30     */
31    public function isIncludeRandomTestQuestionsEnabled()
32    {
33        return $this->includeRandomTestQuestionsEnabled;
34    }
35
36    /**
37     * @param boolean $includeRandomTestQuestionsEnabled
38     */
39    public function setIncludeRandomTestQuestionsEnabled($includeRandomTestQuestionsEnabled)
40    {
41        $this->includeRandomTestQuestionsEnabled = $includeRandomTestQuestionsEnabled;
42    }
43
44    protected function exportActiveIDs()
45    {
46        global $DIC;
47        $ilDB = $DIC['ilDB'];
48        $ilSetting = $DIC['ilSetting'];
49
50        include_once "./Modules/Test/classes/class.ilObjTestAccess.php";
51        $assessmentSetting = new ilSetting("assessment");
52        $user_criteria = $assessmentSetting->get("user_criteria");
53        if (strlen($user_criteria) == 0) {
54            $user_criteria = 'usr_id';
55        }
56
57        if ($this->anonymized) {
58            $result = $ilDB->queryF(
59                "SELECT * FROM tst_active WHERE test_fi = %s",
60                array('integer'),
61                array($this->test_id)
62            );
63        } else {
64            $result = $ilDB->queryF(
65                "SELECT tst_active.*, usr_data." . $user_criteria . " FROM tst_active, usr_data WHERE tst_active.test_fi = %s AND tst_active.user_fi = usr_data.usr_id",
66                array('integer'),
67                array($this->test_id)
68            );
69        }
70        $this->xmlStartTag("tst_active", null);
71        while ($row = $ilDB->fetchAssoc($result)) {
72            $attrs = array(
73                'active_id' => $row['active_id'],
74                'user_fi' => $row['user_fi'],
75                'anonymous_id' => $row['anonymous_id'],
76                'test_fi' => $row['test_fi'],
77                'lastindex' => $row['lastindex'],
78                'tries' => $row['tries'],
79                'last_started_pass' => $row['last_started_pass'],
80                'last_finished_pass' => $row['last_finished_pass'],
81                'submitted' => $row['submitted'],
82                'submittimestamp' => $row['submittimestamp'],
83                'tstamp' => $row['tstamp']
84            );
85            $attrs['fullname'] = ilObjTestAccess::_getParticipantData($row['active_id']);
86            if (!$this->anonymized) {
87                $attrs['user_criteria'] = $user_criteria;
88                $attrs[$user_criteria] = $row[$user_criteria];
89            }
90            array_push($this->active_ids, $row['active_id']);
91            $this->xmlElement("row", $attrs);
92        }
93        $this->xmlEndTag("tst_active");
94    }
95
96    protected function exportPassResult()
97    {
98        global $DIC;
99        $ilDB = $DIC['ilDB'];
100
101        $query = "SELECT * FROM tst_pass_result WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY active_fi, pass";
102        $result = $ilDB->query($query);
103        $this->xmlStartTag("tst_pass_result", null);
104        while ($row = $ilDB->fetchAssoc($result)) {
105            $attrs = array(
106                'active_fi' => $row['active_fi'],
107                'pass' => $row['pass'],
108                'points' => $row['points'],
109                'maxpoints' => $row['maxpoints'],
110                'questioncount' => $row['questioncount'],
111                'answeredquestions' => $row['answeredquestions'],
112                'workingtime' => $row['workingtime'],
113                'tstamp' => $row['tstamp']
114            );
115            $this->xmlElement("row", $attrs);
116        }
117        $this->xmlEndTag("tst_pass_result");
118    }
119
120    protected function exportResultCache()
121    {
122        global $DIC;
123        $ilDB = $DIC['ilDB'];
124
125        $query = "SELECT * FROM tst_result_cache WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY active_fi";
126        $result = $ilDB->query($query);
127        $this->xmlStartTag("tst_result_cache", null);
128        while ($row = $ilDB->fetchAssoc($result)) {
129            $attrs = array(
130                'active_fi' => $row['active_fi'],
131                'pass' => $row['pass'],
132                'max_points' => $row['max_points'],
133                'reached_points' => $row['reached_points'],
134                'mark_short' => $row['mark_short'],
135                'mark_official' => $row['mark_official'],
136                'passed' => $row['passed'],
137                'failed' => $row['failed'],
138                'tstamp' => $row['tstamp']
139            );
140            $this->xmlElement("row", $attrs);
141        }
142        $this->xmlEndTag("tst_result_cache");
143    }
144
145    protected function exportTestSequence()
146    {
147        global $DIC;
148        $ilDB = $DIC['ilDB'];
149
150        $query = "SELECT * FROM tst_sequence WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY active_fi, pass";
151        $result = $ilDB->query($query);
152        $this->xmlStartTag("tst_sequence", null);
153        while ($row = $ilDB->fetchAssoc($result)) {
154            $attrs = array(
155                'active_fi' => $row['active_fi'],
156                'pass' => $row['pass'],
157                'sequence' => $row['sequence'],
158                'postponed' => $row['postponed'],
159                'hidden' => $row['hidden'],
160                'tstamp' => $row['tstamp']
161            );
162            $this->xmlElement("row", $attrs);
163        }
164        $this->xmlEndTag("tst_sequence");
165    }
166
167    protected function exportTestSolutions()
168    {
169        global $DIC;
170        $ilDB = $DIC['ilDB'];
171
172        $query = "SELECT * FROM tst_solutions WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY solution_id";
173        $result = $ilDB->query($query);
174        $this->xmlStartTag("tst_solutions", null);
175        while ($row = $ilDB->fetchAssoc($result)) {
176            $attrs = array(
177                'solution_id' => $row['solution_id'],
178                'active_fi' => $row['active_fi'],
179                'question_fi' => $row['question_fi'],
180                'points' => $row['points'],
181                'pass' => $row['pass'],
182                'value1' => $row['value1'],
183                'value2' => $row['value2'],
184                'tstamp' => $row['tstamp']
185            );
186            $this->xmlElement("row", $attrs);
187        }
188        $this->xmlEndTag("tst_solutions");
189    }
190
191    protected function exportRandomTestQuestions()
192    {
193        global $DIC;
194        $ilDB = $DIC['ilDB'];
195
196        $result = $ilDB->query("
197			  SELECT * FROM tst_test_rnd_qst
198			  WHERE {$ilDB->in('active_fi', $this->active_ids, false, 'integer')}
199			  ORDER BY test_random_question_id
200		");
201
202        $this->xmlStartTag('tst_test_rnd_qst', null);
203        while ($row = $ilDB->fetchAssoc($result)) {
204            $attrs = array();
205
206            foreach ($row as $field => $value) {
207                $attrs[$field] = $value;
208            }
209
210            $this->xmlElement('row', $attrs);
211        }
212        $this->xmlEndTag('tst_test_rnd_qst');
213    }
214
215
216    protected function exportTestResults()
217    {
218        global $DIC;
219        $ilDB = $DIC['ilDB'];
220
221        $query = "SELECT * FROM tst_test_result WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY active_fi";
222        $result = $ilDB->query($query);
223        $this->xmlStartTag("tst_test_result", null);
224        while ($row = $ilDB->fetchAssoc($result)) {
225            $attrs = array(
226                'test_result_id' => $row['test_result_id'],
227                'active_fi' => $row['active_fi'],
228                'question_fi' => $row['question_fi'],
229                'points' => $row['points'],
230                'pass' => $row['pass'],
231                'manual' => $row['manual'],
232                'tstamp' => $row['tstamp']
233            );
234            $this->xmlElement("row", $attrs);
235        }
236        $this->xmlEndTag("tst_test_result");
237    }
238
239    protected function exportTestTimes()
240    {
241        global $DIC;
242        $ilDB = $DIC['ilDB'];
243
244        $query = "SELECT * FROM tst_times WHERE " . $ilDB->in('active_fi', $this->active_ids, false, 'integer') . " ORDER BY active_fi";
245        $result = $ilDB->query($query);
246        $this->xmlStartTag("tst_times", null);
247        while ($row = $ilDB->fetchAssoc($result)) {
248            $attrs = array(
249                'times_id' => $row['times_id'],
250                'active_fi' => $row['active_fi'],
251                'started' => $row['started'],
252                'finished' => $row['finished'],
253                'pass' => $row['pass'],
254                'tstamp' => $row['tstamp']
255            );
256            $this->xmlElement("row", $attrs);
257        }
258        $this->xmlEndTag("tst_times");
259    }
260
261    public function getXML()
262    {
263        $this->active_ids = array();
264        $this->xmlHeader();
265        $attrs = array("version" => "4.1.0");
266        $this->xmlStartTag("results", $attrs);
267        $this->exportActiveIDs();
268
269        if ($this->isIncludeRandomTestQuestionsEnabled()) {
270            $this->exportRandomTestQuestions();
271        }
272
273        $this->exportPassResult();
274        $this->exportResultCache();
275        $this->exportTestSequence();
276        $this->exportTestSolutions();
277        $this->exportTestResults();
278        $this->exportTestTimes();
279        $this->xmlEndTag("results");
280    }
281
282    public function xmlDumpMem($format = true)
283    {
284        $this->getXML();
285        return parent::xmlDumpMem($format);
286    }
287
288    public function xmlDumpFile($file, $format = true)
289    {
290        $this->getXML();
291        return parent::xmlDumpFile($file, $format);
292    }
293}
294