Skip to content

Commit e70fdd3

Browse files
authored
Merge pull request #1738 from maths/nrw9
Nrw9 - Adapt block for API
2 parents 4bbdcc3 + 558e2b8 commit e70fdd3

5 files changed

Lines changed: 56 additions & 14 deletions

File tree

api/public/sample.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ function toggleAnswer(button) {
172172
<div id="stackapi_validity" style="color:darkred"></div>
173173
</div>
174174
<br>
175-
<div id="stackapi_combinedfeedback" class="feedback col-lg-8" style="display: none">
175+
<div id="stackapi_combinedfeedback" class="feedback outcome col-lg-8" style="display: none">
176176
<div id="specificfeedback"></div>
177177
<div id="generalfeedback"></div>
178178
</div>
179179
<div id="stackapi_correct" style="display: none">
180180
<div class="noninfo">
181181
<h2>Correct answers:</h2>
182-
<div id="formatcorrectresponse" class="feedback"></div>
182+
<div id="formatcorrectresponse" class="feedback outcome"></div>
183183
</div>
184184
</div>
185185
</div>

api/public/stack.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,26 @@ function render_directory($dirdetails) {
178178
</div>
179179
<div id="stackapi_generalfeedback" class="col-lg-8" style="display: none">
180180
<h2>General feedback:</h2>
181-
<div id="generalfeedback" class="feedback"></div>
181+
<div id="generalfeedback" class="feedback outcome"></div>
182182
</div>
183183
<div id="stackapi_questionnote" class="col-lg-8" style="display: none">
184184
<h2>Question note:</h2>
185-
<div id="questionnote" class="feedback"></div>
185+
<div id="questionnote" class="feedback outcome"></div>
186186
</div>
187187
<h2 id="stackapi_score" style="display: none">Score: <span id="score"></span></h2>
188188
<div id="stackapi_summary" class="col-lg-10" style="display: none">
189189
<h2><?php echo stack_string('api_response')?>:</h2>
190-
<div id="response_summary" class="feedback"></div>
190+
<div id="response_summary" class="feedback outcome"></div>
191191
</div>
192192
<div id="stackapi_correct" class="col-lg-10" style="display: none">
193193
<div class="noninfo">
194194
<h2>Response summary:</h2>
195-
<div id="formatcorrectresponse" class="feedback"></div>
195+
<div id="formatcorrectresponse" class="feedback outcome"></div>
196196
</div>
197197
</div>
198198
<div id="stackapi_difference" class="col-lg-10" style="display: none">
199199
<h2>YAML representation based on API defaults:</h2>
200-
<div id="difference" class="feedback" style="white-space: pre;"></div>
200+
<div id="difference" class="feedback outcome" style="white-space: pre;"></div>
201201
</div>
202202
</div>
203203
</div>

api/public/stackjsvle.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
* @param {String} id the identifier of the element we want.
6565
*/
6666
function vle_get_element(id) {
67-
/* In the case of Moodle we are happy as long as the element is inside
68-
something with the `formulation`-class. */
6967
let candidate = document.getElementById(id);
7068
let iter = candidate;
71-
while (iter && !iter.classList.contains('formulation')) {
69+
while (iter && !iter.classList.contains('formulation') &&
70+
!iter.classList.contains('outcome')) {
7271
iter = iter.parentElement;
7372
}
74-
if (iter && iter.classList.contains('formulation')) {
73+
if (iter && (iter.classList.contains('formulation') ||
74+
iter.classList.contains('outcome'))) {
7575
return candidate;
7676
}
7777

@@ -99,10 +99,12 @@
9999
something with the `formulation`-class. */
100100
let initialcandidate = document.getElementById(srciframe);
101101
let iter = initialcandidate;
102-
while (iter && !iter.classList.contains('formulation')) {
102+
while (iter && !iter.classList.contains('formulation') &&
103+
!iter.classList.contains('outcome')) {
103104
iter = iter.parentElement;
104105
}
105-
if (iter && iter.classList.contains('formulation')) {
106+
if (iter && (iter.classList.contains('formulation') ||
107+
iter.classList.contains('outcome'))) {
106108
// iter now represents the borders of the question containing
107109
// this IFRAME.
108110
let possible = iter.querySelector('input[id$="_' + name + '"]');
@@ -313,6 +315,14 @@
313315
}
314316
}
315317

318+
// Transfer all data attributes of the input. Note that while most come from
319+
// STACK some might come from the VLE. For truly portable stuff only use ones
320+
// startting with "stack". Basically, the "initialValue" one comes from Moodle.
321+
response['input-dataset'] = {};
322+
for (var k in input.dataset) {
323+
response['input-dataset'][k] = input.dataset[k];
324+
}
325+
316326
// 3. Add listener for changes of this input.
317327
if (input.id in INPUTS) {
318328
if (msg.src in INPUTS[input.id]) {
@@ -468,6 +478,37 @@
468478
}
469479
}
470480

481+
break;
482+
case 'register-button-listener':
483+
// 1. Find the element.
484+
element = vle_get_element(msg.target);
485+
486+
if (element === null) {
487+
// Requested something that is not available.
488+
const ret = {
489+
version: 'STACK-JS:1.2.0',
490+
type: 'error',
491+
msg: 'Failed to find element: "' + msg.target + '"',
492+
tgt: msg.src
493+
};
494+
IFRAMES[msg.src].contentWindow.postMessage(JSON.stringify(ret), '*');
495+
return;
496+
}
497+
498+
// 2. Add a listener, no need to do anything more
499+
// complicated than this.
500+
element.addEventListener('click', (event) => {
501+
let resp = {
502+
version: 'STACK-JS:1.2.0',
503+
type: 'button-click',
504+
name: msg.target,
505+
tgt: msg.src
506+
};
507+
IFRAMES[msg.src].contentWindow.postMessage(JSON.stringify(resp), '*');
508+
// These listeners will stop the submissions of buttons which might be a problem.
509+
event.preventDefault();
510+
});
511+
471512
break;
472513
case 'toggle-visibility':
473514
// 1. Find the element.

api/public/stackshared.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ function answer() {
258258
http.open("POST", url, true);
259259

260260
if (!document.getElementById('output').innerText) {
261+
loading(false);
261262
return;
262263
}
263264

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function compile($format, $options): ?MP_Node {
6060

6161
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
6262
public function is_flat(): bool {
63-
return true;
63+
return false;
6464
}
6565

6666
// phpcs:ignore moodle.Commenting.MissingDocblock.Function

0 commit comments

Comments
 (0)