ILIAS  release_7 Revision v7.30-3-g800a261c036
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 $a_attribs = $this->trimAndStripAttribs($a_attribs);
120 switch ($a_name) {
121 case 'Exercise':
122 if (isset($a_attribs["obj_id"])) {
123 $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
124 if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id) {
125 throw new ilExerciseException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilExerciseException::$ID_MISMATCH);
126 }
127 }
128 break;
129 case 'Member':
130 $this->usr_action = $a_attribs["action"];
131 $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
132 break;
133
134 case 'File':
135 $this->file_action = $a_attribs["action"];
136 break;
137 case 'Content':
139 if ($a_attribs["mode"] == "GZIP") {
140 if (!function_exists("gzdecode")) {
141 throw new ilExerciseException("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
142 }
143
145 } elseif ($a_attribs["mode"] == "ZLIB") {
146 if (!function_exists("gzuncompress")) {
147 throw new ilExerciseException("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
148 }
149
151 }
152 break;
153 case 'Marking':
154 $this->status = $a_attribs["status"];
155 if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED) {
156 $this->status = "notgraded";
157 } elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED) {
158 $this->status = "passed";
159 } else {
160 $this->status = "failed";
161 }
162 break;
163
164 }
165 }
166
167
168
175 public function handlerEndTag($a_xml_parser, $a_name)
176 {
177 switch ($a_name) {
178 case 'Exercise':
179 $this->result = true;
180 break;
181 case 'Title':
182 $this->exercise->setTitle($this->trimAndStrip((string) $this->cdata));
183 $this->assignment->setTitle($this->trimAndStrip((string) $this->cdata));
184 break;
185 case 'Description':
186 $this->exercise->setDescription($this->trimAndStrip((string) $this->cdata));
187 break;
188 case 'Instruction':
189 $this->assignment->setInstruction($this->trimAndStrip((string) $this->cdata));
190 break;
191 case 'DueDate':
192 $this->assignment->setDeadLine($this->trimAndStrip((string) $this->cdata));
193 break;
194 case 'Member':
195 $this->updateMember($this->usr_id, $this->usr_action);
196 // update marking after update member.
197 $this->updateMarking($this->usr_id);
198 break;
199 case 'Filename':
200 $this->file_name = $this->trimAndStrip((string) $this->cdata);
201 break;
202 case 'Content':
203 $this->file_content = $this->trimAndStrip((string) $this->cdata);
204 break;
205 case 'File':
206 $this->updateFile($this->file_name, $this->file_content, $this->file_action);
207 break;
208 case 'Comment':
209 $this->comment = $this->trimAndStrip((string) $this->cdata);
210 break;
211 case 'Notice':
212 $this->notice = $this->trimAndStrip((string) $this->cdata);
213 break;
214 case 'Mark':
215 $this->mark = $this->trimAndStrip((string) $this->cdata);
216 break;
217 case 'Marking':
218 // see Member end tag
219 break;
220 }
221
222 $this->cdata = '';
223
224 return;
225 }
226
233 public function handlerCharacterData($a_xml_parser, $a_data)
234 {
235 if ($a_data != "\n") {
236 $this->cdata .= $a_data;
237 }
238 }
239
240
247 private function updateMember($user_id, $action)
248 {
249 if (!is_int($user_id) || $user_id <= 0) {
250 return;
251 }
252 $memberObject = new ilExerciseMembers($this->exercise);
253
254 if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
255 $memberObject->assignMember($user_id);
256 }
257
258 if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
259 $memberObject->deassignMember($user_id);
260 }
261 }
262
270 private function updateFile($filename, $b64encodedContent, $action)
271 {
272 if (strlen($filename) == 0) {
273 return;
274 }
275 $filename = $this->storage->getAbsolutePath() . "/" . $filename;
276
277 if ($action == "Attach") {
278 $content = base64_decode((string) $b64encodedContent);
280 $content = gzdecode($content);
281 } elseif ($this->mode == ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
282 $content = gzuncompress($content);
283 }
284
285 //echo $filename;
286 $this->storage->writeToFile($content, $filename);
287 }
288 if ($action == "Detach") {
289 $this->storage->deleteFile($filename);
290 }
291 }
292
300 public function start()
301 {
302 $this->startParsing();
303 return $this->result > 0;
304 }
305
311 private function updateMarking($usr_id)
312 {
313 $member_status = $this->assignment->getMemberStatus($usr_id);
314 if (isset($this->mark)) {
315 $member_status->setMark(ilUtil::stripSlashes($this->mark));
316 }
317 if (isset($this->comment)) {
318 $member_status->setComment(ilUtil::stripSlashes($this->comment));
319 }
320 if (isset($this->status)) {
321 $member_status->setStatus(ilUtil::stripSlashes($this->status));
322 }
323 if (isset($this->notice)) {
324 $member_status->setNotice(ilUtil::stripSlashes($this->notice));
325 }
326 $member_status->update();
327
328 // reset variables
329 $this->mark = null;
330 $this->status = null;
331 $this->notice = null;
332 $this->comment = null;
333 }
334
335 public function getAssignment()
336 {
337 return $this->assignment;
338 }
339
340 protected function trimAndStripAttribs(array $attribs) : array
341 {
342 $ret = [];
343 foreach ($attribs as $k => $v) {
344 $ret[$k] = $this->trimAndStrip((string) $v);
345 }
346 return $ret;
347 }
348
349 protected function trimAndStrip(string $input) : string
350 {
351 return ilUtil::stripSlashes(trim($input));
352 }
353}
$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
const IL_INST_ID
Definition: constants.php:38
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$ret
Definition: parser.php:6