diff --git a/src/app/components/fragments/content-choice-creator/content-choice-creator.component.html b/src/app/components/fragments/content-choice-creator/content-choice-creator.component.html index d2866b0b0d4bcb606e1e05ad89dca5e8b06d0ccb..86d65e471251096d86e569a2f0510adbd500b1ab 100644 --- a/src/app/components/fragments/content-choice-creator/content-choice-creator.component.html +++ b/src/app/components/fragments/content-choice-creator/content-choice-creator.component.html @@ -68,7 +68,7 @@ </mat-form-field> <div fxLayout="column" fxLayoutAlign="center"> <button mat-button type="button" - (click)="addAnswer(); answerIsCorrect.checked = false; answerLabel.value = ''; answerPoints.value = ''"> + (click)="addAnswer($event); answerIsCorrect.checked = false; answerLabel.value = ''; answerPoints.value = ''"> Add Answer </button> </div> @@ -76,7 +76,7 @@ <button mat-raised-button type="submit" color="primary">Submit</button> <button mat-raised-button (click)="reset($event)" color="primary">Reset</button> - <button mat-raised-button *ngIf="lastDeletedDisplayAnswer" (click)="recoverDeletedAnswer()" color="primary">Undo + <button mat-raised-button *ngIf="lastDeletedDisplayAnswer" (click)="recoverDeletedAnswer($event)" color="primary">Undo deletion </button> </div> diff --git a/src/app/components/fragments/content-choice-creator/content-choice-creator.component.ts b/src/app/components/fragments/content-choice-creator/content-choice-creator.component.ts index 680c67a74a5e1c1b158283ff658394724e7ec3a4..54b09194e6d80315b63d4506ddfb6866bf38eb4a 100644 --- a/src/app/components/fragments/content-choice-creator/content-choice-creator.component.ts +++ b/src/app/components/fragments/content-choice-creator/content-choice-creator.component.ts @@ -62,31 +62,8 @@ export class ContentChoiceCreatorComponent implements OnInit { } } - submitContent() { - if (this.content.options.length === 0) { - this.notificationService.show('Choice content needs answers. Please add some answers.'); - return; - } - if (this.singleChoice && this.content.correctOptionIndexes.length !== 1) { - this.notificationService.show('In single choice mode you have to select 1 true answer.'); - return; - } - if (this.singleChoice) { - this.content.multiple = false; - } - if (this.multipleChoice) { - this.content.multiple = true; - } - this.notificationService.show('Content submitted.'); - /* if (this.content.contentId === '0') { - this.contentService.addContent(this.content).subscribe(); - } else { - // ToDo: Implement function in service - // this.contentService.updateContent(this.content).subscribe(); - } */ - } - - addAnswer() { + addAnswer($event) { + $event.preventDefault(); if (this.newAnswerOptionLabel === '') { this.notificationService.show('No empty answers allowed.'); this.newAnswerOptionChecked = false; @@ -118,6 +95,12 @@ export class ContentChoiceCreatorComponent implements OnInit { } openAnswerModificationDialog(label: string, points: string, correct: boolean) { + let index = -1; + for (let i = 0; i < this.content.options.length; i++) { + if (this.content.options[i].label.valueOf() === label.valueOf()) { + index = i; + } + } this.editDisplayAnswer = new DisplayAnswer(new AnswerOption(label, points), correct); this.originalDisplayAnswer = new DisplayAnswer(new AnswerOption(label, points), correct); const dialogRef = this.dialog.open(AnswerEditComponent, { @@ -127,40 +110,29 @@ export class ContentChoiceCreatorComponent implements OnInit { dialogRef.afterClosed() .subscribe(result => { if (result === 'edit') { - this.editCheckChanges(); + // this.editCheckChanges(); + this.saveChanges(index, this.editDisplayAnswer); } }); } - editCheckChanges() { - for (let i = 0; i < this.content.options.length; i++) { - if (this.content.options[i].label === this.originalDisplayAnswer.answerOption.label) { - if (this.originalDisplayAnswer.answerOption.label.valueOf() !== this.editDisplayAnswer.answerOption.label.valueOf()) { - this.content.options[i].label = this.editDisplayAnswer.answerOption.label; - } - if (this.originalDisplayAnswer.answerOption.points.valueOf() !== this.editDisplayAnswer.answerOption.points.valueOf()) { - this.content.options[i].points = this.editDisplayAnswer.answerOption.points; - } - if (this.originalDisplayAnswer.correct !== this.editDisplayAnswer.correct) { - if (!this.editDisplayAnswer.correct) { - for (let j = 0; i < this.content.correctOptionIndexes.length; j++) { - if (this.content.correctOptionIndexes[j] === i && !this.editDisplayAnswer.correct) { - this.content.correctOptionIndexes.splice(j, 1); - } - } - } - if (this.editDisplayAnswer.correct) { - if (this.singleChoice && this.content.correctOptionIndexes.length > 0) { - this.notificationService.show('In single mode is only 1 selected answer allowed.'); - this.editDisplayAnswer.correct = false; - } else { - this.content.correctOptionIndexes.push(i); - } - } - } + saveChanges(index: number, answer: DisplayAnswer) { + this.content.options[index].label = answer.answerOption.label; + this.content.options[index].points = answer.answerOption.points; + const indexInCorrectOptionIndexes = this.content.correctOptionIndexes.indexOf(index); + if (indexInCorrectOptionIndexes === -1 && answer.correct) { + if (this.singleChoice) { + this.content.correctOptionIndexes = [index]; + this.fillCorrectAnswers(); + return; } + this.content.correctOptionIndexes.push(index); + } + if (indexInCorrectOptionIndexes !== -1 && !answer.correct) { + this.content.correctOptionIndexes.splice(indexInCorrectOptionIndexes, 1); } this.fillCorrectAnswers(); + this.notificationService.show('Update changes.'); } deleteAnswer(label: string) { @@ -173,7 +145,7 @@ export class ContentChoiceCreatorComponent implements OnInit { this.lastDeletedDisplayAnswer.correct = true; this.content.correctOptionIndexes.splice(j, 1); } - if (this.content.correctOptionIndexes[j] >= i) { + if (this.content.correctOptionIndexes[j] > i) { // [j] > i this.content.correctOptionIndexes[j] = this.content.correctOptionIndexes[j] - 1; } } @@ -183,7 +155,8 @@ export class ContentChoiceCreatorComponent implements OnInit { this.notificationService.show('Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully deleted.'); } - recoverDeletedAnswer() { + recoverDeletedAnswer($event) { + $event.preventDefault(); let msgAddon = 'Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully recovered.'; if (this.lastDeletedDisplayAnswer === null) { this.notificationService.show('Nothing to recover'); @@ -208,21 +181,33 @@ export class ContentChoiceCreatorComponent implements OnInit { } switchValue(label: string) { + let index: number; + let isCorrect: boolean; + + // Get id of answer for (let i = 0; i < this.content.options.length; i++) { if (this.content.options[i].label.valueOf() === label.valueOf()) { - if (this.content.correctOptionIndexes.length === 0) { - this.content.correctOptionIndexes.push(i); - this.fillCorrectAnswers(); - console.log(this.content.correctOptionIndexes); - return; - } - const index = this.content.correctOptionIndexes.indexOf(i); - if (index === -1) { - this.content.correctOptionIndexes.push(i); + index = i; + break; + } + } + // Check if answer is marked as correct + isCorrect = !this.displayAnswers[index].correct; + + // Update correct answers + if (isCorrect) { + if (this.content.correctOptionIndexes.indexOf(index) === -1) { + // ToDo: Set back to data model + if (this.multipleChoice) { + this.content.correctOptionIndexes.push(index); } else { - this.content.correctOptionIndexes.splice(index, 1); + this.content.correctOptionIndexes = [index]; } } + } else { + if (this.content.correctOptionIndexes.indexOf(index) !== -1) { + this.content.correctOptionIndexes.splice(index, 1); + } } this.fillCorrectAnswers(); } @@ -236,4 +221,28 @@ export class ContentChoiceCreatorComponent implements OnInit { this.fillCorrectAnswers(); this.notificationService.show('Reset all inputs to default.'); } + + submitContent() { + if (this.content.options.length === 0) { + this.notificationService.show('Choice content needs answers. Please add some answers.'); + return; + } + if (this.singleChoice && this.content.correctOptionIndexes.length !== 1) { + this.notificationService.show('In single choice mode you have to select 1 true answer.'); + return; + } + if (this.singleChoice) { + this.content.multiple = false; + } + if (this.multipleChoice) { + this.content.multiple = true; + } + this.notificationService.show('Content submitted.'); + /* if (this.content.contentId === '0') { + this.contentService.addContent(this.content).subscribe(); + } else { + // ToDo: Implement function in service + // this.contentService.updateContent(this.content).subscribe(); + } */ + } } diff --git a/src/app/components/fragments/content-choice-participant/content-choice-participant.component.ts b/src/app/components/fragments/content-choice-participant/content-choice-participant.component.ts index ee1c2dd68c9421e1792f315fe84afb48222b0b97..28edee30bcaa670556b724cd219c42f66034ff73 100644 --- a/src/app/components/fragments/content-choice-participant/content-choice-participant.component.ts +++ b/src/app/components/fragments/content-choice-participant/content-choice-participant.component.ts @@ -77,12 +77,15 @@ export class ContentChoiceParticipantComponent implements OnInit { contentId: this.content.contentId, round: this.content.round, selectedChoiceIndexes: selectedAnswers, - } as AnswerChoice).subscribe(); + } as AnswerChoice).subscribe(result => { + TODO: Set isAnswerSent + }); */ } abstain($event) { $event.preventDefault(); console.log('abstain'); + // ToDo: Send emtpy answer to backend } } diff --git a/src/app/components/fragments/content-likert-creator/content-likert-creator.component.ts b/src/app/components/fragments/content-likert-creator/content-likert-creator.component.ts index c8ff79baed6a3e6153e988c56585c5bf1aa6a4b8..e87b1ed5611af9c47440b334144cae7364c576c6 100644 --- a/src/app/components/fragments/content-likert-creator/content-likert-creator.component.ts +++ b/src/app/components/fragments/content-likert-creator/content-likert-creator.component.ts @@ -50,6 +50,8 @@ export class ContentLikertCreatorComponent implements OnInit { this.fillCorrectAnswers(); } + // TODO + submitContent(): void { console.log('submitContent'); } diff --git a/src/app/components/fragments/content-text-participant/content-text-participant.component.ts b/src/app/components/fragments/content-text-participant/content-text-participant.component.ts index 851581a689254e602bde9d8b98fed73dc52a0115..f3938643d8a4a86355bfa5ed27e7aef24e544e6c 100644 --- a/src/app/components/fragments/content-text-participant/content-text-participant.component.ts +++ b/src/app/components/fragments/content-text-participant/content-text-participant.component.ts @@ -38,16 +38,20 @@ export class ContentTextParticipantComponent implements OnInit { } this.isAnswerSent = true; this.notificationService.show('Answer successfully sent.'); - this.answerService.addAnswerText({ - id: '0', - revision: this.content.revision, - contentId: this.content.contentId, - round: this.content.round, - subject: this.content.subject, - body: this.textAnswer, - read: 'false', - creationTimestamp: new Date() - } as AnswerText).subscribe(); + /* + // ToDo: Check correct api call + this.answerService.addAnswerText({ + id: '0', + revision: this.content.revision, + contentId: this.content.contentId, + round: this.content.round, + subject: this.content.subject, + body: this.textAnswer, + read: 'false', + creationTimestamp: new Date() + } as AnswerText).subscribe(result => { + // TODO: Set isAnswerSent + }); */ } abstain($event) { diff --git a/src/app/components/fragments/content-yes-no-creator/content-yes-no-creator.component.ts b/src/app/components/fragments/content-yes-no-creator/content-yes-no-creator.component.ts index fbbc4bfd2d59ca2d483d520556208260d1629463..351a5263ae702afb39071f17122fcef929d15659 100644 --- a/src/app/components/fragments/content-yes-no-creator/content-yes-no-creator.component.ts +++ b/src/app/components/fragments/content-yes-no-creator/content-yes-no-creator.component.ts @@ -45,6 +45,7 @@ export class ContentYesNoCreatorComponent implements OnInit { this.displayAnswers.push(new DisplayAnswer(this.content.options[i], this.content.correctOptionIndexes.includes(i))); } } + setCorrect(label: string) { if (label === 'yes') { this.content.correctOptionIndexes = [0]; @@ -54,10 +55,13 @@ export class ContentYesNoCreatorComponent implements OnInit { } this.fillCorrectAnswers(); } + checkAllowedContent(): boolean { return (this.content.correctOptionIndexes.length === 1); } + + // TODO submitContent(): void { if (!this.checkAllowedContent()) { this.notificationService.show('Select 1 true answer.');