From 8fe3059593bfcc311fea345b6797a9d552d23eed Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Fri, 6 Feb 2015 01:20:34 +0300
Subject: [PATCH] REF: views + 1st revision

---
 db/StoredProcedures.sql | 6530 +++++++++++++++++++--------------------
 db/Views.sql            |  147 +
 2 files changed, 3374 insertions(+), 3303 deletions(-)
 create mode 100644 db/Views.sql

diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index 327f847f5..95f008e62 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -1,423 +1,416 @@
 DELIMITER //
-USE mmcs_rating//
 
--- -------------------------------------------------------------------------------------------
--- Label: abbreviations
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: abbreviations
+# -------------------------------------------------------------------------------------------
 
--- abbreviation: abbr
--- specialization: spec
--- department: dep
+# abbreviation: abbr
+# specialization: spec
+# department: dep
 
--- -------------------------------------------------------------------------------------------
--- Label: internals
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: internals
+# -------------------------------------------------------------------------------------------
 
--- actually check for first scoring, in this case you cannot yet edit discipline
--- "SetRate" stored procedure can change isLocked flag
+# actually check for first scoring, in this case you cannot yet edit discipline
+# "SetRate" stored procedure can change isLocked flag
 DROP FUNCTION IF EXISTS InternalIsMapLocked//
-CREATE FUNCTION `InternalIsMapLocked`	( 	`DisciplineID` INT 
-										) 	RETURNS boolean
+CREATE FUNCTION `InternalIsMapLocked`
+(`disciplineID` INT) RETURNS boolean
     NO SQL
 BEGIN  
-	DECLARE checker boolean;
-	SET checker = -1;
-	SELECT disciplines.isLocked
-	INTO checker
-	FROM `disciplines`
-	WHERE DisciplineID = disciplines.ID
-	LIMIT 1;
-	RETURN  ( checker > 0 );
+    DECLARE checker INT DEFAULT -1;
+
+    SELECT disciplines.isLocked
+        INTO checker
+        FROM `disciplines`
+        WHERE disciplines.ID = disciplineID
+        LIMIT 1;
+    RETURN  ( checker > 0 );
 END //
 
 
--- check, that student really take this course
+# check, that student really take this course
 DROP FUNCTION IF EXISTS InternalIsStudentAttached//
-CREATE FUNCTION `InternalIsStudentAttached`	( 	`StudentID` INT,
-												`DisciplineID` INT 
-											) 	RETURNS boolean
+CREATE FUNCTION `InternalIsStudentAttached`
+(`StudentID` INT, `DisciplineID` INT) RETURNS boolean
     NO SQL
 BEGIN 
-	DECLARE checker boolean;
-
-	-- 1. student in main group and not detached OR
-	-- 2. student attached
-	SELECT	(	(	disciplines_students.Type IS NOT NULL AND disciplines_students.Type = 'attach' ) OR
-				(	disciplines_groups.StudyGroupID IS NOT NULL AND 
-					disciplines_groups.StudyGroupID = study_groups.ID AND
-					( disciplines_students.Type IS NULL OR disciplines_students.Type != 'detach' )
-				)
-			)
-	INTO checker
-	FROM `students`
-	INNER JOIN 	`study_groups` 			ON 	students.StudyGroupID = study_groups.ID
-	LEFT JOIN 	`disciplines_students` 	ON 	StudentID = disciplines_students.StudentID AND
-											DisciplineID = disciplines_students.DisciplineID
-	LEFT JOIN 	`disciplines_groups`	ON 	study_groups.ID = disciplines_groups.StudyGroupID AND
-											DisciplineID = disciplines_groups.DisciplineID
-	WHERE students.ID = StudentID
-	LIMIT 1;
-
-	RETURN (checker IS NOT NULL AND checker);  
+    DECLARE checker boolean;
+
+    # 1. student in main group and not detached OR
+    # 2. student attached
+    SELECT  (   (   disciplines_students.Type IS NOT NULL AND disciplines_students.Type = 'attach' ) OR
+                (   disciplines_groups.StudyGroupID IS NOT NULL AND 
+                    disciplines_groups.StudyGroupID = study_groups.ID AND
+                    ( disciplines_students.Type IS NULL OR disciplines_students.Type != 'detach' )
+                )
+            )
+    INTO checker
+    FROM `students`
+    INNER JOIN  `study_groups`          ON  students.StudyGroupID = study_groups.ID
+    LEFT JOIN   `disciplines_students`  ON  StudentID = disciplines_students.StudentID AND
+                                            DisciplineID = disciplines_students.DisciplineID
+    LEFT JOIN   `disciplines_groups`    ON  study_groups.ID = disciplines_groups.StudyGroupID AND
+                                            DisciplineID = disciplines_groups.DisciplineID
+    WHERE students.ID = StudentID
+    LIMIT 1;
+
+    RETURN (checker IS NOT NULL AND checker);  
 END //
 
 
--- check, that teacher teach this course
-DROP FUNCTION IF EXISTS InternalIsTeacherBinded//
-CREATE FUNCTION `InternalIsTeacherBinded`	( 	`TeacherID` INT,
-												`DisciplineID` INT 
-											) 	RETURNS boolean
+# check, that teacher teach this course
+drop function if exists InternalIsTeacherBinded//
+CREATE FUNCTION `InternalIsTeacherBinded` (   `TeacherID` INT,
+                                                `DisciplineID` INT 
+                                            )   RETURNS boolean
     NO SQL
 BEGIN  
-	DECLARE checker boolean;
-	SET checker = FALSE;
-	SELECT (disciplines_teachers.ID IS NOT NULL AND disciplines_teachers.ID > 0) 
-	INTO checker
-	FROM `disciplines_teachers`
-	WHERE 	TeacherID = disciplines_teachers.TeacherID AND 
-			DisciplineID = disciplines_teachers.DisciplineID
-	LIMIT 1;
-	RETURN (checker IS NOT NULL AND checker); 
+    DECLARE checker boolean DEFAULT FALSE;
+    
+    SELECT (disciplines_teachers.ID IS NOT NULL AND disciplines_teachers.ID > 0) 
+    INTO checker
+    FROM `disciplines_teachers`
+    WHERE   TeacherID = disciplines_teachers.TeacherID AND 
+            DisciplineID = disciplines_teachers.DisciplineID
+    LIMIT 1;
+    RETURN (checker IS NOT NULL AND checker); 
 END //
 
 
 DROP FUNCTION IF EXISTS InternalIsTeacherAuthor//
-CREATE FUNCTION `InternalIsTeacherAuthor`	( 	`TeacherID` INT,
-												`DisciplineID` INT 
-											) 	RETURNS boolean
+CREATE FUNCTION `InternalIsTeacherAuthor`   (   `TeacherID` INT,
+                                                `DisciplineID` INT 
+                                            )   RETURNS boolean
     NO SQL
 BEGIN  
-	DECLARE checker boolean;
-	SELECT 	(TeacherID = disciplines.AuthorID) 
-	INTO checker
-	FROM `disciplines`
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-	RETURN (checker IS NOT NULL AND checker);  
+    DECLARE checker boolean;
+    SELECT  (TeacherID = disciplines.AuthorID) 
+    INTO checker
+    FROM `disciplines`
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+    RETURN (checker IS NOT NULL AND checker);  
 END //
 
 
 DROP FUNCTION IF EXISTS GetDisciplineMaxRate//
