Skip to contents

Adds a button that, when clicked, gathers every question() and student_info() answer currently on the page – each already saved to the browser's localStorage when the reader submitted it – into a single readable JSON file and downloads it. Only submitted answers count: an answer reopened with "Edit" and not resubmitted is reported as its last submitted version, and the download is blocked until every student_info() form is submitted. This happens entirely in the reader's browser; there is no server to submit to, so this is meant for a reader to save and turn in themselves (e.g. attach to an email or upload to an LMS).

Usage

download_answers_button(filename_prefix = "learnr2-answers", label = NULL)

Arguments

filename_prefix

Prefix for the downloaded file's name. Defaults to "learnr2-answers".

label

Button label. NULL, the default, means the page language's "Download My Answers" (see the "Language" section of question()).

Value

A learnr2_download_button object, printed as an interactive HTML button.

Details

The download's answers array is one flat list: every question() plus every {webr} code exercise, each as a { id, answer } pair keyed by the widget's (or exercise's) stable id. A question never submitted has answer: null; a choice question's answer is an array of the picked options; an image-paste reflection's answer is the pasted screenshot as a data-URL string (WebP, or JPEG where the browser can't write WebP; see question()'s allow_image), and a voice question's is the recording as a data-URL string (WebM; see allow_audio). extract_submission_media() writes those out as files a grader can open.

A {webr} exercise appears only under the condition quarto-live itself requires to keep a record of the reader's code at all: #| persist: true (already the convention every bundled tutorial follows). An exercise without persist: true has no saved copy of the reader's code anywhere – learnr2 included – so it can't appear in the download; this is a structural limit of quarto-live's own editor, not something download_answers_button() chooses to skip.

The file opens with what it is a submission for: package and version (the package the tutorial ships in, and that package's version as rendered), tutorial (the tutorial's name, its directory under inst/tutorials/), title (its YAML title) and learnr2Version. These are filled in at render time – by render_tutorials(), and so by run_tutorial() and prerender_tutorials(), which know where the tutorial came from, or from the .qmd's own location when it is rendered with quarto render inside a package tree. A tutorial outside any package has package and version null.

The download also carries a metadata block (timezone, browser info, and a random per-device id persisted across the reader's visits) and a time field: the moment the reader clicked "Download", lightly obfuscated so the raw timestamp isn't readable or hand-editable in the file. Recover it with submission_time().

A reader need not reach this button to download: a Download Answers control sits beside Start Over on every page that has one of these buttons, so a student who stops part-way can hand in what they did without revealing the rest of the tutorial. The file's progress field says how far they got (sections revealed, questions answered), and the confirmation before an incomplete download names only the unanswered questions in sections the reader has reached.

The file ends with two fields for the grader. fingerprint is a SHA-256 of the submission's identifying content (time, device id, loadedFrom, the info fields and every answer), so a name or an answer edited by hand in the file no longer matches it; check_submission() recomputes and compares. loadedFrom is null unless the page was restored from an earlier download (load_tutorial() or the page's Load Answers button), in which case it records that file's time, device id, fingerprint, name and email, so a resubmission can be traced to the file it grew from. Neither is a signature: everything runs in the reader's browser with no secret, so a reader who reads the code can regenerate both. They make the common case, a copied file with the name changed, visible; they do not make the file tamper-proof.

Examples

download_answers_button()
#> <div class="learnr2-download-answers" data-learnr2-download="eyJmaWxlbmFtZVByZWZpeCI6ImxlYXJucjItYW5zd2VycyIsImxhYmVsIjpudWxsLCJwYWNr&#10;YWdlIjpudWxsLCJ2ZXJzaW9uIjpudWxsLCJ0dXRvcmlhbCI6bnVsbCwidGl0bGUiOm51bGws&#10;ImxlYXJucjJWZXJzaW9uIjoiMC4xLjMuOTAxNiJ9">
#>   <noscript>This button requires JavaScript.</noscript>
#> </div>