Skip to content

Conversation

@wch
Copy link
Collaborator

@wch wch commented Jul 14, 2020

This adds a general-purpose event handler system. Closes #357.

It adds:

  • event_register_handler()
  • one_time()

It also converts many of the existing event-like functions into to true event handlers.

It adds a new event, "section_viewed", which is triggered every time a section becomes visible.

The event_register_handler() function can be used to execute code every time an event occurs. For example, this will print a message to the console every time any section becomes visible.

```{r context="server-start"}
event_register_handler("section_viewed", function(session, event, data) {
  message("event: section_viewed: ", data$sectionId)
})
```

The one_time() function can be used to only execute code the first time an event happens AND some condition is satisfied. This can be used to execute code the only the first time a section is viewed:

```{r context="server-start"}
event_register_handler("section_viewed",
  function(session, event, data) {
    one_time(
      session,
      cond = data$sectionId == "section-exercise-with-hint",
      expr = { message("Seeing ", data$sectionId, " for the first time.") }
    )
  }
)
```

Usage notes

If a section doesn't have a name, then one will be auto-generated. In the example below, the first one will trigger a section_viewed event where data$sectionId is "section-section" and the second will be "section-section-1".

###

foo

###

bar

You can also add specific section IDs without having any text show up. This is better in practice, so that adding a section in the middle doesn't alter up later section IDs that you're looking for. In the example below, the first one will be "section-first" and the second will be "section-second".

### {#first}

foo

### A title here {#second}

bar


Implementation notes

The events starts in tutorial-format.js. There are shown and hidden events for the topics and sections. Unfortunately, they fire too often, and sometimes (for example) a shown event occurs when a section isn't actually visible. The code in that file converts those trigger-happy events into state, in the form a Shiny input value. It sets input$`tutorial-visible-sections` to a character vector that contains the IDs of the visible sections.

On the R side, the states from input$`tutorial-visible-sections` are converted back to events, using an observer which calls event_trigger("section_viewed", ....).

@wch wch requested a review from schloerke July 14, 2020 16:25
Copy link
Collaborator

@schloerke schloerke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caching within package is ok. We do this for global setup chunk and exercise chunks.

Let's add a "reset event handlers cache" when starting a learnr tutorial in tutorial().

Let's also reset the global setup chunk and exercise chunk cache when resetting the registered event handlers.

@wch wch requested a review from schloerke July 23, 2020 17:18
@wch wch marked this pull request as ready for review July 24, 2020 17:36
Co-authored-by: Carson Sievert <cpsievert1@gmail.com>
@wch wch merged commit 8ecaab8 into master Jul 28, 2020
@wch wch deleted the wch-events branch July 28, 2020 20:11
schloerke added a commit that referenced this pull request Jul 30, 2020
Co-authored-by: Carson Sievert <cpsievert1@gmail.com>
Co-authored-by: Barret Schloerke <barret@rstudio.com>
@wch wch mentioned this pull request Aug 20, 2020
schloerke added a commit that referenced this pull request Sep 2, 2020
* master:
  Add events to website (#423)
  Add a checker test with error as solution (#422)
  Fixes for learnr on external evaluator (#420)
  Add shinytest support (#407)
  Validate the ability to create an idb store (#417)
  Disable completion of R code inside quotes, closes #401 (#413)
  Bump version (#414)
  Save tutorial output to temp folder when tutorial folder does not have write permissions (#412)
  Throw an informative error if an exercise chunk is NULL (#411)
  Remove outdated exercise option
  Checking for exercise errors (#403)
  Add event handler system (#398)
  External evaluator fixes (#399)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trigger actions when student advances between sections

4 participants