custom-scripts/mccs-it/CSVFilterer.js
2024-08-13 16:28:07 -04:00

140 lines
4.4 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: false,
users: false
}
function filterEnrollments(document) {
let fileInput = document.getElementById('userEnrollmentsCsv');
let filterValue = "KV Instructor";
document.getElementById('downloadBtn')
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);
console.log(JSON.stringify(enrollments));
},
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);
console.log(users);
},
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}`);
});
const postCSV = preCSV.join('\n');
generateCSV(postCSV, document);
}
const generateCSV = (input, document) => {
const downloadLink = document.getElementById("downloadLink");
const blob = new Blob([input], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
console.log(url);
downloadLink.style = "display: inline;";
downloadLink.href = url;
downloadLink.download = 'Instructors.CSV';
// downloadBtn.onclick = function() {
// const a = document.createElement('a');
// a.href = url;
// a.download = 'data.csv';
// document.body.appendChild(a);
// };
}
function refreshStatus(document) {
let statusString = "";
let statusSpan = document.getElementById('statusIndicator')
states.enrollments ? statusString += "Enrollments: Filtered" : statusString += "Enrollments: Not Filtered";
states.users ? statusString += "<br />Users: Parsed" : statusString += "<br />Users: Not Parsed"
statusSpan.innerHTML = statusString;
if (states.enrollments && states.users) {
let refreshBtn = document.getElementById('refreshStatusBtn');
let downloadBtn = document.getElementById('downloadBtn');
refreshBtn.style = "display: none";
downloadBtn.style = "display: block";
}
}