Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once("classes/class.ilFileData.php");
00025
00034 class ilFileDataExercise extends ilFileData
00035 {
00041 var $obj_id;
00042
00048 var $exercise_path;
00049
00057 function ilFileDataExercise($a_obj_id = 0)
00058 {
00059 define('EXERCISE_PATH','exercise');
00060 parent::ilFileData();
00061 $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
00062
00063
00064 if(!$this->__checkPath())
00065 {
00066 $this->__initDirectory();
00067 }
00068 $this->obj_id = $a_obj_id;
00069 }
00070
00071 function getObjId()
00072 {
00073 return $this->obj_id;
00074 }
00075
00081 function getExercisePath()
00082 {
00083 return $this->exercise_path;
00084 }
00085
00086 function getFiles()
00087 {
00088 $files = array();
00089 $dp = opendir($this->exercise_path);
00090
00091 while($file = readdir($dp))
00092 {
00093 if(is_dir($file))
00094 {
00095 continue;
00096 }
00097 list($obj_id,$rest) = split('_',$file,2);
00098 if($obj_id == $this->obj_id)
00099 {
00100 if(!is_dir($this->exercise_path.'/'.$file))
00101 {
00102 $files[] = array(
00103 'name' => $rest,
00104 'size' => filesize($this->exercise_path.'/'.$file),
00105 'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))));
00106 }
00107 }
00108 }
00109 closedir($dp);
00110 return $files;
00111 }
00112
00113 function ilClone($a_new_obj_id)
00114 {
00115 foreach($this->getFiles() as $file)
00116 {
00117 @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
00118 $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
00119 }
00120 return true;
00121 }
00122 function delete()
00123 {
00124 foreach($this->getFiles() as $file)
00125 {
00126 $this->unlinkFile($file["name"]);
00127 }
00128
00129 $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
00130 if (is_dir($delivered_file_path))
00131 {
00132 include_once "./classes/class.ilUtil.php";
00133 ilUtil::delDir($delivered_file_path);
00134 }
00135
00136 return true;
00137 }
00138
00145 function storeUploadedFile($a_http_post_file, $secure_filename = false)
00146 {
00147
00148
00149
00150 $filename = $a_http_post_file['name'];
00151 if ($secure_filename)
00152 {
00153
00154 $filename = preg_replace("/\s/", "_", $filename);
00155
00156 $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00157 }
00158 if(isset($a_http_post_file) && $a_http_post_file['size'])
00159 {
00160
00161 $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00162
00163
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
00181
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
00188 $filename = preg_replace("/\s/", "_", $filename);
00189
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
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
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
00230 function downloadAllDeliveredFiles($members)
00231 {
00232 require_once "./classes/class.ilUtil.php";
00233 global $lng;
00234
00235 ksort($members);
00236 $tmpfile = ilUtil::ilTempnam();
00237 $fh = fopen($tmpfile, "w");
00238
00239 if ($fh)
00240 {
00241 foreach ($members as $id => $member)
00242 {
00243 fwrite($fh, "$id\t$member\n");
00244 }
00245 fclose($fh);
00246 }
00247
00248 $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
00249 copy($tmpfile, $savepath . "users.txt");
00250 $cdir = getcwd();
00251
00252
00253
00254
00255
00256 if (!is_dir($savepath))
00257 {
00258 return;
00259 }
00260
00261 chdir($this->getExercisePath());
00262 $zip = PATH_TO_ZIP;
00263 $tmpfile = ilUtil::ilTempnam();
00264 $tmpzipfile = $tmpfile . ".zip";
00265
00266 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." ".$this->obj_id;
00267 exec($zipcmd);
00268 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00269 chdir($cdir);
00270 unlink($savepath . "users.txt");
00271 unlink($tmpfile);
00272 unlink($tmpzipfile);
00273 }
00274
00281 function unlinkFiles($a_filenames)
00282 {
00283 if(is_array($a_filenames))
00284 {
00285 foreach($a_filenames as $file)
00286 {
00287 if(!$this->unlinkFile($file))
00288 {
00289 return $file;
00290 }
00291 }
00292 }
00293 return '';
00294 }
00301 function unlinkFile($a_filename)
00302 {
00303 if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
00304 {
00305 return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
00306 }
00307 }
00314 function getAbsolutePath($a_path)
00315 {
00316 return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
00317 }
00318
00325 function checkFilesExist($a_files)
00326 {
00327 if($a_files)
00328 {
00329 foreach($a_files as $file)
00330 {
00331 if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
00332 {
00333 return false;
00334 }
00335 }
00336 return true;
00337 }
00338 return true;
00339 }
00340
00341
00342 function __checkPath()
00343 {
00344 if(!@file_exists($this->getExercisePath()))
00345 {
00346 return false;
00347 }
00348 $this->__checkReadWrite();
00349
00350 return true;
00351 }
00358 function __checkReadWrite()
00359 {
00360 if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
00361 {
00362 return true;
00363 }
00364 else
00365 {
00366 $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00367 }
00368 }
00375 function __initDirectory()
00376 {
00377 if(is_writable($this->getPath()))
00378 {
00379 return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
00380 }
00381 return false;
00382 }
00390 function __rotateFiles($a_path)
00391 {
00392 if(file_exists($a_path))
00393 {
00394 $this->__rotateFiles($a_path.".old");
00395 return rename($a_path,$a_path.'.old');
00396 }
00397 return true;
00398 }
00399 }