Created
November 4, 2021 16:48
-
-
Save thinkle/fe2034a8171007653afd54c3969fbc07 to your computer and use it in GitHub Desktop.
Classroom Ownership Rescue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Google Apps Script */ | |
/* This code fixes ownership for every file in a google classroom */ | |
/* Assumes that ownership has accidentally been transferred to the script user | |
(i.e. through moving a folder through a Shared Drive and back out) | |
*/ | |
function rescueHosedClass (courseId) { | |
let course = Classroom.Courses.get(courseId); | |
let owner = Classroom.UserProfiles.get(course.ownerId).emailAddress; | |
rescueHosedMaterials(courseId, owner); | |
rescueAllHosedSubmissions(courseId, owner); | |
} | |
function rescueAllHosedSubmissions (courseId, teacher) { | |
let response = {}; | |
do { | |
response = Classroom.Courses.CourseWork.list(courseId, {nextPageToken:response.nextPageToken}); | |
response.courseWork.forEach( | |
(cwItem)=>{ | |
rescueHosedSubmissions(courseId,cwItem.id,teacher); | |
} | |
) | |
} while (response.nextPageToken) | |
} | |
function rescueHosedSubmissions (courseId, courseWorkId, teacher) { | |
let response = {} | |
do { | |
response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId,courseWorkId,{nextPageToken:response.nextPageToken}); | |
response.studentSubmissions.forEach( | |
(submission)=>{ | |
if (submission?.assignmentSubmission?.attachments) { | |
let studentProfile = Classroom.UserProfiles.get(submission.userId); | |
let student = studentProfile.emailAddress; | |
submission.assignmentSubmission.attachments.forEach( | |
(a)=>{ | |
if (a.driveFile) { | |
console.log('Got drive attachment',a.driveFile,submission.state); | |
console.log('User is',student); | |
let f; | |
try { | |
f = DriveApp.getFileById(a.driveFile.id) | |
} catch (err) { | |
console.log('We cannot access file -- probably a good sign') | |
} | |
if (f) { | |
let owner = f.getOwner().getEmail(); | |
console.log('OWNED BY',owner); | |
if (owner != student && owner != teacher) { | |
console.log("WEIRD: WHY IS THIS OWNED BY",owner) | |
if (submission.state=='CREATED' || submission.state=='RETURNED') { | |
console.log('TRANSFER OWNERSHIP TO STUDENT',student) | |
f.setOwner(student); | |
} else if (submission.state=='TURNED_IN'||submission.state=='RESUBMITTED') { | |
console.log('TRANSFER OWNERSHIP TO TEACHER') | |
f.setOwner(teacher); | |
} else { | |
console.log('What do I do with state:',submission.state) | |
} | |
} | |
} // end if file | |
} // end if driveFile | |
} | |
); // end each attachment | |
} // end if attachments | |
} | |
) // end each submission; | |
} while (response.nextPageToken); | |
} | |
function rescueHosedMaterials (courseId, owner) { | |
let materials = {} | |
do { | |
materials = Classroom.Courses.CourseWorkMaterials.list(courseId, {nextPageToken:materials.nextPageToken}); | |
materials.courseWorkMaterial. | |
forEach( | |
(m,i)=>{ | |
if (m.materials) { | |
m.materials.forEach( | |
(mm,ii)=>{ | |
if (mm.driveFile) { | |
console.log(i,ii,mm.driveFile.shareMode,mm.driveFile.driveFile.id); | |
try { | |
let orig = DriveApp.getFileById(mm.driveFile.driveFile.id); | |
let fileOwner = orig.getOwner().getEmail(); | |
//console.log('Owned by:',JSON.stringify(fileOwner)); | |
//console.log('Eds:',JSON.stringify(orig.getEditors().map((u)=>u.getEmail()))); | |
if (fileOwner != owner) { | |
console.log('Ownership mismatch! Fix it!') | |
if (fileOwner == 'me') { // replace me with your own email | |
console.log('Cool I own it') | |
console.log('Set owner to...',owner); | |
orig.setOwner(owner); | |
} else { | |
console.log('Weird I cannot fix it.'); | |
console.log('Owned by ',fileOwner,'not',owner) | |
} | |
} | |
} catch (err) { | |
console.log('No access'); | |
} | |
} | |
} | |
) | |
} | |
} | |
) | |
} while (materials.nextPageToken); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment