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
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
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');
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/lib.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
|
|
|
|
2013-04-11 11:09:25 +00:00
|
|
|
MoodleQuickForm::registerElementType('customcert_colourpicker',
|
|
|
|
$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
|
2012-12-07 09:34:46 +00:00
|
|
|
* @copyright Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class mod_customcert_edit_form extends moodleform {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The instance id.
|
|
|
|
*/
|
2013-02-20 12:21:57 +00:00
|
|
|
private $id = null;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The total number of pages for this cert.
|
|
|
|
*/
|
2013-02-20 12:21:57 +00:00
|
|
|
private $numpages = 1;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
2013-02-20 12:21:57 +00:00
|
|
|
* The filemanager options.
|
2012-12-07 09:34:46 +00:00
|
|
|
*/
|
2013-02-20 12:21:57 +00:00
|
|
|
private $filemanageroptions = array();
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
2013-02-20 12:21:57 +00:00
|
|
|
* The array of element objects loaded on this form.
|
2012-12-07 09:34:46 +00:00
|
|
|
*/
|
2013-02-20 12:21:57 +00:00
|
|
|
private $elementobjects = array();
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form definition.
|
|
|
|
*/
|
|
|
|
function definition() {
|
2013-05-30 08:28:47 +00:00
|
|
|
global $DB;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$this->id = $this->_customdata['customcertid'];
|
|
|
|
$this->filemanageroptions = array('maxbytes' => $this->_customdata['course']->maxbytes,
|
|
|
|
'subdirs' => 1,
|
|
|
|
'accepted_types' => 'image');
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
|
|
|
// Get the number of pages for this module.
|
|
|
|
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $this->id), 'pagenumber')) {
|
|
|
|
$this->numpages = count($pages);
|
|
|
|
foreach ($pages as $p) {
|
|
|
|
$this->add_customcert_page_elements($p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert'));
|
|
|
|
|
|
|
|
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
|
|
|
|
|
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
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
$mform->addElement('hidden', 'id');
|
|
|
|
$mform->setType('id', PARAM_INT);
|
|
|
|
$mform->setDefault('id', $this->id);
|
|
|
|
$mform->addElement('hidden', 'cmid');
|
|
|
|
$mform->setType('cmid', PARAM_INT);
|
|
|
|
$mform->setDefault('cmid', $this->_customdata['cmid']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function definition_after_data() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$mform = $this->_form;
|
|
|
|
|
|
|
|
// Editing existing instance - copy existing files into draft area.
|
|
|
|
$draftitemid = file_get_submitted_draft_itemid('customcertimage');
|
|
|
|
file_prepare_draft_area($draftitemid, context_system::instance()->id, 'mod_customcert', 'image', 0, $this->filemanageroptions);
|
|
|
|
$element = $mform->getElement('customcertimage');
|
|
|
|
$element->setValue($draftitemid);
|
|
|
|
|
|
|
|
// Check that we are updating a current customcert.
|
|
|
|
if ($this->id) {
|
2013-04-09 04:04:15 +00:00
|
|
|
// Get the pages for this customcert.
|
2012-12-07 09:34:46 +00:00
|
|
|
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $this->id))) {
|
|
|
|
// Loop through the pages.
|
|
|
|
foreach ($pages as $p) {
|
|
|
|
// Set the orientation.
|
2013-02-24 15:41:33 +00:00
|
|
|
$element = $mform->getElement('orientation_' . $p->id);
|
2012-12-07 09:34:46 +00:00
|
|
|
$element->setValue($p->orientation);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some basic validation.
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
* @param $files
|
|
|
|
* @return array the errors that were found
|
|
|
|
*/
|
|
|
|
public function validation($data, $files) {
|
|
|
|
$errors = parent::validation($data, $files);
|
|
|
|
|
|
|
|
// Go through the data and check any width or height values.
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
// Go through each element and perform validation.
|
|
|
|
if (!empty($this->elementobjects)) {
|
|
|
|
foreach ($this->elementobjects as $e) {
|
|
|
|
$errors += $e->validate_form_elements($data, $files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the page elements to the form.
|
|
|
|
*
|
|
|
|
* @param stdClass $page the customcert page
|
|
|
|
**/
|
2013-05-16 09:12:51 +00:00
|
|
|
private function add_customcert_page_elements($page) {
|
2013-02-20 12:21:57 +00:00
|
|
|
global $CFG, $DB, $OUTPUT;
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
// Create the form object.
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->pagenumber));
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
// Place the ordering arrows.
|
|
|
|
// Only display the move up arrow if it is not the first.
|
2013-05-30 08:28:47 +00:00
|
|
|
if ($page->pagenumber > 1) {
|
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'moveup' => $page->id));
|
2012-12-07 09:34:46 +00:00
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup'))));
|
|
|
|
}
|
|
|
|
// Only display the move down arrow if it is not the last.
|
2013-05-30 08:28:47 +00:00
|
|
|
if ($page->pagenumber < $this->numpages) {
|
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'movedown' => $page->id));
|
2012-12-07 09:34:46 +00:00
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))));
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
$orientationoptions = array('L' => get_string('landscape', 'customcert'),
|
|
|
|
'P' => get_string('portrait', 'customcert'));
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('select', 'orientation_' . $page->id, get_string('orientation', 'customcert'), $orientationoptions);
|
|
|
|
$mform->setDefault('orientation_' . $page->id, 'P');
|
|
|
|
$mform->addHelpButton('orientation_' . $page->id, 'orientation', 'customcert');
|
|
|
|
|
|
|
|
$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
|
|
|
|
2013-02-22 09:25:14 +00:00
|
|
|
$group = array();
|
2013-05-30 08:28:47 +00:00
|
|
|
$group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements());
|
|
|
|
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));
|
2013-02-22 09:25:14 +00:00
|
|
|
$mform->addElement('group', 'elementgroup', '', $group, '', false);
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('submit', 'addcertpage_' . $page->id, get_string('addcertpage', 'customcert'));
|
2013-05-16 09:12:51 +00:00
|
|
|
|
|
|
|
// Add option to delete this page if there is more than one page.
|
|
|
|
if ($this->numpages > 1) {
|
2013-04-09 07:52:24 +00:00
|
|
|
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton')));
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('submit', 'deletecertpage_' . $page->id, get_string('deletecertpage', 'customcert'));
|
2013-02-22 09:35:49 +00:00
|
|
|
$mform->addElement('html', html_writer::end_tag('div'));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
// Loop through and add the ones present.
|
|
|
|
foreach ($elements as $element) {
|
|
|
|
$classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php";
|
|
|
|
// It's possible this element was added to the database then the folder was deleted, if
|
|
|
|
// this is the case we do not want to render these elements as an error will occur.
|
|
|
|
if (file_exists($classfile)) {
|
|
|
|
// Add element header.
|
2013-05-30 08:28:47 +00:00
|
|
|
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $page->pagenumber) . " - " .
|
2013-05-16 09:12:51 +00:00
|
|
|
get_string('pluginname', 'customcertelement_' . $element->element));
|
|
|
|
// We do not need to expand these elements if the modified time is greater than the created time as it
|
|
|
|
// means the values have already been altered by the user - ie. the element has not just been created.
|
|
|
|
if ($element->timemodified > $element->timecreated) {
|
|
|
|
$mform->setExpanded('headerelement_' . $element->id, false);
|
|
|
|
} else {
|
|
|
|
$mform->setExpanded('headerelement_' . $element->id, true);
|
|
|
|
}
|
|
|
|
// Only display the move up arrow if it is not the first.
|
|
|
|
if ($element->sequence > 1) {
|
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emoveup' => $element->id));
|
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup'))));
|
|
|
|
}
|
|
|
|
// Only display the move down arrow if it is not the last.
|
|
|
|
if ($element->sequence < $numelements) {
|
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emovedown' => $element->id));
|
|
|
|
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))));
|
2013-04-09 05:27:55 +00:00
|
|
|
}
|
2013-05-16 09:12:51 +00:00
|
|
|
// Add the page number to the element so we can use within the element.
|
2013-05-30 08:28:47 +00:00
|
|
|
$element->pagenum = $page->pagenumber;
|
2013-05-16 09:12:51 +00:00
|
|
|
// Get the classname.
|
|
|
|
$classname = "customcert_element_{$element->element}";
|
|
|
|
$e = new $classname($element);
|
|
|
|
$e->render_form_elements($mform);
|
|
|
|
// Add this to the objects array.
|
|
|
|
$this->elementobjects[] = $e;
|
|
|
|
// Add submit button to delete this.
|
|
|
|
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton')));
|
|
|
|
$mform->addElement('submit', 'deleteelement_' . $element->id, get_string('deleteelement', 'customcert'));
|
|
|
|
$mform->addElement('html', html_writer::end_tag('div'));
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|