From 677cbedbbb3d4bb4a483369c47d3f1028d73dc94 Mon Sep 17 00:00:00 2001
From: Astor Bizard <astor.bizard@grenoble-inp.fr>
Date: Mon, 17 Feb 2020 12:00:20 +0100
Subject: [PATCH] Fixed previous submission download.

---
 views/downloadsubmission.php | 98 +++++++++++++-----------------------
 vpl.class.php                | 86 ++++++++++++++++---------------
 2 files changed, 81 insertions(+), 103 deletions(-)

diff --git a/views/downloadsubmission.php b/views/downloadsubmission.php
index a1aa6658..b885abaf 100644
--- a/views/downloadsubmission.php
+++ b/views/downloadsubmission.php
@@ -30,69 +30,41 @@ require_once(dirname(__FILE__).'/../locallib.php');
 require_once(dirname(__FILE__).'/../vpl.class.php');
 require_once(dirname(__FILE__).'/../vpl_submission.class.php');
 
-global $CFG, $USER;
-try {
-    require_login();
-    $id = required_param( 'id', PARAM_INT );
-    $vpl = new mod_vpl( $id );
-    $userid = optional_param( 'userid', false, PARAM_INT );
-    $submissionid = optional_param( 'submissionid', false, PARAM_INT );
-    if (! $vpl->has_capability( VPL_GRADE_CAPABILITY )) {
-        $userid = false;
-        $submissionid = false;
-    }
-    // Read record.
-    if ($userid && $userid != $USER->id) {
-        // Grader.
-        $vpl->require_capability( VPL_GRADE_CAPABILITY );
-        $grader = true;
-        if ($submissionid) {
-            $subinstance = $DB->get_record( 'vpl_submissions', array (
-                    'id' => $submissionid
-            ) );
-        } else {
-            $subinstance = $vpl->last_user_submission( $userid );
-        }
-    } else {
-        // Download own submission.
-        $vpl->require_capability( VPL_VIEW_CAPABILITY );
-        $userid = $USER->id;
-        $grader = false;
-        if ($submissionid && $vpl->has_capability( VPL_GRADE_CAPABILITY )) {
-            $subinstance = $DB->get_record( 'vpl_submissions', array (
-                    'id' => $submissionid
-            ) );
-        } else {
-            $subinstance = $vpl->last_user_submission( $userid );
-        }
-        $vpl->restrictions_check();
-    }
-
-    // Check consistence.
-    if (! $subinstance) {
-        throw new Exception( get_string( 'nosubmission', VPL ) );
-    }
-    if ($subinstance->vpl != $vpl->get_instance()->id) {
-        throw new Exception( get_string( 'invalidcourseid' ) );
-    }
-    $submissionid = $subinstance->id;
+global $DB, $USER;
+require_login();
+$id = required_param( 'id', PARAM_INT );
+$userid = optional_param( 'userid', $USER->id, PARAM_INT );
+$submissionid = optional_param( 'submissionid', false, PARAM_INT );
+$vpl = new mod_vpl( $id );
+if (!$vpl->has_capability( VPL_GRADE_CAPABILITY )) {
+    // Download own submission.
+    $vpl->require_capability( VPL_VIEW_CAPABILITY );
+    $userid = $USER->id;
+    $vpl->restrictions_check();
+}
 
-    if ($vpl->is_inconsistent_user( $subinstance->userid, $userid )) {
-        throw new Exception( 'vpl submission user inconsistence' );
-    }
-    if ($vpl->get_instance()->id != $subinstance->vpl) {
-        throw new Exception( 'vpl submission vpl inconsistence' );
-    }
-    $submission = new mod_vpl_submission( $vpl, $subinstance );
-    $plugincfg = get_config('mod_vpl');
-    $watermark = isset( $plugincfg->use_watermarks ) && $plugincfg->use_watermarks;
-    $fgm = $submission->get_submitted_fgm();
-    $fgm->download_files( $vpl->get_printable_name() , $watermark);
-} catch ( Exception $e ) {
-    $vpl->prepare_page( 'views/downloadsubmission.php', array (
-            'id' => $id
+// Read record.
+if ($submissionid) {
+    $subinstance = $DB->get_record( 'vpl_submissions', array (
+            'id' => $submissionid
     ) );
-    $vpl->print_header( get_string( 'download', VPL ) );
-    print_error($e->getMessage());
-    $vpl->print_footer();
+} else {
+    $subinstance = $vpl->last_user_submission( $userid );
+}
+
+// Check consistence.
+if (! $subinstance) {
+    throw new moodle_exception( 'nosubmission', VPL );
+}
+if ($subinstance->vpl != $vpl->get_instance()->id) {
+    throw new moodle_exception( 'vpl submission vpl inconsistence' );
+}
+if ($vpl->is_inconsistent_user( $subinstance->userid, $userid )) {
+    throw new moodle_exception( 'nopermissions', null, null, get_string('download') );
 }
+$submission = new mod_vpl_submission( $vpl, $subinstance );
+$plugincfg = get_config('mod_vpl');
+$watermark = isset( $plugincfg->use_watermarks ) && $plugincfg->use_watermarks;
+$fgm = $submission->get_submitted_fgm();
+$fgm->download_files( $vpl->get_printable_name(), $watermark );
+die();
\ No newline at end of file
diff --git a/vpl.class.php b/vpl.class.php
index 2ed35914..50e30c44 100644
--- a/vpl.class.php
+++ b/vpl.class.php
@@ -140,9 +140,9 @@ class mod_vpl {
     protected $instance;
 
     /**
-     * Internal var array of object to  file group manager 
+     * Internal var array of object to  file group manager
      *
-     * @var araay of object of file group manager 
+     * @var araay of object of file group manager
      */
     protected $requiredfgm;
 
@@ -198,7 +198,7 @@ class mod_vpl {
                 $this->instance->cmidnumber = $this->cm->id;
             }
         }
-        $this->fgm = array(); 
+        $this->fgm = array();
     }
 
     /**
@@ -286,55 +286,55 @@ class mod_vpl {
 
     /**
      *
-     * @param $type string 
-     * @return directory to stored initial files 
+     * @param $type string
+     * @return directory to stored initial files
      */
-    public function get_files_directory($type) { 
-        return $this->get_data_directory() . '/'.$type.'_files/'; 
-    } 
+    public function get_files_directory($type) {
+        return $this->get_data_directory() . '/'.$type.'_files/';
+    }
 
     /**
      * Get path to filename to store required files
      * @return string path to filename to store required files
      */
-       public function get_files_filename($type) { 
-        return $this->get_data_directory() . '/'.$type.'_files.lst'; 
-    } 
+    public function get_files_filename($type) {
+        return $this->get_data_directory() . '/'.$type.'_files.lst';
+    }
 
     /**
      * Get array of files required file names
      * @return array of strings
      */
-    public function get_files($type) { 
-        return  file_group_process::read_list( $this->get_files_filename($type) ); 
-    } 
+    public function get_files($type) {
+        return file_group_process::read_list( $this->get_files_filename($type) );
+    }
 
     /**
      *
      * @param $files array
      *            of required files
      */
-       public function set_files($type,$files) { 
-         file_group_process::write_list( $this->get_files_filename($type), $files ); 
-    } 
+    public function set_files($type,$files) {
+         file_group_process::write_list( $this->get_files_filename($type), $files );
+    }
 
     /**
      *
      * @return object file group manager for required files
      */
-     public function get_fgm($type) { 
-        if (! isset($this->fgm[$type])) { 
-            if ($type=='execution'){ 
-            $this->fgm[$type] = new file_group_execution( $this->get_files_filename($type) 
-                                                       , $this->get_files_directory($type) ); 
-            }else{ 
-            $this->fgm[$type] = new file_group_process( $this->get_files_filename($type) 
-                                                       , $this->get_files_directory($type) 
-                                                       , $this->instance->maxfiles ); 
-            } 
-        } 
-        return $this->fgm[$type]; 
-    } 
+    public function get_fgm($type) {
+        if (! isset($this->fgm[$type])) {
+            if ($type=='execution') {
+            $this->fgm[$type] = new file_group_execution( $this->get_files_filename($type)
+                                                       , $this->get_files_directory($type) );
+            } else {
+            $this->fgm[$type] = new file_group_process( $this->get_files_filename($type)
+                                                       , $this->get_files_directory($type)
+                                                       , $this->instance->maxfiles );
+            }
+        }
+        return $this->fgm[$type];
+    }
 
     // FIXME check and remove function.
     public function set_initial_file($name, $files) {
@@ -483,8 +483,8 @@ class mod_vpl {
     function is_use_as_base(){
         return ($this->instance->usableasbase?true:false);
     }
- 
- 
+
+
     /**
      * Check if pass password restriction
      */
@@ -643,7 +643,7 @@ class mod_vpl {
      */
     public function pass_submission_restriction(& $alldata, & $error) {
         $max = $this->get_maxfilesize();
-        $rfn = $this->get_fgm('required'); 
+        $rfn = $this->get_fgm('required');
         $list = $rfn->getFilelist();
         $error = '';
         if (count( $alldata ) > $this->instance->maxfiles) {
@@ -1175,13 +1175,19 @@ class mod_vpl {
     }
 
     /**
-     * Return if is group activity
+     * Check for user consistency.
+     * This checks that both users are the same, or if this is a group activity, that they belong to the same group.
      *
      * @return bool
      */
     public function is_inconsistent_user($current, $real) {
-        if ($this->is_group_Activity()) {
-            return false;
+        if ($this->is_group_activity()) {
+            $g1 = $this->get_usergroup($current);
+            $g2 = $this->get_usergroup($real);
+            if ($g1 === false || $g2 === false) {
+                return $current != $real;
+            }
+            return $g1->id != $g2->id;
         } else {
             return $current != $real;
         }
@@ -1731,7 +1737,7 @@ class mod_vpl {
      */
     public function print_submission_restriction() {
         global $CFG, $USER;
-        $filegroup = $this->get_fgm('required'); 
+        $filegroup = $this->get_fgm('required');
         $files = $filegroup->getfilelist();
         if (count( $files )) {
             $text = '';
@@ -1744,12 +1750,12 @@ class mod_vpl {
                 $needcomma = true;
             }
             $link = ' (<a href="';
-           // $link .= vpl_mod_href( 'views/downloadfiles.php', 'id', $this->get_course_module()->id ,'type','required'); 
-            $link .= vpl_mod_href( 'views/downloadrequiredfiles.php', 'id', $this->get_course_module()->id ); 
+           // $link .= vpl_mod_href( 'views/downloadfiles.php', 'id', $this->get_course_module()->id ,'type','required');
+            $link .= vpl_mod_href( 'views/downloadrequiredfiles.php', 'id', $this->get_course_module()->id );
             $link .= '">';
             $link .= get_string( 'download', VPL );
             $link .= '</a>)';
-            $this->print_restriction( 'requiredfiles', $text . $link ); 
+            $this->print_restriction( 'requiredfiles', $text . $link );
         }
         $instance = $this->get_instance();
         if (count( $files ) != $instance->maxfiles) {
-- 
GitLab