Picture of Russell England
Moodle 2.9+ and AJAX and jquery
by Russell England - Wednesday, 9 March 2016, 2:31 PM
 

Hi chaps

I'm new to Adapt.

I just wondered if there is any Javascript code examples for communicating with Moodle 2.9+ / Totara via AJAX from Adapt?

 

Moodle 2.9+ recommends using a webservice for AJAX : https://docs.moodle.org/dev/AJAX

So I have created a web service and created a Javascript AMD module, also recommended for Moodle 2.9+ : 

https://docs.moodle.org/dev/Javascript_Modules

There is an example here : https://docs.moodle.org/dev/User:Damyon_Wiese/Ajax_Service

 

So for example, in /local/myplugin/classes/external.php, I have a method called get_hello_world that returns 'woohoo'.

So in /local/myplugin/amd/src/hello_world.js I have something like:

// Hello world module.
define(['jquery', 'core/ajax'], function($, ajax) {

    return {

        get_hello_world: function() {

            var deferred = $.Deferred();

            var ajaxrequests = [{
                    methodname: 'local_myplugin_get_hello_world',
                    args: {}
                }];

            var deferreds = ajax.call(ajaxrequests);
            $.when.apply(null, deferreds).done(
                function() {
                    // External web service returns an array of objects
// So grab the first row.
var result = arguments[0];
// result.hello_world now contains 'woohoo' deferred.resolve(result); } ).fail( function(ex) { deferred.reject(ex); } ); return deferred.promise(); } }; });

 

Then to test this works, in /local/myplugin/amd/src/hello_world_test.js I have something like

 

define(['jquery', 'local_myplugin/hello_world'], function($, gethelloworld) {

    var component = {
        init: function() {

            gethelloworld.get_hello_world().done(function (result) {
                $('#testhelloworld').text(result.hello_world);
                // #testhelloworld now contains 'woohoo'.
                // How can I transfer this value to adapt?
            });

        }

    };

    return component;
});

But I don't know how Adapt can pick up the value?

Cheers, Russ

Picture of Matt Leathes
Re: Moodle 2.9+ and AJAX and jquery
by Matt Leathes - Wednesday, 9 March 2016, 3:23 PM
 

Kind of depends what you want to do with that data in Adapt...

Picture of Russell England
Re: Moodle 2.9+ and AJAX and jquery
by Russell England - Wednesday, 9 March 2016, 3:56 PM
 

For now just display it, but I will also need to send values too.

I guess I should be asking how to use the Javascript module from Moodle in Adapt?

Mark
Re: Moodle 2.9+ and AJAX and jquery
by Mark Lynch - Wednesday, 9 March 2016, 4:14 PM
 

Hi Russell,

What are you trying to do, are you going to host an Adapt course someplace outside of Moodle and then send data from the Adapt course to Moodle?

Picture of Russell England
Re: Moodle 2.9+ and AJAX and jquery
by Russell England - Thursday, 10 March 2016, 3:52 PM
 

Hi Mark

I think the Adapt will be used as a Scorm package in Moodle.

Picture of Matt Leathes
Re: Moodle 2.9+ and AJAX and jquery
by Matt Leathes - Wednesday, 9 March 2016, 4:48 PM
 

I think you probably need to develop an Adapt 'extension' plugin.

It's good that what you have already is the same format that Adapt will require i.e. an AMD module

There is some documentation on the Wiki about plugin development but it's not fantastically up-to-date. Most of us either look at existing examples and then ask questions of each other - but if you feel like taking on updating the documentation that would be wonderful!

Picture of Russell England
Re: Moodle 2.9+ and AJAX and jquery
by Russell England - Wednesday, 9 March 2016, 6:06 PM
 

Cheers Matt - I'll have a look through here - https://github.com/adaptlearning/adapt_framework/wiki/Developing-plugins

Picture of John Niezen
Re: Moodle 2.9+ and AJAX and jquery
by John Niezen - Thursday, 10 March 2016, 7:18 AM
 

Hi Russ,

Still not sure what you are going to do, to give you some other pointers, our team has created Scorm packages with Adapt, created a "course" in Moodle and start it from there. We use Adapt pure as content creation.John

Picture of Russell England
Re: Moodle 2.9+ and AJAX and jquery
by Russell England - Thursday, 10 March 2016, 4:38 PM
 

Hi John

The Adapt course will be used as a Scorm package.

But we will want to retrieve some values from Moodle via Ajax, to display on the Adapt page.

Actually, a good example would be to retrieve strings via ajax. There is already a core module in Moodle 2.9+ for this.

The module is ['core/str']

The code is in /lib/amd/src/str.js

https://github.com/moodle/moodle/blob/MOODLE_29_STABLE/lib/amd/src/str.js

So in theory I could use something like this in Moodle.

define(['jquery', 'core/str'], function($, mdlstrings) {
    var savebtn = {
        init : function() {
            $('#savebtn').text(mdlstrings.get_string('save', 'moodle'));
        }
    }
});

But I'm not sure how to use the Moodle AMD code in Adapt?

 

Picture of John Niezen
Re: Moodle 2.9+ and AJAX and jquery
by John Niezen - Friday, 11 March 2016, 7:28 AM
 

Hi Russel,

So to make it clear for me, you let the Adapt SCORM package start from Moodle, and then let them interact with eachother? Sounds more like TinCan, and afik Spoor does not do that. Might want to ask in the xAPI room https://gitter.im/adaptlearning/xAPI. Maybe they can point you forward, but it looks like some work to be done.

Picture of Matt Leathes
Re: Moodle 2.9+ and AJAX and jquery
by Matt Leathes - Friday, 11 March 2016, 10:02 AM
 

Hi John

It's actually more the case that the SCO needs to be moderately 'dynamic' in that it will need to read in data about the user that is well beyond the bounds of what is available with SCORM and then display it in some way.

Quite possibly you could do all this with xAPI but from what I understand from having spoken to Russell separately is that the data is coming from a pre-existing system, so converting this into in xAPI compatible format would be considerably more work that can really be justified.