After login redirects

After login redirects

by Lucie Landsmanová -
Number of replies: 4

Hello.

I have a guest access available at my courses, as well as autoenroll plugin and it does not work as expected. However, I would like to ALWAYS redirect my users to homepage after they log in, not to the last place visited, since I do not want a user to come back to the course available just for guests as registered user. 

A user gets redirected to homepage only if he registers and it is his first time, not if he clicks on the login btn from anywhere on the website where he can get as a guest. 

Is anything like that possible, please?

Thank you!

Average of ratings: -
In reply to Lucie Landsmanová

Re: After login redirects

by Dmitrii Metelkin -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
I don't know any existing plugin that does that. However, this can be implemented as part of Tool Redirects https://moodle.org/plugins/tool_redirects
I have opened this issue https://github.com/catalyst/moodle-tool_redirects/issues/23
If you have a dev capacity, then please feel free to submit a patch.
In reply to Dmitrii Metelkin

Re: After login redirects

by Lucie Landsmanová -
I adjusted the code in login/index.php as follows:

/// Let's get them all set up.
complete_user_login($user);

\core\session\manager::apply_concurrent_login_limit($user->id, session_id());

// sets the username cookie
if (!empty($CFG->nolastloggedin)) {
// do not store last logged in user in cookie
// auth plugins can temporarily override this from loginpage_hook()
// do not save $CFG->nolastloggedin in database!
} else if (empty($CFG->rememberusername)) {
// no permanent cookies, delete old one if exists
set_moodle_cookie('');
} else {
set_moodle_cookie($USER->username);
}

// this adds custom redirection to homepage after logging in
$homepage_url = new moodle_url('/');
redirect($homepage_url);

$SESSION->wantsurl = $homepage_url->out();
redirect(new moodle_url(get_login_url(), array('testsession' => $USER->id)));

and it seems to work as expected, I will test it more during the week.
In reply to Lucie Landsmanová

Re: After login redirects

by Dmitrii Metelkin -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Yes, you can customise Moodle core to do whatever you need, but generally this is not the best way long term.