EHDEM commited on
Commit
323005e
·
verified ·
1 Parent(s): 1c98cb9

Manual changes saved

Browse files
Files changed (1) hide show
  1. student-management.html +65 -124
student-management.html CHANGED
@@ -20,7 +20,6 @@
20
  </div>
21
  </div>
22
  </header>
23
-
24
  <!-- Management Tabs -->
25
  <div class="bg-white rounded-lg shadow-md p-6 mb-8">
26
  <div class="flex border-b border-gray-200 mb-6 overflow-x-auto">
@@ -37,7 +36,6 @@
37
  <i class="fas fa-file-import mr-2"></i> Import/Export
38
  </button>
39
  </div>
40
-
41
  <!-- Add Student Form -->
42
  <div id="add-form" class="space-y-4">
43
  <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
@@ -76,7 +74,6 @@
76
  </div>
77
  </div>
78
  </div>
79
-
80
  <!-- Remove Student Form -->
81
  <div id="remove-form" class="space-y-4 hidden">
82
  <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
@@ -118,8 +115,7 @@
118
  </div>
119
  </div>
120
  </div>
121
-
122
- <!-- Manage Classes Form (UPDATED WITH ALL FORMS OPTION) -->
123
  <div id="class-form" class="space-y-6 hidden">
124
  <div class="bg-blue-50 border border-blue-200 text-blue-700 p-4 rounded-lg">
125
  <div class="flex items-center">
@@ -130,7 +126,6 @@
130
  </div>
131
  </div>
132
  </div>
133
-
134
  <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
135
  <div>
136
  <label for="class-action" class="block text-gray-700 font-medium mb-2">Action</label>
@@ -158,14 +153,10 @@
158
  </select>
159
  </div>
160
  </div>
161
-
162
- <!-- Add Class Input -->
163
  <div id="add-class-container" class="hidden">
164
  <label for="new-class-name" class="block text-gray-700 font-medium mb-2">New Class Name</label>
165
  <input type="text" id="new-class-name" class="w-full px-4 py-2 border border-gray-300 rounded-lg" placeholder="e.g. CERIA">
166
  </div>
167
-
168
- <!-- Remove Class Select -->
169
  <div id="remove-class-container" class="hidden">
170
  <label for="remove-class-name" class="block text-gray-700 font-medium mb-2">Class to Remove</label>
171
  <select id="remove-class-name" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
@@ -173,11 +164,9 @@
173
  </select>
174
  <p class="text-sm text-red-600 mt-2">Warning: This will permanently delete the class and all its students from the selected form(s).</p>
175
  </div>
176
-
177
  <button id="apply-class-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded-lg transition duration-200">
178
  <i class="fas fa-save mr-2"></i> Apply Changes
179
  </button>
180
-
181
  <div id="class-success" class="hidden bg-green-50 border border-green-200 text-green-700 p-4 rounded-lg">
182
  <div class="flex items-center">
183
  <i class="fas fa-check-circle text-green-500 text-xl mr-3"></i>
@@ -188,7 +177,6 @@
188
  </div>
189
  </div>
190
  </div>
191
-
192
  <!-- Import/Export Form -->
193
  <div id="import-export-form" class="space-y-4 hidden">
194
  <div class="bg-blue-50 border border-blue-200 text-blue-700 p-4 rounded-lg mb-4">
@@ -204,7 +192,7 @@
204
  <!-- Export Section -->
205
  <div class="bg-white border border-gray-200 rounded-lg p-4">
206
  <h3 class="text-lg font-medium mb-4 text-gray-800">Export Data</h3>
207
- <p class="text-gray-600 mb-4">Download current student list in CSV format for backup or sharing.</p>
208
  <button id="export-btn" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200">
209
  <i class="fas fa-file-export mr-2"></i> Export to CSV
210
  </button>
@@ -213,8 +201,19 @@
213
  <i class="fas fa-file-download mr-2"></i> Sample CSV
214
  </button>
215
  </div>
216
- <div id="export-success" class="hidden bg-green-50 border border-green-200 text-green-700 p-3 rounded-lg mt-3">
217
- Export completed successfully! <a href="#" id="download-link" class="font-bold underline">Download now</a>
 
 
 
 
 
 
 
 
 
 
 
218
  </div>
219
  </div>
220
  <!-- Import Section -->
@@ -248,9 +247,11 @@
248
  </div>
249
  </div>
250
  </div>
251
-
252
- <script src="config.js"></script>
253
  <script>
 
 
 
 
