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

Accesslib: get_user_capability_course looks slow

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide

      Note: According to my IDE I think this function is currently only used from tool_monitor, although I do want to use it in another place later! Anyhow that's why this testing is in tool_monitor.

      1 As an administrator, go to Site administration / Reports / Event monitoring rules.

      2 Enable event monitoring if necessary (you may wish to disable it after you finish testing).

      3 Follow the link to the 'Event monitoring' page.

      4 Look at the 'Select a course' dropdown.

      EXPECTED: You should see a list of all courses on the server including the site. (Unchanged from previous behaviour.)

      5 Create a test account, or use an existing one.

      6 Assign the test account the 'Manager' role in 2 courses on your site.

      7 Log in with the test account and go to the 'Event monitoring' page by manually pasting in the URL admin/tool/monitor/index.php?courseid=0, and look at the course dropdown.

      EXPECTED: You should see the 2 courses where you have the manager role.

      Note: The unit test contains a much more comprehensive test of the function than just having a role in the course! But this is just a real-world check it isn't completely borked.

      Show
      Note: According to my IDE I think this function is currently only used from tool_monitor, although I do want to use it in another place later! Anyhow that's why this testing is in tool_monitor. 1 As an administrator, go to Site administration / Reports / Event monitoring rules. 2 Enable event monitoring if necessary (you may wish to disable it after you finish testing). 3 Follow the link to the 'Event monitoring' page. 4 Look at the 'Select a course' dropdown. EXPECTED: You should see a list of all courses on the server including the site. (Unchanged from previous behaviour.) 5 Create a test account, or use an existing one. 6 Assign the test account the 'Manager' role in 2 courses on your site. 7 Log in with the test account and go to the 'Event monitoring' page by manually pasting in the URL admin/tool/monitor/index.php?courseid=0, and look at the course dropdown. EXPECTED: You should see the 2 courses where you have the manager role. Note: The unit test contains a much more comprehensive test of the function than just having a role in the course! But this is just a real-world check it isn't completely borked.
    • Affected Branches:
      MOODLE_34_STABLE
    • Fixed Branches:
      MOODLE_34_STABLE
    • Pull Master Branch:
      MDL-59897-master

      Description

      The function get_user_capability_course in accesslib appears to be potentially slow, in that it loads all courses on the system and checks access against each one. For example, say there are 1,000 courses, then if you are checking whether a user has moodle/course:view on a course, the most common situation might be that they do not have it on any course, in which case it seems like a waste to query for 1,000 courses.

      Access checks are done with has_capability which is fast so it's not going to be too terrible, but even so.

      I think it should be possible to work out a cleverer query for a subset of courses, based on the existing cached access data. I'm going to take a look at this.

      The reason for looking at this function is that I've used it to implement an enhancement MDL-53169 which may then be used for global search, so it would be good to ensure performance is not affected. (get_user_capability_course currently only seems to be used in a couple of admin tools so it isn't really performance-critical at the moment.)

      Alongside this potential change, I notice there are no unit tests for this function, so I'll make one.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  13/Nov/17