Skip to content

Commit 3a755cd

Browse files
authored
Merge pull request #1470 from maths/api-static
Add "static" content flag to API question render response.
2 parents c9e40c5 + aa75e5d commit 3a755cd

21 files changed

Lines changed: 178 additions & 1 deletion

api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The response is again a JSON document, with the following fields:
7575
- an int field `questionseed` indicating the seed used for this response
7676
- an int array `questionvariants` containing all variant seeds of the question
7777
- an array of arrays `iframes` of arguments to create iframes to hold JS panels e.g. JSXGraph, GeoGebra
78+
- a boolean field `isinteractive`, indicating if the question contains elements preventing a static representation. If true, a printed version of the question would make no sense
7879

7980
The input configuration consists of the following fields:
8081

api/controller/RenderController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function __invoke(Request $request, Response $response, array $args): Res
137137
$renderresponse->questionseed = $question->seed;
138138
$renderresponse->questionvariants = $question->deployedseeds;
139139
$renderresponse->iframes = StackIframeHolder::$iframes;
140+
$renderresponse->isinteractive = $question->is_interactive();
140141

141142
$response->getBody()->write(json_encode($renderresponse));
142143
return $response->withHeader('Content-Type', 'application/json');

api/dtos/StackRenderResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class StackRenderResponse {
4141
public $questionvariants;
4242
/** @var array */
4343
public $iframes;
44+
/** @var bool */
45+
public $isinteractive;
4446
}
4547

4648
// phpcs:ignore moodle.Commenting.MissingDocblock.Class

question.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,7 @@ public static function compile($id, $questionvariables, $inputs, $prts, $options
20062006
$units = false;
20072007
$forbiddenkeys = [];
20082008
$sec = new stack_cas_security();
2009+
\stack_cas_castext2_block::$isinteractive = false;
20092010

20102011
// Some counter resets to ensure that the result is the same even if
20112012
// we for some reason would compile twice in a session.
@@ -2254,7 +2255,7 @@ public static function compile($id, $questionvariables, $inputs, $prts, $options
22542255

22552256
// Remember to collect the extracted strings once all has been done.
22562257
$cc['static-castext-strings'] = $map->get_map();
2257-
2258+
$cc['is-interactive'] = \stack_cas_castext2_block::$isinteractive;
22582259
return $cc;
22592260
}
22602261

@@ -2289,4 +2290,12 @@ public function format_text($text, $format, $qa, $component, $filearea, $itemid,
22892290
$text = $qa->rewrite_pluginfile_urls($text, $component, $filearea, $itemid);
22902291
return format_text($text, $format, $formatoptions);
22912292
}
2293+
2294+
/**
2295+
* Is the question interactive?
2296+
* @return bool
2297+
*/
2298+
public function is_interactive() {
2299+
return $this->get_cached('is-interactive');
2300+
}
22922301
}

stack/cas/castext2/block.interface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ abstract class stack_cas_castext2_block {
5151
// Store any errors.
5252
// phpcs:ignore moodle.Commenting.VariableComment.Missing
5353
public $err = [];
54+
/**
55+
* Keeps track of whether the current question has interactove elements.
56+
* @var bool
57+
*/
58+
public static $isinteractive = false;
5459

5560
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
5661
public function __construct(
@@ -94,6 +99,15 @@ public function is_flat(): bool {
9499
return true;
95100
}
96101

102+
/**
103+
* Is this an interactive block?
104+
* If true, we can't generate a static version.
105+
* @return bool
106+
*/
107+
public function is_interactive(): bool {
108+
return false;
109+
}
110+
97111
/**
98112
* If this is not a flat block this will be called with the response from CAS and
99113
* should execute whatever additional logic is needed. Register JavaScript and such

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ public function validate(&$errors=[], $options=[]): bool {
8282
}
8383
return true;
8484
}
85+
86+
/**
87+
* Is this an interactive block?
88+
* If true, we can't generate a static version.
89+
* @return bool
90+
*/
91+
public function is_interactive(): bool {
92+
return true;
93+
}
8594
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,13 @@ public function postprocess(array $params, castext2_processor $processor, castex
105105
public function validate_extract_attributes(): array {
106106
return [];
107107
}
108+
109+
/**
110+
* Is this an interactive block?
111+
* If true, we can't generate a static version.
112+
* @return bool
113+
*/
114+
public function is_interactive(): bool {
115+
return true;
116+
}
108117
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,13 @@ public function validate(&$errors=[], $options=[]): bool {
148148
}
149149
return true;
150150
}
151+
152+
/**
153+
* Is this an interactive block?
154+
* If true, we can't generate a static version.
155+
* @return bool
156+
*/
157+
public function is_interactive(): bool {
158+
return true;
159+
}
151160
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ public function postprocess(array $params, castext2_processor $processor,
8282

8383
return $content;
8484
}
85+
86+
/**
87+
* Is this an interactive block?
88+
* If true, we can't generate a static version.
89+
* @return bool
90+
*/
91+
public function is_interactive(): bool {
92+
return true;
93+
}
8594
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,13 @@ public function validate(
6060
}
6161
return true;
6262
}
63+
64+
/**
65+
* Is this an interactive block?
66+
* If true, we can't generate a static version.
67+
* @return bool
68+
*/
69+
public function is_interactive(): bool {
70+
return true;
71+
}
6372
}

0 commit comments

Comments
 (0)