Skip to content

Conversation

Raza403
Copy link
Contributor

@Raza403 Raza403 commented May 1, 2025

Previously, this was working only for some specific certificates.

@mdjnelson
Copy link
Owner

Can you please look at failing CI pipeline please?

@mdjnelson
Copy link
Owner

Please rebase on the latest MOODLE_404_STABLE branch as I have bumped the Moodle Plugin CI version.

@Raza403 Raza403 force-pushed the features/custom-cert-view-all-certificates#231 branch from b215843 to f478725 Compare June 24, 2025 10:17
@Raza403
Copy link
Contributor Author

Raza403 commented Jun 24, 2025

@mdjnelson rebased.

@mdjnelson
Copy link
Owner

mdjnelson commented Jul 6, 2025

This is still failing. Please address the errors you can see at https://github.com/mdjnelson/moodle-mod_customcert/pull/681/checks.

*
* @return string
*/
public static function generate_code(): string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the return type being removed?

private static function generate_code_upper_lower_digits(): string {
return random_string(10);
// Upper/lower/digits random string
private static function generate_code_upper_lower_digits() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify a return type.

random_int(0, 9999)
);
// Digits with hyphens
private static function generate_code_digits_with_hyphens() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, return type missing.

}

// Drop the unique index on 'code' and add a non-unique one.
if ($oldversion < 2024042210) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we touching this upgrade step?

}

$qrcodeurl = new \moodle_url('/mod/customcert/verify_certificate.php', $urlparams);
$qrcodeurl = new \moodle_url('https://app.pacificmedicaltraining.com/verify', $urlparams);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have hardcoded a URL here.

