Last active
May 27, 2018 15:31
-
-
Save cms-jakes/a7cf6602778d0dccf7920049d5688719 to your computer and use it in GitHub Desktop.
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
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| // CANVAS API Call Template | |
| // by Jake Standish [[email protected]] | |
| // Written December, 2016 | |
| // You are free to copy and moodify this code at your own risk. | |
| // | |
| // INSTRUCTIONS | |
| // 1. Copy this code. | |
| // 2. Open a google apps script file from a Google Spreadsheet, Form, Document, or Google Apps Script (script.google.com). | |
| // 3. Obtain your access token from Canvas and paste it's value in the parenthese after var accessToken = | |
| // 4. Enter your Canvas domain url between parentheses after var domain = | |
| // 5. Begin writing function that call cAPI to call data from Canvas. | |
| // Example: | |
| // The following example returns an object containing informaiton about courses you are enrolled in as a teacher. It will return up to 100 courses. | |
| // var data = cAPI('GET', '/api/v1/courses?enrollment_type=teacher&per_page=100') | |
| // | |
| // RESOURCES | |
| // How to get your access token: https://community.canvaslms.com/docs/DOC-3013 | |
| // Canvas API documentation: https://canvas.instructure.com/doc/api/ | |
| // | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| function getHeader(){ | |
| var properties = PropertiesService.getDocumentProperties(); | |
| var accessToken = ""; //paste your access token between the parentheses. | |
| var domain = ""; //type your domain url between the parentheses. (https://domainName.instructure.com) | |
| properties.setProperties({accessToken: accessToken, domain: domain}) | |
| var header = {'Authorization':'Bearer '+accessToken}; | |
| return header; | |
| } | |
| function cAPI(method,url) { | |
| var params = { | |
| 'headers': getHeader(), | |
| 'method': method | |
| } | |
| var domain = PropertiesService.getDocumentProperties().getProperty('domain') | |
| //Retrieve data from Canvas | |
| var call = UrlFetchApp.fetch(domain+url,params); | |
| var data = JSON.parse(call); | |
| var headers = call.getAllHeaders(); | |
| return data; | |
| } |
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
| ////////////////////////////////////////////////////////////////////////// | |
| // Install within a Google Form and have the "On Form Submit" trigger set | |
| // canvasAPI function is seperate and can be found by searching the Canvas Community for [email protected] | |
| ////////////////////////////////////////////////////////////////////////// | |
| function onFormSubmit(e) { | |
| var email = e.response.getRespondentEmail(); | |
| var random = Math.floor(Math.random() * 9999); | |
| var ssID = ""; //put destination spreadsheetID here | |
| var ss = SpreadsheetApp.openById(ssID); | |
| var sheet = ss.getSheetByName(""); //put name of sheet you want to write responses to here | |
| var nextRow = sheet.getLastRow()+1; | |
| var subAccount = 1; //set value for subAccount you want the course to go to here | |
| var e_response = e.response.getItemResponses(); | |
| var courseName = e_response[2].getResponse(); | |
| var name = email.replace(/\@.+/g,""); | |
| var fname = name.replace(/\..+/g,""); | |
| var lname = name.replace(/.+\./g,""); | |
| var domain = email.replace(/.+\@/g,""); | |
| Logger.log(name+" ;"+fname+" ;"+lname+" ;"+ domain) | |
| //Verify CMS Employee domain= "cms.k12.nc.us" | |
| var cmsStaff="cms.k12.nc.us"; | |
| try{ | |
| if (domain == cmsStaff) { | |
| //Search User by Email address | |
| var getUser = cAPI('GET', '/api/v1/accounts/self/users?search_term='+email); | |
| //GET userID | |
| for (i=0; i<getUser.length; ++i){ | |
| var uid = getUser[i].id; | |
| var profile = cAPI('GET', '/api/v1/users/'+uid+'/profile'); | |
| if (profile[11] == email || profile[9] == email){ | |
| var id = getUser[i].id; | |
| sheet.getRange(nextRow, 1).setValue(id); | |
| sheet.getRange(nextRow,2).setValue(email); | |
| //Create course | |
| var makeCourse = cAPI('POST', '/api/v1/accounts/'+subAccount +'/courses?course\[name\]='+courseName +'&course\[course_code\]='+courseName+' '+random); | |
| //Get course ID | |
| var getCourse = cAPI('GET', '/api/v1/accounts/self/courses?search_term='+courseName+' '+random); | |
| var courseID = getCourse[0].id; | |
| sheet.getRange(nextRow,3).setValue(getCourse[0].name); | |
| sheet.getRange(nextRow,4).setValue(courseID); | |
| //Update Course Code | |
| var courseCode = cAPI('PUT','/api/v1/courses/'+courseID+'?course\[course_code\]='+courseName+' '+courseID); | |
| //PUT user in course | |
| var putInCourse = cAPI('POST', '/api/v1/courses/'+courseID+'/enrollments?enrollment\[user_id\]='+id+'&enrollment\[type\]=TeacherEnrollment&enrollment\[enrollment_state\]=active'); | |
| Logger.log("email: "+email+" fname:"+fname+ " lname:"+lname+" CanvasID:"+id + " Domain:"+domain + " Course ID:"+courseID); | |
| } | |
| } | |
| } else { | |
| Logger.log("Student Hack"); | |
| } | |
| }catch(e){ | |
| var values = ["error", email, courseName, "", e]; | |
| sheet.getRange(nextRow,1,1,5).setValues(values) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment