2013-03-14 01:19:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of the customcert module for Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-06-12 10:35:27 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/element/element.class.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/element/grade/lib.php');
|
2013-06-12 10:35:27 +00:00
|
|
|
|
2013-06-24 04:38:37 +00:00
|
|
|
/**
|
|
|
|
* Date - Issue
|
|
|
|
*/
|
|
|
|
define('CUSTOMCERT_DATE_ISSUE', '1');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Date - Completion
|
|
|
|
*/
|
|
|
|
define('CUSTOMCERT_DATE_COMPLETION', '2');
|
|
|
|
|
2013-03-14 01:19:08 +00:00
|
|
|
/**
|
2013-06-12 10:35:27 +00:00
|
|
|
* The customcert element date's core interaction API.
|
2013-03-14 01:19:08 +00:00
|
|
|
*
|
2013-06-21 09:35:14 +00:00
|
|
|
* @package customcertelement_date
|
2013-03-14 01:19:08 +00:00
|
|
|
* @copyright Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2013-06-21 09:35:14 +00:00
|
|
|
class customcert_element_date extends customcert_element_base {
|
2013-03-14 01:19:08 +00:00
|
|
|
|
|
|
|
/**
|
2013-06-06 10:59:08 +00:00
|
|
|
* Constructor.
|
2013-03-14 01:19:08 +00:00
|
|
|
*
|
2013-06-06 10:59:08 +00:00
|
|
|
* @param stdClass $element the element data
|
2013-03-14 01:19:08 +00:00
|
|
|
*/
|
2013-06-06 10:59:08 +00:00
|
|
|
function __construct($element) {
|
|
|
|
parent::__construct($element);
|
|
|
|
|
|
|
|
// Set the item and format for this element.
|
2013-03-14 01:19:08 +00:00
|
|
|
$dateitem = '';
|
|
|
|
$dateformat = '';
|
|
|
|
|
|
|
|
if (!empty($this->element->data)) {
|
|
|
|
$dateinfo = json_decode($this->element->data);
|
|
|
|
$dateitem = $dateinfo->dateitem;
|
|
|
|
$dateformat = $dateinfo->dateformat;
|
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
$this->element->dateitem = $dateitem;
|
|
|
|
$this->element->dateformat = $dateformat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
|
|
|
*
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param mod_customcert_edit_element_form $mform the edit_form instance
|
2013-06-06 10:59:08 +00:00
|
|
|
*/
|
|
|
|
public function render_form_elements($mform) {
|
2013-04-24 09:47:36 +00:00
|
|
|
// Get the possible date options.
|
|
|
|
$dateoptions = array();
|
2013-06-24 04:38:37 +00:00
|
|
|
$dateoptions[CUSTOMCERT_DATE_ISSUE] = get_string('issueddate', 'certificate');
|
|
|
|
$dateoptions[CUSTOMCERT_DATE_COMPLETION] = get_string('completiondate', 'certificate');
|
2013-06-21 09:35:14 +00:00
|
|
|
$dateoptions = $dateoptions + customcert_element_grade::get_grade_items();
|
2013-03-14 01:19:08 +00:00
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions);
|
|
|
|
$mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date');
|
2013-06-05 09:32:12 +00:00
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('select', 'dateformat', get_string('dateformat', 'customcertelement_date'), self::get_date_formats());
|
|
|
|
$mform->addHelpButton('dateformat', 'dateformat', 'customcertelement_date');
|
2013-06-05 09:32:12 +00:00
|
|
|
|
|
|
|
parent::render_form_elements($mform);
|
2013-03-14 01:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will handle how form data will be saved into the data column in the
|
2013-04-24 09:47:36 +00:00
|
|
|
* customcert_elements table.
|
2013-03-14 01:19:08 +00:00
|
|
|
*
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param stdClass $data the form data
|
2013-03-14 01:19:08 +00:00
|
|
|
* @return string the json encoded array
|
|
|
|
*/
|
|
|
|
public function save_unique_data($data) {
|
|
|
|
// Array of data we will be storing in the database.
|
|
|
|
$arrtostore = array(
|
2013-06-06 10:59:08 +00:00
|
|
|
'dateitem' => $data->dateitem,
|
|
|
|
'dateformat' => $data->dateformat
|
2013-03-14 01:19:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Encode these variables before saving into the DB.
|
|
|
|
return json_encode($arrtostore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-10 09:53:56 +00:00
|
|
|
* Handles rendering the element on the pdf.
|
2013-03-14 01:19:08 +00:00
|
|
|
*
|
2013-06-12 10:35:27 +00:00
|
|
|
* @param pdf $pdf the pdf object
|
2013-03-14 01:19:08 +00:00
|
|
|
*/
|
2013-06-05 08:42:18 +00:00
|
|
|
public function render($pdf) {
|
2013-06-24 04:38:37 +00:00
|
|
|
global $COURSE, $DB;
|
|
|
|
|
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the information stored in the database.
|
|
|
|
$dateinfo = json_decode($this->element->data);
|
|
|
|
$dateitem = $dateinfo->dateitem;
|
|
|
|
$dateformat = $dateinfo->dateformat;
|
|
|
|
|
|
|
|
// Get the customcert issue date and set the date to the time the issue was given, can be overwritten later.
|
|
|
|
$issue = $DB->get_record('customcert_issues', array('customcertid' => $this->element->id), '*', MUST_EXIST);
|
|
|
|
|
|
|
|
if ($dateitem == CUSTOMCERT_DATE_ISSUE) {
|
|
|
|
$date = $issue->timecreated;
|
|
|
|
} else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) {
|
|
|
|
// Get the enrolment end date.
|
|
|
|
$sql = "SELECT MAX(c.timecompleted) as timecompleted
|
|
|
|
FROM {course_completions} c
|
|
|
|
WHERE c.userid = :userid
|
|
|
|
AND c.course = :courseid";
|
|
|
|
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $COURSE->id))) {
|
|
|
|
if (!empty($timecompleted->timecompleted)) {
|
|
|
|
$date = $timecompleted->timecompleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$gradeitem = new stdClass();
|
|
|
|
$gradeitem->gradeitem = $dateitem;
|
|
|
|
$gradeitem->gradeformat = GRADE_DISPLAY_TYPE_PERCENTAGE;
|
|
|
|
if ($modinfo = customcert_element_grade::get_grade($gradeitem, $issue->userid)) {
|
|
|
|
$date = $modinfo->dategraded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a date has been set.
|
|
|
|
if (!empty($date)) {
|
|
|
|
switch ($dateformat) {
|
|
|
|
case 1:
|
|
|
|
$certificatedate = userdate($date, '%B %d, %Y');
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$suffix = $this->get_ordinal_number_suffix(userdate($date, '%d'));
|
|
|
|
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
$certificatedate = userdate($date, '%d %B %Y');
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
$certificatedate = userdate($date, '%B %Y');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::render_content($pdf, $certificatedate);
|
|
|
|
}
|
2013-03-14 01:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to return all the date formats.
|
|
|
|
*
|
|
|
|
* @return array the list of date formats
|
|
|
|
*/
|
2013-04-24 09:47:36 +00:00
|
|
|
public static function get_date_formats() {
|
2013-06-24 04:38:37 +00:00
|
|
|
$dateformats = array();
|
|
|
|
$dateformats[1] = 'January 1, 2000';
|
|
|
|
$dateformats[2] = 'January 1st, 2000';
|
|
|
|
$dateformats[3] = '1 January 2000';
|
|
|
|
$dateformats[4] = 'January 2000';
|
|
|
|
$dateformats[5] = get_string('userdateformat', 'customcertelement_date');
|
|
|
|
|
|
|
|
return $dateformats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to return the suffix of the day of
|
|
|
|
* the month, eg 'st' if it is the 1st of the month.
|
|
|
|
*
|
|
|
|
* @param int $day the day of the month
|
|
|
|
* @return string the suffix.
|
|
|
|
*/
|
|
|
|
private function get_ordinal_number_suffix($day) {
|
|
|
|
if (!in_array(($day % 100), array(11, 12, 13))) {
|
|
|
|
switch ($day % 10) {
|
|
|
|
// Handle 1st, 2nd, 3rd.
|
|
|
|
case 1: return get_string('numbersuffix_st', 'customcertelement_date');
|
|
|
|
case 2: return get_string('numbersuffix_nd', 'customcertelement_date');
|
|
|
|
case 3: return get_string('numbersuffix_rd', 'customcertelement_date');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'th';
|
|
|
|
}
|
2013-03-14 01:19:08 +00:00
|
|
|
}
|