From 202abc006926364f570f39f0121e08531185a956 Mon Sep 17 00:00:00 2001 From: Cristian Fasanando Date: Wed, 10 Dec 2008 18:37:31 +0100 Subject: [PATCH] [svn r17211] implemented allow ZIP export of assignments for teacher and tutor (see FS#3229) --- documentation/changelog.html | 4 +- main/work/download.php | 80 +++++++++++++++++ main/work/downloadfolder.inc.php | 150 +++++++++++++++++++++++++++++++ main/work/work.lib.php | 33 ++++++- main/work/work.php | 24 ++--- 5 files changed, 275 insertions(+), 16 deletions(-) create mode 100644 main/work/download.php create mode 100755 main/work/downloadfolder.inc.php diff --git a/documentation/changelog.html b/documentation/changelog.html index 4fbba9a4f5..4d93a2f43f 100644 --- a/documentation/changelog.html +++ b/documentation/changelog.html @@ -47,10 +47,12 @@
  • New glossary tool (FS#3248)
  • Integrated the gradebook at course level (FS#3173)
  • New wiki tool (considerably improved, built on CoolWiki plugin) (FS#2873)
  • -
  • Remove possibility to delete system directories in documents tool (FS#1522)
  • +
  • Remove possibility to delete system directories in documents tool (FS#1522)
  • +
  • Options to order glossary terms (FS#3293)
  • Database server: There is no more need special SQL modes for MySQL 5.0 to be turned off through the "sql_mode" setting. The Dokeos system does it internally (FS#2787)
  • Some low-level functions that intensively use the language translation sub-system have been optimized for speed (FS#3260)
  • Documents tool: The document-type icons have been made clickable (FS#3296)
  • +
  • Allow ZIP export of assignments for teacher and tutor

  • Debugging

    diff --git a/main/work/download.php b/main/work/download.php new file mode 100644 index 0000000000..75a20efea8 --- /dev/null +++ b/main/work/download.php @@ -0,0 +1,80 @@ + \ No newline at end of file diff --git a/main/work/downloadfolder.inc.php b/main/work/downloadfolder.inc.php new file mode 100755 index 0000000000..dea925b9f7 --- /dev/null +++ b/main/work/downloadfolder.inc.php @@ -0,0 +1,150 @@ + 4) { + unlink("$temp_zip_dir/$file"); + } + } + } + closedir($handle); +} + +//create zipfile of given directory +$temp_zip_file = $temp_zip_dir."/".md5(time()).".zip"; +$zip_folder= new PclZip($temp_zip_file); +$tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION); +$prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY); +//Put the files in the zip +//2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones) +//normal users get only visible files that are in visible folders + +//admins are allowed to download invisible files +if (is_allowed_to_edit()) { + //folder we want to zip --> no longer used, deleted files are included too like this + //$what_to_zip = $sys_course_path.$_course['path']."/document".$path; + //creation of the zipped folder + //$zip_folder->create($what_to_zip ,PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir ); + //set the path that will be used in the query + if($path=='/') { + $querypath=''; // to prevent ...path LIKE '//%'... in query + } else { + $querypath=$path; + } + //search for all files that are not deleted => visibility != 2 + $query = api_sql_query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'2'",__FILE__,__LINE__); + //add tem to the zip file + while ($not_deleted_file = mysql_fetch_assoc($query)) { //var_dump($sys_course_path.$_course['path']."/".$not_deleted_file['url']);exit(); + $zip_folder->add($sys_course_path.$_course['path']."/".$not_deleted_file['url'],PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/work".$remove_dir); + } +} + +//for other users, we need to create a zipfile with only visible files and folders +else +{ + if ($path=='/') { + $querypath=''; // to prevent ...path LIKE '//%'... in query + } else { + $querypath=$path; + } + //big problem: visible files that are in a hidden folder are included when we do a query for visiblity='v'!!! + //so... I do it in a couple of steps: + //1st: get all files that are visible in the given path + $query = api_sql_query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility='1' ",__FILE__,__LINE__); + //add them to an array + $all_visible_files_path = array(); + while ($all_visible_files = mysql_fetch_assoc($query)) { + $all_visible_files_path[] = $all_visible_files['url']; + } + //2nd: get all folders that are invisible in the given path + $query2 = api_sql_query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$querypath."/%' AND work.filetype='file' AND props.visibility<>'1'",__FILE__,__LINE__); + //if we get invisible folders, we have to filter out these results from all visible files we found + + if (Database::num_rows($query2)>0) { + //add tem to an array + while ($invisible_folders = mysql_fetch_assoc($query2)) { + //3rd: get all files that are in the found invisible folder (these are "invisible" too) + $query3 = api_sql_query("SELECT url FROM $tbl_student_publication AS work,$prop_table AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work".$invisible_folders['path']."/%' AND work.filetype='file' AND props.visibility='1'",__FILE__,__LINE__); + //add tem to an array + while ($files_in_invisible_folder = mysql_fetch_assoc($query3)) { + $files_in_invisible_folder_path[] = $files_in_invisible_folder['url']; + } + } + //compare the array with visible files and the array with files in invisible folders + //and keep the difference (= all visible files that are not in an invisible folder) + $files_for_zipfile = diff((array) $all_visible_files_path,(array) $files_in_invisible_folder_path); + } else { + //no invisible folders found, so all visible files can be added to the zipfile + $files_for_zipfile = $all_visible_files_path; + } + //add all files in our final array to the zipfile + for ($i=0;$iadd($sys_course_path.$_course['path']."/".$files_for_zipfile[$i],PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/work".$remove_dir); + } +}//end for other users +//logging +// launch event +event_download(basename($path).'.zip (folder)'); + +//start download of created file +$name = basename($path).'.zip'; +DocumentManager::file_send_for_download($temp_zip_file,true,$name); +exit; + +/** +============================================================================== +* Extra function (only used here) +============================================================================== +*/ + +/** + * Return the difference between two arrays, as an array of those key/values + * Use this as array_diff doesn't give the + * + * @param array $arr1 first array + * @param array $arr2 second array + * @return difference between the two arrays + */ +function diff($arr1,$arr2) { + $res = array(); $r=0; + foreach ($arr1 as $av) { + if (!in_array($av,$arr2)){ + $res[$r]=$av; $r++; + } + } + return $res; +} \ No newline at end of file diff --git a/main/work/work.lib.php b/main/work/work.lib.php index 5b76130058..4556ece62c 100644 --- a/main/work/work.lib.php +++ b/main/work/work.lib.php @@ -559,7 +559,13 @@ function display_student_publications_list($work_dir,$sub_course_dir,$currentCou } else { $add_to_name = ''; } - $row[] = ''.$dir.''.$add_to_name.'
    '.$cant_files.' '.$text_file.$dirtext; + + $show_as_icon = get_work_id($mydir); //true or false + if ($show_as_icon && api_is_allowed_to_edit()) { + $row[] = ''.get_lang('Save').''.$dir.''.$add_to_name.'
    '.$cant_files.' '.$text_file.$dirtext; + } else { + $row[] = ''.$dir.''.$add_to_name.'
    '.$cant_files.' '.$text_file.$dirtext; + } } if ($count_files!=0) { @@ -618,9 +624,11 @@ function display_student_publications_list($work_dir,$sub_course_dir,$currentCou } endif; - $url = implode("/", array_map("rawurlencode", explode("/", $work->url))); + $url = implode("/", array_map("rawurlencode", explode("/", $work->url))); + + //$full_file_name = 'download.php?file='.$realname; $row[]= build_document_icon_tag('file',$work->url); - $row[]= ''.get_lang('Save').''.$work->title.'
    '.$work->description; + $row[]= ''.get_lang('Save').''.$work->title.'
    '.$work->description; $row[]= display_user_link($row2['insert_user_id'],$work->author).$qualification_string;// $work->author; $row[]= date_to_str_ago($work->sent_date).$add_string.'
    '.$work->sent_date.''; @@ -1229,4 +1237,21 @@ function to_javascript_work() { } '; -} \ No newline at end of file +} +/** + * Gets the id of a student publication with a given path + * @param string $path + * @return true if is found / false if not found + */ +function get_work_id($path) { + $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION); + $sql = "SELECT id FROM $TBL_STUDENT_PUBLICATION WHERE url LIKE 'work/".$path."%'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + $num_rows = Database::num_rows($result); + + if ($result && $num_rows > 0) { + return true; + } else { + return false; + } +} \ No newline at end of file diff --git a/main/work/work.php b/main/work/work.php index 6a819c99da..15cb4ed90a 100644 --- a/main/work/work.php +++ b/main/work/work.php @@ -27,7 +27,7 @@ * @author Patrick Cool , Ghent University - ability for course admins to specify wether uploaded documents are visible or invisible by default. * @author Roan Embrechts, code refactoring and virtual course support * @author Frederic Vauthier, directories management -* @version $Id: work.php 17192 2008-12-09 23:15:45Z yannoo $ +* @version $Id: work.php 17211 2008-12-10 17:37:31Z cfasanando $ * * @todo refactor more code into functions, use quickforms, coding standards, ... */ @@ -89,7 +89,7 @@ $language_file = array ( 'document', 'admin' ); - +require("../inc/global.inc.php"); // @todo why is this needed? //session if (isset ($_GET['id_session'])) { @@ -101,7 +101,6 @@ isset($_SESSION['id_session'])?$id_session=$_SESSION['id_session']:$id_session=n Including necessary files ----------------------------------------------------------- */ -require_once '../inc/global.inc.php'; require_once 'work.lib.php'; require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php'); require_once (api_get_path(LIBRARY_PATH) . 'debug.lib.inc.php'); @@ -244,6 +243,16 @@ if (!api_is_course_admin()) { api_session_register('toolgroup'); } } + +//-------------------------------------------------------------------// + +//download of an completed folder +if(isset($_GET['action']) && $_GET['action']=="downloadfolder") +{ + include('downloadfolder.inc.php'); +} +//-------------------------------------------------------------------// + /* ----------------------------------------------------------- Header @@ -308,14 +317,7 @@ $is_allowed_to_edit = api_is_allowed_to_edit(false,true); //has to come after di ============================================================================== */ -//-------------------------------------------------------------------// -//download of an completed folder -if(isset($_GET['action']) && $_GET['action']=="downloadfolder") -{ - include('downloadfolder.inc.php'); -} -//-------------------------------------------------------------------// if (!empty ($_POST['changeProperties'])) { $query = "UPDATE " . $main_course_table . " SET show_score='" . $uploadvisibledisabled . "' WHERE code='" . $_course['sysCode'] . "'"; @@ -1141,7 +1143,7 @@ function draw_date_picker($prefix,$default='') { //new additional fields inside the "if condition" just to agroup if(true): - $addtext .= '
    '.get_lang('Description').'
    '; + $addtext = '
    '.get_lang('Description').'
    '; $addtext .= '
     
    ';