Incorrect variable name used for parameter.
[moodle-linuxchix.git] / enrol / flatfile / enrol.php
blob8b51a767e5057cc79e0b0706a724642f83dcc38f
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_allowinternal: allow internal enrolment in courses
7 // $CFG->enrol_emailstudents: send email to students when they are enrolled in a course
8 // $CFG->enrol_emailteachers: send email to teachers when they are enrolled in a course
9 // $CFG->enrol_emailadmins: email the log from the cron job to the admin
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_allowinternal) ) {
22 parent::print_entry($course);
23 } else {
24 print_header();
25 notice(get_string("enrolmentnointernal"), $CFG->wwwroot);
30 /// Override the base check_entry() function
31 function check_entry($form, $course) {
32 global $CFG;
34 if (! empty($CFG->enrol_allowinternal) ) {
35 parent::check_entry($form, $course);
40 /// Override the base config_form() function
41 function config_form($frm) {
42 global $CFG;
44 $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
45 foreach ($vars as $var) {
46 if (!isset($frm->$var)) {
47 $frm->$var = '';
50 include ("$CFG->dirroot/enrol/flatfile/config.html");
54 /// Override the base process_config() function
55 function process_config($config) {
57 if (!isset($config->enrol_flatfilelocation)) {
58 $config->enrol_flatfilelocation = '';
60 set_config('enrol_flatfilelocation', $config->enrol_flatfilelocation);
62 if (!isset($config->enrol_mailstudents)) {
63 $config->enrol_mailstudents = '';
65 set_config('enrol_mailstudents', $config->enrol_mailstudents);
67 if (!isset($config->enrol_mailteachers)) {
68 $config->enrol_mailteachers = '';
70 set_config('enrol_mailteachers', $config->enrol_mailteachers);
72 if (!isset($config->enrol_mailadmins)) {
73 $config->enrol_mailadmins = '';
75 set_config('enrol_mailadmins', $config->enrol_mailadmins);
77 if (!isset($config->enrol_allowinternal)) {
78 $config->enrol_allowinternal = '';
80 set_config('enrol_allowinternal', $config->enrol_allowinternal);
82 return true;
86 /**
87 * Override the base cron() function to read in a file
89 * Comma separated file assumed to have four or six fields per line:
90 * operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
91 * where:
92 * operation = add | del
93 * role = student | teacher | teacheredit
94 * idnumber(user) = idnumber in the user table NB not id
95 * idnumber(course) = idnumber in the course table NB not id
96 * starttime = start time (in seconds since epoch) - optional
97 * endtime = end time (in seconds since epoch) - optional
99 function cron() {
100 global $CFG;
102 /// call the base class
103 parent::cron();
105 if (empty($CFG->enrol_flatfilelocation)) {
106 $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location
107 } else {
108 $filename = $CFG->enrol_flatfilelocation;
111 if ( file_exists($filename) ) {
113 $this->log = userdate(time()) . "\n";
114 $this->log .= "Flatfile enrol cron found file: $filename\n\n";
116 if (($fh = fopen($filename, "r")) != false) {
118 $line = 0;
119 while (!feof($fh)) {
121 $line++;
122 $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
125 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
126 if (count($fields) != 4 and count($fields) !=6) {
127 if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
128 $this->log .= "$line: Line incorrectly formatted - ignoring\n";
130 continue;
134 $fields[0] = trim(strtolower($fields[0]));
135 $fields[1] = trim(strtolower($fields[1]));
136 $fields[2] = trim($fields[2]);
137 $fields[3] = trim($fields[3]);
139 $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
141 if (!empty($fields[5])) {
142 $fields[4] = (int)trim($fields[4]);
143 $fields[5] = (int)trim($fields[5]);
144 $this->log .= "$fields[4] $fields[5]";
145 } else {
146 $fields[4] = 0;
147 $fields[5] = 0;
150 $this->log .= ":";
154 /// check correct formatting of operation field
155 if ($fields[0] != "add" and $fields[0] != "del") {
156 $this->log .= "Unknown operation in field 1 - ignoring line\n";
157 continue;
161 /// check correct formatting of role field
162 if ($fields[1] != "student" and $fields[1] != "teacher" and $fields[1] != "teacheredit") {
163 $this->log .= "Unknown role in field2 - ignoring line\n";
164 continue;
168 if (! $user = get_record("user", "idnumber", $fields[2]) ) {
169 $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
170 continue;
174 if (! $course = get_record("course", "idnumber", $fields[3]) ) {
175 $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
176 continue;
179 if ($fields[4] > $fields[5]) {
180 $this->log .= "Start time was later than end time - ignoring line\n";
181 continue;
185 unset($elog);
186 switch ($fields[1]) {
187 case "student":
188 if ($fields[0] == "add") {
189 if (! enrol_student($user->id, $course->id, $fields[4], $fields[5], 'flatfile')) {
190 $elog = "Error enrolling in course\n";
192 } else {
193 if (! unenrol_student($user->id, $course->id)) {
194 $elog = "Error unenrolling from course\n";
197 break;
199 case "teacher":
200 if ($fields[0] == "add") {
201 if (! add_teacher($user->id, $course->id, 0, '', $fields[4], $fields[5], 'flatfile')) {
202 $elog = "Error adding teacher to course\n";
204 } else {
205 if (! remove_teacher($user->id, $course->id)) {
206 $elog = "Error removing teacher from course\n";
209 break;
211 case "teacheredit":
212 if ($fields[0] == "add") {
213 if (! add_teacher($user->id, $course->id, 1, '', $fields[4], $fields[5], 'flatfile')) {
214 $elog = "Error adding teacher to course\n";
216 } else {
217 if (! remove_teacher($user->id, $course->id)) {
218 $elog = "Error removing teacher from course\n";
221 break;
223 default: // should never get here as checks made above for correct values of $fields[1]
225 } // end of switch
229 if ( empty($elog) and ($fields[0] == "add") ) {
231 if ($fields[1] == "student") {
232 if (! $teacher = get_teacher($course->id)) {
233 $teacher = get_admin();
235 } else {
236 $teacher = get_admin();
239 if (!empty($CFG->enrol_mailstudents)) {
240 $a->coursename = "$course->fullname";
241 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
242 email_to_user($user, $teacher, get_string("enrolmentnew", '', $course->shortname),
243 get_string('welcometocoursetext', '', $a));
246 if (!empty($CFG->enrol_mailteachers)) {
247 $a->course = "$course->fullname";
248 $a->user = fullname($user);
249 email_to_user($teacher, $user, get_string("enrolmentnew", '', $course->shortname),
250 get_string('enrolmentnewuser', '', $a));
255 if (empty($elog)) {
256 $elog = "OK\n";
258 $this->log .= $elog;
260 } // end of while loop
262 fclose($fh);
263 } // end of if(file_open)
265 if(! @unlink($filename)) {
266 email_to_user(get_admin(), get_admin(), get_string("filelockedmailsubject", "enrol_flatfile"), get_string("filelockedmail", "enrol_flatfile", $filename));
267 $this->log .= "Error unlinking file $filename\n";
270 if (!empty($CFG->enrol_mailadmins)) {
271 email_to_user(get_admin(), get_admin(), "Flatfile Enrolment Log", $this->log);
274 } // end of if(file_exists)
276 } // end of function
278 } // end of class