execute custom function after clicking a button

execute custom function after clicking a button

by Andreas Panagiotopoulos -
Number of replies: 1

Hello to all,

I have created a custom course report and I want to execute a custom function, which enrolls users to specific cohort, after clicking a button.

So, I want this:

if_I_click_the_button {

execute function

}

Something like 'Run now' on tasks page. This is the php code of 'run now':

            $runnow = '';

            if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {

                $runnow = html_writer::div(html_writer::link(

                        new moodle_url('/admin/tool/task/schedule_task.php',

                            array('task' => get_class($task))),

                        get_string('runnow', 'tool_task')), 'task-runnow');

            }

So, I want the same but not to 'call' moodle_url but a specific function.

How can I do this??

Thank you!

Andreas


Average of ratings: -
In reply to Andreas Panagiotopoulos

Re: execute custom function after clicking a button

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

Hi Andreas,

Moodle's architecture is fairly typical of a PHP web app, in that in order to execute a function, you send a request to a URL of a .php file on the webserver, which contains the function call.

To do this in a synchronous manner (which will reload the page after you click your button), you need to either have a link to the URL as per your example above (which will be a GET request), or create a form containing the button where the form's action attribute contains the url (which will be a POST request). If your function will change data in the system, you should make a POST request, then redirect back to the original page (to prevent the page being reloaded and repeating the change).

To do this in an asynchronous manner (which will not reload the page after you click the button), you'll need to implement a web service then write a Javascript module to call it via an AJAX request when the button is clicked.

Average of ratings: Useful (1)