diff --git a/admin/uploaduser.php b/admin/uploaduser.php index bc2d1d55d7b..d29fce19ffb 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -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; diff --git a/enrol/ajax.php b/enrol/ajax.php index 273b225bbbb..e65b0dca1ef 100644 --- a/enrol/ajax.php +++ b/enrol/ajax.php @@ -161,7 +161,6 @@ switch ($action) { break; } if ($duration <= 0) { - $timestart = 0; $timeend = 0; } else { $timeend = $timestart + ($duration*24*60*60); diff --git a/enrol/category/locallib.php b/enrol/category/locallib.php index 663d614ac15..434cda8b2ca 100644 --- a/enrol/category/locallib.php +++ b/enrol/category/locallib.php @@ -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(); diff --git a/enrol/manual/manage.php b/enrol/manual/manage.php index c6f35fcd12a..4b8bd0409dc 100644 --- a/enrol/manual/manage.php +++ b/enrol/manual/manage.php @@ -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! } diff --git a/enrol/self/lib.php b/enrol/self/lib.php index fa29bee6ced..f4bc527cd74 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -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); diff --git a/lib/db/install.xml b/lib/db/install.xml index 44da1fabc28..da798691b2f 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -286,8 +286,9 @@ - - + + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 47beaceb648..5192ce88f82 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/lib/enrollib.php b/lib/enrollib.php index 4900195a5d2..7ce55e1e349 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -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; diff --git a/version.php b/version.php index 5a60761859d..6bfeb5d3896 100644 --- a/version.php +++ b/version.php @@ -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