maths / moodle-qtype_stack

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

Template blocks does not work in PRTs #1081

Open jonaslache opened 10 months ago

jonaslache commented 10 months ago

Hello everyone,

I defined a [[template]] block in the question text using [[template name="foobar"]]...[[/template]]. When I then use the [[template name="foobar"/]] placeholder in the question text, it works great and the correct content is filled in.

However, it does not work when I use the [[template name="foobar"/]] placeholder in the PRT. Instead of the previously defined content, the message Warning no template defined with name "foobar" appears.

It would be great if the template blocks could also be used in the PRT text to avoid repetition.

Thanks and best regards, Jonas

aharjula commented 10 months ago

Well, this is a bit of a problem. Templates defined inside question variables (with inline CASText) should be available everywhere (as question variables are mostly available everywhere, validation being the messy exception), but those defined in question text are indeed only available in contexts where that question text is also being evaluated and PRT evaluation is not one of those contexts. Actually, there is only one evaluation context where it gets evaluated, and in that pretty much nothing else happens other than the initialisation of inputs.

To make this work, we would need to extract template content from the question text and transplant it into contexts where the question text is not present, and that is a bit more complex thing to do than it might seem like, at first glance. If mixed with localisation or other conditional definition it would quickly become a nightmare to extract.

Basically, it would make sense to define templates in the question text, but unfortunately, question text is a branch of the evaluation of the question happening so separately from everything else that transferring stuff from it to other branches is a problem. The current recommendation is to target the root of the "evaluation tree" if one wishes to share things around the question, and that root is question variables.

Side note, someone should really write a page of documentation on the way STACK questions are evaluated, where different parts happen, and which parts are visible to each other. In this case, the natural rule of what is higher up in the editor is also available lower down in the logic does apply as question text branches out, the same applies to general feedback and most notably while PRTs are evaluated in the same evaluation context they do not fully see each other and might not even be executed or present.

jonaslache commented 10 months ago

Thanks, Matti, for commenting! Using the question variables instead of the template block actually is a workaround that solves my problem. Could it be an idea to add this information to the docs?

Just one question, before I open a separate issue for something that might be related and thus not to be solved easily: I recently noticed a similar behaviour for the [[debug/]] block: When placed in the question variables, it returns a list of all question variables and their values. But when I use it in a PRT, I don't get a list of question variables and/or feedback variables, but the following message: ["%cs","castext_debug_no_vars"]. Is this related?

aharjula commented 10 months ago

It is a separate issue, certain things it (the [[debug/]]-block) needs just are not being passed to it during the compilation of the PRT. This is probably an oversight as things have been developing fast in certain places and that block has not been the most used one. Hopefully, someone remembers to pass the list of variables it needs through all the layers at some point...

Also for [[debug/]] the list of vars it should display might as well be different inside PRTs than it is at question-text level. After all, it displays the values that were bound at the spot it was defined, which would make use of it with inline CASText somewhat interesting (hmm I wonder if it works at all, if not then it is the same issue as with PRTs)...