ILIAS  release_8 Revision v8.24
class.ilExerciseXMLParser.php
Go to the documentation of this file.
1<?php
2
24{
25 public static int $CONTENT_NOT_COMPRESSED = 0;
26 public static int $CONTENT_GZ_COMPRESSED = 1;
27 public static int $CONTENT_ZLIB_COMPRESSED = 2;
28 protected string $cdata;
29 protected string $mark;
30 protected string $notice;
31 protected string $comment;
32 protected string $file_content;
33 protected string $file_name;
34 protected string $status;
35 protected string $file_action;
36 protected int $usr_id;
37 protected string $usr_action;
38
40 public int $obj_id;
41 public bool $result;
42 public int $mode;
45
46 public function __construct(
48 string $a_xml_data,
49 int $obj_id = -1
50 ) {
51 // @todo: needs to be revised for multiple assignments per exercise
52
54
55 $this->exercise = $exercise;
56 // get all assignments and choose first one if exists, otherwise create
58 if (count($assignments) > 0) {
59 $this->assignment = new ilExAssignment($assignments [0]["id"]);
60 } else {
61 $this->assignment = new ilExAssignment();
62 $this->assignment->setExerciseId($exercise->getId());
63 $this->assignment->save();
64 }
65
66 $this->storage = new ilFSStorageExercise($this->exercise->getId(), $this->assignment->getId());
67 $this->storage->create();
68 $this->storage->init();
69
70 $this->setXMLContent($a_xml_data);
71 $this->obj_id = $obj_id;
72 $this->result = false;
73 }
74
75
76 public function setHandlers($a_xml_parser): void
77 {
78 xml_set_object($a_xml_parser, $this);
79 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
80 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
81 }
82
91 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
92 {
93 $a_attribs = $this->trimAndStripAttribs($a_attribs);
94 switch ($a_name) {
95 case 'Exercise':
96 if (isset($a_attribs["obj_id"])) {
97 $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
98 if ($this->obj_id != -1 && (int) $read_obj_id != -1 && $this->obj_id !== (int) $read_obj_id) {
99 throw new ilExerciseException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilExerciseException::$ID_MISMATCH);
100 }
101 }
102 break;
103 case 'Member':
104 $this->usr_action = $a_attribs["action"];
105 $this->usr_id = (int) ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
106 break;
107
108 case 'File':
109 $this->file_action = $a_attribs["action"];
110 break;
111 case 'Content':
113 if ($a_attribs["mode"] == "GZIP") {
114 if (!function_exists("gzdecode")) {
115 throw new ilExerciseException("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
116 }
117
119 } elseif ($a_attribs["mode"] == "ZLIB") {
120 if (!function_exists("gzuncompress")) {
121 throw new ilExerciseException("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
122 }
123
125 }
126 break;
127 case 'Marking':
128 $this->status = $a_attribs["status"];
129 if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED) {
130 $this->status = "notgraded";
131 } elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED) {
132 $this->status = "passed";
133 } else {
134 $this->status = "failed";
135 }
136 break;
137
138 }
139 }
140
141
142
149 public function handlerEndTag($a_xml_parser, string $a_name): void
150 {
151 switch ($a_name) {
152 case 'Exercise':
153 $this->result = true;
154 break;
155 case 'Title':
156 $this->exercise->setTitle($this->trimAndStrip((string) $this->cdata));
157 $this->assignment->setTitle($this->trimAndStrip((string) $this->cdata));
158 break;
159 case 'Description':
160 $this->exercise->setDescription($this->trimAndStrip((string) $this->cdata));
161 break;
162 case 'Instruction':
163 $this->assignment->setInstruction($this->trimAndStrip((string) $this->cdata));
164 break;
165 case 'DueDate':
166 $this->assignment->setDeadLine($this->trimAndStrip((string) $this->cdata));
167 break;
168 case 'Member':
169 $this->updateMember($this->usr_id, $this->usr_action);
170 // update marking after update member.
171 $this->updateMarking($this->usr_id);
172 break;
173 case 'Filename':
174 $this->file_name = $this->trimAndStrip((string) $this->cdata);
175 break;
176 case 'Content':
177 $this->file_content = $this->trimAndStrip((string) $this->cdata);
178 break;
179 case 'File':
180 $this->updateFile($this->file_name, $this->file_content, $this->file_action);
181 break;
182 case 'Comment':
183 $this->comment = $this->trimAndStrip((string) $this->cdata);
184 break;
185 case 'Notice':
186 $this->notice = $this->trimAndStrip((string) $this->cdata);
187 break;
188 case 'Mark':
189 $this->mark = $this->trimAndStrip((string) $this->cdata);
190 break;
191 case 'Marking':
192 // see Member end tag
193 break;
194 }
195 $this->cdata = '';
196 }
197
204 public function handlerCharacterData($a_xml_parser, string $a_data): void
205 {
206 if ($a_data != "\n") {
207 $this->cdata .= $a_data;
208 }
209 }
210
211
215 private function updateMember(int $user_id, string $action): void
216 {
217 if (!is_int($user_id) || $user_id <= 0) {
218 return;
219 }
220 $memberObject = new ilExerciseMembers($this->exercise);
221
222 if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
223 $memberObject->assignMember($user_id);
224 }
225
226 if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
227 $memberObject->deassignMember($user_id);
228 }
229 }
230
238 private function updateFile(
239 string $filename,
240 string $b64encodedContent,
241 string $action
242 ): void {
243 if (strlen($filename) == 0) {
244 return;
245 }
246 $filename = $this->storage->getAbsolutePath() . "/" . $filename;
247
248 if ($action == "Attach") {
249 $content = base64_decode($b64encodedContent);
251 $content = gzdecode($content);
252 } elseif ($this->mode == ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
253 $content = gzuncompress($content);
254 }
255
256 //echo $filename;
257 //$this->storage->writeToFile($content, $filename);
258 }
259 if ($action == "Detach") {
260 //$this->storage->deleteFile($filename);
261 }
262 }
263
271 public function start(): bool
272 {
273 $this->startParsing();
274 return $this->result > 0;
275 }
276
282 private function updateMarking(int $usr_id): void
283 {
284 $member_status = $this->assignment->getMemberStatus($usr_id);
285 if (isset($this->mark)) {
286 $member_status->setMark(ilUtil::stripSlashes($this->mark));
287 }
288 if (isset($this->comment)) {
289 $member_status->setComment(ilUtil::stripSlashes($this->comment));
290 }
291 if (isset($this->status)) {
292 $member_status->setStatus(ilUtil::stripSlashes($this->status));
293 }
294 if (isset($this->notice)) {
295 $member_status->setNotice(ilUtil::stripSlashes($this->notice));
296 }
297 $member_status->update();
298
299 // reset variables
300 $this->mark = null;
301 $this->status = null;
302 $this->notice = null;
303 $this->comment = null;
304 }
305
306 public function getAssignment(): ilExAssignment
307 {
308 return $this->assignment;
309 }
310
311 protected function trimAndStripAttribs(array $attribs): array
312 {
313 $ret = [];
314 foreach ($attribs as $k => $v) {
315 $ret[$k] = $this->trimAndStrip((string) $v);
316 }
317 return $ret;
318 }
319
320 protected function trimAndStrip(string $input): string
321 {
322 return ilUtil::stripSlashes(trim($input));
323 }
324}
$filename
Definition: buildRTE.php:78
Exercise assignment.
static getAssignmentDataOfExercise(int $a_exc_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
updateFile(string $filename, string $b64encodedContent, string $action)
update file according to filename
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element
handlerEndTag($a_xml_parser, string $a_name)
handler for end of element
__construct(ilObjExercise $exercise, string $a_xml_data, int $obj_id=-1)
start()
starts parsing an changes object by side effect.
handlerCharacterData($a_xml_parser, string $a_data)
handler for character data
updateMarking(int $usr_id)
update marking of member
updateMember(int $user_id, string $action)
update member object according to given action
ilFSStorageExercise $storage
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjExercise.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setXMLContent(string $a_xml_content)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
const IL_INST_ID
Definition: constants.php:40
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc