Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-70741

Capability report gives a fatal error if there are orphan contexts

    XMLWordPrintable

    Details

      Description

      It seems that, in our database, we have some course_modules, and contexts, which exist, but the corresponding course does not exist any more.

      In this case, context.path is null, and this causes the DB queries in tool_capability_calculate_role_data to throw an exception like:

      Debug info: ERROR: invalid input syntax for integer: ""
       
      SELECT ctx.*
      FROM mdl_context ctx
      LEFT JOIN mdl_user u ON ctx.contextlevel = 30 AND u.id = ctx.instanceid
      LEFT JOIN mdl_course_categories cat ON ctx.contextlevel = 40 AND cat.id = ctx.instanceid
      LEFT JOIN mdl_course c ON ctx.contextlevel = 50 AND c.id = ctx.instanceid
      LEFT JOIN mdl_course_modules cm ON ctx.contextlevel = 70 AND cm.id = ctx.instanceid
      LEFT JOIN mdl_block_instances bi ON ctx.contextlevel = 80 AND bi.id = ctx.instanceid
      WHERE ctx.id IN ($1,$2)
      ORDER BY ctx.contextlevel, bi.defaultregion, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.defaultweight), u.lastname, u.firstname, cm.id
       
      [array (
      0 => 1,
      2 => '',
      )]
      Error code: dmlreadexception
      ×Stack trace:
      line 486 of /lib/dml/moodle_database.php: dml_read_exception thrown
      line 329 of /lib/dml/pgsql_native_moodle_database.php: call to moodle_database->query_end()
      line 932 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->query_end()
      line 7694 of /lib/accesslib.php: call to pgsql_native_moodle_database->get_records_sql()
      line 66 of /admin/tool/capability/locallib.php: call to get_sorted_contexts()
      line 100 of /admin/tool/capability/renderer.php: call to tool_capability_calculate_role_data()
      line 111 of /admin/tool/capability/index.php: call to tool_capability_renderer->capability_comparison_table()
      

      Of course, there should not be orphan contexts, but given that it can happen, I think we need to change this report to be robust to the problem.

        Attachments

          Activity

            People

            Assignee:
            timhunt Tim Hunt
            Reporter:
            timhunt Tim Hunt
            Peer reviewer:
            Luca Bösch
            Integrator:
            Sara Arjona (@sarjona)
            Tester:
            CiBoT
            Participants:
            Component watchers:
            Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias, Sujith Haridasan, Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Fix Release Date:
              8/Mar/21

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 hour, 16 minutes
                1h 16m