ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilTestResultsToXML.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\ResourceStorage\Services as ResourceStorage;
22
24{
25 private array $active_ids = [];
26
28
29 public function __construct(
30 private readonly ilObjTest $test_obj,
31 private readonly ilDBInterface $db,
32 private readonly ResourceStorage $irss,
33 private readonly ilObjUser $user,
34 private readonly ilLanguage $lng,
35 private string $objects_export_directory
36 ) {
38 }
39
41 {
43 }
44
46 {
47 $this->include_random_test_questions_enabled = $include_random_test_questions_enabled;
48 }
49
50 protected function exportActiveIDs(): void
51 {
52 $user_criteria = (new ilSetting('assessment'))->get('user_criteria', 'usr_id');
53
54 $query = $this->test_obj->getAnonymity()
55 ? 'SELECT * FROM tst_active WHERE test_fi = %s'
56 : "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";
57 $result = $this->db->queryF($query, [ilDBConstants::T_INTEGER], [$this->test_obj->getTestId()]);
58
59 $test_participant_list = new ilTestParticipantList($this->test_obj, $this->user, $this->lng, $this->db);
60 $test_participant_list->initializeFromDbRows($this->test_obj->getTestParticipants());
61
62 $this->xmlStartTag('tst_active', null);
63 while ($row = $this->db->fetchAssoc($result)) {
64 $this->active_ids[] = $row['active_id'];
65 $participant = $test_participant_list->getParticipantByActiveId($row['active_id']);
66
67 $attrs = [
68 'active_id' => $row['active_id'],
69 'user_fi' => $this->test_obj->getAnonymity() ? '' : ($row['user_fi'] ?? ''),
70 'fullname' => $participant ? $test_participant_list->buildFullname($participant) : '',
71 'anonymous_id' => $row['anonymous_id'] ?? '',
72 'test_fi' => $row['test_fi'],
73 'lastindex' => $row['lastindex'] ?? '',
74 'tries' => $row['tries'] ?? '',
75 'last_started_pass' => $row['last_started_pass'] ?? '',
76 'last_finished_pass' => $row['last_finished_pass'] ?? '',
77 'submitted' => $row['submitted'] ?? '',
78 'submittimestamp' => $row['submittimestamp'] ?? '',
79 'tstamp' => $row['tstamp'] ?? ''
80 ];
81
82 if (!$this->test_obj->getAnonymity()) {
83 $attrs['user_criteria'] = $user_criteria;
84 $attrs[$user_criteria] = $row[$user_criteria];
85 }
86
87 $this->xmlElement('row', $attrs);
88 }
89 $this->xmlEndTag('tst_active');
90 }
91
92 protected function exportPassResult(): void
93 {
94 $query = 'SELECT * FROM tst_pass_result WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY active_fi, pass';
95 $result = $this->db->query($query);
96 $this->xmlStartTag('tst_pass_result', null);
97 while ($row = $this->db->fetchAssoc($result)) {
98 $attrs = [
99 'active_fi' => $row['active_fi'],
100 'pass' => $row['pass'] ?? '',
101 'points' => $row['points'] ?? '',
102 'maxpoints' => $row['maxpoints'] ?? '',
103 'questioncount' => $row['questioncount'] ?? '',
104 'answeredquestions' => $row['answeredquestions'] ?? '',
105 'workingtime' => $row['workingtime'] ?? '',
106 'tstamp' => $row['tstamp'] ?? ''
107 ];
108 $this->xmlElement('row', $attrs);
109 }
110 $this->xmlEndTag('tst_pass_result');
111 }
112
113 protected function exportResultCache(): void
114 {
115 $query = 'SELECT * FROM tst_result_cache WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY active_fi';
116 $result = $this->db->query($query);
117 $this->xmlStartTag('tst_result_cache', null);
118 while ($row = $this->db->fetchAssoc($result)) {
119 $attrs = [
120 'active_fi' => $row['active_fi'],
121 'pass' => $row['pass'],
122 'max_points' => $row['max_points'],
123 'reached_points' => $row['reached_points'],
124 'mark_short' => $row['mark_short'],
125 'mark_official' => $row['mark_official'],
126 'passed' => $row['passed'],
127 'failed' => $row['failed'],
128 'tstamp' => $row['tstamp']
129 ];
130 $this->xmlElement('row', $attrs);
131 }
132 $this->xmlEndTag('tst_result_cache');
133 }
134
135 protected function exportTestSequence(): void
136 {
137 $query = 'SELECT * FROM tst_sequence WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY active_fi, pass';
138 $result = $this->db->query($query);
139 $this->xmlStartTag('tst_sequence', null);
140 while ($row = $this->db->fetchAssoc($result)) {
141 $attrs = [
142 'active_fi' => $row['active_fi'],
143 'pass' => $row['pass'] ?? '',
144 'sequence' => $row['sequence'] ?? '',
145 'postponed' => $row['postponed'] ?? '',
146 'hidden' => $row['hidden'] ?? '',
147 'tstamp' => $row['tstamp'] ?? ''
148 ];
149 $this->xmlElement('row', $attrs);
150 }
151 $this->xmlEndTag('tst_sequence');
152 }
153
154 protected function exportTestSolutions(): void
155 {
156 $query = 'SELECT * FROM tst_solutions WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY solution_id';
157 $result = $this->db->query($query);
158 $this->xmlStartTag('tst_solutions', null);
159 while ($row = $this->db->fetchAssoc($result)) {
160 $attrs = [
161 'solution_id' => $row['solution_id'],
162 'active_fi' => $row['active_fi'],
163 'question_fi' => $row['question_fi'],
164 'points' => $row['points'] ?? '',
165 'pass' => $row['pass'] ?? '',
166 'value1' => $row['value1'] ?? '',
167 'value2' => $row['value2'] ?? '',
168 'tstamp' => $row['tstamp'] ?? ''
169 ];
170 $this->xmlElement('row', $attrs);
171 }
172 $this->xmlEndTag('tst_solutions');
173 }
174
175 protected function exportRandomTestQuestions(): void
176 {
177 $result = $this->db->query("
178 SELECT * FROM tst_test_rnd_qst
179 WHERE {$this->db->in('active_fi', $this->active_ids, false, 'integer')}
180 ORDER BY test_random_question_id
181 ");
182
183 $this->xmlStartTag('tst_test_rnd_qst', null);
184 while ($row = $this->db->fetchAssoc($result)) {
185 $attrs = [];
186
187 foreach ($row as $field => $value) {
188 $attrs[$field] = $value;
189 }
190
191 $this->xmlElement('row', $attrs);
192 }
193 $this->xmlEndTag('tst_test_rnd_qst');
194 }
195
196
197 protected function exportTestResults(): void
198 {
199 $query = 'SELECT * FROM tst_test_result WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY active_fi';
200 $result = $this->db->query($query);
201 $this->xmlStartTag('tst_test_result', null);
202 while ($row = $this->db->fetchAssoc($result)) {
203 $active_fi = $row['active_fi'];
204 $pass = $row['pass'] ?? '';
205 $attrs = [
206 'test_result_id' => $row['test_result_id'],
207 'active_fi' => $active_fi,
208 'question_fi' => $row['question_fi'],
209 'points' => $row['points'] ?? '',
210 'pass' => $pass,
211 'manual' => $row['manual'] ?? '',
212 'tstamp' => $row['tstamp'] ?? ''
213 ];
214
215 if (($question = assQuestion::instantiateQuestion($row['question_fi'])) instanceof assFileUpload) {
216 $this->exportParticipantUploadedFiles($question->getUploadedFiles($active_fi, $pass));
217 }
218
219 $this->xmlElement('row', $attrs);
220 }
221 $this->xmlEndTag('tst_test_result');
222 }
223
227 protected function exportParticipantUploadedFiles(array $uploaded_files): void
228 {
229 foreach ($uploaded_files as $uploaded_file) {
230 if ($uploaded_file['value2'] !== 'rid') {
231 continue;
232 }
233
234 $rid_string = $uploaded_file['value1'];
235 $rid = $this->irss->manage()->find($rid_string);
236 if ($rid === null) {
237 continue;
238 }
239
240 $target_dir = "$this->objects_export_directory/resources/$rid_string";
241 ilFileUtils::makeDirParents($target_dir);
242 file_put_contents(
243 "$target_dir/{$this->irss->manage()->getCurrentRevision($rid)->getTitle()}",
244 $this->irss->consume()->stream($rid)->getStream(),
245 );
246 }
247 }
248
249 protected function exportTestTimes(): void
250 {
251 $query = 'SELECT * FROM tst_times WHERE ' . $this->db->in('active_fi', $this->active_ids, false, 'integer') . ' ORDER BY active_fi';
252 $result = $this->db->query($query);
253 $this->xmlStartTag('tst_times', null);
254 while ($row = $this->db->fetchAssoc($result)) {
255 $attrs = [
256 'times_id' => $row['times_id'],
257 'active_fi' => $row['active_fi'],
258 'started' => $row['started'],
259 'finished' => $row['finished'],
260 'pass' => $row['pass'],
261 'tstamp' => $row['tstamp']
262 ];
263 $this->xmlElement('row', $attrs);
264 }
265 $this->xmlEndTag('tst_times');
266 }
267
268 public function getXML(): void
269 {
270 $this->active_ids = [];
271 $this->xmlHeader();
272 $attrs = ['version' => '4.1.0'];
273 $this->xmlStartTag('results', $attrs);
274 $this->exportActiveIDs();
275
278 }
279
280 $this->exportPassResult();
281 $this->exportResultCache();
282 $this->exportTestSequence();
283 $this->exportTestSolutions();
284 $this->exportTestResults();
285 $this->exportTestTimes();
286 $this->xmlEndTag('results');
287 }
288
289 public function xmlDumpMem(bool $format = true): string
290 {
291 $this->getXML();
292 return parent::xmlDumpMem($format);
293 }
294
295 public function xmlDumpFile(string $file, bool $format = true): void
296 {
297 $this->getXML();
298 parent::xmlDumpFile($file, $format);
299 }
300}
Class for file upload questions.
static instantiateQuestion(int $question_id)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
language handling
User class.
ILIAS Setting Class.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
__construct(private readonly ilObjTest $test_obj, private readonly ilDBInterface $db, private readonly ResourceStorage $irss, private readonly ilObjUser $user, private readonly ilLanguage $lng, private string $objects_export_directory)
xmlDumpFile(string $file, bool $format=true)
Dumps xml document from memory into a file.
exportParticipantUploadedFiles(array $uploaded_files)
setIncludeRandomTestQuestionsEnabled(bool $include_random_test_questions_enabled)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
Interface ilDBInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $lng
Definition: privfeed.php:31