Skip to content

Commit

Permalink
Merge branch 'wip-MDL-28463-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle

Conflicts:
	version.php
  • Loading branch information
Damyon Wiese committed Nov 1, 2013
2 parents 54173d4 + b4dba22 commit e9a20b4
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 107 deletions.
1 change: 1 addition & 0 deletions lang/en/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
$string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_externalbadges'] = 'External badges for particular user';
$string['cachedef_gradecondition'] = 'User grades cached for evaluating conditional availability';
$string['cachedef_groupdata'] = 'Course group information';
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
$string['cachedef_langmenu'] = 'List of available languages';
Expand Down
135 changes: 66 additions & 69 deletions lib/conditionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,6 @@ private function require_data() {
*
* @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
Expand All @@ -1161,71 +1160,73 @@ private function require_data() {
* 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);
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, $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);
$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 {
// Treat the case where row exists but is null, same as
// case where row doesn't exist
$score = false;
// 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;
}
return $score;
$cache->set($userid, $cachedgrades);
}
return $cachedgrades[$gradeitemid];
}

/**
* Called by grade code to inform the completion system when a grade has
* been changed. Grades can be used to determine condition for
* the course-module or section.
*
* Note that this function may be called twice for one changed grade object.
*
* @param grade_grade $grade
* @param bool $deleted
*/
public static function inform_grade_changed($grade, $deleted) {
cache::make('core', 'gradecondition')->delete($grade->userid);
}

/**
Expand Down Expand Up @@ -1389,16 +1390,12 @@ protected function get_cached_user_profile_field($userid, $fieldid) {
}

/**
* 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();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,11 @@
'simplekeys' => true,
'simpledata' => true
),
// Used to cache user grades for conditional availability purposes.
'gradecondition' => array(
'mode' => cache_store::MODE_APPLICATION,
'staticacceleration' => true,
'staticaccelerationsize' => 2, // Should not be required for more than one user at a time.
'ttl' => 3600,
),
);
19 changes: 8 additions & 11 deletions lib/grade/grade_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,7 @@ public function update($source=null) {
* @param bool $deleted True if grade was actually deleted
*/
function notify_changed($deleted) {
global $USER, $SESSION, $CFG,$COURSE, $DB;

// Grades may be cached in user session
if ($USER->id == $this->userid) {
unset($SESSION->gradescorecache[$this->itemid]);
}
global $CFG;

// Ignore during restore
// TODO There should be a proper way to determine when we are in restore
Expand All @@ -787,6 +782,12 @@ function notify_changed($deleted) {
return;
}

// Inform conditionlib since it may cache the grades for conditional availability of modules or sections.
if (!empty($CFG->enableavailability)) {
require_once($CFG->libdir.'/conditionlib.php');
condition_info_base::inform_grade_changed($this, $deleted);
}

require_once($CFG->libdir.'/completionlib.php');

// Bail out immediately if completion is not enabled for site (saves loading
Expand All @@ -804,11 +805,7 @@ function notify_changed($deleted) {
}

// Use $COURSE if available otherwise get it via item fields
if(!empty($COURSE) && $COURSE->id == $this->grade_item->courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', array('id'=>$this->grade_item->courseid));
}
$course = get_course($this->grade_item->courseid, false);

// Bail out if completion is not enabled for course
$completion = new completion_info($course);
Expand Down
Loading

0 comments on commit e9a20b4

Please sign in to comment.