-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathORKTaskResult+FHIR.swift
364 lines (307 loc) · 10.8 KB
/
ORKTaskResult+FHIR.swift
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// ORKTaskResult+FHIR.swift
// C3PRO
//
// Created by Pascal Pfiffner on 6/26/15.
// Copyright © 2015 Boston Children's Hospital. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import ResearchKit
import SMART
/**
Extend ORKTaskResult to add functionality to convert to QuestionnaireResponse.
*/
extension ORKTaskResult {
/**
Extracts all results from the task and converts them to a FHIR QuestionnaireResponse.
- parameter for: The task the receiver is a result for
- returns: A `QuestionnaireResponse` resource or nil
*/
public func c3_asQuestionnaireResponse(for task: ORKTask?) -> QuestionnaireResponse? {
guard let results = results as? [ORKStepResult] else {
return nil
}
// loop results to collect groups
var groups = [QuestionnaireResponseItem]()
for result in results {
if let items = result.c3_responseItems(from: task) {
groups.append(contentsOf: items)
}
}
// create and return questionnaire answers
let questionnaire = Reference()
questionnaire.reference = FHIRString(identifier)
let answer = QuestionnaireResponse(status: .completed)
answer.questionnaire = questionnaire
answer.item = groups
answer.deduplicateItemsByLinkId()
return answer
}
}
extension ORKStepResult {
/**
Creates a QuestionnaireResponseItem resource from all ORKSteps in the given ORKTask. Questions that do not have answers will be omitted,
and groups that do not have at least a single question with answer will likewise be omitted.
- parameter task: The ORKTask the result belongs to
- returns: A QuestionnaireResponseItem element or nil
*/
func c3_responseItems(from task: ORKTask?) -> [QuestionnaireResponseItem]? {
if let results = results {
var items = [QuestionnaireResponseItem]()
// loop results to collect answers; omit questions that do not have answers
for result in results {
if let result = result as? ORKQuestionResult {
if let question = task?.step?(withIdentifier: result.identifier) as? ORKQuestionStep, let answers = result.c3_responseItemAnswers(from: question) {
var response = QuestionnaireResponseItem(linkId: result.identifier.fhir_string)
response.answer = answers
// wrap into parent items - will dedupe by linkId later
if let conditional = question as? ConditionalStep {
var parentIds = conditional.linkIds
while let parentId = parentIds.popLast() {
let parent = QuestionnaireResponseItem(linkId: parentId.fhir_string)
parent.item = [response]
response = parent
}
}
items.append(response)
}
}
else {
c3_warn("I cannot handle ORKStepResult result \(result)")
}
}
// return non-nil if we have at least one response item
if items.count > 0 {
return items
}
}
return nil
}
}
extension ORKQuestionResult {
/**
Instantiate a QuestionnaireResponse.item.answer element from the receiver's answer, if any.
TODO: Cannot override methods defined in extensions, hence we need to check for the ORKQuestionResult subclass and then call the
method implemented in the extensions below.
- parameter from: The ORKQuestionStep the responses belong to
- returns: An array of question answers or nil
*/
func c3_responseItemAnswers(from step: ORKQuestionStep?) -> [QuestionnaireResponseItemAnswer]? {
let fhirType = (step as? ConditionalQuestionStep)?.fhirType
if let this = self as? ORKChoiceQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKTextQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKNumericQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKScaleQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKBooleanQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKTimeOfDayQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKTimeIntervalQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
if let this = self as? ORKDateQuestionResult {
return this.c3_responseItems(ofFHIRType: fhirType)
}
c3_warn("I don't understand ORKQuestionResult answer from \(self)")
return nil
}
/**
Checks whether the receiver is the same result as the other result.
- parameter other: The result to compare against
- returns: A boold indicating whether the results are the same
*/
func c3_hasSameResponse(_ other: ORKQuestionResult) -> Bool {
if let myAnswer = answer as? NSObject {
if let otherAnswer = other.answer as? NSObject {
return myAnswer.isEqual(otherAnswer)
}
}
return false
}
}
extension ORKChoiceQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; ignored, always "coding"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let choices = choiceAnswers as? [String] else {
if let choices = choiceAnswers {
c3_warn("expecting choice question results to be strings, but got: \(choices)")
}
return nil
}
var answers = [QuestionnaireResponseItemAnswer]()
for choice in choices {
let answer = QuestionnaireResponseItemAnswer()
let splat = choice.split() { $0 == kORKTextChoiceSystemSeparator }.map() { String($0) }
let system = splat[0]
let code = (splat.count > 1) ? splat[1..<splat.endIndex].joined(separator: String(kORKTextChoiceSystemSeparator)) : kORKTextChoiceMissingCodeCode
answer.valueCoding = Coding()
answer.valueCoding!.system = FHIRURL(system)
answer.valueCoding!.code = FHIRString(code)
answers.append(answer)
}
return answers
}
}
extension ORKTextQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; "url" or defaults to "string"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let text = textAnswer else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
if let fhir = ofFHIRType, "url" == fhir {
answer.valueUri = FHIRURL(text)
}
else {
answer.valueString = FHIRString(text)
}
return [answer]
}
}
extension ORKNumericQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; "quantity", "integer" or defaults to "number"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let numeric = numericAnswer else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
if let fhir = ofFHIRType, "quantity" == fhir {
answer.valueQuantity = Quantity()
answer.valueQuantity!.value = FHIRDecimal(numeric.decimalValue)
answer.valueQuantity!.unit = unit?.fhir_string
}
else if let fhir = ofFHIRType, "integer" == fhir {
answer.valueInteger = FHIRInteger(numeric.int32Value)
}
else {
answer.valueDecimal = FHIRDecimal(numeric.decimalValue)
}
return [answer]
}
}
extension ORKScaleQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; ignored, always "number"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let numeric = scaleAnswer else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
answer.valueDecimal = FHIRDecimal(numeric.decimalValue)
return [answer]
}
}
extension ORKBooleanQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; ignored, always "boolean"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let boolean = booleanAnswer?.boolValue else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
answer.valueBoolean = FHIRBool(boolean)
return [answer]
}
}
extension ORKTimeOfDayQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; ignored, always "time"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let components = dateComponentsAnswer else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
answer.valueTime = FHIRTime(hour: UInt8(components.hour ?? 0), minute: UInt8(components.minute ?? 0), second: 0.0)
return [answer]
}
}
extension ORKTimeIntervalQuestionResult {
/**
Creates question answers of the receiver.
TODO: implement!
- parameter ofFHIRType: The FHIR answer type that's expected; ignored, always "interval"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let interval = intervalAnswer else {
return nil
}
//let answer = QuestionnaireResponseItemAnswer()
// TODO: support interval answers
print("---> \(interval) for FHIR type “\(String(describing: ofFHIRType))”")
return nil
}
}
extension ORKDateQuestionResult {
/**
Creates question answers of the receiver.
- parameter ofFHIRType: The FHIR answer type that's expected; supports "date", "dateTime" (default) and "instant"
- returns: An array of question answers or nil
*/
func c3_responseItems(ofFHIRType: String?) -> [QuestionnaireResponseItemAnswer]? {
guard let date = dateAnswer else {
return nil
}
let answer = QuestionnaireResponseItemAnswer()
switch ofFHIRType ?? "dateTime" {
case "date":
answer.valueDate = date.fhir_asDate()
case "dateTime":
let dateTime = date.fhir_asDateTime()
// if let tz = timeZone {
// dateTime.timeZone = tz // TODO: reported NSDate is in UTC, convert to the given time zone
// }
answer.valueDateTime = dateTime
default:
c3_warn("unknown date-time FHIR type “\(ofFHIRType!)”, treating as dateTime")
let dateTime = date.fhir_asDateTime()
// if let tz = timeZone {
// dateTime.timeZone = tz
// }
answer.valueDateTime = dateTime
}
return [answer]
}
}