MDL-23237 new timecreated field in user_emrolments and adding timestart to enrol...
authorPetr Skoda <skodak@moodle.org>
Tue, 13 Jul 2010 20:58:17 +0000 (20:58 +0000)
committerPetr Skoda <skodak@moodle.org>
Tue, 13 Jul 2010 20:58:17 +0000 (20:58 +0000)
admin/uploaduser.php
enrol/ajax.php
enrol/category/locallib.php
enrol/manual/manage.php
enrol/self/lib.php
lib/db/install.xml
lib/db/upgrade.php
lib/enrollib.php
version.php

index bc2d1d5..d29fce1 100755 (executable)
@@ -72,6 +72,9 @@ $errorstr                   = get_string('error');
 $returnurl = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php';
 $bulknurl  = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
 
+$today = time();
+$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
+
 // array of all valid fields for validation
 $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email',
         'city', 'country', 'lang', 'auth', 'timezone', 'mailformat',
@@ -628,17 +631,15 @@ if ($formdata = $mform->is_cancelled()) {
 
                 if ($rid) {
                     // find duration
-                    $timestart = 0;
                     $timeend   = 0;
                     if (!empty($user->{'enrolperiod'.$i})) {
                         $duration = (int)$user->{'enrolperiod'.$i} * 86400; // convert days to seconds
                         if ($duration > 0) { // sanity check
-                            $timestart = time();
-                            $timeend   = $timestart + $duration;
+                            $timeend   = $today + $duration;
                         }
                     }
 
-                    $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $timestart, $timeend, true);
+                    $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend, true);
 
                     $a = new object();
                     $a->course = $shortname;
index 273b225..e65b0dc 100644 (file)
@@ -161,7 +161,6 @@ switch ($action) {
                 break;
         }
         if ($duration <= 0) {
-            $timestart = 0;
             $timeend = 0;
         } else {
             $timeend = $timestart + ($duration*24*60*60);
index 663d614..434cda8 100644 (file)
@@ -76,7 +76,7 @@ class enrol_category_handler {
         $params = array('courselevel'=>CONTEXT_COURSE, 'match'=>$parentcontext->path.'/%', 'userid'=>$ra->userid);
         $rs = $DB->get_recordset_sql($sql, $params);
         foreach ($rs as $instance) {
-            $plugin->enrol_user($instance, $ra->userid);
+            $plugin->enrol_user($instance, $ra->userid, null, $ra->timemodified);
         }
         $rs->close();
 
@@ -196,17 +196,18 @@ function enrol_category_sync_course($course) {
     }
 
     // add new enrolments
-    $sql = "SELECT ra.userid
-              FROM (SELECT DISTINCT xra.userid
+    $sql = "SELECT ra.userid, ra.estart
+              FROM (SELECT xra.userid, MIN(xra.timemodified) AS estart
                       FROM {role_assignments} xra
                      WHERE xra.roleid $roleids AND xra.contextid $contextids
+                  GROUP BY xra.userid
                    ) ra
          LEFT JOIN {user_enrolments} ue ON (ue.enrolid = :instanceid AND ue.userid = ra.userid)
              WHERE ue.id IS NULL";
     $params['instanceid'] = $instance->id;
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $ra) {
-        $plugin->enrol_user($instance, $ra->userid);
+        $plugin->enrol_user($instance, $ra->userid, null, $ra->estart);
     }
     $rs->close();
 
@@ -294,21 +295,24 @@ function enrol_category_sync_full() {
     $rs->close();
 
     // add missing enrolments
-    $sql = "SELECT e.*, cat.userid
+    $sql = "SELECT e.*, cat.userid, cat.estart
               FROM {enrol} e
               JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
-              JOIN (SELECT DISTINCT cctx.path, ra.userid
+              JOIN (SELECT cctx.path, ra.userid, MIN(ra.timemodified) AS estart
                       FROM {course_categories} cc
                       JOIN {context} cctx ON (cctx.instanceid = cc.id AND cctx.contextlevel = :catlevel)
                       JOIN {role_assignments} ra ON (ra.contextid = cctx.id AND ra.roleid $roleids)
+                  GROUP BY cctx.path, ra.userid
                    ) cat ON (ctx.path LIKE $parentcat)
          LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cat.userid)
              WHERE e.enrol = 'category' AND ue.id IS NULL";
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach($rs as $instance) {
         $userid = $instance->userid;
+        $estart = $instance->estart;
         unset($instance->userid);
-        $plugin->enrol_user($instance, $userid);
+        unset($instance->estart);
+        $plugin->enrol_user($instance, $userid, null, $estart);
     }
     $rs->close();
 
index c6f35fc..4b8bd04 100644 (file)
@@ -109,12 +109,11 @@ if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
             }
 
             if ($extendperiod <= 0) {
-                $timestart = 0;
                 $timeend = 0;
             } else {
                 $timeend = $timestart + $extendperiod;
             }
-            $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend, true);
+            $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
             add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
         }
 
