2012-12-07 09:34:46 +00:00
|
|
|
<?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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
namespace mod_customcert;
|
|
|
|
|
2013-04-10 09:17:19 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2013-02-24 15:41:33 +00:00
|
|
|
require_once($CFG->dirroot . '/course/moodleform_mod.php');
|
2013-04-11 11:09:25 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php');
|
2013-02-24 15:41:33 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
\MoodleQuickForm::registerElementType('customcert_colourpicker',
|
2013-04-11 11:09:25 +00:00
|
|
|
$CFG->dirroot . '/mod/customcert/includes/colourpicker.php', 'MoodleQuickForm_customcert_colourpicker');
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-09 04:04:15 +00:00
|
|
|
* The form for handling the layout of the customcert instance.
|
2012-12-07 09:34:46 +00:00
|
|
|
*
|
2013-04-26 07:30:33 +00:00
|
|
|
* @package mod_customcert
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2012-12-07 09:34:46 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
class edit_form extends \moodleform {
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* The id of the template being used.
|
2012-12-07 09:34:46 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
protected $tid = null;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The total number of pages for this cert.
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
protected $numpages = 1;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form definition.
|
|
|
|
*/
|
2013-07-23 06:44:11 +00:00
|
|
|
public function definition() {
|
2013-05-30 08:28:47 +00:00
|
|
|
global $DB;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->addElement('text', 'name', get_string('name', 'customcert'));
|
|
|
|
$mform->setType('name', PARAM_TEXT);
|
|
|
|
$mform->addRule('name', null, 'required');
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
// Get the number of pages for this module.
|
2016-02-16 09:03:34 +00:00
|
|
|
if (isset($this->_customdata['tid'])) {
|
|
|
|
$this->tid = $this->_customdata['tid'];
|
2016-02-20 09:46:31 +00:00
|
|
|
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->tid), 'sequence')) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$this->numpages = count($pages);
|
|
|
|
foreach ($pages as $p) {
|
|
|
|
$this->add_customcert_page_elements($p);
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
2016-02-16 09:03:34 +00:00
|
|
|
} else { // Add a new template.
|
|
|
|
// Create a 'fake' page to display the elements on - not yet saved in the DB.
|
|
|
|
$page = new \stdClass();
|
2016-02-20 10:04:56 +00:00
|
|
|
$page->id = 0;
|
2016-02-20 09:46:31 +00:00
|
|
|
$page->sequence = 1;
|
2016-02-16 09:03:34 +00:00
|
|
|
$this->add_customcert_page_elements($page);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->closeHeaderBefore('addcertpage');
|
|
|
|
|
|
|
|
$mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
|
|
|
|
|
2013-05-16 09:12:51 +00:00
|
|
|
$mform->closeHeaderBefore('submitbtn');
|
|
|
|
|
2013-05-03 10:51:41 +00:00
|
|
|
// Add the submit buttons.
|
|
|
|
$group = array();
|
2013-05-16 09:12:51 +00:00
|
|
|
$group[] = $mform->createElement('submit', 'submitbtn', get_string('savechanges'));
|
2013-05-17 03:05:42 +00:00
|
|
|
$group[] = $mform->createElement('submit', 'previewbtn', get_string('savechangespreview', 'customcert'));
|
2013-05-16 09:12:51 +00:00
|
|
|
$mform->addElement('group', 'submitbtngroup', '', $group, '', false);
|
2013-05-03 10:51:41 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->addElement('hidden', 'tid');
|
|
|
|
$mform->setType('tid', PARAM_INT);
|
|
|
|
$mform->setDefault('tid', $this->tid);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-09 04:04:15 +00:00
|
|
|
* Fill in the current page data for this customcert.
|
2012-12-07 09:34:46 +00:00
|
|
|
*/
|
2013-07-23 06:44:11 +00:00
|
|
|
public function definition_after_data() {
|
2012-12-07 09:34:46 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$mform = $this->_form;
|
|
|
|
|
|
|
|
// Check that we are updating a current customcert.
|
2016-02-16 09:03:34 +00:00
|
|
|
if ($this->tid) {
|
2013-04-09 04:04:15 +00:00
|
|
|
// Get the pages for this customcert.
|
2016-02-16 09:03:34 +00:00
|
|
|
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->tid))) {
|
2012-12-07 09:34:46 +00:00
|
|
|
// Loop through the pages.
|
|
|
|
foreach ($pages as $p) {
|
|
|
|
// Set the width.
|
2013-05-30 08:28:47 +00:00
|
|
|
$element = $mform->getElement('pagewidth_' . $p->id);
|
2012-12-07 09:34:46 +00:00
|
|
|
$element->setValue($p->width);
|
|
|
|
// Set the height.
|
2013-05-30 08:28:47 +00:00
|
|
|
$element = $mform->getElement('pageheight_' . $p->id);
|
2012-12-07 09:34:46 +00:00
|
|
|
$element->setValue($p->height);
|
2015-12-08 09:41:38 +00:00
|
|
|
// Set the left margin.
|
|
|
|
$element = $mform->getElement('pageleftmargin_' . $p->id);
|
|
|
|
$element->setValue($p->leftmargin);
|
|
|
|
// Set the right margin.
|
|
|
|
$element = $mform->getElement('pagerightmargin_' . $p->id);
|
|
|
|
$element->setValue($p->rightmargin);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some basic validation.
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
* @param $files
|
|
|
|
* @return array the errors that were found
|
|
|
|
*/
|
|
|
|
public function validation($data, $files) {
|
2016-02-16 09:03:34 +00:00
|
|
|
global $DB;
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
$errors = parent::validation($data, $files);
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Check that the template name does not already exist for another template.
|
|
|
|
if ($template = $DB->get_record('customcert_templates', array('name' => $data['name']))) {
|
|
|
|
if (empty($data['tid']) || $template->id != $data['tid']) {
|
|
|
|
$errors['name'] = get_string('customcertnameexists', 'customcert');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 01:29:16 +00:00
|
|
|
// Go through the data and check any width, height or margin values.
|
2012-12-07 09:34:46 +00:00
|
|
|
foreach ($data as $key => $value) {
|
2013-05-30 08:28:47 +00:00
|
|
|
if (strpos($key, 'pagewidth_') !== false) {
|
|
|
|
$page = str_replace('pagewidth_', '', $key);
|
|
|
|
$widthid = 'pagewidth_' . $page;
|
2013-02-20 12:21:57 +00:00
|
|
|
// Validate that the width is a valid value.
|
2013-04-24 09:43:49 +00:00
|
|
|
if ((!isset($data[$widthid])) || (!is_numeric($data[$widthid])) || ($data[$widthid] <= 0)) {
|
2013-05-30 08:28:47 +00:00
|
|
|
$errors[$widthid] = get_string('invalidwidth', 'customcert');
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-30 08:28:47 +00:00
|
|
|
if (strpos($key, 'pageheight_') !== false) {
|
|
|
|
$page = str_replace('pageheight_', '', $key);
|
|
|
|
$heightid = 'pageheight_' . $page;
|
2012-12-07 09:34:46 +00:00
|
|
|
// Validate that the height is a valid value.
|
2013-04-24 09:43:49 +00:00
|
|
|
if ((!isset($data[$heightid])) || (!is_numeric($data[$heightid])) || ($data[$heightid] <= 0)) {
|
2013-05-30 08:28:47 +00:00
|
|
|
$errors[$heightid] = get_string('invalidheight', 'customcert');
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 09:41:38 +00:00
|
|
|
if (strpos($key, 'pageleftmargin_') !== false) {
|
|
|
|
// Validate that the left margin is a valid value.
|
|
|
|
if (isset($data[$key]) && ($data[$key] < 0)) {
|
|
|
|
$errors[$key] = get_string('invalidmargin', 'customcert');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strpos($key, 'pagerightmargin_') !== false) {
|
|
|
|
// Validate that the right margin is a valid value.
|
2015-07-29 01:29:16 +00:00
|
|
|
if (isset($data[$key]) && ($data[$key] < 0)) {
|
|
|
|
$errors[$key] = get_string('invalidmargin', 'customcert');
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the page elements to the form.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param \stdClass $page the customcert page
|
2013-07-25 10:36:43 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
protected function add_customcert_page_elements($page) {
|
2013-06-06 10:59:08 +00:00
|
|
|
global $DB, $OUTPUT;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
// Create the form object.
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
if ($this->numpages > 1) {
|
2016-02-20 09:46:31 +00:00
|
|
|
$mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->sequence));
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$editlink = '/mod/customcert/edit.php';
|
|
|
|
$editlinkparams = array('tid' => $this->tid);
|
|
|
|
$editelementlink = '/mod/customcert/edit_element.php';
|
|
|
|
$editelementlinkparams = array('tid' => $this->tid);
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
// Place the ordering arrows.
|
|
|
|
// Only display the move up arrow if it is not the first.
|
2016-02-20 09:46:31 +00:00
|
|
|
if ($page->sequence > 1) {
|
2016-02-20 09:12:32 +00:00
|
|
|
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmoveup', 'aid' => $page->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/up', get_string('moveup'))));
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
// Only display the move down arrow if it is not the last.
|
2016-02-20 09:46:31 +00:00
|
|
|
if ($page->sequence < $this->numpages) {
|
2016-02-20 09:12:32 +00:00
|
|
|
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmovedown', 'aid' => $page->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown'))));
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('text', 'pagewidth_' . $page->id, get_string('width', 'customcert'));
|
|
|
|
$mform->setType('pagewidth_' . $page->id, PARAM_INT);
|
|
|
|
$mform->setDefault('pagewidth_' . $page->id, '210');
|
|
|
|
$mform->addRule('pagewidth_' . $page->id, null, 'required', null, 'client');
|
|
|
|
$mform->addHelpButton('pagewidth_' . $page->id, 'width', 'customcert');
|
|
|
|
|
|
|
|
$mform->addElement('text', 'pageheight_' . $page->id, get_string('height', 'customcert'));
|
|
|
|
$mform->setType('pageheight_' . $page->id, PARAM_INT);
|
|
|
|
$mform->setDefault('pageheight_' . $page->id, '297');
|
|
|
|
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
|
|
|
|
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2015-12-08 09:41:38 +00:00
|
|
|
$mform->addElement('text', 'pageleftmargin_' . $page->id, get_string('leftmargin', 'customcert'));
|
|
|
|
$mform->setType('pageleftmargin_' . $page->id, PARAM_INT);
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->setDefault('pageleftmargin_' . $page->id, 0);
|
2015-12-08 09:41:38 +00:00
|
|
|
$mform->addHelpButton('pageleftmargin_' . $page->id, 'leftmargin', 'customcert');
|
|
|
|
|
|
|
|
$mform->addElement('text', 'pagerightmargin_' . $page->id, get_string('rightmargin', 'customcert'));
|
|
|
|
$mform->setType('pagerightmargin_' . $page->id, PARAM_INT);
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->setDefault('pagerightmargin_' . $page->id, 0);
|
2015-12-08 09:41:38 +00:00
|
|
|
$mform->addHelpButton('pagerightmargin_' . $page->id, 'rightmargin', 'customcert');
|
2015-07-29 01:29:16 +00:00
|
|
|
|
2013-06-27 11:26:33 +00:00
|
|
|
$group = array();
|
2016-02-16 09:03:34 +00:00
|
|
|
$group[] = $mform->createElement('select', 'element_' . $page->id, '', element::get_available_types());
|
2013-06-27 11:26:33 +00:00
|
|
|
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));
|
|
|
|
$mform->addElement('group', 'elementgroup', '', $group, '', false);
|
|
|
|
|
2013-05-16 09:12:51 +00:00
|
|
|
// Check if there are elements to add.
|
2013-05-30 08:28:47 +00:00
|
|
|
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
|
2013-05-16 09:12:51 +00:00
|
|
|
// Get the total number of elements.
|
|
|
|
$numelements = count($elements);
|
2013-06-06 10:59:08 +00:00
|
|
|
// Create a table to display these elements.
|
2016-02-16 09:03:34 +00:00
|
|
|
$table = new \html_table();
|
2015-03-13 05:42:20 +00:00
|
|
|
$table->head = array(get_string('name', 'customcert'), get_string('type', 'customcert'), '');
|
2016-08-22 07:28:40 +00:00
|
|
|
$table->align = array('left', 'left', 'left');
|
2013-06-06 10:59:08 +00:00
|
|
|
// Loop through and add the elements to the table.
|
2013-05-16 09:12:51 +00:00
|
|
|
foreach ($elements as $element) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$row = new \html_table_row();
|
2013-06-06 10:59:08 +00:00
|
|
|
$row->cells[] = $element->name;
|
|
|
|
$row->cells[] = $element->element;
|
2013-06-21 06:02:49 +00:00
|
|
|
// Link to edit this element.
|
2016-02-16 09:03:34 +00:00
|
|
|
$link = new \moodle_url($editelementlink, $editelementlinkparams + array('id' => $element->id,
|
2015-03-13 05:42:20 +00:00
|
|
|
'action' => 'edit'));
|
2016-02-16 09:03:34 +00:00
|
|
|
$icons = $OUTPUT->action_icon($link, new \pix_icon('t/edit', get_string('edit')));
|
2015-03-13 05:42:20 +00:00
|
|
|
// Link to delete the element.
|
2016-02-20 09:12:32 +00:00
|
|
|
$link = new \moodle_url($editlink, $editlinkparams + array('action' => 'deleteelement',
|
|
|
|
'aid' => $element->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$icons .= $OUTPUT->action_icon($link, new \pix_icon('t/delete', get_string('delete')));
|
2013-06-06 10:59:08 +00:00
|
|
|
// Now display any moving arrows if they are needed.
|
|
|
|
if ($numelements > 1) {
|
2013-05-16 09:12:51 +00:00
|
|
|
// Only display the move up arrow if it is not the first.
|
2013-06-06 10:59:08 +00:00
|
|
|
$moveicons = '';
|
2013-05-16 09:12:51 +00:00
|
|
|
if ($element->sequence > 1) {
|
2016-02-20 09:12:32 +00:00
|
|
|
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'emoveup',
|
|
|
|
'aid' => $element->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$moveicons .= $OUTPUT->action_icon($url, new \pix_icon('t/up', get_string('moveup')));
|
2013-05-16 09:12:51 +00:00
|
|
|
}
|
|
|
|
// Only display the move down arrow if it is not the last.
|
|
|
|
if ($element->sequence < $numelements) {
|
2016-02-20 09:12:32 +00:00
|
|
|
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'emovedown',
|
|
|
|
'aid' => $element->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$moveicons .= $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown')));
|
2013-04-09 05:27:55 +00:00
|
|
|
}
|
2015-03-13 05:42:20 +00:00
|
|
|
$icons .= $moveicons;
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
2015-03-13 05:42:20 +00:00
|
|
|
$row->cells[] = $icons;
|
2013-06-06 10:59:08 +00:00
|
|
|
$table->data[] = $row;
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
2015-08-05 05:38:35 +00:00
|
|
|
// Create link to order the elements.
|
2016-02-16 09:03:34 +00:00
|
|
|
$link = \html_writer::link(new \moodle_url('/mod/customcert/rearrange.php', array('pid' => $page->id)),
|
|
|
|
get_string('rearrangeelements', 'customcert'));
|
2013-06-06 10:59:08 +00:00
|
|
|
// Add the table to the form.
|
2016-02-16 09:03:34 +00:00
|
|
|
$mform->addElement('static', 'elements_' . $page->id, get_string('elements', 'customcert'), \html_writer::table($table)
|
|
|
|
. \html_writer::tag( 'div', $link, array('style' => 'text-align:right')));
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->addHelpButton('elements_' . $page->id, 'elements', 'customcert');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add option to delete this page if there is more than one page.
|
|
|
|
if ($this->numpages > 1) {
|
|
|
|
// Link to delete the element.
|
2016-02-20 09:12:32 +00:00
|
|
|
$deletelink = new \moodle_url($editlink, $editlinkparams + array('action' => 'deletepage', 'aid' => $page->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
$deletelink = \html_writer::tag('a', get_string('deletecertpage', 'customcert'), array('href' => $deletelink->out(false), 'class' => 'deletebutton'));
|
|
|
|
$mform->addElement('html', \html_writer::tag('div', $deletelink, array('class' => 'deletebutton')));
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|