Newer
Older
import {ChangeDetectorRef, 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;
public nameInvalid: Boolean;
public topicInvalid: Boolean;
public isSubmitted: Boolean;
public noQuestions: Boolean;
public invalidQuestions: Boolean;
private lecturerId: string;
constructor(private lecturerInfoService: LecturerInfoService,
private lecturerDataService: LecturerDataService,
private lecturerViewService: LecturerViewService,
private router: Router,
private detector: ChangeDetectorRef) {
}
ngOnInit(): void {
this.lecturerId = sessionStorage.getItem('username');
this.questions = [];
this.classes = [];
this.templates = [];
this.nameInvalid = false;
this.topicInvalid = false;
this.isSubmitted = false;
this.noQuestions = false;
this.invalidQuestions = false;
this.initialiseForms();
}
ngOnDestroy(): void {
this.questions = [];
this.classes = [];
this.templates = [];
}
private getClasses(): void {
this.lecturerInfoService.getLecturerClasses(this.lecturerId)
.subscribe((data: OmpClassDao[]) => data.forEach(e => {
this.classes.push(e);
this.currentClass = this.classes[0];
}));
}
private initialiseForms(): void {
this.isTemplate = this.lecturerDataService.isTemplate;
if (this.isTemplate) {
this.lecturerViewService.changeToCreateTemplate(true);
} else {
this.lecturerViewService.changeToCreatePaper(true);
}
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.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.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.currentClass = this.classes[0];
this.currentTemplate = this.templates[0];
}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
private validateForm(): Boolean {
this.isSubmitted = true;
if (this.questions.length === 0) {
this.noQuestions = true;
}
this.invalidQuestions = this.validateQuestions();
this.nameInvalid = this.empty(this.name);
this.topicInvalid = this.empty(this.topic);
this.detector.detectChanges();
return !(this.nameInvalid || this.topicInvalid || this.noQuestions || this.invalidQuestions);
}
private validateQuestions(): Boolean {
let result = false;
this.questions.forEach(q => {
if (this.empty(q.questionText)) {
result = true;
}
});
return result;
}
public empty(text: String): Boolean {
if (text.replace(/\s/g, '').length === 0 || text === '') {
return true;
}
return false;
}
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 {
if (this.validateForm()) {
this.lecturerInfoService.createPaper(this.createWrapperObject()).subscribe();
this.router.navigate(['/ldashboard', {id: this.lecturerId}]);
this.ngOnDestroy();
}
}
public createTemplate(): void {
if (this.validateForm()) {
this.lecturerInfoService.createTemplate(this.createWrapperObject()).subscribe();
this.router.navigate(['/ldashboard', {id: this.lecturerId}]);
this.ngOnDestroy();
}
}
public cancel() {
const choice = confirm('Are you sure you want to cancel?\n' +
'Returning to the dashboard will result in all current work being lost!');
if (choice) {
this.router.navigate(['/ldashboard']);
this.ngOnDestroy();
}
}
public flipIsAnonymous(): void {
this.isAnonymous = !this.isAnonymous;
}
public addQuestion(): void {
this.questions.push(new QuestionDao('0', '0', ''));
this.noQuestions = false;
this.invalidQuestions = this.validateQuestions();
}
public removeQuestion(index: number): void {
this.questions.splice(index, 1);