File

src/app/manage-learn/core/services/update-local-school-data.service.ts

Index

Methods

Constructor

constructor(localStorage: LocalStorageService, utils: UtilsService)
Parameters :
Name Type Optional
localStorage LocalStorageService No
utils UtilsService No

Methods

constructMatrixValue
constructMatrixValue(validSubmission, matrixQuestion, ecmId)
Parameters :
Name Optional
validSubmission No
matrixQuestion No
ecmId No
Returns : any
mapSubmissionDataToQuestion
mapSubmissionDataToQuestion(schoolDetails, isObservation?: boolean, isSurvey?: boolean)
Parameters :
Name Type Optional
schoolDetails No
isObservation boolean Yes
isSurvey boolean Yes
Returns : void
Async updateSubmissionIdArr
updateSubmissionIdArr(submissionId)
Parameters :
Name Optional
submissionId No
Returns : any
updateSubmissionsOnLogin
updateSubmissionsOnLogin(schoolData)
Parameters :
Name Optional
schoolData No
Returns : any
import { Injectable } from '@angular/core';
import { LocalStorageService } from '.';
import { storageKeys } from '../../storageKeys';
import { UtilsService } from './utils.service';

@Injectable({
  providedIn: 'root',
})
export class UpdateLocalSchoolDataService {
  constructor(private localStorage: LocalStorageService, private utils: UtilsService) {}

  async updateSubmissionIdArr(submissionId) {
    await this.localStorage
      .getLocalStorage(storageKeys.submissionIdArray)
      .then(async (arr) => {
        Array.isArray(submissionId) ? arr.concat(submissionId) : arr.push(submissionId);
        await this.localStorage.setLocalStorage(storageKeys.submissionIdArray, arr);
      })
      .catch((err) => {
        let arr;
        Array.isArray(submissionId) ? (arr = submissionId) : (arr = [submissionId]);
        this.localStorage.setLocalStorage(storageKeys.submissionIdArray, arr);
      });
  }


  mapSubmissionDataToQuestion(schoolDetails, isObservation?: boolean,isSurvey?:boolean): void {
    let mappedData;

    mappedData = this.updateSubmissionsOnLogin(schoolDetails);
    if (isObservation) {
      mappedData.observation = true;
    }
    
    if(isSurvey){
      mappedData.survey = true;
    }

    this.localStorage.setLocalStorage(
      this.utils.getAssessmentLocalStorageKey(schoolDetails.assessment.submissionId),
      mappedData
    );
  }
  updateSubmissionsOnLogin(schoolData) {
    const assessment = schoolData.assessment;

    for (const evidence of assessment.evidences) {
      const validSubmission = assessment.submissions[evidence.externalId];
      if (validSubmission) {
        evidence.notApplicable = validSubmission.notApplicable;
        if (evidence.notApplicable) {
          continue;
        }

        for (const section of evidence.sections) {
          for (const question of section.questions) {
            if (question.responseType === 'pageQuestions') {
              for (const questions of question.pageQuestions) {
                questions.value =
                  questions.responseType !== 'matrix'
                    ? validSubmission.answers[questions._id].value
                    : this.constructMatrixValue(validSubmission, questions, evidence.externalId);
                questions.remarks = validSubmission.answers[question._id]
                  ? validSubmission.answers[question._id].remarks
                  : '';
              }
            } else if (validSubmission.answers && validSubmission.answers[question._id]) {
              question.value =
                question.responseType !== 'matrix'
                  ? validSubmission.answers[question._id].value
                  : this.constructMatrixValue(validSubmission, question, evidence.externalId);
              question.remarks = validSubmission.answers[question._id]
                ? validSubmission.answers[question._id].remarks
                : '';
            }
          }
        }
      }
    }
    return schoolData;
  }

  constructMatrixValue(validSubmission, matrixQuestion, ecmId) {
    matrixQuestion.value = [];
    if (
      validSubmission.answers &&
      validSubmission.answers[matrixQuestion._id] &&
      validSubmission.answers[matrixQuestion._id].value
    ) {
      for (const answer of validSubmission.answers[matrixQuestion._id].value) {
        matrixQuestion.value.push(JSON.parse(JSON.stringify(matrixQuestion.instanceQuestions)));
      }
      matrixQuestion.value.forEach((instance, index) => {
        instance.forEach((question) => {
          if (
            validSubmission.answers[matrixQuestion._id] &&
            validSubmission.answers[matrixQuestion._id].value[index][question._id]
          ) {
            question.value = validSubmission.answers[matrixQuestion._id].value[index][question._id].value;
            question.remarks = validSubmission.answers[matrixQuestion._id].value[index][question._id].remarks;
          }
        });
      });
      return matrixQuestion.value;
    } else {
      return [];
    }
  }
}

results matching ""

    No results matching ""