@@ -552,23 +552,34 @@ public static function issue_certificate($certificateid, $userid) {
552
552
return $ issueid ;
553
553
}
554
554
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 {
556
561
global $ DB ;
557
562
558
- // Get the user's selected method from settings
563
+ // Get the user's selected method from settings.
559
564
$ method = get_config ('customcert ' , 'codegenerationmethod ' );
560
565
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.
562
567
if ($ method == 0 ) {
563
568
return self ::generate_code_upper_lower_digits ();
564
569
}
565
570
566
- // Otherwise, use the digits with hyphens method (1)
571
+ // Otherwise, use the digits with hyphens method (1).
567
572
return self ::generate_code_digits_with_hyphens ();
568
573
}
569
574
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 {
572
583
global $ DB ;
573
584
574
585
$ uniquecodefound = false ;
@@ -584,29 +595,34 @@ private static function generate_code_upper_lower_digits() {
584
595
return $ code ;
585
596
}
586
597
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 {
589
605
global $ DB ;
590
606
591
607
// Define the character set (digits only).
592
608
$ 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.
595
611
596
612
do {
597
- // Generate a raw code
613
+ // Generate a raw code.
598
614
$ rawcode = '' ;
599
615
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.
601
617
}
602
618
603
- // Format the code as XXXX-XXXX-XXXX
619
+ // Format the code as XXXX-XXXX-XXXX.
604
620
$ code = substr ($ rawcode , 0 , 4 ) . '- ' . substr ($ rawcode , 4 , 4 ) . '- ' . substr ($ rawcode , 8 , 4 );
605
621
606
- // Check if the generated code already exists in the database
622
+ // Check if the generated code already exists in the database.
607
623
$ exists = $ DB ->record_exists ('customcert_issues ' , ['code ' => $ code ]);
608
624
609
- } while ($ exists ); // Repeat until a unique code is found
625
+ } while ($ exists ); // Repeat until a unique code is found.
610
626
611
627
return $ code ;
612
628
}
0 commit comments