Moving code to redirect to a course page in a function
Minor cleaning code
Improving query see BT#4890