$urlparams = [
'code' => $code,
'qrcode' => 1,
'certId' => $code,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing the name of the variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fulldecent our verification URL is like "https://app.pacificmedicaltraining.com/verify?certId=1069-9865-6509", can we replace certId to code, then we won't require this change in the $urlprams.

'customcert/codegenerationmethod',
get_string('codegenerationmethod', 'customcert'),
get_string('codegenerationmethod_desc', 'customcert'),
0, // Default option (0 = Upper/lower/digits random string method).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the '.'?

settings.php Outdated
[
0 => get_string('codegenerationmethod_upperlowerdigits', 'customcert'), // Upper/lower/digits random string.
1 => get_string('codegenerationmethod_digitshyphens', 'customcert'), // Digits with hyphens numeric code.
0 => get_string('Upper/lower/digits', 'customcert'), // Upper/lower/digits random string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need different language strings?

@mdjnelson
Copy link
Owner

Apologies. Initially I said I would address these but there are a few uncertainties about this patch. I would like this cleaned up so I can see a clear solution (right now it's touching code all over the place and im not sure why) at what is trying to be achieved. Please let me know when you are ready for me to re-review. Thanks.

@Raza403 Raza403 changed the title Updated view_user_cert to support all certificates. [WIP] Updated view_user_cert to support all certificates. Jul 8, 2025
@Raza403
Copy link
Contributor Author

Raza403 commented Jul 8, 2025

Hi @mdjnelson, my apologies for bothering you, I have updated the title of this PR to [WIP], We'll let you know and will invite you to review this PR once this is ready.

db/install.xml Outdated
<INDEXES>
<INDEX NAME="userid-customcertid" UNIQUE="false" FIELDS="userid, customcertid"/>
<INDEX NAME="code" UNIQUE="false" FIELDS="code"/>
<INDEX NAME="code" UNIQUE="true" FIELDS="code"/>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be false due to backup/restore issues (which are always a pain cause I almost always forget abt it).

db/upgrade.php Outdated
upgrade_plugin_savepoint(true, 2024042210, 'mod', 'customcert');
}

if ($oldversion < 2025041401) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This upgrade step was part of another PR, so can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed at 8876d20

fulldecent added a commit to modern-training-solutions/moodle-mod_customcert that referenced this pull request Jul 14, 2025
Raza403 and others added 9 commits July 27, 2025 21:10
… being generated. This PR is made for #1 and #2

custom cert, updated the names of both code generation methods.

Added view_user_cert page, a logged in user can see certificate if having a valid token and ecard code. This is being used from view_tokens.php page.

customcert view certificate function is made. This is made for mdjnelson#212

Updated verify certificate URL. This PR is made for 221

Updated the verify URL for QR code.
@Raza403 Raza403 force-pushed the features/custom-cert-view-all-certificates#231 branch from fd94167 to 9b6ab3a Compare July 27, 2025 16:11
@Raza403
Copy link
Contributor Author

Raza403 commented Aug 5, 2025

@fulldecent @mdjnelson this is passing all checks and is working. This PR is good to review.

@Raza403 Raza403 changed the title [WIP] Updated view_user_cert to support all certificates. Updated view_user_cert to support all certificates. Aug 6, 2025
require_once($CFG->dirroot . '/mod/customcert/lib.php');

// Allows guest access to course with ID 1.
require_login(1, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line cause this script to only work if the site is configured with "allow guest login" ?

Are there any other situations where this line of code would cause the certificate to not be displayed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coding the front-page course (ID 1) forces everyone to “log in to course 1 (guest if allowed)” even though this page isn’t about course 1 at all. It can bypass the actual course/module access rules for the certificate and behave unpredictably if guest access to the front page is disabled. Please look at https://github.com/mdjnelson/moodle-mod_customcert/blob/MOODLE_500_STABLE/verify_certificate.php to see how we handle non-users to access the site.

@indicozy
Copy link

I've tried this PR, everything works great. However, is it possible to add option to not require token if option All can verify certificate is set true?

@fulldecent
Copy link
Contributor

Well every certificate has a URL. And the URL has a token. That's the official public URL of the certificate. I don't think it makes sense to have a separate, additional public URL for certificates if a specific setting is turned on.

@mdjnelson
Copy link
Owner

Looking now. Sorry for delay.

} while ($DB->record_exists('customcert_issues', ['code' => $code]));
return $code;
// If the upper/lower/digits is selected (0), use the upper/lower/digits code generation method.
if ($method == 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of using a hard-coded variable like 0. It doesn't mean much to the developer using it.

upgrade_plugin_savepoint(true, 2024042210, 'mod', 'customcert');
}

if ($oldversion < 2024042213) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an upgrade clause to remove these settings from the Moodle DB if it exists, probably use something like unset_config() iirc. Please add another upgrade step to do this and just leave this as is.

*
* @return string
*/
private static function generate_code_digits_with_hyphens(): string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me why we are editing this function?

* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @param string $message The error message to display.
*/
function display_error_page($message) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this and any usages of it, we can rely on Moodle's own error handling. This makes it forward compatible if this ever changes.

require_once($CFG->dirroot . '/mod/customcert/lib.php');

// Allows guest access to course with ID 1.
require_login(1, false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coding the front-page course (ID 1) forces everyone to “log in to course 1 (guest if allowed)” even though this page isn’t about course 1 at all. It can bypass the actual course/module access rules for the certificate and behave unpredictably if guest access to the front page is disabled. Please look at https://github.com/mdjnelson/moodle-mod_customcert/blob/MOODLE_500_STABLE/verify_certificate.php to see how we handle non-users to access the site.

// The template defines the layout and content of the generated certificate.
$template = $DB->get_record('customcert_templates', ['id' => $certificate->templateid]);
if (!$template) {
display_error_page('The certificate template could not be found. Please contact the site administrator for assistance.');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed. If you perform a get_record call and pass MUST_EXIST then an exception is thrown and we want that.

$template->generate_pdf(false, $issue->userid);
} catch (Exception $e) {
// Catch any errors that may occur while generating the certificate PDF.
display_error_page('Error generating certificate PDF. '
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this catch please.

* @param string $certcode The unique code of the certificate.
* @return string The generated public URL for the certificate.
*/
function generate_public_url_for_certificate(string $certcode): string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used anywhere.

* @param string $certcode The unique certificate code.
* @return string The generated HMAC signature.
*/
function calculate_signature(string $certcode): string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lib.php file is for functions that Moodle will call itself. We don't need to put it there. I would suggest creating a new class in the classes folder for this functionality.


if (($user->id != $USER->id)
&& !has_capability('mod/customcert:viewallcertificates', context_system::instance())) {
if (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks worse than before. Was this change necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants