Skip to content

Commit b80bd47

Browse files
committed
Remove per-scope client settings
1 parent 2729850 commit b80bd47

2 files changed

Lines changed: 50 additions & 60 deletions

File tree

xapi_stmt_gen/xapi_stmt_gen/config/app.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,13 @@ const config = {
2121
}
2222
},
2323
LRS:{
24-
clients:{
25-
// LRS client
26-
'default':{
27-
user:'031eb8dccd9729d6a8a16d245b4d1dddf1e2ded7',
28-
pass:'a94fd8e44662fe7cd50cd53812dad84a4a81ab9b'
29-
},
30-
/**
31-
* This 'scoped' setting can be used to send
32-
* statements to separated LRSes based on ePPN in GakuNin.
33-
*/
34-
//'scoped':[
35-
// {
36-
// scope:'foo.co.jp',
37-
// user:'dc052567a686bb93a7e2fb9547ed6f6974171e8b',
38-
// pass:'b0a3f0e04294c258cb3ce8bdbb55d24c6bad15f8'
39-
// },
40-
// {
41-
// scope:'bar.ac.jp',
42-
// user:'74abe7278e28aef9b5548d90700ee423e36c1fbb',
43-
// pass:'8a34c7f8433d51ede87f533afe672d6e357c8d37'
44-
// }
45-
//]
46-
}
24+
// LRS client with 'Overall Scopes: API All' checked
25+
client:{
26+
key:'',
27+
secret:''
28+
},
29+
// Store statements in each LRS with title matching actor's ePPN scope
30+
ePPNScoped:false,
4731
},
4832
category:{
4933
id:'http://moodle.org',

xapi_stmt_gen/xapi_stmt_gen/generator.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,40 +1017,48 @@ async function sendStatements(statements, lrs) {
10171017
});
10181018
}
10191019

1020-
/**
1021-
* Checks if LRSes are scoped.
1022-
*/
1023-
function isScoped() {
1024-
if ('scoped' in config.LRS.clients) {
1025-
return true;
1026-
}
1027-
return false;
1028-
}
1020+
let LRS_CLIENTS;
10291021

10301022
/**
1031-
* Returns LRS settings found by scope.
1023+
* Returns LRS clients.
10321024
*/
1033-
function getLRS(scope) {
1034-
let lrses = {};
1035-
if ('default' in config.LRS.clients) {
1036-
lrses['default'] = {
1037-
'auth':{
1038-
'username':config.LRS.clients.default.user,
1039-
'password':config.LRS.clients.default.pass
1040-
},
1041-
};
1042-
}
1043-
if ('scoped' in config.LRS.clients) {
1044-
config.LRS.clients.scoped.forEach(client => {
1045-
lrses[client.scope] = {
1046-
'auth':{
1047-
'username':client.user,
1048-
'password':client.pass
1049-
},
1050-
};
1051-
});
1025+
async function getLRSClients() {
1026+
if (!(config.LRS.client.key && config.LRS.client.secret)) {
1027+
process.exitCode = 1;
1028+
throw new Error('Specify LRS client in config/app.js');
10521029
}
1053-
return lrses[scope];
1030+
const clientResponse = await axios.get(`${config.url}:3000/api/v2/client`, {
1031+
'auth': {
1032+
'username':config.LRS.client.key,
1033+
'password':config.LRS.client.secret
1034+
}
1035+
}).catch(err => {
1036+
process.exitCode = 1;
1037+
throw new Error(`Failed to get LRS clients - ${err}`);
1038+
});
1039+
const lrsResponse = await axios.get(`${config.url}:3000/api/v2/lrs`, {
1040+
'auth': {
1041+
'username':config.LRS.client.key,
1042+
'password':config.LRS.client.secret
1043+
}
1044+
}).catch(err => {
1045+
process.exitCode = 1;
1046+
throw new Error(`Failed to get LRSes - ${err}`);
1047+
});
1048+
let clients = {}
1049+
lrsResponse.data.forEach((lrs) => {
1050+
const client = clientResponse.data.find(c => c.lrs_id === lrs._id)
1051+
const key = client.api.basic_key
1052+
const secret = client.api.basic_secret
1053+
const scope = (key === config.LRS.client.key && secret === config.LRS.client.secret) ? 'default' : lrs.title
1054+
clients[scope] = {
1055+
'auth': {
1056+
'username': key,
1057+
'password': secret
1058+
}
1059+
};
1060+
});
1061+
return clients;
10541062
}
10551063

10561064
/**
@@ -1067,7 +1075,7 @@ async function routeStatements(objecttable, xapis){
10671075
const statements = xapis[scope].map((value, index, array) => {
10681076
return value.statement;
10691077
});
1070-
const lrs = getLRS(scope);
1078+
const lrs = LRS_CLIENTS[scope];
10711079
const seq = objectids[0];
10721080
logger.info(
10731081
`[SEQ:${seq}][SCOPE:${scope}] Sending ${statements.length} statements...`
@@ -1109,15 +1117,15 @@ function getScopeFromEppn(username) {
11091117
*/
11101118
function getLRSScope(userAttrs, userid) {
11111119
let scope = 'default';
1112-
if (isScoped()) {
1120+
if (config.LRS.ePPNScoped) {
11131121
scope = userid in userAttrs ? userAttrs[userid].scope : null;
11141122
if (!scope) {
11151123
logger.trace(
11161124
`The user(id:${userid}) doesn't have ` +
11171125
'an ePPN scope, sending the statement to the default LRS.'
11181126
);
11191127
scope = 'default';
1120-
} else if (!getLRS(scope)) {
1128+
} else if (!LRS_CLIENTS[scope]) {
11211129
logger.trace(
11221130
`LRS for ${scope} not found, ` +
11231131
'sending the statement to the default LRS.'
@@ -3417,6 +3425,7 @@ async function translateStandardLogs(logs, userAttrs, courseNames){ // eslint-di
34173425
);
34183426
continue;
34193427
}
3428+
34203429
const scope = getLRSScope(userAttrs, log.userid);
34213430
if (!(scope in xapis)) {
34223431
xapis[scope] = [];
@@ -3683,10 +3692,7 @@ const waitForInterval = () => {
36833692
* Processes logs until no more data exists.
36843693
*/
36853694
module.exports = async function main() { // eslint-disable-line max-statements
3686-
if (!('default' in config.LRS.clients) && !('scoped' in config.LRS.clients)) {
3687-
process.exitCode = 1;
3688-
throw new Error('Specify LRS clients in config/app.js');
3689-
}
3695+
LRS_CLIENTS = await getLRSClients();
36903696

36913697
// Retrieve all users
36923698
const users = await USER.findAll({

0 commit comments

Comments
 (0)