ILIAS  release_4-3 Revision
 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->xmlElement ( "Title", null, $this->exercise->getTitle ());
116  $this->xmlElement ( "Description", null, $this->exercise->getDescription () );
117  $this->xmlElement ( "Instruction", null, $assignment ["instruction"] );
118  $this->xmlElement ( "DueDate", null, $assignment ["deadline"] );
119 
120  $this->handleAssignmentFiles ( $this->exercise->getId (), $assignment ["id"] );
121  //todo: we handle only the first one for workaround, remove later for new dtd
122  break;
123  }
124  } else {
125  $this->xmlElement ( "Title", null, $this->exercise->getTitle () );
126  $this->xmlElement ( "Description", null, $this->exercise->getDescription () );
127  }
128 
129  if ($this->attachMembers) {
130  $this->handleAssignmentMembers ($this->exercise->getId (), $assignments);
131  }
132 
133  $this->xmlEndTag ( "Exercise" );
134  $this->__buildFooter ();
135 
136  return true;
137  }
138 
139  function getXML() {
140  return $this->xmlDumpMem ( FALSE );
141  }
142 
143  function __buildHeader() {
144  $this->xmlSetDtdDef ( "<!DOCTYPE Exercise PUBLIC \"-//ILIAS//DTD ExerciseAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_exercise_3_10.dtd\">" );
145  $this->xmlSetGenCmt ( "Exercise Object" );
146  $this->xmlHeader ();
147 
148  return true;
149  }
150 
151  function __buildFooter() {
152 
153  }
154 
160  public function setAttachMembers($value) {
161  $this->attachMembers = $value ? true : false;
162  }
163 
170  private function attachMarking($user_id, $assignment_id) {
171 
172  $amark = ilExAssignment::lookupMarkOfUser($assignment_id, $user_id);
173  $astatus = ilExAssignment::lookupStatusOfUser($assignment_id, $user_id);
174  $acomment = ilExAssignment::lookupCommentForUser($assignment_id, $user_id);
175  $anotice = ilExAssignment::lookupNoticeOfUser($assignment_id, $user_id);
176 
177 
178  if ($astatus == "notgraded") {
180  } elseif ($astatus == "failed") {
182  } else {
184  }
185 
186  $this->xmlStartTag ( "Marking", array ("status" => $status ) );
187  $this->xmlElement ( "Mark", null, $amark);
188  $this->xmlElement ( "Notice", null, $anotice );
189  $this->xmlElement ( "Comment", null, $acomment );
190  $this->xmlEndTag ( "Marking" );
191  }
192 
193  private function handleAssignmentFiles($ex_id, $as_id) {
194  $this->xmlStartTag ( "Files" );
195  include_once ("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
196  $storage = new ilFSStorageExercise ( $ex_id, $as_id );
197  $files = $storage->getFiles ();
198 
199  if (count ( $files )) {
200  foreach ( $files as $file ) {
201  $this->xmlStartTag ( "File", array ("size" => $file ["size"] ) );
202  $this->xmlElement ( "Filename", null, $file ["name"] );
203  if ($this->attachFileContents) {
204  $filename = $file ["fullpath"];
205  if (@is_file ( $filename )) {
206  $content = @file_get_contents ( $filename );
207  $attribs = array ("mode" => "PLAIN" );
208  if ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
209  $attribs = array ("mode" => "ZLIB" );
210  $content = gzcompress ( $content, 9 );
211  } elseif ($this->attachFileContents == ilExerciseXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
212  $attribs = array ("mode" => "GZIP" );
213  $content = gzencode ( $content, 9 );
214  }
215  $content = base64_encode ( $content );
216  $this->xmlElement ( "Content", $attribs, $content );
217  }
218  }
219  $this->xmlEndTag ( "File" );
220  }
221  }
222  $this->xmlEndTag ( "Files" );
223  }
224 
231  private function handleAssignmentMembers($ex_id, $assignments) {
232  $this->xmlStartTag ( "Members" );
233  include_once ("./Modules/Exercise/classes/class.ilExerciseMembers.php");
234  $members = ilExerciseMembers::_getMembers($ex_id);
235  if (count ( $members )) {
236  foreach ( $members as $member_id ) {
237  $this->xmlStartTag ( "Member", array ("usr_id" => "il_" . IL_INST_ID . "_usr_" . $member_id ) );
238  if (count ($assignments) > 0) {
239  foreach($assignments as $assignment) {
240  $this->attachMarking ( $member_id, $assignment["id"] );
241  //todo: handle only first assignment, must be fixed when for dtd
242  break;
243  }
244  }
245  $this->xmlEndTag ( "Member" );
246  }
247  }
248  $this->xmlEndTag ( "Members" );
249  }
250 }
251 
252 ?>