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

Export plugin settings to global M variable for access in javascript

    XMLWordPrintable

Details

    • Improvement
    • Resolution: Unresolved
    • Minor
    • None
    • 4.2.1
    • JavaScript
    • None
    • Any
    • MOODLE_402_STABLE
    • Hide

      1. Install the mod_demoactivity plugin to your moodle installations mod directory:
          git clone git@github.com:kentuckyfriedsoftware/demoactivity.git mod/demoactivity
      2. Run the upgrade command to install the plugin:
          php admin/cli/upgrade.php
      3. Logon to your Moodle instance with an Admin account.
      4. Navigate to the plugin overview in the site administration. Find the mod_demoactivity plugin and click on "Settings".
      5. Adjust the settings "setting1" - "setting4" to your liking. The settings do not have functional meaning and will only be used for the demonstration of the feature.
      5. Create a new course or navigate to an existing course.
      6. Add the demoactivity activity to your course. Set name and description, then click on "Save and display".
      7. Notice the table displaying the plugin settings both from the backend (settings "Setting 1" - "Setting 4") and the 'M' variable (settings "M.mod_demoactivity.setting1" - "M.mod_demoactivity.setting4").
          Notice how the values are identical in the two columns.
          Open your web browsers development tools and check the values in the global 'M' variable manually to confirm the correctness.
      8. In your favorite text editor open the mod/demoactivity/view.php file. Notice that line 55 is commented. Uncomment line 55 and comment line 54. You are now passing an Array of desired settings to the js_call_amd() Method.
          In your Browser go back to the demoactivity Activity you added to your course. Reload the page. Notice how the values of settings "Setting 1" - "Setting 4" in the table remain the same.
          However, the values for the frontend settings "M.mod_demoactivity.setting2" - "M.mod_demoactivity.setting4" should change to 'undefined'. You can change the array of desired settings and observe the difference with every subsequent page refresh.
          Open your web browsers development tools and check the values in the global 'M' variable manually to confirm the correctness.

      Show
      1. Install the mod_demoactivity plugin to your moodle installations mod directory:     git clone git@github.com:kentuckyfriedsoftware/demoactivity.git mod/demoactivity 2. Run the upgrade command to install the plugin:     php admin/cli/upgrade.php 3. Logon to your Moodle instance with an Admin account. 4. Navigate to the plugin overview in the site administration. Find the mod_demoactivity plugin and click on "Settings". 5. Adjust the settings "setting1" - "setting4" to your liking. The settings do not have functional meaning and will only be used for the demonstration of the feature. 5. Create a new course or navigate to an existing course. 6. Add the demoactivity activity to your course. Set name and description, then click on "Save and display". 7. Notice the table displaying the plugin settings both from the backend (settings "Setting 1" - "Setting 4") and the 'M' variable (settings "M.mod_demoactivity.setting1" - "M.mod_demoactivity.setting4").     Notice how the values are identical in the two columns.     Open your web browsers development tools and check the values in the global 'M' variable manually to confirm the correctness. 8. In your favorite text editor open the mod/demoactivity/view.php file. Notice that line 55 is commented. Uncomment line 55 and comment line 54. You are now passing an Array of desired settings to the js_call_amd() Method.     In your Browser go back to the demoactivity Activity you added to your course. Reload the page. Notice how the values of settings "Setting 1" - "Setting 4" in the table remain the same.     However, the values for the frontend settings "M.mod_demoactivity.setting2" - "M.mod_demoactivity.setting4" should change to 'undefined'. You can change the array of desired settings and observe the difference with every subsequent page refresh.     Open your web browsers development tools and check the values in the global 'M' variable manually to confirm the correctness.

    Description

      This idea was pitched at Moodlemoot DACH 2023 and implemented by Florian Reiter and Annika Lambert by modifying the page_requirements_manager.

      When developping javascript for your Moodle plugin passing your plugins configuration settings to javascript is rather copious. The idea is to be able to export all desired settings to the global M variable with one call from the server side code. This way javascript development becomes more convenient and easier.

      Attachments

        Activity

          People

            Unassigned Unassigned
            florian.reiter Florian Reiter
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Clockify

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