From fbd4ed8d71dfafc28119fdeb261fed9f74fa2d6b Mon Sep 17 00:00:00 2001
From: RomanSteinberg <romanofficial@yandex.ru>
Date: Mon, 12 Jan 2015 18:05:02 +0300
Subject: [PATCH] FIX: allow user to set exam ratings

---
 db/StoredProcedures.sql                       |  3 +-
 .../classes/Controller/Handler.php            |  2 +-
 .../Controller/Handler/FileCreator.php        | 48 ++++++++++++-------
 .../application/views/teacher/exam.twig       |  4 +-
 .../application/views/teacher/rating.twig     |  7 ++-
 5 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index 16c39a0c0..215971f8f 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -4126,7 +4126,8 @@ BEGIN
 			students.FirstName			AS 'First',
 			students.SecondName 		AS 'Second',
 			GetRateForDiscSemester(students.ID, DisciplineID) AS 'intermediate',
-			GetRateForDiscBonus(students.ID, DisciplineID) AS 'bonus'
+			GetRateForDiscBonus(students.ID, DisciplineID) AS 'bonus',
+			GetRateForDiscExam(students.ID, DisciplineID) AS 'exam'
 	FROM `students`
 	INNER JOIN `study_groups` ON study_groups.ID = students.StudyGroupID
 	-- LEFT JOIN `modules` ON 	modules.DisciplineID = DisciplineID AND
diff --git a/~dev_rating/application/classes/Controller/Handler.php b/~dev_rating/application/classes/Controller/Handler.php
index 5fb6b4461..d36405de5 100644
--- a/~dev_rating/application/classes/Controller/Handler.php
+++ b/~dev_rating/application/classes/Controller/Handler.php
@@ -16,7 +16,7 @@ class Controller_Handler extends Controller {
         if(!$this->request->is_ajax() && !$isDownload)
         {
             // Перенаправляем на ошибку доступа
-            throw HTTP_Exception::factory (403);   
+            throw HTTP_Exception::factory (403, "У Вас нет разрешения скачивать файлы!");
         }
         
         // Обработка POST-данных
diff --git a/~dev_rating/application/classes/Controller/Handler/FileCreator.php b/~dev_rating/application/classes/Controller/Handler/FileCreator.php
index 10c703f18..1db8ad75e 100644
--- a/~dev_rating/application/classes/Controller/Handler/FileCreator.php
+++ b/~dev_rating/application/classes/Controller/Handler/FileCreator.php
@@ -215,12 +215,14 @@ class Controller_Handler_FileCreator extends Controller_Handler
         $startRow = 12;
         $rowNumber = $startRow;
         $index = 1;
+		
+		$examHold = $this->checkExamIsHold($result);
 
         foreach($result as $studentInfo){
-            $this->addStudentToSheet($objPHPExcel, $type, $studentInfo, $rowNumber, $index, $type);
+		    $this->addStudentToSheet($objPHPExcel, $type, $studentInfo, $rowNumber, $index, $examHold);
             $rowNumber++;
             $index++;				
-        }    
+        }
     }
 
     protected function prepareSheetHeader(&$objPHPExcel, $disciplineType, $data) {
@@ -263,13 +265,13 @@ class Controller_Handler_FileCreator extends Controller_Handler
         $sheet->setCellValue("$range", $controlDate);
     }
 
-    protected function addStudentToSheet(&$objPHPExcel, $disciplineType, $data, $row, $index)
+    protected function addStudentToSheet(&$objPHPExcel, $disciplineType, $data, $row, $index, $examHold1)
     {
         $sheet = $objPHPExcel->getActiveSheet();
         if ($disciplineType == 'credit') {
             $this->addStudentInfoForCreditToSheet($sheet, $data, $row, $index);
         } elseif ($disciplineType == 'exam') {
-            $this->addStudentInfoForExamToSheet($sheet, $data, $row, $index);
+		    $this->addStudentInfoForExamToSheet($sheet, $data, $row, $index, $examHold1);
         }
     }
 
@@ -316,7 +318,7 @@ class Controller_Handler_FileCreator extends Controller_Handler
                     ->setCellValue("J".$row, $tempStr);
     }
 
