diff --git a/CharlieBackend.Business/Services/FileServices/ExportFileServices/ExportServiceHtml.cs b/CharlieBackend.Business/Services/FileServices/ExportFileServices/ExportServiceHtml.cs index 16bfb8f02..34c99c2c7 100644 --- a/CharlieBackend.Business/Services/FileServices/ExportFileServices/ExportServiceHtml.cs +++ b/CharlieBackend.Business/Services/FileServices/ExportFileServices/ExportServiceHtml.cs @@ -55,11 +55,18 @@ public async Task> GetStudentClassbook(StudentsClassbookResultDt "student classbook can't be returned in html format"); } - public Task> GetStudentGroupResults(StudentGroupsResultsDto data) + public async Task> GetStudentGroupResults(StudentGroupsResultsDto data) { - return Task.FromResult(Result.GetError( - ErrorCode.ValidationError, - "student classbook can't be returned in html format")); + using var studentGroupResults = new StudentGroupResultsExportHTML("Student Group Results"); + + studentGroupResults.FillFileAsync(data); + + return Result.GetSuccess(new FileDto() + { + ByteArray = await studentGroupResults.GetByteArrayAsync(), + ContentType = studentGroupResults.GetContentType(), + Filename = studentGroupResults.GetFileName() + }); } public Task> GetStudentResults(StudentsResultsDto data) diff --git a/CharlieBackend.Business/Services/FileServices/ExportFileServices/Html/StudentGroupResultsExportHTML.cs b/CharlieBackend.Business/Services/FileServices/ExportFileServices/Html/StudentGroupResultsExportHTML.cs new file mode 100644 index 000000000..5faeeffba --- /dev/null +++ b/CharlieBackend.Business/Services/FileServices/ExportFileServices/Html/StudentGroupResultsExportHTML.cs @@ -0,0 +1,98 @@ +using CharlieBackend.Core.DTO.Dashboard; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CharlieBackend.Business.Services.FileServices.ExportFileServices.Html +{ + public class StudentGroupResultsExportHTML : BaseFileExportHTML + { + private string _fileName; + private const int _CourseColumnNumber = 0; + private const int _StudentGroupColumnNumber = 1; + private const int _AverageMarkColumnNumber = 2; + + public StudentGroupResultsExportHTML(string fileName) + { + _fileName = fileName; + } + public void FillFileAsync(StudentGroupsResultsDto data) + { + if (data == null) + { + return; + } + FillAverageMarks(data.AverageStudentGroupsMarks); + + FillAverageVisits(data.AverageStudentGroupsVisits); + } + + public void FillAverageMarks(IEnumerable AverageStudentGroupsMarks) + { + if (AverageStudentGroupsMarks.Any()) + { + var orderedList = AverageStudentGroupsMarks.OrderBy(x => x.StudentGroup); + string[] headers = new string[] { "Course", "Student Group", "Average mark" }; + string[][] rows = new string[orderedList.Count()][]; + + for (int i = 0; i < rows.Length; i++) + { + var group = orderedList.ElementAt(i); + rows[i] = new string[headers.Length]; + rows[i][_CourseColumnNumber] = AverageStudentGroupsMarks.First().Course; + rows[i][_StudentGroupColumnNumber] = group.StudentGroup; + rows[i][_AverageMarkColumnNumber] = Math.Round((decimal)group.AverageMark, 2).ToString(); + } + + StringBuilder table = HtmlGenerator.GenerateTable(headers, rows); + StringBuilder html = HtmlGenerator.GenerateHtml(_fileName, table); + byte[] byteLine = ConvertLineToArray(html.ToString()); + _memoryStream.Write(byteLine); + } + } + + public void FillAverageVisits(IEnumerable AverageStudentGroupsVisits) + { + if (AverageStudentGroupsVisits.Any()) + { + var orderedList = AverageStudentGroupsVisits.OrderBy(x => x.StudentGroup); + string[] headers = new string[] { "Course", "Student Group", "Average visits" }; + string[][] rows = new string[orderedList.Count()][]; + + for (int i = 0; i < rows.Length; i++) + { + var group = orderedList.ElementAt(i); + rows[i] = new string[headers.Length]; + rows[i][_CourseColumnNumber] = AverageStudentGroupsVisits.First().Course; + rows[i][_StudentGroupColumnNumber] = group.StudentGroup; + rows[i][_AverageMarkColumnNumber] = ((double)group.AverageVisitPercentage / 100).ToString(); + } + + StringBuilder table = HtmlGenerator.GenerateTable(headers, rows); + StringBuilder html = HtmlGenerator.GenerateHtml(_fileName, table); + byte[] byteLine = ConvertLineToArray(html.ToString()); + _memoryStream.Write(byteLine); + + } + } + + private byte[] ConvertLineToArray(string line) + { + byte[] array = new byte[line.Length]; + + for (int i = 0; i < line.Length; i++) + { + array[i] = (byte)line[i]; + } + + return array; + } + + public override string GetFileName() + { + return "StudentGroupResult_" + DateTime.Now.ToString("yyyy-MM-dd") + ".html"; + } + } +} diff --git a/CharlieBackend.Business/Services/ScheduleServiceFolder/Helpers/SchedulesUpdater.cs b/CharlieBackend.Business/Services/ScheduleServiceFolder/Helpers/SchedulesUpdater.cs index 7ee944f82..0060d835e 100644 --- a/CharlieBackend.Business/Services/ScheduleServiceFolder/Helpers/SchedulesUpdater.cs +++ b/CharlieBackend.Business/Services/ScheduleServiceFolder/Helpers/SchedulesUpdater.cs @@ -11,9 +11,9 @@ public static ScheduledEvent UpdateFields(ScheduledEvent item, UpdateScheduledEv item.MentorId = request.MentorId ?? item.MentorId; item.StudentGroupId = request.StudentGroupId ?? item.StudentGroupId; item.ThemeId = request.ThemeId ?? item.ThemeId; - item.EventStart = request.EventStart.HasValue ? new DateTime(item.EventStart.Year, item.EventStart.Month, item.EventStart.Day, + item.EventStart = request.EventStart.HasValue ? new DateTime(request.EventStart.Value.Year, request.EventStart.Value.Month, request.EventStart.Value.Day, request.EventStart.Value.Hour, request.EventStart.Value.Minute, request.EventStart.Value.Second) : item.EventStart; - item.EventFinish = request.EventEnd.HasValue ? new DateTime(item.EventFinish.Year, item.EventFinish.Month, item.EventFinish.Day, + item.EventFinish = request.EventEnd.HasValue ? new DateTime(request.EventEnd.Value.Year, request.EventEnd.Value.Month, request.EventEnd.Value.Day, request.EventEnd.Value.Hour, request.EventEnd.Value.Minute, request.EventEnd.Value.Second) : item.EventFinish; return item; diff --git a/CharlieBackend.Core/DTO/Dashboard/StudentGroupResults/StudentsGroupsResultsDto.cs b/CharlieBackend.Core/DTO/Dashboard/StudentGroupResults/StudentsGroupsResultsDto.cs index cbe9ee189..8e4a107bd 100644 --- a/CharlieBackend.Core/DTO/Dashboard/StudentGroupResults/StudentsGroupsResultsDto.cs +++ b/CharlieBackend.Core/DTO/Dashboard/StudentGroupResults/StudentsGroupsResultsDto.cs @@ -7,5 +7,12 @@ public class StudentGroupsResultsDto public IEnumerable AverageStudentGroupsVisits { get; set; } public IEnumerable AverageStudentGroupsMarks { get; set; } + + public StudentGroupsResultsDto() + { + AverageStudentGroupsVisits = new List(); + AverageStudentGroupsMarks = new List(); + } + } }