Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  namespace mod_forum\h5p;
  18  
  19  /**
  20   * Class to check if the H5P content can be edited for this plugin.
  21   *
  22   * @package   mod_forum
  23   * @copyright 2021 Sara Arjona (sara@moodle.com)
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class canedit {
  27  
  28      /**
  29       * Check if the user can edit an H5P file. In that case, this method will return true if the file belongs to mod_forum
  30       * filearea is post and the user can edit the post where the H5P is.
  31       *
  32       * @param \stored_file $file The H5P file to check.
  33       *
  34       * @return boolean Whether the user can edit or not the given file.
  35       * @since Moodle 4.0
  36       */
  37      public static function can_edit_content(\stored_file $file): bool {
  38          global $USER;
  39  
  40          list($type, $component) = \core_component::normalize_component($file->get_component());
  41  
  42          if ($type === 'mod' && $component === 'forum') {
  43              // For mod_forum files in posts, check if the user can edit the post where the H5P is.
  44              if ($file->get_filearea() === 'post') {
  45                  // Check if the user can edit the forum post.
  46                  $vaultfactory = \mod_forum\local\container::get_vault_factory();
  47                  $forumvault = $vaultfactory->get_forum_vault();
  48                  $discussionvault = $vaultfactory->get_discussion_vault();
  49                  $postvault = $vaultfactory->get_post_vault();
  50                  $postid = $file->get_itemid();
  51                  $postentity = $postvault->get_from_id($postid);
  52                  if (!empty($postentity)) {
  53                      $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
  54                      $managerfactory = \mod_forum\local\container::get_manager_factory();
  55                      $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
  56                      $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
  57                      if ($capabilitymanager->can_edit_post($USER, $discussionentity, $postentity)) {
  58                          return true;
  59                      }
  60                  }
  61              } else {
  62                  // For any other fileare, check whether the user can add/edit them.
  63                  $context = \context::instance_by_id($file->get_contextid());
  64                  $plugins = \core_component::get_plugin_list($type);
  65                  $isvalid = array_key_exists($component, $plugins);
  66                  if ($isvalid && has_capability("$type/$component:addinstance", $context)) {
  67                      // The user can edit the content because she has the capability for creating instances where the file belongs.
  68                      return true;
  69                  }
  70              }
  71          }
  72  
  73          return false;
  74      }
  75  }