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 core\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_value;
  22  
  23  /**
  24   * The external API to record users action on the feedback notification.
  25   *
  26   * @package    core
  27   * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class record_userfeedback_action extends external_api {
  31      /**
  32       * Returns description of parameters.
  33       *
  34       * @return external_function_parameters
  35       */
  36      public static function execute_parameters() {
  37          return new external_function_parameters([
  38              'action' => new external_value(PARAM_ALPHA, 'The action taken by user'),
  39              'contextid' => new external_value(PARAM_INT, 'The context id of the page the user is in'),
  40          ]);
  41      }
  42  
  43      /**
  44       * Record users action to the feedback CTA
  45       *
  46       * @param string $action The action the user took
  47       * @param int $contextid The context id
  48       * @throws \invalid_parameter_exception
  49       */
  50      public static function execute(string $action, int $contextid) {
  51          external_api::validate_parameters(self::execute_parameters(), [
  52              'action' => $action,
  53              'contextid' => $contextid,
  54          ]);
  55  
  56          $context = \context::instance_by_id($contextid);
  57          self::validate_context($context);
  58  
  59          switch ($action) {
  60              case 'give':
  61                  set_user_preference('core_userfeedback_give', time());
  62                  $event = \core\event\userfeedback_give::create(['context' => $context]);
  63                  $event->trigger();
  64                  break;
  65              case 'remind':
  66                  set_user_preference('core_userfeedback_remind', time());
  67                  $event = \core\event\userfeedback_remind::create(['context' => $context]);
  68                  $event->trigger();
  69                  break;
  70              default:
  71                  throw new \invalid_parameter_exception('Invalid value for action parameter (value: ' . $action . '),' .
  72                          'allowed values are: give,remind');
  73          }
  74      }
  75  
  76      /**
  77       * Returns description of method result value
  78       *
  79       * @return null
  80       */
  81      public static function execute_returns() {
  82          return null;
  83      }
  84  }