-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsignup.js
More file actions
223 lines (190 loc) · 8.05 KB
/
Copy pathsignup.js
File metadata and controls
223 lines (190 loc) · 8.05 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
document.addEventListener("DOMContentLoaded", () => {
const signupBtn = document.getElementById("signupBtn");
if (!signupBtn) {
console.error("❌ ERROR: Signup button not found.");
return;
}
signupBtn.addEventListener("click", async () => {
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const fullName = document.getElementById("fullName").value.trim();
const tutorId = document.getElementById("tutorDropdown").value;
// Get the chosen tests as an array from the multi-select.
const testsDropdown = document.getElementById("testsDropdown");
const chosenTests = Array.from(testsDropdown.selectedOptions).map(opt => opt.value);
if (!email || !password || !fullName || !tutorId || chosenTests.length === 0) {
alert("Please fill in all fields, including selecting at least one test.");
return;
}
await signUpStudent(email, password, fullName, tutorId, chosenTests);
});
});
// Function to Sign Up a Student and Store in Database
async function signUpStudent(email, password, fullName, tutorId, chosenTests) {
const { data, error } = await supabase.auth.signUp({ email, password });
if (error) {
console.error("❌ Signup failed:", error.message);
alert("Signup failed: " + error.message);
return;
}
const user = data.user;
console.log("✅ Signup successful! Auth UID:", user.id);
// Wait until the user exists in `auth.users`
await new Promise(resolve => setTimeout(resolve, 3000)); // Wait 3 seconds
if (!tutorId) {
alert("Please select a tutor.");
return;
}
// Insert student with selected tutor and chosen tests (save in 'tests' column as array of text)
const { error: insertError } = await supabase
.from("students")
.insert([{ auth_uid: user.id, email, name: fullName, tutor_id: tutorId, tests: chosenTests }]);
if (insertError) {
console.error("❌ Error saving student info:", insertError.message);
alert("Error saving student info.");
return;
}
// Initialize test progress in student_tests for each chosen test.
await initializeStudentTests(user.id, chosenTests);
alert("✅ Signup successful! You can now log in.");
window.location.href = "login.html"; // Redirect to login
}
// Function to initialize test progress for a new student based on chosen tests.
// For each chosen test (tipologia_test), we query the appropriate table(s)
// and create entries in the student_tests table with the column tipologia_test set accordingly.
async function initializeStudentTests(authUid, chosenTests) {
console.log("📌 Initializing tests for user:", authUid, "with chosen tests:", chosenTests);
const testEntries = [];
// Process chosen tests from the tolc_i table.
const { data: tolcTests, error: tolcError } = await supabase
.from("questions")
.select("section, tipologia_esercizi, progressivo, tipologia_test")
.in("tipologia_test", chosenTests)
.order("progressivo");
if (tolcError) {
console.error("❌ Error fetching tolc_i test structure:", tolcError.message);
} else if (tolcTests) {
// Remove duplicates manually.
const uniqueTolcTests = Array.from(
new Set(tolcTests.map(test => `${test.section}|${test.tipologia_esercizi}|${test.progressivo}|${test.tipologia_test}`))
).map(key => {
const parts = key.split("|");
return {
section: parts[0],
tipologia_esercizi: parts[1],
progressivo: Number(parts[2]),
tipologia_test: parts[3]
};
});
uniqueTolcTests.forEach(test => {
// Unlock tests with progressivo === 1 (first test for that tipologia) and lock others.
testEntries.push({
auth_uid: authUid,
section: test.section,
tipologia_esercizi: test.tipologia_esercizi,
progressivo: test.progressivo,
tipologia_test: test.tipologia_test,
status: test.progressivo === 1 ? "unlocked" : "locked"
});
});
}
// Process chosen tests from the bocconi table.
const { data: bocconiTests, error: bocconiError } = await supabase
.from("questions_bancaDati")
.select("section, tipologia_esercizi, tipologia_test")
.in("tipologia_test", chosenTests)
.order("progressivo");
if (bocconiError) {
console.error("❌ Error fetching bocconi test structure:", bocconiError.message);
} else if (bocconiTests) {
const uniqueBocconiTests = Array.from(
new Set(bocconiTests.map(test => `${test.section}-${test.tipologia_esercizi}-${test.tipologia_test}`))
).map(key => {
const [section, tipologia_esercizi, tipologia_test] = key.split("-").map((v, i) => i < 2 ? Number(v) : v);
return { section, tipologia_esercizi, tipologia_test };
});
uniqueBocconiTests.forEach(test => {
// For bocconi, we don’t have progressivo so we can set a default value (e.g., 1)
testEntries.push({
auth_uid: authUid,
section: test.section,
tipologia_esercizi: test.tipologia_esercizi,
progressivo: test.progressivo,
tipologia_test: test.tipologia_test,
status: "locked" // or you might want to unlock the first one; adjust as needed
});
});
}
if (testEntries.length > 0) {
const { error: insertError } = await supabase
.from("student_tests")
.insert(testEntries);
if (insertError) {
console.error("❌ Error initializing student tests:", insertError.message);
} else {
console.log("✅ Student test entries created!");
}
} else {
console.log("No test entries to create.");
}
}
async function loadTutors() {
const tutorDropdown = document.getElementById("tutorDropdown");
const { data, error } = await supabase
.from("tutors")
.select("id, name");
if (error) {
console.error("❌ Error fetching tutors:", error.message);
alert("Failed to load tutors.");
return;
}
tutorDropdown.innerHTML = '<option value="">Select Your Tutor</option>';
data.forEach(tutor => {
let option = document.createElement("option");
option.value = tutor.id;
option.textContent = tutor.name;
tutorDropdown.appendChild(option);
});
}
async function loadTestOptions() {
// This function queries both tables to get unique tipologia_test values.
const { data: tolcData, error: tolcError } = await supabase
.from("questions")
.select("tipologia_test");
const { data: bocconiData, error: bocconiError } = await supabase
.from("questions_bancaDati")
.select("tipologia_test");
if (tolcError) {
console.error("❌ Error fetching tolc_i test options:", tolcError.message);
}
if (bocconiError) {
console.error("❌ Error fetching bocconi test options:", bocconiError.message);
}
let options = [];
if (tolcData) {
tolcData.forEach(row => {
if (row.tipologia_test && !options.includes(row.tipologia_test)) {
options.push(row.tipologia_test);
}
});
}
if (bocconiData) {
bocconiData.forEach(row => {
if (row.tipologia_test && !options.includes(row.tipologia_test)) {
options.push(row.tipologia_test);
}
});
}
// Populate the multi-select element.
const testsDropdown = document.getElementById("testsDropdown");
testsDropdown.innerHTML = ""; // Clear existing options.
options.forEach(opt => {
const optionEl = document.createElement("option");
optionEl.value = opt;
optionEl.textContent = opt;
testsDropdown.appendChild(optionEl);
});
}
document.getElementById("refreshTutors").addEventListener("click", loadTutors);
document.addEventListener("DOMContentLoaded", loadTutors);
document.addEventListener("DOMContentLoaded", loadTestOptions);