Moodle 2.6 action link

Moodle 2.6 action link

by Ivana Skelic -
Number of replies: 12

Hello,

I'm trying to implement sending email (to multiple users) from a block using simple action link but I'm not sure how to do it. I tried some combinations with action_link and add_action but didn't make it.

I would like my link to call my function on click.  Can somebody point me in the right direction please?

Average of ratings: -
In reply to Ivana Skelic

Re: Moodle 2.6 action link

by Darko Miletić -

Could you elaborate a bit further? Is this action executing some third party script? If yes let us see it.

Action link offers 3 possible actions:

component_action - used to attach custom javascript method to action

confirm_action - used to display confirmation dialog before executing link

popup_action - used to open new window based on the supplied parameters


Example of popup action would be this:

// print's a popup link to your custom page
echo $OUTPUT->action_link('/path/to/custompage.php', 'clickit', new popup_action('click', '/path/to/custompage.php'));




In reply to Darko Miletić

Re: Moodle 2.6 action link

by Ivana Skelic -

This action should execute a function which will send email to users registered as a teacher on a specific course.

If I am using code like this can i declare send_email function in my lib.php file?

$action=new component_action('click','send_email',array($USER->id);

In reply to Ivana Skelic

Re: Moodle 2.6 action link

by Darko Miletić -

You got it wrong. component_action works only with javascript. It expects a name of an existing javascript function and set of parameters.

What you need here is something else. You need a javascript function that will issue ajax call to custom php file which will in turn send those emails.


In reply to Darko Miletić

Re: Moodle 2.6 action link

by Darko Miletić -

To do this you need:

  • Javascript method
  • ajax php file
  • minor changes in your block
// Place this in new js file located in your block 
function block_myblock_sendemail(e) {
    e.preventDefault();

    Y.log('Enetered method');

    var ioconfig = {
        method: 'POST',
        data: {'sesskey' : M.cfg.sesskey},
        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!');
            }
         }
    };

    Y.io(M.cfg.wwwroot + '/blocks/myblock/myajaxscript.php', ioconfig);
}


// Create new php file called myajaxscript.php and place it with your block
define('AJAX_SCRIPT', true);
require_once('/path/to/config.php');

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

//Do your thing here
$result = true;
echo $OUTPUT->header();
echo json_encode(array('result' => $result));


// Finally add this to your get_content method of the block:
$this->page->requires->js('/blocks/myblock/myscript.js');
//some other code
$this->content->text = $OUTPUT->action_link('/blocks/myblock/myajaxscript.php', 'clickit', new component_action('click', 'block_myblock_sendemail'));


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

Re: Moodle 2.6 action link

by Ivana Skelic -

Help needed again. After I've done what you wrote, I get the following error:

{"error":"A required parameter (sesskey) was missing","stacktrace":"* line 463 of \/lib\/setuplib.php: moodle_exception thrown\n* line 545 of \/lib\/moodlelib.php: call to print_error()\n* line 70 of \/lib\/sessionlib.php: call to required_param()\n* line 81 of \/lib\/sessionlib.php: call to confirm_sesskey()\n* line 7 of \/blocks\/helpdesk\/sendemail.php: call to require_sesskey()\n","debuginfo":"\nError code: missingparam","reproductionlink":"http:\/\/localhost:8888\/moodle\/"}

I'm working on a local MAMP platform

In reply to Ivana Skelic

Re: Moodle 2.6 action link

by Darko Miletić -

The error is clear:

A required parameter (sesskey) was missing

Try changing the data part of AJAX defintion to look like this:


        data: 'sesskey=' + M.cfg.sesskey,



In reply to Darko Miletić

Re: Moodle 2.6 action link

by Ivana Skelic -

Additional question to this. Can I return true/false variable from JS to block php file?

In reply to Ivana Skelic

Re: Moodle 2.6 action link

by Darko Miletić -

No. If you need to present something to the user as a result of click you should do it in javascript, use simple alert like in the example I posted or alter html of block itself. For example if you want to add new message inside block than you need to add it dynamically. See this example in YUI docs

http://yuilibrary.com/yui/docs/node/#create 

In reply to Darko Miletić

Re: Moodle 2.6 action link

by Ivana Skelic -
Another question smile Is it possible to send some arguments to .php script in actionlink?

Similar to this:

$PAGE->set_url('/mod/book/tool/validator/bcindex.html', array('cmid'=>$cmid, 'chapterid'=>$chapterid));

In this part of action link url:

'/blocks/myblock/myajaxscript.php'
In reply to Ivana Skelic

Re: Moodle 2.6 action link

by Darko Miletić -

Of course.


// Change in link generation code.
echo $OUTPUT->action_link(new moodle_url('#'), 'clickit', new component_action('click', 'block_myblock_sendemail', array('param' => 123, 'param2' => 223)));

// Change in Javascript
function block_myblock_sendemail(e, args) {
    e.preventDefault();
//....
    var ioconfig = {
        method: 'POST',
        data: 'sesskey='  + M.cfg.sesskey + 
                  '&param='   + encodeURIComponent(args.param) + 
                  '&param2=' + encodeURIComponent(args.param2),

//....
}