Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-69164

Convert question/qengine.js to AMD modules

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • 4.2
    • 3.9, 4.0
    • Questions
    • MOODLE_39_STABLE, MOODLE_400_STABLE
    • MOODLE_402_STABLE
    • MDL-69164-master
    • Hide

      We have to test the scroll function in both Boost and Clean theme:

      Question preview:

      1. Create a question in question bank
      2. Preview question
      3. In the preview question window, scroll to any position, then click on 'check' button
      4. When the question is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position.

      Quiz attempt:

      1. Create a quiz with 2 or more question in a page
      2. Edit quiz setting and turn on "Allow redo within an attempt" option
         
      3. Attempt the quiz
      4. In the attempt page, scroll to the 2nd question, then click on 'check' button
      5. When the page is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position.
      6. Click on the "Try another question like this one" button
      7. When the page is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position.

      Other quiz and question function:
      Smoke test around the quiz pages and question pages to make sure other function should work normally.

      Show
      We have to test the scroll function in both Boost and Clean theme: Question preview: Create a question in question bank Preview question In the preview question window, scroll to any position, then click on 'check' button When the question is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position. Quiz attempt: Create a quiz with 2 or more question in a page Edit quiz setting and turn on "Allow redo within an attempt" option   Attempt the quiz In the attempt page, scroll to the 2nd question, then click on 'check' button When the page is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position. Click on the "Try another question like this one" button When the page is reloaded, the url should contains mdlscrollto parameter and the window should scroll to the previous position. Other quiz and question function: Smoke test around the quiz pages and question pages to make sure other function should work normally.

      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:

      1. 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.
      2. Get rid of the "Assumes that the link already contains at least one URL parameter." limitaion.
      3. 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.

            jbthong Thong Bui
            timhunt Tim Hunt
            Tim Hunt Tim Hunt
            Andrew Lyons Andrew Lyons
            Kim Jared Lucas Kim Jared Lucas
            Votes:
            1 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 day, 2 hours, 39 minutes
                1d 2h 39m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.