Ejemplo n.º 1
0
/**
 * Returns a little popup menu for switching roles
 *
 * @uses $CFG
 * @uses $USER
 * @param int $courseid The course  to update by id as found in 'course' table
 * @return string
 */
function switchroles_form($courseid)
{
    global $CFG, $USER;
    if (!($context = get_context_instance(CONTEXT_COURSE, $courseid))) {
        return '';
    }
    if (!empty($USER->access['rsw'][$context->path])) {
        // Just a button to return to normal
        $options = array();
        $options['id'] = $courseid;
        $options['sesskey'] = sesskey();
        $options['switchrole'] = 0;
        return print_single_button($CFG->wwwroot . '/course/view.php', $options, get_string('switchrolereturn'), 'post', '_self', true);
    }
    if (has_capability('moodle/role:switchroles', $context)) {
        if (!($roles = get_assignable_roles_for_switchrole($context))) {
            return '';
            // Nothing to show!
        }
        // unset default user role - it would not work
        unset($roles[$CFG->guestroleid]);
        return popup_form($CFG->wwwroot . '/course/view.php?id=' . $courseid . '&sesskey=' . sesskey() . '&switchrole=', $roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', get_string('switchroleto'), true);
    }
    return '';
}
Ejemplo n.º 2
0
}
preload_course_contexts($course->id);
if (!($context = get_context_instance(CONTEXT_COURSE, $course->id))) {
    print_error('nocontext');
}
// 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_assignable_roles_for_switchrole($context);
    if (is_array($aroles) && isset($aroles[$switchrole])) {
        role_switch($switchrole, $context);
        // Double check that this role is allowed here
        require_login($course->id);
    }
    // reset course page state - this prevents some weird problems ;-)
    $USER->activitycopy = false;
    $USER->activitycopycourse = NULL;
    unset($USER->activitycopyname);
    unset($SESSION->modform);
    $USER->editing = 0;
    $reset_user_allowed_editing = true;
}
//If course is hosted on an external server, redirect to corresponding
//url with appropriate authentication attached as parameter