ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
4include_once 'Services/Tracking/classes/class.ilChangeEvent.php';
5include_once 'Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
6include_once './Services/Xml/classes/class.ilSaxParser.php';
7
19{
20 public static $CONTENT_NOT_COMPRESSED = 0;
21 public static $CONTENT_GZ_COMPRESSED = 1;
22 public static $CONTENT_ZLIB_COMPRESSED = 2;
23
29 public $exercise;
30
37 public $obj_id;
38
39
45 public $result;
46
52 public $mode;
53
59
64 public $storage;
65
74 public function __construct($exercise, $a_xml_data, $obj_id = -1)
75 {
76 // @todo: needs to be revised for multiple assignments per exercise
77
78 parent::__construct();
79
80 $this->exercise = $exercise;
81 // get all assignments and choose first one if exists, otherwise create
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 public 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 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
124 {
125 switch ($a_name) {
126 case 'Exercise':
127 if (isset($a_attribs["obj_id"])) {
128 $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
129 if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id) {
130 throw new ilExerciseException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilExerciseException::$ID_MISMATCH);
131 }
132 }
133 break;
134 case 'Member':
135 $this->usr_action = $a_attribs["action"];
136 $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
137 break;
138
139 case 'File':
140 $this->file_action = $a_attribs["action"];
141 break;
142 case 'Content':
144 if ($a_attribs["mode"] == "GZIP") {
145 if (!function_exists("gzdecode")) {
146 throw new ilExerciseException("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
147 }
148
150 } elseif ($a_attribs["mode"] == "ZLIB") {
151 if (!function_exists("gzuncompress")) {
152 throw new ilExerciseException("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
153 }
154
156 }
157 break;
158 case 'Marking':
159 $this->status = $a_attribs["status"];
160 if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED) {
161 $this->status = "notgraded";
162 } elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED) {
163 $this->status = "passed";
164 } else {
165 $this->status = "failed";
166 }
167 break;
168
169 }
170 }
171
172
173
180 public function handlerEndTag($a_xml_parser, $a_name)
181 {
182 switch ($a_name) {
183 case 'Exercise':
184 $this->result = true;
185 break;
186 case 'Title':
187 $this->exercise->setTitle(trim($this->cdata));
188 $this->assignment->setTitle(trim($this->cdata));
189 break;
190 case 'Description':
191 $this->exercise->setDescription(trim($this->cdata));
192 break;
193 case 'Instruction':
194 $this->assignment->setInstruction(trim($this->cdata));
195 break;
196 case 'DueDate':
197 $this->assignment->setDeadLine(trim($this->cdata));
198 break;
199 case 'Member':
200 $this->updateMember($this->usr_id, $this->usr_action);
201 // update marking after update member.
202 $this->updateMarking($this->usr_id);
203 break;
204 case 'Filename':
205 $this->file_name = trim($this->cdata);
206 break;
207 case 'Content':
208 $this->file_content = trim($this->cdata);
209 break;
210 case 'File':
211 $this->updateFile($this->file_name, $this->file_content, $this->file_action);
212 break;
213 case 'Comment':
214 $this->comment = trim($this->cdata);
215 break;
216 case 'Notice':
217 $this->notice = trim($this->cdata);
218 break;
219 case 'Mark':
220 $this->mark = trim($this->cdata);
221 break;
222 case 'Marking':
223 // see Member end tag
224 break;
225 }
226
227 $this->cdata = '';
228
229 return;
230 }
231
238 public function handlerCharacterData($a_xml_parser, $a_data)
239 {
240 if ($a_data != "\n") {
241 $this->cdata .= $a_data;
242 }
243 }
244
245
252 private function updateMember($user_id, $action)
253 {
254 if (!is_int($user_id) || $user_id <= 0) {
255 return;
256 }
257 $memberObject = new ilExerciseMembers($this->exercise);
258
259 if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
260 $memberObject->assignMember($user_id);
261 }
262
263 if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
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 $filename= $this->storage->getAbsolutePath() . "/" . $filename;
281
282 if ($action == "Attach") {
283 $content = base64_decode((string) $b64encodedContent);
285 $content = gzdecode($content);
286 } elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
287 $content = gzuncompress($content);
288 }
289
290 //echo $filename;
291 $this->storage->writeToFile($content, $filename);
292 }
293 if ($action == "Detach") {
294 $this->storage->deleteFile($filename);
295 }
296 }
297
305 public function start()
306 {
307 $this->startParsing();
308 return $this->result > 0;
309 }
310
316 private function updateMarking($usr_id)
317 {
318 $member_status = $this->assignment->getMemberStatus($usr_id);
319 if (isset($this->mark)) {
320 $member_status->setMark(ilUtil::stripSlashes($this->mark));
321 }
322 if (isset($this->comment)) {
323 $member_status->setComment(ilUtil::stripSlashes($this->comment));
324 }
325 if (isset($this->status)) {
326 $member_status->setStatus(ilUtil::stripSlashes($this->status));
327 }
328 if (isset($this->notice)) {
329 $member_status->setNotice(ilUtil::stripSlashes($this->notice));
330 }
331 $member_status->update();
332
333 // reset variables
334 $this->mark = null;
335 $this->status = null;
336 $this->notice = null;
337 $this->comment = null;
338 }
339
340 public function getAssignment()
341 {
342 return $this->assignment;
343 }
344}
An exception for terminatinating execution or to throw for unit testing.
Exercise assignment.
static getAssignmentDataOfExercise($a_exc_id)
Get assignments data of an exercise in an array.
Exercise exceptions class.
Class ilExerciseMembers.
Exercise XML Parser which completes/updates a given exercise by an xml string.
start()
starts parsing an changes object by side effect.
updateMember($user_id, $action)
update member object according to given action
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
__construct($exercise, $a_xml_data, $obj_id=-1)
Constructor.
updateMarking($usr_id)
update marking of member
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
updateFile($filename, $b64encodedContent, $action)
update file according to filename
setHandlers($a_xml_parser)
set event handlers
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
setXMLContent($a_xml_content)
startParsing()
stores xml data in array
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static __extractId($ilias_id, $inst_id)
extract ref id from role title, e.g.
comment()
Definition: comment.php:2
$action