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

classes/class.ilFileDataExercise.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 
00033 require_once("classes/class.ilFileData.php");
00034                                 
00035 class ilFileDataExercise extends ilFileData
00036 {
00042         var $obj_id;
00043 
00049         var $exercise_path;
00050 
00058         function ilFileDataExercise($a_obj_id = 0)
00059         {
00060                 define('EXERCISE_PATH','exercise');
00061                 parent::ilFileData();
00062                 $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
00063                 
00064                 // IF DIRECTORY ISN'T CREATED CREATE IT
00065                 if(!$this->__checkPath())
00066                 {
00067                         $this->__initDirectory();
00068                 }
00069                 $this->obj_id = $a_obj_id;
00070         }
00071 
00072         function getObjId()
00073         {
00074                 return $this->obj_id;
00075         }
00076 
00082         function getExercisePath()
00083         {
00084                 return $this->exercise_path;
00085         }
00086 
00087         function getFiles()
00088         {
00089                 $files = array();
00090                 $dp = opendir($this->exercise_path);
00091 
00092                 while($file = readdir($dp))
00093                 {
00094                         if(is_dir($file))
00095                         {
00096                                 continue;
00097                         }
00098                         list($obj_id,$rest) = split('_',$file,2);
00099                         if($obj_id == $this->obj_id)
00100                         {
00101                                 if(!is_dir($this->exercise_path.'/'.$file))
00102                                 {
00103                                         $files[] = array(
00104                                                 'name'     => $rest,
00105                                                 'size'     => filesize($this->exercise_path.'/'.$file),
00106                                                 'ctime'    => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))));
00107                                 }
00108                         }
00109                 }
00110                 closedir($dp);
00111                 return $files;
00112         }
00113 
00114         function ilClone($a_new_obj_id)
00115         {
00116                 foreach($this->getFiles() as $file)
00117                 {
00118                         @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
00119                                   $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
00120                 }
00121                 return true;
00122         }
00123         function delete()
00124         {
00125                 foreach($this->getFiles() as $file)
00126                 {
00127                         $this->unlinkFile($file["name"]);
00128                 }
00129                 
00130                 $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
00131                 if (is_dir($delivered_file_path))
00132                 {
00133                         system("rm -rf " . ilUtil::escapeShellArg($delivered_file_path));
00134                 }
00135                 
00136                 return true;
00137         }
00138 
00145         function storeUploadedFile($a_http_post_file, $secure_filename = false)
00146         {
00147                 // TODO: 
00148                 // CHECK UPLOAD LIMIT
00149                 // 
00150                 $filename = $a_http_post_file['name'];
00151                 if ($secure_filename)
00152                 {
00153                         // replace whitespaces with underscores
00154                         $filename = preg_replace("/\s/", "_", $filename);
00155                         // remove all special characters
00156                         $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00157                 }
00158                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00159                 {
00160                         // CHECK IF FILE WITH SAME NAME EXISTS
00161                         $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00162                         //move_uploaded_file($a_http_post_file['tmp_name'],$this->getExercisePath().'/'.$this->obj_id.'_'.
00163                         //                                 $filename);
00164                         ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
00165                                 $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00166 
00167                 }
00168                 return true;
00169         }
00170 
00178         function deliverFile($a_http_post_file, $user_id)
00179         {
00180                 // TODO: 
00181                 // CHECK UPLOAD LIMIT
00182                 // 
00183                 $result = false;
00184                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00185                 {
00186                         $filename = $a_http_post_file['name'];
00187                         // replace whitespaces with underscores
00188                         $filename = preg_replace("/\s/", "_", $filename);
00189                         // remove all special characters
00190                         $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00191 
00192                         if(!is_dir($savepath = $this->getExercisePath()."/".$this->obj_id))
00193                         {
00194                                 ilUtil::makeDir($savepath);
00195                         }
00196                         $savepath .= '/' .$user_id;
00197                         if(!is_dir($savepath))
00198                         {
00199                                 ilUtil::makeDir($savepath);
00200                         }                               
00201 
00202                         // CHECK IF FILE PATH EXISTS
00203                         if (!is_dir($savepath))
00204                         {
00205                                 require_once "./classes/class.ilUtil.php";
00206                                 #ilUtil::makeDirParents($savepath);
00207                                 ilUtil::makeDir($savepath);
00208                         }
00209                         $now = getdate();
00210                         $prefix = sprintf("%04d%02d%02d%02d%02d%02d", $now["year"], $now["mon"], $now["mday"], $now["hours"], 
00211                                                           $now["minutes"], $now["seconds"]);
00212                         //move_uploaded_file($a_http_post_file["tmp_name"], $savepath . $prefix . "_" . $filename);
00213                         ilUtil::moveUploadedFile($a_http_post_file["tmp_name"], $a_http_post_file["name"],
00214                                 $savepath . "/" . $prefix . "_" . $filename);
00215                         require_once "./content/classes/Media/class.ilObjMediaObject.php";
00216                         $result = array(
00217                                 "filename" => $prefix . "_" . $filename,
00218                                 "fullname" => $savepath . "/" . $prefix . "_" . $filename,
00219                                 "mimetype" =>   ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
00220                         );
00221                 }
00222                 return $result;
00223         }
00224         
00225         function downloadAllDeliveredFiles($members)
00226         {
00227                 require_once "./classes/class.ilUtil.php";
00228                 global $lng;
00229                 
00230                 ksort($members);
00231                 $tmpfile = ilUtil::ilTempnam();
00232                 $fh = fopen($tmpfile, "w");
00233                 if ($fh)
00234                 {
00235                         foreach ($members as $id => $member)
00236                         {
00237                                 fwrite($fh, "$id\t$member\n");
00238                         }
00239                         fclose($fh);
00240                 }
00241                 $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
00242                 copy($tmpfile, $savepath . "users.txt"); 
00243                 $cdir = getcwd();
00244                 chdir($savepath);
00245                 $zip = PATH_TO_ZIP;
00246                 $tmpfile = ilUtil::ilTempnam();
00247                 $tmpzipfile = $tmpfile . ".zip";
00248                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." *";
00249                 exec($zipcmd);
00250                 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00251                 chdir($cdir);
00252                 unlink($savepath . "users.txt");
00253                 unlink($tmpfile);
00254                 unlink($tmpzipfile);
00255         }
00256         
00263         function unlinkFiles($a_filenames)
00264         {
00265                 if(is_array($a_filenames))
00266                 {
00267                         foreach($a_filenames as $file)
00268                         {
00269                                 if(!$this->unlinkFile($file))
00270                                 {
00271                                         return $file;
00272                                 }
00273                         }
00274                 }
00275                 return '';
00276         }
00283         function unlinkFile($a_filename)
00284         {
00285                 if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
00286                 {
00287                         return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
00288                 }
00289         }
00296         function getAbsolutePath($a_path)
00297         {
00298                 return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
00299         }
00300 
00307         function checkFilesExist($a_files)
00308         {
00309                 if($a_files)
00310                 {
00311                         foreach($a_files as $file)
00312                         {
00313                                 if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
00314                                 {
00315                                         return false;
00316                                 }
00317                         }
00318                         return true;
00319                 }
00320                 return true;
00321         }
00322 
00323         // PRIVATE METHODS
00324         function __checkPath()
00325         {
00326                 if(!@file_exists($this->getExercisePath()))
00327                 {
00328                         return false;
00329                 }
00330                 $this->__checkReadWrite();
00331 
00332                 return true;
00333         }
00340         function __checkReadWrite()
00341         {
00342                 if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
00343                 {
00344                         return true;
00345                 }
00346                 else
00347                 {
00348                         $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00349                 }
00350         }
00357         function __initDirectory()
00358         {
00359                 if(is_writable($this->getPath()))
00360                 {
00361                         return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
00362                 }
00363                 return false;
00364         }
00372         function __rotateFiles($a_path)
00373         {
00374                 if(file_exists($a_path))
00375                 {
00376                         $this->__rotateFiles($a_path.".old");
00377                         return rename($a_path,$a_path.'.old');
00378                 }
00379                 return true;
00380         }
00381 }

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1