MDL-41436 Allow user=-1 in get_fast_modinfo()

Remove ref to nonexisting course.modinfo in MNET auth, use get_fast_modinfo() for user=-1 instead
This commit is contained in:
Marina Glancy 2013-08-01 14:12:05 +10:00
parent 1cf1d6e62a
commit 4cc819adf0
2 changed files with 50 additions and 47 deletions

View File

@ -765,13 +765,6 @@ class auth_plugin_mnet extends auth_plugin_base {
break; break;
} }
$mnethostlogssql = " $mnethostlogssql = "
SELECT
mhostlogs.remoteid, mhostlogs.time, mhostlogs.userid, mhostlogs.ip,
mhostlogs.course, mhostlogs.module, mhostlogs.cmid, mhostlogs.action,
mhostlogs.url, mhostlogs.info, mhostlogs.username, c.fullname as coursename,
c.modinfo
FROM
(
SELECT SELECT
l.id as remoteid, l.time, l.userid, l.ip, l.course, l.module, l.cmid, l.id as remoteid, l.time, l.userid, l.ip, l.course, l.module, l.cmid,
l.action, l.url, l.info, u.username l.action, l.url, l.info, u.username
@ -781,11 +774,9 @@ class auth_plugin_mnet extends auth_plugin_base {
WHERE WHERE
u.mnethostid = ? u.mnethostid = ?
AND l.id > ? AND l.id > ?
ORDER BY remoteid ASC AND l.course IS NOT NULL
LIMIT 500 ORDER by l.id ASC
) mhostlogs LIMIT 500";
INNER JOIN {course} c on c.id = mhostlogs.course
ORDER by mhostlogs.remoteid ASC";
$mnethostlogs = $DB->get_records_sql($mnethostlogssql, array($mnethostid, $mnet_request->response['last log id'])); $mnethostlogs = $DB->get_records_sql($mnethostlogssql, array($mnethostid, $mnet_request->response['last log id']));
@ -796,19 +787,19 @@ class auth_plugin_mnet extends auth_plugin_base {
$processedlogs = array(); $processedlogs = array();
foreach($mnethostlogs as $hostlog) { foreach($mnethostlogs as $hostlog) {
// Extract the name of the relevant module instance from the try {
// course modinfo if possible. // Get impersonalised course information. If it is cached there will be no DB queries.
if (!empty($hostlog->modinfo) && !empty($hostlog->cmid)) { $modinfo = get_fast_modinfo($hostlog->course, -1);
$modinfo = unserialize($hostlog->modinfo); $hostlog->coursename = $modinfo->get_course()->fullname;
unset($hostlog->modinfo); if (!empty($hostlog->cmid) && isset($modinfo->cms[$hostlog->cmid])) {
$modulearray = array(); $hostlog->resource_name = $modinfo->cms[$hostlog->cmid]->name;
foreach($modinfo as $module) {
$modulearray[$module->cm] = $module->name;
}
$hostlog->resource_name = $modulearray[$hostlog->cmid];
} else { } else {
$hostlog->resource_name = ''; $hostlog->resource_name = '';
} }
} catch (moodle_exception $e) {
// Course not found
continue;
}
$processedlogs[] = array ( $processedlogs[] = array (
'remoteid' => $hostlog->remoteid, 'remoteid' => $hostlog->remoteid,

View File

@ -1723,11 +1723,11 @@ class cm_info implements IteratorAggregate {
*/ */
private function obtain_dynamic_data() { private function obtain_dynamic_data() {
global $CFG; global $CFG;
if ($this->state >= self::STATE_BUILDING_DYNAMIC) { $userid = $this->modinfo->get_user_id();
if ($this->state >= self::STATE_BUILDING_DYNAMIC || $userid == -1) {
return; return;
} }
$this->state = self::STATE_BUILDING_DYNAMIC; $this->state = self::STATE_BUILDING_DYNAMIC;
$userid = $this->modinfo->get_user_id();
if (!empty($CFG->enableavailability)) { if (!empty($CFG->enableavailability)) {
require_once($CFG->libdir. '/conditionlib.php'); require_once($CFG->libdir. '/conditionlib.php');
@ -1805,6 +1805,9 @@ class cm_info implements IteratorAggregate {
*/ */
private function update_user_visible() { private function update_user_visible() {
$userid = $this->modinfo->get_user_id(); $userid = $this->modinfo->get_user_id();
if ($userid == -1) {
return null;
}
$this->uservisible = true; $this->uservisible = true;
// If the user cannot access the activity set the uservisible flag to false. // If the user cannot access the activity set the uservisible flag to false.
@ -1835,6 +1838,9 @@ class cm_info implements IteratorAggregate {
if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)) { if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)) {
$userid = $this->modinfo->get_user_id(); $userid = $this->modinfo->get_user_id();
if ($userid == -1) {
return null;
}
if (!has_capability('moodle/site:accessallgroups', $this->get_context(), $userid)) { if (!has_capability('moodle/site:accessallgroups', $this->get_context(), $userid)) {
// If the activity has 'group members only' and you don't have accessallgroups... // If the activity has 'group members only' and you don't have accessallgroups...
$groups = $this->modinfo->get_groups($this->groupingid); $groups = $this->modinfo->get_groups($this->groupingid);
@ -1853,6 +1859,10 @@ class cm_info implements IteratorAggregate {
* @return bool True if the user access is restricted. * @return bool True if the user access is restricted.
*/ */
public function is_user_access_restricted_by_capability() { public function is_user_access_restricted_by_capability() {
$userid = $this->modinfo->get_user_id();
if ($userid == -1) {
return null;
}
$capability = 'mod/' . $this->modname . ':view'; $capability = 'mod/' . $this->modname . ':view';
$capabilityinfo = get_capability_info($capability); $capabilityinfo = get_capability_info($capability);
if (!$capabilityinfo) { if (!$capabilityinfo) {
@ -1861,7 +1871,6 @@ class cm_info implements IteratorAggregate {
} }
// You are blocked if you don't have the capability. // You are blocked if you don't have the capability.
$userid = $this->modinfo->get_user_id();
return !has_capability($capability, $this->get_context(), $userid); return !has_capability($capability, $this->get_context(), $userid);
} }
@ -1877,7 +1886,10 @@ class cm_info implements IteratorAggregate {
return false; return false;
} }
require_once($CFG->libdir. '/conditionlib.php'); $userid = $this->modinfo->get_user_id();
if ($userid == -1) {
return null;
}
// If module will always be visible anyway (but greyed out), don't bother checking anything else // If module will always be visible anyway (but greyed out), don't bother checking anything else
if ($this->get_show_availability() == CONDITION_STUDENTVIEW_SHOW) { if ($this->get_show_availability() == CONDITION_STUDENTVIEW_SHOW) {
@ -1885,7 +1897,6 @@ class cm_info implements IteratorAggregate {
} }
// Can the user see hidden modules? // Can the user see hidden modules?
$userid = $this->modinfo->get_user_id();
if (has_capability('moodle/course:viewhiddenactivities', $this->get_context(), $userid)) { if (has_capability('moodle/course:viewhiddenactivities', $this->get_context(), $userid)) {
return false; return false;
} }
@ -1936,7 +1947,7 @@ class cm_info implements IteratorAggregate {
* @return void * @return void
*/ */
private function obtain_view_data() { private function obtain_view_data() {
if ($this->state >= self::STATE_BUILDING_VIEW) { if ($this->state >= self::STATE_BUILDING_VIEW || $this->modinfo->get_user_id() == -1) {
return; return;
} }
$this->obtain_dynamic_data(); $this->obtain_dynamic_data();
@ -1963,8 +1974,8 @@ class cm_info implements IteratorAggregate {
* and recommended to have field 'cacherev') or just a course id. Just course id * and recommended to have field 'cacherev') or just a course id. Just course id
* is enough when calling get_fast_modinfo() for current course or site or when * is enough when calling get_fast_modinfo() for current course or site or when
* calling for any other course for the second time. * calling for any other course for the second time.
* @param int $userid User id to populate 'uservisible' attributes of modules and sections. * @param int $userid User id to populate 'availble' and 'uservisible' attributes of modules and sections.
* Set to 0 for current user (default) * Set to 0 for current user (default). Set to -1 to avoid calculation of dynamic user-depended data.
* @param bool $resetonly whether we want to get modinfo or just reset the cache * @param bool $resetonly whether we want to get modinfo or just reset the cache
* @return course_modinfo|null Module information for course, or null if resetting * @return course_modinfo|null Module information for course, or null if resetting
* @throws moodle_exception when course is not found (nothing is thrown if resetting) * @throws moodle_exception when course is not found (nothing is thrown if resetting)
@ -2457,8 +2468,9 @@ class section_info implements IteratorAggregate {
*/ */
private function get_available() { private function get_available() {
global $CFG; global $CFG;
if ($this->_available !== null) { $userid = $this->modinfo->get_user_id();
// Has already been calculated. if ($this->_available !== null || $userid == -1) {
// Has already been calculated or does not need calculation.
return $this->_available; return $this->_available;
} }
if (!empty($CFG->enableavailability)) { if (!empty($CFG->enableavailability)) {
@ -2466,7 +2478,7 @@ class section_info implements IteratorAggregate {
// Get availability information // Get availability information
$ci = new condition_info_section($this); $ci = new condition_info_section($this);
$this->_available = $ci->is_available($this->_availableinfo, true, $this->_available = $ci->is_available($this->_availableinfo, true,
$this->modinfo->get_user_id(), $this->modinfo); $userid, $this->modinfo);
if ($this->_availableinfo === '' && $this->_groupingid) { if ($this->_availableinfo === '' && $this->_groupingid) {
// Still may have some extra text in availableinfo because of groupping. // Still may have some extra text in availableinfo because of groupping.
// Set as undefined so the next call to get_availabeinfo() calculates it. // Set as undefined so the next call to get_availabeinfo() calculates it.
@ -2487,8 +2499,9 @@ class section_info implements IteratorAggregate {
private function get_availableinfo() { private function get_availableinfo() {
// Make sure $this->_available has been calculated, it may also fill the _availableinfo property. // Make sure $this->_available has been calculated, it may also fill the _availableinfo property.
$this->get_available(); $this->get_available();
if ($this->_availableinfo !== null) { $userid = $this->modinfo->get_user_id();
// It has been already calculated. if ($this->_availableinfo !== null || $userid == -1) {
// It has been already calculated or does not need calculation.
return $this->_availableinfo; return $this->_availableinfo;
} }
$this->_availableinfo = ''; $this->_availableinfo = '';
@ -2497,7 +2510,6 @@ class section_info implements IteratorAggregate {
// for people with managegroups - same logic/class as grouping label // for people with managegroups - same logic/class as grouping label
// on individual activities. // on individual activities.
$context = context_course::instance($this->get_course()); $context = context_course::instance($this->get_course());
$userid = $this->modinfo->get_user_id();
if ($this->_groupingid && has_capability('moodle/course:managegroups', $context, $userid)) { if ($this->_groupingid && has_capability('moodle/course:managegroups', $context, $userid)) {
$groupings = groups_get_all_groupings($this->get_course()); $groupings = groups_get_all_groupings($this->get_course());
$this->_availableinfo = html_writer::tag('span', '(' . format_string( $this->_availableinfo = html_writer::tag('span', '(' . format_string(
@ -2537,14 +2549,14 @@ class section_info implements IteratorAggregate {
* @return bool * @return bool
*/ */
private function get_uservisible() { private function get_uservisible() {
if ($this->_uservisible !== null) { $userid = $this->modinfo->get_user_id();
// Has already been calculated. if ($this->_uservisible !== null || $userid == -1) {
// Has already been calculated or does not need calculation.
return $this->_uservisible; return $this->_uservisible;
} }
$this->_uservisible = true; $this->_uservisible = true;
if (!$this->_visible || !$this->get_available()) { if (!$this->_visible || !$this->get_available()) {
$coursecontext = context_course::instance($this->get_course()); $coursecontext = context_course::instance($this->get_course());
$userid = $this->modinfo->get_user_id();
if (!has_capability('moodle/course:viewhiddensections', $coursecontext, $userid)) { if (!has_capability('moodle/course:viewhiddensections', $coursecontext, $userid)) {
$this->_uservisible = false; $this->_uservisible = false;
} }