Completely refactored the code for editing a custom certificate

The elements for each page are listed in a table below the other page settings, meaning each page has a separate table for listing their elements. Each element is now edited in a pop-up, rather than having all the element settings displayed at once on the form causing it to look overcrowded. There is also no longer any need for the function add_element to add default values for the elements when they are added, as a a form is shown when you click to add an element where you can enter the values before it is created.

Note: I did not create a db/upgrade.php script to add the new database columns as this module should not be currently used by anyone.
This commit is contained in:
Mark Nelson 2013-06-06 18:59:08 +08:00
parent f7029178ad
commit fa5d45eef3
19 changed files with 521 additions and 384 deletions

View file

@ -53,7 +53,8 @@
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="pageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/> <FIELD NAME="pageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="element" TYPE="text" LENGTH="big" NOTNULL="true" SEQUENCE="false" ENUM="false"/> <FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="element" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
@ -100,7 +101,8 @@
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="templatepageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/> <FIELD NAME="templatepageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="element" TYPE="text" LENGTH="big" NOTNULL="true" SEQUENCE="false" ENUM="false"/> <FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="element" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/> <FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>

110
edit.php
View file

@ -66,6 +66,9 @@ if ($data = $savetemplateform->get_data()) {
redirect($url); redirect($url);
} }
// Flag to determine if we are deleting anything.
$deleting = false;
// Check if they are moving a custom certificate page. // Check if they are moving a custom certificate page.
if ((!empty($moveup)) || (!empty($movedown))) { if ((!empty($moveup)) || (!empty($movedown))) {
// Check if we are moving a page up. // Check if we are moving a page up.
@ -99,15 +102,56 @@ if ((!empty($moveup)) || (!empty($movedown))) {
$DB->set_field('customcert_elements', 'sequence', $swapcertelement->sequence, array('id' => $movecertelement->id)); $DB->set_field('customcert_elements', 'sequence', $swapcertelement->sequence, array('id' => $movecertelement->id));
$DB->set_field('customcert_elements', 'sequence', $movecertelement->sequence, array('id' => $swapcertelement->id)); $DB->set_field('customcert_elements', 'sequence', $movecertelement->sequence, array('id' => $swapcertelement->id));
} }
} else if ((!empty($deletepage)) && (!empty($confirm))) { // Check if we are deleting a page. } else if (!empty($deletepage)) { // Check if we are deleting a page.
if (!empty($confirm)) { // Check they have confirmed the deletion
customcert_delete_page($deletepage); customcert_delete_page($deletepage);
} else if ((!empty($deleteelement)) && (!empty($confirm))) { // Check if we are deleting an element. } else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deletepageconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
'deletepage' => $deletepage,
'confirm' => 1,
'sesskey' => sesskey()));
}
} else if (!empty($deleteelement)) { // Check if we are deleting an element.
if (!empty($confirm)) { // Check they have confirmed the deletion
// Ensure element exists and delete it. // Ensure element exists and delete it.
$element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST); $element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST);
// Get an instance of the element class. // Get an instance of the element class.
if ($e = customcert_get_element_instance($element)) { if ($e = customcert_get_element_instance($element)) {
$e->delete_element(); $e->delete_element();
} }
} else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deleteelementconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
'deleteelement' => $deleteelement,
'confirm' => 1,
'sesskey' => sesskey()));
}
}
// Check if we are deleting either a page or an element.
if ($deleting) {
// Show a confirmation page.
$strheading = get_string('deleteconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$PAGE->set_heading($course->fullname);
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
} }
$mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id, $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id,
@ -121,63 +165,27 @@ if ($data = $mform->get_data()) {
// Save any page data. // Save any page data.
customcert_save_page_data($data); customcert_save_page_data($data);
// Flag to determine if we are deleting anything. // Check if we are adding a page.
$deleting = false; if (!empty($data->addcertpage)) { // Check if they chose to add a page.
customcert_add_page($data);
}
// Loop through the data. // Loop through the data.
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
// Check if they requested to delete a page. if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
if (strpos($key, 'deletecertpage_') !== false) {
// Get the pageid.
$pageid = str_replace('deletecertpage_', '', $key);
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deletepageconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
'deletepage' => $pageid,
'confirm' => 1,
'sesskey' => sesskey()));
} else if (strpos($key, 'deleteelement_') !== false) { // Check if they requested to delete a page element.
// Get the element id.
$elementid = str_replace('deleteelement_', '', $key);
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deleteelementconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
'deleteelement' => $elementid,
'confirm' => 1,
'sesskey' => sesskey()));
} else if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
// Get the page id. // Get the page id.
$pageid = str_replace('addelement_', '', $key); $pageid = str_replace('addelement_', '', $key);
// Get the element. // Get the element.
$element = "element_" . $pageid; $element = "element_" . $pageid;
$element = $data->$element; $element = $data->$element;
customcert_add_element($element, $pageid); // Create the URL to redirect to to add this element.
} else if (strpos($key, 'addcertpage_') !== false) { // Check if they chose to add a page. $params = array();
$data->pageid = str_replace('addcertpage_', '', $key); $params['cmid'] = $cmid;
customcert_add_page($data); $params['action'] = 'add';
} $params['element'] = $element;
$params['pageid'] = $pageid;
// Check if we are deleting either a page or an element. $url = new moodle_url('/mod/customcert/edit_element.php', $params);
if ($deleting) { redirect($url);
// Show a confirmation page.
$strheading = get_string('deleteconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
} }
} }

109
edit_element.php Normal file
View file

