File

src/module/pdf/subservices/PDFGenerator.scheinexam.ts

Extends

PDFWithStudentsGenerator

Index

Properties
Methods

Constructor

constructor(studentService: StudentService, scheinexamService: ScheinexamService, templateService: TemplateService, gradingService: GradingService)
Parameters :
Name Type Optional
studentService StudentService No
scheinexamService ScheinexamService No
templateService TemplateService No
gradingService GradingService No

Methods

Public Async generatePDF
generatePDF(undefined: PDFGeneratorOptions)
Inherited from PDFGenerator
Defined in PDFGenerator:43

Generates a PDF from the given students with their results of the given Scheinexam. Students which are INACTIVE are getting ignored aswell as students which don't have a matriculation number (due to not being able to put those students in the PDF in an "anonymous" way).

Parameters :
Name Type Optional
PDFGeneratorOptions No
Returns : Promise<Buffer>

Buffer containing a PDF which itself contains the results of the given students of the given exam.

Private getResultsOfAllStudents
getResultsOfAllStudents(undefined: GetResultsParams)

Returns the results of all given students for the given exam. Those results are mapped with the student's id as a key and the result as value.

Parameters :
Name Type Optional
GetResultsParams No

The results of all students mapped by their ID.

Private Static getFirstDifferentPosition
getFirstDifferentPosition(first: string, second: string)
Inherited from PDFWithStudentsGenerator

Searches for the position of the first character which both strings DO NOT have in common. This position is then returned.

Parameters :
Name Type Optional Description
first string No

First string

second string No

Second string

Returns : number

The first position in which both string differ. If they are completely equal the length of the first string is returned.

Protected getShortenedMatriculationNumbers
getShortenedMatriculationNumbers(students: (Student | IStudent)[])
Inherited from PDFWithStudentsGenerator

Returns the shortened number for all students together with the ID of the student to which the shortened matriculation number belongs to.

Those shortened numbers are still enough to identify a student. However, this is only true if one only considers the given students. If one extends that array without re-running this function the identifying feature may get lost.

Parameters :
Name Type Optional Description
students (Student | IStudent)[] No

All students to get the shortened number from.

The shortened but still identifying matriculation numbers of all given students.

Private Static reverseString
reverseString(string: string)
Inherited from PDFWithStudentsGenerator
Parameters :
Name Type Optional Description
string string No

String to reverse

Returns : string

The reversed string.

Protected Async generatePDFFromBodyContent
generatePDFFromBodyContent(body: string)
Inherited from PDFGenerator
Defined in PDFGenerator:31

Generates a PDF from the given body. The body gets put in an HTML wrapper first.

Parameters :
Name Type Optional Description
body string No

Body content to be put in the PDF as HTML body.

Returns : Promise<Buffer>

Buffer containing the generated PDF.

Private Static getCustomCSS
getCustomCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:114
Returns : string

Some small customizations to the GitHub markdown CSS.

Private Async getGithubMarkdownCSS
getGithubMarkdownCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:88
Returns : Promise<string>

The GitHub markdown CSS.

Private Async getHighlightCSS
getHighlightCSS()
Inherited from PDFGenerator
Defined in PDFGenerator:95
Returns : Promise<string>

The HighlightJS CSS.

Private Async loadCSSFile
loadCSSFile(moduleName: string)
Inherited from PDFGenerator
Defined in PDFGenerator:99
Parameters :
Name Type Optional
moduleName string No
Returns : Promise<string>
Private Async putBodyInHTML
putBodyInHTML(body: string)
Inherited from PDFGenerator
Defined in PDFGenerator:69

Puts the given body in corresponding a <body> element. The returned string is a complete HTML "file" with slightly customized GitHub Markdown CSS.

Parameters :
Name Type Optional Description
body string No

Body to embed in HTML.

Returns : Promise<string>

Complete HTML "file" which contains the given body as body.

Properties

Private Readonly logger
Default value : new Logger(PDFGenerator.name)
Inherited from PDFGenerator
Defined in PDFGenerator:13
import { Injectable } from '@nestjs/common';
import { StudentStatus } from 'shared/model/Student';
import { Scheinexam } from '../../../database/entities/scheinexam.entity';
import { ScheinexamService } from '../../scheinexam/scheinexam.service';
import { GradingService, StudentAndGradings } from '../../student/grading.service';
import { StudentService } from '../../student/student.service';
import { TemplateService } from '../../template/template.service';
import { PassedState, ScheinexamStatus } from '../../template/template.types';
import { PDFWithStudentsGenerator } from './PDFGenerator.withStudents';

interface PDFGeneratorOptions {
    id: string;
    enableShortMatriculationNo: boolean;
}

interface GetResultsParams {
    exam: Scheinexam;
    studentsAndGradings: StudentAndGradings[];
}

interface ExamResultsByStudents {
    [studentId: string]: PassedState;
}

@Injectable()
export class ScheinexamResultPDFGenerator extends PDFWithStudentsGenerator<PDFGeneratorOptions> {
    constructor(
        private readonly studentService: StudentService,
        private readonly scheinexamService: ScheinexamService,
        private readonly templateService: TemplateService,
        private readonly gradingService: GradingService
    ) {
        super();
    }

    /**
     * Generates a PDF from the given students with their results of the given Scheinexam. Students which are INACTIVE are getting ignored aswell as students which don't have a matriculation number (due to not being able to put those students in the PDF in an "anonymous" way).
     *
     * @param options Must contain a ScheinexamDocument and an array of StudentDocument.
     *
     * @returns Buffer containing a PDF which itself contains the results of the given students of the given exam.
     */
    public async generatePDF({
        id,
        enableShortMatriculationNo,
    }: PDFGeneratorOptions): Promise<Buffer> {
        const [exam, allStudents] = await Promise.all([
            this.scheinexamService.findById(id),
            this.studentService.findAll(),
        ]);
        const students = allStudents
            .filter((student) => !!student.matriculationNo)
            .filter((student) => student.status !== StudentStatus.INACTIVE);
        const studentsAndGradings =
            await this.gradingService.findAllGradingsOfMultipleStudents(students);
        const shortenedMatriculationNumbers = this.getShortenedMatriculationNumbers(students);
        const results = this.getResultsOfAllStudents({ exam, studentsAndGradings });

        const statuses: ScheinexamStatus[] = [];
        const template = this.templateService.getScheinexamTemplate();

        shortenedMatriculationNumbers.forEach(({ studentId, shortenedNo }) => {
            const state = results[studentId] ?? PassedState.NOT_ATTENDED;

            if (enableShortMatriculationNo) {
                statuses.push({ matriculationNo: shortenedNo, state });
            } else {
                const matriculationNo = students.find((s) => s.id === studentId)?.matriculationNo;
                statuses.push({
                    matriculationNo: `${matriculationNo} (${shortenedNo})`,
                    state,
                });
            }
        });

        return this.generatePDFFromBodyContent(
            template({ scheinExamNo: exam.scheinExamNo, statuses })
        );
    }

    /**
     * Returns the results of all given students for the given exam. Those results are mapped with the student's id as a key and the result as value.
     *
     * @param Options Options containing the exam and all students to get the results from.
     *
     * @returns The results of all students mapped by their ID.
     */
    private getResultsOfAllStudents({
        exam,
        studentsAndGradings,
    }: GetResultsParams): ExamResultsByStudents {
        const results: ExamResultsByStudents = {};

        studentsAndGradings.forEach(({ student, gradingsOfStudent }) => {
            const examGrading = gradingsOfStudent.getGradingOfHandIn(exam);
            const hasAttended = examGrading !== undefined;

            if (hasAttended) {
                results[student.id] = exam.hasPassed(student, gradingsOfStudent)
                    ? PassedState.PASSED
                    : PassedState.NOT_PASSED;
            } else {
                results[student.id] = PassedState.NOT_ATTENDED;
            }
        });

        return results;
    }
}

results matching ""

    No results matching ""