-
Notifications
You must be signed in to change notification settings - Fork 10
/
edit_content.php
134 lines (114 loc) · 4.68 KB
/
edit_content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* unilabel module.
*
* @package mod_unilabel
* @author Andreas Grabs <[email protected]>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/course/format/lib.php');
$cmid = required_param('cmid', PARAM_INT); // The course module id.
$switchtype = optional_param('switchtype', false, PARAM_COMPONENT);
if (!$cm = get_coursemodule_from_id('unilabel', $cmid)) {
throw new \moodle_exception('invalidcoursemodule');
}
$unilabel = $DB->get_record('unilabel', ['id' => $cm->instance], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
$section = $DB->get_record('course_sections', ['id' => $cm->section]);
$courseformat = course_get_format($course->id);
$unilabeltype = \mod_unilabel\factory::get_plugin($unilabel->unilabeltype);
if (!$unilabeltype->is_active()) {
$unilabeltype = \mod_unilabel\factory::get_plugin('simpletext');
}
if ($course->id === SITEID) {
require_login($course, true);
} else {
require_course_login($course, true, $cm);
}
$context = context_module::instance($cm->id);
require_capability('mod/unilabel:edit', $context);
$myurl = new \moodle_url($FULLME);
$mybaseurl = new \moodle_url($myurl);
$mybaseurl->remove_all_params();
$returnurl = $courseformat->get_view_url($section);
$strtitle = $course->shortname . ': ' . $unilabeltype->get_name() . ' - ' . $unilabel->name;
/** @var \moodle_page $PAGE */
$PAGE->set_url($myurl);
$PAGE->set_title($strtitle);
$PAGE->set_pagelayout('course'); // This pagelayout is also needed on behat. Without this I had an error.
$PAGE->set_heading($course->fullname);
$PAGE->add_body_class('limitedwidth');
$PAGE->activityheader->disable();
if ($switchtype) {
require_sesskey();
$unilabeltype = \mod_unilabel\factory::get_plugin($switchtype);
if (!$unilabeltype->is_active()) {
$unilabeltype = \mod_unilabel\factory::get_plugin('simpletext');
}
$DB->set_field('unilabel', 'unilabeltype', $unilabeltype->get_plugintype(), ['id' => $unilabel->id]);
redirect(new \moodle_url($mybaseurl, ['cmid' => $cmid]));
}
$form = new \mod_unilabel\edit_content_form(null, ['unilabel' => $unilabel, 'cm' => $cm, 'unilabeltype' => $unilabeltype]);
if ($form->is_cancelled()) {
redirect($returnurl);
}
if ($formdata = $form->get_data()) {
// Save the intro and after that the current unilabeltype plugin.
if ($draftitemid = $formdata->introeditor['itemid']) {
$formdata->intro = file_save_draft_area_files(
$draftitemid, $context->id,
'mod_unilabel',
'intro',
0,
\mod_unilabel\edit_content_form::editor_options($context),
$formdata->introeditor['text']
);
$formdata->introformat = $formdata->introeditor['format'];
}
$updatesuccess = true;
if (!$DB->update_record('unilabel', $formdata)) {
$updatesuccess = false;
}
if ($updatesuccess) {
$updatesuccess = \mod_unilabel\factory::save_plugin_content($formdata, $unilabel);
}
if ($updatesuccess) {
$msg = get_string('updatesuccessful', 'mod_unilabel');
$msgtype = \core\output\notification::NOTIFY_SUCCESS;
rebuild_course_cache($course->id);
} else {
$msg = get_string('updatefailed', 'mod_unilabel');
$msgtype = \core\output\notification::NOTIFY_ERROR;
}
redirect($returnurl, $msg, null, $msgtype);
}
/** @var \mod_unilabel\output\renderer $renderer */
$renderer = $PAGE->get_renderer('mod_unilabel');
$plugins = \mod_unilabel\factory::get_plugin_list();
$select = new single_select(
new \moodle_url($mybaseurl, ['cmid' => $cmid, 'sesskey' => sesskey()]),
'switchtype',
$plugins,
$unilabeltype->get_plugintype()
);
$select->label = get_string('switchtype', 'mod_unilabel');
echo $renderer->header();
echo $renderer->render($select);
$form->display();
echo $renderer->footer();