data-service.ts
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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);
}
}