本文整理汇总了PHP中is_role_switched函数的典型用法代码示例。如果您正苦于以下问题:PHP is_role_switched函数的具体用法?PHP is_role_switched怎么用?PHP is_role_switched使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_role_switched函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: definition
/**
* Defines the form fields.
*/
public function definition()
{
global $USER, $CFG, $DB;
$mform = $this->_form;
$course = $this->_customdata['course'];
// Overall criteria aggregation.
$context = context_course::instance($course->id);
$roles = array();
$assumedrole = -1;
if (is_role_switched($course->id)) {
$roles[0] = get_string('switchrolereturn');
$assumedrole = $USER->access['rsw'][$context->path];
}
$availableroles = get_switchable_roles($context);
if (is_array($availableroles)) {
foreach ($availableroles as $key => $role) {
if ($assumedrole == (int) $key) {
continue;
}
$roles[$key] = $role;
}
}
$mform->addElement('select', 'switchrole', get_string('role'), $roles);
// Add common action buttons.
$this->add_action_buttons();
// Add hidden fields.
$mform->addElement('hidden', 'id', $course->id);
$mform->setType('id', PARAM_INT);
}
示例2: poll_user_eligible
public function poll_user_eligible()
{
global $COURSE, $USER;
$parents = $this->context->get_parent_context_ids();
$parentctx = context::instance_by_id($parents[0]);
$switched = false;
if ($this->poll->eligible == 'students') {
$switched = is_role_switched($COURSE->id);
if (isset($USER->access['rsw'][$parentctx->path])) {
$switched = $switched && !role_context_capabilities($USER->access['rsw'][$parentctx->path], $this->context, 'block/poll:editpoll');
} else {
$switched = false;
}
}
// TODO: Proper roles & capabilities.
return $this->poll->eligible == 'all' || $this->poll->eligible == 'students' && !$this->poll_can_edit() || $switched || $this->poll->eligible == 'teachers' && $this->poll_can_edit();
}
示例3: __construct
/**
* @throws coding_exception
*/
public function __construct()
{
global $PAGE, $COURSE;
// Page path blacklist for admin menu.
$adminblockblacklist = ['/user/profile.php'];
if (in_array(local::current_url_path(), $adminblockblacklist)) {
return;
}
// Admin users always see the admin menu with the exception of blacklisted pages.
// The admin menu shows up for other users if they are a teacher in the current course.
if (!is_siteadmin()) {
// We don't want students to see the admin menu ever.
// Editing teachers are identified as people who can manage activities and non editing teachers as those who
// can view the gradebook. As editing teachers are almost certain to also be able to view the gradebook, the
// grader:view capability is checked first.
$caps = ['gradereport/grader:view', 'moodle/course:manageactivities'];
$canmanageacts = has_any_capability($caps, $PAGE->context);
$isstudent = !$canmanageacts && !is_role_switched($COURSE->id);
if ($isstudent) {
return;
}
}
if (!$PAGE->blocks->is_block_present('settings')) {
// Throw error if on front page or course page.
// (There are pages that don't have a settings block so we shouldn't throw an error on those pages).
if (strpos($PAGE->pagetype, 'course-view') === 0 || $PAGE->pagetype === 'site-index') {
debugging('Settings block was not found on this page', DEBUG_DEVELOPER);
}
return;
}
// Core Moodle API appears to be missing a 'get block by name' function.
// Cycle through all regions and block instances until we find settings.
foreach ($PAGE->blocks->get_regions() as $region) {
foreach ($PAGE->blocks->get_blocks_for_region($region) as $block) {
if (isset($block->instance) && $block->instance->blockname == 'settings') {
$this->instanceid = $block->instance->id;
break 2;
}
}
}
if (!has_capability('moodle/block:view', \context_block::instance($this->instanceid))) {
return;
}
$this->output = true;
}
示例4: add_course
/**
* Adds the given course to the navigation structure.
*
* @param stdClass $course
* @return navigation_node
*/
public function add_course(stdClass $course, $forcegeneric = false) {
global $CFG;
if ($course->id != SITEID) {
if (!$course->visible) {
if (is_role_switched($course->id)) {
// user has to be able to access course in order to switch, let's skip the visibility test here
} else if (!has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) {
return false;
}
}
}
$issite = ($course->id == SITEID);
$ismycourse = (array_key_exists($course->id, $this->mycourses) && !$forcegeneric);
$displaycategories = (!$ismycourse && !$issite && !empty($CFG->navshowcategories));
$shortname = $course->shortname;
if ($issite) {
$parent = $this;
$url = null;
$shortname = get_string('sitepages');
} else if ($ismycourse) {
$parent = $this->rootnodes['mycourses'];
$url = new moodle_url('/course/view.php', array('id'=>$course->id));
} else {
$parent = $this->rootnodes['courses'];
$url = new moodle_url('/course/view.php', array('id'=>$course->id));
}
if ($displaycategories) {
// We need to load the category structure for this course
$categoryfound = false;
if (!empty($course->categorypath)) {
$categories = explode('/', trim($course->categorypath, '/'));
$category = $parent;
while ($category && $categoryid = array_shift($categories)) {
$category = $category->get($categoryid, self::TYPE_CATEGORY);
}
if ($category instanceof navigation_node) {
$parent = $category;
$categoryfound = true;
}
if (!$categoryfound && $forcegeneric) {
$this->load_all_categories($course->category);
if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) {
$parent = $category;
$categoryfound = true;
}
}
} else if (!empty($course->category)) {
$this->load_all_categories($course->category);
if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) {
$parent = $category;
$categoryfound = true;
}
if (!$categoryfound && !$forcegeneric) {
$this->load_all_categories($course->category);
if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) {
$parent = $category;
$categoryfound = true;
}
}
}
}
// We found the course... we can return it now :)
if ($coursenode = $parent->get($course->id, self::TYPE_COURSE)) {
return $coursenode;
}
$coursenode = $parent->add($shortname, $url, self::TYPE_COURSE, $shortname, $course->id);
$coursenode->nodetype = self::NODETYPE_BRANCH;
$coursenode->hidden = (!$course->visible);
$coursenode->title($course->fullname);
$this->addedcourses[$course->id] = &$coursenode;
if ($ismycourse && !empty($CFG->navshowallcourses)) {
// We need to add this course to the general courses node as well as the
// my courses node, rerun the function with the kill param
$genericcourse = $this->add_course($course, true);
if ($genericcourse->isactive) {
$genericcourse->make_inactive();
$genericcourse->collapse = true;
if ($genericcourse->parent && $genericcourse->parent->type == self::TYPE_CATEGORY) {
$parent = $genericcourse->parent;
while ($parent && $parent->type == self::TYPE_CATEGORY) {
$parent->collapse = true;
$parent = $parent->parent;
}
}
}
}
return $coursenode;
}
示例5: print_settings_link
/**
* Print settings link
*
* @return string
*/
public function print_settings_link()
{
global $DB, $PAGE, $COURSE;
$isstudent = !has_capability('moodle/course:manageactivities', $PAGE->context) && !is_role_switched($COURSE->id);
if ($isstudent && $PAGE->pagetype != 'user-profile') {
return '';
}
if (!($instanceid = $DB->get_field('block_instances', 'id', array('blockname' => 'settings')))) {
return '';
}
if (!has_capability('moodle/block:view', context_block::instance($instanceid))) {
return '';
}
$admin = get_string('admin', 'theme_snap');
return '<div><a class="settings-button snap-action-icon" href="#inst' . $instanceid . '">
<i class="icon icon-arrows-02"></i><small>' . $admin . '</small></a></div>';
}
示例6: login_info
/**
* Return the standard string that says whether you are logged in (and switched
* roles/logged in as another user).
* @param bool $withlinks if false, then don't include any links in the HTML produced.
* If not set, the default is the nologinlinks option from the theme config.php file,
* and if that is not set, then links are included.
* @return string HTML fragment.
*/
public function login_info($withlinks = null)
{
global $USER, $CFG, $DB, $SESSION;
if (during_initial_install()) {
return '';
}
if (is_null($withlinks)) {
$withlinks = empty($this->page->layout_options['nologinlinks']);
}
$loginpage = (string) $this->page->url === get_login_url();
$course = $this->page->course;
if (\core\session\manager::is_loggedinas()) {
$realuser = \core\session\manager::get_realuser();
$fullname = fullname($realuser, true);
if ($withlinks) {
$loginastitle = get_string('loginas');
$realuserinfo = " [<a href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&sesskey=" . sesskey() . "\"";
$realuserinfo .= "title =\"" . $loginastitle . "\">{$fullname}</a>] ";
} else {
$realuserinfo = " [{$fullname}] ";
}
} else {
$realuserinfo = '';
}
$loginurl = get_login_url();
if (empty($course->id)) {
// $course->id is not defined during installation
return '';
} else {
if (isloggedin()) {
$context = context_course::instance($course->id);
$fullname = fullname($USER, true);
// Since Moodle 2.0 this link always goes to the public profile page (not the course profile page)
if ($withlinks) {
$linktitle = get_string('viewprofile');
$username = "<a href=\"{$CFG->wwwroot}/user/profile.php?id={$USER->id}\" title=\"{$linktitle}\">{$fullname}</a>";
} else {
$username = $fullname;
}
if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid))) {
if ($withlinks) {
$username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>";
} else {
$username .= " from {$idprovider->name}";
}
}
if (isguestuser()) {
$loggedinas = $realuserinfo . get_string('loggedinasguest');
if (!$loginpage && $withlinks) {
$loggedinas .= " (<a href=\"{$loginurl}\">" . get_string('login') . '</a>)';
}
} else {
if (is_role_switched($course->id)) {
// Has switched roles
$rolename = '';
if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) {
$rolename = ': ' . role_get_name($role, $context);
}
$loggedinas = get_string('loggedinas', 'moodle', $username) . $rolename;
if ($withlinks) {
$url = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false)));
$loggedinas .= '(' . html_writer::tag('a', get_string('switchrolereturn'), array('href' => $url)) . ')';
}
} else {
$loggedinas = $realuserinfo . get_string('loggedinas', 'moodle', $username);
if ($withlinks) {
echo "<i class='fa fa-user hide979 mywhite'></i> ";
//****************$loggedinas .= " (<a href=\"$CFG->wwwroot/login/logout.php?sesskey=".sesskey()."\">".get_string('logout').'</a>)';
$loggedinas .= " <span class=\"line-trans\">|</span><a class=\"logtop\" href=\"{$CFG->wwwroot}/login/logout.php?sesskey=" . sesskey() . "\"> " . get_string('logout') . '</a><span class="line-trans"> |</span>';
}
}
}
} else {
$loggedinas = get_string('loggedinnot', 'moodle');
if (!$loginpage && $withlinks) {
//****************$loggedinas $loggedinas .= " (<a href=\"$loginurl\">".get_string('login').'</a>)';
echo "<i class='fa fa-lock hide979 mywhite'></i> ";
$loggedinas .= " | <a href=\"{$loginurl}\">" . get_string('login') . '</a> |';
}
}
}
$loggedinas = '<div class="logininfo">' . $loggedinas . '</div>';
if (isset($SESSION->justloggedin)) {
unset($SESSION->justloggedin);
if (!empty($CFG->displayloginfailures)) {
if (!isguestuser()) {
if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) {
$loggedinas .= ' <div class="loginfailures">';
if (empty($count->accounts)) {
$loggedinas .= get_string('failedloginattempts', '', $count);
} else {
$loggedinas .= get_string('failedloginattemptsall', '', $count);
//.........这里部分代码省略.........
示例7: add_course
/**
* Adds the given course to the navigation structure.
*
* @param stdClass $course
* @param bool $forcegeneric
* @param bool $ismycourse
* @return navigation_node
*/
public function add_course(stdClass $course, $forcegeneric = false, $coursetype = self::COURSE_OTHER)
{
global $CFG, $SITE;
// We found the course... we can return it now :)
if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) {
return $this->addedcourses[$course->id];
}
$coursecontext = context_course::instance($course->id);
if ($course->id != $SITE->id && !$course->visible) {
if (is_role_switched($course->id)) {
// user has to be able to access course in order to switch, let's skip the visibility test here
} else {
if (!has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
return false;
}
}
}
$issite = $course->id == $SITE->id;
$shortname = format_string($course->shortname, true, array('context' => $coursecontext));
$fullname = format_string($course->fullname, true, array('context' => $coursecontext));
// This is the name that will be shown for the course.
$coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname;
// Can the user expand the course to see its content.
$canexpandcourse = true;
if ($issite) {
$parent = $this;
$url = null;
if (empty($CFG->usesitenameforsitepages)) {
$coursename = get_string('sitepages');
}
} else {
if ($coursetype == self::COURSE_CURRENT) {
$parent = $this->rootnodes['currentcourse'];
$url = new moodle_url('/course/view.php', array('id' => $course->id));
$canexpandcourse = $this->can_expand_course($course);
} else {
if ($coursetype == self::COURSE_MY && !$forcegeneric) {
if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) {
// Nothing to do here the above statement set $parent to the category within mycourses.
} else {
$parent = $this->rootnodes['mycourses'];
}
$url = new moodle_url('/course/view.php', array('id' => $course->id));
} else {
$parent = $this->rootnodes['courses'];
$url = new moodle_url('/course/view.php', array('id' => $course->id));
// They can only expand the course if they can access it.
$canexpandcourse = $this->can_expand_course($course);
if (!empty($course->category) && $this->show_categories($coursetype == self::COURSE_MY)) {
if (!$this->is_category_fully_loaded($course->category)) {
// We need to load the category structure for this course
$this->load_all_categories($course->category, false);
}
if (array_key_exists($course->category, $this->addedcategories)) {
$parent = $this->addedcategories[$course->category];
// This could lead to the course being created so we should check whether it is the case again
if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) {
return $this->addedcourses[$course->id];
}
}
}
}
}
}
$coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id);
$coursenode->hidden = !$course->visible;
$coursenode->title(format_string($course->fullname, true, array('context' => $coursecontext, 'escape' => false)));
if ($canexpandcourse) {
// This course can be expanded by the user, make it a branch to make the system aware that its expandable by ajax.
$coursenode->nodetype = self::NODETYPE_BRANCH;
$coursenode->isexpandable = true;
} else {
$coursenode->nodetype = self::NODETYPE_LEAF;
$coursenode->isexpandable = false;
}
if (!$forcegeneric) {
$this->addedcourses[$course->id] = $coursenode;
}
return $coursenode;
}
示例8: header
/**
* Start output by sending the HTTP headers, and printing the HTML <head>
* and the start of the <body>.
*
* To control what is printed, you should set properties on $PAGE. If you
* are familiar with the old {@link print_header()} function from Moodle 1.9
* you will find that there are properties on $PAGE that correspond to most
* of the old parameters to could be passed to print_header.
*
* Not that, in due course, the remaining $navigation, $menu parameters here
* will be replaced by more properties of $PAGE, but that is still to do.
*
* @return string HTML that you must output this, preferably immediately.
*/
public function header() {
global $USER, $CFG;
if (\core\session\manager::is_loggedinas()) {
$this->page->add_body_class('userloggedinas');
}
// If the user is logged in, and we're not in initial install,
// check to see if the user is role-switched and add the appropriate
// CSS class to the body element.
if (!during_initial_install() && isloggedin() && is_role_switched($this->page->course->id)) {
$this->page->add_body_class('userswitchedrole');
}
// Give themes a chance to init/alter the page object.
$this->page->theme->init_page($this->page);
$this->page->set_state(moodle_page::STATE_PRINTING_HEADER);
// Find the appropriate page layout file, based on $this->page->pagelayout.
$layoutfile = $this->page->theme->layout_file($this->page->pagelayout);
// Render the layout using the layout file.
$rendered = $this->render_page_layout($layoutfile);
// Slice the rendered output into header and footer.
$cutpos = strpos($rendered, $this->unique_main_content_token);
if ($cutpos === false) {
$cutpos = strpos($rendered, self::MAIN_CONTENT_TOKEN);
$token = self::MAIN_CONTENT_TOKEN;
} else {
$token = $this->unique_main_content_token;
}
if ($cutpos === false) {
throw new coding_exception('page layout file ' . $layoutfile . ' does not contain the main content placeholder, please include "<?php echo $OUTPUT->main_content() ?>" in theme layout file.');
}
$header = substr($rendered, 0, $cutpos);
$footer = substr($rendered, $cutpos + strlen($token));
if (empty($this->contenttype)) {
debugging('The page layout file did not call $OUTPUT->doctype()');
$header = $this->doctype() . $header;
}
// If this theme version is below 2.4 release and this is a course view page
if ((!isset($this->page->theme->settings->version) || $this->page->theme->settings->version < 2012101500) &&
$this->page->pagelayout === 'course' && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
// check if course content header/footer have not been output during render of theme layout
$coursecontentheader = $this->course_content_header(true);
$coursecontentfooter = $this->course_content_footer(true);
if (!empty($coursecontentheader)) {
// display debug message and add header and footer right above and below main content
// Please note that course header and footer (to be displayed above and below the whole page)
// are not displayed in this case at all.
// Besides the content header and footer are not displayed on any other course page
debugging('The current theme is not optimised for 2.4, the course-specific header and footer defined in course format will not be output', DEBUG_DEVELOPER);
$header .= $coursecontentheader;
$footer = $coursecontentfooter. $footer;
}
}
send_headers($this->contenttype, $this->page->cacheable);
$this->opencontainers->push('header/footer', $footer);
$this->page->set_state(moodle_page::STATE_IN_BODY);
return $header . $this->skip_link_target('maincontent');
}
示例9: require_login
//.........这里部分代码省略.........
redirect($CFG->wwwroot . '/user/policy.php');
} else {
if (!empty($CFG->sitepolicyguest) and isguestuser()) {
if ($preventredirect) {
throw new moodle_exception('sitepolicynotagreed', 'error', '', $CFG->sitepolicyguest);
}
if ($setwantsurltome) {
$SESSION->wantsurl = qualified_me();
}
redirect($CFG->wwwroot . '/user/policy.php');
}
}
}
// Fetch the system context, the course context, and prefetch its child contexts.
$sysctx = context_system::instance();
$coursecontext = context_course::instance($course->id, MUST_EXIST);
if ($cm) {
$cmcontext = context_module::instance($cm->id, MUST_EXIST);
} else {
$cmcontext = null;
}
// If the site is currently under maintenance, then print a message.
if (!empty($CFG->maintenance_enabled) and !has_capability('moodle/site:maintenanceaccess', $sysctx)) {
if ($preventredirect) {
throw new require_login_exception('Maintenance in progress');
}
$PAGE->set_context(null);
print_maintenance_message();
}
// Make sure the course itself is not hidden.
if ($course->id == SITEID) {
// Frontpage can not be hidden.
} else {
if (is_role_switched($course->id)) {
// When switching roles ignore the hidden flag - user had to be in course to do the switch.
} else {
if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
// Originally there was also test of parent category visibility, BUT is was very slow in complex queries
// involving "my courses" now it is also possible to simply hide all courses user is not enrolled in :-).
if ($preventredirect) {
throw new require_login_exception('Course is hidden');
}
$PAGE->set_context(null);
// We need to override the navigation URL as the course won't have been added to the navigation and thus
// the navigation will mess up when trying to find it.
navigation_node::override_active_url(new moodle_url('/'));
notice(get_string('coursehidden'), $CFG->wwwroot . '/');
}
}
}
// Is the user enrolled?
if ($course->id == SITEID) {
// Everybody is enrolled on the frontpage.
} else {
if (\core\session\manager::is_loggedinas()) {
// Make sure the REAL person can access this course first.
$realuser = \core\session\manager::get_realuser();
if (!is_enrolled($coursecontext, $realuser->id, '', true) and !is_viewing($coursecontext, $realuser->id) and !is_siteadmin($realuser->id)) {
if ($preventredirect) {
throw new require_login_exception('Invalid course login-as access');
}
$PAGE->set_context(null);
echo $OUTPUT->header();
notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot . '/');
}
}
示例10: test_everything_in_accesslib
//.........这里部分代码省略.........
$this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, 0));
$this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, 0));
$this->assertTrue(has_capability('mod/page:view', $frontpagecontext, 0));
$this->assertFalse(has_capability('mod/page:view', $systemcontext, 0));
$this->assertFalse(has_capability('moodle/course:create', $systemcontext, $testusers[11]));
$this->assertTrue(has_capability('moodle/course:create', context_coursecat::instance($testcategories[2]), $testusers[11]));
$this->assertFalse(has_capability('moodle/course:create', context_course::instance($testcourses[1]), $testusers[11]));
$this->assertTrue(has_capability('moodle/course:create', context_course::instance($testcourses[19]), $testusers[11]));
$this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[1]), $testusers[9]));
$this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[19]), $testusers[9]));
$this->assertFalse(has_capability('moodle/course:update', $systemcontext, $testusers[9]));
// Test the list of enrolled users
$coursecontext = context_course::instance($course1->id);
$enrolled = get_enrolled_users($coursecontext);
$this->assertEquals(count($enrolled), 10);
for($i=0; $i<10; $i++) {
$this->assertTrue(isset($enrolled[$testusers[$i]]));
}
$enrolled = get_enrolled_users($coursecontext, 'moodle/course:update');
$this->assertEquals(count($enrolled), 1);
$this->assertTrue(isset($enrolled[$testusers[9]]));
unset($enrolled);
// role switching
$userid = $testusers[9];
$USER = $DB->get_record('user', array('id'=>$userid));
load_all_capabilities();
$coursecontext = context_course::instance($course1->id);
$this->assertTrue(has_capability('moodle/course:update', $coursecontext));
$this->assertFalse(is_role_switched($course1->id));
role_switch($allroles['student'], $coursecontext);
$this->assertTrue(is_role_switched($course1->id));
$this->assertEquals($USER->access['rsw'][$coursecontext->path], $allroles['student']);
$this->assertFalse(has_capability('moodle/course:update', $coursecontext));
reload_all_capabilities();
$this->assertFalse(has_capability('moodle/course:update', $coursecontext));
role_switch(0, $coursecontext);
$this->assertTrue(has_capability('moodle/course:update', $coursecontext));
$userid = $adminid;
$USER = $DB->get_record('user', array('id'=>$userid));
load_all_capabilities();
$coursecontext = context_course::instance($course1->id);
$blockcontext = context_block::instance($block1->id);
$this->assertTrue(has_capability('moodle/course:update', $blockcontext));
role_switch($allroles['student'], $coursecontext);
$this->assertEquals($USER->access['rsw'][$coursecontext->path], $allroles['student']);
$this->assertFalse(has_capability('moodle/course:update', $blockcontext));
reload_all_capabilities();
$this->assertFalse(has_capability('moodle/course:update', $blockcontext));
load_all_capabilities();
$this->assertTrue(has_capability('moodle/course:update', $blockcontext));
// temp course role for enrol
$DB->delete_records('cache_flags', array()); // this prevents problem with dirty contexts immediately resetting the temp role - this is a known problem...
$userid = $testusers[5];
$roleid = $allroles['editingteacher'];
$USER = $DB->get_record('user', array('id'=>$userid));
load_all_capabilities();
$coursecontext = context_course::instance($course1->id);
$this->assertFalse(has_capability('moodle/course:update', $coursecontext));
$this->assertFalse(isset($USER->access['ra'][$coursecontext->path][$roleid]));
load_temp_course_role($coursecontext, $roleid);
示例11: login_info
/**
* Return the standard string that says whether you are logged in (and switched
* roles/logged in as another user).
* @param bool $withlinks if false, then don't include any links in the HTML produced.
* If not set, the default is the nologinlinks option from the theme config.php file,
* and if that is not set, then links are included.
* @return string HTML fragment.
*/
public function login_info($withlinks = null)
{
global $USER, $CFG, $DB, $SESSION;
if (right_to_left()) {
$direction = array('left-side' => 'right', 'right-side' => 'left');
$dir = 'right';
} else {
$direction = array('left-side' => 'left', 'right-side' => 'right');
$dir = '';
}
// Check Page layout options for links
// Obscure, but whatever...
if (is_null($withlinks)) {
$withlinks = empty($this->page->layout_options['nologinlinks']);
}
// Setup a check for if we're on the login page
$loginurl = get_login_url();
$loginpage = (string) $this->page->url === $loginurl;
$logouturl = $CFG->wwwroot . '/login/logout.php';
// This should be overridden with sesskey() info
$logouturl = $CFG->wwwroot . '/login/logout.php?sesskey=' . sesskey();
$course = $this->page->course;
if (during_initial_install() || empty($course->id)) {
// $course->id is not defined during installation
// Logins don't exist yet...
return '';
}
// Assume they're not logged in
$loggedinas = '';
// Build some general output components
// Divider
$divider = html_writer::empty_tag('li', array('class' => 'divider ' . $dir));
// Start li
$startli = html_writer::start_tag('li', array('class' => $dir));
$startdropdownli = html_writer::start_tag('li', array('class' => 'has-dropdown ' . $dir));
// End li
$endli = html_writer::end_tag('li');
// Login button
$loginbutton = $divider;
$loginbutton .= html_writer::start_tag('li', array('class' => 'has-form'));
$loginbutton .= html_writer::tag('a', get_string('login'), array('href' => $loginurl, 'class' => 'button'));
$loginbutton .= html_writer::end_tag('li');
// Logout button
$logoutbutton = html_writer::empty_tag('li', array('class' => 'divider'));
$logoutbutton .= html_writer::start_tag('li', array('class' => 'has-form'));
$logoutbutton .= html_writer::tag('a', get_string('logout'), array('href' => $logouturl, 'class' => 'button'));
$logoutbutton .= html_writer::end_tag('li');
if (!$loginpage) {
// Don't show any login info on the login page
if (isloggedin()) {
// Logged in users (MNET, guest, switched role, loggedinas, normal)
$fullname = fullname($USER, true);
$mnetuser = is_mnet_remote_user($USER) && ($idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid)));
$mnetuserpanel = '';
$roleswitched = is_role_switched($course->id);
$roleswitchedpanel = '';
$loggedinasuser = session_is_loggedinas();
$loggedinasuserpanel = '';
if ($mnetuser) {
$providerurl = $idprovider->wwwroot;
$providername = $idprovider->name;
$providerlink = html_writer::tag('a', $providername, array('href' => $providerurl));
$roleswitched || $loggedinasuser ? $mnetuserpanel .= $divider : null;
if ($withlinks) {
$mnetuserpanel .= $startli;
$mnetuserpanel .= html_writer::tag('label', get_string('yourhost', 'mnet') . ':');
$mnetuserpanel .= html_writer::tag('li', $providerlink);
$mnetuserpanel .= $endli;
} else {
$mnetuserpanel .= $startli;
$mnetuserpanel .= html_writer::tag('label', $providername);
$mnetuserpanel .= $endli;
}
}
if ($roleswitched) {
$rolename = '';
$context = context_course::instance($course->id);
if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) {
$rolename = format_string($role->name);
}
if (empty($rolename)) {
// Specially for Admins - they have no original role Title...
$rolename = get_string('admin');
}
$returnrolelinkparams = array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false));
$returnrolelink = new moodle_url('/course/switchrole.php', $returnrolelinkparams);
$returnrolelink = html_writer::tag('a', $rolename, array('href' => $returnrolelink));
// Add a divider if the user is also role switched or MNET
$mnetuser || $loggedinasuser ? $roleswitchedpanel .= $divider : null;
if ($withlinks) {
$roleswitchedpanel .= $startli;
$roleswitchedpanel .= html_writer::tag('label', get_string('switchrolereturn'));
//.........这里部分代码省略.........
示例12: get_switchable_roles
// inquiring minds want to know...
$aroles = get_switchable_roles($context);
if (is_array($aroles) && isset($aroles[$switchrole])) {
role_switch($switchrole, $context);
}
} else {
if ($switchrole < 0) {
$PAGE->set_title(get_string('switchroleto'));
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('incourse');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('switchroleto'));
// Overall criteria aggregation.
$roles = array();
$assumedrole = -1;
if (is_role_switched($course->id)) {
$roles[0] = get_string('switchrolereturn');
$assumedrole = $USER->access['rsw'][$context->path];
}
$availableroles = get_switchable_roles($context);
if (is_array($availableroles)) {
foreach ($availableroles as $key => $role) {
if ($assumedrole == (int) $key) {
continue;
}
$roles[$key] = $role;
}
}
echo $OUTPUT->box(markdown_to_html(get_string('switchroleto_help')));
foreach ($roles as $key => $role) {
$url = new moodle_url('/course/switchrole.php', array('id' => $id, 'switchrole' => $key, 'returnurl' => $returnurl));
示例13: print_settings_link
/**
* Print settings link
*
* @return string
*/
public function print_settings_link()
{
global $DB, $PAGE, $COURSE;
if (!$PAGE->blocks->is_block_present('settings')) {
return '';
}
$isteacher = has_capability('moodle/course:manageactivities', $PAGE->context);
$display = false;
$userid = optional_param('id', false, PARAM_INT);
if ($isteacher) {
$display = true;
} elseif (is_role_switched($COURSE->id)) {
// IF a teacher or admin switch their role to a student then they still need to be able to see the admin
// menu in order to be able to switch back to their original role!
$display = true;
} elseif ($PAGE->pagetype === 'user-profile') {
// The admin block needs to be shown on user profile pages as it contains the edit profile link.
$display = true;
} elseif ($PAGE->url->get_path() === '/user/view.php' && $userid && has_capability('moodle/user:viewdetails', CONTEXT_USER::instance($userid))) {
// Test to see if we have a mentor viewing this page, if so we need to display the admin block.
$display = true;
}
if (!$display) {
return '';
}
if (!($instanceid = $DB->get_field('block_instances', 'id', array('blockname' => 'settings')))) {
$msg = "Moodle appears to be missing a settings block.\n This shouldn't happen!\n Please speak to your Moodle administrator";
throw new coding_exception($msg);
}
if (!has_capability('moodle/block:view', context_block::instance($instanceid))) {
return '';
}
// User can view admin block - return the link
$admin = get_string('admin', 'theme_snap');
echo '<a id="admin-menu-trigger" class="pull-right" href="#inst' . $instanceid . '" data-toggle="tooltip" data-placement="bottom" title="' . $admin . '" >
<span class="lines"></span></a>';
}
示例14: login_info
/**
* Return the standard string that says whether you are logged in (and switched
* roles/logged in as another user).
* @param bool $withlinks if false, then don't include any links in the HTML produced.
* If not set, the default is the nologinlinks option from the theme config.php file,
* and if that is not set, then links are included.
* @return string HTML fragment.
*/
public function login_info($withlinks = null)
{
global $USER, $CFG, $DB, $SESSION;
if (during_initial_install()) {
return '';
}
if (is_null($withlinks)) {
$withlinks = empty($this->page->layout_options['nologinlinks']);
}
$loginpage = (string) $this->page->url === get_login_url();
$course = $this->page->course;
if (\core\session\manager::is_loggedinas()) {
$realuser = session_get_realuser();
$fullname = fullname($realuser, true);
if ($withlinks) {
$realuserinfo = " [<a href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&sesskey=" . sesskey() . "\">{$fullname}</a>] ";
} else {
$realuserinfo = " [{$fullname}] ";
}
} else {
$realuserinfo = '';
}
$loginurl = get_login_url();
if (empty($course->id)) {
// $course->id is not defined during installation
return '';
} else {
if (isloggedin()) {
$context = context_course::instance($course->id);
$fullname = fullname($USER, true);
// Since Moodle 2.0 this link always goes to the public profile page (not the course profile page)
if ($withlinks) {
$username = "<a href=\"{$CFG->wwwroot}/user/profile.php?id={$USER->id}\">{$fullname}</a>";
} else {
$username = $fullname;
}
if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid))) {
if ($withlinks) {
$username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>";
} else {
$username .= " from {$idprovider->name}";
}
}
if (isguestuser()) {
$loggedinas = $realuserinfo . get_string('loggedinasguest');
if (!$loginpage && $withlinks) {
$loggedinas .= '<a class="btn btn-small btn-default" href="' . $loginurl . '"><i class="fa fa-sign-in"></i> ' . get_string('login') . '</a>';
}
} else {
if (is_role_switched($course->id)) {
// Has switched roles
$rolename = '';
if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) {
$rolename = ': ' . format_string($role->name);
}
$loggedinas = get_string('loggedinas', 'moodle', $username) . $rolename;
if ($withlinks) {
$loggedinas .= ' (<a href="$CFG->wwwroot/course/view.php?id=$course->id&switchrole=0&sesskey=' . sesskey() . '">' . get_string('switchrolereturn') . '</a>)';
}
} else {
$loggedinas = $realuserinfo . get_string('loggedinas', 'moodle', $username);
if ($withlinks) {
$loggedinas .= ' <a class="btn btn-small" href="' . $CFG->wwwroot . '/login/logout.php?sesskey=' . sesskey() . '"><i class="fa fa-sign-out"></i> ' . get_string('logout') . '</a>';
}
}
}
} else {
$loggedinas = get_string('loggedinnot', 'moodle');
if (!$loginpage && $withlinks) {
$loggedinas .= ' <a class="btn btn-small btn-default" href="' . $loginurl . '"><i class="fa fa-sign-in"></i> ' . get_string('login') . '</a>';
}
}
}
$loggedinas = '<div class="logininfo">' . $loggedinas . '</div>';
if (isset($SESSION->justloggedin)) {
unset($SESSION->justloggedin);
if (!empty($CFG->displayloginfailures)) {
if (!isguestuser()) {
// Include this file only when required.
require_once $CFG->dirroot . '/user/lib.php';
if ($count = user_count_login_failures($USER)) {
$loggedinas .= '<div class="loginfailures">';
$a = new stdClass();
$a->attempts = $count;
$loggedinas .= get_string('failedloginattempts', '', $a);
if (file_exists("{$CFG->dirroot}/report/log/index.php") and has_capability('report/log:view', context_system::instance())) {
$loggedinas .= ' (' . html_writer::link(new moodle_url('/report/log/index.php', array('chooselog' => 1, 'id' => 0, 'modid' => 'site_errors')), get_string('logs')) . ')';
}
$loggedinas .= '</div>';
}
}
}
//.........这里部分代码省略.........
示例15: login_info
/**
* Return the standard string that says whether you are logged in (and switched
* roles/logged in as another user).
* @param bool $withlinks if false, then don't include any links in the HTML produced.
* If not set, the default is the nologinlinks option from the theme config.php file,
* and if that is not set, then links are included.
* @return string HTML fragment.
*/
public function login_info($withlinks = null, $asmenu = false)
{
global $USER, $CFG, $DB, $SESSION;
if (during_initial_install()) {
return '';
}
if (is_null($withlinks)) {
$withlinks = empty($this->page->layout_options['nologinlinks']);
}
$loginpage = (string) $this->page->url === get_login_url();
$course = $this->page->course;
if (\core\session\manager::is_loggedinas()) {
$realuser = \core\session\manager::get_realuser();
$fullname = fullname($realuser, true);
if ($withlinks) {
$loginastitle = get_string('loginas');
$realuserinfo = " <small>[</small><a href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&sesskey=" . sesskey() . "\"";
$realuserinfo .= "title =\"" . $loginastitle . "\">{$fullname}</a> <small>]</small> ";
} else {
$realuserinfo = " <small>[</small> {$fullname} <small>]</small> ";
}
} else {
$realuserinfo = '';
}
$loginurl = get_login_url();
$loggedinas = '';
if (empty($course->id)) {
// The $course->id is not defined during installation.
return '';
} else {
if (isloggedin()) {
$context = context_course::instance($course->id);
$fullname = fullname($USER, true);
$linktitle = get_string('viewprofile');
$userpicture = '';
if (!empty($USER->id)) {
$userpicture = $this->user_picture($USER, array('size' => 35, 'link' => false, 'class' => 'nav_userpicture'));
}
$username = "<a href=\"{$CFG->wwwroot}/user/profile.php?id={$USER->id}\" " . "title=\"{$linktitle}\" class='userloginprofile'>{$userpicture}{$fullname}</a>";
if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id' => $USER->mnethostid))) {
$username .= " from <a href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>";
}
$loggedinas = $username;
if (isguestuser()) {
$loggedinas = $realuserinfo . get_string('loggedinasguest');
if (!$loginpage) {
$loggedinas .= " <small>(</small> <a href=\"{$loginurl}\">" . get_string('login') . '</a> <small>)</small>';
}
} else {
if (is_role_switched($course->id)) {
// Has switched roles.
$rolename = '';
if ($role = $DB->get_record('role', array('id' => $USER->access['rsw'][$context->path]))) {
$rolename = '<span class="role-name">: ' . role_get_name($role, $context) . '</span>';
}
$loggedinas .= $rolename;
$url = new moodle_url('/course/switchrole.php', array('id' => $course->id, 'sesskey' => sesskey(), 'switchrole' => 0, 'returnurl' => $this->page->url->out_as_local_url(false)));
$loggedinas .= html_writer::tag('a', get_string('switchrolereturn'), array('href' => $url));
} else {
$loggedinas .= " <small>(</small> <a href=\"{$CFG->wwwroot}/login/logout.php?sesskey=" . sesskey() . "\">" . get_string('logout') . '</a> <small>)</small>';
}
}
} else {
if (!$loginpage) {
$loggedinas = " <small>(</small> <a href=\"{$loginurl}\">" . get_string('login') . '</a> <small>)</small>';
}
}
}
$loggedinas = '<div class="logininfo">' . $loggedinas . '</div>';
if (isset($SESSION->justloggedin)) {
unset($SESSION->justloggedin);
if (!empty($CFG->displayloginfailures)) {
if (!isguestuser()) {
if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) {
$loggedinas .= ' <div class="loginfailures">';
if (empty($count->accounts)) {
$loggedinas .= get_string('failedloginattempts', '', $count);
} else {
$loggedinas .= get_string('failedloginattemptsall', '', $count);
}
if (file_exists("{$CFG->dirroot}/report/log/index.php") and has_capability('report/log:view', context_system::instance())) {
$loggedinas .= ' <a href="' . $CFG->wwwroot . '/report/log/index.php' . '?chooselog=1&id=1&modid=site_errors">' . get_string('logs') . '</a>';
}
$loggedinas .= '</div>';
}
}
}
}
return $loggedinas;
}