MDL-37030 Assignment: Fix error on download all submissions.
authorDamyon Wiese <damyon@moodle.com>
Tue, 18 Dec 2012 03:34:26 +0000 (11:34 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 18 Dec 2012 04:17:25 +0000 (12:17 +0800)
When group submissions and blind marking are enabled, an error is thrown
when a teacher trys to download all submissions in a zip. The fix is to
move the url rewriting from the plugin to the assign class so it is
done in a standard way by all modules. The rewriting is done to make images
in a text editor field resolve correctly.

mod/assign/assignmentplugin.php
mod/assign/feedback/file/importziplib.php
mod/assign/locallib.php
mod/assign/submission/file/locallib.php
mod/assign/submission/onlinetext/locallib.php

index 686bf0a..f6a14f3 100644 (file)
@@ -415,9 +415,11 @@ abstract class assign_plugin {
      *
      * @param stdClass $submissionorgrade assign_submission or assign_grade
      *                 For submission plugins this is the submission data, for feedback plugins it is the grade data
+     * @param stdClass $user The user record for the current submission.
+     *                         Needed for url rewriting if this is a group submission.
      * @return array - return an array of files indexed by filename
      */
-    public function get_files(stdClass $submissionorgrade) {
+    public function get_files(stdClass $submissionorgrade, stdClass $user) {
         return array();
     }
 
index b355b33..b843d49 100644 (file)
@@ -112,7 +112,7 @@ class assignfeedback_file_zip_importer {
         if (!$sg) {
             return true;
         }
-        foreach ($plugin->get_files($sg) as $pluginfilename => $file) {
+        foreach ($plugin->get_files($sg, $user) as $pluginfilename => $file) {
             if ($pluginfilename == $filename) {
                 // Extract the file and compare hashes.
                 $contenthash = '';
@@ -144,7 +144,7 @@ class assignfeedback_file_zip_importer {
         $fs = get_file_storage();
 
         return $fs->delete_area_files($contextid,
-                                      'assignfeedback_files',
+                                      'assignfeedback_file',
                                       ASSIGNFEEDBACK_FILE_IMPORT_FILEAREA,
                                       $USER->id);
     }
index 6163841..01b97f8 100644 (file)
@@ -1879,6 +1879,39 @@ class assign {
         return $o;
     }
 
+    /**
+     * Rewrite plugin file urls so they resolve correctly in an exported zip.
+     *
+     * @param stdClass $user - The user record
+     * @param assign_plugin $plugin - The assignment plugin
+     */
+    public function download_rewrite_pluginfile_urls($text, $user, $plugin) {
+        $groupmode = groups_get_activity_groupmode($this->get_course_module());
+        $groupname = '';
+        if ($groupmode) {
+            $groupid = groups_get_activity_group($this->get_course_module(), true);
+            $groupname = groups_get_group_name($groupid).'-';
+        }
+
+        if ($this->is_blind_marking()) {
+            $prefix = $groupname . get_string('participant', 'assign');
+            $prefix = str_replace('_', ' ', $prefix);
+            $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($user->id) . '_');
+        } else {
+            $prefix = $groupname . fullname($user);
+            $prefix = str_replace('_', ' ', $prefix);
+            $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($user->id) . '_');
+        }
+
+        $subtype = $plugin->get_subtype();
+        $type = $plugin->get_type();
+        $prefix = $prefix . $subtype . '_' . $type . '_';
+
+        $result = str_replace('@@PLUGINFILE@@/', $prefix, $text);
+
+        return $result;
+    }
+
     /**
      * render the content in editor that is often used by plugin
      *
@@ -2028,7 +2061,7 @@ class assign {
                 if ($submission) {
                     foreach ($this->submissionplugins as $plugin) {
                         if ($plugin->is_enabled() && $plugin->is_visible()) {
-                            $pluginfiles = $plugin->get_files($submission);
+                            $pluginfiles = $plugin->get_files($submission, $student);
                             foreach ($pluginfiles as $zipfilename => $file) {
                                 $subtype = $plugin->get_subtype();
                                 $type = $plugin->get_type();
index ed1e234..0c3d9fa 100644 (file)
@@ -228,7 +228,7 @@ class assign_submission_file extends assign_submission_plugin {
      * @param stdClass $submission The submission
      * @return array - return an array of files indexed by filename
      */
-    public function get_files(stdClass $submission) {
+    public function get_files(stdClass $submission, stdClass $user) {
         $result = array();
         $fs = get_file_storage();
 
index 85f57a1..4162157 100644 (file)
@@ -255,29 +255,16 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      * Produce a list of files suitable for export that represent this submission
      *
      * @param stdClass $submission - For this is the submission data
+     * @param stdClass $user - This is the user record for this submission
      * @return array - return an array of files indexed by filename
      */
-    public function get_files(stdClass $submission) {
+    public function get_files(stdClass $submission, stdClass $user) {
         global $DB;
         $files = array();
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
         if ($onlinetextsubmission) {
-            $user = $DB->get_record("user", array("id"=>$submission->userid),'id,username,firstname,lastname', MUST_EXIST);
-
-            if (!$this->assignment->is_blind_marking()) {
-                $filename = str_replace('_', '', fullname($user)) . '_' .
-                            $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
-                            $this->get_name() . '_';
-                $prefix = clean_filename($filename);
-            } else {
-                $filename = get_string('participant', 'assign') . '_' .
-                            $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
-                            $this->get_name() . '_';
-                $prefix = clean_filename($filename);
-            }
-
-            $finaltext = str_replace('@@PLUGINFILE@@/', $prefix, $onlinetextsubmission->onlinetext);
-            $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>";      //fetched from database
+            $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
+            $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>";
 
             $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);