-    protected function addStudentInfoForExamToSheet(&$sheet, $data, $row, $index)
+    protected function addStudentInfoForExamToSheet(&$sheet, $data, $row, $index, $examHold)
     {
         $indPosition = 'A'; // Номер
         $namePosition = 'B';  // ФИО
@@ -351,14 +353,16 @@ class Controller_Handler_FileCreator extends Controller_Handler
         $secondName = $data['Second'];
         $rate = (int)$data['intermediate'];
         $bonus = (int)$data['bonus'];
-        $examRateValue = 0; // TODO: get it from DB
+        $examRateValue = (int)$data['exam'];
+		if ($examRateValue < 0)
+			$examRateValue = 0;
         $fullName = $lastName." ".$firstName." ".$secondName;
         $totalRateValue = $rate + $examRateValue + $bonus;
 
         if ($totalRateValue > 100)
             $totalRateValue = 100;
 
-        list($totalRate, $rateOfFive, $examRate) = $this->formRateOfFive($rate, $examRateValue, $totalRateValue);
+        list($totalRate, $rateOfFive, $examRate) = $this->formRateOfFive($rate, $examRateValue, $totalRateValue, $examHold);
 
         $sheet  ->setCellValue($indPosition.$row, $index)
             ->setCellValue($namePosition.$row, $fullName)
@@ -464,26 +468,34 @@ class Controller_Handler_FileCreator extends Controller_Handler
         $this->response->headers("Content-type", " application/vnd.ms-excel" );
         $this->response->headers("Content-Disposition", "attachment; filename=".$filename.".xls" );
     }
-
-    /**
-     * @param $rate
-     * @param $examRateValue
-     * @param $totalRateValue
-     * @return array
-     */
-    protected function formRateOfFive($rate, $examRateValue, $totalRateValue)
+	
+	protected function checkExamIsHold(&$studentsRates) 
+	{
+		foreach($studentsRates as $studentInfo){
+            if ($studentInfo['exam'] >= 0)
+				return 1;
+        } 
+		return 0;
+	}
+
+    // Определяет оценку по пятибальной системе
+    protected function formRateOfFive($rate, $examRateValue, $totalRateValue, $examHold)
     {
         $totalRate = '';
         $rateOfFive = '';
-        $examRate = '';
+		$examRate = '';
+        if ($examHold != 0) 
+			$examRate = $examRateValue;
+			
         if ($rate < 38) {
             $totalRate = ' ';
             $rateOfFive = 'неуд';
-            $examRate = '0';
+			if ($examHold == 0) 
+				$examRate = '0';
         } elseif (($rate >= 38) and ($examRateValue >= 22)) {
             $totalRate = $totalRateValue;
             $rateOfFive = 'СѓРґРѕРІР»';
-            if (($totalRateValue >= 75) and ($totalRateValue < 85))
+            if (($totalRateValue >= 71) and ($totalRateValue < 85))
                 $rateOfFive = 'С…РѕСЂ';
             elseif ($totalRateValue >= 85)
                 $rateOfFive = 'отл';
diff --git a/~dev_rating/application/views/teacher/exam.twig b/~dev_rating/application/views/teacher/exam.twig
index 53ead1503..ee45f1d58 100644
--- a/~dev_rating/application/views/teacher/exam.twig
+++ b/~dev_rating/application/views/teacher/exam.twig
@@ -81,7 +81,7 @@
 					<span class="downloadExcelStatement" id="group_{{ group.GroupID }}">Скачать ведомость</span>
 				</td>
 			</tr>
-{# 
+ 
 			{% for student in group.Students %}
 				{% set row = row + 1 %}
 				{% set col = 0 %}
@@ -114,7 +114,7 @@
 					<td class="rateResultCell staticCell">{{ student.RateResult }}</td>
 				</tr>
 			{% endfor %} 
- #}
+ 
 
 		{% endfor %}
 	</table>
diff --git a/~dev_rating/application/views/teacher/rating.twig b/~dev_rating/application/views/teacher/rating.twig
index 88ec4e30a..08b19cd6c 100644
--- a/~dev_rating/application/views/teacher/rating.twig
+++ b/~dev_rating/application/views/teacher/rating.twig
@@ -16,7 +16,12 @@
             <p class="canNotEdit">
                 Семестр завершен. Выставление баллов <u>запрещено</u>.
             </p>
-	{% endif %}
+		{% endif %}
+		{% if disciplineInfo.ExamType == 'exam' %}
+			<p class="canNotEdit">
+                Выставление баллов за экзамен возможно только на странице "Сессия".
+            </p>
+		{% endif %}
 	<h2 class="h2_titleSubject">{{ SubjectName }}</h2>
 
 	<button class="downloadExcel" style="display: none">Скачать в excel формате [dev version]</button>
-- 
GitLab