simple ajax.call()

simple ajax.call()

by Matti Jordman -
Number of replies: 4

Hi

I try to read simple text data from database using ajax

Here's js code:

define(['jquery', 'core/notification','core/ajax'],
       function($, notification, ajax) {
    function Ajaxcall() {
        this.value = "ajax ok";
    };
    Ajaxcall.prototype.loadcontent = function() {
        ajax.call([{
            methodname: 'mod_testtest_loadsettings',
            args: {}, //how to add item id here?
            done: console.log("ajax done"), //how can i get the db text as return value?
            fail: notification.exception
        }]);
    };
    return Ajaxcall;

});

I guess that loadsettings function should be in the external.lib file. It would be really nice if some one could write the minimum content of that file. 

DB query shall be something like this:

$sql = 'SELECT content FROM {testtest} WHERE id = ?';
$params = array($itemID);
$db_result = $DB->get_records_sql($sql,$params);

Thanks

Average of ratings: -
In reply to Matti Jordman

Re: simple ajax.call()

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The Web Services documentation provides a pretty good guide to this. Look at the "create_groups" example. You need to define a class with 3 public static methods: Your function (loadsettings()), the parameter definitions (loadsettings_parameters()) and the return definition (loadsettings_returns()).

Note, you'll also need to declare the function in db/services.php for your plugin (also covered in the documentiation).

In reply to Mark Johnson

Re: simple ajax.call()

by Matti Jordman -

Thanks

Here's my code at this stage. Not working yet. Grateful for any extra help.

define(['jquery', 'core/notification','core/ajax'],
       function($, notification, ajax) {
    function Ajaxcall() {
        this.value = "ajax ok";
    };
 
    Ajaxcall.prototype.loadsettings = function() {
        ajax.call([{
            methodname: 'mod_testtest_loadsettings',
            args: {itemID: 1},
            done: console.log("ajax done"),//how can I get the result?
            fail: notification.exception
        }]);
    };
    return Ajaxcall;
});


<?php
require_once("$CFG->libdir/externallib.php");
class mod_testtest extends external_api {
    public static function loadsettings_parameters() {
        return new external_function_parameters(
            array(
            'settings' => new external_multiple_structure(
                    new external_single_structure(
                        array(
                            'itemID' => new external_value(PARAM_INT, 'id of current activity'),
                        )
                    )
                )
            )
        );
    }
    public static function loadsettings_returns() {
        return new external_multiple_structure(
            new external_single_structure(
                array(
                    'content' => new external_value(PARAM_RAW, 'settings content text'),
                )
            )
        );
    }
        
    public static function loadsettings($itemID) {
        global $CFG, $DB;
        //$params = self::validate_parameters(self::getExample_parameters(), array());
        $params = self::validate_parameters(self::loadsettings_parameters(), array('settings'=>$itemID));
        $sql = 'SELECT content FROM {testtest} WHERE id = ?';
        $params = array($itemID);
        $db_result = $DB->get_records_sql($sql,$params);
        
        return $db_result;
    }
    
}


<?php
$services = array(
      'mypluginservice' => array(                      //the name of the web service
          'functions' => array ('mod_testtest_loadsettings'), //web service functions of this service
          'requiredcapability' => '',                //if set, the web service user need this capability to access 
                                                     //any function of this service. For example: 'some/capability:specified'                 
          'restrictedusers' =>0,                      //if enabled, the Moodle administrator must link some user to this service
                                                      //into the administration
          'enabled'=>1,                               //if enabled, the service can be reachable on a default installation
       )
  );
$functions = array(
    'mod_testtest_loadsettings' => array(
        'classname' => 'mod_testtest_external',
        'methodname' => 'loadsettings',
        'classpath' => 'mod/testtest/externallib.php',
        'description' => 'Load settings data',
        'type' => 'read',
        'ajax' => true,
        'loginrequired' => true,
    )
);
In reply to Matti Jordman

Re: simple ajax.call()

by Matti Jordman -
In reply to Matti Jordman

Re: simple ajax.call()

by Matti Jordman -

Problems fixed. Here's the whole module if someone else needs super simple ajax routines.

I'll remove previous drive documents.