From: moodler Date: Thu, 21 Sep 2006 09:16:41 +0000 (+0000) Subject: New "Switch Role" functionality to replace the old "Student view". X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=3a52e764e41f57351535752cd26504a3f96ab402;p=moodle.git New "Switch Role" functionality to replace the old "Student view". It's not well-tested yet and probably doesn't quite work. I will keep debugging this tonight from home. --- diff --git a/course/view.php b/course/view.php index e28746000e..7d85ba9990 100644 --- a/course/view.php +++ b/course/view.php @@ -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)) { @@ -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')) { @@ -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"); diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index c4dce66d42..794afce741 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -706,7 +706,6 @@ $string['includenoneusers'] = 'Include No Users'; $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'; @@ -1274,13 +1273,13 @@ $string['stringsnotset'] = 'The following strings are not defined in $a'; $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'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 78428933d2..1e5cf4a482 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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) { @@ -2668,4 +2724,5 @@ function get_users_from_role_on_context($role, $context) { WHERE contextid = $context->id AND roleid = $role->id"); } -?> \ No newline at end of file + +?> diff --git a/lib/pagelib.php b/lib/pagelib.php index e116c8fb09..fcf059d235 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -402,7 +402,7 @@ class page_course extends page_base { // 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, diff --git a/lib/weblib.php b/lib/weblib.php index 8b8d6b10a9..f918adca3d 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -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. @@ -5284,4 +5324,4 @@ function debugging($message='', $level=E_NOTICE) { } // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: -?> \ No newline at end of file +?>