254
  const CSV_TEMPLATE = `class,form,studentName
255
  BUDI,1,John Doe
256
  BUDI,1,Jane Smith
@@ -258,15 +259,14 @@ USAHA,2,Michael Johnson
258
  USAHA,2,Sarah Williams
259
  MAJU,3,David Brown
260
  MAJU,3,Emily Davis`;
261
-
262
  let students = {};
263
  let availableClasses = [];
264
-
265
  async function loadStudentList() {
266
  try {
267
- const response = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.STUDENT_LIST || GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/student-list.json`, {
268
  headers: {
269
- 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
270
  'Accept': 'application/vnd.github.v3+json'
271
  }
272
  });
@@ -281,46 +281,40 @@ MAJU,3,Emily Davis`;
281
  throw new Error('API error: ' + response.status);
282
  }
283
  } catch (error) {
284
- console.error('Failed to load student list:', error);
285
- alert('Could not load student list from GitHub. Starting with empty list.');
286
  students = {};
287
  availableClasses = [];
288
  }
289
  updateClassDropdowns();
290
  loadStudentsForRemoval();
291
  }
292
-
293
  function updateClassDropdowns() {
294
  const addSelect = document.getElementById('add-class');
295
  const removeSelect = document.getElementById('remove-class');
296
-
297
  addSelect.innerHTML = '<option value="">Select Class</option>';
298
  removeSelect.innerHTML = '<option value="">Select Class</option>';
299
-
300
  availableClasses.forEach(cls => {
301
  const optAdd = document.createElement('option');
302
  optAdd.value = cls;
303
  optAdd.textContent = cls;
304
  addSelect.appendChild(optAdd);
305
-
306
  const optRemove = document.createElement('option');
307
  optRemove.value = cls;
308
  optRemove.textContent = cls;
309
  removeSelect.appendChild(optRemove);
310
  });
311
-
312
  if (availableClasses.length === 0) {
313
  addSelect.innerHTML += '<option disabled>No classes available yet</option>';
314
  removeSelect.innerHTML += '<option disabled>No classes available yet</option>';
315
  }
316
  }
317
-
318
  async function saveStudentListToGitHub() {
319
  try {
320
  let sha = null;
321
- const getResponse = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.STUDENT_LIST || GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/student-list.json`, {
322
  headers: {
323
- 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
324
  'Accept': 'application/vnd.github.v3+json'
325
  }
326
  });
@@ -328,14 +322,12 @@ MAJU,3,Emily Davis`;
328
  const fileData = await getResponse.json();
329
  sha = fileData.sha;
330
  }
331
-
332
  const newContent = JSON.stringify(students, null, 2);
333
  const base64Content = btoa(newContent);
334
-
335
- const putResponse = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.STUDENT_LIST || GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/student-list.json`, {
336
  method: 'PUT',
337
  headers: {
338
- 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
339
  'Content-Type': 'application/json',
340
  'Accept': 'application/vnd.github.v3+json'
341
  },
@@ -346,7 +338,6 @@ MAJU,3,Emily Davis`;
346
  branch: 'main'
347
  })
348
  });
349
-
350
  if (!putResponse.ok) {
351
  const err = await putResponse.json();
352
  throw new Error(err.message || 'Save failed');
@@ -354,28 +345,30 @@ MAJU,3,Emily Davis`;
354
  return true;
355
  } catch (error) {
356
  console.error('Save error:', error);
357
- alert('Failed to save student list: ' + error.message);
358
  return false;
359
  }
360
  }
361
-
362
- // Export: fetch latest from GitHub then generate CSV
363
  document.getElementById('export-btn').addEventListener('click', async function() {
364
  this.disabled = true;
365
  this.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i> Exporting...';
366
-
367
- await loadStudentList(); // Get the absolute latest data
368
-
369
  const csv = generateCSV();
370
- downloadCSV(csv, `student-list-${new Date().toISOString().split('T')[0]}.csv`);
371
-
 
 
 
 
 
 
 
 
372
  document.getElementById('export-success').classList.remove('hidden');
373
- setTimeout(() => document.getElementById('export-success').classList.add('hidden'), 5000);
374
-
375
  this.disabled = false;
376
  this.innerHTML = '<i class="fas fa-file-export mr-2"></i> Export to CSV';
377
  });
378
-
379
  function generateCSV() {
380
  let csv = 'class,form,studentName\n';
381
  for (const key in students) {
@@ -386,24 +379,18 @@ MAJU,3,Emily Davis`;
386
  }
387
  return csv;
388
  }
