Yui IO interaction.

Yui IO interaction.

by Marcus Santos -
Number of replies: 5

Hello, I started doing plugins for moodle at php but now that I've been asked to develop an editor plugin i'm having some problems since i am not quite familiar with js, and not familiar at all with YUI.

Scenario: I have to insert some data into moodle database. It'll be part of a suggestion system. A button at edditor will save user suggestion at database. (The activity need to be at editor because I have developed some repository plugins and this will be part of the plugin family)

I'm running tests at localhost.


Here's the php code:

<?php

define('AJAX_SCRIPT', true);

define('NO_MOODLE_COOKIES', true);

require_once(dirname(dirname(dirname(__DIR__))) . '/config.php');

global $DB;

function dbAcess($parametro){

    $record = new stdClass();

$record->sugestion = $parametro;

$lastinsertid = $DB->insert_record($parametro, $record, false);

    return $lastinsertid;

}

Here's what i'm struggling with (yui io call):

_submitData: function(e) {

            e.preventDefault();

            this.getDialogue({

                focusAfterHide: null

            }).hide();


            //receive sugestion from textbox

            var sugestion = form.one(SELECTORS.SUGESTINPUT).get('value');


            Y.io(M.cfg.wwwroot + '/lib/editor/atto/plugins/alexandria/db.php', {

                context: this,

                on: {

                    sucess: function() {                        

                        var host = this.get('host');

                        var sugestionHtml = '<p>' + 'Suggestion recorded + '</p>';

                        host.insertContentAtFocusPoint(sugestionHtml);

                        this.markUpdated();

                        return;

                    }                    

                }

            });

        }

How do I make this function to work? I need the to call the method at php file passing the var sugestion and receive the last inserted id from php as result.


Any help is welcome, Thanks.

Marcus

Average of ratings: -
In reply to Marcus Santos

Re: Yui IO interaction.

by Darko Miletić -

This thread might give you a clue

https://moodle.org/mod/forum/discuss.php?d=268543


Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Yui IO interaction.

by Marcus Santos -

Hello  Darko,


Your example helped me a lot but i'm still having some issues. The firebug (debug) return 

500 Internal Server Error
and hence failure response.


Could this caused by localhost issues?

Besides, I guessed that php would receive the param from post the same way it would receive from  JQuery. 

using: $parametro = $_POST['suggestionEntry'];

This is the IO adapted from the link you suggested:

Y.io(M.cfg.wwwroot + '/lib/editor/atto/plugins/alexandria/db.php', {

                method: "POST",

                data: 'suggestionEntry=Teste 123',

                on: {

                    success: function (o, response) {

              //OK

              var data = Y.JSON.parse(response.responseText);

              if (data.result) {

                alert('Result is OK!');

              }

            },

            failure: function (o, response) {

              alert('Not OK!');

                     }


                }

            });


and this is the php:

<?php

define('AJAX_SCRIPT', true);

define('NO_MOODLE_COOKIES', true);


require_once(dirname(dirname(dirname(__DIR__))) . '/config.php');

require_login(null, false, null, false, true);

require_sesskey();

global $DB;

$parametro = $_POST['suggestionEntry'];


$record = new stdClass();

$record->sugestion = $parametro;

$lastinsertid = $DB->insert_record('mdl_sugestions', $record, true);


$result = true;

echo $OUTPUT->header();

echo json_encode(array('result' => $result));

  

Any clue on the reason why it's returning this 500 Internal Error ? As soon as possible I'll post Tomcat log.


In reply to Marcus Santos

Re: Yui IO interaction.

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

You should check your web server error log for 500 errors. Thats a lot faster than trying to figure it out by looking at the code.

Its hard to tell what is causing the error from here, but this line

$DB->insert_record('mdl_sugestions', $record, true);

should probably be

$DB->insert_record('sugestions', $record, true);


Average of ratings: Useful (1)
In reply to Marcus Santos

Re: Yui IO interaction.

by Darko Miletić -

Your PHP code is incorrect. Here is the list of issues:

define('NO_MOODLE_COOKIES', true);

Why are you adding this? With this present you are essentially disabling Moodle sessions so require_login will always fail.

$parametro = $_POST['suggestionEntry'];

This is security risk. Parameters in Moodle are handled in special way.

$parametro = required_param('suggestionEntry', PARAM_TEXT);

When using DB API table names are without prefix. (no mdl_ or anything else).

So here is the fixed version:

define('AJAX_SCRIPT', true);
require_once(dirname(dirname(dirname(__DIR__))) . '/config.php');

require_login(null, false, null, false, true);
require_sesskey();

$parametro = required_param('suggestionEntry', PARAM_TEXT);

$record = new stdClass();
$record->sugestion = $parametro;

$lastinsertid = $DB->insert_record('sugestions', $record, true);

$result = true;

echo $OUTPUT->header();

echo json_encode(array('result' => $result));







Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Yui IO interaction.

by Marcus Santos -

Now it works! Problem was the  define('AJAX_SCRIPT', true); wich I got in a db topic here at the forum but it seems it was specific for that case. There was also the prefix "mdl_" that I forgot to remove from the table name.

Plus I corrected the code from $_POST to required_param().


You really saved me there.

Thanks Darko and Justin.