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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sat, 05 Jul 2008 23:21:26 -0000


This is a MIME encoded message

--raeburn1215300086
Content-Type: text/plain

raeburn		Sat Jul  5 19:21:26 2008 EDT

  Modified files:              
    /loncom/interface	lonmenu.pm 
  Log:
  - Inline role switcher now includes sections.
  - Two cases:
  
  (a) User is a CC
  - For any roles with active sections, javascript pop-up requires section (or blank for no section) to be entered.
  
  (b) User is not a CCa
  - Sections are accumulated (including "No section" case) for each active role/section current user has.
  - If user has roles for more than 1 section for a particular role, javascript popup requires section (or blank if user has "no section" for the role) to be entered.
  - If user has only one section, new role defaults to that section.
  
  
--raeburn1215300086
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080705192126.txt"

Index: loncom/interface/lonmenu.pm
diff -u loncom/interface/lonmenu.pm:1.238 loncom/interface/lonmenu.pm:1.239
--- loncom/interface/lonmenu.pm:1.238	Thu Jun 12 21:07:50 2008
+++ loncom/interface/lonmenu.pm	Sat Jul  5 19:21:23 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.238 2008/06/13 01:07:50 www Exp $
+# $Id: lonmenu.pm,v 1.239 2008/07/05 23:21:23 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1427,7 +1427,7 @@
 sub roles_selector {
     my ($cdom,$cnum) = @_;
     my $now = time;
-    my %courseroles;
+    my (%courseroles,%seccount,%gotnosection);
     my $is_cc;
     my $role_selector;
     if ($env{'user.role.cc./'.$cdom.'/'.$cnum}) {
@@ -1444,34 +1444,66 @@
     if ($is_cc) {
         my %adv_roles =
              &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
-        foreach my $role (keys(%adv_roles),'st') {
+        foreach my $role (keys(%adv_roles)) {
             my ($urole,$usec) = split(/:/,$role);
-            @{$courseroles{$urole}} = 'none';
+            if (!$gotnosection{$urole}) {
+                $seccount{$urole} ++;
+                $gotnosection{$urole} = 1;
+            }
+            if (ref($courseroles{$urole}) eq 'ARRAY') {
+                if ($usec ne '') {
+                    if (!grep(/^Q$usec\E$/,@{$courseroles{$urole}})) {
+                        push(@{$courseroles{$urole}},$usec);
+                        $seccount{$urole} ++;
+                    }
+                }
+            } else {
+                @{$courseroles{$urole}} = ();
+                if ($usec ne '') {
+                    $seccount{$urole} ++;
+                    push(@{$courseroles{$urole}},$usec);
+                }
+            }
+        }
+        my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
+        @{$courseroles{'st'}} = ();
+        if (keys(%sections_count) > 0) {
+            push(@{$courseroles{'st'}},keys(%sections_count));
         }
     } else {
         foreach my $item (keys(%env)) {
-            if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E(/?\w*)$-) {
+            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 (!$gotnosection{$role}) {
+                        $seccount{$role} ++;
+                        $gotnosection{$role} = 1;
+                    }
                 }
                 if (ref($courseroles{$role}) eq 'ARRAY') {
-                    if (!grep(/^Q$sec\E$/,@{$courseroles{$role}})) {
-                        push(@{$courseroles{$role}},$sec);
+                    if ($sec ne '') {
+                        if (!grep(/^Q$sec\E$/,@{$courseroles{$role}})) {
+                            push(@{$courseroles{$role}},$sec);
+                            $seccount{$role} ++;
+                        }
                     }
                 } else {
-                    @{$courseroles{$role}} = ($sec);
+                    @{$courseroles{$role}} = ();
+                    if ($sec ne '') {
+                        $seccount{$role} ++;
+                        push(@{$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 = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles);
         $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>';
@@ -1497,23 +1529,95 @@
 }
 
 sub jump_to_role {
-    my ($cdom,$cnum) = @_;
+    my ($cdom,$cnum,$seccount,$courseroles) = @_;
+    my %lt = &Apache::lonlocal::texthash(
+                this => 'This role has section(s) associated with it.',
+                ente => 'Enter a specific section.',
+                orlb => 'Enter a specific section, or leave blank for no section.',
+                avai => 'Available sections are:',
+                youe => 'You entered an invalid section choice:',
+                plst => 'Please try again',
+    );
+    my $js;
+    if (ref($courseroles) eq 'HASH') {
+        $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
+              '    var numsec = new Array();'."\n".
+              '    var rolesections = new Array();'."\n".
+              '    var rolenames = new Array();'."\n".
+              '    var roleseclist = new Array();'."\n";
+        my @items = keys(%{$courseroles});
+        for (my $i=0; $i<@items; $i++) {
+            $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
+            my ($secs,$secstr);
+            if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
+                my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
+                $secs = join('","',@sections);
+                $secstr = join(', ',@sections);
+            }
+            $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
+                   '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
+                   '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
+        }
+    }
     my $output = <<"END";
 <script type="text/javascript">
 function adhocRole(roleitem) {
+    $js
     var newrole =  document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value;
     if (newrole == '') {
         return; 
     } 
-    newrole += './$cdom/$cnum';
-    if (newrole == "$env{'request.role'}") {
+    var fullrole = newrole+'./$cdom/$cnum';
+    var selidx = '';
+    for (var i=0; i<rolenames.length; i++) {
+        if (rolenames[i] == newrole) {
+            selidx = i;
+        }
+    }
+    var secok = 1;
+    var secchoice = '';
+    if (selidx >= 0) {
+        if (numsec[selidx] > 1) {
+            secok = 0;
+            var numrolesec = rolesections[selidx].length;
+            var msgidx = numsec[selidx] - numrolesec;
+            secchoice = prompt("$lt{'this'}\\n"+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
+            if (secchoice == '') {
+                if (msgidx > 0) {
+                    secok = 1;
+                }
+            } else {
+                for (var j=0; j<rolesections[selidx].length; j++) {
+                    if (rolesections[selidx][j] == secchoice) {
+                        secok = 1;
+                    }
+                }
+            }
+        } else {
+            if (rolesections[selidx].length == 1) {
+                secchoice = rolesections[selidx][0];
+            }
+        }
+    }
+    if (secok == 1) {
+        if (secchoice != '') {
+            fullrole += '/'+secchoice;
+        }
+    } else {
+        document.rolechooser.elements[roleitem].selectedIndex = 0;
+        if (secchoice != null) {
+            alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
+        }
+        return;
+    }
+    if (fullrole == "$env{'request.role'}") {
         return;
     }
     itemid = retrieveIndex('gotorole');
     if (itemid != -1) {
-        document.rolechooser.elements[itemid].name = newrole;
+        document.rolechooser.elements[itemid].name = fullrole;
     }
-    document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = newrole;
+    document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = fullrole;
     document.rolechooser.selectrole.value = '1';
     document.rolechooser.submit();
     return;

--raeburn1215300086--