Skip to content

Commit 129747f

Browse files
committed
Fixed errors.
1 parent 2728a48 commit 129747f

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

classes/certificate.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3535
*/
3636
class certificate {
37-
3837
/**
3938
* Send the file inline to the browser.
4039
*/
@@ -293,7 +292,7 @@ public static function download_all_issues_for_instance(\mod_customcert\template
293292
public static function download_all_for_site(): void {
294293
global $DB;
295294

296-
list($namefields, $nameparams) = \core_user\fields::get_sql_fullname();
295+
[$namefields, $nameparams] = \core_user\fields::get_sql_fullname();
297296
$sql = "SELECT ci.*, $namefields as fullname, ct.id as templateid, ct.name as templatename, ct.contextid
298297
FROM {customcert_issues} ci
299298
JOIN {user} u
@@ -350,7 +349,7 @@ public static function get_issues($customcertid, $groupmode, $cm, $limitfrom, $l
350349
global $DB;
351350

352351
// Get the conditional SQL.
353-
list($conditionssql, $conditionsparams) = self::get_conditional_issues_sql($cm, $groupmode);
352+
[$conditionssql, $conditionsparams] = self::get_conditional_issues_sql($cm, $groupmode);
354353

355354
// If it is empty then return an empty array.
356355
if (empty($conditionsparams)) {
@@ -389,7 +388,7 @@ public static function get_number_of_issues($customcertid, $cm, $groupmode) {
389388
global $DB;
390389

391390
// Get the conditional SQL.
392-
list($conditionssql, $conditionsparams) = self::get_conditional_issues_sql($cm, $groupmode);
391+
[$conditionssql, $conditionsparams] = self::get_conditional_issues_sql($cm, $groupmode);
393392

394393
// If it is empty then return 0.
395394
if (empty($conditionsparams)) {
@@ -428,7 +427,7 @@ public static function get_conditional_issues_sql($cm, $groupmode) {
428427
// Get all users that can manage this certificate to exclude them from the report.
429428
$certmanagers = array_keys(get_users_by_capability($context, 'mod/customcert:manage', 'u.id'));
430429
$certmanagers = array_merge($certmanagers, array_keys(get_admins()));
431-
list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert');
430+
[$sql, $params] = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert');
432431
$conditionssql .= "AND NOT u.id $sql \n";
433432
$conditionsparams += $params;
434433

@@ -464,7 +463,7 @@ public static function get_conditional_issues_sql($cm, $groupmode) {
464463
return ['', []];
465464
}
466465

467-
list($sql, $params) = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED, 'grp');
466+
[$sql, $params] = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED, 'grp');
468467
$conditionssql .= "AND u.id $sql ";
469468
$conditionsparams += $params;
470469
}
@@ -605,22 +604,21 @@ private static function generate_code_digits_with_hyphens(): string {
605604

606605
// Define the character set (digits only).
607606
$characters = '0123456789';
608-
$charCount = strlen($characters); // Cache the length to optimize loop performance.
607+
$charcount = strlen($characters); // Cache the length to optimize loop performance.
609608
$length = 12; // Total length excluding hyphens.
610609

611610
do {
612611
// Generate a raw code.
613612
$rawcode = '';
614613
for ($i = 0; $i < $length; $i++) {
615-
$rawcode .= $characters[random_int(0, $charCount - 1)]; // Secure random number selection.
614+
$rawcode .= $characters[random_int(0, $charcount - 1)]; // Secure random number selection.
616615
}
617616

618617
// Format the code as XXXX-XXXX-XXXX.
619618
$code = substr($rawcode, 0, 4) . '-' . substr($rawcode, 4, 4) . '-' . substr($rawcode, 8, 4);
620619

621620
// Check if the generated code already exists in the database.
622621
$exists = $DB->record_exists('customcert_issues', ['code' => $code]);
623-
624622
} while ($exists); // Repeat until a unique code is found.
625623

626624
return $code;

lib.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,26 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
350350
if (has_capability('mod/customcert:manage', $settings->get_page()->cm->context)) {
351351
// Get the template id.
352352
$templateid = $DB->get_field('customcert', 'templateid', ['id' => $settings->get_page()->cm->instance]);
353-
$node = navigation_node::create(get_string('editcustomcert', 'customcert'),
354-
new moodle_url('/mod/customcert/edit.php', ['tid' => $templateid]),
355-
navigation_node::TYPE_SETTING, null, 'mod_customcert_edit',
356-
new pix_icon('t/edit', ''));
353+
$node = navigation_node::create(
354+
get_string('editcustomcert', 'customcert'),
355+
new moodle_url('/mod/customcert/edit.php', ['tid' => $templateid]),
356+
navigation_node::TYPE_SETTING,
357+
null,
358+
'mod_customcert_edit',
359+
new pix_icon('t/edit', '')
360+
);
357361
$customcertnode->add_node($node, $beforekey);
358362
}
359363

360364
if (has_capability('mod/customcert:verifycertificate', $settings->get_page()->cm->context)) {
361-
$node = navigation_node::create(get_string('verifycertificate', 'customcert'),
365+
$node = navigation_node::create(
366+
get_string('verifycertificate', 'customcert'),
362367
new moodle_url('/mod/customcert/verify_certificate.php', ['contextid' => $settings->get_page()->cm->context->id]),
363-
navigation_node::TYPE_SETTING, null, 'mod_customcert_verify_certificate',
364-
new pix_icon('t/check', ''));
368+
navigation_node::TYPE_SETTING,
369+
null,
370+
'mod_customcert_verify_certificate',
371+
new pix_icon('t/check', '')
372+
);
365373
$customcertnode->add_node($node, $beforekey);
366374
}
367375

@@ -380,8 +388,10 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
380388
function mod_customcert_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
381389
global $USER;
382390

383-
if (($user->id != $USER->id)
384-
&& !has_capability('mod/customcert:viewallcertificates', context_system::instance())) {
391+
if (
392+
($user->id != $USER->id)
393+
&& !has_capability('mod/customcert:viewallcertificates', context_system::instance())
394+
) {
385395
return;
386396
}
387397

@@ -392,8 +402,13 @@ function mod_customcert_myprofile_navigation(core_user\output\myprofile\tree $tr
392402
$params['course'] = $course->id;
393403
}
394404
$url = new moodle_url('/mod/customcert/my_certificates.php', $params);
395-
$node = new core_user\output\myprofile\node('miscellaneous', 'mycustomcerts',
396-
get_string('mycertificates', 'customcert'), null, $url);
405+
$node = new core_user\output\myprofile\node(
406+
'miscellaneous',
407+
'mycustomcerts',
408+
get_string('mycertificates', 'customcert'),
409+
null,
410+
$url
411+
);
397412
$tree->add_node($node);
398413
}
399414

@@ -431,8 +446,14 @@ function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) {
431446
$updateelement->name = clean_param($newvalue, PARAM_TEXT);
432447
$DB->update_record('customcert_elements', $updateelement);
433448

434-
return new \core\output\inplace_editable('mod_customcert', 'elementname', $element->id, true,
435-
$updateelement->name, $updateelement->name);
449+
return new \core\output\inplace_editable(
450+
'mod_customcert',
451+
'elementname',
452+
$element->id,
453+
true,
454+
$updateelement->name,
455+
$updateelement->name
456+
);
436457
}
437458
}
438459

@@ -449,14 +470,14 @@ function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) {
449470
* @param string $cert_code The unique code of the certificate.
450471
* @return string The generated public URL for the certificate.
451472
*/
452-
function generate_public_url_for_certificate(string $cert_code): string {
473+
function generate_public_url_for_certificate(string $certcode): string {
453474
global $CFG;
454475

455476
// Generate a security token for the certificate using a private function.
456-
$token = calculate_signature($cert_code);
477+
$token = calculate_signature($certcode);
457478

458479
// Construct and return the public URL to view the certificate.
459-
return $CFG->wwwroot . '/mod/customcert/view_user_cert.php?cert_code=' . urlencode($cert_code) . '&token=' . urlencode($token);
480+
return $CFG->wwwroot . '/mod/customcert/view_user_cert.php?cert_code=' . urlencode($certcode) . '&token=' . urlencode($token);
460481
}
461482

462483
/**
@@ -467,22 +488,22 @@ function generate_public_url_for_certificate(string $cert_code): string {
467488
* It prevents unauthorized access by ensuring that only valid certificates can
468489
* be accessed through a generated URL.
469490
*
470-
* The signature is generated using the HMAC (Hash-based Message Authentication Code)
491+
* The signature is generated using the HMAC (Hash-based Message Authentication Code)
471492
* method with SHA-256, ensuring strong security. It uses Moodle's `siteidentifier`
472493
* as the secret key, making it unique to each Moodle installation.
473494
*
474495
* @param string $cert_code The unique certificate code.
475496
* @return string The generated HMAC signature.
476497
*/
477-
function calculate_signature(string $cert_code): string {
498+
function calculate_signature(string $certcode): string {
478499
global $CFG;
479500

480501
// Define a namespaced message prefix to avoid signature collisions.
481-
$messagePrefix = 'mod_customcert:view_user_cert';
502+
$messageprefix = 'mod_customcert:view_user_cert';
482503

483504
// Construct the message that will be signed.
484505
// This includes the prefix and the certificate code to create a unique hash.
485-
$message = $messagePrefix . '|' . $cert_code;
506+
$message = $messageprefix . '|' . $certcode;
486507

487508
// Use Moodle's unique site identifier as the secret key for HMAC.
488509
// This ensures that signatures are installation-specific.

view_user_cert.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
216
// Include required Moodle configuration and custom certificate library.
17+
318
require_once(__DIR__ . '/../../config.php');
419
require_once($CFG->dirroot . '/mod/customcert/lib.php');
520

@@ -18,6 +33,7 @@
1833
* properly and showing the error message in an alert box.
1934
*
2035
* @param string $message The error message to display.
36+
* @package mod_customcert
2137
*/
2238
function display_error_page($message) {
2339
global $OUTPUT;
@@ -30,23 +46,23 @@ function display_error_page($message) {
3046

3147
// Retrieve certificate code and verification token from URL parameters.
3248
// 'optional_param' is used instead of 'required_param' to avoid Moodle throwing an automatic error page.
33-
$cert_code = optional_param('cert_code', '', PARAM_ALPHANUMEXT);
49+
$certcode = optional_param('cert_code', '', PARAM_ALPHANUMEXT);
3450
$token = optional_param('token', '', PARAM_ALPHANUMEXT);
3551

3652
// Ensure both required parameters are provided.
37-
if (empty($cert_code) || empty($token)) {
53+
if (empty($certcode) || empty($token)) {
3854
display_error_page('Certificate code or verification token is missing. Please check the URL and try again.');
3955
}
4056

4157
// Validate the provided token by regenerating it using the expected algorithm.
42-
$expected_token = calculate_signature($cert_code);
43-
if ($token !== $expected_token) {
58+
$expectedtoken = calculate_signature($certcode);
59+
if ($token !== $expectedtoken) {
4460
display_error_page('The verification token is invalid for this certificate. Please check the URL and try again.');
4561
}
4662

4763
// Retrieve the certificate issue entry using the provided certificate code.
4864
// This helps fetch the associated user ID to verify ownership.
49-
$issue = $DB->get_record('customcert_issues', ['code' => $cert_code], '*');
65+
$issue = $DB->get_record('customcert_issues', ['code' => $certcode], '*');
5066

5167
if (!$issue) {
5268
display_error_page('The certificate with the provided code could not be found. Please verify the certificate code and try again.');

0 commit comments

Comments
 (0)