Skip to content
Snippets Groups Projects
lecturer-create-omp-view.component.ts 7.49 KiB
Newer Older
import {AfterContentInit, AfterViewInit, Component, OnDestroy, OnInit, ViewChild} 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';
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 {
  private lecturerId: string;
  public isTemplate: Boolean;
  public questions: QuestionDao[];
  public classes: OmpClassDao[];
  public templates: MinutePaperWrapperDao[];
  public paperName: String;
  public paperTopic: String;
  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,
              private router: Router) {
    this.lecturerId = sessionStorage.getItem('username');
    this.questions = [];
    this.classes = [];
    this.nameInvalid = false;
    this.topicInvalid = false;
    this.isSubmitted = false;
    this.noQuestions = false;
    this.invalidQuestions = false;
  ngAfterContentInit(): void {
    this.questions = [];
    this.classes = [];
  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.classes.push(new OmpClassDao('NO_CLASS', '0', '', ''));
    this.templates.push(new MinutePaperWrapperDao(new MinutePaperDao('-1', '', 'NO_CLASS', 'No Template', '',
  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);
  }
    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);
    });
    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;
  }

    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();
      }
      this.validateForm();
    this.isSubmitted = true;
    if (this.validateForm()) {
      this.lecturerInfoService.createPaper(this.createWrapperObject()).subscribe(() => {
        this.router.navigate(['/ldashboard']);
        this.ngOnDestroy();
      });
    this.isSubmitted = true;
    if (this.validateForm()) {
      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 {
  public cancel() {
    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);