ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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(
47  ilObjExercise $exercise,
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
57  $assignments = ilExAssignment::getAssignmentDataOfExercise($exercise->getId());
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 
148  public function handlerEndTag($a_xml_parser, string $a_name): void
149  {
150  switch ($a_name) {
151  case 'Exercise':
152  $this->result = true;
153  break;
154  case 'Title':
155  $this->exercise->setTitle($this->trimAndStrip((string) $this->cdata));
156  $this->assignment->setTitle($this->trimAndStrip((string) $this->cdata));
157  break;
158  case 'Description':
159  $this->exercise->setDescription($this->trimAndStrip((string) $this->cdata));
160  break;
161  case 'Instruction':
162  $this->assignment->setInstruction($this->trimAndStrip((string) $this->cdata));
163  break;
164  case 'DueDate':
165  $this->assignment->setDeadLine($this->trimAndStrip((string) $this->cdata));
166  break;
167  case 'Member':
168  $this->updateMember($this->usr_id, $this->usr_action);
169  // update marking after update member.
170  $this->updateMarking($this->usr_id);
171  break;
172  case 'Filename':
173  $this->file_name = $this->trimAndStrip((string) $this->cdata);
174  break;
175  case 'Content':
176  $this->file_content = $this->trimAndStrip((string) $this->cdata);
177  break;
178  case 'File':
179  $this->updateFile($this->file_name, $this->file_content, $this->file_action);
180  break;
181  case 'Comment':
182  $this->comment = $this->trimAndStrip((string) $this->cdata);
183  break;
184  case 'Notice':
185  $this->notice = $this->trimAndStrip((string) $this->cdata);
186  break;
187  case 'Mark':
188  $this->mark = $this->trimAndStrip((string) $this->cdata);
189  break;
190  case 'Marking':
191  // see Member end tag
192  break;
193  }
194  $this->cdata = '';
195  }
196 
203  public function handlerCharacterData($a_xml_parser, string $a_data): void
204  {
205  if ($a_data != "\n") {
206  $this->cdata .= $a_data;
207  }
208  }
209 
210 
214  private function updateMember(int $user_id, string $action): void
215  {
216  if (!is_int($user_id) || $user_id <= 0) {
217  return;
218  }
219  $memberObject = new ilExerciseMembers($this->exercise);
220 
221  if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
222  $memberObject->assignMember($user_id);
223  }
224 
225  if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
226  $memberObject->deassignMember($user_id);
227  }
228  }
229 
237  private function updateFile(
238  string $filename,
239  string $b64encodedContent,
240  string $action
241  ): void {
242  if (strlen($filename) == 0) {
243  return;
244  }
245  $filename = $this->storage->getAbsolutePath() . "/" . $filename;
246 
247  if ($action == "Attach") {
248  $content = base64_decode($b64encodedContent);
249  if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
250  $content = gzdecode($content);
251  } elseif ($this->mode == ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
252  $content = gzuncompress($content);
253  }
254 
255  //echo $filename;
256  //$this->storage->writeToFile($content, $filename);
257  }
258  if ($action == "Detach") {
259  //$this->storage->deleteFile($filename);
260  }
261  }
262 
270  public function start(): bool
271  {
272  $this->startParsing();
273  return $this->result > 0;
274  }
275 
281  private function updateMarking(int $usr_id): void
282  {
283  $member_status = $this->assignment->getMemberStatus($usr_id);
284  if (isset($this->mark)) {
285  $member_status->setMark(ilUtil::stripSlashes($this->mark));
286  }
287  if (isset($this->comment)) {
288  $member_status->setComment(ilUtil::stripSlashes($this->comment));
289  }
290  if (isset($this->status)) {
291  $member_status->setStatus(ilUtil::stripSlashes($this->status));
292  }
293  if (isset($this->notice)) {
294  $member_status->setNotice(ilUtil::stripSlashes($this->notice));
295  }
296  $member_status->update();
297 
298  // reset variables
299  $this->mark = null;
300  $this->status = null;
301  $this->notice = null;
302  $this->comment = null;
303  }
304 
305  public function getAssignment(): ilExAssignment
306  {
307  return $this->assignment;
308  }
309 
310  protected function trimAndStripAttribs(array $attribs): array
311  {
312  $ret = [];
313  foreach ($attribs as $k => $v) {
314  $ret[$k] = $this->trimAndStrip((string) $v);
315  }
316  return $ret;
317  }
318 
319  protected function trimAndStrip(string $input): string
320  {
321  return ilUtil::stripSlashes(trim($input));
322  }
323 }
__construct(ilObjExercise $exercise, string $a_xml_data, int $obj_id=-1)
Exercise assignment.
const IL_INST_ID
Definition: constants.php:40
Class ilExerciseMembers.
startParsing()
stores xml data in array
handlerEndTag($a_xml_parser, string $a_name)
handler for end of element
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element
static getAssignmentDataOfExercise(int $a_exc_id)
Class ilObjExercise.
__construct(VocabulariesInterface $vocabularies)
handlerCharacterData($a_xml_parser, string $a_data)
handler for character data
updateMember(int $user_id, string $action)
update member object according to given action
$filename
Definition: buildRTE.php:78
updateFile(string $filename, string $b64encodedContent, string $action)
update file according to filename
ilFSStorageExercise $storage
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
updateMarking(int $usr_id)
update marking of member
start()
starts parsing an changes object by side effect.
setXMLContent(string $a_xml_content)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...