Newer
Older
import {AfterContentInit, AfterViewInit, Component, OnDestroy, OnInit, ViewChild} 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';
import {NgForm} from '@angular/forms';
@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, AfterContentInit, OnDestroy {
public isTemplate: Boolean;
public questions: QuestionDao[];
public classes: OmpClassDao[];
public templates: MinutePaperWrapperDao[];
public paperName: String;
public paperTopic: 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;
@ViewChild('infoForm') public infoForm: NgForm;
constructor(private lecturerInfoService: LecturerInfoService,
private lecturerDataService: LecturerDataService,
private lecturerViewService: LecturerViewService,
}
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;
ngAfterContentInit(): void {
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);
}
if (this.isTemplate) {
this.classes.push(new OmpClassDao('NO_CLASS', '0', '', ''));
} else {
this.getClasses();
}
this.getTemplates();
this.templates.push(new MinutePaperWrapperDao(new MinutePaperDao('-1', '', 'NO_CLASS', 'No Template', '',
true, true, '0000-00-00'), []));
this.resetForm();
private createWrapperObject() {
const paper = new MinutePaperDao('0', this.lecturerId, this.currentClass.classCode,
this.paperName, this.paperTopic, 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 resetValidation() {
this.infoForm.controls.nameBox.reset('');
this.infoForm.controls.topicBox.reset('');
this.nameInvalid = false;
this.topicInvalid = false;
this.invalidQuestions = false;
}
private resetForm() {
this.questions = [];
this.addQuestion();
this.isAnonymous = false;
this.paperName = '';
this.paperTopic = '';
this.currentClass = this.classes[0];
this.currentTemplate = this.templates[0];
this.isSubmitted = false;
}
private validateForm(): Boolean {
if (this.questions.length === 0) {
this.noQuestions = true;
}
this.invalidQuestions = this.validateQuestions();
this.nameInvalid = this.empty(this.paperName);
this.topicInvalid = this.empty(this.paperTopic);
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;
}
private getCurrentTemplate() {
this.questions = [];
this.currentTemplate.questions.forEach(question => {
this.questions.push(question);
});
ywb16155
committed
this.questions.forEach(question => {
question.questionId = '0';
question.paperId = '0';
});
this.isAnonymous = this.currentTemplate.paper.anonymous;
this.paperName = this.currentTemplate.paper.name;
this.paperTopic = this.currentTemplate.paper.topic;
this.findClass(this.currentTemplate.paper.classCode);
}
private empty(text: String): Boolean {
if (text === null || text.replace(/\s/g, '').length === 0 || text === '') {
return true;
}
return false;
}
public setTemplate() {
if (this.currentTemplate.paper.paperId !== '-1' ) {
this.validateForm();
if (!this.nameInvalid || !this.topicInvalid || !this.invalidQuestions) {
const choice = confirm('Are you sure you want to load the template?\n' +
'All current work will be lost!');
if (choice) {
this.getCurrentTemplate();
}
} else {
this.getCurrentTemplate();
}
}
}
public createPaper(): void {
this.isSubmitted = true;
ywb16155
committed
this.lecturerInfoService.createPaper(this.createWrapperObject()).subscribe(() => {
this.router.navigate(['/ldashboard']);
this.ngOnDestroy();
});
}
public createTemplate(): void {
this.isSubmitted = true;
ywb16155
committed
this.lecturerInfoService.createTemplate(this.createWrapperObject()).subscribe(() => {
this.router.navigate(['/ldashboard']);
this.ngOnDestroy();
});
if (!this.nameInvalid || !this.topicInvalid || !this.invalidQuestions) {
const choice = confirm('Are you sure you want to reset the form?\n' +
'All current work will be lost!');
if (choice) {
this.resetValidation();
}
} else {
this.resetValidation();
}
}
if (!this.nameInvalid || !this.topicInvalid || !this.invalidQuestions) {
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();
}
} else {
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);