execute custom function after clicking a button

execute custom function after clicking a button

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


評比平均分數: -
In reply to Andreas Panagiotopoulos

Re: execute custom function after clicking a button

Mark Johnson發表於
Core developers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 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.

評比平均分數:Useful (1)