--- loncom/interface/lonmenu.pm	2014/12/20 15:35:40	1.430
+++ loncom/interface/lonmenu.pm	2016/01/26 14:30:25	1.438
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.430 2014/12/20 15:35:40 raeburn Exp $
+# $Id: lonmenu.pm,v 1.438 2016/01/26 14:30:25 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -128,6 +128,33 @@ Inputs: 4 - (a) link and (b) target for
             (c) title for text wrapped by anchor tag in top level item.
             (d) reference to array of arrays of sub-menu items.
 
+ The underlying datastructure used in (d) contains data from mydesk.tab.
+ It consists of an array which has an array for each item appearing in
+ the menu (e.g. [["link", "title", "condition"]] for a single-item menu).
+ create_submenu() supports also the creation of XHTML for nested dropdown
+ menus represented by unordered lists. This is done by replacing the
+ scalar used for the link with an arrayreference containing the menuitems
+ for the nested menu. This can be done recursively so that the next menu
+ may also contain nested submenus.
+
+ Example:
+ [											# begin of datastructure
+	["/home/", "Home", "condition1"], 		# 1st item of the 1st layer menu
+	[										# 2nd item of the 1st layer menu
+		[									# anon. array for nested menu
+			["/path1", "Path1", undef], 	# 1st item of the 2nd layer menu
+			["/path2", "Path2", undef], 	# 2nd item of the 2nd layer menu
+			[								# 3rd item of the 2nd layer menu
+				[[...], [...], ..., [...]],	# containing another menu layer
+				"Sub-Sub-Menu",				# title for this container
+				undef
+			]
+		],									# end of array/nested menu
+		"Sub-Menu",							# title for the container item
+		undef
+	]										# end of 2nd item of the 1st layer menu
+]
+
 =item innerregister()
 
 This gets called in order to register a URL in the body of the document
@@ -261,6 +288,11 @@ sub primary_menu {
                     push(@primsub,$item);
                 }
                 if (@primsub > 0) {
+                    if ($title eq 'Personal' && $env{'user.name'} && $env{'user.domain'} ) {
+                        $title = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
+                    } else {
+                        $title = &mt($title);
+                    }
                     $menu{$position} .= &create_submenu($link,$target,$title,\@primsub,1);
                 } elsif ($link) {
                     $menu{$position} .= '<li><a href="'.$link.'" target="'.$target.'">'.&mt($title).'</a></li>';
@@ -483,51 +515,80 @@ sub create_submenu {
     if ($target ne '') {
         $disptarget = ' target="'.$target.'"';
     }
-    my $name;
-    if ($title eq 'Personal') {
-        if ($env{'user.name'} && $env{'user.domain'}) {
-            $name = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
-        } else {
-            $name = &mt($title);
-        }
-    } else {
-        $name = &mt($title);
-    }
     my $menu = '<li class="LC_hoverable">'.
                '<a href="'.$link.'"'.$disptarget.'>'.
-               '<span class="LC_nobreak">'.$name.
+               '<span class="LC_nobreak">'.$title.
                '<span class="LC_fontsize_small" style="font-weight:normal;">'.
                ' &#9660;</span></span></a>'.
                '<ul>';
+
+    # $link and $title are only used in the initial string written in $menu
+    # as seen above, not needed for nested submenus
+    $menu .= &build_submenu($target, $submenu, $translate, '1');
+    $menu .= '</ul></li>';
+
+    return $menu;
+}
+
+# helper routine for create_submenu
+# build the dropdown (and nested submenus) recursively
+# see perldoc create_submenu documentation for further information
+sub build_submenu {
+    my ($target, $submenu, $translate, $first_level) = @_; 
+    if (!defined(@{$submenu})) {
+        return '';
+    }
+
+    my $menu = '';
     my $count = 0;
     my $numsub = scalar(@{$submenu});
     foreach my $item (@{$submenu}) {
         $count ++;
         if (ref($item) eq 'ARRAY') {
             my $href = $item->[0];
-            if ($href =~ /(aboutme|rss\.html)$/) {
-                next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne ''));
-                $href =~ s/\[domain\]/$env{'user.domain'}/g;
-                $href =~ s/\[user\]/$env{'user.name'}/g;
-            }
+            my $bordertop;
             my $borderbot;
-            if ($count == $numsub) {
-                $borderbot = 'border-bottom:1px solid black;';
-            }
-            unless (($href eq '') || ($href =~ /^\#/)) {
-                $target = ' target="_top"';
-            }
-            $menu .= '<li style="margin:0;padding:0;'.
-                     $borderbot.'"><a href="'.$href.'"'.$target.'>';
+            my $title;
+
             if ($translate) {
-                $menu .= &mt($item->[1]);
+                 $title = &mt($item->[1]);
             } else {
-                $menu .= $item->[1];
+                $title = $item->[1];
+            }
+
+            if ($count == 1 && !$first_level) {
+                $bordertop = 'border-top: 1px solid black;';
+            }
+            if ($count == $numsub) {
+                $borderbot = 'border-bottom: 1px solid black;';
+            }
+
+            # href is a reference to another submenu
+            if (ref($href) eq 'ARRAY') {
+                $menu .= '<li style="margin:0;padding:0;'.$bordertop . $borderbot . '">';
+                $menu .= '<p><span class="LC_primary_menu_innertitle">'
+					. $title . '</span><span class="LC_primary_menu_innerarrow">&#9654;</span></p>';
+                $menu .= '<ul>';
+                $menu .= &build_submenu($target, $href, $translate);
+                $menu .= '</ul>';
+                $menu .= '</li>';    
+            } else {    # href is the actual hyperlink and does not represent another submenu
+                        # for the current menu title
+                if ($href =~ /(aboutme|rss\.html)$/) {
+                    next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne ''));
+                    $href =~ s/\[domain\]/$env{'user.domain'}/g;
+                    $href =~ s/\[user\]/$env{'user.name'}/g;
+                }
+                unless (($href eq '') || ($href =~ /^\#/)) {
+                    $target = ' target="_top"';
+                }
+
+                $menu .= '<li style="margin:0;padding:0;'. $bordertop . $borderbot .'">';
+                $menu .= '<a href="'.$href.'"'.$target.'>' .  $title . '</a>';
+                $menu .= '</li>';
             }
-            $menu .= '</a></li>';
         }
     }
