Last active
March 21, 2022 18:55
-
-
Save GaetanoPiazzolla/bbd4e4a029318505192089543697530c 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
private static void main_chapter3() { | |
//0- access token, from the previous chapter | |
String token = getAccessToken(); | |
//1- create a new form | |
String formId = createNewForm(token); | |
//2- transform it into a quiz | |
transformInQuiz(formId,token); | |
//3- add questions | |
addItemToQuiz( | |
"Which of these singers was not a member of Destiny's Child?", | |
Arrays.asList("Kelly Rowland", "Beyoncè", "Rihanna", "Michelle Williams"), | |
"Rihanna", | |
formId, | |
token | |
); | |
} | |
private static String createNewForm(String token) throws IOException { | |
Form form = new Form(); | |
form.setInfo(new Info()); | |
form.getInfo().setTitle("New Form Quiz Created from Java"); | |
form = formsService.forms().create(form) | |
.setAccessToken(token) | |
.execute(); | |
return form.getFormId(); | |
} | |
private static void transformInQuiz(String formId, String token) throws IOException { | |
BatchUpdateFormRequest batchRequest = new BatchUpdateFormRequest(); | |
Request request = new Request(); | |
request.setUpdateSettings(new UpdateSettingsRequest()); | |
request.getUpdateSettings().setSettings(new FormSettings()); | |
request.getUpdateSettings().getSettings().setQuizSettings(new QuizSettings()); | |
request.getUpdateSettings().getSettings().getQuizSettings().setIsQuiz(true); | |
request.getUpdateSettings().setUpdateMask("quizSettings.isQuiz"); | |
batchRequest.setRequests(Collections.singletonList(request)); | |
formsService.forms().batchUpdate(formId, batchRequest) | |
.setAccessToken(token).execute(); | |
} | |
private static void addItemToQuiz( | |
String questionText, | |
List<String> answers, | |
String correctAnswer, | |
String formId, String token) throws IOException { | |
BatchUpdateFormRequest batchRequest = new BatchUpdateFormRequest(); | |
Request request = new Request(); | |
Item item = new Item(); | |
item.setTitle(questionText); | |
item.setQuestionItem(new QuestionItem()); | |
Question question = new Question(); | |
question.setRequired(true); | |
question.setChoiceQuestion(new ChoiceQuestion()); | |
question.getChoiceQuestion().setType("RADIO"); | |
List<Option> options = new ArrayList<>(); | |
for (String answer : answers) { | |
Option option = new Option(); | |
option.setValue(answer); | |
options.add(option); | |
} | |
question.getChoiceQuestion().setOptions(options); | |
Grading grading = new Grading(); | |
grading.setPointValue(2); | |
grading.setCorrectAnswers(new CorrectAnswers()); | |
grading.getCorrectAnswers().setAnswers(new ArrayList<>()); | |
grading.getCorrectAnswers().getAnswers().add(new CorrectAnswer()); | |
grading.getCorrectAnswers().getAnswers().get(0).setValue(correctAnswer); | |
Feedback whenRight = new Feedback(); | |
whenRight.setText("Yeah!"); | |
Feedback whenWrong = new Feedback(); | |
whenWrong.setText("Wrong Answer"); | |
grading.setWhenRight(whenRight); | |
grading.setWhenWrong(whenWrong); | |
question.setGrading(grading); | |
item.getQuestionItem().setQuestion(question); | |
request.setCreateItem(new CreateItemRequest()); | |
request.getCreateItem().setItem(item); | |
request.getCreateItem().setLocation(new Location()); | |
request.getCreateItem().getLocation().setIndex(0); | |
batchRequest.setRequests(Collections.singletonList(request)); | |
formsService.forms().batchUpdate(formId, batchRequest) | |
.setAccessToken(token).execute(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment