Skip to content

Commit

Permalink
CSCEXAM-000 Refactor large components/services to smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
lupari committed Aug 22, 2024
1 parent e6b01fb commit 0c24bda
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import type { OnInit } from '@angular/core';
import { Component, signal } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { DashboardEnrolment } from 'src/app/dashboard/dashboard.model';
import { ActiveEnrolmentComponent } from 'src/app/enrolment/active/active-enrolment.component';
import { PageContentComponent } from 'src/app/shared/components/page-content.component';
import { PageHeaderComponent } from 'src/app/shared/components/page-header.component';
import { OrderByPipe } from 'src/app/shared/sorting/order-by.pipe';
import type { DashboardEnrolment } from './student-dashboard.service';
import { StudentDashboardService } from './student-dashboard.service';

@Component({
Expand Down
8 changes: 5 additions & 3 deletions ui/src/app/exam/editor/sections/section-question.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { map } from 'rxjs/operators';
import type { ExamSection } from 'src/app/exam/exam.model';
import { BaseQuestionEditorComponent } from 'src/app/question/examquestion/base-question-editor.component';
import { ExamQuestionEditorComponent } from 'src/app/question/examquestion/exam-question-editor.component';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { ExamSectionQuestion, ExamSectionQuestionOption, Question } from 'src/app/question/question.model';
import { QuestionService } from 'src/app/question/question.service';
import { AttachmentService } from 'src/app/shared/attachment/attachment.service';
Expand Down Expand Up @@ -64,15 +65,16 @@ export class SectionQuestionComponent {
private toast: ToastrService,
private Confirmation: ConfirmationDialogService,
private Question: QuestionService,
private QuestionScore: QuestionScoringService,
private Attachment: AttachmentService,
private Files: FileService,
) {}

calculateWeightedMaxPoints = () => this.Question.calculateWeightedMaxPoints(this.sectionQuestion.options);
calculateWeightedMaxPoints = () => this.QuestionScore.calculateWeightedMaxPoints(this.sectionQuestion.options);

getCorrectClaimChoiceOptionScore = () => this.Question.getCorrectClaimChoiceOptionScore(this.sectionQuestion);
getCorrectClaimChoiceOptionScore = () => this.QuestionScore.getCorrectClaimChoiceOptionScore(this.sectionQuestion);

getMinimumOptionScore = () => this.Question.getMinimumOptionScore(this.sectionQuestion);
getMinimumOptionScore = () => this.QuestionScore.getMinimumOptionScore(this.sectionQuestion);

editQuestion = () => this.openExamQuestionEditor();

Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/exam/editor/sections/section.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import type { ExamMaterial, ExamSection } from 'src/app/exam/exam.model';
import { ExamService } from 'src/app/exam/exam.service';
import { BaseQuestionEditorComponent } from 'src/app/question/examquestion/base-question-editor.component';
import { QuestionSelectorComponent } from 'src/app/question/picker/question-picker.component';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { ExamSectionQuestion, Question } from 'src/app/question/question.model';
import { QuestionService } from 'src/app/question/question.service';
import { ConfirmationDialogService } from 'src/app/shared/dialogs/confirmation-dialog.service';
import { FileService } from 'src/app/shared/file/file.service';
import { OrderByPipe } from 'src/app/shared/sorting/order-by.pipe';
Expand Down Expand Up @@ -77,7 +77,7 @@ export class SectionComponent {
private modal: NgbModal,
private toast: ToastrService,
private dialogs: ConfirmationDialogService,
private Question: QuestionService,
private QuestionScore: QuestionScoringService,
private Files: FileService,
private Exam: ExamService,
) {}
Expand Down Expand Up @@ -278,7 +278,7 @@ export class SectionComponent {
optional: this.section.optional,
});

private getQuestionScore = (question: ExamSectionQuestion) => this.Question.calculateMaxScore(question);
private getQuestionScore = (question: ExamSectionQuestion) => this.QuestionScore.calculateMaxScore(question);

private insertExamQuestion = (question: Question, seq: number) => {
const resource = this.collaborative
Expand Down
10 changes: 5 additions & 5 deletions ui/src/app/exam/exam.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { parseISO } from 'date-fns';
import { ToastrService } from 'ngx-toastr';
import type { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { QuestionService } from 'src/app/question/question.service';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { SessionService } from 'src/app/session/session.service';
import { ConfirmationDialogService } from 'src/app/shared/dialogs/confirmation-dialog.service';
import { CommonExamService } from 'src/app/shared/miscellaneous/common-exam.service';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class ExamService {
private translate: TranslateService,
private toast: ToastrService,
private CommonExam: CommonExamService,
private Question: QuestionService,
private QuestionScore: QuestionScoringService,
private Session: SessionService,
private ConfirmationDialog: ConfirmationDialogService,
) {}
Expand Down Expand Up @@ -253,7 +253,7 @@ export class ExamService {

getSectionTotalNumericScore = (section: ExamSection): number => {
const score = section.sectionQuestions.reduce((n, sq) => {
const points = this.Question.calculateAnswerScore(sq);
const points = this.QuestionScore.calculateAnswerScore(sq);
// handle only numeric scores (leave out approved/rejected type of scores)
return n + (points.rejected === false && points.approved === false ? points.score : 0);
}, 0);
Expand All @@ -262,7 +262,7 @@ export class ExamService {

getSectionTotalScore = (section: ExamSection): number => {
const score = section.sectionQuestions.reduce((n, sq) => {
const points = this.Question.calculateAnswerScore(sq);
const points = this.QuestionScore.calculateAnswerScore(sq);
return n + points.score;
}, 0);
return Number.isInteger(score) ? score : parseFloat(score.toFixed(2));
Expand All @@ -273,7 +273,7 @@ export class ExamService {
if (!sq || !sq.question) {
return n;
}
return n + this.Question.calculateMaxScore(sq);
return n + this.QuestionScore.calculateMaxScore(sq);
}, 0);
if (section.lotteryOn) {
maxScore = (maxScore * section.lotteryItemCount) / Math.max(1, section.sectionQuestions.length);
Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/question/basequestion/multiple-choice.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { UpperCasePipe } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { ToastrService } from 'ngx-toastr';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { MultipleChoiceOption, Question, QuestionDraft } from 'src/app/question/question.model';
import { QuestionService } from 'src/app/question/question.service';
import { MultipleChoiceOptionEditorComponent } from './multiple-choice-option.component';
import { WeightedMultipleChoiceOptionEditorComponent } from './weighted-multiple-choice-option.component';

Expand Down Expand Up @@ -114,7 +114,7 @@ export class MultipleChoiceEditorComponent implements OnInit {
constructor(
private translate: TranslateService,
private toast: ToastrService,
private Question: QuestionService,
private QuestionScore: QuestionScoringService,
) {}

ngOnInit() {
Expand All @@ -135,5 +135,5 @@ export class MultipleChoiceEditorComponent implements OnInit {
this.question.options.push(option);
};

calculateDefaultMaxPoints = () => this.Question.calculateDefaultMaxPoints(this.question as Question);
calculateDefaultMaxPoints = () => this.QuestionScore.calculateDefaultMaxPoints(this.question as Question);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ControlContainer, FormsModule, NgForm } from '@angular/forms';
import { NgbPopoverModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { ToastrService } from 'ngx-toastr';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { ExamSectionQuestionOption } from 'src/app/question/question.model';
import { QuestionService } from 'src/app/question/question.service';

@Component({
selector: 'xm-eq-weighted-mc',
Expand Down Expand Up @@ -111,12 +111,12 @@ export class WeightedMultiChoiceComponent {
lotteryOn = input(false);
isInPublishedExam = input(false);
optionsChanged = output<ExamSectionQuestionOption[]>();
maxScore = computed<number>(() => this.QuestionService.calculateWeightedMaxPoints(this.options()));
maxScore = computed<number>(() => this.QuestionScore.calculateWeightedMaxPoints(this.options()));

constructor(
private TranslateService: TranslateService,
private ToastrService: ToastrService,
private QuestionService: QuestionService,
private QuestionScore: QuestionScoringService,
) {}

updateScore = (score: number, index: number) => {
Expand Down
8 changes: 4 additions & 4 deletions ui/src/app/question/library/library.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { SESSION_STORAGE, WebStorageService } from 'ngx-webstorage-service';
import type { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import type { Course, Exam, ExamSection } from 'src/app/exam/exam.model';
import { QuestionScoringService } from 'src/app/question/question-scoring.service';
import { LibraryQuestion, Tag } from 'src/app/question/question.model';
import { QuestionService } from 'src/app/question/question.service';
import { User } from 'src/app/session/session.model';
import { UserService } from 'src/app/shared/user/user.service';

Expand All @@ -18,7 +18,7 @@ export class LibraryService {
constructor(
private http: HttpClient,
@Inject(SESSION_STORAGE) private webStorageService: WebStorageService,
private Question: QuestionService,
private QuestionScore: QuestionScoringService,
private User: UserService,
) {}

Expand Down Expand Up @@ -208,9 +208,9 @@ export class LibraryService {
} else if (q.defaultEvaluationType === 'Selection') {
return 'i18n_evaluation_select';
} else if (q.type === 'WeightedMultipleChoiceQuestion') {
return this.Question.calculateDefaultMaxPoints(q);
return this.QuestionScore.calculateDefaultMaxPoints(q);
} else if (q.type === 'ClaimChoiceQuestion') {
return this.Question.getCorrectClaimChoiceOptionDefaultScore(q);
return this.QuestionScore.getCorrectClaimChoiceOptionDefaultScore(q);
}
return '';
};
Expand Down
168 changes: 168 additions & 0 deletions ui/src/app/question/question-scoring.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// SPDX-FileCopyrightText: 2024 The members of the EXAM Consortium
//
// SPDX-License-Identifier: EUPL-1.2

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Exam, ExamSection } from 'src/app/exam/exam.model';
import { isNumber } from 'src/app/shared/miscellaneous/helpers';
import { ExamSectionQuestion, ExamSectionQuestionOption, Question, QuestionAmounts } from './question.model';

@Injectable({ providedIn: 'root' })
export class QuestionScoringService {
constructor(private httpClient: HttpClient) {}

getQuestionAmounts = (exam: Exam): QuestionAmounts => {
const essays = exam.examSections
.flatMap((es) => es.sectionQuestions)
.filter((esq) => esq.question.type === 'EssayQuestion');
const scores = essays
.filter((e) => e.evaluationType === 'Selection' && e.essayAnswer)
.map((e) => e.essayAnswer?.evaluatedScore);
const accepted = scores.filter((s) => s === 1).length;
const rejected = scores.filter((s) => s === 0).length;
return { accepted: accepted, rejected: rejected, hasEssays: essays.length > 0 };
};

getEssayQuestionAmountsBySection = (section: ExamSection) => {
const scores = section.sectionQuestions
.filter((sq) => sq.question.type === 'EssayQuestion' && sq.evaluationType === 'Selection' && sq.essayAnswer)
.map((sq) => sq.essayAnswer?.evaluatedScore);
return {
accepted: scores.filter((s) => s === 1).length,
rejected: scores.filter((s) => s === 0).length,
total: scores.length,
};
};

calculateDefaultMaxPoints = (question: Question) =>
question.options.filter((o) => o.defaultScore > 0).reduce((a, b) => a + b.defaultScore, 0);

getMinimumOptionScore = (sectionQuestion: ExamSectionQuestion): number => {
const optionScores = sectionQuestion.options.map((o) => o.score);
const scores = [0, ...optionScores]; // Make sure 0 is included
return sectionQuestion.question.type === 'WeightedMultipleChoiceQuestion'
? Math.max(0, Math.min(...scores)) // Weighted mcq mustn't have a negative min score
: Math.min(...scores);
};

getCorrectClaimChoiceOptionDefaultScore = (question: Question): number => {
if (!question.options) {
return 0;
}
const correctOption = question.options.filter((o) => o.correctOption && o.claimChoiceType === 'CorrectOption');
return correctOption.length === 1 ? correctOption[0].defaultScore : 0;
};

scoreClozeTestAnswer = (sectionQuestion: ExamSectionQuestion): number => {
if (!sectionQuestion.clozeTestAnswer) {
return 0;
}
if (isNumber(sectionQuestion.forcedScore)) {
return sectionQuestion.forcedScore;
}
const score = sectionQuestion.clozeTestAnswer.score;
if (!score) return 0;
const proportion =
(score.correctAnswers * sectionQuestion.maxScore) / (score.correctAnswers + score.incorrectAnswers);
return parseFloat(proportion.toFixed(2));
};

scoreWeightedMultipleChoiceAnswer = (sectionQuestion: ExamSectionQuestion, ignoreForcedScore: boolean): number => {
if (isNumber(sectionQuestion.forcedScore) && !ignoreForcedScore) {
return sectionQuestion.forcedScore;
}
const score = sectionQuestion.options.filter((o) => o.answered).reduce((a, b) => a + b.score, 0);
return Math.max(0, score);
};

// For non-weighted mcq
scoreMultipleChoiceAnswer = (sectionQuestion: ExamSectionQuestion, ignoreForcedScore: boolean): number => {
if (isNumber(sectionQuestion.forcedScore) && !ignoreForcedScore) {
return sectionQuestion.forcedScore;
}
const answered = sectionQuestion.options.filter((o) => o.answered);
if (answered.length === 0) {
// No answer
return 0;
}
if (answered.length !== 1) {
console.error('multiple options selected for a MultiChoice answer!');
}

return answered[0].option.correctOption ? sectionQuestion.maxScore : 0;
};

scoreClaimChoiceAnswer = (sectionQuestion: ExamSectionQuestion, ignoreForcedScore: boolean): number => {
if (isNumber(sectionQuestion.forcedScore) && !ignoreForcedScore) {
return sectionQuestion.forcedScore;
}
const selected = sectionQuestion.options.filter((o) => o.answered);

// Use the score from the skip option if no option was chosen
const skipOption = sectionQuestion.options.filter((o) => o.option.claimChoiceType === 'SkipOption');
const skipScore = skipOption.length === 1 ? skipOption[0].score : 0;

if (selected.length === 0) {
return skipScore;
}
if (selected.length !== 1) {
console.error('multiple options selected for a ClaimChoice answer!');
}
if (selected[0].score && isNumber(selected[0].score)) {
return selected[0].score;
}
return 0;
};

calculateAnswerScore = (sq: ExamSectionQuestion) => {
switch (sq.question.type) {
case 'MultipleChoiceQuestion':
return { score: this.scoreMultipleChoiceAnswer(sq, false), rejected: false, approved: false };
case 'WeightedMultipleChoiceQuestion':
return { score: this.scoreWeightedMultipleChoiceAnswer(sq, false), rejected: false, approved: false };
case 'ClozeTestQuestion':
return { score: this.scoreClozeTestAnswer(sq), rejected: false, approved: false };
case 'EssayQuestion':
if (sq.essayAnswer && sq.essayAnswer.evaluatedScore && sq.evaluationType === 'Points') {
return { score: sq.essayAnswer.evaluatedScore, rejected: false, approved: false };
} else if (sq.essayAnswer && sq.essayAnswer.evaluatedScore && sq.evaluationType === 'Selection') {
const score = sq.essayAnswer.evaluatedScore;
return { score: score, rejected: score === 0, approved: score === 1 };
}
return { score: 0, rejected: false, approved: false };
case 'ClaimChoiceQuestion':
return { score: this.scoreClaimChoiceAnswer(sq, false), rejected: false, approved: false };
default:
throw Error('unknown question type');
}
};

calculateWeightedMaxPoints = (options: ExamSectionQuestionOption[]): number => {
const points = options.filter((o) => o.score > 0).reduce((a, b) => a + b.score, 0);
return parseFloat(points.toFixed(2));
};

getCorrectClaimChoiceOptionScore = (sectionQuestion: ExamSectionQuestion): number => {
if (!sectionQuestion.options) {
return 0;
}
const optionScores = sectionQuestion.options.map((o) => o.score);
return Math.max(0, ...optionScores);
};

calculateMaxScore = (question: ExamSectionQuestion) => {
const evaluationType = question.evaluationType;
const type = question.question.type;
if (evaluationType === 'Points' || type === 'MultipleChoiceQuestion' || type === 'ClozeTestQuestion') {
return question.maxScore;
}
if (type === 'WeightedMultipleChoiceQuestion') {
return this.calculateWeightedMaxPoints(question.options);
}
if (type === 'ClaimChoiceQuestion') {
return this.getCorrectClaimChoiceOptionScore(question);
}
return 0;
};
}
Loading

0 comments on commit 0c24bda

Please sign in to comment.