Skip to content

Commit 5637a63

Browse files
leonstrmdjnelson
authored andcommitted
Add expiry dates to recipient list, verify (#499)
Add a new element, customcertelement_expiry, which represents the certificate's expiry date, 1-5 years from either the award date or the course completion date. This date is shown in the recipients table shown to the teacher and the verify page when this element is added to a certificate.
1 parent 94cfd4b commit 5637a63

File tree

13 files changed

+739
-47
lines changed

13 files changed

+739
-47
lines changed

classes/output/verify_certificate_result.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class verify_certificate_result implements templatable, renderable {
6161
*/
6262
public $certificatename;
6363

64+
/**
65+
* @var int The certificate's expiry date (optional).
66+
*/
67+
public $expiry;
68+
6469
/**
6570
* Constructor.
6671
*
@@ -76,6 +81,12 @@ public function __construct($result) {
7681
$this->courseurl = new \moodle_url('/course/view.php', ['id' => $result->courseid]);
7782
$this->coursefullname = format_string($result->coursefullname, true, ['context' => $context]);
7883
$this->certificatename = format_string($result->certificatename, true, ['context' => $context]);
84+
85+
if (property_exists($result, 'expiry')) {
86+
$this->expiry = $result->expiry;
87+
} else {
88+
$this->expiry = null;
89+
}
7990
}
8091

8192
/**
@@ -92,6 +103,10 @@ public function export_for_template(\renderer_base $output) {
92103
$result->courseurl = $this->courseurl;
93104
$result->certificatename = $this->certificatename;
94105

106+
if (!empty($this->expiry)) {
107+
$result->expiry = $this->expiry;
108+
}
109+
95110
return $result;
96111
}
97112
}

classes/report_table.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace mod_customcert;
2626

27+
use \customcertelement_expiry\element as expiry_element;
28+
2729
defined('MOODLE_INTERNAL') || die;
2830

2931
global $CFG;
@@ -67,13 +69,23 @@ public function __construct($customcertid, $cm, $groupmode, $download = null) {
6769

6870
$context = \context_module::instance($cm->id);
6971
$extrafields = \core_user\fields::for_identity($context)->get_required_fields();
72+
$showexpiry = false;
73+
74+
if (class_exists('\customcertelement_expiry\element')) {
75+
$showexpiry = expiry_element::has_expiry($customcertid);
76+
}
7077

7178
$columns = [];
7279
$columns[] = 'fullname';
7380
foreach ($extrafields as $extrafield) {
7481
$columns[] = $extrafield;
7582
}
7683
$columns[] = 'timecreated';
84+
85+
if ($showexpiry) {
86+
$columns[] = 'timeexpires';
87+
}
88+
7789
$columns[] = 'code';
7890

7991
$headers = [];
@@ -82,6 +94,11 @@ public function __construct($customcertid, $cm, $groupmode, $download = null) {
8294
$headers[] = \core_user\fields::get_display_name($extrafield);
8395
}
8496
$headers[] = get_string('receiveddate', 'customcert');
97+
98+
if ($showexpiry) {
99+
$headers[] = get_string('expireson', 'customcertelement_expiry');
100+
}
101+
85102
$headers[] = get_string('code', 'customcert');
86103

87104
// Check if we were passed a filename, which means we want to download it.
@@ -142,6 +159,20 @@ public function col_timecreated($user) {
142159
return userdate($user->timecreated, $format);
143160
}
144161

162+
/**
163+
* Generate the optional certificate expires time column.
164+
*
165+
* @param \stdClass $user
166+
* @return string
167+
*/
168+
public function col_timeexpires($user) {
169+
if ($this->is_downloading() === '') {
170+
return expiry_element::get_expiry_html($this->customcertid, $user->id);
171+
}
172+
$format = '%Y-%m-%d %H:%M';
173+
return userdate(expiry_element::get_expiry_date($this->customcertid, $user->id), $format);
174+
}
175+
145176
/**
146177
* Generate the code column.
147178
*
@@ -223,4 +254,3 @@ public function download() {
223254
exit;
224255
}
225256
}
226-

db/upgrade.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,24 @@ function xmldb_customcert_upgrade($oldversion) {
267267
upgrade_mod_savepoint(true, 2024042202, 'customcert');
268268
}
269269

270+
if ($oldversion < 2024042203) {
271+
$elements = $DB->get_records('customcert_elements', ['element' => 'date']);
272+
273+
foreach ($elements as $element) {
274+
$data = json_decode($element->data);
275+
276+
// If dateitem is between CUSTOMCERT_DATE_EXPIRY_ONE and CUSTOMCERT_DATE_EXPIRY_FIVE.
277+
if ((intval($data->dateitem) <= -8) && (intval($data->dateitem) >= -12)) {
278+
$data->startfrom = 'award';
279+
$element->data = json_encode($data);
280+
$element->element = 'expiry';
281+
$DB->update_record('customcert_elements', $element);
282+
}
283+
}
284+
285+
// Customcert savepoint reached.
286+
upgrade_mod_savepoint(true, 2024042203, 'customcert');
287+
}
288+
270289
return true;
271290
}

element/date/classes/element.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,6 @@
6666
*/
6767
define('CUSTOMCERT_DATE_ENROLMENT_END', '-7');
6868

69-
/**
70-
* Date - Relative expiry date of 1 year
71-
*/
72-
define('CUSTOMCERT_DATE_EXPIRY_ONE', '-8');
73-
74-
/**
75-
* Date - Relative expiry date of 2 year
76-
*/
77-
define('CUSTOMCERT_DATE_EXPIRY_TWO', '-9');
78-
79-
/**
80-
* Date - Relative expiry date of 3 year
81-
*/
82-
define('CUSTOMCERT_DATE_EXPIRY_THREE', '-10');
83-
84-
/**
85-
* Date - Relative expiry date of 4 year
86-
*/
87-
define('CUSTOMCERT_DATE_EXPIRY_FOUR', '-11');
88-
89-
/**
90-
* Date - Relative expiry date of 5 year
91-
*/
92-
define('CUSTOMCERT_DATE_EXPIRY_FIVE', '-12');
93-
9469
require_once($CFG->dirroot . '/lib/grade/constants.php');
9570

9671
/**
@@ -120,11 +95,6 @@ public function render_form_elements($mform) {
12095
}
12196
$dateoptions[CUSTOMCERT_DATE_ENROLMENT_START] = get_string('enrolmentstartdate', 'customcertelement_date');
12297
$dateoptions[CUSTOMCERT_DATE_ENROLMENT_END] = get_string('enrolmentenddate', 'customcertelement_date');
123-
$dateoptions[CUSTOMCERT_DATE_EXPIRY_ONE] = get_string('expirydateone', 'customcertelement_date');
124-
$dateoptions[CUSTOMCERT_DATE_EXPIRY_TWO] = get_string('expirydatetwo', 'customcertelement_date');
125-
$dateoptions[CUSTOMCERT_DATE_EXPIRY_THREE] = get_string('expirydatethree', 'customcertelement_date');
126-
$dateoptions[CUSTOMCERT_DATE_EXPIRY_FOUR] = get_string('expirydatefour', 'customcertelement_date');
127-
$dateoptions[CUSTOMCERT_DATE_EXPIRY_FIVE] = get_string('expirydatefive', 'customcertelement_date');
12898
$dateoptions[CUSTOMCERT_DATE_COURSE_START] = get_string('coursestartdate', 'customcertelement_date');
12999
$dateoptions[CUSTOMCERT_DATE_COURSE_END] = get_string('courseenddate', 'customcertelement_date');
130100
$dateoptions[CUSTOMCERT_DATE_COURSE_GRADE] = get_string('coursegradedate', 'customcertelement_date');
@@ -193,16 +163,6 @@ public function render($pdf, $preview, $user) {
193163

194164
if ($dateitem == CUSTOMCERT_DATE_ISSUE) {
195165
$date = $issue->timecreated;
196-
} else if ($dateitem == CUSTOMCERT_DATE_EXPIRY_ONE) {
197-
$date = strtotime('+1 years', $issue->timecreated);
198-
} else if ($dateitem == CUSTOMCERT_DATE_EXPIRY_TWO) {
199-
$date = strtotime('+2 years', $issue->timecreated);
200-
} else if ($dateitem == CUSTOMCERT_DATE_EXPIRY_THREE) {
201-
$date = strtotime('+3 years', $issue->timecreated);
202-
} else if ($dateitem == CUSTOMCERT_DATE_EXPIRY_FOUR) {
203-
$date = strtotime('+4 years', $issue->timecreated);
204-
} else if ($dateitem == CUSTOMCERT_DATE_EXPIRY_FIVE) {
205-
$date = strtotime('+5 years', $issue->timecreated);
206166
} else if ($dateitem == CUSTOMCERT_DATE_CURRENT_DATE) {
207167
$date = time();
208168
} else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) {

element/date/lang/en/customcertelement_date.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
$string['dateitem_help'] = 'This will be the date that is printed on the certificate';
3434
$string['enrolmentenddate'] = 'Enrolment end date';
3535
$string['enrolmentstartdate'] = 'Enrolment start date';
36-
$string['expirydatefive'] = 'Expiry date (5 year)';
37-
$string['expirydatefour'] = 'Expiry date (4 year)';
38-
$string['expirydateone'] = 'Expiry date (1 year)';
39-
$string['expirydatethree'] = 'Expiry date (3 year)';
40-
$string['expirydatetwo'] = 'Expiry date (2 year)';
4136
$string['issueddate'] = 'Issued date';
4237
$string['numbersuffix_nd_as_in_second'] = 'nd';
4338
$string['numbersuffix_rd_as_in_third'] = 'rd';

0 commit comments

Comments
 (0)