From b9e917bd8c671328bbea2cd9ad0d9633caa37373 Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Fri, 23 Jan 2015 00:33:46 +0300
Subject: [PATCH] Rating refactoring

---
 db/StoredProcedures.sql                       |   6 +-
 .../classes/Controller/Teacher/Rating.php     | 534 +++++++-----------
 .../application/views/teacher/rating.twig     |   2 +-
 3 files changed, 220 insertions(+), 322 deletions(-)

diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index ddc45ef74..9e4328932 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -3594,7 +3594,7 @@ END //
 
 -- Вычисление максимального балла для submodule
 DROP FUNCTION IF EXISTS `CalculateMaxRateForExtra`//
-CREATE DEFINER=`mmcs_rating`@`localhost` FUNCTION `CalculateMaxRateForExtra`(`SubmoduleID` INT,	`StudentID` INT
+CREATE FUNCTION `CalculateMaxRateForExtra`(`SubmoduleID` INT,	`StudentID` INT
 							) RETURNS int(11)
 BEGIN
 	DECLARE examTypeVar VARCHAR(30); -- enum('exam', 'credit');-- utf8_general_ci;
@@ -3612,9 +3612,9 @@ BEGIN
 	WHERE modules.type='extra' AND subm1.ID=SubmoduleID;
 	
 	return res;
-END
+END //
 
-DROP FUNCTION IF EXISTS SetStudentRate; //
+DROP FUNCTION IF EXISTS SetStudentRate//
 CREATE FUNCTION `SetStudentRate`(	`TeacherID` 	INT,
 									`StudentID` 	INT,
 									`SubmoduleID` 	INT,
diff --git a/~dev_rating/application/classes/Controller/Teacher/Rating.php b/~dev_rating/application/classes/Controller/Teacher/Rating.php
index 479496e24..630af019b 100644
--- a/~dev_rating/application/classes/Controller/Teacher/Rating.php
+++ b/~dev_rating/application/classes/Controller/Teacher/Rating.php
@@ -7,18 +7,14 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
 
         $this->model_rating = new Model_Teacher_Rating;
         $this->model_discipline = new Model_Teacher_Map;
-
         parent::before();
     }
 
     // Получить из кука SGID выбранную ранее группу для данной дисциплины
-    private function getStudyGroupID_ForFilter($id)
-    {
+    private function getStudyGroupID_ForFilter($id) {
         # $id = disciplineID
         $SG_array = json_decode(Cookie::get('SGID', null), true);
-        if ($SG_array !== null) 
-        {
-            if (array_key_exists($id, $SG_array))
+        if ($SG_array !== null && array_key_exists($id, $SG_array)) {
                 return $SG_array[$id];
         }
         return 0;
@@ -26,374 +22,276 @@ class Controller_Teacher_Rating extends Controller_UserEnvi {
 
     // Настройки дисциплины и выбранная группа(для фильтра)
     private function getDisciplineInformation($id) {
-
+        
         $temp = $this->model_discipline->getDisciplineInfoByID($id);
-        $disciplineInfo['ExamType'] = $temp[0]['ExamType'];
+        $disciplineInfo = $temp[0];
         $disciplineInfo['ID'] = $id;
-        $disciplineInfo['isMilestone'] = $temp[0]['isMilestone'];
-        $disciplineInfo['StudyGroupID_Filter'] = $this->getStudyGroupID_ForFilter($id);
+        $disciplineInfo['StudyGroupID_Filter'] = $this->getStudyGroupID_ForFilter($id);      
 
         return $disciplineInfo;
     }
 
-
-    // Страница оценивания в течение семестра
-    public function action_edit()
+    // Шапка таблицы: структура УКД (модули и мероприятия)
+    private function getStructure($id, $type) 
     {
-        $twig = Twig::factory('teacher/rating');
-        $twig->User = $this->UserInfo;
-        $id = $this->request->param('id');
-
-        // Шапка таблицы: структура УКД (модули и мероприятия)
-        $structure = $this->model_rating->GetMapForDiscipline($this->UserInfo['TeacherID'], $id);
-
-        if($structure->count() == 0)
-            throw HTTP_Exception::factory (404, "Для дисциплины с ID $id не создана УКД или такой дисциплины не существует!");
+        $teacherID = $this->UserInfo['TeacherID'];
+        if ($type == "rating") {
+            $structure = $this->model_rating->getMapForDiscipline($teacherID, $id);
+        } else {
+            $structure = $this->model_rating->GetMapForDisciplineExam($teacherID, $id);
+        }
+        if($structure->count() == 0) {
+            throw HTTP_Exception::factory (404, "Страница не найдена");
+        }
         
-		$structureHandled = array();
-        $maxRate = 0; $i = 0; $temp_moduleID = 0;
+        $structureHandled = array();
+        $maxRate = $i = 0;
+        $temp_moduleID = -1;
+        $try = 0; // try = 1 - экзамен, = 2, 3 - пересдачи
 
-        foreach($structure as $row)
-        {
+        
+        foreach($structure as $row) {
             if($row['ModuleID'] != $temp_moduleID)
             {
-                $i++; // todo
+                ++$i; // todo
                 $temp_moduleID = $row['ModuleID'];
-            }
-            if ($i == 1)
-                $twig->SubjectName = $row['SubjectName'];
-
-            if(!isset($structureHandled[$i]['SubmodulesCount']))
-            {
                 $structureHandled[$i]['SubmodulesCount'] = 0;
                 $structureHandled[$i]['MaxRate'] = 0;
+                $structureHandled[$i]['ModuleTitle'] = $row['ModuleName'];
+                $structureHandled[$i]['ModuleType'] = $row['ModuleType'];
             }
+
             $j = $structureHandled[$i]['SubmodulesCount'] += 1;
             $structureHandled[$i]['MaxRate'] += (int) $row['MaxRate'];
-            $structureHandled[$i]['ModuleTitle'] = $row['ModuleName'];
-            $structureHandled[$i]['ModuleType'] = $row['ModuleType'];
-            if ($row['ModuleType'] == "exam") {
-                $twig->examSubmoduleID = $row['SubmoduleID'];
-            }
-                    
-            $structureHandled[$i][$j]['SubmoduleID'] = $row['SubmoduleID'];
-            $structureHandled[$i][$j]['Title'] = $row['SubModuleName'];
-            //$structureHandled[$i][$j]['Description'] = $row['SubmoduleDescription']; // Описание
-            $structureHandled[$i][$j]['MaxRate'] = (int) $row['MaxRate'];
-            $maxRate += $row['MaxRate'];
 
-            if ($row['ModuleType'] == "exam" || $row['ModuleType'] == "extra") {
-                $structureHandled[$i][$j]['SubmoduleID'] = -1;
+            $cur_submodule = array();
+            $cur_submodule = $row;
+            $cur_submodule['MaxRate'] = (int)$cur_submodule['MaxRate'];
+            
+            if ($type == "rating") {
+                $cur_submodule['Title'] = $row['SubModuleName'];
+                $maxRate += $row['MaxRate'];
+            } else { //$type == "exam"
+                if ($row['ModuleType'] == 'extra') {
+                    $cur_submodule['Title'] = 'Добор баллов';
+                    $maxRate += $row['MaxRate'];
+                }
+                else {
+                    if ($try === 0) {
+                        $cur_submodule['Title'] = 'Основная сдача';
+                    } else {
+                        $cur_submodule['Title'] = 'Пересдача ' . $try;
+                    }
+                    $try++;
+                }
             }
+            $structureHandled[$i][$j] = $cur_submodule;
         }
         $structureHandled['ModulesCount'] = $i;
-        $structureHandled['MaxRate'] = (int) $maxRate; 
-        // end Шапка таблицы
-
-
-        // Студенты и их баллы
-        $students = $this->model_rating->GetStudentsForRating($this->UserInfo['TeacherID'], $id);
-
-        $rateHandled = array();
-        $groupsHandled = array();
-        $i_g = 0; $i_s = 0; $curGroup = 0;
-
-        foreach($students as $row) {
-            // Группа
-            if ($curGroup !== $row['GroupID']) 
-            {
-                $curGroup = $row['GroupID'];
-                $i_g++;
-                $rateHandled[$i_g]['GroupID'] = $row['GroupID'];
-                $rateHandled[$i_g]['GroupNum'] = $row['GroupNum']; // в„– РіСЂСѓРїРїС‹
-                $rateHandled[$i_g]['GradeNum'] = $row['GradeNum']; // в„– РєСѓСЂСЃР°
-                $rateHandled[$i_g]['isAttached'] = $row['isAttached']; // #1
-
-                if ($row['Degree'] == 'bachelor')
-                    $rateHandled[$i_g]['GradeTitle'] = $row['GradeNum'].' РєСѓСЂСЃ';
-
-                if ($row['Degree'] == 'specialist')
-                    $rateHandled[$i_g]['GradeTitle'] = $row['GradeNum'].' РєСѓСЂСЃ';
-
-                if ($row['Degree'] == 'master')
-                    $rateHandled[$i_g]['GradeTitle'] = 'Магистратура, '.$row['GradeNum'].' год';
+        $structureHandled['MaxRate'] = (int) $maxRate;
+        return $structureHandled;
+    }
+    
+    private function processGroupInfo($groupInfo) 
+    {
+        //GroupID, GroupNum, GradeNum, isAttached etc.
+        $out = $groupInfo;
+        
+        //Формирование заголовка курса
+        $gradeNum = $groupInfo['GradeNum'];
+        if ($groupInfo['Degree'] == 'bachelor') {
+            $out['GradeTitle'] = $gradeNum.' РєСѓСЂСЃ';
+        } else if ($groupInfo['Degree'] == 'specialist') {
+            $out['GradeTitle'] = $gradeNum.' РєСѓСЂСЃ';
+        } if ($groupInfo['Degree'] == 'master') {
+            $out['GradeTitle'] = 'Магистратура, '.$gradeNum.' год';
+        }
+        
+        //Формирование заголовка группы
+        $out['GroupTitle'] = $out['GradeTitle']." ".$groupInfo['GroupNum']." РіСЂСѓРїРїР°";
+        return $out;
+    }
+    
+    private function getRatesForRatingPage($info) 
+    {
 
-                $groupsHandled[$curGroup] = $rateHandled[$i_g]['GradeTitle']." ".$row['GroupNum']." РіСЂСѓРїРїР°"; 
+        $rates = array();
+        $i_r = 0;
+        $rateResult = 0;
+
+        foreach($info as $row) {
+             ++$i_r;
+            $cur_rate = array();
+            $rateResult += $cur_rate['Rate'] = $row['Rate'];
+            $moduleType = $row['ModuleType'];
+            $cur_rate['Type'] = $moduleType;
+            if ($moduleType == 'exam') {
+                $cur_rate['SubmoduleID'] = -1;
+            } else if ($moduleType == 'extra') {
+                $cur_rate['SubmoduleID'] = -2;
+            } else {
+                $cur_rate['SubmoduleID'] = $row['SubmoduleID'];
             }
 
-            // Студенты
-            $curStudent = array();
-            $i_s++;
-            $curStudent['ID'] = $row['ID'];
-            $curStudent['Last'] = $row['Last'];
-            $curStudent['First'] = $row['First'];
-            $curStudent['Second'] = $row['Second'];
-            $curStudent['isAttached'] = $row['isAttached']; // #1
+            $rates[$i_r] = $cur_rate;
+           
+        }
+        $rates['RateResult'] = $rateResult;
+        return $rates;
+    }
+    
+    private function correctExtra(&$curStudent, $examType, $lastExtra, $nextExtra) {
+        $bottomLimit = 0;
+        $max_extra_rate = 0;
+        $topLimit = ($examType == 'exam') ? 38 : 60;
+        
+        if ($curStudent['RateSemesterResult'] >= $bottomLimit && 
+            $curStudent['RateSemesterResult'] < $topLimit) // студент задолженик
+        {
+            $max_extra_rate = $topLimit - $curStudent['RateSemesterResult'];
+        }
+        if ($lastExtra >= 0) {
+            $curStudent['Rates'][$lastExtra]['MaxRate'] = $max_extra_rate;
+        } 
+        if ($nextExtra >= 0) {
+            $curStudent['Rates'][$nextExtra]['MaxRate'] = $max_extra_rate;
+        }    
+    }
+    
+    private function getRatesForExamPage(&$curStudent, $rate, $examType, $disciplineID)
+    {
+        $i_r = 0;
+        $lastExam = $lastExtra = $lastNilExam = $lastNilExtra = -1;
 
-            // Баллы студента
-            $rates = $this->model_rating->getMapForStudent($row['ID'], $id);
-            $i_r = 0;
-            $curStudent['RateResult'] = 0;
-            foreach($rates as $r) {
-                $i_r++;
-                $curStudent['Rates'][$i_r]['Rate'] = $r['Rate'];
-                $curStudent['RateResult'] += $r['Rate'];
+        $curStudent['RateSemesterResult'] = 0;
+        foreach($rate as $r) {
 
-                if ($r['ModuleType'] == 'exam')
-                    $curStudent['Rates'][$i_r]['SubmoduleID'] = -1;
-                else if ($r['ModuleType'] == 'extra')
-                    $curStudent['Rates'][$i_r]['SubmoduleID'] = -2;
-                else 
+            if (($r['ModuleType'] == 'exam') or ($r['ModuleType'] == 'extra')) {
+                    $curStudent['Rates'][$i_r] = array();
                     $curStudent['Rates'][$i_r]['SubmoduleID'] = $r['SubmoduleID'];
-
-                $curStudent['Rates'][$i_r]['Type'] = $r['ModuleType'];
+                    $curStudent['Rates'][$i_r]['Rate'] = $r['Rate'];
+                    $curStudent['Rates'][$i_r]['ModuleType'] = $r['ModuleType'];
             }
-            $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
 
-            /*
-            $i_s++;
-            $rateHandled[$i_g]['Students'][$i_s]['ID'] = $row['ID'];
-            $rateHandled[$i_g]['Students'][$i_s]['Last'] = $row['Last'];
-            $rateHandled[$i_g]['Students'][$i_s]['First'] = $row['First'];
-            $rateHandled[$i_g]['Students'][$i_s]['Second'] = $row['Second'];
-            $rateHandled[$i_g]['Students'][$i_s]['isAttached'] = $row['isAttached']; // #1
-
-            // Баллы студента
-            $rate = $this->model_rating->getMapForStudent($row['ID'], $id);
-            $i_r = 0;
-            $rateHandled[$i_g]['Students'][$i_s]['RateResult'] = 0;
-            foreach($rate as $r) {
-                $i_r++;
-                $rateHandled[$i_g]['Students'][$i_s]['Rates'][$i_r]['Rate'] = $r['Rate'];
-                $rateHandled[$i_g]['Students'][$i_s]['RateResult'] += $r['Rate'];
-
-                if ($r['ModuleType'] == 'exam')
-                    $rateHandled[$i_g]['Students'][$i_s]['Rates'][$i_r]['SubmoduleID'] = -1;
-				else if ($r['ModuleType'] == 'extra')
-					$rateHandled[$i_g]['Students'][$i_s]['Rates'][$i_r]['SubmoduleID'] = -2;
-                else 
-                    $rateHandled[$i_g]['Students'][$i_s]['Rates'][$i_r]['SubmoduleID'] = $r['SubmoduleID'];
+            switch ($r['ModuleType'])
+            {
+            case 'regular':
+                    $curStudent['RateSemesterResult'] += $r['Rate'];
+                    break;
+            case 'exam':
+                    if (!is_null($r['Rate'])) {
+                        if ($lastExam >= 0) {
+                            $curStudent['Rates'][$lastExam]['Block'] = 'True';
+                        }
+                        $lastExam = $i_r;
+                    } else {
+                            if ($lastNilExam < 0) {
+                                    $lastNilExam = $i_r;
+                            } else {
+                                    $curStudent['Rates'][$i_r]['Block'] = 'True';
+                            }
+                    }
+                    break;
+            case 'bonus':
+                    $curStudent['Bonus'] = $r['Rate'];
+                    break;
+            case 'extra':
+                    if (!is_null($r['Rate'])) {
+                            if ($lastExtra >= 0) {
+                            $curStudent['Rates'][$lastExtra]['Block'] = 'True';
+                    }
+                    $lastExtra = $i_r;
+                    $curStudent['Rate'] += $r['Rate'];
+                    } else {
+                            if ($lastNilExtra < 0) {
+                                    $lastNilExtra = $i_r;
+                            } else {
+                                    $curStudent['Rates'][$i_r]['Block'] = 'True';
+                            }
+                    }
+                    break;
+            default:
+                    throw HTTP_Exception::factory (500, "Некорректный тип модуля!");
             }
-            */
+            $i_r++;
         }
+        
+        $total = $this->model_rating->GetStudentRate($curStudent['ID'], $disciplineID);
+        $total = $total[0]['Num'];
+        if (is_null($total)) $total = 0;
 
-        // На вывод
-        $twig->headerRate = $structureHandled; 
-        $twig->rateTable = $rateHandled;
-        $twig->groups = $groupsHandled;
-
-        $twig->disciplineInfo = $this->getDisciplineInformation($id);
-        $twig->disciplineInfo_JSON = json_encode($twig->disciplineInfo);
-
-		$this->response->body($twig);
+        if ($lastExam >= 0) {
+            $curStudent['RateResult'] += $curStudent['Rates'][$lastExam]['Rate'];
+        }
+        $curStudent['RateResult'] = $total;
+        $this->correctExtra($curStudent, $examType, $lastExtra, $lastNilExtra);
     }
-
-    // Страница оценивания в сессию
-    public function action_exam()
+    
+    protected function stub_action($page_type) //$page_type: rating, exam
     {
-        $twig = Twig::factory('teacher/exam');
-        $twig->User = $this->UserInfo;
+        $twig = Twig::factory("teacher/".$page_type); //TODO: validate twig 
         $id = $this->request->param('id');
-
-        // Шапка таблицы: структура УКД (модули и мероприятия)
-        $structure = $this->model_rating->GetMapForDisciplineExam($this->UserInfo['TeacherID'], $id);
-		$disciplineInfo = $this->getDisciplineInformation($id);
-
-        if($structure->count() == 0)
-            throw HTTP_Exception::factory (404, "Страница не найдена");
-
+        $disciplineInfo = $this->getDisciplineInformation($id);
+	$structureHandled = $this->getStructure($id, $page_type);
         
-        $structureHandled = array();
-        $maxRate = 0; $i = 0; $temp_moduleID = 0; $try_1 = 0; // try = 1 - экзамен, = 2, 3 - пересдачи
-        $try = 0;
-
-        foreach($structure as $row)
-        {
-            if($row['ModuleID'] != $temp_moduleID)
-            {
-               ++$i; // todo
-               $temp_moduleID = $row['ModuleID'];
-            }
-            if ($i == 1)
-                $twig->SubjectName = $row['SubjectName'];
-
-            if(!isset($structureHandled[$i]['SubmodulesCount']))
-            {
-                $structureHandled[$i]['SubmodulesCount'] = 0;
-                $structureHandled[$i]['MaxRate'] = 0;
-            }
-            $j = $structureHandled[$i]['SubmodulesCount'] += 1;
-            $structureHandled[$i]['MaxRate'] += (int) $row['MaxRate'];
-            $structureHandled[$i]['ModuleTitle'] = $row['ModuleName'];
-            $structureHandled[$i]['ModuleType'] = $row['ModuleType'];
-            if ($row['ModuleType'] == "exam")
-                $twig->examSubmoduleID = $row['SubmoduleID'];
-                    
-            $structureHandled[$i][$j]['SubmoduleID'] = $row['SubmoduleID'];
-            if ($row['ModuleType'] == 'extra') {
-                $structureHandled[$i][$j]['Title'] = 'Добор баллов';
-                $maxRate += $row['MaxRate'];
-            }
-            else {
-                if ($try === 0)
-                    $structureHandled[$i][$j]['Title'] = 'Основная сдача';
-                else
-                    $structureHandled[$i][$j]['Title'] = 'Пересдача '.$try;
-                $try++;
-            }
-            //$structureHandled[$i][$j]['Description'] = $row['SubmoduleDescription']; // Описание
-            $structureHandled[$i][$j]['MaxRate'] = (int) $row['MaxRate'];
-           
-        }
-        $structureHandled['ModulesCount'] = $i;
-        $structureHandled['MaxRate'] = (int) $maxRate; 
-        // end Шапка таблицы
-
-        // Студенты и их баллы по экзамену
+        // Студенты и их баллы
         $students = $this->model_rating->GetStudentsForRating($this->UserInfo['TeacherID'], $id);
-
         $rateHandled = array();
         $groupsHandled = array();
-        $i_g = 0; 
-        $i_s = 0; 
-        $curGroupID = 0;
+        $i_g = $i_s = $curGroup = 0;
 
         foreach($students as $row) {
-            // Группа 
-            if ($curGroupID !== $row['GroupID']) {
-                $curGroupID = $row['GroupID'];
+            
+            if ($curGroup !== $row['GroupID']) {
+                $curGroup = $row['GroupID'];
                 $i_g++;
-                $rateHandled[$i_g]['GroupID'] = $row['GroupID'];
-                $rateHandled[$i_g]['GroupNum'] = $row['GroupNum']; // в„– РіСЂСѓРїРїС‹
-                $rateHandled[$i_g]['GradeNum'] = $row['GradeNum']; // в„– РєСѓСЂСЃР°
-                $rateHandled[$i_g]['isAttached'] = $row['isAttached']; // #1
-
-                if ($row['Degree'] == 'bachelor')
-                    $rateHandled[$i_g]['GradeTitle'] = $row['GradeNum'].' РєСѓСЂСЃ';
-
-                if ($row['Degree'] == 'specialist')
-                    $rateHandled[$i_g]['GradeTitle'] = $row['GradeNum'].' РєСѓСЂСЃ';
-
-                if ($row['Degree'] == 'master')
-                    $rateHandled[$i_g]['GradeTitle'] = 'Магистратура, '.$row['GradeNum'].' год';
-
-                $groupsHandled[$curGroupID] = $rateHandled[$i_g]['GradeTitle']." ".$row['GroupNum']." РіСЂСѓРїРїР°"; 
                 $i_s = 0;
+                $rateHandled[$i_g] = $this->processGroupInfo($row);
+                $groupsHandled[$curGroup] = $rateHandled[$i_g]['GroupTitle'];
             }
-
-            // Студенты   
-            $curStudent = array();
-            $curStudent['ID'] = $row['ID'];
-            $curStudent['Last'] = $row['Last'];
-            $curStudent['First'] = $row['First'];
-            $curStudent['Second'] = $row['Second'];
-            $curStudent['isAttached'] = $row['isAttached']; // #1
+            
+            // Студенты
+            $curStudent = $row; //ID, Name, isAttached
             $curStudent['RateResult'] = 0;
-
+            
             // Баллы студента
-            $i_r = 0;
-            $extraNum = 0;
-            $rate = $this->model_rating->getMapForStudentExam($row['ID'], $id);
-
-            $lastExam = -1;
-            $lastExtra = -1;
-            $lastNilExam = -1;
-            $lastNilExtra = -1;
-			
-			$curStudent['RateSemesterResult'] = 0;
-
-            foreach($rate as $r) {
+            if ($page_type == "rating") {
+                $rates_raw = $this->model_rating->getMapForStudent($row['ID'], $id);
+                $rates = $this->getRatesForRatingPage($rates_raw);
+                $curStudent['RateResult'] = $rates['RateResult'];
+                unset($rates['RateResult']);
+                $curStudent['Rates'] = $rates;
+                $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
                 
-				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'];
-				}
-				
-				switch ($r['ModuleType'])
-				{
-				case 'regular':
-					$curStudent['RateSemesterResult'] += $r['Rate'];
-					break;
-				case 'exam':
-					if (!is_null($r['Rate'])) {
-						if ($lastExam >= 0) {
-	    					$curStudent['Rates'][$lastExam]['Block'] = 'True';
-	        			}
-	        			$lastExam = $i_r;
-					} else {
-						if ($lastNilExam < 0) {
-							$lastNilExam = $i_r;
-						} else {
-							$curStudent['Rates'][$i_r]['Block'] = 'True';
-						}
-					}
-					break;
-				case 'bonus':
-					$curStudent['Bonus'] = $r['Rate'];
-					break;
-				case 'extra':
-					if (!is_null($r['Rate'])) {
-						if ($lastExtra >= 0) {
-	    					$curStudent['Rates'][$lastExtra]['Block'] = 'True';
-	        			}
-	        			$lastExtra = $i_r;
-	        			$curStudent['Rate'] += $r['Rate'];
-					} else {
-						if ($lastNilExtra < 0) {
-							$lastNilExtra = $i_r;
-						} else {
-							$curStudent['Rates'][$i_r]['Block'] = 'True';
-						}
-					}
-					break;
-				default:
-					throw HTTP_Exception::factory (500, "Некорректный тип модуля!");
-				}
-				
-                $i_r++;
+            } else {
+                $rate = $this->model_rating->getMapForStudentExam($row['ID'], $id);
+                $this->getRatesForExamPage($curStudent, $rate, $disciplineInfo['ExamType'], $id);
+                $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
             }
-
-            $total = $this->model_rating->GetStudentRate($row['ID'], $id);
-            $total = $total[0]['Num'];
-            if (is_null($total)) $total = 0;
-            
-            if ($lastExam >= 0) {
-        		$curStudent['RateResult'] += $curStudent['Rates'][$lastExam]['Rate'];
-            }
-            
-            $curStudent['RateResult'] = $total;
-
-            $bottomLimit = 0;
-            $topLimit = ($disciplineInfo['ExamType'] == 'exam') ? 38 : 60;
-
-            $max_extra_rate = 0;
-            if ($curStudent['RateSemesterResult'] >= $bottomLimit && 
-            	$curStudent['RateSemesterResult'] < $topLimit) // студент задолженик
-            {
-            	$max_extra_rate = $topLimit - $curStudent['RateSemesterResult'];
-        	}
-        	if ($lastExtra >= 0) {
-	            $curStudent['Rates'][$lastExtra]['MaxRate'] = $max_extra_rate;
-            } 
-            if ($lastNilExtra >= 0) {
-    			$curStudent['Rates'][$lastNilExtra]['MaxRate'] = $max_extra_rate;
-        	}
-
-            $rateHandled[$i_g]['Students'][$i_s] = $curStudent;
             $i_s++;
         }
-
+        
         // На вывод
-	    $twig->headerRate = $structureHandled;
+        $twig->User = $this->UserInfo;
+        $twig->headerRate = $structureHandled; // Шапка таблицы: структура УКД (модули и мероприятия)
         $twig->rateTable = $rateHandled;
         $twig->groups = $groupsHandled;
-
         $twig->disciplineInfo = $disciplineInfo;
         $twig->disciplineInfo_JSON = json_encode($twig->disciplineInfo);
-
         $this->response->body($twig);
+        
+    }
+
+    
+    
+    // Страница оценивания в течение семестра
+    public function action_edit() {
+        $this->stub_action("rating");
+    }
+
+    // Страница оценивания в сессию
+    public function action_exam() {
+        $this->stub_action("exam");
     }
 }
             
\ No newline at end of file
diff --git a/~dev_rating/application/views/teacher/rating.twig b/~dev_rating/application/views/teacher/rating.twig
index d222de4be..2c33cb211 100644
--- a/~dev_rating/application/views/teacher/rating.twig
+++ b/~dev_rating/application/views/teacher/rating.twig
@@ -22,7 +22,7 @@
                 Выставление баллов за экзамен возможно только на странице "Сессия".
             </p>
 		{% endif %}
-	<h2 class="h2_titleSubject">{{ SubjectName }}</h2>
+	<h2 class="h2_titleSubject">{{ disciplineInfo.SubjectName }}</h2>
 
 	<button class="downloadExcel" style="display: none">Скачать в excel формате [dev version]</button>
 
-- 
GitLab