Skip to content

Instantly share code, notes, and snippets.

@wesbasinger
Created August 7, 2016 20:27
Show Gist options
  • Save wesbasinger/2990bf00d5d1c0942ada9e502f1f7827 to your computer and use it in GitHub Desktop.
Save wesbasinger/2990bf00d5d1c0942ada9e502f1f7827 to your computer and use it in GitHub Desktop.
Pagination in a less hacky way...
// Here's the hacky way. I'm looping 8 times, because I know I have 80 students and therefore need at least 8 pages of ten.
function getSubmissions(course, courseWork) {
var optionalArgs = {
pageSize: 10
}
var submissionObjs = [];
for (var i=0; i<8; i++) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(course, courseWork, optionalArgs); // API call with necessary parameter
optionalArgs.pageToken = response.nextPageToken; //set next page token for a new page of results on next request
var submissions = response.studentSubmissions; // get just the array of data
submissions.forEach(function(submission) { // loop and push each object to the result array
submissionObjs.push(submission);
});
}
return submissionObjs; // return when everything is done
}
// I tried this, but I always come up one student short...
// There may be syntax or logic errors, but I had *something* like this working to give me all but one student back.
// I would much prefer to having to manually set the loop like above!
function getSubmissions(course, courseWork) {
var optionalArgs = {
pageSize: 1
}
var submissionObjs = [];
while (Classroom.Course.CourseWork.StudentSubmissions.list(course, courseWork, optionalArgs).pageToken) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(course, courseWork, optionalArgs);
optionalArgs.pageToken = response.nextPageToken;
submissionObjs.push(response.submissions[0]);
}
return submissionObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment