qa.ts 7.05 KB
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { DataService } from '../../providers/data-service';
import { Qa } from '../../providers/define-qa';
import { Content } from 'ionic-angular';
import { ViewChild } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { QaInfo } from '../../providers/define-qainfo';
/*
  Generated class for the Qa page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-qa',
  templateUrl: 'qa.html'
})
export class QaPage {
    @ViewChild(Content) content: Content;

    qaList: Qa[];
    qa1: Qa;
    menuId: number;
    unitId: number;
    index: number;
    title:string;
    buttonDisabled: boolean;
    nv: NavController;
    selected: number;
    result: string;
    selectstate: Array<boolean> = new Array();
    subscription;
    screenHeight:number;
    positiveCnt: number;
    qaInfoList: Array<QaInfo> = new Array();
    selection:String[] = ["ア","イ","ウ","エ","オ"];

    constructor(public navCtrl: NavController, public navParams: NavParams, private dataSevice: DataService) {
        this.menuId = navParams.get("menuId");
        this.unitId = navParams.get("unitId");
        this.title = navParams.get("title");

        this.index = 0;
        this.qaList = dataSevice.getQa(this.menuId, this.unitId);
        this.qa1 = this.qaList[this.index];
        this.buttonDisabled = null;
        this.nv = navCtrl;
        this.selected = -1;
        this.positiveCnt = 0;
        for (var i = 0; i < this.qa1.choicesId.length; i++) {
            this.selectstate.push(false);
        }
        this.readStatus();

    }

  ionViewDidLoad() {
    console.log('ionViewDidLoad QaPage');
    }

  ionViewWillLeave(){

      this.writeStatus();
  }

  itemSelected(id: number) {

      for (var i = 0; i < this.qa1.choicesId.length; i++) {
          if(this.selectstate[i] == true) {
              this.selectstate[i] = false;
          }
      }
      this.selectstate[id] = true;
      this.selected = id;
//      this.content.scrollToBottom(300);
      this.openAnswer(1);
  }

  openAnswer(selected: number) {
      if (this.buttonDisabled == null)
      {
          this.result = "解答";

          this.qaInfoList[this.index].isDone = 1;
          if(selected == 1)
          {
              this.qaInfoList[this.index].challengeCnt++;

              if (this.selected === this.qa1.answerId) {  //正解
                  this.result = "正解";
                  this.qaInfoList[this.index].positiveFlg_now = 1;
                  this.qaInfoList[this.index].answer = 1;
              }
              else {
                  this.result = "不正解";
                  if(this.qaInfoList[this.index].positiveFlg != 1)
                  {
                      this.qaInfoList[this.index].positiveFlg_now = 0;
                      this.qaInfoList[this.index].answer = 0;
                  }
              }
          }

          this.buttonDisabled = true;
          if (this.index >= (this.qaList.length - 1))
          {

          }

          var element = document.getElementsByClassName('question-card');
          this.screenHeight = element[0].clientHeight;
          this.subscription = Observable.timer(300)
             .subscribe(x => {
                  this.content.scrollTo(0, this.screenHeight, 300);
              }
          );
      }
  }

  showAnswer(res: number) {
      if (this.buttonDisabled == null)
      {
          if(res == -1)
          {
              this.result = "解答";
          }
          else
          {
              if (res == 1) {  //正解
                  this.result = "正解";
              }
              else {
                  this.result = "不正解";
              }
          }

          this.buttonDisabled = true;
      }
  }

  goToNext() {
      if(this.index < this.qaList.length-1)
      {
          this.index++;
          this.buttonDisabled = null;
          this.selected = -1;
          this.qa1 = this.qaList[this.index];
          this.selectstate = new Array();
          for (var i = 0; i < this.qa1.choicesId.length; i++) {
              this.selectstate.push(false);
          }
          if(this.qaInfoList[this.index].isDone != 0)
          {
              this.showAnswer(this.qaInfoList[this.index].answer);
          }

          this.content.scrollToTop(300);
      }
  }

  goToPrev() {
      if(this.index > 0)
      {
          this.index--;
          this.buttonDisabled = null;
          this.selected = -1;
          this.qa1 = this.qaList[this.index];
          this.selectstate = new Array();
          for (var i = 0; i < this.qa1.choicesId.length; i++) {
              this.selectstate.push(false);
          }
          if(this.qaInfoList[this.index].isDone != 0)
          {
              this.showAnswer(this.qaInfoList[this.index].answer);
          }
          this.content.scrollToTop(300);
      }
  }

  backToUnit() {
      this.nv.pop();
  }

  backToTop() {
      this.nv.popToRoot();
  }

  readStatus()
  {
      var promiseArray1 = new Array();
      for (var i = 0; i < this.qaList.length; i++) {
           var key: string;
           key = this.menuId + ":" + this.unitId + ":" + this.qaList[i].qaId;
           promiseArray1.push(this.dataSevice.getInfo(key));
      }

      Promise.all(promiseArray1)
      .then(val =>
      {
          for(var i = 0; i< this.qaList.length; i++)
          {
              var item: QaInfo = new QaInfo();
              item.challengeCnt = 0;
              item.positiveFlg = 2;
              item.positiveFlg_now = 2;
              item.isDone = 0;
              item.answer = -1;
              if(val[i] != null)
              {
                  var svdata = val[i].toString().split(":");
                  item.challengeCnt = +svdata[0];
                  item.positiveFlg = +svdata[1];
                  item.positiveFlg_now = +svdata[1];
                  if(item.positiveFlg == 1)
                  {
                      this.positiveCnt++;
                  }
              }
              this.qaInfoList.push(item);
          }
      });
  }

  writeStatus()
  {
      var cnt:number = 0;
      var startFlg:number = 0;
      for (var i = 0; i < this.qaList.length; i++) {
          var key: string;
          var value: string; 
          key = this.menuId + ":" + this.unitId + ":" + this.qaList[i].qaId;
          value =  this.qaInfoList[i].challengeCnt + ":" +  this.qaInfoList[i].positiveFlg_now;
          if(this.qaInfoList[i].positiveFlg_now == 1)
          {
              cnt++;
          }
          if(this.qaInfoList[i].isDone == 1)  //解答したまたは解説を開いたら着手
          {
              startFlg = 1;
          }
          this.dataSevice.saveInfo(key, value);
      }

      if(startFlg == 1)
      {
          if(cnt >= this.positiveCnt)
          {
              var key: string;
              var value: string;
              key = this.qa1.menuId + ":" + this.qa1.unitId;
              value = cnt.toString();
              this.dataSevice.saveInfo(key, value);
          }
      }

  }

}