Skip to content
Snippets Groups Projects
Commit d5772de2 authored by Thomas Lenz's avatar Thomas Lenz
Browse files

Shrink code, bugfix logic in creator's multiple choice content, add $event to...

Shrink code, bugfix logic in creator's multiple choice content, add $event to all "button"-methods to prevent problems if it is interpreted as submit, add console.log in submit-method for testing, add reset-method to set all values to default after submit
parent 202836cf
No related merge requests found
...@@ -38,11 +38,11 @@ ...@@ -38,11 +38,11 @@
<mat-header-cell *matHeaderCellDef>Actions</mat-header-cell> <mat-header-cell *matHeaderCellDef>Actions</mat-header-cell>
<mat-cell *matCellDef="let answer"> <mat-cell *matCellDef="let answer">
<button mat-icon-button <button mat-icon-button
(click)="openAnswerModificationDialog(answer.answerOption.label, answer.answerOption.points, answer.correct)" (click)="openAnswerModificationDialog($event, answer.answerOption.label, answer.answerOption.points, answer.correct)"
color="primary" matTooltip="Edit answer"> color="primary" matTooltip="Edit answer">
<mat-icon>edit</mat-icon> <mat-icon>edit</mat-icon>
</button> </button>
<button mat-icon-button color="primary" (click)="deleteAnswer(answer.answerOption.label)" matTooltip="Delete answer"> <button mat-icon-button color="primary" (click)="deleteAnswer($event, answer.answerOption.label)" matTooltip="Delete answer">
<mat-icon>delete</mat-icon> <mat-icon>delete</mat-icon>
</button> </button>
</mat-cell> </mat-cell>
......
...@@ -101,7 +101,8 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -101,7 +101,8 @@ export class ContentChoiceCreatorComponent implements OnInit {
this.fillCorrectAnswers(); this.fillCorrectAnswers();
} }
openAnswerModificationDialog(label: string, points: string, correct: boolean) { openAnswerModificationDialog($event, label: string, points: string, correct: boolean) {
$event.preventDefault();
let index = -1; let index = -1;
for (let i = 0; i < this.content.options.length; i++) { for (let i = 0; i < this.content.options.length; i++) {
if (this.content.options[i].label.valueOf() === label.valueOf()) { if (this.content.options[i].label.valueOf() === label.valueOf()) {
...@@ -117,13 +118,12 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -117,13 +118,12 @@ export class ContentChoiceCreatorComponent implements OnInit {
dialogRef.afterClosed() dialogRef.afterClosed()
.subscribe(result => { .subscribe(result => {
if (result === 'edit') { if (result === 'edit') {
// this.editCheckChanges(); this.saveChanges(index, this.editDisplayAnswer, true);
this.saveChanges(index, this.editDisplayAnswer);
} }
}); });
} }
saveChanges(index: number, answer: DisplayAnswer) { saveChanges(index: number, answer: DisplayAnswer, matDialogOutput: boolean) {
this.content.options[index].label = answer.answerOption.label; this.content.options[index].label = answer.answerOption.label;
this.content.options[index].points = answer.answerOption.points; this.content.options[index].points = answer.answerOption.points;
const indexInCorrectOptionIndexes = this.content.correctOptionIndexes.indexOf(index); const indexInCorrectOptionIndexes = this.content.correctOptionIndexes.indexOf(index);
...@@ -139,10 +139,13 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -139,10 +139,13 @@ export class ContentChoiceCreatorComponent implements OnInit {
this.content.correctOptionIndexes.splice(indexInCorrectOptionIndexes, 1); this.content.correctOptionIndexes.splice(indexInCorrectOptionIndexes, 1);
} }
this.fillCorrectAnswers(); this.fillCorrectAnswers();
this.notificationService.show('Update changes.'); if (matDialogOutput) {
this.notificationService.show('Update changes.');
}
} }
deleteAnswer(label: string) { deleteAnswer($event, label: string) {
$event.preventDefault();
for (let i = 0; i < this.content.options.length; i++) { for (let i = 0; i < this.content.options.length; i++) {
if (this.content.options[i].label.valueOf() === label.valueOf()) { if (this.content.options[i].label.valueOf() === label.valueOf()) {
this.lastDeletedDisplayAnswer = new DisplayAnswer(this.content.options[i], false); this.lastDeletedDisplayAnswer = new DisplayAnswer(this.content.options[i], false);
...@@ -166,7 +169,7 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -166,7 +169,7 @@ export class ContentChoiceCreatorComponent implements OnInit {
$event.preventDefault(); $event.preventDefault();
let msgAddon = 'Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully recovered.'; let msgAddon = 'Answer "' + this.lastDeletedDisplayAnswer.answerOption.label + '" successfully recovered.';
if (this.lastDeletedDisplayAnswer === null) { if (this.lastDeletedDisplayAnswer === null) {
this.notificationService.show('Nothing to recover'); this.notificationService.show('Nothing to recover.');
} }
for (let i = 0; i < this.content.options.length; i++) { for (let i = 0; i < this.content.options.length; i++) {
if (this.content.options[i].label.valueOf() === this.lastDeletedDisplayAnswer.answerOption.label.valueOf()) { if (this.content.options[i].label.valueOf() === this.lastDeletedDisplayAnswer.answerOption.label.valueOf()) {
...@@ -189,7 +192,6 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -189,7 +192,6 @@ export class ContentChoiceCreatorComponent implements OnInit {
switchValue(label: string) { switchValue(label: string) {
let index: number; let index: number;
let isCorrect: boolean;
// Get id of answer // Get id of answer
for (let i = 0; i < this.content.options.length; i++) { for (let i = 0; i < this.content.options.length; i++) {
...@@ -198,25 +200,13 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -198,25 +200,13 @@ export class ContentChoiceCreatorComponent implements OnInit {
break; break;
} }
} }
// Check if answer is marked as correct this.editDisplayAnswer = new DisplayAnswer(
isCorrect = !this.displayAnswers[index].correct; new AnswerOption(
this.displayAnswers[index].answerOption.label,
// Update correct answers this.displayAnswers[index].answerOption.points),
if (isCorrect) { !this.displayAnswers[index].correct);
if (this.content.correctOptionIndexes.indexOf(index) === -1) { this.saveChanges(index, this.editDisplayAnswer, false);
// ToDo: Set back to data model // return;
if (this.multipleChoice) {
this.content.correctOptionIndexes.push(index);
} else {
this.content.correctOptionIndexes = [index];
}
}
} else {
if (this.content.correctOptionIndexes.indexOf(index) !== -1) {
this.content.correctOptionIndexes.splice(index, 1);
}
}
this.fillCorrectAnswers();
} }
reset($event) { reset($event) {
...@@ -229,6 +219,15 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -229,6 +219,15 @@ export class ContentChoiceCreatorComponent implements OnInit {
this.notificationService.show('Reset all inputs to default.'); this.notificationService.show('Reset all inputs to default.');
} }
resetAfterSubmit() {
this.content.subject = '';
this.content.body = '';
this.content.options = [];
this.content.correctOptionIndexes = [];
this.fillCorrectAnswers();
this.notificationService.show('Content submitted. Ready for creation of new content.');
}
submitContent() { submitContent() {
if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') { if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') {
this.notificationService.show('No empty fields allowed. Please check subject and body.'); this.notificationService.show('No empty fields allowed. Please check subject and body.');
...@@ -242,7 +241,7 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -242,7 +241,7 @@ export class ContentChoiceCreatorComponent implements OnInit {
this.notificationService.show('In single choice mode you have to select 1 true answer.'); this.notificationService.show('In single choice mode you have to select 1 true answer.');
return; return;
} }
if (this.multipleChoice && this.content.correctOptionIndexes.length !== 1) { if (this.multipleChoice && this.content.correctOptionIndexes.length < 1) {
this.notificationService.show('In multiple choice mode you have to select at least 1 true answer.'); this.notificationService.show('In multiple choice mode you have to select at least 1 true answer.');
return; return;
} }
...@@ -254,8 +253,10 @@ export class ContentChoiceCreatorComponent implements OnInit { ...@@ -254,8 +253,10 @@ export class ContentChoiceCreatorComponent implements OnInit {
this.content.multiple = true; this.content.multiple = true;
this.content.format = ContentType.CHOICE; this.content.format = ContentType.CHOICE;
} }
this.notificationService.show('Content submitted.');
// ToDo: Check api call // ToDo: Check api call
// this.contentService.addContent(this.content); // this.contentService.addContent(this.content);
// For Testing:
// console.log(this.content);
this.resetAfterSubmit();
} }
} }
...@@ -60,7 +60,13 @@ export class ContentLikertCreatorComponent implements OnInit { ...@@ -60,7 +60,13 @@ export class ContentLikertCreatorComponent implements OnInit {
this.fillCorrectAnswers(); this.fillCorrectAnswers();
} }
// TODO resetAfterSubmit() {
this.content.subject = '';
this.content.body = '';
this.content.correctOptionIndexes = [];
this.fillCorrectAnswers();
this.notificationService.show('Content submitted. Ready for creation of new content.');
}
submitContent(): void { submitContent(): void {
if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') { if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') {
...@@ -70,5 +76,8 @@ export class ContentLikertCreatorComponent implements OnInit { ...@@ -70,5 +76,8 @@ export class ContentLikertCreatorComponent implements OnInit {
this.notificationService.show('Content sumbitted.'); this.notificationService.show('Content sumbitted.');
// ToDo: Check api call // ToDo: Check api call
// this.contentService.addContent(this.content); // this.contentService.addContent(this.content);
// For Testing:
// console.log(this.content);
this.resetAfterSubmit();
} }
} }
...@@ -29,13 +29,22 @@ export class ContentTextCreatorComponent implements OnInit { ...@@ -29,13 +29,22 @@ export class ContentTextCreatorComponent implements OnInit {
}); });
} }
resetAfterSubmit() {
this.content.subject = '';
this.content.body = '';
this.notificationService.show('Content submitted. Ready for creation of new content.');
}
submitContent() { submitContent() {
if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') { if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') {
this.notificationService.show('No empty fields allowed. Please check subject and body.'); this.notificationService.show('No empty fields allowed. Please check subject and body.');
return; return;
} }
this.notificationService.show('Content sumbitted.'); this.notificationService.show('Content submitted.');
// ToDo: Check api call // ToDo: Check api call
// this.contentService.addContent(this.content); // this.contentService.addContent(this.content);
// For Testing:
// console.log(this.content);
this.resetAfterSubmit();
} }
} }
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<mat-header-cell *matHeaderCellDef>Answer</mat-header-cell> <mat-header-cell *matHeaderCellDef>Answer</mat-header-cell>
<mat-cell *matCellDef="let answer"> <mat-cell *matCellDef="let answer">
<!-- ToDo: Check ngModel --> <!-- ToDo: Check ngModel -->
<mat-checkbox (click)="setCorrect(answer.answerOption.label)" color="primary" [(ngModel)]="answer.correct" [checked]="answer.correct" <mat-checkbox (click)="setCorrect($event ,answer.answerOption.label)" color="primary" [(ngModel)]="answer.correct" [checked]="answer.correct"
name="{{ answer.answerOption.label }}">{{ answer.answerOption.label }} name="{{ answer.answerOption.label }}">{{ answer.answerOption.label }}
</mat-checkbox> </mat-checkbox>
</mat-cell> </mat-cell>
......
...@@ -56,7 +56,8 @@ export class ContentYesNoCreatorComponent implements OnInit { ...@@ -56,7 +56,8 @@ export class ContentYesNoCreatorComponent implements OnInit {
} }
} }
setCorrect(label: string) { setCorrect($event, label: string) {
$event.preventDefault();
if (label === 'yes') { if (label === 'yes') {
this.content.correctOptionIndexes = [0]; this.content.correctOptionIndexes = [0];
} }
...@@ -70,6 +71,14 @@ export class ContentYesNoCreatorComponent implements OnInit { ...@@ -70,6 +71,14 @@ export class ContentYesNoCreatorComponent implements OnInit {
return (this.content.correctOptionIndexes.length === 1); return (this.content.correctOptionIndexes.length === 1);
} }
resetAfterSubmit() {
this.content.subject = '';
this.content.body = '';
this.content.correctOptionIndexes = [];
this.fillCorrectAnswers();
this.notificationService.show('Content submitted. Ready for creation of new content.');
}
submitContent(): void { submitContent(): void {
if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') { if (this.content.body.valueOf() === '' || this.content.body.valueOf() === '') {
this.notificationService.show('No empty fields allowed. Please check subject and body.'); this.notificationService.show('No empty fields allowed. Please check subject and body.');
...@@ -81,6 +90,9 @@ export class ContentYesNoCreatorComponent implements OnInit { ...@@ -81,6 +90,9 @@ export class ContentYesNoCreatorComponent implements OnInit {
} }
this.notificationService.show('Content sumbitted.'); this.notificationService.show('Content sumbitted.');
// ToDo: Check api call // ToDo: Check api call
// this.contentService.addContent(this.content); // this.contentService.addContent(this.content);
// For Testing:
// console.log(this.content);
this.resetAfterSubmit();
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment