Picture of Jason Chui
Using node modules from plugins
by Jason Chui - Wednesday, 11 July 2018, 8:24 AM
 

Hi,

I am an educator at a tertiary institute in Singapore and we are exploring Adapt Learning as an alternative to proprietary mobile/elearning tools.

I am trying to establish that being open source means the school can extend the capability of the tool.

I am attempting to send data to SurveyJS using a plugin. Aim is to keep track of students' progress real-time by querying the SurveyJS database periodically.

I am using the authoring tool and not solely the framework.

The following code works on a browser i.e. uses XMLHttpRequest to send data to my SurveyJS database.

Posting to SurveyJS by running script in browser:

Posting to SurveyJS by running script in browser

 

I downloaded the adapt-textConfirmation plugin from this thread (https://community.adaptlearning.org/mod/forum/discuss.php?d=2092), and I tried to put in the same code.

Original code screenshot of adapt-textConfirmation.js:

Original code screenshot of adapt-textConfirmation.js

 

As the code needs XMLHttpRequest, but it seems that the framework that comes with the authoring tool does not have XMLHttpRequest, I downloaded the XMLHttpRequest package using npm, and copied it into the same folder as adapt-textConfirmation.js. Then I rewrote the code in front so that adapt-textConfirmation.js can find XMLHttpRequest.

My attempt at copying and pasting the working code into the plugin:
My attempt at copying and pasting the working code into the plugin


File structure:

File structure:

However, when running the preview, this error came out:
Error

I opened XMLHttpRequest.js and realized that it requires the package "url".
XMLHttpRequest requirements

I stopped attempting after downloading using npm and copying the package "url" to the same folder as adapt-textConfirmation.js because there is always some other modules that is missing, and I doubt it is good practice to manually copy node modules to the same folder.

My question hence is this: what is the correct way to require node modules like XMLHttpRequest (which has its own dependencies) when creating a plugin? Can I download new modules to the adapt framework exported with each stand-alone course or is there some other way?

I would appreciate any advice. Thanks in advance!

Picture of Matt Leathes
Re: Using node modules from plugins
by Matt Leathes - Wednesday, 11 July 2018, 9:28 AM
 
it seems that the framework that comes with the authoring tool does not have XMLHttpRequest

That's because XMLHttpRequest is built into every major web browser, supported as far back as IE7. There's therefore no need to include it in the framework.

The framework includes JQuery which provides an AJAX API which you might prefer to use rather than work directly with XMLHttpRequest.

The XMLHttpRequest package you are trying to work with is for NodeJS and so will only run on a NodeJS server.

Picture of Jason Chui
Re: Using node modules from plugins
by Jason Chui - Thursday, 12 July 2018, 8:58 AM
 

Hi Matt,

Thanks for your suggestion.

I changed the code to this:

connected

and when I click on the checkbox,

checkbox

the database is updated!

db_updated

Now to send more specific things other than hard codes.

Thanks!