Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Module to handle AJAX interactions.
18
 *
19
 * @module     core_h5p/repository
20
 * @copyright  2023 Andrew Nicols <andrew@nicols.co.uk>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import {call as fetchMany} from 'core/ajax';
24
import * as config from 'core/config';
25
 
26
/**
27
 * Send a xAPI statement to LMS.
28
 *
29
 * @param {string} component
30
 * @param {Object} statements
31
 * @returns {Promise}
32
 */
33
export const postStatement = (component, statements) => fetchMany([{
34
    methodname: 'core_xapi_statement_post',
35
    args: {
36
        component,
37
        requestjson: JSON.stringify(statements),
38
    }
39
}])[0];
40
 
41
/**
42
 * Send a xAPI state to LMS.
43
 *
44
 * @param {string} component
45
 * @param {string} activityId
46
 * @param {Object} agent
47
 * @param {string} stateId
48
 * @param {string} stateData
49
 */
50
export const postState = (
51
    component,
52
    activityId,
53
    agent,
54
    stateId,
55
    stateData,
56
) => {
57
    // Please note that we must use a Beacon send here.
58
    // The XHR is not guaranteed because it will be aborted on page transition.
59
    // https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API
60
    // Note: Moodle does not currently have a sendBeacon API endpoint.
61
    const requestUrl = new URL(`${config.wwwroot}/lib/ajax/service.php`);
62
    requestUrl.searchParams.set('sesskey', config.sesskey);
63
 
64
    navigator.sendBeacon(requestUrl, JSON.stringify([{
65
        index: 0,
66
        methodname: 'core_xapi_post_state',
67
        args: {
68
            component,
69
            activityId,
70
            agent: JSON.stringify(agent),
71
            stateId,
72
            stateData,
73
        }
74
    }]));
75
};
76
 
77
/**
78
 * Delete a xAPI state from LMS.
79
 *
80
 * @param {string} component
81
 * @param {string} activityId
82
 * @param {Object} agent
83
 * @param {string} stateId
84
 * @returns {Promise}
85
 */
86
export const deleteState = (
87
    component,
88
    activityId,
89
    agent,
90
    stateId,
91
) => fetchMany([{
92
    methodname: 'core_xapi_delete_state',
93
    args: {
94
        component,
95
        activityId,
96
        agent: JSON.stringify(agent),
97
        stateId,
98
    },
99
}])[0];