ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExerciseXMLParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
36 include_once 'classes/class.ilSaxParser.php';
37 include_once 'Modules/Exercise/classes/class.ilExerciseException.php';
38 include_once 'Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
39 
40 
42 {
43 
47 
53  var $exercise;
54 
61  var $obj_id;
62 
63 
69  var $result;
70 
76  var $mode;
77 
86  function ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id = -1)
87  {
89  $this->exercise = $exercise;
90  $this->setXMLContent($a_xml_data);
91  $this->obj_id = $obj_id;
92  $this->result = false;
93  }
94 
95 
102  function setHandlers($a_xml_parser)
103  {
104  xml_set_object($a_xml_parser,$this);
105  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
106  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
107  }
108 
117  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
118  {
119  global $ilErr;
120 
121  switch($a_name)
122  {
123  case 'Exercise':
124  if (isset($a_attribs["obj_id"]))
125  {
126  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
127  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
128  {
129  throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
130  }
131  }
132  break;
133  case 'Member':
134  $this->usr_action = $a_attribs["action"];
135  $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
136  break;
137 
138  case 'File':
139  $this->file_action = $a_attribs["action"];
140  break;
141  case 'Content':
143  if ($a_attribs["mode"] == "GZIP")
144  {
145  if (!function_exists("gzdecode"))
146  throw new ilExerciseException ("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
147 
149  } elseif ($a_attribs["mode"] == "ZLIB")
150  {
151  if (!function_exists("gzuncompress"))
152  throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
153 
155  }
156  break;
157  case 'Marking':
158  $this->status = $a_attribs["status"];
159  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED)
160  $this->status = "notgraded";
161  elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED)
162  $this->status = "passed";
163  else
164  $this->status = "failed";
165  break;
166 
167  }
168  }
169 
170 
171 
178  function handlerEndTag($a_xml_parser,$a_name)
179  {
180  switch($a_name)
181  {
182  case 'Exercise':
183  $this->result = true;
184  break;
185  case 'Title':
186  $this->exercise->setTitle(trim($this->cdata));
187  break;
188  case 'Description':
189  $this->exercise->setDescription(trim($this->cdata));
190  break;
191  case 'Instruction':
192  $this->exercise->setInstruction(trim($this->cdata));
193  break;
194  case 'DueDate':
195  $this->exercise->setTimestamp(trim($this->cdata));
196  break;
197  case 'Member':
198  $this->updateMember($this->usr_id, $this->usr_action);
199  // update marking after update member.
200  $this->updateMarking($this->usr_id);
201  break;
202  case 'Filename':
203  $this->file_name = trim($this->cdata);
204  break;
205  case 'Content':
206  $this->file_content = trim($this->cdata);
207  break;
208  case 'File':
209  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
210  break;
211  case 'Comment':
212  $this->comment = trim($this->cdata);
213  break;
214  case 'Notice':
215  $this->notice = trim($this->cdata);
216  break;
217  case 'Mark':
218  $this->mark = trim($this->cdata);
219  break;
220  case 'Marking':
221  // see Member end tag
222  break;
223  }
224 
225  $this->cdata = '';
226 
227  return;
228  }
229 
236  function handlerCharacterData($a_xml_parser,$a_data)
237  {
238  if($a_data != "\n")
239  {
240  $this->cdata .= $a_data;
241  }
242  }
243 
244 
251  private function updateMember ($user_id, $action) {
252  if (!is_int($user_id) || $user_id <= 0) {
253  return;
254  }
255  $memberObject = $this->exercise->members_obj;
256 
257  if ($action == "Attach" && !$memberObject->isAssigned($user_id))
258  {
259  $memberObject->assignMember ($user_id);
260  }
261 
262  if ($action == "Detach" && $memberObject->isAssigned($user_id))
263  {
264  $memberObject->deassignMember ($user_id);
265  }
266  }
267 
275  private function updateFile ($filename, $b64encodedContent, $action)
276  {
277  if (strlen($filename) == 0) {
278  return;
279  }
280 
281  $fileObject = $this->exercise->file_obj;
282  if ($action == "Attach")
283  {
284  $content = base64_decode((string) $b64encodedContent);
285  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
286  $content = gzdecode($content);
287  }elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
288  $content = gzuncompress($content);
289  }
290  $fileObject->storeContentAsFile ($filename, $content);
291  }
292  if ($action == "Detach")
293  {
294  $fileObject->unlinkFile ($filename);
295  }
296  }
297 
305  public function start () {
306  $this->startParsing();
307  return $this->result > 0;
308  }
309 
315  private function updateMarking($usr_id) {
316  include_once 'Services/Tracking/classes/class.ilLPMarks.php';
317  $marks_obj = new ilLPMarks($this->exercise->getId(), $usr_id);
318  $update = false;
319  if (isset($this->mark))
320  {
321  $update = true;
322  $marks_obj->setMark(ilUtil::stripSlashes($this->mark));
323  }
324  if (isset($this->mark))
325  {
326  $update = true;
327  $marks_obj->setComment(ilUtil::stripSlashes($this->comment));
328  }
329  if ($update)
330  {
331  $marks_obj->update();
332  }
333 
334  $memberObject = $this->exercise->members_obj;
335  if (isset($this->status))
336  $memberObject ->setStatusForMember($usr_id, $this->status);
337 
338  if (isset($this->notice))
339  $memberObject->setNoticeForMember($usr_id, ilUtil::stripSlashes($this->notice));
340 
341  // reset variables
342  $this->mark = null;
343  $this->status = null;
344  $this->notice = null;
345  $this->comment = null;
346  }
347 
348 }
349 ?>