389
-
390
- function downloadCSV(content, filename) {
391
- const blob = new Blob([content], { type: 'text/csv;charset=utf-8;' });
392
  const url = URL.createObjectURL(blob);
393
  const link = document.createElement('a');
394
  link.href = url;
395
- link.download = filename;
396
  document.body.appendChild(link);
397
  link.click();
398
  document.body.removeChild(link);
399
  URL.revokeObjectURL(url);
400
- }
401
-
402
- document.getElementById('export-sample-btn').addEventListener('click', function() {
403
- downloadCSV(CSV_TEMPLATE, 'student-list-sample.csv');
404
  });
405
-
406
- // Parse CSV
407
  function parseCSV(content) {
408
  const lines = content.split('\n').map(l => l.trim()).filter(l => l);
409
  if (lines.length < 1) throw new Error('Empty CSV');
@@ -411,7 +398,6 @@ MAJU,3,Emily Davis`;
411
  if (!headers.includes('class') || !headers.includes('form') || !headers.includes('studentname')) {
412
  throw new Error('Missing required columns: class, form, studentName');
413
  }
414
-
415
  const newStudents = {};
416
  for (let i = 1; i < lines.length; i++) {
417
  const row = lines[i];
@@ -427,7 +413,6 @@ MAJU,3,Emily Davis`;
427
  }
428
  return newStudents;
429
  }
430
-
431
  document.getElementById('import-btn').addEventListener('click', async function() {
432
  const fileInput = document.getElementById('csv-file');
433
  const file = fileInput.files[0];
@@ -460,15 +445,12 @@ MAJU,3,Emily Davis`;
460
  };
461
  reader.readAsText(file);
462
  });
463
-
464
  document.getElementById('csv-file').addEventListener('change', function(e) {
465
  const fileName = e.target.files[0]?.name || 'No file selected';
466
  document.getElementById('file-name').textContent = fileName;
467
  });
468
-
469
  window.addEventListener('load', loadStudentList);
470
-
471
- // Tab switching (your old code)
472
  document.getElementById('add-tab').addEventListener('click', function() {
473
  document.getElementById('add-form').classList.remove('hidden');
474
  document.getElementById('remove-form').classList.add('hidden');
@@ -483,7 +465,6 @@ MAJU,3,Emily Davis`;
483
  document.getElementById('import-export-tab').classList.add('text-gray-500');
484
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
485
  });
486
-
487
  document.getElementById('remove-tab').addEventListener('click', function() {
488
  document.getElementById('remove-form').classList.remove('hidden');
489
  document.getElementById('add-form').classList.add('hidden');
@@ -498,7 +479,6 @@ MAJU,3,Emily Davis`;
498
  document.getElementById('import-export-tab').classList.add('text-gray-500');
499
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
500
  });
501
-
502
  document.getElementById('class-tab').addEventListener('click', function() {
503
  document.getElementById('class-form').classList.remove('hidden');
504
  document.getElementById('add-form').classList.add('hidden');
@@ -514,7 +494,6 @@ MAJU,3,Emily Davis`;
514
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
515
  updateClassFormVisibility();
516
  });
517
-
518
  document.getElementById('import-export-tab').addEventListener('click', function() {
519
  document.getElementById('import-export-form').classList.remove('hidden');
520
  document.getElementById('add-form').classList.add('hidden');
@@ -529,11 +508,8 @@ MAJU,3,Emily Davis`;
529
  document.getElementById('class-tab').classList.add('text-gray-500');
530
  document.getElementById('class-tab').classList.remove('text-blue-600', 'border-blue-600');
531
  });
532
-
533
- // Load students for removal (your old code)
534
  document.getElementById('remove-class').addEventListener('change', loadStudentsForRemoval);
535
  document.getElementById('remove-form-select').addEventListener('change', loadStudentsForRemoval);
536
-
537
  function loadStudentsForRemoval() {
538
  const classVal = document.getElementById('remove-class').value;
539
  const formVal = document.getElementById('remove-form-select').value;
@@ -556,8 +532,6 @@ MAJU,3,Emily Davis`;
556
  }
557
  }
558
  }
