• 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 
00245                 // important check: if the directory does not exist
00246                 // ILIAS stays in the current directory (echoing only a warning)
00247                 // and the zip command below archives the whole ILIAS directory
00248                 // (including the data directory) and sends a mega file to the user :-o
00249                 if (!is_dir($savepath))
00250                 {
00251                         return;
00252                 }
00253                 chdir($savepath);
00254                 $zip = PATH_TO_ZIP;
00255                 $tmpfile = ilUtil::ilTempnam();
00256                 $tmpzipfile = $tmpfile . ".zip";
00257                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." *";
00258                 exec($zipcmd);
00259                 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00260                 chdir($cdir);
00261                 unlink($savepath . "users.txt");
00262                 unlink($tmpfile);
00263                 unlink($tmpzipfile);
00264         }
00265         
00272         function unlinkFiles($a_filenames)
00273         {
00274                 if(is_array($a_filenames))
00275                 {
00276                         foreach($a_filenames as $file)
00277                         {
00278                                 if(!$this->unlinkFile($file))
00279                                 {
00280                                         return $file;
00281                                 }
00282                         }
00283                 }
00284                 return '';
00285         }
00292         function unlinkFile($a_filename)
00293         {
00294                 if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
00295                 {
00296                         return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
00297                 }
00298         }
00305         function getAbsolutePath($a_path)
00306         {
00307                 return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
00308         }
00309 
00316         function checkFilesExist($a_files)
00317         {
00318                 if($a_files)
00319                 {
00320                         foreach($a_files as $file)
00321                         {
00322                                 if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
00323                                 {
00324                                         return false;
00325                                 }
00326                         }
00327                         return true;
00328                 }
00329                 return true;
00330         }
00331 
00332         // PRIVATE METHODS
00333         function __checkPath()
00334         {
00335                 if(!@file_exists($this->getExercisePath()))
00336                 {
00337                         return false;
00338                 }
00339                 $this->__checkReadWrite();
00340 
00341                 return true;
00342         }
00349         function __checkReadWrite()
00350         {
00351                 if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
00352                 {
00353                         return true;
00354                 }
00355                 else
00356                 {
00357                         $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00358                 }
00359         }
00366         function __initDirectory()
00367         {
00368                 if(is_writable($this->getPath()))
00369                 {
00370                         return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
00371                 }
00372                 return false;
00373         }
00381         function __rotateFiles($a_path)
00382         {
00383                 if(file_exists($a_path))
00384                 {
00385                         $this->__rotateFiles($a_path.".old");
00386                         return rename($a_path,$a_path.'.old');
00387                 }
00388                 return true;
00389         }
00390 }

Generated on Fri Dec 13 2013 10:18:26 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1