Introduced a new form element for selecting a colour on the customcert customisation page
This commit is contained in:
parent
0c66a5242f
commit
52fbc108a7
9 changed files with 169 additions and 43 deletions
75
colourpicker.php
Normal file
75
colourpicker.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once("HTML/QuickForm/text.php");
|
||||
|
||||
/**
|
||||
* Form element for handling the colour picker.
|
||||
*
|
||||
* @package customcert_customcert_colourpicker
|
||||
* @copyright Mark Nelson <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class MoodleQuickForm_customcert_colourpicker extends HTML_QuickForm_text {
|
||||
|
||||
/**
|
||||
* The string for the help icon, if empty then no help icon will be displayed.
|
||||
*/
|
||||
public $_helpbutton = '';
|
||||
|
||||
/**
|
||||
* Constructor for the colour picker.
|
||||
*
|
||||
* @param string $elementName
|
||||
* @param string $elementLabel
|
||||
* @param array $attributes
|
||||
*/
|
||||
function MoodleQuickForm_customcert_colourpicker($elementName = null, $elementLabel = null, $attributes = null) {
|
||||
parent::HTML_QuickForm_text($elementName, $elementLabel, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the html string to display this element.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toHtml() {
|
||||
global $PAGE, $OUTPUT;
|
||||
|
||||
$PAGE->requires->js_init_call('M.util.init_colour_picker', array($this->getAttribute('id'), null));
|
||||
$content = '<label class="accesshide" for="' . $this->getAttribute('id') . '" >' . $this->getLabel() . '</label>';
|
||||
$content .= html_writer::start_tag('div', array('class' => 'form-colourpicker defaultsnext'));
|
||||
$content .= html_writer::tag('div', $OUTPUT->pix_icon('i/loading', get_string('loading', 'admin'), 'moodle',
|
||||
array('class' => 'loadingicon')), array('class' => 'admin_colourpicker clearfix'));
|
||||
$content .= html_writer::empty_tag('input', array('type' => 'text', 'id' => $this->getAttribute('id'),
|
||||
'name' => $this->getName(), 'value' => $this->getValue(), 'size' => '12'));
|
||||
$content .= html_writer::end_tag('div');
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the html for the help button.
|
||||
*
|
||||
* @return string html for help button
|
||||
*/
|
||||
public function getHelpButton(){
|
||||
return $this->_helpbutton;
|
||||
}
|
||||
}
|
|
@ -56,7 +56,7 @@
|
|||
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="element" NEXT="font"/>
|
||||
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="data" NEXT="size"/>
|
||||
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="font" NEXT="colour"/>
|
||||
<FIELD NAME="colour" TYPE="char" LENGTH="6" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="size" NEXT="posx"/>
|
||||
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="size" NEXT="posx"/>
|
||||
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="colour" NEXT="posy"/>
|
||||
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="posx" NEXT="timecreated"/>
|
||||
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="posy"/>
|
||||
|
|
|
@ -21,6 +21,9 @@ if (!defined('MOODLE_INTERNAL')) {
|
|||
|
||||
require_once($CFG->dirroot . '/course/moodleform_mod.php');
|
||||
require_once($CFG->dirroot . '/mod/customcert/lib.php');
|
||||
require_once($CFG->dirroot . '/mod/customcert/colourpicker.php');
|
||||
|
||||
MoodleQuickForm::registerElementType('customcert_colourpicker', $CFG->dirroot . '/mod/customcert/colourpicker.php', 'MoodleQuickForm_customcert_colourpicker');
|
||||
|
||||
/**
|
||||
* Instance add/edit form.
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
/**
|
||||
* The base class for the customcert elements.
|
||||
*
|
||||
|
@ -60,7 +59,7 @@ class customcert_element_base {
|
|||
$data->element = $element;
|
||||
$data->font = 'Times-Roman';
|
||||
$data->size = '12';
|
||||
$data->colour = 'FFFFFF';
|
||||
$data->colour = '#FFFFFF';
|
||||
$data->posx = '250';
|
||||
$data->posy = '250';
|
||||
$data->timecreated = time();
|
||||
|
@ -93,17 +92,17 @@ class customcert_element_base {
|
|||
// Commonly used string.
|
||||
$strrequired = get_string('required');
|
||||
|
||||
// The common group of elements.
|
||||
// The common elements.
|
||||
$mform->addElement('select', 'font_' . $id, get_string('font', 'customcert'), customcert_get_fonts());
|
||||
$mform->addElement('select', 'size_' . $id, get_string('fontsize', 'customcert'), customcert_get_font_sizes());
|
||||
$mform->addELement('text', 'colour_' . $id, get_string('fontcolour', 'customcert'), array('size' => 10, 'maxlength' => 6));
|
||||
$mform->addElement('customcert_colourpicker', 'colour_' . $id, get_string('fontcolour', 'customcert'));
|
||||
$mform->addElement('text', 'posx_' . $id, get_string('posx', 'customcert'), array('size' => 10));
|
||||
$mform->addElement('text', 'posy_' . $id, get_string('posy', 'customcert'), array('size' => 10));
|
||||
|
||||
// Set the types of these elements.
|
||||
$mform->setType('font_' . $id, PARAM_TEXT);
|
||||
$mform->setType('size_' . $id, PARAM_INT);
|
||||
$mform->setType('colour_' . $id, PARAM_RAW); // Need to validate this is a hexadecimal value.
|
||||
$mform->setType('colour_' . $id, PARAM_RAW); // Need to validate that this is a valid colour.
|
||||
$mform->setType('posx_' . $id, PARAM_INT);
|
||||
$mform->setType('posy_' . $id, PARAM_INT);
|
||||
|
||||
|
@ -140,42 +139,27 @@ class customcert_element_base {
|
|||
// The identifier.
|
||||
$id = $this->element->id;
|
||||
|
||||
// Get the group name.
|
||||
$group = 'elementfieldgroup_' . $id;
|
||||
|
||||
// Get the colour.
|
||||
// Validate the colour.
|
||||
$colour = 'colour_' . $id;
|
||||
$colour = $data[$colour];
|
||||
$colour = ltrim($colour, "#");
|
||||
// Check if colour is not a valid hexadecimal value.
|
||||
if(!preg_match("/[0-9A-F]{6}/i", $colour)) {
|
||||
$errors[$group] = get_string('invalidcolour', 'customcert');
|
||||
$colourdata = $data[$colour];
|
||||
if (!$this->validate_colour($colourdata)) {
|
||||
$errors[$colour] = get_string('invalidcolour', 'customcert');
|
||||
}
|
||||
|
||||
// Get position X.
|
||||
$posx = 'posx_' . $id;
|
||||
$posx = $data[$posx];
|
||||
$posxdata = $data[$posx];
|
||||
// Check if posx is not numeric or less than 0.
|
||||
if ((!is_numeric($posx)) || ($posx < 0)) {
|
||||
if (!empty($errors[$group])) {
|
||||
$errors[$group] .= "<br />";
|
||||
$errors[$group] .= get_string('invalidposition', 'customcert', 'X');
|
||||
} else {
|
||||
$errors[$group] = get_string('invalidposition', 'customcert', 'X');
|
||||
}
|
||||
if ((!is_numeric($posxdata)) || ($posxdata < 0)) {
|
||||
$errors[$posx] = get_string('invalidposition', 'customcert', 'X');
|
||||
}
|
||||
|
||||
// Get position Y.
|
||||
$posy = 'posy_' . $id;
|
||||
$posy = $data[$posy];
|
||||
$posydata = $data[$posy];
|
||||
// Check if posy is not numeric or less than 0.
|
||||
if ((!is_numeric($posy)) || ($posy < 0)) {
|
||||
if (!empty($errors[$group])) {
|
||||
$errors[$group] .= "<br />";
|
||||
$errors[$group] .= get_string('invalidposition', 'customcert', 'Y');
|
||||
} else {
|
||||
$errors[$group] = get_string('invalidposition', 'customcert', 'Y');
|
||||
}
|
||||
if ((!is_numeric($posydata)) || ($posydata < 0)) {
|
||||
$errors[$posy] = get_string('invalidposition', 'customcert', 'Y');
|
||||
}
|
||||
|
||||
return $errors;
|
||||
|
@ -207,7 +191,7 @@ class customcert_element_base {
|
|||
$element->data = $datainfo;
|
||||
$element->font = (!empty($data->$font)) ? $data->$font : null;
|
||||
$element->size = (!empty($data->$size)) ? $data->$size : null;
|
||||
$element->colour = (!empty($data->$colour)) ? ltrim($data->$colour, "#") : null;
|
||||
$element->colour = (!empty($data->$colour)) ? $data->$colour : null;
|
||||
$element->posx = (!empty($data->$posx)) ? $data->$posx : null;
|
||||
$element->posy = (!empty($data->$posy)) ? $data->$posy : null;
|
||||
|
||||
|
@ -248,4 +232,69 @@ class customcert_element_base {
|
|||
|
||||
return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the colour selected.
|
||||
*
|
||||
* @param string $data
|
||||
* @return string|false
|
||||
*/
|
||||
protected function validate_colour($colour) {
|
||||
/**
|
||||
* List of valid HTML colour names
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
$colournames = array(
|
||||
'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
|
||||
'beige', 'bisque', 'black', 'blanchedalmond', 'blue',
|
||||
'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse',
|
||||
'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson',
|
||||
'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray',
|
||||
'darkgrey', 'darkgreen', 'darkkhaki', 'darkmagenta',
|
||||
'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
|
||||
'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
|
||||
'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink',
|
||||
'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick',
|
||||
'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro',
|
||||
'ghostwhite', 'gold', 'goldenrod', 'gray', 'grey', 'green',
|
||||
'greenyellow', 'honeydew', 'hotpink', 'indianred', 'indigo',
|
||||
'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
|
||||
'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
|
||||
'lightgoldenrodyellow', 'lightgray', 'lightgrey', 'lightgreen',
|
||||
'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
|
||||
'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
|
||||
'lime', 'limegreen', 'linen', 'magenta', 'maroon',
|
||||
'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple',
|
||||
'mediumseagreen', 'mediumslateblue', 'mediumspringgreen',
|
||||
'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream',
|
||||
'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive',
|
||||
'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod',
|
||||
'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip',
|
||||
'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red',
|
||||
'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown',
|
||||
'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue',
|
||||
'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan',
|
||||
'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white',
|
||||
'whitesmoke', 'yellow', 'yellowgreen'
|
||||
);
|
||||
|
||||
if (preg_match('/^#?([[:xdigit:]]{3}){1,2}$/', $colour)) {
|
||||
return true;
|
||||
} else if (in_array(strtolower($colour), $colournames)) {
|
||||
return true;
|
||||
} else if (preg_match('/rgb\(\d{0,3}%?\, ?\d{0,3}%?, ?\d{0,3}%?\)/i', $colour)) {
|
||||
return true;
|
||||
} else if (preg_match('/rgba\(\d{0,3}%?\, ?\d{0,3}%?, ?\d{0,3}%?\, ?\d(\.\d)?\)/i', $colour)) {
|
||||
return true;
|
||||
} else if (preg_match('/hsl\(\d{0,3}\, ?\d{0,3}%, ?\d{0,3}%\)/i', $colour)) {
|
||||
return true;
|
||||
} else if (preg_match('/hsla\(\d{0,3}\, ?\d{0,3}%,\d{0,3}%\, ?\d(\.\d)?\)/i', $colour)) {
|
||||
return true;
|
||||
} else if (($colour == 'transparent') || ($colour == 'currentColor') || ($colour == 'inherit')) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -47,7 +47,7 @@ $string['height'] = 'Height';
|
|||
$string['heightnotvalid'] = 'The height has to be a valid number.';
|
||||
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
|
||||
$string['intro'] = 'Introduction';
|
||||
$string['invalidcolour'] = 'Please select a valid hexadecimal colour.';
|
||||
$string['invalidcolour'] = 'Invalid colour chosen.';
|
||||
$string['invalidposition'] = 'Please select a positive number for position {$a}.';
|
||||
$string['landscape'] = 'Landscape';
|
||||
$string['modulename'] = 'Custom Certificate';
|
||||
|
|
1
lib.php
1
lib.php
|
@ -38,7 +38,6 @@ function customcert_add_instance($data, $mform) {
|
|||
$data->timemodified = $data->timecreated;
|
||||
|
||||
return $DB->insert_record('customcert', $data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
.deletecertpage {
|
||||
#page-mod-customcert-edit .deletecertpage {
|
||||
text-align:right;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue