--- loncom/interface/lonmenu.pm	2023/10/06 02:04:47	1.540
+++ loncom/interface/lonmenu.pm	2024/09/03 10:40:04	1.556
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines to control the menu
 #
-# $Id: lonmenu.pm,v 1.540 2023/10/06 02:04:47 raeburn Exp $
+# $Id: lonmenu.pm,v 1.556 2024/09/03 10:40:04 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -245,7 +245,7 @@ sub prep_menuitem {
 # @primary_menu is filled within the BEGIN block of this module with 
 # entries from mydesk.tab
 sub primary_menu {
-    my ($crstype,$ltimenu,$menucoll,$menuref,$links_disabled,$links_target) = @_;
+    my ($crstype,$ltimenu,$menucoll,$menuref,$links_disabled,$links_target,$collapsible) = @_;
     my (%menu,%ltiexc,%menuopts);
     # each element of @primary contains following array:
     # (link url, icon path, alt text, link text, condition, position)
@@ -366,6 +366,19 @@ sub primary_menu {
                              ($item->[2] eq 'blog')) &&
                              (!&Apache::lonnet::usertools_access('','',$item->[2],
                                                            undef,'tools')));
+                    if (($item->[2] eq 'browsepub') && ($item->[0] eq '/res/')) {
+                        if ($env{'request.role'} =~ /^au\./) {
+                            $item->[0] .= $env{'request.role.domain'}.'/?launch=1';
+                        } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
+                            $item->[0] .= $1.'/'.$2.'/?launch=1';
+                        } elsif (&Apache::lonnet::allowed('bre',$env{'user.domain'})) {
+                            $item->[0] .= $env{'user.domain'}.'/?launch=1';
+                        } elsif (&Apache::lonnet::allowed('bro','/res/')) {
+                            $item->[0] .= '?launch=1';
+                        } else {
+                            next;
+                        }
+                    }
                     if ($env{'request.course.id'} && $menucoll) {
                         next if ($item->[3]) && (!$menuopts{$item->[3]});
                     }
@@ -421,6 +434,10 @@ sub primary_menu {
     }
     my @output = ('','');
     if ($menu{'left'} ne '') {
+        if ($collapsible) {
+            $menu{'left'} = ($listclass?'<li class="'.$listclass.'">':'<li>').
+                            '&nbsp;</li>'.$menu{'left'};
+        }
         $output[0] = "<ol class=\"LC_primary_menu LC_floatleft\">$menu{'left'}</ol>";
     }
     if ($menu{'right'} ne '') {
@@ -436,10 +453,10 @@ sub primary_menu {
 #
 #TODO this should probably be moved somewhere more central
 #since it can be used by different parts of the system
-sub getauthor{
+sub getauthor {
     return unless $env{'request.role'}=~/^(ca|aa|au)/; #nothing to do if user isn't some kind of author
 
-                        #co- or assistent author?
+                        #co- or assistant author?
     my ($dom, $user) = ($env{'request.role'} =~ /^(?:ca|aa)\.\/($match_domain)\/($match_username)$/)
                        ? ($1, $2) #domain, username of the parent author
                        : @env{ ('request.role.domain', 'user.name') }; #own domain, username
@@ -480,6 +497,15 @@ sub secondary_menu {
     my $canplc        = &Apache::lonnet::allowed('plc', $crs_sec);
     my $author        = &getauthor();
 
+    my ($is_author,$is_coauthor);
+    if ($author) {
+        if ($env{'request.role'} =~ /^au\./) {
+            $is_author = 1;
+        } elsif ($env{'request.role'} =~ /^ca\./) {
+            $is_coauthor = 1;
+        }
+    }
+
     my ($cdom,$cnum,$showsyllabus,$showfeeds,$showresv,$grouptools,
         $lti,$ltimapres,%ltiexc,%menuopts);
     $grouptools = 0;
@@ -539,15 +565,25 @@ sub secondary_menu {
         $linkattr = 'aria-disabled="true"';
     }
 
-    my ($canmodifycoauthor); 
+    my ($canlistcoauthors,$canmodifycoauthor);
     if ($env{'request.role'} eq "au./$env{'user.domain'}/") {
         my $extent = "$env{'user.domain'}/$env{'user.name'}";
         if ((&Apache::lonnet::allowed('cca',$extent)) ||
             (&Apache::lonnet::allowed('caa',$extent))) {
             $canmodifycoauthor = 1;
         }
+    } elsif ($env{'request.role'} =~ m{^(aa|ca)\./($match_domain/$match_username)$}) {
+        my ($role,$extent) = ($1,$2);
+        if (&Apache::lonnet::allowed('vca',$extent)) {
+            if ($env{"environment.internal.manager./$extent"}) {
+                $canmodifycoauthor = 1;
+            } else {
+                $canlistcoauthors = 1;
+            }
+        } elsif (&Apache::lonnet::allowed('vaa',$extent)) {
+            $canlistcoauthors = 1;
+        }
     }
-
     my ($roleswitcher_js,$roleswitcher_form);
     if ($links_target ne '') {
         $target = $links_target;
@@ -570,8 +606,12 @@ sub secondary_menu {
         # evaluate conditions 
         next if    ref($menuitem)  ne 'ARRAY';
         next if (($crstype eq 'Placement') && ($$menuitem[3] ne 'Roles') && (!$env{'request.role.adv'}));
-        next if    $$menuitem[4]   ne 'always'
-                && ($$menuitem[4]   ne 'author' && $$menuitem[4] ne 'cca')
+        next if    $$menuitem[4]  ne 'always'
+                && $$menuitem[4]  ne 'coauthor'
+                && $$menuitem[4]  ne 'author'
+                && $$menuitem[4]  ne 'authorspace'
+                && $$menuitem[4]  ne 'vca'
+                && $$menuitem[4]  ne 'mca'
                 && !$env{'request.course.id'};
         next if    $$menuitem[4]   =~ /^crsedit/
                 && (!$canedit && !$canvieweditor);
@@ -599,9 +639,17 @@ sub secondary_menu {
                 && !$showfeeds;
         next if     $$menuitem[4]  eq 'plc'
                 && !$canplc;
-        next if    $$menuitem[4]    eq 'author'
+        next if    $$menuitem[4]   eq 'authorspace'
                 && !$author;
-        next if    $$menuitem[4]    eq 'cca'
+        next if    $$menuitem[4]   eq 'author'
+                && !$is_author;
+        next if    $$menuitem[4]   eq 'coauthor'
+                && !$is_coauthor;
+        next if    $$menuitem[4]    eq 'vca'
+                && (!$canlistcoauthors || $canmodifycoauthor);
+        next if    $$menuitem[4]    eq 'vaa'
+                && (!$canlistcoauthors || $canmodifycoauthor);
+        next if    $$menuitem[4]    eq 'mca'
                 && !$canmodifycoauthor;
         next if    $$menuitem[4]    eq 'notltimapres'
                 && $ltimapres;
@@ -637,8 +685,8 @@ sub secondary_menu {
                         next if ($item->[2] eq 'vcg' && !$canviewgrps);
                         next if ($item->[2] eq 'crsedit' && !$canedit && !$canvieweditor);
                         next if ($item->[2] eq 'params' && !$canmodpara && !$canviewpara);
-                        next if ($item->[2] eq 'author' && !$author);
-                        next if ($item->[2] eq 'cca' && !$canmodifycoauthor);
+                        next if ($item->[2] eq 'author' && !$is_author);
+                        next if ($item->[2] eq 'vca' && !$canlistcoauthors);
                         next if ($item->[2] eq 'lti' && !$lti);
                         if ($item->[2] =~ /^lti(portfolio|wishlist|blog)$/) {
                             my $tool = $1;
@@ -724,6 +772,20 @@ sub secondary_menu {
         }
         $menu =~ s/\[url\]/$escurl/g;
         $menu =~ s/\[symb\]/$escsymb/g;
+    } elsif (($menu =~ m{/adm/preferences\?}) && ($menu =~ /\[returnurl\]/)) {
+        my $returnurl = $ENV{'REQUEST_URI'};
+        if ($ENV{'REQUEST_URI'} =~ m{/adm/preferences\?action=authorsettings\&returnurl=([^\&]+)$}) {
+            $returnurl = $1;
+        }
+        if (($returnurl =~ m{^/adm/createuser($|\?action=)}) ||
+            ($returnurl =~ m{^/priv/$match_domain/$match_username}) ||
+            ($returnurl =~ m{^/res(/?$|/$match_domain/$match_username)})) {
+            $returnurl =~ s{\?.*$}{};
+            $returnurl = '&amp;returnurl='.&HTML::Entities::encode($returnurl,'"<>&\'');
+        } else {
+            undef($returnurl);
+        }
+        $menu =~ s/\[returnurl\]/$returnurl/;
     }
     $menu =~ s/\[uname\]/$$author{user}/g;
     $menu =~ s/\[udom\]/$$author{dom}/g;
@@ -848,7 +910,7 @@ sub innerregister {
     my ($forcereg,$bread_crumbs,$group,$pagebuttonshide,$hostname,
         $ltiscope,$ltiuri,$showncrumbsref) = @_;
     my $const_space = ($env{'request.state'} eq 'construct');
-    my $is_const_dir = 0;
+    my $in_daxe = 0;
 
     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
 
@@ -1173,8 +1235,8 @@ END
 # End course context
 
 # Prepare the rest of the buttons
-        my ($menuitems,$got_prt,$got_wishlist,$crsauthor);
-        if ($const_space) {
+    my ($menuitems,$got_prt,$got_wishlist,$crsauthor,$toplevel_cstr,$crsauthor_cstr);
+    if ($const_space) {
 #
 # We are in construction space
 #
@@ -1182,17 +1244,15 @@ END
             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 	    my ($udom,$uname,$thisdisfn) =
 		($env{'request.filename'}=~m{^\Q$londocroot/priv/\E([^/]+)/([^/]+)/(.*)$});
-            my $crsauthor;
             if (($env{'request.course.id'}) &&
                 ($env{'course.'.$env{'request.course.id'}.'.num'} eq $uname) &&
                 ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom)) {
-                $crsauthor = 1;
+                $crsauthor_cstr = 1;
             }
             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
             if ($currdir =~ m-/$-) {
-                $is_const_dir = 1;
-                if (($thisdisfn eq '') && ($crsauthor)) {
-                    $is_const_dir = 2;
+                if ($thisdisfn eq '') {
+                    $toplevel_cstr = 1;
                 }
                 my $esc_currdir = &Apache::loncommon::escape_single($currdir);
                 $menuitems=(<<ENDMENUITEMS);
@@ -1201,6 +1261,14 @@ s&7&4&docs-22x22.png&Edit Metadata&defau
 s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','$esc_currdir')&Print contents of directory
 s&7&1&del.png&Delete&dir[_3]&gocstr('/adm/cfile?action=delete','$esc_currdir')&Delete this Directory
 ENDMENUITEMS
+                unless ($crsauthor_cstr) {
+                    if (($env{'environment.canarchive'}) &&
+                        ($uname eq $env{'user.name'}) && ($udom eq $env{'user.domain'})) {
+                        $menuitems .= (<<ENDMENUITEMS);
+s&7&7&archive.png&Export&dir[_1]&gocstr('/adm/cfile?action=archive','$esc_currdir')&Export Authoring Space Archive
+ENDMENUITEMS
+                    }
+                }
             } else {
                 $currdir =~ s|[^/]+$||;
 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
@@ -1215,7 +1283,7 @@ ENDMENUITEMS
 #
 # Probably should be in mydesk.tab
 #
-                if (($crsauthor) && ($pubfile eq "/res/$udom/$uname/default.rights")) {
+                if (($crsauthor_cstr) && ($pubfile eq "/res/$udom/$uname/default.rights")) {
                     $menuitems=(<<ENDMENUITEMS);
 s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
 s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/priv/$udom/$uname/$cleandisfn')&Publish this resource
@@ -1237,9 +1305,42 @@ s&7&4&rename.png&Rename&resource[_5]&goc
 s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/priv/$udom/$uname/$cleandisfn')&Delete this resource
 ENDMENUITEMS
                 }
-                $menuitems .= (<<ENDMENUITEMS);
+#
+# Print only makes sense for certain mime types
+#
+                if ($thisdisfn=~/\.(xml|html|htm|xhtml|xhtm|tex)$/ || $thisdisfn=~/$LONCAPA::assess_re/) {
+                    $menuitems .= (<<ENDMENUITEMS);
 s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/priv/$udom/$uname/$cleandisfn')&Prepare a printable document
 ENDMENUITEMS
+                }
+#
+# "Exit Daxe" in Functions menu when using Daxe
+#
+                if ((($env{'form.editmode'} eq 'daxe') &&
+                     ($thisdisfn=~/\.(xml|html|htm|xhtml|xhtm)$/)) ||
+                    (($env{'form.problemmode'} eq 'daxe') &&
+                     ($thisdisfn=~/$LONCAPA::assess_re/))) {
+                    my %editors = &Apache::loncommon::permitted_editors();
+                    if ($editors{'daxe'}) {
+                        my $privfile = &Apache::loncommon::escape_single("/priv/$udom/$uname/$thisdisfn");
+                        $in_daxe = 1;
+                        $menuitems .= (<<ENDMENUITEMS);
+my $privfile = &Apache::loncommon::escape_single("/priv/$udom/$uname/$thisdisfn");
+s&7&6&tolastloc.png&Exit Daxe&resource[_1]&go('$privfile')&Exit editing this resource
+ENDMENUITEMS
+                    }
+                }
+            }
+#
+# Editing options usually accessed via "Settings" in inline menu need to be
+# accessed in a different way, when Authoring Space is accessed in course
+# context
+#
+            if ($env{'request.role'} !~/^(aa|ca|au)/) {
+                my $privfile = &Apache::loncommon::escape_single("/priv/$udom/$uname/$thisdisfn");
+                $menuitems .= (<<ENDMENUITEMS);
+s&7&5&editops.png&Options&edit[_1]&gocstr('/adm/preferences?action=authorsettings','$privfile')&Authoring Space Options
+ENDMENUITEMS
             }
                 if (ref($bread_crumbs) eq 'ARRAY') {
                     &Apache::lonhtmlcommon::clear_breadcrumbs();
@@ -1450,8 +1551,13 @@ ENDMENUITEMS
             &Apache::lonhtmlcommon::add_breadcrumb_tool(
                 'tools',@tools);
 
+            #exit editing link/icon when using daxe in construction space 
             #publish button in construction space
             if ($env{'request.state'} eq 'construct'){
+                if ($in_daxe) {
+                    &Apache::lonhtmlcommon::add_breadcrumb_tool(
+                         'advtools', $inlineremote[76]);
+                }
                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
                      'advtools', $inlineremote[63]);
             } else {
@@ -1459,6 +1565,12 @@ ENDMENUITEMS
                      'tools', $inlineremote[63]);
             }
             &advtools_crumbs(@inlineremote);
+            #options link/icon in constructions space viewed with course role
+            if (($env{'request.state'} eq 'construct') &&
+                ($env{'request.role'} !~/^(aa|ca|au)/)) {
+                &Apache::lonhtmlcommon::add_breadcrumb_tool(
+                         'advtools', $inlineremote[75]);
+            }
         }
     } else {
         if ($showprogress) {
@@ -1469,7 +1581,7 @@ ENDMENUITEMS
         }
     }
     my ($topic_help,$topic_help_text);
-    if ($is_const_dir == 2) {
+    if ($toplevel_cstr && !$crsauthor_cstr) {
         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'))) {
@@ -1559,20 +1671,40 @@ sub prepare_functions {
             $editbutton = &get_editbutton($cfile,$home,$switchserver,
                                           $forceedit,$forceview,$forcereg);
         }
-    } elsif ((!$env{'request.course.id'}) &&
-             ($env{'user.author'}) && ($env{'request.filename'}) &&
-             ($env{'request.role'} !~/^(aa|ca|au)/)) {
+    } elsif (!$env{'request.course.id'}) {
+        if (($env{'user.author'}) && ($resurl eq '/adm/viewcoauthors')) {
+            if ($env{'request.role'} =~/^(ca|au)/) {
+                my ($audom,$auname);
+                if ($env{'request.role'} eq "au./$env{'user.domain'}/") {
+                    ($audom,$auname) = ($env{'user.domain'},$env{'user.name'});
+                } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)}) {
+                    ($audom,$auname) = ($1,$2);
+                }
+                if (($audom ne '') && ($auname ne '')) {
+                    my $file=&Apache::lonnet::declutter($env{'request.filename'});
+                    ($cfile,$home,$switchserver,$forceedit,$forceview) =
+                    &Apache::lonnet::can_edit_resource($file,$auname,$audom,
+                                                       $resurl);
+                    if ($cfile) {
+                        $editbutton = &get_editbutton($resurl,'','',$forceedit,
+                                                      $forceview);
+                    }
+                }
+            }
+        } elsif (($env{'user.author'}) && ($env{'request.filename'}) &&
+                 ($env{'request.role'} !~/^(aa|ca|au)/)) {
 #
 # Currently do not have the role of author or co-author.
 # Do we have authoring privileges for the resource?
 #
-        my $file=&Apache::lonnet::declutter($env{'request.filename'});
-        ($cfile,$home,$switchserver,$forceedit,$forceview) =
-            &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
-                &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
-        if (($cfile) && ($home ne '') && ($home ne 'no_host')) {
-            $editbutton = &get_editbutton($cfile,$home,$switchserver,
-                                          $forceedit,$forceview,$forcereg);
+            my $file=&Apache::lonnet::declutter($env{'request.filename'});
+            ($cfile,$home,$switchserver,$forceedit,$forceview) =
+                &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
+                    &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
+            if (($cfile) && ($home ne '') && ($home ne 'no_host')) {
+                $editbutton = &get_editbutton($cfile,$home,$switchserver,
+                                              $forceedit,$forceview,$forcereg);
+            }
         }
     } elsif ($env{'request.course.id'}) {
 #
@@ -1623,7 +1755,14 @@ sub prepare_functions {
         } elsif (($resurl !~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) &&
                  ($resurl ne '/cgi-bin/printout.pl')) {
             if ($env{'request.filename'}) {
-                my $file=&Apache::lonnet::declutter($env{'request.filename'});
+                my $file;
+                my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
+                if ($env{'request.filename'} =~ m{^\Q$londocroot\E/priv/}) {
+                    $file = $env{'request.filename'};
+                    $file =~ s{^\Q$londocroot\E/}{};
+                } else {
+                    $file=&Apache::lonnet::declutter($env{'request.filename'});
+                }
                 ($cfile,$home,$switchserver,$forceedit,$forceview) =
                     &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
                         &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
@@ -1765,7 +1904,7 @@ sub advtools_crumbs {
     } elsif ($env{'request.noversionuri'} !~ m{^/adm/(navmaps|viewclasslist)(\?|$)}) {
         if ($env{'request.state'} eq 'construct') {
             &Apache::lonhtmlcommon::add_breadcrumb_tool(
-                'advtools', @funcs[61,73,74,71,72]);
+                'advtools', @funcs[61,73,74,71,72,77]);
         } else {
             &Apache::lonhtmlcommon::add_breadcrumb_tool(
                 'advtools', @funcs[61,71,72,73,74,75,92]);
@@ -1980,6 +2119,8 @@ sub rawconfig {
                 } else {
                     next;
                 }
+            } elsif ($priv eq 'cca') {
+                next if ($rol eq 'cm');
             }
             if ((($priv eq 'bre') && (&Apache::lonnet::allowed($priv,$prt) eq 'F')) ||
                 (($priv ne 'bre') && (&Apache::lonnet::allowed($priv,$prt)))) {
@@ -2061,6 +2202,24 @@ sub rawconfig {
                     }
                 }
             }
+        } elsif ($pro eq 'coauthor') {
+            if ($env{'request.role'}=~ m{^(ca|aa)\./($match_domain)/($match_username)$}) {
+                my ($role,$audom,$auname) = ($1,$2,$3);
+                if ((($prt eq 'raa') && ($role eq 'aa')) ||
+                    (($prt eq 'rca') && ($role eq 'ca') &&
+                     (!$env{"environment.internal.manager./$audom/$auname"}))) {
+                    $output.=&switch($auname,$audom,
+                                     $row,$col,$img,$top,$bot,$act,$desc,$cat);
+                }
+            }
+        } elsif ($pro eq 'coauthorenv_manager') {
+            if ($env{'request.role'}=~ m{^ca\./($match_domain)/($match_username)$}) {
+                my ($audom,$auname) = ($1,$2);
+                if ($env{"environment.internal.manager./$audom/$auname"}) {
+                    $output.=&switch($auname,$audom,
+                                     $row,$col,$img,$top,$bot,$act,$desc,$cat);
+                }
+            }
         } elsif ($pro eq 'tools') {
             my @tools = ('aboutme','blog','portfolio');
             if (grep(/^\Q$prt\E$/,@tools)) {
@@ -2528,6 +2687,7 @@ sub utilityfunctions {
     my $esc_url=&escape($currenturl);
     my $esc_symb=&escape($currentsymb);
     my $newname = &mt('New Name');
+    &js_escape(\$newname);
 
     my $countdown = &countdown_toggle_js();
 
@@ -2661,6 +2821,10 @@ function gocstr(url,filename) {
         this.document.cstrprint.submit();
         return;
     }
+    if (url == '/adm/preferences?action=authorsettings') {
+        document.location.href=url+'&returnurl='+filename;
+        return;
+    }
     if (url !='') {
         this.document.constspace.filename.value = filename;
         this.document.constspace.action = url;