maths / moodle-qtype_stack

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

Feature request: input type metadata present as data-attributes in the actual input element #1235

Open aharjula opened 1 month ago

aharjula commented 1 month ago

Those pesky continental commas are popping up everywhere and cause trouble (this overly positive statement coming from a person from one of those countries officially requiring commas but in practice using whatever is available) when one wants to script things with inputs that expect a different syntax depending on the separator setting. This makes it difficult to share code over materials spanning locales as the code cannot self-adapt to that change in syntax due to not being able to see if such a difference is in play. We need to make that expectation of a comma visible...

So could we start adding an overabundance of metadata to our input elements to describe all sorts of things to those that later programmatically have to work with those elements? By metadata, I primarily mean an attribute telling what the decimal separator is and what is the list separator, but we might as well start telling all sorts of other details like the input type (although already present as the CSS-class).

Basically, could we simply add stuff here:

https://github.com/maths/moodle-qtype_stack/blob/f7a400e32db9c81647358ea21abd84390dee6c88/stack/input/algebraic/algebraic.class.php#L39-L82

and agree on the names of the attributes we add and strive to add them to all input types, both current and future, when relevant.

For now, I would suggest this addition, obviously shorter names are an option as well:

        $attributes['data-stack-input-type'] = 'algebraic';
        if ($this->options->get_option('decimals') === ',') {
            $attributes['data-stack-input-decimal-separator']  = ',';
            $attributes['data-stack-input-list-separator'] = ';';
        } else {
            $attributes['data-stack-input-decimal-separator']  = '.';
            $attributes['data-stack-input-list-separator'] = ',';
        }

One could even add an attribute signalling that this input forbids floats... Thus any large library that happens to want to use floats could detect the misconfiguration and alert about it. But maybe that would be a waste of resources. Likewise, printing out the forbidden words setting is unlikely to be of use unless someone attaches some graphical equation editor on top of the input and could use those settings to autoconfigure its palettes of options to match the expected/allowed ones...

After that STACK-JS could be modified so that all inputs being relayed through it will also relay those attributes so that the scripts at the other end can adjust to the active separators. This should help with #1070, and for those using stack_jxg basic bindings we could even make the whole thing automatic, for custom bindings some tooling could be provided to help for converting lists of floats between syntaxes.

georgekinnear commented 1 month ago

I think this seems like a nice idea. Recently I found myself wanting to check the type of an input through STACK-JS, but then I realised I did not have any way to access the CSS class.

aharjula commented 1 month ago

Well, I'll start making this, and it will bring data-stack-input-type over STACK-JS to the iframe instead of the class just because I consider the class attribute more of a presentation thing and the input-type itself more logical and thus more likely to match on other VLEs.