Skip to content

Commit

Permalink
MDL-23237 new timecreated field in user_emrolments and adding timesta…
Browse files Browse the repository at this point in the history
…rt to enrol plugins that know it
  • Loading branch information
skodak committed Jul 13, 2010
1 parent 24d5b99 commit 2a6dcb7
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 27 deletions.
9 changes: 5 additions & 4 deletions admin/uploaduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
$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',
Expand Down Expand Up @@ -628,17 +631,15 @@

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;
Expand Down
1 change: 0 additions & 1 deletion enrol/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
break;
}
if ($duration <= 0) {
$timestart = 0;
$timeend = 0;
} else {
$timeend = $timestart + ($duration*24*60*60);
Expand Down
18 changes: 11 additions & 7 deletions enrol/category/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function role_assigned($ra) {
$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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
3 changes: 1 addition & 2 deletions enrol/manual/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@
}

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!
}

Expand Down
7 changes: 3 additions & 4 deletions enrol/self/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ public function enrol_page_hook(stdClass $instance) {
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);
Expand Down
7 changes: 4 additions & 3 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -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"
>
Expand Down Expand Up @@ -286,8 +286,9 @@
<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"/>
Expand Down
24 changes: 22 additions & 2 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3863,6 +3863,7 @@ function xmldb_main_upgrade($oldversion) {
$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
Expand Down Expand Up @@ -4282,9 +4283,9 @@ function xmldb_main_upgrade($oldversion) {
$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)
Expand Down Expand Up @@ -4741,6 +4742,25 @@ function xmldb_main_upgrade($oldversion) {
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;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ public function try_guestaccess(stdClass $instance) {
* @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) {
Expand Down Expand Up @@ -878,7 +878,8 @@ public function enrol_user(stdClass $instance, $userid, $roleid = null, $timesta
$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;
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a6dcb7

Please sign in to comment.