Merge branch 'MDL-30871-23' of git://github.com/FMCorz/moodle into MOODLE_23_STABLE
[moodle.git] / enrol / flatfile / lib.php
CommitLineData
b8e13a57 1<?php
e8e0d845
AB
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/>.
b8e13a57 16
e8e0d845
AB
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.
4460eab5
PS
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
e8e0d845 27 */
0f093efa 28
97795859 29defined('MOODLE_INTERNAL') || die();
4460eab5 30
e8e0d845
AB
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 */
e8e0d845 36class enrol_flatfile_plugin extends enrol_plugin {
f9667a5a 37
4460eab5
PS
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 */
b6845dba
AB
51 private $log;
52
e8e0d845 53 public function cron() {
b6845dba
AB
54 $this->process_file();
55
56 $this->process_buffer();
57
58 echo $this->log;
59 } // end of function
60
61 protected function process_file() {
50c5bef4 62 global $CFG, $DB;
b8e13a57 63
e8e0d845 64 $filelocation = $this->get_config('location');
e8e0d845 65 $mailadmins = $this->get_config('mailadmins');
e8e0d845 66 if (empty($filelocation)) {
b8e13a57 67 $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location
68 } else {
e8e0d845 69 $filename = $filelocation;
b8e13a57 70 }
71
72 if ( file_exists($filename) ) {
b8e13a57 73 $this->log = userdate(time()) . "\n";
74 $this->log .= "Flatfile enrol cron found file: $filename\n\n";
75
76 if (($fh = fopen($filename, "r")) != false) {
a3081bfd 77
78 list($roles, $rolemap) = $this->get_roles();
4317f92f 79
b8e13a57 80 $line = 0;
81 while (!feof($fh)) {
4317f92f 82
b8e13a57 83 $line++;
84 $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
85
b8e13a57 86 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
76b60bb8 87 if (count($fields) != 4 and count($fields) !=6) {
b8e13a57 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 }
4317f92f 93
b8e13a57 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]);
4317f92f 98
76b60bb8 99 $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
4317f92f 100
76b60bb8 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;
4317f92f 108 }
b8e13a57 109
76b60bb8 110 $this->log .= ":";
b8e13a57 111
b8e13a57 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 }
117
b8e13a57 118 /// check correct formatting of role field
a3081bfd 119 if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
b8e13a57 120 $this->log .= "Unknown role in field2 - ignoring line\n";
121 continue;
122 }
123
50c5bef4 124 if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
b8e13a57 125 $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
126 continue;
127 }
128
50c5bef4 129 if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
b8e13a57 130 $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
131 continue;
132 }
133
a3081bfd 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]];
138
b6845dba
AB
139 if ($fields[4] > $fields[5]) {
140 $this->log .= "Start time was later than end time - ignoring line\n";
141 continue;
0f093efa 142 }
b8e13a57 143
b6845dba 144 $this->process_records($fields[0],$roleid,$user,$course,$fields[4],$fields[5]);
b8e13a57 145
b6845dba 146 } // end of while loop
b8e13a57 147
148 fclose($fh);
149 } // end of if(file_open)
150
151 if(! @unlink($filename)) {
94b9c2e8 152 $eventdata = new stdClass();
3b120e46 153 $eventdata->modulename = 'moodle';
440dec9c 154 $eventdata->component = 'enrol_flatfile';
e8e0d845 155 $eventdata->name = 'flatfile_enrolment';
3b120e46 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 = '';
7c7d3afa 163 message_send($eventdata);
b8e13a57 164 $this->log .= "Error unlinking file $filename\n";
165 }
166
e8e0d845
AB
167 if (!empty($mailadmins)) {
168
169 // Send mail to admin
94b9c2e8 170 $eventdata = new stdClass();
3b120e46 171 $eventdata->modulename = 'moodle';
440dec9c 172 $eventdata->component = 'enrol_flatfile';
e8e0d845 173 $eventdata->name = 'flatfile_enrolment';
3b120e46 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 = '';
7c7d3afa 181 message_send($eventdata);
b8e13a57 182 }
183
184 } // end of if(file_exists)
185
186 } // end of function
187
b6845dba
AB
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";
55fe8074
AB
194 $user = $DB->get_record("user", array("id"=>$future_en->userid));
195 $course = $DB->get_record("course", array("id"=>$future_en->courseid));
b6845dba 196 // enrol the person.
55fe8074 197 if($this->process_records($future_en->action, $future_en->roleid,
b6845dba
AB
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 }
205
206 private function process_records($action, $roleid, $user, $course, $timestart, $timeend, $store_to_buffer = true) {
207 global $CFG, $DB;
208
209 $mailstudents = $this->get_config('mailstudents');
210 $mailteachers = $this->get_config('mailteachers');
211
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.
94b9c2e8 216 $future_en = new stdClass();
b6845dba 217 $future_en->action = $action;
55fe8074
AB
218 $future_en->roleid = $roleid;
219 $future_en->userid = $user->id;
220 $future_en->courseid = $course->id;
b6845dba
AB
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 }
228
229 unset($elog);
230
231 // Create/resurrect a context object
232 $context = get_context_instance(CONTEXT_COURSE, $course->id);
233
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 }
252
253
254 if ( empty($elog) and ($action== "add") ) {
255 $role = $DB->get_record("role", array("id"=>$roleid));
256
257 if ($role->archetype == "student") {
258
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 }
265
266 if (!isset($teacher)) {
267 $teacher = get_admin();
268 }
269 } else {
270 $teacher = get_admin();
271 }
272
273
274 if (!empty($mailstudents)) {
275 // Send mail to students
94b9c2e8 276 $a = new stdClass();
91d284c1 277 $a->coursename = format_string($course->fullname, true, array('context' => $context));
b6845dba 278 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
8ebbb06a 279 $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
b6845dba 280
94b9c2e8 281 $eventdata = new stdClass();
b6845dba 282 $eventdata->modulename = 'moodle';
440dec9c 283 $eventdata->component = 'enrol_flatfile';
b6845dba
AB
284 $eventdata->name = 'flatfile_enrolment';
285 $eventdata->userfrom = $teacher;
286 $eventdata->userto = $user;
8ebbb06a 287 $eventdata->subject = $subject;
b6845dba
AB
288 $eventdata->fullmessage = get_string('welcometocoursetext', '', $a);
289 $eventdata->fullmessageformat = FORMAT_PLAIN;
290 $eventdata->fullmessagehtml = '';
291 $eventdata->smallmessage = '';
292 message_send($eventdata);
293 }
294
295 if (!empty($mailteachers) && $teachers) {
296
297 // Send mail to teachers
298 foreach($teachers as $teacher) {
94b9c2e8 299 $a = new stdClass();
91d284c1 300 $a->course = format_string($course->fullname, true, array('context' => $context));
b6845dba 301 $a->user = fullname($user);
8ebbb06a 302 $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
b6845dba 303
94b9c2e8 304 $eventdata = new stdClass();
b6845dba 305 $eventdata->modulename = 'moodle';
440dec9c 306 $eventdata->component = 'enrol_flatfile';
b6845dba
AB
307 $eventdata->name = 'flatfile_enrolment';
308 $eventdata->userfrom = $user;
309 $eventdata->userto = $teacher;
8ebbb06a 310 $eventdata->subject = $subject;
b6845dba
AB
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 }
319
320
321 if (empty($elog)) {
322 $elog = "OK\n";
323 }
324 $this->log .= $elog;
325
326 return true;
327 }
328
a3081bfd 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() {
50c5bef4 337 global $DB;
338
e8e0d845
AB
339 // Get all roles
340 $roles = $DB->get_records('role', null, '', 'id, name, shortname');
341
342 $config = get_config('enrol_flatfile');
343
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');
a3081bfd 354 // Get a list of all the roles in the database, indexed by their short names.
50c5bef4 355 $roles = $DB->get_records('role', null, '', 'shortname, id');
a3081bfd 356
357 // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
e8e0d845 358 array_walk($roles, create_function('&$value', '$value = $value->id;'));
a3081bfd 359 $rolemap = array();
e8e0d845 360 foreach($config as $name => $value) {
a3081bfd 361 if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
362 $rolemap[$value] = $key;
363 }
364 }
365
366 return array($roles, $rolemap);
367 }
368
b8e13a57 369} // end of class