Skip to content

Commit e9d22ff

Browse files
fulldecentmdjnelson
authored andcommitted
Add choice for code format (#668)
1 parent 6d6eb09 commit e9d22ff

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

classes/certificate.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,23 +539,48 @@ public static function issue_certificate($certificateid, $userid) {
539539
}
540540

541541
/**
542-
* Generates a 10-digit code of random letters and numbers.
542+
* Generates an unused code of random letters and numbers.
543543
*
544544
* @return string
545545
*/
546-
public static function generate_code() {
546+
public static function generate_code(): string {
547547
global $DB;
548548

549-
$uniquecodefound = false;
550-
$code = random_string(10);
551-
while (!$uniquecodefound) {
552-
if (!$DB->record_exists('customcert_issues', ['code' => $code])) {
553-
$uniquecodefound = true;
554-
} else {
555-
$code = random_string(10);
556-
}
557-
}
549+
// Get the user's selected method from settings.
550+
$method = get_config('customcert', 'codegenerationmethod');
558551

552+
do {
553+
$code = match ($method) {
554+
'0' => self::generate_code_upper_lower_digits(),
555+
'1' => self::generate_code_digits_with_hyphens(),
556+
default => self::generate_code_upper_lower_digits(),
557+
};
558+
} while ($DB->record_exists('customcert_issues', ['code' => $code]));
559559
return $code;
560560
}
561+
562+
/**
563+
* Generate a random code of the format XXXXXXXXXX, where each X is a character from the set [A-Za-z0-9].
564+
* Does not check that it is unused.
565+
*
566+
* @return string
567+
*/
568+
private static function generate_code_upper_lower_digits(): string {
569+
return random_string(10);
570+
}
571+
572+
/**
573+
* Generate an random code of the format XXXX-XXXX-XXXX, where each X is a random digit.
574+
* Does not check that it is unused.
575+
*
576+
* @return string
577+
*/
578+
private static function generate_code_digits_with_hyphens(): string {
579+
return sprintf(
580+
'%04d-%04d-%04d',
581+
random_int(0, 9999),
582+
random_int(0, 9999),
583+
random_int(0, 9999)
584+
);
585+
}
561586
}

lang/en/customcert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,7 @@
243243
$string['verifycertificatedesc'] = 'This link will take you to a new screen where you will be able to verify certificates on the site';
244244
$string['width'] = 'Width';
245245
$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.';
246+
$string['codegenerationmethod'] = 'Code generation method';
247+
$string['codegenerationmethod_desc'] = 'Choose between the two methods for generating certificate codes.';
248+
$string['codegenerationmethod_upperlowerdigits'] = '6aOdbLEuoC (Upper/lower/digits random string)';
249+
$string['codegenerationmethod_digitshyphens'] = '0123-4567-8901 (Digits with hyphens)';

settings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@
8181
$settings->add(new admin_setting_heading('defaults',
8282
get_string('modeditdefaults', 'admin'), get_string('condifmodeditdefaults', 'admin')));
8383

84+
$settings->add(new admin_setting_configselect(
85+
'customcert/codegenerationmethod',
86+
get_string('codegenerationmethod', 'customcert'),
87+
get_string('codegenerationmethod_desc', 'customcert'),
88+
0, // Default option (0 = Upper/lower/digits random string method).
89+
[
90+
0 => get_string('codegenerationmethod_upperlowerdigits', 'customcert'), // Upper/lower/digits random string.
91+
1 => get_string('codegenerationmethod_digitshyphens', 'customcert') // Digits with hyphens numeric code.
92+
]
93+
));
94+
8495
$yesnooptions = [
8596
0 => get_string('no'),
8697
1 => get_string('yes'),

0 commit comments

Comments
 (0)