@@ -1007,7 +1007,7 @@ axios.defaults.httpAgent = new http.Agent({
10071007async function sendStatements ( statements , lrs ) {
10081008 return axios ( {
10091009 'method' : 'POST' ,
1010- 'url' : lrs . url + ' statements' ,
1010+ 'url' : ` ${ config . url } :8081/data/xAPI/ statements` ,
10111011 'auth' : lrs . auth ,
10121012 'headers' : {
10131013 'Content-Type' : 'application/json' ,
@@ -1017,42 +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- 'url' :config . LRS . url ,
1038- 'auth' :{
1039- 'username' :config . LRS . clients . default . user ,
1040- 'password' :config . LRS . clients . default . pass
1041- } ,
1042- } ;
1043- }
1044- if ( 'scoped' in config . LRS . clients ) {
1045- config . LRS . clients . scoped . forEach ( client => {
1046- lrses [ client . scope ] = {
1047- 'url' :config . LRS . url ,
1048- 'auth' :{
1049- 'username' :client . user ,
1050- 'password' :client . pass
1051- } ,
1052- } ;
1053- } ) ;
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' ) ;
10541029 }
1055- 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 ;
10561062}
10571063
10581064/**
@@ -1069,11 +1075,10 @@ async function routeStatements(objecttable, xapis){
10691075 const statements = xapis [ scope ] . map ( ( value , index , array ) => {
10701076 return value . statement ;
10711077 } ) ;
1072- const lrs = getLRS ( scope ) ;
1078+ const lrs = LRS_CLIENTS [ scope ] ;
10731079 const seq = objectids [ 0 ] ;
10741080 logger . info (
1075- `[SEQ:${ seq } ][SCOPE:${ scope } ] Sending ${ statements . length } statements ` +
1076- `to ${ config . LRS . url } statements...`
1081+ `[SEQ:${ seq } ][SCOPE:${ scope } ] Sending ${ statements . length } statements...`
10771082 ) ;
10781083 const promise = sendStatements ( statements , lrs ) . then ( ( ) => {
10791084 logger . info ( `[SEQ:${ seq } ][SCOPE:${ scope } ] ${ statements . length } statements added.` ) ;
@@ -1112,15 +1117,15 @@ function getScopeFromEppn(username) {
11121117 */
11131118function getLRSScope ( userAttrs , userid ) {
11141119 let scope = 'default' ;
1115- if ( isScoped ( ) ) {
1120+ if ( config . LRS . ePPNScoped ) {
11161121 scope = userid in userAttrs ? userAttrs [ userid ] . scope : null ;
11171122 if ( ! scope ) {
11181123 logger . trace (
11191124 `The user(id:${ userid } ) doesn't have ` +
11201125 'an ePPN scope, sending the statement to the default LRS.'
11211126 ) ;
11221127 scope = 'default' ;
1123- } else if ( ! getLRS ( scope ) ) {
1128+ } else if ( ! LRS_CLIENTS [ scope ] ) {
11241129 logger . trace (
11251130 `LRS for ${ scope } not found, ` +
11261131 'sending the statement to the default LRS.'
@@ -3420,6 +3425,7 @@ async function translateStandardLogs(logs, userAttrs, courseNames){ // eslint-di
34203425 ) ;
34213426 continue ;
34223427 }
3428+
34233429 const scope = getLRSScope ( userAttrs , log . userid ) ;
34243430 if ( ! ( scope in xapis ) ) {
34253431 xapis [ scope ] = [ ] ;
@@ -3686,10 +3692,7 @@ const waitForInterval = () => {
36863692 * Processes logs until no more data exists.
36873693 */
36883694module . exports = async function main ( ) { // eslint-disable-line max-statements
3689- if ( ! ( 'default' in config . LRS . clients ) && ! ( 'scoped' in config . LRS . clients ) ) {
3690- process . exitCode = 1 ;
3691- throw new Error ( 'Specify LRS clients in config/app.js' ) ;
3692- }
3695+ LRS_CLIENTS = await getLRSClients ( ) ;
36933696
36943697 // Retrieve all users
36953698 const users = await USER . findAll ( {
0 commit comments