Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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_h5pactivity\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_single_structure;
  22  use core_external\external_value;
  23  use core_external\external_warnings;
  24  use context_module;
  25  use mod_h5pactivity\local\manager;
  26  
  27  /**
  28   * This is the external method for getting access information for a h5p activity.
  29   *
  30   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class get_h5pactivity_access_information extends external_api {
  34      /**
  35       * Parameters.
  36       *
  37       * @return external_function_parameters
  38       */
  39      public static function execute_parameters(): external_function_parameters {
  40          return new external_function_parameters(
  41              [
  42                  'h5pactivityid' => new external_value(PARAM_INT, 'h5p activity instance id')
  43              ]
  44          );
  45      }
  46  
  47      /**
  48       * Return access information for a given h5p activity.
  49       *
  50       * @param  int $h5pactivityid The h5p activity id.
  51       * @return array of warnings and the access information
  52       * @since Moodle 3.9
  53       * @throws  moodle_exception
  54       */
  55      public static function execute(int $h5pactivityid): array {
  56          global $DB;
  57  
  58          $params = external_api::validate_parameters(self::execute_parameters(), [
  59              'h5pactivityid' => $h5pactivityid
  60          ]);
  61  
  62          // Request and permission validation.
  63          $h5pactivity = $DB->get_record('h5pactivity', ['id' => $params['h5pactivityid']], '*', MUST_EXIST);
  64  
  65          list($course, $cm) = get_course_and_cm_from_instance($h5pactivity, 'h5pactivity');
  66  
  67          $context = context_module::instance($cm->id);
  68          self::validate_context($context);
  69  
  70          $result = [];
  71          // Return all the available capabilities.
  72          $manager = manager::create_from_coursemodule($cm);
  73          $capabilities = load_capability_def('mod_h5pactivity');
  74          foreach ($capabilities as $capname => $capdata) {
  75              $field = 'can' . str_replace('mod/h5pactivity:', '', $capname);
  76              // For mod/h5pactivity:submit we need to check if tracking is enabled in the h5pactivity for the current user.
  77              if ($field == 'cansubmit') {
  78                  $result[$field] = $manager->is_tracking_enabled();
  79              } else {
  80                  $result[$field] = has_capability($capname, $context);
  81              }
  82          }
  83  
  84          $result['warnings'] = [];
  85          return $result;
  86      }
  87  
  88      /**
  89       * Describes the get_h5pactivity_access_information return value.
  90       *
  91       * @return external_single_structure
  92       * @since Moodle 3.9
  93       */
  94      public static function execute_returns() {
  95  
  96          $structure = [
  97              'warnings' => new external_warnings()
  98          ];
  99  
 100          $capabilities = load_capability_def('mod_h5pactivity');
 101          foreach ($capabilities as $capname => $capdata) {
 102              $field = 'can' . str_replace('mod/h5pactivity:', '', $capname);
 103              $structure[$field] = new external_value(PARAM_BOOL, 'Whether the user has the capability ' . $capname . ' allowed.',
 104                  VALUE_OPTIONAL);
 105          }
 106  
 107          return new external_single_structure($structure);
 108      }
 109  }