-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
673 lines (628 loc) · 23.9 KB
/
Copy pathindex.html
File metadata and controls
673 lines (628 loc) · 23.9 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Section View</title>
<!-- SheetJS for reading Excel/CSV files -->
<script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script>
<!-- html2canvas and jsPDF for PDF export -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<style>
/* Reset */
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: #f5f5f5;
color: #333;
}
/* Container and layout */
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
padding: 20px;
}
header {
grid-column: 1 / -1;
background: #007bff;
color: white;
padding: 15px;
text-align: center;
}
.panel {
background: white;
padding: 15px;
border-radius: 6px;
box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}
/* Controls */
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 15px;
}
.controls input,
.controls select {
flex: 1;
min-width: 150px;
}
input, select, .btn {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
.btn {
cursor: pointer;
font-weight: bold;
background: #007bff;
color: white;
border: none;
}
.btn:disabled {
background: #ccc;
cursor: not-allowed;
}
/* Table styling */
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
cursor: default;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
th {
background: #f0f0f0;
user-select: none;
}
th.sortable:hover {
background: #e0e0e0;
cursor: pointer;
}
/* Routine styling */
#routine table th {
background: #007bff;
color: white;
}
/* Course List styling */
.section-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #eee;
}
.details {
font-size: 0.85em;
color: #555;
}
/* Notification */
.notification {
position: fixed;
top: 20px;
right: 20px;
padding: 10px 15px;
border-radius: 4px;
color: white;
display: none;
box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.notification.success {
background: #28a745;
}
.notification.error {
background: #dc3545;
}
.input-error {
border: 2px solid #dc3545;
}
/* Total count alignment */
#totalCount {
margin-top: 10px;
font-weight: bold;
text-align: right;
}
/* Responsive design */
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
.controls {
flex-direction: column;
}
table, th, td {
font-size: 0.9em;
}
header {
padding: 10px;
}
}
</style>
</head>
<body>
<header>
<h1>Pickora</h1>
</header>
<div class="notification" id="notification"></div>
<div class="container">
<section class="panel" id="controlsPanel">
<h2>Controls</h2>
<div class="controls">
<button id="dropZone" class="btn">Upload Excel/CSV</button>
<input type="file" id="fileInput" accept=".xlsx,.xls,.csv" style="display:none;" />
<input type="text" id="searchInput" placeholder="Search courses..." disabled />
<select id="deptFilter" disabled>
<option value="">All Departments</option>
</select>
<!-- New dropdown for class type filtering -->
<select id="classTypeFilter" disabled>
<option value="">All Class Types</option>
<option value="theory">Theory</option>
<option value="lab">Lab</option>
</select>
<button id="clearAll" class="btn" disabled>Clear All</button>
</div>
<h3>Available Courses</h3>
<div id="courseList" style="max-height: 400px; overflow-y: auto;"></div>
<div id="noCourses" style="display:none;">No courses found.</div>
</section>
<section class="panel">
<h2>Selected Courses</h2>
<table id="selectedTable">
<thead>
<tr>
<th class="sortable" data-column="code">Code</th>
<th>Course</th>
<th>Section</th>
<th>Days</th>
<th>Times</th>
<th>Room(s)</th>
<th>Student Number</th>
<th>Seats Free</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div id="totalCount">Total Students: 0</div>
<div style="margin-top:10px; text-align:right;">
<button id="generateRoutine" class="btn" disabled>Generate Routine</button>
<button id="downloadPdf" class="btn" disabled>Download PDF</button>
</div>
</section>
<section class="panel" style="grid-column: 1 / -1;">
<h2>Generated Routine</h2>
<div id="routine" style="overflow-x:auto;"></div>
</section>
</div>
<script>
// Constants and state variables
const MAX_CAPACITY = 37;
let courses = {};
let groupedCourses = {};
let selectedCourses = [];
let sortAscending = true;
// Helper: Normalize day names (convert abbreviated to full names)
function normalizeDay(dayStr) {
const mapping = {
"Mon": "Monday",
"Tue": "Tuesday",
"Wed": "Wednesday",
"Thu": "Thursday",
"Fri": "Friday",
"Sat": "Saturday",
"Sun": "Sunday"
};
const key = dayStr.trim();
return mapping[key] || key;
}
// Load any persisted data from localStorage
function loadDataFromLocalStorage() {
const storedCourses = localStorage.getItem('courses');
const storedGrouped = localStorage.getItem('groupedCourses');
if (storedCourses && storedGrouped) {
courses = JSON.parse(storedCourses);
groupedCourses = JSON.parse(storedGrouped);
// Enable controls for filtering and searching
document.getElementById('searchInput').disabled = false;
document.getElementById('deptFilter').disabled = false;
document.getElementById('classTypeFilter').disabled = false;
document.getElementById('clearAll').disabled = false;
// Rebuild the department filter options from available courses
const depts = new Set();
Object.values(courses).forEach(c => {
if(c.dept) depts.add(c.dept);
});
document.getElementById('deptFilter').innerHTML = ['<option value="">All Departments</option>']
.concat(Array.from(depts).map(d => `<option>${d}</option>`)).join('');
renderCourseList();
}
}
// Debounce helper: delays execution until a specified time has passed
function debounce(func, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
// Initialization function to wire-up all event listeners and load persisted data
function init() {
document.getElementById('dropZone').addEventListener('click', () => {
document.getElementById('fileInput').click();
});
document.getElementById('fileInput').addEventListener('change', handleFile);
// Use the debounced version of renderCourseList for the search input.
document.getElementById('searchInput').addEventListener('input', debounce(renderCourseList, 300));
document.getElementById('deptFilter').addEventListener('change', renderCourseList);
document.getElementById('classTypeFilter').addEventListener('change', renderCourseList);
document.getElementById('clearAll').addEventListener('click', clearAll);
document.getElementById('generateRoutine').addEventListener('click', generateRoutine);
document.getElementById('downloadPdf').addEventListener('click', downloadPDF);
document.querySelector('th.sortable').addEventListener('click', () => {
sortAscending = !sortAscending;
sortSelectedCourses('code', sortAscending);
renderSelectedTable();
});
// Load persisted data if it exists
loadDataFromLocalStorage();
}
// Notification helper
function notify(msg, type = 'success') {
const el = document.getElementById('notification');
el.textContent = msg;
el.className = `notification ${type}`;
el.style.display = 'block';
setTimeout(() => el.style.display = 'none', 3000);
}
// Clear all courses: clear persistent storage and reload the page
function clearAll() {
if (confirm("Are you sure you want to clear all?")) {
localStorage.removeItem('courses');
localStorage.removeItem('groupedCourses');
location.reload();
}
}
// Handle file input change event
function handleFile(e) {
resetState();
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = evt => processData(evt.target.result, file.name);
reader.onerror = () => notify('Failed to read file', 'error');
if (/\.csv$/i.test(file.name)) {
reader.readAsText(file);
} else {
reader.readAsArrayBuffer(file);
}
}
// Reset state when a new file is loaded
function resetState() {
courses = {};
groupedCourses = {};
selectedCourses = [];
document.querySelector('#selectedTable tbody').innerHTML = '';
document.getElementById('routine').innerHTML = '';
document.getElementById('generateRoutine').disabled = true;
document.getElementById('downloadPdf').disabled = true;
document.getElementById('clearAll').disabled = false;
document.getElementById('searchInput').disabled = true;
document.getElementById('deptFilter').disabled = true;
document.getElementById('classTypeFilter').disabled = true;
document.getElementById('courseList').innerHTML = '';
document.getElementById('noCourses').style.display = 'none';
}
// Process CSV or Excel data and build the courses structure.
// When multiple rows occur for the same course-section key, different room numbers are concatenated.
function processData(data, fileName) {
let sheetData = [];
if (/\.csv$/i.test(fileName)) {
sheetData = data.split(/\r?\n/).map(line => line.split(','));
} else {
const arr = new Uint8Array(data);
const wb = XLSX.read(arr, { type: 'array' });
sheetData = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { header: 1 });
}
const [hdr, ...rows] = sheetData;
const idx = hdr.reduce((acc, h, i) => {
if (h) acc[h.trim()] = i;
return acc;
}, {});
const depts = new Set();
rows.forEach(r => {
const status = r[idx['Status']];
const title = r[idx['Course Title']];
const count = +r[idx['Count']] || 0;
const free = Math.max(0, MAX_CAPACITY - count);
// Skip row if missing title, no free seats or if class is for freshmen.
if (!title || free === 0 || /freshman/i.test(title)) return;
const key = title + '|' + r[idx['Section']];
// Normalize day and add room as meeting property
const dayNormalized = normalizeDay(r[idx['Day']]);
if (!courses[key]) {
courses[key] = {
code: r[idx['Course Code']],
title,
dept: r[idx['Department']],
faculty: r[idx['Faculty']],
type: r[idx['Type']], // theory or lab
section: r[idx['Section']],
free,
status: status, // pick status from uploaded file
meetings: [] // meetings will contain day, start, end, and room
};
depts.add(courses[key].dept);
}
// Create a meeting with day, time and room details.
const meeting = {
day: dayNormalized,
start: r[idx['Start Time']],
end: r[idx['End Time']],
room: r[idx['Room']]
};
// Add meeting if not already present.
if (!courses[key].meetings.some(m => m.day === meeting.day && m.start === meeting.start && m.end === meeting.end)) {
courses[key].meetings.push(meeting);
}
});
// Group courses by title
Object.values(courses).forEach(info => {
groupedCourses[info.title] = groupedCourses[info.title] || [];
groupedCourses[info.title].push(info);
});
// Persist the data in localStorage
localStorage.setItem('courses', JSON.stringify(courses));
localStorage.setItem('groupedCourses', JSON.stringify(groupedCourses));
// Initialize dropdown filters and enable controls
document.getElementById('deptFilter').innerHTML = ['<option value="">All Departments</option>']
.concat(Array.from(depts).map(d => `<option>${d}</option>`)).join('');
document.getElementById('searchInput').disabled = false;
document.getElementById('deptFilter').disabled = false;
document.getElementById('classTypeFilter').disabled = false;
renderCourseList();
}
// Render course list based on search term, department, and class type filters.
function renderCourseList() {
const query = document.getElementById('searchInput').value.trim().toLowerCase();
const dept = document.getElementById('deptFilter').value;
const classType = document.getElementById('classTypeFilter').value;
const listEl = document.getElementById('courseList');
listEl.innerHTML = '';
const filteredTitles = Object.keys(groupedCourses).filter(title => {
// Filter on title search and department match.
const titleMatch = !query || title.toLowerCase().includes(query);
const deptMatch = !dept || groupedCourses[title][0].dept === dept;
// Filter based on class type if selected.
const typeMatch = !classType || groupedCourses[title][0].type.toLowerCase() === classType;
return titleMatch && deptMatch && typeMatch;
});
if (!filteredTitles.length) {
document.getElementById('noCourses').style.display = 'block';
return;
}
document.getElementById('noCourses').style.display = 'none';
filteredTitles.sort().forEach(title => {
groupedCourses[title].forEach((info, index) => {
const btnId = `btn-${title.replace(/\W/g, '')}-${info.section}`;
// Disable button if course already selected or if its status is Reserved.
const alreadySelected = selectedCourses.some(course => course && course.title === title && course.section === info.section);
const disabled = alreadySelected || info.status.toLowerCase() === "reserved" ? 'disabled' : '';
const days = info.meetings.map(m => m.day).join(', ');
const times = info.meetings.map(m => `${m.start}-${m.end}`).join('; ');
// Compute unique room list from meetings
const rooms = [...new Set(info.meetings.map(m => m.room))].join(', ');
const div = document.createElement('div');
div.className = 'section-item';
div.innerHTML = `
<div>
<strong>${info.code} - ${title} (${info.section})</strong>
<div class="details">
${days} ${times}<br>
Room: ${rooms}<br>
Dept: ${info.dept}<br>
Free Seats: ${info.free}<br>
Type: ${info.type}<br>
Status: ${info.status}
</div>
</div>
<button id="${btnId}" class="btn btn-secondary" data-title="${title}" onclick="addCourse('${title}', ${index})" ${disabled}>Add</button>
`;
listEl.appendChild(div);
});
});
}
// Add course to selected list.
function addCourse(title, idx) {
const info = groupedCourses[title][idx];
// Check using both title and section so that different sections can be added.
if (selectedCourses.some(course => course && course.title === title && course.section === info.section)) {
return notify('Course already selected', 'error');
}
selectedCourses.push(info);
renderSelectedTable();
document.getElementById('generateRoutine').disabled = false;
notify('Course added');
}
// Remove course from selected list.
function removeCourse(id) {
const removedCourse = selectedCourses[id];
selectedCourses[id] = null;
document.getElementById(`sel-${id}`).remove();
if (!selectedCourses.some(course => course && course.title === removedCourse.title && course.section === removedCourse.section)) {
Array.from(document.querySelectorAll(`button[data-title='${removedCourse.title}']`)).forEach(button => {
button.disabled = false;
});
}
if (!selectedCourses.some(Boolean)) {
document.getElementById('generateRoutine').disabled = true;
}
updateTotalCount();
}
// Validate student number input.
function validateInput(input, maxVal) {
const value = parseInt(input.value, 10);
if (!value || value < 1 || value > maxVal) {
input.classList.add('input-error');
notify(`Student number must be between 1 and ${maxVal}`, 'error');
} else {
input.classList.remove('input-error');
}
}
// Update total student count.
function updateTotalCount() {
let total = 0;
selectedCourses.forEach((course, i) => {
if (!course) return;
const numInput = document.getElementById(`num-${i}`);
total += numInput && numInput.value ? +numInput.value : 0;
});
document.getElementById('totalCount').textContent = "Total Students: " + total;
}
// Sort selected courses.
function sortSelectedCourses(prop, ascending = true) {
selectedCourses = selectedCourses.filter(Boolean);
selectedCourses.sort((a, b) => {
if (a[prop] < b[prop]) return ascending ? -1 : 1;
if (a[prop] > b[prop]) return ascending ? 1 : -1;
return 0;
});
}
// Convert time string to minutes.
function parseTime(str) {
const parts = str.split(' ');
const timeParts = parts[0].split(':').map(Number);
let [hours, minutes] = timeParts;
if (parts[1]) {
const period = parts[1].toUpperCase();
if (period === 'PM' && hours < 12) hours += 12;
if (period === 'AM' && hours === 12) hours = 0;
}
return hours * 60 + minutes;
}
// Generate routine by checking schedule conflicts.
function generateRoutine() {
const rows = [];
const sched = {};
selectedCourses.forEach((course, i) => {
if (!course) return;
course.meetings.forEach(m => {
const start = parseTime(m.start);
const end = parseTime(m.end);
sched[m.day] = sched[m.day] || [];
const conflict = sched[m.day].some(([ss, ee]) => start < ee && ss < end);
sched[m.day].push([start, end]);
const numInput = document.getElementById(`num-${i}`);
const num = numInput && numInput.value ? +numInput.value : 0;
rows.push({
day: m.day,
time: `${m.start}-${m.end}`,
code: course.code,
sec: course.section,
title: course.title,
room: m.room,
assigned: conflict ? '' : (num > course.free ? 'Over cap' : num),
status: conflict ? 'Time conflict' : ''
});
});
});
renderRoutine(rows);
document.getElementById('downloadPdf').disabled = false;
}
// Render the routine table with days sorted as Sunday, Monday, ...
// and time slots sorted from earliest AM to latest PM.
function renderRoutine(rows) {
const container = document.getElementById('routine');
container.innerHTML = '';
if (!rows.length) {
container.textContent = 'No courses scheduled.';
return;
}
// Define order for days
const dayOrder = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const daysSet = new Set(rows.map(r => r.day));
const days = dayOrder.filter(day => daysSet.has(day));
// Sort slots according to their start time
const slotsSet = new Set(rows.map(r => r.time));
let slots = Array.from(slotsSet);
slots.sort((a, b) => {
const aStart = parseTime(a.split('-')[0]);
const bStart = parseTime(b.split('-')[0]);
return aStart - bStart;
});
const tbl = document.createElement('table');
let html = `<thead><tr><th>Day</th>${slots.map(s => `<th>${s}</th>`).join('')}</tr></thead><tbody>`;
days.forEach(day => {
html += `<tr><td>${day}</td>`;
slots.forEach(slot => {
const cellRows = rows.filter(r => r.day === day && r.time === slot);
html += `<td>${cellRows.map(r =>
`${r.code} (${r.sec})<br>${r.title}<br>${r.room}<br>Assigned: ${r.assigned}` +
(r.status ? `<br><em>${r.status}</em>` : '')
).join('<hr>')}</td>`;
});
html += '</tr>';
});
html += '</tbody>';
tbl.innerHTML = html;
container.appendChild(tbl);
}
// Download routine as a PDF.
function downloadPDF() {
html2canvas(document.getElementById('routine')).then(canvas => {
const img = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'landscape' });
const width = pdf.internal.pageSize.getWidth();
const height = (canvas.height * width) / canvas.width;
pdf.addImage(img, 'PNG', 0, 0, width, height);
pdf.save(`routine_${new Date().toISOString().slice(0,10)}.pdf`);
});
}
// Render selected courses table.
function renderSelectedTable() {
const tbody = document.querySelector('#selectedTable tbody');
tbody.innerHTML = '';
selectedCourses.forEach((course, i) => {
if (!course) return;
const days = course.meetings.map(m => m.day).join(', ');
const times = course.meetings.map(m => `${m.start}-${m.end}`).join('; ');
// Compute unique room list from meetings
const rooms = [...new Set(course.meetings.map(m => m.room))].join(', ');
const row = document.createElement('tr');
row.id = `sel-${i}`;
row.innerHTML = `
<td>${course.code}</td>
<td>${course.title}</td>
<td>${course.section}</td>
<td>${days}</td>
<td>${times}</td>
<td>${rooms}</td>
<td>
<input type="number" id="num-${i}" min="1" max="${course.free}" placeholder="Fill student number"
onblur="validateInput(this, ${course.free}); updateTotalCount();" onchange="updateTotalCount();">
</td>
<td>${course.free}</td>
<td><button class="btn btn-secondary" onclick="removeCourse(${i})">Remove</button></td>
`;
tbody.appendChild(row);
});
updateTotalCount();
}
// Initialize the application
init();
</script>
</body>
</html>