From 199d242993f7595f1c36092f1bcc589a92918667 Mon Sep 17 00:00:00 2001 From: moodler Date: Sun, 19 Oct 2003 15:02:41 +0000 Subject: [PATCH] I finally added a proper course files browser to replace the menu. --- mod/resource/coursefiles.php | 854 +++++++++++++++++++++++++++++++++++ mod/resource/details.php | 128 +++++- 2 files changed, 959 insertions(+), 23 deletions(-) create mode 100644 mod/resource/coursefiles.php diff --git a/mod/resource/coursefiles.php b/mod/resource/coursefiles.php new file mode 100644 index 0000000000..7325c0e122 --- /dev/null +++ b/mod/resource/coursefiles.php @@ -0,0 +1,854 @@ +id); + + if (! isteacher($course->id) ) { + error("Only teachers can edit files"); + } + + function html_footer() { + echo ""; + } + + function html_header($course, $wdir, $formfield=""){ + + global $CFG; + + if (! $site = get_site()) { + error("Invalid site!"); + } + + if ($course->id == $site->id) { + $strfiles = get_string("sitefiles"); + } else { + $strfiles = get_string("files"); + } + + if ($wdir == "/") { + $fullnav = "$strfiles"; + } else { + $dirs = explode("/", $wdir); + $numdirs = count($dirs); + $link = ""; + $navigation = ""; + for ($i=1; $i<$numdirs; $i++) { + $navigation .= " -> "; + $link .= "/".urlencode($dirs[$i]); + $navigation .= "id&wdir=$link\">".$dirs[$i].""; + } + $fullnav = "id&wdir=/\">$strfiles $navigation"; + } + + print_header(); + ?> + + '; + echo ''; + echo ''; + echo ''."$course->shortname -> $fullnav".''; + echo ''; + echo ''; + echo ''; + + if ($course->id == $site->id) { + print_heading(get_string("publicsitefileswarning"), "center", 2); + } + + echo ""; + echo ""; + echo "\n"; +} + +function displaydir ($wdir) { +// $wdir == / or /a or /a/b/c/d etc + + global $basedir; + global $id; + global $USER, $CFG; + + $fullpath = $basedir.$wdir; + + $directory = opendir($fullpath); // Find all files + while ($file = readdir($directory)) { + if ($file == "." || $file == "..") { + continue; + } + + if (is_dir($fullpath."/".$file)) { + $dirlist[] = $file; + } else { + $filelist[] = $file; + } + } + closedir($directory); + + $strname = get_string("name"); + $strsize = get_string("size"); + $strmodified = get_string("modified"); + $straction = get_string("action"); + $strmakeafolder = get_string("makeafolder"); + $struploadafile = get_string("uploadafile"); + $strwithchosenfiles = get_string("withchosenfiles"); + $strmovetoanotherfolder = get_string("movetoanotherfolder"); + $strmovefilestohere = get_string("movefilestohere"); + $strdeletecompletely = get_string("deletecompletely"); + $strcreateziparchive = get_string("createziparchive"); + $strrename = get_string("rename"); + $stredit = get_string("edit"); + $strunzip = get_string("unzip"); + $strlist = get_string("list"); + $strchoose = get_string("choose"); + + + echo ""; + echo "
"; + } + + if (! $basedir = make_upload_directory("$course->id")) { + error("The site administrator needs to fix the file permissions"); + } + + $baseweb = $CFG->wwwroot; + +// End of configuration and access control + + + $regexp="\\.\\."; + if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) { + $message = "Error: Directories can not contain \"..\""; + $wdir = "/"; + $action = ""; + } + + if (!$wdir) { + $wdir="/"; + } + + + switch ($action) { + + case "upload": + html_header($course, $wdir); + + if (!empty($_FILES['userfile'])) { + $userfile = $_FILES['userfile']; + } else { + $save = false; + } + if (!empty($save)) { + if (!is_uploaded_file($userfile['tmp_name']) or $userfile['size'] == 0) { + notify(get_string("uploadnofilefound")); + } else { + $userfile_name = clean_filename($userfile['name']); + if ($userfile_name) { + $newfile = "$basedir$wdir/$userfile_name"; + if (move_uploaded_file($userfile['tmp_name'], $newfile)) { + chmod($newfile, 0666); + $a = NULL; + $a->file = "$userfile_name (".$userfile['type'].")"; + $a->directory = $wdir; + print_string("uploadedfileto", "", $a); + } else { + notify(get_string("uploadproblem", "", $userfile_name)); + } + } + } + displaydir($wdir); + + } else { + $upload_max_filesize = get_max_upload_file_size(); + $filesize = display_size($upload_max_filesize); + + $struploadafile = get_string("uploadafile"); + $struploadthisfile = get_string("uploadthisfile"); + $strmaxsize = get_string("maxsize", "", $filesize); + $strcancel = get_string("cancel"); + + echo "

$struploadafile ($strmaxsize) --> $wdir"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo " "; + echo ""; + echo ""; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } + html_footer(); + break; + + case "delete": + if (!empty($confirm)) { + html_header($course, $wdir); + foreach ($USER->filelist as $file) { + $fullfile = $basedir.$file; + if (! fulldelete($fullfile)) { + echo "
Error: Could not delete: $fullfile"; + } + } + clearfilelist(); + displaydir($wdir); + html_footer(); + + } else { + html_header($course, $wdir); + if (setfilelist($_POST)) { + echo "

".get_string("deletecheckwarning").":

"; + print_simple_box_start("center"); + printfilelist($USER->filelist); + print_simple_box_end(); + echo "
"; + notice_yesno (get_string("deletecheckfiles"), + "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=delete&confirm=1", + "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=cancel"); + } else { + displaydir($wdir); + } + html_footer(); + } + break; + + case "move": + html_header($course, $wdir); + if ($count = setfilelist($_POST)) { + $USER->fileop = $action; + $USER->filesource = $wdir; + echo "

"; + print_string("selectednowmove", "moodle", $count); + echo "

"; + } + displaydir($wdir); + html_footer(); + break; + + case "paste": + html_header($course, $wdir); + if (isset($USER->fileop) and $USER->fileop == "move") { + foreach ($USER->filelist as $file) { + $shortfile = basename($file); + $oldfile = $basedir.$file; + $newfile = $basedir.$wdir."/".$shortfile; + if (!rename($oldfile, $newfile)) { + echo "

Error: $shortfile not moved"; + } + } + } + clearfilelist(); + displaydir($wdir); + html_footer(); + break; + + case "rename": + if (!empty($name)) { + html_header($course, $wdir); + $name = clean_filename($name); + if (file_exists($basedir.$wdir."/".$name)) { + echo "Error: $name already exists!"; + } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) { + echo "Error: could not rename $oldname to $name"; + } + displaydir($wdir); + + } else { + $strrename = get_string("rename"); + $strcancel = get_string("cancel"); + $strrenamefileto = get_string("renamefileto", "moodle", $file); + html_header($course, $wdir, "form.name"); + echo "

$strrenamefileto:"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } + html_footer(); + break; + + case "mkdir": + if (!empty($name)) { + html_header($course, $wdir); + $name = clean_filename($name); + if (file_exists("$basedir$wdir/$name")) { + echo "Error: $name already exists!"; + } else if (! make_upload_directory("$course->id/$wdir/$name")) { + echo "Error: could not create $name"; + } + displaydir($wdir); + + } else { + $strcreate = get_string("create"); + $strcancel = get_string("cancel"); + $strcreatefolder = get_string("createfolder", "moodle", $wdir); + html_header($course, $wdir, "form.name"); + echo "

$strcreatefolder:"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } + html_footer(); + break; + + case "edit": + html_header($course, $wdir); + if (isset($text)) { + $fileptr = fopen($basedir.$file,"w"); + fputs($fileptr, stripslashes($text)); + fclose($fileptr); + displaydir($wdir); + + } else { + $streditfile = get_string("edit", "", "$file"); + $fileptr = fopen($basedir.$file, "r"); + $contents = fread($fileptr, filesize($basedir.$file)); + fclose($fileptr); + + if (mimeinfo("type", $file) == "text/html") { + if ($usehtmleditor = can_use_richtext_editor()) { + $onsubmit = "onsubmit=\"copyrichtext(document.form.text);\""; + } else { + $onsubmit = ""; + } + } else { + $usehtmleditor = false; + $onsubmit = ""; + } + + print_heading("$streditfile"); + + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents); + echo "
"; + echo " "; + echo ""; + echo ""; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + + if ($usehtmleditor) { + print_richedit_javascript("form", "text", "yes"); + } + + + } + html_footer(); + break; + + case "zip": + if (!empty($name)) { + html_header($course, $wdir); + $name = clean_filename($name); + if (empty($CFG->zip)) { // Use built-in php-based zip function + $files = array(); + foreach ($USER->filelist as $file) { + $files[] = "$basedir/$file"; + } + include_once('../pclzip/pclzip.lib.php'); + $archive = new PclZip("$basedir/$wdir/$name"); + if (($list = $archive->create($files,'',"$basedir/$wdir/")) == 0) { + error($archive->errorInfo(true)); + } + } else { // Use external zip program + $files = ""; + foreach ($USER->filelist as $file) { + $files .= basename($file); + $files .= " "; + } + $command = "cd $basedir/$wdir ; $CFG->zip -r $name $files"; + Exec($command); + } + clearfilelist(); + displaydir($wdir); + + } else { + html_header($course, $wdir, "form.name"); + + if (setfilelist($_POST)) { + echo "

".get_string("youareabouttocreatezip").":

"; + print_simple_box_start("center"); + printfilelist($USER->filelist); + print_simple_box_end(); + echo "
"; + echo "

".get_string("whattocallzip"); + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } else { + displaydir($wdir); + clearfilelist(); + } + } + html_footer(); + break; + + case "unzip": + html_header($course, $wdir); + if (!empty($file)) { + $strname = get_string("name"); + $strsize = get_string("size"); + $strmodified = get_string("modified"); + $strstatus = get_string("status"); + $strok = get_string("ok"); + $strunpacking = get_string("unpacking", "", $file); + + echo "

$strunpacking:

"; + + $file = basename($file); + + if (empty($CFG->unzip)) { // Use built-in php-based unzip function + include_once('../pclzip/pclzip.lib.php'); + $archive = new PclZip("$basedir/$wdir/$file"); + if (!$list = $archive->extract("$basedir/$wdir")) { + error($archive->errorInfo(true)); + } else { // print some output + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + foreach ($list as $item) { + echo ""; + $item['filename'] = str_replace("$basedir/$wdir/", "", $item['filename']); + print_cell("left", $item['filename']); + if (! $item['folder']) { + print_cell("right", display_size($item['size'])); + } else { + echo ""; + } + $filedate = userdate($item['mtime'], get_string("strftimedatetime")); + print_cell("right", $filedate); + print_cell("right", $item['status']); + echo ""; + } + echo "
$strname$strsize$strmodified$strstatus
 