559
-
560
- // Add student (your old code + refresh dropdowns)
561
  document.getElementById('add-student-btn').addEventListener('click', async function() {
562
  const className = document.getElementById('add-class').value;
563
  const formVal = document.getElementById('add-form-select').value;
@@ -566,12 +540,14 @@ MAJU,3,Emily Davis`;
566
  alert('Please enter student name');
567
  return;
568
  }
569
- const key = `${className}-${formVal}`;
570
- if (!students[key]) {
571
- students[key] = [];
572
  }
 
 
573
  if (students[key].includes(studentName)) {
574
- alert('This student already exists in this class/form');
575
  return;
576
  }
577
  students[key].push(studentName);
@@ -579,45 +555,34 @@ MAJU,3,Emily Davis`;
579
  if (saved) {
580
  document.getElementById('student-name').value = '';
581
  document.getElementById('add-success').classList.remove('hidden');
582
- setTimeout(() => {
583
- document.getElementById('add-success').classList.add('hidden');
584
- }, 3000);
585
  availableClasses = [...new Set(Object.keys(students).map(k => k.split('-')[0]))].sort();
586
  updateClassDropdowns();
587
  }
588
  });
589
-
590
- // Remove student (your old code + refresh dropdowns)
591
  document.getElementById('remove-student-btn').addEventListener('click', async function() {
592
  const className = document.getElementById('remove-class').value;
593
  const formVal = document.getElementById('remove-form-select').value;
594
  const studentName = document.getElementById('student-select').value;
595
  if (!studentName) {
596
- alert('Please select a student to remove');
597
  return;
598
  }
599
  const key = `${className}-${formVal}`;
600
  if (students[key]) {
601
- const index = students[key].indexOf(studentName);
602
- if (index !== -1) {
603
- students[key].splice(index, 1);
604
- }
605
  }
606
  const saved = await saveStudentListToGitHub();
607
  if (saved) {
608
  loadStudentsForRemoval();
609
  document.getElementById('remove-success').classList.remove('hidden');
610
- setTimeout(() => {
611
- document.getElementById('remove-success').classList.add('hidden');
612
- }, 3000);
613
  availableClasses = [...new Set(Object.keys(students).map(k => k.split('-')[0]))].sort();
614
  updateClassDropdowns();
615
  }
616
  });
617
-
618
- // Manage Classes - NEW LOGIC WITH ALL FORMS OPTION (your old code + refresh dropdowns at end)
619
  const forms = ['1', '2', '3', '4', '5'];
620
-
621
  function updateClassFormVisibility() {
622
  const action = document.getElementById('class-action').value;
623
  const scope = document.getElementById('class-scope').value;
@@ -631,16 +596,12 @@ MAJU,3,Emily Davis`;
631
  populateRemoveClassDropdown(scope);
632
  }
633
  }
634
-
635
  function populateRemoveClassDropdown(scope) {
636
  const select = document.getElementById('remove-class-name');
637
  select.innerHTML = '<option value="">Select Class</option>';
638
  const classes = new Set();
639
  if (scope === 'all') {
640
- for (const key in students) {
641
- const [cls] = key.split('-');
642
- classes.add(cls);
643
- }
644
  } else {
645
  const formVal = document.getElementById('class-form-select').value;
646
  if (!formVal) return;
@@ -655,11 +616,8 @@ MAJU,3,Emily Davis`;
655
  opt.textContent = cls;
656
  select.appendChild(opt);
657
  });
658
- if (classes.size === 0) {
659
- select.innerHTML = '<option value="">No classes found</option>';
660
- }
661
  }
662
-
663
  document.getElementById('class-action').addEventListener('change', updateClassFormVisibility);
664
  document.getElementById('class-scope').addEventListener('change', updateClassFormVisibility);
665
  document.getElementById('class-form-select').addEventListener('change', () => {
@@ -667,18 +625,11 @@ MAJU,3,Emily Davis`;
667
  populateRemoveClassDropdown('specific');
668
  }
669
  });
670
-
671
  document.getElementById('apply-class-btn').addEventListener('click', async () => {
672
  const action = document.getElementById('class-action').value;
673
  const scope = document.getElementById('class-scope').value;
674
- let affectedForms = [];
675
- if (scope === 'all') {
676
- affectedForms = forms;
677
- } else {
678
- const formVal = document.getElementById('class-form-select').value;
679
- if (!formVal) return alert('Please select a form');
680
- affectedForms = [formVal];
681
- }
682
  let msg = '';
683
  if (action === 'add') {
684
  const newClass = document.getElementById('new-class-name').value.trim().toUpperCase();
@@ -691,11 +642,8 @@ MAJU,3,Emily Davis`;
691
  addedCount++;
692
  }
693
  }
694
- if (addedCount === 0) {
695
- alert('This class already exists in all selected forms');
696
- return;
697
- }
698
- msg = `Class ${newClass} added to ${addedCount === forms.length ? 'all forms' : affectedForms.length + ' form(s)'}`;
699
  } else if (action === 'remove') {
700
  const classToRemove = document.getElementById('remove-class-name').value;
701
  if (!classToRemove) return alert('Please select a class');
@@ -703,19 +651,12 @@ MAJU,3,Emily Davis`;
703
  for (const form of affectedForms) {
704
  const key = `${classToRemove}-${form}`;
705
  if (students[key]) {
706
- if (students[key].length > 0) {
707
- if (!confirm(`Remove class ${classToRemove} from Form ${form}? This will delete ${students[key].length} students.`)) {
708
- continue;
709
- }
710
- }
711
  delete students[key];
712
  removedCount++;
713
  }
714
  }
715
- if (removedCount === 0) {
716
- alert('No matching classes found to remove');
717
- return;
718
- }
719
  msg = `Class ${classToRemove} removed from ${removedCount} form(s)`;
720
  }
721
  const saved = await saveStudentListToGitHub();
 
20
  </div>
21
  </div>
22
  </header>
 
23
  <!-- Management Tabs -->
24
  <div class="bg-white rounded-lg shadow-md p-6 mb-8">
25
  <div class="flex border-b border-gray-200 mb-6 overflow-x-auto">
 
36
  <i class="fas fa-file-import mr-2"></i> Import/Export
37
  </button>
38
  </div>
 
39
  <!-- Add Student Form -->
40
  <div id="add-form" class="space-y-4">
41
  <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
 
74
  </div>
75
  </div>
76
  </div>
 
77
  <!-- Remove Student Form -->
78
  <div id="remove-form" class="space-y-4 hidden">
79
  <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
 
115
  </div>
116
  </div>
117
  </div>
118
+ <!-- Manage Classes Form -->
 
119
  <div id="class-form" class="space-y-6 hidden">
120
  <div class="bg-blue-50 border border-blue-200 text-blue-700 p-4 rounded-lg">
121
  <div class="flex items-center">
 
126
  </div>
127
  </div>
128
  </div>
 
129
  <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
130
  <div>
131
  <label for="class-action" class="block text-gray-700 font-medium mb-2">Action</label>
 
153
  </select>
154
  </div>
155
  </div>
 
 
156
  <div id="add-class-container" class="hidden">
157
  <label for="new-class-name" class="block text-gray-700 font-medium mb-2">New Class Name</label>
158
  <input type="text" id="new-class-name" class="w-full px-4 py-2 border border-gray-300 rounded-lg" placeholder="e.g. CERIA">
159
  </div>
 
 
160
  <div id="remove-class-container" class="hidden">
161
  <label for="remove-class-name" class="block text-gray-700 font-medium mb-2">Class to Remove</label>
162
  <select id="remove-class-name" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
 
164
  </select>
165
  <p class="text-sm text-red-600 mt-2">Warning: This will permanently delete the class and all its students from the selected form(s).</p>
166
  </div>
 
167
  <button id="apply-class-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded-lg transition duration-200">
168
  <i class="fas fa-save mr-2"></i> Apply Changes
169
  </button>
 
170
  <div id="class-success" class="hidden bg-green-50 border border-green-200 text-green-700 p-4 rounded-lg">
171
  <div class="flex items-center">
172
  <i class="fas fa-check-circle text-green-500 text-xl mr-3"></i>
 
177
  </div>
178
  </div>
179
  </div>
 
180
  <!-- Import/Export Form -->
181
  <div id="import-export-form" class="space-y-4 hidden">
182
  <div class="bg-blue-50 border border-blue-200 text-blue-700 p-4 rounded-lg mb-4">
 
192
  <!-- Export Section -->
193
  <div class="bg-white border border-gray-200 rounded-lg p-4">
194
  <h3 class="text-lg font-medium mb-4 text-gray-800">Export Data</h3>
195
+ <p class="text-gray-600 mb-4">Download the latest student list from GitHub in CSV format.</p>
196
  <button id="export-btn" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200">
197
  <i class="fas fa-file-export mr-2"></i> Export to CSV
198
  </button>
 
201
  <i class="fas fa-file-download mr-2"></i> Sample CSV
202
  </button>
203
  </div>
204
+ <div id="export-success" class="hidden bg-green-50 border border-green-200 text-green-700 p-4 rounded-lg mt-4">
205
+ <div class="flex items-center justify-between">
206
+ <div class="flex items-center">
207
+ <i class="fas fa-check-circle text-green-500 text-xl mr-3"></i>
208
+ <div>
209
+ <h4 class="font-bold">Export completed successfully!</h4>
210
+ <p class="text-sm">Click the link below to download your file.</p>
211
+ </div>
212
+ </div>
213
+ </div>
214
+ <a id="download-link" class="mt-3 block w-full text-center bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200">
215
+ <i class="fas fa-download mr-2"></i> Download CSV File
216
+ </a>
217
  </div>
218
  </div>
219
  <!-- Import Section -->
 
247
  </div>
248
  </div>
249
  </div>
 
 
250
  <script>
251
+ // Hardcoded configuration (no config.js needed)
252
+ const GITHUB_TOKEN = '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';
253
+ const REPO = 'SMSS-Waste-Less/Waste-LessV5';
254
+ const FILE_PATH = 'student-list.json';
255
  const CSV_TEMPLATE = `class,form,studentName
256
  BUDI,1,John Doe
257
  BUDI,1,Jane Smith
 
259
  USAHA,2,Sarah Williams
260
  MAJU,3,David Brown
261
  MAJU,3,Emily Davis`;
 
262
  let students = {};
263
  let availableClasses = [];
264
+ let currentCsvBlobUrl = null; // To store the blob URL for download link
265
  async function loadStudentList() {
266
  try {
267
+ const response = await fetch(`https://api.github.com/repos/${REPO}/contents/${FILE_PATH}`, {
268
  headers: {
269
+ 'Authorization': `token ${GITHUB_TOKEN}`,
270
  'Accept': 'application/vnd.github.v3+json'
271
  }
272
  });
 
