2 // The following flags are set in the configuration
3 // $CFG->enrol_flatfilelocation: where is the file we are looking for?
4 // $CFG->enrol_emailstudents: send email to students when they are enrolled in a course
5 // $CFG->enrol_emailteachers: send email to teachers when they are enrolled in a course
6 // $CFG->enrol_emailadmins: email the log from the cron job to the admin
10 class enrolment_plugin_flatfile
{
14 /// Override the base config_form() function
15 function config_form($frm) {
18 $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
19 foreach ($vars as $var) {
20 if (!isset($frm->$var)) {
25 $roles = get_records('role', '', '', '', 'id, name, shortname');
26 $ffconfig = get_config('enrol_flatfile');
28 $frm->enrol_flatfilemapping
= array();
29 foreach($roles as $id => $record) {
31 $frm->enrol_flatfilemapping
[$id] = array(
33 isset($ffconfig->{"map_{$record->shortname}"}) ?
$ffconfig->{"map_{$record->shortname}"} : $record->shortname
37 include ("$CFG->dirroot/enrol/flatfile/config.html");
41 /// Override the base process_config() function
42 function process_config($config) {
44 if (!isset($config->enrol_flatfilelocation
)) {
45 $config->enrol_flatfilelocation
= '';
47 set_config('enrol_flatfilelocation', $config->enrol_flatfilelocation
);
49 if (!isset($config->enrol_mailstudents
)) {
50 $config->enrol_mailstudents
= '';
52 set_config('enrol_mailstudents', $config->enrol_mailstudents
);
54 if (!isset($config->enrol_mailteachers
)) {
55 $config->enrol_mailteachers
= '';
57 set_config('enrol_mailteachers', $config->enrol_mailteachers
);
59 if (!isset($config->enrol_mailadmins
)) {
60 $config->enrol_mailadmins
= '';
62 set_config('enrol_mailadmins', $config->enrol_mailadmins
);
64 foreach(get_records('role', '', '', '', 'id, shortname') as $id => $role) {
65 if (isset($config->{"enrol_flatfilemapping_{$id}"})) {
66 set_config('map_'.$role->shortname
, $config->{"enrol_flatfilemapping_{$id}"}, 'enrol_flatfile');
68 set_config('map_'.$role->shortname
, $role->shortname
, 'enrol_flatfile');
76 /// Override the get_access_icons() function
77 function get_access_icons($course) {
81 * Override the base cron() function to read in a file
83 * Comma separated file assumed to have four or six fields per line:
84 * operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
86 * operation = add | del
87 * role = student | teacher | teacheredit
88 * idnumber(user) = idnumber in the user table NB not id
89 * idnumber(course) = idnumber in the course table NB not id
90 * starttime = start time (in seconds since epoch) - optional
91 * endtime = end time (in seconds since epoch) - optional
96 if (empty($CFG->enrol_flatfilelocation
)) {
97 $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location
99 $filename = $CFG->enrol_flatfilelocation
;
102 if ( file_exists($filename) ) {
104 $this->log
= userdate(time()) . "\n";
105 $this->log
.= "Flatfile enrol cron found file: $filename\n\n";
107 if (($fh = fopen($filename, "r")) != false) {
109 list($roles, $rolemap) = $this->get_roles();
115 $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
118 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
119 if (count($fields) != 4 and count($fields) !=6) {
120 if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
121 $this->log
.= "$line: Line incorrectly formatted - ignoring\n";
127 $fields[0] = trim(strtolower($fields[0]));
128 $fields[1] = trim(strtolower($fields[1]));
129 $fields[2] = trim($fields[2]);
130 $fields[3] = trim($fields[3]);
132 $this->log
.= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
134 if (!empty($fields[5])) {
135 $fields[4] = (int)trim($fields[4]);
136 $fields[5] = (int)trim($fields[5]);
137 $this->log
.= "$fields[4] $fields[5]";
147 /// check correct formatting of operation field
148 if ($fields[0] != "add" and $fields[0] != "del") {
149 $this->log
.= "Unknown operation in field 1 - ignoring line\n";
154 /// check correct formatting of role field
155 if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
156 $this->log
.= "Unknown role in field2 - ignoring line\n";
160 if (! $user = get_record("user", "idnumber", $fields[2]) ) {
161 $this->log
.= "Unknown user idnumber in field 3 - ignoring line\n";
166 if (! $course = get_record("course", "idnumber", $fields[3]) ) {
167 $this->log
.= "Unknown course idnumber in field 4 - ignoring line\n";
171 if ($fields[4] > $fields[5]) {
172 $this->log
.= "Start time was later than end time - ignoring line\n";
179 // Either field[1] is a name that appears in the mapping,
180 // or it's an actual short name. It has to be one or the
181 // other, or we don't get to this point.
182 $roleid = isset($rolemap[$fields[1]]) ?
$roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
184 // Create/resurrect a context object
185 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
187 if ($fields[0] == 'add') {
188 role_assign($roleid, $user->id
, null, $context->id
, $fields[4], $fields[5], 0, 'flatfile');
190 role_unassign($roleid, $user->id
, null, $context->id
);
194 switch ($fields[1]) {
196 if ($fields[0] == "add") {
197 if (! enrol_student($user->id, $course->id, $fields[4], $fields[5], 'flatfile')) {
198 $elog = "Error enrolling in course\n";
201 if (! unenrol_student($user->id, $course->id)) {
202 $elog = "Error unenrolling from course\n";
208 if ($fields[0] == "add") {
209 if (! add_teacher($user->id, $course->id, 0, '', $fields[4], $fields[5], 'flatfile')) {
210 $elog = "Error adding teacher to course\n";
213 if (! remove_teacher($user->id, $course->id)) {
214 $elog = "Error removing teacher from course\n";
220 if ($fields[0] == "add") {
221 if (! add_teacher($user->id, $course->id, 1, '', $fields[4], $fields[5], 'flatfile')) {
222 $elog = "Error adding teacher to course\n";
225 if (! remove_teacher($user->id, $course->id)) {
226 $elog = "Error removing teacher from course\n";
231 default: // should never get here as checks made above for correct values of $fields[1]
237 if ( empty($elog) and ($fields[0] == "add") ) {
239 if ($fields[1] == "student") {
241 if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*,ra.hidden', 'sortorder ASC')) {
242 foreach ($teachers as $u) {
243 if (!$u->hidden ||
has_capability('moodle/role:viewhiddenassigns', $context)) {
250 if (!isset($teacher)) {
251 $teacher = get_admin();
254 $teacher = get_admin();
258 if (!empty($CFG->enrol_mailstudents
)) {
259 $a->coursename
= "$course->fullname";
260 $a->profileurl
= "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
261 email_to_user($user, $teacher, get_string("enrolmentnew", '', $course->shortname
),
262 get_string('welcometocoursetext', '', $a));
265 if (!empty($CFG->enrol_mailteachers
) && $teachers) {
267 foreach($teachers as $teacher) {
269 if (!$u->hidden ||
has_capability('moodle/role:viewhiddenassigns', $context)) {
270 $a->course
= "$course->fullname";
271 $a->user
= fullname($user);
272 email_to_user($teacher, $user, get_string("enrolmentnew", '', $course->shortname
),
273 get_string('enrolmentnewuser', '', $a));
285 } // end of while loop
288 } // end of if(file_open)
290 if(! @unlink
($filename)) {
291 email_to_user(get_admin(), get_admin(), get_string("filelockedmailsubject", "enrol_flatfile"), get_string("filelockedmail", "enrol_flatfile", $filename));
292 $this->log
.= "Error unlinking file $filename\n";
295 if (!empty($CFG->enrol_mailadmins
)) {
296 email_to_user(get_admin(), get_admin(), "Flatfile Enrolment Log", $this->log
);
299 } // end of if(file_exists)
304 * Returns a pair of arrays. The first is the set of roleids, indexed by
305 * their shortnames. The second is the set of shortnames that have
306 * mappings, indexed by those mappings.
308 * @return array ($roles, $rolemap)
310 function get_roles() {
311 // Get a list of all the roles in the database, indexed by their short names.
312 $roles = get_records('role', '', '', '', 'shortname, id');
313 array_walk($roles, create_function('&$value', '$value = $value->id;'));
315 // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
316 $ffconfig = get_config('enrol_flatfile');
318 foreach($ffconfig as $name => $value) {
319 if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
320 $rolemap[$value] = $key;
324 return array($roles, $rolemap);