Picture of Jason Chui
Get Course Title programmatically
by Jason Chui - Friday, 9 October 2020, 12:40 PM
 

Hi,

Is there a way to get the course title through JavaScript? I need to pull the information out to store in a database whenever users access a course, so I know which course is more popular. I was thinking of embedding some code in a component like the MCQ component, then Ajax the info to send out.

Thanks in advance!

Picture of Oliver Foster
Re: Get Course Title programmatically
by Oliver Foster - Friday, 9 October 2020, 1:51 PM
 

var Adapt = require('core/js/adapt'); // get me the Adapt singleton!
var courseModel = Adapt.course; // get me the course model
var courseTitle = courseModel.get('title'); // get me the course title from the course model

Will give you the title as defined in the course.json

https://github.com/adaptlearning/adapt_framework/blob/master/src/course/en/course.json#L6

If you already have the singleton:

var courseTitle = Adapt.course.get('title');

Picture of Matt Leathes
Re: Get Course Title programmatically
by Matt Leathes - Friday, 9 October 2020, 2:00 PM
 

Yes:
Adapt.course.get('displayTitle');
or:
Adapt.course.get('title');
If you want the plain text version. For example, the code that applies the course title to the document (broswer window/tab title) uses the plain text version.

You can also include it in a handlebars template by putting {{import_adapt}} in the .hbs file (usually at the top) then using {{{Adapt.course.displayTitle}}} or {{Adapt.course.title}} where you want to show it.

You can do something similar in the .json files as well, e.g. try adding {{import_adapt}}{{{Adapt.course.displayTitle}}} into the body of a text component.

BTW you haven't said what version of Adapt you're using, I've assumed it's fairly recent but if it's quite old (v2.x) then some of this won't work

Picture of Chris Gillison
Re: Get Course Title programmatically
by Chris Gillison - Wednesday, 2 December 2020, 7:54 PM
 

Hi Matt,

This is great! Can you do a similar thing with offlineStorage?

I'm using the nameInput extension. Iser fills in the text field then hits a submit button with the code

Adapt.offlineStorage.set('_username', this.getUserInput());

I want to be able to call the retrieve the _username in the .json files if possible. I tried

{{import_adapt}}{{{Adapt.offlineStorage._username}}}

in the .json component body, but nothing displayed.

Picture of Chris Gillison
Re: Get Course Title programmatically
by Chris Gillison - Wednesday, 2 December 2020, 9:59 PM
 

So I've managed to set & retrieve the username by changing the button code to:

Adapt.course.set('_username', this.getUserInput());

and using the format illustrated above to retrieve the data:

{{import_adapt}}{{{Adapt.course._username}}}

So, is that a 'bad' way to proceed (e.g. impacts elsewhere) or is that ok?

It's framework 4.3.0 BTW, and I guess I should say I'm trying to set something up whereby a client without an LMS can have personalised content & certificate.

Many thanks.

Picture of Oliver Foster
Re: Get Course Title programmatically
by Oliver Foster - Thursday, 3 December 2020, 10:15 AM
 

That seems fine.

{{import_adapt}}

Only imports the json from the models.

 

You could also register a helper with Handlebars.
https://handlebarsjs.com/guide/#custom-helpers

Handlebars.registerHelper('myextensionprefix_username', () => this.getUserInput());

Then:

{{myextensionprefix_username}}

Picture of Chris Gillison
Re: Get Course Title programmatically
by Chris Gillison - Thursday, 3 December 2020, 10:27 AM
 

Great. Thanks Oliver.