-CREATE FUNCTION `GetDisciplineMaxRate`		( 	`DisciplineID` INT 
-											) 	RETURNS int(11)
+CREATE FUNCTION `GetDisciplineMaxRate`      (   `DisciplineID` INT 
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE vMax INT;
-	SET vMax = 0;
-
-	-- discipline map consist of submodules, we sum all their max rates 
-	SELECT SUM(submodules.MaxRate)
-	INTO vMax
-	FROM `submodules`
-	INNER JOIN `modules` 	ON 	submodules.ModuleID = modules.ID AND
-														(modules.Type = 'regular' OR modules.Type = 'exam')
-	WHERE DisciplineID = modules.DisciplineID;
-			
-	RETURN vMax;
+    DECLARE vMax INT;
+    SET vMax = 0;
+
+    # discipline map consist of submodules, we sum all their max rates 
+    SELECT SUM(submodules.MaxRate)
+    INTO vMax
+    FROM `submodules`
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID AND
+                                                        (modules.Type = 'regular' OR modules.Type = 'exam')
+    WHERE DisciplineID = modules.DisciplineID;
+            
+    RETURN vMax;
 END //
 
 
 DROP FUNCTION IF EXISTS GetRateForDisc//
-CREATE FUNCTION `GetRateForDisc`	(	`StudentID` INT,  
-								`DisciplineID` INT
-							) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDisc`    (   `StudentID` INT,  
+                                `DisciplineID` INT
+                            )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-	SELECT SUM(rating_table.Rate) 
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID AND 
-								DisciplineID = modules.DisciplineID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			(	modules.Type != 'exam' OR
-				submodules.ID = 
-					(	SELECT submodules.ID
-						FROM `submodules`
-						INNER JOIN `rating_table` ON rating_table.SubModuleID = submodules.ID
-						WHERE 	submodules.ModuleID = modules.ID AND
-								rating_table.StudentID = StudentID
-						ORDER BY submodules.OrderNum DESC
-						LIMIT 1
-					) 
-			)
-	LIMIT 1;
-
-	RETURN 	rate;
+    DECLARE rate INT;
+    SET rate = -1;
+
+    SELECT SUM(rating_table.Rate) 
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID AND 
+                                DisciplineID = modules.DisciplineID
+    WHERE   rating_table.StudentID = StudentID AND 
+            (   modules.Type != 'exam' OR
+                submodules.ID = 
+                    (   SELECT submodules.ID
+                        FROM `submodules`
+                        INNER JOIN `rating_table` ON rating_table.SubModuleID = submodules.ID
+                        WHERE   submodules.ModuleID = modules.ID AND
+                                rating_table.StudentID = StudentID
+                        ORDER BY submodules.OrderNum DESC
+                        LIMIT 1
+                    ) 
+            )
+    LIMIT 1;
+
+    RETURN  rate;
 END //
 
 
 DROP FUNCTION IF EXISTS GetRateForDiscSemester//
-CREATE FUNCTION `GetRateForDiscSemester`	(	`StudentID` INT,
-												`DisciplineID` INT
-											) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDiscSemester`    (   `StudentID` INT,
+                                                `DisciplineID` INT
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-	SELECT SUM(rating_table.Rate)
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			DisciplineID = modules.DisciplineID AND
-			modules.Type = 'regular'
-	LIMIT 1;
-
-	RETURN 	rate;
+    DECLARE rate INT;
+    SET rate = -1;
+
+    SELECT SUM(rating_table.Rate)
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID
+    WHERE   rating_table.StudentID = StudentID AND 
+            DisciplineID = modules.DisciplineID AND
+            modules.Type = 'regular'
+    LIMIT 1;
+
+    RETURN  rate;
 END //
 
 
 DROP FUNCTION IF EXISTS GetRateForDiscBonus//
-CREATE FUNCTION `GetRateForDiscBonus`	(	`StudentID` INT,  
-											`DisciplineID` INT
-										) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDiscBonus`   (   `StudentID` INT,  
+                                            `DisciplineID` INT
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-
-	SELECT SUM(rating_table.Rate)
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			DisciplineID = modules.DisciplineID AND
-			modules.Type = 'bonus'
-	LIMIT 1;
+    DECLARE rate INT;
+    SET rate = -1;
+
+
+    SELECT SUM(rating_table.Rate)
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID
+    WHERE   rating_table.StudentID = StudentID AND 
+            DisciplineID = modules.DisciplineID AND
+            modules.Type = 'bonus'
+    LIMIT 1;
 
-	RETURN 	rate;
+    RETURN  rate;
 END //
 
 
 
 
 DROP FUNCTION IF EXISTS GetRateForDiscExam//
-CREATE FUNCTION `GetRateForDiscExam`	(	`StudentID` INT,  
-											`DisciplineID` INT
-										) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDiscExam`    (   `StudentID` INT,  
+                                            `DisciplineID` INT
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-
-	SELECT rating_table.Rate
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			DisciplineID = modules.DisciplineID AND
-			modules.Type = 'exam'
-	ORDER BY submodules.OrderNum DESC
-	LIMIT 1;
-
-	RETURN 	rate;
+    DECLARE rate INT;
+    SET rate = -1;
+
+
+    SELECT rating_table.Rate
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID
+    WHERE   rating_table.StudentID = StudentID AND 
+            DisciplineID = modules.DisciplineID AND
+            modules.Type = 'exam'
+    ORDER BY submodules.OrderNum DESC
+    LIMIT 1;
+
+    RETURN  rate;
 END //
 
 
 DROP FUNCTION IF EXISTS GetRateForDiscExamNum//
-CREATE FUNCTION `GetRateForDiscExamNum`	(	`StudentID` INT,  
-												`DisciplineID` INT,
-												`OrderNum` INT
-											) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDiscExamNum` (   `StudentID` INT,  
+                                                `DisciplineID` INT,
+                                                `OrderNum` INT
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-
-	SELECT rating_table.Rate
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			DisciplineID = modules.DisciplineID AND
-			modules.Type = 'exam' AND
-			submodules.OrderNum = OrderNum
-	LIMIT 1;
-
-	RETURN 	rate;
+    DECLARE rate INT;
+    SET rate = -1;
+
+
+    SELECT rating_table.Rate
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID
+    WHERE   rating_table.StudentID = StudentID AND 
+            DisciplineID = modules.DisciplineID AND
+            modules.Type = 'exam' AND
+            submodules.OrderNum = OrderNum
+    LIMIT 1;
+
+    RETURN  rate;
 END //
 
 
 DROP FUNCTION IF EXISTS GetRateForDiscExtra//
-CREATE FUNCTION `GetRateForDiscExtra`	(	`StudentID` INT,  
-											`DisciplineID` INT,
-											`OrderNum` INT
-										) 	RETURNS int(11)
+CREATE FUNCTION `GetRateForDiscExtra`   (   `StudentID` INT,  
+                                            `DisciplineID` INT,
+                                            `OrderNum` INT
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE rate INT;
-	SET rate = -1;
-
-
-	SELECT rating_table.Rate
-	INTO rate
-	FROM `rating_table`
-	INNER JOIN `submodules` ON 	rating_table.SubmoduleID = submodules.ID
-	INNER JOIN `modules`	ON 	submodules.ModuleID = modules.ID
-	WHERE 	rating_table.StudentID = StudentID AND 
-			DisciplineID = modules.DisciplineID AND
-			modules.Type = 'extra' AND
-			submodules.OrderNum = OrderNum
-	LIMIT 1;
-
-	RETURN 	rate;
+    DECLARE rate INT;
+    SET rate = -1;
+
+
+    SELECT rating_table.Rate
+    INTO rate
+    FROM `rating_table`
+    INNER JOIN `submodules` ON  rating_table.SubmoduleID = submodules.ID
+    INNER JOIN `modules`    ON  submodules.ModuleID = modules.ID
+    WHERE   rating_table.StudentID = StudentID AND 
+            DisciplineID = modules.DisciplineID AND
+            modules.Type = 'extra' AND
+            submodules.OrderNum = OrderNum
+    LIMIT 1;
+
+    RETURN  rate;
 END //
 
 
 
 
 
--- check, if any module is created
+# check, if any module is created
 DROP FUNCTION IF EXISTS InternalIsMapCreated//
-CREATE FUNCTION `InternalIsMapCreated`		( 	`DisciplineID` INT 
-											) 	RETURNS int(11)
+CREATE FUNCTION `InternalIsMapCreated`      (   `DisciplineID` INT 
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE res INT;
+    DECLARE res INT;
 
-	SELECT SUM(submodules.MaxRate) 
-	INTO res
-	FROM `modules`
-	LEFT JOIN `submodules` ON 	submodules.ModuleID = modules.ID
-	WHERE 	modules.DisciplineID = DisciplineID AND
-			(modules.Type = 'regular' OR ( modules.Type = 'exam' AND submodules.OrderNum = 1))
-	LIMIT 1;
+    SELECT SUM(submodules.MaxRate) 
+    INTO res
+    FROM `modules`
+    LEFT JOIN `submodules` ON   submodules.ModuleID = modules.ID
+    WHERE   modules.DisciplineID = DisciplineID AND
+            (modules.Type = 'regular' OR ( modules.Type = 'exam' AND submodules.OrderNum = 1))
+    LIMIT 1;
 
 
-	RETURN (res = 100);
+    RETURN (res = 100);
 END //
 
 
 
--- set notification flag
+# set notification flag
 DROP FUNCTION IF EXISTS InternalNotify//
-CREATE FUNCTION `InternalNotify`		( 	`AccountID` INT 
-										) 	RETURNS int(11)
+CREATE FUNCTION `InternalNotify`        (   `AccountID` INT 
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN  
-	UPDATE `accounts`
-	SET accounts.Notification = 1
-	WHERE accounts.ID = AccountID
-	LIMIT 1;
-	RETURN 0;
+    UPDATE `accounts`
+    SET accounts.Notification = 1
+    WHERE accounts.ID = AccountID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: preferences
--- Label: magic
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: preferences
+# Label: magic
+# -------------------------------------------------------------------------------------------
 
 
 DROP FUNCTION IF EXISTS SetHashKey//
-CREATE FUNCTION `SetHashKey`	( 	`Value` VARCHAR(300) charset utf8
-								) 	RETURNS int(11)
+CREATE FUNCTION `SetHashKey` 
+    (`pValue` VARCHAR(300) charset utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	UPDATE `general_settings`
-	SET general_settings.ValS = Value
-	WHERE general_settings.ID = 2
-	LIMIT 1;
-	RETURN 0;
+    UPDATE `general_settings`
+        SET general_settings.ValS = pValue
+        WHERE general_settings.ID = 2
+        LIMIT 1;
+    RETURN 0;
 END //
 
 
-
 DROP FUNCTION IF EXISTS GetHashKey//
-CREATE FUNCTION `GetHashKey`	( ) RETURNS varchar(300) CHARSET utf8
+CREATE FUNCTION `GetHashKey` ( ) RETURNS varchar(300) CHARSET utf8
     NO SQL
 BEGIN
-	RETURN 	(	SELECT general_settings.ValS
-	        	FROM `general_settings`
-	        	WHERE general_settings.ID = 2
-	        	LIMIT 1
-	       	);
+    RETURN( SELECT general_settings.ValS
+                FROM `general_settings`
+                WHERE general_settings.ID = 2
+                LIMIT 1);
 END //
 
 
 
 DROP FUNCTION IF EXISTS SetBitmaskByPagename//
-CREATE FUNCTION `SetBitmaskByPagename`	( 	`Pagename` TEXT CHARSET utf8,
-											`Mask` INT
-										) 	RETURNS int(11)
+CREATE FUNCTION `SetBitmaskByPagename` 
+    (`pPagename` TEXT CHARSET utf8, `pMask` INT) RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-	SELECT page_access.ID
-	INTO checker
-	FROM `page_access`
-	WHERE page_access.Pagename = Pagename
-	LIMIT 1;
-
-	IF checker > 0 THEN
-		UPDATE `page_access`
-		SET page_access.Bitmask = Mask
-		WHERE page_access.Pagename = Pagename
-		LIMIT 1;
-	ELSE
-		INSERT INTO `page_access`	
-				(page_access.Pagename, page_access.Bitmask)
-		VALUES	(Pagename, Mask);
-	END IF;
-
-	RETURN 0;
+    DECLARE vChecker INT DEFAULT -1;
+    SELECT page_access.ID
+        INTO vChecker
+        FROM `page_access`
+        WHERE page_access.Pagename = pPagename
+        LIMIT 1;
+
+    IF vChecker > 0 THEN
+        UPDATE `page_access`
+            SET page_access.Bitmask = pMask
+            WHERE page_access.Pagename = pPagename
+            LIMIT 1;
+    ELSE
+        INSERT INTO `page_access`   
+            (page_access.Pagename, page_access.Bitmask)
+            VALUES  (pPagename, pMask);
+    END IF;
+
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS GetBitmaskByPagename//
-CREATE FUNCTION `GetBitmaskByPagename`	(	`Pagename` TEXT CHARSET utf8
- 										) 	RETURNS int(11)
+CREATE FUNCTION `GetBitmaskByPagename` (`pPagename` TEXT CHARSET utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	RETURN 	(	SELECT page_access.Bitmask
-	        	FROM `page_access`
-	        	WHERE page_access.Pagename = Pagename
-	        	LIMIT 1
-	       	);
+    RETURN( SELECT page_access.Bitmask
+                FROM `page_access`
+                WHERE page_access.Pagename = pPagename
+                LIMIT 1
+        );
 END //
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: semesters
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: semesters
+# -------------------------------------------------------------------------------------------
 
 
 DROP FUNCTION IF EXISTS GetCurSemesterID//
-CREATE FUNCTION `GetCurSemesterID`	( ) RETURNS int(11)
+CREATE FUNCTION `GetCurSemesterID` ( ) RETURNS int(11)
     NO SQL
 BEGIN
-	RETURN 	(	SELECT general_settings.Val 
-				FROM `general_settings` 
-				WHERE general_settings.ID = 1
-				LIMIT 1 
-			);
+    RETURN( SELECT general_settings.Val 
+                FROM `general_settings` 
+                WHERE general_settings.ID = 1
+                LIMIT 1 
+        );
 END //
 
 
 DROP FUNCTION IF EXISTS SetSemesterID//
-CREATE FUNCTION `SetSemesterID`	( `SemesterID` INT
-								) RETURNS int(11)
+CREATE FUNCTION `SetSemesterID` (`SemesterID` INT) RETURNS int(11)
     NO SQL
 BEGIN
-	SET @CurrentSemesterID = SemesterID;
-	RETURN 0;
+    SET @CurrentSemesterID = SemesterID;
+    RETURN 0;
 END //
 
 
@@ -425,974 +418,934 @@ END //
 
 
 DROP FUNCTION IF EXISTS SetCurSemesterID//
-CREATE FUNCTION `SetCurSemesterID`	(	`SemesterID` INT 
-									) 	RETURNS int(11)
+CREATE FUNCTION `SetCurSemesterID` (`pSemesterID` INT) RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-	
-	SELECT semesters.ID
-	INTO checker
-	FROM `semesters`
-	WHERE semesters.ID = SemesterID
-	LIMIT 1;
-	IF semesters.ID IS NULL OR semesters.ID <= 0 THEN
-		INSERT INTO `general_settings`
-				(general_settings.ID, general_settings.Val)
-		VALUES 	(1, SemesterID);
-	ELSE
-		UPDATE `general_settings`
-		SET general_settings.Val = SemesterID
-		WHERE general_settings.ID = 1
-		LIMIT 1;
-	END IF;
-	RETURN 0;
+    DECLARE vChecker INT DEFAULT -1;
+    
+    SELECT semesters.ID
+        INTO vChecker
+        FROM `semesters`
+        WHERE semesters.ID = pSemesterID
+        LIMIT 1;
+
+    IF semesters.ID <= 0 THEN
+        INSERT INTO `general_settings`
+            (general_settings.ID, general_settings.Val)
+            VALUES  (1, pSemesterID);
+    ELSE
+        UPDATE `general_settings`
+            SET general_settings.Val = pSemesterID
+            WHERE general_settings.ID = 1
+            LIMIT 1;
+    END IF;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS GetSemesterID//
-CREATE FUNCTION `GetSemesterID`	( 	`Year` INT, 
-									`Num` INT 
-								) 	RETURNS int(11)
+CREATE FUNCTION `GetSemesterID` (`pYear` INT, `pNum` INT) RETURNS int(11)
     NO SQL
 BEGIN
-	RETURN 	(	SELECT semesters.ID 
-				FROM `semesters` 
-				WHERE 	semesters.Year 	= Year AND 
-						semesters.Num 	= Num 
-				LIMIT 1
-			);
+    RETURN( SELECT semesters.ID 
+                FROM `semesters` 
+                WHERE semesters.Year = pYear AND semesters.Num = pNum 
+                LIMIT 1
+        );
 END //
 
 
--- TODO: deprecated
--- DROP PROCEDURE IF EXISTS GetCurSemesterInfo//
--- CREATE PROCEDURE `GetCurSemesterInfo`	( )
---     NO SQL
--- BEGIN
--- 	SELECT 	semesters.ID 	AS 'SemesterID', 
--- 			semesters.Num 	As 'SemesterNum', 	
--- 			semesters.Year  As 'SemesterYear'
--- 	FROM `general_settings`
--- 	INNER JOIN `semesters` ON general_settings.Val = semesters.ID
--- 	WHERE general_settings.ID = 1
--- 	LIMIT 1;
--- END //
+# TODO: deprecated
+# DROP PROCEDURE IF EXISTS GetCurSemesterInfo//
+# CREATE PROCEDURE `GetCurSemesterInfo`    ( )
+#     NO SQL
+# BEGIN
+#  SELECT  semesters.ID    AS 'SemesterID', 
+#          semesters.Num   As 'SemesterNum',   
+#          semesters.Year  As 'SemesterYear'
+#  FROM `general_settings`
+#  INNER JOIN `semesters` ON general_settings.Val = semesters.ID
+#  WHERE general_settings.ID = 1
+#  LIMIT 1;
+# END //
 
 
 
 DROP PROCEDURE IF EXISTS GetSemesterInfo//
-CREATE PROCEDURE `GetSemesterInfo`	(	IN `SemesterID` INT
-									)
+CREATE PROCEDURE `GetSemesterInfo` (IN `pSemesterID` INT)
     NO SQL
 BEGIN
-	SELECT 	semesters.Num 	As 'Num', 	
-			semesters.Year  As 'Year'
-	FROM `semesters`
-	WHERE semesters.ID = SemesterID
-	LIMIT 1;
+    SELECT  semesters.Num   As 'Num',   
+            semesters.Year  As 'Year'
+        FROM `semesters`
+        WHERE semesters.ID = pSemesterID
+        LIMIT 1;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: faculties
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: faculties
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP PROCEDURE IF EXISTS GetFaculties//
-CREATE PROCEDURE `GetFaculties`	( )
+CREATE PROCEDURE `GetFaculties` ( )
     NO SQL
 BEGIN
-	SELECT 	faculties.ID 		AS 'ID',
-			faculties.Name   	AS 'Name',
-			faculties.Abbr 		AS 'Abbr'
-	FROM 	`faculties`
-	ORDER BY faculties.Name ASC;
+    SELECT  faculties.ID    AS 'ID',
+            faculties.Name  AS 'Name',
+            faculties.Abbr  AS 'Abbr'
+        FROM `faculties`
+        ORDER BY faculties.Name ASC;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: departments
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: departments
+# -------------------------------------------------------------------------------------------
 
 
 DROP PROCEDURE IF EXISTS GetDepartments//
-CREATE PROCEDURE `GetDepartments`	( 	IN `FacultyID` INT )
+CREATE PROCEDURE `GetDepartments` (IN `pFacultyID` INT)
     NO SQL
 BEGIN
-	SELECT 	departments.ID 		AS 'ID',
-			departments.Name 	AS 'Name'
-	FROM `departments`
-	WHERE FacultyID = departments.FacultyID
-	ORDER BY departments.Name ASC;
+    SELECT  departments.ID      AS 'ID',
+            departments.Name    AS 'Name'
+        FROM `departments`
+        WHERE departments.FacultyID = pFacultyID
+        ORDER BY departments.Name ASC;
 END //
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: specializations
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: specializations
+# -------------------------------------------------------------------------------------------
 
 
 DROP PROCEDURE IF EXISTS GetSpecializations//
-CREATE PROCEDURE `GetSpecializations`	(	IN `FacultyID` INT 
-										)
+CREATE PROCEDURE `GetSpecializations` (IN `pFacultyID` INT)
     NO SQL
 BEGIN
-		SELECT 	specializations.ID 			AS 'ID',
-				specializations.Name 		AS 'Name',
-				specializations.Abbr 		AS 'Abbr'
-		FROM	`specializations`
-		WHERE 	specializations.FacultyID = FacultyID
-		ORDER BY subjects.Name ASC;
+    SELECT  specializations.ID      AS 'ID',
+            specializations.Name    AS 'Name',
+            specializations.Abbr    AS 'Abbr'
+        FROM    `specializations`
+        WHERE   specializations.FacultyID = pFacultyID
+        ORDER BY subjects.Name ASC;
 END //
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: job positions
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: job positions
+# -------------------------------------------------------------------------------------------
 
 
 DROP PROCEDURE IF EXISTS GetJobPositions//
-CREATE PROCEDURE `GetJobPositions`	( 	)
+CREATE PROCEDURE `GetJobPositions` ( )
     NO SQL
 BEGIN
-	SELECT 	job_positions.ID 	AS 'ID',
-			job_positions.Name 	AS 'Name'
-	FROM 	`job_positions`
-	ORDER BY job_positions.Name;
+    SELECT  job_positions.ID    AS 'ID',
+            job_positions.Name  AS 'Name'
+        FROM    `job_positions`
+        ORDER BY job_positions.Name;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: grades
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: grades
+# -------------------------------------------------------------------------------------------
 
 
 DROP FUNCTION IF EXISTS GetGradeID//
-CREATE FUNCTION `GetGradeID`(	`GradeNum` 	INT, 
-								`Degree` VARCHAR(30) charset utf8-- enum('bachelor','master','specialist') 
-							)	RETURNS int(11)
+CREATE FUNCTION `GetGradeID`
+    (`pGradeNum` INT, `pDegree` VARCHAR(30) charset utf8) RETURNS int(11)
+    # enum('bachelor','master','specialist')                
     NO SQL
 BEGIN  
-	DECLARE res INT;
-
-	SELECT grades.ID
-	INTO res
-	FROM `grades`
-	WHERE 	grades.Num = GradeNum AND
-			grades.Degree = Degree
-	LIMIT 1;
-	
-	IF res > 0 THEN
-		RETURN res;
-	ELSE
-		RETURN -1;
-	END IF;
+    DECLARE vResult INT DEFAULT -1;
+
+    SELECT grades.ID
+        INTO vResult
+        FROM `grades`
+        WHERE   grades.Num = pGradeNum AND
+                grades.Degree = pDegree
+        LIMIT 1;
+    
+    RETURN vResult;
 END //
 
 
 DROP PROCEDURE IF EXISTS GetGrades//
-CREATE PROCEDURE `GetGrades`	( )
+CREATE PROCEDURE `GetGrades` ( )
     NO SQL
 BEGIN  
-	SELECT 	grades.ID 			AS 'ID',
-			grades.Num 	AS 'Num',
-			grades.Degree 		AS 'Degree'
-	FROM 	`grades`
-	ORDER BY grades.ID;
+    SELECT  grades.ID       AS 'ID',
+            grades.Num      AS 'Num',
+            grades.Degree   AS 'Degree'
+        FROM    `grades`
+        ORDER BY grades.ID;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: study groups
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: study groups
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP FUNCTION IF EXISTS CreateStudyGroup//
-CREATE FUNCTION `CreateStudyGroup`	(	`GradeID` INT, 
-										`GroupNum` INT,
-										`SpecializationID` INT,
-										`GroupName` VARCHAR(50) CHARSET utf8 
-									)	RETURNS int(11)
+CREATE FUNCTION `CreateStudyGroup` 
+    (   `pGradeID` INT, `pGroupNum` INT,
+        `pSpecializationID` INT, `pGroupName` VARCHAR(50) CHARSET utf8 
+    )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE checker INT;
-
-	-- check for grade (GradeID) existence
-	SET checker = -1;
-	SELECT grades.ID
-	INTO checker
-	FROM `grades`
-	INNER JOIN `specializations` ON SpecializationID = specializations.ID
-	WHERE GradeID = grades.ID
-	LIMIT 1;
-	IF checker <= 0 OR GroupName IS NULL THEN 
-		RETURN -1;
-	END IF;
-
-	-- check, that such discipline already created
-	SET checker = -1;
-	SELECT study_groups.ID
-	INTO checker
-	FROM `study_groups`
-	WHERE 	GradeID = study_groups.GradeID AND
-			GroupNum = study_groups.GroupNum AND
-			SpecializationID = study_groups.SpecializationID
-	LIMIT 1;
-	IF checker > 0 THEN
-		RETURN -1;
-	END IF;
-
-	-- create discipline
-	INSERT INTO `study_groups`
-			(study_groups.GradeID, study_groups.GroupNum, study_groups.SpecializationID, study_groups.Name )
-	VALUES 	(GradeID, GroupNum, SpecializationID, GroupName);
-	RETURN 0;
+    DECLARE vChecker INT DEFAULT -1;
+
+    # check for grade (GradeID) existence
+    SELECT grades.ID
+        INTO vChecker
+        FROM `grades`
+        INNER JOIN `specializations` ON pSpecializationID = specializations.ID
+        WHERE pGradeID = grades.ID
+        LIMIT 1;
+    IF vChecker <= 0 OR pGroupName IS NULL THEN 
+        RETURN -1;
+    END IF;
+
+    # check, that such discipline already created
+    SET vChecker = -1;
+    SELECT study_groups.ID
+        INTO vChecker
+        FROM `study_groups`
+        WHERE   pGradeID = study_groups.GradeID AND
+                pGroupNum = study_groups.GroupNum AND
+                pSpecializationID = study_groups.SpecializationID
+        LIMIT 1;
+    IF vChecker > 0 THEN
+        RETURN -1;
+    END IF;
+
+    # create discipline
+    INSERT INTO `study_groups`
+            (study_groups.GradeID, study_groups.GroupNum, study_groups.SpecializationID, study_groups.Name )
+    VALUES  (pGradeID, pGroupNum, pSpecializationID, pGroupName);
+    RETURN 0;
 END //
 
 
--- TODO: check
--- DROP PROCEDURE IF EXISTS GetStudyGroups//
--- CREATE PROCEDURE `GetStudyGroups`	( 	IN `GradeID` 	INT,
--- 										IN `FacultyID` 	INT  
--- 									)
---     NO SQL
--- BEGIN
--- 	SELECT 	study_groups.ID 			AS 'GroupID',
--- 			study_groups.GroupNum 		AS 'GroupNum',
--- 			specializations.ID 			AS 'SpecID',
--- 			specializations.Name 		AS 'SpecName',
--- 			specializations.Abbr 		AS 'SpecAbbr'
--- 	FROM `study_groups`
--- 	INNER JOIN `specializations` 	ON 	specializations.ID = study_groups.SpecializationID AND
--- 										FacultyID = specializations.FacultyID
--- 	WHERE 	GradeID = 0 OR 
--- 			study_groups.GradeID = GradeID
--- 	ORDER BY 	specializations.ID ASC, 
--- 				study_groups.GradeID ASC, 
--- 				study_groups.GroupNum ASC;
--- END //
+# TODO: check
+# DROP PROCEDURE IF EXISTS GetStudyGroups//
+# CREATE PROCEDURE `GetStudyGroups`    (   IN `GradeID`    INT,
+#                                      IN `FacultyID`  INT  
+#                                  )
+#     NO SQL
+# BEGIN
+#  SELECT  study_groups.ID             AS 'GroupID',
+#          study_groups.GroupNum       AS 'GroupNum',
+#          specializations.ID          AS 'SpecID',
+#          specializations.Name        AS 'SpecName',
+#          specializations.Abbr        AS 'SpecAbbr'
+#  FROM `study_groups`
+#  INNER JOIN `specializations`    ON  specializations.ID = study_groups.SpecializationID AND
+#                                      FacultyID = specializations.FacultyID
+#  WHERE   GradeID = 0 OR 
+#          study_groups.GradeID = GradeID
+#  ORDER BY    specializations.ID ASC, 
+#              study_groups.GradeID ASC, 
+#              study_groups.GroupNum ASC;
+# END //
 
 
 
 DROP PROCEDURE IF EXISTS GetStudyGroups//
-CREATE PROCEDURE `GetStudyGroups`	( 	IN `GradeID` 	INT,
-										IN `FacultyID` 	INT  
-									)
+CREATE PROCEDURE `GetStudyGroups`   
+    (IN `pGradeID` INT, IN `pFacultyID` INT)
     NO SQL
 BEGIN
-	SELECT 	study_groups.ID 			AS 'ID',
-			study_groups.GroupNum 		AS 'GroupNum',
-			specializations.ID 			AS 'SpecID',
-			specializations.Name 		AS 'SpecName',
-			specializations.Abbr 		AS 'SpecAbbr'
-	FROM `study_groups`
-	INNER JOIN `specializations` 	ON 	specializations.ID = study_groups.SpecializationID AND
-										FacultyID = specializations.FacultyID
-	WHERE 	GradeID = study_groups.GradeID 
-	ORDER BY study_groups.GroupNum ASC;
+    SELECT  view_study_groups.GroupID   AS 'ID',
+            view_study_groups.GroupNum  AS 'GroupNum',
+            view_study_groups.SpecID,
+            view_study_groups.SpecName,
+            view_study_groups.SpecAbbr 
+        FROM `view_study_groups`
+        WHERE   view_study_groups.GradeID = pGradeID AND
+                view_study_groups.FacultyID = pFacultyID
+        ORDER BY study_groups.GroupNum ASC;
 END //
 
 
--- get all study groups, that takes this course (i.e. Discipline)
+# get all study groups, that takes this course
 DROP PROCEDURE IF EXISTS GetStudyGroupsForDiscipline//
-CREATE PROCEDURE `GetStudyGroupsForDiscipline`	( 	IN `DisciplineID` INT	)
+CREATE PROCEDURE `GetStudyGroupsForDiscipline` (IN `pDisciplineID` INT)
     NO SQL
 BEGIN
-	SELECT 	study_groups.ID 		AS 'ID', 
-			study_groups.GroupNum 	AS 'GroupNum', 
-			study_groups.Name 		AS 'Name',
-			grades.ID 				AS 'GradeID',
-			grades.Num 		AS 'GradeNum',
-			grades.Degree 			AS 'Degree',	
-	-- TODO deprecated disciplines_groups.ID 	AS 'DiscGroupID',
-			specializations.ID 		AS 'SpecID',
-			specializations.Name 	AS 'SpecName',
-			specializations.Abbr 	AS 'SpecAbbr'
-	FROM `disciplines_groups`
-	INNER JOIN `study_groups` 		ON study_groups.ID = disciplines_groups.StudyGroupID
-	INNER JOIN `specializations` 	ON study_groups.SpecializationID = specializations.ID
-	INNER JOIN `grades` 			ON study_groups.GradeID = grades.ID
-	WHERE disciplines_groups.DisciplineID = DisciplineID
-	ORDER BY 	grades.ID ASC, 
-				study_groups.GroupNum ASC;
+    SELECT  view_study_groups.GroupID   AS 'ID',
+            view_study_groups.GroupNum,
+            view_study_groups.GradeID,
+            view_study_groups.GradeNum,
+            view_study_groups.Degree,
+            view_study_groups.SpecID,
+            view_study_groups.SpecName,
+            view_study_groups.SpecAbbr
+        FROM `disciplines_groups`
+        INNER JOIN `view_study_groups` ON disciplines_groups.GroupID = view_study_groups.GroupID
+        WHERE disciplines_groups.DisciplineID = pDisciplineID
+        ORDER BY view_study_groups.GradeID ASC, view_study_groups.GroupID ASC;
 END //
 
-
+# get all study groups, that takes this course, including attached students
 DROP PROCEDURE IF EXISTS GetStudyGroupsForDisciplineFull//
-CREATE PROCEDURE `GetStudyGroupsForDisciplineFull`	( 	IN `DisciplineID` INT	)
+CREATE PROCEDURE `GetStudyGroupsForDisciplineFull` (IN `pDisciplineID` INT)
     NO SQL
 BEGIN
-	SELECT 	DISTINCT
-			study_groups.ID 		AS 'ID', 
-			study_groups.GroupNum 	AS 'GroupNum', 
-			study_groups.Name 		AS 'Name',
-			grades.ID 				AS 'GradeID',
-			grades.Num 		AS 'GradeNum',
-			grades.Degree 			AS 'Degree',	
-	-- TODO deprecated disciplines_groups.ID 	AS 'DiscGroupID',
-			specializations.ID 		AS 'SpecID',
-			specializations.Name 	AS 'SpecName',
-			specializations.Abbr 	AS 'SpecAbbr'
-	FROM `students`
-	INNER JOIN `study_groups` ON students.StudyGroupID = study_groups.ID
-	INNER JOIN `specializations` 	ON study_groups.SpecializationID = specializations.ID
-	INNER JOIN `grades` 			ON study_groups.GradeID = grades.ID		
-	WHERE InternalIsStudentAttached(students.ID, DisciplineID)
-	ORDER BY 	grades.ID ASC, 
-				study_groups.GroupNum ASC;
+    SELECT DISTINCT 
+    # select all students and collapse all groups
+            view_study_groups.GroupID   AS 'ID',
+            view_study_groups.GroupNum,
+            view_study_groups.GradeID,
+            view_study_groups.GradeNum,
+            view_study_groups.Degree,
+            view_study_groups.SpecID,
+            view_study_groups.SpecName,
+            view_study_groups.SpecAbbr
+        FROM `view_disciplines_students`
+        WHERE view_disciplines_students.DisciplineID = pDisciplineID
+        ORDER BY view_disciplines_students.GradeID ASC, view_disciplines_students.GroupNum ASC;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: subjects
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: subjects
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP FUNCTION IF EXISTS CreateSubject//
-CREATE FUNCTION `CreateSubject`	(	`FacultyID` INT, 
-									`SubjectName` VARCHAR(200) charset utf8,
-									`SubjectAbbr` VARCHAR(20) charset utf8
-								)	RETURNS int(11)
+CREATE FUNCTION `CreateSubject` 
+    (   `pFacultyID` INT, 
+        `pSubjectName` VARCHAR(200) charset utf8,
+        `pSubjectAbbr` VARCHAR(20) charset utf8
+    )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE checker, SubjectID INT;
-	
-	SET SubjectID = -1;
-	SELECT subjects.ID
-	INTO SubjectID
-	FROM `subjects`
-	WHERE SubjectName = subjects.Name
-	LIMIT 1;
-
-	IF SubjectID <= 0 THEN 
-		-- subject with this name doesn't exist
-
-		-- create subject
-		INSERT INTO `subjects`
-				(subjects.Name, subjects.Abbr)
-		VALUES 	(SubjectName, SubjectAbbr);
-
-		-- get it's id
-		SET SubjectID = -1;
-		SELECT subjects.ID
-		INTO SubjectID
-		FROM `subjects`
-		WHERE SubjectName = subjects.Name
-		LIMIT 1;
-		IF SubjectID <= 0 THEN
-			RETURN -1;
-		END IF;
-	ELSE
-		-- subject extst 
-		-- if subject already attached to faculty, then exit
-		SET checker = -1;
-		SELECT subjects_faculties.ID
-		INTO checker
-		FROM `subjects_faculties`
-		WHERE 	FacultyID = subjects_faculties.FacultyID AND
-				SubjectID = subjects_faculties.SubjectID
-		LIMIT 1;
-		IF checker > 0 THEN
-			RETURN 0;
-		END IF;
-	END IF;
-
-	-- attach subject to faculty
-	INSERT INTO `subjects_faculties`
-			(subjects_faculties.SubjectID, subjects_faculties.FacultyID)
-	VALUES 	(SubjectID, FacultyID);
-	RETURN 0;
+    DECLARE vChecker, vSubjectID INT DEFAULT -1;
+    
+    SELECT subjects.ID
+        INTO vSubjectID
+        FROM `subjects`
+        WHERE pSubjectName = subjects.Name
+        LIMIT 1;
+
+    IF vSubjectID <= 0 THEN 
+        # subject with this name doesn't exist
+
+        # create subject
+        INSERT INTO `subjects`
+            (subjects.Name, subjects.Abbr)
+            VALUES  (pSubjectName, pSubjectAbbr);
+
+        # get it's id
+        SELECT subjects.ID
+            INTO vSubjectID
+            FROM `subjects`
+            WHERE subjects.Name = pSubjectName
+            LIMIT 1;
+        IF vSubjectID <= 0 THEN
+            RETURN -1;
+        END IF;
+    ELSE
+        # subject extst 
+        # if subject already attached to faculty, then exit
+        SELECT subjects_faculties.ID
+            INTO vChecker
+            FROM `subjects_faculties`
+            WHERE   subjects_faculties.FacultyID = pFacultyID AND
+                    subjects_faculties.SubjectID = vSubjectID
+            LIMIT 1;
+        IF vChecker > 0 THEN
+            RETURN 0;
+        END IF;
+    END IF;
+
+    # attach subject to faculty
+    INSERT INTO `subjects_faculties`
+        (subjects_faculties.SubjectID, subjects_faculties.FacultyID)
+        VALUES (vSubjectID, pFacultyID);
+    RETURN 0;
 END //
 
 
 DROP PROCEDURE IF EXISTS GetSubjects//
-CREATE PROCEDURE `GetSubjects`	( 	IN `FacultyID` INT
-								)
+CREATE PROCEDURE `GetSubjects` (IN `pFacultyID` INT)
     NO SQL
 BEGIN
-	SELECT 	subjects.ID 	AS 'ID',
-			subjects.Name 	AS 'Name',
-			subjects.Abbr 	AS 'Abbr'
-	FROM	`subjects_faculties`
-	INNER JOIN `subjects` 	ON 	subjects_faculties.SubjectID = subjects.ID
-	WHERE 	subjects_faculties.FacultyID = FacultyID
-	ORDER BY subjects.Name ASC;
+    SELECT  subjects.ID     AS 'ID',
+            subjects.Name   AS 'Name',
+            subjects.Abbr   AS 'Abbr'
+        FROM `subjects_faculties`
+        INNER JOIN `subjects` ON subjects_faculties.SubjectID = subjects.ID
+        WHERE   subjects_faculties.FacultyID = pFacultyID
+        ORDER BY subjects.Name ASC;
 END //
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: accounts
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: accounts
+# -------------------------------------------------------------------------------------------
 
--- TODO: count? wtf?
+# TODO: count? wtf?
 
 DROP FUNCTION IF EXISTS GetAccCountByCode//
-CREATE FUNCTION `GetAccCountByCode`	( 	`Code` VARCHAR(40) CHARSET utf8 
-									) 	RETURNS int(11)
+CREATE FUNCTION `GetAccCountByCode` (`pCode` VARCHAR(40) CHARSET utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	RETURN 	(	SELECT COUNT(*) 
-				FROM `accounts` 
-				WHERE accounts.ActivationCode = Code
-				LIMIT 1
-			);
+    RETURN( SELECT COUNT(*) 
+                FROM `accounts` 
+                WHERE accounts.ActivationCode = pCode
+                LIMIT 1
+        );
 END //
 
 
 DROP FUNCTION IF EXISTS GetAccCountByMail //
-CREATE FUNCTION `GetAccCountByMail`	(	`EMail` VARCHAR(50) CHARSET utf8
-									) 	RETURNS int(11)
+CREATE FUNCTION `GetAccCountByMail` (`pEMail` VARCHAR(50) CHARSET utf8) RETURNS int(11)
     NO SQL
 BEGIN 
-	RETURN 	( 	SELECT COUNT(*) 
-				FROM `accounts` 
-				WHERE accounts.EMail = EMail
-				LIMIT 1
-			);
+    RETURN( SELECT COUNT(*) 
+                FROM `accounts` 
+                WHERE accounts.EMail = pEMail
+                LIMIT 1
+        );
 END //
 
 
 DROP FUNCTION IF EXISTS GetAccCountByLogin//
-CREATE FUNCTION `GetAccCountByLogin`	(	`Login` VARCHAR(50) CHARSET utf8
-										) 	RETURNS int(11)
+CREATE FUNCTION `GetAccCountByLogin` (`pLogin` VARCHAR(50) CHARSET utf8) RETURNS int(11)
     NO SQL
 BEGIN 
-	RETURN 	( 	SELECT COUNT(*) 
-				FROM `accounts`
-				WHERE 	accounts.Login = Login
-				LIMIT 1
-			);
+    RETURN( SELECT COUNT(*) 
+                FROM `accounts`
+                WHERE   accounts.Login = pLogin
+                LIMIT 1
+        );
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetAccountInfo//
-CREATE PROCEDURE `GetAccountInfo`	(	IN `UserID` INT
-									)
+CREATE PROCEDURE `GetAccountInfo` ( IN `pUserID` INT )
     NO SQL
 BEGIN 
-	SELECT 	accounts.ID 		AS 'ID', 
-			accounts.Login 		AS 'Login', 
-			accounts.EMail 		AS 'EMail', 
-			user_roles.Type 	AS 'Type',
-			user_roles.RoleName	AS 'Role',	 
-			user_roles.Mark 	AS 'RoleMark',	 
-			accounts.isEnabled,
-			accounts.ActivationCode AS 'Code',
-			accounts.UserAgent	
-	FROM `accounts`
-	INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID
-	WHERE accounts.ID = UserID
-	LIMIT 1;
+    SELECT  accounts.ID, 
+            accounts.Login, 
+            accounts.EMail, 
+            user_roles.Type,
+            user_roles.RoleName AS 'Role',   
+            user_roles.Mark     AS 'RoleMark',   
+            accounts.isEnabled,
+            accounts.ActivationCode AS 'Code',
+            accounts.UserAgent  
+        FROM `accounts`
+        INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID
+        WHERE accounts.ID = pUserID
+        LIMIT 1;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetPersonalInfo//
-CREATE PROCEDURE `GetPersonalInfo`	(	IN `UserID` INT 
-										)
+CREATE PROCEDURE `GetPersonalInfo` ( IN `pUserID` INT )
     NO SQL
 BEGIN 
-	DECLARE accType INT;
-	SELECT user_roles.Type
-	INTO accType
-	FROM `accounts`
-	INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID 
-	WHERE accounts.ID = UserID 
-	LIMIT 1;
-	
-	-- type 1: student
-	-- 		2: teacher
-	IF accType = 1 THEN
-		
-		SELECT 	students.LastName 		AS 'Last', 
-				students.FirstName		AS 'First',  
-				students.SecondName 	AS 'Second', 
-				students.ID 			AS 'StudentID', 
-				grades.ID 				AS 'GradeID',
-				grades.Num 				AS 'GradeNum',
-				study_groups.ID 		AS 'GroupID', 
-				study_groups.GroupNum 	AS 'GroupNum', 
-				study_groups.Name 		AS 'GroupName',
-				grades.Degree 			AS 'Degree', 
-				specializations.ID 		AS 'SpecID', 
-				specializations.Name	AS 'SpecName', 
-				specializations.Abbr 	AS 'SpecAbbr',
-				faculties.ID 			AS 'FacultyID', 
-				faculties.Name 			AS 'FacultyName', 
-				faculties.Abbr 			AS 'FacultyAbbr'		
-		FROM `students`
-		INNER JOIN `study_groups` 		ON students.StudyGroupID = study_groups.ID
-		INNER JOIN `grades`				ON grades.ID = study_groups.GradeID
-		INNER JOIN `specializations` 	ON study_groups.SpecializationID = specializations.ID
-		INNER JOIN `faculties` 			ON specializations.FacultyID = faculties.ID
-		WHERE students.AccountID = UserID
-		LIMIT 1;
-
-	ELSE
-		SELECT 	teachers.LastName 		AS 'Last', 
-				teachers.FirstName 		AS 'First', 
-				teachers.SecondName 	AS 'Second', 
-				teachers.ID 			AS 'TeacherID', 
-				departments.ID 			AS 'DepID',
-				departments.Name 		AS 'DepName',
-				job_positions.Name 		AS 'JobPositionName', 
-				faculties.ID 			AS 'FacultyID', 
-				faculties.Name 			AS 'FacultyName', 
-				faculties.Abbr 			AS 'FacultyAbbr'
-		FROM `teachers`
-		INNER JOIN `departments` ON departments.ID = teachers.DepartmentID
-		INNER JOIN `faculties` ON departments.FacultyID = faculties.ID
-		INNER JOIN `job_positions` ON job_positions.ID = teachers.JobPositionID
-		WHERE teachers.AccountID = UserID
-		LIMIT 1;
-
-	END IF;
+    DECLARE vAccountType INT DEFAULT -1;
+    SELECT user_roles.Type
+        INTO vAccountType
+        FROM `accounts`
+        INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID 
+        WHERE accounts.ID = pUserID 
+        LIMIT 1;
+    
+    # type 1: student
+    #      2: teacher
+    IF vAccountType = 1 THEN
+
+        SELECT  view_students.LastName      AS 'Last', 
+                view_students.FirstName     AS 'First',  
+                view_students.SecondName    AS 'Second', 
+                view_students.StudentID, 
+                view_students.GradeID,
+                view_students.GradeNum,
+                view_students.GroupID, 
+                view_students.GroupNum, 
+                view_students.GroupName,
+                view_students.Degree, 
+                view_students.SpecID, 
+                view_students.SpecName, 
+                view_students.SpecAbbr,
+                view_students.FacultyID, 
+                view_students.FacultyName, 
+                view_students.FacultyAbbr  
+            FROM `view_students`
+            WHERE view_students.AccountID = pUserID
+            LIMIT 1; 
+
+    ELSE
+
+        SELECT  view_teachers.LastName       AS 'Last', 
+                view_teachers.FirstName      AS 'First', 
+                view_teachers.SecondName     AS 'Second', 
+                view_teachers.TeacherID, 
+                view_teachers.DepID,
+                view_teachers.DepName,
+                view_teachers.JobPositionName, 
+                view_teachers.FacultyID, 
+                view_teachers.FacultyName, 
+                view_teachers.FacultyAbbr 
+            FROM `view_teachers`
+            WHERE view_teachers.AccountID = pUserID
+            LIMIT 1;
+    END IF; 
 END //
 
 
 
 DROP FUNCTION IF EXISTS ActivateAccount//
-CREATE FUNCTION `ActivateAccount`	(	`Code` VARCHAR(40) CHARSET utf8, 
-										`Login` VARCHAR(50) CHARSET utf8, 
-										`EMail` VARCHAR(50) CHARSET utf8, 
-										`Password` VARCHAR(255) CHARSET utf8
-									) 	RETURNS int(11)
+CREATE FUNCTION `ActivateAccount`   
+    (   `pCode` VARCHAR(40) CHARSET utf8, 
+        `pLogin` VARCHAR(50) CHARSET utf8, 
+        `pEMail` VARCHAR(50) CHARSET utf8, 
+        `pPassword` VARCHAR(255) CHARSET utf8
+    )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker, UserID INT;
-	
-
-	-- check for matching with existing accounts (note: Login & E-Mail are unique) 
-	SET checker = -1;
-	SELECT accounts.ID
-	INTO checker 
-	FROM `accounts` 
-	WHERE 	accounts.Login = Login OR
-			accounts.EMail = EMail
-	LIMIT 1;
-	IF checker > 0 OR Password IS NULL THEN 
-		RETURN -1;
-	END IF;
-
-	-- check for existing of accounts with such Code
-	SET UserID = -1;
-	SELECT accounts.ID
-	INTO UserID 
-	FROM `accounts` 
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF UserID <= 0 THEN
-		RETURN -2;
-	END IF;
-
-	-- activate account
-	UPDATE `accounts` 
-	SET 	accounts.Login = Login, 
-			accounts.Password = Password, 
-			accounts.EMail = EMail, 
-			accounts.ActivationCode = NULL
-	WHERE accounts.ID = UserID 
-	LIMIT 1;
-	RETURN UserID;
+    DECLARE vChecker, vUserID INT DEFAULT -1;
+    
+    # check for matching with existing accounts (note: Login & E-Mail are unique) 
+    SELECT accounts.ID
+        INTO vChecker 
+        FROM `accounts` 
+        WHERE   accounts.Login = pLogin OR
+                accounts.EMail = pEMail
+        LIMIT 1;
+    IF vChecker > 0 OR pPassword IS NULL THEN 
+        RETURN -1;
+    END IF;
+
+    # check for existing of accounts with such Code
+    SELECT accounts.ID
+        INTO vUserID 
+        FROM `accounts` 
+        WHERE accounts.ActivationCode = pCode
+        LIMIT 1;
+        IF vUserID <= 0 THEN
+            RETURN -2;
+        END IF;
+
+    # activate account
+    UPDATE `accounts` 
+        SET     accounts.Login = pLogin, 
+                accounts.Password = pPassword, 
+                accounts.EMail = pEMail, 
+                accounts.ActivationCode = NULL
+        WHERE accounts.ID = vUserID 
+        LIMIT 1;
+    RETURN vUserID;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangePassword//
-CREATE FUNCTION `ChangePassword`	(	`UserID` INT,  
-										`Password` VARCHAR(255) CHARSET utf8
-									) 	RETURNS int(11)
+CREATE FUNCTION `ChangePassword`    (   `UserID` INT,  
+                                        `Password` VARCHAR(255) CHARSET utf8
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-
-	IF Password IS NULL THEN
-		RETURN -1;
-	END IF;
-
-	-- check account with UserID
-	SET checker = -1;
-	SELECT accounts.ID
-	INTO checker
-	FROM `accounts`
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	-- set new password
-	UPDATE `accounts`
-	SET accounts.Password = Password
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker INT;
+
+    IF Password IS NULL THEN
+        RETURN -1;
+    END IF;
+
+    # check account with UserID
+    SET checker = -1;
+    SELECT accounts.ID
+    INTO checker
+    FROM `accounts`
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    # set new password
+    UPDATE `accounts`
+    SET accounts.Password = Password
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeLogin//
-CREATE FUNCTION `ChangeLogin`	(	`UserID` INT,  
-									`Login` VARCHAR(50) CHARSET utf8
-								) 	RETURNS int(11)
+CREATE FUNCTION `ChangeLogin`   (   `UserID` INT,  
+                                    `Login` VARCHAR(50) CHARSET utf8
+                                )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-
-	-- check for account with UserID
-	SET checker = -1;
-	SELECT accounts.ID
-	INTO checker
-	FROM `accounts` 
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	-- search accounts with Login (login must be unique)
-	SET checker = -1;
-	SELECT accounts.ID 
-	INTO checker
-	FROM `accounts`
-	WHERE accounts.Login = Login
-	LIMIT 1;
-	IF checker > 0 THEN 
-		RETURN -1;
-	END IF;
-
-
-	-- set new login
-	UPDATE `accounts` 
-	SET accounts.Login = Login
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker INT;
+
+    # check for account with UserID
+    SET checker = -1;
+    SELECT accounts.ID
+    INTO checker
+    FROM `accounts` 
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    # search accounts with Login (login must be unique)
+    SET checker = -1;
+    SELECT accounts.ID 
+    INTO checker
+    FROM `accounts`
+    WHERE accounts.Login = Login
+    LIMIT 1;
+    IF checker > 0 THEN 
+        RETURN -1;
+    END IF;
+
+
+    # set new login
+    UPDATE `accounts` 
+    SET accounts.Login = Login
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeMail//
-CREATE FUNCTION `ChangeMail`	(	`UserID` INT,  
-									`EMail` VARCHAR(50) CHARSET utf8
-								) 	RETURNS int(11)
+CREATE FUNCTION `ChangeMail`    (   `UserID` INT,  
+                                    `EMail` VARCHAR(50) CHARSET utf8
+                                )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-	
-	-- check for account with UserID
-	SET checker = -1;
-	SELECT accounts.ID 
-	INTO checker
-	FROM `accounts` 
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	-- search accounts with EMail (e-mail must be unique)
-	SET checker = -1;
-	SELECT accounts.ID 
-	INTO checker
-	FROM `accounts`
-	WHERE accounts.EMail = EMail
-	LIMIT 1;
-	IF checker > 0 THEN 
-		RETURN -1;
-	END IF;
-
-	-- set new e-mail
-	UPDATE `accounts` 
-	SET accounts.EMail = EMail
-	WHERE accounts.ID = UserID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker INT;
+    
+    # check for account with UserID
+    SET checker = -1;
+    SELECT accounts.ID 
+    INTO checker
+    FROM `accounts` 
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    # search accounts with EMail (e-mail must be unique)
+    SET checker = -1;
+    SELECT accounts.ID 
+    INTO checker
+    FROM `accounts`
+    WHERE accounts.EMail = EMail
+    LIMIT 1;
+    IF checker > 0 THEN 
+        RETURN -1;
+    END IF;
+
+    # set new e-mail
+    UPDATE `accounts` 
+    SET accounts.EMail = EMail
+    WHERE accounts.ID = UserID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS SignIn//
-CREATE FUNCTION `SignIn`(	`LoginOrMail` VARCHAR(255) CHARSET utf8, 
-							`Password` 	VARCHAR(64) CHARSET utf8
-						) 	RETURNS int(11)
+CREATE FUNCTION `SignIn`(   `LoginOrMail` VARCHAR(255) CHARSET utf8, 
+                            `Password`  VARCHAR(64) CHARSET utf8
+                        )   RETURNS int(11)
     NO SQL
 BEGIN 
-	DECLARE curID INT;
-
-	SET curID = -1;
-	SELECT accounts.ID
-	INTO curID
-	FROM `accounts`
-	WHERE 	accounts.Password = Password AND
-			(	accounts.Login = LoginOrMail OR 
-				accounts.EMail = LoginOrMail 	)
-	LIMIT 1;
-	IF curID <= 0 THEN 
-		RETURN -1;
-	END IF;
-
-	-- loging
-	INSERT INTO `logs_signin`
-			(	logs_signin.AccountID 	)
-	VALUES 	(	curID	);
-		RETURN curID;
+    DECLARE curID INT;
+
+    SET curID = -1;
+    SELECT accounts.ID
+    INTO curID
+    FROM `accounts`
+    WHERE   accounts.Password = Password AND
+            (   accounts.Login = LoginOrMail OR 
+                accounts.EMail = LoginOrMail    )
+    LIMIT 1;
+    IF curID <= 0 THEN 
+        RETURN -1;
+    END IF;
+
+    # loging
+    INSERT INTO `logs_signin`
+            (   logs_signin.AccountID   )
+    VALUES  (   curID   );
+        RETURN curID;
 END //
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: teachers
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: teachers
+# -------------------------------------------------------------------------------------------
 
 
 DROP FUNCTION IF EXISTS ChangeTeacherInfo//
-CREATE FUNCTION `ChangeTeacherInfo`	(	`TeacherID` 	INT, 
-										`TeacherLast` VARCHAR(30) CHARSET utf8,
-										`TeacherFirst` VARCHAR(30) CHARSET utf8,
-										`TeacherSecond` VARCHAR(30) CHARSET utf8,
-										`JobPositionID` INT,
-										`DepartmentID` INT
-									)	RETURNS int(11)
+CREATE FUNCTION `ChangeTeacherInfo` (   `TeacherID`     INT, 
+                                        `TeacherLast` VARCHAR(30) CHARSET utf8,
+                                        `TeacherFirst` VARCHAR(30) CHARSET utf8,
+                                        `TeacherSecond` VARCHAR(30) CHARSET utf8,
+                                        `JobPositionID` INT,
+                                        `DepartmentID` INT
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE checker INT;
-	SET checker = -1;
-
-	-- check existing of Teacher, jobPosition & Department
-	SELECT teachers.ID
-	INTO checker
-	FROM `teachers`
-	INNER JOIN `job_positions` 	ON 	JobPositionID = job_positions.ID
-	INNER JOIN `departments`	ON 	DepartmentID = departments.ID
-	WHERE TeacherID = teachers.ID
-	LIMIT 1;
-	IF 	checker <= 0 OR
-		TeacherLast IS NULL OR
-		TeacherSecond IS NULL OR
-		TeacherSecond IS NULL
-	THEN 
-		RETURN -1;
-	END IF;
-
-	-- set new info
-	UPDATE `teachers`
-	SET 	teachers.LastName = TeacherLast,
-			teachers.FirstName = TeacherFirst,
-			teachers.SecondName = TeacherSecond,
-			teachers.JobPositionID = JobPositionID,
-			teachers.DepartmentID = DepartmentID
-	WHERE TeacherID = teachers.ID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker INT;
+    SET checker = -1;
+
+    # check existing of Teacher, jobPosition & Department
+    SELECT teachers.ID
+    INTO checker
+    FROM `teachers`
+    INNER JOIN `job_positions`  ON  JobPositionID = job_positions.ID
+    INNER JOIN `departments`    ON  DepartmentID = departments.ID
+    WHERE TeacherID = teachers.ID
+    LIMIT 1;
+    IF  checker <= 0 OR
+        TeacherLast IS NULL OR
+        TeacherSecond IS NULL OR
+        TeacherSecond IS NULL
+    THEN 
+        RETURN -1;
+    END IF;
+
+    # set new info
+    UPDATE `teachers`
+    SET     teachers.LastName = TeacherLast,
+            teachers.FirstName = TeacherFirst,
+            teachers.SecondName = TeacherSecond,
+            teachers.JobPositionID = JobPositionID,
+            teachers.DepartmentID = DepartmentID
+    WHERE TeacherID = teachers.ID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetTeachersByFaculty//
-CREATE PROCEDURE `GetTeachersByFaculty`	( 	IN `FacultyID` INT  
-										)
+CREATE PROCEDURE `GetTeachersByFaculty` (   IN `FacultyID` INT  
+                                        )
     NO SQL
 BEGIN
-	SELECT 	teachers.ID 			AS 'ID', 
-			teachers.LastName 		AS 'Last', 
-			teachers.FirstName 		AS 'First', 
-			teachers.SecondName		AS 'Second', 
-			teachers.AccountID		AS 'AccountID',
-			job_positions.Name 		AS 'JobPositionName', 
-			departments.ID 			AS 'DepID',
-			departments.Name 		AS 'DepName'
-	FROM `teachers`
-	INNER JOIN `departments` 	ON 	departments.ID = teachers.DepartmentID AND
-									FacultyID = departments.FacultyID
-	INNER JOIN `job_positions` 	ON 	job_positions.ID = teachers.JobPositionID
-	ORDER BY 	teachers.LastName ASC, 
-				teachers.FirstName ASC, 
-				teachers.SecondName ASC;
+    SELECT  teachers.ID             AS 'ID', 
+            teachers.LastName       AS 'Last', 
+            teachers.FirstName      AS 'First', 
+            teachers.SecondName     AS 'Second', 
+            teachers.AccountID      AS 'AccountID',
+            job_positions.Name      AS 'JobPositionName', 
+            departments.ID          AS 'DepID',
+            departments.Name        AS 'DepName'
+    FROM `teachers`
+    INNER JOIN `departments`    ON  departments.ID = teachers.DepartmentID AND
+                                    FacultyID = departments.FacultyID
+    INNER JOIN `job_positions`  ON  job_positions.ID = teachers.JobPositionID
+    ORDER BY    teachers.LastName ASC, 
+                teachers.FirstName ASC, 
+                teachers.SecondName ASC;
 END //
 
 
--- CHANGE
+# CHANGE
 
 DROP PROCEDURE IF EXISTS GetTeachersByDepartment//
-CREATE PROCEDURE `GetTeachersByDepartment`	( 	IN `DepartmentID` INT  
-											)
+CREATE PROCEDURE `GetTeachersByDepartment`  (   IN `DepartmentID` INT  
+                                            )
     NO SQL
 BEGIN
-	SELECT 	teachers.ID 			AS 'ID', 
-			teachers.LastName 		AS 'Last', 
-			teachers.FirstName 		AS 'First', 
-			teachers.SecondName		AS 'Second', 
-			teachers.AccountID		AS 'AccountID', 
-			job_positions.Name 		AS 'JobPositionName', 
-			departments.ID 			AS 'DepID',
-			departments.Name 		AS 'DepName'
-	FROM `teachers`
-	INNER JOIN `departments` 	ON 	teachers.DepartmentID = departments.ID AND
-									DepartmentID = departments.ID
-	INNER JOIN `job_positions` 	ON 	job_positions.ID = teachers.JobPositionID
-	ORDER BY 	teachers.LastName ASC, 
-				teachers.FirstName ASC, 
-				teachers.SecondName ASC;
+    SELECT  teachers.ID             AS 'ID', 
+            teachers.LastName       AS 'Last', 
+            teachers.FirstName      AS 'First', 
+            teachers.SecondName     AS 'Second', 
+            teachers.AccountID      AS 'AccountID', 
+            job_positions.Name      AS 'JobPositionName', 
+            departments.ID          AS 'DepID',
+            departments.Name        AS 'DepName'
+    FROM `teachers`
+    INNER JOIN `departments`    ON  teachers.DepartmentID = departments.ID AND
+                                    DepartmentID = departments.ID
+    INNER JOIN `job_positions`  ON  job_positions.ID = teachers.JobPositionID
+    ORDER BY    teachers.LastName ASC, 
+                teachers.FirstName ASC, 
+                teachers.SecondName ASC;
 END //
 
 
 DROP FUNCTION IF EXISTS CreateTeacher//
-CREATE FUNCTION `CreateTeacher`(	`Last` 			VARCHAR(30) CHARSET utf8, 
-									`First` 		VARCHAR(30) CHARSET utf8, 
-									`Second` 		VARCHAR(30) CHARSET utf8, 
-									`JobPositionID` INT, 
-									`DepartmentID` 	INT, 
-									`Code` 			VARCHAR(40) CHARSET utf8 
-								) 	RETURNS int(11)
+CREATE FUNCTION `CreateTeacher`(    `Last`          VARCHAR(30) CHARSET utf8, 
+                                    `First`         VARCHAR(30) CHARSET utf8, 
+                                    `Second`        VARCHAR(30) CHARSET utf8, 
+                                    `JobPositionID` INT, 
+                                    `DepartmentID`  INT, 
+                                    `Code`          VARCHAR(40) CHARSET utf8 
+                                )   RETURNS int(11)
     NO SQL
 BEGIN 
-	DECLARE accID, checker, RoleID INT;
-
-	IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
-		RETURN -1;
-	END IF;
-
-	-- check Department & jobPosition existing
-	SET checker = -1;
-	SELECT departments.ID 
-	INTO checker
-	FROM `departments`
-	INNER JOIN `job_positions` ON job_positions.ID = JobPositionID
-	WHERE departments.ID = DepartmentID
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	-- search accounts with same Code
-	SET accID = -1;
-	SELECT accounts.ID 
-	INTO accID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF accID > 0 THEN
-		RETURN -2;
-	END IF;
-
-	-- user role 2 - common teacher
-	-- add new account
-	INSERT INTO `accounts` 
-			( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
-	VALUES 	( NULL, NULL, NULL, 2, Code);
-	
-
-	-- get this account ID
-	SET accID = -1;
-	SELECT accounts.ID 
-	INTO accID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF accID <= 0 THEN
-		RETURN -3;
-	END IF; 
-	
-	-- add new teacher
-	INSERT INTO `teachers` 
-		(	teachers.AccountID, 
-			teachers.LastName, 
-			teachers.FirstName, 
-			teachers.SecondName, 
-			teachers.JobPositionID, 
-			teachers.DepartmentID
-		)
-	VALUES 	(accID, Last, First, Second, JobPositionID, DepartmentID);
-	RETURN 0;
+    DECLARE accID, checker, RoleID INT;
+
+    IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
+        RETURN -1;
+    END IF;
+
+    # check Department & jobPosition existing
+    SET checker = -1;
+    SELECT departments.ID 
+    INTO checker
+    FROM `departments`
+    INNER JOIN `job_positions` ON job_positions.ID = JobPositionID
+    WHERE departments.ID = DepartmentID
+    LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    # search accounts with same Code
+    SET accID = -1;
+    SELECT accounts.ID 
+    INTO accID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF accID > 0 THEN
+        RETURN -2;
+    END IF;
+
+    # user role 2 - common teacher
+    # add new account
+    INSERT INTO `accounts` 
+            ( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
+    VALUES  ( NULL, NULL, NULL, 2, Code);
+    
+
+    # get this account ID
+    SET accID = -1;
+    SELECT accounts.ID 
+    INTO accID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF accID <= 0 THEN
+        RETURN -3;
+    END IF; 
+    
+    # add new teacher
+    INSERT INTO `teachers` 
+        (   teachers.AccountID, 
+            teachers.LastName, 
+            teachers.FirstName, 
+            teachers.SecondName, 
+            teachers.JobPositionID, 
+            teachers.DepartmentID
+        )
+    VALUES  (accID, Last, First, Second, JobPositionID, DepartmentID);
+    RETURN 0;
 END //
 
 
--- TODO: deprecated
+# TODO: deprecated
 
 DROP FUNCTION IF EXISTS CreateTeacherByDepName//
-CREATE FUNCTION `CreateTeacherByDepName`(	`Last` 			VARCHAR(30) CHARSET utf8, 
-											`First` 		VARCHAR(30) CHARSET utf8, 
-											`Second` 		VARCHAR(30) CHARSET utf8, 
-											`DepartmentName` VARCHAR(200) CHARSET utf8, 
-											`Code` 			VARCHAR(40) CHARSET utf8 
-										) 	RETURNS int(11)
+CREATE FUNCTION `CreateTeacherByDepName`(   `Last`          VARCHAR(30) CHARSET utf8, 
+                                            `First`         VARCHAR(30) CHARSET utf8, 
+                                            `Second`        VARCHAR(30) CHARSET utf8, 
+                                            `DepartmentName` VARCHAR(200) CHARSET utf8, 
+                                            `Code`          VARCHAR(40) CHARSET utf8 
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN 
-	DECLARE curID, checker, RoleID, DepID INT;
-
-	IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT departments.ID 
-	INTO DepID
-	FROM `departments`
-	WHERE 	departments.Name = DepartmentName OR
-			(DepartmentName = '' AND departments.Name IS NULL)
-	LIMIT 1;
-	IF DepID <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT job_positions.ID 
-	INTO checker
-	FROM `job_positions`
-	WHERE job_positions.ID = 12
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	SET curID = -1;
-	SELECT accounts.ID 
-	INTO curID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF curID > 0 THEN
-		RETURN -2;
-	END IF;
-
-	-- TODO: user roles
-	INSERT INTO `accounts` 
-			( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
-	VALUES 	( NULL, NULL, NULL, 2, Code);
-
-	SET curID = -1;
-	SELECT accounts.ID 
-	INTO curID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF curID <= 0 THEN
-		RETURN -3;
-	END IF; 
-
-	INSERT INTO `teachers` 
-		(	teachers.AccountID, 
-			teachers.LastName, 
-			teachers.FirstName, 
-			teachers.SecondName, 
-			teachers.JobPositionID, 
-			teachers.DepartmentID
-		)
-	VALUES 	(curID, Last, First, Second, 12, DepID);
-	RETURN 0;
+    DECLARE curID, checker, RoleID, DepID INT;
+
+    IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT departments.ID 
+    INTO DepID
+    FROM `departments`
+    WHERE   departments.Name = DepartmentName OR
+            (DepartmentName = '' AND departments.Name IS NULL)
+    LIMIT 1;
+    IF DepID <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT job_positions.ID 
+    INTO checker
+    FROM `job_positions`
+    WHERE job_positions.ID = 12
+    LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    SET curID = -1;
+    SELECT accounts.ID 
+    INTO curID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF curID > 0 THEN
+        RETURN -2;
+    END IF;
+
+    # TODO: user roles
+    INSERT INTO `accounts` 
+            ( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
+    VALUES  ( NULL, NULL, NULL, 2, Code);
+
+    SET curID = -1;
+    SELECT accounts.ID 
+    INTO curID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF curID <= 0 THEN
+        RETURN -3;
+    END IF; 
+
+    INSERT INTO `teachers` 
+        (   teachers.AccountID, 
+            teachers.LastName, 
+            teachers.FirstName, 
+            teachers.SecondName, 
+            teachers.JobPositionID, 
+            teachers.DepartmentID
+        )
+    VALUES  (curID, Last, First, Second, 12, DepID);
+    RETURN 0;
 END //
 
 
@@ -1402,115 +1355,115 @@ CREATE PROCEDURE `GetTeachersForDiscipline`(IN `DisciplineID` INT)
     NO SQL
 BEGIN  
 
-		SELECT 	teachers.ID 			AS 'ID', 
-				teachers.LastName 		AS 'Last', 
-				teachers.FirstName 		AS 'First', 
-				teachers.SecondName 	AS 'Second', 
-				job_positions.ID 		AS 'JobPositionID',
-				job_positions.Name 		AS 'JobPositionName', 
-				departments.ID 			AS 'DepID',
-				departments.Name 		AS 'DepName',
-				faculties.ID 			AS 'FacultyID', 
-				faculties.Name 			AS 'FacultyName', 
-				faculties.Abbr 			AS 'FacultyAbbr',
-				(teachers.ID = disciplines.AuthorID)	AS 'isAuthor'
-		FROM `disciplines_teachers`
-		INNER JOIN `disciplines` 	ON 	disciplines.ID = disciplines_teachers.DisciplineID
-		INNER JOIN `teachers` 		ON 	teachers.ID = disciplines_teachers.TeacherID
-		INNER JOIN `departments` 	ON 	departments.ID = teachers.DepartmentID
-		INNER JOIN `faculties` 		ON 	departments.FacultyID = faculties.ID
-		INNER JOIN `job_positions` 	ON 	job_positions.ID = teachers.JobPositionID
-		WHERE disciplines_teachers.DisciplineID = DisciplineID
-		ORDER BY isAuthor DESC, teachers.LastName ASC;
+        SELECT  teachers.ID             AS 'ID', 
+                teachers.LastName       AS 'Last', 
+                teachers.FirstName      AS 'First', 
+                teachers.SecondName     AS 'Second', 
+                job_positions.ID        AS 'JobPositionID',
+                job_positions.Name      AS 'JobPositionName', 
+                departments.ID          AS 'DepID',
+                departments.Name        AS 'DepName',
+                faculties.ID            AS 'FacultyID', 
+                faculties.Name          AS 'FacultyName', 
+                faculties.Abbr          AS 'FacultyAbbr',
+                (teachers.ID = disciplines.AuthorID)    AS 'isAuthor'
+        FROM `disciplines_teachers`
+        INNER JOIN `disciplines`    ON  disciplines.ID = disciplines_teachers.DisciplineID
+        INNER JOIN `teachers`       ON  teachers.ID = disciplines_teachers.TeacherID
+        INNER JOIN `departments`    ON  departments.ID = teachers.DepartmentID
+        INNER JOIN `faculties`      ON  departments.FacultyID = faculties.ID
+        INNER JOIN `job_positions`  ON  job_positions.ID = teachers.JobPositionID
+        WHERE disciplines_teachers.DisciplineID = DisciplineID
+        ORDER BY isAuthor DESC, teachers.LastName ASC;
 END //
 
 
 
--- TODO: rename
+# TODO: rename
 DROP PROCEDURE IF EXISTS SearchTeachers//
-CREATE PROCEDURE `SearchTeachers`		( 	IN `FacultyID` INT,
-											IN `DepartmentID` INT,
-											IN `Name` VARCHAR(100) CHARSET utf8, 
-									 		IN `DisciplineID` INT  
-										)
+CREATE PROCEDURE `SearchTeachers`       (   IN `FacultyID` INT,
+                                            IN `DepartmentID` INT,
+                                            IN `Name` VARCHAR(100) CHARSET utf8, 
+                                            IN `DisciplineID` INT  
+                                        )
     NO SQL
 BEGIN
-	SELECT 	teachers.ID 			AS 'ID', 
-			teachers.LastName 		AS 'Last', 
-			teachers.FirstName 		AS 'First', 
-			teachers.SecondName		AS 'Second', 
-			teachers.AccountID		AS 'AccountID',
-			job_positions.Name 		AS 'JobPositionName', 
-			departments.ID 			AS 'DepID',
-			departments.Name 		AS 'DepName'
-	FROM `teachers`
-	INNER JOIN `departments` 	ON 	teachers.DepartmentID = departments.ID AND
-									( DepartmentID = 0 OR DepartmentID = departments.ID ) AND
-									( FacultyID = 0 OR FacultyID = departments.FacultyID ) 
-	INNER JOIN `job_positions` 	ON 	teachers.JobPositionID = job_positions.ID
-	WHERE	NOT InternalIsTeacherBinded(teachers.ID, DisciplineID) AND
-			CONCAT(teachers.LastName, teachers.FirstName, teachers.SecondName) LIKE CONCAT("%",Name,"%")
-	ORDER BY 	departments.FacultyID ASC, 
-				departments.Name ASC, 
-				teachers.LastName ASC, 
-				teachers.FirstName ASC, 
-				teachers.SecondName ASC;
+    SELECT  teachers.ID             AS 'ID', 
+            teachers.LastName       AS 'Last', 
+            teachers.FirstName      AS 'First', 
+            teachers.SecondName     AS 'Second', 
+            teachers.AccountID      AS 'AccountID',
+            job_positions.Name      AS 'JobPositionName', 
+            departments.ID          AS 'DepID',
+            departments.Name        AS 'DepName'
+    FROM `teachers`
+    INNER JOIN `departments`    ON  teachers.DepartmentID = departments.ID AND
+                                    ( DepartmentID = 0 OR DepartmentID = departments.ID ) AND
+                                    ( FacultyID = 0 OR FacultyID = departments.FacultyID ) 
+    INNER JOIN `job_positions`  ON  teachers.JobPositionID = job_positions.ID
+    WHERE   NOT InternalIsTeacherBindeded(teachers.ID, DisciplineID) AND
+            CONCAT(teachers.LastName, teachers.FirstName, teachers.SecondName) LIKE CONCAT("%",Name,"%")
+    ORDER BY    departments.FacultyID ASC, 
+                departments.Name ASC, 
+                teachers.LastName ASC, 
+                teachers.FirstName ASC, 
+                teachers.SecondName ASC;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: students
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: students
+# -------------------------------------------------------------------------------------------
 
 DROP PROCEDURE IF EXISTS GetStudentsByStudyGroups//
 DROP PROCEDURE IF EXISTS GetStudents//
-CREATE PROCEDURE `GetStudents`	( 	IN `StudyGroupID` INT  
-								)
+CREATE PROCEDURE `GetStudents`  (   IN `StudyGroupID` INT  
+                                )
     NO SQL
 BEGIN
-	SELECT 	students.ID 			AS 'ID', 
-			students.LastName 		AS 'Last', 
-			students.FirstName 		AS 'First',  
-			students.SecondName	 	AS 'Second',
-			students.AccountID 		AS 'AccountID',
-			grades.ID 				AS 'GradeID',
-			grades.Num 				AS 'GradeNum',
-			grades.Degree 			AS 'Degree',	
-			study_groups.ID 		AS 'GroupID',
-			study_groups.GroupNum 	AS 'GroupNum'
-	FROM `students`
-	INNER JOIN `study_groups` 	ON 	students.StudyGroupID = study_groups.ID AND
-									StudyGroupID = study_groups.ID
-	INNER JOIN `grades` ON study_groups.GradeID = grades.ID
-	ORDER BY 	students.LastName ASC, 
-				students.FirstName ASC, 
-				students.SecondName ASC;
+    SELECT  students.ID             AS 'ID', 
+            students.LastName       AS 'Last', 
+            students.FirstName      AS 'First',  
+            students.SecondName     AS 'Second',
+            students.AccountID      AS 'AccountID',
+            grades.ID               AS 'GradeID',
+            grades.Num              AS 'GradeNum',
+            grades.Degree           AS 'Degree',    
+            study_groups.ID         AS 'GroupID',
+            study_groups.GroupNum   AS 'GroupNum'
+    FROM `students`
+    INNER JOIN `study_groups`   ON  students.StudyGroupID = study_groups.ID AND
+                                    StudyGroupID = study_groups.ID
+    INNER JOIN `grades` ON study_groups.GradeID = grades.ID
+    ORDER BY    students.LastName ASC, 
+                students.FirstName ASC, 
+                students.SecondName ASC;
 END //
 
 
 
 
 DROP PROCEDURE IF EXISTS GetStudentsByFaculty//
-CREATE PROCEDURE `GetStudentsByFaculty`	( 	IN `FacultyID` INT  
-                                    	)
+CREATE PROCEDURE `GetStudentsByFaculty` (   IN `FacultyID` INT  
+                                        )
     NO SQL
 BEGIN
-        SELECT 	students.ID 			AS 'ID', 
-                students.LastName 		AS 'Last', 
-                students.FirstName 		AS 'First',  
-                students.SecondName	 	AS 'Second',
-                students.AccountID 		AS 'AccountID',
-                grades.ID 				AS 'GradeID',
-                grades.Num 				AS 'GradeNum',
-                study_groups.ID 		AS 'GroupID',
-                study_groups.GroupNum 	AS 'GroupNum',
-                grades.Degree 			AS 'Degree'		
+        SELECT  students.ID             AS 'ID', 
+                students.LastName       AS 'Last', 
+                students.FirstName      AS 'First',  
+                students.SecondName     AS 'Second',
+                students.AccountID      AS 'AccountID',
+                grades.ID               AS 'GradeID',
+                grades.Num              AS 'GradeNum',
+                study_groups.ID         AS 'GroupID',
+                study_groups.GroupNum   AS 'GroupNum',
+                grades.Degree           AS 'Degree'     
         FROM `students`
-        INNER JOIN `study_groups` 		ON 	study_groups.ID = students.StudyGroupID
-        INNER JOIN `grades` 			ON 	study_groups.GradeID = grades.ID
-        INNER JOIN `specializations` 	ON 	study_groups.SpecializationID =  specializations.ID AND 
-                                        	FacultyID = specializations.FacultyID
-        ORDER BY 	students.LastName ASC, 
+        INNER JOIN `study_groups`       ON  study_groups.ID = students.StudyGroupID
+        INNER JOIN `grades`             ON  study_groups.GradeID = grades.ID
+        INNER JOIN `specializations`    ON  study_groups.SpecializationID =  specializations.ID AND 
+                                            FacultyID = specializations.FacultyID
+        ORDER BY    students.LastName ASC, 
                     students.FirstName ASC, 
                     students.SecondName ASC;
 END //
@@ -1519,28 +1472,28 @@ END //
 
 
 DROP PROCEDURE IF EXISTS GetStudentsByGradeID//
-CREATE PROCEDURE `GetStudentsByGradeID`	(	IN `GradeID` INT,
+CREATE PROCEDURE `GetStudentsByGradeID` (   IN `GradeID` INT,
                                             IN `FacultyID` INT )
     NO SQL
 BEGIN  
-	SELECT DISTINCT 	students.ID 				AS 'StudentID',
-	                    students.LastName			As 'StudentLast',
-	                    students.FirstName 			AS 'StudentFirst',
-	                    students.SecondName			AS 'StudentSecond',
-	                    students.AccountID			AS 'StudentAccID',
-	                    study_groups.ID 			AS 'GroupID',
-	                    grades.ID 					AS 'GradeID',
-	                    grades.Num 					AS 'GroupGrade',
-	                    grades.Degree 				AS 'GroupDegree',	
-	                    study_groups.GroupNum 		AS 'GroupNum',
-	                    disciplines_students.Type 	AS 'Type'
-	FROM `students`
-	INNER JOIN `study_groups` 		ON 	students.StudyGroupID = study_groups.ID AND 
+    SELECT DISTINCT     students.ID                 AS 'StudentID',
+                        students.LastName           As 'StudentLast',
+                        students.FirstName          AS 'StudentFirst',
+                        students.SecondName         AS 'StudentSecond',
+                        students.AccountID          AS 'StudentAccID',
+                        study_groups.ID             AS 'GroupID',
+                        grades.ID                   AS 'GradeID',
+                        grades.Num                  AS 'GroupGrade',
+                        grades.Degree               AS 'GroupDegree',   
+                        study_groups.GroupNum       AS 'GroupNum',
+                        disciplines_students.Type   AS 'Type'
+    FROM `students`
+    INNER JOIN `study_groups`       ON  students.StudyGroupID = study_groups.ID AND 
                                         GradeID = study_groups.GradeID 
-	INNER JOIN `specializations` 	ON 	study_groups.SpecializationID = specializations.ID AND 
+    INNER JOIN `specializations`    ON  study_groups.SpecializationID = specializations.ID AND 
                                         FacultyID = specializations.FacultyID 
-	INNER JOIN `grades`				ON 	study_groups.GradeID = grades.ID
-	ORDER BY 	students.LastName ASC, 
+    INNER JOIN `grades`             ON  study_groups.GradeID = grades.ID
+    ORDER BY    students.LastName ASC, 
                 students.FirstName ASC, 
                 students.SecondName ASC;
 END //
@@ -1548,342 +1501,342 @@ END //
 
 
 DROP FUNCTION IF EXISTS CreateStudent//
-CREATE FUNCTION `CreateStudent`(	`Last` 			VARCHAR(30) CHARSET utf8, 
-									`First` 		VARCHAR(30) CHARSET utf8, 
-									`Second` 		VARCHAR(30) CHARSET utf8, 
-									`GradeID` 		INT, 
-									`GroupN` 		INT, 
-									`FacultyID` 	INT, 
-									`Code` 			VARCHAR(40) CHARSET utf8 
-								) 	RETURNS int(11)
+CREATE FUNCTION `CreateStudent`(    `Last`          VARCHAR(30) CHARSET utf8, 
+                                    `First`         VARCHAR(30) CHARSET utf8, 
+                                    `Second`        VARCHAR(30) CHARSET utf8, 
+                                    `GradeID`       INT, 
+                                    `GroupN`        INT, 
+                                    `FacultyID`     INT, 
+                                    `Code`          VARCHAR(40) CHARSET utf8 
+                                )   RETURNS int(11)
     NO SQL
 BEGIN 
-	DECLARE accID, StudyGroupID INT;
-
-	IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
-		RETURN -1;
-	END IF;
-
-	-- find studygroup
-	SET StudyGroupID = -1;
-	SELECT study_groups.ID 
-	INTO StudyGroupID 
-	FROM `study_groups` 
-	INNER JOIN `specializations` 	ON 	study_groups.SpecializationID = specializations.ID AND
-										FacultyID = specializations.FacultyID
-	WHERE 	study_groups.GradeID = GradeID 	AND 
-			study_groups.ID = GroupN
-	LIMIT 1;
-	IF StudyGroupID <= 0 THEN
-		RETURN -1;
-	END IF; 
-
-
-	-- try to find account with same activation code
-	SET accID = -1;
-	SELECT accounts.ID 
-	INTO accID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF accID > 0 THEN
-		RETURN -2;
-	END IF;
-
-	-- create new account
-	INSERT INTO `accounts` 
-			( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
-	VALUES 	( NULL, NULL, NULL, 1, Code);
-		
-
-	-- get created account ID
-	SET accID = -1;
-	SELECT accounts.ID 
-	INTO accID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF accID <= 0 THEN
-		RETURN -3;
-	END IF; 
-	
-
-	-- create student
-	INSERT INTO `students` 
-			(students.StudyGroupID, students.AccountID, students.LastName, students.FirstName, students.SecondName)
-	VALUES 	(StudyGroupID, accID, Last, First, Second);
-	RETURN 0;
+    DECLARE accID, StudyGroupID INT;
+
+    IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
+        RETURN -1;
+    END IF;
+
+    # find studygroup
+    SET StudyGroupID = -1;
+    SELECT study_groups.ID 
+    INTO StudyGroupID 
+    FROM `study_groups` 
+    INNER JOIN `specializations`    ON  study_groups.SpecializationID = specializations.ID AND
+                                        FacultyID = specializations.FacultyID
+    WHERE   study_groups.GradeID = GradeID  AND 
+            study_groups.ID = GroupN
+    LIMIT 1;
+    IF StudyGroupID <= 0 THEN
+        RETURN -1;
+    END IF; 
+
+
+    # try to find account with same activation code
+    SET accID = -1;
+    SELECT accounts.ID 
+    INTO accID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF accID > 0 THEN
+        RETURN -2;
+    END IF;
+
+    # create new account
+    INSERT INTO `accounts` 
+            ( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
+    VALUES  ( NULL, NULL, NULL, 1, Code);
+        
+
+    # get created account ID
+    SET accID = -1;
+    SELECT accounts.ID 
+    INTO accID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF accID <= 0 THEN
+        RETURN -3;
+    END IF; 
+    
+
+    # create student
+    INSERT INTO `students` 
+            (students.StudyGroupID, students.AccountID, students.LastName, students.FirstName, students.SecondName)
+    VALUES  (StudyGroupID, accID, Last, First, Second);
+    RETURN 0;
 END //
 
 
--- TODO: ?#$@! magic
+# TODO: ?#$@! magic
 DROP FUNCTION IF EXISTS CreateStudentEx//
-CREATE FUNCTION `CreateStudentEx`(	`Last` 			VARCHAR(30) CHARSET utf8, 
-									`First` 		VARCHAR(30) CHARSET utf8, 
-									`Second` 		VARCHAR(30) CHARSET utf8, 
-									`GradeNum` 		INT,
-									`groupNum`		INT,
-									`Degree`		VARCHAR(20) CHARSET utf8, 
-									`SpecName` 		VARCHAR(50) CHARSET utf8, 
-									`FacultyID` 	INT, 
-									`Code` 			VARCHAR(40) CHARSET utf8 
-								) 	RETURNS int(11)
+CREATE FUNCTION `CreateStudentEx`(  `Last`          VARCHAR(30) CHARSET utf8, 
+                                    `First`         VARCHAR(30) CHARSET utf8, 
+                                    `Second`        VARCHAR(30) CHARSET utf8, 
+                                    `GradeNum`      INT,
+                                    `groupNum`      INT,
+                                    `Degree`        VARCHAR(20) CHARSET utf8, 
+                                    `SpecName`      VARCHAR(50) CHARSET utf8, 
+                                    `FacultyID`     INT, 
+                                    `Code`          VARCHAR(40) CHARSET utf8 
+                                )   RETURNS int(11)
     NO SQL
 BEGIN 
-	DECLARE curID, gradeID, specID, SG_ID INT;
-
-	IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
-		RETURN -1;
-	END IF;
-
-	SET gradeID = -1;
-	SELECT grades.ID 
-	INTO gradeID
-	FROM `grades`
-	WHERE 	grades.Num = GradeNum AND
-			grades.Degree = Degree
-	LIMIT 1;
-	IF gradeID <= 0 THEN
-		INSERT INTO `grades`
-				(grades.Num, grades.Degree)
-		VALUES 	(GradeNum, Degree);
-
-		SET gradeID = -1;
-		SELECT grades.ID 
-		INTO gradeID
-		FROM `grades`
-		WHERE 	grades.Num = GradeNum AND
-				grades.Degree = Degree
-		LIMIT 1;
-	END IF;
-
-
-	SET SG_ID = -1;
-	SELECT study_groups.ID 
-	INTO SG_ID
-	FROM `study_groups`
-	INNER JOIN `specializations` ON study_groups.SpecializationID = specializations.ID
- 	WHERE 	study_groups.GroupNum = groupNum AND
-			study_groups.GradeID = gradeID AND
-			specializations.FacultyID = FacultyID 
-	LIMIT 1;
-	IF SG_ID <= 0 THEN
-		SET specID = -1;
-		SELECT specializations.ID
-		INTO specID
-		FROM `specializations`
-		WHERE 	( 	specializations.Name = SpecName OR 
-					( SpecName = '' AND specializations.Name IS NULL ) 
-				) 	AND
-				specializations.FacultyID = FacultyID
-		LIMIT 1;
-		IF specID <= 0 THEN
-			INSERT INTO `specializations`
-					(specializations.Name, specializations.Abbr, specializations.FacultyID )
-			VALUES 	(SpecName, NULL, FacultyID);
-
-			SET specID = -1;
-			SELECT specializations.ID
-			INTO specID
-			FROM `specializations`
-			WHERE 	specializations.Name = SpecName AND
-					specializations.FacultyID = FacultyID
-			LIMIT 1;
-		END IF;
-
-		INSERT INTO `study_groups`
-				(study_groups.GradeID, study_groups.GroupNum, study_groups.SpecializationID)
-		VALUES 	(gradeID, groupNum, specID);
-
-		SET SG_ID = -1;
-		SELECT study_groups.ID 
-		INTO SG_ID
-		FROM `study_groups`
-		INNER JOIN `specializations` ON study_groups.SpecializationID = specializations.ID
-	 	WHERE 	study_groups.GroupNum = groupNum AND
-				study_groups.GradeID = gradeID AND
-				specializations.FacultyID = FacultyID 
-		LIMIT 1;
-		IF SG_ID <= 0 THEN
-			RETURN -1;
-		END IF;
-	END IF;
-
-	SET curID = -1;
-	SELECT accounts.ID 
-	INTO curID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF curID > 0 THEN
-		RETURN -2;
-	END IF;
-
-	-- TODO: user roles
-	INSERT INTO `accounts` 
-			( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
-	VALUES 	( NULL, NULL, NULL, 1, Code);
-		
-	SET curID = -1;
-	SELECT accounts.ID 
-	INTO curID
-	FROM `accounts`
-	WHERE accounts.ActivationCode = Code
-	LIMIT 1;
-	IF curID <= 0 THEN
-		RETURN -3;
-	END IF; 
-	
-	INSERT INTO `students` 
-			(students.StudyGroupID, students.AccountID, students.LastName, students.FirstName, students.SecondName)
-	VALUES 	(SG_ID, curID, Last, First, Second);
-	RETURN 0;
+    DECLARE curID, gradeID, specID, SG_ID INT;
+
+    IF Code IS NULL OR Last IS NULL OR First IS NULL THEN
+        RETURN -1;
+    END IF;
+
+    SET gradeID = -1;
+    SELECT grades.ID 
+    INTO gradeID
+    FROM `grades`
+    WHERE   grades.Num = GradeNum AND
+            grades.Degree = Degree
+    LIMIT 1;
+    IF gradeID <= 0 THEN
+        INSERT INTO `grades`
+                (grades.Num, grades.Degree)
+        VALUES  (GradeNum, Degree);
+
+        SET gradeID = -1;
+        SELECT grades.ID 
+        INTO gradeID
+        FROM `grades`
+        WHERE   grades.Num = GradeNum AND
+                grades.Degree = Degree
+        LIMIT 1;
+    END IF;
+
+
+    SET SG_ID = -1;
+    SELECT study_groups.ID 
+    INTO SG_ID
+    FROM `study_groups`
+    INNER JOIN `specializations` ON study_groups.SpecializationID = specializations.ID
+    WHERE   study_groups.GroupNum = groupNum AND
+            study_groups.GradeID = gradeID AND
+            specializations.FacultyID = FacultyID 
+    LIMIT 1;
+    IF SG_ID <= 0 THEN
+        SET specID = -1;
+        SELECT specializations.ID
+        INTO specID
+        FROM `specializations`
+        WHERE   (   specializations.Name = SpecName OR 
+                    ( SpecName = '' AND specializations.Name IS NULL ) 
+                )   AND
+                specializations.FacultyID = FacultyID
+        LIMIT 1;
+        IF specID <= 0 THEN
+            INSERT INTO `specializations`
+                    (specializations.Name, specializations.Abbr, specializations.FacultyID )
+            VALUES  (SpecName, NULL, FacultyID);
+
+            SET specID = -1;
+            SELECT specializations.ID
+            INTO specID
+            FROM `specializations`
+            WHERE   specializations.Name = SpecName AND
+                    specializations.FacultyID = FacultyID
+            LIMIT 1;
+        END IF;
+
+        INSERT INTO `study_groups`
+                (study_groups.GradeID, study_groups.GroupNum, study_groups.SpecializationID)
+        VALUES  (gradeID, groupNum, specID);
+
+        SET SG_ID = -1;
+        SELECT study_groups.ID 
+        INTO SG_ID
+        FROM `study_groups`
+        INNER JOIN `specializations` ON study_groups.SpecializationID = specializations.ID
+        WHERE   study_groups.GroupNum = groupNum AND
+                study_groups.GradeID = gradeID AND
+                specializations.FacultyID = FacultyID 
+        LIMIT 1;
+        IF SG_ID <= 0 THEN
+            RETURN -1;
+        END IF;
+    END IF;
+
+    SET curID = -1;
+    SELECT accounts.ID 
+    INTO curID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF curID > 0 THEN
+        RETURN -2;
+    END IF;
+
+    # TODO: user roles
+    INSERT INTO `accounts` 
+            ( accounts.Login , accounts.Password , accounts.EMail, accounts.UserRoleID, accounts.ActivationCode )
+    VALUES  ( NULL, NULL, NULL, 1, Code);
+        
+    SET curID = -1;
+    SELECT accounts.ID 
+    INTO curID
+    FROM `accounts`
+    WHERE accounts.ActivationCode = Code
+    LIMIT 1;
+    IF curID <= 0 THEN
+        RETURN -3;
+    END IF; 
+    
+    INSERT INTO `students` 
+            (students.StudyGroupID, students.AccountID, students.LastName, students.FirstName, students.SecondName)
+    VALUES  (SG_ID, curID, Last, First, Second);
+    RETURN 0;
 END //
 
 
 
--- DROP PROCEDURE IF EXISTS SearchStudents//
--- CREATE PROCEDURE `SearchStudents`	(	IN `GradeID` INT, 
--- 										IN `GroupN` INT,
--- 										IN `FacultyID` INT,
--- 										IN `Name` VARCHAR(100) CHARSET utf8
--- 									)
---     NO SQL
--- BEGIN  
--- 	SELECT	students.ID 			AS 'ID',
--- 			students.LastName		AS 'Last',	
--- 			students.FirstName 		AS 'First',
--- 			students.SecondName		AS 'Second',
--- 			grades.ID 				AS 'GradeID',
--- 			grades.Num 				AS 'GradeNum',
--- 			grades.Degree 			AS 'Degree',
--- 			study_groups.ID 		AS 'GroupID',
--- 			study_groups.GroupNum 	AS 'GroupNum'
--- 	FROM `students`
--- 	INNER JOIN `study_groups` 		ON 	students.StudyGroupID = study_groups.ID AND
--- 										GradeID = study_groups.GradeID AND
--- 										( GroupN = 0  OR GroupN = study_groups.GroupNum )
--- 	INNER JOIN `specializations`	ON 	study_groups.SpecializationID = specializations.ID
--- 	INNER JOIN `grades` 			ON 	study_groups.GradeID = grades.ID 
--- 	WHERE 	FacultyID = specializations.FacultyID AND
--- 			CONCAT(students.LastName, students.FirstName, students.SecondName) LIKE CONCAT("%",Name,"%")
--- 	ORDER BY grades.ID ASC, study_groups.GroupNum ASC;
--- END //
+# DROP PROCEDURE IF EXISTS SearchStudents//
+# CREATE PROCEDURE `SearchStudents`    (   IN `GradeID` INT, 
+#                                      IN `GroupN` INT,
+#                                      IN `FacultyID` INT,
+#                                      IN `Name` VARCHAR(100) CHARSET utf8
+#                                  )
+#     NO SQL
+# BEGIN  
+#  SELECT  students.ID             AS 'ID',
+#          students.LastName       AS 'Last',  
+#          students.FirstName      AS 'First',
+#          students.SecondName     AS 'Second',
+#          grades.ID               AS 'GradeID',
+#          grades.Num              AS 'GradeNum',
+#          grades.Degree           AS 'Degree',
+#          study_groups.ID         AS 'GroupID',
+#          study_groups.GroupNum   AS 'GroupNum'
+#  FROM `students`
+#  INNER JOIN `study_groups`       ON  students.StudyGroupID = study_groups.ID AND
+#                                      GradeID = study_groups.GradeID AND
+#                                      ( GroupN = 0  OR GroupN = study_groups.GroupNum )
+#  INNER JOIN `specializations`    ON  study_groups.SpecializationID = specializations.ID
+#  INNER JOIN `grades`             ON  study_groups.GradeID = grades.ID 
+#  WHERE   FacultyID = specializations.FacultyID AND
+#          CONCAT(students.LastName, students.FirstName, students.SecondName) LIKE CONCAT("%",Name,"%")
+#  ORDER BY grades.ID ASC, study_groups.GroupNum ASC;
+# END //
 
 
 
 DROP PROCEDURE IF EXISTS SearchStudents//
-CREATE PROCEDURE `SearchStudents`	(	IN `GradeID` INT, 
-										IN `StudyGroupID` INT,
-										IN `FacultyID` INT,
-										IN `Name` VARCHAR(100) CHARSET utf8,
-									 	IN `DisciplineID` INT
-									)
+CREATE PROCEDURE `SearchStudents`   (   IN `GradeID` INT, 
+                                        IN `StudyGroupID` INT,
+                                        IN `FacultyID` INT,
+                                        IN `Name` VARCHAR(100) CHARSET utf8,
+                                        IN `DisciplineID` INT
+                                    )
     NO SQL
 BEGIN  
-	SELECT	students.ID 			AS 'ID',
-			students.LastName		AS 'Last',	
-			students.FirstName 		AS 'First',
-			students.SecondName		AS 'Second',
-			grades.ID 				AS 'GradeID',
-			grades.Num 				AS 'GradeNum',
-			grades.Degree 			AS 'Degree',
-			study_groups.ID 		AS 'GroupID',
-			study_groups.GroupNum 	AS 'GroupNum'
-	FROM `students`
-	INNER JOIN `study_groups` 			ON 	students.StudyGroupID = study_groups.ID AND
-											GradeID = study_groups.GradeID AND
-											( StudyGroupID = 0  OR StudyGroupID = study_groups.ID )
-	INNER JOIN `specializations`		ON 	study_groups.SpecializationID = specializations.ID AND
-											FacultyID = specializations.FacultyID
-	INNER JOIN `grades`					ON 	study_groups.GradeID = grades.ID 
-	LEFT JOIN 	`disciplines_groups`	ON 	study_groups.ID = disciplines_groups.StudyGroupID AND
-											DisciplineID = disciplines_groups.DisciplineID
-	WHERE 	CONCAT(students.LastName, students.FirstName, students.SecondName) LIKE CONCAT("%",Name,"%") AND
-			NOT InternalIsStudentAttached(students.ID, DisciplineID) AND
-			disciplines_groups.ID IS NULL
-	ORDER BY 	grades.ID ASC, 
-				study_groups.GroupNum ASC;
+    SELECT  students.ID             AS 'ID',
+            students.LastName       AS 'Last',  
+            students.FirstName      AS 'First',
+            students.SecondName     AS 'Second',
+            grades.ID               AS 'GradeID',
+            grades.Num              AS 'GradeNum',
+            grades.Degree           AS 'Degree',
+            study_groups.ID         AS 'GroupID',
+            study_groups.GroupNum   AS 'GroupNum'
+    FROM `students`
+    INNER JOIN `study_groups`           ON  students.StudyGroupID = study_groups.ID AND
+                                            GradeID = study_groups.GradeID AND
+                                            ( StudyGroupID = 0  OR StudyGroupID = study_groups.ID )
+    INNER JOIN `specializations`        ON  study_groups.SpecializationID = specializations.ID AND
+                                            FacultyID = specializations.FacultyID
+    INNER JOIN `grades`                 ON  study_groups.GradeID = grades.ID 
+    LEFT JOIN   `disciplines_groups`    ON  study_groups.ID = disciplines_groups.StudyGroupID AND
+                                            DisciplineID = disciplines_groups.DisciplineID
+    WHERE   CONCAT(students.LastName, students.FirstName, students.SecondName) LIKE CONCAT("%",Name,"%") AND
+            NOT InternalIsStudentAttached(students.ID, DisciplineID) AND
+            disciplines_groups.ID IS NULL
+    ORDER BY    grades.ID ASC, 
+                study_groups.GroupNum ASC;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetStudentsForDiscipline//
-CREATE PROCEDURE `GetStudentsForDiscipline`	(	IN `TeacherID` INT,
-												IN `DisciplineID` INT 
-											)
+CREATE PROCEDURE `GetStudentsForDiscipline` (   IN `TeacherID` INT,
+                                                IN `DisciplineID` INT 
+                                            )
     NO SQL
-BEGIN  				 
-    IF NOT InternalIsTeacherBinded(TeacherID, DisciplineID) 
+BEGIN                
+    IF NOT InternalIsTeacherBindeded(TeacherID, DisciplineID) 
     THEN
-		SELECT 	NULL AS 'ID'
-		LIMIT 0;
+        SELECT  NULL AS 'ID'
+        LIMIT 0;
     
-	ELSE
-		SELECT DISTINCT 	students.ID 				AS 'ID',
-							students.LastName			AS 'Last',
-							students.FirstName			AS 'First',
-							students.SecondName 		AS 'Second',
-							grades.ID  					AS 'GradeID',
-							grades.Num 					AS 'GradeNum',
-							grades.Degree 				AS 'Degree',
-							study_groups.ID 			AS 'GroupID',
-							study_groups.GroupNum 		AS 'GroupNum',
-							disciplines_students.Type 	AS 'Type'
-		FROM `students`
-		INNER JOIN `study_groups` 			ON 	students.StudyGroupID = study_groups.ID
-		INNER JOIN `grades` 				ON 	grades.ID = study_groups.GradeID
-		LEFT JOIN `disciplines_students` 	ON 	DisciplineID = disciplines_students.DisciplineID AND
-												students.ID = disciplines_students.StudentID
-		LEFT JOIN `disciplines_groups`		ON 	DisciplineID = disciplines_groups.DisciplineID AND
-												study_groups.ID = disciplines_groups.StudyGroupID
-		INNER JOIN `disciplines` 			ON  DisciplineID = disciplines.ID
-		WHERE 	disciplines_groups.ID IS NOT NULL OR
-				disciplines_students.ID IS NOT NULL
-		ORDER BY 	grades.ID = disciplines.GradeID DESC,
-					grades.ID ASC, 
-					study_groups.GroupNum ASC, 
-					students.LastName ASC, 
-					students.FirstName ASC, 
-					students.SecondName ASC;
-	END IF;
+    ELSE
+        SELECT DISTINCT     students.ID                 AS 'ID',
+                            students.LastName           AS 'Last',
+                            students.FirstName          AS 'First',
+                            students.SecondName         AS 'Second',
+                            grades.ID                   AS 'GradeID',
+                            grades.Num                  AS 'GradeNum',
+                            grades.Degree               AS 'Degree',
+                            study_groups.ID             AS 'GroupID',
+                            study_groups.GroupNum       AS 'GroupNum',
+                            disciplines_students.Type   AS 'Type'
+        FROM `students`
+        INNER JOIN `study_groups`           ON  students.StudyGroupID = study_groups.ID
+        INNER JOIN `grades`                 ON  grades.ID = study_groups.GradeID
+        LEFT JOIN `disciplines_students`    ON  DisciplineID = disciplines_students.DisciplineID AND
+                                                students.ID = disciplines_students.StudentID
+        LEFT JOIN `disciplines_groups`      ON  DisciplineID = disciplines_groups.DisciplineID AND
+                                                study_groups.ID = disciplines_groups.StudyGroupID
+        INNER JOIN `disciplines`            ON  DisciplineID = disciplines.ID
+        WHERE   disciplines_groups.ID IS NOT NULL OR
+                disciplines_students.ID IS NOT NULL
+        ORDER BY    grades.ID = disciplines.GradeID DESC,
+                    grades.ID ASC, 
+                    study_groups.GroupNum ASC, 
+                    students.LastName ASC, 
+                    students.FirstName ASC, 
+                    students.SecondName ASC;
+    END IF;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetStudentsForRating//
-CREATE PROCEDURE `GetStudentsForRating`		(	IN `TeacherID` INT,
-												IN `DisciplineID` INT 
-											)
+CREATE PROCEDURE `GetStudentsForRating`     (   IN `TeacherID` INT,
+                                                IN `DisciplineID` INT 
+                                            )
     NO SQL
-BEGIN 				 
-	IF NOT InternalIsTeacherBinded(TeacherID, DisciplineID) 
-	THEN
-		SELECT 	NULL AS 'ID'
-		LIMIT 0;
-	ELSE 
-		SELECT DISTINCT 	students.ID 			AS 'ID',
-							students.LastName		AS 'Last',
-							students.FirstName 		AS 'First',
-							students.SecondName		AS 'Second',
-							grades.ID 				AS 'GradeID',
-							grades.Num 				AS 'GradeNum',
-							grades.Degree 			AS 'Degree',
-							study_groups.ID 		AS 'GroupID',
-							study_groups.GroupNum 	AS 'GroupNum',
-							(disciplines_students.Type IS NOT NULL AND disciplines_students.Type = 'attach') AS 'isAttached'
-		FROM `students`
-		INNER JOIN `study_groups` 			ON students.StudyGroupID = study_groups.ID
-		INNER JOIN `grades` 				ON grades.ID = study_groups.GradeID
-		LEFT JOIN 	`disciplines_students`	ON 	DisciplineID = disciplines_students.DisciplineID AND
-												students.ID = disciplines_students.StudentID
-		WHERE 	InternalIsStudentAttached(students.ID, DisciplineID)
-		ORDER BY 	grades.ID ASC, 
-					study_groups.GroupNum ASC, 
-					students.LastName ASC, 
-					students.FirstName ASC, 
-					students.SecondName ASC;
-	END IF;
+BEGIN                
+    IF NOT InternalIsTeacherBindeded(TeacherID, DisciplineID) 
+    THEN
+        SELECT  NULL AS 'ID'
+        LIMIT 0;
+    ELSE 
+        SELECT DISTINCT     students.ID             AS 'ID',
+                            students.LastName       AS 'Last',
+                            students.FirstName      AS 'First',
+                            students.SecondName     AS 'Second',
+                            grades.ID               AS 'GradeID',
+                            grades.Num              AS 'GradeNum',
+                            grades.Degree           AS 'Degree',
+                            study_groups.ID         AS 'GroupID',
+                            study_groups.GroupNum   AS 'GroupNum',
+                            (disciplines_students.Type IS NOT NULL AND disciplines_students.Type = 'attach') AS 'isAttached'
+        FROM `students`
+        INNER JOIN `study_groups`           ON students.StudyGroupID = study_groups.ID
+        INNER JOIN `grades`                 ON grades.ID = study_groups.GradeID
+        LEFT JOIN   `disciplines_students`  ON  DisciplineID = disciplines_students.DisciplineID AND
+                                                students.ID = disciplines_students.StudentID
+        WHERE   InternalIsStudentAttached(students.ID, DisciplineID)
+        ORDER BY    grades.ID ASC, 
+                    study_groups.GroupNum ASC, 
+                    students.LastName ASC, 
+                    students.FirstName ASC, 
+                    students.SecondName ASC;
+    END IF;
 END //
 
 
@@ -1892,62 +1845,62 @@ END //
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: disciplines
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: disciplines
+# -------------------------------------------------------------------------------------------
 
 
 DROP PROCEDURE IF EXISTS GetDisciplines//
 CREATE PROCEDURE `GetDisciplines` (  IN `FacultyID` INT )
     NO SQL
 BEGIN
-	SELECT 	disciplines.ID    		AS 'ID', 
-			subjects.ID    			AS 'SubjectID', 
-			subjects.Name    		AS 'SubjectName', 
-			disciplines.ExamType 	AS 'ExamType',
-			departments.Name   		AS 'DepName',
-			InternalIsMapCreated(disciplines.ID)   AS 'isMapCreated'
-	FROM `disciplines`
-	INNER JOIN  `subjects`    ON  disciplines.SubjectID = subjects.ID 
-	INNER JOIN  `teachers`    ON  disciplines.AuthorID = teachers.ID
-	INNER JOIN  `departments`   ON  teachers.DepartmentID = departments.ID 
-	WHERE  @CurrentSemesterID = disciplines.SemesterID AND
-		disciplines.FacultyID = FacultyID
-	ORDER BY departments.Name ASC;   
+    SELECT  disciplines.ID          AS 'ID', 
+            subjects.ID             AS 'SubjectID', 
+            subjects.Name           AS 'SubjectName', 
+            disciplines.ExamType    AS 'ExamType',
+            departments.Name        AS 'DepName',
+            InternalIsMapCreated(disciplines.ID)   AS 'isMapCreated'
+    FROM `disciplines`
+    INNER JOIN  `subjects`    ON  disciplines.SubjectID = subjects.ID 
+    INNER JOIN  `teachers`    ON  disciplines.AuthorID = teachers.ID
+    INNER JOIN  `departments`   ON  teachers.DepartmentID = departments.ID 
+    WHERE  @CurrentSemesterID = disciplines.SemesterID AND
+        disciplines.FacultyID = FacultyID
+    ORDER BY departments.Name ASC;   
 END //
 
 
--- TODO bad query
+# TODO bad query
 DROP PROCEDURE IF EXISTS GetDisciplinesForTeacher//
 CREATE PROCEDURE `GetDisciplinesForTeacher`(IN `TeacherID` INT)
     NO SQL
 BEGIN  
-	SELECT DISTINCT disciplines.ID 						AS 'ID',
-                    disciplines.ExamType 				AS 'ExamType',
-					disciplines.GradeID 				AS 'GradeID',
-					grades.Num 							AS 'GradeNum',
-					grades.Degree 						AS 'Degree',
-                    study_groups.ID 					AS 'GroupID', 
-                    study_groups.GroupNum 				AS 'GroupNum', 
-                    study_groups.Name 					AS 'GroupName',
-                    subjects.ID 						AS 'SubjectID',
-                    subjects.Name 						AS 'SubjectName',
-                    (TeacherID = disciplines.AuthorID) 		AS 'isAuthor',
-                    InternalIsMapCreated(disciplines.ID)	AS 'isMapCreated',
-                    disciplines.isLocked 					AS 'isLocked'
+    SELECT DISTINCT disciplines.ID                      AS 'ID',
+                    disciplines.ExamType                AS 'ExamType',
+                    disciplines.GradeID                 AS 'GradeID',
+                    grades.Num                          AS 'GradeNum',
+                    grades.Degree                       AS 'Degree',
+                    study_groups.ID                     AS 'GroupID', 
+                    study_groups.GroupNum               AS 'GroupNum', 
+                    study_groups.Name                   AS 'GroupName',
+                    subjects.ID                         AS 'SubjectID',
+                    subjects.Name                       AS 'SubjectName',
+                    (TeacherID = disciplines.AuthorID)      AS 'isAuthor',
+                    InternalIsMapCreated(disciplines.ID)    AS 'isMapCreated',
+                    disciplines.isLocked                    AS 'isLocked'
     FROM `disciplines_groups`
-    RIGHT JOIN `disciplines` 			ON 	disciplines_groups.DisciplineID = disciplines.ID OR
-    										disciplines_groups.DisciplineID IS NULL 
-	INNER JOIN 	`grades` 				ON  grades.ID = disciplines.GradeID
-    INNER JOIN `disciplines_teachers` 	ON 	disciplines.ID = disciplines_teachers.DisciplineID AND 
-											TeacherID = disciplines_teachers.TeacherID 
-    INNER JOIN `subjects`				ON 	subjects.ID = disciplines.SubjectID
-    LEFT JOIN  `study_groups` 			ON 	study_groups.ID = disciplines_groups.StudyGroupID
-    WHERE 	disciplines.SemesterID = @CurrentSemesterID
-    ORDER BY 	grades.ID 				ASC, 
-    			subjects.Name 			ASC,
-				disciplines.ID			ASC,
-    			study_groups.GroupNum 	ASC;
+    RIGHT JOIN `disciplines`            ON  disciplines_groups.DisciplineID = disciplines.ID OR
+                                            disciplines_groups.DisciplineID IS NULL 
+    INNER JOIN  `grades`                ON  grades.ID = disciplines.GradeID
+    INNER JOIN `disciplines_teachers`   ON  disciplines.ID = disciplines_teachers.DisciplineID AND 
+                                            TeacherID = disciplines_teachers.TeacherID 
+    INNER JOIN `subjects`               ON  subjects.ID = disciplines.SubjectID
+    LEFT JOIN  `study_groups`           ON  study_groups.ID = disciplines_groups.StudyGroupID
+    WHERE   disciplines.SemesterID = @CurrentSemesterID
+    ORDER BY    grades.ID               ASC, 
+                subjects.Name           ASC,
+                disciplines.ID          ASC,
+                study_groups.GroupNum   ASC;
 END //
 
 
@@ -1956,195 +1909,195 @@ DROP PROCEDURE IF EXISTS GetDisciplinesForStudent//
 CREATE PROCEDURE `GetDisciplinesForStudent`(IN `StudentID` INT)
     NO SQL
 BEGIN  
-	SELECT 	 			disciplines.ID 			AS 'ID', 
-						subjects.ID 			AS 'SubjectID', 
-						subjects.Name 			AS 'SubjectName', 
-						disciplines.ExamType 	AS 'ExamType',
-						teachers.LastName 		AS 'Last', 
-						teachers.FirstName 		AS 'First',
-						teachers.SecondName 	AS 'Second', 
-						GetRateForDisc(StudentID, disciplines.ID ) 		AS 'Rate', 
-						GetMaxRateForDisc(disciplines.ID ) 				AS 'MaxCurrentRate',
-						InternalIsMapCreated(disciplines.ID)			AS 'isMapCreated'
-	FROM `disciplines`
-	INNER JOIN 	`subjects` 			ON 	disciplines.SubjectID = subjects.ID 
-	INNER JOIN 	`teachers` 			ON 	disciplines.AuthorID = teachers.ID								
-	WHERE 	@CurrentSemesterID = disciplines.SemesterID AND 
-			InternalIsStudentAttached(StudentID, disciplines.ID)
-	ORDER BY disciplines.ExamType ASC, Rate DESC;    
+    SELECT              disciplines.ID          AS 'ID', 
+                        subjects.ID             AS 'SubjectID', 
+                        subjects.Name           AS 'SubjectName', 
+                        disciplines.ExamType    AS 'ExamType',
+                        teachers.LastName       AS 'Last', 
+                        teachers.FirstName      AS 'First',
+                        teachers.SecondName     AS 'Second', 
+                        GetRateForDisc(StudentID, disciplines.ID )      AS 'Rate', 
+                        GetMaxRateForDisc(disciplines.ID )              AS 'MaxCurrentRate',
+                        InternalIsMapCreated(disciplines.ID)            AS 'isMapCreated'
+    FROM `disciplines`
+    INNER JOIN  `subjects`          ON  disciplines.SubjectID = subjects.ID 
+    INNER JOIN  `teachers`          ON  disciplines.AuthorID = teachers.ID                              
+    WHERE   @CurrentSemesterID = disciplines.SemesterID AND 
+            InternalIsStudentAttached(StudentID, disciplines.ID)
+    ORDER BY disciplines.ExamType ASC, Rate DESC;    
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetMapForStudent//
-CREATE PROCEDURE `GetMapForStudent`	(	IN `StudentID` INT, 
-										IN `DisciplineID` INT
-									)
-	NO SQL
+CREATE PROCEDURE `GetMapForStudent` (   IN `StudentID` INT, 
+                                        IN `DisciplineID` INT
+                                    )
+    NO SQL
 BEGIN  
-	SELECT 	modules.ID 				AS 'ModuleID', 
-			modules.Name 			AS 'ModuleName', 
-			submodules.ID 			AS 'SubmoduleID', 
-			submodules.Name 		AS 'SubModuleName', 
-			submodules.Description 	AS 'SubmoduleDescription', 
-			submodules.MaxRate,
-			submodules.Type 		AS 'SubmoduleControl',
-			rating_table.Rate, 
-			rating_table.Date,
-			modules.Type 			AS 'ModuleType'
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID AND 
-									DisciplineID = modules.DisciplineID
-	LEFT JOIN `rating_table` 	ON 	submodules.ID = rating_table.SubmoduleID AND 
-									StudentID = rating_table.StudentID 
-
-
-	WHERE 	modules.Type != 2 OR
-			submodules.ID = 
-				(	@tmp = (SELECT submodules.ID
-					FROM `submodules`
-					INNER JOIN `rating_table` ON rating_table.SubModuleID = submodules.ID
-					WHERE 	submodules.ModuleID = modules.ID AND
-							rating_table.StudentID = StudentID
-					ORDER BY submodules.OrderNum DESC
-					LIMIT 1)
-				)  OR
-				(@tmp IS NULL AND submodules.OrderNum = 1)
-	ORDER BY 	modules.Type ^ 1 ASC,
-					-- 1, 3, 2, 4 ASC
-				modules.OrderNum ASC, 
-				submodules.OrderNum ASC;
+    SELECT  modules.ID              AS 'ModuleID', 
+            modules.Name            AS 'ModuleName', 
+            submodules.ID           AS 'SubmoduleID', 
+            submodules.Name         AS 'SubModuleName', 
+            submodules.Description  AS 'SubmoduleDescription', 
+            submodules.MaxRate,
+            submodules.Type         AS 'SubmoduleControl',
+            rating_table.Rate, 
+            rating_table.Date,
+            modules.Type            AS 'ModuleType'
+    FROM `submodules`
+    INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID AND 
+                                    DisciplineID = modules.DisciplineID
+    LEFT JOIN `rating_table`    ON  submodules.ID = rating_table.SubmoduleID AND 
+                                    StudentID = rating_table.StudentID 
+
+
+    WHERE   modules.Type != 2 OR
+            submodules.ID = 
+                (   @tmp = (SELECT submodules.ID
+                    FROM `submodules`
+                    INNER JOIN `rating_table` ON rating_table.SubModuleID = submodules.ID
+                    WHERE   submodules.ModuleID = modules.ID AND
+                            rating_table.StudentID = StudentID
+                    ORDER BY submodules.OrderNum DESC
+                    LIMIT 1)
+                )  OR
+                (@tmp IS NULL AND submodules.OrderNum = 1)
+    ORDER BY    modules.Type ^ 1 ASC,
+                    # 1, 3, 2, 4 ASC
+                modules.OrderNum ASC, 
+                submodules.OrderNum ASC;
 END //
 
 
 DROP PROCEDURE IF EXISTS GetMapForStudentExam//
-CREATE PROCEDURE `GetMapForStudentExam`	(	IN `StudentID` INT, 
-											IN `DisciplineID` INT
-										)
-	NO SQL
+CREATE PROCEDURE `GetMapForStudentExam` (   IN `StudentID` INT, 
+                                            IN `DisciplineID` INT
+                                        )
+    NO SQL
 BEGIN  
-	SELECT 	modules.ID 				AS 'ModuleID', 
-			modules.Name 			AS 'ModuleName', 
-			submodules.ID 			AS 'SubmoduleID', 
-			submodules.Name 		AS 'SubModuleName', 
-			submodules.Description 	AS 'SubmoduleDescription', 
-			submodules.MaxRate,
-			submodules.Type 		AS 'SubmoduleControl',
-			rating_table.Rate, 
-			rating_table.Date,
-			modules.Type 			AS 'ModuleType'
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID AND 
-									DisciplineID = modules.DisciplineID
-	LEFT JOIN `rating_table` 	ON 	submodules.ID = rating_table.SubmoduleID AND 
-									StudentID = rating_table.StudentID 
-	ORDER BY 	modules.OrderNum ASC, 
-				submodules.OrderNum ASC;
+    SELECT  modules.ID              AS 'ModuleID', 
+            modules.Name            AS 'ModuleName', 
+            submodules.ID           AS 'SubmoduleID', 
+            submodules.Name         AS 'SubModuleName', 
+            submodules.Description  AS 'SubmoduleDescription', 
+            submodules.MaxRate,
+            submodules.Type         AS 'SubmoduleControl',
+            rating_table.Rate, 
+            rating_table.Date,
+            modules.Type            AS 'ModuleType'
+    FROM `submodules`
+    INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID AND 
+                                    DisciplineID = modules.DisciplineID
+    LEFT JOIN `rating_table`    ON  submodules.ID = rating_table.SubmoduleID AND 
+                                    StudentID = rating_table.StudentID 
+    ORDER BY    modules.OrderNum ASC, 
+                submodules.OrderNum ASC;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetMapForDiscipline//
-CREATE PROCEDURE `GetMapForDiscipline`	(	IN `TeacherID` 		INT,
-											IN `DisciplineID` 	INT 
-										)
+CREATE PROCEDURE `GetMapForDiscipline`  (   IN `TeacherID`      INT,
+                                            IN `DisciplineID`   INT 
+                                        )
     NO SQL
 BEGIN  
-	IF NOT InternalIsTeacherBinded(TeacherID, DisciplineID) 
-	THEN
-		SELECT 	NULL AS 'ID'
-		LIMIT 0;  
-	ELSE
-		SELECT 	subjects.ID 			AS 'SubjectID',
-				subjects.Name 			AS 'SubjectName',	
-				modules.ID 				AS 'ModuleID', 
-				modules.Name 			AS 'ModuleName', 
-				modules.Type  			AS 'ModuleType',
-				submodules.ID 			AS 'SubmoduleID', 
-				submodules.Name 		AS 'SubModuleName',  
-				submodules.Description 	AS 'SubmoduleDescription', 
-				submodules.MaxRate,
-				submodules.Type 		AS 'SubmoduleControl'
-		FROM `modules`
-		LEFT JOIN  `submodules` ON modules.ID = submodules.ModuleID
-		INNER JOIN `disciplines` ON modules.DisciplineID = disciplines.ID
-		INNER JOIN `subjects` 	ON disciplines.SubjectID = subjects.ID
-		WHERE 	modules.DisciplineID = DisciplineID AND
-				(modules.Type != 2 OR submodules.OrderNum = 1)
-		ORDER BY 	modules.Type ^ 1 ASC,
-					-- 1, 3, 2, 4 ASC
-					modules.OrderNum ASC, 
-					submodules.OrderNum ASC;
- 	END IF;
+    IF NOT InternalIsTeacherBindeded(TeacherID, DisciplineID) 
+    THEN
+        SELECT  NULL AS 'ID'
+        LIMIT 0;  
+    ELSE
+        SELECT  subjects.ID             AS 'SubjectID',
+                subjects.Name           AS 'SubjectName',   
+                modules.ID              AS 'ModuleID', 
+                modules.Name            AS 'ModuleName', 
+                modules.Type            AS 'ModuleType',
+                submodules.ID           AS 'SubmoduleID', 
+                submodules.Name         AS 'SubModuleName',  
+                submodules.Description  AS 'SubmoduleDescription', 
+                submodules.MaxRate,
+                submodules.Type         AS 'SubmoduleControl'
+        FROM `modules`
+        LEFT JOIN  `submodules` ON modules.ID = submodules.ModuleID
+        INNER JOIN `disciplines` ON modules.DisciplineID = disciplines.ID
+        INNER JOIN `subjects`   ON disciplines.SubjectID = subjects.ID
+        WHERE   modules.DisciplineID = DisciplineID AND
+                (modules.Type != 2 OR submodules.OrderNum = 1)
+        ORDER BY    modules.Type ^ 1 ASC,
+                    # 1, 3, 2, 4 ASC
+                    modules.OrderNum ASC, 
+                    submodules.OrderNum ASC;
+    END IF;
 END //
 
 
 DROP PROCEDURE IF EXISTS GetMapForDisciplineExam//
-CREATE PROCEDURE `GetMapForDisciplineExam`		(	IN `TeacherID` 		INT,
-													IN `DisciplineID` 	INT 
-												)
+CREATE PROCEDURE `GetMapForDisciplineExam`      (   IN `TeacherID`      INT,
+                                                    IN `DisciplineID`   INT 
+                                                )
     NO SQL
 BEGIN  
-	IF NOT InternalIsTeacherBinded(TeacherID, DisciplineID) 
-	THEN
-		SELECT 	NULL AS 'ID'
-		LIMIT 0;  
-	ELSE
-		SELECT 	subjects.ID 			AS 'SubjectID',
-				subjects.Name 			AS 'SubjectName',	
-				modules.ID 				AS 'ModuleID', 
-				modules.Name 			AS 'ModuleName', 
-				modules.Type  			AS 'ModuleType',
-				submodules.ID 			AS 'SubmoduleID', 
-				submodules.Name 		AS 'SubModuleName', 
-				submodules.Description 	AS 'SubmoduleDescription', 
-				submodules.MaxRate,
-				submodules.Type 		AS 'SubmoduleControl'
-		FROM `modules`
-		LEFT JOIN  `submodules` ON modules.ID = submodules.ModuleID
-		INNER JOIN `disciplines` ON modules.DisciplineID = disciplines.ID
-		INNER JOIN `subjects` 	ON disciplines.SubjectID = subjects.ID
-		WHERE 	modules.DisciplineID = DisciplineID AND
-				(modules.Type = 4 OR modules.Type = 2)
-		ORDER BY modules.OrderNum ASC, 
-				 submodules.OrderNum ASC;
- 	END IF;
+    IF NOT InternalIsTeacherBindeded(TeacherID, DisciplineID) 
+    THEN
+        SELECT  NULL AS 'ID'
+        LIMIT 0;  
+    ELSE
+        SELECT  subjects.ID             AS 'SubjectID',
+                subjects.Name           AS 'SubjectName',   
+                modules.ID              AS 'ModuleID', 
+                modules.Name            AS 'ModuleName', 
+                modules.Type            AS 'ModuleType',
+                submodules.ID           AS 'SubmoduleID', 
+                submodules.Name         AS 'SubModuleName', 
+                submodules.Description  AS 'SubmoduleDescription', 
+                submodules.MaxRate,
+                submodules.Type         AS 'SubmoduleControl'
+        FROM `modules`
+        LEFT JOIN  `submodules` ON modules.ID = submodules.ModuleID
+        INNER JOIN `disciplines` ON modules.DisciplineID = disciplines.ID
+        INNER JOIN `subjects`   ON disciplines.SubjectID = subjects.ID
+        WHERE   modules.DisciplineID = DisciplineID AND
+                (modules.Type = 4 OR modules.Type = 2)
+        ORDER BY modules.OrderNum ASC, 
+                 submodules.OrderNum ASC;
+    END IF;
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetDisciplineInfoByID//
-CREATE PROCEDURE `GetDisciplineInfoByID`(	IN `DiscID` INT )
+CREATE PROCEDURE `GetDisciplineInfoByID`(   IN `DiscID` INT )
     NO SQL
 BEGIN  
-	SELECT 	disciplines.AuthorID,
-			disciplines.GradeID 			AS 'GradeID',
-			grades.Num 						AS 'GradeNum',
-			grades.Degree 					AS 'Degree',
-			disciplines.ExamType,
-			disciplines.LectionCount,
-			disciplines.PracticeCount,
-			disciplines.LabCount,
-			disciplines.SemesterID,
-			subjects.ID 					AS 'SubjectID',
-			subjects.Name 					AS 'SubjectName',
-			subjects.Abbr 					AS 'SubjectAbbr',
-			departments.ID 					AS 'DepID',
-			departments.Name 				AS 'DepName',
-			faculties.ID					AS 'FacultyID',
-			faculties.Name 					AS 'FacultyName',
-			disciplines.isLocked 			AS 'isLocked',
-			(modules.ID IS NOT NULL) 		AS 'isBonus',
-			disciplines.isMilestone
-	FROM `disciplines`
-	INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
-	INNER JOIN `faculties` ON faculties.ID = disciplines.FacultyID
-	INNER JOIN `teachers` ON disciplines.AuthorID = teachers.ID
-	INNER JOIN `departments` ON departments.ID = teachers.DepartmentID
-	INNER JOIN `grades` ON grades.ID = disciplines.GradeID 
-	LEFT JOIN  `modules` ON modules.DisciplineID = disciplines.ID AND
-							modules.Type = 3
-	WHERE 	disciplines.ID = DiscID;
+    SELECT  disciplines.AuthorID,
+            disciplines.GradeID             AS 'GradeID',
+            grades.Num                      AS 'GradeNum',
+            grades.Degree                   AS 'Degree',
+            disciplines.ExamType,
+            disciplines.LectionCount,
+            disciplines.PracticeCount,
+            disciplines.LabCount,
+            disciplines.SemesterID,
+            subjects.ID                     AS 'SubjectID',
+            subjects.Name                   AS 'SubjectName',
+            subjects.Abbr                   AS 'SubjectAbbr',
+            departments.ID                  AS 'DepID',
+            departments.Name                AS 'DepName',
+            faculties.ID                    AS 'FacultyID',
+            faculties.Name                  AS 'FacultyName',
+            disciplines.isLocked            AS 'isLocked',
+            (modules.ID IS NOT NULL)        AS 'isBonus',
+            disciplines.isMilestone
+    FROM `disciplines`
+    INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
+    INNER JOIN `faculties` ON faculties.ID = disciplines.FacultyID
+    INNER JOIN `teachers` ON disciplines.AuthorID = teachers.ID
+    INNER JOIN `departments` ON departments.ID = teachers.DepartmentID
+    INNER JOIN `grades` ON grades.ID = disciplines.GradeID 
+    LEFT JOIN  `modules` ON modules.DisciplineID = disciplines.ID AND
+                            modules.Type = 3
+    WHERE   disciplines.ID = DiscID;
 END //
 
 
@@ -2153,15 +2106,15 @@ END //
 
 
 DROP FUNCTION IF EXISTS AddDiscipline//
-CREATE FUNCTION `AddDiscipline`	(	`TeacherID` 	INT,
-									`GradeID`		INT,
-									`SubjectID`		INT,
-									`ExamType`		VARCHAR(30) charset utf8,
-									`LectionCount`  INT,
-									`PracticeCount` INT,
-									`LabCount`		INT,
-									`FacultyID`	INT		
-							 	)	RETURNS int(11)
+CREATE FUNCTION `AddDiscipline` (   `TeacherID`     INT,
+                                    `GradeID`       INT,
+                                    `SubjectID`     INT,
+                                    `ExamType`      VARCHAR(30) charset utf8,
+                                    `LectionCount`  INT,
+                                    `PracticeCount` INT,
+                                    `LabCount`      INT,
+                                    `FacultyID` INT     
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker, DisciplineID INT; 
@@ -2170,1857 +2123,1834 @@ BEGIN
     SELECT grades.ID
     INTO checker
     FROM `grades`
-    INNER JOIN `faculties` 	ON 	FacultyID = faculties.ID
-    INNER JOIN `subjects` 	ON 	SubjectID = subjects.ID
-    INNER JOIN `teachers` 	ON 	TeacherID = teachers.ID
+    INNER JOIN `faculties`  ON  FacultyID = faculties.ID
+    INNER JOIN `subjects`   ON  SubjectID = subjects.ID
+    INNER JOIN `teachers`   ON  TeacherID = teachers.ID
     WHERE GradeID = grades.ID
     LIMIT 1;
-	IF 	checker <= 0 OR
-		( ExamType != 'exam' AND ExamType != 'credit')
-	THEN
-		RETURN -1;
-	END IF;
-
-	INSERT INTO `disciplines` 	
-			(	disciplines.AuthorID, 
-				disciplines.GradeID, 
-				disciplines.SubjectID, 
-				disciplines.ExamType, 
-				disciplines.LectionCount, 
-				disciplines.PracticeCount,
-				disciplines.LabCount,
-				disciplines.SemesterID,
-				disciplines.FacultyID 
-			)
-	VALUES 	( 	TeacherID, GradeID, SubjectID, ExamType, LectionCount, PracticeCount, LabCount, @CurrentSemesterID, FacultyID );
-	
-	SET DisciplineID = LAST_INSERT_ID();
-	INSERT INTO `disciplines_teachers`
-			(	disciplines_teachers.DisciplineID,
-				disciplines_teachers.TeacherID 	
-			)
-	VALUES	( 	DisciplineID, TeacherID );
-
-	IF ExamType = 'exam' THEN
-		SET checker = AddModuleExam(TeacherID, DisciplineID);
-	END IF;
-	SET checker = AddModuleExtra(TeacherID, DisciplineID);
-	RETURN DisciplineID;
+    IF  checker <= 0 OR
+        ( ExamType != 'exam' AND ExamType != 'credit')
+    THEN
+        RETURN -1;
+    END IF;
+
+    INSERT INTO `disciplines`   
+            (   disciplines.AuthorID, 
+                disciplines.GradeID, 
+                disciplines.SubjectID, 
+                disciplines.ExamType, 
+                disciplines.LectionCount, 
+                disciplines.PracticeCount,
+                disciplines.LabCount,
+                disciplines.SemesterID,
+                disciplines.FacultyID 
+            )
+    VALUES  (   TeacherID, GradeID, SubjectID, ExamType, LectionCount, PracticeCount, LabCount, @CurrentSemesterID, FacultyID );
+    
+    SET DisciplineID = LAST_INSERT_ID();
+    INSERT INTO `disciplines_teachers`
+            (   disciplines_teachers.DisciplineID,
+                disciplines_teachers.TeacherID  
+            )
+    VALUES  (   DisciplineID, TeacherID );
+
+    IF ExamType = 'exam' THEN
+        SET checker = AddModuleExam(TeacherID, DisciplineID);
+    END IF;
+    SET checker = AddModuleExtra(TeacherID, DisciplineID);
+    RETURN DisciplineID;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeDisciplineSubject//
-CREATE FUNCTION `ChangeDisciplineSubject`	(	`TeacherID` 	INT,
-												`DisciplineID`	INT,
-												`SubjectID`		INT		
-							 				) 	RETURNS int(11)
+CREATE FUNCTION `ChangeDisciplineSubject`   (   `TeacherID`     INT,
+                                                `DisciplineID`  INT,
+                                                `SubjectID`     INT     
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker INT;  					 
-	
-	IF 	NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) OR
-		InternalIsMapLocked(DisciplineID)
-	THEN 
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT subjects.ID 
-	INTO checker
-	FROM `subjects`
-	WHERE subjects.ID = SubjectID
-	LIMIT 1;
-	IF checker <= 0 THEN 
-		RETURN -1;
-	END IF;
-
-	UPDATE `disciplines`
-	SET disciplines.SubjectID = SubjectID
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker INT;                     
+    
+    IF  NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) OR
+        InternalIsMapLocked(DisciplineID)
+    THEN 
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT subjects.ID 
+    INTO checker
+    FROM `subjects`
+    WHERE subjects.ID = SubjectID
+    LIMIT 1;
+    IF checker <= 0 THEN 
+        RETURN -1;
+    END IF;
+
+    UPDATE `disciplines`
+    SET disciplines.SubjectID = SubjectID
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeDisciplineGrade//
-CREATE FUNCTION `ChangeDisciplineGrade`	(	`TeacherID` 	INT,
-											`DisciplineID`	INT,
-											`GradeID`			INT	
-							 			) 	RETURNS int(11)
+CREATE FUNCTION `ChangeDisciplineGrade` (   `TeacherID`     INT,
+                                            `DisciplineID`  INT,
+                                            `GradeID`           INT 
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker INT;  					 
-	
-	SET checker = -1;
+    DECLARE checker INT;                     
+    
+    SET checker = -1;
     SELECT grades.ID
     INTO checker
     FROM `grades`
     WHERE grades.ID = GradeID
     LIMIT 1;
-	IF 	checker <= 0 OR	
-		InternalIsMapLocked(DisciplineID) OR 
-		NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) 
-	THEN
-		RETURN -1;
-	END IF;
-
-	SELECT disciplines.GradeID 
-	INTO checker
-	FROM `disciplines`
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-
-	IF checker != GradeID THEN
-		DELETE FROM `disciplines_groups`
-		WHERE disciplines_groups.DisciplineID = DisciplineID;
-
-		DELETE FROM `disciplines_students`
-		WHERE disciplines_students.DisciplineID = DisciplineID;
-	END IF;
-
-
-	UPDATE `disciplines`
-	SET disciplines.GradeID = GradeID
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-	RETURN 0;
+    IF  checker <= 0 OR 
+        InternalIsMapLocked(DisciplineID) OR 
+        NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) 
+    THEN
+        RETURN -1;
+    END IF;
+
+    SELECT disciplines.GradeID 
+    INTO checker
+    FROM `disciplines`
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+
+    IF checker != GradeID THEN
+        DELETE FROM `disciplines_groups`
+        WHERE disciplines_groups.DisciplineID = DisciplineID;
+
+        DELETE FROM `disciplines_students`
+        WHERE disciplines_students.DisciplineID = DisciplineID;
+    END IF;
+
+
+    UPDATE `disciplines`
+    SET disciplines.GradeID = GradeID
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 DROP FUNCTION IF EXISTS ChangeDisciplineControl//
-CREATE FUNCTION `ChangeDisciplineControl`	(	`TeacherID` 	INT,
-												`DisciplineID`	INT,
-												`ExamType`		VARCHAR(30)	charset utf8
-							 				) 	RETURNS int(11)
+CREATE FUNCTION `ChangeDisciplineControl`   (   `TeacherID`     INT,
+                                                `DisciplineID`  INT,
+                                                `ExamType`      VARCHAR(30) charset utf8
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker, extraMax, vExtraID INT;  					 
-	
-	IF 	InternalIsMapLocked(DisciplineID) OR
-		NOT InternalIsTeacherAuthor(TeacherID,DisciplineID)
-	THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT disciplines.ExamType 
-	INTO checker
-	FROM `disciplines`
-	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 = 7;
-
-			SET checker = GetDisciplineMaxRate(DisciplineID);
-			IF checker >= 61 THEN
-				RETURN 1;
-			END IF;
-			SET checker = AddModuleExam(TeacherID, DisciplineID);
-			
-			-- delete extra module from 
-			DELETE FROM `submodules`
-			WHERE 	submodules.OrderNum > 1 AND
-					submodules.ModuleID = vExtraID;
-			
-		ELSE
-			SET extraMax = 29;
-			
-			SET checker = DeleteModuleExam(TeacherID, DisciplineID);
-			SET checker = AddSubmodule(TeacherID, vExtraID, extraMax, '', NULL, 'LandmarkControl');	
-			
-		END IF;
-
-		UPDATE `disciplines`
-		SET 	disciplines.ExamType = ExamType
-		WHERE 	DisciplineID = disciplines.ID;
-
-		UPDATE `submodules`
-		SET submodules.MaxRate = extraMax
-		WHERE submodules.ModuleID = vExtraID;
-	END IF;
-	RETURN 0;
+    DECLARE checker, extraMax, vExtraID INT;                     
+    
+    IF  InternalIsMapLocked(DisciplineID) OR
+        NOT InternalIsTeacherAuthor(TeacherID,DisciplineID)
+    THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT disciplines.ExamType 
+    INTO checker
+    FROM `disciplines`
+    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 = 7;
+
+            SET checker = GetDisciplineMaxRate(DisciplineID);
+            IF checker >= 61 THEN
+                RETURN 1;
+            END IF;
+            SET checker = AddModuleExam(TeacherID, DisciplineID);
+            
+            # delete extra module from 
+            DELETE FROM `submodules`
+            WHERE   submodules.OrderNum > 1 AND
+                    submodules.ModuleID = vExtraID;
+            
+        ELSE
+            SET extraMax = 29;
+            
+            SET checker = DeleteModuleExam(TeacherID, DisciplineID);
+            SET checker = AddSubmodule(TeacherID, vExtraID, extraMax, '', NULL, 'LandmarkControl'); 
+            
+        END IF;
+
+        UPDATE `disciplines`
+        SET     disciplines.ExamType = ExamType
+        WHERE   DisciplineID = disciplines.ID;
+
+        UPDATE `submodules`
+        SET submodules.MaxRate = extraMax
+        WHERE submodules.ModuleID = vExtraID;
+    END IF;
+    RETURN 0;
 END //
 
 DROP FUNCTION IF EXISTS ChangeDisciplineHours//
-CREATE FUNCTION `ChangeDisciplineHours`	(	`TeacherID` 	INT,
-											`DisciplineID`	INT,
-											`Hours`  		INT,
-											`Type` 			INT
-										-- Type: 0 - Practice Hours, 1 - Lection Hours, 2 - Lab Hours
-							 		) 	RETURNS int(11)
+CREATE FUNCTION `ChangeDisciplineHours` (   `TeacherID`     INT,
+                                            `DisciplineID`  INT,
+                                            `Hours`         INT,
+                                            `Type`          INT
+                                        # Type: 0 - Practice Hours, 1 - Lection Hours, 2 - Lab Hours
+                                    )   RETURNS int(11)
     NO SQL
-BEGIN 				 
-	IF NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
-	THEN 
-		RETURN -1;
-	END IF;
-
-	-- TODO: switch
-	IF Type = 0 THEN
-		UPDATE `disciplines`
-		SET 	disciplines.PracticeCount = Hours
-		WHERE disciplines.ID = DisciplineID
-		LIMIT 1;
-	END IF;
-	IF Type = 1 THEN
-		UPDATE `disciplines`
-		SET 	disciplines.LectionCount = Hours
-		WHERE disciplines.ID = DisciplineID
-		LIMIT 1;
-	END IF;
-	IF Type = 2 THEN
-		UPDATE `disciplines`
-		SET 	disciplines.LabCount = Hours
-		WHERE disciplines.ID = DisciplineID
-		LIMIT 1;
-	END IF;
-
-	RETURN 0;
+BEGIN                
+    IF NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
+    THEN 
+        RETURN -1;
+    END IF;
+
+    # TODO: switch
+    IF Type = 0 THEN
+        UPDATE `disciplines`
+        SET     disciplines.PracticeCount = Hours
+        WHERE disciplines.ID = DisciplineID
+        LIMIT 1;
+    END IF;
+    IF Type = 1 THEN
+        UPDATE `disciplines`
+        SET     disciplines.LectionCount = Hours
+        WHERE disciplines.ID = DisciplineID
+        LIMIT 1;
+    END IF;
+    IF Type = 2 THEN
+        UPDATE `disciplines`
+        SET     disciplines.LabCount = Hours
+        WHERE disciplines.ID = DisciplineID
+        LIMIT 1;
+    END IF;
+
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS BindGroup//
-CREATE FUNCTION `BindGroup`	(	`TeacherID` 	INT,
-								`DisciplineID` 	INt,
-								`StudyGroupID` 	INT
-					 		) 	RETURNS int(11)
+CREATE FUNCTION `BindGroup` (   `TeacherID`     INT,
+                                `DisciplineID`  INt,
+                                `StudyGroupID`  INT
+                            )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker INT;  					 
-	
--- 1. check if AccessedTeacher is author
-	IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-		InternalIsMapLocked(disciplineID)
-	THEN 
-		RETURN -1;
-	END IF;
-
--- 2. check if study group is bound to discipline
-	SET checker = -1;
-	SELECT disciplines_groups.ID
-	INTO checker
-	FROM `disciplines_groups`
-	WHERE 	disciplines_groups.StudyGroupID = StudyGroupID AND
-			disciplines_groups.DisciplineID = DisciplineID
-	LIMIT 1;
-	IF checker > 0 THEN
-		RETURN 1;
-	END IF;
-
--- 3. delete students of this group which were bound to discipline before
-	DELETE FROM `disciplines_students`
-	WHERE 	DisciplineID = disciplines_students.DisciplineID AND
-			disciplines_students.StudentID IN 
-		(	SELECT students.ID
-			FROM `students`
-			WHERE students.StudyGroupID = StudyGroupID
-		);
-		
--- 4. bind whole group
-	INSERT INTO `disciplines_groups` 	
-			(	disciplines_groups.DisciplineID,
-				disciplines_groups.StudyGroupID
-			)
-	VALUES 	( 	DisciplineID, StudyGroupID );
-	RETURN 0;
+    DECLARE checker INT;                     
+    
+# 1. check if AccessedTeacher is author
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(disciplineID)
+    THEN 
+        RETURN -1;
+    END IF;
+
+# 2. check if study group is bound to discipline
+    SET checker = -1;
+    SELECT disciplines_groups.ID
+    INTO checker
+    FROM `disciplines_groups`
+    WHERE   disciplines_groups.StudyGroupID = StudyGroupID AND
+            disciplines_groups.DisciplineID = DisciplineID
+    LIMIT 1;
+    IF checker > 0 THEN
+        RETURN 1;
+    END IF;
+
+# 3. delete students of this group which were bound to discipline before
+    DELETE FROM `disciplines_students`
+    WHERE   DisciplineID = disciplines_students.DisciplineID AND
+            disciplines_students.StudentID IN 
+        (   SELECT students.ID
+            FROM `students`
+            WHERE students.StudyGroupID = StudyGroupID
+        );
+        
+# 4. bind whole group
+    INSERT INTO `disciplines_groups`    
+            (   disciplines_groups.DisciplineID,
+                disciplines_groups.StudyGroupID
+            )
+    VALUES  (   DisciplineID, StudyGroupID );
+    RETURN 0;
 
 END //
 
 
 
 DROP FUNCTION IF EXISTS BindStudent//
-CREATE FUNCTION `BindStudent`	(	`TeacherID` 	INT,
-									`DisciplineID` 	INT,
-									`StudentID` 	INT
-					 			) 	RETURNS int(11)
+CREATE FUNCTION `BindStudent`   (   `TeacherID`     INT,
+                                    `DisciplineID`  INT,
+                                    `StudentID`     INT
+                                )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker, SG, temp INT;  					 
-
--- 1. check if AccessedTeacher is author
-	IF NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
-	THEN 
-		RETURN -1;
-	END IF;
-	
--- 2. check if student's group is bound yet	
-	SET SG = -1;
-	SELECT students.StudyGroupID
-	INTO SG
-	FROM `students`
-	WHERE students.ID = StudentID
-	LIMIT 1;
-
-	SET checker = -1;
-	SELECT disciplines_groups.ID
-	INTO checker
-	FROM `disciplines_groups`
-	WHERE 	DisciplineID = disciplines_groups.DisciplineID AND
-			SG = disciplines_groups.StudyGroupID 
-	LIMIT 1;
-	IF checker > 0 THEN
-		DELETE FROM `disciplines_students`
-		WHERE 	DisciplineID = disciplines_students.DisciplineID AND
-				StudentID = disciplines_students.StudentID
-		LIMIT 1;
-		RETURN 0;
-	END IF;
-
--- 3. check if student is bound
-	SET checker = -1;
-	SELECT disciplines_students.ID
-	INTO checker
-	FROM `disciplines_students`
-	WHERE 	DisciplineID = disciplines_students.DisciplineID AND
-			StudentID = disciplines_students.StudentID
-	LIMIT 1;
-	IF checker > 0 THEN	
-		RETURN 1;
-	END IF;
-	
--- 4. bind student
-	INSERT INTO `disciplines_students`	
-			(	disciplines_students.DisciplineID,
-				disciplines_students.StudentID,
-				disciplines_students.Type
-			)
-	VALUES	( 	DisciplineID, StudentID, 'attach');
-	RETURN 0;
-	
+    DECLARE checker, SG, temp INT;                       
+
+# 1. check if AccessedTeacher is author
+    IF NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
+    THEN 
+        RETURN -1;
+    END IF;
+    
+# 2. check if student's group is bound yet 
+    SET SG = -1;
+    SELECT students.StudyGroupID
+    INTO SG
+    FROM `students`
+    WHERE students.ID = StudentID
+    LIMIT 1;
+
+    SET checker = -1;
+    SELECT disciplines_groups.ID
+    INTO checker
+    FROM `disciplines_groups`
+    WHERE   DisciplineID = disciplines_groups.DisciplineID AND
+            SG = disciplines_groups.StudyGroupID 
+    LIMIT 1;
+    IF checker > 0 THEN
+        DELETE FROM `disciplines_students`
+        WHERE   DisciplineID = disciplines_students.DisciplineID AND
+                StudentID = disciplines_students.StudentID
+        LIMIT 1;
+        RETURN 0;
+    END IF;
+
+# 3. check if student is bound
+    SET checker = -1;
+    SELECT disciplines_students.ID
+    INTO checker
+    FROM `disciplines_students`
+    WHERE   DisciplineID = disciplines_students.DisciplineID AND
+            StudentID = disciplines_students.StudentID
+    LIMIT 1;
+    IF checker > 0 THEN 
+        RETURN 1;
+    END IF;
+    
+# 4. bind student
+    INSERT INTO `disciplines_students`  
+            (   disciplines_students.DisciplineID,
+                disciplines_students.StudentID,
+                disciplines_students.Type
+            )
+    VALUES  (   DisciplineID, StudentID, 'attach');
+    RETURN 0;
+    
 END //
 
 
 
 DROP FUNCTION IF EXISTS UnbindGroup//
-CREATE FUNCTION `UnbindGroup`	(	`TeacherID` 	INT,
-									`DisciplineID` 	INT,
-									`StudyGroupID` 	INT
-					 			) 	RETURNS int(11)
+CREATE FUNCTION `UnbindGroup`   (   `TeacherID`     INT,
+                                    `DisciplineID`  INT,
+                                    `StudyGroupID`  INT
+                                )   RETURNS int(11)
     NO SQL
-BEGIN 				 
-	IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-		InternalIsMapLocked(disciplineID) 
-	THEN 
-		RETURN -1;
-	END IF;
-
-	DELETE FROM `disciplines_groups`
-	WHERE 	DisciplineID = disciplines_groups.DisciplineID AND
-			StudyGroupID = disciplines_groups.StudyGroupID
-	LIMIT 1;
-
-	DELETE FROM `disciplines_students`
-	WHERE 	DisciplineID = disciplines_students.DisciplineID AND
-			disciplines_students.StudentID IN
-		(	SELECT students.ID
-			FROM `students`
-			WHERE students.StudyGroupID = StudyGroupID
-		);
-	RETURN 0;
+BEGIN                
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(disciplineID) 
+    THEN 
+        RETURN -1;
+    END IF;
+
+    DELETE FROM `disciplines_groups`
+    WHERE   DisciplineID = disciplines_groups.DisciplineID AND
+            StudyGroupID = disciplines_groups.StudyGroupID
+    LIMIT 1;
+
+    DELETE FROM `disciplines_students`
+    WHERE   DisciplineID = disciplines_students.DisciplineID AND
+            disciplines_students.StudentID IN
+        (   SELECT students.ID
+            FROM `students`
+            WHERE students.StudyGroupID = StudyGroupID
+        );
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS UnbindStudent//
-CREATE FUNCTION `UnbindStudent`	(	`TeacherID` 	INT,
-									`DisciplineID` 	INt,
-									`StudentID` 	INT
-					 			) 	RETURNS int(11)
+CREATE FUNCTION `UnbindStudent` (   `TeacherID`     INT,
+                                    `DisciplineID`  INt,
+                                    `StudentID`     INT
+                                )   RETURNS int(11)
     NO SQL
 BEGIN 
-    DECLARE checker, SG INT;  					 
-	
-	SET SG = -1;
+    DECLARE checker, SG INT;                     
+    
+    SET SG = -1;
     SELECT students.StudyGroupID
     INTO SG
     FROM `students`
     WHERE students.ID = StudentID
     LIMIT 1;
-	IF 	SG <= 0 OR 
-		NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
-	THEN 
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT disciplines_groups.ID
-	INTO checker
-	FROM `disciplines_groups`
-	WHERE 	DisciplineID = disciplines_groups.DisciplineID AND
-			SG = disciplines_groups.StudyGroupID
-	LIMIT 1;
-
-
-	IF checker > 0 THEN
-		INSERT INTO `disciplines_students`	
-				(	disciplines_students.DisciplineID,
-					disciplines_students.StudentID,
-					disciplines_students.Type
-				)
-		VALUES	( 	DisciplineID, StudentID, 'detach');
-
-	ELSE 
-		DELETE FROM `disciplines_students`
-		WHERE 	DisciplineID = disciplines_students.DisciplineID AND
-				StudentID = disciplines_students.StudentID
-		LIMIT 1;
-	END IF;
-	RETURN 0;
+    IF  SG <= 0 OR 
+        NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) 
+    THEN 
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT disciplines_groups.ID
+    INTO checker
+    FROM `disciplines_groups`
+    WHERE   DisciplineID = disciplines_groups.DisciplineID AND
+            SG = disciplines_groups.StudyGroupID
+    LIMIT 1;
+
+
+    IF checker > 0 THEN
+        INSERT INTO `disciplines_students`  
+                (   disciplines_students.DisciplineID,
+                    disciplines_students.StudentID,
+                    disciplines_students.Type
+                )
+        VALUES  (   DisciplineID, StudentID, 'detach');
+
+    ELSE 
+        DELETE FROM `disciplines_students`
+        WHERE   DisciplineID = disciplines_students.DisciplineID AND
+                StudentID = disciplines_students.StudentID
+        LIMIT 1;
+    END IF;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS BindTeacher//
-CREATE FUNCTION `BindTeacher`(	`TeacherID` INT,
-								`BindingTeacherID` INT,
-								`DisciplineID` INT		
-							 ) 	RETURNS int(11)
+CREATE FUNCTION `BindTeacher`(  `TeacherID` INT,
+                                `BindingTeacherID` INT,
+                                `DisciplineID` INT      
+                             )  RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker boolean; 
 
--- 1. check if AccessedTeacher is author
-	SET checker = FALSE;
-	SELECT (TeacherID = disciplines.AuthorID)
-	INTO checker
-	FROM `disciplines`
-	WHERE 	DisciplineID = disciplines.ID
-	LIMIT 1;
-	IF NOT checker THEN
-		RETURN -1;
-	END IF;
-
--- 2. check if BindingTeacher has rights to access this discipline
-	IF InternalIsTeacherBinded(BindingTeacherID,DisciplineID)
-	THEN
-		RETURN 1;
-	END IF;
-
--- 3. insert BindingTeacher in access list
-	INSERT INTO `disciplines_teachers`
-		(	disciplines_teachers.DisciplineID,
-			disciplines_teachers.TeacherID 	
-		)
-	VALUES	( DisciplineID, BindingTeacherID );
-	RETURN 0;
+# 1. check if AccessedTeacher is author
+    SET checker = FALSE;
+    SELECT (TeacherID = disciplines.AuthorID)
+    INTO checker
+    FROM `disciplines`
+    WHERE   DisciplineID = disciplines.ID
+    LIMIT 1;
+    IF NOT checker THEN
+        RETURN -1;
+    END IF;
+
+# 2. check if BindingTeacher has rights to access this discipline
+    IF InternalIsTeacherBindeded(BindingTeacherID,DisciplineID)
+    THEN
+        RETURN 1;
+    END IF;
+
+# 3. insert BindingTeacher in access list
+    INSERT INTO `disciplines_teachers`
+        (   disciplines_teachers.DisciplineID,
+            disciplines_teachers.TeacherID  
+        )
+    VALUES  ( DisciplineID, BindingTeacherID );
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS UnbindTeacher//
-CREATE FUNCTION `UnbindTeacher`	(	`AuthorID` INT,
-									`BindingTeacher` INT,
-									`DisciplineID` INT		
-							 	) 	RETURNS int(11)
+CREATE FUNCTION `UnbindTeacher` (   `AuthorID` INT,
+                                    `BindingTeacher` INT,
+                                    `DisciplineID` INT      
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker boolean; 
 
-	IF AuthorID = BindingTeacher THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = FALSE;
-	SELECT (AuthorID = disciplines.AuthorID)
-	INTO checker
-	FROM `disciplines`
-	WHERE 	DisciplineID = disciplines.ID
-	LIMIT 1;
-	IF NOT checker THEN
-		RETURN -1;
-	END IF;
-
-	DELETE FROM `disciplines_teachers`
-	WHERE 	DisciplineID = disciplines_teachers.DisciplineID AND
-			BindingTeacher = disciplines_teachers.TeacherID;
-	RETURN 0;
-	
+    IF AuthorID = BindingTeacher THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = FALSE;
+    SELECT (AuthorID = disciplines.AuthorID)
+    INTO checker
+    FROM `disciplines`
+    WHERE   DisciplineID = disciplines.ID
+    LIMIT 1;
+    IF NOT checker THEN
+        RETURN -1;
+    END IF;
+
+    DELETE FROM `disciplines_teachers`
+    WHERE   DisciplineID = disciplines_teachers.DisciplineID AND
+            BindingTeacher = disciplines_teachers.TeacherID;
+    RETURN 0;
+    
 END //
 
 
 
 DROP FUNCTION IF EXISTS DelegateDiscipline//
-CREATE FUNCTION `DelegateDiscipline`	(	`AuthorID` INT,
-											`NewAuthorID` INT,
-											`DisciplineID` INT		
-							 			) 	RETURNS int(11)
+CREATE FUNCTION `DelegateDiscipline`    (   `AuthorID` INT,
+                                            `NewAuthorID` INT,
+                                            `DisciplineID` INT      
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker boolean; 
 
-	IF 	AuthorID = NewAuthorID OR
-		NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
-	THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = FALSE;
-	SELECT (AuthorID = disciplines.AuthorID)
-	INTO checker
-	FROM `disciplines`
-	INNER JOIN `disciplines_teachers` 	ON 	DisciplineID = disciplines_teachers.DisciplineID AND
-											NewAuthorID = disciplines_teachers.TeacherID
-	WHERE 	DisciplineID = disciplines.ID AND
-			AuthorID = disciplines.AuthorID
-	LIMIT 1;
-	IF NOT checker THEN
-		RETURN -1;
-	END IF;
-
-	UPDATE `disciplines`
-	SET disciplines.AuthorID = NewAuthorID
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-	RETURN 0;
+    IF  AuthorID = NewAuthorID OR
+        NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
+    THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = FALSE;
+    SELECT (AuthorID = disciplines.AuthorID)
+    INTO checker
+    FROM `disciplines`
+    INNER JOIN `disciplines_teachers`   ON  DisciplineID = disciplines_teachers.DisciplineID AND
+                                            NewAuthorID = disciplines_teachers.TeacherID
+    WHERE   DisciplineID = disciplines.ID AND
+            AuthorID = disciplines.AuthorID
+    LIMIT 1;
+    IF NOT checker THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `disciplines`
+    SET disciplines.AuthorID = NewAuthorID
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ClearDiscipline//
-CREATE FUNCTION `ClearDiscipline`	(	`AuthorID` INT,
-										`DisciplineID` INT		
-						 			) 	RETURNS int(11)
+CREATE FUNCTION `ClearDiscipline`   (   `AuthorID` INT,
+                                        `DisciplineID` INT      
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker boolean; 
     DECLARE vtemp INT;
 
-	IF 	NOT InternalIsTeacherAuthor(AuthorID, DisciplineID) THEN
-		RETURN -1;
-	END IF;
-
-	DELETE FROM `logs_rating`
-	WHERE logs_rating.SubModuleID IN 
-		(
-			SELECT submodules.ID
-			FROM `submodules`
-			INNER JOIN `modules` ON submodules.ModuleID = modules.ID
-			WHERE modules.DisciplineID = DisciplineID
-		);
-
-	DELETE FROM `rating_table`
-	WHERE rating_table.SubModuleID IN 
-		(
-			SELECT submodules.ID
-			FROM `submodules`
-			INNER JOIN `modules` ON submodules.ModuleID = modules.ID
-			WHERE modules.DisciplineID = DisciplineID
-		);
-
-	UPDATE `disciplines`
-	SET disciplines.isLocked = 0
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-
-	RETURN 0;	
+    IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    DELETE FROM `logs_rating`
+    WHERE logs_rating.SubModuleID IN 
+        (
+            SELECT submodules.ID
+            FROM `submodules`
+            INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+            WHERE modules.DisciplineID = DisciplineID
+        );
+
+    DELETE FROM `rating_table`
+    WHERE rating_table.SubModuleID IN 
+        (
+            SELECT submodules.ID
+            FROM `submodules`
+            INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+            WHERE modules.DisciplineID = DisciplineID
+        );
+
+    UPDATE `disciplines`
+    SET disciplines.isLocked = 0
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+
+    RETURN 0;   
 END //
 
 
 
 DROP FUNCTION IF EXISTS DeleteDiscipline//
-CREATE FUNCTION `DeleteDiscipline`	(	`AuthorID` INT,
-										`DisciplineID` INT		
-						 			) 	RETURNS int(11)
+CREATE FUNCTION `DeleteDiscipline`  (   `AuthorID` INT,
+                                        `DisciplineID` INT      
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-	DECLARE vtemp INT;
-	IF 	NOT InternalIsTeacherAuthor(AuthorID, DisciplineID) THEN
-		RETURN -1;
-	END IF;
-
-	SELECT disciplines.isLocked
-	INTO vtemp
-	FROM `disciplines`
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-
-	IF vtemp != 0 THEN
-		RETURN -1;
-	END IF;
-
-	SET vtemp = CountRatings(AuthorID, DisciplineID);
-	IF vtemp > 0 THEN
-		RETURN -1;
-	END IF;
-
-
-	DELETE FROM `logs_rating`
-	WHERE logs_rating.SubModuleID IN 
-		(
-			SELECT submodules.ID
-			FROM `submodules`
-			INNER JOIN `modules` ON submodules.ModuleID = modules.ID
-			WHERE modules.DisciplineID = DisciplineID
-		);
-
-	DELETE FROM `rating_table`
-	WHERE rating_table.SubModuleID IN 
-		(
-			SELECT submodules.ID
-			FROM `submodules`
-			INNER JOIN `modules` ON submodules.ModuleID = modules.ID
-			WHERE modules.DisciplineID = DisciplineID
-		);
-	
-	DELETE FROM `submodules`
-	WHERE submodules.ModuleID IN 
-		(
-			SELECT modules.ID
-			FROM `modules`
-			WHERE modules.DisciplineID = DisciplineID
-		);
-
-	DELETE FROM `modules`
-	WHERE modules.DisciplineID = DisciplineID;
-
-	DELETE FROM `disciplines_teachers`
-	WHERE disciplines_teachers.DisciplineID = DisciplineID;
-
-	DELETE FROM `disciplines_students`
-	WHERE disciplines_students.DisciplineID = DisciplineID;
-
-	DELETE FROM `disciplines_groups`
-	WHERE disciplines_groups.DisciplineID = DisciplineID;
-
-	DELETE FROM `disciplines`
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
-
-	RETURN 0;	
+    DECLARE vtemp INT;
+    IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    SELECT disciplines.isLocked
+    INTO vtemp
+    FROM `disciplines`
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+
+    IF vtemp != 0 THEN
+        RETURN -1;
+    END IF;
+
+    SET vtemp = CountRatings(AuthorID, DisciplineID);
+    IF vtemp > 0 THEN
+        RETURN -1;
+    END IF;
+
+
+    DELETE FROM `logs_rating`
+    WHERE logs_rating.SubModuleID IN 
+        (
+            SELECT submodules.ID
+            FROM `submodules`
+            INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+            WHERE modules.DisciplineID = DisciplineID
+        );
+
+    DELETE FROM `rating_table`
+    WHERE rating_table.SubModuleID IN 
+        (
+            SELECT submodules.ID
+            FROM `submodules`
+            INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+            WHERE modules.DisciplineID = DisciplineID
+        );
+    
+    DELETE FROM `submodules`
+    WHERE submodules.ModuleID IN 
+        (
+            SELECT modules.ID
+            FROM `modules`
+            WHERE modules.DisciplineID = DisciplineID
+        );
+
+    DELETE FROM `modules`
+    WHERE modules.DisciplineID = DisciplineID;
+
+    DELETE FROM `disciplines_teachers`
+    WHERE disciplines_teachers.DisciplineID = DisciplineID;
+
+    DELETE FROM `disciplines_students`
+    WHERE disciplines_students.DisciplineID = DisciplineID;
+
+    DELETE FROM `disciplines_groups`
+    WHERE disciplines_groups.DisciplineID = DisciplineID;
+
+    DELETE FROM `disciplines`
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
+
+    RETURN 0;   
 END //
 
 
 
 DROP FUNCTION IF EXISTS CountRatings//
-CREATE FUNCTION `CountRatings`	(	`TeacherID` INT,
-									`DisciplineID` INT		
-					 			) 	RETURNS int(11)
+CREATE FUNCTION `CountRatings`  (   `TeacherID` INT,
+                                    `DisciplineID` INT      
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE res boolean; 
 
-	IF 	NOT InternalIsTeacherBinded(TeacherID, DisciplineID) THEN
-		RETURN -1;
-	END IF;
+    IF  NOT InternalIsTeacherBindeded(TeacherID, DisciplineID) THEN
+        RETURN -1;
+    END IF;
 
-	SET res = 0;
-	SELECT COUNT(rating_table.StudentID)
-	INTO res
-	FROM `rating_table`
-	INNER JOIN `submodules` ON rating_table.SubModuleID = submodules.ID
-	INNER JOIN `modules` ON submodules.ModuleID = modules.ID
-	WHERE modules.DisciplineID = DisciplineID
-	LIMIT 1;
+    SET res = 0;
+    SELECT COUNT(rating_table.StudentID)
+    INTO res
+    FROM `rating_table`
+    INNER JOIN `submodules` ON rating_table.SubModuleID = submodules.ID
+    INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+    WHERE modules.DisciplineID = DisciplineID
+    LIMIT 1;
 
-	RETURN res;	
+    RETURN res; 
 END //
 
 
 
 DROP FUNCTION IF EXISTS RestrictAfterMilestone//
-CREATE FUNCTION `RestrictAfterMilestone`	(	`TeacherID` 	INT,
-												`DisciplineID` 	INT
-			 								) 	RETURNS int(11)
+CREATE FUNCTION `RestrictAfterMilestone`    (   `TeacherID`     INT,
+                                                `DisciplineID`  INT
+                                            )   RETURNS int(11)
     NO SQL
-BEGIN 				 
+BEGIN                
 
-	UPDATE `disciplines`
-	SET disciplines.MilestoneDate = CURDATE(),
-		disciplines.isMilestone = 1
-	WHERE disciplines.ID = DisciplineID
-	LIMIT 1;
+    UPDATE `disciplines`
+    SET disciplines.MilestoneDate = CURDATE(),
+        disciplines.isMilestone = 1
+    WHERE disciplines.ID = DisciplineID
+    LIMIT 1;
 
-	RETURN 0;
+    RETURN 0;
 END //
 
 
 DROP FUNCTION IF EXISTS RestrictAfterMilestoneForCredits//
-CREATE FUNCTION `RestrictAfterMilestoneForCredits`	(	`TeacherID` 	INT,
-														`FacultyID` 	INT
-													) 	RETURNS int(11)
+CREATE FUNCTION `RestrictAfterMilestoneForCredits`  (   `TeacherID`     INT,
+                                                        `FacultyID`     INT
+                                                    )   RETURNS int(11)
     NO SQL
-BEGIN 	
-	DECLARE semID INT;
-
-	SET semID = @CurrentSemesterID;
-	
-	UPDATE `disciplines`
-	SET disciplines.MilestoneDate = CURDATE(),
-		disciplines.isMilestone = 1
-	WHERE 	disciplines.SemesterID = semID AND
-			disciplines.ExamType = 'credit' AND
-			disciplines.FacultyID= FacultyID;
-
-	RETURN 0;
+BEGIN   
+    DECLARE semID INT;
+
+    SET semID = @CurrentSemesterID;
+    
+    UPDATE `disciplines`
+    SET disciplines.MilestoneDate = CURDATE(),
+        disciplines.isMilestone = 1
+    WHERE   disciplines.SemesterID = semID AND
+            disciplines.ExamType = 'credit' AND
+            disciplines.FacultyID= FacultyID;
+
+    RETURN 0;
 END //
 
--- -------------------------------------------------------------------------------------------
--- Label: modules
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: modules
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP FUNCTION IF EXISTS ChangeModuleName//
-CREATE FUNCTION `ChangeModuleName`	(	`TeacherID` INT, 
-										`ModuleID` INT, 
-										`Name` VARCHAR(200) charset utf8
-									) 	RETURNS int(11)
+CREATE FUNCTION `ChangeModuleName`  (   `TeacherID` INT, 
+                                        `ModuleID` INT, 
+                                        `Name` VARCHAR(200) charset utf8
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE DisciplineID INT;  					 
-	
-	SET DisciplineID = -1;
-	SELECT disciplines.ID 
-	INTO DisciplineID 
-	FROM `modules`
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID AND
-									TeacherID = disciplines.AuthorID 
-	WHERE 	ModuleID = modules.ID AND
-			modules.Type = 1
-	LIMIT 1;
-    IF 	DisciplineID <= 0 OR
-    	InternalIsMapLocked(DisciplineID) THEN
-    	RETURN -1;
-	END IF;
-
-	UPDATE `modules` 
-	SET modules.Name = Name
-	WHERE modules.ID = ModuleID 
-	LIMIT 1;
-	RETURN 0;
+    DECLARE DisciplineID INT;                    
+    
+    SET DisciplineID = -1;
+    SELECT disciplines.ID 
+    INTO DisciplineID 
+    FROM `modules`
+    INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID AND
+                                    TeacherID = disciplines.AuthorID 
+    WHERE   ModuleID = modules.ID AND
+            modules.Type = 1
+    LIMIT 1;
+    IF  DisciplineID <= 0 OR
+        InternalIsMapLocked(DisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `modules` 
+    SET modules.Name = Name
+    WHERE modules.ID = ModuleID 
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS AddModule//
-CREATE FUNCTION `AddModule`	(	`TeacherID` 	INT, 
-								`DisciplineID` 	INT, 
-								`Name` 			VARCHAR(200) charset utf8
-							) 	RETURNS int(11)
+CREATE FUNCTION `AddModule` (   `TeacherID`     INT, 
+                                `DisciplineID`  INT, 
+                                `Name`          VARCHAR(200) charset utf8
+                            )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker INT;  					 
-	
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-    	InternalIsMapLocked(DisciplineID) 
+    DECLARE checker INT;                     
+    
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(DisciplineID) 
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET checker = 0;
-	SELECT MAX(modules.OrderNum)
-	INTO checker
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND
-			modules.Type = 1
-	LIMIT 1;
-	IF checker is NULL THEN
-		SET checker = 0;
-	END IF;
-	SET checker = checker + 1;
-
-
-	INSERT INTO `modules` 	
-			( 	modules.Name, modules.OrderNum, modules.DisciplineID )
-	VALUES 	(	Name , checker , DisciplineID );
-	
-
-	RETURN 	(	SELECT 	modules.ID
-				FROM 	`modules`
-				WHERE 	DisciplineID = modules.DisciplineID AND 
-						checker = modules.OrderNum
-				LIMIT 1 
-			);
+        RETURN -1;
+    END IF;
+
+    SET checker = 0;
+    SELECT MAX(modules.OrderNum)
+    INTO checker
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND
+            modules.Type = 1
+    LIMIT 1;
+    IF checker is NULL THEN
+        SET checker = 0;
+    END IF;
+    SET checker = checker + 1;
+
+
+    INSERT INTO `modules`   
+            (   modules.Name, modules.OrderNum, modules.DisciplineID )
+    VALUES  (   Name , checker , DisciplineID );
+    
+
+    RETURN  (   SELECT  modules.ID
+                FROM    `modules`
+                WHERE   DisciplineID = modules.DisciplineID AND 
+                        checker = modules.OrderNum
+                LIMIT 1 
+            );
 END //
 
 
 
 DROP FUNCTION IF EXISTS AddModuleExam//
-CREATE FUNCTION `AddModuleExam`	(	`TeacherID` 	INT, 
-									`DisciplineID` 	INT
-								) 	RETURNS int(11)
+CREATE FUNCTION `AddModuleExam` (   `TeacherID`     INT, 
+                                    `DisciplineID`  INT
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, vModule INT;  					 	
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-    	InternalIsMapLocked(DisciplineID) 
+    DECLARE checker, vModule INT;                       
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(DisciplineID) 
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT modules.ID 
-	INTO checker
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 2;
-	IF checker > 0 THEN
-		RETURN -2;
-	END IF;
-
-
-	INSERT INTO `modules` 	
-			( 	modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
-	VALUES 	( 	'Экзамен' , 3141692 , DisciplineID, 2 );
-
-	SET vModule = -1;
-	SELECT 	modules.ID
-	INTO vModule
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 2
-	LIMIT 1;
-	IF vModule <= 0 THEN
-		RETURN -1; 
-	END IF;
-
-	SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
-	SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
-	SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
-	RETURN vModule;
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT modules.ID 
+    INTO checker
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 2;
+    IF checker > 0 THEN
+        RETURN -2;
+    END IF;
+
+
+    INSERT INTO `modules`   
+            (   modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
+    VALUES  (   'Экзамен' , 3141692 , DisciplineID, 2 );
+
+    SET vModule = -1;
+    SELECT  modules.ID
+    INTO vModule
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 2
+    LIMIT 1;
+    IF vModule <= 0 THEN
+        RETURN -1; 
+    END IF;
+
+    SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
+    SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
+    SET checker = AddSubmodule(TeacherID, vModule, 40, '', NULL, 'LandmarkControl');
+    RETURN vModule;
 END //
 
 
 DROP FUNCTION IF EXISTS AddModuleExtra//
-CREATE FUNCTION `AddModuleExtra`	(	`TeacherID` 	INT, 
-										`DisciplineID` 	INT
-									) 	RETURNS int(11)
+CREATE FUNCTION `AddModuleExtra`    (   `TeacherID`     INT, 
+                                        `DisciplineID`  INT
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, vModule, vType, vGap INT;  					 	
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID)
+    DECLARE checker, vModule, vType, vGap INT;                          
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID)
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET vType = -1;
-	SET checker = -1;
-	SELECT modules.ID 
-	INTO checker
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 4
-	LIMIT 1;
-	IF checker > 0 THEN
-		RETURN -2;
-	END IF;
-
-
-	INSERT INTO `modules` 	
-			( 	modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
-	VALUES 	( 	'Добор баллов' , 2900666 , DisciplineID, 4 );
-
-	SET vModule = -1;
-	SELECT 	modules.ID, disciplines.ExamType 
-	INTO vModule, vType
-	FROM `modules`
-	INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 4
-	LIMIT 1;
-	IF vModule <= 0 THEN
-		RETURN -1; 
-	END IF;
-
-	SET vGap = -1;
-	IF vType = 1 THEN
-	-- exam
-		SET vGap = 7;
-	END IF;
-	IF vType = 2 THEN
-	-- credit
-		SET vGap = 29;
-		SET checker = AddSubmodule(TeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
-	END IF; 
-
-	SET checker = AddSubmodule(TeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
-	RETURN vModule;
+        RETURN -1;
+    END IF;
+
+    SET vType = -1;
+    SET checker = -1;
+    SELECT modules.ID 
+    INTO checker
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 4
+    LIMIT 1;
+    IF checker > 0 THEN
+        RETURN -2;
+    END IF;
+
+
+    INSERT INTO `modules`   
+            (   modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
+    VALUES  (   'Добор баллов' , 2900666 , DisciplineID, 4 );
+
+    SET vModule = -1;
+    SELECT  modules.ID, disciplines.ExamType 
+    INTO vModule, vType
+    FROM `modules`
+    INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 4
+    LIMIT 1;
+    IF vModule <= 0 THEN
+        RETURN -1; 
+    END IF;
+
+    SET vGap = -1;
+    IF vType = 1 THEN
+    # exam
+        SET vGap = 7;
+    END IF;
+    IF vType = 2 THEN
+    # credit
+        SET vGap = 29;
+        SET checker = AddSubmodule(TeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
+    END IF; 
+
+    SET checker = AddSubmodule(TeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
+    RETURN vModule;
 END //
 
 
 DROP FUNCTION IF EXISTS DeleteModule//
-CREATE FUNCTION `DeleteModule`	(	`TeacherID` INT,
-									`ModuleID` 	INT 
-								)  	RETURNS int(11)
+CREATE FUNCTION `DeleteModule`  (   `TeacherID` INT,
+                                    `ModuleID`  INT 
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker INT;
 
     SET checker = -1;
-	SELECT disciplines.ID 
-	INTO checker 
-	FROM `modules`
-	INNER JOIN `disciplines` 	ON 	modules.DisciplineID = disciplines.ID AND
-									TeacherID = disciplines.AuthorID
-	WHERE 	ModuleID = modules.ID
-	LIMIT 1;
-    IF 	checker <= 0 THEN
-    	RETURN -1;
-	END IF;
-
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,checker) OR
-    	InternalIsMapLocked(checker) 
+    SELECT disciplines.ID 
+    INTO checker 
+    FROM `modules`
+    INNER JOIN `disciplines`    ON  modules.DisciplineID = disciplines.ID AND
+                                    TeacherID = disciplines.AuthorID
+    WHERE   ModuleID = modules.ID
+    LIMIT 1;
+    IF  checker <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    IF  NOT InternalIsTeacherAuthor(TeacherID,checker) OR
+        InternalIsMapLocked(checker) 
     THEN
-    	RETURN -1;
-	END IF;
+        RETURN -1;
+    END IF;
 
-	DELETE FROM `submodules`
-	WHERE submodules.ModuleID = ModuleID;
+    DELETE FROM `submodules`
+    WHERE submodules.ModuleID = ModuleID;
 
-	DELETE FROM `modules` 
-	WHERE ModuleID = modules.ID;
+    DELETE FROM `modules` 
+    WHERE ModuleID = modules.ID;
 
-	SET @counter = 0;
-	UPDATE `modules`
-	SET modules.OrderNum = (@counter := @counter + 1)
-	WHERE 	modules.DisciplineID = checker AND
-			modules.Type = 1 -- regular
-	ORDER BY modules.OrderNum ASC;
+    SET @counter = 0;
+    UPDATE `modules`
+    SET modules.OrderNum = (@counter := @counter + 1)
+    WHERE   modules.DisciplineID = checker AND
+            modules.Type = 1 # regular
+    ORDER BY modules.OrderNum ASC;
 
-	RETURN 0;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS DeleteModuleExam//
-CREATE FUNCTION `DeleteModuleExam`		(	`TeacherID` INT,
-											`DisciplineID` 	INT 
-										)  	RETURNS int(11)
+CREATE FUNCTION `DeleteModuleExam`      (   `TeacherID` INT,
+                                            `DisciplineID`  INT 
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE ExamModuleID INT;
-    IF 	NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) OR
-    	InternalIsMapLocked(DisciplineID) 
+    IF  NOT InternalIsTeacherAuthor(TeacherID, DisciplineID) OR
+        InternalIsMapLocked(DisciplineID) 
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET ExamModuleID = -1;
-	SELECT modules.ID
-	INTO ExamModuleID
-	FROM `modules`
-	WHERE 	modules.Type = 2 AND
-			DisciplineID = modules.DisciplineID
-	LIMIT 1;
-	IF ExamModuleID <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	DELETE FROM `submodules`
-	WHERE ExamModuleID = submodules.ModuleID;
-
-	DELETE FROM `modules` 
-	WHERE ExamModuleID = modules.ID
-	LIMIT 1; 
-
-	RETURN 0;
+        RETURN -1;
+    END IF;
+
+    SET ExamModuleID = -1;
+    SELECT modules.ID
+    INTO ExamModuleID
+    FROM `modules`
+    WHERE   modules.Type = 2 AND
+            DisciplineID = modules.DisciplineID
+    LIMIT 1;
+    IF ExamModuleID <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    DELETE FROM `submodules`
+    WHERE ExamModuleID = submodules.ModuleID;
+
+    DELETE FROM `modules` 
+    WHERE ExamModuleID = modules.ID
+    LIMIT 1; 
+
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS SwapModuleOrder//
-CREATE FUNCTION `SwapModuleOrder`	(	`TeacherID` INT, 
-										`ModuleID1` INT, 
-										`ModuleID2` INT 
-									) 	RETURNS int(11)
+CREATE FUNCTION `SwapModuleOrder`   (   `TeacherID` INT, 
+                                        `ModuleID1` INT, 
+                                        `ModuleID2` INT 
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-   	DECLARE checker, ord1, ord2, disc1, disc2 INT; 
-
-	
-	SET disc1 = -1;
-	SET disc2 = -1;
-	SELECT 	modules.OrderNum,
-			modules.DisciplineID
-	INTO ord1, disc1
-	FROM `modules`
-	INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			modules.ID = ModuleID1 AND 
-			modules.Type = 1
-	LIMIT 1;
-
-	SELECT 	modules.OrderNum,
-			modules.DisciplineID
-	INTO ord2, disc2
-	FROM `modules`
-	INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			modules.ID = ModuleID2 AND
-			modules.Type = 1
-	LIMIT 1;
-	IF 	disc1 != disc2 OR disc1 <= 0 OR
-		InternalIsMapLocked(disc1) THEN
-		RETURN -1;
-	END IF;
-
-	UPDATE `modules` 
-	SET modules.OrderNum 	= 271828
-	WHERE modules.ID = ModuleID1;
-
-	UPDATE `modules` 
-	SET modules.OrderNum 	= ord1
-	WHERE modules.ID = ModuleID2
-	LIMIT 1;
-
-	UPDATE `modules` 
-	SET modules.OrderNum 	= ord2
-	WHERE modules.ID = ModuleID1
-	LIMIT 1;
-	
-	RETURN 0;
+    DECLARE checker, ord1, ord2, disc1, disc2 INT; 
+
+    
+    SET disc1 = -1;
+    SET disc2 = -1;
+    SELECT  modules.OrderNum,
+            modules.DisciplineID
+    INTO ord1, disc1
+    FROM `modules`
+    INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
+    WHERE   TeacherID = disciplines.AuthorID AND
+            modules.ID = ModuleID1 AND 
+            modules.Type = 1
+    LIMIT 1;
+
+    SELECT  modules.OrderNum,
+            modules.DisciplineID
+    INTO ord2, disc2
+    FROM `modules`
+    INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID 
+    WHERE   TeacherID = disciplines.AuthorID AND
+            modules.ID = ModuleID2 AND
+            modules.Type = 1
+    LIMIT 1;
+    IF  disc1 != disc2 OR disc1 <= 0 OR
+        InternalIsMapLocked(disc1) THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `modules` 
+    SET modules.OrderNum    = 271828
+    WHERE modules.ID = ModuleID1;
+
+    UPDATE `modules` 
+    SET modules.OrderNum    = ord1
+    WHERE modules.ID = ModuleID2
+    LIMIT 1;
+
+    UPDATE `modules` 
+    SET modules.OrderNum    = ord2
+    WHERE modules.ID = ModuleID1
+    LIMIT 1;
+    
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS AddModuleBonus//
-CREATE FUNCTION `AddModuleBonus`	(	`TeacherID` 	INT, 
-										`DisciplineID` 	INT
-									
-									) 	RETURNS int(11)
+CREATE FUNCTION `AddModuleBonus`    (   `TeacherID`     INT, 
+                                        `DisciplineID`  INT
+                                    
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, vModule INT;  					 	
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-    	InternalIsMapLocked(DisciplineID) 
+    DECLARE checker, vModule INT;                       
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(DisciplineID) 
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT modules.ID 
-	INTO checker
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 3;
-	IF checker > 0 THEN
-		RETURN -2;
-	END IF;
-
-
-	INSERT INTO `modules` 	
-			( 	modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
-	VALUES 	( 	'Бонусные баллы' , 2141692 , DisciplineID, 3 );
-
-	SET vModule = -1;
-	SELECT 	modules.ID
-	INTO vModule
-	FROM `modules`
-	WHERE 	DisciplineID = modules.DisciplineID AND 
-			modules.Type = 3
-	LIMIT 1;
-	IF vModule <= 0 THEN
-		RETURN -1; 
-	END IF;
-
-	SET checker = AddSubmodule(TeacherID, vModule, 10, '', NULL, 'LandmarkControl');
-	RETURN 0;
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT modules.ID 
+    INTO checker
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 3;
+    IF checker > 0 THEN
+        RETURN -2;
+    END IF;
+
+
+    INSERT INTO `modules`   
+            (   modules.Name, modules.OrderNum, modules.DisciplineID, modules.Type )
+    VALUES  (   'Бонусные баллы' , 2141692 , DisciplineID, 3 );
+
+    SET vModule = -1;
+    SELECT  modules.ID
+    INTO vModule
+    FROM `modules`
+    WHERE   DisciplineID = modules.DisciplineID AND 
+            modules.Type = 3
+    LIMIT 1;
+    IF vModule <= 0 THEN
+        RETURN -1; 
+    END IF;
+
+    SET checker = AddSubmodule(TeacherID, vModule, 10, '', NULL, 'LandmarkControl');
+    RETURN 0;
 END //
 
 
 DROP FUNCTION IF EXISTS DeleteModuleBonus//
-CREATE FUNCTION `DeleteModuleBonus`		(	`TeacherID` INT,
-											`DisciplineID` 	INT 
-										)  	RETURNS int(11)
+CREATE FUNCTION `DeleteModuleBonus`     (   `TeacherID` INT,
+                                            `DisciplineID`  INT 
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE BonusModuleID INT;
-    IF 	NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
-    	InternalIsMapLocked(DisciplineID) 
+    IF  NOT InternalIsTeacherAuthor(TeacherID,DisciplineID) OR
+        InternalIsMapLocked(DisciplineID) 
     THEN
-    	RETURN -1;
-	END IF;
-
-	SET BonusModuleID = -1;
-	SELECT modules.ID
-	INTO BonusModuleID
-	FROM `modules`
-	WHERE 	modules.Type = 3 AND
-			DisciplineID = modules.DisciplineID
-	LIMIT 1;
-	IF BonusModuleID <= 0 THEN
-		RETURN -1;
-	END IF;
-
-	DELETE FROM `submodules`
-	WHERE BonusModuleID = submodules.ModuleID;
-
-	DELETE FROM `modules` 
-	WHERE BonusModuleID = modules.ID
-	LIMIT 1; 
-
-	RETURN 0;
+        RETURN -1;
+    END IF;
+
+    SET BonusModuleID = -1;
+    SELECT modules.ID
+    INTO BonusModuleID
+    FROM `modules`
+    WHERE   modules.Type = 3 AND
+            DisciplineID = modules.DisciplineID
+    LIMIT 1;
+    IF BonusModuleID <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    DELETE FROM `submodules`
+    WHERE BonusModuleID = submodules.ModuleID;
+
+    DELETE FROM `modules` 
+    WHERE BonusModuleID = modules.ID
+    LIMIT 1; 
+
+    RETURN 0;
 END //
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: submodules
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: submodules
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP FUNCTION IF EXISTS ChangeSubmoduleMaxAndControl//
-CREATE FUNCTION `ChangeSubmoduleMaxAndControl`	(	`TeacherID` 	INT, 
-													`SubmoduleID` 	INT, 
-													`MaxRate` 		INT,
-													`ControlType`	VARCHAR(30) charset utf8
-											) 	RETURNS int(11)
+CREATE FUNCTION `ChangeSubmoduleMaxAndControl`  (   `TeacherID`     INT, 
+                                                    `SubmoduleID`   INT, 
+                                                    `MaxRate`       INT,
+                                                    `ControlType`   VARCHAR(30) charset utf8
+                                            )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, disciplineID INT; 			 
-	
-	SET checker = -1;
-	SET disciplineID = -1;
-	SELECT  submodules.ID,
-			disciplines.ID
-	INTO checker, disciplineID
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			SubmoduleID = submodules.ID
-	LIMIT 1;
-	IF 	checker <= 0 OR
-		disciplineID <= 0 OR 
-		InternalIsMapLocked(disciplineID) THEN
-		RETURN -1;
-	END IF;
-
-
-	UPDATE `submodules` 
-	SET 	submodules.MaxRate = MaxRate,
-			submodules.Type = ControlType
-	WHERE submodules.ID = SubmoduleID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker, disciplineID INT;           
+    
+    SET checker = -1;
+    SET disciplineID = -1;
+    SELECT  submodules.ID,
+            disciplines.ID
+    INTO checker, disciplineID
+    FROM `submodules`
+    INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID
+    INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID
+    WHERE   TeacherID = disciplines.AuthorID AND
+            SubmoduleID = submodules.ID
+    LIMIT 1;
+    IF  checker <= 0 OR
+        disciplineID <= 0 OR 
+        InternalIsMapLocked(disciplineID) THEN
+        RETURN -1;
+    END IF;
+
+
+    UPDATE `submodules` 
+    SET     submodules.MaxRate = MaxRate,
+            submodules.Type = ControlType
+    WHERE submodules.ID = SubmoduleID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeSubmoduleName//
-CREATE FUNCTION `ChangeSubmoduleName`	(	`TeacherID` 	INT, 
-											`SubmoduleID` 	INT, 
-											`Name` 			VARCHAR(200) charset utf8
-										)	RETURNS int(11)
+CREATE FUNCTION `ChangeSubmoduleName`   (   `TeacherID`     INT, 
+                                            `SubmoduleID`   INT, 
+                                            `Name`          VARCHAR(200) charset utf8
+                                        )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, disciplineID INT; 			 
-	
-	SET disciplineID = -1;
-	SET checker = -1;
-	SELECT  submodules.ID,
-			disciplines.ID
-	INTO checker, disciplineID
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			SubmoduleID = submodules.ID
-	LIMIT 1;
-	IF 	checker <= 0 OR 
-		disciplineID <= 0 OR
-		InternalIsMapLocked(disciplineID) THEN
-		RETURN -1;
-	END IF;
-
-	UPDATE `submodules` 
-	SET submodules.Name = Name
-	WHERE submodules.ID = SubmoduleID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker, disciplineID INT;           
+    
+    SET disciplineID = -1;
+    SET checker = -1;
+    SELECT  submodules.ID,
+            disciplines.ID
+    INTO checker, disciplineID
+    FROM `submodules`
+    INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID
+    INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID
+    WHERE   TeacherID = disciplines.AuthorID AND
+            SubmoduleID = submodules.ID
+    LIMIT 1;
+    IF  checker <= 0 OR 
+        disciplineID <= 0 OR
+        InternalIsMapLocked(disciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `submodules` 
+    SET submodules.Name = Name
+    WHERE submodules.ID = SubmoduleID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS ChangeSubmoduleDescription//
-CREATE FUNCTION `ChangeSubmoduleDescription`	(	`TeacherID` 	INT, 
-													`SubmoduleID` 	INT, 
-													`Description` 	VARCHAR(200) charset utf8
-												) 	RETURNS int(11)
+CREATE FUNCTION `ChangeSubmoduleDescription`    (   `TeacherID`     INT, 
+                                                    `SubmoduleID`   INT, 
+                                                    `Description`   VARCHAR(200) charset utf8
+                                                )   RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, disciplineID INT; 			 
-	
-	SET checker = -1;
-	SET disciplineID = -1;
-	SELECT  submodules.ID,
-			disciplines.ID
-	INTO checker, disciplineID
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			SubmoduleID = submodules.ID
-	LIMIT 1;
-	IF 	checker <= 0 OR 
-		disciplineID <= 0 OR
-		InternalIsMapLocked(disciplineID) THEN
-		RETURN -1;
-	END IF;
-
-	UPDATE `submodules` 
-	SET submodules.Description 	= Description
-	WHERE submodules.ID = SubmoduleID
-	LIMIT 1;
-	RETURN 0;
+    DECLARE checker, disciplineID INT;           
+    
+    SET checker = -1;
+    SET disciplineID = -1;
+    SELECT  submodules.ID,
+            disciplines.ID
+    INTO checker, disciplineID
+    FROM `submodules`
+    INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID
+    INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID
+    WHERE   TeacherID = disciplines.AuthorID AND
+            SubmoduleID = submodules.ID
+    LIMIT 1;
+    IF  checker <= 0 OR 
+        disciplineID <= 0 OR
+        InternalIsMapLocked(disciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `submodules` 
+    SET submodules.Description  = Description
+    WHERE submodules.ID = SubmoduleID
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
 
 
 DROP FUNCTION IF EXISTS DeleteSubmodule//
-CREATE FUNCTION `DeleteSubmodule`	(	`TeacherID` 	INT,
-										`SubmoduleID` 	INT
-									)  	RETURNS int(11)
+CREATE FUNCTION `DeleteSubmodule`   (   `TeacherID`     INT,
+                                        `SubmoduleID`   INT
+                                    )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker, ModID, disciplineID INT;
 
     SET checker = -1;
     SET disciplineID = -1;
-	SELECT submodules.ID, modules.ID, disciplines.ID
-	INTO checker, ModID, disciplineID
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	modules.ID = submodules.ModuleID
-	INNER JOIN `disciplines` 	ON 	modules.DisciplineID = disciplines.ID									
-	WHERE 	TeacherID = disciplines.AuthorID AND 
-			SubmoduleID = submodules.ID 
-	LIMIT 1;
-	IF 	checker <= 0 OR 
-		disciplineID <= 0 OR
-		InternalIsMapLocked(disciplineID) THEN
-		RETURN -1;
-	END IF;
-
-	SET checker = -1;
-	SELECT rating_table.StudentID
-	INTO checker
-	FROM `rating_table`
-	WHERE rating_table.SubmoduleID = SubmoduleID
-	LIMIT 1;
-	IF checker > 0 THEN
-		RETURN -2;
-	END IF;
-
-	DELETE FROM `submodules` 
-	WHERE submodules.ID = SubmoduleID
-	LIMIT 1; 
-
-
-	SET @counter = 0;
-	UPDATE `submodules`
-	SET submodules.OrderNum = (@counter := @counter + 1)
-	WHERE 	submodules.ModuleID = ModID 
-	ORDER BY submodules.OrderNum ASC;
-
-	RETURN 0;
+    SELECT submodules.ID, modules.ID, disciplines.ID
+    INTO checker, ModID, disciplineID
+    FROM `submodules`
+    INNER JOIN `modules`        ON  modules.ID = submodules.ModuleID
+    INNER JOIN `disciplines`    ON  modules.DisciplineID = disciplines.ID                                   
+    WHERE   TeacherID = disciplines.AuthorID AND 
+            SubmoduleID = submodules.ID 
+    LIMIT 1;
+    IF  checker <= 0 OR 
+        disciplineID <= 0 OR
+        InternalIsMapLocked(disciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    SET checker = -1;
+    SELECT rating_table.StudentID
+    INTO checker
+    FROM `rating_table`
+    WHERE rating_table.SubmoduleID = SubmoduleID
+    LIMIT 1;
+    IF checker > 0 THEN
+        RETURN -2;
+    END IF;
+
+    DELETE FROM `submodules` 
+    WHERE submodules.ID = SubmoduleID
+    LIMIT 1; 
+
+
+    SET @counter = 0;
+    UPDATE `submodules`
+    SET submodules.OrderNum = (@counter := @counter + 1)
+    WHERE   submodules.ModuleID = ModID 
+    ORDER BY submodules.OrderNum ASC;
+
+    RETURN 0;
 END //
 
 
 
 DROP FUNCTION IF EXISTS AddSubmodule//
-CREATE FUNCTION `AddSubmodule`	(	`TeacherID` 	INT, 
-									`ModuleID` 		INT, 
-									`MaxRate` 		INT, 
-									`Name` 			VARCHAR(200) charset utf8, 
-									`Description`	VARCHAR(200) charset utf8,
-									`ControlType`	VARCHAR(30) charset utf8
-								) 	RETURNS int(11)
+CREATE FUNCTION `AddSubmodule`  (   `TeacherID`     INT, 
+                                    `ModuleID`      INT, 
+                                    `MaxRate`       INT, 
+                                    `Name`          VARCHAR(200) charset utf8, 
+                                    `Description`   VARCHAR(200) charset utf8,
+                                    `ControlType`   VARCHAR(30) charset utf8
+                                )   RETURNS int(11)
     NO SQL
 BEGIN  
     DECLARE checker INT;  
-	
-	SET checker = -1;
-	SELECT disciplines.ID 
-	INTO checker 
-	FROM `modules`
-	INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-			ModuleID = modules.ID 
-	LIMIT 1;
-    IF 	checker <= 0 OR
-    	InternalIsMapLocked(checker)
+    
+    SET checker = -1;
+    SELECT disciplines.ID 
+    INTO checker 
+    FROM `modules`
+    INNER JOIN `disciplines` ON disciplines.ID = modules.DisciplineID
+    WHERE   TeacherID = disciplines.AuthorID AND
+            ModuleID = modules.ID 
+    LIMIT 1;
+    IF  checker <= 0 OR
+        InternalIsMapLocked(checker)
     THEN
-    	RETURN -1;
-	END IF;
-
-
-	SET checker = 0;
-	SELECT MAX(submodules.OrderNum)
-	INTO checker
-	FROM `submodules`
-	WHERE 	ModuleID = submodules.ModuleID
-	LIMIT 1;
-	IF checker IS NULL THEN
-		SET checker = 0;
-	END IF;
-	SET checker = checker + 1;
-
-	IF Description = "" THEN
-		INSERT INTO `submodules` 	
-				( 	submodules.ModuleID, submodules.MaxRate, submodules.OrderNum, submodules.Name, submodules.Description, submodules.Type )
-		VALUES	( 	ModuleID, MaxRate, checker, Name, NULL, ControlType);
-	ELSE 
-		INSERT INTO `submodules` 	
-				( 	submodules.ModuleID, submodules.MaxRate, submodules.OrderNum, submodules.Name, submodules.Description, submodules.Type )
-		VALUES	( 	ModuleID, MaxRate, checker, Name, Description, ControlType);
-	END IF;
-	
-	RETURN 	(	SELECT submodules.ID
-				FROM `submodules`
-				WHERE 	submodules.ModuleID = ModuleID AND
-						submodules.OrderNum = checker
-				LIMIT 1
-			);
+        RETURN -1;
+    END IF;
+
+
+    SET checker = 0;
+    SELECT MAX(submodules.OrderNum)
+    INTO checker
+    FROM `submodules`
+    WHERE   ModuleID = submodules.ModuleID
+    LIMIT 1;
+    IF checker IS NULL THEN
+        SET checker = 0;
+    END IF;
+    SET checker = checker + 1;
+
+    IF Description = "" THEN
+        INSERT INTO `submodules`    
+                (   submodules.ModuleID, submodules.MaxRate, submodules.OrderNum, submodules.Name, submodules.Description, submodules.Type )
+        VALUES  (   ModuleID, MaxRate, checker, Name, NULL, ControlType);
+    ELSE 
+        INSERT INTO `submodules`    
+                (   submodules.ModuleID, submodules.MaxRate, submodules.OrderNum, submodules.Name, submodules.Description, submodules.Type )
+        VALUES  (   ModuleID, MaxRate, checker, Name, Description, ControlType);
+    END IF;
+    
+    RETURN  (   SELECT submodules.ID
+                FROM `submodules`
+                WHERE   submodules.ModuleID = ModuleID AND
+                        submodules.OrderNum = checker
+                LIMIT 1
+            );
 END //
 
 
 
 DROP FUNCTION IF EXISTS SwapSubmoduleOrder//
-CREATE FUNCTION `SwapSubmoduleOrder`(	`TeacherID` 	INT, 
-										`SubmoduleID1` 	INT, 
-										`SubmoduleID2` 	INT
-									)	RETURNS int(11)
+CREATE FUNCTION `SwapSubmoduleOrder`
+(   `pTeacherID` INT, `pSubmoduleID1` INT, `pSubmoduleID2` INT  ) RETURNS int(11)
     NO SQL
 BEGIN  
-    DECLARE checker, ord1, ord2, mod1, mod2, disciplineID INT;  					 
-	
-	SET mod1 = -1;
-	SET mod2 = -1;
-	SET disciplineID = -1;
-	SELECT 	submodules.OrderNum,
-			submodules.ModuleID,
-			disciplines.ID
-	INTO ord1, mod1, disciplineID
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-		 	SubmoduleID1 = submodules.ID
- 	LIMIT 1; 
-
-	SELECT 	submodules.OrderNum,
-			submodules.ModuleID
-	INTO ord2, mod2
-	FROM `submodules`
-	INNER JOIN `modules` 		ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 	ON 	disciplines.ID = modules.DisciplineID
-	WHERE 	TeacherID = disciplines.AuthorID AND
-		 	SubmoduleID2 = submodules.ID
- 	LIMIT 1; 
-	IF 	mod1 <= 0 OR mod1 != mod2 OR
-		InternalIsMapLocked(disciplineID)  THEN
-		RETURN -1;
-	END IF;
-
-	UPDATE `submodules` 
-	SET submodules.OrderNum 	= 271828
-	WHERE submodules.ID = SubmoduleID1
-	LIMIT 1;
-
-	UPDATE `submodules` 
-	SET submodules.OrderNum 	= ord1
-	WHERE submodules.ID = SubmoduleID2
-	LIMIT 1;
-
-	UPDATE `submodules` 
-	SET submodules.OrderNum 	= ord2
-	WHERE submodules.ID = SubmoduleID1
-	LIMIT 1;
-	RETURN 0;
+    DECLARE vDisciplineID, vOrder1, vOrder2, 
+            vModule1, vModule2 INT DEFAULT -1;  
+
+    SELECT  submodules.OrderNum,
+            submodules.ModuleID,
+            disciplines.ID
+        INTO vOrder1, vModule1, vDisciplineID
+        FROM `submodules`
+        INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID
+        INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID
+        WHERE   disciplines.AuthorID = pTeacherID AND
+                SubmoduleID1 = submodules.ID
+        LIMIT 1; 
+
+    SELECT  submodules.OrderNum,
+            submodules.ModuleID
+        INTO vOrder2, vModule2
+        FROM `submodules`
+        INNER JOIN `modules`        ON  submodules.ModuleID = modules.ID
+        INNER JOIN `disciplines`    ON  disciplines.ID = modules.DisciplineID
+        WHERE   disciplines.AuthorID = pTeacherID AND
+                SubmoduleID2 = submodules.ID
+        LIMIT 1; 
+
+    IF  vModule1 <= 0 OR vModule1 != vModule2 OR
+        InternalIsMapLocked(vDisciplineID) 
+    THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE `submodules` 
+    SET submodules.OrderNum     = 271828
+    WHERE submodules.ID = pSubmoduleID1
+    LIMIT 1;
+
+    UPDATE `submodules` 
+    SET submodules.OrderNum     = vOrder1
+    WHERE submodules.ID = pSubmoduleID2
+    LIMIT 1;
+
+    UPDATE `submodules` 
+    SET submodules.OrderNum     = vOrder2
+    WHERE submodules.ID = pSubmoduleID1
+    LIMIT 1;
+    RETURN 0;
 END //
 
 
--- -------------------------------------------------------------------------------------------
--- Label: rating
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: rating
+# -------------------------------------------------------------------------------------------
 
 
 DROP FUNCTION IF EXISTS GetMaxRateForDisc//
-CREATE FUNCTION `GetMaxRateForDisc`	(	`DisciplineID` INT
-									) 	RETURNS int(11)
+CREATE FUNCTION `GetMaxRateForDisc`
+(   `pDisciplineID` INT  )   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE res INT;
-	SET res = 0;
-
-	SELECT SUM(submodules.MaxRate) 
-	INTO res
-	FROM `modules`
-	LEFT JOIN `submodules` ON 	submodules.ModuleID = modules.ID
-	WHERE 	modules.DisciplineID = DisciplineID AND
-			submodules.isUsed != 0 AND
-			(modules.Type = 1 OR ( modules.Type = 2 AND submodules.OrderNum = 1))
-	LIMIT 1;
-
-	RETURN 	(res);
+    DECLARE vResult INT DEFAULT 0;
+    
+    SELECT SUM(submodules.MaxRate) 
+        INTO vResult
+        FROM `modules`
+        LEFT JOIN `submodules` ON   submodules.ModuleID = modules.ID
+        WHERE   modules.DisciplineID = pDisciplineID AND
+                submodules.isUsed != 0 AND
+                (modules.Type = 1 OR ( modules.Type = 2 AND submodules.OrderNum = 1))
+        LIMIT 1;
+    RETURN  (vResult);
 END //
 
 
--- Вычисление максимального балла для submodule
-
+# Вычисление максимального балла для submodule
 DROP FUNCTION IF EXISTS CalculateMaxRateForExtra//
-CREATE FUNCTION `CalculateMaxRateForExtra` ( `SubmoduleID` INT,	`StudentID` INT
-							) RETURNS int(11)
-	NO SQL
+CREATE FUNCTION `CalculateMaxRateForExtra`
+( `pSubmoduleID` INT, `pStudentID` INT) RETURNS int(11)
+    NO SQL
 BEGIN
-	DECLARE examTypeVar VARCHAR(30) charset utf8; -- enum('exam', 'credit');-- utf8_general_ci;
-	DECLARE lim, res INT;
-        SET res=-1;
-
+    DECLARE examTypeVar VARCHAR(30) charset utf8; # enum('exam', 'credit');# utf8_general_ci;
+    DECLARE vLim INT;
+    DECLARE vResult INT DEFAULT -1;
+     
     IF ExamType = 'exam' THEN
-    	SET lim = 38;
-	ELSE 
-		SET lim = 60;
-	END IF; 
-
-	SELECT lim-GetRateForDiscSemester(StudentID, DisciplineID)  
-    INTO res
-	FROM submodules as subm1
-	JOIN modules ON subm1.ModuleID=modules.ID 
-	JOIN disciplines ON DisciplineID=disciplines.ID
-	WHERE modules.type='extra' AND subm1.ID=SubmoduleID;
-	
-	return res;
+        SET vLim = 38;
+    ELSE
+        SET vLim = 60;
+    END IF;
+
+    SELECT vLim - GetRateForDiscSemester( pStudentID, DisciplineID)
+        INTO vResult
+        FROM `submodules`
+        INNER JOIN `modules` ON submodules.ModuleID = modules.ID
+        INNER JOIN `disciplines` ON modules.DisciplineID = disciplines.ID
+        WHERE modules.type='extra' AND submodules.ID = pSubmoduleID;
+    RETURN vResult;
 END //
 
+
 DROP FUNCTION IF EXISTS SetStudentRate//
-CREATE FUNCTION `SetStudentRate`(	`TeacherID` 	INT,
-									`StudentID` 	INT,
-									`SubmoduleID` 	INT,
-									`Rate` 			INT ) 
-									RETURNS int(11)
+CREATE FUNCTION `SetStudentRate`
+(   `pTeacherID` INT, `pStudentID` INT,
+    `pSubmoduleID` INT, `pRate` INT ) RETURNS int(11)
     NO SQL
 BEGIN
     DECLARE checker, DisciplineID, groupID, rateID, maxRate, isOver, mtype INT;
     DECLARE isLocked, isUsed tinyint; 
 
-	SET groupID = -1;
-	SELECT students.StudyGroupID 
-	INTO groupID 
-	FROM `students` 
-	WHERE students.ID = StudentID 
-	LIMIT 1; 
-	IF groupID <= 0 THEN 
-		RETURN -1;
-	END IF;					 
-
-	SET maxRate = CalculateMaxRateForExtra(SubmoduleID, StudentID);
-	
-	SET isOver = 1;
+    SET groupID = -1;
+    SELECT students.StudyGroupID 
+    INTO groupID 
+    FROM `students` 
+    WHERE students.ID = pStudentID 
+    LIMIT 1; 
+    IF groupID <= 0 THEN 
+        RETURN -1;
+    END IF;                  
+
+    SET maxRate = CalculateMaxRateForExtra(pSubmoduleID, pStudentID);
+    
+    SET isOver = 1;
     SET isLocked = 0;
     SET DisciplineID = -1;
     SET mtype = -1;
-    SELECT 	modules.DisciplineID, 
-    		disciplines.isLocked, 
-    		disciplines.isMilestone, 
-    		rating_table.StudentID, 
-    		submodules.isUsed, 
-    		CASE
-				WHEN modules.type='extra' THEN
-				CalculateMaxRateForExtra(SubmoduleID, StudentID)
-			ELSE
-				submodules.maxRate
-			END,
-			modules.Type
+    SELECT  modules.DisciplineID, 
+            disciplines.isLocked, 
+            disciplines.isMilestone, 
+            rating_table.StudentID, 
+            submodules.isUsed, 
+            CASE
+                WHEN modules.type='extra' THEN
+                CalculateMaxRateForExtra(pSubmoduleID, pStudentID)
+            ELSE
+                submodules.maxRate
+            END,
+            modules.Type
     INTO DisciplineID, isLocked, isOver, rateID, isUsed, maxRate, mtype
-	FROM `submodules`
-	INNER JOIN `modules` 				ON 	submodules.ModuleID = modules.ID
-	INNER JOIN `disciplines` 			ON 	modules.DisciplineID = disciplines.ID
-	INNER JOIN `disciplines_teachers` 	ON 	disciplines.ID = disciplines_teachers.DisciplineID AND
-											TeacherID = disciplines_teachers.TeacherID 
-	LEFT JOIN 	`disciplines_groups`	ON 	disciplines.ID = disciplines_groups.DisciplineID AND
-											groupID = disciplines_groups.StudyGroupID
-	LEFT JOIN 	`disciplines_students`	ON 	disciplines.ID = disciplines_students.DisciplineID AND
-											StudentID = disciplines_students.StudentID 	AND
-											1 = disciplines_students.Type
-	LEFT JOIN 	`rating_table`			ON 	SubModuleID = rating_table.SubmoduleID AND
-											StudentID = rating_table.StudentID
-	WHERE 	submodules.ID = SubModuleID AND
-			(	disciplines_students.ID IS NOT NULL OR
-				disciplines_groups.ID IS NOT NULL
-			)
-	LIMIT 1;
-	IF 	DisciplineID <= 0 OR 
-		Rate > maxRate OR 
-		(isOver > 0 AND (mtype = 1 OR mtype = 3))
-	THEN
-		RETURN -1;
-	END IF;
-
-
-	IF rateID IS NOT NULL AND rateID > 0 THEN
-		INSERT INTO `logs_rating`
-				(logs_rating.StudentID, logs_rating.SubmoduleID, logs_rating.TeacherID, logs_rating.Rate, logs_rating.Action )
-		VALUES 	(StudentID, SubModuleID, TeacherID, Rate, 'change');
-
-		UPDATE `rating_table`
-		SET 	rating_table.TeacherID 	= TeacherID,
-				rating_table.Rate 		= Rate,
-				rating_table.Date 		= CURDATE()
-		WHERE 	SubmoduleID = rating_table.SubmoduleID AND 
-				StudentID = rating_table.StudentID
-		LIMIT 1;
-
-	ELSE
-		IF NOT isLocked THEN
-			UPDATE `disciplines`
-			SET 	disciplines.isLocked = 1
-			WHERE	disciplines.ID = DisciplineID
-			LIMIT 1;
-		END IF;
-
-		INSERT INTO `logs_rating`
-				(logs_rating.StudentID, logs_rating.SubmoduleID, logs_rating.TeacherID, logs_rating.Rate, logs_rating.Action )
-		VALUES 	(StudentID, SubModuleID, TeacherID, Rate, 'add');
-
-		INSERT INTO `rating_table` 
-				( 	rating_table.StudentID, 
-					rating_table.TeacherID, 
-					rating_table.SubmoduleID, 
-					rating_table.Rate, 
-					rating_table.Date 
-				)
-		VALUES 	( StudentID, TeacherID, SubmoduleID, Rate, CURDATE() );
-
-		IF NOT isUsed THEN
-			UPDATE `submodules`
-			SET submodules.isUsed = 1
-			WHERE submodules.ID = SubModuleID
-			LIMIT 1;
-		END IF;
-	END IF;
-	RETURN 0;
+    FROM `submodules`
+    INNER JOIN `modules`                ON  submodules.ModuleID = modules.ID
+    INNER JOIN `disciplines`            ON  modules.DisciplineID = disciplines.ID
+    INNER JOIN `disciplines_teachers`   ON  disciplines.ID = disciplines_teachers.DisciplineID AND
+                                            pTeacherID = disciplines_teachers.TeacherID 
+    LEFT JOIN   `disciplines_groups`    ON  disciplines.ID = disciplines_groups.DisciplineID AND
+                                            groupID = disciplines_groups.StudyGroupID
+    LEFT JOIN   `disciplines_students`  ON  disciplines.ID = disciplines_students.DisciplineID AND
+                                            pStudentID = disciplines_students.StudentID  AND
+                                            1 = disciplines_students.Type
+    LEFT JOIN   `rating_table`          ON  SubModuleID = rating_table.SubmoduleID AND
+                                            pStudentID = rating_table.StudentID
+    WHERE   submodules.ID = SubModuleID AND
+            (   disciplines_students.ID IS NOT NULL OR
+                disciplines_groups.ID IS NOT NULL
+            )
+    LIMIT 1;
+    IF  DisciplineID <= 0 OR 
+        pRate > maxRate OR 
+        (isOver > 0 AND (mtype = 1 OR mtype = 3))
+    THEN
+        RETURN -1;
+    END IF;
+
+
+    IF rateID IS NOT NULL AND rateID > 0 THEN
+        INSERT INTO `logs_rating`
+                (logs_rating.StudentID, logs_rating.SubmoduleID, logs_rating.TeacherID, logs_rating.Rate, logs_rating.Action )
+        VALUES  (pStudentID, SubModuleID, pTeacherID, pRate, 'change');
+
+        UPDATE `rating_table`
+        SET     rating_table.TeacherID  = pTeacherID,
+                rating_table.Rate       = pRate,
+                rating_table.Date       = CURDATE()
+        WHERE   pSubmoduleID = rating_table.SubmoduleID AND 
+                pStudentID = rating_table.StudentID
+        LIMIT 1;
+
+    ELSE
+        IF NOT isLocked THEN
+            UPDATE `disciplines`
+            SET     disciplines.isLocked = 1
+            WHERE   disciplines.ID = DisciplineID
+            LIMIT 1;
+        END IF;
+
+        INSERT INTO `logs_rating`
+                (logs_rating.StudentID, logs_rating.SubmoduleID, logs_rating.TeacherID, logs_rating.Rate, logs_rating.Action )
+        VALUES  (pStudentID, SubModuleID, pTeacherID, pRate, 'add');
+
+        INSERT INTO `rating_table` 
+                (   rating_table.StudentID, 
+                    rating_table.TeacherID, 
+                    rating_table.SubmoduleID, 
+                    rating_table.Rate, 
+                    rating_table.Date 
+                )
+        VALUES  ( pStudentID, pTeacherID, pSubmoduleID, pRate, CURDATE() );
+
+        IF NOT isUsed THEN
+            UPDATE `submodules`
+            SET submodules.isUsed = 1
+            WHERE submodules.ID = SubModuleID
+            LIMIT 1;
+        END IF;
+    END IF;
+    RETURN 0;
 END //
 
 
 
 
 
--- -------------------------------------------------------------------------------------------
--- Label: requests
--- -------------------------------------------------------------------------------------------
+# -------------------------------------------------------------------------------------------
+# Label: requests
+# -------------------------------------------------------------------------------------------
 
 
 
 
 DROP PROCEDURE IF EXISTS GetRequests//
-CREATE PROCEDURE `GetRequests`	( 	IN `OffsetN` INT,
-									IN `CountN` INT,
-									IN `AccountID` INT,
-									IN `vStatus` VARCHAR(20) charset utf8
-								)
+CREATE PROCEDURE `GetRequests`
+(   IN `pOffsetN` INT, IN `pCountN` INT,
+    IN `pAccountID` INT, IN `pStatus` VARCHAR(20) charset utf8)
     NO SQL
 BEGIN 
-	SELECT 	requests.ID,
-			requests.AccountID,
-			requests.Title,
-			requests.Description,
-			requests.Date,
-			requests.Status
-	FROM `requests`
-	WHERE 	( AccountID = 0 OR AccountID = requests.AccountID ) AND
-			( vStatus = 'all' OR requests.Status = vStatus	)
-	LIMIT CountN OFFSET OffsetN;
+    SELECT  requests.ID,
+            requests.AccountID,
+            requests.Title,
+            requests.Description,
+            requests.Date,
+            requests.Status
+        FROM `requests`
+        WHERE   ( pAccountID = 0 OR requests.AccountID = pAccountID) AND
+                ( pStatus = 'all' OR requests.Status = pStatus)
+        LIMIT pCountN OFFSET pOffsetN;
 END //
 
 
 DROP FUNCTION IF EXISTS SetRequestStatus//
-CREATE FUNCTION `SetRequestStatus`	( 	`RequestID` INT,
-										`vStatus` VARCHAR(20) charset utf8
-									)	RETURNS int(11)
+CREATE FUNCTION `SetRequestStatus`  
+(`pRequestID` INT, `pStatus` VARCHAR(20) charset utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	UPDATE 	`requests`
-	SET requests.Status = vStatus
-	WHERE RequestID = requests.ID
-	LIMIT 1;
-	RETURN 0;
+    UPDATE `requests`
+        SET requests.Status = pStatus
+        WHERE requests.ID = pRequestID 
+        LIMIT 1;
+    RETURN 0;
 END//
 
 
 DROP FUNCTION IF EXISTS CreateRequest//
-CREATE FUNCTION `CreateRequest`		( 	`AccountID` INT,
-										`vTitle` VARCHAR(50) charset utf8,
-										`vDescription` TEXT charset utf8
-									)	RETURNS int(11)
+CREATE FUNCTION `CreateRequest`     
+(   `pAccountID` INT, `pTitle` VARCHAR(50) charset utf8,
+    `pDescription` TEXT charset utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	INSERT INTO `requests`
-			(requests.AccountID, requests.Title, requests.Description, requests.Status)
-	VALUES	(AccountID, vTitle, vDescription, 'opened');
-	RETURN LAST_INSERT_ID();
+    INSERT INTO `requests`
+        (requests.AccountID, requests.Title, requests.Description, requests.Status)
+        VALUES  (pAccountID, pTitle, pDescription, 'opened');
+    RETURN LAST_INSERT_ID();
 END//
 
 
 
 
 
--- DROP FUNCTION IF EXISTS SetRequestStatus//
--- CREATE FUNCTION `SetRequestStatus`	( 	`RequestID` INT,
--- 										`vStatus` VARCHAR(20) charset utf8
--- 									)	RETURNS int(11)
---     NO SQL
--- BEGIN
--- 	IF vStatus = "closed" THEN
--- 		INSERT INTO `requests_old`
--- 			(	requests.ID, requests.To, requests.From, 
--- 				requests.Field1, requests.Field2, requests.Field3, 
--- 				requests.Data, requests.DataExt, 
--- 				requests.Date, requests.Type, requests.Status
--- 			)
--- 					SELECT 	requests.ID, requests.To, requests.From, 
--- 							requests.Field1, requests.Field2, requests.Field3, 
--- 							requests.Data, requests.DataExt, 
--- 							requests.Date, requests.Type, 'closed' AS 'Status'
--- 					FROM `requests`
--- 					WHERE requests.ID = RequestID
--- 		LIMIT 1;
-
--- 		DELETE FROM `requests`
--- 		WHERE requests.ID = RequestID
--- 		LIMIT 1;
-
-
-
--- 	ELSE
--- 		UPDATE 	`requests`
--- 		SET requests.Status = vStatus
--- 		WHERE RequestID = requests.ID
--- 		LIMIT 1;
--- 	END IF;
--- 	RETURN 0;
--- END//
-
-
--- DROP PROCEDURE IF EXISTS GetRequests//
--- CREATE PROCEDURE `GetRequests`		( 	IN `AccountID` INT,
--- 										IN `Type` INT -- 0 to me, 1 - from me, 3 - all
--- 									)
---     NO SQL
--- BEGIN
--- 	SELECT 	requests.ID, 
--- 			requests.To, 
--- 			requests.From, 
--- 			requests.Field1, 
--- 			requests.Field2, 
--- 			requests.Field3, 
--- 			requests.Data, 
--- 			requests.DataExt, 
--- 			requests.Date, 
--- 			requests.Type, 
--- 			requests.Status
--- 	FROM `requests`
--- 	WHERE ((Type & 1) != 0 AND requests.To = AccountID) OR
--- 		  ((Type & 2) != 0 AND requests.From = AccountID)
---   	ORDER BY requests.To = AccountID DESC, requests.Type ASC, requests.ID ASC;
--- END//
-
-
-
--- DROP FUNCTION IF EXISTS RequestReport//
--- CREATE FUNCTION `RequestReport`		( 	`AccountID` INT,
--- 										`vTitle` VARCHAR(50) charset utf8,
--- 										`vDescription` TEXT charset utf8
--- 									)	RETURNS int(11)
---     NO SQL
--- BEGIN
--- 	INSERT INTO `requests`
--- 			(requests.To, requests.From, requests.Data, requests.DataExt, requests.Type)
--- 	VALUES	(0, AccountID, vDescription, vTitle, 4); -- 4 - report
--- 	RETURN LAST_INSERT_ID();
--- END//
-
-
-
--- DROP FUNCTION IF EXISTS RequestDelegateDiscipline//
--- CREATE FUNCTION `RequestDelegateDiscipline`	(	`AuthorID` INT,
--- 												`NewAuthorID` INT,
--- 												`DisciplineID` INT		
--- 							 				) 	RETURNS int(11)
---     NO SQL
--- BEGIN  
--- 	IF 	AuthorID = NewAuthorID OR
--- 		NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
--- 	THEN
--- 		RETURN -1;
--- 	END IF;
-
--- 	INSERT INTO `requests`
--- 		(requests.To, requests.From, requests.Field1, requests.Type)
--- 	VALUES	(NewAuthorID, AuthorID, DisciplineID, 1);
--- 	RETURN 0;
--- END //
-
-
--- DROP FUNCTION IF EXISTS RequestDeleteDiscipline//
--- CREATE FUNCTION `RequestDeleteDiscipline`	(	`AuthorID` INT,
--- 												`DisciplineID` INT		
--- 							 				) 	RETURNS int(11)
---     NO SQL
--- BEGIN  
--- 	IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
--- 	THEN
--- 		RETURN -1;
--- 	END IF;
-
--- 	INSERT INTO `requests`
--- 		(requests.To, requests.From, requests.Field1, requests.Type)
--- 	VALUES	(0, AuthorID, DisciplineID, 2);
--- 	RETURN 0;
--- END //
-
--- DROP FUNCTION IF EXISTS RequestClearDiscipline//
--- CREATE FUNCTION `RequestClearDiscipline`	(	`AuthorID` INT,
--- 												`DisciplineID` INT		
--- 							 				) 	RETURNS int(11)
---     NO SQL
--- BEGIN  
--- 	IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
--- 	THEN
--- 		RETURN -1;
--- 	END IF;
-
--- 	INSERT INTO `requests`
--- 		(requests.To, requests.From, requests.Field1, requests.Type)
--- 	VALUES	(0, AuthorID, DisciplineID, 3);
--- 	RETURN 0;
--- END //
-
-
-
-
--- -------------------------------------------------------------------------------------------
--- Label: recovery
--- -------------------------------------------------------------------------------------------
+# DROP FUNCTION IF EXISTS SetRequestStatus//
+# CREATE FUNCTION `SetRequestStatus`   (   `RequestID` INT,
+#                                      `vStatus` VARCHAR(20) charset utf8
+#                                  )   RETURNS int(11)
+#     NO SQL
+# BEGIN
+#  IF vStatus = "closed" THEN
+#      INSERT INTO `requests_old`
+#          (   requests.ID, requests.To, requests.From, 
+#              requests.Field1, requests.Field2, requests.Field3, 
+#              requests.Data, requests.DataExt, 
+#              requests.Date, requests.Type, requests.Status
+#          )
+#                  SELECT  requests.ID, requests.To, requests.From, 
+#                          requests.Field1, requests.Field2, requests.Field3, 
+#                          requests.Data, requests.DataExt, 
+#                          requests.Date, requests.Type, 'closed' AS 'Status'
+#                  FROM `requests`
+#                  WHERE requests.ID = RequestID
+#      LIMIT 1;
+
+#      DELETE FROM `requests`
+#      WHERE requests.ID = RequestID
+#      LIMIT 1;
+
+
+
+#  ELSE
+#      UPDATE  `requests`
+#      SET requests.Status = vStatus
+#      WHERE RequestID = requests.ID
+#      LIMIT 1;
+#  END IF;
+#  RETURN 0;
+# END//
+
+
+# DROP PROCEDURE IF EXISTS GetRequests//
+# CREATE PROCEDURE `GetRequests`       (   IN `AccountID` INT,
+#                                      IN `Type` INT # 0 to me, 1 - from me, 3 - all
+#                                  )
+#     NO SQL
+# BEGIN
+#  SELECT  requests.ID, 
+#          requests.To, 
+#          requests.From, 
+#          requests.Field1, 
+#          requests.Field2, 
+#          requests.Field3, 
+#          requests.Data, 
+#          requests.DataExt, 
+#          requests.Date, 
+#          requests.Type, 
+#          requests.Status
+#  FROM `requests`
+#  WHERE ((Type & 1) != 0 AND requests.To = AccountID) OR
+#        ((Type & 2) != 0 AND requests.From = AccountID)
+#      ORDER BY requests.To = AccountID DESC, requests.Type ASC, requests.ID ASC;
+# END//
+
+
+
+# DROP FUNCTION IF EXISTS RequestReport//
+# CREATE FUNCTION `RequestReport`      (   `AccountID` INT,
+#                                      `vTitle` VARCHAR(50) charset utf8,
+#                                      `vDescription` TEXT charset utf8
+#                                  )   RETURNS int(11)
+#     NO SQL
+# BEGIN
+#  INSERT INTO `requests`
+#          (requests.To, requests.From, requests.Data, requests.DataExt, requests.Type)
+#  VALUES  (0, AccountID, vDescription, vTitle, 4); # 4 - report
+#  RETURN LAST_INSERT_ID();
+# END//
+
+
+
+# DROP FUNCTION IF EXISTS RequestDelegateDiscipline//
+# CREATE FUNCTION `RequestDelegateDiscipline`  (   `AuthorID` INT,
+#                                              `NewAuthorID` INT,
+#                                              `DisciplineID` INT      
+#                                          )   RETURNS int(11)
+#     NO SQL
+# BEGIN  
+#  IF  AuthorID = NewAuthorID OR
+#      NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
+#  THEN
+#      RETURN -1;
+#  END IF;
+
+#  INSERT INTO `requests`
+#      (requests.To, requests.From, requests.Field1, requests.Type)
+#  VALUES  (NewAuthorID, AuthorID, DisciplineID, 1);
+#  RETURN 0;
+# END //
+
+
+# DROP FUNCTION IF EXISTS RequestDeleteDiscipline//
+# CREATE FUNCTION `RequestDeleteDiscipline`    (   `AuthorID` INT,
+#                                              `DisciplineID` INT      
+#                                          )   RETURNS int(11)
+#     NO SQL
+# BEGIN  
+#  IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
+#  THEN
+#      RETURN -1;
+#  END IF;
+
+#  INSERT INTO `requests`
+#      (requests.To, requests.From, requests.Field1, requests.Type)
+#  VALUES  (0, AuthorID, DisciplineID, 2);
+#  RETURN 0;
+# END //
+
+# DROP FUNCTION IF EXISTS RequestClearDiscipline//
+# CREATE FUNCTION `RequestClearDiscipline` (   `AuthorID` INT,
+#                                              `DisciplineID` INT      
+#                                          )   RETURNS int(11)
+#     NO SQL
+# BEGIN  
+#  IF  NOT InternalIsTeacherAuthor(AuthorID, DisciplineID)
+#  THEN
+#      RETURN -1;
+#  END IF;
+
+#  INSERT INTO `requests`
+#      (requests.To, requests.From, requests.Field1, requests.Type)
+#  VALUES  (0, AuthorID, DisciplineID, 3);
+#  RETURN 0;
+# END //
+
+
+
+
+# -------------------------------------------------------------------------------------------
+# Label: recovery
+# -------------------------------------------------------------------------------------------
 
 
 
 DROP FUNCTION IF EXISTS CreateRecoveryToken//
-CREATE FUNCTION `CreateRecoveryToken`	( 	`AccountEMail` VARCHAR(255) charset utf8,
-											`Token` VARCHAR(100) charset utf8
-										)	RETURNS int(11)
+CREATE FUNCTION `CreateRecoveryToken`   
+(   `pAccountOrEMail` VARCHAR(255) charset utf8,
+    `pToken` VARCHAR(100) charset utf8)   RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-        DECLARE accID INT;
+    DECLARE vChecker INT DEFAULT 0;
+    DECLARE vAccountID INT DEFAULT -1;
 
-        SET accID = -1;
-        SELECT accounts.ID
-        INTO accID
+    SELECT accounts.ID
+        INTO vAccountID
         FROM `accounts`
-        WHERE accounts.EMail = AccountEMail
+        WHERE accounts.EMail = pAccountOrEMail
+        LIMIT 1;
+    IF vAccountID <= 0 THEN
+        RETURN -1;
+    END IF; 
+
+    SELECT recovery_tokens.ID
+        INTO vChecker
+        FROM `recovery_tokens`
+        WHERE recovery_tokens.Token = pToken
         LIMIT 1;
-        IF accID <= 0 THEN
-		RETURN -1;
-	END IF; 
-
-	SET checker = 0;
-	SELECT recovery_tokens.ID
-	INTO checker
-	FROM `recovery_tokens`
-	WHERE recovery_tokens.Token = Token
-	LIMIT 1;
-	IF checker > 0 THEN
-		RETURN -1;
-	END IF; 
-
-	INSERT INTO `recovery_tokens`
-			(recovery_tokens.AccountID, recovery_tokens.Token )
-	VALUES	(accID, Token);
-	RETURN LAST_INSERT_ID();
+    IF vChecker > 0 THEN
+        RETURN -1;
+    END IF; 
+
+    INSERT INTO `recovery_tokens`
+        (recovery_tokens.AccountID, recovery_tokens.Token )
+        VALUES  (vAccountID, pToken);
+    RETURN LAST_INSERT_ID();
 END//
 
 
 
 DROP FUNCTION IF EXISTS UseRecoveryToken//
-CREATE FUNCTION `UseRecoveryToken`	( 	`Token` VARCHAR(100) charset utf8
-									)	RETURNS int(11)
+CREATE FUNCTION `UseRecoveryToken`
+(   `pToken` VARCHAR(100) charset utf8) RETURNS int(11)
     NO SQL
 BEGIN
-	DECLARE checker INT;
-
-	SET checker = -1;
-	SELECT recovery_tokens.ID
-	INTO checker
-	FROM `recovery_tokens`
-	WHERE recovery_tokens.Token = Token
-	LIMIT 1;
-	IF checker <= 0 THEN
-		RETURN -1;
-	END IF; 
-
-	UPDATE `recovery_tokens`
-	SET recovery_tokens.isUsed = 1
-	WHERE recovery_tokens.Token = Token
-	LIMIT 1;
-        RETURN 0;
+    DECLARE checker INT DEFAULT -1;
+
+    SELECT recovery_tokens.ID
+        INTO checker
+        FROM `recovery_tokens`
+        WHERE recovery_tokens.Token = pToken
+        LIMIT 1;
+    IF checker <= 0 THEN
+        RETURN -1;
+    END IF; 
+
+    UPDATE `recovery_tokens`
+        SET recovery_tokens.isUsed = 1
+        WHERE recovery_tokens.Token = pToken
+        LIMIT 1;
+    RETURN 0;
 END//
 
 
 
 DROP PROCEDURE IF EXISTS GetRecoveryInfoByToken//
-CREATE PROCEDURE `GetRecoveryInfoByToken`	( 	IN `Token` VARCHAR(100) charset utf8
-											)
+CREATE PROCEDURE `GetRecoveryInfoByToken`   
+(IN `pToken` VARCHAR(100) charset utf8)
     NO SQL
 BEGIN 
-	SELECT 	recovery_tokens.ID,
-			recovery_tokens.AccountID,
-			recovery_tokens.Date,
-			recovery_tokens.Token,
-			recovery_tokens.isUsed
-	FROM `recovery_tokens`
-	WHERE recovery_tokens.Token = Token
-	LIMIT 1;
+    SELECT  recovery_tokens.ID,
+            recovery_tokens.AccountID,
+            recovery_tokens.Date,
+            recovery_tokens.Token,
+            recovery_tokens.isUsed
+        FROM `recovery_tokens`
+        WHERE recovery_tokens.Token = pToken
+        LIMIT 1;
 END //
 
+
+
 DROP PROCEDURE IF EXISTS GetRecoveryInfoByEMail//
-CREATE PROCEDURE `GetRecoveryInfoByEMail`	( 	IN `EMail` VARCHAR(255) charset utf8
-											)
+CREATE PROCEDURE `GetRecoveryInfoByEMail`
+(IN `pEMail` VARCHAR(255) charset utf8)
     NO SQL
 BEGIN 
-        DECLARE accID INT;
-
-        SET accID = -1;
-        SELECT accounts.ID
-        INTO accID
+    SELECT  recovery_tokens.ID,
+            recovery_tokens.AccountID,
+            recovery_tokens.Date,
+            recovery_tokens.Token,
+            recovery_tokens.isUsed
         FROM `accounts`
-        WHERE accounts.EMail = EMail
+        INNER JOIN `recovery_tokens` ON recovery_tokens.AccountID = accounts.ID
+        WHERE   accounts.EMail = pEMail AND  
+                recovery_tokens.isUsed = 0
         LIMIT 1;
-        IF accID <= 0 THEN
-		SELECT 	NULL AS 'ID'
-		LIMIT 0;
-	END IF;
-
-	SELECT 	recovery_tokens.ID,
-			recovery_tokens.AccountID,
-			recovery_tokens.Date,
-			recovery_tokens.Token,
-			recovery_tokens.isUsed
-	FROM `recovery_tokens`
-	WHERE recovery_tokens.AccountID = accID
-        AND recovery_tokens.isUsed = 0;
 END //
 
 
@@ -4029,181 +3959,175 @@ END //
 
 
 DROP PROCEDURE IF EXISTS GetReports//
-CREATE PROCEDURE `GetReports`	( 	IN `StudyGroupID` INT
-								)
+CREATE PROCEDURE `GetReports` (IN `pGroupID` INT)
     NO SQL
 BEGIN 
-		SELECT 	students.ID AS 'StudentID',
-				students.LastName,
-				students.FirstName,
-				students.SecondName,
-				disciplines.ID,
-				GetRateForDisc(students.ID, disciplines.ID) AS 'Rate',
-				GetMaxRateForDisc(disciplines.ID) AS 'MaxRate'
-		FROM `students`
-		LEFT JOIN `disciplines` ON disciplines.ID IN 
-			(
-				SELECT disciplines_groups.DisciplineID
-				FROM `disciplines_groups`
-				WHERE disciplines_groups.StudyGroupID = StudyGroupID 
-				UNION DISTINCT
-				SELECT disciplines_students.DisciplineID
-				FROM `disciplines_students`
-				INNER JOIN `students` ON disciplines_students.ID = students.ID
-				WHERE students.StudyGroupID = StudyGroupID
-			)
-		INNER JOIN `subjects` ON disciplines.SubjectID = subjects.ID
-		WHERE students.StudyGroupID = StudyGroupID;
+    SELECT  students.ID AS 'StudentID',
+            students.LastName,
+            students.FirstName,
+            students.SecondName,
+            disciplines.ID,
+            GetRateForDisc(students.ID, disciplines.ID) AS 'Rate',
+            GetMaxRateForDisc(disciplines.ID) AS 'MaxRate'
+        FROM `students`
+        LEFT JOIN `disciplines` ON disciplines.ID IN 
+            (
+                SELECT disciplines_groups.DisciplineID
+                    FROM `disciplines_groups`
+                    WHERE disciplines_groups.StudyGroupID = pGroupID 
+                    UNION DISTINCT
+                    SELECT disciplines_students.DisciplineID
+                    FROM `disciplines_students`
+                    INNER JOIN `students` ON disciplines_students.ID = students.ID
+                    WHERE students.StudyGroupID = pGroupID
+            )
+        INNER JOIN `subjects` ON disciplines.SubjectID = subjects.ID
+        WHERE students.StudyGroupID = pGroupID;
 END //
 
 
 
 
--- DROP PROCEDURE IF EXISTS GetReports//
--- CREATE PROCEDURE `GetReports`	( 	IN `StudyGroupID` INT
--- 								)
---     NO SQL
--- BEGIN 
--- 		-- CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines` (
--- 		--   	`DisciplineID` int(11) NOT NULL,
--- 		-- 	`MaxRate` int(11) NOT NULL DEFAULT '0',
--- 		--   	PRIMARY KEY (`DisciplineID`)
--- 		-- ) ENGINE=MEMORY DEFAULT CHARSET=utf8 AS (
--- 		-- 	SELECT disciplines_groups.DisciplineID,
--- 		-- 					0 AS 'MaxRate'
--- 		-- 	FROM `disciplines_groups`
--- 		-- 	WHERE disciplines_groups.StudyGroupID = StudyGroupID 
--- 		-- 	UNION DISTINCT
--- 		-- 	SELECT disciplines_students.DisciplineID,
--- 		-- 					0 AS 'MaxRate'
--- 		-- 	FROM `disciplines_students`
--- 		-- 	INNER JOIN `students` ON disciplines_students.ID = students.ID
--- 		-- 	WHERE students.StudyGroupID = StudyGroupID
--- 		-- );
-
--- 		CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines`
--- 			SELECT disciplines_groups.DisciplineID,
--- 							0 AS 'MaxRate'
--- 			FROM `disciplines_groups`
--- 			WHERE disciplines_groups.StudyGroupID = StudyGroupID 
--- 			UNION DISTINCT
--- 			SELECT disciplines_students.DisciplineID,
--- 							0 AS 'MaxRate'
--- 			FROM `disciplines_students`
--- 			INNER JOIN `students` ON disciplines_students.ID = students.ID
--- 			WHERE students.StudyGroupID = StudyGroupID;
-
--- 		UPDATE `temp_disciplines`
---     	SET temp_disciplines.MaxRate = GetMaxRateForDisc(temp_disciplines.DisciplineID);
-
--- 		SELECT 	students.ID AS 'StudentID',
--- 				temp_disciplines.DisciplineID,
--- 				temp_disciplines.MaxRate,
--- 				GetRateForDisc(students.ID, temp_disciplines.DisciplineID) AS 'Rate'
--- 		FROM `students`
--- 		LEFT JOIN `temp_disciplines` ON 1=1
--- 		WHERE students.StudyGroupID = StudyGroupID;
--- END //
+# DROP PROCEDURE IF EXISTS GetReports//
+# CREATE PROCEDURE `GetReports`    (   IN `StudyGroupID` INT
+#                              )
+#     NO SQL
+# BEGIN 
+#      # CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines` (
+#      #      `DisciplineID` int(11) NOT NULL,
+#      #  `MaxRate` int(11) NOT NULL DEFAULT '0',
+#      #      PRIMARY KEY (`DisciplineID`)
+#      # ) ENGINE=MEMORY DEFAULT CHARSET=utf8 AS (
+#      #  SELECT disciplines_groups.DisciplineID,
+#      #                  0 AS 'MaxRate'
+#      #  FROM `disciplines_groups`
+#      #  WHERE disciplines_groups.StudyGroupID = StudyGroupID 
+#      #  UNION DISTINCT
+#      #  SELECT disciplines_students.DisciplineID,
+#      #                  0 AS 'MaxRate'
+#      #  FROM `disciplines_students`
+#      #  INNER JOIN `students` ON disciplines_students.ID = students.ID
+#      #  WHERE students.StudyGroupID = StudyGroupID
+#      # );
+
+#      CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines`
+#          SELECT disciplines_groups.DisciplineID,
+#                          0 AS 'MaxRate'
+#          FROM `disciplines_groups`
+#          WHERE disciplines_groups.StudyGroupID = StudyGroupID 
+#          UNION DISTINCT
+#          SELECT disciplines_students.DisciplineID,
+#                          0 AS 'MaxRate'
+#          FROM `disciplines_students`
+#          INNER JOIN `students` ON disciplines_students.ID = students.ID
+#          WHERE students.StudyGroupID = StudyGroupID;
+
+#      UPDATE `temp_disciplines`
+#      SET temp_disciplines.MaxRate = GetMaxRateForDisc(temp_disciplines.DisciplineID);
+
+#      SELECT  students.ID AS 'StudentID',
+#              temp_disciplines.DisciplineID,
+#              temp_disciplines.MaxRate,
+#              GetRateForDisc(students.ID, temp_disciplines.DisciplineID) AS 'Rate'
+#      FROM `students`
+#      LEFT JOIN `temp_disciplines` ON 1=1
+#      WHERE students.StudyGroupID = StudyGroupID;
+# END //
 
 DROP PROCEDURE IF EXISTS GetDisciplinesForGroup//
-CREATE PROCEDURE `GetDisciplinesForGroup`	(	IN `GroupID` INT
-											)
+CREATE PROCEDURE `GetDisciplinesForGroup`(IN `pGroupID` INT)
     NO SQL
-BEGIN  		
-	SELECT disciplines_groups.DisciplineID As DisciplineID,
-		subjects.Name As SubjectName,
-		disciplines.ExamType As ExamType
-	FROM `disciplines_groups`
-	INNER JOIN `disciplines` ON disciplines.ID = disciplines_groups.DisciplineID
-	INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
-	WHERE disciplines_groups.StudyGroupID = GroupID 
-	UNION DISTINCT
-	SELECT disciplines_students.DisciplineID  As DisciplineID,
-		subjects.Name As SubjectName,
-		disciplines.ExamType As ExamType
-	FROM `disciplines_students`
-	INNER JOIN `students` ON disciplines_students.StudentID = students.ID
-	INNER JOIN `disciplines` ON disciplines.ID = disciplines_students.DisciplineID
-	INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
-	WHERE students.StudyGroupID = GroupID;
+BEGIN       
+    SELECT  disciplines_groups.DisciplineID AS 'DisciplineID',
+            subjects.Name AS 'SubjectName',
+            disciplines.ExamType AS 'ExamType'
+        FROM `disciplines_groups`
+        INNER JOIN `disciplines` ON disciplines.ID = disciplines_groups.DisciplineID
+        INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
+        WHERE disciplines_groups.StudyGroupID = pGroupID 
+    UNION DISTINCT
+    SELECT  disciplines_students.DisciplineID  AS "DisciplineID",
+            subjects.Name AS "SubjectName",
+            disciplines.ExamType AS "ExamType"
+        FROM `disciplines_students`
+        INNER JOIN `students` ON disciplines_students.StudentID = students.ID
+        INNER JOIN `disciplines` ON disciplines.ID = disciplines_students.DisciplineID
+        INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
+        WHERE students.StudyGroupID = pGroupID;
 END //
 
 
 DROP PROCEDURE IF EXISTS GetRatesForStudentsGroup//
-CREATE PROCEDURE `GetRatesForStudentsGroup`	(	IN `TeacherID` INT,
-                                                        IN `DisciplineID` INT,
-                                                        IN `StudyGroupID` INT
-                                                )
+CREATE PROCEDURE `GetRatesForStudentsGroup`
+(IN `somePar` INT, IN `pTeacherID` INT, IN `pDisciplineID` INT, IN `pGroupID` INT)
     NO SQL
-BEGIN  		
--- TODO: TeacherID deprecated		 
-	SELECT 	students.ID 	AS 'ID',
-			students.LastName			AS 'Last',
-			students.FirstName			AS 'First',
-			students.SecondName 		AS 'Second',
-			GetRateForDiscSemester(students.ID, DisciplineID) AS 'intermediate',
-			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
-	-- 						(modules.Type = 'exam' OR modules.Type = 'extra')	
-	-- LEFT JOIN `submodules` ON 	submodules.ModuleID = modules.ID
-	WHERE 	study_groups.ID = StudyGroupID AND 
-			InternalIsStudentAttached(students.ID, DisciplineID)
-	ORDER BY Last ASC;
+BEGIN       
+    # TODO: TeacherID deprecated        
+    SELECT  students.ID,
+            students.LastName AS 'Last',
+            students.FirstName AS 'First',
+            students.SecondName AS 'Second',
+            GetRateForDiscSemester(students.ID, pDisciplineID) AS 'intermediate',
+            GetRateForDiscBonus(students.ID, pDisciplineID) AS 'bonus',
+            GetRateForDiscExam(students.ID, pDisciplineID) AS 'exam'
+        FROM `students`
+        INNER JOIN `study_groups` ON study_groups.ID = students.StudyGroupID
+        # LEFT JOIN `modules` ON   modules.DisciplineID = DisciplineID AND
+        #                      (modules.Type = 'exam' OR modules.Type = 'extra')   
+        # LEFT JOIN `submodules` ON    submodules.ModuleID = modules.ID
+        WHERE   study_groups.ID = pGroupID AND 
+                InternalIsStudentAttached(students.ID, pDisciplineID)
+        ORDER BY students.LastName ASC;
 
 END //
 
 
 
 DROP PROCEDURE IF EXISTS GetFinalFormInfo//
-CREATE PROCEDURE `GetFinalFormInfo`	(	IN `DisciplineID` INT,
-										IN `StudyGroupID` INT
-									)
+CREATE PROCEDURE `GetFinalFormInfo` 
+(IN `pDisciplineID` INT, IN `pGroupID` INT)
     NO SQL
 BEGIN 
-	DECLARE curSem INT;
-	SET curSem = @CurrentSemesterID;
-
-	SELECT 	study_groups.GroupNum 	AS 'GroupNum', 
-			study_groups.Name 		AS 'GroupName',
-			grades.ID 				AS 'GradeID',
-			grades.Num 				AS 'GradeNum',
-			grades.Degree 			AS 'Degree',	
-			specializations.ID 		AS 'SpecID',
-			specializations.Name 	AS 'SpecName',
-			specializations.Abbr 	AS 'SpecAbbr',
-			specializations.Code 	AS 'SpecCode',
-			faculties.ID 			AS 'FacultyID',
-			faculties.Name 			AS 'FacultyName',
-			faculties.Abbr 			AS 'FacultyAbbr',
-			disciplines.ExamType 	AS 'ExamType',
-			subjects.ID 			AS 'SubjectID',
-			subjects.Name 			AS 'SubjectName',
-			subjects.Abbr 			AS 'SubjectAbbr',
-			teachers.ID 			AS 'AuthorID',
-			teachers.LastName 		AS 'LastName',
-			teachers.FirstName 		AS 'FirstName', 
-			teachers.SecondName		AS 'SecondName', 
-			job_positions.Name 		AS 'JobPosition', 
-			departments.ID 			AS 'DepID',
-			departments.Name 		AS 'DepName',
-			semesters.Year 			AS 'Year',
-			semesters.Num 			AS 'SemesterNum'
-            
-	FROM `study_groups`
-	INNER JOIN `specializations` 	ON study_groups.SpecializationID = specializations.ID
-	INNER JOIN `grades` 			ON study_groups.GradeID = grades.ID
-	INNER JOIN `faculties` 			ON faculties.ID = specializations.FacultyID
-	INNER JOIN `disciplines` 		ON disciplines.ID = DisciplineID
-	INNER JOIN `subjects` 			ON disciplines.SubjectID = subjects.ID
-	INNER JOIN `teachers` 			ON teachers.ID = disciplines.AuthorID
-	INNER JOIN `departments` 		ON departments.ID = teachers.DepartmentID
-	INNER JOIN `job_positions` 		ON job_positions.ID = teachers.JobPositionID
-	INNER JOIN `semesters`			ON semesters.ID = curSem
-	WHERE study_groups.ID = StudyGroupID 
-	LIMIT 1;
+    DECLARE curSem INT;
+    SET vCurSemester = @CurrentSemesterID;
+
+    SELECT  study_groups.GroupNum AS 'GroupNum', 
+            study_groups.Name AS 'GroupName',
+            grades.ID AS 'GradeID',
+            grades.Num AS 'GradeNum',
+            grades.Degree AS 'Degree',    
+            specializations.ID AS 'SpecID',
+            specializations.Name AS 'SpecName',
+            specializations.Abbr AS 'SpecAbbr',
+            specializations.Code AS 'SpecCode',
+            faculties.ID AS 'FacultyID',
+            faculties.Name AS 'FacultyName',
+            faculties.Abbr AS 'FacultyAbbr',
+            disciplines.ExamType AS 'ExamType',
+            subjects.ID AS 'SubjectID',
+            subjects.Name AS 'SubjectName',
+            subjects.Abbr AS 'SubjectAbbr',
+            teachers.ID AS 'AuthorID',
+            teachers.LastName AS 'LastName',
+            teachers.FirstName AS 'FirstName', 
+            teachers.SecondName AS 'SecondName', 
+            job_positions.Name AS 'JobPosition', 
+            departments.ID AS 'DepID',
+            departments.Name AS 'DepName',
+            semesters.Year AS 'Year',
+            semesters.Num AS 'SemesterNum'
+        FROM `study_groups`
+        INNER JOIN `specializations` ON study_groups.SpecializationID = specializations.ID
+        INNER JOIN `grades` ON study_groups.GradeID = grades.ID
+        INNER JOIN `faculties` ON faculties.ID = specializations.FacultyID
+        INNER JOIN `disciplines` ON disciplines.ID = pDisciplineID
+        INNER JOIN `subjects` ON disciplines.SubjectID = subjects.ID
+        INNER JOIN `teachers` ON teachers.ID = disciplines.AuthorID
+        INNER JOIN `departments` ON departments.ID = teachers.DepartmentID
+        INNER JOIN `job_positions` ON job_positions.ID = teachers.JobPositionID
+        INNER JOIN `semesters` ON semesters.ID = vCurSemester
+        WHERE study_groups.ID = pGroupID 
+        LIMIT 1;
 END //
 
 DELIMITER ;
\ No newline at end of file
diff --git a/db/Views.sql b/db/Views.sql
new file mode 100644
index 000000000..e6bb6a2ff
--- /dev/null
+++ b/db/Views.sql
@@ -0,0 +1,147 @@
+CREATE OR REPLACE VIEW `view_study_groups` AS 
+    SELECT  study_groups.ID         AS 'GroupID',
+            study_groups.GroupNum   AS 'GroupNum',
+            study_groups.Name       AS 'GroupName',
+            grades.ID               AS 'GradeID',
+            grades.Num              AS 'GradeNum',
+            grades.Degree           AS 'Degree',
+            specializations.ID      AS 'SpecID',
+            specializations.Name    AS 'SpecName',
+            specializations.Abbr    AS 'SpecAbbr',
+            faculties.ID        AS 'FacultyID', 
+            faculties.Name      AS 'FacultyName', 
+            faculties.Abbr      AS 'FacultyAbbr'
+    FROM `study_groups`
+    INNER JOIN `specializations`    ON study_groups.SpecializationID = specializations.ID
+    INNER JOIN `grades` ON study_groups.GradeID = grades.ID
+    INNER JOIN `faculties` ON faculties.ID = specializations.FacultyID; 
+
+
+
+
+
+CREATE OR REPLACE VIEW `view_teachers` AS 
+    SELECT  teachers.ID         AS 'TeacherID', 
+            teachers.LastName, 
+            teachers.FirstName, 
+            teachers.SecondName, 
+            teachers.AccountID,
+            job_positions.ID    AS 'JobPositionID',
+            job_positions.Name  AS 'JobPositionName', 
+            departments.ID      AS 'DepID',
+            departments.Name    AS 'DepName',
+            faculties.ID        AS 'FacultyID', 
+            faculties.Name      AS 'FacultyName', 
+            faculties.Abbr      AS 'FacultyAbbr'
+    FROM `teachers`
+    INNER JOIN `departments`    ON  departments.ID = teachers.DepartmentID
+    INNER JOIN `faculties`      ON  departments.FacultyID = faculties.ID
+    INNER JOIN `job_positions`  ON  job_positions.ID = teachers.JobPositionID;
+
+
+CREATE OR REPLACE VIEW `view_students` AS 
+    SELECT  students.ID         AS 'StudentID',
+            students.LastName,
+            students.FirstName,
+            students.SecondName,
+            students.AccountID,
+            view_study_groups.*
+    FROM `students`
+    INNER JOIN `view_study_groups` ON view_study_groups.GroupID = students.StudyGroupID;
+
+
+CREATE OR REPLACE VIEW `view_disciplines` AS 
+    SELECT  disciplines.ID          AS 'DisciplineID',
+            disciplines.AuthorID,
+            disciplines.ExamType,
+            disciplines.LectionCount,
+            disciplines.PracticeCount,
+            disciplines.LabCount,
+            disciplines.SemesterID,
+            disciplines.isLocked,
+            disciplines.isMilestone,
+            grades.ID               AS 'GradeID',
+            grades.Num              AS 'GradeNum',
+            grades.Degree,
+            subjects.ID             AS 'SubjectID',
+            subjects.Name           AS 'SubjectName',
+            subjects.Abbr           AS 'SubjectAbbr',
+            faculties.ID            AS 'FacultyID',
+            faculties.Name          AS 'FacultyName',
+            faculties.Abbr          AS 'FacultyAbbr'
+    FROM `disciplines`
+    INNER JOIN `subjects` ON subjects.ID = disciplines.SubjectID
+    INNER JOIN `faculties` ON faculties.ID = disciplines.FacultyID
+    INNER JOIN `grades` ON grades.ID = disciplines.GradeID;
+
+
+CREATE OR REPLACE VIEW `view_disciplines_teachers` AS 
+    SELECT  view_disciplines.DisciplineID,
+            view_teachers.*,
+            (view_disciplines.AuthorID = view_teachers.TeacherID) AS 'IsAuthor'
+    FROM `view_disciplines`
+    LEFT JOIN `disciplines_teachers` ON disciplines_teachers.DisciplineID = view_disciplines.DisciplineID
+    INNER JOIN `view_teachers` ON view_teachers.TeacherID = disciplines_teachers.TeacherID;
+
+
+CREATE OR REPLACE VIEW `view_disciplines_students` AS 
+    SELECT  disciplines_students.DisciplineID,  
+            view_students.*
+    FROM `disciplines_students`
+    INNER JOIN `view_students` ON    view_students.StudentID = disciplines_students.StudentID 
+    WHERE disciplines_students.Type = 'attach'
+    UNION DISTINCT
+    SELECT  disciplines_groups.DisciplineID,
+            view_students.*
+    FROM `disciplines_groups`
+    LEFT JOIN `view_students` ON view_students.GroupID = disciplines_groups.StudyGroupID
+    LEFT JOIN `disciplines_students` ON disciplines_students.StudentID = view_students.StudentID AND
+                                        disciplines_students.DisciplineID = disciplines_groups.DisciplineID
+    WHERE disciplines_students.Type IS NULL; 
+
+
+CREATE OR REPLACE VIEW `view_roadmap` AS
+    SELECT  modules.DisciplineID,
+            modules.ID AS 'ModuleID',
+            modules.Name AS 'ModuleName',
+            modules.OrderNum AS 'ModuleOrderNum',
+            modules.Type AS 'ModuleType', 
+            # enum('regular','exam', 'bonus', 'extra')
+            submodules.ID AS 'SubmoduleID',
+            submodules.Name AS 'SubmoduleName',
+            submodules.OrderNum AS 'SubmoduleOrderNum',
+            submodules.MaxRate AS 'SubmoduleRate',
+            submodules.Type AS 'SubmoduleType',
+            # enum('CurrentControl','LandmarkControl')
+            submodules.isUsed AS 'SubmoduleIsUsed'
+    FROM `submodules`
+    INNER JOIN `modules` ON submodules.ModuleID = modules.ID;
+
+
+CREATE OR REPLACE VIEW `view_rating` AS
+    SELECT  view_students.*,
+            view_roadmap.*,
+            rating_table.Rate,
+            rating_table.Date,
+            rating_table.TeacherID
+    FROM `view_roadmap`
+    LEFT JOIN `rating_table` ON rating_table.SubmoduleID = view_roadmap.SubmoduleID 
+    INNER JOIN `view_students` ON rating_table.StudentID = view_students.StudentID; 
+
+
+CREATE OR REPLACE VIEW `view_rating_result` AS
+    SELECT  view_rating.StudentID,
+            view_rating.DisciplineID,  
+            SUM(view_rating.Rate*(view_rating.ModuleType = "regular")) AS 'RateRegular',
+            SUM(view_rating.Rate*(view_rating.ModuleType = "extra")) AS 'RateExtra',
+            SUM(view_rating.Rate*(view_rating.ModuleType = "bonus")) AS 'RateBonus',
+            (SELECT view_rating.Rate*(view_rating.ModuleType = "exam")
+                ORDER BY    view_rating.ModuleType = "exam" DESC, 
+                            (view_rating.Rate IS NULL) ASC, 
+                            view_rating.SubmoduleOrderNum DESC 
+                LIMIT 1
+            ) AS 'RateExam'
+    FROM `view_rating`
+    WHERE view_rating.Rate IS NOT NULL
+    GROUP BY view_rating.StudentID, view_rating.DisciplineID;
+
-- 
GitLab