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 the customcert settings.
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 = optional_param('tid', 0, PARAM_INT);
28
$action = optional_param('action', '', PARAM_ALPHA);
29
if ($action) {
30
    $actionid = required_param('aid', PARAM_INT);
31
}
32
$confirm = optional_param('confirm', 0, PARAM_INT);
33
 
34
// Edit an existing template.
35
if ($tid) {
36
    // Create the template object.
37
    $template = $DB->get_record('customcert_templates', ['id' => $tid], '*', MUST_EXIST);
38
    $template = new \mod_customcert\template($template);
39
    // Set the context.
40
    $contextid = $template->get_contextid();
41
    // Set the page url.
42
    $pageurl = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
43
} else { // Adding a new template.
44
    // Need to supply the contextid.
45
    $contextid = required_param('contextid', PARAM_INT);
46
    // Set the page url.
47
    $pageurl = new moodle_url('/mod/customcert/edit.php', ['contextid' => $contextid]);
48
}
49
 
50
$context = context::instance_by_id($contextid);
51
if ($context->contextlevel == CONTEXT_MODULE) {
52
    $cm = get_coursemodule_from_id('customcert', $context->instanceid, 0, false, MUST_EXIST);
53
    require_login($cm->course, false, $cm);
54
 
55
    $customcert = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
56
    $title = $customcert->name;
57
} else {
58
    require_login();
59
    $title = $SITE->fullname;
60
}
61
 
62
require_capability('mod/customcert:manage', $context);
63
 
64
// Set up the page.
65
\mod_customcert\page_helper::page_setup($pageurl, $context, $title);
66
$PAGE->activityheader->set_attrs(['hidecompletion' => true,
67
            'description' => '']);
68
 
69
if ($context->contextlevel == CONTEXT_SYSTEM) {
70
    // We are managing a template - add some navigation.
71
    $PAGE->navbar->add(get_string('managetemplates', 'customcert'),
72
        new moodle_url('/mod/customcert/manage_templates.php'));
73
    if (!$tid) {
74
        $PAGE->navbar->add(get_string('editcustomcert', 'customcert'));
75
    } else {
76
        $PAGE->navbar->add(get_string('editcustomcert', 'customcert'),
77
            new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]));
78
    }
79
}
80
 
81
// Flag to determine if we are deleting anything.
82
$deleting = false;
83
 
84
if ($tid) {
85
    if ($action && confirm_sesskey()) {
86
        switch ($action) {
87
            case 'pmoveup' :
88
                $template->move_item('page', $actionid, 'up');
89
                break;
90
            case 'pmovedown' :
91
                $template->move_item('page', $actionid, 'down');
92
                break;
93
            case 'emoveup' :
94
                $template->move_item('element', $actionid, 'up');
95
                break;
96
            case 'emovedown' :
97
                $template->move_item('element', $actionid, 'down');
98
                break;
99
            case 'addpage' :
100
                $template->add_page();
101
                $url = new \moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
102
                redirect($url);
103
                break;
104
            case 'deletepage' :
105
                if (!empty($confirm)) { // Check they have confirmed the deletion.
106
                    $template->delete_page($actionid);
107
                    $url = new \moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
108
                    redirect($url);
109
                } else {
110
                    // Set deletion flag to true.
111
                    $deleting = true;
112
                    // Create the message.
113
                    $message = get_string('deletepageconfirm', 'customcert');
114
                    // Create the link options.
115
                    $nourl = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
116
                    $yesurl = new moodle_url('/mod/customcert/edit.php',
117
                        [
118
                            'tid' => $tid,
119
                            'action' => 'deletepage',
120
                            'aid' => $actionid,
121
                            'confirm' => 1,
122
                            'sesskey' => sesskey(),
123
                        ]
124
                    );
125
                }
126
                break;
127
            case 'deleteelement' :
128
                if (!empty($confirm)) { // Check they have confirmed the deletion.
129
                    $template->delete_element($actionid);
130
                } else {
131
                    // Set deletion flag to true.
132
                    $deleting = true;
133
                    // Create the message.
134
                    $message = get_string('deleteelementconfirm', 'customcert');
135
                    // Create the link options.
136
                    $nourl = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
137
                    $yesurl = new moodle_url('/mod/customcert/edit.php',
138
                        [
139
                            'tid' => $tid,
140
                            'action' => 'deleteelement',
141
                            'aid' => $actionid,
142
                            'confirm' => 1,
143
                            'sesskey' => sesskey(),
144
                        ]
145
                    );
146
                }
147
                break;
148
        }
149
    }
150
}
151
 
152
// Check if we are deleting either a page or an element.
153
if ($deleting) {
154
    // Show a confirmation page.
155
    $PAGE->navbar->add(get_string('deleteconfirm', 'customcert'));
156
    echo $OUTPUT->header();
157
    echo $OUTPUT->confirm($message, $yesurl, $nourl);
158
    echo $OUTPUT->footer();
159
    exit();
160
}
161
 
162
if ($tid) {
163
    $mform = new \mod_customcert\edit_form($pageurl, ['tid' => $tid]);
164
    // Set the name for the form.
165
    $mform->set_data(['name' => $template->get_name()]);
166
} else {
167
    $mform = new \mod_customcert\edit_form($pageurl);
168
}
169
 
170
if ($data = $mform->get_data()) {
171
    // If there is no id, then we are creating a template.
172
    if (!$tid) {
173
        $template = \mod_customcert\template::create($data->name, $contextid);
174
 
175
        // Create a page for this template.
176
        $pageid = $template->add_page(false);
177
 
178
        // Associate all the data from the form to the newly created page.
179
        $width = 'pagewidth_' . $pageid;
180
        $height = 'pageheight_' . $pageid;
181
        $leftmargin = 'pageleftmargin_' . $pageid;
182
        $rightmargin = 'pagerightmargin_' . $pageid;
183
        $rightmargin = 'pagerightmargin_' . $pageid;
184
 
185
        $data->$width = $data->pagewidth_0;
186
        $data->$height = $data->pageheight_0;
187
        $data->$leftmargin = $data->pageleftmargin_0;
188
        $data->$rightmargin = $data->pagerightmargin_0;
189
 
190
        // We may also have clicked to add an element, so these need changing as well.
191
        if (isset($data->element_0) && isset($data->addelement_0)) {
192
            $element = 'element_' . $pageid;
193
            $addelement = 'addelement_' . $pageid;
194
            $data->$element = $data->element_0;
195
            $data->$addelement = $data->addelement_0;
196
 
197
            // Need to remove the temporary element and add element placeholders so we
198
            // don't try add an element to the wrong page.
199
            unset($data->element_0);
200
            unset($data->addelement_0);
201
        }
202
    }
203
 
204
    if ($tid) {
205
        // Save any data for the template.
206
        $template->save($data);
207
    }
208
 
209
    // Save any page data.
210
    $template->save_page($data);
211
 
212
    // Loop through the data.
213
    foreach ($data as $key => $value) {
214
        // Check if they chose to add an element to a page.
215
        if (strpos($key, 'addelement_') !== false) {
216
            // Get the page id.
217
            $pageid = str_replace('addelement_', '', $key);
218
            // Get the element.
219
            $element = "element_" . $pageid;
220
            $element = $data->$element;
221
            // Create the URL to redirect to to add this element.
222
            $params = [];
223
            $params['tid'] = $template->get_id();
224
            $params['action'] = 'add';
225
            $params['element'] = $element;
226
            $params['pageid'] = $pageid;
227
            $url = new moodle_url('/mod/customcert/edit_element.php', $params);
228
            redirect($url);
229
        }
230
    }
231
 
232
    // Check if we want to preview this custom certificate.
233
    if (!empty($data->previewbtn)) {
234
        $template->generate_pdf(true);
235
        exit();
236
    }
237
 
238
    // Redirect to the editing page to show form with recent updates.
239
    $url = new moodle_url('/mod/customcert/edit.php', ['tid' => $template->get_id()]);
240
    redirect($url);
241
}
242
 
243
echo $OUTPUT->header();
244
$mform->display();
245
if ($tid && $context->contextlevel == CONTEXT_MODULE) {
246
    $loadtemplateurl = new moodle_url('/mod/customcert/load_template.php', ['tid' => $tid]);
247
    $loadtemplateform = new \mod_customcert\load_template_form($loadtemplateurl, ['context' => $context], 'post',
248
        '', ['id' => 'loadtemplateform']);
249
    $loadtemplateform->display();
250
}
251
echo $OUTPUT->footer();