Skip to content

Commit

Permalink
New "Switch Role" functionality to replace the old "Student view".
Browse files Browse the repository at this point in the history
It's not well-tested yet and probably doesn't quite work.  I will keep debugging this tonight from home.
  • Loading branch information
moodler committed Sep 21, 2006
1 parent 01e52ac commit 3a52e76
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
12 changes: 12 additions & 0 deletions course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
$section = optional_param('section', 0, PARAM_INT);
$move = optional_param('move', 0, PARAM_INT);
$marker = optional_param('marker',-1 , PARAM_INT);
$switchrole = optional_param('switchrole',-1, PARAM_INT);



if (empty($id) && empty($name) && empty($idnumber)) {
Expand All @@ -35,8 +37,17 @@
}
}

if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
print_error('nocontext');
}

require_login($course->id);

if ($switchrole > -1) {
role_switch($switchrole, $context);
require_login($course->id);
}

//If course is hosted on an external server, redirect to corresponding
//url with appropriate authentication attached as parameter
if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
Expand All @@ -48,6 +59,7 @@
}
}


require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER

add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
Expand Down
5 changes: 2 additions & 3 deletions lang/en_utf8/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@
$string['includeuserfiles'] = 'Include User Files';
$string['info'] = 'Information';
$string['institution'] = 'Institution';
$string['instudentview'] = 'in student view';
$string['invalidemail'] = 'Invalid email address';
$string['invalidlogin'] = 'Invalid login, please try again';
$string['ip_address'] = 'IP Address';
Expand Down Expand Up @@ -1274,13 +1273,13 @@
$string['studentnotallowed'] = 'Sorry, but you can not enter this course as \'$a\'';
$string['students'] = 'Students';
$string['studentsandteachers'] = 'Students and teachers';
$string['studentviewoff'] = 'Turn student view off';
$string['studentviewon'] = 'Turn student view on';
$string['subcategories'] = 'Sub-categories';
$string['success'] = 'Success';
$string['summary'] = 'Summary';
$string['summaryof'] = 'Summary of $a';
$string['supplyinfo'] = 'Please supply some information about yourself';
$string['switchrolereturn'] = 'Return to my normal role';
$string['switchroleto'] = 'Switch role to...';
$string['tag'] = 'Tag';
$string['tagmanagement'] = 'Add/delete tags ...';
$string['tags'] = 'Tags';
Expand Down
59 changes: 58 additions & 1 deletion lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,62 @@ function get_roles_on_exact_context($context) {

}

/*
* Switches the current user to another role for the current session and only
* in the given context. If roleid is not valid (eg 0) or the current user
* doesn't have permissions to be switching roles then the user's session
* is compltely reset to have their normal roles.
* @param integer $roleid
* @param object $context
* @return bool
*/
function role_switch($roleid, $context) {
global $USER;

global $db;

/// If we can't use this or are already using it or no role was specified then bail completely and reset
if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
|| !empty($USER->switchrole) || !confirm_sesskey()) {
load_user_capability(); // Reset all permissions to normal
unset($USER->switchrole); // Delete old capabilities
return true;
}

/// We're allowed to switch but can we switch to the specified role? Use assignable roles to check.
if (!$roles = get_assignable_roles($context)) {
return false;
}

if (empty($roles[$roleid])) { /// We can't switch to this particular role
return false;
}

if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) {
return false;
}

/// We have a valid roleid that this user can switch to, so let's set up the session

$USER->switchrole = $roleid; // So we know later what state we are in

unset($USER->capabilities[$context->id]); // Delete old capabilities

if ($capabilities = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $sitecontext->id")) {
foreach ($capabilities as $capability) {
$USER->capabilities[$context->id][$capability->capability] = $capability->permission;
}
}

/// Add some capabilities we are really going to always need, even if the role doesn't have them!

$USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW;

return true;

}


// get any role that has an override on exact context
function get_roles_with_override_on_context($context) {

Expand Down Expand Up @@ -2668,4 +2724,5 @@ function get_users_from_role_on_context($role, $context) {
WHERE contextid = $context->id
AND roleid = $role->id");
}
?>

?>
2 changes: 1 addition & 1 deletion lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ function print_header($title, $morebreadcrumbs = NULL) {

// The "Editing On" button will be appearing only in the "main" course screen
// (i.e., no breadcrumbs other than the default one added inside this function)
$buttons = update_course_icon($this->courserecord->id );
$buttons = switchroles_form($this->courserecord->id) . update_course_icon($this->courserecord->id );
$buttons = empty($morebreadcrumbs) ? $buttons : ' ';

print_header($title, $this->courserecord->fullname, $crumbtext,
Expand Down
42 changes: 41 additions & 1 deletion lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3712,6 +3712,46 @@ function update_course_icon($courseid) {
}
}

/**
* 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 (has_capability('moodle/role:switchroles', $context)) {
if (empty($USER->switchrole)) { // Print a menu
if (!$roles = get_assignable_roles($context)) {
return ''; // Nothing to show!
}

return popup_form($CFG->wwwroot.'/course/view.php?id='.$courseid.'&sesskey='.sesskey().'&switchrole=',
$roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', '', true);

} else { // 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);
}
}

return '';
}


/**
* Returns a turn edit on/off button for course in a self contained form.
Expand Down Expand Up @@ -5284,4 +5324,4 @@ function debugging($message='', $level=E_NOTICE) {
}

// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
?>
?>

0 comments on commit 3a52e76

Please sign in to comment.