Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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