-
Improvement
-
Resolution: Won't Do
-
Major
-
None
-
2.6.2
This issue has three main goals
Goal 1: Performance for course modules.
At some point function require_login() started calling get_fast_modinfo() and taking the course module from there ignoring the comprehensive $cm object passed as argument. Therefore we call very expensive function get_coursemodule_from_id() on EACH module page for nothing. Removing the call to this function gives us 2-3 queries less per page.
Goal 2: Code readability.
Before calling require_login() we usually retrieve lots of records from DB and repeat very similar code on each page.
old code:
$cmrecord = get_coursemodule_from_id('forum', $cmid, 0, true, MUST_EXIST);
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
$forum = $DB->get_record("forum", array("id" => $cm->instance), '*', MUST_EXIST);
|
require_course_login($course, true, $cmrecord);
|
$context = context_module::instance($cmrecord->id);
|
$cminfo = $PAGE->cm; // or: get_fast_modinfo($course)->get_cm($id);
|
new code:
list($context, $course, $cminfo) = $PAGE->login_to_cm('forum', $cmid);
|
$forum = $PAGE->activityrecord;
|
$cmrecord = $cminfo->get_course_module_record(true);
|
another useful function:
list($context, $course, $cminfo) = $PAGE->login_to_activity('forum', $forumid);
|
$cminfo (cm_info) can almost always be used instead of $cmrecord (stdClass). It has more fields, saves additional queries in group functions. But cm_info properties are read-only.
Another example of better readability is substituting arguments with named constants:
OLD:
require_login($course, false);
|
NEW:
$PAGE->login($course, PAGELOGIN_NO_AUTOLOGIN);
|
OLD:
require_course_login($course, true, $cm);
|
NEW:
$PAGE->login_to_cm(null, $cm, $course, PAGELOGIN_ALLOW_FRONTPAGE_GUEST);
|
Goal 3. Privacy.
All exceptions raised during login will be masked with require_login_exception. Therefore modules will not disclose information about particular error (missing course module, missing course, etc.). If page needs to perform additional queries before calling login it can use function login_expected():
$PAGE->login_expected(PAGELOGIN_NO_AUTOLOGIN);
|
$entry = $DB->get_record('glossary_entries', array('id'=> $eid), '*', MUST_EXIST);
|
list($context, $course, $cm) = $PAGE->login_to_activity('glossary', $entry->glossaryid);
|
- has been marked as being related by
-
MDL-53772 Fix $PAGE->context issues with validate_context()
- Closed
-
MDL-48448 Epic: Stop repeating db queries
- Closed
-
MDL-45866 The message telling guests they cannot attempt a quiz could be more friendly
- Open
-
MDL-46706 Library: API to get cm_info more easily
- Closed
- is blocked by
-
MDL-44720 Make it easier to use cm_info instead of get_coursemodule_from_id
- Closed
- will help resolve
-
MDL-46101 Do not call require_login() from external_api::validate_context()
- Closed