ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
14{
15 public static $CONTENT_NOT_COMPRESSED = 0;
16 public static $CONTENT_GZ_COMPRESSED = 1;
17 public static $CONTENT_ZLIB_COMPRESSED = 2;
18
24 public $exercise;
25
32 public $obj_id;
33
34
40 public $result;
41
47 public $mode;
48
54
59 public $storage;
60
69 public function __construct($exercise, $a_xml_data, $obj_id = -1)
70 {
71 // @todo: needs to be revised for multiple assignments per exercise
72
74
75 $this->exercise = $exercise;
76 // get all assignments and choose first one if exists, otherwise create
78 if (count($assignments) > 0) {
79 $this->assignment = new ilExAssignment($assignments [0]["id"]);
80 } else {
81 $this->assignment = new ilExAssignment();
82 $this->assignment->setExerciseId($exercise->getId());
83 $this->assignment->save();
84 }
85
86 $this->storage = new ilFSStorageExercise($this->exercise->getId(), $this->assignment->getId());
87 $this->storage->create();
88 $this->storage->init();
89
90 $this->setXMLContent($a_xml_data);
91 $this->obj_id = $obj_id;
92 $this->result = false;
93 }
94
95
102 public 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 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
118 {
119 switch ($a_name) {
120 case 'Exercise':
121 if (isset($a_attribs["obj_id"])) {
122 $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
123 if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id) {
124 throw new ilExerciseException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilExerciseException::$ID_MISMATCH);
125 }
126 }
127 break;
128 case 'Member':
129 $this->usr_action = $a_attribs["action"];
130 $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
131 break;
132
133 case 'File':
134 $this->file_action = $a_attribs["action"];
135 break;
136 case 'Content':
138 if ($a_attribs["mode"] == "GZIP") {
139 if (!function_exists("gzdecode")) {
140 throw new ilExerciseException("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
141 }
142
144 } elseif ($a_attribs["mode"] == "ZLIB") {
145 if (!function_exists("gzuncompress")) {
146 throw new ilExerciseException("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
147 }
148
150 }
151 break;
152 case 'Marking':
153 $this->status = $a_attribs["status"];
154 if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED) {
155 $this->status = "notgraded";
156 } elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED) {
157 $this->status = "passed";
158 } else {
159 $this->status = "failed";
160 }
161 break;
162
163 }
164 }
165
166
167
174 public function handlerEndTag($a_xml_parser, $a_name)
175 {
176 switch ($a_name) {
177 case 'Exercise':
178 $this->result = true;
179 break;
180 case 'Title':
181 $this->exercise->setTitle(trim($this->cdata));
182 $this->assignment->setTitle(trim($this->cdata));
183 break;
184 case 'Description':
185 $this->exercise->setDescription(trim($this->cdata));
186 break;
187 case 'Instruction':
188 $this->assignment->setInstruction(trim($this->cdata));
189 break;
190 case 'DueDate':
191 $this->assignment->setDeadLine(trim($this->cdata));
192 break;
193 case 'Member':
194 $this->updateMember($this->usr_id, $this->usr_action);
195 // update marking after update member.
196 $this->updateMarking($this->usr_id);
197 break;
198 case 'Filename':
199 $this->file_name = trim($this->cdata);
200 break;
201 case 'Content':
202 $this->file_content = trim($this->cdata);
203 break;
204 case 'File':
205 $this->updateFile($this->file_name, $this->file_content, $this->file_action);
206 break;
207 case 'Comment':
208 $this->comment = trim($this->cdata);
209 break;
210 case 'Notice':
211 $this->notice = trim($this->cdata);
212 break;
213 case 'Mark':
214 $this->mark = trim($this->cdata);
215 break;
216 case 'Marking':
217 // see Member end tag
218 break;
219 }
220
221 $this->cdata = '';
222
223 return;
224 }
225
232 public function handlerCharacterData($a_xml_parser, $a_data)
233 {
234 if ($a_data != "\n") {
235 $this->cdata .= $a_data;
236 }
237 }
238
239
246 private function updateMember($user_id, $action)
247 {
248 if (!is_int($user_id) || $user_id <= 0) {
249 return;
250 }
251 $memberObject = new ilExerciseMembers($this->exercise);
252
253 if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
254 $memberObject->assignMember($user_id);
255 }
256
257 if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
258 $memberObject->deassignMember($user_id);
259 }
260 }
261
269 private function updateFile($filename, $b64encodedContent, $action)
270 {
271 if (strlen($filename) == 0) {
272 return;
273 }
274 $filename = $this->storage->getAbsolutePath() . "/" . $filename;
275
276 if ($action == "Attach") {
277 $content = base64_decode((string) $b64encodedContent);
279 $content = gzdecode($content);
280 } elseif ($this->mode == ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
281 $content = gzuncompress($content);
282 }
283
284 //echo $filename;
285 $this->storage->writeToFile($content, $filename);
286 }
287 if ($action == "Detach") {
288 $this->storage->deleteFile($filename);
289 }
290 }
291
299 public function start()
300 {
301 $this->startParsing();
302 return $this->result > 0;
303 }
304
310 private function updateMarking($usr_id)
311 {
312 $member_status = $this->assignment->getMemberStatus($usr_id);
313 if (isset($this->mark)) {
314 $member_status->setMark(ilUtil::stripSlashes($this->mark));
315 }
316 if (isset($this->comment)) {
317 $member_status->setComment(ilUtil::stripSlashes($this->comment));
318 }
319 if (isset($this->status)) {
320 $member_status->setStatus(ilUtil::stripSlashes($this->status));
321 }
322 if (isset($this->notice)) {
323 $member_status->setNotice(ilUtil::stripSlashes($this->notice));
324 }
325 $member_status->update();
326
327 // reset variables
328 $this->mark = null;
329 $this->status = null;
330 $this->notice = null;
331 $this->comment = null;
332 }
333
334 public function getAssignment()
335 {
336 return $this->assignment;
337 }
338}
$filename
Definition: buildRTE.php:89
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
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc