Page redirect or "back"?

Page redirect or "back"?

by Kevin Burton -
Number of replies: 19

I have an activity plugin that works just fine as long as nothing extraordinary is attempted. At the top of one of the pages that get visited I have:

<?php
require_once("mod_mediasite_site_form.php");
$siteid = required_param('site', PARAM_INT);          // site
$context = context_system::instance();
require_login();
. . . . . .

I control going to this page but for one thing this page has a form that inherits from moodleform and as such has a cancel button. If I click on that button the page gets revisted without the required argument even though I explicitly try to catch the cancellation:

if ($mform->is_cancelled()) {
// Go home
redirect("Configuration.php");
}

So as can be seen I want the redirection to 'Configuration.php' and not a postback to the current page (edit.php). Can this be done? What am I missing with Moodle forms?

Thank you.

Average of ratings: -
In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

See here:

http://docs.moodle.org/dev/lib/formslib.php_Usage

// PLEASE NOTE: is_cancelled() should be called before get_data(), as this may return true
Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

It is called before get_data(). Here is a little bite broader view of the code involved

if ($mform->is_cancelled()) {
// Go home
redirect("Configuration.php");
}
global $OUTPUT;
echo $OUTPUT->header();
echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
echo "<tr>";
echo "<td colspan=\"2\">";
$data = $mform->get_data();
if($data) {

 

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

I'm not sure what exactly are you trying to do but this example works just fine:

 
require('config.php');
require_once($CFG->libdir.'/formslib.php');

class myform extends moodleform {
    protected function definition() {
        $mform = $this->_form;
        $mform->addElement('header', 'headdd', 'Yeah');
        $this->add_action_buttons();
    }
}

$mform = new myform();

if ($mform->is_cancelled()) {
    redirect('http://www.cnn.com/');
}

$PAGE->set_url($ME);
$PAGE->set_context(context_system::instance());
$PAGE->set_title('test');

echo $OUTPUT->header();

$mform->display();

echo $OUTPUT->footer();

 

 

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

Re: Page redirect or "back"?

by Darko Miletić -

If you have a problem with required_param redirect than you can do this in order to redirect to desired page:

 
if (optional_param('myparam', false, PARAM_INT) === false) {
    $url = new moodle_url('/blocks/myblock/somespecialpage.php');
    print_error('missingparam', '', $url->out());
}

 

 

Average of ratings: Useful (1)
In reply to Kevin Burton

Re: Page redirect or "back"?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Is the problem you are getting related to a missing 'site' param in the data posted from the form?

If so, then the best solution is to make sure that there is a hidden field on your form called 'site' which you can set via this code (in the main code of the page, outside the form definition):

$formdata = new stdClass();
$formdata->site = $siteid;
...
$mform->set_data($formdata);

Alternatively you can pass the site id into the form using customdata:

$mform = new my_form_class(null, array('site' => $siteid));

Then within your form definition() function initialise the 'site' param like this:

$mform->addElement('hidden', 'site', $this->_customdata['site']);

A third alternative (not one I'd recommend), is to get the form to post to a different location to the edit.php page, by writing:

$mform = new my_form_class($destinationurl, ...);

The downside of that is the processing isn't as tied together with the display of the form as it is now.

In reply to Davo Smith

Re: Page redirect or "back"?

by Kevin Burton -

The problem is using the optional or required param. When the cancelling the form that is on the page it seems that a post-back is made to the page without the required parameter.

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

It still not clear what is wrong here. Explain better, give a minimal code and steps to reproduce the behavior.

In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

The code at the beginning of this thread is the problem. It uses the 'require_param'. Then further down on the page I instantiate and display a form (which is derived from moodleform). This code is also shown earlier in this thread. When this form is displayed there is a 'cancel' button. If the 'cancel' button is activated I get an error that the page is called without the required parameter (I have attached a screen shot). Everything works just fine if as long as I don't click on the cancel link. The work-around for me has been to use optional_param instead. I was just wondering why this is necessary or if I have missed something? The full source for a working version is also attached. In order to recreate the error just toggle the comments between the optional and required param calls.

 

 

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

As Microsoft states quite often - This behavior is by design. What you can do is to specify custom action url for the form that will include your desired parameter. Like this:

 
$action = new moodle_url(strip_querystring($ME), array('site' => $sitevalue));
$mform = new myform($action->out());
Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

Thank you this is most helpful. 

I am assuming that 'strip_querystring' is part of the Moodle libraries. Also if there is no query string strip_querystring will just return the URL unaltered.

What should the form do with $action->out()? Since I am deriving from moodle form can I just pass it as is to the parent (which will be moodleform)?

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

strip_querystring is standard moodle function that strips query part of any url. If you dont use it url remains unaltered. I just gave you an example how to properly build your own action url with parameters. $action->out() converts the object into string URL which is what moodleform expects as action. Look at the moodle code, that is always the best source of information.

In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

Sorry I guess I don't understand the syntax here. In formslib.php I see:

function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {

I am guessing that the first argument to this method is the one we are focusing on here. So if I have something like:

class myform extends \moodleform {
function __construct($action) {
parent::__construct();
    }

I am not sure what to do with the argument to __construct? From the source I don't see a __construct I only see the above method. Is that the same as the __construct?

 

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

Why do you need to override the constructor? Just use the existing one.

In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

I need to pass in the parameters for my form so I override it.

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

but you can already do that. That can be seen from my test page with form few messages earlier in this thread. In any case this is basic OOP and outside of any Moodle specific stuff so I'll leave you to deal with it as you see fit.

In reply to Darko Miletić

Re: Page redirect or "back"?

by Kevin Burton -

When I inherit from the abstract class moodleform I get all of the properties and methods that moodleform has but it doesn't seem to have a __construct method so I am assuming that the default constructor applies. If I have some parameters that mean something to my form then the parent cannot possibly know about my parameters hence I override the __construct. I see it like this:

 

class A {}

class B extends A {

function __construct($parameters) {

// Use the B specific parameters

}

}

 

I the above A has no knowledge of B so in order to get all of A AND the new parameters of B I override (in this case there is no __construct so it is basically creating a function rather than overriding it, just like moodleform).

 

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

Well it is obvious that you are new to all this. Moodle was started in the time where PHP4 was supreme ruler and hence there is a LOT of PHP 4 code still in the core code base. One of the characteristics of PHP 4 classes is that their constructor always had a name of a class. Hence moodleform constructor is function called moodleform. 

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

Re: Page redirect or "back"?

by Kevin Burton -

You are right I am new to PHP and Moodle so please bear with me.

So in my constructor I can call parent::__constructor($action). Where $action is the string produced by $action->out()?  Will the PHP4 constructor get called when using the later type of constructor syntax.

I still don't see how I avoid overriding the constructor and still allow my parameters to get passed in to the class.

In reply to Kevin Burton

Re: Page redirect or "back"?

by Darko Miletić -

Than I guess you want something like this:

 
require('config.php');
require_once($CFG->libdir.'/formslib.php');

class myform extends moodleform {

    public function __construct() {
        $action = new moodle_url(me(), array('myparam' => 123));
        parent::moodleform($action->out());
    }

    protected function definition() {
        $mform = $this->_form;
        $mform->addElement('header', 'headdd', 'Yeah');
        $this->add_action_buttons();
    }

}

$mform = new myform();


 

 

Average of ratings: Useful (1)