--- loncom/interface/lonmenu.pm	2009/07/21 14:36:08	1.276
+++ loncom/interface/lonmenu.pm	2009/10/04 17:47:21	1.283
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.276 2009/07/21 14:36:08 droeschl Exp $
+# $Id: lonmenu.pm,v 1.283 2009/10/04 17:47:21 droeschl Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -137,19 +137,140 @@ use Apache::loncommon();
 use Apache::lonenc();
 use Apache::lonlocal;
 use LONCAPA qw(:DEFAULT :match);
+use HTML::Entities();
 
-use vars qw(@desklines %category_names %category_members %category_positions $readdesk);
+use vars qw(@desklines %category_names %category_members %category_positions 
+            $readdesk @primary_menu @secondary_menu);
 
 
 my @inlineremote;
 
+sub prep_menuitem {
+    my $menuitem = shift;
+    return '' unless ref $menuitem eq 'ARRAY';
+    my $link;
+    if ($$menuitem[1]) { # graphical Link
+        $link = "<img class=\"LC_noBorder\""
+              . " src=\"" . Apache::loncommon::lonhttpdurl($$menuitem[1]) . "\""
+              . " alt=\"" . mt($$menuitem[2]) . "\" />";
+    } else {             # textual Link
+        $link = mt($$menuitem[3]);
+    }     
+    return "<li><a href=\"javascript:$$menuitem[0]\">$link</a></li>";
+}
+
+# primary_menu() evaluates @primary_menu and returns XHTML for the menu
+# that contains following links:
+# About, Message, Roles, Help, Logout
+# @primary_menu is filled within the BEGIN block of this module with 
+# entries from mydesk.tab
+sub primary_menu {
+    my $menu;
+    # each element of @primary contains following array:
+    # (link url, icon path, alt text, link text, condition)
+    foreach my $menuitem (@primary_menu) {
+        # evaluate conditions 
+        next if    $$menuitem[4]        eq 'nonewmsg'  # show links depending on
+                && Apache::lonmsg::mynewmail();        # whether a new msg 
+        next if    $$menuitem[4]        eq 'newmsg'    # arrived or not
+                && !Apache::lonmsg::mynewmail();       # 
+        next if    $$menuitem[4]        !~ /public/    ##we've a public user, 
+                && $env{'user.name'}    eq 'public'    ##who should not see all 
+                && $env{'user.domain'}  eq 'public';   ##links
+        next if    $$menuitem[4]        eq 'onlypublic'# hide links which are 
+                && $env{'user.name'}    ne 'public'    # only visible to public
+                && $env{'user.domain'}  ne 'public';   # users
+        next if    $$menuitem[4]        eq 'roles'     ##show links depending on
+                && Apache::loncommon::show_course();   ##term 'Courses' or 
+        next if    $$menuitem[4]        eq 'courses'   ##'Roles' wanted
+                && !Apache::loncommon::show_course();  ##
+
+
+        if ($$menuitem[3] eq 'Help') { # special treatment for helplink
+            $menu .= Apache::loncommon::top_nav_help('Help');
+        } else {
+            $menu .= prep_menuitem(\@$menuitem);
+        }
+     }
+
+     return "<ol class=\"LC_smallMenu LC_right\">$menu</ol>";
+}
+
+
+sub secondary_menu {
+    my $menu;
+
+    foreach my $menuitem (@secondary_menu) {
+        # evaluate conditions 
+        next if    $$menuitem[4]   ne 'always'
+                && !$env{'request.course.id'};
+        next if    $$menuitem[4]   eq 'showreturn'
+                && !show_return_link() 
+                && !($env{'request.state'} eq 'construct');
+        next if    $$menuitem[4]   =~ /^mdc/
+                && !Apache::lonnet::allowed('mdc', $env{'request.course.id'});
+        next if    $$menuitem[4]   =~ /nogrp$/
+                && Apache::loncommon::course_type() eq 'Group';
+        next if    $$menuitem[4]   =~ /group$/
+                && Apache::loncommon::course_type() ne 'Group';
+        next if    $$menuitem[4]   eq 'remotenav'
+                && $env{'environment.remotenavmap'} ne 'on';
+        next if    $$menuitem[4]   eq 'noremotenav'
+                && $env{'environment.remotenavmap'} eq 'on';
+        next if    $$menuitem[4]   =~ /showgroups$/
+                && !Apache::lonnet::allowed('vcg', $env{'request.course.id'}
+                   . ($env{'request.course.sec'} ? "/$env{'request.course.sec'}"
+                                                 : ''))
+                && !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'});
+
+
+        if($$menuitem[3] eq 'Roles' && $env{'request.course.id'}) { 
+            # special treatment for role selector
+            my $roles_selector = roles_selector(
+                        $env{'course.' . $env{'request.course.id'} . '.domain'},
+                        $env{'course.' . $env{'request.course.id'} . '.num'}  );
+
+            $menu .= $roles_selector ? "<li>$roles_selector</li>"
+                                     : '';
+        } else {
+            $menu .= prep_menuitem(\@$menuitem);
+        }
+    }
+    if ($menu =~ /\[url\].*\[symb\]/) {
+        my $escurl  = escape( Apache::lonenc::check_encrypt(
+                            $env{'request.noversionuri'}));
+
+        my $escsymb = escape( Apache::lonenc::check_encrypt(
+                            $env{'request.symb'})); 
+
+        if (    $env{'request.state'} eq 'construct'
+            and (   $env{'request.noversionuri'} eq '' 
+                 || !defined($env{'request.noversionuri'}))) 
+        {
+            ($escurl = $env{'request.filename'}) =~ 
+                s{^/home/([^/]+)/public_html/(.*)$}{/priv/$1/$2};
+
+            $escurl  = escape($escurl);
+        }    
+        $menu =~ s/\[url\]/$escurl/g;
+        $menu =~ s/\[symb\]/$escsymb/g;
+    }
+
+    return "<ul id=\"LC_TabMainMenuContent\">$menu</ul>";
+}
+
+
 #
 # This routine returns a translated hash for the menu items in the top inline menu row
 # Probably should be in mydesk.tab
 
