-
Improvement
-
Resolution: Fixed
-
Minor
-
3.9, 4.0
-
MOODLE_39_STABLE, MOODLE_400_STABLE
-
MOODLE_402_STABLE
-
MDL-69164-master -
The question/qengine.js is too old and we need to convert it to an AMD module. After checking the new code from question/bank/previewquestion/amd/src/preview.js, I think that question engine should be a general amd module instead of a part of preview.js, because it is using on other places such as quiz attempt.
I have created 2 modules to split qengine.js to 2 separate part:
1. lib/amd/src/scroll_manager.js:
The scroll function in qengine.js has not working for along time. The root cause it the theme changing. The windows is not the current scrolling element anymore. I have fixed this feature to make it work again for both Boost and Clean theme.
As Tim suggestion, I have remove the scrollPos parameter and using a generic code to add it in redirect()
I have remove the save_scroll_action(), and replace with a new function: initLinksScrollPos(). If a link has 'data-save-scroll' attribute = true, when we click on the link, the js code will add mdlscrollto parameters with the current scroll position into the link and open the link.
I have added the scroll function to the "Try another question like this one" button.
2. question/amd/src/question_engine.js
I have remove the hashtag in form action in init_submit_button() to make the scroll smoother.
I have checked the usage of the old code across the site. It has removed some where because it has been working for along time. I just found some pages to test the old function:
Quiz attempt
Preview question
Other question/quiz pages should work normally.
Original Description:
This is some old JavaScript in the question engine.
I think that the M.core_scroll_manager.save_scroll_action bit is generally useful so should be converted to a module like core/scroll_manager. Looks like there is scope to improve the code there, for example:
- Rather relying on an event handler being added to individual links and buttons, perhaps make it use a data attribute (e.g. data-save-scroll) and have a single delegated event handler on body.
- Get rid of the "Assumes that the link already contains at least one URL parameter." limitaion.
- Hopefully it is now save to remove the IE-specific hack (but this needs to be tested).
Then, M.core_question_engine should become an AMD module in core_question.
Just to note that current best practice for JavaScript is:
- ES6 module syntax
- No third-party JS libraries at all unless really needed.
Apparently https://github.com/moodle/moodle/blob/master/lib/amd/src/toast.js is good recent example.
I think that the historic entry-point into this code are the API
- page->requires->js_module('core_question_engine');
- page->requires->js_init_call('M.core_question_engine.init_submit_button',
array($attributes['id'], $qa->get_slot()));
And https://docs.moodle.org/dev/Using_the_question_engine_from_module#Handling_scrollpos
We need to work out how the normal Moodle deprecation rules apply to this.