<?PHP  // $Id: devoir2zip.php ,v 1.1 2008-04-17 13:11:00 ppollet Exp $


/*
script to collect all uploaded files for an assignement and offer a link to download it
as requested in  MDL-14384

MUST DO  in lib/pclzip/pclzip.lib.php 
 as described in https://www.sugarcrm.com/forums/printthread.php?t=11901&page=3&pp=10
 define( 'PCLZIP_TEMPORARY_DIR', "$CFG->dataroot/temp/" );

MUST DO in mod/assignment/lib.php
 add these lines 

//extra link to collect a zip of all uploaded files 
$button= link_to_popup_window ('/mod/assignment/devoir2zip.php?id='.
                               $this->cm->id.'&amp;group='.$currentgroup,
                               '',get_string('assignment:collectaszip','assignment'),400,500,'','none',true);
echo  '<p align="center">'.$button.get_string('assignment:collectaszipgroupwarning','assignment').'</p>';

just before this (near line 12001) 
        /// Print quickgrade form around the table
        if ($quickgrade){



these strings should be added to lang/fr_utf8/assignment.php

$string['assignment:collectaszip']='télécharger un zip des devoirs';
$string['assignment:collectaszipgroupwarning']=' (n\'oubliez pas de choisir d\'abord le groupe si nécessaire!)';
$string['assignment:downloadzip']='télécharger cette archive';
$string['assignment:creatingzip']='Création de l \'archive pour ';
$string['assignment:zipaddedfiles']=' fichiers ajoutés à ';


these strings should be added to lang/en_utf8/assignment.php

$string['assignment:collectaszip']='collect submissions as a zip file';
$string['assignment:collectaszipgroupwarning']=' (d\'ont forget to select a group first, if needed)';
$string['assignment:downloadzip']='download the zip file';
$string['assignment:creatingzip']='Creating zip file for ';
$string['assignment:zipaddedfiles']=' files added to ';

*/

require_once("../../config.php");

require_login();

$id = optional_param('id', 0, PARAM_INT);  // Course Module ID
$a  = optional_param('a', 0, PARAM_INT);   // Assignment ID
$g  = optional_param('group',0,PARAM_INT); // group ID
$f  = optional_param('file','',PARAM_FILE); // filename to download

if ($id) {
	if (! $cm = get_coursemodule_from_id('assignment', $id)) {
		error("Course Module ID was incorrect");
	}
	
	if (! $assignment = get_record("assignment", "id", $cm->instance)) {
		error("assignment ID was incorrect");
	}
	
	if (! $course = get_record("course", "id", $assignment->course)) {
		error("Course is misconfigured");
	}
} else {
	if (!$assignment = get_record("assignment", "id", $a)) {
		error("Course module is incorrect");
	}
	if (! $course = get_record("course", "id", $assignment->course)) {
		error("Course is misconfigured");
	}
	if (! $cm = get_coursemodule_from_instance("assignment", $assignment->id, $course->id)) {
		error("Course Module ID was incorrect");
	}
}
if ($g) {
	if (!$groupe=get_record("groups","id",$g))
		error ("Group id $g was incorrect");
}

// check capability when creating the zip file or downloading it 
require_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $cm->id));

// where is to be located the zip file
$tmppath=$CFG->dataroot."/temp/";

if ($f) {
	// disable moodle specific debug messages
	disable_debugging();
	$pathname=$tmppath.$f;
	if (file_exists($pathname) and !is_dir($pathname)) {
		require_once($CFG->libdir.'/filelib.php');
		send_file($pathname, $f);
		close_window();  // does not work ?
		exit;
	}
}


require_once ("$CFG->dirroot/mod/assignment/lib.php");
require_once("$CFG->dirroot/lib/pclzip/pclzip.lib.php");


//IE needs proper header with encoding
print_header(get_string('feedback', 'assignment').':'.format_string($assignment->name));
print_heading(get_string('assignment:creatingzip','assignment').format_string($assignment->name));


require ("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
$assignmentclass = "assignment_$assignment->assignmenttype";
$assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course);


$rendus=  assignment_get_all_submissions($assignment);


print "<br/>".count($rendus)." ".get_string('uploadedfiles', 'assignment')."<br/>";


$ficname="devoir".$assignment->id;
if ($g) {
	$ficname .="_g".$groupe->name.".zip";
} else {
	$ficname .="_all.zip";
}
@unlink($tmppath.$ficname);
$archive=new PclZip ($tmppath.$ficname);
$cnt=0;
foreach ($rendus as $rendu) {
	if ($g && !ismember($g,$rendu->userid)) 
		continue;
	$filearea = $assignmentinstance->file_area_name($rendu->userid);
	if ($basedir = $assignmentinstance->file_area($rendu->userid)) {
		if ($files = get_directory_list($basedir)) {  
			foreach ($files as $key => $file) {
				print("$CFG->dataroot/$filearea/$file<br/>");
				$s=$archive->add("$CFG->dataroot/$filearea/$file",PCLZIP_OPT_REMOVE_PATH, "$CFG->dataroot/$filearea");	
				if ($s == 0) {
					die("Error : ".$archive->errorInfo(true));
				}
				$cnt++;
			} 
		}
	}
}
print $cnt.get_string('assignment:zipaddedfiles','assignment').$ficname."<br/>";
//print $archive->errorName()."<br/>";

if ($cnt) {
	echo '<div class="buttons" align="center">';
	print "<a href='devoir2zip.php?id=".$cm->id."&file=".$ficname."'>".get_string('assignment:downloadzip','assignment')."</a>";
	echo '</div>';
} else 
	print get_string('noattempts', 'assignment');
// End of page.
close_window_button();


?>

