[LON-CAPA-cvs] cvs: loncom /auth lonroles.pm /interface lonmenu.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 10 Mar 2008 23:47:36 -0000


This is a MIME encoded message

--raeburn1205192856
Content-Type: text/plain

raeburn		Mon Mar 10 19:47:36 2008 EDT

  Modified files:              
    /loncom/auth	lonroles.pm 
    /loncom/interface	lonmenu.pm 
  Log:
  - Improving ease of use (cf. Moodle).
  
  Inline navigation includes dropdown to change to a different role in the course.
  Course Coordinator - can switch to any role currently used in the course.
  Other users - can switch to any other course roles assigned to them.
  
  Work in progress.
  - Needs additional dropdown for section context.
  - Needs to maintain display of encrypted URLs as unencrypted when switching into CC role.   
  
  
--raeburn1205192856
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080310194736.txt"

Index: loncom/auth/lonroles.pm
diff -u loncom/auth/lonroles.pm:1.184 loncom/auth/lonroles.pm:1.185
--- loncom/auth/lonroles.pm:1.184	Tue Mar  4 17:44:21 2008
+++ loncom/auth/lonroles.pm	Mon Mar 10 19:47:24 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # User Roles Screen
 #
-# $Id: lonroles.pm,v 1.184 2008/03/04 22:44:21 raeburn Exp $
+# $Id: lonroles.pm,v 1.185 2008/03/10 23:47:24 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -110,6 +110,12 @@
             $env{'form.'.$env{'form.newrole'}}=1;
 	}
 	if ($env{'request.course.id'}) {
+            # Check if user is CC trying to select a course role
+            if ($env{'form.switchrole'}) {
+                if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
+                    &adhoc_course_role($then);
+                }
+            }
 	    my %temp=('logout_'.$env{'request.course.id'} => time);
 	    &Apache::lonnet::put('email_status',\%temp);
 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
@@ -121,7 +127,6 @@
 				"request.role"        => 'cm',
                                 "request.role.adv"    => $env{'user.adv'},
 				"request.role.domain" => $env{'user.domain'});
