data-service.ts 4.1 KB
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Menu } from './define-menu';
import { Unit } from './define-unit';
import { Qa } from './define-qa';
import { Contents } from './define-contents';
import { Storage } from '@ionic/storage';
/*
  Generated class for the DataService provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class DataService {
    st: Storage;
    st_res: string;
    contents: Contents;
    menu: Menu[];
    unit: Unit[];
    subject: Qa[];

    constructor(st: Storage) {
        this.st = st;
        this.contents = new Contents;
        this.menu = this.contents.menu;
        this.unit = this.contents.unit;
        this.subject = this.contents.qa;
    }

  public getMenuFirst(): Menu[]{
      var menuFirst: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '1') {
          menuFirst.push(this.menu[i]);
        }
      }
      return menuFirst;
  }

  public getMenuSecond(): Menu[]{
      var menuSecond: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '2') {
          menuSecond.push(this.menu[i]);
        }
      }
      return menuSecond;
  }

  public getMenuThird(): Menu[]{
      var menuThird: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '3') {
          menuThird.push(this.menu[i]);
        }
      }
      return menuThird;
  }

  public getUnitNum(menuId: number): number {
      var num = 0;
      var chk = -1;
      for (var i = 0; i < this.unit.length; i++) {
          if (this.unit[i].menuId === menuId) {
              if(chk != this.unit[i].unitId)
              {
                  num++;
                  chk = this.unit[i].unitId;
              }
          }
      }
      return num;
  }

  public getUnit(menuId: number): Unit[] {
      var subjectList: Unit[] = new Array();
      for (var i = 0; i < this.unit.length; i++) {
          if (this.unit[i].menuId === menuId) {
              subjectList.push(this.unit[i]);
          }
      }
      return subjectList;
  }

  public getQaCnt(menuId:number): number {
      var num = 0;
      for (var i = 0; i < this.subject.length; i++) {
          if (this.subject[i].menuId === menuId) {
              num++;
          }
      }
      return num;
  }

  public getUnitQaCnt(menuId:number,unitId:number): number {
      var num = 0;
      for (var i = 0; i < this.subject.length; i++) {
          if ((this.subject[i].menuId === menuId) && (this.subject[i].unitId == unitId)) {
              num++;
          }
      }
      return num;
  }

  public getQa(menuId: number, unitId: number): Qa[] {
      var qaItem: Qa[] = new Array();
      for (var i = 0; i < this.subject.length; i++) {
          if ((this.subject[i].menuId === menuId) && (this.subject[i].unitId === unitId)) {
              qaItem.push(this.subject[i]);
          }
      }

      //解答並び替え
      for(var j=0; j<qaItem.length; j++)
      {
          qaItem[j].choicesId.sort(function() {
              return Math.random() - 0.5;
          });

          var chString: string[] = new Array(qaItem[j].choicesId.length);
          var chId:number[] = new Array(qaItem[j].choicesId.length);
          var aId:number = qaItem[j].answerId;
          for(var k=0; k<qaItem[j].choicesId.length; k++)
          {
              chString[k] = qaItem[j].choicesString[qaItem[j].choicesId[k]];
              chId[k] = k;
              if(qaItem[j].choicesId[k] == qaItem[j].answerId)
              {
                  aId = k;
              }
          }
          qaItem[j].choicesId = chId;
          qaItem[j].choicesString = chString;
          qaItem[j].answerId = aId;
      } 

      //問題並び替え
      qaItem.sort(function() {
              return Math.random() - 0.5;
      });
      return qaItem;
  }

  public saveInfo(key: string, value: string) {
      this.st.set(key, value);
  }

  public getInfo(key: string): any {
      return this.st.get(key);
  }

}