diff --git a/db/postgresql/add_rate_sync_logs_15_01_18.sql b/db/postgresql/add_rate_sync_logs_15_01_18.sql index 5b4024477ed1791cb7d7bab9051ee207d7154ccb..d5587acaeb5d71ab3dd7ab29cdc410d3b82b3a2a 100644 --- a/db/postgresql/add_rate_sync_logs_15_01_18.sql +++ b/db/postgresql/add_rate_sync_logs_15_01_18.sql @@ -10,6 +10,7 @@ CREATE SEQUENCE seq_logs_form_export CREATE TABLE logs_form_export ( id integer DEFAULT nextval('seq_logs_form_export'::regclass) NOT NULL, discipline_id integer NOT NULL, + group_id integer NOT NULL, account_id integer NOT NULL, date timestamp(0) without time zone DEFAULT now() NOT NULL ); @@ -19,19 +20,29 @@ CREATE INDEX key_logs_form_export ON logs_form_export USING btree (discipline_id ALTER TABLE ONLY logs_form_export ADD CONSTRAINT logs_form_export_pkey PRIMARY KEY (id); -CREATE OR REPLACE FUNCTION public.log_form_export(paccountid integer, pdisciplineid integer) +ALTER TABLE ONLY logs_form_export + ADD CONSTRAINT logs_form_export_ibfk_1 FOREIGN KEY (discipline_id) REFERENCES disciplines(id); + +ALTER TABLE ONLY logs_form_export + ADD CONSTRAINT logs_form_export_ibfk_2 FOREIGN KEY (group_id) REFERENCES study_groups(id); + +ALTER TABLE ONLY logs_form_export + ADD CONSTRAINT logs_form_export_ibfk_3 FOREIGN KEY (account_id) REFERENCES accounts(id); + +DROP FUNCTION IF EXISTS public.log_form_export(paccountid integer, pdisciplineid integer, pgroupid integer); +CREATE OR REPLACE FUNCTION public.log_form_export(paccountid integer, pdisciplineid integer, pgroupid integer) RETURNS integer LANGUAGE plpgsql AS $function$ begin - INSERT INTO logs_form_export (discipline_id, account_id) VALUES (pdisciplineid, paccountid); + INSERT INTO logs_form_export (discipline_id, group_id, account_id) VALUES (pdisciplineid, pgroupid, paccountid); RETURN 0; END $function$; -drop function if exists public.get_form_export_date(pdisciplineid integer); -CREATE OR REPLACE FUNCTION public.get_form_export_date(pdisciplineid integer) - RETURNS integer +drop function if exists public.get_form_export_date(pdisciplineid integer, pgroupid integer); +CREATE OR REPLACE FUNCTION public.get_form_export_date(pdisciplineid integer, pgroupid integer) + RETURNS DATE LANGUAGE plpgsql AS $function$ DECLARE vLastDate DATE DEFAULT NULL; @@ -39,8 +50,41 @@ begin SELECT logs_form_export.date INTO vLastDate FROM logs_form_export WHERE logs_form_export.discipline_id = pdisciplineid + AND logs_form_export.group_id = pgroupid ORDER BY logs_form_export.date DESC LIMIT 1; return vLastDate; END +$function$; + +drop function if exists public.is_form_outdated(pdisciplineid integer, pgroupid integer, psemesterid integer); +CREATE OR REPLACE FUNCTION public.is_form_outdated(pdisciplineid integer, pgroupid integer, psemesterid integer) + RETURNS integer +LANGUAGE plpgsql +AS $function$ +DECLARE vLastDate DATE DEFAULT NULL; + vCountNewGrades integer DEFAULT 0; +begin + SELECT logs_form_export.date INTO vLastDate + FROM logs_form_export + WHERE logs_form_export.discipline_id = pdisciplineid + AND logs_form_export.group_id = pgroupid + ORDER BY logs_form_export.date DESC + LIMIT 1; + + SELECT count(logs_rating.date) INTO vCountNewGrades FROM + logs_rating JOIN students_groups + ON logs_rating.recordbookid = students_groups.recordbookid + JOIN submodules ON logs_rating.submoduleid = submodules.id + JOIN modules ON submodules.moduleid = modules.id + JOIN disciplines ON modules.disciplineid = disciplines.id + WHERE students_groups.semesterid = psemesterid + AND students_groups.groupid = pgroupid + AND disciplines.id = pdisciplineid + AND logs_rating.date > vLastDate + group by logs_rating.date + ORDER BY logs_rating.date DESC; + + return vCountNewGrades; +END $function$; \ No newline at end of file