forked from C3-PRO/c3-pro-ios-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionnaireItemPromise.swift
More file actions
394 lines (331 loc) · 13.8 KB
/
QuestionnaireItemPromise.swift
File metadata and controls
394 lines (331 loc) · 13.8 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// QuestionnaireItemPromise.swift
// C3PRO
//
// Created by Pascal Pfiffner on 4/20/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 Foundation
import SMART
import ResearchKit
let kORKTextChoiceSystemSeparator: Character = " "
let kORKTextChoiceDefaultSystem = "https://fhir.smalthealthit.org"
let kORKTextChoiceMissingCodeCode = "⚠️"
let kC3ValuePickerFormatExtensionURL = "http://fhir-registry.smarthealthit.org/StructureDefinition/value-picker"
/**
A promise that can fulfill a questionnaire question into an ORKQuestionStep.
*/
class QuestionnaireItemPromise: QuestionnairePromiseProto {
/// The promises' item.
let item: QuestionnaireItem
/// Parent item, if any.
weak var parent: QuestionnaireItemPromise?
/// The step(s), internally assigned after the promise has been successfully fulfilled.
internal(set) var steps: [ORKStep]?
/**
Designated initializer.
- parameter question: The question the receiver represents
*/
init(item: QuestionnaireItem, parent: QuestionnaireItemPromise? = nil) {
self.item = item
self.parent = parent
}
// MARK: - Fulfilling
/**
Fulfill the promise.
Once the promise has been successfully fulfilled, the `step` property will be assigned. No guarantees as to on which queue the callback
will be called.
- parameter parentRequirements: Requirements from the parent that must be inherited
- parameter callback: The callback to be called when done; note that even when you get an error, some steps might have successfully been
allocated still, so don't throw everything away just because you receive errors
*/
func fulfill(requiring parentRequirements: [ResultRequirement]?, callback: @escaping (([Error]?) -> Void)) {
// resolve answer format, THEN resolve sub-groups, if any
item.c3_asAnswerFormat() { format, error in
var steps = [ORKStep]()
var thisStep: ConditionalStep?
var errors = [Error]()
var requirements = parentRequirements ?? [ResultRequirement]()
let (title, text) = self.item.c3_bestTitleAndText()
// find item's "enableWhen" requirements
do {
if let myreqs = try self.item.c3_enableQuestionnaireElementWhen() {
requirements.append(contentsOf: myreqs)
}
}
catch let error {
errors.append(error)
}
// we know the answer format, so this is a question, create a conditional step
if let fmt = format {
let step = ConditionalQuestionStep(identifier: self.linkId, linkIds: self.linkIds, title: title, answer: fmt)
step.fhirType = self.item.type?.rawValue
step.text = text
step.isOptional = !(self.item.required ?? false)
thisStep = step
}
else if let error = error {
errors.append(error)
}
// no error and no answer format but title and text - must be "display" or "group" item that has something to show!
else if nil != title || nil != text {
thisStep = ConditionalInstructionStep(identifier: self.linkId, linkIds: self.linkIds, title: title, text: text)
}
// TODO: also look at "initial[x]" value and prepopulate
// collect the step
if var step = thisStep {
if !requirements.isEmpty {
step.add(requirements: requirements)
}
if let step = step as? ORKStep {
steps.append(step)
}
}
// do we have sub-groups?
if let subitems = self.item.item {
let subpromises = subitems.map() { QuestionnaireItemPromise(item: $0, parent: ("{root}" == self.linkId) ? nil : self) }
// fulfill all group promises
let queueGroup = DispatchGroup()
for subpromise in subpromises {
queueGroup.enter()
subpromise.fulfill(requiring: requirements) { berrors in
if nil != berrors {
errors.append(contentsOf: berrors!)
}
queueGroup.leave()
}
}
// all done
queueGroup.notify(queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated)) {
let gsteps = subpromises.filter() { return nil != $0.steps }.flatMap() { return $0.steps! }
steps.append(contentsOf: gsteps)
self.steps = steps
callback(errors.count > 0 ? errors : nil)
}
}
else {
self.steps = steps
callback(errors)
}
}
}
// MARK: - Properties
var linkId: String {
return item.linkId?.string ?? UUID().uuidString
}
/// Returns an array of all linkIds of the parents down to the receiver.
var linkIds: [String] {
var ids = [String]()
var prnt = parent
while let parent = prnt {
ids.append(parent.linkId)
prnt = parent.parent
}
return ids
}
// MARK: - Printable
/// String representation of the receiver.
var description: String {
return "<\(type(of: self))>"
}
}
// MARK: -
extension QuestionnaireItem {
/**
Attempts to create a nice title and text from the various fields of the group.
- returns: A tuple of strings for title and text
*/
func c3_bestTitleAndText() -> (String?, String?) {
let cDisplay = code?.filter() { return nil != $0.display }.map() { return $0.display!.localized }
let cCodes = code?.filter() { return nil != $0.code }.map() { return $0.code!.string } // TODO: can these be localized?
var ttl = cDisplay?.first ?? cCodes?.first
var txt = text?.localized
if nil == ttl {
ttl = text?.localized
txt = nil
}
if nil == txt {
txt = c3_questionInstruction() ?? c3_questionHelpText() // even if the title is still nil, we won't want to populate the title with help text
}
// TODO: Even if we have title and instructions, show help somewhere if present
return (ttl?.c3_stripMultipleSpaces(), txt?.c3_stripMultipleSpaces())
}
func c3_questionMinOccurs() -> Int? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-minOccurs")?.first?.valueInteger?.int
}
func c3_questionMaxOccurs() -> Int? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-maxOccurs")?.first?.valueInteger?.int
}
func c3_questionInstruction() -> String? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-instruction")?.first?.valueString?.localized
}
func c3_questionHelpText() -> String? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-help")?.first?.valueString?.localized
}
func c3_numericAnswerUnit() -> String? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-units")?.first?.valueString?.localized
}
func c3_defaultAnswer() -> Extension? {
return extensions(forURI: "http://hl7.org/fhir/StructureDefinition/questionnaire-defaultValue")?.first
}
/**
Determine ResearchKit's answer format for the question type.
Questions are multiple choice if "repeats" is set to true and the "max-occurs" extension is either not defined or larger than 1. See
`c3_answerChoiceStyle`.
TODO: "open-choice" allows to choose an option OR to give a textual response: implement
[x] ORKScaleAnswerFormat: "integer" plus min- and max-values defined, where max > min
[ ] ORKContinuousScaleAnswerFormat:
[x] ORKValuePickerAnswerFormat: "choice" (not multiple) plus extension `kC3ValuePickerFormatExtensionURL` (bool)
[ ] ORKImageChoiceAnswerFormat:
[x] ORKTextAnswerFormat: "string", "url"
[x] ORKTextChoiceAnswerFormat: "choice", "choice-open" (!)
[x] ORKBooleanAnswerFormat: "boolean"
[x] ORKNumericAnswerFormat: "decimal", "integer", "quantity"
[x] ORKDateAnswerFormat: "date", "dateTime", "instant"
[x] ORKTimeOfDayAnswerFormat: "time"
[ ] ORKTimeIntervalAnswerFormat:
*/
func c3_asAnswerFormat(callback: @escaping ((ORKAnswerFormat?, Error?) -> Void)) {
let link = linkId ?? "<nil>"
if let type = type {
switch type {
case .boolean: callback(ORKAnswerFormat.booleanAnswerFormat(), nil)
case .decimal: callback(ORKAnswerFormat.decimalAnswerFormat(withUnit: nil), nil)
case .integer:
let minVals = c3_minValue()
let maxVals = c3_maxValue()
let minVal = minVals?.filter() { return $0.valueInteger != nil }.first?.valueInteger?.int
let maxVal = maxVals?.filter() { return $0.valueInteger != nil }.first?.valueInteger?.int
if let minVal = minVal, let maxVal = maxVal, maxVal > minVal {
let minDesc = minVals?.filter() { return $0.valueString != nil }.first?.valueString?.localized
let maxDesc = maxVals?.filter() { return $0.valueString != nil }.first?.valueString?.localized
let defVal = c3_defaultAnswer()?.valueInteger?.int ?? minVal
let format = ORKAnswerFormat.scale(withMaximumValue: Int(maxVal), minimumValue: Int(minVal), defaultValue: Int(defVal),
step: 1, vertical: (maxVal - minVal > 5),
maximumValueDescription: maxDesc, minimumValueDescription: minDesc)
callback(format, nil)
}
else {
callback(ORKAnswerFormat.integerAnswerFormat(withUnit: nil), nil)
}
case .quantity: callback(ORKAnswerFormat.decimalAnswerFormat(withUnit: c3_numericAnswerUnit()), nil)
case .date: callback(ORKAnswerFormat.dateAnswerFormat(), nil)
case .dateTime: callback(ORKAnswerFormat.dateTime(), nil)
case .time: callback(ORKAnswerFormat.timeOfDayAnswerFormat(), nil)
case .string: callback(ORKAnswerFormat.textAnswerFormat(), nil)
case .url: callback(ORKAnswerFormat.textAnswerFormat(), nil)
case .choice:
c3_resolveAnswerChoices() { choices, error in
if nil != error || nil == choices {
callback(nil, error ?? C3Error.questionnaireNoChoicesInChoiceQuestion(self))
}
else {
let multiStyle = self.c3_answerChoiceStyle()
if .multipleChoice != multiStyle, self.extensions(forURI: kC3ValuePickerFormatExtensionURL)?.first?.valueBoolean?.bool ?? false {
callback(ORKAnswerFormat.valuePickerAnswerFormat(with: choices!), nil)
}
else {
callback(ORKAnswerFormat.choiceAnswerFormat(with: multiStyle, textChoices: choices!), nil)
}
}
}
case .openChoice:
c3_resolveAnswerChoices() { choices, error in
if nil != error || nil == choices {
callback(nil, error ?? C3Error.questionnaireNoChoicesInChoiceQuestion(self))
}
else {
callback(ORKAnswerFormat.choiceAnswerFormat(with: self.c3_answerChoiceStyle(), textChoices: choices!), nil)
}
}
//case .attachment: callback(format: nil, error: nil)
//case .reference: callback(format: nil, error: nil)
case .display:
callback(nil, nil)
case .group:
callback(nil, nil)
default:
callback(nil, C3Error.questionnaireQuestionTypeUnknownToResearchKit(self))
}
}
else {
NSLog("Question «\(String(describing: text))» does not have an answer type, assuming text answer [linkId: \(link)]")
callback(ORKAnswerFormat.textAnswerFormat(), nil)
}
}
/**
For `choice` type questions, retrieves the possible answers and returns them as ORKTextChoice in the callback.
The `value` property of the text choice is a combination of the coding system URL and the code, separated by
`kORKTextChoiceSystemSeparator` (a space). If no system URL is provided, "https://fhir.smalthealthit.org" is used.
*/
func c3_resolveAnswerChoices(callback: @escaping (([ORKTextChoice]?, Error?) -> Void)) {
// options are defined inline
if let _ = option {
// TODO: implement!
callback(nil, C3Error.notImplemented("Using `option` in Questionnaire.item is not yet supported, use `options`"))
}
// options are a referenced ValueSet
else if let options = options {
options.resolve(ValueSet.self) { valueSet in
var choices = [ORKTextChoice]()
// we have an expanded ValueSet
if let expansion = valueSet?.expansion?.contains {
for option in expansion {
let system = option.system?.absoluteString ?? kORKTextChoiceDefaultSystem
let code = option.code?.string ?? kORKTextChoiceMissingCodeCode
let value = "\(system)\(kORKTextChoiceSystemSeparator)\(code)"
let text = ORKTextChoice(text: option.display_localized ?? code, value: value as NSCoding & NSCopying & NSObjectProtocol)
choices.append(text)
}
}
// valueset includes or defines codes
else if let compose = valueSet?.compose {
if let options = compose.include {
for option in options {
let system = option.system?.absoluteString ?? kORKTextChoiceDefaultSystem // system is a required property
if let concepts = option.concept {
for concept in concepts {
let code = concept.code?.string ?? kORKTextChoiceMissingCodeCode // code is a required property, so SHOULD always be present
let value = "\(system)\(kORKTextChoiceSystemSeparator)\(code)"
let text = ORKTextChoice(text: concept.display_localized ?? code, value: value as NSCoding & NSCopying & NSObjectProtocol)
choices.append(text)
}
}
}
}
// TODO: also support `import`
}
// all done
if choices.count > 0 {
callback(choices, nil)
}
else {
callback(nil, C3Error.questionnaireNoChoicesInChoiceQuestion(self))
}
}
}
else {
callback(nil, C3Error.questionnaireNoChoicesInChoiceQuestion(self))
}
}
/**
For `choice` type questions, inspect if the given question is single or multiple choice. Questions are multiple choice if "repeats" is
true and the "max-occurs" extension is either not defined or larger than 1.
*/
func c3_answerChoiceStyle() -> ORKChoiceAnswerStyle {
let multiple = (repeats?.bool ?? false) && ((c3_questionMaxOccurs() ?? 2) > 1)
return multiple ? .multipleChoice : .singleChoice
}
}