mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-67789 xapi: Add support to save content state
- Loading branch information
1 parent
12a8176
commit 03a4abd
Showing
29 changed files
with
3,349 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace core_xapi; | ||
|
||
/** | ||
* The xAPI internal API. | ||
* | ||
* @package core_xapi | ||
* @copyright 2023 Ferran Recio | ||
* @since Moodle 4.2 | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class api { | ||
/** | ||
* Delete all states from a component. | ||
* | ||
* @param string $component The component name in frankenstyle. | ||
* @return void | ||
*/ | ||
public static function remove_states_from_component(string $component): void { | ||
global $DB; | ||
|
||
$statestore = null; | ||
$dbman = $DB->get_manager(); | ||
try { | ||
$handler = handler::create($component); | ||
$statestore = $handler->get_state_store(); | ||
} catch (xapi_exception $exception) { | ||
// If the component is not available but the xapi_states table exists, use the standard one to ensure we clean it. | ||
$table = new \xmldb_table('xapi_states'); | ||
if ($dbman->table_exists($table)) { | ||
$statestore = new state_store($component); | ||
} | ||
} | ||
if ($statestore) { | ||
$statestore->wipe(); | ||
} | ||
} | ||
|
||
/** | ||
* Execute the states clean up for all compatible components. | ||
* | ||
* @return void | ||
*/ | ||
public static function execute_state_cleanup(): void { | ||
foreach (\core_component::get_plugin_types() as $ptype => $unused) { | ||
$components = \core_component::get_plugin_list_with_class($ptype, 'xapi\handler'); | ||
foreach ($components as $component => $unused) { | ||
$handler = handler::create($component); | ||
$statestore = $handler->get_state_store(); | ||
$statestore->cleanup(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace core_xapi\external; | ||
|
||
use core_xapi\local\state; | ||
use core_xapi\local\statement\item_activity; | ||
use core_xapi\handler; | ||
use core_xapi\xapi_exception; | ||
use core_external\external_api; | ||
use core_external\external_function_parameters; | ||
use core_external\external_value; | ||
use core_xapi\iri; | ||
|
||
/** | ||
* This is the external API for generic xAPI state deletion. | ||
* | ||
* @package core_xapi | ||
* @since Moodle 4.2 | ||
* @copyright 2023 Ferran Recio | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class delete_state extends external_api { | ||
|
||
use \core_xapi\local\helper\state_trait; | ||
|
||
/** | ||
* Parameters for execute. | ||
* | ||
* @return external_function_parameters | ||
*/ | ||
public static function execute_parameters(): external_function_parameters { | ||
return new external_function_parameters([ | ||
'component' => new external_value(PARAM_COMPONENT, 'Component name'), | ||
'activityId' => new external_value(PARAM_URL, 'xAPI activity ID IRI'), | ||
'agent' => new external_value(PARAM_RAW, 'The xAPI agent json'), | ||
'stateId' => new external_value(PARAM_ALPHAEXT, 'The xAPI state ID'), | ||
'registration' => new external_value(PARAM_ALPHANUMEXT, 'The xAPI registration UUID', VALUE_DEFAULT, null), | ||
]); | ||
} | ||
|
||
/** | ||
* Process a state delete request. | ||
* | ||
* @param string $component The component name in frankenstyle. | ||
* @param string $activityiri The activity IRI. | ||
* @param string $agent The agent JSON. | ||
* @param string $stateid The xAPI state id. | ||
* @param string|null $registration The xAPI registration UUID. | ||
* @return bool Whether the state has been removed or not. | ||
*/ | ||
public static function execute( | ||
string $component, | ||
string $activityiri, | ||
string $agent, | ||
string $stateid, | ||
?string $registration = null | ||
): bool { | ||
|
||
$params = self::validate_parameters(self::execute_parameters(), [ | ||
'component' => $component, | ||
'activityId' => $activityiri, | ||
'agent' => $agent, | ||
'stateId' => $stateid, | ||
'registration' => $registration, | ||
]); | ||
[ | ||
'component' => $component, | ||
'activityId' => $activityiri, | ||
'agent' => $agent, | ||
'stateId' => $stateid, | ||
'registration' => $registration, | ||
] = $params; | ||
|
||
static::validate_component($component); | ||
|
||
$handler = handler::create($component); | ||
$activityid = iri::extract($activityiri, 'activity'); | ||
|
||
$state = new state( | ||
self::get_agent_from_json($agent), | ||
item_activity::create_from_id($activityid), | ||
$stateid, | ||
$registration, | ||
null | ||
); | ||
|
||
if (!self::check_state_user($state)) { | ||
throw new xapi_exception('State agent is not the current user'); | ||
} | ||
|
||
return $handler->delete_state($state); | ||
} | ||
|
||
/** | ||
* Return for execute. | ||
*/ | ||
public static function execute_returns(): external_value { | ||
return new external_value(PARAM_BOOL, 'If the state data is deleted'); | ||
} | ||
} |
Oops, something went wrong.