Skip to content

Commit d42a961

Browse files
committed
MDL-86278 grades: Serialise category total item creation
1 parent 5afb4ba commit d42a961

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

‎public/lib/grade/grade_category.php‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,8 @@ public function load_grade_item() {
22852285
* @return grade_item
22862286
*/
22872287
public function get_grade_item() {
2288+
global $DB;
2289+
22882290
if (empty($this->id)) {
22892291
debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
22902292
return false;
@@ -2298,12 +2300,27 @@ public function get_grade_item() {
22982300
}
22992301

23002302
if (!$gradeitems = grade_item::fetch_all($params)) {
2301-
// create a new one
2302-
$gradeitem = new grade_item($params, false);
2303-
$gradeitem->gradetype = GRADE_TYPE_VALUE;
2304-
$gradeitem->insert('system');
2303+
$lockfactory = \core\lock\lock_config::get_lock_factory('core_grades');
2304+
2305+
if (!$lock = $lockfactory->get_lock('categorytotal_' . $this->id, 10)) {
2306+
throw new \moodle_exception('locktimeout');
2307+
}
2308+
2309+
try {
2310+
// Another request may have created the total while we waited for the category lock.
2311+
$DB->mark_tables_for_primary('grade_items');
2312+
if (!$gradeitems = grade_item::fetch_all($params)) {
2313+
$gradeitem = new grade_item($params, false);
2314+
$gradeitem->gradetype = GRADE_TYPE_VALUE;
2315+
$gradeitem->insert('system');
2316+
$gradeitems = [$gradeitem];
2317+
}
2318+
} finally {
2319+
$lock->release();
2320+
}
2321+
}
23052322

2306-
} else if (count($gradeitems) == 1) {
2323+
if (count($gradeitems) == 1) {
23072324
// found existing one
23082325
$gradeitem = reset($gradeitems);
23092326

0 commit comments

Comments
 (0)