MDL-69115 course: More course management accessibility fixes
[moodle.git] / contentbank / edit.php
blobcdddcd4fd78616764cac2d4055101d199f5c27ba
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 * Create or update contents through the specific content type editor
20 * @package core_contentbank
21 * @copyright 2020 Victor Deniz <victor@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
27 require_login();
29 $contextid = required_param('contextid', PARAM_INT);
30 $pluginname = required_param('plugin', PARAM_PLUGIN);
31 $id = optional_param('id', null, PARAM_INT);
32 $context = context::instance_by_id($contextid, MUST_EXIST);
33 require_capability('moodle/contentbank:access', $context);
35 $returnurl = new \moodle_url('/contentbank/view.php', ['id' => $id]);
37 if (!empty($id)) {
38 $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
39 $contentclass = "$record->contenttype\\content";
40 $content = new $contentclass($record);
41 // Set the heading title.
42 $heading = $content->get_name();
43 // The content type of the content overwrites the pluginname param value.
44 $contenttypename = $content->get_content_type();
45 } else {
46 $contenttypename = "contenttype_$pluginname";
47 $heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
48 $content = null;
51 // Check plugin is enabled.
52 $plugin = core_plugin_manager::instance()->get_plugin_info($contenttypename);
53 if (!$plugin || !$plugin->is_enabled()) {
54 print_error('unsupported', 'core_contentbank', $returnurl);
57 // Create content type instance.
58 $contenttypeclass = "$contenttypename\\contenttype";
59 if (class_exists($contenttypeclass)) {
60 $contenttype = new $contenttypeclass($context);
61 } else {
62 print_error('unsupported', 'core_contentbank', $returnurl);
65 // Checks the user can edit this content and content type.
66 if (!$contenttype->can_edit($content)) {
67 print_error('contenttypenoedit', 'core_contentbank', $returnurl);
70 $values = [
71 'contextid' => $contextid,
72 'plugin' => $pluginname,
73 'id' => $id
76 $title = get_string('contentbank');
77 \core_contentbank\helper::get_page_ready($context, $title, true);
78 if ($PAGE->course) {
79 require_login($PAGE->course->id);
82 $PAGE->set_url(new \moodle_url('/contentbank/edit.php', $values));
83 $PAGE->set_context($context);
84 $PAGE->navbar->add(get_string('edit'));
85 $PAGE->set_title($title);
87 $PAGE->set_heading($heading);
89 // Instantiate the content type form.
90 $editorclass = "$contenttypename\\form\\editor";
91 if (!class_exists($editorclass)) {
92 print_error('noformdesc');
95 $editorform = new $editorclass(null, $values);
97 if ($editorform->is_cancelled()) {
98 if (empty($id)) {
99 $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
101 redirect($returnurl);
102 } else if ($data = $editorform->get_data()) {
103 $id = $editorform->save_content($data);
104 // Just in case we've created a new content.
105 $returnurl->param('id', $id);
106 redirect($returnurl);
109 echo $OUTPUT->header();
110 $editorform->display();
111 echo $OUTPUT->footer();