ILIAS  release_8 Revision v8.24
class.ilExerciseXMLWriter.php
Go to the documentation of this file.
1<?php
28{
29 public static int $CONTENT_ATTACH_NO = 0;
30 public static int $CONTENT_ATTACH_ENCODED = 1;
31 public static int $CONTENT_ATTACH_ZLIB_ENCODED = 2;
32 public static int $CONTENT_ATTACH_GZIP_ENCODED = 3;
33
34 public static string $STATUS_NOT_GRADED = "NOT_GRADED";
35 public static string $STATUS_PASSED = "PASSED";
36 public static string $STATUS_FAILED = "FAILED";
37
38 public bool $attachFileContents; // if true, file contents will be attached as base64
39 public bool $attachMembers; // if true, members will be attach to xml
41
42 public function __construct()
43 {
44 // @todo: needs to be revised for multiple assignments per exercise
45 //die ("Needs revision for ILIAS 4.1");
47 $this->attachFileContents = ilExerciseXMLWriter::$CONTENT_ATTACH_NO;
48 }
49
50 public function setExercise(ilObjExercise $exercise): void
51 {
52 $this->exercise = $exercise;
53 }
54
59 public function setAttachFileContents(int $attachFileContents): void
60 {
61 if ($attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED && !function_exists("gzencode")) {
62 throw new ilExerciseException("Inflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
63 }
64 if ($attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED && !function_exists("gzcompress")) {
65 throw new ilExerciseException("Inflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
66 }
67
68 $this->attachFileContents = $attachFileContents;
69 }
70
71 public function start(): bool
72 {
73 $this->__buildHeader();
74
75 $attribs = array("obj_id" => "il_" . IL_INST_ID . "_exc_" . $this->exercise->getId() );
76
77 if ($this->exercise->getOwner() !== 0) {
78 $attribs ["owner"] = "il_" . IL_INST_ID . "_usr_" . $this->exercise->getOwner();
79 }
80
81 $this->xmlStartTag("Exercise", $attribs);
82
83 //todo: create new dtd for new assignment structure
84 $this->xmlElement("Title", null, $this->exercise->getTitle());
85 $this->xmlElement("Description", null, $this->exercise->getDescription());
86 //$this->xmlElement("Instruction", null,$this->exercise->getInstruction());
87 //$this->xmlElement("DueDate", null,$this->exercise->getTimestamp());
88
89
90 //todo: as a workaround use first assignment for compatibility with old exercise dtd
91 $assignments = ilExAssignment::getAssignmentDataOfExercise($this->exercise->getId());
92
93 if (count($assignments) > 0) {
94 foreach ($assignments as $assignment) {
95 $this->xmlStartTag("Assignment");
96 $this->xmlElement("Instruction", null, $assignment ["instruction"]);
97 $this->xmlElement("DueDate", null, $assignment ["deadline"]);
98
99 $this->handleAssignmentFiles($this->exercise->getId(), $assignment ["id"]);
100 if ($this->attachMembers) {
101 $this->handleAssignmentMembers($this->exercise->getId(), $assignment ["id"]);
102 }
103 $this->xmlEndTag("Assignment");
104 }
105 }
106
107
108 $this->xmlEndTag("Exercise");
109 $this->__buildFooter();
110
111 return true;
112 }
113
114 public function getXML(): string
115 {
116 return $this->xmlDumpMem(false);
117 }
118
119 public function __buildHeader(): bool
120 {
121 $this->xmlSetDtdDef("<!DOCTYPE Exercise PUBLIC \"-//ILIAS//DTD ExerciseAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_exercise_4_4.dtd\">");
122 $this->xmlSetGenCmt("Exercise Object");
123 $this->xmlHeader();
124
125 return true;
126 }
127
128 public function __buildFooter(): void
129 {
130 }
131
135 public function setAttachMembers(bool $value): void
136 {
137 $this->attachMembers = $value;
138 }
139
143 private function attachMarking(
144 int $user_id,
145 int $assignment_id
146 ): void {
147 $ass = new ilExAssignment($assignment_id);
148
149 $amark = $ass->getMemberStatus($user_id)->getMark();
150 $astatus = $ass->getMemberStatus($user_id)->getStatus();
151 $acomment = $ass->getMemberStatus($user_id)->getComment();
152 $anotice = $ass->getMemberStatus($user_id)->getNotice();
153
154
155 if ($astatus == "notgraded") {
157 } elseif ($astatus == "failed") {
159 } else {
161 }
162
163 $this->xmlStartTag("Marking", array("status" => $status ));
164 $this->xmlElement("Mark", null, $amark);
165 $this->xmlElement("Notice", null, $anotice);
166 $this->xmlElement("Comment", null, $acomment);
167 $this->xmlEndTag("Marking");
168 }
169
170 private function handleAssignmentFiles(
171 int $ex_id,
172 int $as_id
173 ): void {
174 $this->xmlStartTag("Files");
175 $storage = new ilFSStorageExercise($ex_id, $as_id);
176 $files = $storage->getFiles();
177
178 if (count($files)) {
179 foreach ($files as $file) {
180 $this->xmlStartTag("File", array("size" => $file ["size"] ));
181 $this->xmlElement("Filename", null, $file ["name"]);
182 if ($this->attachFileContents) {
183 $filename = $file ["fullpath"];
184 if (is_file($filename)) {
185 $content = file_get_contents($filename);
186 $attribs = array("mode" => "PLAIN" );
187 if ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
188 $attribs = array("mode" => "ZLIB" );
189 $content = gzcompress($content, 9);
190 } elseif ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
191 $attribs = array("mode" => "GZIP" );
192 $content = gzencode($content, 9);
193 }
194 $content = base64_encode($content);
195 $this->xmlElement("Content", $attribs, $content);
196 }
197 }
198 $this->xmlEndTag("File");
199 }
200 }
201 $this->xmlEndTag("Files");
202 }
203
207 private function handleAssignmentMembers(
208 int $ex_id,
209 int $assignment_id
210 ): void {
211 $this->xmlStartTag("Members");
212 $members = ilExerciseMembers::_getMembers($ex_id);
213 if (count($members)) {
214 foreach ($members as $member_id) {
215 $this->xmlStartTag("Member", array("usr_id" => "il_" . IL_INST_ID . "_usr_" . $member_id ));
216
217 $name = ilObjUser::_lookupName($member_id);
218
219 $this->xmlElement("Firstname", array(), $name['firstname']);
220 $this->xmlElement("Lastname", array(), $name['lastname']);
221 $this->xmlElement("Login", array(), $name['login']);
222 $this->attachMarking($member_id, $assignment_id);
223 $this->xmlEndTag("Member");
224 }
225 }
226 $this->xmlEndTag("Members");
227 }
228}
$filename
Definition: buildRTE.php:78
Exercise assignment.
static getAssignmentDataOfExercise(int $a_exc_id)
getMemberStatus(?int $a_user_id=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getMembers(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
attachMarking(int $user_id, int $assignment_id)
attach marking tag to member for given assignment
setAttachFileContents(int $attachFileContents)
set attachment content mode
handleAssignmentMembers(int $ex_id, int $assignment_id)
create xml for files per assignment
handleAssignmentFiles(int $ex_id, int $as_id)
setAttachMembers(bool $value)
write access to property attchMarkings
setExercise(ilObjExercise $exercise)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjExercise.
static _lookupName(int $a_user_id)
lookup user name
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
if($format !==null) $name
Definition: metadata.php:247
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc