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 |
* This file contains the form for handling the layout of the customcert instance.
|
|
|
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 |
namespace mod_customcert;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
28 |
|
|
|
29 |
require_once($CFG->dirroot . '/course/moodleform_mod.php');
|
|
|
30 |
require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php');
|
|
|
31 |
|
|
|
32 |
\MoodleQuickForm::registerElementType('customcert_colourpicker',
|
|
|
33 |
$CFG->dirroot . '/mod/customcert/includes/colourpicker.php', 'MoodleQuickForm_customcert_colourpicker');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* The form for handling the layout of the customcert instance.
|
|
|
37 |
*
|
|
|
38 |
* @package mod_customcert
|
|
|
39 |
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
*/
|
|
|
42 |
class edit_form extends \moodleform {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* @var int The id of the template being used.
|
|
|
46 |
*/
|
|
|
47 |
protected $tid = null;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* @var int The total number of pages for this cert.
|
|
|
51 |
*/
|
|
|
52 |
protected $numpages = 1;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Form definition.
|
|
|
56 |
*/
|
|
|
57 |
public function definition() {
|
|
|
58 |
global $DB, $OUTPUT;
|
|
|
59 |
|
|
|
60 |
$mform =& $this->_form;
|
|
|
61 |
|
|
|
62 |
$mform->addElement('text', 'name', get_string('name', 'customcert'), 'maxlength="255"');
|
|
|
63 |
$mform->setType('name', PARAM_TEXT);
|
|
|
64 |
$mform->addRule('name', null, 'required');
|
|
|
65 |
|
|
|
66 |
// Get the number of pages for this module.
|
|
|
67 |
if (isset($this->_customdata['tid'])) {
|
|
|
68 |
$this->tid = $this->_customdata['tid'];
|
|
|
69 |
if ($pages = $DB->get_records('customcert_pages', ['templateid' => $this->tid], 'sequence')) {
|
|
|
70 |
$this->numpages = count($pages);
|
|
|
71 |
foreach ($pages as $p) {
|
|
|
72 |
$this->add_customcert_page_elements($p);
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
} else { // Add a new template.
|
|
|
76 |
// Create a 'fake' page to display the elements on - not yet saved in the DB.
|
|
|
77 |
$page = new \stdClass();
|
|
|
78 |
$page->id = 0;
|
|
|
79 |
$page->sequence = 1;
|
|
|
80 |
$this->add_customcert_page_elements($page);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Link to add another page, only display it when the template has been created.
|
|
|
84 |
if (isset($this->_customdata['tid'])) {
|
|
|
85 |
$addpagelink = new \moodle_url('/mod/customcert/edit.php',
|
|
|
86 |
[
|
|
|
87 |
'tid' => $this->tid,
|
|
|
88 |
'aid' => 1,
|
|
|
89 |
'action' => 'addpage',
|
|
|
90 |
'sesskey' => sesskey(),
|
|
|
91 |
]
|
|
|
92 |
);
|
|
|
93 |
$icon = $OUTPUT->pix_icon('t/switch_plus', get_string('addcertpage', 'customcert'));
|
|
|
94 |
$addpagehtml = \html_writer::link($addpagelink, $icon . get_string('addcertpage', 'customcert'));
|
|
|
95 |
$mform->addElement('html', \html_writer::tag('div', $addpagehtml, ['class' => 'addpage']));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Add the submit buttons.
|
|
|
99 |
$group = [];
|
|
|
100 |
$group[] = $mform->createElement('submit', 'submitbtn', get_string('savechanges'));
|
|
|
101 |
$group[] = $mform->createElement('submit', 'previewbtn', get_string('savechangespreview', 'customcert'), [], false);
|
|
|
102 |
$mform->addElement('group', 'submitbtngroup', '', $group, '', false);
|
|
|
103 |
|
|
|
104 |
$mform->addElement('hidden', 'tid');
|
|
|
105 |
$mform->setType('tid', PARAM_INT);
|
|
|
106 |
$mform->setDefault('tid', $this->tid);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Fill in the current page data for this customcert.
|
|
|
111 |
*/
|
|
|
112 |
public function definition_after_data() {
|
|
|
113 |
global $DB;
|
|
|
114 |
|
|
|
115 |
$mform = $this->_form;
|
|
|
116 |
|
|
|
117 |
// Check that we are updating a current customcert.
|
|
|
118 |
if ($this->tid) {
|
|
|
119 |
// Get the pages for this customcert.
|
|
|
120 |
if ($pages = $DB->get_records('customcert_pages', ['templateid' => $this->tid])) {
|
|
|
121 |
// Loop through the pages.
|
|
|
122 |
foreach ($pages as $p) {
|
|
|
123 |
// Set the width.
|
|
|
124 |
$element = $mform->getElement('pagewidth_' . $p->id);
|
|
|
125 |
$element->setValue($p->width);
|
|
|
126 |
// Set the height.
|
|
|
127 |
$element = $mform->getElement('pageheight_' . $p->id);
|
|
|
128 |
$element->setValue($p->height);
|
|
|
129 |
// Set the left margin.
|
|
|
130 |
$element = $mform->getElement('pageleftmargin_' . $p->id);
|
|
|
131 |
$element->setValue($p->leftmargin);
|
|
|
132 |
// Set the right margin.
|
|
|
133 |
$element = $mform->getElement('pagerightmargin_' . $p->id);
|
|
|
134 |
$element->setValue($p->rightmargin);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Some basic validation.
|
|
|
142 |
*
|
|
|
143 |
* @param array $data
|
|
|
144 |
* @param array $files
|
|
|
145 |
* @return array the errors that were found
|
|
|
146 |
*/
|
|
|
147 |
public function validation($data, $files) {
|
|
|
148 |
$errors = parent::validation($data, $files);
|
|
|
149 |
|
|
|
150 |
if (\core_text::strlen($data['name']) > 255) {
|
|
|
151 |
$errors['name'] = get_string('nametoolong', 'customcert');
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// Go through the data and check any width, height or margin values.
|
|
|
155 |
foreach ($data as $key => $value) {
|
|
|
156 |
if (strpos($key, 'pagewidth_') !== false) {
|
|
|
157 |
$page = str_replace('pagewidth_', '', $key);
|
|
|
158 |
$widthid = 'pagewidth_' . $page;
|
|
|
159 |
// Validate that the width is a valid value.
|
|
|
160 |
if ((!isset($data[$widthid])) || (!is_numeric($data[$widthid])) || ($data[$widthid] <= 0)) {
|
|
|
161 |
$errors[$widthid] = get_string('invalidwidth', 'customcert');
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
if (strpos($key, 'pageheight_') !== false) {
|
|
|
165 |
$page = str_replace('pageheight_', '', $key);
|
|
|
166 |
$heightid = 'pageheight_' . $page;
|
|
|
167 |
// Validate that the height is a valid value.
|
|
|
168 |
if ((!isset($data[$heightid])) || (!is_numeric($data[$heightid])) || ($data[$heightid] <= 0)) {
|
|
|
169 |
$errors[$heightid] = get_string('invalidheight', 'customcert');
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
if (strpos($key, 'pageleftmargin_') !== false) {
|
|
|
173 |
// Validate that the left margin is a valid value.
|
|
|
174 |
if (isset($data[$key]) && ($data[$key] < 0)) {
|
|
|
175 |
$errors[$key] = get_string('invalidmargin', 'customcert');
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
if (strpos($key, 'pagerightmargin_') !== false) {
|
|
|
179 |
// Validate that the right margin is a valid value.
|
|
|
180 |
if (isset($data[$key]) && ($data[$key] < 0)) {
|
|
|
181 |
$errors[$key] = get_string('invalidmargin', 'customcert');
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
return $errors;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Adds the page elements to the form.
|
|
|
191 |
*
|
|
|
192 |
* @param \stdClass $page the customcert page
|
|
|
193 |
*/
|
|
|
194 |
protected function add_customcert_page_elements($page) {
|
|
|
195 |
global $DB, $OUTPUT;
|
|
|
196 |
|
|
|
197 |
// Create the form object.
|
|
|
198 |
$mform =& $this->_form;
|
|
|
199 |
|
|
|
200 |
if ($this->numpages > 1) {
|
|
|
201 |
$mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->sequence));
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$editlink = '/mod/customcert/edit.php';
|
|
|
205 |
$editlinkparams = ['tid' => $this->tid, 'sesskey' => sesskey()];
|
|
|
206 |
$editelementlink = '/mod/customcert/edit_element.php';
|
|
|
207 |
$editelementlinkparams = ['tid' => $this->tid, 'sesskey' => sesskey()];
|
|
|
208 |
|
|
|
209 |
// Place the ordering arrows.
|
|
|
210 |
// Only display the move up arrow if it is not the first.
|
|
|
211 |
if ($page->sequence > 1) {
|
|
|
212 |
$url = new \moodle_url($editlink, $editlinkparams + ['action' => 'pmoveup', 'aid' => $page->id]);
|
|
|
213 |
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/up', get_string('moveup'))));
|
|
|
214 |
}
|
|
|
215 |
// Only display the move down arrow if it is not the last.
|
|
|
216 |
if ($page->sequence < $this->numpages) {
|
|
|
217 |
$url = new \moodle_url($editlink, $editlinkparams + ['action' => 'pmovedown', 'aid' => $page->id]);
|
|
|
218 |
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown'))));
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$mform->addElement('text', 'pagewidth_' . $page->id, get_string('width', 'customcert'));
|
|
|
222 |
$mform->setType('pagewidth_' . $page->id, PARAM_INT);
|
|
|
223 |
$mform->setDefault('pagewidth_' . $page->id, '210');
|
|
|
224 |
$mform->addRule('pagewidth_' . $page->id, null, 'required', null, 'client');
|
|
|
225 |
$mform->addHelpButton('pagewidth_' . $page->id, 'width', 'customcert');
|
|
|
226 |
|
|
|
227 |
$mform->addElement('text', 'pageheight_' . $page->id, get_string('height', 'customcert'));
|
|
|
228 |
$mform->setType('pageheight_' . $page->id, PARAM_INT);
|
|
|
229 |
$mform->setDefault('pageheight_' . $page->id, '297');
|
|
|
230 |
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
|
|
|
231 |
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');
|
|
|
232 |
|
|
|
233 |
$mform->addElement('text', 'pageleftmargin_' . $page->id, get_string('leftmargin', 'customcert'));
|
|
|
234 |
$mform->setType('pageleftmargin_' . $page->id, PARAM_INT);
|
|
|
235 |
$mform->setDefault('pageleftmargin_' . $page->id, 0);
|
|
|
236 |
$mform->addHelpButton('pageleftmargin_' . $page->id, 'leftmargin', 'customcert');
|
|
|
237 |
|
|
|
238 |
$mform->addElement('text', 'pagerightmargin_' . $page->id, get_string('rightmargin', 'customcert'));
|
|
|
239 |
$mform->setType('pagerightmargin_' . $page->id, PARAM_INT);
|
|
|
240 |
$mform->setDefault('pagerightmargin_' . $page->id, 0);
|
|
|
241 |
$mform->addHelpButton('pagerightmargin_' . $page->id, 'rightmargin', 'customcert');
|
|
|
242 |
|
|
|
243 |
// Check if there are elements to add.
|
|
|
244 |
if ($elements = $DB->get_records('customcert_elements', ['pageid' => $page->id], 'sequence ASC')) {
|
|
|
245 |
// Get the total number of elements.
|
|
|
246 |
$numelements = count($elements);
|
|
|
247 |
// Create a table to display these elements.
|
|
|
248 |
$table = new \html_table();
|
|
|
249 |
$table->attributes = ['class' => 'generaltable elementstable'];
|
|
|
250 |
$table->head = [get_string('name', 'customcert'), get_string('type', 'customcert'), get_string('actions')];
|
|
|
251 |
$table->align = ['left', 'left', 'left'];
|
|
|
252 |
// Loop through and add the elements to the table.
|
|
|
253 |
foreach ($elements as $element) {
|
|
|
254 |
$elementname = new \core\output\inplace_editable('mod_customcert', 'elementname', $element->id,
|
|
|
255 |
true, format_string($element->name), $element->name);
|
|
|
256 |
|
|
|
257 |
$row = new \html_table_row();
|
|
|
258 |
$row->cells[] = $OUTPUT->render($elementname);
|
|
|
259 |
$row->cells[] = $element->element;
|
|
|
260 |
// Link to edit this element.
|
|
|
261 |
$link = new \moodle_url($editelementlink, $editelementlinkparams + ['id' => $element->id,
|
|
|
262 |
'action' => 'edit']);
|
|
|
263 |
$icons = $OUTPUT->action_icon($link, new \pix_icon('t/edit', get_string('edit')), null,
|
|
|
264 |
['class' => 'action-icon edit-icon']);
|
|
|
265 |
// Link to delete the element.
|
|
|
266 |
$link = new \moodle_url($editlink, $editlinkparams + ['action' => 'deleteelement',
|
|
|
267 |
'aid' => $element->id]);
|
|
|
268 |
$icons .= $OUTPUT->action_icon($link, new \pix_icon('t/delete', get_string('delete')), null,
|
|
|
269 |
['class' => 'action-icon delete-icon']);
|
|
|
270 |
// Now display any moving arrows if they are needed.
|
|
|
271 |
if ($numelements > 1) {
|
|
|
272 |
// Only display the move up arrow if it is not the first.
|
|
|
273 |
$moveicons = '';
|
|
|
274 |
if ($element->sequence > 1) {
|
|
|
275 |
$url = new \moodle_url($editlink, $editlinkparams + ['action' => 'emoveup',
|
|
|
276 |
'aid' => $element->id]);
|
|
|
277 |
$moveicons .= $OUTPUT->action_icon($url, new \pix_icon('t/up', get_string('moveup')));
|
|
|
278 |
}
|
|
|
279 |
// Only display the move down arrow if it is not the last.
|
|
|
280 |
if ($element->sequence < $numelements) {
|
|
|
281 |
$url = new \moodle_url($editlink, $editlinkparams + ['action' => 'emovedown',
|
|
|
282 |
'aid' => $element->id]);
|
|
|
283 |
$moveicons .= $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown')));
|
|
|
284 |
}
|
|
|
285 |
$icons .= $moveicons;
|
|
|
286 |
}
|
|
|
287 |
$row->cells[] = $icons;
|
|
|
288 |
$table->data[] = $row;
|
|
|
289 |
}
|
|
|
290 |
// Create link to order the elements.
|
|
|
291 |
$link = \html_writer::link(new \moodle_url('/mod/customcert/rearrange.php', ['pid' => $page->id]),
|
|
|
292 |
get_string('rearrangeelements', 'customcert'));
|
|
|
293 |
// Add the table to the form.
|
|
|
294 |
$mform->addElement('static', 'elements_' . $page->id, get_string('elements', 'customcert'), \html_writer::table($table)
|
|
|
295 |
. \html_writer::tag( 'div', $link));
|
|
|
296 |
$mform->addHelpButton('elements_' . $page->id, 'elements', 'customcert');
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
$group = [];
|
|
|
300 |
$group[] = $mform->createElement('select', 'element_' . $page->id, '', element_helper::get_available_element_types());
|
|
|
301 |
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'),
|
|
|
302 |
[], false);
|
|
|
303 |
$mform->addElement('group', 'elementgroup', '', $group, '', false);
|
|
|
304 |
|
|
|
305 |
// Add option to delete this page if there is more than one page.
|
|
|
306 |
if ($this->numpages > 1) {
|
|
|
307 |
// Link to delete the page.
|
|
|
308 |
$deletelink = new \moodle_url($editlink, $editlinkparams + ['action' => 'deletepage', 'aid' => $page->id]);
|
|
|
309 |
$icon = $OUTPUT->pix_icon('t/delete', get_string('deletecertpage', 'customcert'));
|
|
|
310 |
$deletepagehtml = \html_writer::link($deletelink, $icon . get_string('deletecertpage', 'customcert'));
|
|
|
311 |
$mform->addElement('html', \html_writer::tag('div', $deletepagehtml, ['class' => 'deletebutton']));
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
}
|