Custom login and redirect to specific course

Custom login and redirect to specific course

by Simon Clegg -
Number of replies: 12
Update: Apologies! I posted this as my previous post was part of a tread started in 2004 and I thoughtt it might be a dead discussion!! blush

Hi there,

I want to create a custom login page that redirects the user to a specific course when authentication is complete.

I understand that I can create a simple form for logging in from external pages:

<form action="http://loquela.co.uk/moodle/login/index.php" method="post" name="form" id="form">
<p><input type="text" name="username" size="15" /></p>
<p><input type="password" name="password" size="15" /></p>
<p><input type="submit" name="Submit" value="Login" /></p>
</form>

But how would I redirect the user to a specific course?

Many thanks in advance for any help.

S.
Average of ratings: -
In reply to Simon Clegg

Re: Custom login and redirect to specific course

by Nicholas Harvey -
I've been trawling moodle's forums and can't find an answer to this one but there must be one!
In reply to Nicholas Harvey

Re: Custom login and redirect to specific course

by Héctor Reyes -

I have this:

1. Copy login/index.php to login/index2.php

2. Localice section /// Prepare redirection , line 177aprox. and change:

/// Prepare redirection

if (user_not_fully_set_up($USER)) {
$urltogo = $CFG->wwwroot.'/user/edit.php';
// We don't delete $SESSION->wantsurl yet, so we get there later

} else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
$urltogo = $SESSION->wantsurl;    /// Because it's an address in this site
unset($SESSION->wantsurl);

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

by

/// Prepare redirection

if (user_not_fully_set_up($USER)) {
$urltogo = $CFG->wwwroot.'/user/edit.php';
// We don't delete $SESSION->wantsurl yet, so we get there later

} else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
$urltogo = $SESSION->wantsurl;    /// Because it's an address in this site
unset($SESSION->wantsurl);

} else {
// no wantsurl stored or external - go to homepage
$urltogo = $CFG->wwwroot.'/auth/mnet/jump.php?hostid=3';
unset($SESSION->wantsurl);
}

3. In your form to login change ( i have aardvark_pro 1.5 theme, so the file to change is profileblock.php) index.php by index2.php

4. Finish., but in my case i needed /auth/mnet/jump.php?hostid=3 but i suppose you can use /course/view.php?id=XXXX where XXXX is course id

In reply to Héctor Reyes

Re: Custom login and redirect to specific course

by khalil baraket -

Hello,

If I don't have a theme, in which file would I change index.php by index2.php? I don't have profileblock.php in the default moodle.

Thanks a lot for your help!

In reply to khalil baraket

Re: Custom login and redirect to specific course

by khalil baraket -

I found it smile...changes should be done in index_form.php

In reply to Héctor Reyes

Re: Custom login and redirect to specific course

by John Daniel -

Hi,

I'm struggling with this on a Moodle 1.9.14, and everything seems to work because I made the index2.php just fine, changed the reverences in index_form.html, etc.  I appreciate the suggested path above, because it does make sense.  I can tell it is working, because the message I am getting is something like "invalid Course ID."

But the course ID is set as Admin in Moodle, right, under "Settings."  If I put an ID there, and then code the XXXX in the line above, is that it?  Or do I need to somehow identify the cousre ID in the files database somewhere?  Do I need to wait 24 hours, click "notifications," and it'll work?

I apologize if I don't make sense, because the only coding I learned is from you guys!  Please feel free to address the course ID problem if you can, and ignore some of the other foolishness I typed.

In reply to John Daniel

Re: Custom login and redirect to specific course

by John Daniel -

OK, I figured it out.  As embarassing as it is, I only needed to look at the URL once logged into the course, and I garnered the ID the code referred to from there.

Anyway, thanks guys from this thread:  very helpful.big grin

In reply to Héctor Reyes

Re: Custom login and redirect to specific course

by Andranik Sharabchian -

Hello Hector, thank you for your post. I have a qeustion. I've made index2.php file. I have many courses. I've made an html page with many buttons. I want, that when anyone presses one of the buttons, he can go directly to the appropriate course. What I must do for this in index2.php?

P. S. Sorry, if my english is ugly.

In reply to Simon Clegg

Re: Custom login and redirect to specific course

by Darren Stoltz -

I also am having this same problem, but I'm trying to automatically direct guests to a sample course directly.

I am using Moodle 1.9.8.

I would love to have the link in "Login as guest" take them to the course I want, but I've found it very difficult to find where this code is.

I have tried to change the instructions through "authenticated instructions" using the "&username=guest" option, but I have two main problems ( I am also wanting to do this from an external webpage)

1) When I first had it in, some kind of cookie problem occurred, where I was redirected to the login page. At that point, I could not log in at all, user name or guest. Removing the cookies fixes this problem until I clicked it again.

2) I changed the status of the particular course, removing it from being a meta course. I'm not sure if this created the current problem, which is to say that the course doesn't exist. I get this error: "The requested URL /moodle/view.php was not found on this server. Apache/2.0.52 (CentOS) Server at chekabc.ca Port 80"

I have guests automatically enrolled, and the course allows guests, and has no key.

Any help out there would be greatly appeciated!

Darren

In reply to Darren Stoltz

Re: Custom login and redirect to specific course

by Charandeep Singh -

I have done this just put this

$SESSION->wantsurl='url to redirect after login';

in login/index.php

In reply to Charandeep Singh

Re: Custom login and redirect to specific course

by Felipe Carasso -

Hi all,

I decided to mix both solutions into one: a plugin that redirects based on config.php settings.

It's called "Login/Logout user redirect" and it's pending approval on Moodle.org.

The source is available here: https://github.com/hrimhari/moodle-auth_loginlogoutredir/archive/master.zip

Best regards,

Felipe
In reply to Felipe Carasso

Re: Custom login and redirect to specific course

by Dave Perry -
Picture of Testers

That's a nice idea. I've had a go at making something (using a PHP include) that allows the user to set any course page they're on as their 'favourite' page, then have a 'go to my favourite page' link on our main menu bar. The logic being, I wasn't sure how to have a 'go to this page when I login' setting which would still let them go to actual homepage.

If your plugin factors this in, and lets the user easily set the page they want to see when they login, I'll give it a whirl.

In reply to Felipe Carasso

Re: Custom login and redirect to specific course

by Sergio Ubeda -

Hi!

I don't know if your plugin can help me.

What I need is that when a user enrolled in any course, login is redirected to the Main Page but when a new user not enrolled in any course (self registration) login is redirected to MyMoodle page (each one with diferent information).

Is this possible with your plugin or editing login/index.php?

Thank you very much!