281
  throw new Error('API error: ' + response.status);
282
  }
283
  } catch (error) {
284
+ console.error('Failed to load:', error);
285
+ alert('Could not load student list. Check internet or create student-list.json in repo.');
286
  students = {};
287
  availableClasses = [];
288
  }
289
  updateClassDropdowns();
290
  loadStudentsForRemoval();
291
  }
 
292
  function updateClassDropdowns() {
293
  const addSelect = document.getElementById('add-class');
294
  const removeSelect = document.getElementById('remove-class');
 
295
  addSelect.innerHTML = '<option value="">Select Class</option>';
296
  removeSelect.innerHTML = '<option value="">Select Class</option>';
 
297
  availableClasses.forEach(cls => {
298
  const optAdd = document.createElement('option');
299
  optAdd.value = cls;
300
  optAdd.textContent = cls;
301
  addSelect.appendChild(optAdd);
 
302
  const optRemove = document.createElement('option');
303
  optRemove.value = cls;
304
  optRemove.textContent = cls;
305
  removeSelect.appendChild(optRemove);
306
  });
 
307
  if (availableClasses.length === 0) {
308
  addSelect.innerHTML += '<option disabled>No classes available yet</option>';
309
  removeSelect.innerHTML += '<option disabled>No classes available yet</option>';
310
  }
311
  }
 
312
  async function saveStudentListToGitHub() {
313
  try {
314
  let sha = null;
315
+ const getResponse = await fetch(`https://api.github.com/repos/${REPO}/contents/${FILE_PATH}`, {
316
  headers: {
317
+ 'Authorization': `token ${GITHUB_TOKEN}`,
318
  'Accept': 'application/vnd.github.v3+json'
319
  }
320
  });
 
322
  const fileData = await getResponse.json();
323
  sha = fileData.sha;
324
  }
 
325
  const newContent = JSON.stringify(students, null, 2);
326
  const base64Content = btoa(newContent);
327
+ const putResponse = await fetch(`https://api.github.com/repos/${REPO}/contents/${FILE_PATH}`, {
 
328
  method: 'PUT',
329
  headers: {
330
+ 'Authorization': `token ${GITHUB_TOKEN}`,
331
  'Content-Type': 'application/json',
332
  'Accept': 'application/vnd.github.v3+json'
333
  },
 
338
  branch: 'main'
339
  })
340
  });
 
341
  if (!putResponse.ok) {
342
  const err = await putResponse.json();
343
  throw new Error(err.message || 'Save failed');
 
345
  return true;
346
  } catch (error) {
347
  console.error('Save error:', error);
348
+ alert('Failed to save: ' + error.message);
349
  return false;
350
  }
351
  }
352
+ // Export: fetch latest from GitHub, generate CSV, and show downloadable link
 
