Enrolment architecture version 0.5 !
[moodle.git] / enrol / flatfile / enrol.php
1 <?php
2 require_once("$CFG->dirroot/enrol/enrol.class.php");
4 // The following flags are set in the configuration
5 // $CFG->enrol_flatfilelocation:       where is the file we are looking for?
6 // $CFG->enrol_flatfilemailusers:      send email to users when they are enrolled in a course
7 // $CFG->enrol_flatfilemailadmin:      email the log from the cron job to the admin
8 // $CFG->enrol_flatfileallowinternal:  allow internal enrolment in courses
13 class enrolment_plugin extends enrolment_base {
15     var $log;    
17 /// Override the base print_entry() function
18 function print_entry($course) {
19     global $CFG;
21     if (! empty($CFG->enrol_flatfileallowinternal) ) {
22         parent::print_entry($course);
23     } else {
24         print_header();
25         notice(get_string("enrolmentnointernal"), $CFG->wwwroot);
26     }
27 }
30 /// Override the base check_entry() function
31 function check_entry($form, $course) {
32     global $CFG;
34     if (! empty($CFG->enrol_flatfileallowinternal) ) {
35         parent::check_entry($form, $course);
36     }
37 }
41 /**
42 * Override the base cron() function to read in a file
43 *
44 * Comma separated file assumed to have four fields per line:
45 *   operation, role, idnumber(user), idnumber(course)
46 * where:
47 *   operation        = add | del
48 *   role             = student | teacher | teacheredit
49 *   idnumber(user)   = idnumber in the user table NB not id
50 *   idnumber(course) = idnumber in the course table NB not id
51 */
52     function cron() {
53         global $CFG;
55         if (empty($CFG->enrol_flatfilelocation)) {
56             $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
57         } else {
58             $filename = $CFG->enrol_flatfilelocation;
59         }
61         if ( file_exists($filename) ) {
62             
63             $this->log  = userdate(time()) . "\n";
64             $this->log .= "Flatfile enrol cron found file: $filename\n\n";
66             if (($fh = fopen($filename, "r")) != false) {
67             
68                 $line = 0;
69                 while (!feof($fh)) {
70                 
71                     $line++;
72                     $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
75                 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
76                     if ( count($fields) != 4) {
77                         if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
78                             $this->log .= "$line: Line incorrectly formatted - ignoring\n";
79                         }
80                         continue;
81                     }
82                     
84                     $fields[0] = trim(strtolower($fields[0]));
85                     $fields[1] = trim(strtolower($fields[1]));
86                     $fields[2] = trim($fields[2]);
87                     $fields[3] = trim($fields[3]);
90                     $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3]: ";
94                 /// check correct formatting of operation field
95                     if ($fields[0] != "add" and $fields[0] != "del") {
96                         $this->log .= "Unknown operation in field 1 - ignoring line\n";
97                         continue;
98                     }
101                 /// check correct formatting of role field
102                     if ($fields[1] != "student" and $fields[1] != "teacher" and $fields[1] != "teacheredit") {
103                         $this->log .= "Unknown role in field2 - ignoring line\n";
104                         continue;
105                     }
108                     if (! $user = get_record("user", "idnumber", $fields[2]) ) {
109                         $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
110                         continue;
111                     }
114                     if (! $course = get_record("course", "idnumber", $fields[3]) ) {
115                         $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
116                         continue;
117                     }
120                     unset($elog);
121                     switch ($fields[1]) {
122                         case "student":
123                             if ($fields[0] == "add") {
124                                 if (! enrol_student($user->id, $course->id)) {
125                                     $elog = "Error enrolling in course\n";
126                                 }
127                             } else {
128                                 if (! unenrol_student($user->id, $course->id)) {
129                                     $elog = "Error unenrolling from course\n";
130                                 }
131                             }
132                             break;
134                         case "teacher":
135                             if ($fields[0] == "add") {
136                                 if (! add_teacher($user->id, $course->id, 0)) {
137                                     $elog = "Error adding teacher to course\n";
138                                 }
139                             } else {
140                                 if (! remove_teacher($user->id, $course->id)) {
141                                     $elog = "Error removing teacher from course\n";
142                                 }
143                             }
144                             break;
146                         case "teacheredit":
147                             if ($fields[0] == "add") {
148                                 if (! add_teacher($user->id, $course->id, 1)) {
149                                     $elog = "Error adding teacher to course\n";
150                                 }
151                             } else {
152                                 if (! remove_teacher($user->id, $course->id)) {
153                                     $elog = "Error removing teacher from course\n";
154                                 }
155                             }
156                             break;
158                         default: // should never get here as checks made above for correct values of $fields[1]
160                     } // end of switch
164                     if ( (! empty($CFG->enrol_flatfilemailusers)) and empty($elog) and ($fields[0] == "add") ) {
165                         $subject = get_string("welcometocourse", "", $course->fullname);
166                         $a->coursename = $course->fullname;
167                         $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
168                         $message = get_string("welcometocoursetext", "", $a);
170                         if ($fields[1] == "student") {
171                             if (! $teacher = get_teacher($course->id)) {
172                                 $teacher = get_admin();
173                             }
174                         } else {
175                             $teacher = get_admin();
176                         }
178                         email_to_user($user, $teacher, $subject, $message);
179                     }
183                     if (empty($elog)) {
184                         $elog = "OK\n";
185                     }
186                     $this->log .= $elog;
188                 } // end of while loop
190             fclose($fh);
191             } // end of if(file_open)
193             if(! @unlink($filename)) {
194                 email_to_user(get_admin(), get_admin(), get_string("filelockedmailsubject", "enrol_flatfile"), get_string("filelockedmail", "enrol_flatfile", $filename));
195                 $this->log .= "Error unlinking file $filename\n";
196             }
198             if (! empty($CFG->enrol_flatfilemailadmin)) {
199                 email_to_user(get_admin(), get_admin(), "Flatfile Enrolment Log", $this->log);
200             }
202         } // end of if(file_exists)
204     } // end of function
206 } // end of class
208 ?>