Skip to content

Commit

Permalink
Merge branch 'main' into feat/pdf-report
Browse files Browse the repository at this point in the history
  • Loading branch information
anasinik committed May 27, 2024
2 parents ced1a50 + 2c3127c commit 50d0aee
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 47 deletions.
27 changes: 8 additions & 19 deletions LangLang/BusinessLogic/UseCases/CourseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Delete(int id)
}
public void DeleteByTutor(Tutor tutor)
{
foreach (Course course in GetByTutor(tutor))
foreach (Course course in GetByTutor(tutor.Id))
{
if (course.StartDateTime > DateTime.Now)
{
Expand Down Expand Up @@ -180,30 +180,19 @@ public List<Course> GetAll()
{
return _courses.GetAll();
}
public DateTime GetEnd(Course course)
{
return course.TimeSlots[course.TimeSlots.Count - 1].GetEnd();
}

public bool IsActive(Course course)
{
if (course.StartDateTime <= DateTime.Now && GetEnd(course) >= DateTime.Now) return true;
return false;
}

public List<Course> GetByTutor(Tutor tutor)
public int NumActiveCourses(Tutor tutor)
{
List<Course> coursesByTutor = new List<Course>();

foreach (Course course in GetAll())
int active = 0;
List<Course> coursesByTutor = GetByTutor(tutor.Id);
foreach (Course course in coursesByTutor)
{
if (course.TutorId == tutor.Id)
if (course.IsActive())
{
coursesByTutor.Add(course);
active++;
}
}

return coursesByTutor;
return active;
}

public List<Course> GetByTutor(int tutorId)
Expand Down
43 changes: 42 additions & 1 deletion LangLang/BusinessLogic/UseCases/PdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,47 @@ public PdfGrid DataToGrid(Dictionary<string, double> data)
}
return grid;
}
public PdfGrid DataToGrid(Dictionary<string, List<double>> data)
{
PdfGrid grid = new PdfGrid();
grid.Columns.Add(4);
foreach (var item in data)
{
PdfGridRow row = grid.Rows.Add();
row.Cells[0].Value = item.Key;
row.Cells[1].Value = item.Value[0].ToString();
row.Cells[2].Value = item.Value[1].ToString();
row.Cells[3].Value = item.Value[2].ToString();
}
return grid;
}

public PdfGrid DataToGrid(float[] data)
{
PdfGrid grid = new PdfGrid();
grid.Columns.Add(4);
PdfGridRow row = grid.Rows.Add();
row.Cells[0].Value = data[0].ToString();
row.Cells[1].Value = data[1].ToString();
row.Cells[2].Value = data[2].ToString();
row.Cells[3].Value = data[3].ToString();
return grid;
}

public PdfGrid DataToGrid(Dictionary<string, float[]> data)
{
PdfGrid grid = new PdfGrid();
grid.Columns.Add(4);
foreach (var item in data)
{
PdfGridRow row = grid.Rows.Add();
row.Cells[0].Value = item.Key;
row.Cells[1].Value = item.Value[0].ToString();
row.Cells[2].Value = item.Value[1].ToString();
row.Cells[3].Value = item.Value[2].ToString();
}
return grid;
}

public PdfGrid DataToGrid(Dictionary<(Course, int), double> data)
{
Expand Down Expand Up @@ -77,8 +118,8 @@ public PdfGrid DataToGrid(Dictionary<Course, int> data)
row.Cells[0].Value = item.Key.Language + " " + item.Key.Level.ToString();
row.Cells[1].Value = item.Value.ToString();
}

return grid;
}

}
}
29 changes: 24 additions & 5 deletions LangLang/BusinessLogic/UseCases/ReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ public float[] GetAverageResults()
averages[2] += result.WritingPoints;
averages[3] += result.ListeningPoints;
}

if (results.Count == 0) return averages;

for (int i = 0; i < averages.Length; i++)
{
averages[i] /= results.Count;
}

return averages;
}
public int NumStudentsAttended(Course course)
Expand All @@ -82,16 +86,31 @@ public int NumStudentsPassed(Course course)
CourseService coursesService = new();
return coursesService.NumStudentsPassed(course);
}
public double CalculatePassingPercentage(Course course)
public float CalculatePassingPercentage(Course course)
{
int attended = NumStudentsAttended(course);
if (attended == 0) return 0;
int passed = NumStudentsPassed(course);
return (passed / attended)*100;
}

