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 311 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 mod_h5pactivity\local\manager;
  25  use mod_h5pactivity\event\report_viewed;
  26  use context_module;
  27  use stdClass;
  28  
  29  /**
  30   * This is the external method for logging that the h5pactivity was viewed.
  31   *
  32   * @package    mod_h5pactivity
  33   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 3.11
  36   */
  37  class log_report_viewed extends external_api {
  38  
  39      /**
  40       * Webservice parameters.
  41       *
  42       * @return external_function_parameters
  43       */
  44      public static function execute_parameters(): external_function_parameters {
  45          return new external_function_parameters(
  46              [
  47                  'h5pactivityid' => new external_value(PARAM_INT, 'h5p activity instance id'),
  48                  'userid' => new external_value(
  49                      PARAM_INT,
  50                      'The user id to log attempt (null means only current user)',
  51                      VALUE_DEFAULT
  52                  ),
  53                  'attemptid' => new external_value(PARAM_INT, 'The attempt id', VALUE_DEFAULT),
  54              ]
  55          );
  56      }
  57  
  58      /**
  59       * Logs that the h5pactivity was viewed.
  60       *
  61       * @throws  moodle_exception if the user cannot see the report
  62       * @param  int $h5pactivityid The h5p activity id
  63       * @param  int|null $userid The user id
  64       * @param  int|null $attemptid The attempt id
  65       * @return array of warnings and status result
  66       */
  67      public static function execute(int $h5pactivityid, int $userid = null, int $attemptid = null): stdClass {
  68          $params = external_api::validate_parameters(self::execute_parameters(), [
  69              'h5pactivityid' => $h5pactivityid,
  70              'userid' => $userid,
  71              'attemptid' => $attemptid,
  72          ]);
  73          $h5pactivityid = $params['h5pactivityid'];
  74          $userid = $params['userid'];
  75          $attemptid = $params['attemptid'];
  76  
  77          // Request and permission validation.
  78          list ($course, $cm) = get_course_and_cm_from_instance($h5pactivityid, 'h5pactivity');
  79  
  80          $context = context_module::instance($cm->id);
  81          self::validate_context($context);
  82  
  83          $manager = manager::create_from_coursemodule($cm);
  84  
  85          $instance = $manager->get_instance();
  86  
  87          // Trigger event.
  88          $other = [
  89              'instanceid' => $instance->id,
  90              'userid' => $userid,
  91              'attemptid' => $attemptid,
  92          ];
  93          $event = report_viewed::create([
  94              'objectid' => $instance->id,
  95              'context' => $context,
  96              'other' => $other,
  97          ]);
  98          $event->trigger();
  99  
 100          $result = (object)[
 101              'status' => true,
 102              'warnings' => [],
 103          ];
 104  
 105          return $result;
 106      }
 107  
 108      /**
 109       * Describes the report_viewed return value.
 110       *
 111       * @return external_single_structure
 112       * @since Moodle 3.11
 113       */
 114      public static function execute_returns() {
 115          return new external_single_structure(
 116              [
 117                  'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 118                  'warnings' => new external_warnings()
 119              ]
 120          );
 121      }
 122  }