enrol flatfile MDL-23892 the flatfile plugin now processes future enrolment start...
authorAparup Banerjee <nebgor@gmail.com>
Tue, 14 Sep 2010 03:52:59 +0000 (03:52 +0000)
committerAparup Banerjee <nebgor@gmail.com>
Tue, 14 Sep 2010 03:52:59 +0000 (03:52 +0000)
enrol/flatfile/db/install.xml [new file with mode: 0644]
enrol/flatfile/db/upgrade.php [new file with mode: 0644]
enrol/flatfile/lib.php
enrol/flatfile/version.php

diff --git a/enrol/flatfile/db/install.xml b/enrol/flatfile/db/install.xml
new file mode 100644 (file)
index 0000000..8bd4e38
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="enrol/flatfile/db" VERSION="20100914" COMMENT="XMLDB file for Moodle enrol/flatfile"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
+>
+  <TABLES>
+    <TABLE NAME="enrol_flatfile" COMMENT="enrol_flatfile table retrofitted from MySQL">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="action"/>
+        <FIELD NAME="action" TYPE="char" LENGTH="10" NOTNULL="false" DEFAULT="add" SEQUENCE="false" PREVIOUS="id" NEXT="role_id"/>
+        <FIELD NAME="role_id" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="action" NEXT="user_id"/>
+        <FIELD NAME="user_id" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="role_id" NEXT="course_id"/>
+        <FIELD NAME="course_id" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="user_id" NEXT="timestart"/>
+        <FIELD NAME="timestart" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="course_id" NEXT="timeend"/>
+        <FIELD NAME="timeend" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="timestart" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="20" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="timeend"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="id" TYPE="primary" FIELDS="id" COMMENT="primary key"/>
+      </KEYS>
+    </TABLE>
+  </TABLES>
+</XMLDB>
\ No newline at end of file
diff --git a/enrol/flatfile/db/upgrade.php b/enrol/flatfile/db/upgrade.php
new file mode 100644 (file)
index 0000000..d495d78
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Keeps track of upgrades to the enrol_flatfile plugin
+ *
+ * @package    enrol
+ * @subpackage flatfile
+ * @copyright  2010 Aparup Banerjee <aparup@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+function xmldb_enrol_flatfile_upgrade($oldversion) {
+    global $CFG, $DB;
+
+    $result = TRUE;
+    $dbman = $DB->get_manager();
+
+    if ($oldversion < 2010091400) {
+
+        // Define table enrol_flatfile to be created
+        $table = new xmldb_table('enrol_flatfile');
+
+        // Adding fields to table enrol_flatfile
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+        $table->add_field('action', XMLDB_TYPE_CHAR, '10', null, null, null, 'add');
+        $table->add_field('role_id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
+        $table->add_field('user_id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
+        $table->add_field('course_id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
+        $table->add_field('timestart', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
+        $table->add_field('timeend', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, '0');
+        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null);
+
+        // Adding keys to table enrol_flatfile
+        $table->add_key('id', XMLDB_KEY_PRIMARY, array('id'));
+
+        // Conditionally launch create table for enrol_flatfile
+        if (!$dbman->table_exists($table)) {
+            $dbman->create_table($table);
+        }
+
+        // flatfile savepoint reached
+        upgrade_plugin_savepoint(true, 2010091400, 'enrol', 'flatfile');
+    }
+
+    return $result;
+}
index 0a415b2..fabfa2c 100644 (file)
@@ -48,14 +48,21 @@ class enrol_flatfile_plugin extends enrol_plugin {
      *   starttime        = start time (in seconds since epoch) - optional
      *   endtime          = end time (in seconds since epoch) - optional
      */
+    private $log;
+
     public function cron() {
+        $this->process_file();
+
+        $this->process_buffer();
+
+        echo $this->log;
+    } // end of function
+
+    protected function process_file() {
         global $CFG, $DB;
 
         $filelocation = $this->get_config('location');
-        $mailstudents = $this->get_config('mailstudents');
-        $mailteachers = $this->get_config('mailteachers');
         $mailadmins   = $this->get_config('mailadmins');
-
         if (empty($filelocation)) {
             $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
         } else {
@@ -63,7 +70,6 @@ class enrol_flatfile_plugin extends enrol_plugin {
         }
 
         if ( file_exists($filename) ) {
-
             $this->log  = userdate(time()) . "\n";
             $this->log .= "Flatfile enrol cron found file: $filename\n\n";
 
@@ -77,7 +83,6 @@ class enrol_flatfile_plugin extends enrol_plugin {
                     $line++;
                     $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
 
-
                 /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
                     if (count($fields) != 4 and count($fields) !=6) {
                         if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
@@ -86,7 +91,6 @@ class enrol_flatfile_plugin extends enrol_plugin {
                         continue;
                     }
 
-
                     $fields[0] = trim(strtolower($fields[0]));
                     $fields[1] = trim(strtolower($fields[1]));
                     $fields[2] = trim($fields[2]);
@@ -105,15 +109,12 @@ class enrol_flatfile_plugin extends enrol_plugin {
 
                     $this->log .= ":";
 
-
-
                 /// check correct formatting of operation field
                     if ($fields[0] != "add" and $fields[0] != "del") {
                         $this->log .= "Unknown operation in field 1 - ignoring line\n";
                         continue;
                     }
 
-
                 /// check correct formatting of role field
                     if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
                         $this->log .= "Unknown role in field2 - ignoring line\n";
@@ -125,118 +126,24 @@ class enrol_flatfile_plugin extends enrol_plugin {
                         continue;
                     }
 
-
                     if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
                         $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
                         continue;
                     }
 
-                    if ($fields[4] > $fields[5]) {
-                        $this->log .= "Start time was later than end time - ignoring line\n";
-                        continue;
-                    }
-
-
-                    unset($elog);
-
                     // Either field[1] is a name that appears in the mapping,
                     // or it's an actual short name. It has to be one or the
                     // other, or we don't get to this point.
                     $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
 
-                    // Create/resurrect a context object
-                    $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
-
-                    if ($fields[0] == 'add') {
-                        $instance = $DB->get_record('enrol',
-                                        array('courseid' => $course->id, 'enrol' => 'flatfile'));
-                        if (empty($instance)) {
-                            // Only add an enrol instance to the course if non-existent
-                            $enrolid = $this->add_instance($course);
-                            $instance = $DB->get_record('enrol', array('id' => $enrolid));
-                        }
-
-                        // Enrol the user with this plugin instance
-                        $this->enrol_user($instance, $user->id, $roleid, $fields[4], $fields[5]);
-                    } else {
-                        $instances = $DB->get_records('enrol',
-                                        array('enrol' => 'flatfile', 'courseid' => $course->id));
-                        foreach ($instances as $instance) {
-                            // Unenrol the user from all flatfile enrolment instances
-                            $this->unenrol_user($instance, $user->id);
-                        }
-                    }
-
-
-                    if ( empty($elog) and ($fields[0] == "add") ) {
-
-                        if ($fields[1] == "student") {
-
-                            // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
-                            if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) {
-                                foreach ($teachers as $u) {
-                                    $teacher = $u;
-                                }
-                            }
-
-                            if (!isset($teacher)) {
-                                $teacher = get_admin();
-                            }
-                        } else {
-                            $teacher = get_admin();
-                        }
-
-
-                        if (!empty($mailstudents)) {
-                            // Send mail to students
-                            $a->coursename = "$course->fullname";
-                            $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
-
-                            $eventdata = new object();
-                            $eventdata->modulename        = 'moodle';
-                            $eventdata->component         = 'course';
-                            $eventdata->name              = 'flatfile_enrolment';
-                            $eventdata->userfrom          = $teacher;
-                            $eventdata->userto            = $user;
-                            $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
-                            $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
-                            $eventdata->fullmessageformat = FORMAT_PLAIN;
-                            $eventdata->fullmessagehtml   = '';
-                            $eventdata->smallmessage      = '';
-                            message_send($eventdata);
-                        }
-
-                        if (!empty($mailteachers) && $teachers) {
-
-                            // Send mail to teachers
-                            foreach($teachers as $teacher) {
-                                $a->course = "$course->fullname";
-                                $a->user = fullname($user);
-
-                                $eventdata = new object();
-                                $eventdata->modulename        = 'moodle';
-                                $eventdata->component         = 'course';
-                                $eventdata->name              = 'flatfile_enrolment';
-                                $eventdata->userfrom          = $user;
-                                $eventdata->userto            = $teacher;
-                                $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
-                                $eventdata->fullmessage       = get_string('enrolmentnewuser', 'enrol', $a);
-                                $eventdata->fullmessageformat = FORMAT_PLAIN;
-                                $eventdata->fullmessagehtml   = '';
-                                $eventdata->smallmessage      = '';
-                                message_send($eventdata);
-                            }
-                        }
+                    if ($fields[4] > $fields[5]) {
+                        $this->log .= "Start time was later than end time - ignoring line\n";
+                        continue;
                     }
 
+                    $this->process_records($fields[0],$roleid,$user,$course,$fields[4],$fields[5]);
 
-                    if (empty($elog)) {
-                        $elog = "OK\n";
-                    }
-                    $this->log .= $elog;
-
-                } // end of while loop
+                 } // end of while loop
 
             fclose($fh);
             } // end of if(file_open)
@@ -278,6 +185,143 @@ class enrol_flatfile_plugin extends enrol_plugin {
 
     } // end of function
 
+    protected function process_buffer() {
+        global $DB;
+        // get records from enrol_flatfile table and process any records that are due.
+        if ($future_enrols = $DB->get_records('enrol_flatfile', null, '')) {
+            foreach($future_enrols as $id => $future_en) {
+                    $this->log .= "Processing buffered enrolments.\n";
+                    $user = $DB->get_record("user", array("id"=>$future_en->user_id));
+                    $course = $DB->get_record("course", array("id"=>$future_en->course_id));
+                    // enrol the person.
+                    if($this->process_records($future_en->action, $future_en->role_id,
+                            $user, $course, $future_en->timestart, $future_en->timeend, false)) {
+                        //ok record went thru, get rid of the record.
+                        $DB->delete_records('enrol_flatfile', array('id'=>$future_en->id));
+                    }
+            }
+        }
+    }
+
+    private function process_records($action, $roleid, $user, $course, $timestart, $timeend, $store_to_buffer = true) {
+        global $CFG, $DB;
+
+        $mailstudents = $this->get_config('mailstudents');
+        $mailteachers = $this->get_config('mailteachers');
+
+        // check if timestart is for future processing.
+        if ($timestart > time()) {
+            if ($store_to_buffer) {
+                // populate into enrol_flatfile table as a future role to be assigned by cron.
+                $future_en = new object();
+                $future_en->action = $action;
+                $future_en->role_id = $roleid;
+                $future_en->user_id = $user->id;
+                $future_en->course_id = $course->id;
+                $future_en->timestart = $timestart;
+                $future_en->timeend     = $timeend;
+                $future_en->timemodified  = time();
+                $future_en->id = $DB->insert_record('enrol_flatfile', $future_en);
+            }
+            return false;
+        }
+
+        unset($elog);
+
+        // Create/resurrect a context object
+        $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
+        if ($action == 'add') {
+            $instance = $DB->get_record('enrol',
+                            array('courseid' => $course->id, 'enrol' => 'flatfile'));
+            if (empty($instance)) {
+                // Only add an enrol instance to the course if non-existent
+                $enrolid = $this->add_instance($course);
+                $instance = $DB->get_record('enrol', array('id' => $enrolid));
+            }
+            // Enrol the user with this plugin instance
+            $this->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
+        } else {
+            $instances = $DB->get_records('enrol',
+                            array('enrol' => 'flatfile', 'courseid' => $course->id));
+            foreach ($instances as $instance) {
+                // Unenrol the user from all flatfile enrolment instances
+                $this->unenrol_user($instance, $user->id);
+            }
+        }
+
+
+        if ( empty($elog) and ($action== "add") ) {
+            $role = $DB->get_record("role", array("id"=>$roleid));
+
+            if ($role->archetype == "student") {
+
+                // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong
+                if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) {
+                    foreach ($teachers as $u) {
+                        $teacher = $u;
+                    }
+                }
+
+                if (!isset($teacher)) {
+                    $teacher = get_admin();
+                }
+            } else {
+                $teacher = get_admin();
+            }
+
+
+            if (!empty($mailstudents)) {
+                // Send mail to students
+                $a->coursename = "$course->fullname";
+                $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
+
+                $eventdata = new object();
+                $eventdata->modulename        = 'moodle';
+                $eventdata->component         = 'course';
+                $eventdata->name              = 'flatfile_enrolment';
+                $eventdata->userfrom          = $teacher;
+                $eventdata->userto            = $user;
+                $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
+                $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
+                $eventdata->fullmessageformat = FORMAT_PLAIN;
+                $eventdata->fullmessagehtml   = '';
+                $eventdata->smallmessage      = '';
+                message_send($eventdata);
+            }
+
+            if (!empty($mailteachers) && $teachers) {
+
+                // Send mail to teachers
+                foreach($teachers as $teacher) {
+                    $a->course = "$course->fullname";
+                    $a->user = fullname($user);
+
+                    $eventdata = new object();
+                    $eventdata->modulename        = 'moodle';
+                    $eventdata->component         = 'course';
+                    $eventdata->name              = 'flatfile_enrolment';
+                    $eventdata->userfrom          = $user;
+                    $eventdata->userto            = $teacher;
+                    $eventdata->subject           = get_string("enrolmentnew", 'enrol', $course->shortname);
+                    $eventdata->fullmessage       = get_string('enrolmentnewuser', 'enrol', $a);
+                    $eventdata->fullmessageformat = FORMAT_PLAIN;
+                    $eventdata->fullmessagehtml   = '';
+                    $eventdata->smallmessage      = '';
+                    message_send($eventdata);
+                }
+            }
+        }
+
+
+        if (empty($elog)) {
+            $elog = "OK\n";
+        }
+        $this->log .= $elog;
+
+        return true;
+    }
+
     /**
      * Returns a pair of arrays.  The first is the set of roleids, indexed by
      * their shortnames.  The second is the set of shortnames that have
index 70fb056..c0aa218 100644 (file)
@@ -27,5 +27,5 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2010070800;
+$plugin->version = 2010091400;
 $plugin->cron = 60;