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

  require_login();


  // disable moodle specific debug messages
    disable_debugging();
    $tmppath=$CFG->dataroot."/temp/";
    $relativepath = get_file_argument('devoir2zip.php'); 
    $args = explode('/', trim($relativepath, '/'));

    if (count($args)==1)  {
      $pathname=$tmppath.$args[0];
    if (file_exists($pathname) and !is_dir($pathname)) {
         require_once($CFG->libdir.'/filelib.php');

      send_file($pathname, $args[0]);
      close_window();  //marche pas ????
      exit;
    }
    }



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


//POUR QUE ca fonctionne j'ai du définir ca dans lib/pclzip/pclzip.lib.php ! 
// https://www.sugarcrm.com/forums/printthread.php?t=11901&page=3&pp=10
//  define( 'PCLZIP_TEMPORARY_DIR', '/tmp/' );


    $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

//print $relativepath;
//print_r ($args);

//$a=47;
//$g=5258 ; // g 201;
    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");
    }

//IE needs proper header with encoding
     print_header(get_string('feedback', 'assignment').':'.format_string($assignment->name));
     print_heading('Création de l \'archive pour '.format_string($assignment->name));


   require_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $cm->id));


//print_r($course);
//print"<br/>";
//print_r($assignment);
//print"<br/>";

//print_r($cm);
//print"<br/>";


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_r($rendus);

print "<br/>".count($rendus)."<br/>";

/*
foreach ($rendus as $rendu) {
  print $rendu->userid." ".$assignmentinstance->print_student_answer($rendu->userid)."<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 fichiers ajoutés à $ficname<br/>";
print $archive->errorName()."<br/>";

if ($cnt) {
  echo '<div class="buttons" align="center">';
  print "<a href='devoir2zip.php/".$ficname."'>Télécharger cette archive </a>";
  echo '</div>';
} else 
  print "aucun devoir rendu";
// End of page.
close_window_button();

//close_window();

?>

