Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added functional export student group result to html #794

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ public async Task<Result<FileDto>> GetStudentClassbook(StudentsClassbookResultDt
"student classbook can't be returned in html format");
}

public Task<Result<FileDto>> GetStudentGroupResults(StudentGroupsResultsDto data)
public async Task<Result<FileDto>> GetStudentGroupResults(StudentGroupsResultsDto data)
{
return Task.FromResult(Result<FileDto>.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<FileDto>.GetSuccess(new FileDto()
{
ByteArray = await studentGroupResults.GetByteArrayAsync(),
ContentType = studentGroupResults.GetContentType(),
Filename = studentGroupResults.GetFileName()
});
}

public Task<Result<FileDto>> GetStudentResults(StudentsResultsDto data)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AverageStudentGroupMarkDto> 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<AverageStudentGroupVisitDto> AverageStudentGroupsVisits)
Sergey-Kisenko marked this conversation as resolved.
Show resolved Hide resolved
{
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()
Sergey-Kisenko marked this conversation as resolved.
Show resolved Hide resolved
{
return "StudentGroupResult_" + DateTime.Now.ToString("yyyy-MM-dd") + ".html";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ public class StudentGroupsResultsDto
public IEnumerable<AverageStudentGroupVisitDto> AverageStudentGroupsVisits { get; set; }
Sergey-Kisenko marked this conversation as resolved.
Show resolved Hide resolved

public IEnumerable<AverageStudentGroupMarkDto> AverageStudentGroupsMarks { get; set; }
Sergey-Kisenko marked this conversation as resolved.
Show resolved Hide resolved

public StudentGroupsResultsDto()
Sergey-Kisenko marked this conversation as resolved.
Show resolved Hide resolved
{
AverageStudentGroupsVisits = new List<AverageStudentGroupVisitDto>();
AverageStudentGroupsMarks = new List<AverageStudentGroupMarkDto>();
}

}
}