@ -0,0 +1,109 @@
<?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/>.
/**
* Edit a customcert element.
*
* @package mod_customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/customcert/edit_element_form.php');
require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
$cmid = required_param('cmid', PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
$popup = optional_param('popup', '0', PARAM_INT);
$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST);
$context = context_module::instance($cm->id);
if ($action == 'edit') {
// The id of the element must be supplied if we are currently editing one.
$id = required_param('id', PARAM_INT);
$element = $DB->get_record('customcert_elements', array('id' => $id), '*', MUST_EXIST);
$pageurl = new moodle_url('/mod/customcert/edit_element.php', array('id' => $id, 'cmid' => $cmid, 'action' => $action, 'popup' => $popup));
} else { // Must be adding an element.
// Page id must be supplied in order to add an element.
$pageid = required_param('pageid', PARAM_INT);
// Create the new element object, will have no data.
$element = new stdClass();
$element->element = required_param('element', PARAM_ALPHA);
// Set the page url.
$params = array();
$params['cmid'] = $cmid;
$params['action'] = 'add';
$params['element'] = $element->element;
$params['pageid'] = $pageid;
$params['popup'] = $popup;
$pageurl = new moodle_url('/mod/customcert/edit_element.php', $params);
}
require_login($course, false, $cm);
require_capability('mod/customcert:manage', $context);
if ($popup) {
$PAGE->set_pagelayout('popup');
} else {
$PAGE->set_heading($course->fullname);
}
$PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name)));
$PAGE->set_url($pageurl);
$mform = new mod_customcert_edit_element_form($pageurl, array('element' => $element, 'cmid' => $cmid));
// Check if they cancelled.
if ($mform->is_cancelled()) {
if ($popup) {
close_window();
} else {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
redirect($url);
}
}
if ($data = $mform->get_data()) {
// Set the id, or page id depending on if we are editing an element, or adding a new one.
if ($action == 'edit') {
$data->id = $id;
} else {
$data->pageid = $pageid;
}
// Set the element variable.
$data->element = $element->element;
// Get an instance of the element class.
if ($e = customcert_get_element_instance($data)) {
$e->save_form_elements($data);
}
if ($popup) {
close_window();
} else {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
redirect($url);
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcustomcert', 'customcert'));
$mform->display();
echo $OUTPUT->footer();

78
edit_element_form.php Normal file
View file

@ -0,0 +1,78 @@
<?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('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php');
MoodleQuickForm::registerElementType('customcert_colourpicker',
$CFG->dirroot . '/mod/customcert/includes/colourpicker.php', 'MoodleQuickForm_customcert_colourpicker');
/**
* The form for handling editing a customcert element.
*
* @package mod_customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_edit_element_form extends moodleform {
/**
* The element object.
*/
private $element;
/**
* Form definition.
*/
function definition() {
$mform =& $this->_form;
$element = $this->_customdata['element'];
// Add the field for the name of the variable, this is required for all elements.
$mform->addElement('text', 'name', get_string('elementname', 'customcert'));
$mform->setType('name', PARAM_TEXT);
$mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('name', 'elementname', 'customcert');
$this->element = customcert_get_element_instance($element);
$this->element->render_form_elements($mform);
$this->add_action_buttons(true);
}
/**
* Fill in the current page data for this customcert.
*/
function definition_after_data() {
$this->element->definition_after_data($this->_form);
}
/**
* Validation.
*
* @param $data
* @param $files
* @return array the errors that were found
*/
public function validation($data, $files) {
return $this->element->validate_form_elements($data, $files);
}
}

View file

@ -18,7 +18,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/course/moodleform_mod.php'); require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/customcert/lib.php');
require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php'); require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php');
MoodleQuickForm::registerElementType('customcert_colourpicker', MoodleQuickForm::registerElementType('customcert_colourpicker',
@ -48,11 +47,6 @@ class mod_customcert_edit_form extends moodleform {
*/ */
private $filemanageroptions = array(); private $filemanageroptions = array();
/**
* The array of element objects loaded on this form.
*/
private $elementobjects = array();
/** /**
* Form definition. * Form definition.
*/ */
@ -74,6 +68,10 @@ class mod_customcert_edit_form extends moodleform {
} }
} }
$mform->closeHeaderBefore('addcertpage');
$mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
$mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert')); $mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert'));
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
@ -158,13 +156,6 @@ class mod_customcert_edit_form extends moodleform {
} }
} }
// Go through each element and perform validation.
if (!empty($this->elementobjects)) {
foreach ($this->elementobjects as $e) {
$errors += $e->validate_form_elements($data, $files);
}
}
return $errors; return $errors;
} }
@ -174,7 +165,7 @@ class mod_customcert_edit_form extends moodleform {
* @param stdClass $page the customcert page * @param stdClass $page the customcert page
**/ **/
private function add_customcert_page_elements($page) { private function add_customcert_page_elements($page) {
global $CFG, $DB, $OUTPUT; global $DB, $OUTPUT;
// Create the form object. // Create the form object.
$mform =& $this->_form; $mform =& $this->_form;
@ -211,64 +202,74 @@ class mod_customcert_edit_form extends moodleform {
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client'); $mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert'); $mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');
// Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// Get the total number of elements.
$numelements = count($elements);
// Create a table to display these elements.
$table = new html_table();
$table->head = array(get_string('name', 'customcert'), get_string('type', 'customcert'), '', '');
// If we have more than one element then we can change the order, so add extra column for the up and down arrow.
if ($numelements > 1) {
$table->head[] = '';
}
$table->align = array('left', 'left', 'center', 'center');
if ($numelements > 1) {
$table->align[] = 'center';
}
// Loop through and add the elements to the table.
foreach ($elements as $element) {
$row = new html_table_row();
$row->cells[] = $element->name;
$row->cells[] = $element->element;
// Link to delete the element.
$deletelink = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'deleteelement' => $element->id));
$deletelink = html_writer::tag('a', get_string('delete', 'customcert'), array('href' => $deletelink->out(false)));
$row->cells[] = $deletelink;
// Link to configure this element.
$params = array();
$params['id'] = $element->id;
$params['cmid'] = $this->_customdata['cmid'];
$params['action'] = 'edit';
$nopopupconfigurelink = new moodle_url('/mod/customcert/edit_element.php', $params);
$params['popup'] = 1;
$popupconfigurelink = new moodle_url('/mod/customcert/edit_element.php', $params);
$action = new popup_action('click', $popupconfigurelink, 'edit_element_popup', array('height' => 400, 'width' => 600));
$configurelink = $OUTPUT->action_link($nopopupconfigurelink->out(false), get_string('configure', 'customcert'), $action);
$row->cells[] = $configurelink;
// Now display any moving arrows if they are needed.
if ($numelements > 1) {
// Only display the move up arrow if it is not the first.
$moveicons = '';
if ($element->sequence > 1) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emoveup' => $element->id));
$moveicons .= $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));
$moveicons .= $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
}
$row->cells[] = $moveicons;
}
$table->data[] = $row;
}
// Add the table to the form.
$mform->addElement('static', 'elements_' . $page->id, get_string('elements', 'customcert'), html_writer::table($table));
$mform->addHelpButton('elements_' . $page->id, 'elements', 'customcert');
}
$group = array(); $group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements()); $group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert')); $group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false); $mform->addElement('group', 'elementgroup', '', $group, '', false);
$mform->addElement('submit', 'addcertpage_' . $page->id, get_string('addcertpage', 'customcert'));
// Add option to delete this page if there is more than one page. // Add option to delete this page if there is more than one page.
if ($this->numpages > 1) { if ($this->numpages > 1) {
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton'))); // Link to delete the element.
$mform->addElement('submit', 'deletecertpage_' . $page->id, get_string('deletecertpage', 'customcert')); $deletelink = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'deletepage' => $page->id));
$mform->addElement('html', html_writer::end_tag('div')); $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')));
// Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// 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.
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $page->pagenumber) . " - " .
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'))));
}
// Add the page number to the element so we can use within the element.
$element->pagenum = $page->pagenumber;
// 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'));
}
}
} }
} }
} }

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013060500; $plugin->version = 2013060600;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_code'; $plugin->component = 'customcertelement_code';

