ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 'Services/Tracking/classes/class.ilChangeEvent.php';
5 include_once 'Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
6 include_once './Services/Xml/classes/class.ilSaxParser.php';
7 
19 {
20 
24 
30  var $exercise;
31 
38  var $obj_id;
39 
40 
46  var $result;
47 
53  var $mode;
54 
60 
65  var $storage;
66 
75  function ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id = -1)
76  {
77 // @todo: needs to be revised for multiple assignments per exercise
78 
79  parent::ilSaxParser();
80  $this->exercise = $exercise;
81  // get all assignments and choose first one if exists, otherwise create
82  $assignments = ilExAssignment::getAssignmentDataOfExercise($exercise->getId());
83  if (count ($assignments) > 0) {
84  $this->assignment = new ilExAssignment($assignments [0]["id"]);
85  } else {
86  $this->assignment = new ilExAssignment();
87  $this->assignment->setExerciseId($exercise->getId());
88  $this->assignment->save();
89  }
90 
91  include_once ("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
92  $this->storage = new ilFSStorageExercise ( $this->exercise->getId(), $this->assignment->getId());
93  $this->storage->create();
94  $this->storage->init();
95 
96  $this->setXMLContent($a_xml_data);
97  $this->obj_id = $obj_id;
98  $this->result = false;
99  }
100 
101 
108  function setHandlers($a_xml_parser)
109  {
110  xml_set_object($a_xml_parser,$this);
111  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
112  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
113  }
114 
123  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
124  {
125  global $ilErr;
126 
127  switch($a_name)
128  {
129  case 'Exercise':
130  if (isset($a_attribs["obj_id"]))
131  {
132  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
133  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
134  {
135  throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
136  }
137  }
138  break;
139  case 'Member':
140  $this->usr_action = $a_attribs["action"];
141  $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
142  break;
143 
144  case 'File':
145  $this->file_action = $a_attribs["action"];
146  break;
147  case 'Content':
149  if ($a_attribs["mode"] == "GZIP")
150  {
151  if (!function_exists("gzdecode"))
152  throw new ilExerciseException ("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
153 
155  } elseif ($a_attribs["mode"] == "ZLIB")
156  {
157  if (!function_exists("gzuncompress"))
158  throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
159 
161  }
162  break;
163  case 'Marking':
164  $this->status = $a_attribs["status"];
165  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED)
166  $this->status = "notgraded";
167  elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED)
168  $this->status = "passed";
169  else
170  $this->status = "failed";
171  break;
172 
173  }
174  }
175 
176 
177 
184  function handlerEndTag($a_xml_parser,$a_name)
185  {
186  switch($a_name)
187  {
188  case 'Exercise':
189  $this->result = true;
190  break;
191  case 'Title':
192  $this->exercise->setTitle(trim($this->cdata));
193  $this->assignment->setTitle(trim($this->cdata));
194  break;
195  case 'Description':
196  $this->exercise->setDescription(trim($this->cdata));
197  break;
198  case 'Instruction':
199  $this->assignment->setInstruction(trim($this->cdata));
200  break;
201  case 'DueDate':
202  $this->assignment->setDeadLine(trim($this->cdata));
203  break;
204  case 'Member':
205  $this->updateMember($this->usr_id, $this->usr_action);
206  // update marking after update member.
207  $this->updateMarking($this->usr_id);
208  break;
209  case 'Filename':
210  $this->file_name = trim($this->cdata);
211  break;
212  case 'Content':
213  $this->file_content = trim($this->cdata);
214  break;
215  case 'File':
216  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
217  break;
218  case 'Comment':
219  $this->comment = trim($this->cdata);
220  break;
221  case 'Notice':
222  $this->notice = trim($this->cdata);
223  break;
224  case 'Mark':
225  $this->mark = trim($this->cdata);
226  break;
227  case 'Marking':
228  // see Member end tag
229  break;
230  }
231 
232  $this->cdata = '';
233 
234  return;
235  }
236 
243  function handlerCharacterData($a_xml_parser,$a_data)
244  {
245  if($a_data != "\n")
246  {
247  $this->cdata .= $a_data;
248  }
249  }
250 
251 
258  private function updateMember ($user_id, $action) {
259  if (!is_int($user_id) || $user_id <= 0) {
260  return;
261  }
262  $memberObject = new ilExerciseMembers($this->exercise);
263 
264  if ($action == "Attach" && !$memberObject->isAssigned($user_id))
265  {
266  $memberObject->assignMember ($user_id);
267  }
268 
269  if ($action == "Detach" && $memberObject->isAssigned($user_id))
270  {
271  $memberObject->deassignMember ($user_id);
272  }
273  }
274 
282  private function updateFile ($filename, $b64encodedContent, $action)
283  {
284  if (strlen($filename) == 0) {
285  return;
286  }
287  $filename= $this->storage->getAbsolutePath()."/".$filename;
288 
289  if ($action == "Attach")
290  {
291  $content = base64_decode((string) $b64encodedContent);
292  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
293  $content = gzdecode($content);
294  }elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
295  $content = gzuncompress($content);
296  }
297 
298  //echo $filename;
299  $this->storage->writeToFile($content, $filename);
300 
301  }
302  if ($action == "Detach")
303  {
304  $this->storage->deleteFile($filename);
305  }
306  }
307 
315  public function start () {
316  $this->startParsing();
317  return $this->result > 0;
318  }
319 
325  private function updateMarking($usr_id) {
326 
327  $member_status = $this->assignment->getMemberStatus($usr_id);
328  if (isset($this->mark))
329  {
330  $member_status->setMark(ilUtil::stripSlashes($this->mark));
331  }
332  if (isset($this->comment))
333  {
334  $member_status->setComment(ilUtil::stripSlashes($this->comment));
335  }
336  if (isset($this->status))
337  {
338  $member_status->setStatus(ilUtil::stripSlashes($this->status));
339  }
340  if (isset($this->notice))
341  {
342  $member_status->setNotice(ilUtil::stripSlashes($this->notice));
343  }
344  $member_status->update();
345 
346  // reset variables
347  $this->mark = null;
348  $this->status = null;
349  $this->notice = null;
350  $this->comment = null;
351  }
352 
353  public function getAssignment () {
354  return $this->assignment;
355  }
356 
357 }
358 ?>
updateMarking($usr_id)
update marking of member
ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id=-1)
Constructor.
static getAssignmentDataOfExercise($a_exc_id)
Get assignments data of an exercise in an array.
Exercise assignment.
Class ilExerciseMembers.
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
updateFile($filename, $b64encodedContent, $action)
update file according to filename
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
$filename
Definition: buildRTE.php:89
setHandlers($a_xml_parser)
set event handlers
updateMember($user_id, $action)
update member object according to given action
Exercise XML Parser which completes/updates a given exercise by an xml string.
setXMLContent($a_xml_content)
static __extractId($ilias_id, $inst_id)
extract ref id from role title, e.g.
start()
starts parsing an changes object by side effect.
Class to report exception.