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;
                                break;
                          }
                    }
          }
 } 


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.

39 comments:

  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

    ReplyDelete
  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 :)

    ReplyDelete
  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?

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

    ReplyDelete
  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 คือ

    ReplyDelete
  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 คือ

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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

    ReplyDelete
  16. 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?

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

    ReplyDelete
  18. 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.

    ReplyDelete
  19. 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

    ReplyDelete
  20. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    AI course in mumbai

    ReplyDelete

  21. Great Article
    Cloud Computing Projects


    Networking Projects

    Final Year Projects for CSE


    JavaScript Training in Chennai

    JavaScript Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete
  22. I also want to make some addition on this platform which must be in knowledge of people who really in need. ..Really I am impressed by this post. the person who created this post was a great.
    python training in chennai

    python online training in chennai

    python training in bangalore

    python training in hyderabad

    python online training

    python flask training

    python flask online training

    python training in coimbatore



    ReplyDelete
  23. Data Analytics Courses in PuneI am a new user of this site so here i saw multiple articles and posts posted by this site,I curious more interest in some of them hope you will give more information on this topics in your next articles.
    I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

    ReplyDelete
  24. Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up.
    data analytics courses in hyderabad with placements

    ReplyDelete
  25. Thanks for posting the best information and the blog is very.business analytics course in rajkot

    ReplyDelete
  26. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers. data scientist course in surat

    ReplyDelete
  27. Impressive blog and knowledgeable content. Please share more blogs with us. Thank you.
    Data Science Institute in Hyderabad

    ReplyDelete
  28. I have bookmarked your site since this site contains significant data in it. You rock for keeping incredible stuff. I am very appreciative of this site.
    data analytics training in hyderabad

    ReplyDelete