-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathedit_element.php
More file actions
208 lines (182 loc) · 7.7 KB
/
edit_element.php
File metadata and controls
208 lines (182 loc) · 7.7 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
// This file is part of the customcert module for 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/>.
/**
* Edit a customcert element.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_customcert\edit_element_form;
use mod_customcert\element;
use mod_customcert\event\template_updated;
use mod_customcert\page_helper;
use mod_customcert\service\element_factory;
use mod_customcert\service\element_repository;
use mod_customcert\service\form_service;
use mod_customcert\service\persistence_helper;
use mod_customcert\service\page_repository;
use mod_customcert\service\template_repository;
use mod_customcert\template;
require_once('../../config.php');
$templaterepo = new template_repository();
$pagerepo = new page_repository();
$factory = element_factory::build_with_defaults();
$elementrepo = new element_repository($factory);
$tid = required_param('tid', PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
// Set the template object.
$template = template::load((int)$tid);
// Perform checks.
if ($cm = $template->get_cm()) {
require_login($cm->course, false, $cm);
} else {
require_login();
}
// Make sure the user has the required capabilities.
$template->require_manage();
if ($template->get_context()->contextlevel == CONTEXT_MODULE) {
$customcert = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
$title = $customcert->name;
} else {
$title = $SITE->fullname;
}
if ($action == 'edit') {
// The id of the element must be supplied if we are currently editing one.
$id = required_param('id', PARAM_INT);
$element = $elementrepo->get_by_id_or_fail($id);
$pageurl = new moodle_url('/mod/customcert/edit_element.php', ['id' => $id, 'tid' => $tid, 'action' => $action]);
} else { // Must be adding an element.
// We need to supply what element we want added to what page.
$pageid = required_param('pageid', PARAM_INT);
$element = new stdClass();
$element->element = required_param('element', PARAM_ALPHA);
$pageurl = new moodle_url('/mod/customcert/edit_element.php', ['tid' => $tid, 'element' => $element->element,
'pageid' => $pageid, 'action' => $action]);
}
// Set up the page.
page_helper::page_setup($pageurl, $template->get_context(), $title);
$PAGE->activityheader->set_attrs(['hidecompletion' => true,
'description' => '']);
// Additional page setup.
if ($template->get_context()->contextlevel == CONTEXT_SYSTEM) {
$PAGE->navbar->add(
get_string('managetemplates', 'customcert'),
new moodle_url('/mod/customcert/manage_templates.php')
);
}
$PAGE->navbar->add(get_string('editcustomcert', 'customcert'), new moodle_url(
'/mod/customcert/edit.php',
['tid' => $tid]
));
$PAGE->navbar->add(get_string('editelement', 'customcert'));
$mform = new edit_element_form($pageurl, ['element' => $element, 'factory' => $factory]);
// Check if they cancelled.
if ($mform->is_cancelled()) {
$url = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
redirect($url);
}
if ($data = $mform->get_data()) {
// Set the id, or page id depending on if we are editing an element, or adding a new one.
if ($action == 'edit') {
$data->id = $id;
$data->pageid = $element->pageid;
} else {
$data->pageid = $pageid;
}
// Set the element variable.
$data->element = $element->element;
// Normalise submission: process file uploads from draft areas before saving.
$formservice = new form_service();
$dataarray = (array) $data;
$formservice->normalise_submission($dataarray);
// Merge back any changes (e.g., file metadata populated from fileid).
foreach ($dataarray as $key => $value) {
$data->$key = $value;
}
// Get an instance of the element class.
$elementinstance = $factory->create_from_legacy_record($data);
if ($elementinstance) {
// Build record similar to legacy element::save_form_elements().
$record = new stdClass();
$record->pageid = (int)$data->pageid;
$record->element = (string)$data->element;
$record->name = $data->name;
if (!empty($data->id)) {
$record->id = (int)$data->id;
// Preserve existing positional fields when not provided in the form.
$record->posx = $element->posx ?? null;
$record->posy = $element->posy ?? null;
$record->refpoint = $element->refpoint ?? null;
$record->alignment = $element->alignment ?? element::ALIGN_LEFT;
}
// Persist JSON using helper (supports persistable and legacy elements).
$record->data = persistence_helper::to_json_data($elementinstance, $data);
// Merge font-related fields into JSON 'data' rather than separate DB columns.
$rawjson = $record->data;
$decoded = is_string($rawjson) && $rawjson !== '' ? json_decode($rawjson, true) : null;
if (!is_array($decoded)) {
// Start from an envelope if we had scalar/non-JSON previously.
$decoded = [];
}
if (isset($data->font) && $data->font !== '') {
$decoded['font'] = (string)$data->font;
}
if (isset($data->fontsize) && $data->fontsize !== '') {
$decoded['fontsize'] = (int)$data->fontsize;
}
if (isset($data->colour) && $data->colour !== '') {
$decoded['colour'] = (string)$data->colour;
}
if (!empty(get_config('customcert', 'showposxy'))) {
$record->posx = $data->posx ?? null;
$record->posy = $data->posy ?? null;
}
// Merge width into JSON 'data' rather than a dropped DB column.
if (isset($data->width) && $data->width !== '') {
$decoded['width'] = (int)$data->width;
}
// Persist the merged JSON payload.
$record->data = json_encode($decoded);
$record->refpoint = $data->refpoint ?? $record->refpoint ?? null;
$record->alignment = $data->alignment ?? $record->alignment ?? element::ALIGN_LEFT;
$instance = $factory->create($record->element, $record);
if (!empty($record->id)) {
$elementrepo->save($instance);
$newlyid = $record->id;
} else {
$newlyid = $elementrepo->create($instance);
}
// Trigger updated event for the template containing the element.
template_updated::create_from_template($template)->trigger();
$url = new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]);
$editurl = new moodle_url('/mod/customcert/edit_element.php', [
'id' => $newlyid,
'tid' => $tid,
'action' => 'edit',
]);
$redirecturl = $url;
if (isset($data->saveandcontinue)) {
$redirecturl = ($action === 'add') ? $editurl : $PAGE->url;
}
redirect($redirecturl);
}
// Element type could not be resolved; redirect back without saving.
redirect(new moodle_url('/mod/customcert/edit.php', ['tid' => $tid]));
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();