-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modified student lesson summaries to include enrollments, updat…
…ed trigger
- Loading branch information
1 parent
738491a
commit 7dcb52f
Showing
3 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
db/migrate/20240716100102_update_student_lesson_summaries_with_enrollments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class UpdateStudentLessonSummariesWithEnrollments < ActiveRecord::Migration[7.1] | ||
def up | ||
drop_view :group_lesson_summaries | ||
drop_view :lesson_table_rows | ||
|
||
update_view :student_lesson_summaries, version: 7 | ||
|
||
create_view :group_lesson_summaries, version: 4 | ||
create_view :lesson_table_rows, version: 3 | ||
update_trigger | ||
end | ||
|
||
def down | ||
drop_view :group_lesson_summaries | ||
drop_view :lesson_table_rows | ||
|
||
update_view :student_lesson_summaries, version: 6 | ||
|
||
create_view :group_lesson_summaries, version: 4 | ||
create_view :lesson_table_rows, version: 3 | ||
old_trigger | ||
end | ||
|
||
def update_trigger | ||
execute <<~SQL | ||
create or replace function update_enrollments() | ||
returns trigger AS | ||
$BODY$ | ||
declare | ||
current_enrollment_group_id int := null; | ||
BEGIN | ||
SELECT group_id into current_enrollment_group_id FROM enrollments e where e.student_id = new.id and e.inactive_since is null; | ||
if current_enrollment_group_id is null then | ||
insert into enrollments (student_id, group_id, active_since, inactive_since, created_at, updated_at) | ||
values (new.id, new.group_id, now(), null, now(), now()); | ||
else if current_enrollment_group_id != new.group_id then | ||
if (SELECT group_id from enrollments e where e.student_id = new.id and e.group_id = new.group_id) is null then | ||
insert into enrollments (student_id, group_id, active_since, inactive_since, created_at, updated_at) | ||
values (new.id, new.group_id, now(), null, now(), now()); | ||
else | ||
update enrollments set inactive_since = null, active_since = now(), updated_at = now() | ||
where group_id = new.group_id and student_id = new.id; | ||
end if; | ||
update enrollments set inactive_since = now(), updated_at = now() | ||
where inactive_since is null and group_id = current_enrollment_group_id and student_id = new.id; | ||
end if; | ||
end if; | ||
return new; | ||
END; | ||
$BODY$ language plpgsql; | ||
drop trigger if exists update_enrollments_on_student_group_change_trigger on students; | ||
create trigger update_enrollments_on_student_group_change_trigger | ||
after insert or update on students for each row execute procedure update_enrollments(); | ||
SQL | ||
end | ||
|
||
def old_trigger | ||
execute <<~SQL | ||
create or replace function update_enrollments() | ||
returns trigger AS | ||
$BODY$ | ||
declare | ||
current_enrollment_group_id int := null; | ||
BEGIN | ||
SELECT group_id into current_enrollment_group_id FROM enrollments e where e.student_id = new.id; | ||
if current_enrollment_group_id is null or current_enrollment_group_id != new.group_id then | ||
update enrollments set inactive_since = now() where inactive_since is null; | ||
insert into enrollments (student_id, group_id, active_since, inactive_since, created_at, updated_at) | ||
values (new.id, new.group_id, now(), null, now(), now()); | ||
end if; | ||
return new; | ||
END; | ||
$BODY$ language plpgsql; | ||
drop trigger if exists update_enrollments_on_student_group_change_trigger on students; | ||
create trigger update_enrollments_on_student_group_change_trigger | ||
after insert or update on students for each row execute procedure update_enrollments(); | ||
SQL | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
SELECT united.*, skill_count | ||
FROM ( | ||
SELECT s.id AS student_id, | ||
s.group_id AS group_id, | ||
s.first_name AS first_name, | ||
s.last_name AS last_name, | ||
s.deleted_at AS deleted_at, | ||
l.id AS lesson_id, | ||
l.date AS lesson_date, | ||
l.subject_id AS subject_id, | ||
round(AVG(mark), 2) AS average_mark, | ||
COUNT(mark) AS grade_count | ||
FROM students s | ||
JOIN groups g ON g.id = s.group_id | ||
JOIN lessons l ON g.id = l.group_id | ||
LEFT JOIN grades ON (grades.student_id = s.id AND grades.lesson_id = l.id AND grades.deleted_at IS NULL) | ||
GROUP BY s.id, l.id | ||
UNION | ||
SELECT s.id AS student_id, | ||
s.group_id AS group_id, | ||
s.first_name AS first_name, | ||
s.last_name AS last_name, | ||
s.deleted_at AS deleted_at, | ||
l.id AS lesson_id, | ||
l.date AS lesson_date, | ||
l.subject_id AS subject_id, | ||
round(AVG(mark), 2) AS average_mark, | ||
COUNT(mark) AS grade_count | ||
FROM lessons l | ||
JOIN groups g ON g.id = l.group_id | ||
JOIN grades ON (grades.lesson_id = l.id AND grades.deleted_at IS NULL) | ||
JOIN students s ON grades.student_id = s.id | ||
JOIN enrollments en ON s.id = en.student_id | ||
WHERE en.active_since::date <= l.date AND ((en.inactive_since IS NULL) OR (en.inactive_since::date >= l.date)) | ||
GROUP BY s.id, l.id | ||
) united JOIN subject_summaries su on united.subject_id = su.id |