/
remove_subject.go
229 lines (196 loc) · 5.93 KB
/
remove_subject.go
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
package randomization
import (
"fmt"
"net/http"
"strings"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
)
// removeSubject
func removeSubject(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
pkey := r.FormValue("pkey")
if ok := checkAccess(ctx, user, pkey, &w, r); !ok {
return
}
proj, err := getProjectFromKey(ctx, pkey)
if err != nil {
msg := "Datastore error: unable to retrieve project."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
if proj.Owner != user.String() {
msg := "Only the project owner can remove treatment group assignments that have already been made."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
if !proj.StoreRawData {
msg := "Subjects cannot be removed for a project in which the subject level data is not stored"
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
tvals := struct {
User string
LoggedIn bool
Pkey string
ProjectName string
Any_removed_subjects bool
RemovedSubjects string
}{
User: user.String(),
LoggedIn: user != nil,
Pkey: pkey,
ProjectName: proj.Name,
}
if len(proj.RemovedSubjects) > 0 {
tvals.Any_removed_subjects = true
tvals.RemovedSubjects = strings.Join(proj.RemovedSubjects, ", ")
} else {
tvals.Any_removed_subjects = false
}
if err := tmpl.ExecuteTemplate(w, "remove_subject.html", tvals); err != nil {
log.Errorf(ctx, "Failed to execute template: %v", err)
}
}
// removeSubjectConfirm
func removeSubjectConfirm(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
pkey := r.FormValue("pkey")
subjectId := r.FormValue("subject_id")
proj, err := getProjectFromKey(ctx, pkey)
if err != nil {
msg := "Datastore error: unable to retrieve project."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
if proj.Owner != user.String() {
msg := "Only the project owner can remove treatment group assignments that have already been made."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
// Check if the subject has already been removed
for _, s := range proj.RemovedSubjects {
if s == subjectId {
msg := fmt.Sprintf("Subject '%s' has already been removed from the study.", subjectId)
rmsg := "Return to project"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
}
// Check if the subject exists
found := false
for _, rec := range proj.RawData {
if rec.SubjectId == subjectId {
found = true
break
}
}
if !found {
msg := fmt.Sprintf("There is no subject with id '%s' in the project.", subjectId)
rmsg := "Return to project"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
tvals := struct {
User string
LoggedIn bool
Pkey string
SubjectId string
ProjectName string
}{
User: user.String(),
LoggedIn: user != nil,
SubjectId: subjectId,
Pkey: pkey,
ProjectName: proj.Name,
}
if err := tmpl.ExecuteTemplate(w, "remove_subject_confirm.html", tvals); err != nil {
log.Errorf(ctx, "Failed to execute template: %v", err)
}
}
// removeSubjectCompleted
func removeSubjectCompleted(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
pkey := r.FormValue("pkey")
if ok := checkAccess(ctx, user, pkey, &w, r); !ok {
msg := "You do not have access to this page."
rmsg := "Return"
messagePage(w, r, user, msg, rmsg, "/")
return
}
proj, err := getProjectFromKey(ctx, pkey)
if err != nil {
msg := "Datastore error: unable to retrieve project."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
if proj.Owner != user.String() {
msg := "Only the project owner can remove treatment group assignments that have already been made."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
if !proj.StoreRawData {
msg := "Subjects cannot be removed for a project in which the subject level data is not stored"
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
subjectId := r.FormValue("subject_id")
found := false
var removeRec *DataRecord
for _, rec := range proj.RawData {
if rec.SubjectId == subjectId {
rec.Included = false
removeRec = rec
found = true
}
}
proj.RemovedSubjects = append(proj.RemovedSubjects, subjectId)
comment := new(Comment)
comment.Person = user.String()
comment.DateTime = time.Now()
comment.Comment = []string{fmt.Sprintf("Subject '%s' removed from the project.", subjectId)}
proj.Comments = append(proj.Comments, comment)
if !found {
msg := fmt.Sprintf("Unable to remove subject '%s' from the project.", subjectId)
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
removeFromAggregate(removeRec, proj)
proj.NumAssignments--
err = storeProject(ctx, proj, pkey)
if err != nil {
msg := "Error, unable to save project."
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
msg := fmt.Sprintf("Subject '%s' has been removed from the study.", subjectId)
rmsg := "Return to project dashboard"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
}