UNPKG

@liascript/exporter

Version:
2 lines (1 loc) 16.7 kB
!function(){function e(e,t,n,i){Object.defineProperty(e,t,{get:n,set:i,enumerable:!0,configurable:!0})}var t=("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{}).parcelRequire55a5;t.register("4yc0d",(function(n,i){e(n.exports,"Connector",(function(){return h}));var r=t("f1evb"),o=t("9Klpk"),s=t("3bWnf"),a=t("9WzNc"),c=t("div3d"),u=t("kb0qF"),l=t("bWNdT"),d=t("knzCS"),f=t("4MgDM"),p=t("4Vevw"),h=function(e){"use strict";(0,u.default)(n,e);var t=(0,l.default)(n);function n(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};(0,o.default)(this,n),(e=t.call(this)).active=!1,e.debug=i.debug||!1,e.totalScore=0,e.maxScore=0,e.visitedSlides=new Set,e.totalSlides=0,e.quizStates={},e.startTime=Date.now(),e.lastActivityTime=e.startTime,e.completionSent=!1,e.progressThreshold=.9,e.actor=i.actor||{objectType:"Agent",name:"Anonymous",mbox:"mailto:anonymous@example.com"};var s=window.location.origin;if(e.courseId=i.courseId||new URL(window.location.href,s).href,e.courseTitle=i.courseTitle||document.title||"LiaScript Course",i.endpoint)try{e.lrs=new p.LRSConnection(i.endpoint,i.auth||"","1.0.3",e.debug),e.active=!0,e.debug&&console.log("xAPI connector initialized with LRS endpoint:",i.endpoint),window.addEventListener("beforeunload",e.handleUnload.bind((0,r.default)(e)))}catch(t){console.error("Failed to initialize LRS connection:",t),e.lrs=null}else console.warn("No LRS endpoint provided, xAPI tracking will be disabled"),e.lrs=null;return e}return(0,s.default)(n,[{key:"handleUnload",value:function(){if(this.active&&this.lrs){var e=f.formatDuration(Date.now()-this.startTime),t=f.generateTerminatedStatement(this.actor,this.courseId,this.courseTitle,e);navigator.sendBeacon&&this.lrs.endpoint?(this.lrs.auth&&this.lrs.auth,navigator.sendBeacon(this.lrs.endpoint,JSON.stringify([t]))):this.lrs.sendStatement(t)}}},{key:"checkCompletion",value:function(){var e=0;this.totalSlides>0&&(e=this.visitedSlides.size/this.totalSlides);var t=!0;return this.maxScore>0&&(t=Object.values(this.quizStates).every((function(e){return Object.values(e).every((function(e){return void 0!==e.solved}))}))),this.maxScore>0?e>=this.progressThreshold&&t:e>=this.progressThreshold}},{key:"sendCompletionIfNeeded",value:function(){var e=this;if(!this.completionSent&&this.active&&this.lrs&&this.checkCompletion()){var t=!(this.maxScore>0)||this.totalScore/this.maxScore>=.7,n=f.formatDuration(Date.now()-this.startTime),i=f.generateCompletedStatement(this.actor,this.courseId,this.courseTitle,t,this.totalScore,this.maxScore,n);this.lrs.sendStatement(i).then((function(t){e.debug&&console.log("Sent completed statement, ID:",t),e.completionSent=!0})).catch((function(e){console.error("Failed to send completed statement:",e)}))}}},{key:"open",value:function(e,t,n){var i=this;if(this.active&&this.lrs){this.startTime=Date.now(),this.lastActivityTime=this.startTime,this.completionSent=!1;var r=f.generateInitializedStatement(this.actor,this.courseId,this.courseTitle);this.lrs.sendStatement(r).then((function(e){i.debug&&console.log("Sent initialized statement, ID:",e)})).catch((function(e){console.error("Failed to send initialized statement:",e)})),void 0!==n&&this.slide(n)}}},{key:"slide",value:function(e){var t=this;if(this.active&&this.lrs){this.lastActivityTime=Date.now(),this.visitedSlides.add(e);var n="Slide ".concat(e);window.LIA&&window.LIA.course&&window.LIA.course.slides[e]&&(n=window.LIA.course.slides[e].title||n);var i=f.generateExperiencedStatement(this.actor,this.courseId,this.courseTitle,e,n);if(this.lrs.sendStatement(i).then((function(e){t.debug&&console.log("Sent experienced statement, ID:",e)})).catch((function(e){console.error("Failed to send experienced statement:",e)})),this.totalSlides>0){var r=this.visitedSlides.size/this.totalSlides,o=f.generateProgressedStatement(this.actor,this.courseId,this.courseTitle,r);this.lrs.sendStatement(o).then((function(){t.sendCompletionIfNeeded()})).catch((function(e){console.error("Failed to send progress statement:",e)}))}}}},{key:"store",value:function(e){var t=this;if(this.active&&this.lrs){this.lastActivityTime=Date.now();var n=e.table,i=e.id,r=e.data;if("quiz"===n&&r){this.quizStates[i]||(this.quizStates[i]={});for(var o=0;o<r.length;o++){var s=r[o];if(s){this.quizStates[i][o]=s,void 0!==s.score&&(1===s.solved&&(this.totalScore+=s.score),this.maxScore+=s.score);var a=f.generateAnsweredStatement(this.actor,this.courseId,this.courseTitle,i,o,s.type||"unknown",s.input||s.answer||"",1===s.solved,1===s.solved?s.score||1:0,s.score||1);this.lrs.sendStatement(a).then((function(e){t.debug&&console.log("Sent answered statement, ID:",e),t.sendCompletionIfNeeded()})).catch((function(e){console.error("Failed to send answered statement:",e)}))}}}}}},{key:"update",value:function(e,t){(0,a.default)((0,c.default)(n.prototype),"update",this).call(this,e,t),this.store(e)}},{key:"getSettings",value:function(){return(0,a.default)((0,c.default)(n.prototype),"getSettings",this).call(this)}},{key:"setSettings",value:function(e){(0,a.default)((0,c.default)(n.prototype),"setSettings",this).call(this,e)}},{key:"reset",value:function(e,t){this.totalScore=0,this.maxScore=0,this.visitedSlides.clear(),this.quizStates={},this.startTime=Date.now(),this.lastActivityTime=this.startTime,this.completionSent=!1,(0,a.default)((0,c.default)(n.prototype),"reset",this).call(this,e,t)}}]),n}(d.Connector)})),t.register("9WzNc",(function(n,i){e(n.exports,"default",(function(){return s}));var r=t("jwduU");function o(e,t,n){return o="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var i=(0,r.default)(e,t);if(i){var o=Object.getOwnPropertyDescriptor(i,t);return o.get?o.get.call(n||e):o.value}},o(e,t,n)}function s(e,t,n){return o(e,t,n)}})),t.register("jwduU",(function(n,i){e(n.exports,"default",(function(){return o}));var r=t("div3d");function o(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=(0,r.default)(e)););return e}})),t.register("knzCS",(function(n,i){e(n.exports,"Connector",(function(){return l}));var r=t("hnmIs"),o=t("9Klpk"),s=t("3bWnf"),a=t("32Euv"),c=t("87lFe"),u=t("cRTHJ"),l=function(){"use strict";function e(){(0,o.default)(this,e)}return(0,s.default)(e,[{key:"hasIndex",value:function(){return!1}},{key:"storage",value:function(){return new c.LiaStorage}},{key:"initSettings",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return u.Settings.init(e,t,this.setSettings)}},{key:"setSettings",value:function(e){u.Settings.update(e)}},{key:"getSettings",value:function(){var e="";try{e=localStorage.getItem(u.Settings.PORT)}catch(e){console.warn("cannot write to localStorage")}var t=null;if("string"==typeof e){try{t=JSON.parse(e)}catch(e){console.warn("getSettings =>",e)}t||(t=u.Settings.data),window.innerWidth<=768&&(t.table_of_contents=!1)}return t}},{key:"open",value:function(e,t,n){}},{key:"load",value:function(e){}},{key:"store",value:function(e){}},{key:"update",value:function(e,t){}},{key:"slide",value:function(e){}},{key:"getIndex",value:function(){}},{key:"deleteFromIndex",value:function(e){}},{key:"storeToIndex",value:function(e){return(0,r.default)((function(){return(0,a.__generator)(this,(function(e){return[2]}))}))()}},{key:"restoreFromIndex",value:function(e,t){}},{key:"reset",value:function(e,t){this.initSettings(null,!0)}},{key:"getFromIndex",value:function(e){return(0,r.default)((function(){return(0,a.__generator)(this,(function(e){return[2,null]}))}))()}},{key:"addMisc",value:function(e,t,n,i){return(0,r.default)((function(){return(0,a.__generator)(this,(function(e){return console.log("addMisc not implemented"),[2]}))}))()}},{key:"getMisc",value:function(e,t,n){return(0,r.default)((function(){return(0,a.__generator)(this,(function(e){return console.log("getMisc not implemented"),[2]}))}))()}}]),e}()})),t.register("87lFe",(function(n,i){e(n.exports,"LiaStorage",(function(){return a}));var r=t("9Klpk"),o=t("3bWnf"),s=t("5ENmm"),a=function(){"use strict";function e(){(0,r.default)(this,e)}return(0,o.default)(e,[{key:"getItems",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];"string"==typeof e&&(e=[e]);for(var t={},n=0;n<e.length;n++){var i=localStorage.getItem(e[n]);t[e[n]]=i?JSON.parse(i):i}return t}},{key:"setItems",value:function(e){this._setLocal(e)}},{key:"_setLocal",value:function(e){var t=!0,n=!1,i=void 0;if("object"==typeof e)try{for(var r,o=Object.entries(e)[Symbol.iterator]();!(t=(r=o.next()).done);t=!0){var a=(0,s.default)(r.value,2),c=a[0],u=a[1];localStorage.setItem(c,JSON.stringify(u))}}catch(e){n=!0,i=e}finally{try{t||null==o.return||o.return()}finally{if(n)throw i}}}}]),e}()})),t.register("4MgDM",(function(t,n){function i(e,t,n){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/initialized",display:{"en-US":"initialized"}},object:{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}},timestamp:(new Date).toISOString()}}function r(e,t,n,i,r){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/experienced",display:{"en-US":"experienced"}},object:{id:"".concat(t,"/slides/").concat(i),objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/module",name:{"en-US":r||"Slide ".concat(i)}}},context:{contextActivities:{parent:[{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}}]}},timestamp:(new Date).toISOString()}}function o(e,t,n,i,r,o,s,a,c,u){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/answered",display:{"en-US":"answered"}},object:{id:"".concat(t,"/slides/").concat(i,"/quiz/").concat(r),objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/question",name:{"en-US":"".concat(o," Question ").concat(r)}}},result:{success:a,completion:!0,response:JSON.stringify(s),score:{scaled:c/u,raw:c,min:0,max:u}},context:{contextActivities:{parent:[{id:"".concat(t,"/slides/").concat(i),objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/module"}}],grouping:[{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}}]}},timestamp:(new Date).toISOString()}}function s(e,t,n,i,r,o,s){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/completed",display:{"en-US":"completed"}},object:{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}},result:{success:i,completion:!0,score:{scaled:o>0?r/o:0,raw:r,min:0,max:o},duration:s},timestamp:(new Date).toISOString()}}function a(e,t,n,i){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/progressed",display:{"en-US":"progressed"}},object:{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}},result:{completion:i>=1,progress:i},timestamp:(new Date).toISOString()}}function c(e,t,n,i){return{actor:e,verb:{id:"http://adlnet.gov/expapi/verbs/terminated",display:{"en-US":"terminated"}},object:{id:t,objectType:"Activity",definition:{type:"http://adlnet.gov/expapi/activities/course",name:{"en-US":n}}},result:{duration:i},timestamp:(new Date).toISOString()}}function u(e){var t=Math.floor(e/1e3),n=Math.floor(t/3600),i=Math.floor(t%3600/60),r=t%60,o="PT";return n>0&&(o+="".concat(n,"H")),i>0&&(o+="".concat(i,"M")),(r>0||0===n&&0===i)&&(o+="".concat(r,"S")),o}e(t.exports,"generateInitializedStatement",(function(){return i})),e(t.exports,"generateExperiencedStatement",(function(){return r})),e(t.exports,"generateAnsweredStatement",(function(){return o})),e(t.exports,"generateCompletedStatement",(function(){return s})),e(t.exports,"generateProgressedStatement",(function(){return a})),e(t.exports,"generateTerminatedStatement",(function(){return c})),e(t.exports,"formatDuration",(function(){return u}))})),t.register("4Vevw",(function(n,i){e(n.exports,"LRSConnection",(function(){return u}));var r=t("hnmIs"),o=t("9Klpk"),s=t("3bWnf"),a=t("5ENmm"),c=t("32Euv"),u=function(){"use strict";function e(t,n){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"1.0.3",r=arguments.length>3&&void 0!==arguments[3]&&arguments[3];(0,o.default)(this,e),this.endpoint=t.endsWith("/")?t:t+"/",this.endpoint.endsWith("statements/")&&(this.endpoint=this.endpoint.slice(0,-11)),this.auth=n,this.version=i,this.debug=r,this.debug&&console.log("LRS endpoint configured as:",this.endpoint)}return(0,s.default)(e,[{key:"sendStatement",value:function(e){var t=this;return(0,r.default)((function(){var n,i,r,o,s,a;return(0,c.__generator)(this,(function(c){switch(c.label){case 0:return c.trys.push([0,5,,6]),t.debug&&console.log("Sending statement to LRS:",JSON.stringify(e,null,2)),n=t.ensureStringReferences(e),t.validateStatement(n)?[4,fetch(t.endpoint+"statements",{method:"POST",headers:{"Content-Type":"application/json","X-Experience-API-Version":t.version,Authorization:t.auth},body:JSON.stringify(n)})]:(console.error("Invalid statement structure:",n),[2,null]);case 1:return(i=c.sent()).ok?[3,3]:(r=console.error,o=["LRS error:",i.status],[4,i.text()]);case 2:return r.apply(console,o.concat([c.sent()])),[2,null];case 3:return[4,i.json()];case 4:return s=c.sent(),t.debug&&console.log("LRS response:",s),Array.isArray(s)&&s.length>0?[2,s[0]]:s&&"object"==typeof s?[2,s.id||null]:[2,null];case 5:return a=c.sent(),console.error("Error sending statement to LRS:",a),[2,null];case 6:return[2]}}))}))()}},{key:"sendStatements",value:function(e){var t=this;return(0,r.default)((function(){var n,i,r,o,s,a;return(0,c.__generator)(this,(function(c){switch(c.label){case 0:return c.trys.push([0,5,,6]),t.debug&&console.log("Sending statements to LRS:",JSON.stringify(e,null,2)),n=e.map((function(e){return t.ensureStringReferences(e)})),[4,fetch(t.endpoint+"statements",{method:"POST",headers:{"Content-Type":"application/json","X-Experience-API-Version":t.version,Authorization:t.auth},body:JSON.stringify(n)})];case 1:return(i=c.sent()).ok?[3,3]:(r=console.error,o=["LRS error:",i.status],[4,i.text()]);case 2:return r.apply(console,o.concat([c.sent()])),[2,[]];case 3:return[4,i.json()];case 4:return s=c.sent(),t.debug&&console.log("LRS response:",s),Array.isArray(s)?[2,s]:[2,[]];case 5:return a=c.sent(),console.error("Error sending statements to LRS:",a),[2,[]];case 6:return[2]}}))}))()}},{key:"getStatements",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=this;return(0,r.default)((function(){var n,i,r,o,s,u,l,d,f,p,h,v,g,S,m;return(0,c.__generator)(this,(function(c){switch(c.label){case 0:c.trys.push([0,5,,6]),n=new URLSearchParams,i=!0,r=!1,o=void 0;try{for(s=Object.entries(e)[Symbol.iterator]();!(i=(u=s.next()).done);i=!0)l=(0,a.default)(u.value,2),d=l[0],null!=(f=l[1])&&("object"==typeof f?n.append(d,JSON.stringify(f)):n.append(d,String(f)))}catch(e){r=!0,o=e}finally{try{i||null==s.return||s.return()}finally{if(r)throw o}}return p="".concat(t.endpoint,"statements").concat(n.toString()?"?".concat(n.toString()):""),t.debug&&console.log("Getting statements from LRS:",p),[4,fetch(p,{method:"GET",headers:{"X-Experience-API-Version":t.version,Authorization:t.auth}})];case 1:return(h=c.sent()).ok?[3,3]:(v=console.error,g=["LRS error:",h.status],[4,h.text()]);case 2:return v.apply(console,g.concat([c.sent()])),[2,null];case 3:return[4,h.json()];case 4:return S=c.sent(),t.debug&&console.log("LRS response:",S),[2,S];case 5:return m=c.sent(),console.error("Error getting statements from LRS:",m),[2,null];case 6:return[2]}}))}))()}},{key:"testConnection",value:function(){var e=this;return(0,r.default)((function(){var t,n;return(0,c.__generator)(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),(t=e.endpoint).includes("api/v2/statements")?t=t.replace("api/v2/statements","api/v2/about"):t.endsWith("/statements")&&(t=t.replace("/statements","/about")),[4,fetch(t,{method:"GET",headers:{"X-Experience-API-Version":e.version,Authorization:e.auth}})];case 1:return[2,i.sent().ok];case 2:return n=i.sent(),console.error("Error testing LRS connection:",n),[2,!1];case 3:return[2]}}))}))()}},{key:"ensureStringReferences",value:function(e){var t=this;if(null==e)return e;if("object"==typeof e){if(Array.isArray(e))return e.map((function(e){return t.ensureStringReferences(e)}));var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(n[i]=this.ensureStringReferences(e[i]));return n}return"function"==typeof e?e.toString():e}},{key:"validateStatement",value:function(e){return!!(e&&e.actor&&"object"==typeof e.actor&&e.verb&&"object"==typeof e.verb&&e.verb.id&&"string"==typeof e.verb.id&&e.object&&"object"==typeof e.object&&e.object.id&&"string"==typeof e.object.id)}}]),e}()}))}();