ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExerciseXMLParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'classes/class.ilSaxParser.php';
5 include_once 'Services/Tracking/classes/class.ilChangeEvent.php';
6 include_once 'Modules/Exercise/classes/class.ilExerciseException.php';
7 include_once 'Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
8 
20 {
21 
25 
31  var $exercise;
32 
39  var $obj_id;
40 
41 
47  var $result;
48 
54  var $mode;
55 
61 
66  var $storage;
67 
76  function ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id = -1)
77  {
78 // @todo: needs to be revised for multiple assignments per exercise
79 
81  $this->exercise = $exercise;
82  // get all assignments and choose first one if exists, otherwise create
84  if (count ($assignments) > 0) {
85  $this->assignment = new ilExAssignment($assignments [0]["id"]);
86  } else {
87  $this->assignment = new ilExAssignment();
88  $this->assignment->setExerciseId($exercise->getId());
89  $this->assignment->save();
90  }
91 
92  include_once ("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
93  $this->storage = new ilFSStorageExercise ( $this->exercise->getId(), $this->assignment->getId());
94  $this->storage->create();
95  $this->storage->init();
96 
97  $this->setXMLContent($a_xml_data);
98  $this->obj_id = $obj_id;
99  $this->result = false;
100  }
101 
102 
109  function setHandlers($a_xml_parser)
110  {
111  xml_set_object($a_xml_parser,$this);
112  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
113  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
114  }
115 
124  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
125  {
126  global $ilErr;
127 
128  switch($a_name)
129  {
130  case 'Exercise':
131  if (isset($a_attribs["obj_id"]))
132  {
133  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
134  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
135  {
136  throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
137  }
138  }
139  break;
140  case 'Member':
141  $this->usr_action = $a_attribs["action"];
142  $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
143  break;
144 
145  case 'File':
146  $this->file_action = $a_attribs["action"];
147  break;
148  case 'Content':
150  if ($a_attribs["mode"] == "GZIP")
151  {
152  if (!function_exists("gzdecode"))
153  throw new ilExerciseException ("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
154 
156  } elseif ($a_attribs["mode"] == "ZLIB")
157  {
158  if (!function_exists("gzuncompress"))
159  throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
160 
162  }
163  break;
164  case 'Marking':
165  $this->status = $a_attribs["status"];
166  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED)
167  $this->status = "notgraded";
168  elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED)
169  $this->status = "passed";
170  else
171  $this->status = "failed";
172  break;
173 
174  }
175  }
176 
177 
178 
185  function handlerEndTag($a_xml_parser,$a_name)
186  {
187  switch($a_name)
188  {
189  case 'Exercise':
190  $this->result = true;
191  break;
192  case 'Title':
193  $this->exercise->setTitle(trim($this->cdata));
194  $this->assignment->setTitle(trim($this->cdata));
195  break;
196  case 'Description':
197  $this->exercise->setDescription(trim($this->cdata));
198  break;
199  case 'Instruction':
200  $this->assignment->setInstruction(trim($this->cdata));
201  break;
202  case 'DueDate':
203  $this->assignment->setDeadLine(trim($this->cdata));
204  break;
205  case 'Member':
206  $this->updateMember($this->usr_id, $this->usr_action);
207  // update marking after update member.
208  $this->updateMarking($this->usr_id);
209  break;
210  case 'Filename':
211  $this->file_name = trim($this->cdata);
212  break;
213  case 'Content':
214  $this->file_content = trim($this->cdata);
215  break;
216  case 'File':
217  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
218  break;
219  case 'Comment':
220  $this->comment = trim($this->cdata);
221  break;
222  case 'Notice':
223  $this->notice = trim($this->cdata);
224  break;
225  case 'Mark':
226  $this->mark = trim($this->cdata);
227  break;
228  case 'Marking':
229  // see Member end tag
230  break;
231  }
232 
233  $this->cdata = '';
234 
235  return;
236  }
237 
244  function handlerCharacterData($a_xml_parser,$a_data)
245  {
246  if($a_data != "\n")
247  {
248  $this->cdata .= $a_data;
249  }
250  }
251 
252 
259  private function updateMember ($user_id, $action) {
260  if (!is_int($user_id) || $user_id <= 0) {
261  return;
262  }
263  $memberObject = new ilExerciseMembers($this->exercise);
264 
265  if ($action == "Attach" && !$memberObject->isAssigned($user_id))
266  {
267  $memberObject->assignMember ($user_id);
268  }
269 
270  if ($action == "Detach" && $memberObject->isAssigned($user_id))
271  {
272  $memberObject->deassignMember ($user_id);
273  }
274  }
275 
283  private function updateFile ($filename, $b64encodedContent, $action)
284  {
285  if (strlen($filename) == 0) {
286  return;
287  }
288  $filename= $this->storage->getAbsolutePath()."/".$filename;
289 
290  if ($action == "Attach")
291  {
292  $content = base64_decode((string) $b64encodedContent);
293  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
294  $content = gzdecode($content);
295  }elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
296  $content = gzuncompress($content);
297  }
298 
299  //echo $filename;
300  $this->storage->writeToFile($content, $filename);
301 
302  }
303  if ($action == "Detach")
304  {
305  $this->storage->deleteFile($filename);
306  }
307  }
308 
316  public function start () {
317  $this->startParsing();
318  return $this->result > 0;
319  }
320 
326  private function updateMarking($usr_id) {
327  if (isset($this->mark))
328  {
329  ilExAssignment::updateMarkOfUser($this->assignment->getId(), $usr_id, ilUtil::stripSlashes($this->mark));
330  }
331  if (isset($this->comment))
332  {
333 
334  ilExAssignment::updateCommentForUser($this->assignment->getId(), $usr_id, ilUtil::stripSlashes($this->comment));
335  }
336 
337  //$memberObject = $this->exercise->members_obj;
338  if (isset ( $this->status )){
339 
340  ilExAssignment::updateStatusOfUser ( $this->assignment->getId (), $usr_id, ilUtil::stripSlashes ( $this->status ) );
341  }
342  if (isset($this->notice)){
343  ilExAssignment::updateNoticeForUser($this->assignment->getId(), $usr_id, ilUtil::stripSlashes($this->notice));
344  }
345  // reset variables
346  $this->mark = null;
347  $this->status = null;
348  $this->notice = null;
349  $this->comment = null;
350  }
351 
352  public function getAssignment () {
353  return $this->assignment;
354  }
355 
356 }
357 ?>