| 1 | 
           efrain | 
           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 deletion.
  | 
        
        
            | 
            | 
           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 delete_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 | 
                       'registration' => new external_value(PARAM_ALPHANUMEXT, 'The xAPI registration UUID', VALUE_DEFAULT, null),
  | 
        
        
            | 
            | 
           52 | 
                   ]);
  | 
        
        
            | 
            | 
           53 | 
               }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               /**
  | 
        
        
            | 
            | 
           56 | 
                * Process a state delete request.
  | 
        
        
            | 
            | 
           57 | 
                *
  | 
        
        
            | 
            | 
           58 | 
                * @param string $component The component name in frankenstyle.
  | 
        
        
            | 
            | 
           59 | 
                * @param string $activityiri The activity IRI.
  | 
        
        
            | 
            | 
           60 | 
                * @param string $agent The agent JSON.
  | 
        
        
            | 
            | 
           61 | 
                * @param string $stateid The xAPI state id.
  | 
        
        
            | 
            | 
           62 | 
                * @param string|null $registration The xAPI registration UUID.
  | 
        
        
            | 
            | 
           63 | 
                * @return bool Whether the state has been removed or not.
  | 
        
        
            | 
            | 
           64 | 
                */
  | 
        
        
            | 
            | 
           65 | 
               public static function execute(
  | 
        
        
            | 
            | 
           66 | 
                   string $component,
  | 
        
        
            | 
            | 
           67 | 
                   string $activityiri,
  | 
        
        
            | 
            | 
           68 | 
                   string $agent,
  | 
        
        
            | 
            | 
           69 | 
                   string $stateid,
  | 
        
        
            | 
            | 
           70 | 
                   ?string $registration = null
  | 
        
        
            | 
            | 
           71 | 
               ): bool {
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   $params = self::validate_parameters(self::execute_parameters(), [
  | 
        
        
            | 
            | 
           74 | 
                       'component' => $component,
  | 
        
        
            | 
            | 
           75 | 
                       'activityId' => $activityiri,
  | 
        
        
            | 
            | 
           76 | 
                       'agent' => $agent,
  | 
        
        
            | 
            | 
           77 | 
                       'stateId' => $stateid,
  | 
        
        
            | 
            | 
           78 | 
                       'registration' => $registration,
  | 
        
        
            | 
            | 
           79 | 
                   ]);
  | 
        
        
            | 
            | 
           80 | 
                   [
  | 
        
        
            | 
            | 
           81 | 
                       'component' => $component,
  | 
        
        
            | 
            | 
           82 | 
                       'activityId' => $activityiri,
  | 
        
        
            | 
            | 
           83 | 
                       'agent' => $agent,
  | 
        
        
            | 
            | 
           84 | 
                       'stateId' => $stateid,
  | 
        
        
            | 
            | 
           85 | 
                       'registration' => $registration,
  | 
        
        
            | 
            | 
           86 | 
                   ] = $params;
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
                   static::validate_component($component);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $handler = handler::create($component);
  | 
        
        
            | 
            | 
           91 | 
                   $activityid = iri::extract($activityiri, 'activity');
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   $state = new state(
  | 
        
        
            | 
            | 
           94 | 
                       self::get_agent_from_json($agent),
  | 
        
        
            | 
            | 
           95 | 
                       item_activity::create_from_id($activityid),
  | 
        
        
            | 
            | 
           96 | 
                       $stateid,
  | 
        
        
            | 
            | 
           97 | 
                       $registration,
  | 
        
        
            | 
            | 
           98 | 
                       null
  | 
        
        
            | 
            | 
           99 | 
                   );
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   if (!self::check_state_user($state)) {
  | 
        
        
            | 
            | 
           102 | 
                       throw new xapi_exception('State agent is not the current user');
  | 
        
        
            | 
            | 
           103 | 
                   }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   return $handler->delete_state($state);
  | 
        
        
            | 
            | 
           106 | 
               }
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
               /**
  | 
        
        
            | 
            | 
           109 | 
                * Return for execute.
  | 
        
        
            | 
            | 
           110 | 
                */
  | 
        
        
            | 
            | 
           111 | 
               public static function execute_returns(): external_value {
  | 
        
        
            | 
            | 
           112 | 
                   return new external_value(PARAM_BOOL, 'If the state data is deleted');
  | 
        
        
            | 
            | 
           113 | 
               }
  | 
        
        
            | 
            | 
           114 | 
           }
  |