353
  document.getElementById('export-btn').addEventListener('click', async function() {
354
  this.disabled = true;
355
  this.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i> Exporting...';
356
+ await loadStudentList(); // Fetch the latest data
 
 
357
  const csv = generateCSV();
358
+ const filename = `student-list-${new Date().toISOString().split('T')[0]}.csv`;
359
+ // Revoke previous blob URL if exists
360
+ if (currentCsvBlobUrl) {
361
+ URL.revokeObjectURL(currentCsvBlobUrl);
362
+ }
363
+ const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
364
+ currentCsvBlobUrl = URL.createObjectURL(blob);
365
+ const downloadLink = document.getElementById('download-link');
366
+ downloadLink.href = currentCsvBlobUrl;
367
+ downloadLink.download = filename;
368
  document.getElementById('export-success').classList.remove('hidden');
 
 
369
  this.disabled = false;
370
  this.innerHTML = '<i class="fas fa-file-export mr-2"></i> Export to CSV';
371
  });
 
372
  function generateCSV() {
373
  let csv = 'class,form,studentName\n';
374
  for (const key in students) {
 
379
  }
380
  return csv;
381
  }
382
+ document.getElementById('export-sample-btn').addEventListener('click', function() {
383
+ const blob = new Blob([CSV_TEMPLATE], { type: 'text/csv;charset=utf-8;' });
 
384
  const url = URL.createObjectURL(blob);
385
  const link = document.createElement('a');
386
  link.href = url;
387
+ link.download = 'student-list-sample.csv';
388
  document.body.appendChild(link);
389
  link.click();
390
  document.body.removeChild(link);
391
  URL.revokeObjectURL(url);
 
 
 
 
392
  });
393
+ // Import CSV
 
394
  function parseCSV(content) {
395
  const lines = content.split('\n').map(l => l.trim()).filter(l => l);
396
  if (lines.length < 1) throw new Error('Empty CSV');
 
398
  if (!headers.includes('class') || !headers.includes('form') || !headers.includes('studentname')) {
399
  throw new Error('Missing required columns: class, form, studentName');
400
  }
 
401
  const newStudents = {};
402
  for (let i = 1; i < lines.length; i++) {
403
  const row = lines[i];
 
413
  }
414
  return newStudents;
415
  }
 
416
  document.getElementById('import-btn').addEventListener('click', async function() {
417
  const fileInput = document.getElementById('csv-file');
418
  const file = fileInput.files[0];
 
445
  };
446
  reader.readAsText(file);
447
  });
 
448
  document.getElementById('csv-file').addEventListener('change', function(e) {
449
  const fileName = e.target.files[0]?.name || 'No file selected';
450
  document.getElementById('file-name').textContent = fileName;
451
  });
 
452
  window.addEventListener('load', loadStudentList);
453
+ // Tab switching
 
454
  document.getElementById('add-tab').addEventListener('click', function() {
455
  document.getElementById('add-form').classList.remove('hidden');
456
  document.getElementById('remove-form').classList.add('hidden');
 
465
  document.getElementById('import-export-tab').classList.add('text-gray-500');
466
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
467
  });
 
468
  document.getElementById('remove-tab').addEventListener('click', function() {
469
  document.getElementById('remove-form').classList.remove('hidden');
470
  document.getElementById('add-form').classList.add('hidden');
 
479
  document.getElementById('import-export-tab').classList.add('text-gray-500');
480
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
481
  });
 
482
  document.getElementById('class-tab').addEventListener('click', function() {
483
  document.getElementById('class-form').classList.remove('hidden');
484
  document.getElementById('add-form').classList.add('hidden');
 
494
  document.getElementById('import-export-tab').classList.remove('text-blue-600', 'border-blue-600');
495
  updateClassFormVisibility();
496
  });
 
497
  document.getElementById('import-export-tab').addEventListener('click', function() {
498
  document.getElementById('import-export-form').classList.remove('hidden');
499
  document.getElementById('add-form').classList.add('hidden');
 
508
  document.getElementById('class-tab').classList.add('text-gray-500');
509
  document.getElementById('class-tab').classList.remove('text-blue-600', 'border-blue-600');
510
  });
 
 
511
  document.getElementById('remove-class').addEventListener('change', loadStudentsForRemoval);
512
  document.getElementById('remove-form-select').addEventListener('change', loadStudentsForRemoval);
 
513
  function loadStudentsForRemoval() {
514
  const classVal = document.getElementById('remove-class').value;
515
  const formVal = document.getElementById('remove-form-select').value;
 
532
  }
533
  }
534
  }
 
 
