Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Library functions to facilitate the use of JavaScript in Moodle.
  19   *
  20   * @copyright 2016 Adrian Greeve <adrian@moodle.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core
  23   * @category output
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * This requirements manager captures the appropriate html for creating a fragment to
  30   * be inserted elsewhere.
  31   *
  32   * @copyright 2016 Adrian Greeve <adrian@moodle.com>
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @since Moodle 3.1
  35   * @package core
  36   * @category output
  37   */
  38  class fragment_requirements_manager extends page_requirements_manager {
  39  
  40      /**
  41       * Page fragment constructor.
  42       */
  43      public function __construct() {
  44          parent::__construct();
  45          // As this is a fragment the header should already be done.
  46          $this->headdone = true;
  47      }
  48  
  49      /**
  50       * Returns js code to load amd module loader, then insert inline script tags
  51       * that contain require() calls using RequireJS.
  52       *
  53       * @return string
  54       */
  55      protected function get_amd_footercode() {
  56          global $CFG;
  57          $output = '';
  58  
  59          // First include must be to a module with no dependencies, this prevents multiple requests.
  60          $prefix = 'M.util.js_pending("core/first");';
  61          $prefix .= "require(['core/first'], function() {\n";
  62          $suffix = "\n});";
  63          $suffix .= 'M.util.js_complete("core/first");';
  64          $output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
  65          return $output;
  66      }
  67  
  68  
  69      /**
  70       * Generate any HTML that needs to go at the end of the page.
  71       *
  72       * @return string the HTML code to to at the end of the page.
  73       */
  74      public function get_end_code() {
  75          global $CFG;
  76  
  77          $output = '';
  78  
  79          // Call amd init functions.
  80          $output .= $this->get_amd_footercode();
  81  
  82          // Add other requested modules.
  83          $output .= $this->get_extra_modules_code();
  84  
  85          // All the other linked scripts - there should be as few as possible.
  86          if ($this->jsincludes['footer']) {
  87              foreach ($this->jsincludes['footer'] as $url) {
  88                  $output .= html_writer::script('', $url);
  89              }
  90          }
  91  
  92          if (!empty($this->stringsforjs)) {
  93              // Add all needed strings.
  94              $strings = array();
  95              foreach ($this->stringsforjs as $component => $v) {
  96                  foreach ($v as $indentifier => $langstring) {
  97                      $strings[$component][$indentifier] = $langstring->out();
  98                  }
  99              }
 100              // Append don't overwrite.
 101              $output .= html_writer::script('require(["jquery"], function($) {
 102                  M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
 103              });');
 104          }
 105  
 106          // Add variables.
 107          if ($this->jsinitvariables['footer']) {
 108              $js = '';
 109              foreach ($this->jsinitvariables['footer'] as $data) {
 110                  list($var, $value) = $data;
 111                  $js .= js_writer::set_variable($var, $value, true);
 112              }
 113              $output .= html_writer::script($js);
 114          }
 115  
 116          $inyuijs = $this->get_javascript_code(false);
 117          $ondomreadyjs = $this->get_javascript_code(true);
 118          // See if this is still needed when we get to the ajax page.
 119          $jsinit = $this->get_javascript_init_code();
 120          $handlersjs = $this->get_event_handler_code();
 121  
 122          // There is a global Y, make sure it is available in your scope.
 123          $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
 124  
 125          $output .= html_writer::script($js);
 126  
 127          return $output;
 128      }
 129  }