numbas / editor

An editor for the Numbas e-learning system.
http://numbas.mathcentre.ac.uk
Apache License 2.0
59 stars 61 forks source link

An interface for extensions to provide new part types. #291

Closed christianp closed 2 years ago

christianp commented 8 years ago

It would be nice if it was possible to add new part types with extensions. As well as the code for the Numbas runtime, which I reckon you can already add, we also need code to display the part, which should belong to the theme, and a specification of an editing interface for the question editor to use. At the moment, part types in the editor are defined with by an object with a few settings: a name, a list of tabs, a knockout viewmodel, and functions to save and load to/from the question JSON format. Then there's a django template to render the corresponding HTML The only bits of that that I'm not comfortable with exposing publicly are the knockout viewmodel and the django template - both are very implementation-specific, and would cause a headache if I changed the way the editor works in future. A JSON format to specify just what the options are, without the ability to give any code to do anything clever with the UI, would at least allow a basic editor interface to be created in a fairly future-proof way. And if we're restricting the format of the options, then we can write generic code to load and save the part.

So, I imagine an extension package which adds a new part type would look like this:

myextension_part_types.json will look like the following:

[
    {
        name: 'mypart',
        niceName: 'My part type',
        has_marks: true,
        tabs: [
            {name: 'marking', niceName: 'marking', icon: 'pencil', fields: ['correctanswer','precision']},
            {name: 'extras1', niceName: 'Extra options 1', icon: 'asterisk', fields: ['magicword','requiremagicword']}
        ],
        fields: [
            {
                name: 'correctanswer',
                label: 'Correct answer',
                type: 'jme',
                show_preview: true,
                help_text: 'The answer the student should write'
            },
            {
                name: 'precision',
                label: 'Checking precision',
                type: 'number',
                min: 0,
                max: 100,
                help_text: "How accurate should the user's answer be, in microsmoots?"
            }
            {
                name: 'magicword',
                niceName: 'Magic word',
                type: 'string',
            },
            {
                name: 'requiremagicword',
                niceName: 'Require the magic word?',
                type: 'boolean',
                default: true
            }
        ]
    }
]

The editor would interpret this spec to create the relevant viewmodel and HTML, and the exam compiler and runtime would use it to create the XML/HTML representation of the part as well as the Numbas.Part class to actually run it.

christianp commented 2 years ago

These days, extensions bundle a .cpt file containing a definition of a part type.