Index: lib/db/install.xml =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/install.xml,v retrieving revision 1.172 diff -u -r1.172 install.xml --- lib/db/install.xml 10 Oct 2008 18:06:16 -0000 1.172 +++ lib/db/install.xml 5 Nov 2008 11:14:17 -0000 @@ -831,7 +831,8 @@ - + + Index: lib/db/upgrade.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v retrieving revision 1.244 diff -u -r1.244 upgrade.php --- lib/db/upgrade.php 5 Nov 2008 00:12:30 -0000 1.244 +++ lib/db/upgrade.php 5 Nov 2008 11:14:17 -0000 @@ -873,6 +873,20 @@ upgrade_main_savepoint($result, 2008101300); } + if ($result && $oldversion < 2008110600) { + + /// Define field active to be added to role_assignments + $table = new XMLDBTable('role_assignments'); + $field = new XMLDBField('active'); + $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'sortorder'); + + /// Launch add field active + $result = $result && add_field($table, $field); + + /// Main savepoint reached + upgrade_main_savepoint($result, 2008110600); + } + return $result; } Index: lib/accesslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/accesslib.php,v retrieving revision 1.530 diff -u -r1.530 accesslib.php --- lib/accesslib.php 5 Nov 2008 08:17:31 -0000 1.530 +++ lib/accesslib.php 5 Nov 2008 11:14:11 -0000 @@ -517,6 +517,7 @@ ON ra.roleid=rc.roleid AND ra.contextid=ctx.id WHERE ctx.contextlevel=10 AND ra.userid=? + AND ra.active = 1 AND rc.capability IN (?, ?, ?) GROUP BY rc.capability HAVING SUM(rc.permission) > 0"; @@ -1036,7 +1037,7 @@ JOIN {$CFG->prefix}context ctx ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.") JOIN {$CFG->prefix}role_assignments ra - ON (ra.contextid=ctx.id AND ra.userid=:userid) + ON (ra.contextid=ctx.id AND ra.userid=:userid AND ra.active=1) UNION SELECT c.id, ctx.id AS ctxid, ctx.path AS ctxpath, @@ -1154,6 +1155,7 @@ LEFT OUTER JOIN {role_capabilities} rc ON (rc.roleid=ra.roleid AND rc.contextid=ra.contextid) WHERE ra.userid = ? AND ctx.contextlevel <= ".CONTEXT_COURSE." + AND ra.active=1 ORDER BY ctx.depth, ctx.path, ra.roleid"; $params = array($userid); $rs = $DB->get_recordset_sql($sql, $params); @@ -1255,6 +1257,7 @@ JOIN {role_capabilities} rco ON (rco.roleid=ra.roleid AND rco.contextid=sctx.id) WHERE ra.userid = ? + AND ra.active = 1 AND sctx.contextlevel <= ".CONTEXT_COURSE." ORDER BY sctx.depth, sctx.path, ra.roleid"; $params = array($userid); @@ -1326,6 +1329,7 @@ JOIN {context} ctx ON ra.contextid=ctx.id WHERE ra.userid = ? + AND ra.active = 1 AND (ctx.path = ? OR ctx.path LIKE ?) ORDER BY ctx.depth, ctx.path, ra.roleid"; $params = array($userid, $context->path, $context->path."/%"); @@ -2785,6 +2789,11 @@ /// by repeating queries with the same exact parameters in a 100 secs time window $ra->timestart = round($timestart, -2); $ra->timeend = $timeend; + /// If timestart is in the future then the role must be set to not be active + /// (otherwise, database defaults to active=1); will be activated later in cron + if($timestart > time()) { + $newra->active = 0; + } $ra->timemodified = $timemodified; $ra->modifierid = empty($USER->id) ? 0 : $USER->id; @@ -2800,6 +2809,11 @@ /// by repeating queries with the same exact parameters in a 100 secs time window $ra->timestart = round($timestart, -2); $ra->timeend = $timeend; + /// If timestart is in the future then the role must be set to not be active + /// (otherwise, database defaults to active=1); will be activated later in cron + if($timestart > time()) { + $ra->active = 0; + } $ra->timemodified = $timemodified; $ra->modifierid = empty($USER->id) ? 0 : $USER->id; @@ -4097,6 +4111,7 @@ WHERE ra.userid = ? AND ra.roleid = r.id AND ra.contextid = c.id + AND ra.active = 1 AND ra.contextid $contextids $hiddensql ORDER BY $order"; @@ -5168,6 +5183,35 @@ } /** + * If the user has any role on the specified (exact) context which is not + * yet active, then we return the timestart of the role assignment. + * - If they have two such roles then we return the earliest timestart. + * - Note that they may have other active roles. This function is intended + * to be used after a has_capability fails, so that information can be + * displayed to the user about when they might have access again. + * @param int $contextid ID of context + * @param int $userid User ID, leave out for current user + * @return False if none, or an object containing fields from role_assignments + * (including timestart). + */ +function get_inactive_role_on_context($contextid,$userid=0) { + global $CFG,$USER; + if(!$userid) { + $userid=$USER->id; + } + $rs=get_recordset_sql("SELECT ra.* + FROM {$CFG->prefix}role_assignments ra + WHERE ra.userid=$userid + AND ra.contextid=$contextid + AND ra.active=0 + AND ra.timestart<>0 + ORDER BY ra.timestart"); + $firstresult=rs_fetch_next_record($rs); + rs_close($rs); + return $firstresult; // False if no records, or the first record +} + +/** * Switches the current user to another role for the current session and only * in the given context. * Index: lib/moodlelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v retrieving revision 1.1127 diff -u -r1.1127 moodlelib.php --- lib/moodlelib.php 5 Nov 2008 08:17:31 -0000 1.1127 +++ lib/moodlelib.php 5 Nov 2008 11:14:14 -0000 @@ -2072,9 +2072,16 @@ } } - /// Non-guests who don't currently have access, check if they can be allowed in as a guest + /// Non-guests who don't currently have access... if ($USER->username != 'guest' and !has_capability('moodle/course:view', $COURSE->context)) { + /// Is it because they have a role which hasn't started yet? + if($inactive=get_inactive_role_on_context($COURSE->context->id)) { + /// Display info about the start date and stop + notice(get_string('courserolenotstarted','',userdate($inactive->timestart)), $CFG->wwwroot .'/'); + } + + /// Check if they can be allowed in as a guest if ($COURSE->guest == 1) { // Temporarily assign them guest role for this context, if it fails later user is asked to enrol $USER->access = load_temp_role($COURSE->context, $CFG->guestroleid, $USER->access); Index: admin/cron.php =================================================================== RCS file: /cvsroot/moodle/moodle/admin/cron.php,v retrieving revision 1.151 diff -u -r1.151 cron.php --- admin/cron.php 13 Oct 2008 21:53:49 -0000 1.151 +++ admin/cron.php 5 Nov 2008 11:14:08 -0000 @@ -242,6 +242,9 @@ } + mtrace('Activating any enrolments with start times ...'); + execute_sql("UPDATE {$CFG->prefix}role_assignments SET active=1 WHERE active=0 AND timestart<".time(),false); + mtrace('done.'); mtrace('Starting main gradebook job ...'); grade_cron(); Index: lang/en_utf8/moodle.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/moodle.php,v retrieving revision 1.205 diff -u -r1.205 moodle.php --- lang/en_utf8/moodle.php 4 Nov 2008 05:12:12 -0000 1.205 +++ lang/en_utf8/moodle.php 5 Nov 2008 11:14:09 -0000 @@ -300,6 +300,7 @@ $string['courserequestreason'] = 'Reasons for wanting this course'; $string['courserequestsuccess'] = 'Successfully saved your course request. Expect an email within a few days with the outcome'; $string['courserestore'] = 'Course restore'; +$string['courserolenotstarted'] = 'You don\'t yet have access to this course. Your access should be enabled shortly after $a - please try again then.'; $string['courses'] = 'Courses'; $string['coursescategory'] = 'Courses in the same category'; $string['coursesettings'] = 'Course default settings'; Index: version.php =================================================================== RCS file: /cvsroot/moodle/moodle/version.php,v retrieving revision 1.865 diff -u -r1.865 version.php --- version.php 4 Nov 2008 02:02:13 -0000 1.865 +++ version.php 5 Nov 2008 11:14:07 -0000 @@ -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 = 2008101300; // YYYYMMDD = date of the last version bump + $version = 2008110600; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20081104)'; // Human-friendly version name