mdjnelson / moodle-mod_customcert

Enables the creation of dynamically generated certificates with complete customisation via the web browser.
https://moodle.org/plugins/mod_customcert
GNU General Public License v3.0
93 stars 159 forks source link

Exception - Argument 2 passed to has_capability() must be an instance of context in single activity course #544

Closed berthelemy closed 1 year ago

berthelemy commented 1 year ago

I want to use the Custom Cert module to allow people to claim a certificate after attending face-to-face training, using an "enrolment key" provided by the instructor.

I have created the certificate within a course, using the Topics format. I am system administrator.

Now the certificate is ready, I switched the course format to: Single Activity

I can still view and edit the certificate. However, when I go to change the course settings, I get this error:

Exception - Argument 2 passed to has_capability() must be an instance of context, null given, called in [dirroot]/mod/customcert/lib.php on line 342

Using Moodle 4.1

berthelemy commented 1 year ago

The same error appears when I try to go to Participants and Grades.

leonstr commented 1 year ago

Try changing the following 4 lines in mod/customcert/lib.php:

342     if (has_capability('mod/customcert:manage', $PAGE->cm->context)) {
    ⋮
344         $templateid = $DB->get_field('customcert', 'templateid', array('id' => $PAGE->cm->instance));
    ⋮
352     if (has_capability('mod/customcert:verifycertificate', $PAGE->cm->context)) {
    ⋮
354             new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $PAGE->cm->context->id)),

to:

342     if (has_capability('mod/customcert:manage', $settings->get_page()->cm->context)) {
    ⋮
344         $templateid = $DB->get_field('customcert', 'templateid', array('id' => $settings->get_page()->cm->instance));
    ⋮
352     if (has_capability('mod/customcert:verifycertificate', $settings->get_page()->cm->context)) {
    ⋮
354             new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $settings->get_page()->cm->context->id)),
mdjnelson commented 1 year ago

Fixed. Thanks to all involved.