adds comments and replaces sql query with moodle bd lib fn