Skip to content

Commit 9a08c0d

Browse files
committed
removed some errors in the code
1 parent f478725 commit 9a08c0d

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

classes/certificate.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -552,23 +552,34 @@ public static function issue_certificate($certificateid, $userid) {
552552
return $issueid;
553553
}
554554

555-
public static function generate_code() {
555+
/**
556+
* Generates an unused code of random letters and numbers.
557+
*
558+
* @return string
559+
*/
560+
public static function generate_code(): string {
556561
global $DB;
557562

558-
// Get the user's selected method from settings
563+
// Get the user's selected method from settings.
559564
$method = get_config('customcert', 'codegenerationmethod');
560565

561-
// If the upper/lower/digits is selected (0), use the upper/lower/digits code generation method
566+
// If the upper/lower/digits is selected (0), use the upper/lower/digits code generation method.
562567
if ($method == 0) {
563568
return self::generate_code_upper_lower_digits();
564569
}
565570

566-
// Otherwise, use the digits with hyphens method (1)
571+
// Otherwise, use the digits with hyphens method (1).
567572
return self::generate_code_digits_with_hyphens();
568573
}
569574

570-
// Upper/lower/digits random string
571-
private static function generate_code_upper_lower_digits() {
575+
// Upper/lower/digits random string.
576+
/**
577+
* Generate a random code of the format XXXXXXXXXX, where each X is a character from the set [A-Za-z0-9].
578+
* Does not check that it is unused.
579+
*
580+
* @return string
581+
*/
582+
private static function generate_code_upper_lower_digits(): string {
572583
global $DB;
573584

574585
$uniquecodefound = false;
@@ -584,29 +595,34 @@ private static function generate_code_upper_lower_digits() {
584595
return $code;
585596
}
586597

587-
// Digits with hyphens
588-
private static function generate_code_digits_with_hyphens() {
598+
/**
599+
* Generate an random code of the format XXXX-XXXX-XXXX, where each X is a random digit.
600+
* Does not check that it is unused.
601+
*
602+
* @return string
603+
*/
604+
private static function generate_code_digits_with_hyphens(): string {
589605
global $DB;
590606

591607
// Define the character set (digits only).
592608
$characters = '0123456789';
593-
$charCount = strlen($characters); // Cache the length to optimize loop performance
594-
$length = 12; // Total length excluding hyphens
609+
$charCount = strlen($characters); // Cache the length to optimize loop performance.
610+
$length = 12; // Total length excluding hyphens.
595611

596612
do {
597-
// Generate a raw code
613+
// Generate a raw code.
598614
$rawcode = '';
599615
for ($i = 0; $i < $length; $i++) {
600-
$rawcode .= $characters[random_int(0, $charCount - 1)]; // Secure random number selection
616+
$rawcode .= $characters[random_int(0, $charCount - 1)]; // Secure random number selection.
601617
}
602618

603-
// Format the code as XXXX-XXXX-XXXX
619+
// Format the code as XXXX-XXXX-XXXX.
604620
$code = substr($rawcode, 0, 4) . '-' . substr($rawcode, 4, 4) . '-' . substr($rawcode, 8, 4);
605621

606-
// Check if the generated code already exists in the database
622+
// Check if the generated code already exists in the database.
607623
$exists = $DB->record_exists('customcert_issues', ['code' => $code]);
608624

609-
} while ($exists); // Repeat until a unique code is found
625+
} while ($exists); // Repeat until a unique code is found.
610626

611627
return $code;
612628
}

db/upgrade.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,25 +298,42 @@ function xmldb_customcert_upgrade($oldversion) {
298298
upgrade_mod_savepoint(true, 2024042205, 'customcert');
299299
}
300300

301-
// Drop the unique index on 'code' and add a non-unique one.
302301
if ($oldversion < 2024042210) {
303302
$table = new xmldb_table('customcert_issues');
304-
305-
// Drop existing unique index if it exists.
306303
$index = new xmldb_index('code', XMLDB_INDEX_UNIQUE, ['code']);
304+
307305
if ($dbman->index_exists($table, $index)) {
308306
$dbman->drop_index($table, $index);
309307
}
310308

311-
// Add non-unique index.
312309
$index = new xmldb_index('code', XMLDB_INDEX_NOTUNIQUE, ['code']);
310+
313311
if (!$dbman->index_exists($table, $index)) {
314312
$dbman->add_index($table, $index);
315313
}
316314

317-
// Save the upgrade step.
315+
// Update the plugin version in the database.
318316
upgrade_plugin_savepoint(true, 2024042210, 'mod', 'customcert');
319317
}
320318

319+
if ($oldversion < 2025041401) {
320+
$table = new xmldb_table('customcert');
321+
322+
// Add 'usecustomfilename' field.
323+
$field = new xmldb_field('usecustomfilename', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'deliveryoption');
324+
if (!$dbman->field_exists($table, $field)) {
325+
$dbman->add_field($table, $field);
326+
}
327+
328+
// Add 'customfilenamepattern' field.
329+
$field = new xmldb_field('customfilenamepattern', XMLDB_TYPE_TEXT, null, null, null, null, null, 'usecustomfilename');
330+
if (!$dbman->field_exists($table, $field)) {
331+
$dbman->add_field($table, $field);
332+
}
333+
334+
// Savepoint reached.
335+
upgrade_mod_savepoint(true, 2025041401, 'customcert');
336+
}
337+
321338
return true;
322-
}
339+
}

element/qrcode/classes/element.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public function render($pdf, $preview, $user) {
158158
$context = \context::instance_by_id($issue->contextid);
159159

160160
$urlparams = [
161-
'certId' => $code,
161+
'code' => $code,
162+
'qrcode' => 1,
162163
];
163164

164165
// We only add the 'contextid' to the link if the site setting for verifying all certificates is off,

settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@
8585
'customcert/codegenerationmethod',
8686
get_string('codegenerationmethod', 'customcert'),
8787
get_string('codegenerationmethod_desc', 'customcert'),
88-
0, // Default option (0 = Upper/lower/digits random string method)
88+
0, // Default option (0 = Upper/lower/digits random string method).
8989
[
90-
0 => get_string('Upper/lower/digits', 'customcert'), // Upper/lower/digits random string
91-
1 => get_string('digits-with-hyphens', 'customcert') // Digits with hyphens numeric code
90+
0 => get_string('codegenerationmethod_upperlowerdigits', 'customcert'), // Upper/lower/digits random string.
91+
1 => get_string('codegenerationmethod_digitshyphens', 'customcert'), // Digits with hyphens numeric code.
9292
]
9393
));
9494

0 commit comments

Comments
 (0)