本文整理汇总了PHP中context_helper::get_all_levels方法的典型用法代码示例。如果您正苦于以下问题:PHP context_helper::get_all_levels方法的具体用法?PHP context_helper::get_all_levels怎么用?PHP context_helper::get_all_levels使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类context_helper
的用法示例。
在下文中一共展示了context_helper::get_all_levels方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: costcenter_custom_contexts_setup
/**
* Test case for custom context classes
*/
public function costcenter_custom_contexts_setup() {
global $CFG;
static $customcontexts = array(
27 => 'context_costcenter'
);
// save any existing custom contexts
$existingcustomcontexts = get_config(null, 'custom_context_classes');
set_config('custom_context_classes', serialize($customcontexts));
initialise_cfg();
context_helper::reset_levels();
$alllevels = context_helper::get_all_levels();
/// $this->assertEquals($alllevels[11], 'context_costcenter');
// clean-up & restore any custom contexts
//set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
// initialise_cfg();
// context_helper::reset_levels();
// $alllevels = context_helper::get_all_levels();
// print_object($alllevels);
}
示例2: __construct
public function __construct($context, $roleid)
{
$this->roleid = $roleid;
parent::__construct($context, 'defineroletable', $roleid);
$this->displaypermissions = $this->allpermissions;
$this->strperms[$this->allpermissions[CAP_INHERIT]] = get_string('notset', 'core_role');
$this->allcontextlevels = array();
$levels = context_helper::get_all_levels();
foreach ($levels as $level => $classname) {
$this->allcontextlevels[$level] = context_helper::get_level_name($level);
}
}
示例3: get_context
/**
* Gets the internal context id from the context reference.
*
* The context reference changes depending on the context
* level, it can be the system, a user, a category, a course or
* a module.
*
* @throws Exception
* @param string $levelname The context level string introduced by the test writer
* @param string $contextref The context reference introduced by the test writer
* @return context
*/
protected function get_context($levelname, $contextref) {
global $DB;
// Getting context levels and names (we will be using the English ones as it is the test site language).
$contextlevels = context_helper::get_all_levels();
$contextnames = array();
foreach ($contextlevels as $level => $classname) {
$contextnames[context_helper::get_level_name($level)] = $level;
}
if (empty($contextnames[$levelname])) {
throw new Exception('The specified "' . $levelname . '" context level does not exist');
}
$contextlevel = $contextnames[$levelname];
// Return it, we don't need to look for other internal ids.
if ($contextlevel == CONTEXT_SYSTEM) {
return context_system::instance();
}
switch ($contextlevel) {
case CONTEXT_USER:
$instanceid = $DB->get_field('user', 'id', array('username' => $contextref));
break;
case CONTEXT_COURSECAT:
$instanceid = $DB->get_field('course_categories', 'id', array('idnumber' => $contextref));
break;
case CONTEXT_COURSE:
$instanceid = $DB->get_field('course', 'id', array('shortname' => $contextref));
break;
case CONTEXT_MODULE:
$instanceid = $DB->get_field('course_modules', 'id', array('idnumber' => $contextref));
break;
default:
break;
}
$contextclass = $contextlevels[$contextlevel];
if (!$context = $contextclass::instance($instanceid, IGNORE_MISSING)) {
throw new Exception('The specified "' . $contextref . '" context reference does not exist');
}
return $context;
}
示例4: get_level_from_class_name
/**
* Return the context level associated with the context class name
* @param $classname the class name
* @throws coding_exception
* @return int context level for given class name
*/
public static function get_level_from_class_name($classname)
{
if (empty(self::$puballlevels)) {
self::$puballlevels = parent::get_all_levels();
}
// Function get_called_class() does not prefix the result with a '\', as in the internal classnames.
if ($classname[0] !== '\\') {
$classname = '\\' . $classname;
}
$contextlevel = array_search($classname, self::$puballlevels);
if (false !== $contextlevel) {
return $contextlevel;
}
throw new \coding_exception('Context class name [' . $classname . '] not found in defined custom contexts.');
}
示例5: parse_preset
/**
* Parse role preset xml file.
*
* @param string $xml
* @return array role info, null on error
*/
public static function parse_preset($xml)
{
global $DB;
$info = array();
if (!self::is_valid_preset($xml)) {
return null;
}
$dom = new DOMDocument();
$dom->loadXML($xml);
$info['shortname'] = self::get_node_value($dom, '/role/shortname');
if (isset($info['shortname'])) {
$info['shortname'] = strtolower(clean_param($info['shortname'], PARAM_ALPHANUMEXT));
}
$info['name'] = self::get_node_value($dom, '/role/name');
if (isset($value)) {
$info['name'] = clean_param($info['name'], PARAM_TEXT);
}
$info['description'] = self::get_node_value($dom, '/role/description');
if (isset($value)) {
$info['description'] = clean_param($info['description'], PARAM_CLEANHTML);
}
$info['archetype'] = self::get_node_value($dom, '/role/archetype');
if (isset($value)) {
$archetypes = get_role_archetypes();
if (!isset($archetypes[$info['archetype']])) {
$info['archetype'] = null;
}
}
$values = self::get_node_children_values($dom, '/role/contextlevels', 'level');
if (isset($values)) {
$info['contextlevels'] = array();
$levelmap = array_flip(context_helper::get_all_levels());
foreach ($values as $value) {
$level = 'context_' . $value;
if (isset($levelmap[$level])) {
$cl = $levelmap[$level];
$info['contextlevels'][$cl] = $cl;
}
}
}
foreach (array('assign', 'override', 'switch') as $type) {
$values = self::get_node_children_values($dom, '/role/allow' . $type, 'shortname');
if (!isset($values)) {
$info['allow' . $type] = null;
continue;
}
$info['allow' . $type] = array();
foreach ($values as $value) {
if ($value === $info['shortname']) {
array_unshift($info['allow' . $type], -1);
// Means self.
}
if ($role = $DB->get_record('role', array('shortname' => $value))) {
$info['allow' . $type][] = $role->id;
continue;
}
}
}
$info['permissions'] = array();
$values = self::get_node_children_values($dom, '/role/permissions', 'inherit');
if (isset($values)) {
foreach ($values as $value) {
if ($value = clean_param($value, PARAM_CAPABILITY)) {
$info['permissions'][$value] = CAP_INHERIT;
}
}
}
$values = self::get_node_children_values($dom, '/role/permissions', 'allow');
if (isset($values)) {
foreach ($values as $value) {
if ($value = clean_param($value, PARAM_CAPABILITY)) {
$info['permissions'][$value] = CAP_ALLOW;
}
}
}
$values = self::get_node_children_values($dom, '/role/permissions', 'prevent');
if (isset($values)) {
foreach ($values as $value) {
if ($value = clean_param($value, PARAM_CAPABILITY)) {
$info['permissions'][$value] = CAP_PREVENT;
}
}
}
$values = self::get_node_children_values($dom, '/role/permissions', 'prohibit');
if (isset($values)) {
foreach ($values as $value) {
if ($value = clean_param($value, PARAM_CAPABILITY)) {
$info['permissions'][$value] = CAP_PROHIBIT;
}
}
}
return $info;
}
示例6: test_everything_in_accesslib
//.........这里部分代码省略.........
}
// Add manual enrol instance
$manualenrol->add_default_instance($DB->get_record('course', array('id'=>$course->id)));
// Add block to each course
$bi = $generator->create_block('online_users', array('parentcontextid'=>$coursecontext->id));
$testblocks[] = $bi->id;
// Add a resource to each course
$page = $generator->create_module('page', array('course'=>$course->id));
$testpages[] = $page->id;
$modcontext = context_module::instance($page->cmid);
// Add block to each module
$bi = $generator->create_block('online_users', array('parentcontextid'=>$modcontext->id));
$testblocks[] = $bi->id;
}
}
// Make sure all contexts were created properly
$count = 1; //system
$count += $DB->count_records('user', array('deleted'=>0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEquals($DB->count_records('context'), $count);
$this->assertEquals($DB->count_records('context', array('depth'=>0)), 0);
$this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0);
// ====== context_helper::get_level_name() ================================
$levels = context_helper::get_all_levels();
foreach ($levels as $level=>$classname) {
$name = context_helper::get_level_name($level);
$this->assertFalse(empty($name));
}
// ======= context::instance_by_id(), context_xxx::instance();
$context = context::instance_by_id($frontpagecontext->id);
$this->assertSame($context->contextlevel, CONTEXT_COURSE);
$this->assertFalse(context::instance_by_id(-1, IGNORE_MISSING));
try {
context::instance_by_id(-1);
$this->fail('exception expected');
} catch (Exception $e) {
$this->assertTrue(true);
}
$this->assertTrue(context_system::instance() instanceof context_system);
$this->assertTrue(context_coursecat::instance($testcategories[0]) instanceof context_coursecat);
$this->assertTrue(context_course::instance($testcourses[0]) instanceof context_course);
$this->assertTrue(context_module::instance($testpages[0]) instanceof context_module);
$this->assertTrue(context_block::instance($testblocks[0]) instanceof context_block);
$this->assertFalse(context_coursecat::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_course::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_module::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_block::instance(-1, IGNORE_MISSING));
try {
context_coursecat::instance(-1);
$this->fail('exception expected');
} catch (Exception $e) {
$this->assertTrue(true);
示例7: create_role
/**
* Creates a new role in the system.
*
* You can fill $record with the role 'name',
* 'shortname', 'description' and 'archetype'.
*
* If an archetype is specified it's capabilities,
* context where the role can be assigned and
* all other properties are copied from the archetype;
* if no archetype is specified it will create an
* empty role.
*
* @param array|stdClass $record
* @return int The new role id
*/
public function create_role($record=null) {
global $DB;
$this->rolecount++;
$i = $this->rolecount;
$record = (array)$record;
if (empty($record['shortname'])) {
$record['shortname'] = 'role-' . $i;
}
if (empty($record['name'])) {
$record['name'] = 'Test role ' . $i;
}
if (empty($record['description'])) {
$record['description'] = 'Test role ' . $i . ' description';
}
if (empty($record['archetype'])) {
$record['archetype'] = '';
} else {
$archetypes = get_role_archetypes();
if (empty($archetypes[$record['archetype']])) {
throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' .
'valid archetype shortname (editingteacher, student...)');
}
}
// Creates the role.
if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) {
throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role');
}
// If no archetype was specified we allow it to be added to all contexts,
// otherwise we allow it in the archetype contexts.
if (!$record['archetype']) {
$contextlevels = array_keys(context_helper::get_all_levels());
} else {
// Copying from the archetype default rol.
$archetyperoleid = $DB->get_field(
'role',
'id',
array('shortname' => $record['archetype'], 'archetype' => $record['archetype'])
);
$contextlevels = get_role_contextlevels($archetyperoleid);
}
set_role_contextlevels($newroleid, $contextlevels);
if ($record['archetype']) {
// We copy all the roles the archetype can assign, override and switch to.
if ($record['archetype']) {
$types = array('assign', 'override', 'switch');
foreach ($types as $type) {
$rolestocopy = get_default_role_archetype_allows($type, $record['archetype']);
foreach ($rolestocopy as $tocopy) {
$functionname = 'allow_' . $type;
$functionname($newroleid, $tocopy);
}
}
}
// Copying the archetype capabilities.
$sourcerole = $DB->get_record('role', array('id' => $archetyperoleid));
role_cap_duplicate($sourcerole, $newroleid);
}
return $newroleid;
}
示例8: get_context_from_params
/**
* Get context from passed parameters.
* The passed array must either contain a contextid or a combination of context level and instance id to fetch the context.
* For example, the context level can be "course" and instanceid can be courseid.
*
* See context_helper::get_all_levels() for a list of valid context levels.
*
* @param array $param
* @since Moodle 2.6
* @throws invalid_parameter_exception
* @return context
*/
protected static function get_context_from_params($param)
{
$levels = context_helper::get_all_levels();
if (!empty($param['contextid'])) {
return context::instance_by_id($param['contextid'], IGNORE_MISSING);
} else {
if (!empty($param['contextlevel']) && isset($param['instanceid'])) {
$contextlevel = "context_" . $param['contextlevel'];
if (!array_search($contextlevel, $levels)) {
throw new invalid_parameter_exception('Invalid context level = ' . $param['contextlevel']);
}
return $contextlevel::instance($param['instanceid'], IGNORE_MISSING);
} else {
// No valid context info was found.
throw new invalid_parameter_exception('Missing parameters, please provide either context level with instance id or contextid');
}
}
}
示例9: test_everything_in_accesslib
//.........这里部分代码省略.........
$manualenrol->add_default_instance($DB->get_record('course', array('id' => $courseid)));
// Add block to each course
$bi->parentcontextid = $coursecontext->id;
$biid = $DB->insert_record('block_instances', $bi);
context_block::instance($biid);
$testblocks[] = $biid;
// Add a resource to each course
$page->course = $courseid;
$pageid = $DB->insert_record('page', $page);
$testpages[] = $pageid;
$cm->course = $courseid;
$cm->instance = $pageid;
$cm->id = $DB->insert_record('course_modules', $cm);
$modcontext = context_module::instance($cm->id);
// Add block to each module
$bi->parentcontextid = $modcontext->id;
$biid = $DB->insert_record('block_instances', $bi);
context_block::instance($biid);
$testblocks[] = $biid;
}
}
// Make sure all contexts were created properly
$count = 1;
//system
$count += $DB->count_records('user', array('deleted' => 0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEqual($DB->count_records('context'), $count);
$this->assertEqual($DB->count_records('context', array('depth' => 0)), 0);
$this->assertEqual($DB->count_records('context', array('path' => NULL)), 0);
// ====== context_helper::get_level_name() ================================
$levels = context_helper::get_all_levels();
foreach ($levels as $level => $classname) {
$name = context_helper::get_level_name($level);
$this->assertFalse(empty($name));
}
// ======= context::instance_by_id(), context_xxx::instance();
$context = context::instance_by_id($frontpagecontext->id);
$this->assertidentical($context->contextlevel, CONTEXT_COURSE);
$this->assertFalse(context::instance_by_id(-1, IGNORE_MISSING));
try {
context::instance_by_id(-1);
$this->fail('exception expected');
} catch (Exception $e) {
$this->assertTrue(true);
}
$this->assertTrue(context_system::instance() instanceof context_system);
$this->assertTrue(context_coursecat::instance($testcategories[0]) instanceof context_coursecat);
$this->assertTrue(context_course::instance($testcourses[0]) instanceof context_course);
$this->assertTrue(context_module::instance($testpages[0]) instanceof context_module);
$this->assertTrue(context_block::instance($testblocks[0]) instanceof context_block);
$this->assertFalse(context_coursecat::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_course::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_module::instance(-1, IGNORE_MISSING));
$this->assertFalse(context_block::instance(-1, IGNORE_MISSING));
try {
context_coursecat::instance(-1);
$this->fail('exception expected');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
context_course::instance(-1);
$this->fail('exception expected');
示例10: costcenter_custom_contexts_setup
function costcenter_custom_contexts_setup() {
global $CFG;
static $customcontexts = array(
27 => 'context_costcenter'
);
// save any existing custom contexts
$existingcustomcontexts = get_config(null, 'custom_context_classes');
set_config('custom_context_classes', serialize($customcontexts));
initialise_cfg();
context_helper::reset_levels();
$alllevels = context_helper::get_all_levels();
}
示例11: test_customcontexts
/**
* Test case for custom context classes
*/
public function test_customcontexts()
{
global $CFG;
static $customcontexts = array(11 => 'context_bogus1', 12 => 'context_bogus2', 13 => 'context_bogus3');
// save any existing custom contexts
$existingcustomcontexts = get_config(null, 'custom_context_classes');
set_config('custom_context_classes', serialize($customcontexts));
initialise_cfg();
context_helper::reset_levels();
$alllevels = context_helper::get_all_levels();
$this->assertEquals($alllevels[11], 'context_bogus1');
$this->assertEquals($alllevels[12], 'context_bogus2');
$this->assertEquals($alllevels[13], 'context_bogus3');
// clean-up & restore any custom contexts
set_config('custom_context_classes', $existingcustomcontexts === false ? null : $existingcustomcontexts);
initialise_cfg();
context_helper::reset_levels();
}