From 52fbc108a74b707f9020ee7916f9f6454d3f8a72 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sun, 24 Feb 2013 23:41:33 +0800 Subject: [PATCH] Introduced a new form element for selecting a colour on the customcert customisation page --- colourpicker.php | 75 +++++++++++++++++++++++ db/install.xml | 2 +- edit_form.php | 13 ++-- elements/element.class.php | 113 +++++++++++++++++++++++++---------- elements/grade/lib.php | 2 +- elements/studentname/lib.php | 2 +- lang/en/customcert.php | 2 +- lib.php | 1 - styles.css | 2 +- 9 files changed, 169 insertions(+), 43 deletions(-) create mode 100644 colourpicker.php diff --git a/colourpicker.php b/colourpicker.php new file mode 100644 index 0000000..40d1c00 --- /dev/null +++ b/colourpicker.php @@ -0,0 +1,75 @@ +. + +defined('MOODLE_INTERNAL') || die(); + +require_once("HTML/QuickForm/text.php"); + +/** + * Form element for handling the colour picker. + * + * @package customcert_customcert_colourpicker + * @copyright Mark Nelson + * @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 = ''; + $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; + } +} diff --git a/db/install.xml b/db/install.xml index 868b773..5b8a232 100644 --- a/db/install.xml +++ b/db/install.xml @@ -56,7 +56,7 @@ - + diff --git a/edit_form.php b/edit_form.php index 19e0b79..e782ac1 100644 --- a/edit_form.php +++ b/edit_form.php @@ -19,8 +19,11 @@ if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page } -require_once($CFG->dirroot.'/course/moodleform_mod.php'); -require_once($CFG->dirroot.'/mod/customcert/lib.php'); +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. @@ -119,13 +122,13 @@ class mod_customcert_edit_form extends moodleform { // Loop through the pages. foreach ($pages as $p) { // Set the orientation. - $element = $mform->getElement('orientation_'.$p->id); + $element = $mform->getElement('orientation_' . $p->id); $element->setValue($p->orientation); // Set the width. - $element = $mform->getElement('width_'.$p->id); + $element = $mform->getElement('width_' . $p->id); $element->setValue($p->width); // Set the height. - $element = $mform->getElement('height_'.$p->id); + $element = $mform->getElement('height_' . $p->id); $element->setValue($p->height); } } diff --git a/elements/element.class.php b/elements/element.class.php index b1f4847..55c39ae 100644 --- a/elements/element.class.php +++ b/elements/element.class.php @@ -15,7 +15,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - /** * 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] .= "
"; - $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] .= "
"; - $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)); } -} \ No newline at end of file + + /** + * 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; + } + } +} diff --git a/elements/grade/lib.php b/elements/grade/lib.php index 7fae181..17a31e2 100644 --- a/elements/grade/lib.php +++ b/elements/grade/lib.php @@ -187,4 +187,4 @@ class customcert_element_grade extends customcert_element_base { return $gradeformat; } -} \ No newline at end of file +} diff --git a/elements/studentname/lib.php b/elements/studentname/lib.php index e180502..ac39747 100644 --- a/elements/studentname/lib.php +++ b/elements/studentname/lib.php @@ -67,4 +67,4 @@ class customcert_element_studentname extends customcert_element_base { $pdf->SetXY($this->element->posx, $this->element->posy); $pdf->writeHTMLCell(0, 0, '', '', fullname($USER), 0, 0, 0, true, $align); } -} \ No newline at end of file +} diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 76c8b53..c1873d0 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -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'; diff --git a/lib.php b/lib.php index b955465..b12e5ec 100644 --- a/lib.php +++ b/lib.php @@ -38,7 +38,6 @@ function customcert_add_instance($data, $mform) { $data->timemodified = $data->timecreated; return $DB->insert_record('customcert', $data); - } /** diff --git a/styles.css b/styles.css index 86185df..f2c25c6 100644 --- a/styles.css +++ b/styles.css @@ -1,3 +1,3 @@ -.deletecertpage { +#page-mod-customcert-edit .deletecertpage { text-align:right; }