Merge branch 'MDL-69672-310' of git://github.com/aanabit/moodle into MOODLE_310_STABLE
[moodle.git] / contentbank / upload.php
CommitLineData
33b8ca26
AA
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
25require('../config.php');
26require_once("$CFG->dirroot/contentbank/files_form.php");
27
3dfbd5a1
FR
28use core\output\notification;
29
33b8ca26
AA
30require_login();
31
32$contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
33$context = context::instance_by_id($contextid, MUST_EXIST);
34
2dab18e6 35$cb = new \core_contentbank\contentbank();
3490f29a
AA
36if (!$cb->is_context_allowed($context)) {
37 print_error('contextnotallowed', 'core_contentbank');
38}
39
40require_capability('moodle/contentbank:upload', $context);
2dab18e6
FR
41
42$id = optional_param('id', null, PARAM_INT);
43if ($id) {
44 $content = $cb->get_content_from_id($id);
45 $contenttype = $content->get_content_type_instance();
46 if (!$contenttype->can_manage($content) || !$contenttype->can_upload()) {
47 print_error('nopermissions', 'error', $returnurl, get_string('replacecontent', 'contentbank'));
48 }
49}
50
33b8ca26
AA
51$title = get_string('contentbank');
52\core_contentbank\helper::get_page_ready($context, $title, true);
53if ($PAGE->course) {
54 require_login($PAGE->course->id);
55}
56$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
57
58$PAGE->set_url('/contentbank/upload.php');
59$PAGE->set_context($context);
60$PAGE->navbar->add(get_string('upload', 'contentbank'));
61$PAGE->set_title($title);
62$PAGE->set_heading($title);
061893d8 63$PAGE->set_pagetype('contentbank');
33b8ca26
AA
64
65$maxbytes = $CFG->userquota;
66$maxareabytes = $CFG->userquota;
67if (has_capability('moodle/user:ignoreuserquota', $context)) {
68 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
69 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
70}
71
2dab18e6
FR
72if ($id) {
73 $extensions = $contenttype->get_manageable_extensions();
74 $accepted = implode(',', $extensions);
75} else {
76 $accepted = $cb->get_supported_extensions_as_string($context);
77}
33b8ca26
AA
78
79$data = new stdClass();
80$options = array(
81 'subdirs' => 1,
82 'maxbytes' => $maxbytes,
83 'maxfiles' => -1,
84 'accepted_types' => $accepted,
85 'areamaxbytes' => $maxareabytes
86);
87file_prepare_standard_filemanager($data, 'files', $options, $context, 'contentbank', 'public', 0);
88
2dab18e6 89$mform = new contentbank_files_form(null, ['contextid' => $contextid, 'data' => $data, 'options' => $options, 'id' => $id]);
33b8ca26 90
3dfbd5a1
FR
91$error = '';
92
33b8ca26
AA
93if ($mform->is_cancelled()) {
94 redirect($returnurl);
95} else if ($formdata = $mform->get_data()) {
96 require_sesskey();
48bcb1f2
AA
97 // Get the file and create the content based on it.
98 $usercontext = \context_user::instance($USER->id);
33b8ca26
AA
99 $fs = get_file_storage();
100 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $formdata->file, 'itemid, filepath, filename', false);
33b8ca26
AA
101 if (!empty($files)) {
102 $file = reset($files);
2dab18e6
FR
103 if ($id) {
104 $content = $contenttype->replace_content($file, $content);
105 } else {
106 $content = $cb->create_content_from_file($context, $USER->id, $file);
107 }
a09129b0
AA
108 $viewurl = new \moodle_url('/contentbank/view.php', ['id' => $content->get_id(), 'contextid' => $contextid]);
109 redirect($viewurl);
3dfbd5a1
FR
110 } else {
111 $error = get_string('errornofile', 'contentbank');
33b8ca26 112 }
33b8ca26
AA
113}
114
115echo $OUTPUT->header();
116echo $OUTPUT->box_start('generalbox');
117
3dfbd5a1
FR
118if (!empty($error)) {
119 echo $OUTPUT->notification($error, notification::NOTIFY_ERROR);
120}
121
33b8ca26
AA
122$mform->display();
123
124echo $OUTPUT->box_end();
125echo $OUTPUT->footer();