-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
240 lines (164 loc) · 6.23 KB
/
index.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
const debug = require('debug')('limesurvey')
const fetch = require('node-fetch');
class client {
constructor(opts) {
this.opts = opts
this.limesurveyUrl = opts.url
this.username = opts.username
this.password = opts.password
}
callApi(method, params) {
return new Promise(async(resolve, reject) => {
if (method !== 'get_session_key') {
let token = await this.getToken()
params.unshift(token)
}
debug('---------------')
debug('method=', method)
debug('params=', params)
fetch(this.limesurveyUrl, {
method: 'post',
body: JSON.stringify({ method: method, params: params, id: 1 }),
headers: { 'Content-Type': 'application/json' },
})
.then((res) => {
debug('res=', res.ok)
return res.json()
})
.then(json => {
debug('json=', json)
if (json && json.result) {
resolve(json.result)
} else {
reject(new Error('no token'))
}
})
.catch((error) => {
reject(new Error(error))
})
})
}
async getToken() {
if (this.token && this.regenerateAfter && new Date().getTime() < this.regenerateAfter) {
debug('from cache')
return this.token
} else {
debug('ask new token')
this.token = await this.callApi('get_session_key', [this.username, this.password])
this.regenerateAfter = new Date().getTime() + (2 * 60 * 60 * 1000)
return this.token
}
}
getSurveyList() {
return this.callApi('list_surveys', [null])
}
getSurveyInfo(surveyId) {
return this.callApi('get_survey_properties', [surveyId])
}
getGroups(surveyId) {
return this.callApi('list_groups', [surveyId])
}
getQuestions(surveyId, groupId) {
let params = [surveyId]
if (groupId) {
params.push(groupId)
}
return this.callApi('list_questions', params)
}
async getResponsesBySurveyId(surveyId) {
var json = await this.callApi('export_responses', [surveyId, 'json', null, 'all', 'code', 'long'])
let obj = JSON.parse(Buffer.from(json, 'base64').toString('utf-8'))
return obj.responses
}
async getResponsesByToken(surveyId, tokenId) {
var json = await this.callApi('export_responses_by_token', [surveyId, 'json', tokenId, null, 'all', 'code', 'long'])
let obj = JSON.parse(Buffer.from(json, 'base64').toString('utf-8'))
return obj.responses
}
async getStatistics(surveyId, format) {
var fileContent = await this.callApi('export_statistics', [surveyId, format, null, 'yes'])
let obj = Buffer.from(fileContent, 'base64')
return obj
}
activateTokens(surveyId) {
return this.callApi('activate_tokens', [surveyId])
}
addParticipants(surveyId, participants) {
return this.callApi('add_participants', [surveyId, participants])
}
async getPrettyResponses(surveyId, tokenId, aggregateByGroup = false) {
let groups = await this.getGroups(surveyId)
let groupsByGid = {}
for (let group of groups) {
groupsByGid[group.gid] = group
}
//debug('groupsByGid', groupsByGid)
let questions = await this.getQuestions(surveyId, tokenId)
var titleByQid = {}
var questionsByCode = {}
for (let question of questions) {
titleByQid[question.qid] = question.title
if (question.parent_qid !== '0') {
question.title = titleByQid[question.parent_qid] + '[' + question.title + ']'
}
questionsByCode[question.title] = question
}
debug('questionsByCode', questionsByCode)
let reponses = await this.getResponsesBySurveyId(surveyId)
var prettyResponses = []
for (let response of reponses) {
var prettyResponse = []
let keys = Object.keys(response)
for (let index of keys) {
let codes = Object.keys(response[index])
for (let code of codes) {
let question = questionsByCode[code]
if (question) {
//debug(code, question.question, response[index][code])
let group = groupsByGid[question.gid]
//debug(group)
prettyResponse.push({ code: code, qid: question.qid, groupOrder: group.group_order, groupName: group.group_name, order: question.question_order, questionName: question.question, value: response[index][code], /*question: question*/ })
}
}
}
prettyResponse.sort((a, b) => {
if (a.groupOrder === b.groupOrder) {
if (a.qid === b.qid) {
return Number(a.order) - Number(b.order)
} else {
return Number(a.qid) - Number(b.qid)
}
} else {
return Number(a.groupOrder) - Number(b.groupOrder)
}
})
if(aggregateByGroup === true)
{
let prettyResponseByGroup = {}
for(let item of prettyResponse)
{
if(prettyResponseByGroup[item.groupName])
{
prettyResponseByGroup[item.groupName].push(item)
}
else{
prettyResponseByGroup[item.groupName] = [item]
}
}
prettyResponses.push(prettyResponseByGroup)
}
else{
prettyResponses.push(prettyResponse)
}
}
return prettyResponses
}
}
module.exports = (opts) => {
var instance
if (!instance) {
instance = new client(opts);
}
return instance
};