Module will install successfully with limited functionality

The module will add tables to the DB on install, you can then add/edit instances and add/edit pages to these instances.
This commit is contained in:
Mark Nelson 2012-12-07 17:34:46 +08:00
commit 2b8403438f
12 changed files with 1225 additions and 0 deletions

24
README.md Normal file
View file

@ -0,0 +1,24 @@
QUICK INSTALL
=============
There are two installation methods that are available. Follow one of these, then log into your Moodle site as an administrator and visit the notifications page to complete the install.
==================== MOST RECOMMENDED METHOD - Git ====================
If you do not have git installed, please see the below link. Please note, it is not necessary to set up the SSH Keys. This is only needed if you are going to create a repository of your own on github.com.
Information on installing git - http://help.github.com/set-up-git-redirect/
Once you have git installed, simply visit the Moodle mod directory and clone git://github.com/markn86/moodle-mod_customcert.git, remember to rename the folder to customcert if you do not specify this in the clone command
Eg. Linux command line would be as follow -
git clone git://github.com/markn86/moodle-mod_customcert.git customcert
Use git pull to update this repository periodically to ensure you have the latest version.
==================== Download the customcert module. ====================
Visit https://github.com/markn86/moodle-mod_customcert and download the zip, uncompress this zip and extract the folder. The folder will have a name similar to markn86-moodle-mod_customcert-c9fbadb, you MUST rename this to customcert. Place this folder in your mod folder in your Moodle directory.
nb. The reason this is not the recommended method is due to the fact you have to over-write the contents of this folder to apply any future updates to the customcert module. In the above method there is a simple command to update the files.

62
db/access.php Normal file
View file

