Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/h5p/ -> libraries.php (source)

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

   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   * Manage H5P libraries settings page.
  19   *
  20   * @package    core_h5p
  21   * @copyright  2019 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../config.php');
  26  
  27  require_login(null, false);
  28  
  29  $deletelibrary = optional_param('deletelibrary', null, PARAM_INT);
  30  $confirm = optional_param('confirm', false, PARAM_BOOL);
  31  $action = optional_param('action', null, PARAM_ALPHANUMEXT);
  32  
  33  $context = context_system::instance();
  34  require_capability('moodle/h5p:updatelibraries', $context);
  35  
  36  $pagetitle = get_string('h5pmanage', 'core_h5p');
  37  $url = new \moodle_url("/h5p/libraries.php");
  38  
  39  $PAGE->set_context($context);
  40  $PAGE->set_url($url);
  41  $PAGE->set_pagelayout('admin');
  42  $PAGE->set_title("$SITE->shortname: " . $pagetitle);
  43  $PAGE->set_heading($SITE->fullname);
  44  
  45  $h5pfactory = new \core_h5p\factory();
  46  if ($deletelibrary) {
  47      $library = \core_h5p\api::get_library($deletelibrary);
  48      if ($confirm) {
  49          require_sesskey();
  50          \core_h5p\api::delete_library($h5pfactory, $library);
  51          redirect(new moodle_url('/h5p/libraries.php'));
  52      }
  53  
  54      echo $OUTPUT->header();
  55      echo $OUTPUT->heading(get_string('deleting', 'core_h5p'));
  56      echo $OUTPUT->confirm(
  57          get_string('deletelibraryconfirm', 'core_h5p', [
  58              'name' => format_string($library->title),
  59              'version' => format_string($library->majorversion . '.' . $library->minorversion . '.' . $library->patchversion),
  60          ]),
  61          new moodle_url($PAGE->url, ['deletelibrary' => $deletelibrary, 'confirm' => 1]),
  62          new moodle_url('/h5p/libraries.php')
  63      );
  64      echo $OUTPUT->footer();
  65      die();
  66  }
  67  
  68  if (!is_null($action)) {
  69      require_sesskey();
  70  
  71      if ($action == 'enable' || $action == 'disable') {
  72          // If action is enable or disable, library id is required too.
  73          $libraryid = required_param('id', PARAM_INT);
  74  
  75          \core_h5p\api::set_library_enabled($libraryid, ($action == 'enable'));
  76      }
  77  }
  78  
  79  echo $OUTPUT->header();
  80  echo $OUTPUT->heading($pagetitle);
  81  echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));
  82  
  83  $form = new \core_h5p\form\uploadlibraries_form();
  84  if ($data = $form->get_data()) {
  85      require_sesskey();
  86  
  87      // Get the file from the users draft area.
  88      $usercontext = context_user::instance($USER->id);
  89      $fs = get_file_storage();
  90      $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->h5ppackage, 'id',
  91          false);
  92      $file = reset($files);
  93  
  94      // Validate and save the H5P package.
  95      // Because we are passing skipcontent = true to save_h5p function, the returning value is false if an error
  96      // is encountered, null when successfully saving the package without creating the content.
  97      if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
  98          echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
  99      } else {
 100          echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
 101      }
 102  }
 103  $form->display();
 104  
 105  // Load installed Libraries.
 106  $framework = $h5pfactory->get_framework();
 107  $libraries = $framework->loadLibraries();
 108  
 109  if (!empty($libraries)) {
 110      $libs = new \core_h5p\output\libraries($h5pfactory, $libraries);
 111      echo $OUTPUT->render_from_template('core_h5p/h5plibraries', $libs->export_for_template($OUTPUT));
 112  }
 113  
 114  echo $OUTPUT->footer();