-    $menu .= '</ul></li>';
     return $menu;
 }
 
@@ -573,8 +634,18 @@ sub innerregister {
                                                        && $maptitle ne $coursetitle);
 
             push @crumbs, {text => $restitle, no_mt => 1} if $restitle; 
+            my @tools;
+            if ($env{'request.filename'} =~ /\.page$/) {
+                my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
+                if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
+                    @tools = @{$breadcrumb_tools{'tools'}};
+                }
+            }
             &Apache::lonhtmlcommon::clear_breadcrumbs();
             &Apache::lonhtmlcommon::add_breadcrumb(@crumbs);
+            if (@tools) {
+                &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',@tools);
+            }
         } else {
             $resurl = $env{'request.noversionuri'};
             my $courseurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
@@ -710,6 +781,9 @@ sub innerregister {
             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
             if ($currdir =~ m-/$-) {
                 $is_const_dir = 1;
+                if ($thisdisfn eq '') {
+                    $is_const_dir = 2;
+                }
             } else {
                 $currdir =~ s|[^/]+$||;
 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
@@ -752,7 +826,7 @@ ENDMENUITEMS
                 # wishlist is only available for users with access to resource-pool
                 # and links can only be set for resources within the resource-pool
                 $menuitems .= (<<ENDMENUITEMS);
-s&9&1&wishlist-link.png&Stored Links&wishlistlink[_2]&set_wishlistlink('',currentURL)&Save a link for this resource in my personal Stored Links repository&&1
+s&9&1&wishlist-link.png&Stored Links&wishlistlink[_2]&set_wishlistlink()&Save a link for this resource in my personal Stored Links repository&&1
 ENDMENUITEMS
                 $got_wishlist = 1;
             }
@@ -773,7 +847,7 @@ if ($env{'browser.mobile'}) {
     $is_mobile = 1;
 }
 
-            unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio)(\?|$)/) {
+            unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio|exttools?)(\?|$)/) {
 		if ((!$env{'request.enc'}) && ($env{'request.noversionuri'} !~ m{^/adm/wrapper/ext/}) && ($env{'request.noversionuri'} !~ m{^/uploaded/$match_domain/$match_courseid/docs/})) {
 		    $menuitems.=(<<ENDREALRES);
 s&6&3&catalog.png&Info&info[_1]&catalog_info('$is_mobile')&Show Metadata
@@ -803,7 +877,7 @@ ENDMENUITEMS
                 if (($env{'user.adv'}) && (!$env{'request.enc'})) {
                     # wishlist is only available for users with access to resource-pool
                     $menuitems .= (<<ENDMENUITEMS);
-s&9&1&wishlist-link.png&Stored Links&wishlistlink[_2]&set_wishlistlink('',currentURL)&Save a link for this resource in your personal Stored Links repository&&1
+s&9&1&wishlist-link.png&Stored Links&wishlistlink[_2]&set_wishlistlink()&Save a link for this resource in your personal Stored Links repository&&1
 ENDMENUITEMS
                     $got_wishlist = 1;
                 }
@@ -831,13 +905,20 @@ ENDMENUITEMS
 	    my $addremote=0;
 	    foreach (@inlineremote) { if ($_ ne '') { $addremote=1; last;} }
     if ($addremote) {
-
+        my $countdown;
+        if ($env{'request.filename'} =~ /\.page$/) {
+            my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
+            if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
+                $countdown = $breadcrumb_tools{'tools'}[0];
+            }
+        } else {
+            $countdown = &countdown_timer();
+        }
         &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 
             &Apache::lonhtmlcommon::add_breadcrumb_tool(
                 'navigation', @inlineremote[21,23]);
 
-        my $countdown = &countdown_timer();
         if (&hidden_button_check() eq 'yes') {
             if ($countdown) {
                 &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$countdown);
@@ -861,9 +942,18 @@ ENDMENUITEMS
             &advtools_crumbs(@inlineremote);
         }
     }
-
+    my ($topic_help,$topic_help_text);
+    if ($is_const_dir == 2) {
+        if ((($ENV{'SERVER_PORT'} == 443) || 
+             ($Apache::lonnet::protocol{$Apache::lonnet::perlvar{'lonHostID'}} eq 'https')) && 
+            (&Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},'webdav'))) {
+            $topic_help = 'Authoring_WebDAV,Authoring_WebDAV_Mac_10v6,Authoring_WebDAV_Mac_10v10,'.
+                          'Authoring_WebDAV_Windows_v7,Authoring_WebDAV_Linux_Centos';
+            $topic_help_text = 'About WebDAV access';
+        }
+    }
     return   &Apache::lonhtmlcommon::scripttag('', 'start')
