Skip to content

Instantly share code, notes, and snippets.

@cmacrander
Created September 30, 2015 21:12
Show Gist options
  • Save cmacrander/01965fbbef0735ee52f8 to your computer and use it in GitHub Desktop.
Save cmacrander/01965fbbef0735ee52f8 to your computer and use it in GitHub Desktop.
Javascript console tricks for hacking the Platform
// var allUsers = [];
// var allActivities = [];
var activitiesByClassroom = {};
forEach(allActivities, function (act) {
if (act.activity_ordinal === "1") {
activitiesByClassroom[act.assc_classroom_list[0]] = act;
}
});
var injector = angular.element(document.body).injector();
var $pertsAjax = injector.get('$pertsAjax');
var $forEachAsync = injector.get('$forEachAsync');
var $getStore = injector.get('$getStore');
var numClasses = 0;
var classProgIndex = {}; // classId: int, sum of progress
var studentsByClass = {};
var sixtySixers = forEach(allUsers, function (user) {
var classId = user.assc_classroom_list[0];
var aggregationData = JSON.parse(user._aggregation_json);
var progress = aggregationData[1].progress;
// Sum student progress, grouped by classroom.
if (classProgIndex[classId] === undefined) {
classProgIndex[classId] = 0;
numClasses += 1;
}
classProgIndex[classId] += progress;
// Build an index of students by classroom.
if (studentsByClass[classId] === undefined) {
studentsByClass[classId] = [];
}
studentsByClass[classId].push(user);
// Build a list of sixtySixers who need a completion code.
if (aggregationData[1].progress === 66 &&
!user.s1_status_code) {
return user;
}
});
var sixtySixerSuccess = {};
var sixtySixerErrors = {};
var numErrors = 0;
var fixSixtySix = function () {
$forEachAsync(sixtySixers, function (user) {
return $pertsAjax({
url: '/api/put/user/' + user.id,
params: {s1_status_code: 'COM'}
}).then(function successCallback(user) {
sixtySixerSuccess[user.id] = user;
}, function errorCallback(message) {
sixtySixerErrors[user.id] = message;
numErrors += 1;
});
}, 'parallel');
};
var today = new Date();
var deletedClassrooms = [];
var deletedStudents = [];
var zeroProgClasses = forEach(classProgIndex, function (classId, sumProg) {
var activity = activitiesByClassroom[classId];
var scheduled = activity.scheduled_date ?
Date.createFromString(activity.scheduled_date, 'local') :
undefined;
var datePassed = (today - scheduled) >= (Date.intervals.day * 14);
if (scheduled && datePassed && sumProg === 0) {
deletedClassrooms.push(classId);
var students = forEach(studentsByClass[classId], function (user) {
return user.id;
});
deletedStudents = deletedStudents.concat(students);
return classId;
}
});
var deleteZeroProgress = function () {
// First delete students, then classrooms, just like the UI, to avoid
// association problems.
$forEachAsync(deletedStudents, function (userId) {
return $pertsAjax({url: '/api/delete/' + userId});
}, 'parallel').then(function () {
$forEachAsync(deletedClassrooms, function (classId) {
return $pertsAjax({url: '/api/delete/' + classId});
}, 'parallel');
});
};
var report = function () {
console.log("Of a total of", allUsers.length, "users");
console.log("...", sixtySixers.length, "were identified as 66%ers");
console.log("...there were", numErrors, "errors while marking them complete. Details in `sixtySixerErrors`.");
console.log("Of a total of", numClasses, "classrooms");
console.log("...there were", deletedClassrooms.length, "zero-progress classes which were deleted (their students were also deleted). They are listed in `deletedClassrooms`, and they and all their students are listed together in `toDelete`.");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment