From 4436e76de413d7ad145a0815bc0cb4da752b48ed Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Sun, 21 Dec 2014 22:25:26 +0300
Subject: [PATCH] change discipline control type: extra

---
 db/StoredProcedures.sql | 46 ++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 24 deletions(-)

diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index 1744c5924..f1a00280e 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -2312,7 +2312,7 @@ CREATE FUNCTION `ChangeDisciplineControl`	(	`TeacherID` 	INT,
 							 				) 	RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker, extraMax INT;  					 
+    DECLARE checker, extraMax, vExtraID INT;  					 
 	
 	IF 	InternalIsMapLocked(DisciplineID) OR
 		NOT InternalIsTeacherAuthor(TeacherID,DisciplineID)
@@ -2327,9 +2327,25 @@ BEGIN
 	WHERE disciplines.ID = DisciplineID
 	LIMIT 1;
 
+	-- get Extra module id
+	SET vExtraID = -1;
+	SELECT modules.ID
+	INTO vExtraID
+	FROM `modules`
+    WHERE 	modules.Type = 4 AND 
+			modules.DisciplineID = DisciplineID
+	LIMIT 1;
+	IF vExtraID <= 0 THEN
+			RETURN -1;
+	END IF;
+
+
 	IF 	(checker != ExamType) 		
 	THEN
 		IF ExamType = 'exam' THEN
+
+			SET extraMax = 6;
+
 			SET checker = GetDisciplineMaxRate(DisciplineID);
 			IF checker >= 61 THEN
 				RETURN 1;
@@ -2338,26 +2354,14 @@ BEGIN
 			
 			-- delete extra module from 
 			DELETE FROM `submodules`
-			WHERE submodules.ID IN 
-		 		(
-					SELECT submodules.ID 
-					FROM `modules` 
-					LEFT JOIN `submodules` ON submodules.ModuleID = modules.ID
-					WHERE modules.Type = 4 and modules.DisciplineID = DisciplineID and submodules.OrderNum > 1
-	 			);
-			SET extraMax = 6;
+			WHERE 	submodules.OrderNum > 1 AND
+					submodules.ModuleID = vExtraID;
+			
 		ELSE
 			SET extraMax = 30;
 			
 			SET checker = DeleteModuleExam(TeacherID, DisciplineID);
-			
-			
-			SELECT AddSubmodule(disciplines.AuthorID, modules.ID, extraMax, '', NULL, 'LandmarkControl')
-			INTO checker
-			FROM `modules`
-			INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID
-            WHERE 	modules.Type = 4 AND 
-					modules.DisciplineID = DisciplineID;		
+			SET checker = AddSubmodule(TeacherID, vExtraID, extraMax, '', NULL, 'LandmarkControl');	
 			
 		END IF;
 
@@ -2367,13 +2371,7 @@ BEGIN
 
 		UPDATE `submodules`
 		SET submodules.MaxRate = extraMax
-		WHERE submodules.ModuleID IN 
-			(
-				SELECT modules.ID
-				FROM `modules`
-				WHERE 	modules.DisciplineID = DisciplineID AND
-						modules.Type = 4
-			);
+		WHERE submodules.ModuleID = vExtraID;
 	END IF;
 	RETURN 0;
 END //
-- 
GitLab