@ -0,0 +1,62 @@
<?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/>.
/**
* Customcert module capability definition
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$capabilities = array(
'mod/customcert:addinstance' => array(
'riskbitmask' => RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/course:manageactivities'
),
'mod/customcert:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'mod/customcert:manage' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
);

69
db/install.xml Normal file
View file

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/customcert/db" VERSION="20121205" COMMENT="XMLDB file for Moodle mod/customcert"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="customcert" COMMENT="Defines customcerts" NEXT="customcert_issues">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="name"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="requiredtime"/>
<FIELD NAME="requiredtime" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="introformat" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="requiredtime" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_issues" COMMENT="Stores each issue of a customcert" PREVIOUS="customcert" NEXT="customcert_pages">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="customcertid"/>
<FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="userid" NEXT="code"/>
<FIELD NAME="code" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="customcertid" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="code"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_issues" NEXT="customcert"/>
<KEY NAME="customcert" TYPE="foreign" FIELDS="customcertid" REFTABLE="customcert" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_pages" COMMENT="Stores each page of a custom cert" PREVIOUS="customcert_issues" NEXT="customcert_text_fields">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false" NEXT="customcertid"/>
<FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="orientation"/>
<FIELD NAME="orientation" TYPE="char" LENGTH="10" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="customcertid" NEXT="width"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="orientation" NEXT="height"/>
<FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="width" NEXT="backgroundimage"/>
<FIELD NAME="backgroundimage" TYPE="char" LENGTH="255" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="height" NEXT="pagenumber"/>
<FIELD NAME="pagenumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="backgroundimage" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="pagenumber" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_pages" NEXT="customcert"/>
<KEY NAME="customcert" TYPE="foreign" FIELDS="customcertid" REFTABLE="customcert" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_text_fields" COMMENT="Stores the text fields for a given page" PREVIOUS="customcert_pages">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false" NEXT="customcertpageid"/>
<FIELD NAME="customcertpageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="text"/>
<FIELD NAME="text" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="customcertpageid" NEXT="font"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="text" NEXT="size"/>
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="font" NEXT="posx"/>
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="size" NEXT="posy"/>
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" 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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_text_fields" NEXT="customcertpage"/>
<KEY NAME="customcertpage" TYPE="foreign" FIELDS="customcertpageid" REFTABLE="customcert_pages" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

34
db/log.php Normal file
View file

@ -0,0 +1,34 @@
<?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/>.
/**
* Definition of log events
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$logs = array(
array('module' => 'customcert', 'action' => 'view', 'mtable' => 'customcert', 'field' => 'name'),
array('module' => 'customcert', 'action' => 'add', 'mtable' => 'customcert', 'field' => 'name'),
array('module' => 'customcert', 'action' => 'update', 'mtable' => 'customcert', 'field' => 'name'),
array('module' => 'customcert', 'action' => 'received', 'mtable' => 'customcert', 'field' => 'name'),
);

118
edit.php Normal file
View file

@ -0,0 +1,118 @@
<?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 the customcert settings.
*
* @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/lib.php');
require_once($CFG->dirroot . '/mod/customcert/edit_form.php');
$cmid = required_param('cmid', PARAM_INT);
$moveup = optional_param('moveup', 0, PARAM_INT);
$movedown = optional_param('movedown', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', 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);
require_login($course, false, $cm);
require_capability('mod/customcert:manage', $context);
// Check if they are moving a custom certificate page.
if ((!empty($moveup)) || (!empty($movedown))) {
// Check if we are moving a page up.
if (!empty($moveup)) {
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $moveup))) {
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber - 1));
}
} else { // Must be moving a page down.
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $movedown))) {
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber + 1));
}
}
// Check that there is a page to move, and a page to swap it with.
if ($swapcertpage && $movecertpage) {
$DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id));
$DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id));
}
} else if ((!empty($delete)) && (!empty($confirm))) {
customcert_delete_page($delete);
}
$mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id,
'cmid' => $cm->id,
'course' => $course));
if ($data = $mform->get_data()) {
// Handle file uploads.
customcert_upload_imagefiles($data->customcertimage);
// Save any page data.
customcert_save_page_data($data);
// Check if they requested to delete a page.
foreach ($data as $key => $value) {
if (strpos($key, 'deletecertpage_') !== false) {
$pageid = str_replace('deletecertpage_', '', $key);
// 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,
'delete' => $pageid,
'confirm' => 1,
'sesskey' => sesskey()));
// Show a confirmation page.
$strheading = get_string('deletecertpage', '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);
$message = get_string('deletecertpageconfirm', 'customcert');
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
}
// If they chose to add another page, enter it into database.
if (!empty($data->addcertpage)) {
customcert_add_page($data);
}
// Redirect to the editing page to show form with recent updates.
redirect($CFG->wwwroot . '/mod/customcert/edit.php?cmid=' . $cm->id);
}
$PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name)));
$PAGE->set_heading($course->fullname);
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcustomcert', 'mod_customcert'));
$mform->display();
echo $OUTPUT->footer();

250
edit_form.php Normal file
View file

@ -0,0 +1,250 @@
<?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/>.
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');
/**
* Instance add/edit form.
*
* @package mod
* @subpackage customcert
* @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.
*/
protected $id = null;
/**
* The course.
*/
protected $course = null;
/**
* The total number of pages for this cert.
*/
protected $numpages = 1;
/**
* The orientation options.
*/
protected $orientationoptions = array();
/**
* The image options.
*/
protected $imageoptions = array();
/**
* The filemanager options.
*/
protected $filemanageroptions = array();
/**
* Form definition.
*/
function definition() {
global $CFG, $DB, $OUTPUT;
$this->id = $this->_customdata['customcertid'];
$this->orientationoptions = array('L' => get_string('landscape', 'customcert'),
'P' => get_string('portrait', 'customcert'));
$this->imageoptions = customcert_get_images();
$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);
}
} else {
$this->add_customcert_page_elements();
}
$mform->closeHeaderBefore('addcertpage');
$mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
$mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert'));
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
$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']);
$this->add_action_buttons();
}
/**
* Fill in the current page data for this certificate.
*/
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) {
// Get the pages for this certificate.
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $this->id))) {
// Loop through the pages.
foreach ($pages as $p) {
// Set the orientation.
$element = $mform->getElement('orientation_'.$p->id);
$element->setValue($p->orientation);
// Set the width.
$element = $mform->getElement('width_'.$p->id);
$element->setValue($p->width);
// Set the height.
$element = $mform->getElement('height_'.$p->id);
$element->setValue($p->height);
// Set the background image.
$element = $mform->getElement('backgroundimage_'.$p->id);
$element->setValue($p->backgroundimage);
// Now get the page text fields.
if ($textfields = $DB->get_records('customcert_text_fields', array())) {
}
}
}
}
}
/**
* 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) {
if (strpos($key, 'width_') !== false) {
$page = str_replace('width_', '', $key);
// Validate that the weight is a valid value.
if (!isset($data['width_'.$page]) || !is_number($data['width_'.$page])) {
$errors['width_'.$page] = get_string('widthnotvalid', 'customcert');
}
}
if (strpos($key, 'height_') !== false) {
$page = str_replace('height_', '', $key);
// Validate that the height is a valid value.
if (!isset($data['height_'.$page]) || !is_number($data['height_'.$page])) {
$errors['height_'.$page] = get_string('heightnotvalid', 'customcert');
}
}
}
return $errors;
}
/**
* Adds the page elements to the form.
*
* @param stdClass $page the customcert page
**/
private function add_customcert_page_elements($page = null) {
global $DB, $OUTPUT;
// Create the form object.
$mform =& $this->_form;
// If page is null we are adding a customcert, not editing one, so set identifier to 1.
if (is_null($page)) {
$identifier = 1;
$pagenum = 1;
} else {
$identifier = $page->id;
$pagenum = $page->pagenumber;
}
$mform->addElement('header', 'page_'.$identifier, get_string('page', 'customcert', $pagenum));
// Place the ordering arrows.
// Only display the move up arrow if it is not the first.
if ($pagenum > 1) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'moveup' => $identifier));
$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 ($pagenum < $this->numpages) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'movedown' => $identifier));
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))));
}
$mform->addElement('select', 'orientation_'.$identifier, get_string('orientation', 'customcert'), $this->orientationoptions);
$mform->setDefault('orientation_'.$identifier, 'P');
$mform->addHelpButton('orientation_'.$identifier, 'orientation', 'customcert');
$mform->addElement('text', 'width_'.$identifier, get_string('width', 'customcert'));
$mform->addRule('width_'.$identifier, null, 'required', null, 'client');
$mform->addHelpButton('width_'.$identifier, 'width', 'customcert');
$mform->addElement('text', 'height_'.$identifier, get_string('height', 'customcert'));
$mform->addRule('height_'.$identifier, null, 'required', null, 'client');
$mform->addHelpButton('height_'.$identifier, 'height', 'customcert');
// Get the other image options.
$mform->addElement('select', 'backgroundimage_'.$identifier, get_string('backgroundimage', 'customcert'), $this->imageoptions);
$mform->setDefault('backgroundimage_'.$identifier, 0);
$mform->addHelpButton('backgroundimage_'.$identifier, 'backgroundimage', 'customcert');
// Add text fields.
$textgroup = array();
$textgroup[] =& $mform->createElement('text', 'certtext_'.$identifier, '',
array('cols' => '40', 'rows' => '4', 'wrap' => 'virtual'));
$group = $mform->createElement('group', 'customcerttextgroup_'.$identifier,
get_string('addtext', 'customcert'), $textgroup);
$count = (is_null($page)) ? 1 : $DB->count_records('customcert_text_fields', array('customcertpageid' => $identifier)) + 1;
$this->repeat_elements(array($group), $count, array(), 'customcertimagerepeats_'.$identifier, 'imageadd_'.$identifier, 1,
get_string('addanothertextfield', 'customcert'), true);
// Add option to delete this page if it is not the first page.
if ($pagenum > 1) {
$mform->addElement('html', '<div class=\'deletecertpage\'>');
$mform->addElement('submit', 'deletecertpage_'.$identifier, get_string('deletecertpage', 'customcert'));
$mform->addElement('html', '</div>');
}
}
}

