本文整理汇总了PHP中get_child_contexts函数的典型用法代码示例。如果您正苦于以下问题:PHP get_child_contexts函数的具体用法?PHP get_child_contexts怎么用?PHP get_child_contexts使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_child_contexts函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: reset_course_userdata
/**
* This function will empty a course of user data.
* It will retain the activities and the structure of the course.
* @param object $data an object containing all the settings including courseid (without magic quotes)
* @return array status array of array component, item, error
*/
function reset_course_userdata($data)
{
global $CFG, $USER;
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/group/lib.php';
$data->courseid = $data->id;
$context = get_context_instance(CONTEXT_COURSE, $data->courseid);
// calculate the time shift of dates
if (!empty($data->reset_start_date)) {
// time part of course startdate should be zero
$data->timeshift = $data->reset_start_date - usergetmidnight($data->reset_start_date_old);
} else {
$data->timeshift = 0;
}
// result array: component, item, error
$status = array();
// start the resetting
$componentstr = get_string('general');
// move the course start time
if (!empty($data->reset_start_date) and $data->timeshift) {
// change course start data
set_field('course', 'startdate', $data->reset_start_date, 'id', $data->courseid);
// update all course and group events - do not move activity events
$updatesql = "UPDATE {$CFG->prefix}event\n SET timestart = timestart + ({$data->timeshift})\n WHERE courseid={$data->courseid} AND instance=0";
execute_sql($updatesql, false);
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
}
if (!empty($data->reset_logs)) {
delete_records('log', 'course', $data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('deletelogs'), 'error' => false);
}
if (!empty($data->reset_events)) {
delete_records('event', 'courseid', $data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('deleteevents', 'calendar'), 'error' => false);
}
if (!empty($data->reset_notes)) {
require_once $CFG->dirroot . '/notes/lib.php';
note_delete_all($data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('deletenotes', 'notes'), 'error' => false);
}
$componentstr = get_string('roles');
if (!empty($data->reset_roles_overrides)) {
$children = get_child_contexts($context);
foreach ($children as $child) {
delete_records('role_capabilities', 'contextid', $child->id);
}
delete_records('role_capabilities', 'contextid', $context->id);
//force refresh for logged in users
mark_context_dirty($context->path);
$status[] = array('component' => $componentstr, 'item' => get_string('deletecourseoverrides', 'role'), 'error' => false);
}
if (!empty($data->reset_roles_local)) {
$children = get_child_contexts($context);
foreach ($children as $child) {
role_unassign(0, 0, 0, $child->id);
}
//force refresh for logged in users
mark_context_dirty($context->path);
$status[] = array('component' => $componentstr, 'item' => get_string('deletelocalroles', 'role'), 'error' => false);
}
// First unenrol users - this cleans some of related user data too, such as forum subscriptions, tracking, etc.
$data->unenrolled = array();
if (!empty($data->reset_roles)) {
foreach ($data->reset_roles as $roleid) {
if ($users = get_role_users($roleid, $context, false, 'u.id', 'u.id ASC')) {
foreach ($users as $user) {
role_unassign($roleid, $user->id, 0, $context->id);
if (!has_capability('moodle/course:view', $context, $user->id)) {
$data->unenrolled[$user->id] = $user->id;
}
}
}
}
}
if (!empty($data->unenrolled)) {
$status[] = array('component' => $componentstr, 'item' => get_string('unenrol') . ' (' . count($data->unenrolled) . ')', 'error' => false);
}
$componentstr = get_string('groups');
// remove all group members
if (!empty($data->reset_groups_members)) {
groups_delete_group_members($data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('removegroupsmembers', 'group'), 'error' => false);
}
// remove all groups
if (!empty($data->reset_groups_remove)) {
groups_delete_groups($data->courseid, false);
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallgroups', 'group'), 'error' => false);
}
// remove all grouping members
if (!empty($data->reset_groupings_members)) {
groups_delete_groupings_groups($data->courseid, false);
$status[] = array('component' => $componentstr, 'item' => get_string('removegroupingsmembers', 'group'), 'error' => false);
}
// remove all groupings
//.........这里部分代码省略.........
示例2: test_everything_in_accesslib
//.........这里部分代码省略.........
$userid = reset($testusers);
$usercontext = context_user::instance($userid);
$this->assertSame($usercontext->get_parent_context(), $systemcontext);
$this->assertSame($usercontext->get_parent_contexts(), array($systemcontext->id=>$systemcontext));
$this->assertSame($usercontext->get_parent_contexts(true), array($usercontext->id=>$usercontext, $systemcontext->id=>$systemcontext));
$this->assertSame($systemcontext->get_parent_contexts(), array());
$this->assertSame($systemcontext->get_parent_contexts(true), array($systemcontext->id=>$systemcontext));
$this->assertSame($systemcontext->get_parent_context_ids(), array());
$this->assertSame($systemcontext->get_parent_context_ids(true), array($systemcontext->id));
$this->assertSame($frontpagecontext->get_parent_context(), $systemcontext);
$this->assertSame($frontpagecontext->get_parent_contexts(), array($systemcontext->id=>$systemcontext));
$this->assertSame($frontpagecontext->get_parent_contexts(true), array($frontpagecontext->id=>$frontpagecontext, $systemcontext->id=>$systemcontext));
$this->assertSame($frontpagecontext->get_parent_context_ids(), array($systemcontext->id));
$this->assertEquals($frontpagecontext->get_parent_context_ids(true), array($frontpagecontext->id, $systemcontext->id));
$this->assertSame($systemcontext->get_parent_context(), false);
$frontpagecontext = context_course::instance($SITE->id);
$parent = $systemcontext;
foreach ($testcategories as $catid) {
$catcontext = context_coursecat::instance($catid);
$this->assertSame($catcontext->get_parent_context(), $parent);
$parent = $catcontext;
}
$this->assertSame($frontpagepagecontext->get_parent_context(), $frontpagecontext);
$this->assertSame($frontpageblockcontext->get_parent_context(), $frontpagecontext);
$this->assertSame($frontpagepageblockcontext->get_parent_context(), $frontpagepagecontext);
// ====== $context->get_child_contexts() ================================
$CFG->debug = 0;
$children = $systemcontext->get_child_contexts();
$CFG->debug = DEBUG_DEVELOPER;
$this->assertEquals(count($children)+1, $DB->count_records('context'));
$context = context_coursecat::instance($testcategories[3]);
$children = $context->get_child_contexts();
$countcats = 0;
$countcourses = 0;
$countblocks = 0;
foreach ($children as $child) {
if ($child->contextlevel == CONTEXT_COURSECAT) {
$countcats++;
}
if ($child->contextlevel == CONTEXT_COURSE) {
$countcourses++;
}
if ($child->contextlevel == CONTEXT_BLOCK) {
$countblocks++;
}
}
$this->assertEquals(count($children), 8);
$this->assertEquals($countcats, 1);
$this->assertEquals($countcourses, 6);
$this->assertEquals($countblocks, 1);
$context = context_course::instance($testcourses[2]);
$children = $context->get_child_contexts();
$this->assertEquals(count($children), 7); // depends on number of default blocks
$context = context_module::instance($testpages[3]);
$children = $context->get_child_contexts();
$this->assertEquals(count($children), 1);
示例3: get_role_context_caps
function get_role_context_caps($roleid, $context)
{
global $DB;
//this is really slow!!!! - do not use above course context level!
$result = array();
$result[$context->id] = array();
// first emulate the parent context capabilities merging into context
$searchcontexts = array_reverse(get_parent_contexts($context));
array_push($searchcontexts, $context->id);
foreach ($searchcontexts as $cid) {
if ($capabilities = $DB->get_records('role_capabilities', array('roleid' => $roleid, 'contextid' => $cid))) {
foreach ($capabilities as $cap) {
if (!array_key_exists($cap->capability, $result[$context->id])) {
$result[$context->id][$cap->capability] = 0;
}
$result[$context->id][$cap->capability] += $cap->permission;
}
}
}
// now go through the contexts bellow given context
$searchcontexts = array_keys(get_child_contexts($context));
foreach ($searchcontexts as $cid) {
if ($capabilities = $DB->get_records('role_capabilities', array('roleid' => $roleid, 'contextid' => $cid))) {
foreach ($capabilities as $cap) {
if (!array_key_exists($cap->contextid, $result)) {
$result[$cap->contextid] = array();
}
$result[$cap->contextid][$cap->capability] = $cap->permission;
}
}
}
return $result;
}
示例4: reset_course_userdata
/**
* This function will empty a course of user data.
* It will retain the activities and the structure of the course.
*
* @param object $data an object containing all the settings including courseid (without magic quotes)
* @return array status array of array component, item, error
*/
function reset_course_userdata($data)
{
global $CFG, $USER, $DB;
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->libdir . '/completionlib.php';
require_once $CFG->dirroot . '/group/lib.php';
$data->courseid = $data->id;
$context = get_context_instance(CONTEXT_COURSE, $data->courseid);
// calculate the time shift of dates
if (!empty($data->reset_start_date)) {
// time part of course startdate should be zero
$data->timeshift = $data->reset_start_date - usergetmidnight($data->reset_start_date_old);
} else {
$data->timeshift = 0;
}
// result array: component, item, error
$status = array();
// start the resetting
$componentstr = get_string('general');
// move the course start time
if (!empty($data->reset_start_date) and $data->timeshift) {
// change course start data
$DB->set_field('course', 'startdate', $data->reset_start_date, array('id' => $data->courseid));
// update all course and group events - do not move activity events
$updatesql = "UPDATE {event}\n SET timestart = timestart + ?\n WHERE courseid=? AND instance=0";
$DB->execute($updatesql, array($data->timeshift, $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
}
if (!empty($data->reset_logs)) {
$DB->delete_records('log', array('course' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('deletelogs'), 'error' => false);
}
if (!empty($data->reset_events)) {
$DB->delete_records('event', array('courseid' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('deleteevents', 'calendar'), 'error' => false);
}
if (!empty($data->reset_notes)) {
require_once $CFG->dirroot . '/notes/lib.php';
note_delete_all($data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('deletenotes', 'notes'), 'error' => false);
}
if (!empty($data->delete_blog_associations)) {
require_once $CFG->dirroot . '/blog/lib.php';
blog_remove_associations_for_course($data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('deleteblogassociations', 'blog'), 'error' => false);
}
if (!empty($data->reset_course_completion)) {
// Delete course completion information
$course = $DB->get_record('course', array('id' => $data->courseid));
$cc = new completion_info($course);
$cc->delete_course_completion_data();
$status[] = array('component' => $componentstr, 'item' => get_string('deletecoursecompletiondata', 'completion'), 'error' => false);
}
$componentstr = get_string('roles');
if (!empty($data->reset_roles_overrides)) {
$children = get_child_contexts($context);
foreach ($children as $child) {
$DB->delete_records('role_capabilities', array('contextid' => $child->id));
}
$DB->delete_records('role_capabilities', array('contextid' => $context->id));
//force refresh for logged in users
mark_context_dirty($context->path);
$status[] = array('component' => $componentstr, 'item' => get_string('deletecourseoverrides', 'role'), 'error' => false);
}
if (!empty($data->reset_roles_local)) {
$children = get_child_contexts($context);
foreach ($children as $child) {
role_unassign_all(array('contextid' => $child->id));
}
//force refresh for logged in users
mark_context_dirty($context->path);
$status[] = array('component' => $componentstr, 'item' => get_string('deletelocalroles', 'role'), 'error' => false);
}
// First unenrol users - this cleans some of related user data too, such as forum subscriptions, tracking, etc.
$data->unenrolled = array();
if (!empty($data->unenrol_users)) {
$plugins = enrol_get_plugins(true);
$instances = enrol_get_instances($data->courseid, true);
foreach ($instances as $key => $instance) {
if (!isset($plugins[$instance->enrol])) {
unset($instances[$key]);
continue;
}
if (!$plugins[$instance->enrol]->allow_unenrol($instance)) {
unset($instances[$key]);
}
}
$sqlempty = $DB->sql_empty();
foreach ($data->unenrol_users as $withroleid) {
$sql = "SELECT DISTINCT ue.userid, ue.enrolid\n FROM {user_enrolments} ue\n JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)\n JOIN {context} c ON (c.contextlevel = :courselevel AND c.instanceid = e.courseid)\n JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.roleid = :roleid AND ra.userid = ue.userid)";
$params = array('courseid' => $data->courseid, 'roleid' => $withroleid, 'courselevel' => CONTEXT_COURSE);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
//.........这里部分代码省略.........
示例5: role_unassign_all
/**
* Removes multiple role assignments, parameters may contain:
* 'roleid', 'userid', 'contextid', 'component', 'enrolid'.
*
* @param array $params role assignment parameters
* @param bool $subcontexts unassign in subcontexts too
* @param bool $includmanual include manual role assignments too
* @return void
*/
function role_unassign_all(array $params, $subcontexts = false, $includemanual = false)
{
global $USER, $CFG, $DB;
if (!$params) {
throw new coding_exception('Missing parameters in role_unsassign_all() call');
}
$allowed = array('roleid', 'userid', 'contextid', 'component', 'itemid');
foreach ($params as $key => $value) {
if (!in_array($key, $allowed)) {
throw new coding_exception('Unknown role_unsassign_all() parameter key', 'key:' . $key);
}
}
if (isset($params['component']) and $params['component'] !== '' and strpos($params['component'], '_') === false) {
throw new coding_exception('Invalid component paramter in role_unsassign_all() call', 'component:' . $params['component']);
}
if ($includemanual) {
if (!isset($params['component']) or $params['component'] === '') {
throw new coding_exception('include manual parameter requires component parameter in role_unsassign_all() call');
}
}
if ($subcontexts) {
if (empty($params['contextid'])) {
throw new coding_exception('subcontexts paramtere requires component parameter in role_unsassign_all() call');
}
}
$ras = $DB->get_records('role_assignments', $params);
foreach ($ras as $ra) {
$DB->delete_records('role_assignments', array('id' => $ra->id));
if ($context = get_context_instance_by_id($ra->contextid)) {
// this is a bit expensive but necessary
mark_context_dirty($context->path);
/// If the user is the current user, then do full reload of capabilities too.
if (!empty($USER->id) && $USER->id == $ra->userid) {
load_all_capabilities();
}
}
events_trigger('role_unassigned', $ra);
}
unset($ras);
// process subcontexts
if ($subcontexts and $context = get_context_instance_by_id($params['contextid'])) {
$contexts = get_child_contexts($context);
$mparams = $params;
foreach ($contexts as $context) {
$mparams['contextid'] = $context->id;
$ras = $DB->get_records('role_assignments', $mparams);
foreach ($ras as $ra) {
$DB->delete_records('role_assignments', array('id' => $ra->id));
// this is a bit expensive but necessary
mark_context_dirty($context->path);
/// If the user is the current user, then do full reload of capabilities too.
if (!empty($USER->id) && $USER->id == $ra->userid) {
load_all_capabilities();
}
events_trigger('role_unassigned', $ra);
}
}
}
// do this once more for all manual role assignments
if ($includemanual) {
$params['component'] = '';
role_unassign_all($params, $subcontexts, false);
}
}
示例6: reset_course_page_comments
/**
* Delete page_comments in whole course, used by course reset
*
* @param stdClass $context course context
*/
public static function reset_course_page_comments($context) {
global $DB;
$contexts = array();
$contexts[] = $context->id;
$children = get_child_contexts($context);
foreach ($children as $c) {
$contexts[] = $c->id;
}
list($ids, $params) = $DB->get_in_or_equal($contexts);
$DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params);
}
示例7: init_full
function init_full()
{
global $COURSE;
if ($this->full_init_done) {
return;
}
if (empty($this->id)) {
$this->id = 0;
// avoid db errors
}
if ($this->id == $COURSE->id) {
$this->courserecord = $COURSE;
} else {
$this->courserecord = get_record('course', 'id', $this->id);
}
if (empty($this->courserecord) && !defined('ADMIN_STICKYBLOCKS')) {
error('Cannot fully initialize page: invalid course id ' . $this->id);
}
$this->context = get_context_instance(CONTEXT_COURSE, $this->id);
// Preload - ensures that the context cache is populated
// in one DB query...
$this->childcontexts = get_child_contexts($this->context);
// Mark we're done
$this->full_init_done = true;
}
示例8: test_everything_in_accesslib
//.........这里部分代码省略.........
} catch (Exception $e) {
$this->assertTrue(true);
}
$this->assertIdentical($frontpagecontext->get_course_context(true), $frontpagecontext);
$this->assertIdentical($frontpagepagecontext->get_course_context(true), $frontpagecontext);
$this->assertIdentical($frontpagepageblockcontext->get_course_context(true), $frontpagecontext);
// ======= $context->get_parent_context(), $context->get_parent_contexts(), $context->get_parent_context_ids() =======
$userid = reset($testusers);
$usercontext = context_user::instance($userid);
$this->assertIdentical($usercontext->get_parent_context(), $systemcontext);
$this->assertIdentical($usercontext->get_parent_contexts(), array($systemcontext->id => $systemcontext));
$this->assertIdentical($usercontext->get_parent_contexts(true), array($usercontext->id => $usercontext, $systemcontext->id => $systemcontext));
$this->assertIdentical($systemcontext->get_parent_contexts(), array());
$this->assertIdentical($systemcontext->get_parent_contexts(true), array($systemcontext->id => $systemcontext));
$this->assertIdentical($systemcontext->get_parent_context_ids(), array());
$this->assertIdentical($systemcontext->get_parent_context_ids(true), array($systemcontext->id));
$this->assertIdentical($frontpagecontext->get_parent_context(), $systemcontext);
$this->assertIdentical($frontpagecontext->get_parent_contexts(), array($systemcontext->id => $systemcontext));
$this->assertIdentical($frontpagecontext->get_parent_contexts(true), array($frontpagecontext->id => $frontpagecontext, $systemcontext->id => $systemcontext));
$this->assertIdentical($frontpagecontext->get_parent_context_ids(), array($systemcontext->id));
$this->assertEqual($frontpagecontext->get_parent_context_ids(true), array($frontpagecontext->id, $systemcontext->id));
$this->assertIdentical($systemcontext->get_parent_context(), false);
$frontpagecontext = context_course::instance($SITE->id);
$parent = $systemcontext;
foreach ($testcategories as $catid) {
$catcontext = context_coursecat::instance($catid);
$this->assertIdentical($catcontext->get_parent_context(), $parent);
$parent = $catcontext;
}
$this->assertIdentical($frontpagepagecontext->get_parent_context(), $frontpagecontext);
$this->assertIdentical($frontpageblockcontext->get_parent_context(), $frontpagecontext);
$this->assertIdentical($frontpagepageblockcontext->get_parent_context(), $frontpagepagecontext);
// ====== $context->get_child_contexts() ================================
$children = $systemcontext->get_child_contexts();
$this->assertEqual(count($children) + 1, $DB->count_records('context'));
$context = context_coursecat::instance($testcategories[3]);
$children = $context->get_child_contexts();
$countcats = 0;
$countcourses = 0;
$countblocks = 0;
foreach ($children as $child) {
if ($child->contextlevel == CONTEXT_COURSECAT) {
$countcats++;
}
if ($child->contextlevel == CONTEXT_COURSE) {
$countcourses++;
}
if ($child->contextlevel == CONTEXT_BLOCK) {
$countblocks++;
}
}
$this->assertEqual(count($children), 8);
$this->assertEqual($countcats, 1);
$this->assertEqual($countcourses, 6);
$this->assertEqual($countblocks, 1);
$context = context_course::instance($testcourses[2]);
$children = $context->get_child_contexts();
$this->assertEqual(count($children), 3);
$context = context_module::instance($testpages[3]);
$children = $context->get_child_contexts();
$this->assertEqual(count($children), 1);
$context = context_block::instance($testblocks[1]);
$children = $context->get_child_contexts();
$this->assertEqual(count($children), 0);
unset($children);
unset($countcats);
示例9: get_child_contexts
/**
* Recursive function which, given a context, find all its children context ids.
* @param object $context.
* @return array of children context ids.
*/
function get_child_contexts($context)
{
global $CFG;
$children = array();
switch ($context->contextlevel) {
case CONTEXT_BLOCK:
// No children.
return array();
break;
case CONTEXT_MODULE:
// No children.
return array();
break;
case CONTEXT_GROUP:
// No children.
return array();
break;
case CONTEXT_COURSE:
// Find all block instances for the course.
$page = new page_course();
$page->id = $context->instanceid;
$page->type = 'course-view';
if ($blocks = blocks_get_by_page_pinned($page)) {
foreach ($blocks['l'] as $leftblock) {
if ($child = get_context_instance(CONTEXT_BLOCK, $leftblock->id)) {
array_push($children, $child->id);
}
}
foreach ($blocks['r'] as $rightblock) {
if ($child = get_context_instance(CONTEXT_BLOCK, $rightblock->id)) {
array_push($children, $child->id);
}
}
}
// Find all module instances for the course.
if ($modules = get_records('course_modules', 'course', $context->instanceid)) {
foreach ($modules as $module) {
if ($child = get_context_instance(CONTEXT_MODULE, $module->id)) {
array_push($children, $child->id);
}
}
}
// Find all group instances for the course.
if ($groupids = groups_get_groups($context->instanceid)) {
foreach ($groupids as $groupid) {
if ($child = get_context_instance(CONTEXT_GROUP, $groupid)) {
array_push($children, $child->id);
}
}
}
return $children;
break;
case CONTEXT_COURSECAT:
// We need to get the contexts for:
// 1) The subcategories of the given category
// 2) The courses in the given category and all its subcategories
// 3) All the child contexts for these courses
$categories = get_all_subcategories($context->instanceid);
// Add the contexts for all the subcategories.
foreach ($categories as $catid) {
if ($catci = get_context_instance(CONTEXT_COURSECAT, $catid)) {
array_push($children, $catci->id);
}
}
// Add the parent category as well so we can find the contexts
// for its courses.
array_unshift($categories, $context->instanceid);
foreach ($categories as $catid) {
// Find all courses for the category.
if ($courses = get_records('course', 'category', $catid)) {
foreach ($courses as $course) {
if ($courseci = get_context_instance(CONTEXT_COURSE, $course->id)) {
array_push($children, $courseci->id);
$children = array_merge($children, get_child_contexts($courseci));
}
}
}
}
return $children;
break;
case CONTEXT_USER:
// No children.
return array();
break;
case CONTEXT_PERSONAL:
// No children.
return array();
break;
case CONTEXT_SYSTEM:
// Just get all the contexts except for CONTEXT_SYSTEM level.
$sql = 'SELECT c.id ' . 'FROM ' . $CFG->prefix . 'context AS c ' . 'WHERE contextlevel != ' . CONTEXT_SYSTEM;
$contexts = get_records_sql($sql);
foreach ($contexts as $cid) {
array_push($children, $cid->id);
}
//.........这里部分代码省略.........