6.1.1 Core
On this page:
defstep
defstep/  study
defview
defstudy
put/  identity
~current-view-uri
button
with-bot
6.1.1.1 Boxes
defbox
define-var-box
6.1.1.2 Logging
log-conscript-debug
log-conscript-info
log-conscript-warning
log-conscript-error
log-conscript-fatal
6.1.1 Core🔗

 (require conscript/base) package: conscript

The bindings provided by this module are also provided by #lang conscript.

syntax

(defstep (step-id) step-body)

Defines a study step. The step-body must be an expression that evaluates to a study-page — usually md or html.

syntax

(defstep/study step-id #:study child-study-expr
               maybe-require-bindings
               maybe-provide-bindings)
 
maybe-require-bindings = 
  | #:require-bindings ([child-id parent-id] ...)
     
maybe-provide-bindings = 
  | #:provide-bindings ([parent-id child-id] ...)

Defines a study step that runs child-study-expr when reached.

The step step-id is said to be part of the “parent” study, of which child-study-expr becomes a “child” study.

The #:require-bindings argument is used to map identifiers required by the child study to identifiers available in the current study context if their names differ. Any identifiers required by the child study that are not mapped here are assumed to be named identically to identifiers in the parent study context, and defstep/study will attempt to map them accordingly.

The #:provide-bindings argument can be used to map identifiers in the parent study to particular identifiers provided by child-study-expr upon completion. When #:provide-bindings is not specified, no values are assigned.

syntax

(defview ...)

...

syntax

(defstudy study-id
          maybe-requires
          maybe-provides
          step-transition ...)
 
maybe-requires = 
  | #:requires (value-id-sym ...)
     
maybe-provides = 
  | #:provides (value-id-sym ...)
     
step-transition = [step --> step maybe-more-steps]
     
maybe-more-steps = 
  | --> step ...
 
  value-id-sym : symbol?
  step : step?
Defines a study in terms of steps joined by transitions. The transitions follow the same grammar as transition-graph.

For any “final” step (that is, a step that marks the study’s end or one of the study’s endings) you must include a separate step-transition with the step at both ends.

Example:

(defstudy college-try
  [attempt --> ,(lambda () (if success
                               (goto good-ending)
                               (goto bad-ending)))]
  [bad-ending --> bad-ending]
  [good-ending --> good-ending])

procedure

(put/identity key value)  void?

  key : symbol?
  value : any/c
If the user enrolled in the study via an identity server, stores value under key within their enrollment on that server; if the user did not enroll through an identity server, it stores the value under key directly on the Congame server.

The point of enrolling via an identity server is to prevent the study author from connecting a participant’s answers with their identity — but there are cases where a well-designed study will still need to attach some data to their identity. For example, at the end of a study you might call (put/identity 'payment-due pay-amount) so that, despite not knowing the participant’s answers, you know how much you need to pay them.

procedure

(~current-view-uri)  string?

Returns the URI of the current view handler page.

procedure

(button [action-proc]    
  label    
  [#:id id    
  #:to-step-id to-step-id    
  #:up-target up-target    
  #:up-transition up-transition])  xexpr?
  action-proc : (-> any/c) = void
  label : string?
  id : string? = ""
  to-step-id : (or/c identifier? #f) = #f
  up-target : string? = ".step"
  up-transition : string? = "none"
Returns a representation of an HTML <a> element styled as a button that navigates to the next step in the study when clicked.

If action-proc is provided, it will be called when the button is clicked, just before the next step in the study is loaded. Rather: before the transition is run.

The href attribute is dynamically set to the URL of a continuation that first calls action with no arguments, then returns the page provided by to-step-id (or that provided by the next step in the study if to-step-id is #f).

If #:id is provided, it sets the value of the data-widget-id attribute for the element.

syntax

(with-bot step-expr bot-expr)

Wraps step-expr so that its default bot is bot-expr.

(defstep (hello)
  (button "Continue..."))
(defstudy s
  [[hello (with-bot hello bot:continuer)] --> ,(lambda () done)])
6.1.1.1 Boxes🔗

syntax

(defbox id)

Defines two functions, get-id (which takes no arguments and returns the value of id) and set!-id (which takes one argument and updates the value of id).

Examples:
> (define flux 81)
> (defbox flux)
> (get-flux)

81

> (set!-flux "new")
> (get-flux)

"new"

> flux

"new"

syntax

(define-var-box id var)

Binds id to a function that can take zero arguments or one argument. If given no arguments, the function returns the current value of var (which must already be defined elsewhere); if given a single argument, the function set!s the value of var to that value.

The function id can be passed to other functions, allowing them to access or update a local variable.

Examples:
> (define flux #f)
> (define-var-box get-or-set flux)
> (get-or-set)

#f

> (get-or-set 'foo)
> (get-or-set)

'foo

> flux

'foo

6.1.1.2 Logging🔗

syntax

(log-conscript-debug string-expr)

(log-conscript-debug format-string-expr v ...)

syntax

(log-conscript-info string-expr)

(log-conscript-info format-string-expr v ...)

syntax

(log-conscript-warning string-expr)

(log-conscript-warning format-string-expr v ...)

syntax

(log-conscript-error string-expr)

(log-conscript-error format-string-expr v ...)

syntax

(log-conscript-fatal string-expr)

(log-conscript-fatal format-string-expr v ...)

See Logging in the Racket Reference for more information on logging.

Logs an event with the Conscript logger, evaluating string-expr or (format format-string-expr v ...). These forms are listed above in order of severity.

Logging functions are useful for debugging: for example, when you want to capture and surface state information in the middle of a process, or extra context about possible causes of a problem.

Logged events are printed on the console (stderr) of a running Congame server.

Note that since the result of a log-conscript-level form is #<void>, you can’t use it directly inside a study step. Instead, wrap it in a begin expression that returns an empty string:

(defstep (age-name-survey)
  @md{
    # Survey
 
    @form{
      What is your first name? @(set! first-name (input-text))
 
      @(begin
        (log-conscript-info "Hello")
        "")
      @submit-button
  }})