Updated the 19 build version to 20081120
[moodle.git] / enrol / flatfile / enrol.php
blob2295c6b4bbede97013ff4b57cc9f5c6631e5adee
1 <?php
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 {
12 var $log;
14 /// Override the base config_form() function
15 function config_form($frm) {
16 global $CFG;
18 $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
19 foreach ($vars as $var) {
20 if (!isset($frm->$var)) {
21 $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(
32 $record->name,
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');
67 } else {
68 set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
72 return true;
76 /// Override the get_access_icons() function
77 function get_access_icons($course) {
80 /**
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]
85 * where:
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
93 function cron() {
94 global $CFG;
96 if (empty($CFG->enrol_flatfilelocation)) {
97 $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location
98 } else {
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();
111 $line = 0;
112 while (!feof($fh)) {
114 $line++;
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";
123 continue;
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]";
138 } else {
139 $fields[4] = 0;
140 $fields[5] = 0;
143 $this->log .= ":";
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";
150 continue;
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";
157 continue;
160 if (! $user = get_record("user", "idnumber", $fields[2]) ) {
161 $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
162 continue;
166 if (! $course = get_record("course", "idnumber", $fields[3]) ) {
167 $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
168 continue;
171 if ($fields[4] > $fields[5]) {
172 $this->log .= "Start time was later than end time - ignoring line\n";
173 continue;
177 unset($elog);
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');
189 } else {
190 role_unassign($roleid, $user->id, null, $context->id);
194 switch ($fields[1]) {
195 case "student":
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";
200 } else {
201 if (! unenrol_student($user->id, $course->id)) {
202 $elog = "Error unenrolling from course\n";
205 break;
207 case "teacher":
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";
212 } else {
213 if (! remove_teacher($user->id, $course->id)) {
214 $elog = "Error removing teacher from course\n";
217 break;
219 case "teacheredit":
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";
224 } else {
225 if (! remove_teacher($user->id, $course->id)) {
226 $elog = "Error removing teacher from course\n";
229 break;
231 default: // should never get here as checks made above for correct values of $fields[1]
233 } // end of switch*/
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)) {
244 $teacher = $u;
245 break;
250 if (!isset($teacher)) {
251 $teacher = get_admin();
253 } else {
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&amp;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));
280 if (empty($elog)) {
281 $elog = "OK\n";
283 $this->log .= $elog;
285 } // end of while loop
287 fclose($fh);
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)
301 } // end of function
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');
317 $rolemap = array();
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);
327 } // end of class