diff --git a/db/postgresql/functions.sql b/db/postgresql/functions.sql
new file mode 100644
index 0000000000000000000000000000000000000000..75e4e22b08aa8c6bea25bb8ccc699283451feb4c
--- /dev/null
+++ b/db/postgresql/functions.sql
@@ -0,0 +1,4185 @@
+-- ВСПОМОГАТЕЛЬНЫЕ
+DROP FUNCTION IF EXISTS public.iif_sql(BOOLEAN, integer, integer);
+
+CREATE OR REPLACE FUNCTION iif_sql(BOOLEAN, integer, integer) 
+RETURNS integer AS
+$$ 
+SELECT CASE $1 WHEN TRUE THEN $2 ELSE $3 END 
+$$
+LANGUAGE SQL IMMUTABLE;
+
+
+
+-------------------------------------------------------------------------------------------
+
+
+--
+-- Name: getsemestersinfo(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getsemestersinfo(integer);
+
+CREATE OR REPLACE FUNCTION public.getsemestersinfo(psemesterid integer)
+ RETURNS  setof semesters
+AS $$
+-- select public.getsemestersinfo(:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4));
+SELECT  semesters.* -- ID, Year, Num
+    FROM semesters
+    WHERE  case when $1 <> 0 then semesters.ID = $1 else true end
+    ORDER BY semesters.ID DESC;
+$$ LANGUAGE sql;
+
+--
+-- Name: getdepartmentidbyname(character varying); Type: FUNCTION;
+--
+DROP FUNCTION IF EXISTS public.getdepartmentidbyname(character varying);
+
+CREATE OR REPLACE FUNCTION public.getdepartmentidbyname(pdepartmentname character varying)
+ RETURNS integer
+as $$
+-- select public.getdepartmentidbyname(	:pdepartmentname 	-- put the pdepartmentname parameter value instead of 'pdepartmentname' (varchar));
+-- видимо расчитано на уникальность имени кафедры, возможно не используется, или нужно добавить еще параметр - факультет
+    SELECT departments.ID as ID
+    FROM departments
+    WHERE departments.Name = pDepartmentName;
+$$ language sql;
+
+--
+-- Name: getbonusmodule(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getbonusmodule(integer);
+
+CREATE OR REPLACE FUNCTION public.getbonusmodule(pdisciplineid integer)
+ RETURNS integer
+AS $$
+-- select public.getbonusmodule(:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4));
+-- limit 1 оставлено, хотя ситуация с возникновением в одной дисциплине нескольких бонусных модулей сомнительная,
+-- может это нужно контролировать ограничением на таблицу
+    SELECT modules.ID FROM modules
+        WHERE modules.DisciplineID = pDisciplineID AND modules.Type = 'bonus'
+        LIMIT 1;
+$$ LANGUAGE sql;
+
+--
+-- Name: getauthtokens(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getauthtokens(integer);
+
+CREATE OR REPLACE FUNCTION public.getauthtokens(paccountid integer)
+ RETURNS TABLE(accountid integer, created timestamp without time zone, accessed timestamp without time zone, mask integer)
+AS $$
+-- select * from public.getauthtokens(:paccountid 	-- put the paccountid parameter value instead of 'paccountid' (int4));
+-- если вызывать select  public.getauthtokens(...); выдает таблицу с одним солбцом - все данные склены в список
+    SELECT accountID, Created, Accessed, Mask
+    FROM auth_tokens
+    INNER JOIN accounts ON accounts.ID = auth_tokens.AccountID
+    WHERE
+        auth_tokens.AccountID = pAccountID OR
+        pAccountID = 0
+    ORDER BY auth_tokens.Created DESC;
+$$ LANGUAGE sql
+
+--
+-- Name: getcompounddisciplinesforgrade(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getcompounddisciplinesforgrade(integer);
+
+CREATE OR REPLACE FUNCTION public.getcompounddisciplinesforgrade(pgradeid integer)
+ RETURNS TABLE(id integer, name character varying)
+AS $$
+--select * from public.getcompounddisciplinesforgrade(	:pgradeid 	-- put the pgradeid parameter value instead of 'pgradeid' (int4));
+    SELECT  compound_disciplines.ID,
+            compound_disciplines.Name
+        FROM compound_disciplines
+        WHERE compound_disciplines.GradeID = pGradeID;
+$$ LANGUAGE sql;
+
+--
+-- Name: getdisciplines(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getdisciplines(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getdisciplines(
+	pfacultyid integer,
+	psemesterid integer)
+RETURNS table(
+id integer
+,SubjectID  integer
+,SubjectName  character varying
+,Type exam_credit_grading_credit
+,isMapCreated boolean)
+AS $$
+--select * from public.getdisciplines(
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+    SELECT  view_disciplines.DisciplineID AS ID,
+            view_disciplines.SubjectID,
+            view_disciplines.SubjectName,
+            view_disciplines.ExamType AS Type,
+            (view_disciplines.MaxRate = 100) AS isMapCreated
+        FROM view_disciplines
+        WHERE   view_disciplines.SemesterID = pSemesterID AND
+                view_disciplines.FacultyID = pFacultyID
+        ORDER BY view_disciplines.SubjectName ASC;
+$$ LANGUAGE sql;
+
+--
+-- Name: getdisciplinesforteacher(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getdisciplinesforteacher(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getdisciplinesforteacher(
+	pteacherid integer,
+	psemesterid integer)
+RETURNS table(
+id integer
+,Type exam_credit_grading_credit
+,Subtype scientific_disciplinary_coursework
+,GradeID integer
+,GradeNum integer
+,Degree bachelor_master_specialist
+,GroupID integer
+,GroupNum integer
+,GroupName character varying
+,SubjectID integer
+,SubjectName character varying
+,AuthorID integer
+,IsLocked integer
+,IsMapCreated boolean)
+AS $$
+--select public.getdisciplinesforteacher(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+    SELECT DISTINCT view_disciplines.DisciplineID AS ID,
+                view_disciplines.ExamType AS Type,
+                view_disciplines.Subtype,
+                view_disciplines.GradeID,
+                view_disciplines.GradeNum,
+                view_disciplines.Degree,
+                view_groups.GroupID,
+                view_groups.GroupNum,
+                view_groups.GroupName,
+                view_disciplines.SubjectID,
+                view_disciplines.SubjectName,
+                view_disciplines.AuthorID,
+                view_disciplines.IsLocked AS IsLocked, -- bodging, db schema remembered lowerCase
+                (view_disciplines.MaxRate = 100)    AS IsMapCreated
+    FROM disciplines_teachers
+    INNER JOIN view_disciplines ON disciplines_teachers.DisciplineID = view_disciplines.DisciplineID
+    INNER JOIN semesters ON semesters.ID = view_disciplines.SemesterID
+    LEFT JOIN disciplines_groups ON disciplines_groups.DisciplineID = disciplines_teachers.DisciplineID
+    LEFT JOIN view_groups ON view_groups.GroupID = disciplines_groups.GroupID AND view_groups.Year = semesters.Year
+    WHERE disciplines_teachers.TeacherID = pTeacherID AND view_disciplines.SemesterID = pSemesterID
+    ORDER BY    view_disciplines.GradeID ASC,
+                view_disciplines.SubjectName ASC,
+                view_disciplines.DisciplineID ASC,
+                view_groups.GroupNum ASC;
+$$ LANGUAGE sql;
+
+--
+-- Name: getfinalforminfo(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getfinalforminfo(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getfinalforminfo(
+	pdisciplineid integer,
+	pgroupid integer)
+RETURNS table(
+GroupNum integer
+,GroupName character varying
+,GradeID integer
+,GradeNum integer
+,Degree bachelor_master_specialist
+,SpecID integer
+,SpecName character varying
+,SpecAbbr character varying
+,SpecCode character varying
+,FacultyID integer
+,FacultyName character varying
+,FacultyAbbr character varying
+,ExamType exam_credit_grading_credit
+,SubjectID integer
+,SubjectName character varying
+,SubjectAbbr character varying
+,AuthorID integer
+,LastName character varying
+,FirstName character varying
+,SecondName character varying
+,JobPosition character varying
+,DepID integer
+,DepName character varying
+,Year integer
+,SemesterNum integer)
+AS $$
+--select * from public.getfinalforminfo(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pgroupid 	-- put the pgroupid parameter value instead of 'pgroupid' (int4)
+--);
+    SELECT  study_groups.GroupNum AS GroupNum,
+            groups_years.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,
+            accounts.LastName AS LastName,
+            accounts.FirstName AS FirstName,
+            accounts.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 grades ON study_groups.GradeID = grades.ID
+        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 accounts ON teachers.AccountID = accounts.ID
+        INNER JOIN departments ON departments.ID = teachers.DepartmentID
+        INNER JOIN job_positions ON job_positions.ID = teachers.JobPositionID
+        INNER JOIN semesters ON disciplines.SemesterID = semesters.ID
+        INNER JOIN groups_years ON groups_years.GroupID = study_groups.ID AND groups_years.Year = semesters.Year
+        INNER JOIN specializations ON groups_years.SpecializationID = specializations.ID
+        INNER JOIN faculties ON faculties.ID = specializations.FacultyID
+        WHERE study_groups.ID = pGroupID
+        LIMIT 1;
+$$ LANGUAGE sql;
+
+--
+-- Name: getgrades(); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getgrades();
+
+CREATE OR REPLACE FUNCTION public.getgrades(
+	)
+RETURNS setof grades
+AS $$
+--select public.getgrades();
+SELECT  grades.* -- ID, Num, Degree
+    FROM grades
+    ORDER BY grades.ID;   
+$$ LANGUAGE sql;
+
+--
+-- Name: getgroups(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getgroups(integer, integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getgroups(
+	pgradeid integer,
+	pfacultyid integer,
+	psemesterid integer)
+RETURNS table(
+id integer
+,GroupNum integer
+,SpecID integer
+,SpecName character varying
+,SpecAbbr character varying)  
+AS $$
+--select * from public.getgroups(
+--	:pgradeid,	-- put the pgradeid parameter value instead of 'pgradeid' (int4)
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+    SELECT  view_groups.GroupID AS ID,
+            view_groups.GroupNum,
+            view_groups.SpecID,
+            view_groups.SpecName,
+            view_groups.SpecAbbr
+        FROM view_groups
+        INNER JOIN semesters ON semesters.ID = pSemesterID
+        WHERE   view_groups.GradeID = pGradeID AND
+                view_groups.FacultyID = pFacultyID AND
+                view_groups.Year = semesters.Year
+        ORDER BY view_groups.GroupNum ASC;
+$$ LANGUAGE sql;
+
+--
+-- Name: userecoverytoken(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.userecoverytoken(character varying);
+
+CREATE OR REPLACE FUNCTION public.userecoverytoken(ptoken character varying)
+ RETURNS integer
+ LANGUAGE plpgsql
+AS $$
+    DECLARE vChecker INT DEFAULT -1;
+BEGIN
+    -- set token used
+	-- select public.userecoverytoken(
+	-- :ptoken 	-- put the ptoken parameter value instead of 'ptoken' (varchar)
+    --);
+    UPDATE recovery_tokens
+        SET IsUsed = 1
+        WHERE Token = pToken;
+      -- LIMIT 1;  не нужен, т.к. token имеет unique key
+      GET diagnostics vChecker=ROW_COUNT; --другого способа не нашла
+   -- RETURN ROW_COUNT-1;
+   return vchecker-1;
+END;
+$$;
+
+--
+-- Name: updaterequest(integer, character varying, text); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.updaterequest(integer, character varying, text);
+
+CREATE OR REPLACE FUNCTION public.updaterequest(prequestid integer, ptitle character varying, pdescription text)
+ RETURNS integer
+ LANGUAGE plpgsql
+AS $$
+    DECLARE retValue INT DEFAULT -1;
+BEGIN
+    --DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -1; непонятно зачем обрабочик,ошибки быть не может
+--  select public.UpdateRequest(
+--	:pRequestID,	-- put the pRequestID parameter value instead of 'pRequestID' (INT)
+--	:pTitle,	-- put the pTitle parameter value instead of 'pTitle' (VARCHAR(50))
+--	:pDescription 	-- put the pDescription parameter value instead of 'pDescription' (CHAR(1))
+--);
+    UPDATE requests
+        SET requests.Description = pDescription,
+            requests.Title = pTitle,
+            requests.Date = NOW()
+        WHERE   requests.ID = pRequestID AND
+                requests.Description IS NULL AND
+                requests.Title IS NULL;
+        --LIMIT 1; -- запрос через id не может вернуть больше 1 строки
+    --RETURN ROW_COUNT()-1;
+    GET diagnostics retValue=ROW_COUNT;
+   return retValue-1;
+END;
+$$;
+
+--
+-- Name: teacher_getidfromexternalid(varchar); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.teacher_getidfromexternalid(varchar); 
+
+CREATE OR REPLACE FUNCTION public.teacher_getidfromexternalid(pteacherexternalid character varying)
+ RETURNS integer
+ LANGUAGE plpgsql
+AS $$
+        DECLARE pID INT DEFAULT -1;
+begin
+--	select public.teacher_getidfromexternalid(
+--	:pteacherexternalid 	-- put the pteacherexternalid parameter value instead of 'pteacherexternalid' (varchar)
+--);
+        SELECT teachers.ID INTO pID
+        FROM teachers
+            INNER JOIN accounts ON teachers.AccountID = accounts.ID
+        WHERE accounts.ExternalID = pTeacherExternalID
+        LIMIT 1; -- скорее всего не требуется
+        RETURN pID;
+    END;
+$$;
+
+--
+-- Name: jobpositions_get(); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.jobpositions_get();
+
+CREATE OR REPLACE FUNCTION public.jobpositions_get()
+ RETURNS setof job_positions
+ LANGUAGE sql
+AS $$
+--select public.jobpositions_get();
+SELECT  job_positions.* -- ID, Name
+  FROM    job_positions
+  ORDER BY job_positions.Name;
+$$;
+
+--
+-- Name: getgroupsfordiscipline(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.etgroupsfordiscipline(integer);
+
+CREATE OR REPLACE FUNCTION public.getgroupsfordiscipline(
+	pdisciplineid integer)
+RETURNS table(
+ID integer
+,GroupNum integer
+,GradeID integer
+,GradeNum integer
+,Degree bachelor_master_specialist
+,SpecID integer
+,SpecName character varying
+,SpecAbbr character varying)
+AS $$
+--select * from public.getgroupsfordiscipline(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    SELECT  view_groups.GroupID AS ID,
+            view_groups.GroupNum,
+            view_groups.GradeID,
+            view_groups.GradeNum,
+            view_groups.Degree,
+            view_groups.SpecID,
+            view_groups.SpecName,
+            view_groups.SpecAbbr
+        FROM disciplines_groups
+        INNER JOIN disciplines ON disciplines_groups.DisciplineID = disciplines.ID
+        INNER JOIN semesters ON disciplines.SemesterID = semesters.ID
+        INNER JOIN view_groups ON disciplines_groups.GroupID = view_groups.GroupID
+        WHERE   disciplines_groups.DisciplineID = pDisciplineID
+                AND view_groups.Year = semesters.Year
+        ORDER BY view_groups.GradeID ASC, view_groups.GroupID ASC;
+
+$$ Language sql;
+
+--
+-- Name: getjobpositionidbyname(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getjobpositionidbyname(character varying);
+
+CREATE OR REPLACE FUNCTION public.getjobpositionidbyname(
+	pjobpositionname character varying)
+RETURNS integer
+AS $$
+--select public.getjobpositionidbyname(
+--	:pjobpositionname 	-- put the pjobpositionname parameter value instead of 'pjobpositionname' (varchar)
+--);
+
+    SELECT job_positions.ID as ID
+    FROM job_positions
+    WHERE job_positions.Name = pJobPositionName;
+$$ Language sql;
+
+--
+-- Name: getjobpositions(); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getjobpositions();
+
+CREATE OR REPLACE FUNCTION public.getjobpositions(
+	)
+RETURNS setof job_positions
+AS $$
+--select * from public.getjobpositions();
+SELECT  job_positions.* -- ID, Name
+    FROM    job_positions
+    ORDER BY job_positions.Name;
+$$ Language sql;
+
+--
+-- Name: getratesall(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getratesall(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getratesall(
+	pstudentid integer,
+	pdisciplineid integer)
+RETURNS table(
+ModuleID integer
+,ModuleName character varying
+,SubmoduleID integer
+,SubmoduleName character varying
+,MaxRate integer
+,SubmoduleType current_landmark_control
+,SubmoduleOrderNum integer
+,Rate integer
+,Date date
+,ModuleType regular_exam_bonus_extra
+,ExamPeriodOption absence_pass)
+AS $$
+--select * from public.getratesall(
+--	:pstudentid,	-- put the pstudentid parameter value instead of 'pstudentid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    SELECT  view_roadmap.ModuleID,
+            view_roadmap.ModuleName,
+            view_roadmap.SubmoduleID,
+            view_roadmap.SubmoduleName,
+            view_roadmap.SubmoduleRate AS MaxRate,
+            view_roadmap.SubmoduleType,
+            view_roadmap.SubmoduleOrderNum,
+            rating_table.Rate,
+            rating_table.Date,
+            view_roadmap.ModuleType,
+            exam_period_options.Type As ExamPeriodOption
+    FROM view_roadmap
+    LEFT JOIN rating_table    ON  rating_table.SubmoduleID = view_roadmap.SubmoduleID AND
+                                    rating_table.recordbookid = pStudentID
+    LEFT JOIN exam_period_options ON  exam_period_options.recordbookid = pStudentID AND
+                                        exam_period_options.SubmoduleID = view_roadmap.SubmoduleID
+    WHERE view_roadmap.DisciplineID = pDisciplineID
+    ORDER BY    view_roadmap.ModuleOrderNum ASC,
+                view_roadmap.SubmoduleOrderNum ASC;
+$$ Language sql;
+
+--
+-- Name: getratesexam(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getratesexam(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.getratesexam(
+	pstudentid integer,
+	pdisciplineid integer)
+RETURNS table(
+ModuleID integer
+,ModuleName character varying
+,SubmoduleID integer
+,SubmoduleName character varying
+,MaxRate integer
+,SubmoduleType current_landmark_control
+,Rate integer
+,Date date
+,ModuleType regular_exam_bonus_extra
+,ExamPeriodOption absence_pass)
+AS $$
+--select * from public.getratesexam(
+--	:pstudentid,	-- put the pstudentid parameter value instead of 'pstudentid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    SELECT  view_roadmap.ModuleID,
+            view_roadmap.ModuleName,
+            view_roadmap.SubmoduleID,
+            view_roadmap.SubmoduleName,
+            view_roadmap.SubmoduleRate AS MaxRate,
+            view_roadmap.SubmoduleType,
+            rating_table.Rate,
+            rating_table.Date,
+            view_roadmap.ModuleType,
+            exam_period_options.Type As ExamPeriodOption
+    FROM view_roadmap
+    LEFT JOIN rating_table    ON  rating_table.SubmoduleID = view_roadmap.SubmoduleID AND
+                                    rating_table.recordbookid = pStudentID
+    LEFT JOIN exam_period_options ON  exam_period_options.recordbookid = pStudentID AND
+                                        exam_period_options.SubmoduleID = view_roadmap.SubmoduleID
+    WHERE view_roadmap.DisciplineID = pDisciplineID
+    ORDER BY    view_roadmap.ModuleOrderNum ASC,
+                view_roadmap.SubmoduleOrderNum ASC;
+$$ Language sql;
+
+--
+-- Name: getrecoveryinfobyemail(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getrecoveryinfobyemail(character varying);
+
+CREATE OR REPLACE FUNCTION public.getrecoveryinfobyemail(
+	pemail character varying)
+RETURNS table(
+ID integer
+,AccountID integer
+,Date timestamp
+,Token character varying
+,IsUsed smallint)
+AS $$
+--select * from public.getrecoveryinfobyemail(
+--	:pemail 	-- put the pemail parameter value instead of 'pemail' (varchar)
+--);
+
+    SELECT  recovery_tokens.ID,
+            recovery_tokens.AccountID,
+            recovery_tokens.Date,
+            recovery_tokens.Token,
+            recovery_tokens.IsUsed
+        FROM accounts
+        INNER JOIN recovery_tokens ON recovery_tokens.AccountID = accounts.ID
+        WHERE   accounts.EMail = pEMail AND
+                recovery_tokens.IsUsed = 0
+        LIMIT 1;
+$$ Language sql;
+
+--
+-- Name: getrecoveryinfobytoken(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getrecoveryinfobytoken(character varying);
+
+CREATE OR REPLACE FUNCTION public.getrecoveryinfobytoken(
+	ptoken character varying)
+RETURNS table(
+ID integer
+,AccountID integer
+,Date timestamp
+,Token character varying
+,IsUsed smallint)
+AS $$
+--select * from public.getrecoveryinfobytoken(
+--	:ptoken 	-- put the ptoken parameter value instead of 'ptoken' (varchar)
+--);
+
+    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;
+$$ Language sql;
+
+--
+-- Name: internalismapcreated(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.internalismapcreated(integer);
+
+CREATE OR REPLACE FUNCTION public.internalismapcreated(pdisciplineid integer)
+ RETURNS boolean
+ AS $$
+--select public.internalismapcreated(
+--	:pdisciplineid-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+declare maxRate integer;
+BEGIN
+    maxRate=null;
+    SELECT disciplines.MaxRate FROM disciplines into maxRate
+    WHERE disciplines.ID = pDisciplineID ;
+    RETURN (maxrate is not null and maxrate=100);
+END
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: internalismaplocked(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.internalismaplocked(integer);
+
+CREATE OR REPLACE FUNCTION public.internalismaplocked(pdisciplineid integer)
+ RETURNS boolean
+AS $$
+--select public.internalismaplocked(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+declare isLocked integer;
+BEGIN
+	isLocked =null;
+	SELECT disciplines.IsLocked FROM disciplines into isLocked
+    WHERE disciplines.ID = pDisciplineID;
+    return (isLocked is not null and isLocked =1);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: internalisteacherauthor(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.internalisteacherauthor(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.internalisteacherauthor(pteacherid integer, pdisciplineid integer)
+ RETURNS boolean
+AS $$
+--select public.internalisteacherauthor(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+declare author integer;
+BEGIN
+    author = NULL; 
+    SELECT disciplines.AuthorID FROM disciplines into author
+    WHERE disciplines.ID = pDisciplineID;
+    return (author is not null and author = pTeacherID);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: internalisteacherbound(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.internalisteacherbound(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.internalisteacherbound(pteacherid integer, pdisciplineid integer)
+ RETURNS boolean
+AS $$
+--select public.internalisteacherbound(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--  :pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+BEGIN
+RETURN EXISTS (
+    SELECT * FROM disciplines_teachers
+    WHERE disciplines_teachers.TeacherID = pTeacherID AND disciplines_teachers.DisciplineID = pDisciplineID
+    );
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: internalordermoduletypesforsession(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.internalordermoduletypesforsession(integer);
+
+CREATE OR REPLACE FUNCTION public.internalordermoduletypesforsession(pmoduletype integer)
+ RETURNS integer
+AS $$
+--select public.internalordermoduletypesforsession(
+--	:pmoduletype 	-- put the pmoduletype parameter value instead of 'pmoduletype' (int4)
+--);
+BEGIN
+RETURN (CASE pModuleType
+    WHEN 4 THEN 1 -- extra
+    WHEN 2 THEN 2 -- exam
+    WHEN 3 THEN 3 -- bonus
+    ELSE 4
+END);
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addmodule(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addmodule(integer, integer, character varying);
+
+CREATE OR REPLACE FUNCTION public.addmodule(pteacherid integer, pdisciplineid integer, pname character varying)
+ RETURNS integer
+AS $$
+
+    DECLARE vOrderNum INT DEFAULT 0;
+    DECLARE currID int DEFAULT 0;
+BEGIN
+--select public.addmodule(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pname 	-- put the pname parameter value instead of 'pname' (varchar)
+--);
+ 
+    if  (NOT InternalIsTeacherAuthor(pTeacherID, pDisciplineID) OR InternalIsMapLocked(pDisciplineID) )THEN
+        RETURN -2;
+       end if;
+  -- get free orderNum for module with type 'regular'
+    SELECT coalesce(MAX(modules.OrderNum)+1,1) into  vOrderNum 
+    FROM modules
+       WHERE   modules.DisciplineID = pDisciplineID AND modules.Type = 'regular';
+
+    INSERT INTO modules (Name, OrderNum, DisciplineID ) VALUES (pName, vOrderNum, pDisciplineID) returning  id into currID;
+    RETURN currID;
+EXCEPTION
+   WHEN others  then return -1;
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addsubmoduleunsafe(integer, integer, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addsubmoduleunsafe(integer, integer, character varying, character varying, character varying);
+
+CREATE OR REPLACE FUNCTION public.addsubmoduleunsafe(pmoduleid integer, pmaxrate integer, pname character varying, pdescription character varying, pcontroltype character varying)
+ RETURNS integer
+
+AS $$
+--select public.addsubmoduleunsafe(
+--	:pmoduleid,	-- put the pmoduleid parameter value instead of 'pmoduleid' (int4)
+--	:pmaxrate,	-- put the pmaxrate parameter value instead of 'pmaxrate' (int4)
+--	:pname,	-- put the pname parameter value instead of 'pname' (varchar)
+--	:pdescription,	-- put the pdescription parameter value instead of 'pdescription' (varchar)
+--	:pcontroltype 	-- put the pcontroltype parameter value instead of 'pcontroltype' (varchar)
+--);
+    DECLARE vOrderNum INT DEFAULT -1; vIsLocked INT DEFAULT -1; vDescription VARCHAR(200); recId int ;
+BEGIN
+    -- check discipline lock
+    SELECT disciplines.IsLocked INTO vIsLocked
+        FROM modules
+        INNER JOIN disciplines ON disciplines.ID = modules.DisciplineID
+        WHERE modules.ID = pModuleID
+        LIMIT 1;
+    IF  vIsLocked != 0 THEN
+        RETURN -2;
+    END IF;
+
+    -- get free order
+        SELECT COALESCE(MAX(submodules.OrderNum),0)+1 FROM submodules
+        WHERE submodules.ModuleID = pModuleID LIMIT 1 into vOrderNum;
+
+         if (vDescription = '')   THEN   vDescription := NULL ;
+          ELSE  vDescription :=pDescription; 
+         end if ; -- очень странная проверка
+    INSERT INTO submodules (ModuleID, MaxRate, OrderNum, Name, Description, Type) VALUES
+        (pModuleID, pMaxRate, vOrderNum, pName, vDescription, pControlType) returning id into recID;
+    RETURN  recID;
+  exception 
+   when others then return -1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addsubmodule(integer, integer, integer, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addsubmodule(integer, integer, integer, character varying, character varying, character varying);
+
+CREATE OR REPLACE FUNCTION public.addsubmodule(pteacherid integer, pmoduleid integer, pmaxrate integer, pname character varying, pdescription character varying, pcontroltype character varying)
+ RETURNS integer
+
+AS $$
+
+    DECLARE vIsLocked BOOLEAN DEFAULT TRUE;
+   vID integer default null;
+begin
+--	select public.addsubmodule(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pmoduleid,	-- put the pmoduleid parameter value instead of 'pmoduleid' (int4)
+--	:pmaxrate,	-- put the pmaxrate parameter value instead of 'pmaxrate' (int4)
+--	:pname,	-- put the pname parameter value instead of 'pname' (varchar)
+--	:pdescription,	-- put the pdescription parameter value instead of 'pdescription' (varchar)
+--	:pcontroltype 	-- put the pcontroltype parameter value instead of 'pcontroltype' (varchar)
+--);
+
+    -- check author and discipline lock
+    SELECT disciplines.AuthorID into vID
+         FROM modules
+        INNER JOIN disciplines ON  disciplines.ID = modules.DisciplineID
+        WHERE modules.ID = pModuleID
+        LIMIT 1;
+       IF vID is null or vID !=pTeacherID  THEN
+        RETURN -2;
+    END IF;
+    
+ RETURN AddSubmoduleUnsafe(pModuleID, pMaxRate, pName, pDescription, pControlType);
+
+END;
+$$  LANGUAGE plpgsql;
+
+
+--
+-- Name: addmoduleextra(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addmoduleextra(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.addmoduleextra(pteacherid integer, pdisciplineid integer)
+ RETURNS integer
+AS $$
+    DECLARE vChecker INT DEFAULT -1; vModule INT DEFAULT -1; vType INT DEFAULT -1; vGap INT DEFAULT -1;
+begin
+--	select public.AddModuleExtra(
+--	:pTeacherID,	-- put the pTeacherID parameter value instead of 'pTeacherID' (INT)
+--	:pDisciplineID 	-- put the pDisciplineID parameter value instead of 'pDisciplineID' (INT)
+--);
+
+    IF  NOT InternalIsTeacherAuthor(pTeacherID, pDisciplineID)
+    THEN
+        RETURN -1;
+    END IF;
+    -- try to find existing extra module
+    SELECT modules.ID INTO vChecker
+        FROM modules
+        WHERE modules.DisciplineID = pDisciplineID AND modules.Type = 'extra'
+        LIMIT 1;
+    IF vChecker > 0 THEN
+        RETURN -2;
+    END IF;
+
+    -- add extra module
+    INSERT INTO modules
+        (Name, OrderNum, DisciplineID, Type)
+        VALUES ('Добор баллов' , 2900666 , pDisciplineID, 'extra');
+
+	-- get discipline exam type
+    SELECT  modules.ID, disciplines.ExamType INTO vModule, vType
+        FROM modules
+        INNER JOIN disciplines ON disciplines.ID = modules.DisciplineID
+        WHERE   modules.DisciplineID = pDisciplineID AND
+                modules.Type = 'extra'
+        LIMIT 1;
+    IF vModule <= 0 THEN
+        RETURN -1;
+    END IF;
+
+    -- 1 extra attempt for exam and 2 for credit
+    vGap := -1;
+    IF vType = 1 THEN -- exam
+        vGap := 7;
+    END IF;
+    IF vType = 2 OR vType = 3 THEN -- credit, grading_credit
+        vGap := 29;
+        vChecker := addsubmodule(pTeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
+    END IF;
+
+    vChecker := AddSubmodule(pTeacherID, vModule, vGap, '', NULL, 'LandmarkControl');
+    RETURN vModule;
+   exception 
+   when others then return -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addmoduleexamunsafe(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addmoduleexamunsafe(integer);
+
+CREATE OR REPLACE FUNCTION public.addmoduleexamunsafe(pdisciplineid integer)
+ RETURNS integer
+AS $$
+    DECLARE vModule INT DEFAULT -1; vChecker INT DEFAULT -1;
+    vIsExamExists BOOLEAN; vTemp int;
+BEGIN
+--select public.AddModuleExamUnsafe(
+--	:pDisciplineID 	-- put the pDisciplineID parameter value instead of 'pDisciplineID' (INT)
+--);
+    IF InternalIsMapLocked(pDisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    vIsExamExists := EXISTS(
+        SELECT * FROM modules
+        WHERE modules.DisciplineID = pDisciplineID AND modules.Type = 'exam'
+        LIMIT 1
+    );
+    IF vIsExamExists THEN
+        RETURN -2;
+    END IF;
+
+    INSERT INTO modules (Name, OrderNum, DisciplineID, Type) VALUES  ('Экзамен' , 3141592 , pDisciplineID, 'exam') 
+   				returning  ID into vModule;
+    -- 3 attempt for pass exam -- разобраться почему так?
+    SELECT AddSubmoduleUnsafe(vModule, 40, '', NULL, 'LandmarkControl') INTO vTemp;
+    SELECT AddSubmoduleUnsafe(vModule, 40, '', NULL, 'LandmarkControl') INTO vTemp;
+    SELECT AddSubmoduleUnsafe(vModule, 40, '', NULL, 'LandmarkControl') INTO vTemp;
+    RETURN vModule;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addmoduleexam(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addmoduleexam(integer, integer);
+CREATE OR REPLACE FUNCTION public.addmoduleexam(pteacherid integer, pdisciplineid integer)
+ RETURNS integer
+AS $$
+    DECLARE vModule INT DEFAULT -1; vChecker INT DEFAULT -1;
+    vIsExamExists BOOLEAN;
+begin
+--	select public.addmoduleexam(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    IF (NOT InternalIsTeacherAuthor(pTeacherID, pDisciplineID) OR
+        InternalIsMapLocked(pDisciplineID))
+    THEN
+        RETURN -1;
+    END IF;
+
+    RETURN AddModuleExamUnsafe(pDisciplineID);
+END 
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: addmodulebonus(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.addmodulebonus(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.addmodulebonus(pteacherid integer, pdisciplineid integer)
+ RETURNS integer
+AS $$
+    DECLARE vChecker INT DEFAULT -1; vModuleID INT DEFAULT -1;
+begin
+--	select public.addmodulebonus(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    IF  (NOT InternalIsTeacherAuthor(pTeacherID, pDisciplineID) OR
+        InternalIsMapLocked(pDisciplineID))
+    THEN
+        RETURN -1;
+    END IF;
+    -- check existing of bonus module
+    SELECT modules.ID INTO vChecker
+        FROM modules
+        WHERE   modules.DisciplineID = pDisciplineID AND
+                modules.Type = 'bonus';
+    IF vChecker > 0 THEN
+        RETURN -2;
+    END IF;
+
+    INSERT INTO modules
+        (Name, OrderNum, DisciplineID, Type)
+        VALUES  ('Бонусные баллы' , 2141692 , pDisciplineID, 'bonus') RETURNING ID into  vModuleID;
+    vChecker := AddSubmodule(pTeacherID, vModuleID, 10, '', NULL, 'LandmarkControl');
+    RETURN 0; -- почему???
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: getdisciplinetypeexam(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getdisciplinetypeexam(integer);
+
+CREATE OR REPLACE FUNCTION public.getdisciplinetypeexam(pdisciplineid integer)
+ RETURNS exam_credit_grading_credit
+AS $$ 
+--select public.getdisciplinetypeexam(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+       select examtype 
+          from disciplines 
+          where id=pdisciplineid;
+ $$  LANGUAGE sql;
+
+--
+-- Name: getdisciplineproperty(integer, grade_subject_author_semester_milestone_type_maxrate); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getdisciplineproperty(integer, grade_subject_author_semester_milestone_type_maxrate); 
+
+CREATE OR REPLACE FUNCTION public.getdisciplineproperty(pdisciplineid integer, ptype grade_subject_author_semester_milestone_type_maxrate)
+ RETURNS integer
+AS $$
+    DECLARE vRes INT DEFAULT -1;
+begin
+--	select public.getdisciplineproperty(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (grade_subject_author_semester_milestone_type_maxrate)
+--);
+    SELECT CASE pType
+            WHEN 'semester' THEN disciplines.SemesterID
+            WHEN 'author' THEN disciplines.AuthorID
+            WHEN 'grade' THEN disciplines.GradeID
+            WHEN 'subject' THEN disciplines.SubjectID
+            WHEN 'milestone' THEN disciplines.Milestone
+         --   WHEN 'type' THEN cast (disciplines.ExamType as integer) +0
+            WHEN 'maxrate' THEN disciplines.MaxRate
+            else -1
+        END INTO vRes
+        FROM disciplines
+        WHERE disciplines.ID = pDisciplineID
+        LIMIT 1;
+     RETURN vRes;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: calculatemaxrateforextra(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.calculatemaxrateforextra(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.calculatemaxrateforextra(pdisciplineid integer, pRecordBookID integer)
+ RETURNS integer
+AS $$
+    DECLARE vExamType exam_credit_grading_credit DEFAULT NULL;
+    vDiscID INT DEFAULT 0; vLim INT DEFAULT 0; vResult INT DEFAULT 0;
+BEGIN
+--select public.calculatemaxrateforextra(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pRecordBookID 	-- put the pRecordBookID parameter value instead of 'pRecordBookID' (int4)
+--);
+    -- get disc type
+    --vExamType := GetDisciplineProperty(pDisciplineID, 'type');
+    vExamType := GetDisciplineTypeExam(pDisciplineID);
+    -- submodule doesn't exists
+    IF vExamType IS NULL --OR vExamType <= 0 
+    THEN
+        RETURN -1;
+    END IF;
+    vLim := CASE WHEN vExamType = 'exam' THEN  38 ELSE  60 END;
+
+    SELECT SUM(CASE WHEN view_roadmap.ModuleType = 'regular' THEN  rating_table.Rate ELSE  0 END) INTO vResult
+        FROM view_roadmap
+        LEFT JOIN rating_table
+        --ON rating_table.StudentID = pStudentID AND
+        ON rating_table.recordbookid = pRecordBookID AND
+                                    rating_table.SubmoduleID = view_roadmap.SubmoduleID
+        WHERE view_roadmap.DisciplineID = pDisciplineID
+        LIMIT 1;
+    RETURN vLim - vResult;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: changemodulename(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.changemodulename(integer, integer, character varying);
+
+CREATE OR REPLACE FUNCTION public.changemodulename(pteacherid integer, pmoduleid integer, pname character varying)
+ RETURNS integer
+AS $$
+    DECLARE vDisciplineID INT DEFAULT -1;
+    vAlreadySameName BOOL DEFAULT FALSE;
+    rowUpd int;
+BEGIN
+--select public.changesubmoduledescription(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psubmoduleid,	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--	:pdescription 	-- put the pdescription parameter value instead of 'pdescription' (varchar)
+--);
+
+     SELECT DisciplineID FROM modules WHERE ID = pModuleID LIMIT 1 into vDisciplineID;
+    IF InternalIsMapLocked(vDisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    -- Bodging, strange row_count() behaviour in some cases.
+    -- i.e. update 1 module row. In case it's new and old names are same, row_count value may be 0.
+    vAlreadySameName := EXISTS(
+        SELECT * FROM modules
+        WHERE modules.ID = pModuleID AND modules.Type = 'regular' AND modules.Name = pName
+        LIMIT 1
+    );
+    IF vAlreadySameName THEN
+        return 0;
+    END IF;
+
+    UPDATE modules
+        SET modules.Name = pName
+        WHERE modules.ID = pModuleID AND modules.Type = 'regular';
+        --LIMIT 1;
+        get diagnostics rowUpd = ROW_COUNT;
+    RETURN rowUpd-1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: changesubmoduledescription(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.changesubmoduledescription(integer, integer, character varying);
+
+CREATE OR REPLACE FUNCTION public.changesubmoduledescription(pteacherid integer, psubmoduleid integer, pdescription character varying)
+ RETURNS integer
+AS $$
+    DECLARE vIsLocked INT DEFAULT -1;
+	declare test int default 0;
+BEGIN
+--select public.changesubmoduledescription(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psubmoduleid,	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--	:pdescription 	-- put the pdescription parameter value instead of 'pdescription' (varchar)
+--);
+
+    SELECT disciplines.IsLocked INTO vIsLocked
+        FROM view_roadmap
+        INNER JOIN disciplines ON disciplines.ID = view_roadmap.DisciplineID
+        WHERE   view_roadmap.SubmoduleID = pSubmoduleID AND disciplines.AuthorID = pTeacherID
+        LIMIT 1;
+    IF  vIsLocked != 0 THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE submodules
+        SET submodules.Description  = pDescription
+        WHERE submodules.ID = pSubmoduleID;	    
+        --LIMIT 1;
+    RETURN 0;
+END;
+
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: changesubmodulemaxandcontrol(integer, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.changesubmodulemaxandcontrol(integer, integer, integer, character varying);
+
+CREATE OR REPLACE FUNCTION public.changesubmodulemaxandcontrol(pteacherid integer, psubmoduleid integer, pmaxrate integer, pcontroltype character varying)
+ RETURNS integer
+AS $$
+    DECLARE vChecker INT DEFAULT -1; 
+   			vDisciplineID INT DEFAULT -1; 
+   			vIsLocked INT DEFAULT -1; 
+   			vNewDiscMaxRate INT DEFAULT -1; 
+   			vCurRate INT DEFAULT -1;
+   		    rowUpd INT;
+BEGIN
+--select public.changesubmodulemaxandcontrol(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psubmoduleid,	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--	:pmaxrate,	-- put the pmaxrate parameter value instead of 'pmaxrate' (int4)
+--	:pcontroltype 	-- put the pcontroltype parameter value instead of 'pcontroltype' (varchar)
+--);
+    -- check that discipline and submodule exists and doesn't locked
+    SELECT  disciplines.IsLocked,
+            disciplines.MaxRate - submodules.MaxRate + pMaxRate
+            INTO vIsLocked, vNewDiscMaxRate
+        FROM submodules
+        INNER JOIN modules        ON  submodules.ModuleID = modules.ID
+        INNER JOIN disciplines    ON  disciplines.ID = modules.DisciplineID
+        WHERE submodules.ID = pSubmoduleID AND
+              disciplines.AuthorID = pTeacherID
+        LIMIT 1;
+    IF  vIsLocked != 0 OR
+        vNewDiscMaxRate > 100
+    THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE submodules
+        SET     submodules.MaxRate = pMaxRate,
+                submodules.Type = pControlType
+        WHERE submodules.ID = pSubmoduleID;
+        --LIMIT 1;
+    get diagnostics rowUpd = ROW_COUNT;
+    RETURN rowUpd-1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: changesubmodulename(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.changesubmodulename(integer, integer, character varying);
+
+CREATE OR REPLACE FUNCTION public.changesubmodulename(pteacherid integer, psubmoduleid integer, pname character varying)
+ RETURNS integer
+AS $$
+
+    DECLARE vIsLocked INT DEFAULT -1;
+BEGIN
+--select public.changesubmodulename(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psubmoduleid,	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--	:pname 	-- put the pname parameter value instead of 'pname' (varchar)
+--);
+
+    SELECT disciplines.IsLocked INTO vIsLocked
+        FROM view_roadmap
+        INNER JOIN disciplines ON  disciplines.ID = view_roadmap.DisciplineID
+        WHERE  view_roadmap.SubmoduleID = pSubmoduleID AND disciplines.AuthorID = pTeacherID
+        LIMIT 1;
+    IF  vIsLocked != 0 THEN
+        RETURN -1;
+    END IF;
+
+    UPDATE submodules
+        SET submodules.Name = pName
+        WHERE submodules.ID = pSubmoduleID;
+        --LIMIT 1;
+    RETURN 0;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: createrequest(integer, character varying, text, boolean); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createrequest(integer, character varying, text, boolean);
+
+CREATE OR REPLACE FUNCTION public.createrequest(paccountid integer, ptitle character varying, pdescription text, pimage boolean)
+ RETURNS integer
+AS $$
+declare ROWID integer; hi integer default 0;
+begin
+--	select public.createrequest(
+--	:paccountid,	-- put the paccountid parameter value instead of 'paccountid' (int4)
+--	:ptitle,	-- put the ptitle parameter value instead of 'ptitle' (varchar)
+--	:pdescription,	-- put the pdescription parameter value instead of 'pdescription' (text)
+--	:pimage 	-- put the pimage parameter value instead of 'pimage' (bool)
+--);
+
+    --DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -1;
+    
+    if pImage then hi=1; --несовпадение типов boolean и integer
+    end if;
+    
+    INSERT INTO requests (AccountID, Title, Description, Status, HasImage)
+         --  VALUES  (pAccountID, pTitle, pDescription, 'opened', pImage) 
+         VALUES  (pAccountID, pTitle, pDescription, 'opened', hi) 
+           returning id into rowid;
+    RETURN rowid;
+   exception
+   when others then return -1;
+   
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: deletemodule(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.deletemodule(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.deletemodule(pteacherid integer, pmoduleid integer)
+ RETURNS integer
+AS $$
+    DECLARE vDisciplineID INT DEFAULT -1;
+    curs cursor for select  id from modules   WHERE   modules.DisciplineID = vDisciplineID AND
+                modules.Type = 'regular'
+        ORDER BY modules.OrderNum ASC;
+       counter int;
+BEGIN
+--select public.deletemodule(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pmoduleid 	-- put the pmoduleid parameter value instead of 'pmoduleid' (int4)
+--);
+
+    -- get discipline ID
+        SELECT disciplines.ID FROM modules
+        INNER JOIN disciplines    ON  modules.DisciplineID = disciplines.ID AND disciplines.AuthorID = pTeacherID
+        WHERE modules.ID = pModuleID LIMIT 1
+into vDisciplineID;
+
+    -- check rights
+    if NOT InternalIsTeacherAuthor(pTeacherID, vDisciplineID) OR
+        InternalIsMapLocked(vDisciplineID)
+    THEN
+        RETURN -1;
+    END if;
+
+    DELETE FROM submodules
+        WHERE submodules.ModuleID = pModuleID;
+    DELETE FROM modules
+        WHERE modules.ID = pModuleID;
+
+    -- restore continuous ordering
+    --нужно сделать через курсор
+    set counter = 0;
+   for rec in curs loop
+     counter = counter + 1;
+     update modules set modules.OrderNum  = counter WHERE CURRENT of curs;
+   end loop;
+    /*update modules
+        SET modules.OrderNum = (counter = counter + 1)
+        WHERE   modules.DisciplineID = vDisciplineID AND
+                modules.Type = 'regular';
+    select modules
+        ORDER BY modules.OrderNum ASC;
+*/
+    RETURN 0;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: deletemodulebonus(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.deletemodulebonus(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.deletemodulebonus(pteacherid integer, pdisciplineid integer)
+ RETURNS integer
+AS $$
+    DECLARE vBonusModuleID INT DEFAULT -1;
+begin
+--	select public.deletemodulebonus(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    CASE WHEN  NOT InternalIsTeacherAuthor(pTeacherID, pDisciplineID) OR
+        InternalIsMapLocked(pDisciplineID)
+    THEN
+        RETURN -1;
+	else null;	
+    END CASE;
+
+    CASE WHEN GetBonusModule(pDisciplineID) <= 0 THEN
+        RETURN -1;
+	else null;	
+    END CASE;
+
+    DELETE FROM submodules
+        WHERE vBonusModuleID = submodules.ModuleID;
+
+    DELETE FROM modules
+        WHERE vBonusModuleID = modules.ID;
+        --LIMIT 1;
+
+    RETURN 0;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: deletesubmodule(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.deletesubmodule(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.deletesubmodule(pteacherid integer, psubmoduleid integer)
+ RETURNS integer
+AS $$
+
+    DECLARE vIsLocked INT DEFAULT -1; vModuleID INT DEFAULT -1;
+            counter INT;
+            curs cursor for 
+              select ID from submodules 
+                        where submodules.ModuleID = vModuleID
+             			ORDER BY submodules.OrderNum ASC;
+BEGIN
+--select public.deletesubmodule(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:psubmoduleid 	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--);
+    SELECT modules.ID, disciplines.IsLocked
+        INTO vModuleID, vIsLocked
+        FROM submodules
+        INNER JOIN modules ON modules.ID = submodules.ModuleID
+        INNER JOIN disciplines ON modules.DisciplineID = disciplines.ID
+        WHERE   disciplines.AuthorID = pTeacherID AND
+                submodules.ID = pSubmoduleID
+        LIMIT 1;
+    IF  vIsLocked != 0 THEN
+        RETURN -1;
+    END IF;
+
+    -- handler will catch constraint violation
+    DELETE FROM submodules
+        WHERE submodules.ID = pSubmoduleID;
+        --LIMIT 1;
+
+    -- restore continuous ordering
+    counter = 0;
+     for rec in curs loop
+     counter = counter + 1;
+     update sumodules set submodules.OrderNum  = counter WHERE CURRENT of curs;
+   end loop;
+   /* UPDATE submodules
+        SET submodules.OrderNum = (counter = counter + 1)
+        WHERE   submodules.ModuleID = vModuleID;
+        
+    select submodules
+        ORDER BY submodules.OrderNum ASC;
+*/
+    RETURN 0;
+    exception 
+    when others then return -1;
+END
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: department_getidbyexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_getidbyexternalid(character varying);
+
+CREATE OR REPLACE FUNCTION public.department_getidbyexternalid(pdepartmentexternalid character varying)
+ RETURNS integer
+AS $$
+--select public.department_getidbyexternalid(
+--	:pdepartmentexternalid 	-- put the pdepartmentexternalid parameter value instead of 'pdepartmentexternalid' (varchar)
+--);
+  SELECT departments.ID as ID
+  FROM departments
+  WHERE departments.ExternalID = pDepartmentExternalID; --поле unique
+$$  LANGUAGE sql;
+
+--
+-- Name: department_getidbyname(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_getidbyname(character varying);
+
+CREATE OR REPLACE FUNCTION public.department_getidbyname(pdepartmentname character varying)
+ RETURNS setof integer
+AS $$
+--	select public.department_getidbyname(
+--	:pdepartmentname 	-- put the pdepartmentname parameter value instead of 'pdepartmentname' (varchar)
+--);
+  SELECT departments.ID as ID
+  FROM departments
+  WHERE departments.Name = pDepartmentName;
+$$  LANGUAGE sql;
+
+--
+-- Name: department_search(character varying, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_search(character varying, integer);
+
+CREATE OR REPLACE FUNCTION public.department_search(pname character varying, pfacultyid integer)
+ RETURNS integer
+AS $$
+BEGIN
+--	select public.department_search(
+--	:pname,	-- put the pname parameter value instead of 'pname' (varchar)
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--);
+RETURN COALESCE((
+    SELECT departments.ID FROM departments
+    WHERE   departments.Name = pName AND departments.FacultyID = pFacultyID LIMIT 1
+), -1);
+END
+$$   LANGUAGE plpgsql;
+
+--
+-- Name: discipline_countratings(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_countratings(integer);
+
+CREATE OR REPLACE FUNCTION public.discipline_countratings(pdisciplineid integer)
+ RETURNS bigint
+AS $$
+--select public.discipline_countratings(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    SELECT COUNT(rating_table.recordbookid) FROM view_roadmap
+        LEFT JOIN rating_table ON rating_table.SubmoduleID = view_roadmap.SubmoduleID
+        WHERE view_roadmap.DisciplineID = pDisciplineID
+$$  language sql;
+
+--
+-- Name: getratesfordiscipline(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getratesfordiscipline(integer);
+
+CREATE OR REPLACE FUNCTION public.getratesfordiscipline(pdisciplineid integer)
+ RETURNS 
+ table (
+SubmoduleID integer
+,StudentID integer
+,Rate integer)
+AS $$
+    DECLARE vSemesterID INT DEFAULT -1;
+begin
+--	select * from  public.getratesfordiscipline(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+    vSemesterID := GetDisciplineProperty(pDisciplineID, 'semester');
+
+     return QUERY SELECT  view_roadmap.SubmoduleID,
+            rating_table.recordbookid,
+            rating_table.Rate
+        FROM view_roadmap
+            LEFT JOIN rating_table ON rating_table.SubmoduleID = view_roadmap.SubmoduleID
+            INNER JOIN students_groups ON students_groups.SemesterID = vSemesterID AND
+                                            students_groups.recordbookid = rating_table.recordbookid
+            WHERE view_roadmap.DisciplineID = pDisciplineID;
+            --ORDER BY rating_table.recordbookid;
+     return;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: faculty_getidfromexternalid(varchar); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.faculty_getidfromexternalid(varchar); 
+
+CREATE OR REPLACE FUNCTION public.faculty_getidfromexternalid(pFacultyExternalID character varying)
+ RETURNS integer
+
+AS $$
+        DECLARE pID INT DEFAULT -1;
+begin
+--	select public.faculty_getidfromexternalid(
+--	:pFacultyExternalID	-- put the pFacultyExternalID parameter value instead of 'pFacultyExternalID' (varchar)
+--);
+        SELECT faculties.ID INTO pID
+        FROM faculties
+        WHERE faculties.ExternalID = pFacultyExternalID
+        LIMIT 1; -- скорее всего не требуется
+        RETURN pID;
+    END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: faculty_update(integer,  text, text,  text); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.faculty_update(integer,  text, text,  text);
+
+CREATE OR REPLACE FUNCTION public.faculty_update(pfacultyid integer, pfacultyexternalid text, pfacultyname text, pfacultyabbr text)
+ RETURNS integer
+AS $$ 
+begin
+--	select public.faculty_update(
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:pfacultyexternalid,	-- put the pfacultyexternalid parameter value instead of 'pfacultyexternalid' (text)
+--	:pfacultyname,	-- put the pfacultyname parameter value instead of 'pfacultyname' (text)
+--	:pfacultyabbr 	-- put the pfacultyabbr parameter value instead of 'pfacultyabbr' (text)
+--);
+  IF pFacultyExternalID IS NOT NULL THEN
+    UPDATE faculties SET ExternalID = pFacultyExternalID WHERE ID = pFacultyID;
+  END IF;
+
+  IF pFacultyName IS NOT NULL THEN
+    UPDATE faculties SET Name = pFacultyName WHERE ID = pFacultyID;
+  END IF;
+
+  IF pFacultyAbbr IS NOT NULL THEN
+    UPDATE faculties SET Abbr = pFacultyAbbr WHERE ID = pFacultyID;
+  END IF;
+
+  RETURN pFacultyID;
+  EXCEPTION 
+  when others then 
+    RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+ 
+--
+-- Name: faculty_create(integer,  text, text,  text); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.faculty_create(  text, text,  text);
+
+ CREATE OR REPLACE FUNCTION public.faculty_create(
+  pFacultyExternalID TEXT,
+  pFacultyName TEXT,
+  pFacultyAbbr TEXT
+)
+RETURNS INT
+AS $$ 
+  DECLARE vInnerID integer DEFAULT -1;
+begin
+--	select public.faculty_create(
+--	:pfacultyexternalid,	-- put the pfacultyexternalid parameter value instead of 'pfacultyexternalid' (text)
+--	:pfacultyname,	-- put the pfacultyname parameter value instead of 'pfacultyname' (text)
+--	:pfacultyabbr 	-- put the pfacultyabbr parameter value instead of 'pfacultyabbr' (text)
+--);
+  SELECT faculties.id  into vInnerID
+            FROM faculties
+            WHERE faculties.externalid LIKE pFacultyExternalID OR faculties."name" LIKE pFacultyName
+            LIMIT 1  ;
+
+  IF vInnerID IS NULL THEN
+    INSERT INTO faculties (ExternalID, "name", Abbr) VALUES (pFacultyExternalID, pFacultyName, pFacultyAbbr) 
+       returning id into vInnerID;
+    RETURN vInnnerID;
+  ELSE
+    RETURN (SELECT Faculty_Update(vInnerID, pFacultyExternalID, pFacultyName, pFacultyAbbr));
+  END IF;
+ EXCEPTION 
+ when others then
+ RETURN -1;
+END
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: creategroup(integer, integer, character varying, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.creategroup(integer, integer, character varying, integer, integer);
+
+CREATE OR REPLACE FUNCTION public.creategroup(
+   pgradeid integer, 
+   pgroupnum integer, 
+   pSpecName character varying, 
+   pFacultyID integer, 
+   pyear integer)
+ RETURNS integer
+AS $$
+ DECLARE vGroupID INT DEFAULT null; 
+         vSpecId INT DEFAULT null; 
+         vGroupYear INT DEFAULT null; 
+         vIsSpecMatch INT DEFAULT null;
+   
+BEGIN
+--select public.creategroup(
+--	:pgradeid,	-- put the pgradeid parameter value instead of 'pgradeid' (int4)
+--	:pgroupnum,	-- put the pgroupnum parameter value instead of 'pgroupnum' (int4)
+--	:pspecname,	-- put the pspecname parameter value instead of 'pspecname' (varchar)
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:pyear 	-- put the pyear parameter value instead of 'pyear' (int4)
+--);    
+-- create specialization vSpecId - UNIQUE (pSpecName,pFacultyID)
+    select specializations.id into vSpecId
+           from specializations
+           where specializations.facultyid=pFacultyID and specializations."name" like pSpecName;
+    if (vSpecId is null) then
+         INSERT INTO specializations (Name, Abbr, FacultyID) 
+         VALUES  (pSpecName, NULL, pFacultyID)
+         returning id into vSpecId;
+    end if;
+
+-- create group  vGroupID - UNIQUE (pGradeID, pGroupNum, pFacultyID)
+    select study_groups.id into vGroupID 
+           from study_groups
+           where study_groups.gradeid=pgradeID 
+           		 and study_groups.groupnum= pGroupNum
+           		 and study_groups.facultyid=pFacultyID;
+    if ( vGroupID  is null) then
+        INSERT INTO study_groups (GradeID, GroupNum, FacultyID) 
+               VALUES (pGradeID, pGroupNum, pFacultyID)
+               returning id into  vGroupID ;
+    end if;
+
+    SELECT groups_years.GroupID, groups_years.SpecializationID = vSpecId
+        INTO vGroupYear, vIsSpecMatch
+        FROM groups_years
+        WHERE groups_years.GroupID = vGroupID AND groups_years.Year = pYear
+        LIMIT 1;
+
+    IF  (vGroupYear is null) THEN
+        INSERT INTO groups_years (GroupID, Year, SpecializationID)
+        VALUES (vGroupID, pYear, vSpecId);
+    ELSEIF NOT vIsSpecMatch THEN
+        RETURN -1;
+    END IF;
+
+    RETURN vGroupID;
+   EXCEPTION 
+   		when others then 
+   			RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: department_create_with_externalid(character varying, text, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_create_with_externalid(character varying, text, character varying);
+
+CREATE OR REPLACE FUNCTION public.department_create_with_externalid(
+  pDepartmentExternalID VARCHAR,
+  pDepartmentName TEXT,
+  pFacultyExternalID VARCHAR 
+)
+RETURNS INT
+AS $$ 
+  DECLARE vInnerID INT default null; vFacultyInnerID INT DEFAULT null;
+begin
+--	select public.department_create_with_externalid(
+--	:pdepartmentexternalid,	-- put the pdepartmentexternalid parameter value instead of 'pdepartmentexternalid' (varchar)
+--	:pdepartmentname,	-- put the pdepartmentname parameter value instead of 'pdepartmentname' (text)
+--	:pfacultyexternalid 	-- put the pfacultyexternalid parameter value instead of 'pfacultyexternalid' (varchar)
+--);
+  vFacultyInnerID = Faculty_GetIdByExternalID(pFacultyExternalID);
+  IF vFacultyInnerID IS NULL THEN
+    RETURN -2;
+  END IF;
+
+ SELECT ID into  vInnerID
+           FROM departments
+           WHERE departments.ExternalID LIKE pDepartmentExternalID OR departments.Name LIKE pDepartmentName
+           LIMIT 1;
+
+  IF vInnerID IS NULL THEN
+    INSERT INTO departments (ExternalID, Name, FacultyID) 
+           VALUES (pDepartmentExternalID, pDepartmentName, vFacultyInnerID) returning id into vInnerID;
+    RETURN vInnerID;
+  else
+     SELECT Department_Update(vInnerID, pDepartmentExternalID, pDepartmentName, vFacultyInnerID) into vInnerID;
+    RETURN vInnerID;
+  END IF;
+ EXCEPTION 
+  when others then RETURN -1;
+END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: jobposition_create(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.jobposition_create(character varying);
+
+ CREATE OR REPLACE FUNCTION public.jobposition_create(pjobpositionname character varying)
+ RETURNS integer
+AS $$
+declare vInsertID integer;
+begin
+--	select public.jobposition_create(
+--	:pjobpositionname 	-- put the pjobpositionname parameter value instead of 'pjobpositionname' (varchar)
+--);
+  INSERT INTO job_positions (Name) VALUES (pJobPositionName) returning id into vInsertID;
+  RETURN vInsertID;
+ exception
+  when others then RETURN -1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: recordbook_getidfromexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.recordbook_getidfromexternalid(character varying);
+
+CREATE OR REPLACE FUNCTION public.recordbook_getidfromexternalid(
+   pRecordBookExternalID VARCHAR
+)
+RETURNS INT
+AS $$ 
+DECLARE pID INT DEFAULT null;
+begin
+--	select public.recordbook_getidfromexternalid(
+--	:precordbookexternalid 	-- put the precordbookexternalid parameter value instead of 'precordbookexternalid' (varchar)
+--);
+
+        SELECT record_books.ID INTO pID
+        FROM record_books
+        WHERE record_books.ExternalID = pRecordBookExternalID
+        LIMIT 1;
+		if pID is null then return -1;
+        else  RETURN pID;
+        end if; 
+    END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: signinbytoken(character); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.signinbytoken(character);
+
+ CREATE OR REPLACE FUNCTION public.signinbytoken(ptoken character)
+ RETURNS integer
+
+AS $$
+
+    DECLARE vAccountID INT DEFAULT -1;
+BEGIN
+--select public.signinbytoken(
+--	:ptoken 	-- put the ptoken parameter value instead of 'ptoken' (bpchar)
+--);
+    SELECT auth_tokens.AccountID INTO vAccountID
+        FROM auth_tokens
+        WHERE auth_tokens.Token = pToken;
+        --LIMIT 1;
+
+    IF vAccountID is null THEN
+        RETURN -1; -- token not found
+    END IF;
+
+    UPDATE auth_tokens
+        SET Accessed = CURRENT_TIMESTAMP
+        WHERE auth_tokens.Token = pToken;
+        --LIMIT 1;
+
+    INSERT INTO logs_signin(AccountID) VALUES (vAccountID);
+    RETURN vAccountID;
+END;
+$$  LANGUAGE plpgsql;
+ 
+--
+-- Name: internalisstudentattached(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+
+DROP FUNCTION IF EXISTS public.internalisstudentattached(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.internalisstudentattached(pstudentid integer, pdisciplineid integer)
+ RETURNS boolean
+
+AS $$
+    DECLARE vAttachType attach_detach DEFAULT 'detach';
+BEGIN
+--select public.internalisstudentattached(
+--	:pstudentid,	-- put the pstudentid parameter value instead of 'pstudentid' (int4)
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    SELECT view_disciplines_recordbooks.Type INTO vAttachType
+    FROM view_disciplines_recordbooks
+    WHERE view_disciplines_recordbooks.RecordBookID = pRecordBookID AND
+          view_disciplines_recordbooks.DisciplineID = pDisciplineID
+    LIMIT 1;
+	RETURN 'attach' = COALESCE(vAttachType, 'attach');
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: Discipline_SetExamPeriodOption(integer, integer, absence_pass); Type: FUNCTION; Schema: public; Owner: -
+--
+
+DROP FUNCTION IF EXISTS public.Discipline_SetExamPeriodOption(integer, integer, absence_pass);
+
+CREATE or REPLACE FUNCTION public.Discipline_SetExamPeriodOption(
+    pRecordBookID INT,
+    pSubmoduleID INT,
+    pType absence_pass default null
+) RETURNS int
+AS $$
+ DECLARE vType INT DEFAULT NULL;
+BEGIN
+--select public.discipline_setexamperiodoption(
+--	:precordbookid,	-- put the precordbookid parameter value instead of 'precordbookid' (int4)
+--	:psubmoduleid,	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (absence_pass)
+--);
+    IF pType is not null THEN
+         vType = pType;
+    END IF;
+
+    INSERT INTO exam_period_options (RecordBookID, SubmoduleID, Type) VALUES(pRecordBookID, pSubmoduleID, vType)
+        ON  conflict ON CONSTRAINT  exam_period_options_recordbookid_submoduleid_key
+          do update set  exam_period_options.Type = vType;
+
+    RETURN 0;
+   EXCEPTION 
+  		 when others 
+  			  then RETURN -1;
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: getstudentgroup(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getstudentgroup(integer, integer); 
+
+CREATE OR REPLACE FUNCTION public.getstudentgroup(precordbookID integer, psemesterid integer)
+ RETURNS integer
+AS $$
+begin
+--	select public.GetStudentGroup(
+--	:pRecordBookID,	-- put the pRecordBookID parameter value instead of 'pRecordBookID' (INT)
+--	:pSemesterID 	-- put the pSemesterID parameter value instead of 'pSemesterID' (INT)
+--);
+
+RETURN COALESCE((
+    SELECT students_groups.GroupID FROM students_groups
+        join record_books on record_books.id=students_groups.recordbookid
+        WHERE   record_books.id=precordbookID
+                AND students_groups.SemesterID = pSemesterID
+                AND students_groups.State != 'expulsion'
+        LIMIT 1
+), -1);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: subject_create(integer, text, varchar, varchar); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.subject_create(integer, text, varchar, varchar);
+
+CREATE OR REPLACE FUNCTION public.subject_create(pfacultyid integer, psubjectname text, psubjectabbr character varying, pexternalid character varying)
+ RETURNS integer
+AS $$ 
+    DECLARE vSubjectID INT DEFAULT -1;
+begin
+--	select public.subject_create(
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:psubjectname,	-- put the psubjectname parameter value instead of 'psubjectname' (text)
+--	:psubjectabbr,	-- put the psubjectabbr parameter value instead of 'psubjectabbr' (varchar)
+--	:pexternalid 	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--);
+    IF pSubjectName = '' THEN RETURN -1; END IF;
+-- create/get subject (subject name is unique key)
+    INSERT INTO subjects (Name, Abbr, ExternalID) VALUES (pSubjectName, pSubjectAbbr, pExternalID)
+        ON conflict on constraint subjects_name_key
+        do update set
+          --  subjects.ID = LAST_INSERT_ID(subjects.ID),
+            subjects.Name = pSubjectName,
+            subjects.Abbr = COALESCE(pSubjectAbbr, subjects.Abbr),
+            subjects.ExternalID = COALESCE(pExternalID, subjects.ExternalID)
+           returning id into vSubjectID; -- должно вернуть или новый ID или ID измененной записи
+    BEGIN --handler block
+      
+        INSERT INTO subjects_faculties (SubjectID, FacultyID) VALUES (vSubjectID, pFacultyID)
+            on conflict on constraint subjects_faculties_subjectid_facultyid_key --just stub
+               do nothing; 
+       EXCEPTION 
+       when others then RETURN -1;        
+    END;
+    RETURN vSubjectID;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: sha1(varchar); Type: FUNCTION; Schema: public; Owner: -
+-- для шифровки пароля
+--
+DROP FUNCTION IF EXISTS public.sha1(varchar);
+
+CREATE OR REPLACE FUNCTION public.sha1(
+password varchar)
+RETURNS varchar
+AS $$ 
+ SELECT ENCODE(DIGEST(password,'sha1'),'hex');
+$$ LANGUAGE sql;
+
+--
+-- Name: signin(character varying, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.signin(character varying, character varying);
+
+CREATE OR REPLACE FUNCTION public.signin(ploginormail character varying, ppassword character varying)
+ RETURNS integer
+
+AS $$
+    DECLARE vAccountID INT DEFAULT -1;
+BEGIN
+--select public.signin(
+--	:ploginormail,	-- put the ploginormail parameter value instead of 'ploginormail' (varchar)
+--	:ppassword 	-- put the ppassword parameter value instead of 'ppassword' (varchar)
+--);
+    --check account existence
+    SELECT accounts.ID INTO vAccountID FROM accounts
+        WHERE   accounts.Password = SHA1(pPassword) AND
+                (accounts.Login = pLoginOrMail OR accounts.EMail = pLoginOrMail)
+        LIMIT 1;
+    IF vAccountID is null THEN
+        RETURN -1;
+    END IF;
+
+    INSERT INTO logs_signin (AccountID) VALUES (vAccountID);
+    RETURN vAccountID;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: subject_getbyexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.subject_getbyexternalid(character varying);
+
+CREATE OR REPLACE FUNCTION public.subject_getbyexternalid(psubjectexternalid character varying)
+ RETURNS integer
+
+AS $$
+    DECLARE vID INT DEFAULT -1;
+begin
+--	select public.subject_getbyexternalid(
+--	:psubjectexternalid 	-- put the psubjectexternalid parameter value instead of 'psubjectexternalid' (varchar)
+--);
+    SELECT subjects.ID INTO vID
+    FROM subjects
+    WHERE subjects.ExternalID = pSubjectExternalID
+    LIMIT 1;
+  if vID is null then return -1;
+    else  RETURN vID;
+   end if;
+END
+ $$  LANGUAGE plpgsql;
+ 
+ --
+-- Name: account_createactivated(character varying,character varying,character varying,character varying,character varying,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.account_createactivated(character varying,character varying,character varying,character varying,character varying,integer);
+ 
+ CREATE OR REPLACE FUNCTION public.account_createactivated(
+plastname character varying, 
+pfirstname character varying, 
+psecondname character varying, 
+pexternalid character varying,
+pINILA character varying,
+puserroleid integer)
+ RETURNS integer
+AS $$
+declare vID int;
+begin
+--	select public.account_createactivated(
+--	:plastname,	-- put the plastname parameter value instead of 'plastname' (varchar)
+--	:pfirstname,	-- put the pfirstname parameter value instead of 'pfirstname' (varchar)
+--	:psecondname,	-- put the psecondname parameter value instead of 'psecondname' (varchar)
+--	:pexternalid,	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--	:pinila,	-- put the pinila parameter value instead of 'pinila' (varchar)
+--	:puserroleid 	-- put the puserroleid parameter value instead of 'puserroleid' (int4)
+--);
+
+        INSERT INTO accounts (ExternalID, INILA, Login , Password , EMail, LastName, FirstName, SecondName, UserRoleID)
+        VALUES  ( pExternalID,pINILA,NULL, NULL, NULL, pLastName, pFirstName, pSecondName, pUserRoleID) returning id into vID;
+        RETURN vID;
+END;
+$$  LANGUAGE plpgsql;
+ 
+--
+-- Name: account_getidfrominila(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.account_getidfrominila(character varying);
+
+CREATE OR REPLACE FUNCTION public.account_getidfrominila(
+ pAccountINILA VARCHAR
+)
+RETURNS INT
+
+AS $$ 
+  DECLARE pID INT DEFAULT -1;
+begin
+--	select public.account_getidfrominila(
+--	:paccountinila 	-- put the paccountinila parameter value instead of 'paccountinila' (varchar)
+--);
+        SELECT accounts.ID INTO pID
+        FROM accounts
+        WHERE accounts.INILA = pAccountINILA
+        LIMIT 1;
+         if pID is null then return -1;
+          else RETURN pID;
+         end if;
+    END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: account_getidfromexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.account_getidfromexternalid(character varying);
+ 
+ CREATE OR REPLACE FUNCTION public.account_getidfromexternalid(
+ pAccountExternalID VARCHAR
+)
+RETURNS INT
+AS $$
+        DECLARE pID INT DEFAULT null;
+begin
+--	select public.account_getidfromexternalid(
+--	:paccountexternalid 	-- put the paccountexternalid parameter value instead of 'paccountexternalid' (varchar)
+--);
+        SELECT accounts.ID INTO pID
+        FROM accounts
+        WHERE accounts.ExternalID = pAccountExternalID
+        LIMIT 1;
+        if pID isnull then return -1;
+          else  RETURN pID;
+         
+         end if;
+    END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: logbind(integer,integer,integer,attach_detach, group_student); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.logbind(integer,integer,integer,attach_detach, group_student);
+ 
+ CREATE OR REPLACE FUNCTION public.logbind(pdisciplineid integer, pteacherid integer, entityid integer, pattach attach_detach, ptype group_student)
+ RETURNS integer
+AS $$
+ declare countR int default 0;
+BEGIN 
+--	select public.logbind(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:entityid,	-- put the entityid parameter value instead of 'entityid' (int4)
+--	:pattach,	-- put the pattach parameter value instead of 'pattach' (attach_detach)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (group_student)
+--);
+
+    IF pType = 'group' THEN
+        INSERT INTO logs_binds_groups
+            (DisciplineID, TeacherID, GroupID, Type)
+            VALUES (pDisciplineID, pTeacherID, EntityID, pAttach);
+    ELSEIF pType = 'student' THEN
+        INSERT INTO logs_binds_students
+            (DisciplineID, TeacherID, StudentID, Type)
+            VALUES (pDisciplineID, pTeacherID, EntityID, pAttach);
+    END IF;
+    get diagnostics countR = ROW_COUNT;
+    RETURN countR-1;
+   EXCEPTION 
+    when others then RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: bindgroup(integer,integer,integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.bindgroup(integer,integer,integer,integer);
+
+CREATE OR REPLACE FUNCTION public.bindgroup(pteacherid integer, pdisciplineid integer, pgroupid integer, vres integer)
+ RETURNS integer
+
+AS $$
+declare vres int;
+begin
+--	select public.bindgroup(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pgroupid,	-- put the pgroupid parameter value instead of 'pgroupid' (int4)
+--	:vres 	-- put the vres parameter value instead of 'vres' (int4)
+--);
+
+    -- todo: move to php layer
+   IF (pTeacherID IS NOT NULL AND NOT InternalIsTeacherBound(pTeacherID, pDisciplineID))
+       OR InternalIsMapLocked(pDisciplineID) THEN
+        RETURN -1;
+    END IF;
+    -- already bound
+   IF EXISTS(
+        SELECT * FROM disciplines_groups
+        WHERE disciplines_groups.DisciplineID = pDisciplineID AND disciplines_groups.GroupID = pGroupID
+    ) THEN
+        RETURN 1;
+    END IF;
+
+    -- bind whole group
+    INSERT INTO disciplines_groups (DisciplineID, GroupID) SELECT ( pDisciplineID, pGroupID );
+    SELECT LogBind(pDisciplineID, pTeacherID, pGroupID, 'attach', 'group') INTO vRes;
+    RETURN RemoveStudentsAttach(pDisciplineID, pGroupID); -- delete students of this group which were bound to discipline before
+    exception
+    when others then RETURN -3;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: controlstudentgroup(integer,integer,common_outlet_expulsion_leave,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.controlstudentgroup(integer,integer,common_outlet_expulsion_leave,integer);
+
+CREATE OR REPLACE FUNCTION public.controlstudentgroup(precordbookid integer, pgroupid integer, pstate common_outlet_expulsion_leave, psemesterid integer)
+ RETURNS integer
+
+AS $$ 
+DECLARE vChecker INT DEFAULT 0;
+begin
+--	select public.controlstudentgroup(
+--	:precordbookid,	-- put the precordbookid parameter value instead of 'precordbookid' (int4)
+--	:pgroupid,	-- put the pgroupid parameter value instead of 'pgroupid' (int4)
+--	:pstate,	-- put the pstate parameter value instead of 'pstate' (common_outlet_expulsion_leave)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+    INSERT INTO students_groups (RecordBookID, GroupID, SemesterID, State)
+        VALUES (pRecordBookID, pGroupID, pSemesterID, pState)
+        ON conflict on CONSTRAINT students_groups_semesterid_recordbookid_key
+          do update set
+            students_groups.GroupID = pGroupID,
+            students_groups.State = pState,
+            students_groups.Date = current_date;
+    get diagnostics vChecker=ROW_COUNT;
+    RETURN vchecker-1;
+   EXCEPTION 
+    when others then RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: departments_get(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+ DROP FUNCTION IF EXISTS public.departments_get(integer);
+ 
+ CREATE OR REPLACE FUNCTION public.departments_get(pfacultyid integer)
+ RETURNS SETOF departments
+AS $$ 
+begin
+--	select public.departments_get(
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--);
+  if (pFacultyID is null) or (pFacultyID = 0) THEN
+    SELECT departments.* FROM departments
+    ORDER BY departments.Name ASC;
+  ELSE
+    SELECT  departments.* -- ID, Name, FacultyID
+    FROM departments
+    WHERE departments.FacultyID = pFacultyID
+    ORDER BY departments.Name ASC;
+  END if;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_bindteacher(integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_bindteacher(integer,integer);
+ 
+CREATE OR REPLACE FUNCTION public.discipline_bindteacher(pdisciplineid integer, pbindingteacherid integer)
+ RETURNS integer
+AS $$ 
+BEGIN
+--select public.discipline_bindteacher(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pbindingteacherid 	-- put the pbindingteacherid parameter value instead of 'pbindingteacherid' (int4)
+--);
+
+--try to insert BindingTeacher in access list
+    INSERT INTO disciplines_teachers (DisciplineID, TeacherID) VALUES (pDisciplineID, pBindingTeacherID)
+     on conflict on constraint disciplines_teachers_disciplineid_teacherid_key --just stub
+               do nothing; 
+    RETURN 0;
+   EXCEPTION 
+    when others then RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_create(integer, integer, integer, exam_credit_grading_credit, integer, integer, integer, integer, integer, scientific_disciplinary_coursework); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_create(integer, integer, integer, exam_credit_grading_credit, integer, integer, integer, integer, integer, scientific_disciplinary_coursework);
+
+CREATE OR REPLACE FUNCTION public.discipline_create(pteacherid integer, pgradeid integer, psubjectid integer, pexamtype exam_credit_grading_credit, plecturecount integer, ppracticecount integer, plabcount integer, pfacultyid integer, psemesterid integer, psubtype scientific_disciplinary_coursework)
+ RETURNS integer
+
+AS $$
+    DECLARE vDisciplineID INT DEFAULT -1;
+    		vTemp int;
+begin
+--	select public.Discipline_Create(
+--	:pTeacherID,	-- put the pTeacherID parameter value instead of 'pTeacherID' (INT)
+--	:pGradeID,	-- put the pGradeID parameter value instead of 'pGradeID' (INT)
+--	:pSubjectID,	-- put the pSubjectID parameter value instead of 'pSubjectID' (INT)
+--	:pExamType,	-- put the pExamType parameter value instead of 'pExamType' (ENUM(15))
+--	:pLectureCount,	-- put the pLectureCount parameter value instead of 'pLectureCount' (INT)
+--	:pPracticeCount,	-- put the pPracticeCount parameter value instead of 'pPracticeCount' (INT)
+--	:pLabCount,	-- put the pLabCount parameter value instead of 'pLabCount' (INT)
+--	:pFacultyID,	-- put the pFacultyID parameter value instead of 'pFacultyID' (INT)
+--	:pSemesterID,	-- put the pSemesterID parameter value instead of 'pSemesterID' (INT)
+--	:pSubtype 	-- put the pSubtype parameter value instead of 'pSubtype' (ENUM(24))
+--);
+
+-- todo: make more flexible creation of coursework
+-- I mean, while creating scientific coursework
+--    we don't have the SubjectID field, but we must.
+--    346 is used as a default id for scientific courseworks.
+--    This constant is duplicated in Model_Helper_CourseWorkBuilder
+
+    IF pSubtype IS NOT NULL THEN
+        pSubjectID := 346;
+    END IF;
+    -- create discipline
+    INSERT INTO disciplines
+        ( AuthorID, GradeID, SubjectID, ExamType, LectureCount, PracticeCount,
+          LabCount, SemesterID, FacultyID, Subtype) 
+          VALUES
+        (pTeacherID, pGradeID, pSubjectID, pExamType, pLectureCount, pPracticeCount,
+         pLabCount, pSemesterID, pFacultyID, pSubtype)
+        returning ID into  vDisciplineID;
+    
+    SELECT Discipline_BindTeacher(vDisciplineID, pTeacherID) INTO vTemp;
+    -- add exam and extra modules
+    IF pExamType = 'exam' THEN
+        SELECT AddModuleExamUnsafe(vDisciplineID) INTO vTemp;
+    END IF;
+    SELECT AddModuleExtra(pTeacherID, vDisciplineID) INTO vTemp;
+
+    RETURN vDisciplineID;
+   EXCEPTION 
+   when others then RETURN -1;
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_delegate(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_delegate(integer, integer, integer);
+
+CREATE OR REPLACE FUNCTION public.discipline_delegate(pdisciplineid integer, pnewauthorid integer, vtemp integer)
+ RETURNS integer
+
+AS $$
+ declare vtemp int;
+BEGIN  
+--	select public.discipline_delegate(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pnewauthorid,	-- put the pnewauthorid parameter value instead of 'pnewauthorid' (int4)
+--	:vtemp 	-- put the vtemp parameter value instead of 'vtemp' (int4)
+--);
+
+    SELECT Discipline_BindTeacher(pDisciplineID, pNewAuthorID) INTO vTemp;
+    UPDATE disciplines
+        SET disciplines.AuthorID = pNewAuthorID
+        WHERE disciplines.ID = pDisciplineID;
+    get diagnostics vtemp = ROW_COUNT;
+    RETURN vtemp-1;
+   EXCEPTION 
+   when others then RETURN -1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: createaccount(character varying, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createaccount(character varying, integer);
+
+CREATE OR REPLACE FUNCTION public.createaccount(pcode character varying, puserroleid integer)
+ RETURNS integer
+
+AS $$
+declare insID int;
+begin
+--	select public.createaccount(
+--	:pcode,	-- put the pcode parameter value instead of 'pcode' (varchar)
+--	:puserroleid 	-- put the puserroleid parameter value instead of 'puserroleid' (int4)
+--);
+
+    INSERT INTO accounts (Login , Password , EMail, UserRoleID, ActivationCode)
+        VALUES  ( NULL, NULL, NULL, pUserRoleID, pCode) returning ID into insID;
+    RETURN insID;
+END;
+$$  LANGUAGE plpgsql;
+
+
+--
+-- Name: createoraddstudent(character varying,  character varying,  character varying,  character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createoraddstudent(character varying,  character varying,  character varying,  character varying);
+
+CREATE OR REPLACE FUNCTION public.createoraddstudent(pexternalid character varying, plastname character varying, pfirstname character varying, psecondname character varying)
+ RETURNS integer
+AS $$ 
+        DECLARE vAccountID INT DEFAULT -1; 
+                vStudentID INT DEFAULT -1;
+begin
+--	select public.createoraddstudent(
+--	:pexternalid,	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--	:plastname,	-- put the plastname parameter value instead of 'plastname' (varchar)
+--	:pfirstname,	-- put the pfirstname parameter value instead of 'pfirstname' (varchar)
+--	:psecondname 	-- put the psecondname parameter value instead of 'psecondname' (varchar)
+--);
+        vAccountID = CreateAccount(pLastName, pFirstName, pSecondName, null, GetUserRole('student'));
+
+        IF pExternalID is not null THEN
+            UPDATE accounts
+                SET ExternalID = pExternalID
+                WHERE ID = vAccountID;
+        END IF;
+
+       --create student
+        INSERT INTO students (AccountID)
+        VALUES  (vAccountID) returning ID into vStudentID;
+        RETURN vStudentID;
+       EXCEPTION 
+        when others then RETURN -1;
+    END
+ $$  LANGUAGE plpgsql;
+
+--
+-- Name: teacher_createactivated(character varying, character varying, character varying, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.teacher_createactivated(character varying, character varying, character varying, integer, integer, character varying);
+
+ CREATE OR REPLACE FUNCTION public.teacher_createactivated(
+   plastname character varying,
+   pfirstname character varying, 
+   psecondname character varying, 
+   pjobpositionid integer, 
+   pFacultyID  integer,
+   pdepartmentid integer, 
+   pexternalid character varying,
+   pINILA  VARCHAR)
+ RETURNS integer
+
+AS $$
+        DECLARE vAccountID INT DEFAULT -1;
+       			insID int;
+BEGIN
+--select public.teacher_createactivated(
+--	:plastname,	-- put the plastname parameter value instead of 'plastname' (varchar)
+--	:pfirstname,	-- put the pfirstname parameter value instead of 'pfirstname' (varchar)
+--	:psecondname,	-- put the psecondname parameter value instead of 'psecondname' (varchar)
+--	:pjobpositionid,	-- put the pjobpositionid parameter value instead of 'pjobpositionid' (int4)
+--	:pdepartmentid,	-- put the pdepartmentid parameter value instead of 'pdepartmentid' (int4)
+--	:pexternalid 	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--);
+        vAccountID = Account_CreateActivated(pLastName, pFirstName, pSecondName, pExternalID, pINILA, GetUserRole('teacher'));
+
+        -- add new teacher
+               INSERT INTO teachers (AccountID, JobPositionID, DepartmentID, FacultyID) VALUES
+            (vAccountID, pJobPositionID, pDepartmentID, pFacultyID) returning ID into insID;
+        RETURN insID;
+       EXCEPTION 
+        when others then RETURN -1;
+    END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: teacher_getidfrominila(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.teacher_getidfrominila(character varying);
+
+CREATE OR REPLACE FUNCTION public.teacher_getidfrominila(
+pTeacherINILA VARCHAR
+)
+RETURNS INT
+
+AS $$ 
+        DECLARE pID INT DEFAULT null;
+begin
+--	select public.Teacher_GetIDFromINILA(
+--	:pTeacherINILA 	-- put the pTeacherINILA parameter value instead of 'pTeacherINILA' (VARCHAR(40))
+--);
+        SELECT teachers.ID INTO pID
+        FROM teachers
+            INNER JOIN accounts ON teachers.AccountID = accounts.ID
+        WHERE accounts.INILA = pTeacherINILA
+        LIMIT 1;
+        if pID is null then pID = -1;
+        end if;
+        RETURN pID;
+    END
+ $$ LANGUAGE plpgsql;
+
+--
+-- Name: student_changeinfo integer,character varying,character varying,character varying,character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_changeinfo (integer,character varying,character varying,character varying,character varying);
+ 
+ CREATE OR REPLACE FUNCTION public.student_changeinfo(pid integer, pexternalid character varying, plastname character varying, pfirstname character varying, psecondname character varying)
+ RETURNS integer
+
+AS $$ 
+DECLARE vAccountID INT DEFAULT -1;
+		vStudentID INT DEFAULT -1;
+begin
+--	select public.student_changeinfo(
+--	:pid,	-- put the pid parameter value instead of 'pid' (int4)
+--	:pexternalid,	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--	:plastname,	-- put the plastname parameter value instead of 'plastname' (varchar)
+--	:pfirstname,	-- put the pfirstname parameter value instead of 'pfirstname' (varchar)
+--	:psecondname 	-- put the psecondname parameter value instead of 'psecondname' (varchar)
+--);
+        SELECT students.AccountID INTO vAccountID
+            FROM students
+            WHERE students.ID = pID;
+
+        UPDATE accounts
+            SET accounts.ExternalID = COALESCE(pExternalID, accounts.ExternalID),
+                accounts.LastName = COALESCE(pLastName, accounts.LastName),
+                accounts.FirstName = COALESCE(pFirstName, accounts.FirstName),
+                accounts.SecondName = COALESCE(pSecondName, accounts.SecondName)
+            WHERE accounts.ID = vAccountID;
+
+        RETURN 0;
+      EXCEPTION 
+      when others then RETURN -1; 
+    END
+ $$  LANGUAGE plpgsql;
+ 
+ --
+-- Name: createstudentex(character varying,character varying,character varying,integer,integer, bachelor_master_specialist, character varying,integer, character varying,integer);Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createstudentex(character varying,character varying,character varying,integer,integer, bachelor_master_specialist, character varying,integer, character varying,integer);
+
+ CREATE OR REPLACE FUNCTION public.createstudentex(
+  plastname character varying, 
+  pfirstname character varying, 
+  psecondname character varying, 
+  pgradenum integer, 
+  pgroupnum integer, 
+  pdegree bachelor_master_specialist, 
+  pspecname character varying, 
+  pfacultyid integer, 
+  pactivationcode character varying, 
+  psemesterid integer)
+ RETURNS integer
+
+AS $$
+
+DECLARE  vGradeID INT DEFAULT -1;  vGroupID INT DEFAULT -1; vYear INT DEFAULT -1;
+begin
+--	select public.createstudentex(
+--	:plastname,	-- put the plastname parameter value instead of 'plastname' (varchar)
+--	:pfirstname,	-- put the pfirstname parameter value instead of 'pfirstname' (varchar)
+--	:psecondname,	-- put the psecondname parameter value instead of 'psecondname' (varchar)
+--	:pgradenum,	-- put the pgradenum parameter value instead of 'pgradenum' (int4)
+--	:pgroupnum,	-- put the pgroupnum parameter value instead of 'pgroupnum' (int4)
+--	:pdegree,	-- put the pdegree parameter value instead of 'pdegree' (bachelor_master_specialist)
+--	:pspecname,	-- put the pspecname parameter value instead of 'pspecname' (varchar)
+--	:pfacultyid,	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--	:pactivationcode,	-- put the pactivationcode parameter value instead of 'pactivationcode' (varchar)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+    vYear := COALESCE((SELECT Year FROM semesters WHERE semesters.ID = pSemesterID LIMIT 1), -1);
+    vGradeID := CreateGrade(pGradeNum, pDegree);
+    vGroupID := CreateGroup(vGradeID, pGroupNum, pSpecName, pFacultyID, vYear);
+    RETURN CreateStudent(pLastName, pFirstName, pSecondName, vGroupID, pActivationCode, pSemesterID);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: student_getidfromexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getidfromexternalid(character varying); 
+
+CREATE OR REPLACE FUNCTION public.student_getidfromexternalid(paccountexternalid character varying)
+ RETURNS integer
+
+AS $$ 
+ DECLARE pID INT DEFAULT null;
+BEGIN   
+--	select public.student_getidfromexternalid(
+--	:paccountexternalid 	-- put the paccountexternalid parameter value instead of 'paccountexternalid' (varchar)
+--);
+        SELECT students.ID INTO pID
+        FROM accounts
+        JOIN students ON accounts.ID = students.AccountID
+        WHERE accounts.ExternalID = pAccountExternalID
+        LIMIT 1;
+        RETURN coalesce(pID, -1);
+    END
+ $$  LANGUAGE plpgsql;
+ 
+--
+-- Name: removefromgroupinsemester(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.removefromgroupinsemester(integer, integer, integer); 
+
+ CREATE OR REPLACE FUNCTION public.removefromgroupinsemester(
+pRecordBook integer, 
+pgroupid integer, 
+psemesterid integer)
+ RETURNS integer
+
+AS $$
+declare vRowCount int;
+BEGIN
+-- select public.RemoveFromGroupInSemester(
+--	:pRecordBook,	-- put the pRecordBook parameter value instead of 'pRecordBook' (INT)
+--	:pGroupID,	-- put the pGroupID parameter value instead of 'pGroupID' (INT)
+--	:pSemesterID 	-- put the pSemesterID parameter value instead of 'pSemesterID' (INT)
+--);   
+    DELETE FROM students_groups
+        WHERE   students_groups.GroupID = pGroupID
+                and students_groups.pRecordBook = pRecordBook
+                and students_groups.SemesterID = pSemesterID
+                and students_groups.State = 'common';
+    get diagnostics vRowCount = ROW_COUNT;
+   RETURN vRowCount-1;
+   EXCEPTION 
+   when others then RETURN -1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: plan_binddiscipline(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_binddiscipline(integer, integer);
+
+CREATE OR REPLACE FUNCTION public.plan_binddiscipline(
+    pStudyPlanID INT,
+    pDisciplineID  INT
+)
+RETURNS INT
+AS $$ 
+BEGIN
+--    select public.Plan_BindDiscipline(
+--	:pStudyPlanID,	-- put the pStudyPlanID parameter value instead of 'pStudyPlanID' (INT)
+--	:pDisciplineID 	-- put the pDisciplineID parameter value instead of 'pDisciplineID' (INT)
+--);   
+        INSERT INTO disciplines_study_plans (StudyPlanID, DisciplineID) VALUES (pStudyPlanID, pDisciplineID)
+         on conflict on constraint disciplines_study_plans_studyplanid_disciplineid_key
+         do nothing;
+        RETURN 0;
+       EXCEPTION 
+       when others then RETURN -1;
+    END
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: plan_changeinfo(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_changeinfo(integer, integer, integer);
+
+CREATE OR REPLACE FUNCTION public.plan_changeinfo(
+    pID INT,
+    pYear INT,
+    pFacultyID INT
+)
+RETURNS INT
+
+AS $$ 
+begin
+--	select public.Plan_ChangeInfo(
+--	:pID,	-- put the pID parameter value instead of 'pID' (INT)
+--	:pYear,	-- put the pYear parameter value instead of 'pYear' (INT)
+--	:pFacultyID 	-- put the pFacultyID parameter value instead of 'pFacultyID' (INT)
+--);
+    UPDATE study_plans
+        SET study_plans.Year = COALESCE(pYear, study_plans.Year),
+            study_plans.FacultyID = COALESCE(pFacultyID, study_plans.FacultyID)
+        WHERE study_plans.ID = pID;
+    RETURN 0;
+   EXCEPTION 
+    when others then RETURN -1;
+END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: plan_create(VARCHAR, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_create(VARCHAR, integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.plan_create(
+    pPlanExternalID VARCHAR,
+    pYear INT,
+    pFacultyID INT
+)
+RETURNS INT
+
+AS $$ 
+        DECLARE vID INT DEFAULT -1;
+begin
+--	select public.Plan_Create(
+--	:pPlanExternalID,	-- put the pPlanExternalID parameter value instead of 'pPlanExternalID' (VARCHAR(9))
+--	:pYear,	-- put the pYear parameter value instead of 'pYear' (INT)
+--	:pFacultyID 	-- put the pFacultyID parameter value instead of 'pFacultyID' (INT)
+--);
+        INSERT INTO study_plans (ExternalID, Year, FacultyID) VALUES (pPlanExternalID, pYear, pFacultyID)
+         on conflict on constraint study_plans_externalid_key
+         do UPDATE set study_plans.Year = pYear, study_plans.FacultyID = pFacultyID
+            returning id into vID;
+        
+        RETURN vID;
+    END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: plan_getdisciplines(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_getdisciplines(integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.plan_getdisciplines(pstudyplanid integer, psemesterid integer)
+ RETURNS SETOF integer
+
+AS $$ 
+--select public.plan_getdisciplines(
+--	:pstudyplanid,	-- put the pstudyplanid parameter value instead of 'pstudyplanid' (int4)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+
+SELECT disciplines.ID AS ID
+      FROM disciplines
+      INNER JOIN disciplines_study_plans ON disciplines_study_plans.DisciplineID = disciplines.ID
+    WHERE disciplines_study_plans.StudyPlanID = pStudyPlanID AND
+          disciplines.SemesterID = COALESCE(pSemesterID, disciplines.SemesterID); --???????
+ $$  LANGUAGE sql;
+
+--
+-- Name: plan_getidfromexternalid(VARCHAR); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_getidfromexternalid(VARCHAR);
+
+ CREATE OR REPLACE FUNCTION public.plan_getidfromexternalid(pplanexternalid character varying)
+ RETURNS integer
+
+AS $$ 
+        DECLARE vID INT DEFAULT null;
+begin
+--	select public.plan_getidfromexternalid(
+--	:pplanexternalid 	-- put the pplanexternalid parameter value instead of 'pplanexternalid' (varchar)
+--);
+        SELECT study_plans.ID INTO vID
+        FROM study_plans
+        WHERE study_plans.ExternalID = pPlanExternalID
+        LIMIT 1;
+
+        RETURN coalesce(vID,-1);
+    END
+ $$  LANGUAGE plpgsql;
+ 
+--
+-- Name: plan_getinfo(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.plan_getinfo(integer);
+
+ CREATE OR REPLACE FUNCTION public.plan_getinfo(pid integer)
+ RETURNS SETOF study_plans
+
+AS $$ 
+--select public.plan_getinfo(
+--	:pid 	-- put the pid parameter value instead of 'pid' (int4)
+--);
+
+SELECT study_plans.*
+    FROM study_plans
+    WHERE study_plans.ID = pID
+    LIMIT 1;
+ $$  LANGUAGE sql;
+
+--
+-- Name:recordbook_bind(VARCHAR, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.recordbook_bind(VARCHAR, integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.recordbook_bind(pexternalid character varying, pstudentid integer, pplanid integer)
+ RETURNS integer
+
+AS $$ 
+        DECLARE vRecordBookID INT DEFAULT -1;
+BEGIN
+--select public.recordbook_bind(
+--	:pexternalid,	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--	:pstudentid,	-- put the pstudentid parameter value instead of 'pstudentid' (int4)
+--	:pplanid 	-- put the pplanid parameter value instead of 'pplanid' (int4)
+--);
+        INSERT INTO record_books (StudentID, ExternalID)
+            VALUES  (pStudentID, pExternalID)
+           on conflict on constraint record_books_externalid_key do
+           update set StudentID = pStudentID,
+                      record_books.ID = LAST_INSERT_ID(record_books.ID)
+                     returning ID into vRecordBookID;
+                    
+        INSERT INTO record_books_plans (RecordBookID, StudyPlanID)
+            VALUES (vRecordBookID, pPlanID)
+            on conflict on constraint recordbookid_studyplanid_record_books_plans_key do
+            nothing;
+
+        RETURN vRecordBookID;
+       
+       EXCEPTION 
+       when others then RETURN -1;
+    END
+ $$  LANGUAGE plpgsql;
+
+ --
+-- Name:recordbook_changeinfo(integer, VARCHAR, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.recordbook_changeinfo(integer, VARCHAR, integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.recordbook_changeinfo(
+    pID INT,
+    pExternalID VARCHAR,
+    pStudentID  int,
+    pPlanID     int
+)
+RETURNS INT
+
+AS $$ 
+ declare vRes int;
+begin
+--	select public.recordbook_changeinfo(
+--	:pid,	-- put the pid parameter value instead of 'pid' (int4)
+--	:pexternalid,	-- put the pexternalid parameter value instead of 'pexternalid' (varchar)
+--	:pstudentid,	-- put the pstudentid parameter value instead of 'pstudentid' (int4)
+--	:pplanid 	-- put the pplanid parameter value instead of 'pplanid' (int4)
+--);
+
+
+        UPDATE record_books
+            SET record_books.ExternalID = COALESCE(pExternalID, record_books.ExternalID)
+            WHERE record_books.ID = pID;
+        SELECT RecordBook_Bind(pExternalID, pStudentID, pPlanID) INTO vRes;
+        RETURN 0;
+       EXCEPTION 
+       when others then RETURN -1;
+    END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: recordbook_getinfo(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.recordbook_getinfo(integer);
+
+ CREATE OR REPLACE FUNCTION public.recordbook_getinfo(precordbook integer)
+ RETURNS TABLE(id integer, externalid character varying, studentid integer)
+
+AS $$ 
+--select * from public.recordbook_getinfo(
+--	:precordbook 	-- put the precordbook parameter value instead of 'precordbook' (int4)
+--);
+
+SELECT
+      record_books.ID,
+      record_books.ExternalID,
+      record_books.StudentID
+    FROM record_books
+    WHERE record_books.ID = pRecordBook;
+ $$  LANGUAGE sql;
+
+--
+-- Name: gettextmark(character,character varying,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.gettextmark(character,character varying,integer);
+
+ CREATE OR REPLACE FUNCTION public.gettextmark(isafterexam character, examtype character varying, rate integer)
+ RETURNS character varying
+
+AS $$
+
+	declare markTextId int;
+    markText varchar(100);
+begin
+--	select public.gettextmark(
+--	:isafterexam,	-- put the isafterexam parameter value instead of 'isafterexam' (bpchar)
+--	:examtype,	-- put the examtype parameter value instead of 'examtype' (varchar)
+--	:rate 	-- put the rate parameter value instead of 'rate' (int4)
+--);
+    if examType = 'credit' then
+        if rate < 60 then
+			markTextId := 6;--незачет
+		else 
+			markTextId := 7;--зачет
+		end if;
+    elseif examType = 'gradingcredit' then
+		if rate >= 85 then 
+			markTextId := 5;--неуд.
+		elseif rate >= 70 then 
+			markTextId := 4;--хорошо
+		elseif rate >= 60 then
+			markTextId := 3;--СѓРґРѕРІР».
+		else
+			markTextId := 2;--неуд.
+		end if;
+    elseif isAfterExam = '0' then
+		if rate < 38 then
+			markTextId := 1;--недопуск
+		else
+			return ' ' ;
+		end if;
+	else
+		if rate >= 85 then 
+			markTextId := 5;--неуд.
+		elseif rate >= 70 then 
+			markTextId := 4;--хорошо
+		elseif rate >= 60 then
+			markTextId := 3;--СѓРґРѕРІР».
+		else
+			markTextId := 2;--неуд.
+		end if;
+	end if;
+    select name from text_mark where id = markTextId into markText;
+    return markText;
+END;
+$$  LANGUAGE plpgsql;
+
+
+--
+-- Name: checkaccountexistence(text, login_email_code); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.checkaccountexistence(text, login_email_code);
+
+CREATE OR REPLACE FUNCTION public.checkaccountexistence(pdata text, ptype login_email_code)
+ RETURNS boolean
+AS $$
+begin
+--	select public.checkaccountexistence(
+--	:pdata,	-- put the pdata parameter value instead of 'pdata' (text)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (login_email_code)
+--);
+
+RETURN EXISTS(
+    SELECT * FROM accounts
+        WHERE CASE pType
+            WHEN 'login' THEN pData = accounts.Login
+            WHEN 'email' THEN pData = accounts.EMail
+            WHEN 'code' THEN pData = accounts.ActivationCode
+            ELSE FALSE
+        END
+    LIMIT 1
+);
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: createauthtoken(integer, character varying, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createauthtoken(integer, character varying, integer);
+
+CREATE OR REPLACE FUNCTION public.createauthtoken(paccountid integer, pdescription character varying, prightmask integer)
+ RETURNS character varying
+AS $$
+
+    DECLARE vTries int DEFAULT 13; -- number of tries to generate unique token
+    vCreated boolean DEFAULT FALSE;
+ --   vSeed int DEFAULT FLOOR(4294967296 * RAND(CURRENT_TIMESTAMP ^ LAST_INSERT_ID() ^ (pAccountID << 10)));
+    vToken char(40);
+BEGIN
+--select public.createauthtoken(
+--	:paccountid,	-- put the paccountid parameter value instead of 'paccountid' (int4)
+--	:pdescription,	-- put the pdescription parameter value instead of 'pdescription' (varchar)
+--	:prightmask 	-- put the prightmask parameter value instead of 'prightmask' (int4)
+--);
+    SELECT setseed(FLOOR(4294967296 * (CURRENT_TIMESTAMP # (pAccountID << 10)))); -- ???????
+    WHILE (NOT vCreated AND vTries > 0) loop
+    begin
+    
+        vToken := SHA1(RANDOM());
+        vTries := vTries - 1;
+        vCreated := TRUE;  
+        INSERT INTO auth_tokens(Token, AccountID, Description, Mask) VALUES (vToken, pAccountID, pDescription, pRightMask);
+   		EXCEPTION 
+   			when others 
+   			then  vCreated := FALSE; 
+   	end;
+   END loop; 
+
+    RETURN IF(vCreated, vToken, NULL);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: getuserfullnamebyaccountid(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getuserfullnamebyaccountid(integer);
+
+CREATE OR REPLACE FUNCTION public.getuserfullnamebyaccountid(paccountid integer)
+ RETURNS character varying
+
+AS $$
+
+    DECLARE vUserFullName VARCHAR(255);
+    vChecker INT DEFAULT null;
+BEGIN
+--select public.getuserfullnamebyaccountid(
+--	:paccountid 	-- put the paccountid parameter value instead of 'paccountid' (int4)
+--);
+    -- try to find student with that account id
+    SELECT students.ID, CONCAT(students.LastName,' ',students.FirstName,' ',students.SecondName)
+        INTO vChecker, vUserFullName
+        FROM students
+        WHERE students.AccountID = pAccountID
+        LIMIT 1;
+
+    IF vChecker is null THEN -- try to find teacher with that account id
+        SELECT teachers.ID, CONCAT(teachers.LastName,' ',teachers.FirstName,' ',teachers.SecondName)
+            INTO vChecker, vUserFullName
+            FROM teachers
+            WHERE teachers.AccountID = pAccountID
+            LIMIT 1;
+
+        IF vChecker is null THEN
+            RETURN '';
+        END IF;
+    END IF;
+
+    RETURN vUserFullName;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: getuserrole(student_teacher_admin_dean); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.getuserrole(student_teacher_admin_dean);
+
+CREATE OR REPLACE FUNCTION public.getuserrole(ptype student_teacher_admin_dean)
+ RETURNS integer
+AS $$
+
+BEGIN
+--select public.getuserrole(
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (student_teacher_admin_dean)
+--);
+RETURN (CASE pType
+    WHEN 'dean' THEN 4
+    WHEN 'teacher' THEN 2
+    WHEN 'admin' THEN 3
+    ELSE 1
+END);
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: createrecoverytoken(character varying, character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.createrecoverytoken(character varying, character varying);
+
+CREATE OR REPLACE FUNCTION public.createrecoverytoken(paccountoremail character varying, ptoken character varying)
+ RETURNS character varying
+AS $$
+    DECLARE vAccountID INT DEFAULT null;
+    DECLARE vUserFullName TEXT; 
+BEGIN
+--select public.createrecoverytoken(
+--	:paccountoremail,	-- put the paccountoremail parameter value instead of 'paccountoremail' (varchar)
+--	:ptoken 	-- put the ptoken parameter value instead of 'ptoken' (varchar)
+--);
+	-- get account ID
+    SELECT accounts.ID INTO vAccountID
+        FROM accounts
+        WHERE accounts.EMail = pAccountOrEMail OR
+              accounts.Login = pAccountOrEMail
+        LIMIT 1;
+    IF vAccountID is null THEN
+        RETURN '';
+    END IF;
+
+    vUserFullName := GetUserFullNameByAccountID(vAccountID);
+    IF vUserFullName IS NULL OR vUserFullName = '' THEN
+        RETURN '';
+    END IF;
+
+    -- transform all unused recovery tokens into used
+    UPDATE recovery_tokens
+    SET recovery_tokens.isUsed = 1
+    WHERE   recovery_tokens.isUsed = 0 AND
+            recovery_tokens.AccountID = vAccountID;
+
+    -- handle catch constraints violations
+    INSERT INTO recovery_tokens
+        ( AccountID, Token )
+        VALUES  (vAccountID, pToken);
+    RETURN vUserFullName;
+   EXCEPTION 
+   when others then  RETURN -2; -- тип ???
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: findgroup(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.findgroup(integer, integer, integer);
+
+CREATE OR REPLACE FUNCTION public.findgroup(pgradeid integer, pgroupnum integer, pfacultyid integer)
+ RETURNS integer
+AS $$
+begin
+--	select public.findgroup(
+--	:pgradeid,	-- put the pgradeid parameter value instead of 'pgradeid' (int4)
+--	:pgroupnum,	-- put the pgroupnum parameter value instead of 'pgroupnum' (int4)
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--);
+
+RETURN COALESCE((
+    SELECT study_groups.ID FROM study_groups
+    WHERE   study_groups.GradeID = pGradeID
+            AND study_groups.GroupNum = pGroupNum
+            AND study_groups.FacultyID = pFacultyID
+    LIMIT 1
+), -1);
+END
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: deleteauthtoken(character); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.deleteauthtoken(character); 
+
+CREATE OR REPLACE FUNCTION public.deleteauthtoken(ptoken character)
+ RETURNS integer
+
+AS $$
+declare vCount int;
+begin
+--	select public.deleteauthtoken(
+--	:ptoken 	-- put the ptoken parameter value instead of 'ptoken' (bpchar)
+--);
+
+    DELETE FROM auth_tokens
+        WHERE auth_tokens.Token = pToken;
+     get diagnostics vCount=ROW_COUNT;
+    RETURN vCount-1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: department_create(character varying, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_create(character varying, integer);
+
+CREATE OR REPLACE FUNCTION public.department_create(pname character varying, pfacultyid integer)
+ RETURNS integer
+
+AS $$ 
+declare vID int;
+begin
+--	select public.department_create(
+--	:pname,	-- put the pname parameter value instead of 'pname' (varchar)
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--);
+
+	INSERT INTO departments (Name, FacultyID) VALUES (pName, pFacultyID)
+      ON conflict on constraint departments_name_facultyid_key do
+       update set departments.facultyid=pFacultyID returning id into vID;
+ 
+    RETURN vID; -- может достаточно return 0; ???
+	EXCEPTION 
+	when others then RETURN -1;
+end
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: department_update(integer, text, text, text); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.department_update(integer, text, text, text);
+
+CREATE OR REPLACE FUNCTION public.department_update(pdepartmentid integer, pdepartmentexternalid text, pdepartmentname text, pfacultyid text)
+ RETURNS integer
+
+AS $$ 
+begin
+--	select public.department_update(
+--	:pdepartmentid,	-- put the pdepartmentid parameter value instead of 'pdepartmentid' (int4)
+--	:pdepartmentexternalid,	-- put the pdepartmentexternalid parameter value instead of 'pdepartmentexternalid' (text)
+--	:pdepartmentname,	-- put the pdepartmentname parameter value instead of 'pdepartmentname' (text)
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (text)
+--);
+	
+  IF pDepartmentExternalID IS NOT NULL THEN
+    UPDATE departments SET ExternalID = pDepartmentExternalID WHERE ID = pDepartmentID;
+  END IF;
+
+  IF pDepartmentName IS NOT NULL THEN
+    UPDATE departments SET Name = pDepartmentName WHERE ID = pDepartmentID;
+  END IF;
+
+  IF pFacultyID IS NOT NULL THEN
+    UPDATE departments SET FacultyID = pFacultyID WHERE ID = pDepartmentID;
+  END IF;
+
+  RETURN pDepartmentID;
+  EXCEPTION  when others then RETURN -1;
+end
+ $$  LANGUAGE plpgsql;
+
+--
+-- Name: detachallstudents(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.detachallstudents(integer);
+
+CREATE OR REPLACE FUNCTION public.detachallstudents(pdisciplineid integer)
+ RETURNS integer
+
+AS $$
+begin
+--	select public.detachallstudents(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    DELETE FROM disciplines_groups
+        WHERE disciplines_groups.DisciplineID = pDisciplineID;
+    DELETE FROM disciplines_students
+        WHERE disciplines_students.DisciplineID = pDisciplineID;
+    RETURN 0;
+END;
+$$  LANGUAGE plpgsql;
+
+-----------------------------------------------------------------------------------------------------------------------------------------
+
+--
+-- Name: discipline_find(integer, integer, integer, exam_credit_grading_credit); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_find(integer, integer, integer, exam_credit_grading_credit);
+
+CREATE OR REPLACE FUNCTION public.discipline_find(pstudyplanid integer, psemesterid integer, psubjectid integer, pexamtype exam_credit_grading_credit)
+ RETURNS SETOF integer
+
+AS $$
+--select public.discipline_find(
+--	:pstudyplanid,	-- put the pstudyplanid parameter value instead of 'pstudyplanid' (int4)
+--	:psemesterid,	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--	:psubjectid,	-- put the psubjectid parameter value instead of 'psubjectid' (int4)
+--	:pexamtype 	-- put the pexamtype parameter value instead of 'pexamtype' (exam_credit_grading_credit)
+--);
+	SELECT disciplines.ID
+    FROM disciplines
+      INNER JOIN disciplines_study_plans ON disciplines.ID = disciplines_study_plans.DisciplineID
+    WHERE disciplines_study_plans.StudyPlanID = pStudyPlanID AND
+          disciplines.SemesterID = pSemesterID AND
+          disciplines.SubjectID = pSubjectID AND
+          disciplines.ExamType = pExamType;
+ $$  LANGUAGE sql;
+ 
+ --
+-- Name: discipline_getidfromexternalid(character varying); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getidfromexternalid(character varying);
+ 
+ CREATE OR REPLACE FUNCTION public.discipline_getidfromexternalid(pdisciplineexternalid character varying)
+ RETURNS integer
+
+AS $$ 
+begin
+--	select public.discipline_getidfromexternalid(
+--	:pdisciplineexternalid 	-- put the pdisciplineexternalid parameter value instead of 'pdisciplineexternalid' (varchar)
+--);
+	return 
+	coalesce(
+	  (SELECT disciplines.ID    FROM disciplines
+        WHERE disciplines.ExternalID = pDisciplineExternalID
+        LIMIT 1),
+	-1);
+	
+end
+ $$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_getinfo(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getinfo(integer); 
+
+CREATE OR REPLACE FUNCTION public.discipline_getinfo(pdisciplineid integer)
+ RETURNS TABLE(id integer, authorid integer, gradeid integer, gradenum integer, degree bachelor_master_specialist, type exam_credit_grading_credit, lectures integer, practice integer, labs integer, semesterid integer, subjectid integer, subjectname character varying, subjectabbr character varying, facultyid integer, facultyname character varying, islocked integer, milestone integer, subtype scientific_disciplinary_coursework, compounddiscid integer, ismapcreated boolean, isbonus boolean, semesternum integer, semesteryear integer)
+
+AS $$
+
+    DECLARE vIsBonus BOOLEAN;
+begin
+--	select * from public.discipline_getinfo(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    vIsBonus := EXISTS(
+        SELECT * FROM modules
+            WHERE   modules.DisciplineID = pDisciplineID AND
+                    modules.Type = 'bonus'
+            LIMIT 1
+    );
+
+    SELECT
+        view_disciplines.DisciplineID AS ID,
+        view_disciplines.AuthorID,
+        view_disciplines.GradeID,
+        view_disciplines.GradeNum,
+        view_disciplines.Degree,
+        view_disciplines.ExamType       AS Type,
+        view_disciplines.LectureCount   AS Lectures,
+        view_disciplines.PracticeCount  AS Practice,
+        view_disciplines.LabCount       AS Labs,
+        view_disciplines.SemesterID,
+        view_disciplines.SubjectID,
+        view_disciplines.SubjectName,
+        view_disciplines.SubjectAbbr,
+        view_disciplines.FacultyID,
+        view_disciplines.FacultyName,
+        view_disciplines.IsLocked,
+        view_disciplines.Milestone,
+        view_disciplines.Subtype,
+        view_disciplines.CompoundDiscID,
+        (view_disciplines.MaxRate = 100) AS IsMapCreated,
+        vIsBonus AS IsBonus,
+        semesters.Num AS semesterNum, -- TODO: Camelize
+        semesters.Year AS semesterYear
+    FROM view_disciplines
+    INNER JOIN semesters ON semesters.ID = view_disciplines.SemesterID
+    WHERE view_disciplines.DisciplineID = pDisciplineID
+    LIMIT 1;
+END ;
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: discipline_delete(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_delete(integer);
+
+CREATE OR REPLACE FUNCTION public.discipline_delete(pdisciplineid integer)
+ RETURNS integer
+ 
+AS $$
+begin
+--	select public.discipline_delete(
+--	:pdisciplineid 	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--);
+
+    -- delete roadmap
+    DELETE FROM submodules
+        WHERE submodules.moduleID in (
+            select modules.id FROM modules
+            WHERE modules.DisciplineID = pDisciplineID
+        );
+
+    DELETE FROM modules
+        WHERE modules.DisciplineID = pDisciplineID;
+
+    -- detach all entities from discipline
+    DELETE FROM disciplines_teachers
+        WHERE disciplines_teachers.DisciplineID = pDisciplineID;
+    DELETE FROM disciplines_students
+        WHERE disciplines_students.DisciplineID = pDisciplineID;
+    DELETE FROM disciplines_groups
+        WHERE disciplines_groups.DisciplineID = pDisciplineID;
+    DELETE FROM logs_binds_groups
+        WHERE logs_binds_groups.DisciplineID = pDisciplineID;
+    DELETE FROM logs_binds_students
+        WHERE logs_binds_students.DisciplineID = pDisciplineID;
+
+    -- delete discipline
+    DELETE FROM disciplines
+        WHERE disciplines.ID = pDisciplineID;
+        --LIMIT 1;
+
+    RETURN 0;
+END;
+$$ LANGUAGE plpgsql;
+
+--
+-- Name: discipline_getinfobysubmodule(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getinfobysubmodule(integer);
+
+CREATE OR REPLACE FUNCTION public.discipline_getinfobysubmodule(psubmoduleid integer)
+
+ RETURNS TABLE(id integer, authorid integer, gradeid integer, gradenum integer, degree bachelor_master_specialist, type exam_credit_grading_credit, lectures integer, practice integer, labs integer, semesterid integer, subjectid integer, subjectname character varying, subjectabbr character varying, facultyid integer, facultyname character varying, islocked integer, milestone integer, subtype scientific_disciplinary_coursework, compounddiscid integer, ismapcreated boolean, isbonus boolean, semesternum integer, semesteryear integer)
+
+AS $$
+
+    DECLARE vDisciplineID INT DEFAULT -1;
+begin
+--	select * from public.discipline_getinfobysubmodule(
+--	:psubmoduleid 	-- put the psubmoduleid parameter value instead of 'psubmoduleid' (int4)
+--);
+    SELECT  modules.DisciplineID
+        INTO vDisciplineID
+        FROM submodules
+        INNER JOIN modules ON  submodules.ModuleID = modules.ID
+        WHERE submodules.ID = pSubmoduleID
+        LIMIT 1;
+
+   return query select * from Discipline_GetInfo(vDisciplineID);
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_getmodules(integer, exam_rate_all); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getmodules(integer, exam_rate_all);
+
+CREATE OR REPLACE FUNCTION public.discipline_getmodules(pdisciplineid integer, ptype exam_rate_all)
+ RETURNS 
+  table ( 
+   ID integer,
+   Name varchar,
+   type regular_exam_bonus_extra
+  )
+
+AS $$
+begin
+--	select public.discipline_getmodules(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (exam_rate_all)
+--);
+    SELECT  modules.ID,
+            modules.Name,
+            modules.Type
+        FROM modules
+        WHERE   modules.DisciplineID = pDisciplineID AND
+            CASE pType
+                WHEN 'exam' THEN modules.Type = 'exam' OR modules.Type = 'extra'
+                WHEN 'rate' THEN modules.Type != 'exam'
+                ELSE TRUE
+            END
+        ORDER BY
+            CASE pType
+                WHEN 'exam' THEN InternalOrderModuleTypesForSession(modules.Type)
+                WHEN 'rate' THEN modules.Type # 1 -- XOR 1, 3, 2, 4 ASC
+                ELSE TRUE
+            END ASC,
+            modules.OrderNum ASC;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_getstudents(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getstudents(integer);
+
+CREATE OR REPLACE FUNCTION public.discipline_getstudents(pdisciplineid integer)
+ RETURNS TABLE(id integer, recordbookid integer, lastname character varying, firstname character varying, secondname character varying, gradeid integer, gradenum integer, degree bachelor_master_specialist, groupid integer, groupnum integer, attachtype attach_detach)
+
+AS $$ 
+    DECLARE vSemesterID int DEFAULT -1;
+BEGIN
+ --select  * from public.Discipline_GetStudents(
+--	:pDisciplineID 	-- put the pDisciplineID parameter value instead of 'pDisciplineID' (INT)
+--);
+    vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
+  return query
+    SELECT  view_students.StudentID AS ID,
+            view_students.RecordBookID,
+            view_students.LastName,
+            view_students.FirstName,
+            view_students.SecondName,
+            view_students.GradeID,
+            view_students.GradeNum,
+            view_students.Degree,
+            view_students.GroupID,
+            view_students.GroupNum,
+            view_disciplines_recordbooks.Type AS AttachType
+        FROM view_disciplines_recordbooks
+        INNER JOIN view_students ON  view_disciplines_recordbooks.RecordBookID = view_students.RecordBookID
+        WHERE view_students.SemesterID = vSemesterID AND view_disciplines_recordbooks.DisciplineID = pDisciplineID
+        ORDER BY    view_disciplines_recordbooks.Type = 'attach' ASC,
+                    view_students.GradeID ASC,
+                    view_students.GroupNum ASC,
+                    view_students.LastName ASC,
+                    view_students.FirstName ASC;
+END
+ $$  LANGUAGE plpgsql;
+ 
+--
+-- Name: discipline_getsubmodules(integer,exam_rate_all); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getsubmodules(integer,exam_rate_all); 
+
+ CREATE OR REPLACE FUNCTION public.discipline_getsubmodules(pdisciplineid integer, ptype exam_rate_all)
+ RETURNS table(
+  ModuleID integer,
+  ID integer,
+  Name varchar,
+  Rate integer,
+  type current_landmark_control
+ )
+
+AS $$
+
+begin
+--	select * from public.discipline_getsubmodules(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:ptype 	-- put the ptype parameter value instead of 'ptype' (exam_rate_all)
+--);
+return query
+    SELECT  view_roadmap.ModuleID,
+            view_roadmap.SubmoduleID    AS ID,
+            view_roadmap.SubmoduleName  AS Name,
+            view_roadmap.SubmoduleRate  AS Rate,
+            view_roadmap.SubmoduleType  AS Type
+        FROM view_roadmap
+        WHERE   view_roadmap.DisciplineID = pDisciplineID AND
+            CASE pType
+                WHEN 'exam' THEN view_roadmap.ModuleType = 'exam' OR view_roadmap.ModuleType = 'extra'
+                WHEN 'rate' THEN view_roadmap.ModuleType != 'exam' OR view_roadmap.SubmoduleOrderNum = 1
+                ELSE TRUE
+            END
+        ORDER BY
+            view_roadmap.ModuleOrderNum ASC,
+            view_roadmap.SubmoduleOrderNum ASC;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: discipline_getwithfullness( noukd_norate_full, integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_getwithfullness( noukd_norate_full, integer,integer); 
+
+CREATE OR REPLACE FUNCTION public.discipline_getwithfullness(ptype noukd_norate_full, psemesterid integer, pfacultyid integer)
+ RETURNS SETOF integer
+
+AS $$ 
+--select public.discipline_getwithfullness(
+--	:ptype,	-- put the ptype parameter value instead of 'ptype' (noukd_norate_full)
+--	:psemesterid,	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--	:pfacultyid 	-- put the pfacultyid parameter value instead of 'pfacultyid' (int4)
+--);
+
+SELECT view_disciplines.DisciplineID AS ID
+        FROM view_disciplines
+        WHERE view_disciplines.SemesterID = pSemesterID AND
+              view_disciplines.FacultyID = pFacultyID AND
+              CASE WHEN pType = 'noukd' 
+                   THEN (view_disciplines.MaxRate != 100 AND view_disciplines.IsLocked = 0) 
+                   ELSE true
+              END 
+              AND
+              CASE WHEN pType = 'norate' 
+              	THEN (view_disciplines.MaxRate = 100 AND view_disciplines.IsLocked = 0) 
+              	ELSE TRUE 
+              END 
+              AND
+              CASE WHEN pType = 'full' 
+             	   THEN (view_disciplines.MaxRate = 100 AND view_disciplines.IsLocked = 1) 
+                   ELSE TRUE 
+               END;
+ $$  LANGUAGE sql;
+
+--
+-- Name: discipline_setgradeunsafe(integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_setgradeunsafe(integer,integer);
+
+ CREATE OR REPLACE FUNCTION public.discipline_setgradeunsafe(
+    pDisciplineID INT,
+    pGradeID INT
+)
+RETURNS INT
+
+AS $$ 
+declare vRow int;
+begin
+--	select public.Discipline_SetGradeUnsafe(
+--	:pDisciplineID,	-- put the pDisciplineID parameter value instead of 'pDisciplineID' (INT)
+--	:pGradeID 	-- put the pGradeID parameter value instead of 'pGradeID' (INT)
+--);
+
+    -- set grade
+    UPDATE disciplines
+        SET disciplines.GradeID = pGradeID
+        WHERE disciplines.ID = pDisciplineID ;
+     get diagnostics vRow = ROW_COUNT;
+       RETURN vRow-1;
+   EXCEPTION 
+   when others then RETURN -1;
+END
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: discipline_setrate(integer,integer,integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_setrate(integer,integer,integer,integer);
+
+ CREATE OR REPLACE FUNCTION public.discipline_setrate(pteacherid integer, precordbookid integer, psubmoduleid integer, prate integer)
+ RETURNS integer
+
+AS $$ 
+    DECLARE vDisciplineID INT DEFAULT -1;
+   			vMaxRate INT DEFAULT -1;
+   			vModuleType INT DEFAULT -1;
+    DECLARE vIsOver BOOLEAN DEFAULT FALSE; 
+   			vIsLocked BOOLEAN DEFAULT FALSE; 
+   			vIsUsed BOOLEAN DEFAULT FALSE;
+   		    vIsUpdated boolean default false;
+begin
+--	select Discipline_SetRate(
+--	:pTeacherID,	-- put the pTeacherID parameter value instead of 'pTeacherID' (INT)
+--	:pRecordBookID,	-- put the pRecordBookID parameter value instead of 'pRecordBookID' (INT)
+--	:pSubmoduleID,	-- put the pSubmoduleID parameter value instead of 'pSubmoduleID' (INT)
+--	:pRate 	-- put the pRate parameter value instead of 'pRate' (INT)
+--);
+
+      -- 0) check rights ПОЧЕМУ ТАК ПОЗДНО ????
+      IF NOT InternalIsStudentAttached(pRecordBookID, vDisciplineID) OR
+       NOT InternalIsTeacherBound(pTeacherID, vDisciplineID) THEN
+        RETURN 1;
+      END IF;
+	
+	    IF pRate < 0 THEN
+        INSERT INTO logs_rating (RecordBookID, SubmoduleID, TeacherID, Rate, Action ) VALUES
+            (pRecordBookID, pSubmoduleID, pTeacherID, pRate, 'delete');
+
+        -- TODO: extract method log rate
+        DELETE FROM rating_table
+            WHERE   rating_table.RecordBookID = pRecordBookID AND
+                    rating_table.SubmoduleID = pSubmoduleID
+           ;
+        RETURN 0;
+    	END IF;
+    
+    vIsOver = TRUE;
+   SELECT  disciplines.ID,
+            disciplines.IsLocked,
+            disciplines.Milestone,
+            submodules.IsUsed,
+            submodules.maxRate,
+            modules.Type
+        INTO vDisciplineID, vIsLocked, vIsOver, vIsUsed, vMaxRate, vModuleType
+        FROM submodules
+        INNER JOIN modules                ON  submodules.ModuleID = modules.ID
+        INNER JOIN disciplines            ON  modules.DisciplineID = disciplines.ID
+        WHERE   submodules.ID = pSubmoduleID
+        LIMIT 1;
+    -- correct max rate for extra module
+    IF vModuleType = 4 THEN -- 4 - extra
+         vMaxRate = CalculateMaxRateForExtra(vDisciplineID, pRecordBookID);
+    END IF;
+      -- 1) check rights ПОЧЕМУ ТАК ПОЗДНО ????
+    IF NOT InternalIsStudentAttached(pRecordBookID, vDisciplineID) OR
+       NOT InternalIsTeacherBound(pTeacherID, vDisciplineID) THEN
+        RETURN 1;
+    END IF;
+    --2) check, you can't rate regular and bonus after milestone
+    IF (vIsOver AND (vModuleType = 1 OR vModuleType = 3)) THEN --1 - regular, 3 - bonus
+        RETURN 2;
+    END IF;
+    --3) check, max rate exceeding
+    IF  pRate > vMaxRate THEN
+        RETURN 3;
+    END IF;
+     -- add rate, or update old
+    visUpdated = FALSE;
+    INSERT INTO rating_table (RecordBookID, TeacherID, SubmoduleID, Rate, Date)
+        VALUES  (pRecordBookID, pTeacherID, pSubmoduleID, pRate, current_date )
+        on conflict on constraint rating_table_pkey do
+         update set
+            rating_table.TeacherID  = pTeacherID, --(@isUpdated := pTeacherID),
+            rating_table.Rate       = pRate,
+            rating_table.Date       = current_date
+         returning true into visUpdated ; ---??????? если не получится, разделить insert и update
+          --log rate
+    INSERT INTO logs_rating (RecordBookID, SubmoduleID, TeacherID, Rate, Action )
+        VALUES (pRecordBookID, pSubmoduleID, pTeacherID, pRate, IF(@isUpdated, 'change', 'add') );
+
+   -- lock discipline for structure editing
+    IF NOT vIsLocked THEN
+        UPDATE disciplines
+            SET disciplines.IsLocked = TRUE
+            WHERE disciplines.ID = vDisciplineID
+            ;
+    END IF;
+--add submodule to max rate counting (see triggers)
+    IF NOT vIsUsed THEN
+        UPDATE submodules
+            SET submodules.IsUsed = TRUE
+            WHERE submodules.ID = pSubmoduleID
+           ;
+    END IF;
+    RETURN 0;
+EXCEPTION 
+when others then RETURN -1;
+end
+ $$ LANGUAGE plpgsql;
+ 
+--
+-- Name: discipline_unbindteacher(integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.discipline_unbindteacher(integer,integer);
+
+ CREATE OR REPLACE FUNCTION public.discipline_unbindteacher(pdisciplineid integer, pbindingteacher integer)
+ RETURNS integer
+AS $$
+declare vRow int;
+begin
+--	select public.discipline_unbindteacher(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pbindingteacher 	-- put the pbindingteacher parameter value instead of 'pbindingteacher' (int4)
+--);
+
+    DELETE FROM disciplines_teachers
+        WHERE   disciplines_teachers.DisciplineID = pDisciplineID AND disciplines_teachers.TeacherID = pBindingTeacher;
+        --LIMIT 1;
+        get diagnostics vRow=ROW_COUNT;
+    RETURN vRow-1;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: student_getrecordbooks(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getrecordbooks(integer);
+
+CREATE OR REPLACE FUNCTION public.student_getrecordbooks(pStudentID INT)
+RETURNS table (
+ID integer,
+ExternalID varchar,
+degree bachelor_master_specialist
+)
+
+AS $$ 
+--	select  * from Student_GetRecordBooks(
+--	:pStudentID 	-- put the pStudentID parameter value instead of 'pStudentID' (INT)
+--);
+
+    SELECT DISTINCT
+      view_students.RecordBookID       AS ID,
+      view_students.RecordBookNumber       AS ExternalID,
+      view_students.Degree
+    FROM view_students
+    WHERE view_students.StudentID = pStudentID;
+ $$ LANGUAGE sql;
+
+--
+-- Name: student_getrecordbookandgroup(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getrecordbookandgroup(integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.student_getrecordbookandgroup(
+  pStudentID INT, 
+  pSemesterID int)
+RETURNS table (
+RecordBookID integer,
+GroupID integer
+)
+
+AS $$ 
+-- select * from Student_GetRecordBookAndGroup(
+--	:pStudentID,	-- put the pStudentID parameter value instead of 'pStudentID' (INT)
+--	:pSemesterID 	-- put the pSemesterID parameter value instead of 'pSemesterID' (INT)
+--);
+    SELECT  record_books.ID AS RecordBookID,
+            students_groups.GroupID AS GroupID
+        FROM students_groups
+        JOIN record_books ON record_books.ID = students_groups.RecordBookID
+        WHERE record_books.StudentID = pStudentID AND
+          students_groups.SemesterID = pSemesterID AND
+          students_groups.State != 'expulsion';
+ $$ LANGUAGE sql;
+ 
+--
+-- Name: student_getdisciplinestemp(integer, integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getdisciplinestemp(integer, integer);
+
+ CREATE OR REPLACE FUNCTION public.student_getdisciplinestemp(
+pRecordBookID INT, pSemesterID INT
+)
+RETURNS INT
+
+AS $$ 
+begin
+--вспомогательная 
+--создает временную таблицу для другой функции	
+-- изменила группировку !!!!!
+    CREATE TEMPORARY TABLE tStudentDisciplines AS (
+        SELECT disc_rb.DisciplineID
+        FROM (
+            SELECT  view_disciplines_recordbooks.DisciplineID,
+                    view_disciplines_recordbooks.RecordBookID,
+                    COALESCE(view_disciplines_recordbooks.Type) AS type
+                    FROM view_disciplines_recordbooks
+                WHERE view_disciplines_recordbooks.SemesterID = pSemesterID AND
+                      view_disciplines_recordbooks.RecordBookID = pRecordBookID
+                GROUP BY view_disciplines_recordbooks.DisciplineID, 
+                		view_disciplines_recordbooks.RecordBookID,
+                		view_disciplines_recordbooks.Type
+            ) AS disc_rb
+        where disc_rb.type is null or  disc_rb.Type != 'detach'
+    );
+    RETURN 0;
+END
+ $$ LANGUAGE plpgsql;
+
+--
+-- Name: student_getteacherslist(integer, integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getteacherslist(integer, integer,integer);
+
+ CREATE OR REPLACE FUNCTION public.student_getteacherslist(
+pRecordBookID INT,
+pSemesterID INT,
+pLoadAll INT
+)
+RETURNS table (
+DisciplineID integer,
+TeacherID integer,
+LastName varchar,
+FirstName varchar,
+SecondName varchar
+)
+
+AS $$ 
+    DECLARE vRes integer; vStudentGroup INT DEFAULT -1;
+BEGIN
+--select * from public.Student_GetTeachersList(
+--	:pRecordBookID,	-- put the pRecordBookID parameter value instead of 'pRecordBookID' (INT)
+--	:pSemesterID,	-- put the pSemesterID parameter value instead of 'pSemesterID' (INT)
+--	:pLoadAll 	-- put the pLoadAll parameter value instead of 'pLoadAll' (INT)
+--);
+-- разобраться с параметром pLoadAll - поставила для него проверку ==1
+    DROP TABLE IF EXISTS tStudentDisciplines;
+     vRes = Student_GetDisciplinesTemp(pRecordBookID, pSemesterID);
+   return query
+    SELECT  tStudentDisciplines.DisciplineID,
+            teachers.ID AS TeacherID,
+            accounts.LastName,
+            accounts.FirstName,
+            accounts.SecondName
+        FROM tStudentDisciplines
+        INNER JOIN disciplines ON disciplines.ID = tStudentDisciplines.DisciplineID
+        LEFT JOIN disciplines_teachers ON disciplines_teachers.DisciplineID = tStudentDisciplines.DisciplineID
+        INNER JOIN teachers ON teachers.ID = disciplines_teachers.TeacherID
+        INNER JOIN accounts ON teachers.AccountID = accounts.ID
+        WHERE pLoadAll=1 OR disciplines.Subtype IS NULL
+        ORDER BY    tStudentDisciplines.DisciplineID ASC,
+                    accounts.LastName ASC,
+                    accounts.FirstName ASC;
+END
+ $$ LANGUAGE plpgsql;
+
+--
+-- Name: student_getinfo(integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getinfo(integer);
+
+ CREATE OR REPLACE FUNCTION public.student_getinfo(pid integer)
+ RETURNS TABLE(id integer, lastname character varying, firstname character varying, secondname character varying, accountid integer, semesterid integer, groupid integer, groupnum integer, groupname character varying, gradeid integer, gradenum integer, degree bachelor_master_specialist, specid integer, specname character varying, specabbr character varying, speccode character varying, facultyid integer, facultyname character varying, facultyabbr character varying)
+ 
+AS $$
+--select * from public.student_getinfo(
+--	:pid 	-- put the pid parameter value instead of 'pid' (int4)
+--);
+
+    SELECT
+        view_students.StudentID   AS ID,
+        view_students.LastName,
+        view_students.FirstName,
+        view_students.SecondName,
+        view_students.AccountID,
+        view_students.SemesterID,
+        view_students.GroupID,
+        view_students.GroupNum,
+        view_students.GroupName,
+        view_students.GradeID,
+        view_students.GradeNum,
+        view_students.Degree,
+        view_students.SpecID,
+        view_students.SpecName,
+        view_students.SpecAbbr,
+        view_students.SpecCode,
+        view_students.FacultyID,
+        view_students.FacultyName,
+        view_students.FacultyAbbr
+    FROM view_students WHERE pID = view_students.StudentID;
+    --LIMIT 1;
+
+$$ LANGUAGE sql;
+
+
+--
+-- Name: student_getdisciplines(integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.student_getdisciplines(integer,integer);
+
+CREATE OR REPLACE FUNCTION public.student_getdisciplines(precordbookid integer, psemesterid integer)
+ RETURNS TABLE(id integer, subjectid integer, subjectname character varying, type exam_credit_grading_credit, subtype scientific_disciplinary_coursework, lastname character varying, firstname character varying, secondname character varying, rate integer, maxcurrentrate integer)
+
+AS $$ 
+DECLARE vRes INT DEFAULT -1;
+begin
+--	select public.student_getdisciplines(
+--	:precordbookid,	-- put the precordbookid parameter value instead of 'precordbookid' (int4)
+--	:psemesterid 	-- put the psemesterid parameter value instead of 'psemesterid' (int4)
+--);
+
+	    DROP TABLE IF EXISTS tStudentDisciplines;
+   		vRes = Student_GetDisciplinesTemp(pRecordBookID, pSemesterID);
+   	return query
+   	SELECT  view_disciplines.DisciplineID AS ID,
+            view_disciplines.SubjectID,
+            view_disciplines.SubjectName,
+            view_disciplines.ExamType AS Type,
+            view_disciplines.Subtype,
+            accounts.LastName,
+            accounts.FirstName,
+            accounts.SecondName,
+            ( tDR.RateExam + tDR.RateMExam ) AS Rate,
+            ( tDR.MaxRegularRate + tDR.MaxExamRate ) AS MaxCurrentRate
+        FROM (
+            SELECT  tRating.DisciplineID,
+                    SUM(
+                        iif_sql( tRating.SubmoduleIsUsed AND 
+                            tRating.ModuleType  is not null and 
+                            tRating.ModuleType = 'regular',
+                            tRating.SubmoduleRate, 0)
+                    ) AS MaxRegularRate,
+                    MAX(
+                        iif_sql( tRating.SubmoduleIsUsed AND 
+                        tRating.ModuleType  is not null and 
+                        tRating.ModuleType = 'exam',
+                            tRating.SubmoduleRate, 0)
+                    ) AS MaxExamRate,
+                    SUM(
+                    	 iif_sql(tRating.ModuleType  is not null and 
+                         tRating.ModuleType != 'exam', tRating.Rate, 0)
+                         ) AS RateMExam,
+                    MAX(
+                    	iif_sql(tRating.ModuleType  is not null and 
+                    	       tRating.ModuleType = 'exam' AND 
+                    	       tRating.Rate IS NOT NULL, tRating.Rate, 0)
+                    	       ) AS RateExam
+                FROM (
+                    SELECT  tStudentDisciplines.DisciplineID,
+                            vr.SubmoduleRate,
+                            vr.ModuleType,
+                            rt.Rate,
+                            vr.SubmoduleIsUsed
+                        FROM tStudentDisciplines
+                        LEFT JOIN view_roadmap AS vr ON   vr.DisciplineID = tStudentDisciplines.DisciplineID
+                        LEFT JOIN rating_table AS rt  ON  rt.RecordBookID = pRecordBookID AND
+                                                            rt.SubmoduleID = vr.SubmoduleID
+                ) AS tRating
+                GROUP BY tRating.DisciplineID
+            ) AS tDR
+        INNER JOIN view_disciplines ON view_disciplines.DisciplineID = tDR.DisciplineID
+        INNER JOIN teachers ON teachers.ID = view_disciplines.AuthorID
+        INNER JOIN accounts ON teachers.AccountID = accounts.ID
+        ORDER BY view_disciplines.ExamType ASC, view_disciplines.SubjectName ASC;
+end
+ $$  LANGUAGE plpgsql;
+ 
+--
+-- Name: stats_getsupportrequestfrequency(date); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.stats_getsupportrequestfrequency(date); 
+
+ CREATE OR REPLACE FUNCTION public.stats_getsupportrequestfrequency(dlimit date)
+ RETURNS 
+ table (
+ counted_leads bigint,
+ count_date date
+ )
+
+AS $$
+
+--	select public.stats_getsupportrequestfrequency(
+--	:dlimit 	-- put the dlimit parameter value instead of 'dlimit' (date)
+--);
+SELECT
+  count(DATE(requests.Date)) as counted_leads, DATE(requests.Date) as count_date
+FROM requests WHERE requests.Date > dLimit
+GROUP BY DATE(requests.Date);
+ 
+$$  LANGUAGE sql;
+
+--
+-- Name: unbindstudent(integer,integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.unbindstudent(integer,integer,integer);
+
+CREATE OR REPLACE FUNCTION public.unbindstudent(pteacherid integer, pdisciplineid integer, precordbookid integer)
+ RETURNS integer
+
+AS $$ 
+   DECLARE vInGroup int DEFAULT -1;  
+  		   vStudentGroupID int DEFAULT -1; 
+  		   vSemesterID INT DEFAULT -1;
+BEGIN
+--select public.unbindstudent(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:precordbookid 	-- put the precordbookid parameter value instead of 'precordbookid' (int4)
+--);
+
+-- todo: move to php layer
+    IF pTeacherID IS NOT NULL AND NOT InternalIsTeacherBound(pTeacherID, pDisciplineID) THEN
+        RETURN -1;
+    END IF;
+
+    vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
+    vStudentGroupID = GetStudentGroup(pRecordBookID, vSemesterID);
+
+    --2. check if student's group is bound yet
+     vInGroup = EXISTS(
+        SELECT * FROM disciplines_groups
+        WHERE disciplines_groups.DisciplineID = pDisciplineID AND
+              disciplines_groups.GroupID = vStudentGroupID
+        LIMIT 1
+    );
+
+    IF vInGroup > 0 THEN --student in general group
+        INSERT INTO disciplines_students (DisciplineID, RecordBookID, Type)
+            VALUES (pDisciplineID, pRecordBookID, 'detach')
+            ON conflict on constraint disciplines_students_recordbookid_disciplineid_key do 
+            UPDATE  set disciplines_students.Type = 'detach';
+    ELSE
+        DELETE FROM disciplines_students
+            WHERE   disciplines_students.DisciplineID = pDisciplineID AND
+                    disciplines_students.RecordBookID = pRecordBookID
+            ;
+    END IF;
+    RETURN LogBind(pDisciplineID, pTeacherID, pRecordBookID, 'detach', 'student');
+   EXCEPTION 
+   when others then RETURN -1;
+END
+ $$  LANGUAGE plpgsql;
+
+--
+-- Name: removestudentsattach(integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.removestudentsattach(integer,integer);
+
+ CREATE OR REPLACE FUNCTION public.removestudentsattach(pdisciplineid integer, pgroupid integer)
+ RETURNS integer
+
+AS $$
+
+    DECLARE vSemesterID INT; 
+begin
+--	select public.removestudentsattach(
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pgroupid 	-- put the pgroupid parameter value instead of 'pgroupid' (int4)
+--);
+
+    vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
+    DELETE FROM disciplines_students
+        WHERE   disciplines_students.DisciplineID = pDisciplineID AND
+                disciplines_students.StudentID IN (
+                    SELECT students_groups.StudentID FROM students_groups
+                    WHERE   students_groups.GroupID = pGroupID AND students_groups.SemesterID = vSemesterID
+                );
+        RETURN 0;
+END;
+$$  LANGUAGE plpgsql;
+
+--
+-- Name: unbindgroup(integer,integer,integer,integer); Type: FUNCTION; Schema: public; Owner: -
+--
+DROP FUNCTION IF EXISTS public.unbindgroup(integer,integer,integer,integer);
+
+CREATE OR REPLACE FUNCTION public.unbindgroup(pteacherid integer, pdisciplineid integer, pgroupid integer, vres integer)
+ RETURNS integer
+
+AS $$
+declare vRes int;
+begin
+--	select public.unbindgroup(
+--	:pteacherid,	-- put the pteacherid parameter value instead of 'pteacherid' (int4)
+--	:pdisciplineid,	-- put the pdisciplineid parameter value instead of 'pdisciplineid' (int4)
+--	:pgroupid,	-- put the pgroupid parameter value instead of 'pgroupid' (int4)
+--	:vres 	-- put the vres parameter value instead of 'vres' (int4)
+--);
+
+    -- todo: move to php layer
+    CASE WHEN NOT InternalIsTeacherBound(pTeacherID, pDisciplineID) OR InternalIsMapLocked(pDisciplineID) THEN
+        RETURN -1;
+		else null;
+    END CASE;
+    -- detach group from the discipline
+    DELETE FROM disciplines_groups
+        WHERE   disciplines_groups.DisciplineID = pDisciplineID AND disciplines_groups.GroupID = pGroupID;
+        --LIMIT 1;
+
+    SELECT LogBind(pDisciplineID, pTeacherID, pGroupID, 'detach', 'group') INTO vRes;
+    RETURN RemoveStudentsAttach(pDisciplineID, pGroupID);
+END
+$$  LANGUAGE plpgsql;
+
+
+--- Триггеры ----------------------------------------------------------------------------------------------------------------------------
+
+--
+-- Name: submodule_i(); Type: TRIGGER FUNCTION; Schema: public; Owner: -
+--
+DROP TRIGGER IF EXISTS  tr_i_submodule ON submodules; 
+DROP FUNCTION IF EXISTS public.submodule_i();
+
+CREATE OR REPLACE FUNCTION public.submodule_i()
+ RETURNS trigger
+ AS $$ 
+    declare vDisciplineID int default 0; addmax int default 0; addcur int default 0;
+    declare vAccumType boolean default false; vChangeMax boolean default false;
+    declare vModuleType regular_exam_bonus_extra default 'extra';
+begin
+	 -- todo: check repeated bonus
+	    select modules.Type into vModuleType
+        from modules
+        where modules.ID = new.ModuleID
+        limit 1;
+ 
+    if (vModuleType is not null and(vModuleType = 'regular' or vModuleType = 'exam') )then 
+    
+	    vAccumType = (vModuleType != 'exam');
+       
+       select disciplines.ID into vDisciplineID
+            from modules
+                inner join disciplines on disciplines.ID = modules.DisciplineID
+            where modules.ID = new.ModuleID
+            limit 1;
+           
+         if (new.OrderNum is null)  then
+         		vChangeMax = vAccumType;
+        		else  vChangeMax = vAccumType or  new.OrderNum = 1;
+        end if;	
+       
+        if (new.isused) then addcur=new.MaxRate; end if;
+        if (vChangeMax) then addmax = new.maxrate; end if;
+       
+        update disciplines
+        set     disciplines.CurRate = disciplines.CurRate + addcur,
+                disciplines.MaxRate = disciplines.MaxRate + addmax
+        where disciplines.ID = vDisciplineID;	    
+    end if;
+end
+ $$ LANGUAGE plpgsql;
+ 
+ CREATE TRIGGER tr_i_submodule BEFORE INSERT on submodules for each row
+    EXECUTE PROCEDURE submodule_i();
+
+--
+-- Name: submodule_u(); Type: TRIGGER FUNCTION; Schema: public; Owner: -
+--
+DROP TRIGGER IF EXISTS  tr_u_submodule ON submodules; 
+DROP FUNCTION IF EXISTS public.submodule_u();
+
+CREATE OR REPLACE FUNCTION public.submodule_u()
+RETURNS trigger
+
+AS $$ 
+    declare vDisciplineID int default 0;
+    declare vAccumType    boolean default false; 
+            vChangeMaxOld boolean default false;
+            vChangeMaxNew boolean default false;
+    declare vModuleType regular_exam_bonus_extra default 'extra';
+begin
+/*    -- странная ситуация
+ * if old.ModuleID != new.ModuleID then
+        signal sqlstate '45000';
+    end if;*/
+
+    -- todo: check repeated bonus
+    select modules.Type into vModuleType
+        from modules
+        where modules.ID = new.ModuleID
+        limit 1;
+
+
+    if (vModuleType is not null and (vModuleType = 'regular' or vModuleType = 'exam') )then 
+        vAccumType = (vModuleType != 'exam');
+       
+        -- todo: extract foo
+       select disciplines.ID into vDisciplineID
+            from modules
+                inner join disciplines on disciplines.ID = modules.DisciplineID
+            where modules.ID = old.ModuleID
+            limit 1;
+     if (new.OrderNum is null)  then
+			vChangeMaxOld = vAccumType;
+			vChangeMaxNew = vAccumType;
+     else
+     	 vChangeMaxOld = vAccumType or old.OrderNum = 1;
+         vChangeMaxNew = vAccumType or new.OrderNum = 1;
+     end if;
+      
+        update disciplines
+        set     disciplines.CurRate = disciplines.CurRate
+                    - iif_sql(old.isUsed, old.MaxRate, 0)
+                    + iif_sql(new.isUsed, new.MaxRate, 0),
+                disciplines.MaxRate = disciplines.MaxRate
+                    - iif_sql(vChangeMaxOld, old.MaxRate, 0)
+                    + iif_sql(vChangeMaxNew, new.MaxRate, 0)
+        where disciplines.ID = vDisciplineID;
+    
+   end if;
+end
+ $$ LANGUAGE plpgsql;
+
+CREATE trigger tr_u_submodule  before update on submodules for each row
+    execute procedure submodule_u();
+
+--
+-- Name: submodule_d(); Type: TRIGGER FUNCTION; Schema: public; Owner: -
+--
+DROP TRIGGER IF EXISTS  tr_d_submodule ON submodules; 
+DROP FUNCTION IF EXISTS public.submodule_d();
+
+CREATE OR REPLACE FUNCTION public.submodule_d()
+ RETURNS trigger
+AS $$ 
+    declare vDisciplineID int default 0;
+    declare vAccumType boolean default false; 
+   			vChangeMax boolean default false;
+    declare vModuleType regular_exam_bonus_extra default 'extra';
+begin
+    -- todo: check repeated bonus
+    select modules.Type into vModuleType
+        from modules
+        where modules.ID = old.ModuleID
+        limit 1;
+
+    if (vModuleType is not null)and (vModuleType = 'regular' or vModuleType = 'exam') then 
+        vAccumType = ( vModuleType != 'exam');
+
+        -- todo: extract foo
+        select disciplines.ID into vDisciplineID
+            from modules
+                inner join disciplines on disciplines.ID = modules.DisciplineID
+            where modules.ID = old.ModuleID
+            limit 1;
+       if old.OrderNum is null then
+       	   vChangeMax = vAccumType;	
+       else
+           vChangeMax = vAccumType or old.OrderNum = 1;
+       end if;
+
+        update disciplines
+        set     disciplines.CurRate = disciplines.CurRate - iif_sql(old.isUsed, old.MaxRate, 0),
+                disciplines.MaxRate = disciplines.MaxRate - iif_sql(vChangeMax, old.MaxRate, 0)
+        where disciplines.ID = vDisciplineID;
+    end if;
+
+end
+ $$  LANGUAGE plpgsql;
+
+CREATE trigger tr_d_submodule  before delete on submodules for each row
+    execute procedure submodule_d();
diff --git a/db/postgresql/keys_and_index.sql b/db/postgresql/keys_and_index.sql
new file mode 100644
index 0000000000000000000000000000000000000000..1bcb258ec3db45e9d4e9f272b66b24987c2f8eb1
--- /dev/null
+++ b/db/postgresql/keys_and_index.sql
@@ -0,0 +1,1608 @@
+ALTER TABLE ONLY accounts
+    ADD CONSTRAINT accounts_email_key UNIQUE (email);
+
+
+--
+-- TOC entry 2422 (class 2606 OID 45947)
+-- Name: accounts accounts_lactivationcode_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY accounts
+    ADD CONSTRAINT accounts_lactivationcode_key UNIQUE (activationcode);
+
+
+--
+-- TOC entry 2424 (class 2606 OID 45949)
+-- Name: accounts accounts_login_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY accounts
+    ADD CONSTRAINT accounts_login_key UNIQUE (login);
+
+
+--
+-- TOC entry 2426 (class 2606 OID 45943)
+-- Name: accounts accounts_pk; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY accounts
+    ADD CONSTRAINT accounts_pk PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2430 (class 2606 OID 45964)
+-- Name: auth_tokens auth_tokens_token_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY auth_tokens
+    ADD CONSTRAINT auth_tokens_token_key UNIQUE (token);
+
+
+--
+-- TOC entry 2451 (class 2606 OID 46040)
+-- Name: compound_disciplines compound_disciplines_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY compound_disciplines
+    ADD CONSTRAINT compound_disciplines_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2453 (class 2606 OID 46061)
+-- Name: departments departments_name_facultyid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY departments
+    ADD CONSTRAINT departments_name_facultyid_key UNIQUE (name, facultyid);
+
+
+--
+-- TOC entry 2455 (class 2606 OID 46059)
+-- Name: departments departments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY departments
+    ADD CONSTRAINT departments_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2457 (class 2606 OID 46063)
+-- Name: departments departments_uk_1; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY departments
+    ADD CONSTRAINT departments_uk_1 UNIQUE (externalid);
+
+
+--
+-- TOC entry 2489 (class 2606 OID 46284)
+-- Name: disciplines_exam_type disciplines_exam_examtypename_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_exam_type
+    ADD CONSTRAINT disciplines_exam_examtypename_key UNIQUE (examtypename);
+
+
+--
+-- TOC entry 2491 (class 2606 OID 46282)
+-- Name: disciplines_exam_type disciplines_exam_type_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_exam_type
+    ADD CONSTRAINT disciplines_exam_type_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2500 (class 2606 OID 46325)
+-- Name: disciplines_groups disciplines_groups_disciplineid_groupid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_groups
+    ADD CONSTRAINT disciplines_groups_disciplineid_groupid_key UNIQUE (disciplineid, groupid);
+
+
+--
+-- TOC entry 2502 (class 2606 OID 46323)
+-- Name: disciplines_groups disciplines_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_groups
+    ADD CONSTRAINT disciplines_groups_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2482 (class 2606 OID 46234)
+-- Name: disciplines disciplines_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2511 (class 2606 OID 46371)
+-- Name: disciplines_students disciplines_students_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_students
+    ADD CONSTRAINT disciplines_students_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2513 (class 2606 OID 46373)
+-- Name: disciplines_students disciplines_students_recordbookid_disciplineid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_students
+    ADD CONSTRAINT disciplines_students_recordbookid_disciplineid_key UNIQUE (recordbookid, disciplineid);
+
+
+--
+-- TOC entry 2522 (class 2606 OID 46436)
+-- Name: disciplines_study_plans disciplines_study_plans_pk; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_study_plans
+    ADD CONSTRAINT disciplines_study_plans_pk PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2524 (class 2606 OID 46438)
+-- Name: disciplines_study_plans disciplines_study_plans_studyplanid_disciplineid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_study_plans
+    ADD CONSTRAINT disciplines_study_plans_studyplanid_disciplineid_key UNIQUE (studyplanid, disciplineid);
+
+
+--
+-- TOC entry 2528 (class 2606 OID 46457)
+-- Name: disciplines_teachers disciplines_teachers_disciplineid_teacherid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_teachers
+    ADD CONSTRAINT disciplines_teachers_disciplineid_teacherid_key UNIQUE (disciplineid, teacherid);
+
+
+--
+-- TOC entry 2530 (class 2606 OID 46455)
+-- Name: disciplines_teachers disciplines_teachers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_teachers
+    ADD CONSTRAINT disciplines_teachers_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2543 (class 2606 OID 46525)
+-- Name: exam_period_options exam_period_options_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY exam_period_options
+    ADD CONSTRAINT exam_period_options_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2545 (class 2606 OID 46527)
+-- Name: exam_period_options exam_period_options_recordbookid_submoduleid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY exam_period_options
+    ADD CONSTRAINT exam_period_options_recordbookid_submoduleid_key UNIQUE (recordbookid, submoduleid);
+
+
+--
+-- TOC entry 2432 (class 2606 OID 45992)
+-- Name: faculties faculties_name_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY faculties
+    ADD CONSTRAINT faculties_name_key UNIQUE (name);
+
+
+--
+-- TOC entry 2434 (class 2606 OID 45990)
+-- Name: faculties faculties_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY faculties
+    ADD CONSTRAINT faculties_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2436 (class 2606 OID 45994)
+-- Name: faculties faculties_uk_1; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY faculties
+    ADD CONSTRAINT faculties_uk_1 UNIQUE (externalid);
+
+
+--
+-- TOC entry 2438 (class 2606 OID 45996)
+-- Name: faculties faculties_uk_3; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY faculties
+    ADD CONSTRAINT faculties_uk_3 UNIQUE (abbr);
+
+
+--
+-- TOC entry 2445 (class 2606 OID 46033)
+-- Name: grades grade_2; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY grades
+    ADD CONSTRAINT grade_2 UNIQUE (num, degree);
+
+
+--
+-- TOC entry 2447 (class 2606 OID 46031)
+-- Name: grades grades_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY grades
+    ADD CONSTRAINT grades_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2550 (class 2606 OID 46545)
+-- Name: groups_years groups_years_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY groups_years
+    ADD CONSTRAINT groups_years_pkey PRIMARY KEY (year, groupid);
+
+
+--
+-- TOC entry 2471 (class 2606 OID 46189)
+-- Name: job_positions job_positions_name_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY job_positions
+    ADD CONSTRAINT job_positions_name_key UNIQUE (name);
+
+
+--
+-- TOC entry 2473 (class 2606 OID 46187)
+-- Name: job_positions job_positions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY job_positions
+    ADD CONSTRAINT job_positions_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2556 (class 2606 OID 46570)
+-- Name: logs_binds_groups logs_binds_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_groups
+    ADD CONSTRAINT logs_binds_groups_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2560 (class 2606 OID 46595)
+-- Name: logs_binds_students logs_binds_students_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_students
+    ADD CONSTRAINT logs_binds_students_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2564 (class 2606 OID 46620)
+-- Name: logs_rating logs_rating_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_rating
+    ADD CONSTRAINT logs_rating_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2570 (class 2606 OID 46645)
+-- Name: logs_signin logs_signin_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_signin
+    ADD CONSTRAINT logs_signin_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2534 (class 2606 OID 46496)
+-- Name: modules modules_ordernum_disciplineid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY modules
+    ADD CONSTRAINT modules_ordernum_disciplineid_key UNIQUE (ordernum, disciplineid);
+
+
+--
+-- TOC entry 2536 (class 2606 OID 46494)
+-- Name: modules modules_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY modules
+    ADD CONSTRAINT modules_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2572 (class 2606 OID 46658)
+-- Name: rating_table rating_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY rating_table
+    ADD CONSTRAINT rating_table_pkey PRIMARY KEY (recordbookid, submoduleid);
+
+
+--
+-- TOC entry 2505 (class 2606 OID 46364)
+-- Name: record_books record_books_externalid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books
+    ADD CONSTRAINT record_books_externalid_key UNIQUE (externalid);
+
+
+--
+-- TOC entry 2507 (class 2606 OID 46362)
+-- Name: record_books record_books_pk; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books
+    ADD CONSTRAINT record_books_pk PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2577 (class 2606 OID 46688)
+-- Name: record_books_plans record_books_plans_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books_plans
+    ADD CONSTRAINT record_books_plans_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2579 (class 2606 OID 46690)
+-- Name: record_books_plans recordbookid_studyplanid_record_books_plans_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books_plans
+    ADD CONSTRAINT recordbookid_studyplanid_record_books_plans_key UNIQUE (recordbookid, studyplanid);
+
+
+--
+-- TOC entry 2583 (class 2606 OID 46709)
+-- Name: recovery_tokens recovery_tokens_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY recovery_tokens
+    ADD CONSTRAINT recovery_tokens_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2585 (class 2606 OID 46711)
+-- Name: recovery_tokens recovery_tokens_token_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY recovery_tokens
+    ADD CONSTRAINT recovery_tokens_token_key UNIQUE (token);
+
+
+--
+-- TOC entry 2588 (class 2606 OID 46730)
+-- Name: requests requests_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY requests
+    ADD CONSTRAINT requests_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2590 (class 2606 OID 46746)
+-- Name: schema_version schema_version_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY schema_version
+    ADD CONSTRAINT schema_version_pkey PRIMARY KEY (installed_rank);
+
+
+--
+-- TOC entry 2466 (class 2606 OID 46132)
+-- Name: semesters semesters_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY semesters
+    ADD CONSTRAINT semesters_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2468 (class 2606 OID 46134)
+-- Name: semesters semesters_year_num_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY semesters
+    ADD CONSTRAINT semesters_year_num_key UNIQUE (year, num);
+
+
+--
+-- TOC entry 2441 (class 2606 OID 46007)
+-- Name: specializations specializations_name_facultyid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY specializations
+    ADD CONSTRAINT specializations_name_facultyid_key UNIQUE (name, facultyid);
+
+
+--
+-- TOC entry 2443 (class 2606 OID 46005)
+-- Name: specializations specializations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY specializations
+    ADD CONSTRAINT specializations_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2599 (class 2606 OID 46766)
+-- Name: students_groups students_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students_groups
+    ADD CONSTRAINT students_groups_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2601 (class 2606 OID 46768)
+-- Name: students_groups students_groups_semesterid_recordbookid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students_groups
+    ADD CONSTRAINT students_groups_semesterid_recordbookid_key UNIQUE (semesterid, recordbookid);
+
+
+--
+-- TOC entry 2594 (class 2606 OID 46753)
+-- Name: students students_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students
+    ADD CONSTRAINT students_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2603 (class 2606 OID 46799)
+-- Name: study_form study_form_formname_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_form
+    ADD CONSTRAINT study_form_formname_key UNIQUE (formname);
+
+
+--
+-- TOC entry 2605 (class 2606 OID 46797)
+-- Name: study_form study_form_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_form
+    ADD CONSTRAINT study_form_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2495 (class 2606 OID 46305)
+-- Name: study_groups study_groups_facultyid_gradeid_groupnum_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_groups
+    ADD CONSTRAINT study_groups_facultyid_gradeid_groupnum_key UNIQUE (facultyid, gradeid, groupnum);
+
+
+--
+-- TOC entry 2497 (class 2606 OID 46303)
+-- Name: study_groups study_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_groups
+    ADD CONSTRAINT study_groups_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2517 (class 2606 OID 46416)
+-- Name: study_plans study_plans_externalid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_plans
+    ADD CONSTRAINT study_plans_externalid_key UNIQUE (externalid);
+
+
+--
+-- TOC entry 2519 (class 2606 OID 46414)
+-- Name: study_plans study_plans_pk; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_plans
+    ADD CONSTRAINT study_plans_pk PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2609 (class 2606 OID 46805)
+-- Name: subjects_faculties subjects_faculties_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects_faculties
+    ADD CONSTRAINT subjects_faculties_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2611 (class 2606 OID 46807)
+-- Name: subjects_faculties subjects_faculties_subjectid_facultyid_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects_faculties
+    ADD CONSTRAINT subjects_faculties_subjectid_facultyid_key UNIQUE (subjectid, facultyid);
+
+
+--
+-- TOC entry 2460 (class 2606 OID 46094)
+-- Name: subjects subjects_name_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects
+    ADD CONSTRAINT subjects_name_key UNIQUE (name);
+
+
+--
+-- TOC entry 2462 (class 2606 OID 46092)
+-- Name: subjects subjects_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects
+    ADD CONSTRAINT subjects_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2539 (class 2606 OID 46513)
+-- Name: submodules submodules_moduleid_ordernum_key; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY submodules
+    ADD CONSTRAINT submodules_moduleid_ordernum_key UNIQUE (moduleid, ordernum);
+
+
+--
+-- TOC entry 2541 (class 2606 OID 46511)
+-- Name: submodules submodules_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY submodules
+    ADD CONSTRAINT submodules_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2479 (class 2606 OID 46196)
+-- Name: teachers teachers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY teachers
+    ADD CONSTRAINT teachers_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2613 (class 2606 OID 46825)
+-- Name: text_mark text_mark_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY text_mark
+    ADD CONSTRAINT text_mark_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2418 (class 2606 OID 45927)
+-- Name: user_roles user_roles_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY user_roles
+    ADD CONSTRAINT user_roles_pkey PRIMARY KEY (id);
+
+
+--
+-- TOC entry 2464 (class 2606 OID 46126)
+-- Name: years years_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY years
+    ADD CONSTRAINT years_pkey PRIMARY KEY (num);
+
+
+--
+-- TOC entry 2428 (class 1259 OID 45965)
+-- Name: accountid_auth_tokens; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_auth_tokens ON auth_tokens USING btree (accountid);
+
+
+--
+-- TOC entry 2568 (class 1259 OID 46651)
+-- Name: accountid_logs_signin; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_logs_signin ON logs_signin USING btree (accountid);
+
+
+--
+-- TOC entry 2581 (class 1259 OID 46717)
+-- Name: accountid_recovery_tokens; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_recovery_tokens ON recovery_tokens USING btree (accountid);
+
+
+--
+-- TOC entry 2586 (class 1259 OID 46736)
+-- Name: accountid_requests; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_requests ON requests USING btree (accountid);
+
+
+--
+-- TOC entry 2592 (class 1259 OID 46759)
+-- Name: accountid_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_students ON students USING btree (accountid);
+
+
+--
+-- TOC entry 2474 (class 1259 OID 46217)
+-- Name: accountid_teachers; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX accountid_teachers ON teachers USING btree (accountid);
+
+
+--
+-- TOC entry 2448 (class 1259 OID 46051)
+-- Name: compound_disciplines_ibfk_1; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX compound_disciplines_ibfk_1 ON compound_disciplines USING btree (specializationid);
+
+
+--
+-- TOC entry 2449 (class 1259 OID 46052)
+-- Name: compound_disciplines_ibfk_2; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX compound_disciplines_ibfk_2 ON compound_disciplines USING btree (gradeid);
+
+
+--
+-- TOC entry 2480 (class 1259 OID 46265)
+-- Name: compounddiscid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX compounddiscid_disciplines ON disciplines USING btree (compounddiscid);
+
+
+--
+-- TOC entry 2498 (class 1259 OID 46336)
+-- Name: disciplineid_disciplines_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_disciplines_groups ON disciplines_groups USING btree (disciplineid);
+
+
+--
+-- TOC entry 2509 (class 1259 OID 46384)
+-- Name: disciplineid_disciplines_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_disciplines_students ON disciplines_students USING btree (disciplineid);
+
+
+--
+-- TOC entry 2526 (class 1259 OID 46468)
+-- Name: disciplineid_disciplines_teachers; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_disciplines_teachers ON disciplines_teachers USING btree (disciplineid);
+
+
+--
+-- TOC entry 2553 (class 1259 OID 46586)
+-- Name: disciplineid_logs_binds_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_logs_binds_groups ON logs_binds_groups USING btree (disciplineid);
+
+
+--
+-- TOC entry 2558 (class 1259 OID 46611)
+-- Name: disciplineid_logs_binds_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_logs_binds_students ON logs_binds_students USING btree (disciplineid);
+
+
+--
+-- TOC entry 2532 (class 1259 OID 46502)
+-- Name: disciplineid_modules; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX disciplineid_modules ON modules USING btree (disciplineid);
+
+
+--
+-- TOC entry 2458 (class 1259 OID 46069)
+-- Name: facultyid_departments; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_departments ON departments USING btree (facultyid);
+
+
+--
+-- TOC entry 2483 (class 1259 OID 46266)
+-- Name: facultyid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_disciplines ON disciplines USING btree (facultyid);
+
+
+--
+-- TOC entry 2439 (class 1259 OID 46013)
+-- Name: facultyid_specializations; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_specializations ON specializations USING btree (facultyid);
+
+
+--
+-- TOC entry 2492 (class 1259 OID 46316)
+-- Name: facultyid_study_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_study_groups ON study_groups USING btree (facultyid);
+
+
+--
+-- TOC entry 2515 (class 1259 OID 46427)
+-- Name: facultyid_study_plans; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_study_plans ON study_plans USING btree (facultyid);
+
+
+--
+-- TOC entry 2606 (class 1259 OID 46818)
+-- Name: facultyid_subjects_faculties; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_subjects_faculties ON subjects_faculties USING btree (facultyid);
+
+
+--
+-- TOC entry 2475 (class 1259 OID 46218)
+-- Name: facultyid_teachers; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX facultyid_teachers ON teachers USING btree (departmentid);
+
+
+--
+-- TOC entry 2484 (class 1259 OID 46267)
+-- Name: gradeid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX gradeid_disciplines ON disciplines USING btree (gradeid);
+
+
+--
+-- TOC entry 2493 (class 1259 OID 46317)
+-- Name: gradeid_study_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX gradeid_study_groups ON study_groups USING btree (gradeid);
+
+
+--
+-- TOC entry 2503 (class 1259 OID 46337)
+-- Name: groupid_disciplines_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX groupid_disciplines_groups ON disciplines_groups USING btree (groupid);
+
+
+--
+-- TOC entry 2548 (class 1259 OID 46561)
+-- Name: groupid_groups_years; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX groupid_groups_years ON groups_years USING btree (groupid);
+
+
+--
+-- TOC entry 2554 (class 1259 OID 46587)
+-- Name: groupid_logs_binds_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX groupid_logs_binds_groups ON logs_binds_groups USING btree (groupid);
+
+
+--
+-- TOC entry 2595 (class 1259 OID 46784)
+-- Name: groupid_students_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX groupid_students_groups ON students_groups USING btree (groupid);
+
+
+--
+-- TOC entry 2476 (class 1259 OID 46219)
+-- Name: jobpositionid_teachers; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX jobpositionid_teachers ON teachers USING btree (jobpositionid);
+
+
+--
+-- TOC entry 2537 (class 1259 OID 46519)
+-- Name: moduleid_submodules; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX moduleid_submodules ON submodules USING btree (moduleid);
+
+
+--
+-- TOC entry 2514 (class 1259 OID 46385)
+-- Name: recordbookid_disciplines_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_disciplines_students ON disciplines_students USING btree (recordbookid);
+
+
+--
+-- TOC entry 2546 (class 1259 OID 46538)
+-- Name: recordbookid_exam_period_options; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_exam_period_options ON exam_period_options USING btree (recordbookid);
+
+
+--
+-- TOC entry 2561 (class 1259 OID 46612)
+-- Name: recordbookid_logs_binds_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_logs_binds_students ON logs_binds_students USING btree (recordbookid);
+
+
+--
+-- TOC entry 2565 (class 1259 OID 46636)
+-- Name: recordbookid_logs_rating; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_logs_rating ON logs_rating USING btree (recordbookid);
+
+
+--
+-- TOC entry 2573 (class 1259 OID 46674)
+-- Name: recordbookid_rating_table; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_rating_table ON rating_table USING btree (recordbookid);
+
+
+--
+-- TOC entry 2596 (class 1259 OID 46785)
+-- Name: recordbookid_students_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX recordbookid_students_groups ON students_groups USING btree (recordbookid);
+
+
+--
+-- TOC entry 2591 (class 1259 OID 46747)
+-- Name: schema_version_s_idx; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX schema_version_s_idx ON schema_version USING btree (success);
+
+
+--
+-- TOC entry 2485 (class 1259 OID 46268)
+-- Name: semesterid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX semesterid_disciplines ON disciplines USING btree (semesterid);
+
+
+--
+-- TOC entry 2597 (class 1259 OID 46786)
+-- Name: semesterid_students_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX semesterid_students_groups ON students_groups USING btree (semesterid);
+
+
+--
+-- TOC entry 2551 (class 1259 OID 46562)
+-- Name: specializationid_groups_years; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX specializationid_groups_years ON groups_years USING btree (specializationid);
+
+
+--
+-- TOC entry 2508 (class 1259 OID 46365)
+-- Name: studentid_record_books; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX studentid_record_books ON record_books USING btree (studentid);
+
+
+--
+-- TOC entry 2525 (class 1259 OID 46449)
+-- Name: studyplanid_disciplines_study_plans; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX studyplanid_disciplines_study_plans ON disciplines_study_plans USING btree (studyplanid);
+
+
+--
+-- TOC entry 2580 (class 1259 OID 46701)
+-- Name: studyplanid_record_books_plans; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX studyplanid_record_books_plans ON record_books_plans USING btree (studyplanid);
+
+
+--
+-- TOC entry 2486 (class 1259 OID 46269)
+-- Name: subjectid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX subjectid_disciplines ON disciplines USING btree (subjectid);
+
+
+--
+-- TOC entry 2607 (class 1259 OID 46819)
+-- Name: subjectid_subjects_faculties; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX subjectid_subjects_faculties ON subjects_faculties USING btree (subjectid);
+
+
+--
+-- TOC entry 2547 (class 1259 OID 46539)
+-- Name: submoduleid_exam_period_options; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX submoduleid_exam_period_options ON exam_period_options USING btree (submoduleid);
+
+
+--
+-- TOC entry 2566 (class 1259 OID 46637)
+-- Name: submoduleid_logs_rating; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX submoduleid_logs_rating ON logs_rating USING btree (submoduleid);
+
+
+--
+-- TOC entry 2574 (class 1259 OID 46675)
+-- Name: submoduleid_rating_table; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX submoduleid_rating_table ON rating_table USING btree (submoduleid);
+
+
+--
+-- TOC entry 2487 (class 1259 OID 46270)
+-- Name: teacherid_disciplines; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_disciplines ON disciplines USING btree (authorid);
+
+
+--
+-- TOC entry 2531 (class 1259 OID 46469)
+-- Name: teacherid_disciplines_teachers; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_disciplines_teachers ON disciplines_teachers USING btree (teacherid);
+
+
+--
+-- TOC entry 2557 (class 1259 OID 46588)
+-- Name: teacherid_logs_binds_groups; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_logs_binds_groups ON logs_binds_groups USING btree (teacherid);
+
+
+--
+-- TOC entry 2562 (class 1259 OID 46613)
+-- Name: teacherid_logs_binds_students; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_logs_binds_students ON logs_binds_students USING btree (teacherid);
+
+
+--
+-- TOC entry 2567 (class 1259 OID 46638)
+-- Name: teacherid_logs_rating; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_logs_rating ON logs_rating USING btree (teacherid);
+
+
+--
+-- TOC entry 2575 (class 1259 OID 46676)
+-- Name: teacherid_rating_table; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teacherid_rating_table ON rating_table USING btree (teacherid);
+
+
+--
+-- TOC entry 2477 (class 1259 OID 46220)
+-- Name: teachers_ibfk_4; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX teachers_ibfk_4 ON teachers USING btree (facultyid);
+
+
+--
+-- TOC entry 2427 (class 1259 OID 45955)
+-- Name: userroleid; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX userroleid ON accounts USING btree (userroleid);
+
+
+--
+-- TOC entry 2469 (class 1259 OID 46140)
+-- Name: year_2; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX year_2 ON semesters USING btree (year);
+
+
+--
+-- TOC entry 2552 (class 1259 OID 46563)
+-- Name: year_groups_years; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX year_groups_years ON groups_years USING btree (year);
+
+
+--
+-- TOC entry 2520 (class 1259 OID 46428)
+-- Name: year_study_plans; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX year_study_plans ON study_plans USING btree (year);
+
+
+--
+-- TOC entry 2614 (class 2606 OID 45950)
+-- Name: accounts accounts_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY accounts
+    ADD CONSTRAINT accounts_ibfk_1 FOREIGN KEY (userroleid) REFERENCES user_roles(id);
+
+
+--
+-- TOC entry 2616 (class 2606 OID 46041)
+-- Name: compound_disciplines compound_disciplines_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY compound_disciplines
+    ADD CONSTRAINT compound_disciplines_ibfk_1 FOREIGN KEY (specializationid) REFERENCES specializations(id);
+
+
+--
+-- TOC entry 2617 (class 2606 OID 46046)
+-- Name: compound_disciplines compound_disciplines_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY compound_disciplines
+    ADD CONSTRAINT compound_disciplines_ibfk_2 FOREIGN KEY (gradeid) REFERENCES grades(id);
+
+
+--
+-- TOC entry 2618 (class 2606 OID 46064)
+-- Name: departments departments_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY departments
+    ADD CONSTRAINT departments_ibfk_1 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2632 (class 2606 OID 46326)
+-- Name: disciplines_groups disciplines_groups_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_groups
+    ADD CONSTRAINT disciplines_groups_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2633 (class 2606 OID 46331)
+-- Name: disciplines_groups disciplines_groups_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_groups
+    ADD CONSTRAINT disciplines_groups_ibfk_2 FOREIGN KEY (groupid) REFERENCES study_groups(id);
+
+
+--
+-- TOC entry 2624 (class 2606 OID 46235)
+-- Name: disciplines disciplines_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_1 FOREIGN KEY (subjectid) REFERENCES subjects(id);
+
+
+--
+-- TOC entry 2625 (class 2606 OID 46240)
+-- Name: disciplines disciplines_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_2 FOREIGN KEY (semesterid) REFERENCES semesters(id);
+
+
+--
+-- TOC entry 2626 (class 2606 OID 46245)
+-- Name: disciplines disciplines_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_3 FOREIGN KEY (authorid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2627 (class 2606 OID 46250)
+-- Name: disciplines disciplines_ibfk_4; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_4 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2628 (class 2606 OID 46255)
+-- Name: disciplines disciplines_ibfk_5; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_5 FOREIGN KEY (gradeid) REFERENCES grades(id);
+
+
+--
+-- TOC entry 2629 (class 2606 OID 46260)
+-- Name: disciplines disciplines_ibfk_6; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines
+    ADD CONSTRAINT disciplines_ibfk_6 FOREIGN KEY (compounddiscid) REFERENCES compound_disciplines(id);
+
+
+--
+-- TOC entry 2634 (class 2606 OID 46374)
+-- Name: disciplines_students disciplines_students_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_students
+    ADD CONSTRAINT disciplines_students_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2635 (class 2606 OID 46379)
+-- Name: disciplines_students disciplines_students_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_students
+    ADD CONSTRAINT disciplines_students_ibfk_2 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2638 (class 2606 OID 46439)
+-- Name: disciplines_study_plans disciplines_study_plans_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_study_plans
+    ADD CONSTRAINT disciplines_study_plans_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2639 (class 2606 OID 46444)
+-- Name: disciplines_study_plans disciplines_study_plans_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_study_plans
+    ADD CONSTRAINT disciplines_study_plans_ibfk_2 FOREIGN KEY (studyplanid) REFERENCES study_plans(id);
+
+
+--
+-- TOC entry 2640 (class 2606 OID 46458)
+-- Name: disciplines_teachers disciplines_teachers_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_teachers
+    ADD CONSTRAINT disciplines_teachers_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2641 (class 2606 OID 46463)
+-- Name: disciplines_teachers disciplines_teachers_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY disciplines_teachers
+    ADD CONSTRAINT disciplines_teachers_ibfk_2 FOREIGN KEY (teacherid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2644 (class 2606 OID 46528)
+-- Name: exam_period_options exam_period_options_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY exam_period_options
+    ADD CONSTRAINT exam_period_options_ibfk_1 FOREIGN KEY (submoduleid) REFERENCES submodules(id);
+
+
+--
+-- TOC entry 2645 (class 2606 OID 46533)
+-- Name: exam_period_options exam_period_options_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY exam_period_options
+    ADD CONSTRAINT exam_period_options_ibfk_2 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2646 (class 2606 OID 46546)
+-- Name: groups_years groups_years_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY groups_years
+    ADD CONSTRAINT groups_years_ibfk_1 FOREIGN KEY (groupid) REFERENCES study_groups(id);
+
+
+--
+-- TOC entry 2647 (class 2606 OID 46551)
+-- Name: groups_years groups_years_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY groups_years
+    ADD CONSTRAINT groups_years_ibfk_2 FOREIGN KEY (year) REFERENCES years(num);
+
+
+--
+-- TOC entry 2648 (class 2606 OID 46556)
+-- Name: groups_years groups_years_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY groups_years
+    ADD CONSTRAINT groups_years_ibfk_3 FOREIGN KEY (specializationid) REFERENCES specializations(id);
+
+
+--
+-- TOC entry 2649 (class 2606 OID 46571)
+-- Name: logs_binds_groups logs_binds_groups_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_groups
+    ADD CONSTRAINT logs_binds_groups_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2650 (class 2606 OID 46576)
+-- Name: logs_binds_groups logs_binds_groups_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_groups
+    ADD CONSTRAINT logs_binds_groups_ibfk_2 FOREIGN KEY (teacherid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2651 (class 2606 OID 46581)
+-- Name: logs_binds_groups logs_binds_groups_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_groups
+    ADD CONSTRAINT logs_binds_groups_ibfk_3 FOREIGN KEY (groupid) REFERENCES study_groups(id);
+
+
+--
+-- TOC entry 2652 (class 2606 OID 46596)
+-- Name: logs_binds_students logs_binds_students_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_students
+    ADD CONSTRAINT logs_binds_students_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2653 (class 2606 OID 46601)
+-- Name: logs_binds_students logs_binds_students_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_students
+    ADD CONSTRAINT logs_binds_students_ibfk_2 FOREIGN KEY (teacherid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2654 (class 2606 OID 46606)
+-- Name: logs_binds_students logs_binds_students_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_binds_students
+    ADD CONSTRAINT logs_binds_students_ibfk_3 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2655 (class 2606 OID 46621)
+-- Name: logs_rating logs_rating_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_rating
+    ADD CONSTRAINT logs_rating_ibfk_1 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2656 (class 2606 OID 46626)
+-- Name: logs_rating logs_rating_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_rating
+    ADD CONSTRAINT logs_rating_ibfk_2 FOREIGN KEY (submoduleid) REFERENCES submodules(id);
+
+
+--
+-- TOC entry 2657 (class 2606 OID 46631)
+-- Name: logs_rating logs_rating_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_rating
+    ADD CONSTRAINT logs_rating_ibfk_3 FOREIGN KEY (teacherid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2658 (class 2606 OID 46646)
+-- Name: logs_signin logs_signin_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY logs_signin
+    ADD CONSTRAINT logs_signin_ibfk_1 FOREIGN KEY (accountid) REFERENCES accounts(id);
+
+
+--
+-- TOC entry 2642 (class 2606 OID 46497)
+-- Name: modules modules_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY modules
+    ADD CONSTRAINT modules_ibfk_1 FOREIGN KEY (disciplineid) REFERENCES disciplines(id);
+
+
+--
+-- TOC entry 2659 (class 2606 OID 46659)
+-- Name: rating_table rating_table_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY rating_table
+    ADD CONSTRAINT rating_table_ibfk_1 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2660 (class 2606 OID 46664)
+-- Name: rating_table rating_table_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY rating_table
+    ADD CONSTRAINT rating_table_ibfk_2 FOREIGN KEY (submoduleid) REFERENCES submodules(id);
+
+
+--
+-- TOC entry 2661 (class 2606 OID 46669)
+-- Name: rating_table rating_table_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY rating_table
+    ADD CONSTRAINT rating_table_ibfk_3 FOREIGN KEY (teacherid) REFERENCES teachers(id);
+
+
+--
+-- TOC entry 2662 (class 2606 OID 46691)
+-- Name: record_books_plans record_books_plans_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books_plans
+    ADD CONSTRAINT record_books_plans_ibfk_1 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2663 (class 2606 OID 46696)
+-- Name: record_books_plans record_books_plans_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY record_books_plans
+    ADD CONSTRAINT record_books_plans_ibfk_2 FOREIGN KEY (studyplanid) REFERENCES study_plans(id);
+
+
+--
+-- TOC entry 2664 (class 2606 OID 46712)
+-- Name: recovery_tokens recovery_tokens_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY recovery_tokens
+    ADD CONSTRAINT recovery_tokens_ibfk_1 FOREIGN KEY (accountid) REFERENCES accounts(id);
+
+
+--
+-- TOC entry 2665 (class 2606 OID 46731)
+-- Name: requests requests_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY requests
+    ADD CONSTRAINT requests_ibfk_1 FOREIGN KEY (accountid) REFERENCES accounts(id) ON UPDATE SET NULL ON DELETE SET NULL;
+
+
+--
+-- TOC entry 2619 (class 2606 OID 46135)
+-- Name: semesters semesters_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY semesters
+    ADD CONSTRAINT semesters_ibfk_1 FOREIGN KEY (year) REFERENCES years(num);
+
+
+--
+-- TOC entry 2615 (class 2606 OID 46008)
+-- Name: specializations specializations_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY specializations
+    ADD CONSTRAINT specializations_ibfk_1 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2667 (class 2606 OID 46769)
+-- Name: students_groups students_groups_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students_groups
+    ADD CONSTRAINT students_groups_ibfk_1 FOREIGN KEY (recordbookid) REFERENCES record_books(id);
+
+
+--
+-- TOC entry 2668 (class 2606 OID 46774)
+-- Name: students_groups students_groups_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students_groups
+    ADD CONSTRAINT students_groups_ibfk_2 FOREIGN KEY (groupid) REFERENCES study_groups(id);
+
+
+--
+-- TOC entry 2669 (class 2606 OID 46779)
+-- Name: students_groups students_groups_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students_groups
+    ADD CONSTRAINT students_groups_ibfk_3 FOREIGN KEY (semesterid) REFERENCES semesters(id);
+
+
+--
+-- TOC entry 2666 (class 2606 OID 46754)
+-- Name: students students_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY students
+    ADD CONSTRAINT students_ibfk_2 FOREIGN KEY (accountid) REFERENCES accounts(id);
+
+
+--
+-- TOC entry 2630 (class 2606 OID 46306)
+-- Name: study_groups study_groups_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_groups
+    ADD CONSTRAINT study_groups_ibfk_2 FOREIGN KEY (gradeid) REFERENCES grades(id);
+
+
+--
+-- TOC entry 2631 (class 2606 OID 46311)
+-- Name: study_groups study_groups_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_groups
+    ADD CONSTRAINT study_groups_ibfk_3 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2636 (class 2606 OID 46417)
+-- Name: study_plans study_plans_fk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_plans
+    ADD CONSTRAINT study_plans_fk_1 FOREIGN KEY (year) REFERENCES years(num);
+
+
+--
+-- TOC entry 2637 (class 2606 OID 46422)
+-- Name: study_plans study_plans_fk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY study_plans
+    ADD CONSTRAINT study_plans_fk_2 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2670 (class 2606 OID 46808)
+-- Name: subjects_faculties subjects_faculties_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects_faculties
+    ADD CONSTRAINT subjects_faculties_ibfk_1 FOREIGN KEY (facultyid) REFERENCES faculties(id);
+
+
+--
+-- TOC entry 2671 (class 2606 OID 46813)
+-- Name: subjects_faculties subjects_faculties_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY subjects_faculties
+    ADD CONSTRAINT subjects_faculties_ibfk_2 FOREIGN KEY (subjectid) REFERENCES subjects(id);
+
+
+--
+-- TOC entry 2643 (class 2606 OID 46514)
+-- Name: submodules submodules_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY submodules
+    ADD CONSTRAINT submodules_ibfk_1 FOREIGN KEY (moduleid) REFERENCES modules(id);
+
+
+--
+-- TOC entry 2620 (class 2606 OID 46197)
+-- Name: teachers teachers_ibfk_1; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY teachers
+    ADD CONSTRAINT teachers_ibfk_1 FOREIGN KEY (accountid) REFERENCES accounts(id);
+
+
+--
+-- TOC entry 2621 (class 2606 OID 46202)
+-- Name: teachers teachers_ibfk_2; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY teachers
+    ADD CONSTRAINT teachers_ibfk_2 FOREIGN KEY (departmentid) REFERENCES departments(id);
+
+
+--
+-- TOC entry 2622 (class 2606 OID 46207)
+-- Name: teachers teachers_ibfk_3; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY teachers
+    ADD CONSTRAINT teachers_ibfk_3 FOREIGN KEY (jobpositionid) REFERENCES job_positions(id);
+
+
+--
+-- TOC entry 2623 (class 2606 OID 46212)
+-- Name: teachers teachers_ibfk_4; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY teachers
+    ADD CONSTRAINT teachers_ibfk_4 FOREIGN KEY (facultyid) REFERENCES faculties(id);
diff --git a/db/postgresql/tables_and_views.sql b/db/postgresql/tables_and_views.sql
new file mode 100644
index 0000000000000000000000000000000000000000..4cf5f205c1f7ccbd665033333e47a36011f0d368
--- /dev/null
+++ b/db/postgresql/tables_and_views.sql
@@ -0,0 +1,1232 @@
+CREATE SEQUENCE seq_accounts
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+SET default_with_oids = false;
+
+--
+-- TOC entry 202 (class 1259 OID 45928)
+-- Name: accounts; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE accounts (
+    id integer DEFAULT nextval('seq_accounts'::regclass) NOT NULL,
+    inila character varying(40) DEFAULT NULL::character varying,
+    externalid character varying(9) DEFAULT NULL::character varying,
+    login character varying(50) DEFAULT NULL::character varying,
+    password character varying(64) DEFAULT NULL::character varying,
+    email character varying(255) DEFAULT NULL::character varying,
+    lastname character varying(30) NOT NULL,
+    firstname character varying(30) NOT NULL,
+    secondname character varying(30) NOT NULL,
+    userroleid integer NOT NULL,
+    activationcode character varying(40) DEFAULT NULL::character varying,
+    isenabled smallint DEFAULT (1)::smallint NOT NULL
+);
+
+
+--
+-- TOC entry 203 (class 1259 OID 45956)
+-- Name: auth_tokens; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE auth_tokens (
+    token character(40) NOT NULL,
+    accountid integer NOT NULL,
+    description character varying(60) DEFAULT ''::character varying NOT NULL,
+    created timestamp(0) without time zone DEFAULT now() NOT NULL,
+    accessed timestamp(0) without time zone DEFAULT '2000-01-01 00:00:00'::timestamp without time zone NOT NULL,
+    mask integer DEFAULT 0 NOT NULL
+);
+
+
+--
+-- TOC entry 171 (class 1259 OID 35310)
+-- Name: seq_compound_discip; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_compound_discip
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 207 (class 1259 OID 46034)
+-- Name: compound_disciplines; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE compound_disciplines (
+    id integer DEFAULT nextval('seq_compound_discip'::regclass) NOT NULL,
+    name character varying(200) DEFAULT 'Курс по выбору'::character varying,
+    gradeid integer NOT NULL,
+    specializationid integer NOT NULL
+);
+
+
+--
+-- TOC entry 172 (class 1259 OID 35313)
+-- Name: seq_departments; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_departments
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 208 (class 1259 OID 46053)
+-- Name: departments; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE departments (
+    id integer DEFAULT nextval('seq_departments'::regclass) NOT NULL,
+    name character varying(200) DEFAULT NULL::character varying,
+    facultyid integer NOT NULL,
+    externalid character varying(9)
+);
+
+
+--
+-- TOC entry 173 (class 1259 OID 35316)
+-- Name: seq_disciplines; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 214 (class 1259 OID 46221)
+-- Name: disciplines; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines (
+    id integer DEFAULT nextval('seq_disciplines'::regclass) NOT NULL,
+    externalid character varying(9) DEFAULT NULL::character varying,
+    gradeid integer,
+    subjectid integer NOT NULL,
+    authorid integer NOT NULL,
+    examtype exam_credit_grading_credit NOT NULL,
+    semesterid integer NOT NULL,
+    practicecount integer DEFAULT 0 NOT NULL,
+    lecturecount integer DEFAULT 0 NOT NULL,
+    labcount integer DEFAULT 0 NOT NULL,
+    facultyid integer NOT NULL,
+    milestone integer DEFAULT 0 NOT NULL,
+    milestonedate date,
+    subtype scientific_disciplinary_coursework,
+    compounddiscid integer,
+    maxrate integer DEFAULT 0 NOT NULL,
+    currate integer DEFAULT 0 NOT NULL,
+    islocked integer DEFAULT 0 NOT NULL
+);
+
+
+--
+-- TOC entry 215 (class 1259 OID 46275)
+-- Name: seq_disciplines_exam_type; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines_exam_type
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 216 (class 1259 OID 46277)
+-- Name: disciplines_exam_type; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines_exam_type (
+    id integer DEFAULT nextval('seq_disciplines_exam_type'::regclass) NOT NULL,
+    examtypename character varying(40) NOT NULL
+);
+
+
+--
+-- TOC entry 174 (class 1259 OID 35319)
+-- Name: seq_disciplines_groups; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines_groups
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 218 (class 1259 OID 46318)
+-- Name: disciplines_groups; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines_groups (
+    id integer DEFAULT nextval('seq_disciplines_groups'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    groupid integer NOT NULL
+);
+
+
+--
+-- TOC entry 175 (class 1259 OID 35322)
+-- Name: seq_disciplines_students; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines_students
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 221 (class 1259 OID 46366)
+-- Name: disciplines_students; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines_students (
+    id integer DEFAULT nextval('seq_disciplines_students'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    type attach_detach NOT NULL,
+    recordbookid integer
+);
+
+
+--
+-- TOC entry 224 (class 1259 OID 46429)
+-- Name: seq_disciplines_study_plans; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines_study_plans
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 225 (class 1259 OID 46431)
+-- Name: disciplines_study_plans; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines_study_plans (
+    id integer DEFAULT nextval('seq_disciplines_study_plans'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    studyplanid integer NOT NULL
+);
+
+
+--
+-- TOC entry 176 (class 1259 OID 35325)
+-- Name: seq_disciplines_teachers; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_disciplines_teachers
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 226 (class 1259 OID 46450)
+-- Name: disciplines_teachers; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE disciplines_teachers (
+    id integer DEFAULT nextval('seq_disciplines_teachers'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    teacherid integer NOT NULL
+);
+
+
+--
+-- TOC entry 177 (class 1259 OID 35328)
+-- Name: seq_exam_period_options; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_exam_period_options
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 229 (class 1259 OID 46520)
+-- Name: exam_period_options; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE exam_period_options (
+    id integer DEFAULT nextval('seq_exam_period_options'::regclass) NOT NULL,
+    submoduleid integer NOT NULL,
+    recordbookid integer,
+    type absence_pass
+);
+
+
+--
+-- TOC entry 178 (class 1259 OID 35331)
+-- Name: seq_faculties; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_faculties
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 204 (class 1259 OID 45984)
+-- Name: faculties; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE faculties (
+    id integer DEFAULT nextval('seq_faculties'::regclass) NOT NULL,
+    name character varying(100) NOT NULL,
+    abbr character varying(20) NOT NULL,
+    externalid character varying(9) DEFAULT NULL::character varying
+);
+
+
+--
+-- TOC entry 179 (class 1259 OID 35334)
+-- Name: seq_grades; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_grades
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 206 (class 1259 OID 46026)
+-- Name: grades; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE grades (
+    id integer DEFAULT nextval('seq_grades'::regclass) NOT NULL,
+    num integer NOT NULL,
+    degree bachelor_master_specialist NOT NULL
+);
+
+
+--
+-- TOC entry 230 (class 1259 OID 46540)
+-- Name: groups_years; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE groups_years (
+    groupid integer NOT NULL,
+    year integer NOT NULL,
+    specializationid integer NOT NULL,
+    name character varying(50) DEFAULT NULL::character varying
+);
+
+
+--
+-- TOC entry 180 (class 1259 OID 35337)
+-- Name: seq_job_positions; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_job_positions
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 212 (class 1259 OID 46182)
+-- Name: job_positions; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE job_positions (
+    id integer DEFAULT nextval('seq_job_positions'::regclass) NOT NULL,
+    name character varying(200) NOT NULL
+);
+
+
+--
+-- TOC entry 181 (class 1259 OID 35340)
+-- Name: seq_logs_binds_groups; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_logs_binds_groups
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 231 (class 1259 OID 46564)
+-- Name: logs_binds_groups; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE logs_binds_groups (
+    id integer DEFAULT nextval('seq_logs_binds_groups'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    teacherid integer NOT NULL,
+    groupid integer NOT NULL,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL,
+    type attach_detach NOT NULL
+);
+
+
+--
+-- TOC entry 182 (class 1259 OID 35343)
+-- Name: seq_logs_binds_students; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_logs_binds_students
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 232 (class 1259 OID 46589)
+-- Name: logs_binds_students; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE logs_binds_students (
+    id integer DEFAULT nextval('seq_logs_binds_students'::regclass) NOT NULL,
+    disciplineid integer NOT NULL,
+    teacherid integer,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL,
+    type attach_detach NOT NULL,
+    recordbookid integer
+);
+
+
+--
+-- TOC entry 183 (class 1259 OID 35346)
+-- Name: seq_logs_rating; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_logs_rating
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 233 (class 1259 OID 46614)
+-- Name: logs_rating; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE logs_rating (
+    id integer DEFAULT nextval('seq_logs_rating'::regclass) NOT NULL,
+    submoduleid integer NOT NULL,
+    teacherid integer NOT NULL,
+    rate integer NOT NULL,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL,
+    action add_change_delete NOT NULL,
+    recordbookid integer
+);
+
+
+--
+-- TOC entry 184 (class 1259 OID 35349)
+-- Name: seq_logs_signin; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_logs_signin
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 234 (class 1259 OID 46639)
+-- Name: logs_signin; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE logs_signin (
+    id integer DEFAULT nextval('seq_logs_signin'::regclass) NOT NULL,
+    accountid integer NOT NULL,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL
+);
+
+
+--
+-- TOC entry 200 (class 1259 OID 35604)
+-- Name: seq_modules; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_modules
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 227 (class 1259 OID 46489)
+-- Name: modules; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE modules (
+    id integer DEFAULT nextval('seq_modules'::regclass) NOT NULL,
+    name character varying(200) NOT NULL,
+    ordernum integer NOT NULL,
+    disciplineid integer NOT NULL,
+    type regular_exam_bonus_extra
+);
+
+
+--
+-- TOC entry 236 (class 1259 OID 46680)
+-- Name: seq_record_books_plans; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_record_books_plans
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 237 (class 1259 OID 46682)
+-- Name: record_books_plans; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE record_books_plans (
+    id integer DEFAULT nextval('seq_record_books_plans'::regclass) NOT NULL,
+    recordbookid integer NOT NULL,
+    studyplanid integer NOT NULL,
+    bindingtime timestamp(0) without time zone DEFAULT now() NOT NULL
+);
+
+
+--
+-- TOC entry 191 (class 1259 OID 35371)
+-- Name: seq_students_groups; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_students_groups
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 242 (class 1259 OID 46760)
+-- Name: students_groups; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE students_groups (
+    id integer DEFAULT nextval('seq_students_groups'::regclass) NOT NULL,
+    groupid integer NOT NULL,
+    semesterid integer NOT NULL,
+    date date,
+    state common_outlet_expulsion_leave DEFAULT 'common'::common_outlet_expulsion_leave NOT NULL,
+    recordbookid integer
+);
+
+
+--
+-- TOC entry 252 (class 1259 OID 46937)
+-- Name: private_view_disciplines_recordbooks_without_type; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW private_view_disciplines_recordbooks_without_type AS
+ SELECT DISTINCT disciplines_students.recordbookid,
+    disciplines_students.disciplineid,
+    disciplines.semesterid
+   FROM (disciplines_students
+     JOIN disciplines ON ((disciplines_students.disciplineid = disciplines.id)))
+UNION
+ SELECT DISTINCT students_groups.recordbookid,
+    disciplines_groups.disciplineid,
+    disciplines.semesterid
+   FROM ((disciplines_groups
+     JOIN disciplines ON ((disciplines_groups.disciplineid = disciplines.id)))
+     LEFT JOIN students_groups ON ((((students_groups.groupid = disciplines_groups.groupid) AND (students_groups.semesterid = disciplines.semesterid)) AND (students_groups.state <= 'outlet'::common_outlet_expulsion_leave))))
+  GROUP BY students_groups.recordbookid, disciplines_groups.disciplineid, disciplines.semesterid
+UNION
+ SELECT DISTINCT record_books_plans.recordbookid,
+    disciplines_study_plans.disciplineid,
+    disciplines.semesterid
+   FROM ((disciplines_study_plans
+     LEFT JOIN record_books_plans ON ((disciplines_study_plans.studyplanid = record_books_plans.studyplanid)))
+     JOIN disciplines ON ((disciplines_study_plans.disciplineid = disciplines.id)))
+  GROUP BY record_books_plans.recordbookid, disciplines_study_plans.disciplineid, disciplines.semesterid;
+
+
+--
+-- TOC entry 235 (class 1259 OID 46653)
+-- Name: rating_table; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE rating_table (
+    teacherid integer NOT NULL,
+    submoduleid integer NOT NULL,
+    rate integer NOT NULL,
+    date date NOT NULL,
+    recordbookid integer DEFAULT 0 NOT NULL
+);
+
+
+--
+-- TOC entry 219 (class 1259 OID 46354)
+-- Name: seq_record_books; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_record_books
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 220 (class 1259 OID 46356)
+-- Name: record_books; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE record_books (
+    id integer DEFAULT nextval('seq_record_books'::regclass) NOT NULL,
+    studentid integer NOT NULL,
+    externalid character varying(50) DEFAULT NULL::character varying
+);
+
+
+--
+-- TOC entry 185 (class 1259 OID 35352)
+-- Name: seq_recovery_tokens; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_recovery_tokens
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 238 (class 1259 OID 46702)
+-- Name: recovery_tokens; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE recovery_tokens (
+    id integer DEFAULT nextval('seq_recovery_tokens'::regclass) NOT NULL,
+    accountid integer NOT NULL,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL,
+    token character varying(100) NOT NULL,
+    isused smallint DEFAULT (0)::smallint NOT NULL
+);
+
+
+--
+-- TOC entry 187 (class 1259 OID 35359)
+-- Name: seq_requests; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_requests
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 239 (class 1259 OID 46718)
+-- Name: requests; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE requests (
+    id integer DEFAULT nextval('seq_requests'::regclass) NOT NULL,
+    accountid integer,
+    title character varying(50) DEFAULT NULL::character varying,
+    description text,
+    date timestamp(0) without time zone DEFAULT now() NOT NULL,
+    status opened_processed_closed DEFAULT 'opened'::opened_processed_closed,
+    hasimage integer DEFAULT 0 NOT NULL
+);
+
+
+--
+-- TOC entry 240 (class 1259 OID 46737)
+-- Name: schema_version; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE schema_version (
+    installed_rank integer NOT NULL,
+    version character varying(50) DEFAULT NULL::character varying,
+    description character varying(200) NOT NULL,
+    type character varying(20) NOT NULL,
+    script character varying(1000) NOT NULL,
+    checksum integer,
+    installed_by character varying(100) NOT NULL,
+    installed_on timestamp(0) without time zone DEFAULT now() NOT NULL,
+    execution_time integer NOT NULL,
+    success smallint NOT NULL
+);
+
+
+--
+-- TOC entry 188 (class 1259 OID 35362)
+-- Name: seq_semesters; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_semesters
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 211 (class 1259 OID 46127)
+-- Name: semesters; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE semesters (
+    id integer DEFAULT nextval('seq_semesters'::regclass) NOT NULL,
+    year integer NOT NULL,
+    num integer NOT NULL
+);
+
+
+--
+-- TOC entry 186 (class 1259 OID 35357)
+-- Name: seq_report_params; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_report_params
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 189 (class 1259 OID 35365)
+-- Name: seq_specializations; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_specializations
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 190 (class 1259 OID 35368)
+-- Name: seq_students; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_students
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 243 (class 1259 OID 46790)
+-- Name: seq_study_form; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_study_form
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 192 (class 1259 OID 35374)
+-- Name: seq_study_groups; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_study_groups
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 222 (class 1259 OID 46407)
+-- Name: seq_study_plans; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_study_plans
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 193 (class 1259 OID 35377)
+-- Name: seq_subjects; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_subjects
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 194 (class 1259 OID 35380)
+-- Name: seq_subjects_faculties; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_subjects_faculties
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 195 (class 1259 OID 35384)
+-- Name: seq_submodules; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_submodules
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 196 (class 1259 OID 35387)
+-- Name: seq_teachers; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_teachers
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 197 (class 1259 OID 35390)
+-- Name: seq_text_mark; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_text_mark
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 198 (class 1259 OID 35393)
+-- Name: seq_user_roles; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE seq_user_roles
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- TOC entry 205 (class 1259 OID 45997)
+-- Name: specializations; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE specializations (
+    id integer DEFAULT nextval('seq_specializations'::regclass) NOT NULL,
+    name character varying(200) DEFAULT NULL::character varying,
+    abbr character varying(20) DEFAULT NULL::character varying,
+    code character varying(12) DEFAULT NULL::character varying,
+    facultyid integer NOT NULL
+);
+
+
+--
+-- TOC entry 241 (class 1259 OID 46748)
+-- Name: students; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE students (
+    id integer DEFAULT nextval('seq_students'::regclass) NOT NULL,
+    accountid integer NOT NULL
+);
+
+
+--
+-- TOC entry 244 (class 1259 OID 46792)
+-- Name: study_form; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE study_form (
+    id integer DEFAULT nextval('seq_study_form'::regclass) NOT NULL,
+    formname character varying(40) NOT NULL
+);
+
+
+--
+-- TOC entry 217 (class 1259 OID 46298)
+-- Name: study_groups; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE study_groups (
+    id integer DEFAULT nextval('seq_study_groups'::regclass) NOT NULL,
+    gradeid integer NOT NULL,
+    groupnum integer NOT NULL,
+    facultyid integer NOT NULL,
+    formid integer
+);
+
+
+--
+-- TOC entry 223 (class 1259 OID 46409)
+-- Name: study_plans; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE study_plans (
+    id integer DEFAULT nextval('seq_study_plans'::regclass) NOT NULL,
+    externalid integer NOT NULL,
+    year integer,
+    facultyid integer
+);
+
+
+--
+-- TOC entry 209 (class 1259 OID 46084)
+-- Name: subjects; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE subjects (
+    id integer DEFAULT nextval('seq_subjects'::regclass) NOT NULL,
+    externalid character varying(9) DEFAULT NULL::character varying,
+    typeid integer DEFAULT (-1),
+    name character varying(200) NOT NULL,
+    abbr character varying(20) DEFAULT NULL::character varying
+);
+
+
+--
+-- TOC entry 245 (class 1259 OID 46800)
+-- Name: subjects_faculties; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE subjects_faculties (
+    id integer DEFAULT nextval('seq_subjects_faculties'::regclass) NOT NULL,
+    subjectid integer NOT NULL,
+    facultyid integer NOT NULL
+);
+
+
+--
+-- TOC entry 228 (class 1259 OID 46503)
+-- Name: submodules; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE submodules (
+    id integer DEFAULT nextval('seq_submodules'::regclass) NOT NULL,
+    moduleid integer NOT NULL,
+    maxrate integer NOT NULL,
+    ordernum integer NOT NULL,
+    name character varying(200) NOT NULL,
+    description character varying(200) DEFAULT NULL::character varying,
+    isused smallint DEFAULT (0)::smallint NOT NULL,
+    type current_landmark_control DEFAULT 'CurrentControl'::current_landmark_control NOT NULL
+);
+
+
+--
+-- TOC entry 213 (class 1259 OID 46190)
+-- Name: teachers; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE teachers (
+    id integer DEFAULT nextval('seq_teachers'::regclass) NOT NULL,
+    jobpositionid integer NOT NULL,
+    departmentid integer,
+    accountid integer NOT NULL,
+    status integer DEFAULT 1,
+    facultyid integer NOT NULL
+);
+
+
+--
+-- TOC entry 246 (class 1259 OID 46820)
+-- Name: text_mark; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE text_mark (
+    id integer DEFAULT nextval('seq_text_mark'::regclass) NOT NULL,
+    name character varying(100) NOT NULL
+);
+
+
+--
+-- TOC entry 201 (class 1259 OID 45922)
+-- Name: user_roles; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE user_roles (
+    id integer DEFAULT nextval('seq_user_roles'::regclass) NOT NULL,
+    type student_teacher NOT NULL,
+    rolename character varying(30) NOT NULL,
+    mark integer NOT NULL
+);
+
+
+--
+-- TOC entry 247 (class 1259 OID 46889)
+-- Name: view_disciplines; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_disciplines AS
+ SELECT disciplines.id AS disciplineid,
+    disciplines.authorid,
+    disciplines.examtype,
+    disciplines.lecturecount,
+    disciplines.practicecount,
+    disciplines.labcount,
+    disciplines.semesterid,
+    disciplines.islocked,
+    disciplines.milestone,
+    disciplines.subtype,
+    disciplines.compounddiscid,
+    disciplines.maxrate,
+    disciplines.currate,
+    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,
+    compound_disciplines.name AS compounddiscname
+   FROM ((((disciplines
+     JOIN subjects ON ((subjects.id = disciplines.subjectid)))
+     JOIN faculties ON ((faculties.id = disciplines.facultyid)))
+     LEFT JOIN grades ON ((grades.id = disciplines.gradeid)))
+     LEFT JOIN compound_disciplines ON ((compound_disciplines.id = disciplines.compounddiscid)));
+
+
+--
+-- TOC entry 253 (class 1259 OID 46947)
+-- Name: view_disciplines_recordbooks; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_disciplines_recordbooks AS
+ SELECT private_view_disciplines_recordbooks_without_type.recordbookid,
+    private_view_disciplines_recordbooks_without_type.disciplineid,
+    private_view_disciplines_recordbooks_without_type.semesterid,
+    disciplines_students.type
+   FROM ((private_view_disciplines_recordbooks_without_type
+     JOIN record_books ON ((private_view_disciplines_recordbooks_without_type.recordbookid = record_books.id)))
+     LEFT JOIN disciplines_students ON (((private_view_disciplines_recordbooks_without_type.recordbookid = disciplines_students.recordbookid) AND (private_view_disciplines_recordbooks_without_type.disciplineid = disciplines_students.disciplineid))));
+
+
+--
+-- TOC entry 249 (class 1259 OID 46899)
+-- Name: view_groups; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_groups AS
+ SELECT study_groups.id AS groupid,
+    study_groups.groupnum,
+    groups_years.name AS groupname,
+    grades.id AS gradeid,
+    grades.num AS gradenum,
+    grades.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,
+    groups_years.year
+   FROM ((((groups_years
+     JOIN study_groups ON ((groups_years.groupid = study_groups.id)))
+     JOIN specializations ON ((groups_years.specializationid = specializations.id)))
+     JOIN grades ON ((study_groups.gradeid = grades.id)))
+     JOIN faculties ON ((faculties.id = specializations.facultyid)));
+
+
+--
+-- TOC entry 248 (class 1259 OID 46894)
+-- Name: view_groups_reduced; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_groups_reduced AS
+ SELECT study_groups.id AS groupid,
+    study_groups.groupnum,
+    groups_years.name AS groupname,
+    grades.id AS gradeid,
+    grades.num AS gradenum,
+    grades.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
+   FROM (((((groups_years
+     LEFT JOIN groups_years t ON (((groups_years.groupid = t.groupid) AND (groups_years.year < t.year))))
+     JOIN study_groups ON ((groups_years.groupid = study_groups.id)))
+     JOIN specializations ON ((groups_years.specializationid = specializations.id)))
+     JOIN grades ON ((study_groups.gradeid = grades.id)))
+     JOIN faculties ON ((faculties.id = specializations.facultyid)))
+  WHERE (t.groupid IS NULL);
+
+
+--
+-- TOC entry 254 (class 1259 OID 46958)
+-- Name: view_roadmap; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_roadmap AS
+ SELECT modules.disciplineid,
+    modules.id AS moduleid,
+    modules.name AS modulename,
+    modules.ordernum AS moduleordernum,
+    modules.type AS moduletype,
+    submodules.id AS submoduleid,
+    submodules.name AS submodulename,
+    submodules.ordernum AS submoduleordernum,
+    submodules.maxrate AS submodulerate,
+    submodules.type AS submoduletype,
+    submodules.isused AS submoduleisused
+   FROM (modules
+     LEFT JOIN submodules ON ((submodules.moduleid = modules.id)));
+
+
+--
+-- TOC entry 251 (class 1259 OID 46909)
+-- Name: view_students; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_students AS
+ SELECT students.id AS studentid,
+    accounts.externalid,
+    accounts.lastname,
+    accounts.firstname,
+    accounts.secondname,
+    students.accountid,
+    record_books.id AS recordbookid,
+    record_books.externalid AS recordbooknumber,
+    students_groups.semesterid,
+    view_groups_reduced.groupid,
+    view_groups_reduced.groupnum,
+    view_groups_reduced.groupname,
+    view_groups_reduced.gradeid,
+    view_groups_reduced.gradenum,
+    view_groups_reduced.degree,
+    view_groups_reduced.specid,
+    view_groups_reduced.specname,
+    view_groups_reduced.specabbr,
+    view_groups_reduced.speccode,
+    view_groups_reduced.facultyid,
+    view_groups_reduced.facultyname,
+    view_groups_reduced.facultyabbr
+   FROM ((((students
+     LEFT JOIN accounts ON ((accounts.id = students.accountid)))
+     LEFT JOIN record_books ON ((record_books.studentid = students.id)))
+     LEFT JOIN students_groups ON ((students_groups.recordbookid = record_books.id)))
+     LEFT JOIN view_groups_reduced ON ((view_groups_reduced.groupid = students_groups.groupid)));
+
+
+--
+-- TOC entry 250 (class 1259 OID 46904)
+-- Name: view_teachers; Type: VIEW; Schema: public; Owner: -
+--
+
+CREATE VIEW view_teachers AS
+ SELECT teachers.id AS teacherid,
+    accounts.lastname,
+    accounts.firstname,
+    accounts.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
+     LEFT JOIN accounts ON ((teachers.accountid = accounts.id)))
+     LEFT JOIN departments ON ((departments.id = teachers.departmentid)))
+     JOIN faculties ON ((teachers.facultyid = faculties.id)))
+     JOIN job_positions ON ((job_positions.id = teachers.jobpositionid)));
+
+
+--
+-- TOC entry 210 (class 1259 OID 46122)
+-- Name: years; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE years (
+    num integer NOT NULL
+);
diff --git a/db/postgresql/types.sql b/db/postgresql/types.sql
new file mode 100644
index 0000000000000000000000000000000000000000..b39c1190259e410a005659dc9450083aa3f51b07
--- /dev/null
+++ b/db/postgresql/types.sql
@@ -0,0 +1,153 @@
+CREATE TYPE absence_pass AS ENUM (
+    'absence',
+    'pass'
+);
+
+--ALTER TYPE absence_pass;
+
+CREATE TYPE add_change_delete AS ENUM (
+    'add',
+    'change',
+    'delete'
+);
+
+--ALTER TYPE add_change_delete;
+
+CREATE TYPE attach_detach AS ENUM (
+    'attach',
+    'detach'
+);
+
+--ALTER TYPE attach_detach;
+
+CREATE TYPE bachelor_master_specialist AS ENUM (
+    'bachelor',
+    'master',
+    'specialist'
+);
+
+--ALTER TYPE bachelor_master_specialist;
+
+CREATE TYPE common_outlet_expulsion_leave AS ENUM (
+    'common',
+    'outlet',
+    'expulsion',
+    'leave'
+);
+
+--ALTER TYPE common_outlet_expulsion_leave;
+
+CREATE TYPE current_landmark_control AS ENUM (
+    'CurrentControl',
+    'LandmarkControl'
+);
+
+--ALTER TYPE current_landmark_control;
+
+CREATE TYPE exam_credit_grading_credit AS ENUM (
+    'exam',
+    'credit',
+    'grading_credit'
+);
+
+--ALTER TYPE exam_credit_grading_credit;
+
+CREATE TYPE exam_rate_all AS ENUM (
+    'exam',
+    'rate',
+    'all'
+);
+
+--ALTER TYPE exam_rate_all;
+
+CREATE TYPE grade_subject_author_semester_milestone_type_maxrate AS ENUM (
+    'grade',
+    'subject',
+    'author',
+    'semester',
+    'milestone',
+    'type',
+    'maxrate'
+);
+
+--ALTER TYPE grade_subject_author_semester_milestone_type_maxrate;
+
+CREATE TYPE group_student AS ENUM (
+    'group',
+    'student'
+);
+
+--ALTER TYPE group_student;
+
+CREATE TYPE login_email_code AS ENUM (
+    'login',
+    'email',
+    'code'
+);
+
+--ALTER TYPE login_email_code;
+
+CREATE TYPE login_email_password AS ENUM (
+    'login',
+    'email',
+    'password'
+);
+
+--ALTER TYPE login_email_password;
+
+CREATE TYPE none_src_dst AS ENUM (
+    'none',
+    'src',
+    'dst'
+);
+
+--ALTER TYPE none_src_dst;
+
+CREATE TYPE opened_processed_closed AS ENUM (
+    'opened',
+    'processed',
+    'closed'
+);
+
+--ALTER TYPE opened_processed_closed;
+
+CREATE TYPE opened_processed_closed_all AS ENUM (
+    'opened',
+    'processed',
+    'closed',
+    'all'
+);
+
+--ALTER TYPE opened_processed_closed_all;
+
+CREATE TYPE regular_exam_bonus_extra AS ENUM (
+    'regular',
+    'exam',
+    'bonus',
+    'extra'
+);
+
+--ALTER TYPE regular_exam_bonus_extra;
+
+CREATE TYPE scientific_disciplinary_coursework AS ENUM (
+    'scientific_coursework',
+    'disciplinary_coursework'
+);
+
+--ALTER TYPE scientific_disciplinary_coursework;
+
+CREATE TYPE student_teacher AS ENUM (
+    'student',
+    'teacher'
+);
+
+--ALTER TYPE student_teacher;
+
+CREATE TYPE student_teacher_admin_dean AS ENUM (
+    'student',
+    'teacher',
+    'admin',
+    'dean'
+);
+
+CREATE TYPE noukd_norate_full AS ENUM ('noukd', 'norate', 'full');
\ No newline at end of file