// This file defines settingpages and externalpages under the "users" category
+if (has_capability('moodle/site:config',$systemcontext)) { // speedup for non-admins
+
$ADMIN->add('users', new admin_externalpage('userauthentication', get_string('authentication','admin'), "$CFG->wwwroot/$CFG->admin/auth.php"));
// "userpolicies" settingpage
$temp = new admin_settingpage('userpolicies', get_string('userpolicies', 'admin'));
-$context = get_context_instance(CONTEXT_SYSTEM);
-if (!$guestrole = get_guest_role()) {
- $guestrole->id = 0;
-}
-if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
- $studentrole = array_shift($studentroles); /// Take the first one
-} else {
- $studentrole->id = 0;
-}
-if ($userroles = get_roles_with_capability('moodle/legacy:user', CAP_ALLOW)) {
- $userrole = array_shift($userroles); /// Take the first one
-} else {
- $userrole->id = 0;
-}
-if (empty($CFG->creatornewroleid)) {
- if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
- $teachereditrole = array_shift($teacherroles);
- set_config('creatornewroleid', $teachereditrole->id);
+if (!empty($CFG->rolesactive)) {
+ $context = get_context_instance(CONTEXT_SYSTEM);
+ if (!$guestrole = get_guest_role()) {
+ $guestrole->id = 0;
+ }
+ if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
+ $studentrole = array_shift($studentroles); /// Take the first one
} else {
- set_config('creatornewroleid', 0);
+ $studentrole->id = 0;
}
-}
-// we must not use assignable roles here:
-// 1/ unsetting roles as assignable for admin might bork the settings!
-// 2/ default user role should not be assignable anyway
-$allroles = array();
-if ($roles = get_all_roles()) {
- foreach ($roles as $role) {
- $allroles[$role->id] = strip_tags(format_string($role->name, true));
+ if ($userroles = get_roles_with_capability('moodle/legacy:user', CAP_ALLOW)) {
+ $userrole = array_shift($userroles); /// Take the first one
+ } else {
+ $userrole->id = 0;
+ }
+ if (empty($CFG->creatornewroleid)) {
+ if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
+ $teachereditrole = array_shift($teacherroles);
+ set_config('creatornewroleid', $teachereditrole->id);
+ } else {
+ set_config('creatornewroleid', 0);
+ }
+ }
+ // we must not use assignable roles here:
+ // 1/ unsetting roles as assignable for admin might bork the settings!
+ // 2/ default user role should not be assignable anyway
+ $allroles = array();
+ if ($roles = get_all_roles()) {
+ foreach ($roles as $role) {
+ $allroles[$role->id] = strip_tags(format_string($role->name, true));
+ }
}
-}
-$temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'),
- get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $allroles ));
-$temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'),
- get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
-$temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
- get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));
+ $temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'),
+ get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $allroles ));
+ $temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'),
+ get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
+ $temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
+ get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));
+}
$temp->add(new admin_setting_configcheckbox('nodefaultuserrolelists', get_string('nodefaultuserrolelists', 'admin'), get_string('confignodefaultuserrolelists', 'admin'), 0));
-$temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
- get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
-$temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
- get_string('configcreatornewroleid', 'admin'), $CFG->creatornewroleid, $allroles));
+if (!empty($CFG->rolesactive)) {
+ $temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
+ get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
+ $temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
+ get_string('configcreatornewroleid', 'admin'), $CFG->creatornewroleid, $allroles));
+}
$temp->add(new admin_setting_configcheckbox('autologinguests', get_string('autologinguests', 'admin'), get_string('configautologinguests', 'admin'), 0));
-$temp->add(new admin_setting_configmultiselect('nonmetacoursesyncroleids', get_string('nonmetacoursesyncroleids', 'admin'),
+if (!empty($CFG->rolesactive)) {
+ $temp->add(new admin_setting_configmultiselect('nonmetacoursesyncroleids', get_string('nonmetacoursesyncroleids', 'admin'),
get_string('confignonmetacoursesyncroleids', 'admin'), array(), $allroles));
+}
//$temp->add(new admin_setting_configcheckbox('allusersaresitestudents', get_string('allusersaresitestudents', 'admin'), get_string('configallusersaresitestudents','admin'), 1));
$temp->add(new admin_setting_configmultiselect('hiddenuserfields', get_string('hiddenuserfields', 'admin'),
$ADMIN->add('roles', $temp);
+} // end of admin tree capability if
?>
}
-/**
- * Get the id for the not-logged-in role - or set it up if needed
- * @return bool
- */
-function get_notloggedin_roleid($return=false) {
- global $CFG, $USER;
-
- if (empty($CFG->notloggedinroleid)) { // Let's set the default to the guest role
- if ($role = get_guest_role()) {
- set_config('notloggedinroleid', $role->id);
- return $role->id;
- } else {
- return false;
- }
- } else {
- return $CFG->notloggedinroleid;
- }
-
- return (get_record('role','id', $CFG->notloggedinas));
-}
-
/**
* Get the default guest role
* @return object role
}
}
-function has_capability($capability, $context=NULL, $userid=NULL, $doanything=true) {
- global $USER, $CONTEXT, $ACCESS, $CFG, $DIRTYCONTEXTS;
+function has_capability($capability, $context, $userid=NULL, $doanything=true) {
+ global $USER, $ACCESS, $CFG, $DIRTYCONTEXTS;
- /// Make sure we know the current context
- if (empty($context)) { // Use default CONTEXT if none specified
- if (empty($CONTEXT)) {
- return false;
- } else {
- $context = $CONTEXT;
- }
- }
- if (empty($CONTEXT)) {
- $CONTEXT = $context;
+ // the original $CONTEXT here was hiding serious errors
+ // for security reasons do notreuse previous context
+ if (empty($context)) {
+ debugging('Incorrect context specified');
+ return false;
}
if (is_null($userid) || $userid===0) {
}
if ($USER->id === 0 && !isset($USER->access)) {
+ // not-logged-in user first time here
load_all_capabilities();
- }
- if (defined('FULLME') && FULLME === 'cron' && !isset($USER->access)) {
+ } else if (defined('FULLME') && FULLME === 'cron' && !isset($USER->access)) {
//
// In cron, some modules setup a 'fake' $USER,
// ensure we load the appropriate accessdata.
}
$USER->access = $ACCESS[$userid];
$DIRTYCONTEXTS = array();
+
+ } else if ($USER->id === $userid && !isset($USER->access)) {
+ // caps not loaded yet - better to load them to keep BC with 1.8
+ // probably $USER object set up manually
+ load_all_capabilities();
}
// Careful check for staleness...
//
$raparents = array();
$lastseen = '';
- if ($rs->RecordCount()) {
+ if ($rs and $rs->RecordCount()) {
while ($ra = rs_fetch_next_record($rs)) {
// RAs leafs are arrays to support multi
// role assignments...
//
// provide "default role" & set 'dr'
//
- $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
- if (!isset($accessdata['ra'][$base])) {
- $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
- } else {
- array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+ if (!empty($CFG->defaultuserroleid)) {
+ $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
+ if (!isset($accessdata['ra'][$base])) {
+ $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
+ } else {
+ array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+ }
+ $accessdata['dr'] = $CFG->defaultuserroleid;
}
- $accessdata['dr'] = $CFG->defaultuserroleid;
//
// provide "default frontpage role"
//
// provide "default role" & set 'dr'
//
- $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
- if (!isset($accessdata['ra'][$base])) {
- $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
- } else {
- array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+ if (!empty($CFG->defaultuserroleid)) {
+ $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
+ if (!isset($accessdata['ra'][$base])) {
+ $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
+ } else {
+ array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+ }
+ $accessdata['dr'] = $CFG->defaultuserroleid;
}
- $accessdata['dr'] = $CFG->defaultuserroleid;
$frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
}
$USER->access = $accessdata;
- } else {
- if ($roleid = get_notloggedin_roleid()) {
- $USER->access = get_role_access($roleid);
- $USER->access['ra'][$base] = array($roleid);
- }
+ } else if (!empty($CFG->notloggedinroleid)) {
+ $USER->access = get_role_access($CFG->notloggedinroleid);
+ $USER->access['ra'][$base] = array($CFG->notloggedinroleid);
}
// Timestamp to read
/// Delete the old user tables when we are done
- drop_table(new XMLDBTable('user_students'));
- drop_table(new XMLDBTable('user_teachers'));
- drop_table(new XMLDBTable('user_coursecreators'));
- drop_table(new XMLDBTable('user_admins'));
-
+ $tables = array('user_students', 'user_teachers', 'user_coursecreators', 'user_admins');
+ foreach ($tables as $tablename) {
+ $table = new XMLDBTable($tablename);
+ if (table_exists($table)) {
+ drop_table($table);
+ }
+ }
}
/**
$basepath = '/' . SYSCONTEXTID;
$basedepth = 1;
+ $result = true;
+
switch ($contextlevel) {
case CONTEXT_COURSECAT:
$sql = "SELECT ctx.path, ctx.depth
if ($p = get_record_sql($sql)) {
$basepath = $p->path;
$basedepth = $p->depth;
+ } else if ($category = get_record('course_categories', 'id', $instanceid)) {
+ if (empty($category->parent)) {
+ // ok - this is a top category
+ } else if ($parent = get_context_instance(CONTEXT_COURSECAT, $category->parent)) {
+ $basepath = $parent->path;
+ $basedepth = $parent->depth;
+ } else {
+ // wrong parent category - no big deal, this can be fixed later
+ $basepath = null;
+ $basedepth = 0;
+ }
+ } else {
+ // incorrect category id
+ $result = false;
}
break;
if ($p = get_record_sql($sql)) {
$basepath = $p->path;
$basedepth = $p->depth;
+ } else if ($course = get_record('course', 'id', $instanceid)) {
+ if ($course->id == SITEID) {
+ //ok - no parent category
+ } else if ($parent = get_context_instance(CONTEXT_COURSECAT, $course->category)) {
+ $basepath = $parent->path;
+ $basedepth = $parent->depth;
+ } else {
+ // wrong parent category of course - no big deal, this can be fixed later
+ $basepath = null;
+ $basedepth = 0;
+ }
+ } else if ($instanceid == SITEID) {
+ // no errors for missing site course during installation
+ return false;
+ } else {
+ // incorrect course id
+ $result = false;
}
break;
JOIN {$CFG->prefix}course_modules cm
ON (cm.course=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
WHERE cm.id={$instanceid}";
- $p = get_record_sql($sql);
- $basepath = $p->path;
- $basedepth = $p->depth;
+ if ($p = get_record_sql($sql)) {
+ $basepath = $p->path;
+ $basedepth = $p->depth;
+ } else if ($cm = get_record('course_modules', 'id', $instanceid)) {
+ if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) {
+ $basepath = $parent->path;
+ $basedepth = $parent->depth;
+ } else {
+ // course does not exist - modules can not exist without a course
+ $result = false;
+ }
+ } else {
+ // cm does not exist
+ $result = false;
+ }
break;
case CONTEXT_BLOCK:
if ($p = get_record_sql($sql)) {
$basepath = $p->path;
$basedepth = $p->depth;
+ } else if ($bi = get_record('block_instance', 'id', $instanceid)) {
+ if ($bi->pagetype != 'course-view') {
+ // ok - not a course block
+ } else if ($parent = get_context_instance(CONTEXT_COURSE, $bi->pageid)) {
+ $basepath = $parent->path;
+ $basedepth = $parent->depth;
+ } else {
+ // parent course does not exist - course blocks can not exist without a course
+ $result = false;
+ }
+ } else {
+ // block does not exist
+ $result = false;
}
break;
case CONTEXT_USER:
break;
}
- $context->depth = $basedepth+1;
+ // if grandparents unknown, maybe rebuild_context_path() will solve it later
+ if ($basedepth != 0) {
+ $context->depth = $basedepth+1;
+ }
+
+ if ($result and $id = insert_record('context', $context)) {
+ // can't set the full path till we know the id!
+ if ($basedepth != 0 and !empty($basepath)) {
+ set_field('context', 'path', $basepath.'/'. $id, 'id', $id);
+ }
+ return get_context_instance_by_id($id);
- if ($id = insert_record('context',$context)) {
- // can't set the path till we know the id!
- set_field('context', 'path', $basepath . '/' . $id,
- 'id', $id);
- $c = get_context_instance_by_id($id);
- return $c;
} else {
debugging('Error: could not insert new context level "'.
s($contextlevel).'", instance "'.
s($instanceid).'".');
- return NULL;
+ return false;
}
}
/**
* This hacky function is needed because we can not change system context instanceid using normal upgrade routine.
*/
-function create_system_context() {
- if ($context = get_record('context', 'contextlevel', CONTEXT_SYSTEM, 'instanceid', SITEID)) {
- // we are going to change instanceid of system context to 0 now
- $context->instanceid = 0;
- update_record('context', $context);
- //context rel not affected
- return $context;
+function get_system_context($cache=true) {
+ static $cached = null;
+ if ($cache and defined('SYSCONTEXTID')) {
+ if (is_null($cached)) {
+ $cached = new object();
+ $cached->id = SYSCONTEXTID;
+ $cached->contextlevel = CONTEXT_SYSTEM;
+ $cached->instanceid = 0;
+ $cached->path = '/'.SYSCONTEXTID;
+ $cached->depth = 1;
+ }
+ return $cached;
+ }
- } else {
+ if (!$context = get_record('context', 'contextlevel', CONTEXT_SYSTEM)) {
$context = new object();
$context->contextlevel = CONTEXT_SYSTEM;
- $context->instanceid = 0;
- if ($context->id = insert_record('context',$context)) {
- return $context;
- } else {
+ $context->instanceid = 0;
+ $context->depth = 1;
+ $context->path = NULL; //not known before insert
+
+ if (!$context->id = insert_record('context', $context)) {
+ // better something than nothing - let's hope it will work somehow
+ if (!defined('SYSCONTEXTID')) {
+ define('SYSCONTEXTID', 1);
+ }
debugging('Can not create system context');
- return NULL;
+ $context->id = SYSCONTEXTID;
+ $context->path = '/'.SYSCONTEXTID;
+ return $context;
}
}
+
+ if (!isset($context->depth) or $context->depth != 1 or $context->instanceid != 0 or $context->path != '/'.$context->id) {
+ $context->instanceid = 0;
+ $context->path = '/'.$context->id;
+ $context->depth = 1;
+ update_record('context', $context);
+ }
+
+ if (!defined('SYSCONTEXTID')) {
+ define('SYSCONTEXTID', $context->id);
+ }
+
+ $cached = $context;
+ return $cached;
}
/**
* Remove a context record and any dependent entries
* for $level = CONTEXT_MODULE, this would be $cm->id. And so on.
* @return object The context object.
*/
-function get_context_instance($contextlevel=NULL, $instance=0) {
+function get_context_instance($contextlevel, $instance=0) {
- global $context_cache, $context_cache_id, $CONTEXT;
+ global $context_cache, $context_cache_id;
static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK);
if ($contextlevel === 'clearcache') {
return false;
}
-/// If no level is supplied then return the current global context if there is one
- if (empty($contextlevel)) {
- if (empty($CONTEXT)) {
- //fatal error, code must be fixed
- error("Error: get_context_instance() called without a context");
- } else {
- return $CONTEXT;
- }
- }
-
-/// Backwards compatibility with obsoleted (CONTEXT_SYSTEM, SITEID)
+/// System context has special cache
if ($contextlevel == CONTEXT_SYSTEM) {
- $instance = 0;
+ return get_system_context();
}
/// check allowed context levels
/// Get it from the database, or create it
if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) {
- create_context($contextlevel, $instance);
- $context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance);
+ $context = create_context($contextlevel, $instance);
}
/// Only add to cache if context isn't empty.
global $context_cache, $context_cache_id;
+ if ($id == SYSCONTEXTID) {
+ return get_system_context();
+ }
+
if (isset($context_cache_id[$id])) { // Already cached
return $context_cache_id[$id];
}
'instanceid', SITEID);
}
- $ctxemptyclause = " AND (ctx.depth IS NULL
+ $ctxemptyclause = " AND (ctx.path IS NULL
OR ctx.depth=0) ";
- $emptyclause = " AND ({$CFG->prefix}context.depth IS NULL
+ $emptyclause = " AND ({$CFG->prefix}context.path IS NULL
OR {$CFG->prefix}context.depth=0) ";
if ($force) {
$ctxemptyclause = $emptyclause = '';
// Personal TODO
//TODO: fix group contexts
+
+ // reset static course cache - it might have incorrect cached data
+ global $context_cache, $context_cache_id;
+ $context_cache = array();
+ $context_cache_id = array();
+
}
/**
$result = $result && add_key($table, $key);
}
- if ($result && $oldversion < 2007080101) {
- if ($firstadmin = get_admin()) { // the person currently used for support emails
- set_config('supportname', s(fullname($firstadmin))); // New settings same as old
- set_config('supportemail', s($firstadmin->email));
- }
- }
+ /// originally there was supportname and supportemail upgrade code - this is handled in upgradesettings.php instead
/// MDL-10679, context_rel clean up
if ($result && $oldversion < 2007080200) {
/// Add context.path & index
$table = new XMLDBTable('context');
$field = new XMLDBField('path');
- $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'instanceid');
+ $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
$result = $result && add_field($table, $field);
$table = new XMLDBTable('context');
$index = new XMLDBIndex('path');
$field = new XMLDBField('depth');
$field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'path');
$result = $result && add_field($table, $field);
+
+ /// make sure the system context has proper data
+ get_system_context(false);
}
if ($result && $oldversion < 2007080903) {
$result = $result && create_table($table);
/// Recalculate depths, paths and so on
- cleanup_contexts();
- build_context_path(true);
- load_all_capabilities();
+ if (!empty($CFG->rolesactive)) {
+ cleanup_contexts();
+ build_context_path(true);
+ load_all_capabilities();
+ }
}
/**
$result = $result && change_field_notnull($table, $field);
}
+ if ($result && $oldversion < 2007100500) {
+ /// for dev sites - it is ok to do this repeatedly
+
+ /// Changing nullability of field path on table context to null
+ $table = new XMLDBTable('context');
+ $field = new XMLDBField('path');
+ $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
+
+ /// Launch change of nullability for field path
+ $result = $result && change_field_notnull($table, $field);
+ }
+
/* NOTE: please keep this at the end of upgrade file for now ;-)
/// drop old gradebook tables