Skip to content

Commit

Permalink
Merge branch 'MDL-70245-master' of https://github.com/TomoTsuyuki/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 7, 2021
2 parents c2a21a2 + 745d4c2 commit 74200b8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions lang/en/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
$string['cachedef_user_group_groupings'] = 'User\'s groupings and groups per course';
$string['cachedef_user_course_content_items'] = 'User\'s content items (activities, resources and their subtypes) per course';
$string['cachedef_yuimodules'] = 'YUI Module definitions';
$string['cachedef_gradesetting'] = 'Course grade setting';
$string['cachelock_file_default'] = 'Default file locking';
$string['cachestores'] = 'Cache stores';
$string['canuselocalstore'] = 'Can use local store';
Expand Down
8 changes: 8 additions & 0 deletions lib/db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,12 @@
'simplekeys' => true,
'simpledata' => false,
],

// Cache the grade setting for faster retrieval.
'gradesetting' => [
'mode' => cache_store::MODE_REQUEST,
'simplekeys' => true,
'staticacceleration' => true,
'staticaccelerationsize' => 100
],
);
15 changes: 9 additions & 6 deletions lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,18 @@ function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $use
function grade_get_setting($courseid, $name, $default=null, $resetcache=false) {
global $DB;

static $cache = array();
$cache = cache::make('core', 'gradesetting');
$gradesetting = $cache->get($courseid) ?: array();

if ($resetcache or !array_key_exists($courseid, $cache)) {
$cache[$courseid] = array();
if ($resetcache or empty($gradesetting)) {
$gradesetting = array();
$cache->set($courseid, $gradesetting);

} else if (is_null($name)) {
return null;

} else if (array_key_exists($name, $cache[$courseid])) {
return $cache[$courseid][$name];
} else if (array_key_exists($name, $gradesetting)) {
return $gradesetting[$name];
}

if (!$data = $DB->get_record('grade_settings', array('courseid'=>$courseid, 'name'=>$name))) {
Expand All @@ -698,7 +700,8 @@ function grade_get_setting($courseid, $name, $default=null, $resetcache=false) {
$result = $default;
}

$cache[$courseid][$name] = $result;
$gradesetting[$name] = $result;
$cache->set($courseid, $gradesetting);
return $result;
}

Expand Down
49 changes: 49 additions & 0 deletions lib/tests/upgradelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,55 @@ public function test_upgrade_calculated_grade_items_freeze() {
$this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course2->id});
}

/**
* Test the upgrade function for final grade after setting grade max for category and grade item.
*/
public function test_upgrade_update_category_grademax_regrade_final_grades() {
global $DB;

$this->resetAfterTest();

$generator = $this->getDataGenerator();
$user = $generator->create_user();

// Create a new course.
$course = $generator->create_course();

// Set the course aggregation to weighted mean of grades.
$unitcategory = \grade_category::fetch_course_category($course->id);
$unitcategory->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
$unitcategory->update();

// Set grade max for category.
$gradecategoryitem = grade_item::fetch(array('iteminstance' => $unitcategory->id));
$gradecategoryitem->grademax = 50;
$gradecategoryitem->update();

// Make new grade item.
$gradeitem = new \grade_item($generator->create_grade_item([
'itemname' => 'Grade item',
'idnumber' => 'git1',
'courseid' => $course->id,
'grademin' => 0,
'grademax' => 50,
'aggregationcoef' => 100.0,
]));

// Set final grade.
$grade = $gradeitem->get_grade($user->id, true);
$grade->finalgrade = 20;
$grade->update();

$courseitem = \grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'course']);
$gradeitem->force_regrading();

// Trigger regrade because the grade items needs to be updated.
grade_regrade_final_grades($course->id);

$coursegrade = new \grade_grade($courseitem->get_final($user->id), false);
$this->assertEquals(20, $coursegrade->finalgrade);
}

function test_upgrade_calculated_grade_items_regrade() {
global $DB, $CFG;

Expand Down

0 comments on commit 74200b8

Please sign in to comment.