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
89 stars 157 forks source link

Error on deleting page: context (or contextid) is a required event property... #571

Closed leonstr closed 6 months ago

leonstr commented 1 year ago

Steps to reproduce issue:

  1. Create a new certificate template (system-level or course-level).
  2. Add a second page.
  3. Add an element to the second page.
  4. Delete the second page.

Expected results:

Second page is deleted.

Actual results:

Coding error detected, it must be fixed by a programmer: context (or contextid) is a required event property, system context may be hardcoded in init() method.

More information about this error
Debug info:
Error code: codingerror
Stack trace:

    line 224 of /lib/classes/event/base.php: coding_exception thrown
    line 87 of /mod/customcert/classes/event/element_deleted.php: call to core\event\base::create()
    line 463 of /mod/customcert/classes/element.php: call to mod_customcert\event\element_deleted::create_from_element()
    line 228 of /mod/customcert/classes/template.php: call to mod_customcert\element->delete()
    line 106 of /mod/customcert/edit.php: call to mod_customcert\template->delete_page()

Output buffer: <br /> <b>Warning</b>: Attempt to read property "templateid" on bool in <b>/var/www/moodle/mod/customcert/classes/event/element_deleted.php</b> on line <b>80</b><br /> <br /> <b>Warning</b>: Attempt to read property "contextid" on bool in <b>/var/www/m401moodle/mod/customcert/classes/event/element_deleted.php</b> on line <b>83</b><br />

Problem seen with Moodle 4.1.2+ (Build: 20230406) with mod_customcert 2022112801 (7ee063e) and Moodle 4.2.1+ (Build: 20230714) and mod_customcert 2023042402 (9a11c53).

leonstr commented 1 year ago

It looks like the cause is that element_deleted->create_from_element() uses $DB->get_record('customcert_pages', ...) using the obtained $page->templateid to get the template record and so obtain the contextid to use for this event.

But template->delete_page() calls $DB->delete_records('customcert_pages', ...) before calling element_deleted->create_from_element() so the required mdl_customcert_pages row no longer exists.

PR #572 might do the trick.

leonstr commented 12 months ago

Updated PR to include PHPUnit test that shows this error with the previous version but passes with the proposed fix.