Picture of Jason Chui
Reading local file
by Jason Chui - Tuesday, 27 August 2019, 2:50 AM
 

Hi,

I am using the Authoring Tool v0.7.1 and Framework 4.1.1.

I would like to read the contents of a local text file in the exported course. It contains a database ID and access key.

Simply using ajax doesn't work cos it's a local file so I don't have permissions to read it.

I am thinking of using nodejs's fs module.

But it is not a library that is exported with the course.

I am thinking of using fs in scriptloader.js to read the database info into, say, localStorage.

How can I use the fs module to read a local text file in an exported course created using the AT?

Thanks!

Picture of Jason Chui
Re: Reading local file
by Jason Chui - Tuesday, 27 August 2019, 9:38 AM
 

I'm experimenting with extensions now.

Say instead of scriptloader.js, I create an extension to read in the contents of a text file.

Can I do something like this in the extension's js file?

 

Can I use the node module fs like this?

 

Another question is, where do I put testDBinfo.txt when

1) still authoring the course in the AT, or

2) after exporting the course?

 

For 2) I am thinking of this:

Am I on the right track?

 

Thanks a lot for reading through and answering!

Picture of Matt Leathes
Re: Reading local file
by Matt Leathes - Tuesday, 27 August 2019, 10:21 AM
 

What exactly is it that needs to read the contents of the text file? The authoring tool - or the course?

Picture of Jason Chui
Re: Reading local file
by Jason Chui - Tuesday, 27 August 2019, 11:46 PM
 

The course. The use case is as such:

1) Course designer has AT installed and he designs the course.

2) He distributes the exported course with SPOOR plugin enabled to a bunch of tutors to upload to their own LMS module page.

3) Each of these tutors have their own database (we are using free SurveyJS accounts for now) to store data.

4) For now (testing), the designer (aka yours truly) will put the database id into the course components. I edited properties.schema to introduce a couple of input boxes for that for each components.

5) But I don't want to put one database id into a course and export it for one tutor, then change the database id and export again for another tutor. Too troublesome.

6) So I am thinking if the exported course can read a text file, then all the tutors (whom you recall do not have the AT) have to do is unzip the SCORM zip file, then put a text file into the, say, adapt/js folder for adapt.min.js to read, then rezip the SCORM package, before uploading to the LMS. 

7) I am hoping if an extension can help to read the DB id from the textfile into sessionStorage (it's not a security risk. SurveyJS is not linked to our main DB), then individual components (I use MCQ and text input components) can read the id from sessionStorage and push the data captured from interactions with the components up to the cloud immediately. We pull the data down realtime for learning analytics purposes.

Please advise if this is a good way to do it, and if so, how to let the exported course read a text file.

 

Thanks!!

Picture of Matt Leathes
Re: Reading local file
by Matt Leathes - Wednesday, 28 August 2019, 9:36 AM
 

So, the text file containing the data you need will be part of the course? In which case it is not a 'local file' - so you can load it and read it just like you can any other file that is part of the course. As jQuery is bundled with Adapt, it's probably easiest to use jQuery.load

Local file security restrictions apply when you are trying to get the course to load a file from the end user's computer i.e. trying to get a webpage to load a file like C:\users\jason\file.txt will not be allowed, for reasons that should be pretty obvious ;-)

Personally I would favour simply putting the data you need in course.json - as that file is already loaded that means one less step to get at the data you need. But equally putting the data in a .txt file will work too and if that makes life easier for your tutors then that's worth doing. If you were to put the zip file in the 'course root' (alongside index.html) then they wouldn't even have to unzip the zip file in many cases, they could simply drop their version of the .txt file onto the .zip file to add it (depending on what zip utility they have).

individual components can read the id from sessionStorage

I might be missing something but using session storage seems unnecessary here. As Adapt is a single page application, you shouldn't need to use session storage. You can simply add your data to the Adapt object e.g.:

var Adapt = require('core/js/adapt');
if (!Adapt.config.dbInfo) {
  Adapt.config.dbInfo = {
    dbId: 'foo',
    dbAccessKey: 'bar'
  }
}

Hope this helps?