ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExerciseXMLWriter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Services/Xml/classes/class.ilXmlWriter.php";
5 include_once "./Modules/Exercise/classes/class.ilExAssignment.php";
6 
22 
23  static $CONTENT_ATTACH_NO = 0;
27 
28  static $STATUS_NOT_GRADED = "NOT_GRADED";
29  static $STATUS_PASSED = "PASSED";
30  static $STATUS_FAILED = "FAILED";
37 
44 
50  var $exercise;
51 
59  function ilExerciseXMLWriter() {
60  // @todo: needs to be revised for multiple assignments per exercise
61  //die ("Needs revision for ILIAS 4.1");
63  $this->attachFileContents = ilExerciseXMLWriter::$CONTENT_ATTACH_NO;
64  }
65 
72  function setExercise(& $exercise) {
73  $this->exercise = & $exercise;
74  }
75 
83  if ($attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED && ! function_exists ( "gzencode" )) {
84  throw new ilExerciseException ( "Inflating with gzip is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH );
85  }
86  if ($attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED && ! function_exists ( "gzcompress" )) {
87  throw new ilExerciseException ( "Inflating with zlib (compress/uncompress) is not supported", ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH );
88  }
89 
90  $this->attachFileContents = $attachFileContents;
91  }
92 
93  function start() {
94  $this->__buildHeader ();
95 
96  $attribs = array ("obj_id" => "il_" . IL_INST_ID . "_exc_" . $this->exercise->getId () );
97 
98  if ($this->exercise->getOwner ())
99  $attribs ["owner"] = "il_" . IL_INST_ID . "_usr_" . $this->exercise->getOwner ();
100 
101  $this->xmlStartTag ( "Exercise", $attribs );
102 
103  //todo: create new dtd for new assignment structure
104  $this->xmlElement("Title", null,$this->exercise->getTitle());
105  $this->xmlElement("Description", null,$this->exercise->getDescription());
106  //$this->xmlElement("Instruction", null,$this->exercise->getInstruction());
107  //$this->xmlElement("DueDate", null,$this->exercise->getTimestamp());
108 
109 
110  //todo: as a workaround use first assignment for compatibility with old exercise dtd
111  $assignments = ilExAssignment::getAssignmentDataOfExercise ( $this->exercise->getId () );
112 
113  if (count ( $assignments ) > 0) {
114  foreach ( $assignments as $assignment ) {
115  $this->xmlStartTag ("Assignment");
116  $this->xmlElement ( "Instruction", null, $assignment ["instruction"] );
117  $this->xmlElement ( "DueDate", null, $assignment ["deadline"] );
118 
119  $this->handleAssignmentFiles ( $this->exercise->getId (), $assignment ["id"] );
120  if ($this->attachMembers)
121  {
122  $this->handleAssignmentMembers ($this->exercise->getId (), $assignment ["id"]);
123  }
124  $this->xmlEndTag ( "Assignment" );
125  }
126  }
127 
128 
129  $this->xmlEndTag ( "Exercise" );
130  $this->__buildFooter ();
131 
132  return true;
133  }
134 
135  function getXML() {
136  return $this->xmlDumpMem ( FALSE );
137  }
138 
139  function __buildHeader() {
140  $this->xmlSetDtdDef ( "<!DOCTYPE Exercise PUBLIC \"-//ILIAS//DTD ExerciseAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_exercise_4_4.dtd\">" );
141  $this->xmlSetGenCmt ( "Exercise Object" );
142  $this->xmlHeader ();
143 
144  return true;
145  }
146 
147  function __buildFooter() {
148 
149  }
150 
156  public function setAttachMembers($value) {
157  $this->attachMembers = $value ? true : false;
158  }
159 
166  private function attachMarking($user_id, $assignment_id) {
167 
168  $amark = ilExAssignment::lookupMarkOfUser($assignment_id, $user_id);
169  $astatus = ilExAssignment::lookupStatusOfUser($assignment_id, $user_id);
170  $acomment = ilExAssignment::lookupCommentForUser($assignment_id, $user_id);
171  $anotice = ilExAssignment::lookupNoticeOfUser($assignment_id, $user_id);
172 
173 
174  if ($astatus == "notgraded") {
176  } elseif ($astatus == "failed") {
178  } else {
180  }
181 
182  $this->xmlStartTag ( "Marking", array ("status" => $status ) );
183  $this->xmlElement ( "Mark", null, $amark);
184  $this->xmlElement ( "Notice", null, $anotice );
185  $this->xmlElement ( "Comment", null, $acomment );
186  $this->xmlEndTag ( "Marking" );
187  }
188 
189  private function handleAssignmentFiles($ex_id, $as_id) {
190  $this->xmlStartTag ( "Files" );
191  include_once ("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
192  $storage = new ilFSStorageExercise ( $ex_id, $as_id );
193  $files = $storage->getFiles ();
194 
195  if (count ( $files )) {
196  foreach ( $files as $file ) {
197  $this->xmlStartTag ( "File", array ("size" => $file ["size"] ) );
198  $this->xmlElement ( "Filename", null, $file ["name"] );
199  if ($this->attachFileContents) {
200  $filename = $file ["fullpath"];
201  if (@is_file ( $filename )) {
202  $content = @file_get_contents ( $filename );
203  $attribs = array ("mode" => "PLAIN" );
204  if ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
205  $attribs = array ("mode" => "ZLIB" );
206  $content = gzcompress ( $content, 9 );
207  } elseif ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
208  $attribs = array ("mode" => "GZIP" );
209  $content = gzencode ( $content, 9 );
210  }
211  $content = base64_encode ( $content );
212  $this->xmlElement ( "Content", $attribs, $content );
213  }
214  }
215  $this->xmlEndTag ( "File" );
216  }
217  }
218  $this->xmlEndTag ( "Files" );
219  }
220 
227  private function handleAssignmentMembers($ex_id, $assignment_id) {
228  $this->xmlStartTag ( "Members" );
229  include_once ("./Modules/Exercise/classes/class.ilExerciseMembers.php");
230  $members = ilExerciseMembers::_getMembers($ex_id);
231  if (count ( $members )) {
232  foreach ( $members as $member_id ) {
233  $this->xmlStartTag ( "Member", array ("usr_id" => "il_" . IL_INST_ID . "_usr_" . $member_id ) );
234  $this->attachMarking ( $member_id, $assignment_id);
235  $this->xmlEndTag ( "Member" );
236  }
237  }
238  $this->xmlEndTag ( "Members" );
239  }
240 }
241 
242 ?>