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.

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