Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * H5P content type manager class
  19   *
  20   * @package    contenttype_h5p
  21   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace contenttype_h5p;
  26  
  27  use core\event\contentbank_content_viewed;
  28  use stdClass;
  29  use core_h5p\editor_ajax;
  30  use core_h5p\file_storage;
  31  use core_h5p\local\library\autoloader;
  32  use Moodle\H5PCore;
  33  
  34  /**
  35   * H5P content bank manager class
  36   *
  37   * @package    contenttype_h5p
  38   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class contenttype extends \core_contentbank\contenttype {
  42  
  43      /**
  44       * Delete this content from the content_bank and remove all the H5P related information.
  45       *
  46       * @param  content $content The content to delete.
  47       * @return boolean true if the content has been deleted; false otherwise.
  48       */
  49      public function delete_content(\core_contentbank\content $content): bool {
  50          // Delete the H5P content.
  51          $factory = new \core_h5p\factory();
  52          if (!empty($content->get_file_url())) {
  53              \core_h5p\api::delete_content_from_pluginfile_url($content->get_file_url(), $factory);
  54          }
  55  
  56          // Delete the content from the content_bank.
  57          return parent::delete_content($content);
  58      }
  59  
  60      /**
  61       * Returns the HTML content to add to view.php visualizer.
  62       *
  63       * @param  content $content The content to be displayed.
  64       * @return string            HTML code to include in view.php.
  65       */
  66      public function get_view_content(\core_contentbank\content $content): string {
  67          // Trigger an event for viewing this content.
  68          $event = contentbank_content_viewed::create_from_record($content->get_content());
  69          $event->trigger();
  70  
  71          $fileurl = $content->get_file_url();
  72          $html = \core_h5p\player::display($fileurl, new \stdClass(), true);
  73          return $html;
  74      }
  75  
  76      /**
  77       * Returns the HTML code to render the icon for H5P content types.
  78       *
  79       * @param  content $content The content to be displayed.
  80       * @return string            HTML code to render the icon
  81       */
  82      public function get_icon(\core_contentbank\content $content): string {
  83          global $OUTPUT, $DB;
  84  
  85          $iconurl = $OUTPUT->image_url('f/h5p-64', 'moodle')->out(false);
  86          $file = $content->get_file();
  87          if (!empty($file)) {
  88              $h5p = \core_h5p\api::get_content_from_pathnamehash($file->get_pathnamehash());
  89              if (!empty($h5p)) {
  90                  \core_h5p\local\library\autoloader::register();
  91                  if ($h5plib = $DB->get_record('h5p_libraries', ['id' => $h5p->mainlibraryid])) {
  92                      $h5pfilestorage = new \core_h5p\file_storage();
  93                      $h5picon = $h5pfilestorage->get_icon_url(
  94                              $h5plib->id,
  95                              $h5plib->machinename,
  96                              $h5plib->majorversion,
  97                              $h5plib->minorversion);
  98                      if (!empty($h5picon)) {
  99                          $iconurl = $h5picon;
 100                      }
 101                  }
 102              }
 103          }
 104          return $iconurl;
 105      }
 106  
 107      /**
 108       * Return an array of implemented features by this plugin.
 109       *
 110       * @return array
 111       */
 112      protected function get_implemented_features(): array {
 113          return [self::CAN_UPLOAD, self::CAN_EDIT, self::CAN_DOWNLOAD];
 114      }
 115  
 116      /**
 117       * Return an array of extensions this contenttype could manage.
 118       *
 119       * @return array
 120       */
 121      public function get_manageable_extensions(): array {
 122          return ['.h5p'];
 123      }
 124  
 125      /**
 126       * Returns user has access capability for the content itself.
 127       *
 128       * @return bool     True if content could be accessed. False otherwise.
 129       */
 130      protected function is_access_allowed(): bool {
 131          return true;
 132      }
 133  
 134      /**
 135       * Returns the list of different H5P content types the user can create.
 136       *
 137       * @return array An object for each H5P content type:
 138       *     - string typename: descriptive name of the H5P content type.
 139       *     - string typeeditorparams: params required by the H5P editor.
 140       *     - url typeicon: H5P content type icon.
 141       */
 142      public function get_contenttype_types(): array {
 143          // Get the H5P content types available.
 144          autoloader::register();
 145          $editorajax = new editor_ajax();
 146          $h5pcontenttypes = $editorajax->getLatestLibraryVersions();
 147  
 148          $types = [];
 149          $h5pfilestorage = new file_storage();
 150          foreach ($h5pcontenttypes as $h5pcontenttype) {
 151              $library = [
 152                  'name' => $h5pcontenttype->machine_name,
 153                  'majorVersion' => $h5pcontenttype->major_version,
 154                  'minorVersion' => $h5pcontenttype->minor_version,
 155              ];
 156              $key = H5PCore::libraryToString($library);
 157              $type = new stdClass();
 158              $type->key = $key;
 159              $type->typename = $h5pcontenttype->title;
 160              $type->typeeditorparams = 'library=' . $key;
 161              $type->typeicon = $h5pfilestorage->get_icon_url(
 162                  $h5pcontenttype->id,
 163                  $h5pcontenttype->machine_name,
 164                  $h5pcontenttype->major_version,
 165                  $h5pcontenttype->minor_version);
 166              $types[] = $type;
 167          }
 168  
 169          return $types;
 170      }
 171  }