Skip to content

Instantly share code, notes, and snippets.

@hinklefoxmail hinklefoxmail/detention.gs Secret
Created Mar 9, 2017

Embed
What would you like to do?
Schedule Detentions, Invite, and Send Notifications
function onFormSubmit() {
//get forms info
var form = FormApp.getActiveForm();
var currentResponse = form.getResponses()[form.getResponses().length-1];
var timestamp = currentResponse.getTimestamp();
var teacherEmail = currentResponse.getRespondentEmail();
var studentEmail = currentResponse.getItemResponses()[0].getResponse();
studentEmail = studentEmail.toLowerCase();
//Get tiers responses
var tier1 = currentResponse.getItemResponses()[1].getResponse();
try{
var tier2 = currentResponse.getItemResponses()[2].getResponse();
var isTier2 = true;
}
catch(err){
var tier2 = [];
var isTier2 = false;
if (tier1 == '***Tier 2 Offence Only'){
var teacherEmail = currentResponse.getRespondentEmail();
MailApp.sendEmail(teacherEmail, 'Detention Not Scheduled', 'Please fill out a reason for the detention and resubmit');
return;
}
}
//change array of violations to string for emailing
var reason = '';
for (var i = 0; i < tier1.length; i++){
if (tier1 != 'Tier 2 Offence Only'){
reason = reason + tier1[i] + '\n';
}
}
for (var i = 0; i < tier2.length; i++){
reason = reason + tier2[i] + '\n';
}
//Get date
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDate();
var year = date.getFullYear();
var hour = date.getHours()+1;
var minute = date.getMinutes()+1;
var today = month+'/'+day;
var myDay = month+'/'+day+'/'+year;
//Put in correct sheet for date, or create a new one
var sheetName = month+'-'+year;
var spreadsheet = SpreadsheetApp.openById('1XFIKxvUnhjfIljKenoAqsFJWbqPnMCXqsLCMHfKSNzg');
try{
var sheet = spreadsheet.getSheetByName(sheetName);
var lastRow = sheet.getLastRow();
Logger.log('Hit');
}
catch(err){
var sheet = spreadsheet.insertSheet(sheetName);
sheet.appendRow(['Student Name','Grade','Date Assigned','Reason','Teacher','Detention Date']);
var lastRow = sheet.getLastRow();
Logger.log('caught');
}
//Get dates
var now = new Date();
var currentDate = new Date(now.getTime() + (3000000));
var inTwoWeeks = new Date(now.getTime() + (2*7*24 * 60 * 60 * 1000));
Logger.log(studentEmail);
//Find student by email or send error message
var studentInfo = findParents(studentEmail);
if (studentInfo[0]=='unknown'){
Logger.log('unknown');
MailApp.sendEmail(teacherEmail, 'Error in Scheduling Detention', 'There was a error in scheduling detention for '+studentEmail+' due to the student email not being recognized. Please resubmit the form and try again');
return;
}
//Get detentions for next 2 weeks
var calendar = CalendarApp.getCalendarById('aisabuja.com_3h1a0srt0pcd5cormp4p5t2mos@group.calendar.google.com');
var events = calendar.getEvents(currentDate, inTwoWeeks);
//Search events for next 2 weeks
for (var i = 0; i<events.length; i++){
var detentionEvent = events[i];
var studentIsGuest = false;
var guests = events[i].getGuestList();
//Find event student not in
for (var j = 0; j<guests.length; j++){
var studentEmail2 = guests[j].getEmail();
studentEmail2 = studentEmail2.toLowerCase();
if (studentEmail2 == studentEmail){
studentIsGuest = true;
break;
}
}
if (studentIsGuest){
Logger.log('continue');
continue;
}
//Get event info
var detentionDate = detentionEvent.getStartTime();
var detentionLocation = detentionEvent.getLocation();
var guest = detentionEvent.addGuest(studentEmail);
guest.addEmailReminder(30);
var studentName = studentInfo[0];
var studentGrade = studentInfo[1];
var parentEmail1 = studentInfo[3];
var parentEmail2 = studentInfo[4];
var parents = '';
//Check if both parent emails are filled
if (parentEmail1.length > 2 && parentEmail2.length >2){
parents = parentEmail1 + ' and ' + parentEmail2;
}
else{
if (parentEmail1.length > 2){
parents = parentEmail1;
}
if (parentEmail2.length > 2){
parents = parentEmail2;
}
}
//send emails
var message = 'Detention is scheduled for '+studentName+' at '+detentionDate+' for:\n'+reason+'\nYou may also notify their parents at '+parents+'\n\nThank you.';
var message2 = 'You will report immediately at the beginning of lunch with a pencil or pen and homework/book to read. You will not be allowed to use any electronics or talk. If you are purchasing a lunch, it will be brought to you. Failure to attend will result in your parents being contacted and you being reassigned two detentions.\nIf you have any questions, please contact '+teacherEmail+' or visit during office hours.';
MailApp.sendEmail(teacherEmail, 'Detention Scheduled for '+studentName, message);
MailApp.sendEmail(studentEmail, 'Detention', 'You have been scheduled for detention at lunch on '+detentionDate+' in '+detentionLocation+'. for:\n'+reason+'\n\n'+message2);
sheet.appendRow([studentEmail,studentGrade,timestamp,reason,teacherEmail,detentionDate]);
//if tier 2 repeat once
if (isTier2){
isTier2 = false;
continue;
}
//else break
break;
}
}
function findParents(studentEmail){
var sheet = SpreadsheetApp.openById('1LSkEcu2_4VCL-7LtnM4cwFMT2eQHXRzTuxl--6SYOW8').getSheets()[0];
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange(1, 1, lastRow, 5).getValues();
for (var i = 0; i<dataRange.length; i++){
var studentEmail2 = dataRange[i][2]
studentEmail2 = studentEmail2.toLowerCase();
if (studentEmail2 == studentEmail){
Logger.log('hit');
return dataRange[i];
}
}
return ['unknown','','','',''];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.