From 77318aa153f35d1872816a3e13028d56aec3c85e Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.silence@gmail.com>
Date: Thu, 14 Jul 2016 14:15:54 +0300
Subject: [PATCH] #98, Fix incorrect result rate after exam deletion

---
 media/js/config.js                 |  6 ++++-
 media/js/discipline/rating/exam.js | 43 +++++++++++-------------------
 2 files changed, 20 insertions(+), 29 deletions(-)

diff --git a/media/js/config.js b/media/js/config.js
index 281d0427b..011b7ba23 100644
--- a/media/js/config.js
+++ b/media/js/config.js
@@ -15,10 +15,14 @@ $.fn.extend({
     turnOn: function () {
         return $(this).prop('disabled', false)
     },
-
     turnOff: function () {
         return $(this).prop('disabled', true)
     },
+    aggregate: function (foo, init = 0) {
+        var res = init;
+        this.each((_, elem) => { res = foo(res, elem); });
+        return res;
+    }
 });
 
 var Auth = (function () {
diff --git a/media/js/discipline/rating/exam.js b/media/js/discipline/rating/exam.js
index c21e1e04b..b551311a4 100644
--- a/media/js/discipline/rating/exam.js
+++ b/media/js/discipline/rating/exam.js
@@ -143,11 +143,7 @@ $(function () {
         }
 
         // суммарный добор
-        var extraRate = 0;
-        jExtraInputs.each(function () {
-            extraRate += +$(this).val();
-        });
-
+        var extraRate = jExtraInputs.aggregate((cur, elem) => +$(elem).val() + cur);
         var row = +jRow.attr('id').substr(4); // одинаковая для всех
 
         // Определим экзамен
@@ -349,8 +345,8 @@ $(function () {
 
             // Открываем доступ к след. ячейке добора баллов
             if (jThis.hasClass('additionalCell')) {
-                nextAdditionalCell = $('#col_' + (g_col + 1) + '_row_' + g_row);
-                placeholderMaxVal = (rateResult < 60) ? (60 - rateResult) : 0;//(60 - oldRate);
+                var nextAdditionalCell = $('#col_' + (g_col + 1) + '_row_' + g_row);
+                var placeholderMaxVal = (rateResult < 60) ? (60 - rateResult) : 0;//(60 - oldRate);
 
                 if (nextAdditionalCell.hasClass('additionalCell')) {
                     var placeholderMax = (placeholderMaxVal > 0) ? placeholderMax = 'макс. ' + placeholderMaxVal : '---';
@@ -396,7 +392,7 @@ $(function () {
         // блокируем ячейку пока не обработаем коллбек
         jThis.children('input').attr('disabled', true);
 
-        var rateResult = newRate;
+        var rateResult = Math.max(0, newRate);
         var bonus = parseInt(jThis.siblings('.bonus').text());
 
         // считаем баллы по строке
@@ -404,26 +400,19 @@ $(function () {
         {
             // страница сессии
             rateResult += parseInt(jThis.siblings('.semesterRateResultCell').text());
-
-            jThis.siblings('.additionalCell').each(function () {
-                if ($(this).children('input').val() !== '')
-                    rateResult += +$(this).children('input').val();
-            });
+            jThis.siblings('.additionalCell').aggregate((init, elem) => +$(elem).children('input').val() + init);
         }
+        if (newRate === -1)
+            rateResult += jThis.siblings('.attemptCell').not('.autoPass').not('.absenceCell')
+                .aggregate((init, elem) => Math.max(init, +$(elem).find('input').val()));
 
-        if (newRate <= g_submoduleMaxRate) {
+        if (newRate <= g_submoduleMaxRate)
             setRate(newRate, jThis, oldRate, rateResult, bonus);
-        }
         else {
-            if (oldRate <= g_submoduleMaxRate) {
-                if (oldRate != -1)
-                    jThis.children('input').val(oldRate);
-                else
-                    jThis.children('input').val('');
-            }
-            else
-                jThis.children('input').val('0');
-
+            var cellRate = (oldRate <= g_submoduleMaxRate)
+                ? ((oldRate != -1) ? oldRate : '')
+                : '0';
+            jThis.children('input').val(cellRate);
             EventInspector.error('Текущий балл превышает максимальный для данного модуля');
         }
         jThis.children('input').removeAttr('disabled');
@@ -445,10 +434,8 @@ $(function () {
         TdFocus($(this));
         TdInfo($(this));
 
-        if ($(this).children('input').val() !== '') {
-            oldRate = $(this).children('input').val();
-        }
-        else oldRate = -1;
+        var value = $(this).children('input').val();
+        oldRate = (value !== '') ? +value : -1;
     });
 
     jCommonCell.focusout(function () {
-- 
GitLab