MDL-68991 core: delete core_get_userfeedback_url webservice
[moodle.git] / lib / classes / external / record_userfeedback_action.php
CommitLineData
1a4c8e02
SR
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
ce80716a 25namespace core\external;
1a4c8e02
SR
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once("$CFG->libdir/externallib.php");
30
31use external_api;
32use external_function_parameters;
33use 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 */
ce80716a 41class record_userfeedback_action extends external_api {
1a4c8e02
SR
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'),
363aaf6e 50 'contextid' => new external_value(PARAM_INT, 'The context id of the page the user is in'),
1a4c8e02
SR
51 ]);
52 }
53
54 /**
55 * Record users action to the feedback CTA
56 *
57 * @param string $action The action the user took
363aaf6e 58 * @param int $contextid The context id
1a4c8e02
SR
59 * @throws \invalid_parameter_exception
60 */
363aaf6e
SR
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);
1a4c8e02
SR
69
70 switch ($action) {
71 case 'give':
72 set_user_preference('core_userfeedback_give', time());
363aaf6e
SR
73 $event = \core\event\userfeedback_give::create(['context' => $context]);
74 $event->trigger();
1a4c8e02
SR
75 break;
76 case 'remind':
77 set_user_preference('core_userfeedback_remind', time());
363aaf6e
SR
78 $event = \core\event\userfeedback_remind::create(['context' => $context]);
79 $event->trigger();
1a4c8e02
SR
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}