-
Notifications
You must be signed in to change notification settings - Fork 6
/
Contract+C3-PRO.swift
181 lines (161 loc) · 5.86 KB
/
Contract+C3-PRO.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
//
// Contract+C3-PRO.swift
// C3PRO
//
// Created by Pascal Pfiffner on 8/14/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 SMART
import ResearchKit
public let kContractTermConsentSectionExtension = "http://fhir-registry.smarthealthit.org/StructureDefinition/ORKConsentSection"
public let kContractTermConsentSectionType = "http://researchkit.org/docs/Constants/ORKConsentSectionType"
/** Extending `Contract` for usage with ResearchKit. */
public extension Contract {
/**
Converts the receiver into a ResearchKit consent document.
- returns: An `ORKConsentDocument` created from the receiver
*/
public func c3_asConsentDocument() throws -> ORKConsentDocument {
let sections = try c3_termsAsConsentSections()
let document = ORKConsentDocument()
document.title = "Consent".c3_localized
document.signaturePageTitle = "Consent".c3_localized
document.signaturePageContent = "By agreeing you confirm that you read the consent and that you wish to take part in this research study.".c3_localized
document.sections = sections
return document
}
/**
Creates ResearchKit consent sections from the receiver's `term`s.
- returns: An array of `ORKConsentSection`
*/
public func c3_termsAsConsentSections() throws -> [ORKConsentSection] {
guard let terms = term else {
throw C3Error.consentContractHasNoTerms
}
var sections = [ORKConsentSection]()
for term in terms {
do {
let section = try term.c3_asConsentSection()
sections.append(section)
}
catch let error {
c3_warn("Contract `term` section \(term) cannot be used for consenting: \(error)")
}
}
if sections.isEmpty {
throw C3Error.consentContractHasNoTerms
}
return sections
}
}
/** Extending `ContractTerm` for usage with ResearchKit. */
public extension ContractTerm {
/**
Creates a ResearchKit consent section from the receiver.
- returns: An ORKConsentSection representing the receiver
*/
public func c3_asConsentSection() throws -> ORKConsentSection {
let type = try c3_consentSectionType()
let section = ORKConsentSection(type: type)
section.summary = text?.string
// We need extensions for some other properties
if let subs = extensions(forURI: kContractTermConsentSectionExtension)?.first?.extension_fhir {
for sub in subs {
if let url = sub.url?.absoluteString {
switch url {
case "title":
section.title = sub.valueString?.string
case "htmlContent":
section.htmlContent = sub.valueString?.string
case "htmlContentFile":
let bundle = Bundle.main
if let name = sub.valueString?.string, let url = bundle.url(forResource: name, withExtension: "html") ?? bundle.url(forResource: name, withExtension: "html", subdirectory: "HTMLContent") {
do {
section.htmlContent = try String(contentsOf: url, encoding: String.Encoding.utf8)
}
catch let error {
c3_warn("Failed to read from bundled file «\(url)»: \(error)")
}
}
else {
c3_warn("HTML consent section with name «\(String(describing: sub.valueString))» is not in main bundle nor in its «HTMLContent» subdirectory")
}
case "image":
if let name = sub.valueString?.string, let image = UIImage(named: name) {
section.customImage = image
}
else {
c3_warn("Custom consent image named «\(String(describing: sub.valueString))» is not in main bundle")
}
case "animation":
let multi = (UIScreen.main.scale >= 3.0) ? "@3x" : "@2x"
if let name = sub.valueString?.string, let url = Bundle.main.url(forResource: name + multi, withExtension: "m4v") {
section.customAnimationURL = url
}
else {
c3_warn("Custom animation named «\(String(describing: sub.valueString))» is not in main bundle")
}
default:
break
}
}
}
}
section.omitFromDocument = (section.content?.isEmpty ?? true) && (section.htmlContent?.isEmpty ?? true)
return section
}
/**
Determines the ResearchKit consent section type the receiver wants for representation.
- returns: A matching ORKConsentSectionType
*/
public func c3_consentSectionType() throws -> ORKConsentSectionType {
if let codings = type?.coding {
for code in codings {
if let url = code.system?.absoluteString, url != kContractTermConsentSectionType {
c3_logIfDebug("Ignoring consent section system “\(url)” (expecting “\(kContractTermConsentSectionType)”)")
continue
}
guard let code = code.code?.string else {
continue
}
switch code {
case "Overview":
return ORKConsentSectionType.overview
case "Privacy":
return ORKConsentSectionType.privacy
case "DataGathering":
return ORKConsentSectionType.dataGathering
case "DataUse":
return ORKConsentSectionType.dataUse
case "TimeCommitment":
return ORKConsentSectionType.timeCommitment
case "StudySurvey":
return ORKConsentSectionType.studySurvey
case "StudyTasks":
return ORKConsentSectionType.studyTasks
case "Withdrawing":
return ORKConsentSectionType.withdrawing
case "Custom":
return ORKConsentSectionType.custom
case "OnlyInDocument":
return ORKConsentSectionType.onlyInDocument
default:
throw C3Error.consentSectionTypeUnknownToResearchKit(code)
}
}
}
throw C3Error.consentSectionHasNoType
}
}