Merge branch 'MDL-26439_message_search_23' of git://github.com/andyjdavis/moodle...
[moodle.git] / enrol / flatfile / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Flatfile enrolment plugin.
19  *
20  * This plugin lets the user specify a "flatfile" (CSV) containing enrolment information.
21  * On a regular cron cycle, the specified file is parsed and then deleted.
22  *
23  * @package    enrol
24  * @subpackage flatfile
25  * @copyright  2010 Eugene Venter
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 defined('MOODLE_INTERNAL') || die();
31 /**
32  * Flatfile enrolment plugin implementation.
33  * @author  Eugene Venter - based on code by Petr Skoda, Martin Dougiamas, Martin Langhoff and others
34  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class enrol_flatfile_plugin extends enrol_plugin {
38     /**
39      * Override the base cron() function to read in a file
40      *
41      * Comma separated file assumed to have four or six fields per line:
42      *   operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
43      * where:
44      *   operation        = add | del
45      *   role             = student | teacher | teacheredit
46      *   idnumber(user)   = idnumber in the user table NB not id
47      *   idnumber(course) = idnumber in the course table NB not id
48      *   starttime        = start time (in seconds since epoch) - optional
49      *   endtime          = end time (in seconds since epoch) - optional
50      */
51     private $log;
53     public function cron() {
54         $this->process_file();
56         $this->process_buffer();
58         echo $this->log;
59     } // end of function
61     protected function process_file() {
62         global $CFG, $DB;
64         $filelocation = $this->get_config('location');
65         $mailadmins   = $this->get_config('mailadmins');
66         if (empty($filelocation)) {
67             $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
68         } else {
69             $filename = $filelocation;
70         }
72         if ( file_exists($filename) ) {
73             $this->log  = userdate(time()) . "\n";
74             $this->log .= "Flatfile enrol cron found file: $filename\n\n";
76             if (($fh = fopen($filename, "r")) != false) {
78                 list($roles, $rolemap) = $this->get_roles();
80                 $line = 0;
81                 while (!feof($fh)) {
83                     $line++;
84                     $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
86                 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
87                     if (count($fields) != 4 and count($fields) !=6) {
88                         if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
89                             $this->log .= "$line: Line incorrectly formatted - ignoring\n";
90                         }
91                         continue;
92                     }
94                     $fields[0] = trim(strtolower($fields[0]));
95                     $fields[1] = trim(strtolower($fields[1]));
96                     $fields[2] = trim($fields[2]);
97                     $fields[3] = trim($fields[3]);
99                     $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
101                     if (!empty($fields[5])) {
102                         $fields[4] = (int)trim($fields[4]);
103                         $fields[5] = (int)trim($fields[5]);
104                         $this->log .= "$fields[4] $fields[5]";
105                     } else {
106                         $fields[4] = 0;
107                         $fields[5] = 0;
108                     }
110                     $this->log .= ":";
112                 /// check correct formatting of operation field
113                     if ($fields[0] != "add" and $fields[0] != "del") {
114                         $this->log .= "Unknown operation in field 1 - ignoring line\n";
115                         continue;
116                     }
118                 /// check correct formatting of role field
119                     if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
120                         $this->log .= "Unknown role in field2 - ignoring line\n";
121                         continue;
122                     }
124                     if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
125                         $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
126                         continue;
127                     }
129                     if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
130                         $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
131                         continue;
132                     }
134                     // Either field[1] is a name that appears in the mapping,
135                     // or it's an actual short name. It has to be one or the
136                     // other, or we don't get to this point.
137                     $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
139                     if ($fields[4] > $fields[5]) {
140                         $this->log .= "Start time was later than end time - ignoring line\n";
141                         continue;
142                     }
144                     $this->process_records($fields[0],$roleid,$user,$course,$fields[4],$fields[5]);
146                  } // end of while loop
148             fclose($fh);
149             } // end of if(file_open)
151             if(! @unlink($filename)) {
152                 $eventdata = new stdClass();
153                 $eventdata->modulename        = 'moodle';
154                 $eventdata->component         = 'course';
155                 $eventdata->name              = 'flatfile_enrolment';
156                 $eventdata->userfrom          = get_admin();
157                 $eventdata->userto            = get_admin();
158                 $eventdata->subject           = get_string("filelockedmailsubject", "enrol_flatfile");
159                 $eventdata->fullmessage       = get_string("filelockedmail", "enrol_flatfile", $filename);
160                 $eventdata->fullmessageformat = FORMAT_PLAIN;
161                 $eventdata->fullmessagehtml   = '';
162                 $eventdata->smallmessage      = '';
163                 message_send($eventdata);
164                 $this->log .= "Error unlinking file $filename\n";
165             }
167             if (!empty($mailadmins)) {
169                 // Send mail to admin
170                 $eventdata = new stdClass();
171                 $eventdata->modulename        = 'moodle';
172                 $eventdata->component         = 'course';
173                 $eventdata->name              = 'flatfile_enrolment';
174                 $eventdata->userfrom          = get_admin();
175                 $eventdata->userto            = get_admin();
176                 $eventdata->subject           = "Flatfile Enrolment Log";
177                 $eventdata->fullmessage       = $this->log;
178                 $eventdata->fullmessageformat = FORMAT_PLAIN;
179                 $eventdata->fullmessagehtml   = '';
180                 $eventdata->smallmessage      = '';
181                 message_send($eventdata);
182             }
184         } // end of if(file_exists)
186     } // end of function
188     protected function process_buffer() {
189         global $DB;
190         // get records from enrol_flatfile table and process any records that are due.
191         if ($future_enrols = $DB->get_records('enrol_flatfile', null, '')) {
192             foreach($future_enrols as $id => $future_en) {
193                     $this->log .= "Processing buffered enrolments.\n";
194                     $user = $DB->get_record("user", array("id"=>$future_en->userid));
195                     $course = $DB->get_record("course", array("id"=>$future_en->courseid));
196                     // enrol the person.
197                     if($this->process_records($future_en->action, $future_en->roleid,
198                             $user, $course, $future_en->timestart, $future_en->timeend, false)) {
199                         //ok record went thru, get rid of the record.
200                         $DB->delete_records('enrol_flatfile', array('id'=>$future_en->id));
201                     }
202             }
203         }
204     }
206     private function process_records($action, $roleid, $user, $course, $timestart, $timeend, $store_to_buffer = true) {
207         global $CFG, $DB;
209         $mailstudents = $this->get_config('mailstudents');
210         $mailteachers = $this->get_config('mailteachers');
212         // check if timestart is for future processing.
213         if ($timestart > time()) {
214             if ($store_to_buffer) {
215                 // populate into enrol_flatfile table as a future role to be assigned by cron.
216                 $future_en = new stdClass();
217                 $future_en->action = $action;
218                 $future_en->roleid = $roleid;
219                 $future_en->userid = $user->id;
220                 $future_en->courseid = $course->id;
221                 $future_en->timestart = $timestart;
222                 $future_en->timeend     = $timeend;
223                 $future_en->timemodified  = time();
224                 $future_en->id = $DB->insert_record('enrol_flatfile', $future_en);
225             }
226             return false;
227         }
229         unset($elog);
231         // Create/resurrect a context object
232         $context = get_context_instance(CONTEXT_COURSE, $course->id);
234         if ($action == 'add') {
235             $instance = $DB->get_record('enrol',
236                             array('courseid' => $course->id, 'enrol' => 'flatfile'));
237             if (empty($instance)) {
238                 // Only add an enrol instance to the course if non-existent
239                 $enrolid = $this->add_instance($course);
240                 $instance = $DB->get_record('enrol', array('id' => $enrolid));
241             }
242             // Enrol the user with this plugin instance
243             $this->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
244         } else {
245             $instances = $DB->get_records('enrol',
246                             array('enrol' => 'flatfile', 'courseid' => $course->id));
247             foreach ($instances as $instance) {
248                 // Unenrol the user from all flatfile enrolment instances
249                 $this->unenrol_user($instance, $user->id);
250             }
251         }
254         if ( empty($elog) and ($action== "add") ) {
255             $role = $DB->get_record("role", array("id"=>$roleid));
257             if ($role->archetype == "student") {
259                 // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
260                 if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) {
261                     foreach ($teachers as $u) {
262                         $teacher = $u;
263                     }
264                 }
266                 if (!isset($teacher)) {
267                     $teacher = get_admin();
268                 }
269             } else {
270                 $teacher = get_admin();
271             }
274             if (!empty($mailstudents)) {
275                 // Send mail to students
276                 $a = new stdClass();
277                 $a->coursename = format_string($course->fullname, true, array('context' => $context));
278                 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
279                 $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
281                 $eventdata = new stdClass();
282                 $eventdata->modulename        = 'moodle';
283                 $eventdata->component         = 'course';
284                 $eventdata->name              = 'flatfile_enrolment';
285                 $eventdata->userfrom          = $teacher;
286                 $eventdata->userto            = $user;
287                 $eventdata->subject           = $subject;
288                 $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
289                 $eventdata->fullmessageformat = FORMAT_PLAIN;
290                 $eventdata->fullmessagehtml   = '';
291                 $eventdata->smallmessage      = '';
292                 message_send($eventdata);
293             }
295             if (!empty($mailteachers) && $teachers) {
297                 // Send mail to teachers
298                 foreach($teachers as $teacher) {
299                     $a = new stdClass();
300                     $a->course = format_string($course->fullname, true, array('context' => $context));
301                     $a->user = fullname($user);
302                     $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
304                     $eventdata = new stdClass();
305                     $eventdata->modulename        = 'moodle';
306                     $eventdata->component         = 'course';
307                     $eventdata->name              = 'flatfile_enrolment';
308                     $eventdata->userfrom          = $user;
309                     $eventdata->userto            = $teacher;
310                     $eventdata->subject           = $subject;
311                     $eventdata->fullmessage       = get_string('enrolmentnewuser', 'enrol', $a);
312                     $eventdata->fullmessageformat = FORMAT_PLAIN;
313                     $eventdata->fullmessagehtml   = '';
314                     $eventdata->smallmessage      = '';
315                     message_send($eventdata);
316                 }
317             }
318         }
321         if (empty($elog)) {
322             $elog = "OK\n";
323         }
324         $this->log .= $elog;
326         return true;
327     }
329     /**
330      * Returns a pair of arrays.  The first is the set of roleids, indexed by
331      * their shortnames.  The second is the set of shortnames that have
332      * mappings, indexed by those mappings.
333      *
334      * @return array ($roles, $rolemap)
335      */
336     function get_roles() {
337         global $DB;
339         // Get all roles
340         $roles = $DB->get_records('role', null, '', 'id, name, shortname');
342         $config = get_config('enrol_flatfile');
344         // Set some usable mapping configs for later
345         foreach($roles as $id => $role) {
346             if (isset($config->{"map_{$id}"})) {
347                 set_config('map_'.$role->shortname, $config->{"map_{$id}"}, 'enrol_flatfile');
348             } else {
349                 set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
350             }
351         }
352         // Get the updated config
353         $config = get_config('enrol_flatfile');
354         // Get a list of all the roles in the database, indexed by their short names.
355         $roles = $DB->get_records('role', null, '', 'shortname, id');
357         // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
358         array_walk($roles, create_function('&$value', '$value = $value->id;'));
359         $rolemap = array();
360         foreach($config as $name => $value) {
361             if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
362                 $rolemap[$value] = $key;
363             }
364         }
366         return array($roles, $rolemap);
367     }
369 } // end of class