/** * Initialise download of all solutions in all task sets belonging to this course. */ public function download_all_solutions() { if (!is_null($this->id)) { $task_sets = new Task_set(); $task_sets->where_related('course', $this); $task_sets->get_iterated(); $filename = $this->get_new_solution_zip_filename(); $zip_archive = new ZipArchive(); if ($zip_archive->open($filename, ZipArchive::CREATE)) { $period = $this->period->get(); $readme = $this->lang->text($this->name); $readme .= "\r\n" . $this->lang->text($period->name); $zip_archive->addFromString('readme.txt', $readme); foreach ($task_sets as $task_set) { $overlay_name = $this->lang->get_overlay('task_sets', $task_set->id, 'name'); $task_set->add_files_to_zip_archive($zip_archive, $this, normalizeForFilesystem(trim($overlay_name) == '' ? $task_set->name : $overlay_name) . '_(' . $task_set->id . ')'); } $zip_archive->close(); header('Content-Description: File Transfer'); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename=' . basename($filename)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); ob_clean(); flush(); $f = fopen($filename, 'r'); while (!feof($f)) { echo fread($f, 1024); } fclose($f); unlink($filename); } else { header("HTTP/1.0 404 Not Found"); } } else { header("HTTP/1.0 404 Not Found"); } die; }
/** * Add files to open zip archive. * @param ZipArchive $zip_archive open zip archive. * @param Course $course course object with loaded course. * @param string|NULL $subdirectory subdirectory where to add files. */ public function add_files_to_zip_archive(ZipArchive $zip_archive, Course $course, $subdirectory = NULL) { if (!is_null($this->id)) { ini_set('max_execution_time', 300); $path_to_task_set_files = 'private/uploads/solutions/task_set_' . $this->id . '/'; if (file_exists($path_to_task_set_files)) { $groups = $course->groups->get_iterated(); $group_names = array(0 => 'unassigned'); foreach ($groups as $group) { $group_names[$group->id] = normalizeForFilesystem($this->lang->text($group->name)); } $students = new Student(); $students->include_related('participant'); $students->where_related('participant/course', $course); $students->get_iterated(); $student_groups = array(); foreach ($students as $studnet) { $student_groups[$studnet->id] = intval($studnet->participant_group_id); } $files = scandir($path_to_task_set_files); foreach ($files as $file) { if ($file != '.' && $file != '..') { if (preg_match(self::STUDENT_FILE_NAME_REGEXP, $file, $matches)) { $student_id = intval($matches['student_id']); $path = ($subdirectory !== NULL && trim($subdirectory) != '' ? $subdirectory . '/' : '') . $group_names[$student_groups[$student_id]] . '/' . $file; $zip_archive->addFile($path_to_task_set_files . $file, $path); } } } } } }