Custom theme login block on every page to use $SESSION->wantsurl

Custom theme login block on every page to use $SESSION->wantsurl

od Andreas Giesen -
Število odgovorov: 3

Hi! 

I have a custom theme with a login on every page with the code derived from the login block. Now - I suppose since the login block is usually meant only for the frontpage - the user is redirected to the frontpage after login, not using the $SESSION->wantsurl to redirect to where the user came from. 

Meaning: if the user opens an URL to a course that needs logging in, the login page appears and the user is redirected to the course - so this works. 

But if the user opens a course area URL and logs in through the "global login block" there is no redirection back to the course area. 

This is important, as the course area has a cloned theme and the user should stay within that environment. The way it is now the user might not find the way back to where he came from and gets the impression to be "somewhere completely different" due to the different theme (that has different logo etc.)

So: how can I modify the login block code to redirect to where the user came from in any case. Ideally utilizing the means already present in the moodle code without modification to login/index.php - but I would consider having a modified login/index2.php and sending to that one from the login form if that is the only way to achieve what I need. 

I played around with $_server contents to add to the submitted URL, but probably due to the $SESSION->wantsurl working with session content that seems not to work out. 

Any solutions or ideas for that kind of thing? 

(Moodle 3.1.2 Theme Lambda)


Povprečje ocenitev: -
V odgovor na Andreas Giesen

Re: Custom theme login block on every page to use $SESSION->wantsurl

od Andreas Giesen -

The more I look into this the less I understand what the problem is. 

First thing I noticed was that the course category page I am coming from to login had the $_SESSION['wantsurl'] empty. Thus I filled it with $_SERVER['REQUEST_URI'] as that is the value I found it to have on the login page itself. In my understanding of sessions this value should be available in the /login/index.php now same way it is when coming from a course page that needs login. 

This is (e.g.) where I am trying to get back to: "/course/index.php?categoryid=2" which is the value of $_SESSION['wantsurl']

I suspect this should be rather easy to solve if one knows whats happening in the /login/index.php - actually I think this should be standard behaviour of the login block. One could then add the login block on every page - in order to wherever you jump to you could login directly and get back where you came from. 

V odgovor na Andreas Giesen

Re: Custom theme login block on every page to use $SESSION->wantsurl

od Andreas Giesen -

I actually had it working shortly with the following code, but then it stopped working again. 

-----------------------------------

In theme header.php add at the top: 

$_SESSION['backtocat'] = $_SERVER['REQUEST_URI'];


In /login/index.php add at the top: 

if (isset($_SESSION['backtocat'])) {

$SESSION->wantsurl = $_SESSION['backtocat']; }


Since the change in /login/index.php only occurs if the session variable is set and that variable is unique and new, I suppose this should not break anything. 

-----------------------------------

I am sure now, there should be an easy way, but I keep getting stuck again... 

Could it be that sometimes for some reason the session breaks and sometimes not?