-
 # Check if user is a DC trying to enter a course or author space and needs privs to be created
         if ($numdc > 0) {
             foreach my $envkey (keys %env) {
@@ -319,6 +324,14 @@
 				    $furl = "/adm/helper/course.initialization.helper";
 				    # Send the user to the course they selected
 				} elsif ($env{'request.course.id'}) {
+                                    if ($env{'form.destinationurl'}) {
+                                        my $dest = $env{'form.destinationurl'};
+                                        &redirect_user($r,&mt('Entering [_1]',
+                                                      $env{'course.'.$courseid.'.description'}),
+                                               $dest,$msg,
+                                               $env{'environment.remotenavmap'});
+                                        return OK;
+                                    }
 				    if (&Apache::lonnet::allowed('whn',
 								 $env{'request.course.id'})
 					|| &Apache::lonnet::allowed('whn',
@@ -956,6 +969,69 @@
     return $numdc;
 }
 
+sub adhoc_course_role {
+    my ($then) = @_; 
+    my ($cdom,$cnum);
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    if (&check_forcc($cdom,$cnum,$then)) {
+        my $setprivs;
+        if (!defined($env{'user.role.'.$env{'form.selectrole'}})) {
+            $setprivs = 1;
+        } else {
+            my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.selectrole'}});
+            if (($start && ($start>$then || $start == -1)) ||
+                ($end && $end<$then)) {
+                $setprivs = 1;
+            }
+        } 
+        if ($setprivs) {
+            if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E(/?\w*)$-) {
+                my $role = $1;
+                my $custom_role = $2;
+                my $usec = $3;
+                if ($role eq 'cr') {
+                    if ($custom_role =~ m-^$match_domain/$match_courseid/\w+$-) {
+                        $role .= $custom_role;
+                    } else {
+                        return;
+                    }
+                }
+                my (%userroles,%newrole,%newgroups);
+                my $area = '/'.$cdom.'/'.$cnum;
+                my $spec = $role.'.'.$area;
+                if ($usec ne '') {
+                    $spec .= '/'.$usec;
+                    $area .= '/'.$usec;
+                }
+                &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
+                &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
+                my $adhocstart = $then-1;
+                $userroles{'user.role.'.$spec} = $adhocstart.'.';
+                &Apache::lonnet::appenv(%userroles);
+            }
+        }
+    }
+    return;
+}
+
+sub check_forcc {
+    my ($cdom,$cnum,$then) = @_;
+    my $is_cc;
+    if ($cdom ne '' && $cnum ne '') {
+        if (&Apache::lonnet::is_course($cdom,$cnum)) {
+            my $envkey = 'user.role.cc./'.$cdom.'/'.$cnum;
+            if (defined($env{$envkey})) {
+                $is_cc = 1;
+                my ($tstart,$tend)=split(/\./,$env{$envkey});
+                if ($tstart && $tstart>$then) { $is_cc = 0; }
+                if ($tend   && $tend  <$then) { $is_cc = 0; }
+            }
+        }
+    }
+    return $is_cc;
+}
+
 sub courselink {
     my ($dcdom,$rowtype,$selecttype) = @_;
     my $courseform=&Apache::loncommon::selectcourse_link
Index: loncom/interface/lonmenu.pm
diff -u loncom/interface/lonmenu.pm:1.234 loncom/interface/lonmenu.pm:1.235
--- loncom/interface/lonmenu.pm:1.234	Sun Feb  3 00:07:58 2008
+++ loncom/interface/lonmenu.pm	Mon Mar 10 19:47:35 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.234 2008/02/03 05:07:58 raeburn Exp $
+# $Id: lonmenu.pm,v 1.235 2008/03/10 23:47:35 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -74,6 +74,7 @@
 				       'main' => 'Main Menu',
                                        'roles' => (&show_course()?
                                                     'Courses':'Roles'),
+                                       'other' => 'Other Roles',
                                        'docs' => 'Edit Course',
                                        'exit' => 'Exit',
                                        'login' => 'Log In',
@@ -101,7 +102,10 @@
     my $reloadlink='';
     my $docs='';
     my $groups='';
+    my $roles='<a href="/adm/roles" target="_top">'.$lt{'roles'}.'</a>';
+    my $role_selector;
     my $showgroups=0;
+    my ($cnum,$cdom);
     my $escurl=&escape(&Apache::lonenc::check_encrypt($env{'request.noversionuri'}));
     my $escsymb=&escape(&Apache::lonenc::check_encrypt($env{'request.symb'}));
 
@@ -117,15 +121,21 @@
         }
     }
     if ($env{'request.course.id'}) {
+        $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
         my %coursegroups;
         my $viewgrps_permission =
 	    &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
         if (!$viewgrps_permission) {
-            %coursegroups = &Apache::lonnet::get_active_groups($env{'user.domain'},$env{'user.name'},$env{'course.'.$env{'request.course.id'}.'.domain'}, $env{'course.'.$env{'request.course.id'}.'.num'});
+            %coursegroups = &Apache::lonnet::get_active_groups($env{'user.domain'},$env{'user.name'},$cdom,$cnum);
 	}
         if ((keys(%coursegroups) > 0) || ($viewgrps_permission)) {
             $showgroups = 1;
         }
+        $role_selector = &roles_selector($cdom,$cnum);
+        if ($role_selector) {
+            $roles = '<span class="LC_nobreak">'.$role_selector.'&nbsp;&nbsp;<a href="/adm/roles" target="_top">'.$lt{'other'}.'</a></span>';
+        }
     }
 
     if ($env{'browser.interface'} eq 'textual') {
@@ -160,8 +170,7 @@
 </script>
 <div id="LC_top_nav">
 <a href="/adm/menu" target="_top">$lt{'main'}</a>
-$reloadlink $navmaps $docs $groups
-<a href="/adm/roles" target="_top">$lt{'roles'}</a>
+$reloadlink $navmaps $docs $groups $roles
 <a href="/adm/logout" target="_top">$lt{'exit'}</a>
 </div>
 <br />
@@ -197,6 +206,7 @@
 </font>
 ENDINLINEMENU
         }
+        $roles = '<td><a href="/adm/roles" target="_top">'.$lt{'roles'}.'</a></td>';
 # Do we have a NAV link?
         if ($env{'request.course.id'}) {
 	    my $link='/adm/navmaps?postdata='.$escurl.'&amp;postsymb='.
@@ -225,6 +235,9 @@
 <td><a href="/adm/flip?postdata=$escreload" target="_top">$lt{'ret'}</a></td>
 ENDRELOAD
             }
+            if ($role_selector) {
+                $roles = '<td>'.$role_selector.'</td><td><a href="/adm/roles" target="_top">'.$lt{'other'}.'</a></td>';
+            }
         }
 	if (($env{'request.state'} eq 'construct') && ($env{'request.course.id'})) {
 	    my $escreload=&escape('return:');
@@ -260,7 +273,7 @@
 $docs
 $groups
 $remote
-<td><a href="/adm/roles" target="_top">$lt{'roles'}</a></td>
+$roles
 <td class="LC_top_nav_help">$helplink</td>
 <td class="LC_top_nav_exit"><a href="/adm/logout" target="_top">$lt{'exit'}</a></td>
 </tr>
@@ -1410,6 +1423,117 @@
     return $buttonshide; 
 }
 
