ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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;
44 
45  public function __construct(
46  ilObjExercise $exercise,
47  string $a_xml_data,
48  int $obj_id = -1
49  ) {
50  // @todo: needs to be revised for multiple assignments per exercise
51 
53 
54  $this->exercise = $exercise;
55  // get all assignments and choose first one if exists, otherwise create
56  $assignments = ilExAssignment::getAssignmentDataOfExercise($exercise->getId());
57  if (count($assignments) > 0) {
58  $this->assignment = new ilExAssignment($assignments [0]["id"]);
59  } else {
60  $this->assignment = new ilExAssignment();
61  $this->assignment->setExerciseId($exercise->getId());
62  $this->assignment->save();
63  }
64 
65  $this->setXMLContent($a_xml_data);
66  $this->obj_id = $obj_id;
67  $this->result = false;
68  }
69 
70 
71  public function setHandlers($a_xml_parser): void
72  {
73  xml_set_object($a_xml_parser, $this);
74  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
75  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
76  }
77 
86  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
87  {
88  $a_attribs = $this->trimAndStripAttribs($a_attribs);
89  switch ($a_name) {
90  case 'Exercise':
91  if (isset($a_attribs["obj_id"])) {
92  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
93  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && $this->obj_id !== (int) $read_obj_id) {
94  throw new ilExerciseException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilExerciseException::$ID_MISMATCH);
95  }
96  }
97  break;
98  case 'Member':
99  $this->usr_action = $a_attribs["action"];
100  $this->usr_id = (int) ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
101  break;
102 
103  case 'File':
104  $this->file_action = $a_attribs["action"];
105  break;
106  case 'Content':
108  if ($a_attribs["mode"] == "GZIP") {
109  if (!function_exists("gzdecode")) {
110  throw new ilExerciseException("Deflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
111  }
112 
114  } elseif ($a_attribs["mode"] == "ZLIB") {
115  if (!function_exists("gzuncompress")) {
116  throw new ilExerciseException("Deflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
117  }
118 
120  }
121  break;
122  case 'Marking':
123  $this->status = $a_attribs["status"];
124  if ($this->status == ilExerciseXMLWriter::$STATUS_NOT_GRADED) {
125  $this->status = "notgraded";
126  } elseif ($this->status == ilExerciseXMLWriter::$STATUS_PASSED) {
127  $this->status = "passed";
128  } else {
129  $this->status = "failed";
130  }
131  break;
132  }
133  }
134 
135 
136 
143  public function handlerEndTag($a_xml_parser, string $a_name): void
144  {
145  switch ($a_name) {
146  case 'Exercise':
147  $this->result = true;
148  break;
149  case 'Title':
150  $this->exercise->setTitle($this->trimAndStrip((string) $this->cdata));
151  $this->assignment->setTitle($this->trimAndStrip((string) $this->cdata));
152  break;
153  case 'Description':
154  $this->exercise->setDescription($this->trimAndStrip((string) $this->cdata));
155  break;
156  case 'Instruction':
157  $this->assignment->setInstruction($this->trimAndStrip((string) $this->cdata));
158  break;
159  case 'DueDate':
160  $this->assignment->setDeadLine($this->trimAndStrip((string) $this->cdata));
161  break;
162  case 'Member':
163  $this->updateMember($this->usr_id, $this->usr_action);
164  // update marking after update member.
165  $this->updateMarking($this->usr_id);
166  break;
167  case 'Filename':
168  $this->file_name = $this->trimAndStrip((string) $this->cdata);
169  break;
170  case 'Content':
171  $this->file_content = $this->trimAndStrip((string) $this->cdata);
172  break;
173  case 'Comment':
174  $this->comment = $this->trimAndStrip((string) $this->cdata);
175  break;
176  case 'Notice':
177  $this->notice = $this->trimAndStrip((string) $this->cdata);
178  break;
179  case 'Mark':
180  $this->mark = $this->trimAndStrip((string) $this->cdata);
181  break;
182  case 'Marking':
183  // see Member end tag
184  break;
185  }
186  $this->cdata = '';
187  }
188 
195  public function handlerCharacterData($a_xml_parser, string $a_data): void
196  {
197  if ($a_data != "\n") {
198  $this->cdata .= $a_data;
199  }
200  }
201 
202 
206  private function updateMember(int $user_id, string $action): void
207  {
208  if (!is_int($user_id) || $user_id <= 0) {
209  return;
210  }
211  $memberObject = new ilExerciseMembers($this->exercise);
212 
213  if ($action == "Attach" && !$memberObject->isAssigned($user_id)) {
214  $memberObject->assignMember($user_id);
215  }
216 
217  if ($action == "Detach" && $memberObject->isAssigned($user_id)) {
218  $memberObject->deassignMember($user_id);
219  }
220  }
221 
229  public function start(): bool
230  {
231  $this->startParsing();
232  return $this->result > 0;
233  }
234 
240  private function updateMarking(int $usr_id): void
241  {
242  $member_status = $this->assignment->getMemberStatus($usr_id);
243  if (isset($this->mark)) {
244  $member_status->setMark(ilUtil::stripSlashes($this->mark));
245  }
246  if (isset($this->comment)) {
247  $member_status->setComment(ilUtil::stripSlashes($this->comment));
248  }
249  if (isset($this->status)) {
250  $member_status->setStatus(ilUtil::stripSlashes($this->status));
251  }
252  if (isset($this->notice)) {
253  $member_status->setNotice(ilUtil::stripSlashes($this->notice));
254  }
255  $member_status->update();
256 
257  // reset variables
258  $this->mark = null;
259  $this->status = null;
260  $this->notice = null;
261  $this->comment = null;
262  }
263 
264  public function getAssignment(): ilExAssignment
265  {
266  return $this->assignment;
267  }
268 
269  protected function trimAndStripAttribs(array $attribs): array
270  {
271  $ret = [];
272  foreach ($attribs as $k => $v) {
273  $ret[$k] = $this->trimAndStrip((string) $v);
274  }
275  return $ret;
276  }
277 
278  protected function trimAndStrip(string $input): string
279  {
280  return ilUtil::stripSlashes(trim($input));
281  }
282 }
__construct(ilObjExercise $exercise, string $a_xml_data, int $obj_id=-1)
Exercise assignment.
const IL_INST_ID
Definition: constants.php:40
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.
handlerCharacterData($a_xml_parser, string $a_data)
handler for character data
updateMember(int $user_id, string $action)
update member object according to given action
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
comment()
description: > Example for rendring a comment glyph.
Definition: comment.php:25
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...