Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * Upload a file to content bank.
  19   *
  20   * @package    core_contentbank
  21   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../config.php');
  26  require_once("$CFG->dirroot/contentbank/files_form.php");
  27  
  28  use core\output\notification;
  29  
  30  require_login();
  31  
  32  $contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
  33  $context = context::instance_by_id($contextid, MUST_EXIST);
  34  
  35  $cb = new \core_contentbank\contentbank();
  36  if (!$cb->is_context_allowed($context)) {
  37      print_error('contextnotallowed', 'core_contentbank');
  38  }
  39  
  40  require_capability('moodle/contentbank:upload', $context);
  41  
  42  $title = get_string('contentbank');
  43  \core_contentbank\helper::get_page_ready($context, $title, true);
  44  if ($PAGE->course) {
  45      require_login($PAGE->course->id);
  46  }
  47  $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
  48  
  49  $PAGE->set_url('/contentbank/upload.php');
  50  $PAGE->set_context($context);
  51  $PAGE->navbar->add(get_string('upload', 'contentbank'));
  52  $PAGE->set_title($title);
  53  $PAGE->set_heading($title);
  54  $PAGE->set_pagetype('contentbank');
  55  
  56  $maxbytes = $CFG->userquota;
  57  $maxareabytes = $CFG->userquota;
  58  if (has_capability('moodle/user:ignoreuserquota', $context)) {
  59      $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
  60      $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
  61  }
  62  
  63  $accepted = $cb->get_supported_extensions_as_string($context);
  64  
  65  $data = new stdClass();
  66  $options = array(
  67      'subdirs' => 1,
  68      'maxbytes' => $maxbytes,
  69      'maxfiles' => -1,
  70      'accepted_types' => $accepted,
  71      'areamaxbytes' => $maxareabytes
  72  );
  73  file_prepare_standard_filemanager($data, 'files', $options, $context, 'contentbank', 'public', 0);
  74  
  75  $mform = new contentbank_files_form(null, ['contextid' => $contextid, 'data' => $data, 'options' => $options]);
  76  
  77  $error = '';
  78  
  79  if ($mform->is_cancelled()) {
  80      redirect($returnurl);
  81  } else if ($formdata = $mform->get_data()) {
  82      require_sesskey();
  83      // Get the file and create the content based on it.
  84      $usercontext = \context_user::instance($USER->id);
  85      $fs = get_file_storage();
  86      $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $formdata->file, 'itemid, filepath, filename', false);
  87      if (!empty($files)) {
  88          $file = reset($files);
  89          $content = $cb->create_content_from_file($context, $USER->id, $file);
  90          $viewurl = new \moodle_url('/contentbank/view.php', ['id' => $content->get_id(), 'contextid' => $contextid]);
  91          redirect($viewurl);
  92      } else {
  93          $error = get_string('errornofile', 'contentbank');
  94      }
  95  }
  96  
  97  echo $OUTPUT->header();
  98  echo $OUTPUT->box_start('generalbox');
  99  
 100  if (!empty($error)) {
 101      echo $OUTPUT->notification($error, notification::NOTIFY_ERROR);
 102  }
 103  
 104  $mform->display();
 105  
 106  echo $OUTPUT->box_end();
 107  echo $OUTPUT->footer();