本文整理汇总了PHP中context_helper::preload_course方法的典型用法代码示例。如果您正苦于以下问题:PHP context_helper::preload_course方法的具体用法?PHP context_helper::preload_course怎么用?PHP context_helper::preload_course使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类context_helper
的用法示例。
在下文中一共展示了context_helper::preload_course方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: preload_course_contexts
/**
* Preloads all contexts relating to a course: course, modules. Block contexts
* are no longer loaded here. The contexts for all the blocks on the current
* page are now efficiently loaded by {@link block_manager::load_blocks()}.
*
* @deprecated since 2.2
* @param int $courseid Course ID
* @return void
*/
function preload_course_contexts($courseid)
{
context_helper::preload_course($courseid);
}
示例2: array
}
}
$course = $DB->get_record('course', $params, '*', MUST_EXIST);
$urlparams = array('id' => $course->id);
// Sectionid should get priority over section number
if ($sectionid) {
$section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
}
if ($section) {
$urlparams['section'] = $section;
}
$PAGE->set_url('/course/view.php', $urlparams);
// Defined here to avoid notices on errors etc
// Prevent caching of this page to stop confusion when changing page after making AJAX changes
$PAGE->set_cacheable(false);
context_helper::preload_course($course->id);
$context = context_course::instance($course->id, MUST_EXIST);
// Remove any switched roles before checking login
if ($switchrole == 0 && confirm_sesskey()) {
role_switch($switchrole, $context);
}
require_login($course);
// Switchrole - sanity check in cost-order...
$reset_user_allowed_editing = false;
if ($switchrole > 0 && confirm_sesskey() && has_capability('moodle/role:switchroles', $context)) {
// is this role assignable in this context?
// inquiring minds want to know...
$aroles = get_switchable_roles($context);
if (is_array($aroles) && isset($aroles[$switchrole])) {
role_switch($switchrole, $context);
// Double check that this role is allowed here
示例3: preload_course_contexts
/**
* Preloads all contexts relating to a course: course, modules. Block contexts
* are no longer loaded here. The contexts for all the blocks on the current
* page are now efficiently loaded by {@link block_manager::load_blocks()}.
*
* @deprecated since Moodle 2.2
* @see context_helper::preload_course()
* @param int $courseid Course ID
* @return void
*/
function preload_course_contexts($courseid)
{
debugging('preload_course_contexts() is deprecated, please use context_helper::preload_course() instead.', DEBUG_DEVELOPER);
context_helper::preload_course($courseid);
}
示例4: test_everything_in_accesslib
//.........这里部分代码省略.........
unset($countcourses);
unset($countblocks);
// ======= context_helper::reset_caches() ============================
context_helper::reset_caches();
$this->assertEquals(context_inspection::test_context_cache_size(), 0);
context_course::instance($SITE->id);
$this->assertEquals(context_inspection::test_context_cache_size(), 1);
// ======= context preloading ========================================
context_helper::reset_caches();
$sql = "SELECT ".context_helper::get_preload_record_columns_sql('c')."
FROM {context} c
WHERE c.contextlevel <> ".CONTEXT_SYSTEM;
$records = $DB->get_records_sql($sql);
$firstrecord = reset($records);
$columns = context_helper::get_preload_record_columns('c');
$firstrecord = (array)$firstrecord;
$this->assertSame(array_keys($firstrecord), array_values($columns));
context_helper::reset_caches();
foreach ($records as $record) {
context_helper::preload_from_record($record);
$this->assertEquals($record, new stdClass());
}
$this->assertEquals(context_inspection::test_context_cache_size(), count($records));
unset($records);
unset($columns);
context_helper::reset_caches();
context_helper::preload_course($SITE->id);
$this->assertEquals(7, context_inspection::test_context_cache_size()); // depends on number of default blocks
// ====== assign_capability(), unassign_capability() ====================
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertFalse($rc);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext->id);
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertEquals($rc->permission, CAP_ALLOW);
assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext->id);
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertEquals($rc->permission, CAP_ALLOW);
assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext, true);
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertEquals($rc->permission, CAP_PREVENT);
assign_capability('moodle/site:accessallgroups', CAP_INHERIT, $allroles['teacher'], $frontpagecontext);
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertFalse($rc);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext);
unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext, true);
$rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups'));
$this->assertFalse($rc);
unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true);
unset($rc);
accesslib_clear_all_caches(false); // must be done after assign_capability()
// ======= role_assign(), role_unassign(), role_unassign_all() ==============
$context = context_course::instance($testcourses[1]);
示例5: __construct
/**
* Constructs based on course.
* Note: This constructor should not usually be called directly.
* Use get_fast_modinfo($course) instead as this maintains a cache.
* @param stdClass $course course object, only property id is required.
* @param int $userid User ID
* @throws moodle_exception if course is not found
*/
public function __construct($course, $userid) {
global $CFG, $COURSE, $SITE, $DB;
if (!isset($course->cacherev)) {
// We require presence of property cacherev to validate the course cache.
// No need to clone the $COURSE or $SITE object here because we clone it below anyway.
$course = get_course($course->id, false);
}
$cachecoursemodinfo = cache::make('core', 'coursemodinfo');
// Retrieve modinfo from cache. If not present or cacherev mismatches, call rebuild and retrieve again.
$coursemodinfo = $cachecoursemodinfo->get($course->id);
if ($coursemodinfo === false || ($course->cacherev != $coursemodinfo->cacherev)) {
$coursemodinfo = self::build_course_cache($course);
}
// Set initial values
$this->userid = $userid;
$this->sections = array();
$this->cms = array();
$this->instances = array();
$this->groups = null;
// If we haven't already preloaded contexts for the course, do it now
// Modules are also cached here as long as it's the first time this course has been preloaded.
context_helper::preload_course($course->id);
// Quick integrity check: as a result of race conditions modinfo may not be regenerated after the change.
// It is especially dangerous if modinfo contains the deleted course module, as it results in fatal error.
// We can check it very cheap by validating the existence of module context.
if ($course->id == $COURSE->id || $course->id == $SITE->id) {
// Only verify current course (or frontpage) as pages with many courses may not have module contexts cached.
// (Uncached modules will result in a very slow verification).
foreach ($coursemodinfo->modinfo as $mod) {
if (!context_module::instance($mod->cm, IGNORE_MISSING)) {
debugging('Course cache integrity check failed: course module with id '. $mod->cm.
' does not have context. Rebuilding cache for course '. $course->id);
// Re-request the course record from DB as well, don't use get_course() here.
$course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
$coursemodinfo = self::build_course_cache($course);
break;
}
}
}
// Overwrite unset fields in $course object with cached values, store the course object.
$this->course = fullclone($course);
foreach ($coursemodinfo as $key => $value) {
if ($key !== 'modinfo' && $key !== 'sectioncache' &&
(!isset($this->course->$key) || $key === 'cacherev')) {
$this->course->$key = $value;
}
}
// Loop through each piece of module data, constructing it
static $modexists = array();
foreach ($coursemodinfo->modinfo as $mod) {
if (empty($mod->name)) {
// something is wrong here
continue;
}
// Skip modules which don't exist
if (!array_key_exists($mod->mod, $modexists)) {
$modexists[$mod->mod] = file_exists("$CFG->dirroot/mod/$mod->mod/lib.php");
}
if (!$modexists[$mod->mod]) {
continue;
}
// Construct info for this module
$cm = new cm_info($this, null, $mod, null);
// Store module in instances and cms array
if (!isset($this->instances[$cm->modname])) {
$this->instances[$cm->modname] = array();
}
$this->instances[$cm->modname][$cm->instance] = $cm;
$this->cms[$cm->id] = $cm;
// Reconstruct sections. This works because modules are stored in order
if (!isset($this->sections[$cm->sectionnum])) {
$this->sections[$cm->sectionnum] = array();
}
$this->sections[$cm->sectionnum][] = $cm->id;
}
// Expand section objects
$this->sectioninfo = array();
foreach ($coursemodinfo->sectioncache as $number => $data) {
$this->sectioninfo[$number] = new section_info($data, $number, null, null,
//.........这里部分代码省略.........
示例6: test_everything_in_accesslib
//.........这里部分代码省略.........
$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);
unset($countcourses);
unset($countblocks);
// ======= context_helper::reset_caches() ============================
context_helper::reset_caches();
$this->assertEqual(context_inspection::test_context_cache_size(), 0);
context_course::instance($SITE->id);
$this->assertEqual(context_inspection::test_context_cache_size(), 1);
// ======= context preloading ========================================
context_helper::reset_caches();
$sql = "SELECT " . context_helper::get_preload_record_columns_sql('c') . "\n FROM {context} c\n WHERE c.contextlevel <> " . CONTEXT_SYSTEM;
$records = $DB->get_records_sql($sql);
$firstrecord = reset($records);
$columns = context_helper::get_preload_record_columns('c');
$firstrecord = (array) $firstrecord;
$this->assertIdentical(array_keys($firstrecord), array_values($columns));
context_helper::reset_caches();
foreach ($records as $record) {
context_helper::preload_from_record($record);
$this->assertIdentical($record, new stdClass());
}
$this->assertEqual(context_inspection::test_context_cache_size(), count($records));
unset($records);
unset($columns);
context_helper::reset_caches();
context_helper::preload_course($SITE->id);
$this->assertEqual(context_inspection::test_context_cache_size(), 4);
// ====== assign_capability(), unassign_capability() ====================
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertFalse($rc);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext->id);
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertEqual($rc->permission, CAP_ALLOW);
assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext->id);
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertEqual($rc->permission, CAP_ALLOW);
assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext, true);
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertEqual($rc->permission, CAP_PREVENT);
assign_capability('moodle/site:accessallgroups', CAP_INHERIT, $allroles['teacher'], $frontpagecontext);
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertFalse($rc);
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext);
unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext, true);
$rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups'));
$this->assertFalse($rc);
unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true);
unset($rc);
accesslib_clear_all_caches(false);
// must be done after assign_capability()
// ======= role_assign(), role_unassign(), role_unassign_all() ==============
$context = context_course::instance($testcourses[1]);
$this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 0);
role_assign($allroles['teacher'], $testusers[1], $context->id);
role_assign($allroles['teacher'], $testusers[2], $context->id);
role_assign($allroles['manager'], $testusers[1], $context->id);
$this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 3);
role_unassign($allroles['teacher'], $testusers[1], $context->id);