From 4e1fe7d1095382fa113914365052eb6c24668c07 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Wed, 26 Sep 2007 07:12:38 +0000 Subject: [PATCH] MDL-11143, adding a new setting "defaultfrontpageroleid" where you choose a role for all users on the front page --- admin/settings/frontpage.php | 11 ++++++ lib/accesslib.php | 71 +++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/admin/settings/frontpage.php b/admin/settings/frontpage.php index 1393208acf..6522632f7d 100644 --- a/admin/settings/frontpage.php +++ b/admin/settings/frontpage.php @@ -40,6 +40,17 @@ $ADMIN->add('frontpage', new admin_externalpage('frontpagebackup', get_string('f $ADMIN->add('frontpage', new admin_externalpage('frontpagerestore', get_string('frontpagerestore', 'admin'), $CFG->wwwroot.'/files/index.php?id='.SITEID.'&wdir=/backupdata', 'moodle/site:restore', false, $frontpagecontext)); +// front page default role +$temp = new admin_settingpage('frontpagedefaultrole', get_string('frontpagedefaultrole', 'admin'), 'moodle/site:config', false, get_context_instance(CONTEXT_SYSTEM)); +$roleoptions = array(0=>'N/A'); // roles to choose from +if ($roles = get_records('role')) { + foreach ($roles as $role) { + $roleoptions[$role->id] = $role->name; + } +} +$temp->add(new admin_setting_configselect('defaultfrontpageroleid', get_string('frontpagedefaultrole', 'admin'), '', '', $roleoptions)); +$ADMIN->add('frontpage', $temp); + $ADMIN->add('frontpage', new admin_externalpage('sitefiles', get_string('sitefiles'), $CFG->wwwroot . '/files/index.php?id=' . SITEID, 'moodle/course:managefiles', false, $frontpagecontext)); ?> \ No newline at end of file diff --git a/lib/accesslib.php b/lib/accesslib.php index 1818e0d0f4..fe151c74c2 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -242,6 +242,46 @@ function get_role_access($roleid, $accessdata=NULL) { return $accessdata; } +/** + * Gets the accessdata for role "sitewide" + * (system down to course) + * + * @return array + */ +function get_default_frontpage_role_access($roleid, $accessdata=NULL) { + + global $CFG; + + $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); + $base = '/'. SYSCONTEXTID .'/'. $frontpagecontext->id; + + // + // Overrides for the role in any contexts related to the course + // + $sql = "SELECT ctx.path, + rc.capability, rc.permission + FROM {$CFG->prefix}context ctx + JOIN {$CFG->prefix}role_capabilities rc + ON rc.contextid=ctx.id + WHERE rc.roleid = {$roleid} + AND (ctx.id = ".SYSCONTEXTID." OR ctx.path LIKE '$base/%') + ORDER BY ctx.depth, ctx.path"; + + if ($rs = get_recordset_sql($sql)) { + if ($rs->RecordCount()) { + while ($rd = rs_fetch_next_record($rs)) { + $k = "{$rd->path}:{$roleid}"; + $accessdata['rdef'][$k][$rd->capability] = $rd->permission; + } + unset($rd); + } + rs_close($rs); + } + + return $accessdata; +} + + /** * Get the id for the not-logged-in role - or set it up if needed * @return bool @@ -1514,7 +1554,19 @@ function load_user_accessdata($userid) { } $accessdata['dr'] = $CFG->defaultuserroleid; - $ACCESS[$userid] = $accessdata; + // + // provide "default frontpage role" + // + if ($CFG->defaultfrontpageroleid) { + $base = '/'. SYSCONTEXTID .'/'. $frontpagecontext->id; + $accessdata = get_default_frontpage_role_access($CFG->defaultfrongpageroleid, $accessdata); + if (!isset($accessdata['ra'][$base])) { + $accessdata['ra'][$base] = array($CFG->defaultfrongpageroleid); + } else { + array_push($accessdata['ra'][$base], $CFG->defaultfrongpageroleid); + } + } + $ACCESS[$userid] = $accessdata; return true; } @@ -1553,8 +1605,23 @@ function load_all_capabilities() { } $accessdata['dr'] = $CFG->defaultuserroleid; - $USER->access = $accessdata; + $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); + // + // provide "default frontpage role" + // + + if ($CFG->defaultfrontpageroleid) { + $base = '/'. SYSCONTEXTID .'/'. $frontpagecontext->id; + $accessdata = get_default_frontpage_role_access($CFG->defaultfrontpageroleid, $accessdata); + if (!isset($accessdata['ra'][$base])) { + $accessdata['ra'][$base] = array($CFG->defaultfrontpageroleid); + } else { + array_push($accessdata['ra'][$base], $CFG->defaultfrontpageroleid); + } + } + $USER->access = $accessdata; + } else { if ($roleid = get_notloggedin_roleid()) { $USER->access = get_role_access($roleid); -- 2.39.5