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