ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTestResultsImportParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 include_once("./Services/Xml/classes/class.ilSaxParser.php");
12 
14 {
15  private $tst_obj;
16  private $table;
21  private $user_criteria_checked = false;
22 
24 
28  public function __construct($a_xml_file, &$test_object)
29  {
30  parent::__construct($a_xml_file, true);
31  $this->tst_obj = &$test_object;
32  $this->table = '';
33  $this->active_id_mapping = array();
34  $this->question_id_mapping = array();
35  $this->user_criteria_checked = false;
36  $this->src_pool_def_id_mapping = array();
37  }
38 
42  public function getQuestionIdMapping()
43  {
45  }
46 
51  {
52  $this->question_id_mapping = $question_id_mapping;
53  }
54 
58  public function getSrcPoolDefIdMapping()
59  {
61  }
62 
67  {
68  $this->src_pool_def_id_mapping = $src_pool_def_id_mapping;
69  }
70 
76  public function setHandlers($a_xml_parser)
77  {
78  xml_set_object($a_xml_parser, $this);
79  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
80  xml_set_character_data_handler($a_xml_parser, 'handlerParseCharacterData');
81  }
82 
86  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
87  {
88  global $DIC;
89  $ilDB = $DIC['ilDB'];
90 
91  $this->sametag = false;
92  $this->characterbuffer = "";
93  $this->depth[$a_xml_parser]++;
94  $this->path[$this->depth[$a_xml_parser]] = strtolower($a_name);
95  $this->qti_element = $a_name;
96 
97  switch (strtolower($a_name)) {
98  case "results":
99  break;
100  case "row":
101  switch ($this->table) {
102  case 'tst_active':
103  if (!$this->user_criteria_checked) {
104  $this->user_criteria_checked = true;
105  if ($ilDB->tableColumnExists('usr_data', $a_attribs['user_criteria'])) {
106  include_once './Services/Database/classes/class.ilDBAnalyzer.php';
107  $analyzer = new ilDBAnalyzer();
108  $info = $analyzer->getFieldInformation('usr_data');
109  $this->user_criteria_field = $a_attribs['user_criteria'];
110  $this->user_criteria_type = $info[$a_attribs['user_criteria']]['type'];
111  }
112  }
113  $usr_id = ANONYMOUS_USER_ID;
114  if (strlen($this->user_criteria_field)) {
115  $result = $ilDB->queryF(
116  "SELECT usr_id FROM usr_data WHERE " . $this->user_criteria_field . " = %s",
117  array($this->user_criteria_type),
118  array($a_attribs[$this->user_criteria_field])
119  );
120  if ($result->numRows()) {
121  $row = $ilDB->fetchAssoc($result);
122  $usr_id = $row['usr_id'];
123  }
124  }
125  $next_id = $ilDB->nextId('tst_active');
126 
127  $ilDB->insert('tst_active', array(
128  'active_id' => array('integer', $next_id),
129  'user_fi' => array('integer', $usr_id),
130  'anonymous_id' => array('text', strlen($a_attribs['anonymous_id']) ? $a_attribs['anonymous_id'] : null),
131  'test_fi' => array('integer', $this->tst_obj->getTestId()),
132  'lastindex' => array('integer', $a_attribs['lastindex']),
133  'tries' => array('integer', $a_attribs['tries']),
134  'submitted' => array('integer', $a_attribs['submitted']),
135  'submittimestamp' => array('timestamp', strlen($a_attribs['submittimestamp']) ? $a_attribs['submittimestamp'] : null),
136  'tstamp' => array('integer', $a_attribs['tstamp']),
137  'importname' => array('text', $a_attribs['fullname']),
138  'last_finished_pass' => array('integer', $this->fetchLastFinishedPass($a_attribs)),
139  'last_started_pass' => array('integer', $this->fetchLastStartedPass($a_attribs)),
140  'answerstatusfilter' => array('integer', $this->fetchAttribute($a_attribs, 'answer_status_filter')),
141  'objective_container' => array('integer', $this->fetchAttribute($a_attribs, 'objective_container'))
142  ));
143  $this->active_id_mapping[$a_attribs['active_id']] = $next_id;
144  break;
145  case 'tst_test_rnd_qst':
146  $nextId = $ilDB->nextId('tst_test_rnd_qst');
147  $newActiveId = $this->active_id_mapping[$a_attribs['active_fi']];
148  $newQuestionId = $this->question_id_mapping[$a_attribs['question_fi']];
149  $newSrcPoolDefId = $this->src_pool_def_id_mapping[$a_attribs['src_pool_def_fi']];
150  $ilDB->insert('tst_test_rnd_qst', array(
151  'test_random_question_id' => array('integer', $nextId),
152  'active_fi' => array('integer', $newActiveId),
153  'question_fi' => array('integer', $newQuestionId),
154  'sequence' => array('integer', $a_attribs['sequence']),
155  'pass' => array('integer', $a_attribs['pass']),
156  'tstamp' => array('integer', $a_attribs['tstamp']),
157  'src_pool_def_fi' => array('integer', $newSrcPoolDefId)
158  ));
159  break;
160  case 'tst_pass_result':
161  $affectedRows = $ilDB->manipulateF(
162  "INSERT INTO tst_pass_result (active_fi, pass, points, maxpoints, questioncount, answeredquestions, workingtime, tstamp) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",
163  array(
164  'integer',
165  'integer',
166  'float',
167  'float',
168  'integer',
169  'integer',
170  'integer',
171  'integer'
172  ),
173  array(
174  $this->active_id_mapping[$a_attribs['active_fi']],
175  strlen($a_attribs['pass']) ? $a_attribs['pass'] : 0,
176  ($a_attribs["points"]) ? $a_attribs["points"] : 0,
177  ($a_attribs["maxpoints"]) ? $a_attribs["maxpoints"] : 0,
178  $a_attribs["questioncount"],
179  $a_attribs["answeredquestions"],
180  ($a_attribs["workingtime"]) ? $a_attribs["workingtime"] : 0,
181  $a_attribs["tstamp"]
182  )
183  );
184  break;
185  case 'tst_result_cache':
186  $affectedRows = $ilDB->manipulateF(
187  "INSERT INTO tst_result_cache (active_fi, pass, max_points, reached_points, mark_short, mark_official, passed, failed, tstamp) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)",
188  array(
189  'integer',
190  'integer',
191  'float',
192  'float',
193  'text',
194  'text',
195  'integer',
196  'integer',
197  'integer'
198  ),
199  array(
200  $this->active_id_mapping[$a_attribs['active_fi']],
201  strlen($a_attribs['pass']) ? $a_attribs['pass'] : 0,
202  ($a_attribs["max_points"]) ? $a_attribs["max_points"] : 0,
203  ($a_attribs["reached_points"]) ? $a_attribs["reached_points"] : 0,
204  strlen($a_attribs["mark_short"]) ? $a_attribs["mark_short"] : " ",
205  strlen($a_attribs["mark_official"]) ? $a_attribs["mark_official"] : " ",
206  ($a_attribs["passed"]) ? 1 : 0,
207  ($a_attribs["failed"]) ? 1 : 0,
208  $a_attribs["tstamp"]
209  )
210  );
211  break;
212  case 'tst_sequence':
213  $affectedRows = $ilDB->insert("tst_sequence", array(
214  "active_fi" => array("integer", $this->active_id_mapping[$a_attribs['active_fi']]),
215  "pass" => array("integer", $a_attribs['pass']),
216  "sequence" => array("clob", $a_attribs['sequence']),
217  "postponed" => array("text", (strlen($a_attribs['postponed'])) ? $a_attribs['postponed'] : null),
218  "hidden" => array("text", (strlen($a_attribs['hidden'])) ? $a_attribs['hidden'] : null),
219  "tstamp" => array("integer", $a_attribs['tstamp'])
220  ));
221  break;
222  case 'tst_solutions':
223  $next_id = $ilDB->nextId('tst_solutions');
224  $affectedRows = $ilDB->insert("tst_solutions", array(
225  "solution_id" => array("integer", $next_id),
226  "active_fi" => array("integer", $this->active_id_mapping[$a_attribs['active_fi']]),
227  "question_fi" => array("integer", $this->question_id_mapping[$a_attribs['question_fi']]),
228  "value1" => array("clob", (strlen($a_attribs['value1'])) ? $a_attribs['value1'] : null),
229  "value2" => array("clob", (strlen($a_attribs['value2'])) ? $a_attribs['value2'] : null),
230  "pass" => array("integer", $a_attribs['pass']),
231  "tstamp" => array("integer", $a_attribs['tstamp'])
232  ));
233  break;
234  case 'tst_test_result':
235  $next_id = $ilDB->nextId('tst_test_result');
236  $affectedRows = $ilDB->manipulateF(
237  "INSERT INTO tst_test_result (test_result_id, active_fi, question_fi, points, pass, manual, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
238  array('integer', 'integer','integer', 'float', 'integer', 'integer','integer'),
239  array($next_id, $this->active_id_mapping[$a_attribs['active_fi']], $this->question_id_mapping[$a_attribs['question_fi']], $a_attribs['points'], $a_attribs['pass'], (strlen($a_attribs['manual'])) ? $a_attribs['manual'] : 0, $a_attribs['tstamp'])
240  );
241  break;
242  case 'tst_times':
243  $next_id = $ilDB->nextId('tst_times');
244  $affectedRows = $ilDB->manipulateF(
245  "INSERT INTO tst_times (times_id, active_fi, started, finished, pass, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
246  array('integer', 'integer', 'timestamp', 'timestamp', 'integer', 'integer'),
247  array($next_id, $this->active_id_mapping[$a_attribs['active_fi']], $a_attribs['started'], $a_attribs['finished'], $a_attribs['pass'], $a_attribs['tstamp'])
248  );
249  break;
250  }
251  break;
252  default:
253  $this->table = $a_name;
254  break;
255  }
256  }
257 
261  public function handlerEndTag($a_xml_parser, $a_name)
262  {
263  switch (strtolower($a_name)) {
264  case "tst_active":
265  global $DIC;
266  $ilLog = $DIC['ilLog'];
267  $ilLog->write("active id mapping: " . print_r($this->active_id_mapping, true));
268  break;
269  case "tst_test_question":
270  global $DIC;
271  $ilLog = $DIC['ilLog'];
272  $ilLog->write("question id mapping: " . print_r($this->question_id_mapping, true));
273  break;
274  }
275  }
276 
280  public function handlerParseCharacterData($a_xml_parser, $a_data)
281  {
282  // do nothing
283  }
284 
285  private function fetchAttribute($attributes, $name)
286  {
287  if (isset($attributes[$name])) {
288  return $attributes[$name];
289  }
290 
291  return null;
292  }
293 
294  private function fetchLastFinishedPass($attribs)
295  {
296  if (isset($attribs['last_finished_pass'])) {
297  return $attribs['last_finished_pass'];
298  }
299 
300  if ($attribs['tries'] > 0) {
301  return $attribs['tries'] - 1;
302  }
303 
304  return null;
305  }
306 
307  private function fetchLastStartedPass($attribs)
308  {
309  if (isset($attribs['last_started_pass'])) {
310  return $attribs['last_started_pass'];
311  }
312 
313  if ($attribs['tries'] > 0) {
314  return $attribs['tries'] - 1;
315  }
316 
317  return null;
318  }
319 }
setSrcPoolDefIdMapping($src_pool_def_id_mapping)
handlerParseCharacterData($a_xml_parser, $a_data)
handler for character data
$result
global $DIC
Definition: saml.php:7
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element parser
__construct($a_xml_file, &$test_object)
Constructor.
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
$row
global $ilDB
This class gives all kind of DB information using the database manager and reverse module...
$info
Definition: index.php:5
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class private