+#SD this sub is deprecated - don't use it
 sub initlittle {
     return &Apache::lonlocal::texthash('ret' => 'Return to Last Location',
-				       'nav' => 'Navigate Contents',
+				       'nav' => 'Course Contents',
 				       'main' => 'Main Menu',
                                        'roles' => (&Apache::loncommon::show_course()?
                                                     'Courses':'Roles'),
@@ -163,6 +284,8 @@ sub initlittle {
                                        );
 }
 
+#SD this sub is deprecated - don't use it
+#SD functionality is covered by new loncommon::bodytag and primary_menu(), secondary_menu()
 sub menubuttons {
     my $forcereg=shift;
     my $titletable=shift;
@@ -299,8 +422,8 @@ ENDCRELOAD
     my $helplink = &Apache::loncommon::top_nav_help('Help');
 	return (<<ENDINLINEMENU);
 <script type="text/javascript">
-// BEGIN LON-CAPA Internal
 // <![CDATA[
+// BEGIN LON-CAPA Internal
 $utility
 // ]]>
 </script>
@@ -357,8 +480,14 @@ sub registerurl {
          (!&Apache::lonnet::is_on_map(
 	   &unescape($env{'request.noversionuri'})))) &&
         (!$forcereg))) {
- 	return $result.
-          '<script type="text/javascript">function LONCAPAreg(){;} function LONCAPAstale(){}</script>'.$force_title;
+ 	return
+        $result
+       .'<script type="text/javascript">'."\n"
+       .'// <![CDATA['."\n"
+       .'function LONCAPAreg(){;} function LONCAPAstale(){}'."\n"
+       .'// ]]>'."\n"
+       .'</script>'
+       .$force_title;
     }
 # Graphical display after login only
     if ($env{'request.registered'} && !$forcereg) { return ''; }
@@ -611,6 +740,13 @@ if(length($annotation) > 0){
 $menuitems.="&anno-[_1]&tations[_1]&annotate()&";
 $menuitems.="Make notes and annotations about this resource&&1\n";
 
+            unless ($noremote) { 
+                my $showreqcrs = &check_for_rcrs();
+                if ($showreqcrs) {
+                    $menuitems.="s&8&1&rcrs.gif&request[_1]&course[_16]".
+                                "&go('/adm/requestcourse')&Course requests\n";
+                }
+            }
             unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme)(\?|$)/) {
 		if (!$env{'request.enc'}) {
 		    $menuitems.=(<<ENDREALRES);
@@ -712,6 +848,7 @@ ENDREGTEXT
 	    $result = (<<ENDREGTHIS);
      
 <script type="text/javascript">
+// <![CDATA[
 // BEGIN LON-CAPA Internal
 var swmenu=null;
 
@@ -747,6 +884,7 @@ var swmenu=null;
       }
 
 // END LON-CAPA Internal 
+// ]]>
 </script>
 ENDREGTHIS
         }
@@ -762,6 +900,7 @@ ENDDONOTREGTEXT
            $result = (<<ENDDONOTREGTHIS);
 
 <script type="text/javascript">
+// <![CDATA[
 // BEGIN LON-CAPA Internal
 var swmenu=null;
 
@@ -786,6 +925,7 @@ var swmenu=null;
     }
 
 // END LON-CAPA Internal
+// ]]>
 </script>
 ENDDONOTREGTHIS
        }
