• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Modules/Exercise/classes/class.ilExerciseXMLParser.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00036 include_once 'classes/class.ilSaxParser.php';
00037 include_once 'Modules/Exercise/classes/class.ilExerciseException.php';
00038 
00039 
00040 
00041 class ilExerciseXMLParser extends ilSaxParser
00042 {
00043 
00044     static $CONTENT_NOT_COMPRESSED = 0;
00045     static $CONTENT_GZ_COMPRESSED = 1;
00046     static $CONTENT_ZLIB_COMPRESSED = 2;
00047 
00053     var $exercise;
00054 
00061     var $obj_id;
00062 
00063 
00069     var $result;
00070 
00076     var $mode;
00077 
00086         function ilExerciseXMLParser(& $exercise, $a_xml_data, $obj_id = -1)
00087         {
00088                 parent::ilSaxParser();
00089                 $this->exercise = $exercise;
00090                 $this->setXMLContent($a_xml_data);
00091                 $this->obj_id = $obj_id;
00092                 $this->result = false;
00093         }
00094 
00095 
00102         function setHandlers($a_xml_parser)
00103         {
00104                 xml_set_object($a_xml_parser,$this);
00105                 xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
00106                 xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
00107         }
00108 
00117         function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
00118         {
00119                 global $ilErr;
00120 
00121                 switch($a_name)
00122                 {
00123                         case 'Exercise':
00124                             if (isset($a_attribs["obj_id"]))
00125                             {
00126                    $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
00127                                if ($this->obj_id != -1 && (int) $read_obj_id  != -1 && (int) $this->obj_id != (int) $read_obj_id)
00128                                {
00129                        throw new ilExerciseException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilExerciseException::$ID_MISMATCH);
00130                    }
00131                             }
00132                                 break;
00133                         case 'Member':
00134                             $this->usr_action = $a_attribs["action"];
00135                             $this->usr_id = ilUtil::__extractId($a_attribs["usr_id"], IL_INST_ID);
00136                                 break;
00137 
00138                         case 'File':
00139                 $this->file_action = $a_attribs["action"];
00140                                 break;
00141                         case 'Content':
00142                 $this->mode = ilExerciseXMLParser::$CONTENT_NOT_COMPRESSED;
00143                             if ($a_attribs["mode"] == "GZIP")
00144                             {
00145                     if (!function_exists("gzdecode"))
00146                         throw new  ilExerciseException ("Deflating with gzip is not supported",  ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
00147 
00148                                 $this->mode = ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED;
00149                             } elseif ($a_attribs["mode"] == "ZLIB")
00150                             {
00151                     if (!function_exists("gzuncompress"))
00152                         throw new ilExerciseException ("Deflating with zlib (compress/uncompress) is not supported",  ilExerciseException::$ID_DEFLATE_METHOD_MISMATCH);
00153 
00154                                 $this->mode = ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED;
00155                             }
00156                             break;
00157 
00158                 }
00159         }
00160 
00161 
00162 
00169         function handlerEndTag($a_xml_parser,$a_name)
00170         {
00171                 switch($a_name)
00172                 {
00173                         case 'Exercise':
00174                   $this->result = true;
00175                                 break;
00176                         case 'Title':
00177                             $this->exercise->setTitle(trim($this->cdata));
00178                                 break;
00179                         case 'Description':
00180                             $this->exercise->setDescription(trim($this->cdata));
00181                                 break;
00182                         case 'Instruction':
00183                                 $this->exercise->setInstruction(trim($this->cdata));
00184                                 break;
00185                         case 'DueDate':
00186                                 $this->exercise->setTimestamp(trim($this->cdata));
00187                                 break;
00188                         case 'Member':
00189                             $this->updateMember($this->usr_id, $this->usr_action);
00190                             break;
00191                         case 'Filename':
00192                             $this->file_name = trim($this->cdata);
00193                             break;
00194                         case 'Content':
00195                             $this->file_content = trim($this->cdata);
00196                             break;
00197                         case 'File':
00198                 $this->updateFile($this->file_name, $this->file_content, $this->file_action);
00199                             break;
00200 
00201                 }
00202 
00203                 $this->cdata = '';
00204 
00205                 return;
00206         }
00207 
00214         function handlerCharacterData($a_xml_parser,$a_data)
00215         {
00216                 if($a_data != "\n")
00217                 {
00218                         $this->cdata .= $a_data;
00219                 }
00220         }
00221 
00222 
00229         private function updateMember ($user_id, $action) {
00230        if (!is_int($user_id) || $user_id <= 0) {
00231            return;
00232        }
00233            $memberObject = $this->exercise->members_obj;
00234 
00235            if ($action == "Attach" && !$memberObject->isAssigned($user_id))
00236            {
00237             $memberObject->assignMember ($user_id);
00238        }
00239 
00240        if ($action == "Detach" && $memberObject->isAssigned($user_id))
00241        {
00242             $memberObject->deassignMember ($user_id);
00243        }
00244         }
00245 
00253         private function updateFile ($filename, $b64encodedContent, $action)
00254         {
00255        if (strlen($filename) == 0) {
00256            return;
00257        }
00258 
00259             $fileObject = $this->exercise->file_obj;
00260         if ($action == "Attach")
00261         {
00262            $content = base64_decode((string) $b64encodedContent);
00263            if ($this->mode == ilExerciseXMLParser::$CONTENT_GZ_COMPRESSED) {
00264                 $content = gzdecode($content);
00265                }elseif ($this->mode ==ilExerciseXMLParser::$CONTENT_ZLIB_COMPRESSED) {
00266                 $content = gzuncompress($content);
00267                }
00268            $fileObject->storeContentAsFile ($filename, $content);
00269         }
00270         if ($action == "Detach")
00271         {
00272             $fileObject->unlinkFile ($filename);
00273         }
00274         }
00275 
00283         public function start () {
00284             $this->startParsing();
00285             return $this->result > 0;
00286         }
00287 
00288 }
00289 ?>

Generated on Fri Dec 13 2013 17:56:50 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1