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