From 1d693611e51a408800fa681f8e01f3571a27d341 Mon Sep 17 00:00:00 2001 From: Huy Hoang Date: Mon, 10 Nov 2014 12:14:31 +0800 Subject: [PATCH 1/2] MDL-39692 grade: Ability to delete letter boundary --- grade/edit/letter/index.php | 43 +++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/grade/edit/letter/index.php b/grade/edit/letter/index.php index 19743672f9d..c0e468f8d73 100644 --- a/grade/edit/letter/index.php +++ b/grade/edit/letter/index.php @@ -137,7 +137,7 @@ if (!$edit) { } $letters = array(); - for($i=1; $i<$num+1; $i++) { + for ($i=1; $i < $num+1; $i++) { $gradelettername = 'gradeletter'.$i; $gradeboundaryname = 'gradeboundary'.$i; @@ -146,33 +146,54 @@ if (!$edit) { if ($letter == '') { continue; } + $boundary = floatval($data->$gradeboundaryname); + + if ($boundary < 0 || $boundary > 100) { + continue; // skip if out of range + } + // The keys need to be strings so floats are not truncated. - $letters[strval($data->$gradeboundaryname)] = $letter; + $letters[number_format($boundary, 5)] = $letter; } } - krsort($letters, SORT_NUMERIC); - $old_ids = array(); - if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC', 'id')) { - $old_ids = array_keys($records); + $pool = array(); + if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) { + foreach ($records as $r) { + // will re-use the lowerboundary to avoid duplicate during the update process + $pool[number_format($r->lowerboundary, 5)] = $r; + } } - foreach($letters as $boundary=>$letter) { + foreach ($letters as $boundary => $letter) { $record = new stdClass(); $record->letter = $letter; $record->lowerboundary = $boundary; $record->contextid = $context->id; - if ($old_id = array_pop($old_ids)) { - $record->id = $old_id; + // re-use the existing boundary to avoid key constraint + if (isset($pool[$boundary])) { + // skip if the letter has been assigned to the boundary already + if ($letter == $pool[$boundary]->letter) { + unset($pool[$boundary]); // take it out of the pool + } + else { + $record->id = $pool[$boundary]->id; + $DB->update_record('grade_letters', $record); + unset($pool[$boundary]); // remove the ID from the pool + } + } + else if ($candidate = array_pop($pool)) { + $record->id = $candidate->id; $DB->update_record('grade_letters', $record); } else { $DB->insert_record('grade_letters', $record); } } - foreach($old_ids as $old_id) { - $DB->delete_records('grade_letters', array('id' => $old_id)); + // delete the unused records + foreach($pool as $leftover) { + $DB->delete_records('grade_letters', array('id' => $leftover->id)); } redirect($returnurl); From b53a658b267f0acb9e3cac845ff136ba7d69fbff Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 10 Nov 2014 12:59:59 +0800 Subject: [PATCH 2/2] MDL-39692 grade: Little code clean up of letter boundary deletion --- grade/edit/letter/index.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/grade/edit/letter/index.php b/grade/edit/letter/index.php index c0e468f8d73..00fb110d8b9 100644 --- a/grade/edit/letter/index.php +++ b/grade/edit/letter/index.php @@ -146,10 +146,10 @@ if (!$edit) { if ($letter == '') { continue; } - $boundary = floatval($data->$gradeboundaryname); + $boundary = floatval($data->$gradeboundaryname); if ($boundary < 0 || $boundary > 100) { - continue; // skip if out of range + continue; // Skip if out of range. } // The keys need to be strings so floats are not truncated. @@ -160,7 +160,7 @@ if (!$edit) { $pool = array(); if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) { foreach ($records as $r) { - // will re-use the lowerboundary to avoid duplicate during the update process + // Will re-use the lowerboundary to avoid duplicate during the update process. $pool[number_format($r->lowerboundary, 5)] = $r; } } @@ -171,27 +171,25 @@ if (!$edit) { $record->lowerboundary = $boundary; $record->contextid = $context->id; - // re-use the existing boundary to avoid key constraint if (isset($pool[$boundary])) { - // skip if the letter has been assigned to the boundary already - if ($letter == $pool[$boundary]->letter) { - unset($pool[$boundary]); // take it out of the pool - } - else { + // Re-use the existing boundary to avoid key constraint. + if ($letter != $pool[$boundary]->letter) { + // The letter has been assigned to another boundary, we update it. $record->id = $pool[$boundary]->id; $DB->update_record('grade_letters', $record); - unset($pool[$boundary]); // remove the ID from the pool } - } - else if ($candidate = array_pop($pool)) { + unset($pool[$boundary]); // Remove the letter from the pool. + } else if ($candidate = array_pop($pool)) { + // The boundary is new, we update a random record from the pool. $record->id = $candidate->id; $DB->update_record('grade_letters', $record); } else { + // No records were found, this must be a new letter. $DB->insert_record('grade_letters', $record); } } - // delete the unused records + // Delete the unused records. foreach($pool as $leftover) { $DB->delete_records('grade_letters', array('id' => $leftover->id)); }