535
  document.getElementById('add-student-btn').addEventListener('click', async function() {
536
  const className = document.getElementById('add-class').value;
537
  const formVal = document.getElementById('add-form-select').value;
 
540
  alert('Please enter student name');
541
  return;
542
  }
543
+ if (!className) {
544
+ alert('Please select a class');
545
+ return;
546
  }
547
+ const key = `${className}-${formVal}`;
548
+ if (!students[key]) students[key] = [];
549
  if (students[key].includes(studentName)) {
550
+ alert('This student already exists');
551
  return;
552
  }
553
  students[key].push(studentName);
 
555
  if (saved) {
556
  document.getElementById('student-name').value = '';
557
  document.getElementById('add-success').classList.remove('hidden');
558
+ setTimeout(() => document.getElementById('add-success').classList.add('hidden'), 3000);
 
 
559
  availableClasses = [...new Set(Object.keys(students).map(k => k.split('-')[0]))].sort();
560
  updateClassDropdowns();
561
  }
562
  });
 
 
563
  document.getElementById('remove-student-btn').addEventListener('click', async function() {
564
  const className = document.getElementById('remove-class').value;
565
  const formVal = document.getElementById('remove-form-select').value;
566
  const studentName = document.getElementById('student-select').value;
567
  if (!studentName) {
568
+ alert('Please select a student');
569
  return;
570
  }
571
  const key = `${className}-${formVal}`;
572
  if (students[key]) {
573
+ students[key] = students[key].filter(s => s !== studentName);
574
+ if (students[key].length === 0) delete students[key];
 
 
575
  }
576
  const saved = await saveStudentListToGitHub();
577
  if (saved) {
578
  loadStudentsForRemoval();
579
  document.getElementById('remove-success').classList.remove('hidden');
580
+ setTimeout(() => document.getElementById('remove-success').classList.add('hidden'), 3000);
 
 
581
  availableClasses = [...new Set(Object.keys(students).map(k => k.split('-')[0]))].sort();
582
  updateClassDropdowns();
583
  }
584
  });
 
 
585
  const forms = ['1', '2', '3', '4', '5'];
 
586
  function updateClassFormVisibility() {
587
  const action = document.getElementById('class-action').value;
588
  const scope = document.getElementById('class-scope').value;
 
596
  populateRemoveClassDropdown(scope);
597
  }
598
  }
 
599
  function populateRemoveClassDropdown(scope) {
600
  const select = document.getElementById('remove-class-name');
601
  select.innerHTML = '<option value="">Select Class</option>';
602
  const classes = new Set();
603
  if (scope === 'all') {
604
+ for (const key in students) classes.add(key.split('-')[0]);
 
 
 
605
  } else {
606
  const formVal = document.getElementById('class-form-select').value;
607
  if (!formVal) return;
 
616
  opt.textContent = cls;
617
  select.appendChild(opt);
618
  });
619
+ if (classes.size === 0) select.innerHTML = '<option value="">No classes found</option>';
 
 
620
  }
 
621
  document.getElementById('class-action').addEventListener('change', updateClassFormVisibility);
622
  document.getElementById('class-scope').addEventListener('change', updateClassFormVisibility);
623
  document.getElementById('class-form-select').addEventListener('change', () => {
 
625
  populateRemoveClassDropdown('specific');
626
  }
627
  });
 
628
  document.getElementById('apply-class-btn').addEventListener('click', async () => {
629
  const action = document.getElementById('class-action').value;
630
  const scope = document.getElementById('class-scope').value;
631
+ let affectedForms = scope === 'all' ? forms : [document.getElementById('class-form-select').value];
632
+ if (scope === 'specific' && !affectedForms[0]) return alert('Please select a form');
 
 
 
 
 
 
633
  let msg = '';
634
  if (action === 'add') {
635
  const newClass = document.getElementById('new-class-name').value.trim().toUpperCase();
 
642
  addedCount++;
643
  }
644
  }
645
+ if (addedCount === 0) return alert('Class already exists');
646
+ msg = `Class ${newClass} added to ${addedCount} form(s)`;
 
 
 
647
  } else if (action === 'remove') {
648
  const classToRemove = document.getElementById('remove-class-name').value;
649
  if (!classToRemove) return alert('Please select a class');
 
651
  for (const form of affectedForms) {
652
  const key = `${classToRemove}-${form}`;
653
  if (students[key]) {
654
+ if (students[key].length > 0 && !confirm(`Delete ${students[key].length} students in ${classToRemove} Form ${form}?`)) continue;
 
 
 
 
655
  delete students[key];
656
  removedCount++;
657
  }
658
  }
659
+ if (removedCount === 0) return alert('No classes removed');
 
 
 
660
  msg = `Class ${classToRemove} removed from ${removedCount} form(s)`;
661
  }
662
  const saved = await saveStudentListToGitHub();