public Dictionary<Course, List<double>> GetAverageGradesOfCourses()
public Dictionary<string,float[]> GetCoursesAccomplishment()
{
Dictionary<string, float[]> accomplishments = new();
CourseService courseService = new();
List<Course> courses = courseService.GetCoursesHeldInLastYear();
foreach (Course course in courses)
{
float[] res = new float[3];
res[0] = NumStudentsAttended(course);
res[1] = NumStudentsPassed(course);
res[2] = CalculatePassingPercentage(course);
accomplishments.Add(course.ToPdfString(), res);
}
return accomplishments;
}
public Dictionary<string, List<double>> GetAverageGradesOfCourses()
{
Dictionary<Course, List<double>> averages = new();
Dictionary<string, List<double>> averages = new();
CourseService courseService = new();
List<Course> courses = courseService.GetCoursesHeldInLastYear();
GradeService gradeService = new();
Expand All @@ -102,7 +121,7 @@ public Dictionary<Course, List<double>> GetAverageGradesOfCourses()
avg.Add(gradeService.GetAverageKnowledgeGrade(course));
avg.Add(gradeService.GetAverageActivityGrade(course));
avg.Add(tutorRatingService.GetAverageTutorRating(course));
averages.Add(course, avg);
averages.Add(course.ToPdfString(), avg);
}
return averages;
}
Expand Down
36 changes: 36 additions & 0 deletions LangLang/BusinessLogic/UseCases/SenderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ public void SendResults(ExamSlot exam)
EmailService.SendEmail(student.Profile.Email, subject, body);
}
}
public void SendAverageCourseGrades(Director director)
{
var reportService = new ReportService();
var pdfService = new PdfService();

var reportName = "Average grades and tutor rating per course";
var headers = new string[] { "Course", "Average knowledge grade", "Average activity grade", "Average tutor rating" };

var document = PdfService.GeneratePdf<Dictionary<string, List<double>>>(reportService.GetAverageGradesOfCourses(), headers, reportName, data => pdfService.DataToGrid(data));
EmailService.SendEmail(director.Profile.Email, reportName, "", document);

}
public void SendAverageResultsPerSkill(Director director)
{
var reportService = new ReportService();
var pdfService = new PdfService();

var reportName = "Average exams results per skill in last year";
var headers = new string[] { "Reading average score", "Writing average score", "Listening average score", "Speaking average score" };

var document = PdfService.GeneratePdf<float[]>(reportService.GetAverageResults(), headers, reportName, data => pdfService.DataToGrid(data));
EmailService.SendEmail(director.Profile.Email, reportName, "", document);

}
public void SendCoursesAccomplishments(Director director)
{
var reportService = new ReportService();
var pdfService = new PdfService();

var reportName = "Courses enrollments and pass rates analysis";
var headers = new string[] { "Course", "Total Attendees", "Students Passed", "Pass Rate (%)" };

var document = PdfService.GeneratePdf<Dictionary<string, float[]>>(reportService.GetCoursesAccomplishment(), headers, reportName, data => pdfService.DataToGrid(data));
EmailService.SendEmail(director.Profile.Email, reportName, "", document);

}

