diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 50f114437eb2aaec92ba4ecf1144afdc9e6136dd..f1834cbec00849ab5f244cd3421e2f644678a9f2 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -123,7 +123,8 @@ import { AnswerEditComponent } from './components/dialogs/answer-edit/answer-edi PasswordResetComponent, RoomCreateComponent, RoomDeleteComponent, - RoomEditComponent + RoomEditComponent, + AnswerEditComponent ], imports: [ AppRoutingModule, diff --git a/src/app/components/dialogs/answer-edit/answer-edit.component.html b/src/app/components/dialogs/answer-edit/answer-edit.component.html index 87879964c6a8614420b02a2c211693fa3e5b8d88..797df3faf590ab249296c1cc70d5c175db3d6918 100644 --- a/src/app/components/dialogs/answer-edit/answer-edit.component.html +++ b/src/app/components/dialogs/answer-edit/answer-edit.component.html @@ -1,3 +1,19 @@ -<p> - answer-edit works! -</p> +<div *ngIf="answer"> + <mat-form-field class="input-block"> + <input [(ngModel)]="answer.answerOption.label" #roomName matInput placeholder="Answer label" name="answer-label"/> + </mat-form-field> + <mat-form-field class="input-block"> + <textarea [(ngModel)]="answer.answerOption.points" #roomDescription matInput matTextareaAutosize + matAutosizeMinRows="2" matAutosizeMaxRows="5" placeholder="Points" name="points"> + </textarea> + </mat-form-field> + <mat-checkbox [(ngModel)]="answer.correct" color="primary">Answer is {{answer.correct}}.</mat-checkbox> + <div fxLayout="row" fxLayoutAlign="center" fxLayoutGap="10px"> + <button (click)="dialogRef.close()" mat-button color="primary"> + Leave + </button> + <button (click)="dialogRef.close('edit')" mat-raised-button color="primary"> + Update + </button> + </div> +</div> diff --git a/src/app/components/dialogs/answer-edit/answer-edit.component.ts b/src/app/components/dialogs/answer-edit/answer-edit.component.ts index 61642ebb0fcd424d79ecb88b2290da10b0327605..c3627bd627dee4e3343a9dbd9361a67fc79a5e0f 100644 --- a/src/app/components/dialogs/answer-edit/answer-edit.component.ts +++ b/src/app/components/dialogs/answer-edit/answer-edit.component.ts @@ -1,4 +1,6 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, Inject, OnInit } from '@angular/core'; +import { ContentChoiceCreatorComponent, DisplayAnswer } from '../../fragments/content-choice-creator/content-choice-creator.component'; +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; @Component({ selector: 'app-answer-edit', @@ -6,10 +8,16 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./answer-edit.component.scss'] }) export class AnswerEditComponent implements OnInit { + answer: DisplayAnswer; - constructor() { } + constructor(public dialogRef: MatDialogRef<ContentChoiceCreatorComponent>, + @Inject(MAT_DIALOG_DATA) public data: any) { + } ngOnInit() { } + onNoClick(): void { + this.dialogRef.close(); + } } 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 5027652d6350280a92d7f3b120f77f4ea482010c..14da28b01576bb8c447d307635ff05273c21ccd5 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 @@ -23,9 +23,10 @@ <mat-cell *matCellDef="let answer"> <!-- ToDo: Check ngModel --> <mat-checkbox color="primary" [(ngModel)]="answer.correct" [checked]="answer.correct" - name="{{ answer.answerOption.label }}">{{ answer.answerOption.label }} is {{ answer.correct }} + name="{{ answer.answerOption.label }}" disabled>{{ answer.answerOption.label }} </mat-checkbox> - <button mat-raised-button color="primary" (click)="deleteAnswer(answer.answerOption.label)">Delete</button> + <button mat-icon-button (click)="openAnswerModificationDialog(answer.answerOption.label, answer.answerOption.points, answer.correct)" color="primary"><mat-icon>edit</mat-icon></button> + <button mat-icon-button color="primary" (click)="deleteAnswer(answer.answerOption.label)"><mat-icon>delete</mat-icon></button> </mat-cell> </ng-container> 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 aa2a48ed78c8281e6c6614123fa1c1ce7f46ce43..ab643983509c7e3ffae077c21efa8097e0c82b46 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 @@ -3,6 +3,8 @@ import { AnswerOption } from '../../../models/answer-option'; import { ContentChoice } from '../../../models/content-choice'; import { ContentService } from '../../../services/http/content.service'; import { NotificationService } from '../../../services/util/notification.service'; +import { MatDialog } from '@angular/material'; +import { AnswerEditComponent } from '../../dialogs/answer-edit/answer-edit.component'; export class DisplayAnswer { answerOption: AnswerOption; @@ -41,8 +43,12 @@ export class ContentChoiceCreatorComponent implements OnInit { newAnswerOptionLabel = ''; newAnswerOptionPoints = ''; + editDisplayAnswer: DisplayAnswer; + originalDisplayAnswer: DisplayAnswer; + constructor(private contentService: ContentService, - private notificationService: NotificationService) { + private notificationService: NotificationService, + public dialog: MatDialog) { } ngOnInit() { @@ -92,6 +98,50 @@ export class ContentChoiceCreatorComponent implements OnInit { this.submitContent(); } + openAnswerModificationDialog(label: string, points: string, correct: boolean) { + this.editDisplayAnswer = new DisplayAnswer(new AnswerOption(label, points), correct); + this.originalDisplayAnswer = new DisplayAnswer(new AnswerOption(label, points), correct); + const dialogRef = this.dialog.open(AnswerEditComponent, { + width: '400px' + }); + dialogRef.componentInstance.answer = this.editDisplayAnswer; + dialogRef.afterClosed() + .subscribe(result => { + if (result === 'edit') { + this.editCheckChanges(); + } + }); + } + + 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 !== this.editDisplayAnswer.answerOption.label) { + this.content.options[i].label = this.editDisplayAnswer.answerOption.label; + } + if (this.originalDisplayAnswer.answerOption.points !== this.editDisplayAnswer.answerOption.points) { + 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++) { + console.log(this.content.correctOptionIndexes); + if (this.content.correctOptionIndexes[j] === i && !this.editDisplayAnswer.correct) { + this.content.correctOptionIndexes.splice(j, 1); + console.log(this.content.correctOptionIndexes); + } + } + } + if (this.editDisplayAnswer.correct) { + this.content.correctOptionIndexes.push(i); + } + } + } + } + this.fillCorrectAnswers(); + } + deleteAnswer(label: string) { console.log('deleteAnswer: ' + label); console.log('Antwortmöglichkeiten vorher:'); @@ -132,7 +182,7 @@ export class ContentChoiceCreatorComponent implements OnInit { if (this.lastDeletedDisplayAnswer.correct) { this.content.correctOptionIndexes.push(this.content.options.length - 1); } - this.notificationService.show('Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully recovered.') + this.notificationService.show('Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully recovered.'); this.lastDeletedDisplayAnswer = null; this.fillCorrectAnswers(); } @@ -143,4 +193,4 @@ export class ContentChoiceCreatorComponent implements OnInit { } -//TODO: nicht 2x die gleiche Antwort +// TODO: nicht 2x die gleiche Antwort