-           . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
+           . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0,'','','','',$topic_help,$topic_help_text)
            . &Apache::lonhtmlcommon::scripttag('', 'end');
 }
 
@@ -1035,6 +1125,7 @@ sub prepare_functions {
         if (($env{'form.folderpath'} =~ /^supplemental/) &&
             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
             (($resurl =~ m{^/adm/wrapper/ext/}) ||
+             ($resurl =~ m{^/adm/$cdom/$cnum/\d+/exttools?$}) ||
              ($resurl =~ m{^/uploaded/$cdom/$cnum/supplemental/}) ||
              ($resurl eq '/adm/supplemental') ||
              ($resurl =~ m{^/public/$cdom/$cnum/syllabus$}) ||
@@ -1470,6 +1561,69 @@ function toggleCountdown() {
 END
 }
 
+# This creates a "done button" for timed events.  The confirmation box is a jQuery
+# dialog widget.  Clicking OK will set (LC_interval_done = 'true') which is checked in 
+# lonhomework.pm.
+sub done_button_js {
+    my ($type,$height) = @_;
+    if ($height !~ /^\d+$/) {
+        $height = 320;
+    }
+    my %lt = &Apache::lonlocal::texthash(
+                 title    => 'WARNING!',
+                 button   => 'Done',
+                 preamble => 'You are trying to end this timed event early.',
+                 map      => 'Confirming that you are done will cause the time to expire and prevent you from changing any answers in the current folder.',
+                 resource => 'Confirming that you are done will cause the time to expire for this question, and prevent you from changing your answer(s).', 
+                 ok       => 'Click "OK" if you are completely finished.',
+                 cancel   => 'Click "Cancel" to continue working.',
+    );
+    my $confirm;
+    if (($type eq 'map') || ($type eq 'resource')) {
+        $confirm = $lt{'preamble'}.' '.$lt{$type}.' '.$lt{'ok'}.' '.$lt{'cancel'};
+    }
+    if ($confirm) {
+        return <<END;
+
+<form method="post" name="LCdoneButton" action="">
+    <input type="hidden" name="LC_interval_done" value="" />
+    <button id="LC_done-confirm-opener" type="button">$lt{'button'}</button>
+</form>
+
+<div id="LC_done-confirm" title="$lt{'title'}">
+    <p>$confirm</p>
+</div>
+
+<script type="text/javascript">
+// <![CDATA[
+\$( "#LC_done-confirm" ).dialog({ autoOpen: false });
+\$( "#LC_done-confirm-opener" ).click(function() {
+    \$( "#LC_done-confirm" ).dialog( "open" );
+    \$( "#LC_done-confirm" ).dialog({
+      resizable: false,
+      height: $height,
+      modal: true,
+      buttons: {
+        "OK": function() {
+            \$( this ).dialog( "close" );
+            \$( '[name="LC_interval_done"]' )[0].value = 'true';
+            \$( '[name="LCdoneButton"]' )[0].submit();
+        },
+        Cancel: function() {
+          \$( this ).dialog( "close" );
+        }
+      }    
+    })
+});
+// ]]>
+</script>
+
+END
+    } else {
+        return;
+    }
+}
+
 sub utilityfunctions {
     my ($httphost) = @_;
     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
@@ -1719,7 +1873,7 @@ sub roles_selector {
     my $now = time;
     my (%courseroles,%seccount,%courseprivs);
     my $is_cc;
-    my ($js,$form,$switcher,$switchtext);
+    my ($js,$form,$switcher);
     my $ccrole;
     if ($crstype eq 'Community') {
         $ccrole = 'co';
@@ -1807,7 +1961,6 @@ sub roles_selector {
             }
         }
     }
-    $switchtext = 'Switch role'; # do not translate here
     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
     my $numdiffsec;
     if (keys(%seccount) == 1) {
@@ -1863,7 +2016,7 @@ sub roles_selector {
             }
         }
         if (@submenu > 0) {
-            $switcher = &create_submenu('','',$switchtext,\@submenu);
+            $switcher = &create_submenu('','',&mt('Switch role'),\@submenu);
         }
     }
     return ($js,$form,$switcher);
@@ -2134,10 +2287,12 @@ sub countdown_timer {
         }
         my $duedate = &Apache::lonnet::EXT("resource.0.duedate");
         my @interval=&Apache::lonnet::EXT("resource.0.interval");
+        my ($timelimit,$usesdone);
         if (@interval > 1) {
+            ($timelimit,$usesdone) = split(/_/,$interval[0]); 
             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
             if ($first_access > 0) {
-                if ($first_access+$interval[0] > time) {
+                if ($first_access+$timelimit > time) {
                     $hastimeleft = 1;
                 }
             }
@@ -2145,11 +2300,16 @@ sub countdown_timer {
         if (($duedate && $duedate > time) ||
             (!$duedate && $hastimeleft) ||
             ($slot_name ne '' && $slothastime)) {
-            my ($collapse,$expand,$alttxt,$title,$currdisp);
+            my ($collapse,$expand,$alttxt,$title,$currdisp,$donebutton);
             if ((@interval > 1 && $hastimeleft) ||
                 ($type eq 'Task' && $slothastime)) {
                 $currdisp = 'inline';
                 $collapse = '&#9658;&nbsp;';
+                if ((@interval > 1) && ($hastimeleft)) {
+                    if ($usesdone eq 'done') {
+                        $donebutton = &done_button_js($interval[1]);
+                    }
+                }
             } else {
                 $currdisp = 'none';
                 $expand = '&#9668;&nbsp;';
@@ -2159,8 +2319,9 @@ sub countdown_timer {
                 $title = $alttxt.'&nbsp;';
             }
             my $desc = &mt('Countdown to due date/time');
-            return <<END;
 
+            return <<END;
+$donebutton
 <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
 $collapse