Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/flatfile/lib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00029 defined('MOODLE_INTERNAL') || die();
00030 
00036 class enrol_flatfile_plugin extends enrol_plugin {
00037 
00051     private $log;
00052 
00053     public function cron() {
00054         $this->process_file();
00055 
00056         $this->process_buffer();
00057 
00058         echo $this->log;
00059     } // end of function
00060 
00061     protected function process_file() {
00062         global $CFG, $DB;
00063 
00064         $filelocation = $this->get_config('location');
00065         $mailadmins   = $this->get_config('mailadmins');
00066         if (empty($filelocation)) {
00067             $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
00068         } else {
00069             $filename = $filelocation;
00070         }
00071 
00072         if ( file_exists($filename) ) {
00073             $this->log  = userdate(time()) . "\n";
00074             $this->log .= "Flatfile enrol cron found file: $filename\n\n";
00075 
00076             if (($fh = fopen($filename, "r")) != false) {
00077 
00078                 list($roles, $rolemap) = $this->get_roles();
00079 
00080                 $line = 0;
00081                 while (!feof($fh)) {
00082 
00083                     $line++;
00084                     $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
00085 
00087                     if (count($fields) != 4 and count($fields) !=6) {
00088                         if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
00089                             $this->log .= "$line: Line incorrectly formatted - ignoring\n";
00090                         }
00091                         continue;
00092                     }
00093 
00094                     $fields[0] = trim(strtolower($fields[0]));
00095                     $fields[1] = trim(strtolower($fields[1]));
00096                     $fields[2] = trim($fields[2]);
00097                     $fields[3] = trim($fields[3]);
00098 
00099                     $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
00100 
00101                     if (!empty($fields[5])) {
00102                         $fields[4] = (int)trim($fields[4]);
00103                         $fields[5] = (int)trim($fields[5]);
00104                         $this->log .= "$fields[4] $fields[5]";
00105                     } else {
00106                         $fields[4] = 0;
00107                         $fields[5] = 0;
00108                     }
00109 
00110                     $this->log .= ":";
00111 
00113                     if ($fields[0] != "add" and $fields[0] != "del") {
00114                         $this->log .= "Unknown operation in field 1 - ignoring line\n";
00115                         continue;
00116                     }
00117 
00119                     if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
00120                         $this->log .= "Unknown role in field2 - ignoring line\n";
00121                         continue;
00122                     }
00123 
00124                     if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
00125                         $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
00126                         continue;
00127                     }
00128 
00129                     if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
00130                         $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
00131                         continue;
00132                     }
00133 
00134                     // Either field[1] is a name that appears in the mapping,
00135                     // or it's an actual short name. It has to be one or the
00136                     // other, or we don't get to this point.
00137                     $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
00138 
00139                     if ($fields[4] > $fields[5]) {
00140                         $this->log .= "Start time was later than end time - ignoring line\n";
00141                         continue;
00142                     }
00143 
00144                     $this->process_records($fields[0],$roleid,$user,$course,$fields[4],$fields[5]);
00145 
00146                  } // end of while loop
00147 
00148             fclose($fh);
00149             } // end of if(file_open)
00150 
00151             if(! @unlink($filename)) {
00152                 $eventdata = new stdClass();
00153                 $eventdata->modulename        = 'moodle';
00154                 $eventdata->component         = 'course';
00155                 $eventdata->name              = 'flatfile_enrolment';
00156                 $eventdata->userfrom          = get_admin();
00157                 $eventdata->userto            = get_admin();
00158                 $eventdata->subject           = get_string("filelockedmailsubject", "enrol_flatfile");
00159                 $eventdata->fullmessage       = get_string("filelockedmail", "enrol_flatfile", $filename);
00160                 $eventdata->fullmessageformat = FORMAT_PLAIN;
00161                 $eventdata->fullmessagehtml   = '';
00162                 $eventdata->smallmessage      = '';
00163                 message_send($eventdata);
00164                 $this->log .= "Error unlinking file $filename\n";
00165             }
00166 
00167             if (!empty($mailadmins)) {
00168 
00169                 // Send mail to admin
00170                 $eventdata = new stdClass();
00171                 $eventdata->modulename        = 'moodle';
00172                 $eventdata->component         = 'course';
00173                 $eventdata->name              = 'flatfile_enrolment';
00174                 $eventdata->userfrom          = get_admin();
00175                 $eventdata->userto            = get_admin();
00176                 $eventdata->subject           = "Flatfile Enrolment Log";
00177                 $eventdata->fullmessage       = $this->log;
00178                 $eventdata->fullmessageformat = FORMAT_PLAIN;
00179                 $eventdata->fullmessagehtml   = '';
00180                 $eventdata->smallmessage      = '';
00181                 message_send($eventdata);
00182             }
00183 
00184         } // end of if(file_exists)
00185 
00186     } // end of function
00187 
00188     protected function process_buffer() {
00189         global $DB;
00190         // get records from enrol_flatfile table and process any records that are due.
00191         if ($future_enrols = $DB->get_records('enrol_flatfile', null, '')) {
00192             foreach($future_enrols as $id => $future_en) {
00193                     $this->log .= "Processing buffered enrolments.\n";
00194                     $user = $DB->get_record("user", array("id"=>$future_en->userid));
00195                     $course = $DB->get_record("course", array("id"=>$future_en->courseid));
00196                     // enrol the person.
00197                     if($this->process_records($future_en->action, $future_en->roleid,
00198                             $user, $course, $future_en->timestart, $future_en->timeend, false)) {
00199                         //ok record went thru, get rid of the record.
00200                         $DB->delete_records('enrol_flatfile', array('id'=>$future_en->id));
00201                     }
00202             }
00203         }
00204     }
00205 
00206     private function process_records($action, $roleid, $user, $course, $timestart, $timeend, $store_to_buffer = true) {
00207         global $CFG, $DB;
00208 
00209         $mailstudents = $this->get_config('mailstudents');
00210         $mailteachers = $this->get_config('mailteachers');
00211 
00212         // check if timestart is for future processing.
00213         if ($timestart > time()) {
00214             if ($store_to_buffer) {
00215                 // populate into enrol_flatfile table as a future role to be assigned by cron.
00216                 $future_en = new stdClass();
00217                 $future_en->action = $action;
00218                 $future_en->roleid = $roleid;
00219                 $future_en->userid = $user->id;
00220                 $future_en->courseid = $course->id;
00221                 $future_en->timestart = $timestart;
00222                 $future_en->timeend     = $timeend;
00223                 $future_en->timemodified  = time();
00224                 $future_en->id = $DB->insert_record('enrol_flatfile', $future_en);
00225             }
00226             return false;
00227         }
00228 
00229         unset($elog);
00230 
00231         // Create/resurrect a context object
00232         $context = get_context_instance(CONTEXT_COURSE, $course->id);
00233 
00234         if ($action == 'add') {
00235             $instance = $DB->get_record('enrol',
00236                             array('courseid' => $course->id, 'enrol' => 'flatfile'));
00237             if (empty($instance)) {
00238                 // Only add an enrol instance to the course if non-existent
00239                 $enrolid = $this->add_instance($course);
00240                 $instance = $DB->get_record('enrol', array('id' => $enrolid));
00241             }
00242             // Enrol the user with this plugin instance
00243             $this->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
00244         } else {
00245             $instances = $DB->get_records('enrol',
00246                             array('enrol' => 'flatfile', 'courseid' => $course->id));
00247             foreach ($instances as $instance) {
00248                 // Unenrol the user from all flatfile enrolment instances
00249                 $this->unenrol_user($instance, $user->id);
00250             }
00251         }
00252 
00253 
00254         if ( empty($elog) and ($action== "add") ) {
00255             $role = $DB->get_record("role", array("id"=>$roleid));
00256 
00257             if ($role->archetype == "student") {
00258 
00259                 // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
00260                 if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) {
00261                     foreach ($teachers as $u) {
00262                         $teacher = $u;
00263                     }
00264                 }
00265 
00266                 if (!isset($teacher)) {
00267                     $teacher = get_admin();
00268                 }
00269             } else {
00270                 $teacher = get_admin();
00271             }
00272 
00273 
00274             if (!empty($mailstudents)) {
00275                 // Send mail to students
00276                 $a = new stdClass();
00277                 $a->coursename = format_string($course->fullname, true, array('context' => $context));
00278                 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
00279                 $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
00280 
00281                 $eventdata = new stdClass();
00282                 $eventdata->modulename        = 'moodle';
00283                 $eventdata->component         = 'course';
00284                 $eventdata->name              = 'flatfile_enrolment';
00285                 $eventdata->userfrom          = $teacher;
00286                 $eventdata->userto            = $user;
00287                 $eventdata->subject           = $subject;
00288                 $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
00289                 $eventdata->fullmessageformat = FORMAT_PLAIN;
00290                 $eventdata->fullmessagehtml   = '';
00291                 $eventdata->smallmessage      = '';
00292                 message_send($eventdata);
00293             }
00294 
00295             if (!empty($mailteachers) && $teachers) {
00296 
00297                 // Send mail to teachers
00298                 foreach($teachers as $teacher) {
00299                     $a = new stdClass();
00300                     $a->course = format_string($course->fullname, true, array('context' => $context));
00301                     $a->user = fullname($user);
00302                     $subject = get_string("enrolmentnew", 'enrol', format_string($course->shortname, true, array('context' => $context)));
00303 
00304                     $eventdata = new stdClass();
00305                     $eventdata->modulename        = 'moodle';
00306                     $eventdata->component         = 'course';
00307                     $eventdata->name              = 'flatfile_enrolment';
00308                     $eventdata->userfrom          = $user;
00309                     $eventdata->userto            = $teacher;
00310                     $eventdata->subject           = $subject;
00311                     $eventdata->fullmessage       = get_string('enrolmentnewuser', 'enrol', $a);
00312                     $eventdata->fullmessageformat = FORMAT_PLAIN;
00313                     $eventdata->fullmessagehtml   = '';
00314                     $eventdata->smallmessage      = '';
00315                     message_send($eventdata);
00316                 }
00317             }
00318         }
00319 
00320 
00321         if (empty($elog)) {
00322             $elog = "OK\n";
00323         }
00324         $this->log .= $elog;
00325 
00326         return true;
00327     }
00328 
00336     function get_roles() {
00337         global $DB;
00338 
00339         // Get all roles
00340         $roles = $DB->get_records('role', null, '', 'id, name, shortname');
00341 
00342         $config = get_config('enrol_flatfile');
00343 
00344         // Set some usable mapping configs for later
00345         foreach($roles as $id => $role) {
00346             if (isset($config->{"map_{$id}"})) {
00347                 set_config('map_'.$role->shortname, $config->{"map_{$id}"}, 'enrol_flatfile');
00348             } else {
00349                 set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
00350             }
00351         }
00352         // Get the updated config
00353         $config = get_config('enrol_flatfile');
00354         // Get a list of all the roles in the database, indexed by their short names.
00355         $roles = $DB->get_records('role', null, '', 'shortname, id');
00356 
00357         // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
00358         array_walk($roles, create_function('&$value', '$value = $value->id;'));
00359         $rolemap = array();
00360         foreach($config as $name => $value) {
00361             if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
00362                 $rolemap[$value] = $key;
00363             }
00364         }
00365 
00366         return array($roles, $rolemap);
00367     }
00368 
00369 } // end of class
 All Data Structures Namespaces Files Functions Variables Enumerations