import {Component, OnDestroy, OnInit} from '@angular/core'; import {LecturerInfoService} from 'src/app/services/lecturer-info.service'; import {LecturerDataService} from 'src/app/services/lecturer-data.service'; import {LecturerViewService} from 'src/app/services/lecturer-view.service'; import {Router} from '@angular/router'; import {QuestionDao} from 'src/app/dao/question.dao'; import {OmpClassDao} from 'src/app/dao/omp-class.dao'; import {MinutePaperDao} from 'src/app/dao/minute-paper.dao'; import {MinutePaperWrapperDao} from 'src/app/dao/minute-paper-wrapper.dao'; @Component({ selector: 'app-lecturer-create-omp-view', templateUrl: './lecturer-create-omp-view.component.html', styleUrls: ['./lecturer-create-omp-view.component.css'] }) export class LecturerCreateOmpViewComponent implements OnInit, OnDestroy { public isTemplate: Boolean; public questions: QuestionDao[]; public classes: OmpClassDao[]; public templates: MinutePaperWrapperDao[]; public name: String; public topic: String; public isAnonymous: Boolean; public currentClass: OmpClassDao; public currentTemplate: MinutePaperWrapperDao; constructor(private lecturerInfoService: LecturerInfoService, private lecturerDataService: LecturerDataService, private lecturerViewService: LecturerViewService, private router: Router) { } ngOnInit(): void { this.questions = []; this.classes = []; this.templates = []; this.initialiseForms(); } ngOnDestroy(): void { this.questions = []; this.classes = []; this.templates = []; } private getClasses(): void { this.lecturerInfoService.getLecturerClasses(this.lecturerDataService.lecturerId) .subscribe((data: OmpClassDao[]) => data.forEach(e => { this.classes.push(e); this.currentClass = this.classes[0]; })); } private initialiseForms(): void { this.isTemplate = this.lecturerDataService.isTemplate; this.getClasses(); this.getTemplates(); this.templates.push(new MinutePaperWrapperDao(new MinutePaperDao('-1', 'default', 'default', 'No Template', '', true, true, '0000-00-00'), [])); this.resetForm(); } private createWrapperObject() { const paper = new MinutePaperDao('0', this.lecturerDataService.lecturerId, this.currentClass.classCode, this.name, this.topic, this.isTemplate, this.isAnonymous, '0000-00-00'); return new MinutePaperWrapperDao(paper, this.questions); } private getTemplates() { this.lecturerInfoService.getTemplates(this.lecturerDataService.lecturerId) .subscribe((data: MinutePaperWrapperDao[]) => data.forEach(e => {this.templates.push(e); })); } private findClass(classCode: String) { for (const ompclass of this.classes) { if (ompclass.classCode === classCode) { this.currentClass = ompclass; } } } private resetForm() { this.questions = []; this.addQuestion(); this.isAnonymous = false; this.name = 'Enter a name...'; this.topic = 'Enter a topic...'; this.currentClass = this.classes[0]; this.currentTemplate = this.templates[0]; } // TODO check if form has been modified at all public setTemplate() { if (this.currentTemplate.paper.paperId !== '-1') { this.questions = []; this.questions = this.currentTemplate.questions; this.isAnonymous = this.currentTemplate.paper.anonymous; this.name = this.currentTemplate.paper.name; this.topic = this.currentTemplate.paper.topic; this.findClass(this.currentTemplate.paper.classCode); } else { this.resetForm(); } } public createPaper(): void { this.lecturerInfoService.createPaper(this.createWrapperObject()).subscribe(); } public createTemplate(): void { this.lecturerInfoService.createTemplate(this.createWrapperObject()).subscribe(); } public flipIsAnonymous(): void { this.isAnonymous = !this.isAnonymous; } public addQuestion(): void { this.questions.push(new QuestionDao('0', '0', 'Enter your question text!')); } public removeQuestion(index: number): void { this.questions.splice(index, 1); } }