Skip to content

Commit

Permalink
Merge pull request #445 from kzi-nastava/feat/get-exams-not-passed-lo…
Browse files Browse the repository at this point in the history
…wer-level

[refactor] Modify GetAvailableExams method so it doesnt return exams of lower levels if an exam if a higher level has been passed
  • Loading branch information
anasinik authored May 27, 2024
2 parents bbb4cf6 + b8d4a8f commit 7ea489b
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions LangLang/BusinessLogic/UseCases/ExamSlotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,51 +258,49 @@ public List<ExamSlot> SearchByStudent(Student student, DateTime examDate, string
}


// returns a list of exams that are available for student application
public List<ExamSlot> GetAvailableExams(Student student)
{

if (student == null) return null;
List<ExamSlot> availableExams = new();

var enrollmentReqService = new EnrollmentRequestService();
List<EnrollmentRequest> studentRequests = enrollmentReqService.GetByStudent(student);
var resultService = new ExamResultService();
var applicationService = new ExamApplicationService();
var examService = new ExamSlotService();
var courseService = new CourseService();

foreach (ExamSlot exam in GetAll())
{
//don't include filled exams and exams that passed or are less then a month away
if (!IsAvailable(exam)) continue;

//don't include exams for which student has already applied
ExamApplicationService appService = new();
bool hasAlreadyApplied = appService.HasApplied(student, exam);
if (hasAlreadyApplied) continue;
List<EnrollmentRequest> studentRequests = enrollmentReqService.GetByStudent(student);
List<ExamResult> studentResults = resultService.GetByStudent(student);

foreach (EnrollmentRequest enrollmentRequest in studentRequests)
{
CourseService courseService = new();
Course course = courseService.Get(enrollmentRequest.CourseId);
if (HasStudentAttendedCourse(course, enrollmentRequest, exam))
{
availableExams.Add(exam);
}
}
}
var exams = GetAll().Where(exam => IsAvailable(exam)) // exclude exams that have been filled, passed, or are less than a month away.
.Where(exam => applicationService.HasApplied(student, exam)) //exclude exams for which student has already applied
.Where(exam => !HasPassedLowerLevel(studentResults, exam))
.Where(exam => HasAttendedRequiredCourse(studentRequests, exam)).ToList();
return exams;
}

return availableExams;
private bool HasPassedLowerLevel(List<ExamResult> results, ExamSlot exam)
{
var examService = new ExamSlotService();
return results.Any(result => result.Outcome == ExamOutcome.Passed &&
exam.Language == examService.Get(result.ExamSlotId).Language &&
exam.Level < examService.Get(result.ExamSlotId).Level);
}

private bool HasStudentAttendedCourse(Course course, EnrollmentRequest enrollmentRequest, ExamSlot examSlot)
private bool HasAttendedRequiredCourse(List<EnrollmentRequest> requests, ExamSlot exam)
{
if (course.Language == examSlot.Language && course.Level == examSlot.Level)
{
if (enrollmentRequest.Status == Status.Accepted && course.IsCompleted())
{
return true;
}
}
return false;
var courseService = new CourseService();
return requests.Select(request => courseService.Get(request.CourseId))
.Any(course => HasStudentAttendedCourse(course, exam));
}

private bool HasStudentAttendedCourse(Course course, ExamSlot examSlot)
{
return course.Language == examSlot.Language &&
course.Level == examSlot.Level &&
course.IsCompleted();
}

public List<ExamSlot> GetExamsHeldInLastYear()
{
return GetAll().Where(exam => exam.IsHeldInLastYear()).ToList();
Expand Down

0 comments on commit 7ea489b

Please sign in to comment.