weekly release 3.11.17+
[moodle.git] / course / downloadcontent.php
blob7813a0fb2e31ed3671701d6350df107b0715bca9
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.
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/>.
17 /**
18 * Download course content confirmation and execution.
20 * @package core
21 * @subpackage course
22 * @copyright 2020 Michael Hawkins <michaelh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
28 use core\content;
29 use core\content\export\zipwriter;
31 $contextid = required_param('contextid', PARAM_INT);
32 $isdownload = optional_param('download', 0, PARAM_BOOL);
33 $coursecontext = context::instance_by_id($contextid);
34 $courseid = $coursecontext->instanceid;
35 $courselink = new moodle_url('/course/view.php', ['id' => $courseid]);
37 if (!\core\content::can_export_context($coursecontext, $USER)) {
38 redirect($courselink);
41 $PAGE->set_url('/course/downloadcontent.php', ['contextid' => $contextid]);
42 require_login($courseid);
44 $courseinfo = get_fast_modinfo($courseid)->get_course();
45 $filename = str_replace('/', '', str_replace(' ', '_', $courseinfo->shortname)) . '_' . time() . '.zip';
47 // If download confirmed, prepare and start the zipstream of the course download content.
48 if ($isdownload) {
49 confirm_sesskey();
51 $exportoptions = null;
53 if (!empty($CFG->maxsizeperdownloadcoursefile)) {
54 $exportoptions = new stdClass();
55 $exportoptions->maxfilesize = $CFG->maxsizeperdownloadcoursefile;
58 // Use file writer in debug developer mode, so any errors can be displayed instead of being streamed into the output file.
59 if (debugging('', DEBUG_DEVELOPER)) {
60 $writer = zipwriter::get_file_writer($filename, $exportoptions);
62 ob_start();
63 content::export_context($coursecontext, $USER, $writer);
64 $content = ob_get_clean();
66 // If no errors found, output the file.
67 if (empty($content)) {
68 send_file($writer->get_file_path(), $filename);
69 redirect($courselink);
70 } else {
71 // If any errors occurred, display them instead of outputting the file.
72 debugging("Errors found while producing the download course content output:\n {$content}", DEBUG_DEVELOPER);
74 } else {
75 // If not developer debugging, stream the output file directly.
76 $writer = zipwriter::get_stream_writer($filename, $exportoptions);
77 content::export_context($coursecontext, $USER, $writer);
79 redirect($courselink);
82 } else {
83 $PAGE->set_title(get_string('downloadcoursecontent', 'course'));
84 $PAGE->set_heading(format_string($courseinfo->fullname));
86 echo $OUTPUT->header();
87 echo $OUTPUT->heading(get_string('downloadcoursecontent', 'course'));
89 // Prepare download confirmation information and display it.
90 $maxfilesize = display_size($CFG->maxsizeperdownloadcoursefile);
91 $downloadlink = new moodle_url('/course/downloadcontent.php', ['contextid' => $contextid, 'download' => 1]);
93 echo $OUTPUT->confirm(get_string('downloadcourseconfirmation', 'course', $maxfilesize), $downloadlink, $courselink);