本文整理汇总了PHP中moodle_page::set_cm方法的典型用法代码示例。如果您正苦于以下问题:PHP moodle_page::set_cm方法的具体用法?PHP moodle_page::set_cm怎么用?PHP moodle_page::set_cm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类moodle_page
的用法示例。
在下文中一共展示了moodle_page::set_cm方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load_module_settings
/**
* This function calls the module function to inject module settings into the
* settings navigation tree.
*
* This only gets called if there is a corrosponding function in the modules
* lib file.
*
* For examples mod/forum/lib.php ::: forum_extend_settings_navigation()
*
* @return navigation_node|false
*/
protected function load_module_settings()
{
global $CFG;
if (!$this->page->cm && $this->context->contextlevel == CONTEXT_MODULE && $this->context->instanceid) {
$cm = get_coursemodule_from_id(false, $this->context->instanceid, 0, false, MUST_EXIST);
$this->page->set_cm($cm, $this->page->course);
}
$file = $CFG->dirroot . '/mod/' . $this->page->activityname . '/lib.php';
if (file_exists($file)) {
require_once $file;
}
$modulenode = $this->add(get_string('pluginadministration', $this->page->activityname));
$modulenode->force_open();
// Settings for the module
if (has_capability('moodle/course:manageactivities', $this->page->cm->context)) {
$url = new moodle_url('/course/modedit.php', array('update' => $this->page->cm->id, 'return' => true, 'sesskey' => sesskey()));
$modulenode->add(get_string('editsettings'), $url, navigation_node::TYPE_SETTING, null, 'modedit');
}
// Assign local roles
if (count(get_assignable_roles($this->page->cm->context)) > 0) {
$url = new moodle_url('/' . $CFG->admin . '/roles/assign.php', array('contextid' => $this->page->cm->context->id));
$modulenode->add(get_string('localroles', 'role'), $url, self::TYPE_SETTING, null, 'roleassign');
}
// Override roles
if (has_capability('moodle/role:review', $this->page->cm->context) or count(get_overridable_roles($this->page->cm->context)) > 0) {
$url = new moodle_url('/' . $CFG->admin . '/roles/permissions.php', array('contextid' => $this->page->cm->context->id));
$modulenode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING, null, 'roleoverride');
}
// Check role permissions
if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:assign'), $this->page->cm->context)) {
$url = new moodle_url('/' . $CFG->admin . '/roles/check.php', array('contextid' => $this->page->cm->context->id));
$modulenode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING, null, 'rolecheck');
}
// Manage filters
if (has_capability('moodle/filter:manage', $this->page->cm->context) && count(filter_get_available_in_context($this->page->cm->context)) > 0) {
$url = new moodle_url('/filter/manage.php', array('contextid' => $this->page->cm->context->id));
$modulenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, 'filtermanage');
}
if (has_capability('coursereport/log:view', get_context_instance(CONTEXT_COURSE, $this->page->cm->course))) {
$url = new moodle_url('/course/report/log/index.php', array('chooselog' => '1', 'id' => $this->page->cm->course, 'modid' => $this->page->cm->id));
$modulenode->add(get_string('logs'), $url, self::TYPE_SETTING, null, 'logreport');
}
// Add a backup link
$featuresfunc = $this->page->activityname . '_supports';
if (function_exists($featuresfunc) && $featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/backup:backupactivity', $this->page->cm->context)) {
$url = new moodle_url('/backup/backup.php', array('id' => $this->page->cm->course, 'cm' => $this->page->cm->id));
$modulenode->add(get_string('backup'), $url, self::TYPE_SETTING, null, 'backup');
}
// Restore this activity
$featuresfunc = $this->page->activityname . '_supports';
if (function_exists($featuresfunc) && $featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/restore:restoreactivity', $this->page->cm->context)) {
$url = new moodle_url('/backup/restorefile.php', array('contextid' => $this->page->cm->context->id));
$modulenode->add(get_string('restore'), $url, self::TYPE_SETTING, null, 'restore');
}
$function = $this->page->activityname . '_extend_settings_navigation';
if (!function_exists($function)) {
return $modulenode;
}
$function($this, $modulenode);
// Remove the module node if there are no children
if (empty($modulenode->children)) {
$modulenode->remove();
}
return $modulenode;
}
示例2: process_url_edit
/**
* Handle showing/processing the submission from the block editing form.
* @return boolean true if the form was submitted and the new config saved. Does not
* return if the editing form was displayed. False otherwise.
*/
public function process_url_edit()
{
global $CFG, $DB, $PAGE, $OUTPUT;
$blockid = optional_param('bui_editid', null, PARAM_INT);
if (!$blockid) {
return false;
}
require_sesskey();
require_once $CFG->dirroot . '/blocks/edit_form.php';
$block = $this->find_instance($blockid);
if (!$block->user_can_edit() && !$this->page->user_can_edit_blocks()) {
throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock'));
}
$editpage = new moodle_page();
$editpage->set_pagelayout('admin');
$editpage->set_course($this->page->course);
//$editpage->set_context($block->context);
$editpage->set_context($this->page->context);
if ($this->page->cm) {
$editpage->set_cm($this->page->cm);
}
$editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
$editurlparams = $this->page->url->params();
$editurlparams['bui_editid'] = $blockid;
$editpage->set_url($editurlbase, $editurlparams);
$editpage->set_block_actions_done();
// At this point we are either going to redirect, or display the form, so
// overwrite global $PAGE ready for this. (Formslib refers to it.)
$PAGE = $editpage;
//some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that to
$output = $editpage->get_renderer('core');
$OUTPUT = $output;
$formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php';
if (is_readable($formfile)) {
require_once $formfile;
$classname = 'block_' . $block->name() . '_edit_form';
if (!class_exists($classname)) {
$classname = 'block_edit_form';
}
} else {
$classname = 'block_edit_form';
}
$mform = new $classname($editpage->url, $block, $this->page);
$mform->set_data($block->instance);
if ($mform->is_cancelled()) {
redirect($this->page->url);
} else {
if ($data = $mform->get_data()) {
$bi = new stdClass();
$bi->id = $block->instance->id;
// This may get overwritten by the special case handling below.
$bi->pagetypepattern = $data->bui_pagetypepattern;
$bi->showinsubcontexts = (bool) $data->bui_contexts;
if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') {
$bi->subpagepattern = null;
} else {
$bi->subpagepattern = $data->bui_subpagepattern;
}
$systemcontext = context_system::instance();
$frontpagecontext = context_course::instance(SITEID);
$parentcontext = context::instance_by_id($data->bui_parentcontextid);
// Updating stickiness and contexts. See MDL-21375 for details.
if (has_capability('moodle/site:manageblocks', $parentcontext)) {
// Check permissions in destination
// Explicitly set the default context
$bi->parentcontextid = $parentcontext->id;
if ($data->bui_editingatfrontpage) {
// The block is being edited on the front page
// The interface here is a special case because the pagetype pattern is
// totally derived from the context menu. Here are the excpetions. MDL-30340
switch ($data->bui_contexts) {
case BUI_CONTEXTS_ENTIRE_SITE:
// The user wants to show the block across the entire site
$bi->parentcontextid = $systemcontext->id;
$bi->showinsubcontexts = true;
$bi->pagetypepattern = '*';
break;
case BUI_CONTEXTS_FRONTPAGE_SUBS:
// The user wants the block shown on the front page and all subcontexts
$bi->parentcontextid = $frontpagecontext->id;
$bi->showinsubcontexts = true;
$bi->pagetypepattern = '*';
break;
case BUI_CONTEXTS_FRONTPAGE_ONLY:
// The user want to show the front page on the frontpage only
$bi->parentcontextid = $frontpagecontext->id;
$bi->showinsubcontexts = false;
$bi->pagetypepattern = 'site-index';
// This is the only relevant page type anyway but we'll set it explicitly just
// in case the front page grows site-index-* subpages of its own later
break;
}
}
}
$bits = explode('-', $bi->pagetypepattern);
//.........这里部分代码省略.........
示例3: test_forum_get_context
/**
* Test that context fetching returns the appropriate context.
*/
public function test_forum_get_context()
{
global $DB, $PAGE;
$this->resetAfterTest();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
$forum = $this->getDataGenerator()->create_module('forum', $options);
$forumcm = get_coursemodule_from_instance('forum', $forum->id);
$forumcontext = \context_module::instance($forumcm->id);
// First check that specifying the context results in the correct context being returned.
// Do this before we set up the page object and we should return from the coursemodule record.
// There should be no DB queries here because the context type was correct.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id, $forumcontext);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(0, $aftercount - $startcount);
// And a context which is not the correct type.
// This tests will result in a DB query to fetch the course_module.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id, $coursecontext);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(1, $aftercount - $startcount);
// Now do not specify a context at all.
// This tests will result in a DB query to fetch the course_module.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(1, $aftercount - $startcount);
// Set up the default page event to use the forum.
$PAGE = new moodle_page();
$PAGE->set_context($forumcontext);
$PAGE->set_cm($forumcm, $course, $forum);
// Now specify a context which is not a context_module.
// There should be no DB queries here because we use the PAGE.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id, $coursecontext);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(0, $aftercount - $startcount);
// Now do not specify a context at all.
// There should be no DB queries here because we use the PAGE.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(0, $aftercount - $startcount);
// Now specify the page context of the course instead..
$PAGE = new moodle_page();
$PAGE->set_context($coursecontext);
// Now specify a context which is not a context_module.
// This tests will result in a DB query to fetch the course_module.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id, $coursecontext);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(1, $aftercount - $startcount);
// Now do not specify a context at all.
// This tests will result in a DB query to fetch the course_module.
$startcount = $DB->perf_get_reads();
$result = forum_get_context($forum->id);
$aftercount = $DB->perf_get_reads();
$this->assertEquals($forumcontext, $result);
$this->assertEquals(1, $aftercount - $startcount);
}
示例4: test_forum_subscription_page_context_valid
/**
* Test that the correct context is used in the events when subscribing
* users.
*/
public function test_forum_subscription_page_context_valid()
{
global $CFG, $PAGE;
require_once $CFG->dirroot . '/mod/forum/lib.php';
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
$forum = $this->getDataGenerator()->create_module('forum', $options);
$quiz = $this->getDataGenerator()->create_module('quiz', $options);
// Add a discussion.
$record = array();
$record['course'] = $course->id;
$record['forum'] = $forum->id;
$record['userid'] = $user->id;
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Add a post.
$record = array();
$record['discussion'] = $discussion->id;
$record['userid'] = $user->id;
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
// Set up the default page event to use this forum.
$PAGE = new moodle_page();
$cm = get_coursemodule_from_instance('forum', $discussion->forum);
$context = \context_module::instance($cm->id);
$PAGE->set_context($context);
$PAGE->set_cm($cm, $course, $forum);
// Trigger and capturing the event.
$sink = $this->redirectEvents();
// Trigger the event by subscribing the user to the forum.
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event);
$this->assertEquals($context, $event->get_context());
// Trigger the event by unsubscribing the user to the forum.
\mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event);
$this->assertEquals($context, $event->get_context());
// Trigger the event by subscribing the user to the discussion.
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event);
$this->assertEquals($context, $event->get_context());
// Trigger the event by unsubscribing the user from the discussion.
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event);
$this->assertEquals($context, $event->get_context());
// Now try with the context for a different module (quiz).
$PAGE = new moodle_page();
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
$quizcontext = \context_module::instance($cm->id);
$PAGE->set_context($quizcontext);
$PAGE->set_cm($cm, $course, $quiz);
// Trigger and capturing the event.
$sink = $this->redirectEvents();
// Trigger the event by subscribing the user to the forum.
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event);
$this->assertEquals($context, $event->get_context());
// Trigger the event by unsubscribing the user to the forum.
\mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
$events = $sink->get_events();
$sink->clear();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event);
$this->assertEquals($context, $event->get_context());
// Trigger the event by subscribing the user to the discussion.
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
$events = $sink->get_events();
$sink->clear();
//.........这里部分代码省略.........
示例5: process_url_edit
/**
* Handle showing/processing the submission from the block editing form.
* @return boolean true if the form was submitted and the new config saved. Does not
* return if the editing form was displayed. False otherwise.
*/
public function process_url_edit()
{
global $CFG, $DB, $PAGE;
$blockid = optional_param('bui_editid', null, PARAM_INTEGER);
if (!$blockid) {
return false;
}
require_sesskey();
require_once $CFG->dirroot . '/blocks/edit_form.php';
$block = $this->find_instance($blockid);
if (!$block->user_can_edit() && !$this->page->user_can_edit_blocks()) {
throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock'));
}
$editpage = new moodle_page();
$editpage->set_pagelayout('admin');
$editpage->set_course($this->page->course);
$editpage->set_context($block->context);
if ($this->page->cm) {
$editpage->set_cm($this->page->cm);
}
$editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
$editurlparams = $this->page->url->params();
$editurlparams['bui_editid'] = $blockid;
$editpage->set_url($editurlbase, $editurlparams);
$editpage->set_block_actions_done();
// At this point we are either going to redirect, or display the form, so
// overwrite global $PAGE ready for this. (Formslib refers to it.)
$PAGE = $editpage;
$formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php';
if (is_readable($formfile)) {
require_once $formfile;
$classname = 'block_' . $block->name() . '_edit_form';
if (!class_exists($classname)) {
$classname = 'block_edit_form';
}
} else {
$classname = 'block_edit_form';
}
$mform = new $classname($editpage->url, $block, $this->page);
$mform->set_data($block->instance);
if ($mform->is_cancelled()) {
redirect($this->page->url);
} else {
if ($data = $mform->get_data()) {
$bi = new stdClass();
$bi->id = $block->instance->id;
$bi->pagetypepattern = $data->bui_pagetypepattern;
if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') {
$bi->subpagepattern = null;
} else {
$bi->subpagepattern = $data->bui_subpagepattern;
}
$parentcontext = get_context_instance_by_id($data->bui_parentcontextid);
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
// Updating stickiness and contexts. See MDL-21375 for details.
if (has_capability('moodle/site:manageblocks', $parentcontext)) {
// Check permissions in destination
// Explicitly set the context
$bi->parentcontextid = $parentcontext->id;
// If the context type is > 0 then we'll explicitly set the block as sticky, otherwise not
$bi->showinsubcontexts = (int) (!empty($data->bui_contexts));
// If the block wants to be system-wide, then explicitly set that
if ($data->bui_contexts == 2) {
// Only possible on a frontpage or system page
$bi->parentcontextid = $systemcontext->id;
} else {
// The block doesn't want to be system-wide, so let's ensure that
if ($parentcontext->id == $systemcontext->id) {
// We need to move it to the front page
$frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
$bi->parentcontextid = $frontpagecontext->id;
$bi->pagetypepattern = '*';
// Just in case
}
}
}
$bi->defaultregion = $data->bui_defaultregion;
$bi->defaultweight = $data->bui_defaultweight;
$DB->update_record('block_instances', $bi);
if (!empty($block->config)) {
$config = clone $block->config;
} else {
$config = new stdClass();
}
foreach ($data as $configfield => $value) {
if (strpos($configfield, 'config_') !== 0) {
continue;
}
$field = substr($configfield, 7);
$config->{$field} = $value;
}
$block->instance_config_save($config);
$bp = new stdClass();
$bp->visible = $data->bui_visible;
$bp->region = $data->bui_region;
//.........这里部分代码省略.........
示例6: set_context
/**
* Overridden to return the course module context
*
* @param moodle_page $PAGE global PAGE
*/
public function set_context($PAGE)
{
$PAGE->set_cm($this->cm);
}