MDL-64878 grades: Rebuild cache on import

When the course module cache is out of date during a gradebook re-calculcation it throws
throws exceptions because the module cannot be found. This prevents access to gradebook or
any type of grading functions until the cache is rebuilt.

When the cache still has no module record we log an error and return the course context.
This commit is contained in:
Damyon Wiese 2019-02-21 16:21:58 +08:00
parent 124999563a
commit 031cf251c1
2 changed files with 19 additions and 2 deletions

View File

@ -485,6 +485,9 @@ class restore_gradebook_structure_step extends restore_structure_step {
// Freeze gradebook calculations if needed.
$this->gradebook_calculation_freeze();
// Ensure the module cache is current when recalculating grades.
rebuild_course_cache($this->get_courseid(), true);
// Restore marks items as needing update. Update everything now.
grade_regrade_final_grades($this->get_courseid());
}

View File

@ -2495,8 +2495,22 @@ class grade_item extends grade_object {
*/
public function get_context() {
if ($this->itemtype == 'mod') {
$cm = get_fast_modinfo($this->courseid)->instances[$this->itemmodule][$this->iteminstance];
$context = \context_module::instance($cm->id);
$modinfo = get_fast_modinfo($this->courseid);
// Sometimes the course module cache is out of date and needs to be rebuilt.
if (!isset($modinfo->instances[$this->itemmodule][$this->iteminstance])) {
rebuild_course_cache($this->courseid, true);
$modinfo = get_fast_modinfo($this->courseid);
}
// Even with a rebuilt cache the module does not exist. This means the
// database is in an invalid state - we will log an error and return
// the course context but the calling code should be updated.
if (!isset($modinfo->instances[$this->itemmodule][$this->iteminstance])) {
mtrace(get_string('moduleinstancedoesnotexist', 'error'));
$context = \context_course::instance($this->courseid);
} else {
$cm = $modinfo->instances[$this->itemmodule][$this->iteminstance];
$context = \context_module::instance($cm->id);
}
} else {
$context = \context_course::instance($this->courseid);
}