MDL-67813 core: Add a manual 'enter' key press behat action step
[moodle.git] / contentbank / upload.php
blob371d27f98439d405d2a9bc6eee44bc1592027f49
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 * Upload a file to content bank.
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
25 require('../config.php');
26 require_once("$CFG->dirroot/contentbank/files_form.php");
28 require_login();
30 $contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
31 $context = context::instance_by_id($contextid, MUST_EXIST);
33 require_capability('moodle/contentbank:upload', $context);
35 $title = get_string('contentbank');
36 \core_contentbank\helper::get_page_ready($context, $title, true);
37 if ($PAGE->course) {
38 require_login($PAGE->course->id);
40 $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
42 $PAGE->set_url('/contentbank/upload.php');
43 $PAGE->set_context($context);
44 $PAGE->navbar->add(get_string('upload', 'contentbank'));
45 $PAGE->set_title($title);
46 $PAGE->set_heading($title);
47 $PAGE->set_pagetype('contenbank');
49 $maxbytes = $CFG->userquota;
50 $maxareabytes = $CFG->userquota;
51 if (has_capability('moodle/user:ignoreuserquota', $context)) {
52 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
53 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
56 $cb = new \core_contentbank\contentbank();
57 $accepted = $cb->get_supported_extensions_as_string($context);
59 $data = new stdClass();
60 $options = array(
61 'subdirs' => 1,
62 'maxbytes' => $maxbytes,
63 'maxfiles' => -1,
64 'accepted_types' => $accepted,
65 'areamaxbytes' => $maxareabytes
67 file_prepare_standard_filemanager($data, 'files', $options, $context, 'contentbank', 'public', 0);
69 $mform = new contentbank_files_form(null, ['contextid' => $contextid, 'data' => $data, 'options' => $options]);
71 if ($mform->is_cancelled()) {
72 redirect($returnurl);
73 } else if ($formdata = $mform->get_data()) {
74 require_sesskey();
75 // Get the file and create the content based on it.
76 $usercontext = \context_user::instance($USER->id);
77 $fs = get_file_storage();
78 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $formdata->file, 'itemid, filepath, filename', false);
79 if (!empty($files)) {
80 $file = reset($files);
81 $content = $cb->create_content_from_file($context, $USER->id, $file);
82 file_save_draft_area_files($formdata->file, $contextid, 'contentbank', 'public', $content->get_id());
83 $viewurl = new \moodle_url('/contentbank/view.php', ['id' => $content->get_id(), 'contextid' => $contextid]);
84 redirect($viewurl);
86 redirect($returnurl);
89 echo $OUTPUT->header();
90 echo $OUTPUT->box_start('generalbox');
92 $mform->display();
94 echo $OUTPUT->box_end();
95 echo $OUTPUT->footer();