Sunday, March 10, 2013

PATCH: Fix default homepage for users when using "force login"

At the moment, a certain feature is broken in the stable release of Moodle 2.4.1.
If you enable the "force login" setting on the site, users will always be directed to the site home.
This is bad if you have also allowed users to choose their own homepage -- even if they have chosen the "my moodle" page as a default homepage, they will never be redirected there upon login.

Just as a recap for those settings:

  •  "site administration -> appearance -> navigation". Set the default homepage to:  user preference.
  • "site administration -> security -> site policies. Enable the setting called: Force users to login

I have followed the trackers on moodle.org for a solution and a patch has recently been uploaded. AND it works!
https://tracker.moodle.org/browse/MDL-37983

Here are the steps to implement the patch:
1)  Go to /login/index.php file.
2)  Around line 209, we are going to add some code.
This is the original code:


            // no wantsurl stored or external - go to homepage
            $urltogo = $CFG->wwwroot.'/';
            unset($SESSION->wantsurl);

            $home_page = get_home_page();

            // Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my
            if ($home_page == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {
                if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') 
                {
                    $urltogo = $CFG->wwwroot.'/my/';
                }
       } 
}

AND here is the new code. I will highlight all the new parts of code you must add.


           // no wantsurl stored or external - go to homepage
            $urltogo = $CFG->wwwroot.'/';
            unset($SESSION->wantsurl);
        }

        // If the url to go to is the same as the site page, check for default homepage.
        if ($urltogo == ($CFG->wwwroot . '/')) 
        {
            $home_page = get_home_page();

            // Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my
            if ($home_page == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {
                if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') 
                {
                    $urltogo = $CFG->wwwroot.'/my/';
                }
       } 
}

3) Now the site will save and display the user's preferred homepage. Yay!

No comments:

Post a Comment