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


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


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

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