-
-
Save patrickdavey/61f00ecebf7a97529a4ab68cbbf86e6c 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
function onOpen(e) { | |
FormApp.getUi() | |
.createAddonMenu() | |
.addItem("Add Questions to Blank Quiz", "processUpload") | |
.addToUi(); | |
} | |
function onInstall(e){ | |
onOpen(e); | |
}; | |
function processUpload() { | |
var form = FormApp.getActiveForm(); | |
var ui = FormApp.getUi(); | |
// Delete all items in the form | |
var items = form.getItems(); | |
while(items.length > 0){ | |
form.deleteItem(items.pop()); | |
} | |
createTest("ignored", true); | |
} | |
function questionUrl(url) { | |
if (url.lastIndexOf("https:", 0) !== 0) { | |
return "https:" + url; | |
} else { | |
return url; | |
} | |
} | |
function fetchImages(questions) { | |
let urls = questions | |
.map(q => questionUrl(q.url)) | |
let imageResponses = UrlFetchApp.fetchAll(urls); | |
return imageResponses.map(e => e.getBlob()); | |
} | |
function createTest (url, required) { | |
var data = sampleData(); | |
var form = FormApp.getActiveForm(); | |
form.setIsQuiz(true); | |
form.setTitle(data.title) | |
// don't want shuffling since image items are separate from answers | |
form.setShuffleQuestions(false); | |
let questionImages = fetchImages(data.quiz.questions); | |
data.quiz.questions.forEach(function(question, index) { | |
var position = question.position || index + 1; | |
var points = question.points || 1; | |
var title = "Question " + position; | |
if (!data.answer_sheet) { | |
form.addImageItem() | |
.setImage(questionImages[index]) | |
.setTitle("Question " + position) | |
} | |
if (question.type === "mc") { | |
var item = form.addMultipleChoiceItem(); | |
item.setPoints(points); | |
if (data.answer_sheet) { item.setTitle("Question " + position) } | |
var choices = []; | |
for (var i=0; i < question.count; i++) { | |
choices.push(item.createChoice(String.fromCharCode(65 + i), i+1 == question.correct)); | |
} | |
item.setRequired(required); | |
item.setChoices(choices); | |
} else { | |
var item = form.addTextItem(); | |
if (data.answer_sheet) { item.setTitle("Question " + position) } | |
item.setPoints(points); | |
item.setRequired(required); | |
} | |
}); | |
} | |
function sampleData() { | |
return { | |
"answer_sheet": false, | |
"quiz": { | |
"questions": [ | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 1, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 2, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 3, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 4, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 5, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 6, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 1, | |
"count": 4, | |
"points": 1, | |
"position": 7, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 8, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 9, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 10, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 11, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 1, | |
"count": 4, | |
"points": 1, | |
"position": 12, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 13, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 14, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 15, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 1, | |
"count": 4, | |
"points": 1, | |
"position": 16, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 17, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 18, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 19, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 20, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 21, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 22, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 23, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 24, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 25, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 26, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 27, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 3, | |
"count": 4, | |
"points": 1, | |
"position": 28, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 4, | |
"count": 4, | |
"points": 1, | |
"position": 29, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
}, | |
{ | |
"correct": 2, | |
"count": 4, | |
"points": 1, | |
"position": 30, | |
"type": "mc", | |
"url": "//via.placeholder.com/468x100" | |
} | |
] | |
}, | |
"title": "30 problem doc" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment