forked from SeifYounis/Medical-Imaging-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
307 lines (253 loc) · 11 KB
/
admin.js
File metadata and controls
307 lines (253 loc) · 11 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
import { Component } from "react";
import { io } from "socket.io-client";
import Table from "./Table";
import tableStyles from "./Table/Table.module.css";
// async function displayResults (url) {
// const serveHTML = await fetch("/scripts/serve-html")
// const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
// if (newWindow) newWindow.opener = null
// }
/**
* Instructor page to control assessment flow and adjust settings. Guided only.
*/
class Admin extends Component {
constructor() {
super();
this.state = {
// Assessments unlocked
trainingUnlocked: false,
testing1Unlocked: false,
testing2Unlocked: false,
ratingUnlocked: false,
AFCUnlocked: false,
// Number of students in each section
numTrainingStudents: 0,
numTestingStudents1: 0,
numTestingStudents2: 0,
numRatingStudents: 0,
num2AFCStudents: 0,
// Assessment config info
timeLimit: 0,
secondsVisible: 0,
numImages: 0,
isDisabled: true
}
this.socket = io()
}
// Add row to table of active student connections and updates number of students in section
addRow = (data, users) => {
let countToUpdate;
// console.log(data.current_test)
if (data.current_test === "testing1") {
countToUpdate = "numTestingStudents1"
} else if (data.current_test === "testing2") {
countToUpdate = "numTestingStudents2"
} else if (data.current_test === "training") {
countToUpdate = "numTrainingStudents"
} else if (data.current_test === "rating") {
countToUpdate = "numRatingStudents"
} else if (data.current_test === "2AFC") {
countToUpdate = "num2AFCStudents"
}
// Increment count and add student to table
this.setState({ [countToUpdate]: this.state[countToUpdate] + 1 }, () => {
let username, student_id, current_test, date_joined;
let studentInfo = { student_id, username, current_test, date_joined }
let row = document.createElement('tr');
row.setAttribute('id', data.student_id)
row.setAttribute('class', tableStyles.tableRowItems)
for (let prop in studentInfo) {
studentInfo[prop] = document.createElement('td');
studentInfo[prop].setAttribute('class', tableStyles.tableCell)
}
studentInfo['student_id'].textContent = data.student_id
studentInfo['username'].textContent = data.username
studentInfo['current_test'].textContent = data.current_test
studentInfo['date_joined'].textContent = new Date(data.date_joined).toLocaleString()
for (let prop in studentInfo) {
row.appendChild(studentInfo[prop])
}
users.appendChild(row)
})
}
// Emit event to unlock assessment to web socket server and pass in configuration
unlockAssessment(assessment) {
console.log(assessment)
this.setState({
// [assessment.replace('2', '') + "Unlocked"]: true
[assessment + "Unlocked"]: true
}, () => {
// console.log(this.state[assessment.replace('2', '') + "Unlocked"])
console.log(this.state[assessment + "Unlocked"])
})
if(assessment !== '2AFC') {
this.socket.emit(`unlock ${assessment}`, {
timeLimit: this.state.timeLimit,
secondsVisible: this.state.secondsVisible,
numImages: this.state.numImages
})
} else {
this.socket.emit('unlock 2AFC', {
numImages: this.state.numImages
})
}
alert(assessment.charAt(0).toUpperCase() + assessment.slice(1) + " Unlocked")
}
configTests(e) {
e.preventDefault()
this.setState({
timeLimit: Number(e.target.timeLimit.value),
secondsVisible: Number(e.target.secondsVisible.value),
numImages: Number(e.target.numImages.value),
isDisabled: false
}, () => alert('Settings saved'))
}
componentDidMount() {
let users = document.getElementById("active-users")
// When the admin first connects, fetch active connections from database
fetch('/admin/get-active-connections')
.then(res => {
if (res.ok) return res.json();
})
.then(conn => {
conn.forEach(data => {
this.addRow(data, users)
});
})
.catch(err => console.log("Failed to retrieve active connections"));
this.socket.emit('connect-admin');
// Handle new user connection
this.socket.on('new user', (user) => {
if (user) {
let assessment = user.current_test
this.addRow(user, users)
// If admin has set tests to unlocked, then allow incoming users to access test right away
if(this.state[assessment + "Unlocked"] === true) {
if(assessment !== '2AFC') {
this.socket.emit(`unlock ${assessment}`, {
timeLimit: this.state.timeLimit,
secondsVisible: this.state.secondsVisible,
numImages: this.state.numImages
})
} else {
this.socket.emit('unlock 2AFC', {
numImages: this.state.numImages
})
}
}
} else {
console.log("Socket sent blank data in new user")
}
})
// Remove active connection. Delete row and update count
this.socket.on('remove user', (student) => {
let countToUpdate;
if (student.current_test.includes("testing1")) {
countToUpdate = "numTestingStudents1"
} else if (student.current_test.includes("testing2")) {
countToUpdate = "numTestingStudents2"
} else if (student.current_test.includes("training")) {
countToUpdate = "numTrainingStudents"
} else if (student.current_test.includes("rating")) {
countToUpdate = "numRatingStudents"
} else if (student.current_test.includes("2AFC")) {
countToUpdate = "num2AFCStudents"
}
this.setState({ [countToUpdate]: this.state[countToUpdate] - 1 }, () => {
// console.log(student)
let user = document.getElementById(student.id)
user?.remove()
})
})
}
render() {
return (
<div className="admin-page">
<h1>Admin Page</h1>
<fieldset id="config-tests">
<legend>Configure Assessments</legend>
<form onSubmit={this.configTests.bind(this)}>
<label htmlFor="timeLimit">Enter a time limit for each question: {' '}
<input
type="number"
name="timeLimit"
min={0}
placeholder="10 seconds"
required>
</input>
</label>
<br />
<label htmlFor="secondsVisible">Number of seconds image is visible: {' '}
<input
type="number"
name="secondsVisible"
min={0}
placeholder="5 seconds"
required>
</input>
</label>
<br />
<label htmlFor="numImages">Number of images: {' '}
<select id="numImages">
<option value={10}>10</option>
<option value={20}>20</option>
<option value={30}>30</option>
<option value={40}>40</option>
<option value={50}>50</option>
<option value={60}>60</option>
</select>
</label>
<br />
<button type="submit">Confirm</button>
</form>
</fieldset>
<br />
<fieldset id="unlock-tests">
<legend>Unlock Assessments</legend>
<button
className="unlockButton"
onClick={() => this.unlockAssessment("training")}
disabled={this.state.isDisabled}>
Unlock Training</button>
<span> Students in training section: {this.state.numTrainingStudents}</span>
<br />
<button
className="unlockButton"
onClick={() => this.unlockAssessment("testing1")}
disabled={this.state.isDisabled}>
Unlock Testing 1</button>
<span> Students in testing 1 section: {this.state.numTestingStudents1}</span>
<br />
<button
className="unlockButton"
onClick={() => this.unlockAssessment("testing2")}
disabled={this.state.isDisabled}>
Unlock Testing 2</button>
<span> Students in testing 2 section: {this.state.numTestingStudents2}</span>
<br />
<button
className="unlockButton"
onClick={() => this.unlockAssessment("rating")}
disabled={this.state.isDisabled}>
Unlock Rating</button>
<span> Students in rating section: {this.state.numRatingStudents}</span>
<br />
<button
className="unlockButton"
onClick={() => this.unlockAssessment("2AFC")}
disabled={this.state.isDisabled}>
Unlock 2AFC</button>
<span> Students in 2AFC section: {this.state.num2AFCStudents}</span>
<br />
</fieldset>
<br />
{/* <fieldset id="get-results">
<legend>Get Results</legend>
<button onClick={() => displayResults('/test-display')}>Serve HTML</button>
</fieldset> */}
<Table />
</div >
)
}
}
export default Admin