From de617a2d1bf4d74fa4239f64e7c75db86a9ba9f0 Mon Sep 17 00:00:00 2001 From: Sam Chaffee Date: Wed, 20 Jan 2016 16:34:24 -0700 Subject: [PATCH] INT-8591: Don't update grades on preferences save when they don't exist yet --- locallib.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index cd6d622..1b8ed98 100644 --- a/locallib.php +++ b/locallib.php @@ -1706,10 +1706,19 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) { $grades = array(); $userids = array_keys(get_enrolled_users($context, 'mod/attendance:canbelisted', 0, 'u.id')); + $attgrades = grade_get_grades($course->id, 'mod', 'attendance', $attid, $userids); + $usergrades = []; + if (!empty($attgrades->items[0]) and !empty($attgrades->items[0]->grades)) { + $usergrades = $attgrades->items[0]->grades; + } $statuses = att_get_statuses($attid); $gradebookmaxgrade = att_get_gradebook_maxgrade($attid); - foreach ($userids as $userid) { + foreach ($usergrades as $userid => $existinggrade) { + if (is_null($existinggrade->grade)) { + // Don't update grades where one doesn't exist yet. + continue; + } $grade = new stdClass; $grade->userid = $userid; $userstatusesstat = att_get_user_statuses_stat($attid, $course->startdate, $userid, $coursemodule); @@ -1720,8 +1729,14 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) { $grades[$userid] = $grade; } - return grade_update('mod/attendance', $course->id, 'mod', 'attendance', - $attid, 0, $grades); + if (!empty($grades)) { + $result = grade_update('mod/attendance', $course->id, 'mod', 'attendance', + $attid, 0, $grades); + } else { + $result = true; + } + + return $result; } function att_has_logs_for_status($statusid) {