MDL-20045 add support for custom context levels

This commit is contained in:
Petr Škoda 2013-08-24 21:09:36 +02:00
parent ee788142f2
commit 0aa535bf1d
2 changed files with 39 additions and 14 deletions

View File

@ -49,14 +49,11 @@ class core_role_define_role_table_advanced extends core_role_capability_table_wi
$this->displaypermissions = $this->allpermissions;
$this->strperms[$this->allpermissions[CAP_INHERIT]] = get_string('notset', 'core_role');
$this->allcontextlevels = array(
CONTEXT_SYSTEM => get_string('coresystem'),
CONTEXT_USER => get_string('user'),
CONTEXT_COURSECAT => get_string('category'),
CONTEXT_COURSE => get_string('course'),
CONTEXT_MODULE => get_string('activitymodule'),
CONTEXT_BLOCK => get_string('block')
);
$this->allcontextlevels = array();
$levels = context_helper::get_all_levels();
foreach ($levels as $level => $classname) {
$this->allcontextlevels[$level] = context_helper::get_level_name($level);
}
}
protected function load_current_permissions() {

View File

@ -5763,19 +5763,41 @@ class context_helper extends context {
/**
* @var array An array mapping context levels to classes
*/
private static $alllevels = array(
private static $alllevels;
/**
* Instance does not make sense here, only static use
*/
protected function __construct() {
}
/**
* Initialise context levels, call before using self::$alllevels.
*/
private static function init_levels() {
global $CFG;
if (isset(self::$alllevels)) {
return;
}
self::$alllevels = array(
CONTEXT_SYSTEM => 'context_system',
CONTEXT_USER => 'context_user',
CONTEXT_COURSECAT => 'context_coursecat',
CONTEXT_COURSE => 'context_course',
CONTEXT_MODULE => 'context_module',
CONTEXT_BLOCK => 'context_block',
);
);
/**
* Instance does not make sense here, only static use
*/
protected function __construct() {
if (empty($CFG->custom_context_classes)) {
return;
}
// Unsupported custom levels, use with care!!!
foreach ($CFG->custom_context_classes as $level => $classname) {
self::$alllevels[$level] = $classname;
}
ksort(self::$alllevels);
}
/**
@ -5786,6 +5808,7 @@ class context_helper extends context {
* @return string class name of the context class
*/
public static function get_class_for_level($contextlevel) {
self::init_levels();
if (isset(self::$alllevels[$contextlevel])) {
return self::$alllevels[$contextlevel];
} else {
@ -5800,6 +5823,7 @@ class context_helper extends context {
* @return array int=>string (level=>level class name)
*/
public static function get_all_levels() {
self::init_levels();
return self::$alllevels;
}
@ -5812,6 +5836,8 @@ class context_helper extends context {
*/
public static function cleanup_instances() {
global $DB;
self::init_levels();
$sqls = array();
foreach (self::$alllevels as $level=>$classname) {
$sqls[] = $classname::get_cleanup_sql();
@ -5841,6 +5867,7 @@ class context_helper extends context {
* @return void
*/
public static function create_instances($contextlevel = null, $buildpaths = true) {
self::init_levels();
foreach (self::$alllevels as $level=>$classname) {
if ($contextlevel and $level > $contextlevel) {
// skip potential sub-contexts
@ -5861,6 +5888,7 @@ class context_helper extends context {
* @return void
*/
public static function build_all_paths($force = false) {
self::init_levels();
foreach (self::$alllevels as $classname) {
$classname::build_paths($force);
}