ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 __construct($exercise, $a_xml_data, $obj_id = -1)
76  {
77 // @todo: needs to be revised for multiple assignments per exercise
78 
79  parent::__construct();
80 
81  $this->exercise = $exercise;
82  // get all assignments and choose first one if exists, otherwise create
83  $assignments = ilExAssignment::getAssignmentDataOfExercise($exercise->getId());
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  switch($a_name)
127  {
128  case 'Exercise':
129  if (isset($a_attribs["obj_id"]))
130  {
131  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
132  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
133  {
134  throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
135  }
136  }
137  break;
138  case 'Member':
139  $this->usr_action = $a_attribs["action"];
140  $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
141  break;
142 
143  case 'File':
144  $this->file_action = $a_attribs["action"];
145  break;
146  case 'Content':
148  if ($a_attribs["mode"] == "GZIP")
149  {
150  if (!function_exists("gzdecode"))
151  throw new ilExerciseException ("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
152 
154  } elseif ($a_attribs["mode"] == "ZLIB")
155  {
156  if (!function_exists("gzuncompress"))
157  throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
158 
160  }
161  break;
162  case 'Marking':
163  $this->status = $a_attribs["status"];
164  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED)
165  $this->status = "notgraded";
166  elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED)
167  $this->status = "passed";
168  else
169  $this->status = "failed";
170  break;
171 
172  }
173  }
174 
175 
176 
183  function handlerEndTag($a_xml_parser,$a_name)
184  {
185  switch($a_name)
186  {
187  case 'Exercise':
188  $this->result = true;
189  break;
190  case 'Title':
191  $this->exercise->setTitle(trim($this->cdata));
192  $this->assignment->setTitle(trim($this->cdata));
193  break;
194  case 'Description':
195  $this->exercise->setDescription(trim($this->cdata));
196  break;
197  case 'Instruction':
198  $this->assignment->setInstruction(trim($this->cdata));
199  break;
200  case 'DueDate':
201  $this->assignment->setDeadLine(trim($this->cdata));
202  break;
203  case 'Member':
204  $this->updateMember($this->usr_id, $this->usr_action);
205  // update marking after update member.
206  $this->updateMarking($this->usr_id);
207  break;
208  case 'Filename':
209  $this->file_name = trim($this->cdata);
210  break;
211  case 'Content':
212  $this->file_content = trim($this->cdata);
213  break;
214  case 'File':
215  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
216  break;
217  case 'Comment':
218  $this->comment = trim($this->cdata);
219  break;
220  case 'Notice':
221  $this->notice = trim($this->cdata);
222  break;
223  case 'Mark':
224  $this->mark = trim($this->cdata);
225  break;
226  case 'Marking':
227  // see Member end tag
228  break;
229  }
230 
231  $this->cdata = '';
232 
233  return;
234  }
235 
242  function handlerCharacterData($a_xml_parser,$a_data)
243  {
244  if($a_data != "\n")
245  {
246  $this->cdata .= $a_data;
247  }
248  }
249 
250 
257  private function updateMember ($user_id, $action) {
258  if (!is_int($user_id) || $user_id <= 0) {
259  return;
260  }
261  $memberObject = new ilExerciseMembers($this->exercise);
262 
263  if ($action == "Attach" && !$memberObject->isAssigned($user_id))
264  {
265  $memberObject->assignMember ($user_id);
266  }
267 
268  if ($action == "Detach" && $memberObject->isAssigned($user_id))
269  {
270  $memberObject->deassignMember ($user_id);
271  }
272  }
273 
281  private function updateFile ($filename, $b64encodedContent, $action)
282  {
283  if (strlen($filename) == 0) {
284  return;
285  }
286  $filename= $this->storage->getAbsolutePath()."/".$filename;
287 
288  if ($action == "Attach")
289  {
290  $content = base64_decode((string) $b64encodedContent);
291  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
292  $content = gzdecode($content);
293  }elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
294  $content = gzuncompress($content);
295  }
296 
297  //echo $filename;
298  $this->storage->writeToFile($content, $filename);
299 
300  }
301  if ($action == "Detach")
302  {
303  $this->storage->deleteFile($filename);
304  }
305  }
306 
314  public function start () {
315  $this->startParsing();
316  return $this->result > 0;
317  }
318 
324  private function updateMarking($usr_id) {
325 
326  $member_status = $this->assignment->getMemberStatus($usr_id);
327  if (isset($this->mark))
328  {
329  $member_status->setMark(ilUtil::stripSlashes($this->mark));
330  }
331  if (isset($this->comment))
332  {
333  $member_status->setComment(ilUtil::stripSlashes($this->comment));
334  }
335  if (isset($this->status))
336  {
337  $member_status->setStatus(ilUtil::stripSlashes($this->status));
338  }
339  if (isset($this->notice))
340  {
341  $member_status->setNotice(ilUtil::stripSlashes($this->notice));
342  }
343  $member_status->update();
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 ?>
updateMarking($usr_id)
update marking of member
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
__construct($exercise, $a_xml_data, $obj_id=-1)
Constructor.
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
comment()
Definition: comment.php:2
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
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.