]> git.mjollnir.org Git - moodle.git/commitdiff
New "Switch Role" functionality to replace the old "Student view".
authormoodler <moodler>
Thu, 21 Sep 2006 09:16:41 +0000 (09:16 +0000)
committermoodler <moodler>
Thu, 21 Sep 2006 09:16:41 +0000 (09:16 +0000)
It's not well-tested yet and probably doesn't quite work.  I will keep debugging this tonight from home.

course/view.php
lang/en_utf8/moodle.php
lib/accesslib.php
lib/pagelib.php
lib/weblib.php

index e28746000e2d375495aab0f7d42838bee2b6d8d5..7d85ba99900fa799c3216d9c1113a69ab075cb1c 100644 (file)
@@ -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)) {
         }
     }
 
+    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");
index c4dce66d4257b04f08848c8a49ef60ec7f57dd75..794afce7417a972d74ea921166ad63389e892600 100644 (file)
@@ -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';
index 78428933d2901d393819a07e6486ad4a9103352a..1e5cf4a482e36d96789d39c90cc8b6846a75afc2 100755 (executable)
@@ -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
+
+?>
index e116c8fb0907855a569533304ed7d392ebcf27bb..fcf059d2359f8f08bc0c6ab6bdeb0b69b83cee39 100644 (file)
@@ -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 : '&nbsp;';
 
         print_header($title, $this->courserecord->fullname, $crumbtext,
index 8b8d6b10a914df5e57d04f5bc3dc540b496af6db..f918adca3da9e2ff9ef893450e90a630b83702e0 100644 (file)
@@ -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
+?>