Create/handle button in view.php

Re: Create/handle button in view.php

by Peter Bri -
Number of replies: 3

Thanks Richard.

Used and mostly understood your example on how to handle a button click. But how to do something else than alert()?

e.g. execute on button click a db query in a php file and display the result of the query next to the button on the same page? I guess AJAX is needed!?

I worked with ajax a few years ago, but I dont't get the way Moodle uses it. I should use ajax.call from core/ajax which  directly calls an existing webservice - do I really have to write a new webservice for this simple job?

Moodle documentation says 'webservices enable external systems to login to moodle and to execeute actions' - what external systems? Generally, why do I have to enable access for external systems when I want to use AJAX?

Thanks.

Peter

In reply to Peter Bri

Re: Create/handle button in view.php

by Richard Jones -
Picture of Plugin developers Picture of Testers

Hi Peter

Maybe we are at cross-purposes, I'm not sure why you need any JavaScript at all just to execute and display the results of a DB query, especially as you say you are developing an activity module.  That can all be handled in PHP.

1) should view.php contain html tags like survey view.php? Shouldn't a custom renderer create something like this?

Hmmm, where to start?  view.php is usually the main page for your activity.  Typically it might contain programming logic.  You would likely use the renderer.php file for display only (an MVC type approach).  Strictly speaking you only want display stuff there (perhaps using the output renderer and functions of html_writer (that includes your singlebutton, for example).  For simple output you can just use $OUTPUT in the view.php (see the Page API for detail).

Forms can go into any page but you are most likely to add a form and process it in the same page.  I'm not sure where that ties in to the activity you are creating.

>>Also tried to add a button using this...<<

You might want to check out the html_writer::singlebutton() method and provide a url that takes you either back to your page or to another page when the button is clicked.

Your reference to webservices is (to me anyway) obscure - are you just  writing a simple activity  module?

I'm not sure I'm helping but will continue to try.




In reply to Richard Jones

Re: Create/handle button in view.php

by Peter Bri -

Hi Richard

yes, I am just trying to write a simple activity module, which in frist step only displays a button and executes a DB query on button click. The result of the query should be displayed on the same page as the button - in a div or something.

Because the button is clicked on client side, I thought JS should be used to handle it. I created an AMD module as shown in your example but then got stuck in ajax and webservice stuff.

Generally I'm not sure when JS should be used and how ajax is tied to webservices in Moodle. I read Moodle should work when JS is disabled in client browser.

Thanks a lot for your help.

Peter

In reply to Peter Bri

Re: Create/handle button in view.php

by Richard Jones -
Picture of Plugin developers Picture of Testers

Here's what I can suggest:

1. Download moodle-mod_NEWMODULE from Moodle HQ's GitHub account.  

2.  Follow the instructions carefully and test that it installs and works.

3. Near the lines 'Yay, it works!' add something like this:

// Replace the following lines with you own code.
echo $OUTPUT->heading('Yay! It works!');
$link = new moodle_url('/mod/widget/view.php', ['id' => $cm->id]);
echo html_writer::link($link, get_string('click', 'mod_widget'), ['class' => 'btn btn-primary']);

You will have a button that sends you back to the view.php page (ie the one you are already on).  What you could then do is copy and rename the view.php page and modify it to display data you obtain from the database, say in a table.  

Other html_writer functions can be found here (or browse the source code):

http://xref-diff.mukudu-dev.net/

Hope this will help you get started.

Richard