Skip to content
Classroom

Conditional

Answer type: conditional

For answers no built-in type can grade — you write the check yourself.

When to reach for it

Rarely. A built-in type is compared, displayed and made accessible by the engine; a conditional question is only as good as the logic you write. Use it when the correctness rule genuinely cannot be expressed as a value, an expression or a selection — for example when several parts must be consistent with one another rather than each correct on its own.

The minimum

Like Multipart, a conditional question declares $anstypes:

$anstypes = array("number", "number")

No [conditional] tag, and no Blade directives

Earlier versions of this page showed a bracketed [conditional] section and @if / @endif directives. Blade is Laravel's template engine and has never been part of this system. A commit already removed the directives; the bracket tag is the same class of error.

Options

Variable Effect
$anstypes the input types offered — required
$answer the grade — true/false, a partial score from 0 to 1, or an array of checks such as array("number", $a, $b) or array("function", $f, $g) (all must pass) — required
$showanswer what the review screen shows as the answer (default: "Answers may vary")
$variables variable names permitted in the response
$domain sampling interval, for function-valued parts
$abstolerance / $reltolerance tolerance for array("number", …) / array("function", …) checks in $answer (default: relative 0.0015); a compare* call you write uses the tolerance you pass it
$reqdecimals required precision
$ansprompt text before the input box

Writing the check

Compare with the compare* macros rather than ==. They apply tolerance and handle equivalent forms, which raw comparison does not:

Macro Compares
comparenumbers($a, $b, $tol) numbers
comparenumberswithunits($a, $b, $tol) numbers carrying units
comparefunctions($a, $b, $vars, $tol, $domain) functions over a domain
comparesameform($a, $b, $vars) requires the same written form
comparecomplex($a, $b, $tol) complex numbers — not available on this platform's engine build
comparelogic($a, $b, $vars) logical expressions
comparesetexp($a, $b, $vars) set expressions
comparentuples($a, $b, $tol) n-tuples such as "(1,2)"; the brackets must match unless you add "ignoreparens"

The student's entries are in $stuanswers[$thisq].

Example

Accept any pair of integers whose product is the target — no single correct answer exists. A number box also accepts decimals, so the check tests that both entries are whole numbers, not just the product.

Common Control — setup:

$target = randfrom(array(12, 18, 24, 36))

Question Text:

<p>Give any two integers whose product is $target.</p>
<p>[AB0] and [AB1]</p>

Common Control, continued — answer and options:

$anstypes = array("number", "number")
$stu = $stuanswers[$thisq]
$answer = false
$answer = (floor($stu[0]) == $stu[0] && floor($stu[1]) == $stu[1] && comparenumbers($stu[0] * $stu[1], $target, "|0.0001")) if (is_numeric($stu[0]) && is_numeric($stu[1]))
$showanswer = "for example, 1 and $target"

Accessibility and review

Two things the engine does for built-in types that it cannot do here:

  • Show the correct answer. Set $showanswer to a representative response; otherwise the review screen only says "Answers may vary". Never put display text in $answer, which is the grade: the engine reads a string there as a score, so text such as "for example, 1 and 12" gives every student full credit.
  • Explain the rule. State the acceptance condition in qtext — "any two integers whose product is 36" — so a student who is marked wrong can tell why.

See Also