Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  /**
  18   * Download course content confirmation and execution.
  19   *
  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
  24   */
  25  
  26  require_once('../config.php');
  27  
  28  use core\content;
  29  use core\content\export\zipwriter;
  30  
  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]);
  36  
  37  if (!\core\content::can_export_context($coursecontext, $USER)) {
  38      redirect($courselink);
  39  }
  40  
  41  $PAGE->set_url('/course/downloadcontent.php', ['contextid' => $contextid]);
  42  require_login($courseid);
  43  
  44  $courseinfo = get_fast_modinfo($courseid)->get_course();
  45  $filename = str_replace('/', '', str_replace(' ', '_', $courseinfo->shortname)) . '_' . time() . '.zip';
  46  
  47  // If download confirmed, prepare and start the zipstream of the course download content.
  48  if ($isdownload) {
  49      confirm_sesskey();
  50  
  51      $exportoptions = null;
  52  
  53      if (!empty($CFG->maxsizeperdownloadcoursefile)) {
  54          $exportoptions = new stdClass();
  55          $exportoptions->maxfilesize = $CFG->maxsizeperdownloadcoursefile;
  56      }
  57  
  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);
  61  
  62          ob_start();
  63          content::export_context($coursecontext, $USER, $writer);
  64          $content = ob_get_clean();
  65  
  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);
  73          }
  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);
  78  
  79          redirect($courselink);
  80      }
  81  
  82  } else {
  83      $PAGE->set_title(get_string('downloadcoursecontent', 'course'));
  84      $PAGE->set_heading(format_string($courseinfo->fullname));
  85  
  86      echo $OUTPUT->header();
  87      echo $OUTPUT->heading(get_string('downloadcoursecontent', 'course'));
  88  
  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]);
  92  
  93      echo $OUTPUT->confirm(get_string('downloadcourseconfirmation', 'course', $maxfilesize), $downloadlink, $courselink);
  94  }