From 2406815bbfee959bce83c46cb10e48b08d7020ac Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Tue, 18 Dec 2012 11:34:26 +0800 Subject: [PATCH] MDL-37030 Assignment: Fix error on download all submissions. 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 | 4 ++- mod/assign/feedback/file/importziplib.php | 4 +-- mod/assign/locallib.php | 35 ++++++++++++++++++- mod/assign/submission/file/locallib.php | 2 +- mod/assign/submission/onlinetext/locallib.php | 21 +++-------- 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/mod/assign/assignmentplugin.php b/mod/assign/assignmentplugin.php index 686bf0a41a2..f6a14f3ccd2 100644 --- a/mod/assign/assignmentplugin.php +++ b/mod/assign/assignmentplugin.php @@ -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(); } diff --git a/mod/assign/feedback/file/importziplib.php b/mod/assign/feedback/file/importziplib.php index b355b33cad3..b843d495193 100644 --- a/mod/assign/feedback/file/importziplib.php +++ b/mod/assign/feedback/file/importziplib.php @@ -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); } diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 6163841679b..01b97f8e685 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -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(); diff --git a/mod/assign/submission/file/locallib.php b/mod/assign/submission/file/locallib.php index ed1e23471dc..0c3d9fabc3e 100644 --- a/mod/assign/submission/file/locallib.php +++ b/mod/assign/submission/file/locallib.php @@ -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(); diff --git a/mod/assign/submission/onlinetext/locallib.php b/mod/assign/submission/onlinetext/locallib.php index 85f57a13728..416215720ba 100644 --- a/mod/assign/submission/onlinetext/locallib.php +++ b/mod/assign/submission/onlinetext/locallib.php @@ -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 = "". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). ""; //fetched from database + $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this); + $submissioncontent = "". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). ""; $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent); -- 2.43.0