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_xapi\external; 18 19 use core_xapi\local\state; 20 use core_xapi\local\statement\item_activity; 21 use core_xapi\handler; 22 use core_xapi\xapi_exception; 23 use core_external\external_api; 24 use core_external\external_function_parameters; 25 use core_external\external_value; 26 use core_xapi\iri; 27 28 /** 29 * This is the external API for generic xAPI state post. 30 * 31 * @package core_xapi 32 * @since Moodle 4.2 33 * @copyright 2023 Ferran Recio 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class post_state extends external_api { 37 38 use \core_xapi\local\helper\state_trait; 39 40 /** 41 * Parameters for execute 42 * 43 * @return external_function_parameters 44 */ 45 public static function execute_parameters(): external_function_parameters { 46 return new external_function_parameters([ 47 'component' => new external_value(PARAM_COMPONENT, 'Component name'), 48 'activityId' => new external_value(PARAM_URL, 'xAPI activity ID IRI'), 49 'agent' => new external_value(PARAM_RAW, 'The xAPI agent json'), 50 'stateId' => new external_value(PARAM_ALPHAEXT, 'The xAPI state ID'), 51 'stateData' => new external_value(PARAM_RAW, 'JSON object with the state data'), 52 'registration' => new external_value(PARAM_ALPHANUMEXT, 'The xAPI registration UUID', VALUE_DEFAULT, null), 53 ]); 54 } 55 56 /** 57 * Process a state post request. 58 * 59 * @param string $component The component name in frankenstyle. 60 * @param string $activityiri The activity IRI. 61 * @param string $agent The agent JSON. 62 * @param string $stateid The xAPI state id. 63 * @param string $statedata JSON object with the state data 64 * @param string|null $registration The xAPI registration UUID. 65 * @return bool 66 */ 67 public static function execute( 68 string $component, 69 string $activityiri, 70 string $agent, 71 string $stateid, 72 string $statedata, 73 ?string $registration = null 74 ): bool { 75 76 $params = self::validate_parameters(self::execute_parameters(), [ 77 'component' => $component, 78 'activityId' => $activityiri, 79 'agent' => $agent, 80 'stateId' => $stateid, 81 'stateData' => $statedata, 82 'registration' => $registration, 83 ]); 84 [ 85 'component' => $component, 86 'activityId' => $activityiri, 87 'agent' => $agent, 88 'stateId' => $stateid, 89 'stateData' => $statedata, 90 'registration' => $registration, 91 ] = $params; 92 93 static::validate_component($component); 94 95 $handler = handler::create($component); 96 $activityid = iri::extract($activityiri, 'activity'); 97 98 $state = new state( 99 self::get_agent_from_json($agent), 100 item_activity::create_from_id($activityid), 101 $stateid, 102 self::get_statedata_from_json($statedata), 103 $registration 104 ); 105 106 if (!self::check_state_user($state)) { 107 throw new xapi_exception('State agent is not the current user'); 108 } 109 110 return $handler->save_state($state); 111 } 112 113 /** 114 * Return for execute. 115 */ 116 public static function execute_returns(): external_value { 117 return new external_value(PARAM_BOOL, 'If the state is accepted'); 118 } 119 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body