custom-scripts/mccs-it/CSVFilterer.js
2024-08-13 09:48:52 -04:00

125 lines
3.8 KiB
JavaScript

class UserData {
constructor(dataRow) {
this.userId = dataRow[0];
this.uName = dataRow[1];
this.fName = dataRow[3];
this.sName = dataRow[5];
}
}
class EnrollmentData {
constructor(dataRow) {
this.orgUnitId = dataRow[0];
this.userId = dataRow[1];
this.roleName = dataRow[2];
this.timestamp = dataRow[3];
this.roleId = dataRow[5];
this.fName = "N/A";
this.sName = "N/A";
this.uName = "N/A";
}
}
// Create empty Maps to store the filtered enrollment data and user info in
// These will be cross checked later to generate the list of all instructors
const users = new Map();
const enrollments = new Map();
let states = {
enrollments: "Unfiltered",
users: "Unparsed"
}
function filterEnrollments(document) {
let fileInput = document.getElementById('ueFileInput');
let filterValue = "KV Instructor";
document.getElementById('downloadLink')
if (!fileInput.files.length) {
alert('Please select a CSV file.');
return;
}
const file = fileInput.files[0];
Papa.parse(file, {
complete: function(results) {
// Iterate over the entire data set
results.data.forEach(row => {
// Create an EnrollmentData object with it:
const enrollmentData = new EnrollmentData(row);
// If the row contains the filter string and is not in the 'seen' Set
if (row.some(cell => cell.includes(filterValue)) && !enrollments.has(enrollmentData.userId)) {
// Add it to the data array
enrollments.set(enrollmentData.userId, enrollmentData);
}
});
states.enrollments = "Filtered";
refreshStatus(document);
},
error: function(error) {
console.error(error);
alert('An error occurred while processing the file.');
}
});
}
const parseUsers = (usersFileInputs, document) => {
if (!usersFileInputs.files.length) {
alert('Please select a CSV file.');
return;
}
const usersFile = usersFileInputs.files[0];
Papa.parse(usersFile, {
complete: function(results) {
// Iterate over the entire data set
results.data.forEach(row => {
// Create an object with it:
const userData = new UserData(row);
// Add it to the data array
users.set(userData.userId, userData);
});
states.users = "Parsed";
refreshStatus(document);
generateList(document);
},
error: function(error) {
console.error(error);
alert('An error occurred while processing the Users.csv file.');
}
});
}
const generateList = (document) => {
const preCSV = [
"UserId,Timestamp,FirstName,LastName,Username"
];
enrollments.forEach(e => {
// Get the associated user
const user = users.get(e.userId);
preCSV.push(`${user.userId},${e.timestamp}.${user.fName},${user.sName},${user.uName}`);
});
generateCSV(preCSV, document);
}
const generateCSV = (input, document) => {
const downloadLink = document.getElementById("downloadLink");
const csv = Papa.unparse(input);
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = 'filtered.csv';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Filtered CSV';
}
function refreshStatus(document) {
let statusSpan = document.getElementById('status')
let statusString = `Enrollments: ${states.enrollments} | Users: ${states.users}`;
statusSpan.innerHTML = statusString;
}