"; + } + + } else { // Use external unzip program + print_simple_box_start("center"); + echo "
";
+                    $command = "cd $basedir/$wdir ; $CFG->unzip -o $file 2>&1";
+                    passthru($command);
+                    echo "
"; + print_simple_box_end(); + } + + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } else { + displaydir($wdir); + } + html_footer(); + break; + + case "listzip": + html_header($course, $wdir); + if (!empty($file)) { + $strname = get_string("name"); + $strsize = get_string("size"); + $strmodified = get_string("modified"); + $strok = get_string("ok"); + $strlistfiles = get_string("listfiles", "", $file); + + echo "

$strlistfiles:

"; + $file = basename($file); + + include_once('../lib/pclzip/pclzip.lib.php'); + $archive = new PclZip("$basedir/$wdir/$file"); + if (!$list = $archive->listContent("$basedir/$wdir")) { + notify($archive->errorInfo(true)); + + } else { + echo ""; + echo ""; + foreach ($list as $item) { + echo ""; + print_cell("left", $item['filename']); + if (! $item['folder']) { + print_cell("right", display_size($item['size'])); + } else { + echo ""; + } + $filedate = userdate($item['mtime'], get_string("strftimedatetime")); + print_cell("right", $filedate); + echo ""; + } + echo "
$strname$strsize$strmodified
 
"; + } + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + } else { + displaydir($wdir); + } + html_footer(); + break; + + case "torte": + if($_POST) + { + while(list($key, $val) = each($_POST)) + { + if(ereg("file([0-9]+)", $key, $regs)) + { + $file = $val; + } + } + if(@filetype($CFG->dataroot ."/". $course->id . $file) == "file") + { + if(mimeinfo("icon", $file) == "image.gif") + { + $url = $CFG->wwwroot ."/file.php?file=/" .$course->id . $file; + runjavascript($url); + } + else + { + print "File is not a image!"; + } + } + else + { + print "You cannot insert FOLDER into richtext editor!!!"; + } + } + break; + case "cancel"; + clearfilelist(); + + default: + html_header($course, $wdir); + displaydir($wdir); + html_footer(); + break; +} + + +/// FILE FUNCTIONS /////////////////////////////////////////////////////////// + + +function fulldelete($location) { + if (is_dir($location)) { + $currdir = opendir($location); + while ($file = readdir($currdir)) { + if ($file <> ".." && $file <> ".") { + $fullfile = $location."/".$file; + if (is_dir($fullfile)) { + if (!fulldelete($fullfile)) { + return false; + } + } else { + if (!unlink($fullfile)) { + return false; + } + } + } + } + closedir($currdir); + if (! rmdir($location)) { + return false; + } + + } else { + if (!unlink($location)) { + return false; + } + } + return true; +} + + + +function setfilelist($VARS) { + global $USER; + + $USER->filelist = array (); + $USER->fileop = ""; + + $count = 0; + foreach ($VARS as $key => $val) { + if (substr($key,0,4) == "file") { + $count++; + $USER->filelist[] = rawurldecode($val); + } + } + return $count; +} + +function clearfilelist() { + global $USER; + + $USER->filelist = array (); + $USER->fileop = ""; +} + + +function printfilelist($filelist) { + global $basedir, $CFG; + + foreach ($filelist as $file) { + if (is_dir($basedir.$file)) { + echo "wwwroot/files/pix/folder.gif\" HEIGHT=16 WIDTH=16> $file
"; + $subfilelist = array(); + $currdir = opendir($basedir.$file); + while ($subfile = readdir($currdir)) { + if ($subfile <> ".." && $subfile <> ".") { + $subfilelist[] = $file."/".$subfile; + } + } + printfilelist($subfilelist); + + } else { + $icon = mimeinfo("icon", $file); + echo "wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16> $file
"; + } + } +} + + +function print_cell($alignment="center", $text=" ") { + echo "
"; + echo ""; + echo "$text"; + echo ""; + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "\n"; + + if ($wdir == "/") { + $wdir = ""; + } + + $count = 0; + + if (!empty($dirlist)) { + asort($dirlist); + foreach ($dirlist as $dir) { + + $count++; + + $filename = $fullpath."/".$dir; + $fileurl = rawurlencode($wdir."/".$dir); + $filesafe = rawurlencode($dir); + $filedate = userdate(filectime($filename), "%d %b %Y, %I:%M %p"); + + echo ""; + + print_cell("center", ""); + print_cell("left", "wwwroot/files/pix/folder.gif\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"Folder\">".htmlspecialchars($dir).""); + print_cell("right", "-"); + print_cell("right", $filedate); + print_cell("right", "$strrename"); + + echo ""; + } + } + + + if (!empty($filelist)) { + asort($filelist); + foreach ($filelist as $file) { + + $icon = mimeinfo("icon", $file); + + $count++; + $filename = "$fullpath/$file"; + $fileurl = "$wdir/$file"; + $filesafe = rawurlencode($file); + $fileurlsafe = rawurlencode($fileurl); + $filedate = userdate(filectime($filename), "%d %b %Y, %I:%M %p"); + + if (substr($fileurl,0,1) == '/') { + $selectfile = substr($fileurl,1); + } else { + $selectfile = $fileurl; + } + if ($CFG->slasharguments) { + $ffurl = "/file.php/$id$fileurl"; + } else { + $ffurl = "/file.php?file=/$id$fileurl"; + } + + echo ""; + + print_cell("center", ""); + + echo ""; + + $file_size = filesize($filename); + print_cell("right", display_size($file_size)); + print_cell("right", $filedate); + + $edittext = "$strchoose "; + + if ($icon == "text.gif" || $icon == "html.gif") { + $edittext .= "$stredit"; + } else if ($icon == "zip.gif") { + $edittext .= "$strunzip "; + $edittext .= "$strlist "; + } + + print_cell("right", "$edittext $strrename"); + + echo ""; + } + } + echo "
$strname$strsize$strmodified$straction
"; + link_to_popup_window ($ffurl, "display", + "wwwroot/files/pix/$icon\" height=16 width=16 border=0 alt=\"file\">", + 480, 640); + echo ""; + link_to_popup_window ($ffurl, "display", htmlspecialchars($file), 480, 640); + echo "
"; + echo "
"; + + if (empty($wdir)) { + $wdir = "/"; + } + + echo ""; + echo ""; + echo ""; + echo "
"; + echo ""; + echo " "; + $options = array ( + "move" => "$strmovetoanotherfolder", + "delete" => "$strdeletecompletely", + "zip" => "$strcreateziparchive" + ); + if (!empty($count)) { + choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:document.dirform.submit()"); + } + + echo ""; + echo ""; + if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) { + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + } + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + +} + +?> diff --git a/mod/resource/details.php b/mod/resource/details.php index a134fce1d2..c88c921ff4 100644 --- a/mod/resource/details.php +++ b/mod/resource/details.php @@ -207,61 +207,143 @@ $optionname = ""; + $jsoption[] = "\"$optionname\""; + } + $alljsoptions = implode(",", $jsoption); + + if ($form->instance) { // Re-editing + if (!$form->alltext) { + $newwindow = ""; + $window->resizable = "checked"; // Defaults + $window->scrollbars = "checked"; + $window->status = "checked"; + $window->location = "checked"; + $window->width = 620; + $window->height = 450; + } else { + $newwindow = "checked"; + $rawoptions = explode(',', $form->alltext); + foreach ($rawoptions as $rawoption) { + $option = explode('=', trim($rawoption)); + $optionname = $option[0]; + $optionvalue = $option[1]; + if ($optionname == "height" or $optionname == "width") { + $window->$optionname = $optionvalue; + } else if ($optionvalue) { + $window->$optionname = "checked"; + } + } + } + } else { + $newwindow = "checked"; + $window->resizable = "checked"; + $window->scrollbars = "checked"; + $window->status = "checked"; + $window->location = "checked"; + $window->width = 620; + $window->height = 450; + } + + echo $alloptions; + ?> + -

:

+

:

- + reference\"> "; + echo ""; + link_to_popup_window ("/mod/resource/coursefiles.php?id=$course->id", + "coursefiles", $strchooseafile, 500, 750, $strchooseafile); + echo ""; + ?> -   + +

-

+ + + + onclick="return lockoptions('theform','newwindow', subitems)"> + + +

wwwroot/files/index.php?id=$course->id"); + + case PROGRAM: + $strexampleurl = get_string("exampleurl", "resource"); ?> -

:

+

:

- dataroot."/".$course->id; - $coursedirs = get_directory_list($rootdir, $CFG->moddata); - foreach ($coursedirs as $dir) { - $options["$dir"] = $dir; - } - choose_from_menu ($options, "reference", $form->reference); - ?> + - -

:

+   -

-

+

-- 2.39.5