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 the customcert module for 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
 * Edit a customcert element.
19
 *
20
 * @package    mod_customcert
21
 * @copyright  2013 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require_once('../../config.php');
26
 
27
$tid = required_param('tid', PARAM_INT);
28
$action = required_param('action', PARAM_ALPHA);
29
 
30
$template = $DB->get_record('customcert_templates', ['id' => $tid], '*', MUST_EXIST);
31
 
32
// Set the template object.
33
$template = new \mod_customcert\template($template);
34
 
35
// Perform checks.
36
if ($cm = $template->get_cm()) {
37
    require_login($cm->course, false, $cm);
38
} else {
39
    require_login();
40
}
41
// Make sure the user has the required capabilities.
42
$template->require_manage();
43
 
44
if ($template->get_context()->contextlevel == CONTEXT_MODULE) {
45
    $customcert = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
46
    $title = $customcert->name;
47
} else {
48
    $title = $SITE->fullname;
49
}
50
 
51
if ($action == 'edit') {
52
    // The id of the element must be supplied if we are currently editing one.
53
    $id = required_param('id', PARAM_INT);
54
    $element = $DB->get_record('customcert_elements', ['id' => $id], '*', MUST_EXIST);
55
    $pageurl = new moodle_url('/mod/customcert/edit_element.php', ['id' => $id, 'tid' => $tid, 'action' => $action]);
56
} else { // Must be adding an element.
57
    // We need to supply what element we want added to what page.
58
    $pageid = required_param('pageid', PARAM_INT);
59
    $element = new stdClass();
60
    $element->element = required_param('element', PARAM_ALPHA);
61
    $pageurl = new moodle_url('/mod/customcert/edit_element.php', ['tid' => $tid, 'element' => $element->element,
62
        'pageid' => $pageid, 'action' => $action]);
63
}
64
 
65
// Set up the page.
66
\mod_customcert\page_helper::page_setup($pageurl, $template->get_context(), $title);
67
$PAGE->activityheader->set_attrs(['hidecompletion' => true,
68
            'description' => '']);
69
 
70
// Additional page setup.
71
if ($template->get_context()->contextlevel == CONTEXT_SYSTEM) {
72
    $PAGE->navbar->add(get_string('managetemplates', 'customcert'),
73
        new moodle_url('/mod/customcert/manage_templates.php'));
74
}
75
$PAGE->navbar->add(get_string('editcustomcert', 'customcert'), new moodle_url('/mod/customcert/edit.php',
76
    ['tid' => $tid]));
77
$PAGE->navbar->add(get_string('editelement', 'customcert'));
78
 
79
$mform = new \mod_customcert\edit_element_form($pageurl, ['element' => $element]);
80
 
81
// Check if they cancelled.
82
if ($mform->is_cancelled()) {
83
    $url = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
84
    redirect($url);
85
}
86
 
87
if ($data = $mform->get_data()) {
88
    // Set the id, or page id depending on if we are editing an element, or adding a new one.
89
    if ($action == 'edit') {
90
        $data->id = $id;
91
        $data->pageid = $element->pageid;
92
    } else {
93
        $data->pageid = $pageid;
94
    }
95
    // Set the element variable.
96
    $data->element = $element->element;
97
    // Get an instance of the element class.
98
    if ($e = \mod_customcert\element_factory::get_element_instance($data)) {
99
        $e->save_form_elements($data);
100
 
101
        // Trigger updated event.
102
        \mod_customcert\event\template_updated::create_from_template($template)->trigger();
103
    }
104
 
105
    $url = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
106
    redirect($url);
107
}
108
 
109
echo $OUTPUT->header();
110
$mform->display();
111
echo $OUTPUT->footer();