Skip to content

Commit

Permalink
Merge branch 'w36_MDL-20045_m26_customlevels' of https://github.com/s…
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Sep 3, 2013
2 parents e034d84 + 0aa535b commit 8a1b820
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
13 changes: 5 additions & 8 deletions admin/roles/classes/define_role_table_advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ public function __construct($context, $roleid) {
$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() {
Expand Down
40 changes: 34 additions & 6 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -5786,6 +5808,7 @@ protected function __construct() {
* @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 {
Expand All @@ -5800,6 +5823,7 @@ public static function get_class_for_level($contextlevel) {
* @return array int=>string (level=>level class name)
*/
public static function get_all_levels() {
self::init_levels();
return self::$alllevels;
}

Expand All @@ -5812,6 +5836,8 @@ public static function get_all_levels() {
*/
public static function cleanup_instances() {
global $DB;
self::init_levels();

$sqls = array();
foreach (self::$alllevels as $level=>$classname) {
$sqls[] = $classname::get_cleanup_sql();
Expand Down Expand Up @@ -5841,6 +5867,7 @@ public static function cleanup_instances() {
* @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
Expand All @@ -5861,6 +5888,7 @@ public static function create_instances($contextlevel = null, $buildpaths = true
* @return void
*/
public static function build_all_paths($force = false) {
self::init_levels();
foreach (self::$alllevels as $classname) {
$classname::build_paths($force);
}
Expand Down

0 comments on commit 8a1b820

Please sign in to comment.