index fa29bee..f4bc527 100644 (file)
@@ -120,12 +120,11 @@ class enrol_self_plugin extends enrol_plugin {
         if ($instance->id == $instanceid) {
             if ($data = $form->get_data()) {
                 $enrol = enrol_get_plugin('self');
+                $timestart = time();
                 if ($instance->enrolperiod) {
-                    $timestart = time();
-                    $tineend   = $timestart + $instance->enrolperiod;
+                    $tineend = $timestart + $instance->enrolperiod;
                 } else {
-                    $timestart = 0;
-                    $tineend   = 0;
+                    $tineend = 0;
                 }
 
                 $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $tineend);
index 44da1fa..da79869 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20100706" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20100713" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
         <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="enrolid" NEXT="timestart"/>
         <FIELD NAME="timestart" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="timeend"/>
         <FIELD NAME="timeend" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="2147483647" SEQUENCE="false" PREVIOUS="timestart" NEXT="modifierid"/>
-        <FIELD NAME="modifierid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeend" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="modifierid"/>
+        <FIELD NAME="modifierid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeend" NEXT="timecreated"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="modifierid" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="enrolid"/>
index 47beace..5192ce8 100644 (file)
@@ -3863,6 +3863,7 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
         $table->add_field('timestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
         $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2147483647');
         $table->add_field('modifierid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
 
         // Adding keys to table course_participant
@@ -4282,9 +4283,9 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
         $roles = $DB->get_fieldset_sql("SELECT DISTINCT roleid FROM {role_capabilities} WHERE contextid = :syscontext AND capability = :participate AND permission = 1", $params);
         list($sqlroles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r00');
 
-        $sql = "INSERT INTO {user_enrolments} (status, enrolid, userid, timestart, timeend, modifierid, timemodified)
+        $sql = "INSERT INTO {user_enrolments} (status, enrolid, userid, timestart, timeend, modifierid, timecreated, timemodified)
 
-                SELECT 0, e.id, ra.userid, MIN(ra.timestart), MIN(ra.timeend), 0, MAX(ra.timemodified)
+                SELECT 0, e.id, ra.userid, MIN(ra.timestart), MIN(ra.timeend), 0, MIN(ra.timemodified), MAX(ra.timemodified)
                   FROM {role_assignments} ra
                   JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = 50)
                   JOIN {enrol} e ON (e.enrol = ra.enrol AND e.courseid = c.instanceid)
@@ -4741,6 +4742,25 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
         upgrade_main_savepoint(true, 2010071101);
     }
 
+    if ($oldversion < 2010071300) {
+        // Define field timecreated to be added to user_enrolments
+        $table = new xmldb_table('user_enrolments');
+        $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'modifierid');
+
+        // Launch add field timecreated
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        // now try to guess the time created
+        $sql = "UPDATE {user_enrolments} SET timecreated = timemodified WHERE timecreated = 0";
+        $DB->execute($sql);
+        $sql = "UPDATE {user_enrolments} SET timecreated = timestart WHERE timestart <> 0 AND timestart < timemodified";
+        $DB->execute($sql);
+
+        upgrade_main_savepoint(true, 2010071300);
+    }
+
 
     return true;
 }
index 4900195..7ce55e1 100644 (file)
@@ -842,8 +842,8 @@ abstract class enrol_plugin {
      * @param stdClass $instance
      * @param int $userid
      * @param int $roleid optional role id
-     * @param int $timestart
-     * @param int $timeend
+     * @param int $timestart 0 means unknown
+     * @param int $timeend 0 means forever
      * @return void
      */
     public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0) {
@@ -878,7 +878,8 @@ abstract class enrol_plugin {
             $ue->timestart    = $timestart;
             $ue->timeend      = $timeend;
             $ue->modifier     = $USER->id;
-            $ue->timemodified = time();
+            $ue->timecreated  = time();
+            $ue->timemodified = $ue->timecreated;
             $ue->id = $DB->insert_record('user_enrolments', $ue);
 
             $inserted = true;
index 5a60761..6bfeb5d 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2010071200;  // YYYYMMDD   = date of the last version bump
+    $version = 2010071300;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 Preview 4+ (Build: 20100713)';  // Human-friendly version name