// Numbas version: exam_results_page_options {"name": "Fetch data from an external source before variable generation", "extensions": [], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Fetch data from an external source before variable generation", "tags": [], "metadata": {"description": "

This question shows how to use the question's JavaScript preamble to request data from an external source, and use that data in question variables.

\n

Note that this means the question only works when the external source is available. Use this very carefully, and avoid it if you possibly can!

", "licence": "Creative Commons Attribution 4.0 International"}, "statement": "

The preamble of this question calls https://fakerapi.it/api/v1/books?_quantity=1 to get information about a randomly generated fake book. This particular task could be done entirely in Numbas, but it demonstrates the principle of requesting data from an external source.

\n

If the request succeeds, then the variable book is set with the returned data. The variables title and author are both defined to extract information from book.

", "advice": "", "rulesets": {}, "extensions": [], "builtin_constants": {"e": true, "pi,\u03c0": true, "i": true}, "constants": [], "variables": {"author": {"name": "author", "group": "Ungrouped variables", "definition": "book['author']", "description": "", "templateType": "anything", "can_override": false}, "title": {"name": "title", "group": "Ungrouped variables", "definition": "book['title']", "description": "", "templateType": "anything", "can_override": false}, "book": {"name": "book", "group": "Ungrouped variables", "definition": "json_decode(safe(\"{\\n \\\"id\\\": 1,\\n \\\"title\\\": \\\"Dormouse, not.\\\",\\n \\\"author\\\": \\\"Rosalyn Kub\\\",\\n \\\"genre\\\": \\\"Et\\\",\\n \\\"description\\\": \\\"Mock Turtle, and said to the croquet-ground. The other guests had taken his watch out of their hearing her; and when she next peeped out the answer to shillings and pence. \\'Take off your hat,\\' the.\\\",\\n \\\"isbn\\\": \\\"9788125034827\\\",\\n \\\"image\\\": \\\"http://placeimg.com/480/640/any\\\",\\n \\\"published\\\": \\\"1998-04-11\\\",\\n \\\"publisher\\\": \\\"Laudantium Sit\\\"\\n}\"))", "description": "

The data about the book is stored in this variable once it's received from the external API. A default value is given so that the editor doesn't complain about an undefined variable, and to document the variable's structure.

", "templateType": "json", "can_override": false}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["book", "author", "title"], "variable_groups": [], "functions": {}, "preamble": {"js": "const url = 'https://fakerapi.it/api/v1/books?_quantity=1';\nreturn fetch(url, {headers: {'Accept': 'application/json'}}).then(response => {\n return response.json().then(({data}) => {\n const [book] = data;\n question.scope.setVariable('book', Numbas.jme.wrapValue(book));\n })\n});", "css": ""}, "parts": [{"type": "information", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "

This question is about the book \"{title}\" by {author}.

"}], "partsMode": "all", "maxMarks": 0, "objectives": [], "penalties": [], "objectiveVisibility": "always", "penaltyVisibility": "always", "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}