Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a version for question_submission events. Add boxes to quiz questions #291

Merged
merged 10 commits into from
Oct 31, 2019
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ learnr 0.10.0 (unreleased)

* When questions are tried again, the existing answer will remain, not forcing the user to restart from scratch. ([#270](https://github.com/rstudio/learnr/issues/270))

* A version number has been added to `question_submission` events. This will help when using custom storage methods. ([#291](https://github.com/rstudio/learnr/pull/291))


## Bug fixes

Expand Down
39 changes: 26 additions & 13 deletions R/storage.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ save_question_submission <- function(session, label, question, answer) {
session = session,
object_id = label,
tutorial_object("question_submission", list(
version = 1,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
question = question,
answer = answer
))
Expand Down Expand Up @@ -103,17 +104,6 @@ get_all_state_objects <- function(session, exercise_output = TRUE) {
object$data["output"] <- list(NULL)
}
}
if (object$type == "question_submission") {
if (!is.null(object$data$answers)) {
# as of v0.10.0...
# upgrade from old storage format to new storage format
# rename answers -> answer
object$data$answer <- object$data$answers
object$data$answers <- NULL
# do not record correct information
object$data$correct <- NULL
}
}
object
})

Expand Down Expand Up @@ -205,18 +195,41 @@ save_object <- function(session, object_id, data) {
tutorial_storage(session)$save_object(tutorial_id, tutorial_version, user_id, object_id, data)
}


update_object <- function(object) {
if (object$type == "question_submission") {
version <- object$data$version
if (!is.null(version)) {
if (identical(version, 1)) {
# do nothing
} else {
schloerke marked this conversation as resolved.
Show resolved Hide resolved
# as of v0.10.0...
# upgrade from old storage format to new storage format
# rename answers -> answer
object$data$answer <- object$data$answers
object$data$answers <- NULL
# do not record correct information
object$data$correct <- NULL
}
}
}
object
}

get_object <- function(session, object_id) {
tutorial_id <- read_request(session, "tutorial.tutorial_id")
tutorial_version <- read_request(session, "tutorial.tutorial_version")
user_id <- read_request(session, "tutorial.user_id")
tutorial_storage(session)$get_object(tutorial_id, tutorial_version, user_id, object_id)
object <- tutorial_storage(session)$get_object(tutorial_id, tutorial_version, user_id, object_id)
update_object(object)
}

get_objects <- function(session) {
tutorial_id <- read_request(session, "tutorial.tutorial_id")
tutorial_version <- read_request(session, "tutorial.tutorial_version")
user_id <- read_request(session, "tutorial.user_id")
tutorial_storage(session)$get_objects(tutorial_id, tutorial_version, user_id)
objects <- tutorial_storage(session)$get_objects(tutorial_id, tutorial_version, user_id)
lapply(objects, update_object)
}

remove_all_objects <- function(session) {
Expand Down