59
lang/en/customcert.php Normal file
View file

@ -0,0 +1,59 @@
<?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/>.
/**
* Language strings for the customcert module
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['addtext'] = 'Add Text';
$string['addanothertextfield'] = 'Add another text field';
$string['addcertpage'] = 'Add another certificate page';
$string['backgroundimage'] = 'Background image';
$string['backgroundimage_help'] = 'This image will be take up the entire PDF page as the background image.';
$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['customcert:addinstance'] = 'Add a new custom certificate instance';
$string['customcert:manage'] = 'Manage a custom certificate';
$string['customcert:view'] = 'View a custom certificate';
$string['deletecertpage'] = 'Delete certificate page';
$string['deletecertpageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['editcustomcert'] = 'Edit custom certificate';
$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['landscape'] = 'Landscape';
$string['modulename'] = 'Custom Certificate';
$string['modulenameplural'] = 'Custom Certificates';
$string['name'] = 'Name';
$string['noimage'] = 'No image';
$string['options'] = 'Options';
$string['orientation'] = 'Orientation';
$string['orientation_help'] = 'Choose whether you want your certificate orientation to be portrait or landscape.';
$string['page'] = 'Page {$a}';
$string['pluginadministration'] = 'Custom Certificate administration';
$string['pluginname'] = 'Custom Certificate';
$string['portrait'] = 'Portrait';
$string['uploadimage'] = 'Upload image';
$string['width'] = 'Width';
$string['widthnotvalid'] = 'The width has to be a valid number.';
$string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.';

488
lib.php Normal file
View file

@ -0,0 +1,488 @@
<?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/>.
/**
* Customcert module core interaction API.
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->dirroot.'/grade/querylib.php');
/**
* Add customcert instance.
*
* @param stdClass $customcert
* @param stdClass $mform
* @return int new customcert instance id
*/
function customcert_add_instance($data, $mform) {
global $DB;
$time = time();
$data->timecreated = $time;
$data->timemodified = $data->timecreated;
return $DB->insert_record('customcert', $data);
}
/**
* Update customcert instance.
*
* @param stdClass $data
* @param stdClass $mform
* @return bool true
*/
function customcert_update_instance($data, $mform) {
global $DB;
// Update the time modified.
$data->timemodified = time();
$data->id = $data->instance;
return $DB->update_record('customcert', $data);
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id
* @return bool true if successful
*/
function customcert_delete_instance($id) {
global $DB;
// Ensure the customcert exists.
if (!$customcert = $DB->get_record('customcert', array('id' => $id))) {
return false;
}
// Get the course module used when deleting files associated to the customcert.
if (!$cm = get_coursemodule_from_instance('customcert', $id)) {
return false;
}
// Delete the customcert instance.
if (!$DB->delete_records('customcert', array('id' => $id))) {
return false;
}
// Delete the customcert issues.
if (!$DB->delete_records('customcert_issues', array('customcertid' => $id))) {
return false;
}
// Get all the customcert pages.
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $id))) {
// Loop through pages.
foreach ($pages as $p) {
// Delete the text fields.
if (!$DB->delete_records('customcert_text_fields', array('customcertpageid' => $p->id))) {
return false;
}
}
// Delete the pages
if (!$DB->delete_records('customcert_pages', array('customcertid' => $id))) {
return false;
}
}
// Delete any files associated with the customcert.
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$fs = get_file_storage();
$fs->delete_area_files($context->id);
return true;
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* This function will remove all posts from the specified customcert
* and clean up any related data.
*
* @param $data the data submitted from the reset course.
* @return array status array
*/
function customcert_reset_userdata($data) {
global $CFG, $DB;
$componentstr = get_string('modulenameplural', 'customcert');
$status = array();
if (!empty($data->reset_customcert)) {
$sql = "SELECT cert.id
FROM {customcert} cert
WHERE cert.course = :courseid";
$DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", array('courseid' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('customcertremoved', 'customcert'), 'error' => false);
}
// Updating dates - shift may be negative too.
if ($data->timeshift) {
shift_course_mod_dates('customcert', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
}
return $status;
}
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the customcert.
*
* @param $mform form passed by reference
*/
function customcert_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'customcertheader', get_string('modulenameplural', 'customcert'));
$mform->addElement('advcheckbox', 'reset_customcert', get_string('deletissuedcustomcerts', 'customcert'));
}
/**
* Course reset form defaults.
*
* @param stdClass $course
* @return array
*/
function customcert_reset_course_form_defaults($course) {
return array('reset_customcert' => 1);
}
/**
* Returns information about received customcert.
* Used for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $customcert
* @return stdClass the user outline object
*/
function customcert_user_outline($course, $user, $mod, $customcert) {
global $DB;
$result = new stdClass();
if ($issue = $DB->get_record('customcert_issues', array('customcertid' => $customcert->id, 'userid' => $user->id))) {
$result->info = get_string('issued', 'customcert');
$result->time = $issue->timecreated;
} else {
$result->info = get_string('notissued', 'customcert');
}
return $result;
}
/**
* Returns information about received customcert.
* Used for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $page
* @return string the user complete information
*/
function customcert_user_complete($course, $user, $mod, $customcert) {
global $DB, $OUTPUT;
if ($issue = $DB->get_record('customcert_issues', array('customcertid' => $customcert->id, 'userid' => $user->id))) {
echo $OUTPUT->box_start();
echo get_string('issued', 'customcert') . ": ";
echo userdate($issue->timecreated);
customcert_print_user_files($customcert->id, $user->id);
echo '<br />';
echo $OUTPUT->box_end();
} else {
print_string('notissuedyet', 'customcert');
}
}
/**
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function customcert_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS: return true;
case FEATURE_GROUPINGS: return true;
case FEATURE_GROUPMEMBERSONLY: return true;
case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_BACKUP_MOODLE2: return true;
default: return null;
}
}
/**
* Used for course participation report (in case customcert is added).
*
* @return array
*/
function customcert_get_view_actions() {
return array('view', 'view all', 'view report');
}
/**
* Used for course participation report (in case customcert is added).
*
* @return array
*/
function customcert_get_post_actions() {
return array('received');
}
/**
* Function to be run periodically according to the moodle cron.
*/
function customcert_cron () {
return true;
}
/**
* This function extends the settings navigation block for the site.
*
* It is safe to rely on PAGE here as we will only ever be within the module
* context when this is called.
*
* @param settings_navigation $settings
* @param navigation_node $customcertnode
*/
function customcert_extend_settings_navigation(settings_navigation $settings, navigation_node $customcertnode) {
global $PAGE, $CFG;
$keys = $customcertnode->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if ($i === false and array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
if (has_capability('mod/customcert:manage', $PAGE->cm->context)) {
$node = navigation_node::create(get_string('editcustomcert', 'customcert'),
new moodle_url('/mod/customcert/edit.php', array('cmid'=>$PAGE->cm->id)),
navigation_node::TYPE_SETTING, null, 'mod_customcert_edit',
new pix_icon('t/edit', ''));
$customcertnode->add_node($node, $beforekey);
}
return $customcertnode->trim_if_empty();
}
/**
* Return the list of possible images to use.
*
* @return array the list of images that can be used.
*/
function customcert_get_images() {
// Create file storage object.
$fs = get_file_storage();
// The array used to store the images.
$arrfiles = array();
$arrfiles[0] = get_string('noimage', 'customcert');
if ($files = $fs->get_area_files(context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$hash] = $file->get_filename();
}
}
return $arrfiles;
}
/**
* Handles uploading an image for the customcert module.
*
* @param stdClass $draftiemtid the draft area containing the files
*/
function customcert_upload_imagefiles($draftitemid) {
// Get the filestorage object.
$fs = get_file_storage();
// Save the file if it exists that is currently in the draft area.
file_save_draft_area_files($draftitemid, context_system::instance()->id, 'mod_customcert', 'image', 0);
}
/**
* Handles saving page data.
*
* @param stdClass $data the customcert data
*/
function customcert_save_page_data($data) {
global $DB;
// Set the time to a variable.
$time = time();
// Get the total number of pages that exist for this customcert.
$totalpages = $DB->count_records('customcert_pages', array('customcertid' => $data->id));
// Check if this file has any pages yet, if not we are inserting.
if ($totalpages == 0) {
// Create the page to insert.
$page = new stdClass();
$page->customcertid = $data->id;
$page->orientation = $data->orientation_1;
$page->width = $data->width_1;
$page->height = $data->height_1;
$page->backgroundimage = $data->backgroundimage_1;
$page->pagenumber = 1;
$page->timecreated = $time;
$page->timemodified = $time;
// Insert the page.
$DB->insert_record('customcert_pages', $page);
} else {
// Go through the data and check for any page data.
foreach ($data as $key => $value) {
if (strpos($key, 'orientation_') !== false) {
// Get the page id.
$pageid = str_replace('orientation_', '', $key);
// Get the rest of the elements now.
$orientation = "orientation_$pageid";
$width = "width_$pageid";
$height = "height_$pageid";
$backgroundimage = "backgroundimage_$pageid";
// Create the page to update.
$page = new stdClass();
$page->id = $pageid;
$page->customcertid = $data->id;
$page->orientation = $data->$orientation;
$page->width = $data->$width;
$page->height = $data->$height;
$page->backgroundimage = $data->$backgroundimage;
$page->timemodified = $time;
// Update the page.
$DB->update_record('customcert_pages', $page);
}
}
}
}
/**
* Handles adding another page to the customcert.
*
* @param stdClass $data the customcert data
*/
function customcert_add_page($data) {
global $DB;
// Set the number of the page we are creating.
$pagenum = 1;
// Check if there already pages that exist, if so, overwrite value.
$sql = "SELECT MAX(pagenumber) as maxpagenumber
FROM {customcert_pages}
WHERE customcertid = :id";
// Get the current max page number and add 1 to page number for new page.
if ($maxnum = $DB->get_record_sql($sql, array('id' => $data->id))) {
$pagenum = $maxnum->maxpagenumber + 1;
}
// New page creation.
$time = time();
$page = new stdClass();
$page->customcertid = $data->id;
$page->orientation = 'P';
$page->pagenumber = $pagenum;
$page->timecreated = $time;
$page->timemodified = $time;
// Insert the page.
$DB->insert_record('customcert_pages', $page);
}
/**
* Handles deleting a page from the customcert.
*
* @param int $pageid the customcert page
*/
function customcert_delete_page($pageid) {
global $DB;
// Get the page.
$page = $DB->get_record('customcert_pages', array('id' => $pageid), '*', MUST_EXIST);
// Delete this page.
$DB->delete_records('customcert_pages', array('id' => $page->id));
// Delete any text fields belonging to this page.
$DB->delete_records('customcert_text_fields', array('customcertpageid' => $page->id));
// Now we want to decrease the page number values of
// the pages that are greater than the page we deleted.
$sql = "UPDATE {customcert_pages}
SET pagenumber = pagenumber - 1
WHERE customcertid = :customcertid
AND pagenumber > :pagenumber";
$DB->execute($sql, array('customcertid' => $page->customcertid,
'pagenumber' => $page->pagenumber));
}
/**
* Get the time the user has spent in the course.
*
* @param int $courseid
* @return int the total time spent in seconds
*/
function customcert_get_course_time($courseid) {
global $CFG, $USER;
set_time_limit(0);
$totaltime = 0;
$sql = "l.course = :courseid AND l.userid = :userid";
if ($logs = get_logs($sql, array('courseid' => $courseid, 'userid' => $USER->id), 'l.time ASC', '', '', $totalcount)) {
foreach ($logs as $log) {
if (!isset($login)) {
// For the first time $login is not set so the first log is also the first login.
$login = $log->time;
$lasthit = $log->time;
$totaltime = 0;
}
$delay = $log->time - $lasthit;
if ($delay > ($CFG->sessiontimeout * 60)) {
// The difference between the last log and the current log is more than
// the timeout register session value meaning we have found a session.
$login = $log->time;
} else {
$totaltime += $delay;
}
// Now the actual log became the previous log for the next cycle.
$lasthit = $log->time;
}
return $totaltime;
}
return 0;
}