public void SendGratitudeMail(Course course, List<Student> students)
{
Expand Down
62 changes: 54 additions & 8 deletions LangLang/BusinessLogic/UseCases/SmartSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LangLang.Configuration;
using LangLang.Domain.Models;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace LangLang.BusinessLogic.UseCases
Expand Down Expand Up @@ -65,21 +66,66 @@ public static int MostSuitableTutor(Course course)
{
TutorService tutorService = new();
List<Tutor> tutors = tutorService.GetBySkill(course.Language, course.Level);
//there is no free tutors for given course
if (tutors.Count == 0) return -1;
TutorRatingService tutorRatingService = new();
Dictionary<Tutor, double> tutorsAndRatings = new();

//consider only tutors that are free in time of course
List<Tutor> availableTutors = new();
CourseService coursesService = new();
foreach (Tutor tutor in tutors)
{
tutorsAndRatings[tutor] = tutorRatingService.GetAverageRating(tutor);
course.TutorId = tutor.Id;
if (coursesService.CanCreateOrUpdate(course)) availableTutors.Add(tutor);
}
Dictionary<Tutor, double> sorted = tutorsAndRatings.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
//find least busy tutors
List<Tutor> leastBusyTutors = WithLeastActiveCourses(availableTutors);
//check who is rated the best
return GetBestRankedTutor(leastBusyTutors);
}
private static int MinimumActiveCourses(List<Tutor> tutors)
{
CourseService coursesService = new();
foreach (Tutor tutor in sorted.Keys)
int minActive = int.MaxValue;
int active = 0;
foreach (var tutor in tutors)
{
course.TutorId = tutor.Id;
if (coursesService.CanCreateOrUpdate(course)) return tutor.Id;
active = coursesService.NumActiveCourses(tutor);
if (active < minActive)
{
minActive = active;
}
}
return -1;

return minActive;
}
private static List<Tutor> WithLeastActiveCourses(List<Tutor> tutors)
{
int minActive = MinimumActiveCourses(tutors);
List<Tutor> leastBusyTutors = new List<Tutor>();
CourseService coursesService = new();

foreach (var tutor in tutors)
{
int active = coursesService.NumActiveCourses(tutor);
if (active == minActive)
{
leastBusyTutors.Add(tutor);
}
}

return leastBusyTutors;
}
private static int GetBestRankedTutor(List<Tutor> tutors)
{
TutorRatingService tutorRatingService = new();
Dictionary<Tutor, double> ratings = new();
foreach (Tutor tutor in tutors)
{
ratings[tutor] = tutorRatingService.GetAverageRating(tutor);
}
Tutor bestRanked = ratings.Aggregate((x, y) => x.Value < y.Value ? x : y).Key;
return bestRanked.Id;
}

}
}
2 changes: 1 addition & 1 deletion LangLang/BusinessLogic/UseCases/TutorRatingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public double GetAverageRating(Tutor tutor)
{
CourseService courseService = new();
List<double> ratings = new();
foreach (Course course in courseService.GetByTutor(tutor))
foreach (Course course in courseService.GetByTutor(tutor.Id))
{
ratings.Add(GetAverageTutorRating(course));
}
Expand Down
22 changes: 20 additions & 2 deletions LangLang/Domain/Models/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ public int DaysUntilStart()

public bool IsHeldInLastYear()
{
var endDate = TimeSlots[^1].GetEnd();
DateTime oneYearAgo = DateTime.Now.AddYears(-1);
return endDate > oneYearAgo;
return GetEnd() > oneYearAgo && GetEnd() <= DateTime.Now;
}

public override string ToString()
Expand All @@ -125,5 +124,24 @@ public override string ToString()
}
return string.Join("|", new object[] { Id, TutorId, Language, Level.ToString(), NumberOfWeeks, sbDays.ToString(), Online, NumberOfStudents, MaxStudents, StartDateTime.ToString(), CreatedByDirector, Modifiable, GratitudeEmailSent });
}

public string ToPdfString()
{
return Id + " " + " " + Language + " " + Level;
}

public DateTime GetEnd()
{
return TimeSlots[TimeSlots.Count - 1].GetEnd();
}
public bool IsActive()
{
if (StartDateTime <= DateTime.Now && GetEnd() >= DateTime.Now) return true;
return false;
}
public bool CanChange()
{
return StartDateTime >= DateTime.Now.AddDays(Constants.COURSE_MODIFY_PERIOD); ;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Update()
{
Courses.Clear();
CourseService courseService = new();
foreach (Course course in courseService.GetByTutor(LoggedIn))
foreach (Course course in courseService.GetByTutor(LoggedIn.Id))
{
Courses.Add(new CourseViewModel(course));
}
Expand Down
18 changes: 18 additions & 0 deletions LangLang/WPF/ViewModels/DirectorViewModels/ReportsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public void SendPenaltiesCountLastYear()
ShowSuccess();
}

public void SentAverageCourseGrades()
{
var senderService = new SenderService();
senderService.SendAverageCourseGrades(_director);
ShowSuccess();
}
public void SentAverageResultsPerSkill()
{
var senderService = new SenderService();
senderService.SendAverageResultsPerSkill(_director);
ShowSuccess();
}
public void SentCoursesAccomplishments()
{
var senderService = new SenderService();
senderService.SendCoursesAccomplishments(_director);
ShowSuccess();
}
private void ShowSuccess()
{
MessageBox.Show("Successfully completed!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
Expand Down
5 changes: 3 additions & 2 deletions LangLang/WPF/Views/DirectorView/Tabs/Reports.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
<Button Grid.Row="3" Content="Number of average penalty points per language" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" Click="AveragePenaltyPoints_Click" />
<Button Grid.Row="4" Content="Number of created courses for each of the existing languages in the system in the last year" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" />
<Button Grid.Row="5" Content="Number of created exams for each of the existing languages in the system in the last year" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" />
<Button Grid.Row="6" Content="Report b" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" />
<Button Grid.Row="7" Content="Report c" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" />
<Button Grid.Row="6" Content="The average grades of students as well as the average rating of tutor per course in the last year" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" Click="AverageCourseGrades_Click" />
<Button Grid.Row="7" Content="The average exams score for each skill in the last year" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" Click="AverageResultsPerSkill_Click"/>
<Button Content="Enrollments and pass rate of courses held in the last year" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF817C7C" FontFamily="Segoe UI Black" Click="CoursesAccomplishments_Click" Margin="0,50,0,-50" Grid.Row="7" />
</Grid>
</Grid>
</UserControl>
Loading

0 comments on commit 50d0aee

Please sign in to comment.