Skip to content

Commit b9e5e33

Browse files
committed
iss1757 - Add filter block
1 parent be3e14d commit b9e5e33

8 files changed

Lines changed: 124 additions & 7 deletions

File tree

corsscripts/ascii/stackascii.bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corsscripts/ascii/stackascii.bundle.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corsscripts/ascii/stackascii.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ import regexall from './extractors/regexall.js';
2222

2323
const extractorlib = { finalfunction, lastexpr, lastblock, regexmatch, regexall };
2424

25-
export default function init(inputIds, transforms, operations) {
25+
export default function init(inputIds, operations) {
2626
const markdownContainerId = inputIds[0];
2727
// inputIds[1..N] correspond to each parsed answer entry in order.
28+
const alloperations = operations;
29+
const filters = operations.filter(operator => operator.operation === 'filter');
2830
const extractors = operations.filter(operator => operator.operation === 'extractor');
29-
const inputTransforms = transforms ? transforms : 'latexwrap,boldfilter';
31+
const markdownitinfo = filters.find(operator => operator.type === 'markdownit');
32+
const inputTransforms = markdownitinfo ? markdownitinfo.transforms : 'latexwrap,boldfilter';
3033

3134
const blockCollector = { blocks: [] };
3235

lang/en/qtype_stack.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,10 +1050,13 @@
10501050
$string['stackBlock_ascii_unknown_param'] = 'Unknown parameter \'{$a}\' for the ASCII block.';
10511051
$string['stackBlock_ascii_param'] = 'Valid parameters are: {$a->param}.';
10521052

1053-
$string['stackBlock_extractor_targetinput_required'] = 'You must define the targetinput for the extractor block to use.';
1053+
$string['stackBlock_extractor_targetinput_required'] = 'You must define the targetinput for the extractor block to send the answer to.';
1054+
$string['stackBlock_extractor_type_required'] = 'You must define the type of extractor you want to use.';
10541055
$string['stackBlock_extractor_unknown_param'] = 'Unknown parameter \'{$a}\' for the extractor block.';
10551056
$string['stackBlock_extractor_param'] = 'Valid parameters are: {$a->param}.';
10561057

1058+
$string['stackBlock_filter_type_required'] = 'You must define the type of filter you want to use.';
1059+
10571060
$string['stackBlock_jsxgraph_width'] = 'The width of a JSXGraph must use a known CSS-length unit.';
10581061
$string['stackBlock_jsxgraph_height'] = 'The height of a JSXGraph must use a known CSS-length unit.';
10591062
$string['stackBlock_jsxgraph_width_num'] = 'The numeric portion of the width of a JSXGraph must be a raw number and must not contain any extra chars.';

samplequestions/stacklibrary/Features/input-type-sample-questions/Free-text_final_answer.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
<p>[[input:ans1]] [[validation:ans1]]</p>
1313
[[ascii input="ans1" transforms="latexwrap"]]
14+
[[filter type="markdownit" transforms="latexwrap" /]]
1415
[[extractor targetinput="ans2" type="finalfunction"/]]
1516
[[/ascii]]
1617
<p>[[hint title="Input help"]][[commonstring key="free_text_fact"/]][[/hint]]</p>

stack/cas/castext2/blocks/ascii.block.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,28 @@ public function compile($format, $options): ?MP_Node {
5656

5757
// Get the details of filter and extractor blocks.
5858
$operations = [];
59+
$isfilter = false;
5960
foreach ($this->children as $child) {
6061
if (is_a($child, 'stack_cas_castext2_extractor')) {
6162
$options = $child->params;
6263
$options['operation'] = 'extractor';
6364
$inputs[] = $options['targetinput'];
6465
$operations[] = $options;
66+
} else if (is_a($child, 'stack_cas_castext2_filter')) {
67+
$options = $child->params;
68+
$options['operation'] = 'filter';
69+
$operations[] = $options;
70+
$isfilter = true;
6571
}
6672
}
6773

74+
if (!$isfilter) {
75+
$defaultfilter = new StdClass();
76+
$defaultfilter->operation = 'filter';
77+
$defaultfilter->type = 'markdownit';
78+
$defaultfilter->transforms = 'latexwrap,boldfilter';
79+
}
80+
6881
// Set default width and height here.
6982
// We want to push forward to overwrite the iframe defaults if they are not provided in the block parameters.
7083
$existsuserwidth = array_key_exists('width', $xpars);
@@ -115,7 +128,7 @@ public function compile($format, $options): ?MP_Node {
115128
return 'stack_js.request_access_to_input("' . $item . '"' . $extra . ')';
116129
}, $inputs, array_keys($inputs)));
117130
$linkcode = 'Promise.all([' . $answercalls . '])';
118-
$linkcode .= ".then((inputIds) => {init(inputIds,'" . $xpars['transforms'] . "'," . json_encode($operations) . ");});";
131+
$linkcode .= ".then((inputIds) => {init(inputIds," . json_encode($operations) . ");});";
119132

120133
$r->items[] = new MP_String($linkcode);
121134
$r->items[] = new MP_String("\n</script>");

stack/cas/castext2/blocks/extractor.block.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public function validate(
6363
$err[] = stack_string('stackBlock_extractor_targetinput_required');
6464
}
6565

66+
if (!array_key_exists('type', $this->params)) {
67+
$valid = false;
68+
$err[] = stack_string('stackBlock_extractor_type_required');
69+
}
70+
6671
// Wrap the old string errors with the context details.
6772
foreach ($err as $er) {
6873
$errors[] = new $options['errclass']($er, $options['context'] . '/' . $this->position['start'] . '-' .
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
// This file is part of Stack - http://stack.maths.ed.ac.uk/
3+
//
4+
// Stack is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Stack is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Stack. If not, see <http://www.gnu.org/licenses/>.
16+
//
17+
18+
/**
19+
* A block for specifying parsing options in ascii blocks.
20+
* @package qtype_stack
21+
* @copyright 2024 University of Edinburgh.
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
23+
*/
24+
25+
defined('MOODLE_INTERNAL') || die();
26+
27+
require_once(__DIR__ . '/../block.interface.php');
28+
require_once(__DIR__ . '/../../../utils.class.php');
29+
30+
class stack_cas_castext2_filter extends stack_cas_castext2_block {
31+
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
32+
public function compile($format, $options): ?MP_Node {
33+
$r = new MP_List([
34+
new MP_String('filter'),
35+
new MP_String(json_encode($this->params)),
36+
]);
37+
38+
return $r;
39+
}
40+
41+
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
42+
public function is_flat(): bool {
43+
// These are never flat.
44+
return false;
45+
}
46+
47+
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
48+
public function validate_extract_attributes(): array {
49+
// No CAS arguments.
50+
return [];
51+
}
52+
53+
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
54+
public function validate(
55+
&$errors = [],
56+
$options = []
57+
): bool {
58+
$valid = true;
59+
$err = [];
60+
61+
if (!array_key_exists('type', $this->params)) {
62+
$valid = false;
63+
$err[] = stack_string('stackBlock_filter_type_required');
64+
}
65+
66+
// Wrap the old string errors with the context details.
67+
foreach ($err as $er) {
68+
$errors[] = new $options['errclass']($er, $options['context'] . '/' . $this->position['start'] . '-' .
69+
$this->position['end']);
70+
}
71+
72+
return $valid;
73+
}
74+
75+
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
76+
public function postprocess(
77+
array $params,
78+
castext2_processor $processor,
79+
castext2_placeholder_holder $holder
80+
): string {
81+
return '';
82+
}
83+
84+
/**
85+
* Is this an interactive block?
86+
* If true, we can't generate a static version.
87+
* @return bool
88+
*/
89+
public function is_interactive(): bool {
90+
return true;
91+
}
92+
}

0 commit comments

Comments
 (0)