openwebwork/pg — pull request #153 by dpvc (2014-08-21 17:53:24+00:00)
Title: New scaffolding macros.

This file implements a new set of scaffolding macros based on Mike's compoundProblem5.pl, but using a new approach to mark the sections. This allows the content of the sections to be regular old PG code (rather than just text content), so you can use `BEGIN_TEXT/END_TEXT` or `BEGIN_PGML/END_PGML` to generate the text, and can use `ANS()` or `NAMED_ANS()` to create answer checkers just like in a normal problem (no need for `SECTION_ANS()` and `SECTION_NAMED_ANS()` anymore). Also, it keeps track of the answer banks used in the sections, so you don't have to manage those by hand. Basically, a problem looks like

loadMacros("scaffold.pl");

Scaffold::Begin();

Section::Begin("Part 1: The first part");
BEGIN_TEXT
This is the text for part 1. \(1+1\) = \{ans_rule\}
END_TEXT
ANS(Real(2)->cmp);
Section::End();

Section::Begin("Part 2: The second part");
BEGIN_TEXT
This is text for the second part. \(2*2\) = \{ans_rule\}
END_TEXT
ANS(Real(4)->cmp);
Section::End();

Scaffold::End();

Here is a real problem:

```
DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
"scaffold.pl",
"PGcourse.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

############################################
#
# Setup
#

Context("Numeric");
Context()->variables->are(x => 'Real');

$a = Compute(random(2,9,1));
$c = Compute(random(1,9,1));
$b = Compute(random(2,6,1));
if ($a*$c > 0) {$b += floor(sqrt(4*$a*$c))}

$quadratic = Formula("$a x^2 + $b x + $c");
$x1 = (-$b + sqrt($b**2-4*$a*$c))/(2*$a);
$x2 = (-$b - sqrt($b**2-4*$a*$c))/(2*$a);

###########################################
#
# The scaffold
#

Scaffold::Begin();

###########################################
Section::Begin("Part 1: Identify the coefficients");

BEGIN_PGML
Consider the quadratic equation given by [`[$quadratic] = 0`].

In this exercise, we will consider three different ways to solve this expression.

First, identify the coefficients for the quadratic using the standard form
[`ax^2 + bx + c = 0`]:

[`a`] = [____]{$a}, [`b`] = [____]{$b}, [`c`] = [____]{$c}
END_PGML

BEGIN_PGML_SOLUTION
Take the coefficient of [`x^2`] for the value of [`a`], the coefficient
of [`x`] for [`b`], and the constant for [`c`]. In this case, they
are [`a = [$a]`], [`b = [$b]`], and [`c = [$c]`].
END_PGML_SOLUTION

Section::End();

###########################################
Section::Begin("Part 2: Solve using the quadratic formula");

$multians1 = MultiAnswer($x1,$x2)->with(
singleResult => 0,
allowBlankAnswers => 1,
checker => sub {
my ($correct,$student,$self) = @_;
my ($s1, $s2) = @{$student};
my ($c1, $c2) = @{$correct};

return (1,1) if ($c1 == $s1 and $c2 == $s2) or
($c1 == $s2 and $c2 == $s1);
return (1,0) if $c1 == $s1 or $c2 == $s1;
return (0,1) if $c1 == $s2 or $c2 == $s2;
return (0,0);
}
);

BEGIN_PGML
Using the quadratic formula, solve [`[$quadratic] = 0`]

>> [`x`] = [______]{$multians1} or [`x`] = [______]{$multians1} <<
END_PGML

BEGIN_PGML_SOLUTION
Recall that the quadratic equation is

[``x = {-b \pm \sqrt{b^2 - 4ac} \over 2a}``].

You already identified [`a = [$a]`], [`b = [$b]`], and [`c = [$c]`],
so the results are:

[``x = {-[$b] + \sqrt{[$b]^2 - 4[$a][$c]} \over 2[$a]} = [$x1]``]

or

[``x = {-[$b] - \sqrt{[$b]^2 - 4[$a][$c]} \over 2[$a]} = [$x2]``].
END_PGML_SOLUTION

Section::End();

###########################################

Scaffold::End();

ENDDOCUMENT();
```