85
mod_form.php Normal file
View file

@ -0,0 +1,85 @@
<?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/>.
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');
/**
* Instance add/edit form.
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_mod_form extends moodleform_mod {
/**
* Form definition.
*/
function definition() {
global $CFG, $DB, $OUTPUT;
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('name', 'customcert'), array('size' => '64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEAN);
}
$mform->addRule('name', null, 'required', null, 'client');
$this->add_intro_editor(false, get_string('intro', 'customcert'));
// Design Options.
$mform->addElement('header', 'options', get_string('options', 'customcert'));
$mform->addElement('text', 'requiredtime', get_string('coursetimereq', 'customcert'), array('size' => '3'));
$mform->addHelpButton('requiredtime', 'coursetimereq', 'customcert');
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
* Some basic validation.
*
* @param $data
* @param $files
* @return array the errors that were found
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Check that the required time entered is valid if it was entered at all.
if (!empty($data['requiredtime'])) {
if ((!is_number($data['requiredtime']) || $data['requiredtime'] < 0)) {
$errors['requiredtime'] = get_string('requiredtimenotvalid', 'customcert');
}
}
return $errors;
}
}

BIN
pix/icon.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 B

3
styles.css Normal file
View file

@ -0,0 +1,3 @@
.deletecertpage {
text-align:right;
}

33
version.php Normal file
View file

@ -0,0 +1,33 @@
<?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/>.
/**
* Code fragment to define the version of the customcert module
*
* @package mod
* @subpackage customcert
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
*/
$module->version = 2012120701; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2012062500; // Requires this Moodle version
$module->cron = 0; // Period for cron to check this module (secs)
$module->component = 'mod_customcert';
$module->maturity = MATURITY_ALPHA;
$module->release = "Alpha release"; // User-friendly version number