Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * unilabel module.
19
 *
20
 * @package     mod_unilabel
21
 * @author      Andreas Grabs <info@grabs-edv.de>
22
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
require_once(__DIR__ . '/../../config.php');
27
require_once($CFG->dirroot . '/course/format/lib.php');
28
 
29
$cmid       = required_param('cmid', PARAM_INT);   // The course module id.
30
$switchtype = optional_param('switchtype', false, PARAM_COMPONENT);
31
 
32
if (!$cm = get_coursemodule_from_id('unilabel', $cmid)) {
33
    throw new \moodle_exception('invalidcoursemodule');
34
}
35
 
36
$unilabel = $DB->get_record('unilabel', ['id' => $cm->instance], '*', MUST_EXIST);
37
$course   = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
38
 
39
$section      = $DB->get_record('course_sections', ['id' => $cm->section]);
40
$courseformat = course_get_format($course->id);
41
 
42
$unilabeltype = \mod_unilabel\factory::get_plugin($unilabel->unilabeltype);
43
if (!$unilabeltype->is_active()) {
44
    $unilabeltype = \mod_unilabel\factory::get_plugin('simpletext');
45
}
46
 
47
if ($course->id === SITEID) {
48
    require_login($course, true);
49
} else {
50
    require_course_login($course, true, $cm);
51
}
52
$context = context_module::instance($cm->id);
53
require_capability('mod/unilabel:edit', $context);
54
 
55
$myurl     = new \moodle_url($FULLME);
56
$mybaseurl = new \moodle_url($myurl);
57
$mybaseurl->remove_all_params();
58
$returnurl = $courseformat->get_view_url($section);
59
$strtitle  = $course->shortname . ': ' . $unilabeltype->get_name() . ' - ' . $unilabel->name;
60
 
61
/** @var \moodle_page $PAGE */
62
$PAGE->set_url($myurl);
63
$PAGE->set_title($strtitle);
64
$PAGE->set_pagelayout('course'); // This pagelayout is also needed on behat. Without this I had an error.
65
$PAGE->set_heading($course->fullname);
66
$PAGE->add_body_class('limitedwidth');
67
$PAGE->activityheader->disable();
68
 
69
if ($switchtype) {
70
    require_sesskey();
71
    $unilabeltype = \mod_unilabel\factory::get_plugin($switchtype);
72
    if (!$unilabeltype->is_active()) {
73
        $unilabeltype = \mod_unilabel\factory::get_plugin('simpletext');
74
    }
75
 
76
    $DB->set_field('unilabel', 'unilabeltype', $unilabeltype->get_plugintype(), ['id' => $unilabel->id]);
77
    redirect(new \moodle_url($mybaseurl, ['cmid' => $cmid]));
78
}
79
 
80
$form = new \mod_unilabel\edit_content_form(null, ['unilabel' => $unilabel, 'cm' => $cm, 'unilabeltype' => $unilabeltype]);
81
if ($form->is_cancelled()) {
82
    redirect($returnurl);
83
}
84
 
85
if ($formdata = $form->get_data()) {
86
    // Save the intro and after that the current unilabeltype plugin.
87
    if ($draftitemid = $formdata->introeditor['itemid']) {
88
        $formdata->intro = file_save_draft_area_files(
89
            $draftitemid, $context->id,
90
            'mod_unilabel',
91
            'intro',
92
            0,
93
            \mod_unilabel\edit_content_form::editor_options($context),
94
            $formdata->introeditor['text']
95
        );
96
 
97
        $formdata->introformat = $formdata->introeditor['format'];
98
    }
99
    $updatesuccess = true;
100
    if (!$DB->update_record('unilabel', $formdata)) {
101
        $updatesuccess = false;
102
    }
103
 
104
    if ($updatesuccess) {
105
        $updatesuccess = \mod_unilabel\factory::save_plugin_content($formdata, $unilabel);
106
    }
107
 
108
    if ($updatesuccess) {
109
        $msg     = get_string('updatesuccessful', 'mod_unilabel');
110
        $msgtype = \core\output\notification::NOTIFY_SUCCESS;
111
    } else {
112
        $msg     = get_string('updatefailed', 'mod_unilabel');
113
        $msgtype = \core\output\notification::NOTIFY_ERROR;
114
    }
115
    redirect($returnurl, $msg, null, $msgtype);
116
}
117
 
118
/** @var \mod_unilabel\output\renderer $renderer */
119
$renderer = $PAGE->get_renderer('mod_unilabel');
120
 
121
$plugins = \mod_unilabel\factory::get_plugin_list();
122
$select  = new single_select(
123
    new \moodle_url($mybaseurl, ['cmid' => $cmid, 'sesskey' => sesskey()]),
124
    'switchtype',
125
    $plugins,
126
    $unilabeltype->get_plugintype()
127
);
128
$select->label = get_string('switchtype', 'mod_unilabel');
129
 
130
echo $renderer->header();
131
echo $renderer->render($select);
132
$form->display();
133
echo $renderer->footer();