refactor oncourse

cleaned up oncourse.php and moved code into separate functions