maths / moodle-qtype_stack

Stack question type for Moodle
GNU General Public License v3.0
141 stars 150 forks source link

STACK-JS toggle visibility not working in general feedback #1153

Closed bgailer closed 6 months ago

bgailer commented 6 months ago

When I want to show or hide content in my question, I now use the new STACK-KS function toggle_visibility for that. This works great in the question text. However, I have a question where I want to show content in the general and specific feedback and in this case, toggle_visibility does not work. This is the code I use inside the general feedback:

[[iframe width="150px" height="50px"]]
[[script type="module"]]
import {stack_js} from '[[cors src="stackjsiframe.js"/]]';
document.body.innerHTML = `
<button type="button" id="btn1" class="btn">Show more feedback</button>
`;
const btn1 = document.getElementById("btn1");
btn1.addEventListener("click", (e) => {
stack_js.toggle_visibility("feedback-2", true);
});
[[/script]]
[[/iframe]]

The error says it could not find the element with that id (Error: Failed to find element: "feedback-1"), but the element is definitely present in the question. Maybe it is because the element is not inside the question text but inside the general feedback?

aharjula commented 6 months ago

Well, this is a problem. Currently, STACK-JS limits the targets it can affect to elements inside the `formulation '-class elements, and apparently, those things do not come out within such wrapping. As having dynamic things is something that would be nice to have even with model solutions, I guess we need to find a similar wrapper to detect those.

We cannot allow free access to all the elements on the page but hopefully model solutions are a thing that can be allowed.

A quick look at the DOM says to me that outcome is probably the wrapper we would allow, but I will need to do some checking.

One thing that won't be supported, however, is targeting "fake inputs" within non "formulation" content using the new restrict to only this questions inputs logic, for such "fake inputs" one should use [[quid]] (assuming it works with feedback). I am not keeping track of which question maps to a given model solution.

aharjula commented 6 months ago

I saw no interesting use of the outcome class in the Moodle code base, so we now allow targeting things inside that, which should fix this issue.