Merge branch 'MDL-69672-310' of git://github.com/aanabit/moodle into MOODLE_310_STABLE
[moodle.git] / contentbank / edit.php
CommitLineData
c49d8663
VDF
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 * Create or update contents through the specific content type editor
19 *
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
23 */
24
25require('../config.php');
26
27require_login();
28
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);
3490f29a
AA
33
34$cb = new \core_contentbank\contentbank();
35if (!$cb->is_context_allowed($context)) {
36 print_error('contextnotallowed', 'core_contentbank');
37}
38
c49d8663
VDF
39require_capability('moodle/contentbank:access', $context);
40
41$returnurl = new \moodle_url('/contentbank/view.php', ['id' => $id]);
42
43if (!empty($id)) {
44 $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
45 $contentclass = "$record->contenttype\\content";
46 $content = new $contentclass($record);
47 // Set the heading title.
48 $heading = $content->get_name();
49 // The content type of the content overwrites the pluginname param value.
50 $contenttypename = $content->get_content_type();
51} else {
52 $contenttypename = "contenttype_$pluginname";
53 $heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
0633e60a 54 $content = null;
c49d8663
VDF
55}
56
57// Check plugin is enabled.
58$plugin = core_plugin_manager::instance()->get_plugin_info($contenttypename);
59if (!$plugin || !$plugin->is_enabled()) {
60 print_error('unsupported', 'core_contentbank', $returnurl);
61}
62
63// Create content type instance.
64$contenttypeclass = "$contenttypename\\contenttype";
65if (class_exists($contenttypeclass)) {
66 $contenttype = new $contenttypeclass($context);
67} else {
68 print_error('unsupported', 'core_contentbank', $returnurl);
69}
70
0633e60a
AA
71// Checks the user can edit this content and content type.
72if (!$contenttype->can_edit($content)) {
73 print_error('contenttypenoedit', 'core_contentbank', $returnurl);
c49d8663
VDF
74}
75
76$values = [
77 'contextid' => $contextid,
78 'plugin' => $pluginname,
79 'id' => $id
80];
81
82$title = get_string('contentbank');
83\core_contentbank\helper::get_page_ready($context, $title, true);
84if ($PAGE->course) {
85 require_login($PAGE->course->id);
86}
87
88$PAGE->set_url(new \moodle_url('/contentbank/edit.php', $values));
89$PAGE->set_context($context);
90$PAGE->navbar->add(get_string('edit'));
91$PAGE->set_title($title);
92
93$PAGE->set_heading($heading);
94
95// Instantiate the content type form.
96$editorclass = "$contenttypename\\form\\editor";
97if (!class_exists($editorclass)) {
98 print_error('noformdesc');
99}
100
101$editorform = new $editorclass(null, $values);
102
103if ($editorform->is_cancelled()) {
104 if (empty($id)) {
105 $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
106 }
107 redirect($returnurl);
108} else if ($data = $editorform->get_data()) {
109 $id = $editorform->save_content($data);
110 // Just in case we've created a new content.
111 $returnurl->param('id', $id);
112 redirect($returnurl);
113}
114
115echo $OUTPUT->header();
116$editorform->display();
117echo $OUTPUT->footer();