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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-14 01:19:08 +00:00
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
/**
|
|
|
|
* This file contains the customcert element date's core interaction API.
|
|
|
|
*
|
|
|
|
* @package customcertelement_date
|
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
namespace customcertelement_date;
|
2013-06-12 10:35:27 +00:00
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2013-06-12 10:35:27 +00:00
|
|
|
|
2013-06-24 04:38:37 +00:00
|
|
|
/**
|
|
|
|
* Date - Issue
|
|
|
|
*/
|
2016-08-26 08:43:45 +00:00
|
|
|
define('CUSTOMCERT_DATE_ISSUE', '-1');
|
2013-06-24 04:38:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Date - Completion
|
|
|
|
*/
|
2016-08-26 08:43:45 +00:00
|
|
|
define('CUSTOMCERT_DATE_COMPLETION', '-2');
|
2013-06-24 04:38:37 +00:00
|
|
|
|
2017-06-01 06:47:52 +00:00
|
|
|
/**
|
|
|
|
* Date - Course start
|
|
|
|
*/
|
|
|
|
define('CUSTOMCERT_DATE_COURSE_START', '-3');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Date - Course end
|
|
|
|
*/
|
|
|
|
define('CUSTOMCERT_DATE_COURSE_END', '-4');
|
|
|
|
|
2016-03-15 06:50:22 +00:00
|
|
|
require_once($CFG->dirroot . '/lib/grade/constants.php');
|
|
|
|
|
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-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2013-03-14 01:19:08 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2015-12-11 06:53:45 +00:00
|
|
|
class element extends \mod_customcert\element {
|
2013-03-14 01:19:08 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
|
|
|
*
|
2016-02-16 09:03:34 +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-25 01:30:45 +00:00
|
|
|
$dateoptions[CUSTOMCERT_DATE_ISSUE] = get_string('issueddate', 'customcertelement_date');
|
|
|
|
$dateoptions[CUSTOMCERT_DATE_COMPLETION] = get_string('completiondate', 'customcertelement_date');
|
2017-06-01 06:47:52 +00:00
|
|
|
$dateoptions[CUSTOMCERT_DATE_COURSE_START] = get_string('coursestartdate', 'customcertelement_date');
|
|
|
|
$dateoptions[CUSTOMCERT_DATE_COURSE_END] = get_string('courseenddate', 'customcertelement_date');
|
2017-05-31 04:40:10 +00:00
|
|
|
$dateoptions = $dateoptions + \customcertelement_grade\element::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-07-23 04:43:56 +00:00
|
|
|
}
|
2013-03-14 01:19:08 +00:00
|
|
|
|
2013-07-23 04:43:56 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2015-12-11 06:53:45 +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-07-23 04:43:56 +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
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \pdf $pdf the pdf object
|
2013-06-28 07:17:43 +00:00
|
|
|
* @param bool $preview true if it is a preview, false otherwise
|
2016-08-23 08:22:58 +00:00
|
|
|
* @param \stdClass $user the user we are rendering this for
|
2013-03-14 01:19:08 +00:00
|
|
|
*/
|
2016-08-23 08:22:58 +00:00
|
|
|
public function render($pdf, $preview, $user) {
|
2017-05-30 09:38:32 +00:00
|
|
|
global $DB;
|
2013-06-24 04:38:37 +00:00
|
|
|
|
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:38:32 +00:00
|
|
|
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
|
|
|
|
|
2013-06-24 04:38:37 +00:00
|
|
|
// Decode the information stored in the database.
|
|
|
|
$dateinfo = json_decode($this->element->data);
|
|
|
|
$dateitem = $dateinfo->dateitem;
|
|
|
|
$dateformat = $dateinfo->dateformat;
|
|
|
|
|
2013-07-23 10:04:46 +00:00
|
|
|
// If we are previewing this certificate then just show a demonstration date.
|
|
|
|
if ($preview) {
|
|
|
|
$date = time();
|
2013-06-24 04:38:37 +00:00
|
|
|
} else {
|
2013-07-23 10:04:46 +00:00
|
|
|
// Get the page.
|
|
|
|
$page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST);
|
2016-03-15 06:50:22 +00:00
|
|
|
// Get the customcert this page belongs to.
|
|
|
|
$customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST);
|
2013-07-23 10:04:46 +00:00
|
|
|
// Now we can get the issue for this user.
|
2017-02-16 12:12:19 +00:00
|
|
|
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id),
|
|
|
|
'*', MUST_EXIST);
|
2013-07-23 10:04:46 +00:00
|
|
|
|
|
|
|
if ($dateitem == CUSTOMCERT_DATE_ISSUE) {
|
|
|
|
$date = $issue->timecreated;
|
|
|
|
} else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) {
|
2017-08-08 05:46:16 +00:00
|
|
|
// Get the last completion date.
|
2013-07-23 10:04:46 +00:00
|
|
|
$sql = "SELECT MAX(c.timecompleted) as timecompleted
|
2015-03-11 08:22:44 +00:00
|
|
|
FROM {course_completions} c
|
|
|
|
WHERE c.userid = :userid
|
|
|
|
AND c.course = :courseid";
|
2017-05-30 09:38:32 +00:00
|
|
|
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $courseid))) {
|
2013-07-23 10:04:46 +00:00
|
|
|
if (!empty($timecompleted->timecompleted)) {
|
|
|
|
$date = $timecompleted->timecompleted;
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 06:47:52 +00:00
|
|
|
} else if ($dateitem == CUSTOMCERT_DATE_COURSE_START) {
|
|
|
|
$date = $DB->get_field('course', 'startdate', array('id' => $courseid));
|
|
|
|
} else if ($dateitem == CUSTOMCERT_DATE_COURSE_END) {
|
|
|
|
$date = $DB->get_field('course', 'enddate', array('id' => $courseid));
|
2013-07-23 10:04:46 +00:00
|
|
|
} else {
|
2017-08-08 05:46:16 +00:00
|
|
|
if ($modinfo = \customcertelement_grade\element::get_mod_grade($dateitem, GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
|
$issue->userid)) {
|
2016-03-15 06:50:22 +00:00
|
|
|
if (!empty($modinfo->dategraded)) {
|
|
|
|
$date = $modinfo->dategraded;
|
|
|
|
}
|
2013-07-23 10:04:46 +00:00
|
|
|
}
|
2013-06-24 04:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a date has been set.
|
|
|
|
if (!empty($date)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_date_format_string($date, $dateformat));
|
2013-06-24 04:38:37 +00:00
|
|
|
}
|
2013-03-14 01:19:08 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* Render the element in html.
|
|
|
|
*
|
|
|
|
* This function is used to render the element when we are using the
|
|
|
|
* drag and drop interface to position it.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
|
|
|
* @return string the html
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
|
|
|
public function render_html() {
|
|
|
|
// 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);
|
|
|
|
$dateformat = $dateinfo->dateformat;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
return \mod_customcert\element_helper::render_html_content($this, $this->get_date_format_string(time(), $dateformat));
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param \mod_customcert\edit_element_form $mform the edit_form instance
|
2013-06-25 03:40:56 +00:00
|
|
|
*/
|
|
|
|
public function definition_after_data($mform) {
|
|
|
|
// Set the item and format for this element.
|
2015-12-10 07:26:49 +00:00
|
|
|
if (!empty($this->element->data)) {
|
|
|
|
$dateinfo = json_decode($this->element->data);
|
|
|
|
$this->element->dateitem = $dateinfo->dateitem;
|
|
|
|
$this->element->dateformat = $dateinfo->dateformat;
|
|
|
|
}
|
2013-06-25 03:40:56 +00:00
|
|
|
|
|
|
|
parent::definition_after_data($mform);
|
|
|
|
}
|
|
|
|
|
2013-07-23 05:21:54 +00:00
|
|
|
/**
|
|
|
|
* This function is responsible for handling the restoration process of the element.
|
|
|
|
*
|
|
|
|
* We will want to update the course module the date element is pointing to as it will
|
|
|
|
* have changed in the course restore.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \restore_customcert_activity_task $restore
|
2013-07-23 05:21:54 +00:00
|
|
|
*/
|
|
|
|
public function after_restore($restore) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$dateinfo = json_decode($this->element->data);
|
2015-12-11 06:53:45 +00:00
|
|
|
if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $dateinfo->dateitem)) {
|
2013-07-23 05:21:54 +00:00
|
|
|
$dateinfo->dateitem = $newitem->newitemid;
|
|
|
|
$DB->set_field('customcert_elements', 'data', self::save_unique_data($dateinfo), array('id' => $this->element->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 04:43:56 +00:00
|
|
|
/**
|
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() {
|
2017-06-01 08:51:50 +00:00
|
|
|
$date = time();
|
|
|
|
|
2017-06-11 05:57:04 +00:00
|
|
|
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
|
|
|
|
|
2017-06-01 08:51:50 +00:00
|
|
|
$dateformats = array(
|
2017-06-11 05:57:04 +00:00
|
|
|
1 => userdate($date, '%B %d, %Y'),
|
|
|
|
2 => userdate($date, '%B %d' . $suffix . ', %Y'),
|
2017-06-01 08:51:50 +00:00
|
|
|
'strftimedate' => userdate($date, get_string('strftimedate', 'langconfig')),
|
|
|
|
'strftimedatefullshort' => userdate($date, get_string('strftimedatefullshort', 'langconfig')),
|
|
|
|
'strftimedateshort' => userdate($date, get_string('strftimedateshort', 'langconfig')),
|
|
|
|
'strftimedatetime' => userdate($date, get_string('strftimedatetime', 'langconfig')),
|
|
|
|
'strftimedatetimeshort' => userdate($date, get_string('strftimedatetimeshort', 'langconfig')),
|
|
|
|
'strftimedaydate' => userdate($date, get_string('strftimedaydate', 'langconfig')),
|
|
|
|
'strftimedaydatetime' => userdate($date, get_string('strftimedaydatetime', 'langconfig')),
|
|
|
|
'strftimedayshort' => userdate($date, get_string('strftimedayshort', 'langconfig')),
|
|
|
|
'strftimedaytime' => userdate($date, get_string('strftimedaytime', 'langconfig')),
|
|
|
|
'strftimemonthyear' => userdate($date, get_string('strftimemonthyear', 'langconfig')),
|
|
|
|
'strftimerecent' => userdate($date, get_string('strftimerecent', 'langconfig')),
|
|
|
|
'strftimerecentfull' => userdate($date, get_string('strftimerecentfull', 'langconfig')),
|
|
|
|
'strftimetime' => userdate($date, get_string('strftimetime', 'langconfig'))
|
|
|
|
);
|
2013-06-24 04:38:37 +00:00
|
|
|
|
|
|
|
return $dateformats;
|
|
|
|
}
|
|
|
|
|
2015-12-10 03:33:22 +00:00
|
|
|
/**
|
|
|
|
* Returns the date in a readable format.
|
|
|
|
*
|
|
|
|
* @param int $date
|
|
|
|
* @param string $dateformat
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
protected function get_date_format_string($date, $dateformat) {
|
2017-06-01 08:51:50 +00:00
|
|
|
// Keeping for backwards compatibility.
|
|
|
|
if (is_number($dateformat)) {
|
|
|
|
switch ($dateformat) {
|
|
|
|
case 1:
|
|
|
|
$certificatedate = userdate($date, '%B %d, %Y');
|
|
|
|
break;
|
|
|
|
case 2:
|
2017-06-11 05:57:04 +00:00
|
|
|
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
|
2017-06-01 08:51:50 +00:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, so we must have been passed the actual format in the lang file.
|
|
|
|
if (!isset($certificatedate)) {
|
|
|
|
$certificatedate = userdate($date, get_string($dateformat, 'langconfig'));
|
2015-12-10 03:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $certificatedate;
|
|
|
|
}
|
|
|
|
|
2013-06-24 04:38:37 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-06-11 05:57:04 +00:00
|
|
|
protected static function get_ordinal_number_suffix($day) {
|
2013-06-24 04:38:37 +00:00
|
|
|
if (!in_array(($day % 100), array(11, 12, 13))) {
|
|
|
|
switch ($day % 10) {
|
|
|
|
// Handle 1st, 2nd, 3rd.
|
2013-07-23 04:43:56 +00:00
|
|
|
case 1:
|
2016-08-26 08:28:34 +00:00
|
|
|
return get_string('numbersuffix_st_as_in_first', 'customcertelement_date');
|
2013-07-23 04:43:56 +00:00
|
|
|
case 2:
|
2016-08-26 08:28:34 +00:00
|
|
|
return get_string('numbersuffix_nd_as_in_second', 'customcertelement_date');
|
2013-07-23 04:43:56 +00:00
|
|
|
case 3:
|
2016-08-26 08:28:34 +00:00
|
|
|
return get_string('numbersuffix_rd_as_in_third', 'customcertelement_date');
|
2013-06-24 04:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'th';
|
|
|
|
}
|
2013-03-14 01:19:08 +00:00
|
|
|
}
|