Skip to content

Commit

Permalink
fix: Fix empty submisisons creation for team assessments (#1202)
Browse files Browse the repository at this point in the history
* feat: Implement unique index for team-assessment in submissions

* fix: Fix creation of empty submission for teams

* chore: Negate the if statement

* chore: Format

* Let Elixir do its magic

* fix: Duplicate team submissions being created

---------

Co-authored-by: Richard Dominick <[email protected]>
  • Loading branch information
GabrielCWT and RichDom2185 authored Oct 14, 2024
1 parent 945db2c commit d7c147f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/cadet/assessments/submission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule Cadet.Assessments.Submission do
|> validate_xor_relationship
|> validate_required(@required_fields)
|> foreign_key_constraint(:student_id)
|> foreign_key_constraint(:team_id)
|> foreign_key_constraint(:assessment_id)
|> foreign_key_constraint(:unsubmitted_by_id)
end
Expand Down
52 changes: 35 additions & 17 deletions lib/cadet/jobs/autograder/grading_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,36 +112,54 @@ defmodule Cadet.Autograder.GradingJob do
|> where([_, tm], tm.student_id == ^student_id)
|> Repo.one()

if !team do
# Student is not in any team
# Create new team just for the student
team =
%Team{}
|> Team.changeset(%{
assessment_id: assessment.id
team =
if team do
team
else
# Student is not in any team
# Create new team just for the student
team =
%Team{}
|> Team.changeset(%{
assessment_id: assessment.id
})
|> Repo.insert!()

%TeamMember{}
|> TeamMember.changeset(%{
team_id: team.id,
student_id: student_id
})
|> Repo.insert!()

%TeamMember{}
|> TeamMember.changeset(%{
team_id: team.id,
student_id: student_id
})
|> Repo.insert!()
end
team
end

find_or_create_team_submission(team.id, assessment)
else
# Individual assessment
%Submission{}
|> Submission.changeset(%{
team_id: team.id,
student_id: student_id,
assessment: assessment,
status: :submitted
})
|> Repo.insert!()
end
end

defp find_or_create_team_submission(team_id, assessment) when is_ecto_id(team_id) do
submission =
Submission
|> where(team_id: ^team_id, assessment_id: ^assessment.id)
|> Repo.one()

if submission do
submission
else
# Individual assessment
%Submission{}
|> Submission.changeset(%{
student_id: student_id,
team_id: team_id,
assessment: assessment,
status: :submitted
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Cadet.Repo.Migrations.CreateTeamsSubmissionConstraint do
use Ecto.Migration

def change do
create(index(:submissions, :team_id))
create(unique_index(:submissions, [:assessment_id, :team_id]))
end
end

0 comments on commit d7c147f

Please sign in to comment.