See Release Notes
Long Term Support Release
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace mod_assign; 18 19 use assign; 20 use core_php_time_limit; 21 use mod_assign\event\all_submissions_downloaded; 22 use core\session\manager as sessionmanager; 23 use core_files\archive_writer; 24 use stdClass; 25 use assign_plugin; 26 use stored_file; 27 28 /** 29 * Class to download user submissions. 30 * 31 * @package mod_assign 32 * @copyright 2022 Ferran Recio <ferran@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class downloader { 36 /** @var assign the module manager instance. */ 37 private $manager; 38 39 /** @var stdClass the assign instance record. */ 40 private $instance; 41 42 /** @var array|null the selected user ids, if any. */ 43 private $userids = null; 44 45 /** @var int $groupmode the activity group mode. */ 46 private $groupmode = ''; 47 48 /** @var int $groupid the exported groupid. */ 49 private $groupid = 0; 50 51 /** @var array $filesforzipping the files to zipo (path => file) */ 52 protected $filesforzipping; 53 54 /** @var array $prefixes all loaded the student prefixes. 55 * 56 * A prefix will be converted into a file prefix or a folder name (depends on downloadasfolders). 57 */ 58 private $prefixes; 59 60 /** @var int $downloadasfolders the files to zipo (path => file) */ 61 private $downloadasfolders; 62 63 /** 64 * Class constructor. 65 * 66 * @param assign $manager the instance manager 67 * @param array|null $userids the user ids to download. 68 */ 69 public function __construct(assign $manager, ?array $userids = null) { 70 $this->manager = $manager; 71 $this->userids = $userids; 72 $this->instance = $manager->get_instance(); 73 74 $this->downloadasfolders = get_user_preferences('assign_downloadasfolders', 1); 75 76 $cm = $manager->get_course_module(); 77 $this->groupmode = groups_get_activity_groupmode($cm); 78 if ($this->groupmode) { 79 $this->groupid = groups_get_activity_group($cm, true); 80 } 81 } 82 83 /** 84 * Load the filelist. 85 * 86 * @return bool true if there are some files to zip. 87 */ 88 public function load_filelist(): bool { 89 $manager = $this->manager; 90 $groupid = $this->groupid; 91 92 // Increase the server timeout to handle the creation and sending of large zip files. 93 core_php_time_limit::raise(); 94 95 $manager->require_view_grades(); 96 97 // Load all users with submit. 98 $students = get_enrolled_users( 99 $manager->get_context(), 100 "mod/assign:submit", 101 0, 102 'u.*', 103 null, 104 0, 105 0, 106 $manager->show_only_active_users() 107 ); 108 109 // Build a list of files to zip. 110 $this->filesforzipping = []; 111 112 // Get all the files for each student. 113 foreach ($students as $student) { 114 // Download all assigments submission or only selected users. 115 if ($this->userids && !in_array($student->id, $this->userids)) { 116 continue; 117 } 118 if (!groups_is_member($groupid, $student->id) && $this->groupmode && $groupid) { 119 continue; 120 } 121 $this->load_student_filelist($student); 122 } 123 return !empty($this->filesforzipping); 124 } 125 126 /** 127 * Load an individual student filelist. 128 * 129 * @param stdClass $student the user record 130 */ 131 private function load_student_filelist(stdClass $student) { 132 $submission = $this->get_student_submission($student); 133 if (!$submission) { 134 return; 135 } 136 $prefix = $this->get_student_prefix($student); 137 if (isset($this->prefixes[$prefix])) { 138 // We already send that file (in group mode). 139 return; 140 } 141 $this->prefixes[$prefix] = $student->id; 142 143 foreach ($this->manager->get_submission_plugins() as $plugin) { 144 if (!$plugin->is_enabled() || !$plugin->is_visible()) { 145 continue; 146 } 147 $this->load_submissionplugin_filelist($student, $plugin, $submission, $prefix); 148 } 149 } 150 151 /** 152 * Return the student submission if any. 153 * 154 * @param stdClass $student the user record 155 * @return stdClass|null the user submission or null if none 156 */ 157 private function get_student_submission(stdClass $student): ?stdClass { 158 if ($this->instance->teamsubmission) { 159 $submission = $this->manager->get_group_submission($student->id, 0, false); 160 } else { 161 $submission = $this->manager->get_user_submission($student->id, false); 162 } 163 return $submission ?: null; 164 } 165 166 /** 167 * Return the file prefix used to generate the each submission folder or file. 168 * 169 * @param stdClass $student the user record 170 * @return string the submission prefix 171 */ 172 private function get_student_prefix(stdClass $student): string { 173 $manager = $this->manager; 174 175 // Team submissions are by group, not by student. 176 if ($this->instance->teamsubmission) { 177 $submissiongroup = $manager->get_submission_group($student->id); 178 if ($submissiongroup) { 179 $groupname = format_string($submissiongroup->name, true, ['context' => $manager->get_context()]); 180 $groupinfo = '_' . $submissiongroup->id; 181 } else { 182 $groupname = get_string('defaultteam', 'mod_assign'); 183 $groupinfo = ''; 184 } 185 $prefix = str_replace('_', ' ', $groupname); 186 return clean_filename($prefix . $groupinfo); 187 } 188 // Individual submissions are by user. 189 if ($manager->is_blind_marking()) { 190 $fullname = get_string('participant', 'mod_assign'); 191 } else { 192 $fullname = fullname($student, has_capability('moodle/site:viewfullnames', $manager->get_context())); 193 } 194 $prefix = str_replace('_', ' ', $fullname); 195 $prefix = clean_filename($prefix . '_' . $manager->get_uniqueid_for_user($student->id)); 196 return $prefix; 197 } 198 199 /** 200 * Load a submission plugin filelist for a specific user. 201 * 202 * @param stdClass $student the user record 203 * @param assign_plugin $plugin the submission plugin instance 204 * @param stdClass $submission the submission object 205 * @param string $prefix the files prefix 206 */ 207 private function load_submissionplugin_filelist( 208 stdClass $student, 209 assign_plugin $plugin, 210 stdClass $submission, 211 string $prefix 212 ) { 213 $subtype = $plugin->get_subtype(); 214 $type = $plugin->get_type(); 215 216 if ($this->downloadasfolders) { 217 // Create a folder for each user for each assignment plugin. 218 // This is the default behavior for version of Moodle >= 3.1. 219 $submission->exportfullpath = true; 220 $pluginfiles = $plugin->get_files($submission, $student); 221 foreach ($pluginfiles as $zipfilepath => $file) { 222 $zipfilename = basename($zipfilepath); 223 $prefixedfilename = clean_filename($prefix . '_' . $subtype . '_' . $type); 224 if ($type == 'file') { 225 $pathfilename = $prefixedfilename . $file->get_filepath() . $zipfilename; 226 } else { 227 $pathfilename = $prefixedfilename . '/' . $zipfilename; 228 } 229 $pathfilename = clean_param($pathfilename, PARAM_PATH); 230 $this->filesforzipping[$pathfilename] = $file; 231 } 232 } else { 233 // Create a single folder for all users of all assignment plugins. 234 // This was the default behavior for version of Moodle < 3.1. 235 $submission->exportfullpath = false; 236 $pluginfiles = $plugin->get_files($submission, $student); 237 foreach ($pluginfiles as $zipfilename => $file) { 238 $prefixedfilename = clean_filename($prefix . '_' . $subtype . '_' . $type . '_' . $zipfilename); 239 $this->filesforzipping[$prefixedfilename] = $file; 240 } 241 } 242 } 243 244 /** 245 * Download the exported zip. 246 * 247 * This method will terminate the current script when the file is send. 248 */ 249 public function download_zip() { 250 $filename = $this->get_zip_filename(); 251 all_submissions_downloaded::create_from_assign($this->manager)->trigger(); 252 sessionmanager::write_close(); 253 $zipwriter = archive_writer::get_stream_writer($filename, archive_writer::ZIP_WRITER); 254 255 // Stream the files into the zip. 256 foreach ($this->filesforzipping as $pathinzip => $file) { 257 if ($file instanceof stored_file) { 258 // Most of cases are stored_file. 259 $zipwriter->add_file_from_stored_file($pathinzip, $file); 260 } else if (is_array($file)) { 261 // Save $file as contents, from onlinetext subplugin. 262 $content = reset($file); 263 $zipwriter->add_file_from_string($pathinzip, $content); 264 } 265 } 266 // Finish the archive. 267 $zipwriter->finish(); 268 exit(); 269 } 270 271 /** 272 * Generate the zip filename. 273 * 274 * @return string the zip filename 275 */ 276 private function get_zip_filename(): string { 277 $manager = $this->manager; 278 $filenameparts = [ 279 $manager->get_course()->shortname, 280 $this->instance->name, 281 ]; 282 if (!empty($this->groupid)) { 283 $filenameparts[] = format_string(groups_get_group_name($this->groupid), true, ['context' => $manager->get_context()]); 284 } 285 $filenameparts[] = $manager->get_course_module()->id; 286 287 return clean_filename(implode('-', $filenameparts). '.zip'); 288 } 289 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body