General developer forum

Create/handle button in view.php

Richard Jones
Re: Create/handle button in view.php
Particularly helpful MoodlersPlugin developersTesters

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.

Average of ratings: -
Picture of Peter Bri
Re: Create/handle button in view.php

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.


Average of ratings: -
Richard Jones
Re: Create/handle button in view.php
Particularly helpful MoodlersPlugin developersTesters

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):

Hope this will help you get started.


Average of ratings: -