Submit a request
Submit a request

Creating Custom Features

This article outlines how to create your own custom feature type to be used within Learnosity's Questions API. In this context of Learnosity, by "features" we are referring to interactive elements that can be added to an assessment, such as the rulers, protractors, or calculators.

Questions API's custom feature type allows you to create your own features, giving you full control over the rendering of the feature area and the user interaction with the feature.

A custom feature is defined like any other feature type, by passing a JSON object to the Questions API. In the feature JSON, you provide a URL to a JavaScript file containing the logic to control your feature.

 
{
    "feature_id": "custom-feature-1",
    "type": "customfeature",
    "js": "//docs.learnosity.com/demos/products/questionsapi/customfeatures/textinput/index.js",
    ...
}

The JavaScript file that you provide must define an AMD module using Learnosity's namespaced require.js instance, LearnosityAmd.define. The module must define a function to act as a constructor for your feature. The module must return an object with a Feature property containing the constructor, like so:

LearnosityAmd.define(function () {

    function CustomFeature(init) {
        console.log(init);
    }

    return {
        Feature: CustomFeature
    };
});

Questions API will instantiate your feature by calling this function, passing in an object with the following properties:

state The state Questions API was initialised in (initialresume, or review).
feature The feature object to be rendered.
$el A jQuery selection containing the DOM element for your feature's response area. This is where you will render the feature.
events Backbone.js Events object. You must use this to communicate with Questions API (see below).
quesitonsApiVersion A string containing the version number of the current instance of Questions API.

Your JavaScript module must render the HTML for the feature inside the provided element. When this is complete, you must notify Questions API that the feature is ready. This example renders a simple text input, and then triggers the ready event:

init.$el.html("<input type="text" />");

init.events.trigger("ready");

 

Questions API will trigger an error event if your feature does not call the ready event within an acceptable timeframe.

You can request access to certain JavaScript libraries as part of your module definition:

LearnosityAmd.define(["jquery-v1.10.2"], function ($) {

});

 

Questions API currently exposes the following libraries:

  • jquery-v1.10.2
  • backbone-v0.9.10
  • underscore-v1.5.2
  • mathcore

You can use this module to validate math response LaTex against defined specification. Working example:

LearnosityAmd.define(['mathcore'], function (mathcore) {

        // response value
    var value = 'x+3',
        //specifications to validate against
        spec1 = {
            method: 'equivSymbolic',
            value: 'x+2+1'
        },
        spec2 = {
            method: 'equivLiteral',
            value: 'x+2+1'
        },
        spec3 = {
            method: 'equivLiteral',
            value: 'x+3'
        };

    mathcore.validate(spec1, value);
    // returns:
    // {
    //     result: true,
    //     message: 'Normal completion'
    // }

    mathcore.validate(spec2, value);
    // returns:
    // {
    //     result: false,
    //     message: 'Normal completion'
    // }

    mathcore.validate(spec3, value);
    // returns:
    // {
    //     result: true,
    //     message: 'Normal completion'
    // }

});

 

Passed value can be any valid LaTex string. Specification, from the other hand is a complex object. We encourage you to build it using UI of Math Formula feature type in Feature Editor. This way you can quickly learn what methods and options are available. To do so, simply use Validation tab to build and Source button to preview specification JSON.

You can also read full documentation of mathcore auto-grading library by clicking on the link below.

Go to Mathematics Auto-Grading knowledge base article 
 
Was this article helpful?
0 out of 0 found this helpful

Did you arrive here by accident? If so, learn more about Learnosity by clicking here.