MDL-68819 core_contentbank: "Upload" button is not localized.
[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
28require_login();
29
30$contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
31$context = context::instance_by_id($contextid, MUST_EXIST);
32
33require_capability('moodle/contentbank:upload', $context);
34
35$title = get_string('contentbank');
36\core_contentbank\helper::get_page_ready($context, $title, true);
37if ($PAGE->course) {
38 require_login($PAGE->course->id);
39}
40$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
41
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');
48
49$maxbytes = $CFG->userquota;
50$maxareabytes = $CFG->userquota;
51if (has_capability('moodle/user:ignoreuserquota', $context)) {
52 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
53 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
54}
55
56$cb = new \core_contentbank\contentbank();
57$accepted = $cb->get_supported_extensions_as_string($context);
58
59$data = new stdClass();
60$options = array(
61 'subdirs' => 1,
62 'maxbytes' => $maxbytes,
63 'maxfiles' => -1,
64 'accepted_types' => $accepted,
65 'areamaxbytes' => $maxareabytes
66);
67file_prepare_standard_filemanager($data, 'files', $options, $context, 'contentbank', 'public', 0);
68
69$mform = new contentbank_files_form(null, ['contextid' => $contextid, 'data' => $data, 'options' => $options]);
70
71if ($mform->is_cancelled()) {
72 redirect($returnurl);
73} else if ($formdata = $mform->get_data()) {
74 require_sesskey();
48bcb1f2
AA
75 // Get the file and create the content based on it.
76 $usercontext = \context_user::instance($USER->id);
33b8ca26
AA
77 $fs = get_file_storage();
78 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $formdata->file, 'itemid, filepath, filename', false);
33b8ca26
AA
79 if (!empty($files)) {
80 $file = reset($files);
48bcb1f2
AA
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());
a09129b0
AA
83 $viewurl = new \moodle_url('/contentbank/view.php', ['id' => $content->get_id(), 'contextid' => $contextid]);
84 redirect($viewurl);
33b8ca26
AA
85 }
86 redirect($returnurl);
87}
88
89echo $OUTPUT->header();
90echo $OUTPUT->box_start('generalbox');
91
92$mform->display();
93
94echo $OUTPUT->box_end();
95echo $OUTPUT->footer();