View file

@ -26,38 +26,49 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
require_once($CFG->dirroot . '/mod/customcert/elements/grade/lib.php');
class customcert_element_date extends customcert_element_base { class customcert_element_date extends customcert_element_base {
/**
* Constructor.
*
* @param stdClass $element the element data
*/
function __construct($element) {
parent::__construct($element);
// Set the item and format for this element.
$dateitem = '';
$dateformat = '';
if (!empty($this->element->data)) {
$dateinfo = json_decode($this->element->data);
$dateitem = $dateinfo->dateitem;
$dateformat = $dateinfo->dateformat;
}
$this->element->dateitem = $dateitem;
$this->element->dateformat = $dateformat;
}
/** /**
* This function renders the form elements when adding a customcert element. * This function renders the form elements when adding a customcert element.
* *
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
$dateitem = '';
$dateformat = '';
// Check if there is any data for this element.
if (!empty($this->element->data)) {
$dateinfo = json_decode($this->element->data);
$dateitem = $dateinfo->dateitem;
$dateformat = $dateinfo->dateformat;
}
// Get the possible date options. // Get the possible date options.
$dateoptions = array(); $dateoptions = array();
$dateoptions['1'] = get_string('issueddate', 'certificate'); $dateoptions['1'] = get_string('issueddate', 'certificate');
$dateoptions['2'] = get_string('completiondate', 'certificate'); $dateoptions['2'] = get_string('completiondate', 'certificate');
$dateoptions = $dateoptions + customcert_element_grade::get_grade_items(); $dateoptions = $dateoptions + customcert_element_grade::get_grade_items();
$mform->addElement('select', 'dateitem_' . $this->element->id, get_string('dateitem', 'customcertelement_date'), $dateoptions); $mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions);
$mform->setDefault('dateitem_' . $this->element->id, $dateitem); $mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date');
$mform->addHelpButton('dateitem_' . $this->element->id, 'dateitem', 'customcertelement_date');
$mform->addElement('select', 'dateformat_' . $this->element->id, get_string('dateformat', 'customcertelement_date'), customcert_element_date::get_date_formats()); $mform->addElement('select', 'dateformat', get_string('dateformat', 'customcertelement_date'), $this->get_date_formats());
$mform->setDefault('dateformat_' . $this->element->id, $dateformat); $mform->addHelpButton('dateformat', 'dateformat', 'customcertelement_date');
$mform->addHelpButton('dateformat_' . $this->element->id, 'dateformat', 'customcertelement_date');
parent::render_form_elements($mform); parent::render_form_elements($mform);
} }
@ -70,16 +81,10 @@ class customcert_element_date extends customcert_element_base {
* @return string the json encoded array * @return string the json encoded array
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
// Get the date item and format from the form.
$dateitem = 'dateitem_' . $this->element->id;
$dateitem = $data->$dateitem;
$dateformat = 'dateformat_' . $this->element->id;
$dateformat = $data->$dateformat;
// Array of data we will be storing in the database. // Array of data we will be storing in the database.
$arrtostore = array( $arrtostore = array(
'dateitem' => $dateitem, 'dateitem' => $data->dateitem,
'dateformat' => $dateformat 'dateformat' => $data->dateformat
); );
// Encode these variables before saving into the DB. // Encode these variables before saving into the DB.

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013041600; $plugin->version = 2013060700;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_date'; $plugin->component = 'customcertelement_date';

View file

@ -40,29 +40,7 @@ class customcert_element_base {
* @param stdClass $element the element data * @param stdClass $element the element data
*/ */
function __construct($element) { function __construct($element) {
$this->element = new stdClass(); $this->element = clone($element);
$this->element = $element;
}
/**
* This function is responsible for adding the element for the first time
* to the database when no data has yet been specified, default values set.
* Can be overridden if more functionality is needed.
*
* @param string $element the name of the element
* @param int $pageid the page id we are saving it to
*/
public static function add_element($element, $pageid) {
global $DB;
$data = customcert_element_base::get_required_attributes($element, $pageid);
$data->font = 'times';
$data->size = '12';
$data->colour = '#000000';
$data->posx = '0';
$data->posy = '0';
$DB->insert_record('customcert_elements', $data);
} }
/** /**
@ -78,6 +56,24 @@ class customcert_element_base {
$this->render_form_elements_position($mform); $this->render_form_elements_position($mform);
} }
/**
* Sets the data on the form when editing an element.
* Can be overridden if more functionality is needed.
*
* @param stdClass $mform the edit_form instance
* @param array the form elements to set
*/
public function definition_after_data($mform) {
// Loop through the properties of the element and set the values
// of the corresponding form element, if it exists.
foreach ($this->element as $property => $value) {
if ($mform->elementExists($property)) {
$element = $mform->getElement($property);
$element->setValue($value);
}
}
}
/** /**
* Performs validation on the element values. * Performs validation on the element values.
* Can be overridden if more functionality is needed. * Can be overridden if more functionality is needed.
@ -101,31 +97,33 @@ class customcert_element_base {
* Handles saving the form elements created by this element. * Handles saving the form elements created by this element.
* Can be overridden if more functionality is needed. * Can be overridden if more functionality is needed.
* *
* @param stdClass $data the form data. * @param stdClass $data the form data
*/ */
public function save_form_elements($data) { public function save_form_elements($data) {
global $DB; global $DB;
// Get the name of the fields we want from the form.
$font = 'font_' . $this->element->id;
$size = 'size_' . $this->element->id;
$colour = 'colour_' . $this->element->id;
$posx = 'posx_' . $this->element->id;
$posy = 'posy_' . $this->element->id;
// Get the data from the form. // Get the data from the form.
$element = new stdClass(); $element = new stdClass();
$element->id = $this->element->id; $element->name = $data->name;
$element->data = $this->save_unique_data($data); $element->data = $this->save_unique_data($data);
$element->font = (isset($data->$font)) ? $data->$font : null; $element->font = (isset($data->font)) ? $data->font : null;
$element->size = (isset($data->$size)) ? $data->$size : null; $element->size = (isset($data->size)) ? $data->size : null;
$element->colour = (isset($data->$colour)) ? $data->$colour : null; $element->colour = (isset($data->colour)) ? $data->colour : null;
$element->posx = (isset($data->$posx)) ? $data->$posx : null; $element->posx = (isset($data->posx)) ? $data->posx : null;
$element->posy = (isset($data->$posy)) ? $data->$posy : null; $element->posy = (isset($data->posy)) ? $data->posy : null;
$element->timemodified = time(); $element->timemodified = time();
// Ok, now update record in the database. // Check if we are updating, or inserting a new element.
if (!empty($this->element->id)) { // Must be updating a record in the database.
$element->id = $this->element->id;
$DB->update_record('customcert_elements', $element); $DB->update_record('customcert_elements', $element);
} else { // Must be adding a new one.
$element->element = $data->element;
$element->pageid = $data->pageid;
$element->sequence = $this->get_element_sequence($element->pageid);
$element->timecreated = time();
$DB->insert_record('customcert_elements', $element);
}
} }
/** /**
@ -133,7 +131,7 @@ class customcert_element_base {
* customcert column. * customcert column.
* Can be overridden if more functionality is needed. * Can be overridden if more functionality is needed.
* *
* @param stdClass $data the form data. * @param stdClass $data the form data
* @return string the unique data to save * @return string the unique data to save
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
@ -199,28 +197,6 @@ class customcert_element_base {
return $DB->delete_records('customcert_elements', array('id' => $this->element->id)); return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
} }
/**
* Helper function responsible for setting the default values for the required variables
* for an element. This should be set the same way regardless of elements.
*
* @param string $element the name of the element
* @param int $pageid the page id we are saving it to
* @return stdClass the required attributes
*/
public static function get_required_attributes($element, $pageid) {
// Set the time as a variable.
$time = time();
$data = new stdClass();
$data->pageid = $pageid;
$data->element = $element;
$data->sequence = customcert_element_base::get_element_sequence($pageid);
$data->timecreated = $time;
$data->timemodified = $time;
return $data;
}
/** /**
* Helper function that returns the sequence on a specified customcert page for a * Helper function that returns the sequence on a specified customcert page for a
* newly created element. * newly created element.
@ -251,28 +227,27 @@ class customcert_element_base {
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
*/ */
public function render_form_elements_font($mform) { public function render_form_elements_font($mform) {
$mform->addElement('select', 'font_' . $this->element->id, get_string('font', 'customcert'), customcert_get_fonts()); $mform->addElement('select', 'font', get_string('font', 'customcert'), customcert_get_fonts());
$mform->setType('font_' . $this->element->id, PARAM_TEXT); $mform->setType('font', PARAM_TEXT);
$mform->setDefault('font_' . $this->element->id, $this->element->font); $mform->setDefault('font', 'times');
$mform->addHelpButton('font_' . $this->element->id, 'font', 'customcert'); $mform->addHelpButton('font', 'font', 'customcert');
$mform->addElement('select', 'size_' . $this->element->id, get_string('fontsize', 'customcert'), customcert_get_font_sizes()); $mform->addElement('select', 'size', get_string('fontsize', 'customcert'), customcert_get_font_sizes());
$mform->setType('size_' . $this->element->id, PARAM_INT); $mform->setType('size', PARAM_INT);
$mform->setDefault('size_' . $this->element->id, $this->element->size); $mform->setDefault('size', 12);
$mform->addHelpButton('size_' . $this->element->id, 'fontsize', 'customcert'); $mform->addHelpButton('size', 'fontsize', 'customcert');
} }
/** /**
* Helper function to render the colour elements. * Helper function to render the colour elements.
* *
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
*/ */
public function render_form_elements_colour($mform) { public function render_form_elements_colour($mform) {
$mform->addElement('customcert_colourpicker', 'colour_' . $this->element->id, get_string('fontcolour', 'customcert')); $mform->addElement('customcert_colourpicker', 'colour', get_string('fontcolour', 'customcert'));
$mform->setType('colour_' . $this->element->id, PARAM_RAW); // Need to validate that this is a valid colour. $mform->setType('colour', PARAM_RAW); // Need to validate that this is a valid colour.
$mform->setDefault('colour_' . $this->element->id, $this->element->colour); $mform->setDefault('colour', '#000000');
$mform->addHelpButton('colour_' . $this->element->id, 'fontcolour', 'customcert'); $mform->addHelpButton('colour', 'fontcolour', 'customcert');
} }
/** /**
@ -281,15 +256,15 @@ class customcert_element_base {
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
*/ */
public function render_form_elements_position($mform) { public function render_form_elements_position($mform) {
$mform->addElement('text', 'posx_' . $this->element->id, get_string('posx', 'customcert'), array('size' => 10)); $mform->addElement('text', 'posx', get_string('posx', 'customcert'), array('size' => 10));
$mform->setType('posx_' . $this->element->id, PARAM_INT); $mform->setType('posx', PARAM_INT);
$mform->setDefault('posx_' . $this->element->id, $this->element->posx); $mform->setDefault('posx', '0');
$mform->addHelpButton('posx_' . $this->element->id, 'posx', 'customcert'); $mform->addHelpButton('posx', 'posx', 'customcert');
$mform->addElement('text', 'posy_' . $this->element->id, get_string('posy', 'customcert'), array('size' => 10)); $mform->addElement('text', 'posy', get_string('posy', 'customcert'), array('size' => 10));
$mform->setType('posy_' . $this->element->id, PARAM_INT); $mform->setType('posy', PARAM_INT);
$mform->setDefault('posy_' . $this->element->id, $this->element->posy); $mform->setDefault('posy', '0');
$mform->addHelpButton('posy_' . $this->element->id, 'posy', 'customcert'); $mform->addHelpButton('posy', 'posy', 'customcert');
} }
/** /**
@ -302,10 +277,8 @@ class customcert_element_base {
$errors = array(); $errors = array();
// Validate the colour. // Validate the colour.
$colour = 'colour_' . $this->element->id; if (!$this->validate_colour($data['colour'])) {
$colourdata = $data[$colour]; $errors['colour'] = get_string('invalidcolour', 'customcert');
if (!$this->validate_colour($colourdata)) {
$errors[$colour] = get_string('invalidcolour', 'customcert');
} }
return $errors; return $errors;
@ -320,18 +293,14 @@ class customcert_element_base {
public function validate_form_elements_position($data) { public function validate_form_elements_position($data) {
$errors = array(); $errors = array();
// Get position X.
$posx = 'posx_' . $this->element->id;
// Check if posx is not set, or not numeric or less than 0. // Check if posx is not set, or not numeric or less than 0.
if ((!isset($data[$posx])) || (!is_numeric($data[$posx])) || ($data[$posx] < 0)) { if ((!isset($data['posx'])) || (!is_numeric($data['posx'])) || ($data['posx'] < 0)) {
$errors[$posx] = get_string('invalidposition', 'customcert', 'X'); $errors['posx'] = get_string('invalidposition', 'customcert', 'X');
} }
// Get position Y.
$posy = 'posy_' . $this->element->id;
// Check if posy is not set, or not numeric or less than 0. // Check if posy is not set, or not numeric or less than 0.
if ((!isset($data[$posy])) || (!is_numeric($data[$posy])) || ($data[$posy] < 0)) { if ((!isset($data['posy'])) || (!is_numeric($data['posy'])) || ($data['posy'] < 0)) {
$errors[$posy] = get_string('invalidposition', 'customcert', 'Y'); $errors['posy'] = get_string('invalidposition', 'customcert', 'Y');
} }
return $errors; return $errors;

View file

@ -38,38 +38,47 @@ define('CUSTOMCERT_GRADE_COURSE', '0');
class customcert_element_grade extends customcert_element_base { class customcert_element_grade extends customcert_element_base {
/** /**
* This function renders the form elements when adding a customcert element. * Constructor.
* *
* @param stdClass $mform the edit_form instance. * @param stdClass $element the element data
*/ */
public function render_form_elements($mform) { function __construct($element) {
parent::__construct($element);
// Set the item and format for this element.
$gradeitem = ''; $gradeitem = '';
$gradeformat = ''; $gradeformat = '';
// Check if there is any data for this element.
if (!empty($this->element->data)) { if (!empty($this->element->data)) {
$gradeinfo = json_decode($this->element->data); $gradeinfo = json_decode($this->element->data);
$gradeitem = $gradeinfo->gradeitem; $gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat; $gradeformat = $gradeinfo->gradeformat;
} }
$this->element->gradeitem = $gradeitem;
$this->element->gradeformat = $gradeformat;
}
/**
* This function renders the form elements when adding a customcert element.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
// Get the grade items we can display. // Get the grade items we can display.
$gradeitems = array(); $gradeitems = array();
$gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade'); $gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
$gradeitems = $gradeitems + customcert_element_grade::get_grade_items(); $gradeitems = $gradeitems + $this->get_grade_items();
// The grade items. // The grade items.
$mform->addElement('select', 'gradeitem_' . $this->element->id, get_string('gradeitem', 'customcertelement_grade'), $gradeitems); $mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
$mform->setType('gradeitem_', PARAM_INT); $mform->setType('gradeitem', PARAM_INT);
$mform->setDefault('gradeitem_' . $this->element->id, $gradeitem); $mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
$mform->addHelpButton('gradeitem_' . $this->element->id, 'gradeitem', 'customcertelement_grade');
// The grade format. // The grade format.
$mform->addElement('select', 'gradeformat_' . $this->element->id, get_string('gradeformat', 'customcertelement_grade'), $mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'), $this->get_grade_format_options());
customcert_element_grade::get_grade_format_options()); $mform->setType('gradeformat', PARAM_INT);
$mform->setType('gradeformat_', PARAM_INT); $mform->addHelpButton('gradeformat', 'gradeformat', 'customcertelement_grade');
$mform->setDefault('gradeformat_' . $this->element->id, $gradeformat);
$mform->addHelpButton('gradeformat_' . $this->element->id, 'gradeformat', 'customcertelement_grade');
parent::render_form_elements($mform); parent::render_form_elements($mform);
} }
@ -82,16 +91,10 @@ class customcert_element_grade extends customcert_element_base {
* @return string the json encoded array * @return string the json encoded array
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
// Get the grade item and format from the form.
$gradeitem = 'gradeitem_' . $this->element->id;
$gradeitem = $data->$gradeitem;
$gradeformat = 'gradeformat_' . $this->element->id;
$gradeformat = $data->$gradeformat;
// Array of data we will be storing in the database. // Array of data we will be storing in the database.
$arrtostore = array( $arrtostore = array(
'gradeitem' => $gradeitem, 'gradeitem' => $data->gradeitem,
'gradeformat' => $gradeformat 'gradeformat' => $data->gradeformat
); );
// Encode these variables before saving into the DB. // Encode these variables before saving into the DB.
@ -116,7 +119,7 @@ class customcert_element_grade extends customcert_element_base {
$gradeinfo = json_decode($this->element->data); $gradeinfo = json_decode($this->element->data);
// Get the grade for the grade item. // Get the grade for the grade item.
$grade = customcert_element_grade::get_grade($gradeinfo, $USER->id); $grade = $this->get_grade($gradeinfo, $USER->id);
parent::render_content($pdf, $grade); parent::render_content($pdf, $grade);
} }
@ -169,8 +172,8 @@ class customcert_element_grade extends customcert_element_base {
// Get the grade items for this activity. // Get the grade items for this activity.
if ($grade_items = grade_get_grade_items_for_activity($mod)) { if ($grade_items = grade_get_grade_items_for_activity($mod)) {
$mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance);
$item = reset($mod_item->items); $gradeitem = reset($mod_item->items);
if (isset($item->grademax)) { if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
} }
} }
@ -243,22 +246,22 @@ class customcert_element_grade extends customcert_element_base {
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST); $module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
if ($gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid)) { if ($gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid)) {
$item = new grade_item(); $gradeitem = new grade_item();
$item->gradetype = GRADE_TYPE_VALUE; $gradeitem->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course; $gradeitem->courseid = $cm->course;
$itemproperties = reset($gradeitem->items); $gradeitemproperties = reset($gradeitem->items);
foreach ($itemproperties as $key => $value) { foreach ($gradeitemproperties as $key => $value) {
$item->$key = $value; $gradeitem->$key = $value;
} }
// Grade for the user. // Grade for the user.
$grade = $item->grades[$userid]->grade; $grade = $gradeitem->grades[$userid]->grade;
// Create the object we will be returning. // Create the object we will be returning.
$modinfo = new stdClass; $modinfo = new stdClass;
$modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance)); $modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance));
$modinfo->gradetodisplay = grade_format_gradevalue($grade, $item, true, $gradeformat, 2); $modinfo->gradetodisplay = grade_format_gradevalue($grade, $gradeitem, true, $gradeformat, 2);
if ($grade) { if ($grade) {
$modinfo->dategraded = $item->grades[$userid]->dategraded; $modinfo->dategraded = $gradeitem->grades[$userid]->dategraded;
} else { } else {
$modinfo->dategraded = time(); $modinfo->dategraded = time();
} }

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013041600; $plugin->version = 2013060700;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_grade'; $plugin->component = 'customcertelement_grade';

View file

@ -30,31 +30,14 @@ require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
class customcert_element_image extends customcert_element_base { class customcert_element_image extends customcert_element_base {
/** /**
* This function is responsible for adding the element for the first time * Constructor.
* to the database when no data has yet been specified, default values set.
* Can be overridden if more functionality is needed.
* *
* @param string $element the name of the element * @param stdClass $element the element data
* @param int $pageid the page id we are saving it to
*/ */
public static function add_element($element, $pageid) { function __construct($element) {
global $DB; parent::__construct($element);
$data = customcert_element_base::get_required_attributes($element, $pageid); // Set the image, width and height for this element.
$data->width = '0';
$data->height = '0';
$data->posx = '0';
$data->posy = '0';
$DB->insert_record('customcert_elements', $data);
}
/**
* This function renders the form elements when adding a customcert element.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
$image = ''; $image = '';
$width = '0'; $width = '0';
$height = '0'; $height = '0';
@ -67,18 +50,26 @@ class customcert_element_image extends customcert_element_base {
$height = $imageinfo->height; $height = $imageinfo->height;
} }
$mform->addElement('select', 'image_' . $this->element->id, get_string('image', 'customcertelement_image'), self::get_images()); $this->element->image = $image;
$mform->setDefault('image_' . $this->element->id, $image); $this->element->width = $width;
$this->element->height = $height;
}
$mform->addElement('text', 'imagewidth_' . $this->element->id, get_string('width', 'customcertelement_image'), array('size' => 10)); /**
$mform->setType('imagewidth_' . $this->element->id, PARAM_INT); * This function renders the form elements when adding a customcert element.
$mform->setDefault('imagewidth_' . $this->element->id, $width); *
$mform->addHelpButton('imagewidth_' . $this->element->id, 'width', 'customcertelement_image'); * @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
$mform->addElement('select', 'image', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('text', 'imageheight_' . $this->element->id, get_string('height', 'customcertelement_image'), array('size' => 10)); $mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10));
$mform->setType('imageheight_' . $this->element->id, PARAM_INT); $mform->setType('width', PARAM_INT);
$mform->setDefault('imageheight_' . $this->element->id, $height); $mform->addHelpButton('width', 'width', 'customcertelement_image');
$mform->addHelpButton('imageheight_' . $this->element->id, 'height', 'customcertelement_image');
$mform->addElement('text', 'height', get_string('height', 'customcertelement_image'), array('size' => 10));
$mform->setType('height', PARAM_INT);
$mform->addHelpButton('height', 'height', 'customcertelement_image');
parent::render_form_elements_position($mform); parent::render_form_elements_position($mform);
} }
@ -94,18 +85,14 @@ class customcert_element_image extends customcert_element_base {
// Array to return the errors. // Array to return the errors.
$errors = array(); $errors = array();
// Get width.
$width = 'imagewidth_' . $this->element->id;
// Check if width is not set, or not numeric or less than 0. // Check if width is not set, or not numeric or less than 0.
if ((!isset($data[$width])) || (!is_numeric($data[$width])) || ($data[$width] < 0)) { if ((!isset($data['width'])) || (!is_numeric($data['width'])) || ($data['width'] < 0)) {
$errors[$width] = get_string('invalidwidth', 'customcertelement_image'); $errors['width'] = get_string('invalidwidth', 'customcertelement_image');
} }
// Get height.
$height = 'imageheight_' . $this->element->id;
// Check if height is not set, or not numeric or less than 0. // Check if height is not set, or not numeric or less than 0.
if ((!isset($data[$height])) || (!is_numeric($data[$height])) || ($data[$height] < 0)) { if ((!isset($data['height'])) || (!is_numeric($data['height'])) || ($data['height'] < 0)) {
$errors[$height] = get_string('invalidheight', 'customcertelement_image'); $errors['height'] = get_string('invalidheight', 'customcertelement_image');
} }
// Validate the position. // Validate the position.
@ -122,19 +109,11 @@ class customcert_element_image extends customcert_element_base {
* @return string the json encoded array * @return string the json encoded array
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
// Get the date item and format from the form.
$image = 'image_' . $this->element->id;
$image = $data->$image;
$width = 'imagewidth_' . $this->element->id;
$width = $data->$width;
$height = 'imageheight_' . $this->element->id;
$height = $data->$height;
// Array of data we will be storing in the database. // Array of data we will be storing in the database.
$arrtostore = array( $arrtostore = array(
'pathnamehash' => $image, 'pathnamehash' => $data->image,
'width' => $width, 'width' => $data->width,
'height' => $height 'height' => $data->height
); );
return json_encode($arrtostore); return json_encode($arrtostore);
@ -154,18 +133,15 @@ class customcert_element_image extends customcert_element_base {
} }
$imageinfo = json_decode($this->element->data); $imageinfo = json_decode($this->element->data);
$image = $imageinfo->pathnamehash;
$width = $imageinfo->width;
$height = $imageinfo->height;
// Get the image. // Get the image.
$fs = get_file_storage(); $fs = get_file_storage();
if ($file = $fs->get_file_by_hash($image)) { if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
$contenthash = $file->get_contenthash(); $contenthash = $file->get_contenthash();
$l1 = $contenthash[0] . $contenthash[1]; $l1 = $contenthash[0] . $contenthash[1];
$l2 = $contenthash[2] . $contenthash[3]; $l2 = $contenthash[2] . $contenthash[3];
$location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash; $location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;
$pdf->Image($location, $this->element->posx, $this->element->posy, $width, $height); $pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height);
} }
} }

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013053000; $plugin->version = 2013060600;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_image'; $plugin->component = 'customcertelement_image';

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013041200; $plugin->version = 2013060600;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_studentname'; $plugin->component = 'customcertelement_studentname';

View file

@ -29,16 +29,26 @@ require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
class customcert_element_text extends customcert_element_base { class customcert_element_text extends customcert_element_base {
/**
* Constructor.
*
* @param stdClass $element the element data
*/
function __construct($element) {
parent::__construct($element);
$this->element->text = (!empty($element->data)) ? $element->data : '';
}
/** /**
* This function renders the form elements when adding a customcert element. * This function renders the form elements when adding a customcert element.
* *
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
$mform->addElement('textarea', 'text_' . $this->element->id, get_string('text', 'customcertelement_text')); $mform->addElement('textarea', 'text', get_string('text', 'customcertelement_text'));
$mform->setType('text_' . $this->element->id, PARAM_RAW); $mform->setType('text', PARAM_RAW);
$mform->setDefault('text_' . $this->element->id, $this->element->data); $mform->addHelpButton('text', 'text', 'customcertelement_text');
$mform->addHelpButton('text_' . $this->element->id, 'text', 'customcertelement_text');
parent::render_form_elements($mform); parent::render_form_elements($mform);
} }
@ -51,8 +61,7 @@ class customcert_element_text extends customcert_element_base {
* @return string the text * @return string the text
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
$text = 'text_' . $this->element->id; return $data->text;
return $data->$text;
} }
/** /**

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013041200; $plugin->version = 2013060700;
$plugin->requires = 2013040500; // Requires this Moodle version. $plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_text'; $plugin->component = 'customcertelement_text';

View file

@ -28,11 +28,13 @@ $string['addelement'] = 'Add element';
$string['awardedto'] = 'Awarded to'; $string['awardedto'] = 'Awarded to';
$string['code'] = 'Code'; $string['code'] = 'Code';
$string['copy'] = 'Copy'; $string['copy'] = 'Copy';
$string['configure'] = 'Configure';
$string['coursetimereq'] = 'Required minutes in course'; $string['coursetimereq'] = 'Required minutes in course';
$string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive the certificate.'; $string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive the certificate.';
$string['customcert:addinstance'] = 'Add a new custom certificate instance'; $string['customcert:addinstance'] = 'Add a new custom certificate instance';
$string['customcert:manage'] = 'Manage a custom certificate'; $string['customcert:manage'] = 'Manage a custom certificate';
$string['customcert:view'] = 'View a custom certificate'; $string['customcert:view'] = 'View a custom certificate';
$string['delete'] = 'Delete';
$string['deletecertpage'] = 'Delete certificate page'; $string['deletecertpage'] = 'Delete certificate page';
$string['deleteconfirm'] = 'Delete confirmation'; $string['deleteconfirm'] = 'Delete confirmation';
$string['deleteelement'] = 'Delete element'; $string['deleteelement'] = 'Delete element';
@ -40,6 +42,10 @@ $string['deleteelementconfirm'] = 'Are you sure you want to delete this element?
$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['description'] = 'Description'; $string['description'] = 'Description';
$string['editcustomcert'] = 'Edit custom certificate'; $string['editcustomcert'] = 'Edit custom certificate';
$string['elementname'] = 'Element name';
$string['elementname_help'] = 'This will be the name used to identify this element when editing a custom certificate. For example, you may have multiple images on a page and will want to distinguish between them quickly when editing the certificate. Note: this will not displayed on the PDF.';
$string['elements'] = 'Elements';
$string['elements_help'] = 'These are the list of elements that will be displayed on this PDF page.';
$string['errorloadingelement'] = 'Error loading the element "{$a}"'; $string['errorloadingelement'] = 'Error loading the element "{$a}"';
$string['errorsavingelement'] = 'Error saving the element "{$a}"'; $string['errorsavingelement'] = 'Error saving the element "{$a}"';
$string['font'] = 'Font'; $string['font'] = 'Font';
@ -87,6 +93,7 @@ $string['setprotection_help'] = 'Choose the actions you wish to prevent users fr
$string['summaryofissue'] = 'Summary of issue'; $string['summaryofissue'] = 'Summary of issue';
$string['templatename'] = 'Template name'; $string['templatename'] = 'Template name';
$string['templatenameexists'] = 'That template name is currently in use, please choose another.'; $string['templatenameexists'] = 'That template name is currently in use, please choose another.';
$string['type'] = 'Type';
$string['uploadimage'] = 'Upload image'; $string['uploadimage'] = 'Upload image';
$string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['viewcustomcertissues'] = 'View {$a} issued custom certificates';
$string['width'] = 'Width'; $string['width'] = 'Width';

44
lib.php
View file

@ -496,16 +496,6 @@ function customcert_save_page_data($data) {
$p->timemodified = $time; $p->timemodified = $time;
// Update the page. // Update the page.
$DB->update_record('customcert_pages', $p); $DB->update_record('customcert_pages', $p);
// Get the elements for the page.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
// Loop through the elements.
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = customcert_get_element_instance($element)) {
$e->save_form_elements($data);
}
}
}
} }
} }
} }
@ -531,17 +521,6 @@ function customcert_get_element_instance($element) {
return false; return false;
} }
/**
* Handles adding another element to a page in the customcert.
*
* @param string $element the name of the element
* @param int $pageid the page id we are saving it to
*/
function customcert_add_element($element, $pageid) {
$classname = "customcert_element_{$element}";
$classname::add_element($element, $pageid);
}
/** /**
* Handles adding another page to the customcert. * Handles adding another page to the customcert.
* *
@ -550,23 +529,14 @@ function customcert_add_element($element, $pageid) {
function customcert_add_page($data) { function customcert_add_page($data) {
global $DB; global $DB;
// If no pageid is passed then we are creating the first page. // Set the page number to 1 to begin with.
if (empty($data->pageid)) {
$pagenumber = 1; $pagenumber = 1;
} else { // Create a page after an existing one. // Get the max page number.
// Get the page we are inserting the new one after. $sql = "SELECT MAX(pagenumber) as maxpage
$currentpage = $DB->get_record('customcert_pages', array('id' => $data->pageid), '*', MUST_EXIST); FROM {customcert_pages} cp
WHERE cp.customcertid = :customcertid";
// Increase the page numbers of the pages that are going if ($maxpage = $DB->get_record_sql($sql, array('customcertid' => $data->id))) {
// to be in front of the new page we are creating $pagenumber = $maxpage->maxpage + 1;
$sql = "UPDATE {customcert_pages}
SET pagenumber = pagenumber + 1
WHERE customcertid = :customcertid
AND pagenumber > :pagenumber";
$DB->execute($sql, array('customcertid' => $currentpage->customcertid,
'pagenumber' => $currentpage->pagenumber));
$pagenumber = $currentpage->pagenumber + 1;
} }
// Store time in a variable. // Store time in a variable.

View file

@ -43,7 +43,7 @@ class mod_customcert_mod_form extends moodleform_mod {
if (!empty($CFG->formatstringstriptags)) { if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT); $mform->setType('name', PARAM_TEXT);
} else { } else {
$mform->setType('name', PARAM_CLEAN); $mform->setType('name', PARAM_CLEANHTML);
} }
$mform->addRule('name', null, 'required', null, 'client'); $mform->addRule('name', null, 'required', null, 'client');