ILIAS  Release_4_0_x_branch Revision 61816
 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 'Modules/Exercise/classes/class.ilExerciseException.php';
6 include_once 'Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
7 
19 {
20 
24 
30  var $exercise;
31 
38  var $obj_id;
39 
40 
46  var $result;
47 
53  var $mode;
54 
63  function ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id = -1)
64  {
66  $this->exercise = $exercise;
67  $this->setXMLContent($a_xml_data);
68  $this->obj_id = $obj_id;
69  $this->result = false;
70  }
71 
72 
79  function setHandlers($a_xml_parser)
80  {
81  xml_set_object($a_xml_parser,$this);
82  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
83  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
84  }
85 
94  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
95  {
96  global $ilErr;
97 
98  switch($a_name)
99  {
100  case 'Exercise':
101  if (isset($a_attribs["obj_id"]))
102  {
103  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
104  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
105  {
106  throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
107  }
108  }
109  break;
110  case 'Member':
111  $this->usr_action = $a_attribs["action"];
112  $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
113  break;
114 
115  case 'File':
116  $this->file_action = $a_attribs["action"];
117  break;
118  case 'Content':
120  if ($a_attribs["mode"] == "GZIP")
121  {
122  if (!function_exists("gzdecode"))
123  throw new ilExerciseException ("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
124 
126  } elseif ($a_attribs["mode"] == "ZLIB")
127  {
128  if (!function_exists("gzuncompress"))
129  throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
130 
132  }
133  break;
134  case 'Marking':
135  $this->status = $a_attribs["status"];
136  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED)
137  $this->status = "notgraded";
139  $this->status = "passed";
140  else
141  $this->status = "failed";
142  break;
143 
144  }
145  }
146 
147 
148 
155  function handlerEndTag($a_xml_parser,$a_name)
156  {
157  switch($a_name)
158  {
159  case 'Exercise':
160  $this->result = true;
161  break;
162  case 'Title':
163  $this->exercise->setTitle(trim($this->cdata));
164  break;
165  case 'Description':
166  $this->exercise->setDescription(trim($this->cdata));
167  break;
168  case 'Instruction':
169  $this->exercise->setInstruction(trim($this->cdata));
170  break;
171  case 'DueDate':
172  $this->exercise->setTimestamp(trim($this->cdata));
173  break;
174  case 'Member':
175  $this->updateMember($this->usr_id, $this->usr_action);
176  // update marking after update member.
177  $this->updateMarking($this->usr_id);
178  break;
179  case 'Filename':
180  $this->file_name = trim($this->cdata);
181  break;
182  case 'Content':
183  $this->file_content = trim($this->cdata);
184  break;
185  case 'File':
186  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
187  break;
188  case 'Comment':
189  $this->comment = trim($this->cdata);
190  break;
191  case 'Notice':
192  $this->notice = trim($this->cdata);
193  break;
194  case 'Mark':
195  $this->mark = trim($this->cdata);
196  break;
197  case 'Marking':
198  // see Member end tag
199  break;
200  }
201 
202  $this->cdata = '';
203 
204  return;
205  }
206 
213  function handlerCharacterData($a_xml_parser,$a_data)
214  {
215  if($a_data != "\n")
216  {
217  $this->cdata .= $a_data;
218  }
219  }
220 
221 
228  private function updateMember ($user_id, $action) {
229  if (!is_int($user_id) || $user_id <= 0) {
230  return;
231  }
232  $memberObject = $this->exercise->members_obj;
233 
234  if ($action == "Attach" && !$memberObject->isAssigned($user_id))
235  {
236  $memberObject->assignMember ($user_id);
237  }
238 
239  if ($action == "Detach" && $memberObject->isAssigned($user_id))
240  {
241  $memberObject->deassignMember ($user_id);
242  }
243  }
244 
252  private function updateFile ($filename, $b64encodedContent, $action)
253  {
254  if (strlen($filename) == 0) {
255  return;
256  }
257 
258  $fileObject = $this->exercise->file_obj;
259  if ($action == "Attach")
260  {
261  $content = base64_decode((string) $b64encodedContent);
262  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
263  $content = gzdecode($content);
265  $content = gzuncompress($content);
266  }
267  $fileObject->storeContentAsFile ($filename, $content);
268  }
269  if ($action == "Detach")
270  {
271  $fileObject->unlinkFile ($filename);
272  }
273  }
274 
282  public function start () {
283  $this->startParsing();
284  return $this->result > 0;
285  }
286 
292  private function updateMarking($usr_id) {
293  include_once 'Services/Tracking/classes/class.ilLPMarks.php';
294  $marks_obj = new ilLPMarks($this->exercise->getId(), $usr_id);
295  $update = false;
296  if (isset($this->mark))
297  {
298  $update = true;
299  $marks_obj->setMark(ilUtil::stripSlashes($this->mark));
300  }
301  if (isset($this->mark))
302  {
303  $update = true;
304  $marks_obj->setComment(ilUtil::stripSlashes($this->comment));
305  }
306  if ($update)
307  {
308  $marks_obj->update();
309  }
310 
311  $memberObject = $this->exercise->members_obj;
312  if (isset($this->status))
313  $memberObject ->setStatusForMember($usr_id, $this->status);
314 
315  if (isset($this->notice))
316  $memberObject->setNoticeForMember($usr_id, ilUtil::stripSlashes($this->notice));
317 
318  // reset variables
319  $this->mark = null;
320  $this->status = null;
321  $this->notice = null;
322  $this->comment = null;
323  }
324 
325 }
326 ?>