Sunday, March 17, 2013

Redirect user to course page upon login

This is a feature I've been wanting for a while, especially for teachers. I would like that teachers are re-directed to their course page immediately after login. I've created some code inside the "login/index.php" file to accomplish this feature.

NOTE:  This code will only redirect the teacher to the FIRST course they are enrolled in. For me, this is okay because each teacher is only enrolled in one course anyway. If you want the user to be redirected to a specific course (out of many), you will need to modify this code and search for the course by name or id.

1)  Go to login/index.php on your moodle server.

2)  Around line 222, you will see some code that determines if the user will be redirected to the site home or the my moodle page. (In a previous post, I set up my system that teachers were sent to the my moodle page, and students were sent to the site home)

3)  Now we will add some code to get the user's list of courses and redirect the page to a specific course 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/';

                    // I set up the page to redirect teachers to the 'my' page. 
                    // So here, instead of looking for user's role, I simply see if they
                    // are directed to the mymoodle page. Then I know it's a teacher and 
                   // I redirect to their course.
   if ($courses = enrol_get_my_courses(NULL, 'visible DESC, fullname ASC')) 
           foreach($courses as $course)
                                $urltogo = $CFG->wwwroot.'/course/view.php?id='.$course->id;

4)  If you wanted to search for a specific course, then add logic inside of the foreach loop to look at the $course->id, $course->name, etc.


  1. Hi, probably our entry may be off topic but anyways, I have been surfing around your blog and it looks very professional. It’s obvious you know your topic and you

    appear fervent about it. I’m developing a fresh blog plus I’m struggling to make it look good, as well as offer the best quality content. I have learned much at your

    web site and also I anticipate alot more articles and will be coming back soon. Thanks you.

    PHP Course in Chennai

  2. Thank you for the lovely comment. While most of my current work is under contract, I will try to update my blog with more common solutions for moodle. Especially I should write more posts about handling javascript and Ajax :)

  3. Hello, Can anyone help me about the career path and chances in choosing PHP? I have planned to do PHP Training in Chennai. Someone suggest where to join for the best PHP Training in Chennai where I could also get placement services?

  4. i am giving online training for html,css,javascript,php,moodle....This is my email ( if you want to interest to lean php contact me through my email i will help you.

  5. I am thankful for this blog to gave me much knowledge regarding my area of work. I also want to make some addition on this platform which must be in knowledge of people who really in need. Thanks.
    หุ้น forex คือ

  6. Now day, everything is going to find a new but well settled and successful stream for their career. When I came to this blog, I really impressed by all the knowledge points mentioned here. Thank you for this assistance.
    Bearish Divergence คือ

  7. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.

    Java training in Chennai | Java training in Bangalore

    Java interview questions and answers | Core Java interview questions and answers

  8. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    angularjs Training in marathahalli

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in chennai

    automation anywhere online Training

  9. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    python training in rajajinagar
    Python training in bangalore
    Python training in usa

  10. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    excel advanced excel training in bangalore | Devops Training in Chennai

  11. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.

    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    Java training in USA

    Java training in Bangalore | Java training in Indira nagar

    Java training in Bangalore | Java training in Rajaji nagar

  12. You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

  13. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

  14. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    excel advanced excel training in bangalore
    Devops Training in Chennai

  15. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
    Data Science Course in Indira nagar
    Data Science Course in btm layout
    Python course in Kalyan nagar
    Data Science course in Indira nagar
    Data Science Course in Marathahalli
    Data Science Course in BTM Layout

  16. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
    Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai


  17. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTM, Marathahalli
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR

  18. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    Devops Training in bangalore
    Digital Marketing Training in bangalore
    Data Science Training in bangalore
    Java Training in bangalore

  19. Wonderful Blog post, great article that you have provided for peoples. Its really good. Nice information.

    Data Science in Bangalore

  20. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    date analytics certification training courses
    data science courses training
    data analytics certification courses in Bangalore
    ExcelR Data science courses in Bangalore

  21. I don’t think many of websites provide this type of information.
    Data Science Course in Pune

  22. Really impressive post. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
    AI learning course malaysia

  23. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.


  24. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page digital marketing course in singapore

  25. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    Great Post

  26. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! digital marketing course in singapore

  27. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this,
    Data science Courses banglore

  28. I found Hubwit as a transparent s ite, a social hub which is a conglomerate of Buyers and Sellers who are ready to offer online digital consultancy at decent cost.
    AI course malaysia

  29. You have given very nice post it is very knowledgeable and result oriented. Website is very good tool for any companyWeb Designing Company Bangalore | Website Development Bangalore.
    outsourcing training in dhaka

  30. Hi,
    Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take python training in bangalore. Because Python course in Bangalore is one of the best that one can do while choosing the course.

  31. how to check if a user is suspended then when login is clicked instead of showing invalid login, redirect the user to a different page?

  32. Very nice blog here and thanks for post it.. Keep blogging...
    ExcelR data science training

  33. Good blog post. I definitely love this website. Keep writing!
    đồ gỗ nội thất

  34. Thank you for sharing such a nice post!

    Start your journey with In Software Training in Bangalore and get hands-on Experience with 100% Placement assistance from experts Trainers @eTechno Soft Solutions Located in BTM Layout Bangalore.

  35. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

  36. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    machine learning course

    artificial intelligence course in mumbai

  37. Attend The Data Analytics Courses From ExcelR. Practical Data Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
    ExcelR Data Analytics Courses
    Data Science Interview Questions

  38. I truly like this post..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    If you need any logo then,
    Please visit our site,

  39. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

    artificial Intelligence course

    machine learning courses in mumbai