@@ -856,6 +996,7 @@ sub startupremote {
     my $message=&mt('"Waiting for Remote Control window to load: "+[_1]','waited');
     return(<<ENDREMOTESTARTUP);
 <script type="text/javascript">
+// <![CDATA[
 var timestart;
 function wheelswitch() {
     if (typeof(document.wheel) != 'undefined') {
@@ -906,6 +1047,7 @@ function main() {
    wait();
 }
 
+// ]]>
 </script>
 ENDREMOTESTARTUP
 }
@@ -913,8 +1055,10 @@ ENDREMOTESTARTUP
 sub setflags() {
     return(<<ENDSETFLAGS);
 <script type="text/javascript">
+// <![CDATA[
     menuloaded=0;
     tim=0;
+// ]]>
 </script>
 ENDSETFLAGS
 }
@@ -923,7 +1067,9 @@ sub maincall() {
     if ($env{'environment.remote'} eq 'off') { return ''; }
     return(<<ENDMAINCALL);
 <script type="text/javascript">
+// <![CDATA[
     main();
+// ]]>
 </script>
 ENDMAINCALL
 }
@@ -965,7 +1111,12 @@ sub reopenmenu {
 sub open {
     my $returnval='';
     if ($env{'environment.remote'} eq 'off') { 
-	return '<script type="text/javascript">self.name="loncapaclient";</script>';
+	return
+        '<script type="text/javascript">'."\n"
+       .'// <![CDATA['."\n"
+       .'self.name="loncapaclient";'."\n"
+       .'// ]]>'."\n"
+       .'</script>';
     }
     my $menuname = &get_menu_name();
     
@@ -978,11 +1129,13 @@ sub open {
 #}
 #ENDRESIZE
 #    }
-    $returnval.=(<<ENDOPEN);
+    $returnval=(<<ENDOPEN);
+// <![CDATA[
 window.status='Opening LON-CAPA Remote Control';
 var menu=window.open("/res/adm/pages/menu.html?inhibitmenu=yes","$menuname",
 "height=375,width=150,scrollbars=no,menubar=no,top=5,left=5,screenX=5,screenY=5");
 self.name='loncapaclient';
+// ]]>
 ENDOPEN
     return '<script type="text/javascript">'.$returnval.'</script>';
 }
@@ -1248,16 +1401,14 @@ sub rawconfig {
                     $output.=&clear($row,$col);
                     next;
                 }
-            } elsif ($prt eq 'reqcrs') {
-                my $showreqcrs = 0;
-                foreach my $type ('official','unofficial') {
-                    if (&Apache::lonnet::usertools_access($env{'user.name'},
-                                                    $env{'user.domain'},
-                                                    $type,undef,'requestcourses')) {
-                        $showreqcrs = 1;
-                        last;
-                    }
+            } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
+                if (($prt eq 'reqcrsnsc') && ($show_course))   {
+                    next;
                 }
+                if (($prt eq 'reqcrsshc') && (!$show_course)) {
+                    next;
+                }
+                my $showreqcrs = &check_for_rcrs();
                 if (!$showreqcrs) {
                     $output.=&clear($row,$col);
                     next;
@@ -1278,6 +1429,28 @@ sub rawconfig {
     return $output;
 }
 
+sub check_for_rcrs {
+    my $showreqcrs = 0;
+    my @reqtypes = ('official','unofficial','community');
+    foreach my $type (@reqtypes) {
+        if (&Apache::lonnet::usertools_access($env{'user.name'},
+                                              $env{'user.domain'},
+                                              $type,undef,'requestcourses')) {
+            $showreqcrs = 1;
+            last;
+        }
+    }
+    if (!$showreqcrs) {
+        foreach my $type (@reqtypes) {
+            if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
+                $showreqcrs = 1;
+                last;
+            }
+        }
+    }
+    return $showreqcrs;
+}
+
 # ======================================================================= Close
 
 sub close {
@@ -1285,6 +1458,7 @@ sub close {
     my $menuname = &get_menu_name();
     return(<<ENDCLOSE);
 <script type="text/javascript">
+// <![CDATA[
 window.status='Accessing Remote Control';
 menu=window.open("/adm/rat/empty.html","$menuname",
                  "height=350,width=150,scrollbars=no,menubar=no");
@@ -1294,6 +1468,7 @@ menu.autologout=0;
 window.status='Closing Remote Control';
 menu.close();
 window.status='Done.';
+// ]]>
 </script>
 ENDCLOSE
 }
@@ -1636,7 +1811,7 @@ sub roles_selector {
         }
         $role_selector .= '</select>'."\n".
                '<input type="hidden" name="destinationurl" value="'.
-               $ENV{'REQUEST_URI'}.'" />'."\n".
+               &HTML::Entities::encode($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".
@@ -1814,34 +1989,34 @@ END
 
 BEGIN {
     if (! defined($readdesk)) {
-	{
-	    my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
-	    if ( CORE::open( my $config,"<$tabfile") ) {
-		while (my $configline=<$config>) {
-		    $configline=(split(/\#/,$configline))[0];
-		    $configline=~s/^\s+//;
-		    chomp($configline);
+        {
+            my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
+            if ( CORE::open( my $config,"<$tabfile") ) {
+                while (my $configline=<$config>) {
+                    $configline=(split(/\#/,$configline))[0];
+                    $configline=~s/^\s+//;
+                    chomp($configline);
                     if ($configline=~/^cat\:/) {
-                       my @entries=split(/\:/,$configline);
-                       $category_positions{$entries[2]}=$entries[1];
-                       $category_names{$entries[2]}=$entries[3];
-		    } elsif ($configline) {
-			push(@desklines,$configline);
-		    }
-		}
-		CORE::close($config);
-	    }
-	}
-	$readdesk='done';
+                        my @entries=split(/\:/,$configline);
+                        $category_positions{$entries[2]}=$entries[1];
+                        $category_names{$entries[2]}=$entries[3];
+                    } elsif ($configline=~/^prim\:/) {
+                        my @entries = (split(/\:/, $configline))[1..5];
+                        push @primary_menu, \@entries;
+                    } elsif ($configline=~/^scnd\:/) {
+                        my @entries = (split(/\:/, $configline))[1..5];
+                        push @secondary_menu, \@entries; 
+                    } elsif ($configline) {
+                        push(@desklines,$configline);
+                    }
+                }
+                CORE::close($config);
+            }
+        }
+        $readdesk='done';
     }
 }
 
 1;
 __END__
 
-
-
-
-
-
-