MDL-28463 conditionlib: use MUC for caching grades, reset when grades are changed
This commit is contained in:
+57
-78
@@ -1151,7 +1151,6 @@ abstract class condition_info_base {
|
||||
*
|
||||
* @global stdClass $USER
|
||||
* @global moodle_database $DB
|
||||
* @global stdClass $SESSION
|
||||
* @param int $gradeitemid Grade item ID we're interested in
|
||||
* @param bool $grabthelot If true, grabs all scores for current user on
|
||||
* this course, so that later ones come from cache
|
||||
@@ -1161,71 +1160,59 @@ abstract class condition_info_base {
|
||||
* or 37.21), or false if user does not have a grade yet
|
||||
*/
|
||||
private function get_cached_grade_score($gradeitemid, $grabthelot=false, $userid=0) {
|
||||
global $USER, $DB, $SESSION;
|
||||
if ($userid==0 || $userid==$USER->id) {
|
||||
// For current user, go via cache in session
|
||||
if (empty($SESSION->gradescorecache) || $SESSION->gradescorecacheuserid!=$USER->id) {
|
||||
$SESSION->gradescorecache = array();
|
||||
$SESSION->gradescorecacheuserid = $USER->id;
|
||||
}
|
||||
if (!array_key_exists($gradeitemid, $SESSION->gradescorecache)) {
|
||||
if ($grabthelot) {
|
||||
// Get all grades for the current course
|
||||
$rs = $DB->get_recordset_sql('
|
||||
SELECT
|
||||
gi.id,gg.finalgrade,gg.rawgrademin,gg.rawgrademax
|
||||
FROM
|
||||
{grade_items} gi
|
||||
LEFT JOIN {grade_grades} gg ON gi.id=gg.itemid AND gg.userid=?
|
||||
WHERE
|
||||
gi.courseid = ?', array($USER->id, $this->item->course));
|
||||
foreach ($rs as $record) {
|
||||
$SESSION->gradescorecache[$record->id] =
|
||||
is_null($record->finalgrade)
|
||||
// No grade = false
|
||||
? false
|
||||
// Otherwise convert grade to percentage
|
||||
: (($record->finalgrade - $record->rawgrademin) * 100) /
|
||||
($record->rawgrademax - $record->rawgrademin);
|
||||
|
||||
}
|
||||
$rs->close();
|
||||
// And if it's still not set, well it doesn't exist (eg
|
||||
// maybe the user set it as a condition, then deleted the
|
||||
// grade item) so we call it false
|
||||
if (!array_key_exists($gradeitemid, $SESSION->gradescorecache)) {
|
||||
$SESSION->gradescorecache[$gradeitemid] = false;
|
||||
}
|
||||
} else {
|
||||
// Just get current grade
|
||||
$record = $DB->get_record('grade_grades', array(
|
||||
'userid'=>$USER->id, 'itemid'=>$gradeitemid));
|
||||
if ($record && !is_null($record->finalgrade)) {
|
||||
$score = (($record->finalgrade - $record->rawgrademin) * 100) /
|
||||
($record->rawgrademax - $record->rawgrademin);
|
||||
} else {
|
||||
// Treat the case where row exists but is null, same as
|
||||
// case where row doesn't exist
|
||||
$score = false;
|
||||
}
|
||||
$SESSION->gradescorecache[$gradeitemid]=$score;
|
||||
}
|
||||
}
|
||||
return $SESSION->gradescorecache[$gradeitemid];
|
||||
} else {
|
||||
// Not the current user, so request the score individually
|
||||
$record = $DB->get_record('grade_grades', array(
|
||||
'userid'=>$userid, 'itemid'=>$gradeitemid));
|
||||
if ($record && !is_null($record->finalgrade)) {
|
||||
$score = (($record->finalgrade - $record->rawgrademin) * 100) /
|
||||
($record->rawgrademax - $record->rawgrademin);
|
||||
} else {
|
||||
// Treat the case where row exists but is null, same as
|
||||
// case where row doesn't exist
|
||||
$score = false;
|
||||
}
|
||||
return $score;
|
||||
global $USER, $DB;
|
||||
if (!$userid) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
$cache = cache::make('core', 'gradecondition');
|
||||
if (($cachedgrades = $cache->get($userid)) === false) {
|
||||
$cachedgrades = array();
|
||||
}
|
||||
if (!array_key_exists($gradeitemid, $cachedgrades)) {
|
||||
if ($grabthelot) {
|
||||
// Get all grades for the current course
|
||||
$rs = $DB->get_recordset_sql('
|
||||
SELECT
|
||||
gi.id,gg.finalgrade,gg.rawgrademin,gg.rawgrademax
|
||||
FROM
|
||||
{grade_items} gi
|
||||
LEFT JOIN {grade_grades} gg ON gi.id=gg.itemid AND gg.userid=?
|
||||
WHERE
|
||||
gi.courseid = ?', array($userid, $this->item->course));
|
||||
foreach ($rs as $record) {
|
||||
$cachedgrades[$record->id] =
|
||||
is_null($record->finalgrade)
|
||||
// No grade = false
|
||||
? false
|
||||
// Otherwise convert grade to percentage
|
||||
: (($record->finalgrade - $record->rawgrademin) * 100) /
|
||||
($record->rawgrademax - $record->rawgrademin);
|
||||
|
||||
}
|
||||
$rs->close();
|
||||
// And if it's still not set, well it doesn't exist (eg
|
||||
// maybe the user set it as a condition, then deleted the
|
||||
// grade item) so we call it false
|
||||
if (!array_key_exists($gradeitemid, $cachedgrades)) {
|
||||
$cachedgrades[$gradeitemid] = false;
|
||||
}
|
||||
} else {
|
||||
// Just get current grade
|
||||
$record = $DB->get_record('grade_grades', array(
|
||||
'userid'=>$userid, 'itemid'=>$gradeitemid));
|
||||
if ($record && !is_null($record->finalgrade)) {
|
||||
$score = (($record->finalgrade - $record->rawgrademin) * 100) /
|
||||
($record->rawgrademax - $record->rawgrademin);
|
||||
} else {
|
||||
// Treat the case where row exists but is null, same as
|
||||
// case where row doesn't exist
|
||||
$score = false;
|
||||
}
|
||||
$cachedgrades[$gradeitemid]=$score;
|
||||
}
|
||||
$cache->set($userid, $cachedgrades);
|
||||
}
|
||||
return $cachedgrades[$gradeitemid];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1239,11 +1226,7 @@ abstract class condition_info_base {
|
||||
* @param bool $deleted
|
||||
*/
|
||||
public static function inform_grade_changed($grade, $deleted) {
|
||||
global $USER, $SESSION;
|
||||
// In case of self-grading we can purge cache immediately.
|
||||
if ($USER->id == $grade->userid) {
|
||||
unset($SESSION->gradescorecache[$grade->itemid]);
|
||||
}
|
||||
cache::make('core', 'gradecondition')->delete($grade->userid);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1407,16 +1390,12 @@ abstract class condition_info_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* For testing only. Wipes information cached in user session.
|
||||
*
|
||||
* @global stdClass $SESSION
|
||||
* For testing only. Wipes information cached in cache.
|
||||
* Replaced with {@link core_conditionlib_testcase::wipe_condition_cache()}
|
||||
* @deprecated since 2.6
|
||||
*/
|
||||
static function wipe_session_cache() {
|
||||
global $SESSION;
|
||||
unset($SESSION->gradescorecache);
|
||||
unset($SESSION->gradescorecacheuserid);
|
||||
unset($SESSION->userfieldcache);
|
||||
unset($SESSION->userfieldcacheuserid);
|
||||
cache::make('core', 'gradecondition')->purge();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user