Search code examples
phpmoodlemoodle-api

Moodle: Using the returnto query parameter to redirect back to my local plugins index after course edit


tl;dr; Trying to link the 'save and return' button when editing/deleting a course to my local plugins index.php instead of moodles default redirect for these features, moodle allready has a returnTo query parameter so i was thinking if that could be used somehow.

Hey

I am creating a local plugin that has a administration panel, where you can access CRUD on all courses in the system as seen in the picture below: enter image description here

The problem now is that whenever I click edit, I get into the course edit page of course, but when I return from there I click "save and return" I would like to get back to my own admin page instead of the course page or category manage page.

The code I have right now looks like this:

//edit
$edit_course_moodle_url = new moodle_url('/course/edit.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$edit_course_url = $edit_course_moodle_url->get_path() . '?id=' . $edit_course_moodle_url->get_param('id') . '&returnto=' . $edit_course_moodle_url->get_param('returnto');

//delete
$delete_course_moodle_url = new moodle_url('/course/delete.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$delete_course_url = $delete_course_moodle_url->get_path() . '?id=' . $delete_course_moodle_url->get_param('id') . '&returnto=' . $delete_course_moodle_url->get_param('returnto');

As you can see I use the "returnto" query parameter, normally moodle has a "catmanage" as "returnto" that returns you to the category management page, where moodle has its own CRUD for categories and courses. So my question is, can I create my own alias for a link and use it like moodle uses the catmanage link, but for my admin page instead.

Thanks a lot ! :)

EDIT:

Change code to the following:

if (empty($CFG->loginhttps)) {
    $securewwwroot = $CFG->wwwroot;
} else {
    $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
}
$returnurl = new moodle_url($securewwwroot . '/local/adminpanel/index.php');

$edit_course_moodle_url = new moodle_url($securewwwroot . '/course/edit.php', array(
            'id' => $course->id,
            'sesskey' => sesskey(),
            'returnto' => 'url',
            'returnurl' => $returnurl->out(false))
        );

        $edit_course_url =  $edit_course_moodle_url->out();

But it looks like moodle took away the button from edit course called "save and return" now it only has "save and display" or "Cancel" , both of which brings me back to the course, sad times :(


Solution

  • According to the code I can see in course/edit.php, you should use the following URL arguments:

    • returnto: 'url'
    • returnurl: The url
    • sesskey: sesskey()

    In code that gives us:

    $returnurl = new moodle_url('/local/plugin/page.php');
    $editurl = new moodle_url('/course/edit.php', array(
        'id' => 2,
        'sesskey' => sesskey(),
        'returnto' => 'url',
        'returnurl' => $url->out(false)
    ));
    echo $editurl->out();
    

    The page course/delete.php does not seem to support those arguments. But it's probably easier for your plugin to delete the course by itself, it's as simple as calling delete_course($courseid);.