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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  namespace core_h5p\output;
  18  
  19  use plugin_renderer_base;
  20  
  21  /**
  22   * Renderer class.
  23   *
  24   * @package    core_h5p
  25   * @copyright  2020 Victor Deniz {victor@moodle.com}
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class renderer extends plugin_renderer_base {
  29  
  30      /**
  31       * Alter which stylesheets are loaded for H5P.
  32       * This is useful for adding custom styles or replacing existing ones.
  33       *
  34       * This method can be overridden by other themes if the styles must be loaded from
  35       * a different place than the "Raw initial SCSS" and "Raw SCSS" theme settings.
  36       *
  37       * @param \stdClass[] $styles List of stylesheets that will be loaded
  38       * @param array $libraries Array of libraries indexed by the library's machineName
  39       * @param string $embedtype Possible values: div, iframe, external, editor
  40       */
  41      public function h5p_alter_styles(&$styles, array $libraries, string $embedtype) {
  42          global $CFG, $DB;
  43  
  44          $record = [
  45              'contextid' => \context_system::instance()->id,
  46              'component' => \core_h5p\file_storage::COMPONENT,
  47              'filearea' => \core_h5p\file_storage::CSS_FILEAREA,
  48              'itemid' => 0,
  49              'filepath' => '/',
  50              'filename' => $CFG->theme . '_h5p.css',
  51          ];
  52          $fs = get_file_storage();
  53          // Check if the CSS file for the current theme needs to be updated (because the SCSS settings have changed recently).
  54          if ($cssfile = $fs->get_file(
  55                  $record['contextid'],
  56                  $record['component'],
  57                  $record['filearea'],
  58                  $record['itemid'],
  59                  $record['filepath'],
  60                  $record['filename'])) {
  61              // Get the last time when the SCSS and CSSPRE settings were updated for the current theme and compare it with the
  62              // time modified of the H5P CSS file, to determine whether it needs to be updated.
  63              $sql = "SELECT MAX(timemodified) as timemodified
  64                        FROM {config_log}
  65                       WHERE plugin = :theme AND (name = 'scss' OR name = 'scsspre')";
  66              $params = ['theme' => 'theme_' . $CFG->theme];
  67              $setting = $DB->get_record_sql($sql, $params);
  68              if ($setting && $setting->timemodified > $cssfile->get_timemodified()) {
  69                  // The CSS file needs to be updated. First, delete it to recreate it later with the current CSS.
  70                  $cssfile->delete();
  71                  $cssfile = null;
  72              }
  73          }
  74  
  75          $theme = \theme_config::load($CFG->theme);
  76          // When 'Raw initial SCSS' and 'Raw SCSS' theme settings are empty, the file doesn't need to be created.
  77          if (empty($theme->settings->scsspre) && empty($theme->settings->scss)) {
  78              return;
  79          }
  80  
  81          // If the CSS file doesn't exist, create it with the styles defined in 'Raw initial SCSS' and 'Raw SCSS' theme settings.
  82          // As these scss and scsspre settings might have dependencies on the theme, the whole CSS theme content will be used and
  83          // passed to the H5P player.
  84          if (!$cssfile) {
  85              $css = $theme->get_css_content();
  86              $cssfile = $fs->create_file_from_string($record, $css);
  87          }
  88  
  89          $cssurl = \moodle_url::make_pluginfile_url(
  90              $record['contextid'],
  91              $record['component'],
  92              $record['filearea'],
  93              null,
  94              $record['filepath'],
  95              $record['filename']
  96          );
  97  
  98          // Add the CSS file to the styles array, to load it from the H5P player.
  99          $styles[] = (object) [
 100              'path' => $cssurl->out(),
 101              'version' => '?ver='.$cssfile->get_timemodified(),
 102          ];
 103      }
 104  
 105      /**
 106       * Alter which scripts are loaded for H5P.
 107       * This is useful for adding custom scripts or replacing existing ones.
 108       *
 109       * @param array|object $scripts List of JavaScripts that will be loaded
 110       * @param array $libraries Array of libraries indexed by the library's machineName
 111       * @param string $embedtype Possible values: div, iframe, external, editor
 112       */
 113      public function h5p_alter_scripts(&$scripts, array $libraries, string $embedtype) {
 114      }
 115  
 116      /**
 117       * Alter semantics before they are processed. This is useful for changing
 118       * how the editor looks and how content parameters are filtered.
 119       *
 120       * @param object|object $semantics Semantics as object
 121       * @param string $name Machine name of library
 122       * @param int $majorversion Major version of library
 123       * @param int $minorversion Minor version of library
 124       */
 125      public function h5p_alter_semantics(&$semantics, $name, $majorversion, $minorversion) {
 126      }
 127  
 128      /**
 129       * Alter parameters of H5P content after it has been filtered through semantics.
 130       * This is useful for adapting the content to the current context.
 131       *
 132       * @param array|object $parameters The content parameters for the library
 133       * @param string $name The machine readable name of the library
 134       * @param int $majorversion Major version of the library
 135       * @param int $minorversion Minor version of the library
 136       */
 137      public function h5p_alter_filtered_parameters(&$parameters, string $name, int $majorversion, int $minorversion) {
 138      }
 139  }