diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index <HASH>..<HASH> 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -614,17 +614,19 @@ function authenticate_user_login($username, $password) {
 }
 
 
-function enrol_student($user, $course) {
+function enrol_student($userid, $courseid) {
 /// Enrols a student in a given course
     global $db;
 
-    $record->userid = $user;
-    $record->course = $course;
-    $record->start = 0;
-    $record->end = 0;
-    $record->time = time();
-
-    return insert_record("user_students", $record);
+    if (!record_exists("user_students", "userid", $userid, "course", $courseid)) {
+        $student->userid = $userid;
+        $student->course = $courseid;
+        $student->start = 0;
+        $student->end = 0;
+        $student->time = time();
+        return insert_record("user_students", $student);
+    }
+    return true;
 }
 
 function unenrol_student($user, $course=0) {
Don't enrol student if they are already enrolled.