Skip to content

Commit

Permalink
Merge pull request #177 from kzi-nastava/feat/final-coursegui
Browse files Browse the repository at this point in the history
[Update] fix all bugs for Course gui
  • Loading branch information
DusicaPesic authored Apr 2, 2024
2 parents 512fccd + ea99b77 commit 11bb052
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 7 deletions.
135 changes: 135 additions & 0 deletions LangLang/Core/Controller/CourseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,57 @@ public bool CanCreateOnlineCourse(Course course, List<ExamSlot> exams)
return true;
}

public bool CanUpdateOnlineCourse(Course course, List<ExamSlot> exams)
{
// Get the time and the date of the beginning and of the end of the new couse
TimeSpan courseTime = course.StartDateTime.TimeOfDay;
DateTime courseStartDate = course.StartDateTime.Date;
DateTime courseEndDate = GetCourseEnd(course);

// Go through each course with the same tutor and check if there is time overlapping
foreach (Course cour in GetCoursesByTutor(course.TutorId).Values)
{
if(cour.Id == course.Id)
{
continue;
}
// Check if the courses are overlapping
DateTime courStartDate = cour.StartDateTime.Date;
DateTime courEndDate = GetCourseEnd(cour);
if (courStartDate <= courseEndDate && courEndDate >= courseStartDate)
{
foreach (DayOfWeek day in cour.Days)
{
if (course.Days.Contains(day))
{
TimeSpan courTime = cour.StartDateTime.TimeOfDay;
TimeSpan difference = courseTime - courTime;
if (difference.Duration().TotalMinutes < 90)
{
return false;
}
}
}
}
}

// Go through all exams of the same tutor
foreach (ExamSlot exam in exams)
{
DateTime examDate = exam.ExamDateTime.Date;
if (examDate >= courseStartDate && examDate <= courseEndDate && course.Days.Contains(examDate.DayOfWeek))
{
// Check if there are time overlaps
TimeSpan examTime = exam.ExamDateTime.TimeOfDay;
TimeSpan difference = courseTime - examTime;
if ((examTime < courseTime && difference.Duration().TotalMinutes < 240) || (examTime > courseTime && difference.Duration().TotalMinutes < 90))
{
return false;
}
}
}
return true;
}
// Method returns true if a new live course can be added
// or false if there are time overlaps and the course cannot be created
// The parameter exams is a list of ExamSlots of all the exams
Expand Down Expand Up @@ -212,6 +263,90 @@ public bool CanCreateLiveCourse(Course course, List<ExamSlot> exams)
}
return true;
}
public bool CanUpdateLiveCourse(Course course, List<ExamSlot> exams)
{
// Get the time and the date of the beginning and of the end of the new couse
TimeSpan courseTime = course.StartDateTime.TimeOfDay;
DateTime courseStartDate = course.StartDateTime.Date;
DateTime courseEndDate = GetCourseEnd(course);

bool firstClassroomTaken = false;
bool secondClassromTaken = false;
// Go through each course with the same tutor or held in a classroom and check if there is time overlapping
foreach (Course cour in _courses.GetAllCourses().Values)
{
if(cour.Id == course.Id)
{
continue;
}
if (cour.TutorId != course.TutorId && cour.Online)
{
continue;
}
// Check if the courses are overlapping
DateTime courStartDate = cour.StartDateTime.Date;
DateTime courEndDate = GetCourseEnd(cour);
if (courStartDate <= courseEndDate && courEndDate >= courseStartDate)
{
foreach (DayOfWeek day in cour.Days)
{
if (course.Days.Contains(day))
{
TimeSpan courTime = cour.StartDateTime.TimeOfDay;
TimeSpan difference = courseTime - courTime;
if (difference.Duration().TotalMinutes < 90)
{
if (firstClassroomTaken && secondClassromTaken)
{
return false;
}
else if (firstClassroomTaken)
{
secondClassromTaken = true;
}
else
{
firstClassroomTaken = true;
}
}
}
}
}
}

// Go through all exams of the same tutor
foreach (ExamSlot exam in exams)
{
DateTime examDate = exam.ExamDateTime.Date;
if (examDate >= courseStartDate && examDate <= courseEndDate && course.Days.Contains(examDate.DayOfWeek))
{
// Check if there are time overlaps
TimeSpan examTime = exam.ExamDateTime.TimeOfDay;
TimeSpan difference = courseTime - examTime;
if ((examTime < courseTime && difference.Duration().TotalMinutes < 240) || (examTime > courseTime && difference.Duration().TotalMinutes < 90))
{
if (firstClassroomTaken && secondClassromTaken)
{
return false;
}
else if (firstClassroomTaken)
{
secondClassromTaken = true;
}
else
{
firstClassroomTaken = true;
}
}
}
}

if (firstClassroomTaken && secondClassromTaken)
{
return false;
}
return true;
}
// Method checks if the course is valid for updating or canceling
public bool IsCourseValid(int courseId)
{
Expand Down
4 changes: 3 additions & 1 deletion LangLang/Core/Model/DAO/CoursesDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public Course UpdateCourse(Course course)
oldCourse.Online = course.Online;
oldCourse.NumberOfStudents = course.NumberOfStudents;
oldCourse.MaxStudents = course.MaxStudents;

oldCourse.StartDateTime = course.StartDateTime;
oldCourse.TutorId = course.TutorId;

_repository.Save(_courses);
NotifyObservers();
return oldCourse;
Expand Down
35 changes: 31 additions & 4 deletions LangLang/View/CourseGUI/CourseUpdateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public partial class CourseUpdateWindow : Window
{
public CourseDTO Course { get; set; }
private CourseController courseController;
public CourseUpdateWindow(CourseController courseControler, int courseId)
private ExamSlotController examController { get; set; }
public CourseUpdateWindow(CourseController courseControler, ExamSlotController exams, int courseId)
{
this.courseController = courseControler;
Course = new CourseDTO(courseController.GetById(courseId));

examController = new ExamSlotController();
examController = exams;
InitializeComponent();
DataContext = this;
languageLvlCb.ItemsSource = Enum.GetValues(typeof(LanguageLevel));
Expand All @@ -37,9 +41,32 @@ private void CourseUpdateBtn_Click(object sender, RoutedEventArgs e)
{
if (Course.IsValid)
{
courseController.Update(Course.ToCourse());
MessageBox.Show("Success!");
Close();
if (Course.NotOnline)
{
if (courseController.CanUpdateLiveCourse(Course.ToCourse(), examController.GetAllExamSlots().Values.ToList<ExamSlot>()))
{
courseController.Update(Course.ToCourse());
MessageBox.Show("Success!");
Close();
}
else
{
MessageBox.Show("There seem to be time overlaps.");
}
}
else
{
if (courseController.CanUpdateOnlineCourse(Course.ToCourse(), examController.GetExamSlotsByTutor(Course.TutorId, courseController)))
{
courseController.Update(Course.ToCourse());
MessageBox.Show("Success!");
Close();
}
else
{
MessageBox.Show("There seem to be time overlaps.");
}
}
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions LangLang/View/TutorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private void CourseUpdateWindowBtn_Click(object sender, RoutedEventArgs e)
{
if (coursesController.IsCourseValid(SelectedCourse.Id))
{
CourseUpdateWindow courseUpdateWindow = new CourseUpdateWindow(coursesController, SelectedCourse.Id);
CourseUpdateWindow courseUpdateWindow = new CourseUpdateWindow(coursesController, examSlotsController, SelectedCourse.Id);
courseUpdateWindow.Show();
Update();
}
Expand All @@ -164,7 +164,9 @@ private void CourseDeleteBtn_Click(object sender, RoutedEventArgs e)
{
if (coursesController.IsCourseValid(SelectedCourse.Id))
{
coursesController.Delete(SelectedCourse.Id);
int id = SelectedCourse.Id;
examSlotsController.DeleteExamSlotsByCourseId(id);
coursesController.Delete(id);
Update();
MessageBox.Show("The course has successfully been deleted.");
}
Expand Down

0 comments on commit 11bb052

Please sign in to comment.