Newer
Older
import {Component, OnDestroy, OnInit} from '@angular/core';
ywb16155
committed
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.name = 'Enter a name...';
this.topic = 'Enter a topic...';
this.isTemplate = this.lecturerDataService.isTemplate;
this.getClasses();
this.getTemplates();
this.isAnonymous = false;
this.addQuestion();
this.templates.push(new MinutePaperWrapperDao(new MinutePaperDao('-1', 'default', 'default', 'No Template', '',
true, true, '0000-00-00'), []));
this.currentTemplate = this.templates[0];
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);
}
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
}
}
}
// 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.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];
}
}
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);