diff --git a/enrol/flatfile/config.html b/enrol/flatfile/config.html
deleted file mode 100644
index e91df2f..0000000
--- a/enrol/flatfile/config.html
+++ /dev/null
@@ -1,55 +0,0 @@
-<table cellspacing="0" cellpadding="5" border="0" class="boxaligncenter">
-
-<tr valign="top">
-    <td align="right">enrol_flatfilelocation:</td>
-    <td>
-        <input type="text" size="20" name="enrol_flatfilelocation" value="<?php echo $frm->enrol_flatfilelocation ?>" />
-    </td>
-    <td>
-    <?php  print_string("location", "enrol_flatfile") ?>
-    </td>
-</tr>
-
-<tr valign="top">
-    <td align="right">enrol_mailstudents:</td>
-    <td>
-        <input type="checkbox" value="1" name="enrol_mailstudents" <?php if ($frm->enrol_mailstudents) echo "checked=\"checked\"" ?> />
-    </td>
-    <td>
-    <?php  print_string("mailstudents") ?>
-    </td>
-</tr>
-
-<tr valign="top">
-    <td align="right">enrol_mailteachers:</td>
-    <td>
-        <input type="checkbox" value="1" name="enrol_mailteachers" <?php if ($frm->enrol_mailteachers) echo "checked=\"checked\"" ?> />
-    </td>
-    <td>
-    <?php  print_string("mailteachers") ?>
-    </td>
-</tr>
-
-<tr valign="top">
-    <td align="right">enrol_mailadmins:</td>
-    <td>
-        <input type="checkbox" value="1" name="enrol_mailadmins" <?php if ($frm->enrol_mailadmins) echo "checked=\"checked\"" ?> />
-    </td>
-    <td>
-    <?php  print_string("mailadmins") ?>
-    </td>
-</tr>
-
-<tr valign="top">
-    <td align="left" colspan="2">enrol_flatfilemapping:</td>
-</tr>
-
-<?php foreach ($frm->enrol_flatfilemapping as $id => $record) {
-        list($name, $mapping) = $record; ?>
-<tr valign="top">
-    <td align="right"><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></td>
-    <td><input type="text" size="20" name="enrol_flatfilemapping_<?php echo $id; ?>" value="<?php echo htmlspecialchars($mapping, ENT_COMPAT, 'UTF-8'); ?>" /></td>
-</tr>
-<?php } ?>
-
-</table>
diff --git a/enrol/flatfile/enrol.php b/enrol/flatfile/enrol.php
deleted file mode 100644
index 2291624..0000000
--- a/enrol/flatfile/enrol.php
+++ /dev/null
@@ -1,322 +0,0 @@
-<?php
-// The following flags are set in the configuration
-// $CFG->enrol_flatfilelocation:       where is the file we are looking for?
-// $CFG->enrol_emailstudents:          send email to students when they are enrolled in a course
-// $CFG->enrol_emailteachers:          send email to teachers when they are enrolled in a course
-// $CFG->enrol_emailadmins:            email the log from the cron job to the admin
-
-require_once($CFG->libdir.'/eventslib.php');
-
-
-class enrolment_plugin_flatfile {
-
-    var $log;
-
-/// Override the base config_form() function
-function config_form($frm) {
-    global $CFG, $DB;
-
-    $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
-    foreach ($vars as $var) {
-        if (!isset($frm->$var)) {
-            $frm->$var = '';
-        }
-    }
-
-    $roles = $DB->get_records('role', null, '', 'id, name, shortname');
-    $ffconfig = get_config('enrol_flatfile');
-
-    $frm->enrol_flatfilemapping = array();
-    foreach($roles as $id => $record) {
-
-        $frm->enrol_flatfilemapping[$id] = array(
-            $record->name,
-            isset($ffconfig->{"map_{$record->shortname}"}) ? $ffconfig->{"map_{$record->shortname}"} : $record->shortname
-        );
-    }
-
-    include ("$CFG->dirroot/enrol/flatfile/config.html");
-}
-
-
-/// Override the base process_config() function
-function process_config($config) {
-    global $DB;
-
-    if (!isset($config->enrol_flatfilelocation)) {
-        $config->enrol_flatfilelocation = '';
-    }
-    set_config('enrol_flatfilelocation', $config->enrol_flatfilelocation);
-
-    if (!isset($config->enrol_mailstudents)) {
-        $config->enrol_mailstudents = '';
-    }
-    set_config('enrol_mailstudents', $config->enrol_mailstudents);
-
-    if (!isset($config->enrol_mailteachers)) {
-        $config->enrol_mailteachers = '';
-    }
-    set_config('enrol_mailteachers', $config->enrol_mailteachers);
-
-    if (!isset($config->enrol_mailadmins)) {
-        $config->enrol_mailadmins = '';
-    }
-    set_config('enrol_mailadmins', $config->enrol_mailadmins);
-
-    foreach($DB->get_records('role', null, '', 'id, shortname') as $id => $role) {
-        if (isset($config->{"enrol_flatfilemapping_{$id}"})) {
-            set_config('map_'.$role->shortname, $config->{"enrol_flatfilemapping_{$id}"}, 'enrol_flatfile');
-        } else {
-            set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
-        }
-    }
-
-    return true;
-
-}
-
-/// Override the get_access_icons() function
-function get_access_icons($course) {
-}
-
-/**
-* Override the base cron() function to read in a file
-*
-* Comma separated file assumed to have four or six fields per line:
-*   operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
-* where:
-*   operation        = add | del
-*   role             = student | teacher | teacheredit
-*   idnumber(user)   = idnumber in the user table NB not id
-*   idnumber(course) = idnumber in the course table NB not id
-*   starttime        = start time (in seconds since epoch) - optional
-*   endtime          = end time (in seconds since epoch) - optional
-*/
-    function cron() {
-        global $CFG, $DB;
-
-        if (empty($CFG->enrol_flatfilelocation)) {
-            $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
-        } else {
-            $filename = $CFG->enrol_flatfilelocation;
-        }
-
-        if ( file_exists($filename) ) {
-
-            $this->log  = userdate(time()) . "\n";
-            $this->log .= "Flatfile enrol cron found file: $filename\n\n";
-
-            if (($fh = fopen($filename, "r")) != false) {
-
-                list($roles, $rolemap) = $this->get_roles();
-
-                $line = 0;
-                while (!feof($fh)) {
-
-                    $line++;
-                    $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
-
-
-                /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
-                    if (count($fields) != 4 and count($fields) !=6) {
-                        if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
-                            $this->log .= "$line: Line incorrectly formatted - ignoring\n";
-                        }
-                        continue;
-                    }
-
-
-                    $fields[0] = trim(strtolower($fields[0]));
-                    $fields[1] = trim(strtolower($fields[1]));
-                    $fields[2] = trim($fields[2]);
-                    $fields[3] = trim($fields[3]);
-
-                    $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
-
-                    if (!empty($fields[5])) {
-                        $fields[4] = (int)trim($fields[4]);
-                        $fields[5] = (int)trim($fields[5]);
-                        $this->log .= "$fields[4] $fields[5]";
-                    } else {
-                        $fields[4] = 0;
-                        $fields[5] = 0;
-                    }
-
-                    $this->log .= ":";
-
-
-
-                /// check correct formatting of operation field
-                    if ($fields[0] != "add" and $fields[0] != "del") {
-                        $this->log .= "Unknown operation in field 1 - ignoring line\n";
-                        continue;
-                    }
-
-
-                /// check correct formatting of role field
-                    if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
-                        $this->log .= "Unknown role in field2 - ignoring line\n";
-                        continue;
-                    }
-
-                    if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
-                        $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
-                        continue;
-                    }
-
-
-                    if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
-                        $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
-                        continue;
-                    }
-
-                    if ($fields[4] > $fields[5]) {
-                        $this->log .= "Start time was later than end time - ignoring line\n";
-                        continue;
-                    }
-
-
-                    unset($elog);
-
-                    // Either field[1] is a name that appears in the mapping,
-                    // or it's an actual short name. It has to be one or the
-                    // other, or we don't get to this point.
-                    $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
-
-                    // Create/resurrect a context object
-                    $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
-                    if ($fields[0] == 'add') {
-                        // TODO: real enrol, and maybe manual
-                        role_assign($roleid, $user->id, $context->id, 'enrol_flatfile');
-                    } else {
-                        role_unassign($roleid, $user->id, $context->id);
-                    }
-
-
-                    if ( empty($elog) and ($fields[0] == "add") ) {
-
-                        if ($fields[1] == "student") {
-
-                            // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
-                            if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'ra.sortorder ASC')) {
-                                foreach ($teachers as $u) {
-                                    $teacher = $u;
-                                }
-                            }
-
-                            if (!isset($teacher)) {
-                                $teacher = get_admin();
-                            }
-                        } else {
-                            $teacher = get_admin();
-                        }
-
-
-                        if (!empty($CFG->enrol_mailstudents)) {
-                            $a->coursename = "$course->fullname";
-                            $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
-
-                            $eventdata = new object();
-                            $eventdata->modulename        = 'moodle';
-                            $eventdata->userfrom          = $teacher;
-                            $eventdata->userto            = $user;
-                            $eventdata->subject           = get_string("enrolmentnew", '', $course->shortname);
-                            $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
-                            $eventdata->fullmessageformat = FORMAT_PLAIN;
-                            $eventdata->fullmessagehtml   = '';
-                            $eventdata->smallmessage      = '';
-                            message_send($eventdata);
-                        }
-
-                        if (!empty($CFG->enrol_mailteachers) && $teachers) {
-
-                            foreach($teachers as $teacher) {
-                                $a->course = "$course->fullname";
-                                $a->user = fullname($user);
-
-                                $eventdata = new object();
-                                $eventdata->modulename        = 'moodle';
-                                $eventdata->userfrom          = $user;
-                                $eventdata->userto            = $teacher;
-                                $eventdata->subject           = get_string("enrolmentnew", '', $course->shortname);
-                                $eventdata->fullmessage       = get_string('enrolmentnewuser', '', $a);
-                                $eventdata->fullmessageformat = FORMAT_PLAIN;
-                                $eventdata->fullmessagehtml   = '';
-                                $eventdata->smallmessage      = '';
-                                message_send($eventdata);
-                            }
-                        }
-                    }
-
-
-                    if (empty($elog)) {
-                        $elog = "OK\n";
-                    }
-                    $this->log .= $elog;
-
-                } // end of while loop
-
-            fclose($fh);
-            } // end of if(file_open)
-
-            if(! @unlink($filename)) {
-                $eventdata = new object();
-                $eventdata->modulename        = 'moodle';
-                $eventdata->userfrom          = get_admin();
-                $eventdata->userto            = get_admin();
-                $eventdata->subject           = get_string("filelockedmailsubject", "enrol_flatfile");
-                $eventdata->fullmessage       = get_string("filelockedmail", "enrol_flatfile", $filename);
-                $eventdata->fullmessageformat = FORMAT_PLAIN;
-                $eventdata->fullmessagehtml   = '';
-                $eventdata->smallmessage      = '';
-                message_send($eventdata);
-                $this->log .= "Error unlinking file $filename\n";
-            }
-
-            if (!empty($CFG->enrol_mailadmins)) {
-                $eventdata = new object();
-                $eventdata->modulename        = 'moodle';
-                $eventdata->userfrom          = get_admin();
-                $eventdata->userto            = get_admin();
-                $eventdata->subject           = "Flatfile Enrolment Log";
-                $eventdata->fullmessage       = $this->log;
-                $eventdata->fullmessageformat = FORMAT_PLAIN;
-                $eventdata->fullmessagehtml   = '';
-                $eventdata->smallmessage      = '';
-                message_send($eventdata);
-            }
-
-        } // end of if(file_exists)
-
-    } // end of function
-
-    /**
-     * Returns a pair of arrays.  The first is the set of roleids, indexed by
-     * their shortnames.  The second is the set of shortnames that have
-     * mappings, indexed by those mappings.
-     *
-     * @return array ($roles, $rolemap)
-     */
-    function get_roles() {
-        global $DB;
-
-        // Get a list of all the roles in the database, indexed by their short names.
-        $roles = $DB->get_records('role', null, '', 'shortname, id');
-        array_walk($roles, create_function('&$value', '$value = $value->id;'));
-
-        // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
-        $ffconfig = get_config('enrol_flatfile');
-        $rolemap = array();
-        foreach($ffconfig as $name => $value) {
-            if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
-                $rolemap[$value] = $key;
-            }
-        }
-
-        return array($roles, $rolemap);
-    }
-
-} // end of class
-
-
diff --git a/enrol/flatfile/lang/en/enrol_flatfile.php b/enrol/flatfile/lang/en/enrol_flatfile.php
index 535de5f..929548f 100644
--- a/enrol/flatfile/lang/en/enrol_flatfile.php
+++ b/enrol/flatfile/lang/en/enrol_flatfile.php
@@ -23,9 +23,9 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-$string['description'] = 'This method will repeatedly check for and process a specially-formatted text file in the location that you specify.
+$string['pluginname_desc'] = 'This method will repeatedly check for and process a specially-formatted text file in the location that you specify.
 The file is a comma separated file assumed to have four or six fields per line:
-<pre>
+<pre class="informationbox">
 *  operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
 where:
 *  operation        = add | del
@@ -36,7 +36,7 @@ where:
 *  endtime          = end time (in seconds since epoch) - optional
 </pre>
 It could look something like this:
-<pre>
+<pre class="informationbox">
    add, student, 5, CF101
    add, teacher, 6, CF101
    add, teacheredit, 7, CF101
@@ -44,9 +44,11 @@ It could look something like this:
    del, student, 17, CF101
    add, student, 21, CF101, 1091115000, 1091215000
 </pre>';
-$string['enrolname'] = 'Flat file';
+$string['pluginname'] = 'Flat file (CSV)';
 $string['filelockedmail'] = 'The text file you are using for file-based enrolments ({$a}) can not be deleted by the cron process.  This usually means the permissions are wrong on it.  Please fix the permissions so that Moodle can delete the file, otherwise it might be processed repeatedly.';
 $string['filelockedmailsubject'] = 'Important error: Enrolment file';
 $string['location'] = 'File location';
+$string['mailstudents'] = 'Notify students by email';
+$string['mailteachers'] = 'Notify teachers by email';
 $string['mailadmin'] = 'Notify admin by email';
-$string['mailusers'] = 'Notify users by email';
+$string['mapping'] = 'Flat file mapping';
diff --git a/enrol/flatfile/lib.php b/enrol/flatfile/lib.php
new file mode 100644
index 0000000..2a283b5
--- /dev/null
+++ b/enrol/flatfile/lib.php
@@ -0,0 +1,321 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Flatfile enrolment plugin.
+ *
+ * This plugin lets the user specify a "flatfile" (CSV) containing enrolment information.
+ * On a regular cron cycle, the specified file is parsed and then deleted.
+ * @package   enrol_flatfile
+ * @copyright 2010 Eugene Venter
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Flatfile enrolment plugin implementation.
+ * @author  Eugene Venter - based on code by Petr Skoda, Martin Dougiamas, Martin Langhoff and others
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once($CFG->libdir.'/eventslib.php');
+
+
+class enrol_flatfile_plugin extends enrol_plugin {
+
+/**
+* Override the base cron() function to read in a file
+*
+* Comma separated file assumed to have four or six fields per line:
+*   operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
+* where:
+*   operation        = add | del
+*   role             = student | teacher | teacheredit
+*   idnumber(user)   = idnumber in the user table NB not id
+*   idnumber(course) = idnumber in the course table NB not id
+*   starttime        = start time (in seconds since epoch) - optional
+*   endtime          = end time (in seconds since epoch) - optional
+*/
+    public function cron() {
+        global $CFG, $DB;
+
+        $filelocation = $this->get_config('location');
+        $mailstudents = $this->get_config('mailstudents');
+        $mailteachers = $this->get_config('mailteachers');
+        $mailadmins   = $this->get_config('mailadmins');
+
+        if (empty($filelocation)) {
+            $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
+        } else {
+            $filename = $filelocation;
+        }
+
+        if ( file_exists($filename) ) {
+
+            $this->log  = userdate(time()) . "\n";
+            $this->log .= "Flatfile enrol cron found file: $filename\n\n";
+
+            if (($fh = fopen($filename, "r")) != false) {
+
+                list($roles, $rolemap) = $this->get_roles();
+
+                $line = 0;
+                while (!feof($fh)) {
+
+                    $line++;
+                    $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
+
+
+                /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
+                    if (count($fields) != 4 and count($fields) !=6) {
+                        if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
+                            $this->log .= "$line: Line incorrectly formatted - ignoring\n";
+                        }
+                        continue;
+                    }
+
+
+                    $fields[0] = trim(strtolower($fields[0]));
+                    $fields[1] = trim(strtolower($fields[1]));
+                    $fields[2] = trim($fields[2]);
+                    $fields[3] = trim($fields[3]);
+
+                    $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
+
+                    if (!empty($fields[5])) {
+                        $fields[4] = (int)trim($fields[4]);
+                        $fields[5] = (int)trim($fields[5]);
+                        $this->log .= "$fields[4] $fields[5]";
+                    } else {
+                        $fields[4] = 0;
+                        $fields[5] = 0;
+                    }
+
+                    $this->log .= ":";
+
+
+
+                /// check correct formatting of operation field
+                    if ($fields[0] != "add" and $fields[0] != "del") {
+                        $this->log .= "Unknown operation in field 1 - ignoring line\n";
+                        continue;
+                    }
+
+
+                /// check correct formatting of role field
+                    if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
+                        $this->log .= "Unknown role in field2 - ignoring line\n";
+                        continue;
+                    }
+
+                    if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
+                        $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
+                        continue;
+                    }
+
+
+                    if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
+                        $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
+                        continue;
+                    }
+
+                    if ($fields[4] > $fields[5]) {
+                        $this->log .= "Start time was later than end time - ignoring line\n";
+                        continue;
+                    }
+
+
+                    unset($elog);
+
+                    // Either field[1] is a name that appears in the mapping,
+                    // or it's an actual short name. It has to be one or the
+                    // other, or we don't get to this point.
+                    $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
+
+                    // Create/resurrect a context object
+                    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
+
+                    if ($fields[0] == 'add') {
+                        $instance = $DB->get_record('enrol',
+                                        array('courseid' => $course->id, 'enrol' => 'flatfile'));
+                        if (empty($instance)) {
+                            // Only add an enrol instance to the course if non-existent
+                            $enrolid = $this->add_instance($course);
+                            $instance = $DB->get_record('enrol', array('id' => $enrolid));
+                        }
+
+                        // Enrol the user with this plugin instance
+                        $this->enrol_user($instance, $user->id, $roleid, $fields[4], $fields[5]);
+                    } else {
+                        $instances = $DB->get_records('enrol', 
+                                        array('enrol' => 'flatfile', 'courseid' => $course->id));
+                        foreach ($instances as $instance) {
+                            // Unenrol the user from all flatfile enrolment instances
+                            $this->unenrol_user($instance, $user->id);
+                        }
+                    }
+
+
+                    if ( empty($elog) and ($fields[0] == "add") ) {
+
+                        if ($fields[1] == "student") {
+
+                            // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
+                            if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) {
+                                foreach ($teachers as $u) {
+                                    $teacher = $u;
+                                }
+                            }
+
+                            if (!isset($teacher)) {
+                                $teacher = get_admin();
+                            }
+                        } else {
+                            $teacher = get_admin();
+                        }
+
+
+                        if (!empty($mailstudents)) {
+                            // Send mail to students
+                            $a->coursename = "$course->fullname";
+                            $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
+
+                            $eventdata = new object();
+                            $eventdata->modulename        = 'moodle';
+                            $eventdata->component         = 'course';
+                            $eventdata->name              = 'flatfile_enrolment';
+                            $eventdata->userfrom          = $teacher;
+                            $eventdata->userto            = $user;
+                            $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
+                            $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
+                            $eventdata->fullmessageformat = FORMAT_PLAIN;
+                            $eventdata->fullmessagehtml   = '';
+                            $eventdata->smallmessage      = '';
+                            message_send($eventdata);
+                        }
+
+                        if (!empty($mailteachers) && $teachers) {
+
+                            // Send mail to teachers
+                            foreach($teachers as $teacher) {
+                                $a->course = "$course->fullname";
+                                $a->user = fullname($user);
+
+                                $eventdata = new object();
+                                $eventdata->modulename        = 'moodle';
+                                $eventdata->component         = 'course';
+                                $eventdata->name              = 'flatfile_enrolment';
+                                $eventdata->userfrom          = $user;
+                                $eventdata->userto            = $teacher;
+                                $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
+                                $eventdata->fullmessage       = get_string('enrolmentnewuser', 'enrol', $a);
+                                $eventdata->fullmessageformat = FORMAT_PLAIN;
+                                $eventdata->fullmessagehtml   = '';
+                                $eventdata->smallmessage      = '';
+                                message_send($eventdata);
+                            }
+                        }
+                    }
+
+
+                    if (empty($elog)) {
+                        $elog = "OK\n";
+                    }
+                    $this->log .= $elog;
+
+                } // end of while loop
+
+            fclose($fh);
+            } // end of if(file_open)
+
+            if(! @unlink($filename)) {
+                $eventdata = new object();
+                $eventdata->modulename        = 'moodle';
+                $eventdata->component         = 'course';
+                $eventdata->name              = 'flatfile_enrolment';
+                $eventdata->userfrom          = get_admin();
+                $eventdata->userto            = get_admin();
+                $eventdata->subject           = get_string("filelockedmailsubject", "enrol_flatfile");
+                $eventdata->fullmessage       = get_string("filelockedmail", "enrol_flatfile", $filename);
+                $eventdata->fullmessageformat = FORMAT_PLAIN;
+                $eventdata->fullmessagehtml   = '';
+                $eventdata->smallmessage      = '';
+                message_send($eventdata);
+                $this->log .= "Error unlinking file $filename\n";
+            }
+
+            if (!empty($mailadmins)) {
+
+                // Send mail to admin
+                $eventdata = new object();
+                $eventdata->modulename        = 'moodle';
+                $eventdata->component         = 'course';
+                $eventdata->name              = 'flatfile_enrolment';
+                $eventdata->userfrom          = get_admin();
+                $eventdata->userto            = get_admin();
+                $eventdata->subject           = "Flatfile Enrolment Log";
+                $eventdata->fullmessage       = $this->log;
+                $eventdata->fullmessageformat = FORMAT_PLAIN;
+                $eventdata->fullmessagehtml   = '';
+                $eventdata->smallmessage      = '';
+                message_send($eventdata);
+            }
+
+        } // end of if(file_exists)
+
+    } // end of function
+
+    /**
+     * Returns a pair of arrays.  The first is the set of roleids, indexed by
+     * their shortnames.  The second is the set of shortnames that have
+     * mappings, indexed by those mappings.
+     *
+     * @return array ($roles, $rolemap)
+     */
+    function get_roles() {
+        global $DB;
+
+        // Get all roles
+        $roles = $DB->get_records('role', null, '', 'id, name, shortname');
+
+        $config = get_config('enrol_flatfile');
+
+        // Set some usable mapping configs for later
+        foreach($roles as $id => $role) {
+            if (isset($config->{"map_{$id}"})) {
+                set_config('map_'.$role->shortname, $config->{"map_{$id}"}, 'enrol_flatfile');
+            } else {
+                set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
+            }
+        }
+        // Get the updated config
+        $config = get_config('enrol_flatfile');
+        // Get a list of all the roles in the database, indexed by their short names.
+        $roles = $DB->get_records('role', null, '', 'shortname, id');
+
+        // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
+        array_walk($roles, create_function('&$value', '$value = $value->id;'));
+        $rolemap = array();
+        foreach($config as $name => $value) {
+            if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
+                $rolemap[$value] = $key;
+            }
+        }
+
+        return array($roles, $rolemap);
+    }
+
+} // end of class
diff --git a/enrol/flatfile/settings.php b/enrol/flatfile/settings.php
new file mode 100644
index 0000000..f869b13
--- /dev/null
+++ b/enrol/flatfile/settings.php
@@ -0,0 +1,52 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Flatfile enrolments plugin settings and presets.
+ *
+ * @package   enrol_flatfile
+ * @copyright 2010 Eugene Venter
+ * @author    Eugene Venter - based on code by Petr Skoda and others
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die;
+
+if ($ADMIN->fulltree) {
+
+    //--- general settings -----------------------------------------------------------------------------------
+    $settings->add(new admin_setting_heading('enrol_flatfile_settings', '', get_string('pluginname_desc', 'enrol_flatfile')));
+
+    $settings->add(new admin_setting_configtext('enrol_flatfile/location', get_string('location', 'enrol_flatfile'), '', ''));
+
+    $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailstudents', get_string('mailstudents', 'enrol_flatfile'), '', 0));
+
+    $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailteachers', get_string('mailteachers', 'enrol_flatfile'), '', 0));
+
+    $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailadmins', get_string('mailadmin', 'enrol_flatfile'), '', 0));
+
+    //--- mapping -------------------------------------------------------------------------------------------
+    if (!during_initial_install()) {
+        $settings->add(new admin_setting_heading('enrol_flatfile_mapping', get_string('mapping', 'enrol_flatfile'), ''));
+
+        $roles = $DB->get_records('role', null, '', 'id, name, shortname');
+
+        foreach ($roles as $id => $record) {
+            $settings->add(new admin_setting_configtext('enrol_flatfile/map_'.$id, format_string($record->name), '', format_string($record->shortname)));
+        }
+    }
+}
diff --git a/enrol/flatfile/version.php b/enrol/flatfile/version.php
new file mode 100644
index 0000000..0582228
--- /dev/null
+++ b/enrol/flatfile/version.php
@@ -0,0 +1,28 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Flat file enrolment plugin version specification.
+ *
+ * @package   enrol_flatfile
+ * @copyright 2010 Eugene Venter
+ * @author Eugene Venter
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$plugin->version = 2010070800;
+$plugin->cron = 60;
