1 |
efrain |
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 |
|
|
|
25 |
require('../config.php');
|
|
|
26 |
|
|
|
27 |
require_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 |
$library = optional_param('library', null, PARAM_RAW);
|
|
|
33 |
|
|
|
34 |
$context = context::instance_by_id($contextid, MUST_EXIST);
|
|
|
35 |
|
|
|
36 |
$cb = new \core_contentbank\contentbank();
|
|
|
37 |
if (!$cb->is_context_allowed($context)) {
|
|
|
38 |
throw new \moodle_exception('contextnotallowed', 'core_contentbank');
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
require_capability('moodle/contentbank:access', $context);
|
|
|
42 |
|
|
|
43 |
$returnurl = new \moodle_url('/contentbank/view.php', ['id' => $id]);
|
|
|
44 |
|
|
|
45 |
if (!empty($id)) {
|
|
|
46 |
$record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
|
|
|
47 |
$contentclass = "$record->contenttype\\content";
|
|
|
48 |
$content = new $contentclass($record);
|
|
|
49 |
// Set the heading title.
|
|
|
50 |
$heading = $content->get_name();
|
|
|
51 |
// The content type of the content overwrites the pluginname param value.
|
|
|
52 |
$contenttypename = $content->get_content_type();
|
|
|
53 |
$breadcrumbtitle = get_string('edit');
|
|
|
54 |
} else {
|
|
|
55 |
$contenttypename = "contenttype_$pluginname";
|
|
|
56 |
$heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
|
|
|
57 |
$content = null;
|
|
|
58 |
$breadcrumbtitle = get_string('add');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
// Check plugin is enabled.
|
|
|
62 |
$plugin = core_plugin_manager::instance()->get_plugin_info($contenttypename);
|
|
|
63 |
if (!$plugin || !$plugin->is_enabled()) {
|
|
|
64 |
throw new \moodle_exception('unsupported', 'core_contentbank', $returnurl);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// Create content type instance.
|
|
|
68 |
$contenttypeclass = "$contenttypename\\contenttype";
|
|
|
69 |
if (class_exists($contenttypeclass)) {
|
|
|
70 |
$contenttype = new $contenttypeclass($context);
|
|
|
71 |
} else {
|
|
|
72 |
throw new \moodle_exception('unsupported', 'core_contentbank', $returnurl);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Checks the user can edit this content and content type.
|
|
|
76 |
if (!$contenttype->can_edit($content)) {
|
|
|
77 |
throw new \moodle_exception('contenttypenoedit', 'core_contentbank', $returnurl);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$values = [
|
|
|
81 |
'contextid' => $contextid,
|
|
|
82 |
'plugin' => $pluginname,
|
|
|
83 |
'id' => $id,
|
|
|
84 |
'heading' => $heading,
|
|
|
85 |
'library' => $library
|
|
|
86 |
];
|
|
|
87 |
|
|
|
88 |
$title = get_string('contentbank');
|
|
|
89 |
\core_contentbank\helper::get_page_ready($context, $title, true);
|
|
|
90 |
if ($PAGE->course) {
|
|
|
91 |
require_login($PAGE->course->id);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($context->contextlevel == CONTEXT_COURSECAT) {
|
|
|
95 |
$PAGE->set_primary_active_tab('home');
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$PAGE->set_url(new \moodle_url('/contentbank/edit.php', $values));
|
|
|
99 |
if ($context->id == \context_system::instance()->id) {
|
|
|
100 |
$PAGE->set_context(context_course::instance($context->id));
|
|
|
101 |
} else {
|
|
|
102 |
$PAGE->set_context($context);
|
|
|
103 |
}
|
|
|
104 |
if ($content) {
|
|
|
105 |
$PAGE->navbar->add($content->get_name(), new \moodle_url('/contentbank/view.php', ['id' => $id]));
|
|
|
106 |
}
|
|
|
107 |
$PAGE->navbar->add($breadcrumbtitle);
|
|
|
108 |
$PAGE->set_title($title);
|
|
|
109 |
$PAGE->set_pagelayout('incourse');
|
|
|
110 |
$PAGE->set_secondary_active_tab('contentbank');
|
|
|
111 |
|
|
|
112 |
// Instantiate the content type form.
|
|
|
113 |
$editorclass = "$contenttypename\\form\\editor";
|
|
|
114 |
if (!class_exists($editorclass)) {
|
|
|
115 |
throw new \moodle_exception('noformdesc');
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
$editorform = new $editorclass(null, $values);
|
|
|
119 |
|
|
|
120 |
if ($editorform->is_cancelled()) {
|
|
|
121 |
if (empty($id)) {
|
|
|
122 |
$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
|
|
|
123 |
}
|
|
|
124 |
redirect($returnurl);
|
|
|
125 |
} else if ($data = $editorform->get_data()) {
|
|
|
126 |
if (empty($id)) {
|
|
|
127 |
$msg = get_string('contentcreated', 'contentbank');
|
|
|
128 |
} else {
|
|
|
129 |
$msg = get_string('contentupdated', 'contentbank');
|
|
|
130 |
}
|
|
|
131 |
$id = $editorform->save_content($data);
|
|
|
132 |
// Just in case we've created a new content.
|
|
|
133 |
$returnurl->param('id', $id);
|
|
|
134 |
redirect($returnurl, $msg, null, \core\output\notification::NOTIFY_SUCCESS);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
echo $OUTPUT->header();
|
|
|
138 |
$editorform->display();
|
|
|
139 |
echo $OUTPUT->footer();
|