Affects Version/s: 3.10
Fix Version/s: None
Component/s: Set: Course certificate
Plugin Version:3.9.2 (2020100700)
We've been hit hard by a problem at the learn.moodle.org staging site where after creating a new Course certificate module instance, the view.php script died with all the available memory exhausted.
I was able to trace down the problem to mod_coursecertificate\output\view_page::export_for_template() which has the line:
that is expected to render a line like "Currently, X students meet this activity's access restrictions and will be issued with their certificate once they access it." with "X" being replaced with the number of users.
There are several major issues with this:
- The get_users_to_issue() method calls get_enrolled_users() which in turn calls get_records_sql() - that is, it loads all the records into the PHP memory. In our case, the query returns 43.000 records.
- That call uses u.* to obtain all user record fields include the ones it never needs such as rich text profile description fields.
- All these records and all their fields are loaded into the memory just to display their count.
These deserves some optimisation, for example:
- Use get_enrolled_sql() to construct more effective SQL.
- Load only those user fields that are actually needed (using asterisk wildcard * is almost never a good idea).
- Have a dedicated method to return the number of these candidate users for this case where we do not actually need to load them.