Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hyphenated digits format #669

Open
wants to merge 1 commit into
base: MOODLE_404_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions classes/certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,23 +539,55 @@ public static function issue_certificate($certificateid, $userid) {
}

/**
* Generates a 10-digit code of random letters and numbers.
* Generates an unused code of random letters and numbers.
*
* @return string
*/
public static function generate_code() {
public static function generate_code(): string {
global $DB;

$uniquecodefound = false;
$code = random_string(10);
while (!$uniquecodefound) {
if (!$DB->record_exists('customcert_issues', ['code' => $code])) {
$uniquecodefound = true;
} else {
$code = random_string(10);
// Get the user's selected method from settings
$method = get_config('customcert', 'codegenerationmethod');

do {
switch ($method) {
case 0:
$code = self::generate_code_upper_lower_digits();
break;
case 1:
$code = self::generate_code_digits_with_hyphens();
break;
default:
$code = self::generate_code_upper_lower_digits();
break;
}
}

} while ($DB->record_exists('customcert_issues', ['code' => $code]));
return $code;
}

/**
* Generate a random code of the format XXXXXXXXXX, where each X is a character from the set [A-Za-z0-9].
* Does not check that it is unused.
*
* @return string
*/
private static function generate_code_upper_lower_digits(): string {
return random_string(10);
}

/**
* Generate an random code of the format XXXX-XXXX-XXXX, where each X is a random digit.
* Does not check that it is unused.
*
* @return string
*/
private static function generate_code_digits_with_hyphens(): string
{
return sprintf(
'%04d-%04d-%04d',
random_int(0, 9999),
random_int(0, 9999),
random_int(0, 9999)
);
}
}
4 changes: 4 additions & 0 deletions lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,7 @@
$string['verifycertificatedesc'] = 'This link will take you to a new screen where you will be able to verify certificates on the site';
$string['width'] = 'Width';
$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.';
$string['codegenerationmethod'] = 'Code generation method';
$string['codegenerationmethod_desc'] = 'Choose between the two methods for generating certificate codes.';
$string['Upper/lower/digits'] = '6aOdbLEuoC (Upper/lower/digits random string)';
$string['digits-with-hyphens'] = '0123-4567-8901 (Digits with hyphens)';
11 changes: 11 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@
$settings->add(new admin_setting_heading('defaults',
get_string('modeditdefaults', 'admin'), get_string('condifmodeditdefaults', 'admin')));

$settings->add(new admin_setting_configselect(
'customcert/codegenerationmethod',
get_string('codegenerationmethod', 'customcert'),
get_string('codegenerationmethod_desc', 'customcert'),
0, // Default option (0 = Upper/lower/digits random string method)
[
0 => get_string('Upper/lower/digits', 'customcert'), // Upper/lower/digits random string
1 => get_string('digits-with-hyphens', 'customcert') // Digits with hyphens numeric code
]
));

$yesnooptions = [
0 => get_string('no'),
1 => get_string('yes'),
Expand Down