From bf9445900d8599c2d80489d702fdbb25be7af100 Mon Sep 17 00:00:00 2001
From: RomanSteinberg <romanofficial@yandex.ru>
Date: Fri, 27 Feb 2015 18:29:47 +0300
Subject: [PATCH] FIX: two extra modules and max rate calculation bug

---
 .../classes/Controller/Teacher/Rating.php     | 98 ++++++++++---------
 1 file changed, 51 insertions(+), 47 deletions(-)

diff --git a/~dev_rating/application/classes/Controller/Teacher/Rating.php b/~dev_rating/application/classes/Controller/Teacher/Rating.php
index 3a9a514b0..032282708 100644
--- a/~dev_rating/application/classes/Controller/Teacher/Rating.php
+++ b/~dev_rating/application/classes/Controller/Teacher/Rating.php
@@ -32,7 +32,7 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
     }
 
     // Шапка таблицы: структура УКД (модули и мероприятия)
-    private function getStructure($id, $type) 
+    private function getStructure($id, $type)
     {
         $teacherID = $this->UserInfo['TeacherID'];
         if ($type == "rating") {
@@ -140,91 +140,95 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
         return $rates;
     }
     
-    private function correctExtra(&$curStudent, $examType, $lastExtra, $nextExtra) {
+    private function correctExtra(&$curStudent, $examType, $lastExtraIndex, $firstEmptyExtraIndex, $totalExtraRate) {
         $bottomLimit = 0;
-        $max_extra_rate = 0;
+        $maxExtraRate = 0;
         $topLimit = ($examType == 'exam') ? 38 : 60;
         
         if ($curStudent['RateSemesterResult'] >= $bottomLimit && 
             $curStudent['RateSemesterResult'] < $topLimit) // студент задолженик
         {
-            $max_extra_rate = $topLimit - $curStudent['RateSemesterResult'];
+            $maxExtraRate = $topLimit - $curStudent['RateSemesterResult'];
         }
-        if ($lastExtra >= 0) {
-            $curStudent['Rates'][$lastExtra]['MaxRate'] = $max_extra_rate;
+        if ($lastExtraIndex >= 0) {
+            $curStudent['Rates'][$lastExtraIndex]['MaxRate'] = $maxExtraRate - $totalExtraRate
+                + $curStudent['Rates'][$lastExtraIndex]['Rate'];
         } 
-        if ($nextExtra >= 0) {
-            $curStudent['Rates'][$nextExtra]['MaxRate'] = $max_extra_rate;
-        }    
+        if ($firstEmptyExtraIndex >= 0) {
+            $curStudent['Rates'][$firstEmptyExtraIndex]['MaxRate'] = $maxExtraRate - $totalExtraRate;
+        }
     }
     
     private function getRatesForExamPage(&$curStudent, $rate, $examType, $disciplineID)
     {
-        $i_r = 0;
-        $lastExam = $lastExtra = $lastNilExam = $lastNilExtra = -1;
+        $rowIndex = 0;
+        $lastExam = $lastExtraIndex = $lastNilExam = $firstEmptyExtra = -1;
+        $rateExtra = 0;
 
         $curStudent['RateSemesterResult'] = 0;
-        foreach($rate as $r) {
+        foreach($rate as $curRate) {
 
-            if (($r['ModuleType'] == 'exam') or ($r['ModuleType'] == 'extra')) {
-                    $curStudent['Rates'][$i_r] = array();
-                    $curStudent['Rates'][$i_r]['SubmoduleID'] = $r['SubmoduleID'];
-                    $curStudent['Rates'][$i_r]['Rate'] = $r['Rate'];
-                    $curStudent['Rates'][$i_r]['ModuleType'] = $r['ModuleType'];
+            if (($curRate['ModuleType'] == 'exam') or ($curRate['ModuleType'] == 'extra')) {
+                    $curStudent['Rates'][$rowIndex] = array();
+                    $curStudent['Rates'][$rowIndex]['SubmoduleID'] = $curRate['SubmoduleID'];
+                    $curStudent['Rates'][$rowIndex]['Rate'] = $curRate['Rate'];
+                    $curStudent['Rates'][$rowIndex]['ModuleType'] = $curRate['ModuleType'];
             }
 
-            switch ($r['ModuleType'])
+            switch ($curRate['ModuleType'])
             {
             case 'regular':
-                    $curStudent['RateSemesterResult'] += $r['Rate'];
+                    $curStudent['RateSemesterResult'] += $curRate['Rate'];
                     break;
             case 'exam':
-                    if (!is_null($r['Rate'])) {
+                    if (!is_null($curRate['Rate'])) {
                         if ($lastExam >= 0) {
                             $curStudent['Rates'][$lastExam]['Block'] = 'True';
                         }
-                        $lastExam = $i_r;
+                        $lastExam = $rowIndex;
                     } else {
                             if ($lastNilExam < 0) {
-                                    $lastNilExam = $i_r;
+                                    $lastNilExam = $rowIndex;
                             } else {
-                                    $curStudent['Rates'][$i_r]['Block'] = 'True';
+                                    $curStudent['Rates'][$rowIndex]['Block'] = 'True';
                             }
                     }
                     break;
             case 'bonus':
-                    $curStudent['Bonus'] = $r['Rate'];
+                    $curStudent['Bonus'] = $curRate['Rate'];
                     break;
             case 'extra':
-                    if (!is_null($r['Rate'])) {
-                            if ($lastExtra >= 0) {
-                            $curStudent['Rates'][$lastExtra]['Block'] = 'True';
+                if ($curRate['Rate']) {
+                    if ($lastExtraIndex >= 0) {
+                        $curStudent['Rates'][$lastExtraIndex]['Block'] = 'True';
                     }
-                    $lastExtra = $i_r;
-                    $curStudent['Rate'] += $r['Rate'];
+                    $lastExtraIndex = $rowIndex;
+                    $curStudent['Rate'] += $curRate['Rate'];
+                    $rateExtra += $curRate['Rate'];
+                } else {
+                    if ($firstEmptyExtra < 0) {
+                        $firstEmptyExtra = $rowIndex;
                     } else {
-                            if ($lastNilExtra < 0) {
-                                    $lastNilExtra = $i_r;
-                            } else {
-                                    $curStudent['Rates'][$i_r]['Block'] = 'True';
-                            }
+                        $curStudent['Rates'][$rowIndex]['Block'] = 'True';
                     }
-                    break;
+                }
+                break;
             default:
                     throw HTTP_Exception::factory (500, "Некорректный тип модуля!");
             }
-            $i_r++;
+            $rowIndex++;
         }
         
         $total = $this->model_rating->GetStudentRate($curStudent['ID'], $disciplineID);
-        $total = $total[0]['Num'];
-        if (is_null($total)) $total = 0;
+        $total = ($total[0])?$total[0]['Num']:0;
+        if ($total)
+            $total = 0;
 
         if ($lastExam >= 0) {
             $curStudent['RateResult'] += $curStudent['Rates'][$lastExam]['Rate'];
         }
         $curStudent['RateResult'] = $total;
-        $this->correctExtra($curStudent, $examType, $lastExtra, $lastNilExtra);
+        $this->correctExtra($curStudent, $examType, $lastExtraIndex, $firstEmptyExtra, $rateExtra);
     }
 	
 	protected function get_edit_rights_for_teacher($teacherID, $disciplineID) // $this->UserInfo['TeacherID']=id
@@ -238,12 +242,12 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
     protected function stub_action($page_type) //$page_type: rating, exam
     {
         $twig = Twig::factory("teacher/".$page_type); //TODO: validate twig 
-        $id = $this->request->param('id');
-        $disciplineInfo = $this->getDisciplineInformation($id);
-	$structureHandled = $this->getStructure($id, $page_type);
+        $disciplineId = $this->request->param('id');
+        $disciplineInfo = $this->getDisciplineInformation($disciplineId);
+	    $structureHandled = $this->getStructure($disciplineId, $page_type);
         
         // Студенты и их баллы
-        $students = $this->model_rating->GetStudentsForRating($this->UserInfo['TeacherID'], $id);
+        $students = $this->model_rating->GetStudentsForRating($this->UserInfo['TeacherID'], $disciplineId);
         $rateHandled = array();
         $groupsHandled = array();
         $i_g = $i_s = $curGroup = 0;
@@ -264,7 +268,7 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
             
             // Баллы студента
             if ($page_type == "rating") {
-                $rates_raw = $this->model_rating->getMapForStudent($row['ID'], $id);
+                $rates_raw = $this->model_rating->getMapForStudent($row['ID'], $disciplineId);
                 $rates = $this->getRatesForRatingPage($rates_raw);
                 $curStudent['RateResult'] = $rates['RateResult'];
                 unset($rates['RateResult']);
@@ -272,14 +276,14 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
                 $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
                 
             } else {
-                $rate = $this->model_rating->getMapForStudentExam($row['ID'], $id);
-                $this->getRatesForExamPage($curStudent, $rate, $disciplineInfo['ExamType'], $id);
+                $rate = $this->model_rating->getMapForStudentExam($row['ID'], $disciplineId);
+                $this->getRatesForExamPage($curStudent, $rate, $disciplineInfo['ExamType'], $disciplineId);
                 $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
             }
             $i_s++;
         }
         
-		$editRifhts=$this->get_edit_rights_for_teacher($this->UserInfo['TeacherID']=id, $disciplineInfo['ID']);
+		$editRights=$this->get_edit_rights_for_teacher($this->UserInfo['TeacherID']=id, $disciplineInfo['ID']);
         if($editRights == 1)
 			$twig->editRights=$editRights;
 		else
-- 
GitLab