+sub roles_selector {
+    my ($cdom,$cnum) = @_;
+    my $now = time;
+    my %courseroles;
+    my $is_cc;
+    my $role_selector;
+    if ($env{'user.role.cc./'.$cdom.'/'.$cnum}) {
+        my ($start,$end) = split(/\./,$env{'user.role.cc./'.$cdom.'/'.$cnum});
+        
+        if ((($start) && ($start<0)) || 
+            (($end) && ($end<$now))  ||
+            (($start) && ($now<$start))) {
+            $is_cc = 0;
+        } else {
+            $is_cc = 1;
+        }
+    }
+    if ($is_cc) {
+        my %adv_roles =
+             &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
+        foreach my $role (keys(%adv_roles),'st') {
+            my ($urole,$usec) = split(/:/,$role);
+            @{$courseroles{$urole}} = 'none';
+        }
+    } else {
+        foreach my $item (keys(%env)) {
+            if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E(/?\w*)$-) {
+                my $role = $1;
+                my $sec = $2;
+                next if ($role eq 'gr');
+                my ($start,$end) = split(/\./,$env{$item});
+                next if (($start && $start > $now) || ($end && $end < $now));
+                if ($sec eq '') {
+                    $sec = 'none';
+                }
+                if (ref($courseroles{$role}) eq 'ARRAY') {
+                    if (!grep(/^Q$sec\E$/,@{$courseroles{$role}})) {
+                        push(@{$courseroles{$role}},$sec);
+                    }
+                } else {
+                    @{$courseroles{$role}} = ($sec);
+                }
+            }
+        }
+    }
+    my @roles_order = ('cc','in','ta','ep','ad','st');
+    if (keys(%courseroles) > 1) {
+        $role_selector = &jump_to_role($cdom,$cnum);
+        $role_selector .= '<form name="rolechooser" method="post" action="/adm/roles">
+                          <select name="switchrole" onchange="javascript:adhocRole('."'switchrole'".')">';
+        $role_selector .= '<option value="">'.&mt('Switch course role to..').'</option>';
+        foreach my $role (@roles_order) {
+            if (defined($courseroles{$role})) {
+                $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role).'</option>'; 
+            }
+        }
+        foreach my $role (sort(keys(%courseroles))) {
+            if ($role =~ /^cr/) {
+                $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role).'</option>'; 
+            }
+        }
+        $role_selector .= '</select>'."\n".
+               '<input type="hidden" name="destinationurl" value="'.
+               $ENV{'REQUEST_URI'}.'" />'."\n".
+               '<input type="hidden" name="gotorole" value="1" />'."\n".
+               '<input type="hidden" name="selectrole" value="" />'."\n".
+               '<input type="hidden" name="switch" value="1" />'."\n".
+               '</form>';
+    }
+    return $role_selector;
+}
+
+sub jump_to_role {
+    my ($cdom,$cnum) = @_;
+    my $output = <<"END";
+<script type="text/javascript">
+function adhocRole(roleitem) {
+    var newrole =  document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value;
+    if (newrole == '') {
+        return; 
+    } 
+    newrole += './$cdom/$cnum';
+    if (newrole == "$env{'request.role'}") {
+        return;
+    }
+    itemid = retrieveIndex('gotorole');
+    if (itemid != -1) {
+        document.rolechooser.elements[itemid].name = newrole;
+    }
+    document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = newrole;
+    document.rolechooser.selectrole.value = '1';
+    document.rolechooser.submit();
+    return;
+}
+
+function retrieveIndex(item) {
+    for (var i=0;i<document.rolechooser.elements.length;i++) {
+        if (document.rolechooser.elements[i].name == item) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+
+</script>
+END
+    return $output;
+}
+
+
 # ================================================================ Main Program
 
 BEGIN {

--raeburn1205192856--