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.
/h5p/ -> edit.php (source)
   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   * Open the editor to modify an H5P content from a given H5P URL.
  19   *
  20   * @package    core_h5p
  21   * @copyright  2021 Sara Arjona <sara@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  require_once("$CFG->libdir/formslib.php");
  27  require_once("$CFG->libdir/filestorage/file_storage.php");
  28  
  29  require_login(null, false);
  30  
  31  $contenturl = required_param('url', PARAM_LOCALURL);
  32  $returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
  33  
  34  // If no returnurl is defined, use local_referer.
  35  if (empty($returnurl)) {
  36      $returnurl = get_local_referer(false);
  37      if (empty($returnurl)) {
  38          // If local referer is empty, returnurl will be set to default site page.
  39          $returnurl = new \moodle_url('/');
  40      }
  41  }
  42  
  43  $contentid = null;
  44  $isreferenced = false;
  45  $context = \context_system::instance();
  46  if (!empty($contenturl)) {
  47      list($originalfile, $h5p, $file) = \core_h5p\api::get_original_content_from_pluginfile_url($contenturl);
  48      $isreferenced = ($file !== false);
  49      if ($originalfile) {
  50          // Check if the user can edit the content behind the given URL.
  51          if (\core_h5p\api::can_edit_content($originalfile)) {
  52              if (!$h5p) {
  53                  // This H5P file hasn't been deployed yet, so it should be saved to create the entry into the H5P DB.
  54                  \core_h5p\local\library\autoloader::register();
  55                  $factory = new \core_h5p\factory();
  56                  $config = new \stdClass();
  57                  $onlyupdatelibs = !\core_h5p\helper::can_update_library($originalfile);
  58                  $contentid = \core_h5p\helper::save_h5p($factory, $originalfile, $config, $onlyupdatelibs, false);
  59              } else {
  60                  // The H5P content exists. Update the contentid value.
  61                  $contentid = $h5p->id;
  62              }
  63          }
  64          if ($file) {
  65              list($context, $course, $cm) = get_context_info_array($file->get_contextid());
  66              if ($course) {
  67                  $context = \context_course::instance($course->id);
  68              }
  69          } else {
  70              list($context, $course, $cm) = get_context_info_array($originalfile->get_contextid());
  71              if ($course) {
  72                  $context = \context_course::instance($course->id);
  73              }
  74          }
  75      }
  76  }
  77  
  78  if (empty($contentid)) {
  79      throw new \moodle_exception('error:emptycontentid', 'core_h5p', $returnurl);
  80  }
  81  
  82  $pagetitle = get_string('h5peditor', 'core_h5p');
  83  $url = new \moodle_url("/h5p/edit.php");
  84  
  85  $PAGE->set_context($context);
  86  $PAGE->set_url($url);
  87  $PAGE->set_title($pagetitle);
  88  $PAGE->set_heading($pagetitle);
  89  $PAGE->set_pagelayout('course');
  90  
  91  $values = [
  92      'id' => $contentid,
  93      'contenturl' => $contenturl,
  94      'returnurl' => $returnurl,
  95  ];
  96  
  97  $form = new \core_h5p\form\editcontent_form(null, $values);
  98  if ($form->is_cancelled()) {
  99      redirect($returnurl);
 100  } else if ($data = $form->get_data()) {
 101      $form->save_h5p($data);
 102      if (!empty($returnurl)) {
 103          redirect($returnurl);
 104      }
 105  }
 106  
 107  echo $OUTPUT->header();
 108  
 109  if ($isreferenced) {
 110      echo $OUTPUT->notification(get_string('contentinuse', 'core_h5p'), 'info');
 111  }
 112  
 113  $form->display();
 114  
 115  echo $OUTPUT->footer();