Skip to content
Draft
Changes from all 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
115 changes: 69 additions & 46 deletions src/app/exams/exams-view.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
import { UntypedFormControl, AbstractControl } from '@angular/forms';
import { AbstractControl, FormBuilder, FormControl, FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { MatLegacyDialog as MatDialog } from '@angular/material/legacy-dialog';
import { Subject, forkJoin, of } from 'rxjs';
Expand All @@ -15,6 +15,14 @@ import {
} from '../shared/dialogs/dialogs-announcement.component';
import { StateService } from '../shared/state.service';

type ExamAnswerOption = { id: string; text: string; isOther?: boolean };

type ExamAnswerValue = string | ExamAnswerOption | ExamAnswerOption[] | null;

interface ExamViewForm {
answer: FormControl<ExamAnswerValue>;
}

@Component({
selector: 'planet-exams-view',
templateUrl: './exams-view.component.html',
Expand All @@ -33,7 +41,32 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
question: ExamQuestion;
stepNum = 0;
maxQuestions = 0;
answer = new UntypedFormControl(null, this.answerValidator);
private readonly answerValidator: ValidatorFn = (ac: AbstractControl<ExamAnswerValue | ExamAnswerOption[] | null>): ValidationErrors | null => {
if (typeof ac.value === 'string') {
return ac.value.trim() ? null : { required: true };
}

if (Array.isArray(ac.value)) {
if (ac.value.length === 0) {
return { required: true };
}
const hasEmptyOther = ac.value.some(option =>
option && option.isOther && (!option.text || !option.text.trim())
);
return hasEmptyOther ? { required: true } : null;
}
if (ac.value && ac.value.isOther && (!ac.value.text || !ac.value.text.trim())) {
return { required: true };
}

return ac.value !== null && ac.value !== undefined ? null : { required: true };
};

readonly examForm: FormGroup<ExamViewForm>;

get answer(): FormControl<ExamAnswerValue> {
return this.examForm.controls.answer;
}
statusMessage = '';
spinnerOn = true;
title = '';
Expand All @@ -52,7 +85,7 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
isLoading = true;
courseId: string;
teamId = this.route.snapshot.params.teamId || null;
currentOtherOption: { id: 'other'; text: string; isOther: true } | null = null;
currentOtherOption: ExamAnswerOption & { isOther: true } = { id: 'other', text: '', isOther: true };

constructor(
private router: Router,
Expand All @@ -64,7 +97,12 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
private planetMessageService: PlanetMessageService,
private dialog: MatDialog,
private stateService: StateService,
) { }
private formBuilder: FormBuilder,
) {
this.examForm = this.formBuilder.group({
answer: this.formBuilder.control<ExamAnswerValue>(null, { validators: this.answerValidator })
});
}

ngOnInit() {
this.setCourseListener();
Expand Down Expand Up @@ -289,16 +327,14 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
});
}

setAnswer(event, option) {
const value = this.answer.value || [];
setAnswer(event: { checked: boolean }, option: ExamAnswerOption) {
const value: ExamAnswerOption[] = Array.isArray(this.answer.value) ? [ ...this.answer.value ] : [];
if (event.checked) {
if (!value.some(val => val.id === option.id)) {
const existingIndex = value.findIndex(val => val.id === option.id);
if (existingIndex === -1) {
value.push(option);
} else if (option.id === 'other') {
const otherIndex = value.findIndex(val => val.id === 'other');
if (otherIndex > -1) {
value[otherIndex].text = option.text;
}
value[existingIndex] = { ...value[existingIndex], text: option.text };
}
} else {
const index = value.findIndex(val => val.id === option.id);
Expand All @@ -319,17 +355,23 @@ export class ExamsViewComponent implements OnInit, OnDestroy {

calculateCorrect() {
const value = this.answer.value;
const answers = value instanceof Array ? value : [ value ];
if (answers.every(answer => answer === null || answer === undefined)) {
const answers = Array.isArray(value) ? value : [ value ];
const answerIds = answers
.map(ans => typeof ans === 'string' ? ans : ans?.id)
.filter((id): id is string => !!id);

if (answerIds.length === 0) {
return undefined;
}
const isMultiCorrect = (correctChoice, ans: any[]) => (
correctChoice.every(choice => ans.find((a: any) => a.id === choice)) &&
ans.every((a: any) => correctChoice.find(choice => a.id === choice))

const isMultiCorrect = (correctChoice: string[], ans: string[]) => (
correctChoice.every(choice => ans.includes(choice)) &&
ans.every(answer => correctChoice.includes(answer))
);

return this.question.correctChoice instanceof Array ?
isMultiCorrect(this.question.correctChoice, answers) :
answers[0].id === this.question.correctChoice;
isMultiCorrect(this.question.correctChoice, answerIds) :
answerIds[0] === this.question.correctChoice;
}

createAnswerObservable(isFinish = false) {
Expand All @@ -348,7 +390,7 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
}

setAnswerForRetake(answer: any) {
const setSelectMultipleAnswer = (answers: any[]) => {
const setSelectMultipleAnswer = (answers: ExamAnswerOption[]) => {
answers.forEach(ans => {
this.setAnswer({ checked: true }, ans);
});
Expand All @@ -359,7 +401,7 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
}
switch (this.question.type) {
case 'selectMultiple':
const rebuilt = answer.value.map(val => {
const rebuilt = (answer.value as ExamAnswerOption[]).map(val => {
if (val.id === 'other') {
this.currentOtherOption.text = val.text || '';
return this.currentOtherOption;
Expand All @@ -373,33 +415,13 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
this.currentOtherOption.text = answer.value.text;
this.answer.setValue(this.currentOtherOption);
} else {
this.answer.setValue(this.question.choices.find((choice) => choice.text === answer.value.text));
const selectedChoice = this.question.choices.find((choice) => choice.text === answer.value.text) || null;
this.answer.setValue(selectedChoice);
}
break;
default:
this.answer.setValue(answer.value);
}
}

answerValidator(ac: AbstractControl) {
if (typeof ac.value === 'string') {
return ac.value.trim() ? null : { required: true };
}

if (Array.isArray(ac.value)) {
if (ac.value.length === 0) {
return { required: true };
}
const hasEmptyOther = ac.value.some(option =>
option && option.isOther && (!option.text || !option.text.trim())
);
return hasEmptyOther ? { required: true } : null;
this.answer.setValue(answer.value as ExamAnswerValue);
}
if (ac.value && ac.value.isOther && (!ac.value.text || !ac.value.text.trim())) {
return { required: true };
}

return ac.value !== null && ac.value !== undefined ? null : { required: true };
}

setViewAnswerText(answer: any) {
Expand All @@ -409,15 +431,16 @@ export class ExamsViewComponent implements OnInit, OnDestroy {
}

isOtherSelected() {
return this.answer.value?.id === 'other';
const value = this.answer.value;
return !!value && !Array.isArray(value) && typeof value !== 'string' && value.id === 'other';
}

toggleOtherMultiple({ checked }): void {
toggleOtherMultiple({ checked }: { checked: boolean }): void {
this.checkboxState['other'] = checked;
if (checked) {
this.setAnswer({ checked: true }, this.currentOtherOption);
} else {
const remaining = (this.answer.value || []).filter(o => o.id !== 'other');
const remaining = Array.isArray(this.answer.value) ? this.answer.value.filter(o => o.id !== 'other') : [];
this.answer.setValue(remaining.length ? remaining : null);
this.answer.updateValueAndValidity();
}
Expand Down
Loading