File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.83.2.1: download - view: text, annotated - select for diffs
Mon Jan 3 16:37:24 2022 UTC (2 years, 5 months ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.369.2.83: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.435, 1.437, 1.438, 1.440, 1.441, 1.442, 1.448, 1.449,
  1.450, 1.471, 1.472, 1.484 (part), 1.489, 1.496 (part), 1.500, 1.503,
  1.507, 1.508

    1: # The LearningOnline Network with CAPA
    2: # Routines to control the menu
    3: #
    4: # $Id: lonmenu.pm,v 1.369.2.83.2.1 2022/01/03 16:37:24 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: 
   30: =head1 NAME
   31: 
   32: Apache::lonmenu
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Loads contents of /home/httpd/lonTabs/mydesk.tab, 
   37: used to generate inline menu, and Main Menu page. 
   38: 
   39: This is part of the LearningOnline Network with CAPA project
   40: described at http://www.lon-capa.org.
   41: 
   42: =head1 GLOBAL VARIABLES
   43: 
   44: =over
   45: 
   46: =item @desklines
   47: 
   48: Each element of this array contains a line of mydesk.tab that doesn't start with
   49: cat, prim or scnd. 
   50: It gets filled in the BEGIN block of this module.
   51: 
   52: =item %category_names
   53: 
   54: The keys of this hash are the abbreviations used in mydesk.tab in those lines that 
   55: start with cat, the values are strings representing titles. 
   56: It gets filled in the BEGIN block of this module.
   57: 
   58: =item %category_members
   59: 
   60: TODO 
   61: 
   62: =item %category_positions
   63: 
   64: The keys of this hash are the abbreviations used in mydesk.tab in those lines that
   65: start with cat, its values are position vectors (column, row). 
   66: It gets filled in the BEGIN block of this module.
   67: 
   68: =item $readdesk
   69: 
   70: Indicates that mydesk.tab has been read. 
   71: It is set to 'done' in the BEGIN block of this module.
   72: 
   73: =item @primary_menu
   74: 
   75: The elements of this array reference arrays that are made up of the components
   76: of those lines of mydesk.tab that start with prim:.
   77: It is used by primary_menu() to generate the corresponding menu.
   78: It gets filled in the BEGIN block of this module.
   79: 
   80: =item %primary_sub_menu
   81: 
   82: The keys of this hash reference are the names of items in the primary_menu array 
   83: which have sub-menus.  For each key, the corresponding value is a reference to
   84: an array containing components extracted from lines in mydesk.tab which begin
   85: with primsub:.
   86: This hash, which is used by primary_menu to generate sub-menus, is populated in
   87: the BEGIN block.
   88: 
   89: =item @secondary_menu
   90: 
   91: The elements of this array reference arrays that are made up of the components
   92: of those lines of mydesk.tab that start with scnd.
   93: It is used by secondary_menu() to generate the corresponding menu.
   94: It gets filled in the BEGIN block of this module.
   95: 
   96: =back
   97: 
   98: =head1 SUBROUTINES
   99: 
  100: =over
  101: 
  102: =item prep_menuitems(\@menuitem)
  103: 
  104: This routine wraps a menuitem in proper HTML. It is used by primary_menu() and 
  105: secondary_menu().
  106: 
  107: =item primary_menu()
  108: 
  109: This routine evaluates @primary_menu and returns a two item array, 
  110: with the array elements containing XHTML for the left and right sides of 
  111: the menu that contains the following links: About, Message, Roles, Help, Logout 
  112: @primary_menu is filled within the BEGIN block of this module with 
  113: entries from mydesk.tab
  114: 
  115: =item secondary_menu()
  116: 
  117: Same as primary_menu() but operates on @secondary_menu.
  118: 
  119: =item create_submenu()
  120: 
  121: Creates XHTML for unordered list of sub-menu items which belong to a
  122: particular top-level menu item. Uses hover pseudo class in css to display
  123: dropdown list when mouse hovers over top-level item. Support for IE6
  124: (no hover psuedo class) via LC_hoverable class for <li> tag for top-
  125: level item, which employs jQuery to handle behavior on mouseover.
  126: 
  127: Inputs: 6 - (a) link and (b) target for anchor href in top level item,
  128:             (c) title for text wrapped by anchor tag in top level item.
  129:             (d) reference to array of arrays of sub-menu items.
  130:             (e) boolean to indicate whether to call &mt() to translate
  131:                 name of menu item,
  132:             (f) optional class for <li> element in primary menu, for which
  133:                 sub menu is being generated.
  134: 
  135: The underlying datastructure used in (d) contains data from mydesk.tab.
  136: It consists of an array which has an array for each item appearing in
  137: the menu (e.g. [["link", "title", "condition"]] for a single-item menu).
  138: create_submenu() supports also the creation of XHTML for nested dropdown
  139: menus represented by unordered lists. This is done by replacing the
  140: scalar used for the link with an arrayreference containing the menuitems
  141: for the nested menu. This can be done recursively so that the next menu
  142: may also contain nested submenus.
  143: 
  144:  Example:
  145:  [                                                                                      # begin of datastructure
  146:         ["/home/", "Home", "condition1"],               # 1st item of the 1st layer menu
  147:         [                                                                               # 2nd item of the 1st layer menu
  148:                 [                                                                       # anon. array for nested menu
  149:                         ["/path1", "Path1", undef],     # 1st item of the 2nd layer menu
  150:                         ["/path2", "Path2", undef],     # 2nd item of the 2nd layer menu
  151:                         [                                                               # 3rd item of the 2nd layer menu
  152:                                 [[...], [...], ..., [...]],     # containing another menu layer
  153:                                 "Sub-Sub-Menu",                         # title for this container
  154:                                 undef
  155:                         ]
  156:                 ],                                                                      # end of array/nested menu
  157:                 "Sub-Menu",                                                     # title for the container item
  158:                 undef
  159:         ]                                                                               # end of 2nd item of the 1st layer menu
  160: ]
  161: 
  162: 
  163: =item innerregister()
  164: 
  165: This gets called in order to register a URL in the body of the document
  166: 
  167: =item loadevents()
  168: 
  169: =item unloadevents()
  170: 
  171: =item startupremote()
  172: 
  173: =item setflags()
  174: 
  175: =item maincall()
  176: 
  177: =item load_remote_msg()
  178: 
  179: =item get_menu_name()
  180: 
  181: =item reopenmenu()
  182: 
  183: =item open()
  184: 
  185: Open the menu
  186: 
  187: =item clear()
  188: 
  189: =item switch()
  190: 
  191: Switch a button or create a link
  192: Switch acts on the javascript that is executed when a button is clicked.  
  193: The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
  194: 
  195: =item secondlevel()
  196: 
  197: =item openmenu()
  198: 
  199: =item inlinemenu()
  200: 
  201: =item rawconfig()
  202: 
  203: =item utilityfunctions()
  204: 
  205: Output from this routine is a number of javascript functions called by
  206: items in the inline menu, and in some cases items in the Main Menu page. 
  207: 
  208: =item serverform()
  209: 
  210: =item constspaceform()
  211: 
  212: =item get_nav_status()
  213: 
  214: =item hidden_button_check()
  215: 
  216: =item roles_selector()
  217: 
  218: =item jump_to_role()
  219: 
  220: =back
  221: 
  222: =cut
  223: 
  224: package Apache::lonmenu;
  225: 
  226: use strict;
  227: use Apache::lonnet;
  228: use Apache::lonhtmlcommon();
  229: use Apache::loncommon();
  230: use Apache::lonenc();
  231: use Apache::lonlocal;
  232: use Apache::lonmsg();
  233: use LONCAPA qw(:DEFAULT :match);
  234: use HTML::Entities();
  235: use Apache::lonwishlist();
  236: 
  237: use vars qw(@desklines %category_names %category_members %category_positions 
  238:             $readdesk @primary_menu %primary_submenu @secondary_menu %secondary_submenu);
  239: 
  240: my @inlineremote;
  241: 
  242: sub prep_menuitem {
  243:     my ($menuitem,$listclass,$linkattr) = @_;
  244:     return '' unless(ref($menuitem) eq 'ARRAY');
  245:     my $link;
  246:     if ($$menuitem[1]) { # graphical Link
  247:         $link = "<img class=\"LC_noBorder\""
  248:               . " src=\"" . &Apache::loncommon::lonhttpdurl($$menuitem[1]) . "\"" 
  249:               . " alt=\"" . &mt($$menuitem[2]) . "\" />";
  250:     } else {             # textual Link
  251:         $link = &mt($$menuitem[3]);
  252:     }
  253:     return ($listclass?'<li class="'.$listclass.'">':'<li>').'<a'
  254:            # highlighting for new messages
  255:            . ( $$menuitem[4] eq 'newmsg' ? ' class="LC_new_message"' : '') 
  256:            . qq| href="$$menuitem[0]" target="_top" $linkattr>$link</a></li>|;
  257: }
  258: 
  259: # primary_menu() evaluates @primary_menu and returns a two item array,
  260: # with the array elements containing XHTML for the left and right sides of 
  261: # the menu that contains the following links:
  262: # Personal, About, Message, Roles, Help, Logout
  263: # @primary_menu is filled within the BEGIN block of this module with 
  264: # entries from mydesk.tab
  265: sub primary_menu {
  266:     my ($crstype,$ltimenu,$menucoll,$menuref,$links_disabled) = @_;
  267:     my (%menu,%menuopts);
  268:     # each element of @primary contains following array:
  269:     # (link url, icon path, alt text, link text, condition, position)
  270:     my $public;
  271:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
  272:         || (($env{'user.name'} eq '') && ($env{'user.domain'} eq ''))) {
  273:         $public = 1;
  274:     }
  275:     if (($menucoll) && (ref($menuref) eq 'HASH')) {
  276:         %menuopts = %{$menuref};
  277:     }
  278:     foreach my $menuitem (@primary_menu) {
  279:         # evaluate conditions 
  280:         next if    ref($menuitem)       ne 'ARRAY';    #
  281:         next if    $$menuitem[4]        eq 'nonewmsg'  # show links depending on
  282:                 && &Apache::lonmsg::mynewmail();       # whether a new msg 
  283:         next if    $$menuitem[4]        eq 'newmsg'    # arrived or not
  284:                 && !&Apache::lonmsg::mynewmail();      # 
  285:         next if    $$menuitem[4]        !~ /public/    ##we've a public user,
  286:                 && $public;                            ##who should not see all
  287:                                                        ##links
  288:         next if    $$menuitem[4]        eq 'onlypublic'# hide links which are 
  289:                 && !$public;                           # only visible to public
  290:                                                        # users
  291:         next if    $$menuitem[4]        eq 'roles'     ##show links depending on
  292:                 && &Apache::loncommon::show_course();  ##term 'Courses' or 
  293:         next if    $$menuitem[4]        eq 'courses'   ##'Roles' wanted
  294:                 && !&Apache::loncommon::show_course(); ##
  295:         
  296:         my $title = $menuitem->[3];
  297:         my $position = $menuitem->[5];
  298:         if ($position eq '') {
  299:             $position = 'right';
  300:         }
  301:         if ($env{'request.course.id'} && $menucoll) {
  302:             if (($menuitem->[6]) && (!$menuopts{$menuitem->[6]})) {
  303:                 if ($menuitem->[6] eq 'pers') {
  304:                     if ($menuopts{'name'} &&
  305:                         $env{'user.name'} && $env{'user.domain'}) {
  306:                         $menu{$position} .= '<li><a href="#">'.
  307:                             &Apache::loncommon::plainname($env{'user.name'},
  308:                                                           $env{'user.domain'}).'</a></li>';
  309:                         next;
  310:                     } else {
  311:                         next;
  312:                     }
  313:                 } else {
  314:                     next;
  315:                 }
  316:             }
  317:         }
  318:         my ($listclass,$linkattr);
  319:         if ($links_disabled) {
  320:             $listclass = 'LCisDisabled';
  321:             $linkattr = 'aria-disabled="true"';
  322:         }
  323:         if (defined($primary_submenu{$title})) {
  324:             my ($link,$target);
  325:             if ($menuitem->[0] ne '') {
  326:                 $link = $menuitem->[0];
  327:                 $target = '_top';
  328:             } else {
  329:                 $link = '#';
  330:             }
  331:             my @primsub;
  332:             if (ref($primary_submenu{$title}) eq 'ARRAY') {
  333:                 foreach my $item (@{$primary_submenu{$title}}) {
  334:                     next if (($item->[2] eq 'wishlist') && (!$env{'user.adv'})); 
  335:                     next if ((($item->[2] eq 'portfolio') || 
  336:                              ($item->[2] eq 'blog')) && 
  337:                              (!&Apache::lonnet::usertools_access('','',$item->[2],
  338:                                                            undef,'tools')));
  339:                     if ($env{'request.course.id'} && $menucoll) {
  340:                         next if ($item->[3]) && (!$menuopts{$item->[3]});
  341:                     }
  342:                     push(@primsub,$item);
  343:                 }
  344:                 if ($title eq 'Personal') {
  345:                     if ($env{'user.name'} && $env{'user.domain'}) {
  346:                         unless (($env{'request.course.id'}) && ($menucoll) && (!$menuopts{'name'})) {
  347:                             $title = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
  348:                         }
  349:                     }
  350:                     next if (($env{'request.course.id'}) && ($menucoll) && ($title eq 'Personal') &&
  351:                              (!@primsub));
  352:                     if ($title eq 'Personal') {
  353:                         $title = &mt($title);
  354:                     }
  355:                 } else {
  356:                     $title = &mt($title);
  357:                 }
  358:                 if (@primsub > 0) {
  359:                     $menu{$position} .= &create_submenu($link,$target,$title,\@primsub,1,undef,$listclass,$linkattr);
  360:                 } elsif ($link) {
  361:                     $menu{$position} .= ($listclass?'<li class="'.$listclass.'">':'<li>').
  362:                                         '<a href="'.$link.'" target="'.$target.'" '.$linkattr.'>'.$title.'</a></li>';
  363:                 }
  364:             }
  365:         } elsif ($$menuitem[3] eq 'Help') { # special treatment for helplink
  366:             if ($public) {
  367:                 my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  368:                 my $defdom = &Apache::lonnet::default_login_domain();
  369:                 my $to = &Apache::loncommon::build_recipient_list(undef,
  370:                                                                   'helpdeskmail',
  371:                                                                   $defdom,$origmail);
  372:                 if ($to ne '') {
  373:                     $menu{$position} .= &prep_menuitem($menuitem,$listclass,$linkattr); 
  374:                 }
  375:             } else {
  376:                 $menu{$position} .= ($listclass?'<li class="'.$listclass.'">':'<li>').
  377:                                     &Apache::loncommon::top_nav_help('Help',$linkattr).
  378:                                     '</li>';
  379:             }
  380:         } elsif ($$menuitem[3] eq 'Log In') {
  381:             if ($public) {
  382:                 if (&Apache::lonnet::get_saml_landing()) {
  383:                     $$menuitem[0] = '/adm/login';
  384:                 }
  385:             }
  386:             $menu{$position} .= prep_menuitem($menuitem,$listclass,$linkattr);
  387:         } else {
  388:             $menu{$position} .= prep_menuitem($menuitem,$listclass,$linkattr);
  389:         }
  390:     }
  391:     my @output = ('','');
  392:     if ($menu{'left'} ne '') {
  393:         $output[0] = "<ol class=\"LC_primary_menu LC_floatleft\">$menu{'left'}</ol>";
  394:     }
  395:     if ($menu{'right'} ne '') {
  396:         $output[1] = "<ol class=\"LC_primary_menu LC_floatright LC_right\">$menu{'right'}</ol>";
  397:     }
  398:     return @output;
  399: }
  400: 
  401: #returns hashref {user=>'',dom=>''} containing:
  402: #   own name, domain if user is au
  403: #   name, domain of parent author if user is ca or aa
  404: #empty return if user is not an author or not on homeserver
  405: #
  406: #TODO this should probably be moved somewhere more central
  407: #since it can be used by different parts of the system
  408: sub getauthor{
  409:     return unless $env{'request.role'}=~/^(ca|aa|au)/; #nothing to do if user isn't some kind of author
  410: 
  411:                         #co- or assistent author?
  412:     my ($dom, $user) = ($env{'request.role'} =~ /^(?:ca|aa)\.\/($match_domain)\/($match_username)$/)
  413:                        ? ($1, $2) #domain, username of the parent author
  414:                        : @env{ ('request.role.domain', 'user.name') }; #own domain, username
  415: 
  416:     # current server == home server?
  417:     my $home =  &Apache::lonnet::homeserver($user,$dom);
  418:     foreach (&Apache::lonnet::current_machine_ids()){
  419:         return {user => $user, dom => $dom} if $_ eq $home;
  420:     }
  421: 
  422:     # if wrong server
  423:     return;
  424: }
  425: 
  426: sub secondary_menu {
  427:     my ($httphost,$ltiscope,$ltimenu,$noprimary,$menucoll,$menuref,$links_disabled) = @_;
  428:     my $menu;
  429: 
  430:     my $crstype = &Apache::loncommon::course_type();
  431:     my $crs_sec = $env{'request.course.id'} . ($env{'request.course.sec'} 
  432:                                                ? "/$env{'request.course.sec'}"
  433:                                                : '');
  434:     my $canedit       = &Apache::lonnet::allowed('mdc', $env{'request.course.id'});
  435:     my $canvieweditor = &Apache::lonnet::allowed('cev', $env{'request.course.id'}); 
  436:     my $canviewroster = $env{'course.'.$env{'request.course.id'}.'.student_classlist_view'};
  437:     if ($canviewroster eq 'disabled') {
  438:         undef($canviewroster);
  439:     }
  440:     my $canviewgrps   = &Apache::lonnet::allowed('vcg', $crs_sec);
  441:     my $canmodifyuser = &Apache::lonnet::allowed('cst', $crs_sec);
  442:     my $canviewusers  = &Apache::lonnet::allowed('vcl', $crs_sec);
  443:     my $canviewwnew   = &Apache::lonnet::allowed('whn', $crs_sec);
  444:     my $canviewpara   = &Apache::lonnet::allowed('vpa', $crs_sec);
  445:     my $canmodpara    = &Apache::lonnet::allowed('opa', $crs_sec);
  446:     my $canvgr        = &Apache::lonnet::allowed('vgr', $crs_sec);
  447:     my $canmgr        = &Apache::lonnet::allowed('mgr', $crs_sec);
  448:     my $canplc        = &Apache::lonnet::allowed('plc', $crs_sec);
  449:     my $author        = &getauthor();
  450: 
  451:     my ($cdom,$cnum,$showsyllabus,$showfeeds,$showresv,$grouptools,%menuopts);
  452:     $grouptools = 0; 
  453:     if ($env{'request.course.id'}) {
  454:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  455:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  456:         unless ($canedit || $canvieweditor) {
  457:             unless (&Apache::lonnet::is_on_map("public/$cdom/$cnum/syllabus")) {
  458:                 if (($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'}) ||
  459:                     ($env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) ||
  460:                     ($env{'course.'.$env{'request.course.id'}.'.updatedsyllabus'}) ||
  461:                     ($env{'request.course.syllabustime'})) {
  462:                     $showsyllabus = 1;
  463:                 }
  464:             }
  465:             if ($env{'request.course.feeds'}) {
  466:                 $showfeeds = 1;
  467:             }
  468:         }
  469:         unless ($canmgr || $canvgr) {
  470:             my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
  471:             if (keys(%slots) > 0) {
  472:                 $showresv = 1;
  473:             }
  474:         }
  475:         if ($env{'request.course.groups'} ne '') {
  476:             foreach my $group (split(/:/,$env{'request.course.groups'})) {
  477:                 next unless ($group =~ /^\w+$/);
  478:                 my @privs = split(/:/,$env{"user.priv.$env{'request.role'}./$cdom/$cnum/$group"});
  479:                 shift(@privs);
  480:                 if (@privs) {
  481:                     $grouptools ++;
  482:                 }
  483:             }
  484:         }
  485:     }
  486:     if (($menucoll) && (ref($menuref) eq 'HASH')) {
  487:         %menuopts = %{$menuref};
  488:     }
  489: 
  490:     my ($listclass,$linkattr);
  491:     if ($links_disabled) {
  492:         $listclass = 'LCisDisabled';
  493:         $linkattr = 'aria-disabled="true"';
  494:     }
  495: 
  496:     my ($canmodifycoauthor);
  497:     if ($env{'request.role'} eq "au./$env{'user.domain'}/") {
  498:         my $extent = "$env{'user.domain'}/$env{'user.name'}";
  499:         if ((&Apache::lonnet::allowed('cca',$extent)) ||
  500:             (&Apache::lonnet::allowed('caa',$extent))) {
  501:             $canmodifycoauthor = 1;
  502:         }
  503:     }
  504: 
  505:     my ($roleswitcher_js,$roleswitcher_form);
  506: 
  507:     foreach my $menuitem (@secondary_menu) {
  508:         # evaluate conditions 
  509:         next if    ref($menuitem)  ne 'ARRAY';
  510:         next if    $$menuitem[4]   ne 'always'
  511:                 && ($$menuitem[4]  ne 'author' && $$menuitem[4] ne 'cca')
  512:                 && !$env{'request.course.id'};
  513:         next if    $$menuitem[4]   =~ /^crsedit/
  514:                 && (!$canedit && !$canvieweditor);
  515:         next if    $$menuitem[4]  eq 'crseditCourse'
  516:                 && ($crstype eq 'Community');
  517:         next if    $$menuitem[4]  eq 'crseditCommunity'
  518:                 && ($crstype eq 'Course');
  519:         next if    $$menuitem[4]  eq 'nvgr'
  520:                 && $canvgr;
  521:         next if    $$menuitem[4]  eq 'vgr'
  522:                 && !$canvgr;
  523:         next if    $$menuitem[4]   eq 'viewusers'
  524:                 && !$canmodifyuser && !$canviewusers;
  525:         next if    $$menuitem[4]   eq 'noviewusers'
  526:                 && ($canmodifyuser || $canviewusers || !$canviewroster);
  527:         next if    $$menuitem[4]   eq 'mgr'
  528:                 && !$canmgr;
  529:         next if    $$menuitem[4]   eq 'showresv'
  530:                 && !$showresv;
  531:         next if    $$menuitem[4]   eq 'whn'
  532:                 && !$canviewwnew;
  533:         next if    $$menuitem[4]   eq 'params'
  534:                 && (!$canmodpara && !$canviewpara);
  535:         next if    $$menuitem[4]   eq 'nvcg'
  536:                 && ($canviewgrps || !$grouptools);
  537:         next if    $$menuitem[4]   eq 'showsyllabus'
  538:                 && !$showsyllabus;
  539:         next if    $$menuitem[4]   eq 'showfeeds'
  540:                 && !$showfeeds;
  541:         next if     $$menuitem[4]  eq 'plc'
  542:                 && !$canplc;
  543:         next if    $$menuitem[4]    eq 'author'
  544:                 && !$author;
  545:         next if    $$menuitem[4]    eq 'cca'
  546:                 && !$canmodifycoauthor;
  547: 
  548:         my $title = $menuitem->[3];
  549:         if ($env{'request.course.id'} && $menucoll) {
  550:             if ($$menuitem[5] eq 'main') {
  551:                 next if ($menuopts{$$menuitem[5]} eq 'n');
  552:             } elsif ($$menuitem[5] ne 'roles') {
  553:                 next if (($$menuitem[5]) && (!$menuopts{$$menuitem[5]}));
  554:             }
  555:         }
  556:         if (defined($secondary_submenu{$title})) {
  557:             my ($link,$target);
  558:             if ($menuitem->[0] ne '') {
  559:                 $link = $menuitem->[0];
  560:                 $target = '_top';
  561:             } else {
  562:                 $link = '#';
  563:             }
  564:             my @scndsub;
  565:             if (ref($secondary_submenu{$title}) eq 'ARRAY') {
  566:                 foreach my $item (@{$secondary_submenu{$title}}) {
  567:                     if (ref($item) eq 'ARRAY') {
  568:                         next if ($item->[2] eq 'vgr' && !$canvgr);
  569:                         next if ($item->[2] eq 'opa' && !$canmodpara);
  570:                         next if ($item->[2] eq 'vpa' && !$canviewpara);
  571:                         next if ($item->[2] eq 'viewusers' && !($canmodifyuser || $canviewusers));
  572:                         next if ($item->[2] eq 'mgr' && !$canmgr);
  573:                         next if ($item->[2] eq 'vcg' && !$canviewgrps);
  574:                         next if ($item->[2] eq 'crsedit' && !$canedit && !$canvieweditor);
  575:                         next if ($item->[2] eq 'params' && !$canmodpara && !$canviewpara);
  576:                         next if ($item->[2] eq 'author' && !$author);
  577:                         next if ($item->[2] eq 'cca' && !$canmodifycoauthor);
  578:                         push(@scndsub,$item); 
  579:                     }
  580:                 }
  581:                 if (@scndsub > 0) {
  582:                     $menu .= &create_submenu($link,$target,&mt($title),\@scndsub,1,undef,
  583:                                              $listclass,$linkattr);
  584:                 } elsif ($link ne '#') {
  585:                     $menu .= ($listclass?'<li class="'.$listclass.'">':'<li>').
  586:                              '<a href="'.$link.'" target="'.$target.'" '.$linkattr.'>'.
  587:                              &mt($title).'</a></li>';
  588:                 }
  589:             }
  590:         } elsif ($$menuitem[3] eq 'Roles' && $env{'request.course.id'}) {
  591:             # special treatment for role selector
  592:             my ($switcher,$has_opa_priv);
  593:             ($roleswitcher_js,$roleswitcher_form,$switcher,$has_opa_priv) =
  594:                 &roles_selector(
  595:                         $env{'course.' . $env{'request.course.id'} . '.domain'},
  596:                         $env{'course.' . $env{'request.course.id'} . '.num'},
  597:                         $httphost
  598:                 );
  599:             if (($$menuitem[5]) && (!$menuopts{$$menuitem[5]})) {
  600:                 next unless ($has_opa_priv);
  601:             }
  602:             $menu .= $switcher;
  603:         } else {
  604:             if ($$menuitem[3] eq 'Syllabus' && $env{'request.course.id'}) {
  605:                 my $url = $$menuitem[0];
  606:                 $url =~ s{\[cdom\]/\[cnum\]}{$cdom/$cnum};
  607:                 if (&Apache::lonnet::is_on_map($url)) {
  608:                     unless ($$menuitem[0] =~ /(\?|\&)register=1/) {
  609:                         $$menuitem[0] .= (($$menuitem[0]=~/\?/)? '&' : '?').'register=1';
  610:                     }
  611:                 } else {
  612:                     $$menuitem[0] =~ s{\&?register=1}{};
  613:                 }
  614:                 if ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://}) {
  615:                     if (($ENV{'SERVER_PORT'} == 443) || ($env{'request.use_absolute'} =~ m{^https://})) {
  616:                         unless ((&Apache::lonnet::uses_sts()) || (&Apache::lonnet::waf_allssl())) {
  617:                             unless ($$menuitem[0] =~ m{^https?://}) {
  618:                                 $$menuitem[0] = 'http://'.$ENV{'SERVER_NAME'}.$$menuitem[0];
  619:                             }
  620:                             unless ($$menuitem[0] =~ /(\&|\?)usehttp=1/) {
  621:                                 $$menuitem[0] .= (($$menuitem[0]=~/\?/) ? '&' : '?').'usehttp=1';
  622:                             }
  623:                         }
  624:                     }
  625:                 }
  626:                 $$menuitem[0] = &HTML::Entities::encode($$menuitem[0],'&<>"');
  627:             }
  628:             $menu .= &prep_menuitem(\@$menuitem,$listclass,$linkattr);
  629:         }
  630:     }
  631:     if ($menu =~ /\[url\].*\[symb\]/) {
  632:         my $escurl  = &escape( &Apache::lonenc::check_encrypt(
  633:                              $env{'request.noversionuri'}));
  634: 
  635:         my $escsymb = &escape( &Apache::lonenc::check_encrypt(
  636:                              $env{'request.symb'})); 
  637: 
  638:         if (    $env{'request.state'} eq 'construct'
  639:             and (   $env{'request.noversionuri'} eq '' 
  640:                  || !defined($env{'request.noversionuri'}))) 
  641:         {
  642:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  643:             ($escurl = $env{'request.filename'}) =~ s{^\Q$londocroot\E}{};
  644:             $escurl  = &escape($escurl);
  645:         }
  646:         $menu =~ s/\[url\]/$escurl/g;
  647:         $menu =~ s/\[symb\]/$escsymb/g;
  648:     }
  649:     $menu =~ s/\[uname\]/$$author{user}/g;
  650:     $menu =~ s/\[udom\]/$$author{dom}/g;
  651:     $menu =~ s/\[javascript\]/javascript:/g;
  652:     if ($env{'request.course.id'}) {  
  653:         $menu =~ s/\[cnum\]/$cnum/g;
  654:         $menu =~ s/\[cdom\]/$cdom/g;
  655:     }
  656:     if ($menu) {
  657:         $menu = "<ul id=\"LC_secondary_menu\">$menu</ul>";
  658:     }
  659:     if ($roleswitcher_form) {
  660:         $menu .= "\n$roleswitcher_js\n$roleswitcher_form";
  661:     }
  662:     return $menu;
  663: }
  664: 
  665: sub create_submenu {
  666:     my ($link,$target,$title,$submenu,$translate,$addclass,$listclass,$linkattr) = @_;
  667:     return unless (ref($submenu) eq 'ARRAY');
  668:     my $disptarget;
  669:     if ($target ne '') {
  670:         $disptarget = ' target="'.$target.'"';
  671:     }
  672:     my $menu = '<li class="LC_hoverable '.$addclass.'">'.
  673:                '<a href="'.$link.'"'.$disptarget.'>'.
  674:                '<span class="LC_nobreak">'.$title.
  675:                '<span class="LC_fontsize_small" style="font-weight:normal;">'.
  676:                ' &#9660;</span></span></a>'.
  677:                '<ul>';
  678: 
  679:     # $link and $title are only used in the initial string written in $menu
  680:     # as seen above, not needed for nested submenus
  681:     $menu .= &build_submenu($target, $submenu, $translate, '1', $listclass, $linkattr);
  682:     $menu .= '</ul></li>';
  683: 
  684:     return $menu;
  685: }
  686: 
  687: # helper routine for create_submenu
  688: # build the dropdown (and nested submenus) recursively
  689: # see perldoc create_submenu documentation for further information
  690: sub build_submenu {
  691:     my ($target, $submenu, $translate, $first_level, $listclass, $linkattr) = @_;
  692:     unless (@{$submenu}) {
  693:         return '';
  694:     }
  695: 
  696:     my $menu = '';
  697:     my $count = 0;
  698:     my $numsub = scalar(@{$submenu});
  699:     foreach my $item (@{$submenu}) {
  700:         $count ++;
  701:         if (ref($item) eq 'ARRAY') {
  702:             my $href = $item->[0];
  703:             my $bordertop;
  704:             my $borderbot;
  705:             my $title;
  706: 
  707:             if ($translate) {
  708:                  $title = &mt($item->[1]);
  709:             } else {
  710:                 $title = $item->[1];
  711:             }
  712: 
  713:             if ($count == 1 && !$first_level) {
  714:                 $bordertop = 'border-top: 1px solid black;';
  715:             }
  716:             if ($count == $numsub) {
  717:                 $borderbot = 'border-bottom: 1px solid black;';
  718:             }
  719: 
  720:             # href is a reference to another submenu
  721:             if (ref($href) eq 'ARRAY') {
  722:                 $menu .= '<li style="margin:0;padding:0;'.$bordertop . $borderbot . '">';
  723:                 $menu .= '<p><span class="LC_primary_menu_innertitle">'
  724:                                         . $title . '</span><span class="LC_primary_menu_innerarrow">&#9654;</span></p>';
  725:                 $menu .= '<ul>';
  726:                 $menu .= &build_submenu($target, $href, $translate);
  727:                 $menu .= '</ul>';
  728:                 $menu .= '</li>';
  729:             } else {    # href is the actual hyperlink and does not represent another submenu
  730:                         # for the current menu title
  731:                 if ($href =~ /(aboutme|rss\.html)$/) {
  732:                     next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne ''));
  733:                     $href =~ s/\[domain\]/$env{'user.domain'}/g;
  734:                     $href =~ s/\[user\]/$env{'user.name'}/g;
  735:                 } elsif (($href =~ m{^/adm/preferences\?}) && ($href =~ /\[returnurl\]/)) {
  736:                     my $returnurl = $ENV{'REQUEST_URI'};
  737:                     if ($ENV{'REQUEST_URI'} =~ m{/adm/preferences\?action=(?:changedomcoord|authorsettings)\&returnurl=([^\&]+)$}) {
  738:                         $returnurl = $1;
  739:                     }
  740:                     if (($returnurl =~ m{^/adm/createuser($|\?action=)}) ||
  741:                         ($returnurl =~ m{^/priv/$match_domain/$match_username}) ||
  742:                         ($returnurl =~ m{^/res(/?$|/$match_domain/$match_username)})) {
  743:                         $returnurl =~ s{\?.*$}{};
  744:                         $returnurl = '&amp;returnurl='.&HTML::Entities::encode($returnurl,'"<>&\'');
  745:                     } else {
  746:                         undef($returnurl);
  747:                     }
  748:                     $href =~ s/\[returnurl\]/$returnurl/;
  749:                 }
  750:                 unless (($href eq '') || ($href =~ /^\#/)) {
  751:                     $target = ' target="_top"';
  752:                 }
  753: 
  754:                 $menu .= '<li ';
  755:                 $menu .= ($listclass?'class="'.$listclass.'" ':'');
  756:                 $menu .= 'style="margin:0;padding:0;'. $bordertop . $borderbot .'">';
  757:                 $menu .= '<a href="'.$href.'"'.$target.' '.$linkattr.'>' .  $title . '</a>';
  758:                 $menu .= '</li>';
  759:             }
  760:         }
  761:     }
  762:     return $menu;
  763: }
  764: 
  765: sub registerurl {
  766:     my ($forcereg) = @_;
  767:     my $result = '';
  768:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  769:     my $force_title='';
  770:     if ($env{'request.state'} eq 'construct') {
  771:         $force_title=&Apache::lonxml::display_title();
  772:     }
  773:     if (($env{'environment.remote'} ne 'on') ||
  774:         ((($env{'request.publicaccess'}) ||
  775:          (!&Apache::lonnet::is_on_map(
  776:            &unescape($env{'request.noversionuri'})))) &&
  777:         (!$forcereg))) {
  778:         return
  779:         $result
  780:        .'<script type="text/javascript">'."\n"
  781:        .'// <![CDATA['."\n"
  782:        .'function LONCAPAreg(){;} function LONCAPAstale(){}'."\n"
  783:        .'// ]]>'."\n"
  784:        .'</script>'
  785:        .$force_title;
  786:     }
  787: # Graphical display after login only
  788:     if ($env{'request.registered'} && !$forcereg) { return ''; }
  789:     $result.=&innerregister($forcereg);
  790:     return $result.$force_title;
  791: }
  792: 
  793: sub innerregister {
  794:     my ($forcereg,$bread_crumbs,$group,$pagebuttonshide,$hostname) = @_;
  795:     my $const_space = ($env{'request.state'} eq 'construct');
  796:     my $is_const_dir = 0;
  797: 
  798:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  799: 
  800:     $env{'request.registered'} = 1;
  801: 
  802:     my $noremote = ($env{'environment.remote'} ne 'on');
  803: 
  804:     undef(@inlineremote);
  805: 
  806:     my $reopen=&Apache::lonmenu::reopenmenu();
  807: 
  808:     my $newmail='';
  809: 
  810:     if (&Apache::lonmsg::newmail() && !$noremote) {
  811:         # We have new mail and remote is up
  812:         $newmail= 'swmenu.setstatus("you have","messages");';
  813:     }
  814: 
  815:     my ($mapurl,$resurl,$crstype,$navmap);
  816: 
  817:     if ($env{'request.course.id'}) {
  818: #
  819: #course_type:  Course or Community
  820: #
  821:         $crstype = &Apache::loncommon::course_type();
  822:         if ($env{'request.symb'}) {
  823:             my $ignorenull;
  824:             unless ($env{'request.noversionuri'} eq '/adm/navmaps') {
  825:                 $ignorenull = 1;
  826:             }
  827:             my $symb = &Apache::lonnet::symbread('','',$ignorenull);
  828:             ($mapurl, my $rid, $resurl) = &Apache::lonnet::decode_symb($symb);
  829:             my $coursetitle = $env{'course.'.$env{'request.course.id'}.'.description'};
  830: 
  831:             my $maptitle = &Apache::lonnet::gettitle($mapurl);
  832:             my $restitle = &Apache::lonnet::gettitle($symb);
  833: 
  834:             my (@crumbs,@mapcrumbs);
  835:             if (($env{'request.noversionuri'} ne '/adm/navmaps') && ($mapurl ne '') &&
  836:                 ($mapurl ne $env{'course.'.$env{'request.course.id'}.'.url'})) {
  837:                 $navmap = Apache::lonnavmaps::navmap->new();
  838:                 if (ref($navmap)) {
  839:                     @mapcrumbs = $navmap->recursed_crumbs($mapurl,$restitle);
  840:                 }
  841:             }
  842:             unless (($forcereg) && 
  843:                     ($env{'request.noversionuri'} eq '/adm/navmaps') &&
  844:                     ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'})) {
  845:                 @crumbs = ({text  => $crstype.' Contents', 
  846:                             href  => "Javascript:gopost('/adm/navmaps','')"});
  847:             }
  848:             if ($mapurl ne $env{'course.'.$env{'request.course.id'}.'.url'}) { 
  849:                 if (@mapcrumbs) {
  850:                     push(@crumbs,@mapcrumbs);
  851:                 } else {
  852:                     push(@crumbs, {text  => '...',
  853:                                    no_mt => 1});
  854:                 }
  855:             }
  856: 
  857:             unless ((@mapcrumbs) || (!$maptitle) || ($maptitle eq 'default.sequence') ||
  858:                     ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'})) {
  859:                 push @crumbs, {text => $maptitle, no_mt => 1,
  860:                                href => &Apache::lonnet::clutter($mapurl).'?navmap=1'};
  861:             }
  862:             if ($restitle && !@mapcrumbs) {
  863:                 push(@crumbs,{text => $restitle, no_mt => 1});
  864:             }
  865:             my @tools;
  866:             if ($env{'request.filename'} =~ /\.page$/) {
  867:                 my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
  868:                 if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
  869:                     @tools = @{$breadcrumb_tools{'tools'}};
  870:                 }
  871:             }
  872:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  873:             &Apache::lonhtmlcommon::add_breadcrumb(@crumbs);
  874:             if (@tools) {
  875:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',@tools);
  876:             }
  877:         } else {
  878:             $resurl = $env{'request.noversionuri'};
  879:             my $courseurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
  880:             my $title = &mt('View Resource');
  881:             if ($resurl =~ m{^\Q/uploaded$courseurl/supplemental/\E(default|\d+)/}) {
  882:                 &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['folderpath','title']);
  883:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  884:                 if ($env{'form.title'}) {
  885:                     $title = $env{'form.title'};
  886:                 }
  887:                 my $trail;
  888:                 if ($env{'form.folderpath'}) {
  889:                     &prepare_functions($resurl,$forcereg,$group,undef,undef,1,$hostname);
  890:                     ($trail) =
  891:                         &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  892:                 } else {
  893:                     &Apache::lonhtmlcommon::add_breadcrumb(
  894:                     {text  => "Supplemental $crstype Content",
  895:                      href  => "javascript:gopost('/adm/supplemental','')"});
  896:                     $title = &mt('View Resource');
  897:                     ($trail) =
  898:                         &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  899:                 }
  900:                 return $trail;
  901:             } elsif ($resurl =~ m{^\Q/uploaded$courseurl/portfolio/syllabus/}) {
  902:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  903:                 &prepare_functions('/public'.$courseurl."/syllabus",
  904:                                    $forcereg,$group,undef,undef,1,$hostname);
  905:                 $title = &mt('Syllabus File');
  906:                 my ($trail) =
  907:                     &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1,$hostname);
  908:                 return $trail;
  909:             }
  910:             unless ($env{'request.state'} eq 'construct') {
  911:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  912:                 &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  913:             }
  914:         }
  915:     } elsif (! $const_space){
  916:         #a situation when we're looking at a resource outside of context of a 
  917:         #course or construction space (e.g. with cumulative rights)
  918:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  919:         unless ($env{'request.noversionuri'} =~ m{^/adm/$match_domain/$match_username/aboutme$}) {
  920:             &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  921:         }
  922:     }
  923:     my $timesync   = ( $noremote ? '' : 'swmenu.syncclock(1000*'.time.');' );
  924: # =============================================================================
  925: # ============================ This is for URLs that actually can be registered
  926:     if ( ($env{'request.noversionuri'}!~m{^/(res/)*adm/})
  927:                        || ($forcereg)) {
  928: 
  929:         my %swtext;
  930:         if ($noremote) {
  931:             %swtext = &get_inline_text();
  932:         } else {
  933:             %swtext = &get_rc_text();
  934:         }
  935:         my $hwkadd='';
  936: 
  937:         my ($cdom,$cnum,%perms,$cfile,$switchserver,$home,$forceedit,
  938:             $forceview,$editbutton);
  939:         if (($resurl =~ m{^/adm/($match_domain)/($match_username)/aboutme$}) ||
  940:             ($env{'request.role'} !~/^(aa|ca|au)/)) {
  941:             if (($env{'environment.remote'} eq 'on') && ($env{'request.symb'})) {
  942:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  943:             }
  944:             $editbutton = &prepare_functions($resurl,$forcereg,$group,'','','',$hostname);
  945:         }
  946:         if ($editbutton eq '') {
  947:             $editbutton = &clear(6,1);
  948:         }
  949: 
  950: #
  951: # This applies in course context
  952: #
  953:         if ($env{'request.course.id'}) {
  954:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  955:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  956:             $perms{'mdc'} = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  957:             $perms{'cev'} = &Apache::lonnet::allowed('cev',$env{'request.course.id'});
  958:             my @privs;
  959:             if ($env{'request.symb'} ne '') {
  960:                 if ($env{'request.filename'}=~/$LONCAPA::assess_re/) {
  961:                     push(@privs,('mgr','vgr'));
  962:                 }
  963:                 push(@privs,('opa','vpa'));
  964:             }
  965:             foreach my $priv (@privs) {
  966:                 $perms{$priv} = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
  967:                 if (!$perms{$priv} && $env{'request.course.sec'} ne '') {
  968:                     $perms{$priv} =
  969:                         &Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}");
  970:                 }
  971:             }
  972: #
  973: # Determine whether or not to show Grades and Submissions buttons
  974: #
  975:             if ($env{'request.symb'} ne '' &&
  976:                 $env{'request.filename'}=~/$LONCAPA::assess_re/) {
  977:                 if ($perms{'mgr'}) {
  978:                     $hwkadd.= &switch('','',7,2,'pgrd.png','Content Grades',
  979:                                       'grades[_4]',
  980:                                       "gocmd('/adm/grades','gradingmenu')",
  981:                                       'Content Grades');
  982:                 } elsif ($perms{'vgr'}) {
  983:                     $hwkadd .= &switch('','',7,2,'subm.png','Content Submissions',
  984:                                        'missions[_1]',
  985:                                        "gocmd('/adm/grades','submission')",
  986:                                        'Content Submissions');
  987:                 }
  988:             }
  989:             if (($env{'request.symb'} ne '') && (($perms{'opa'}) || ($perms{'vpa'}))) {
  990:                 $hwkadd .= &switch('','',7,3,'pparm.png','Content Settings',
  991:                                    'parms[_2]',"gocmd('/adm/parmset','set')",
  992:                                    'Content Settings');
  993:             }
  994: # End grades/submissions check
  995: 
  996: #
  997: # This applies to items inside a folder/page modifiable in the course.
  998: #
  999:             if (($env{'request.symb'}=~/^uploaded/) && (($perms{'mdc'}) || ($perms{'cev'}))) {
 1000:                 my $text = 'Edit Folder';
 1001:                 if (($mapurl =~ /\.page$/) ||
 1002:                     ($env{'request.symb'}=~
 1003:                          m{uploaded/$cdom/$cnum/default_\d+\.page$}))  {
 1004:                     $text = 'Edit Page';
 1005:                 }
 1006:                 $hwkadd .= &switch('','',7,4,'docs-22x22.png',$text,'parms[_2]',
 1007:                                    "gocmd('/adm/coursedocs','direct')",
 1008:                                    'Folder/Page Content');
 1009:             }
 1010: # End modifiable folder/page container check
 1011:         }
 1012: # End course context
 1013: 
 1014: # Prepare the rest of the buttons
 1015:         my ($menuitems,$got_prt,$got_wishlist,$cstritems);
 1016:         if ($const_space) {
 1017: #
 1018: # We are in construction space
 1019: #
 1020: 
 1021:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1022: 	    my ($udom,$uname,$thisdisfn) =
 1023: 		($env{'request.filename'}=~m{^\Q$londocroot/priv/\E([^/]+)/([^/]+)/(.*)$});
 1024:             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
 1025:             if ($currdir =~ m-/$-) {
 1026:                 $is_const_dir = 1;
 1027:                 if ($thisdisfn eq '') {
 1028:                     $is_const_dir = 2;
 1029:                 }
 1030:             } else {
 1031:                 $currdir =~ s|[^/]+$||;
 1032: 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
 1033: 		my $esc_currdir = &Apache::loncommon::escape_single($currdir);
 1034: #
 1035: # Probably should be in mydesk.tab
 1036: #
 1037:                 $menuitems=(<<ENDMENUITEMS);
 1038: s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
 1039: s&6&2&rtrv.png&Retrieve&version[_1]&gocstr('/adm/retrieve','/priv/$udom/$uname/$cleandisfn')&Retrieve old version
 1040: s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/priv/$udom/$uname/$cleandisfn')&Publish this resource
 1041: s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/priv/$udom/$uname/$cleandisfn')&Delete this resource
 1042: s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/priv/$udom/$uname/$cleandisfn')&Prepare a printable document
 1043: ENDMENUITEMS
 1044:                 unless ($noremote) {
 1045:                     $cstritems = $menuitems;
 1046:                     undef($menuitems);
 1047:                 }
 1048:             }
 1049:             if (ref($bread_crumbs) eq 'ARRAY') {
 1050:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
 1051:                 foreach my $crumb (@{$bread_crumbs}){
 1052:                      &Apache::lonhtmlcommon::add_breadcrumb($crumb);
 1053:                 }
 1054:             }
 1055:         } elsif ( defined($env{'request.course.id'}) && 
 1056: 		 $env{'request.symb'} ne '' ) {
 1057: #
 1058: # We are in a course and looking at a registered URL
 1059: # Should probably be in mydesk.tab
 1060: #
 1061: 
 1062: 	    $menuitems=(<<ENDMENUITEMS);
 1063: c&3&1
 1064: s&2&1&back.png&$swtext{'back'}&&gopost('/adm/flip','back:'+currentURL)&Previous content resource&&1
 1065: s&2&3&forw.png&$swtext{'forw'}&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3
 1066: c&6&3
 1067: c&8&1
 1068: c&8&2
 1069: s&8&3&prt.png&$swtext{'prt'}&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
 1070: ENDMENUITEMS
 1071:             $got_prt = 1;
 1072:             if (($env{'user.adv'}) && ($env{'request.uri'} =~ /^\/res/)
 1073:                 && (!$env{'request.enc'})) {
 1074:                 # wishlist is only available for users with access to resource-pool
 1075:                 # and links can only be set for resources within the resource-pool
 1076:                 $menuitems .= (<<ENDMENUITEMS);
 1077: s&9&1&alnk.png&$swtext{'alnk'}&linkstor[_1]&set_wishlistlink('',currentURL)&Save a link for this resource in my personal Stored Links repository&&1
 1078: ENDMENUITEMS
 1079:                 $got_wishlist = 1;
 1080:             }
 1081: 
 1082: my $currentURL = &Apache::loncommon::get_symb();
 1083: my ($symb_old,$symb_old_enc) = &Apache::loncommon::clean_symb($currentURL);
 1084: my $annotation = &Apache::loncommon::get_annotation($symb_old,$symb_old_enc);
 1085: $menuitems.="s&9&3&";
 1086: if(length($annotation) > 0){
 1087: 	$menuitems.="anot2.png";
 1088: }else{
 1089: 	$menuitems.="anot.png";
 1090: }
 1091: $menuitems.="&$swtext{'anot'}&tations[_1]&annotate()&";
 1092: $menuitems.="Make notes and annotations about this resource&&1\n";
 1093: my $is_mobile;
 1094: if ($env{'browser.mobile'}) {
 1095:     $is_mobile = 1;
 1096: }
 1097: 
 1098:             unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio)(\?|$)/) {
 1099: 		if ((!$env{'request.enc'}) && ($env{'request.noversionuri'} !~ m{^/adm/wrapper/ext/}) &&
 1100:                     ($env{'request.noversionuri'} !~ m{^/uploaded/$match_domain/$match_courseid/(docs/|default_\d+\.page$)}) &&
 1101:                     ($env{'request.noversionuri'} !~ m{^/adm/.+/ext\.tool$})) {
 1102: 		    $menuitems.=(<<ENDREALRES);
 1103: s&6&3&catalog.png&$swtext{'catalog'}&info[_1]&catalog_info(currentURL,'$is_mobile')&Show Metadata
 1104: ENDREALRES
 1105:                 }
 1106:                 unless (($env{'request.noversionuri'} =~ m{^/uploaded/$match_domain/$match_courseid/(docs/|default_\d+\.page$)}) ||
 1107:                         ($env{'request.noversionuri'} =~ m{^\Q/adm/wrapper/\E(ext|uploaded)/}) ||
 1108:                         ($env{'request.noversionuri'} =~ m{^/adm/.+/ext\.tool$})) { 
 1109: 	            $menuitems.=(<<ENDREALRES);
 1110: s&8&1&eval.png&$swtext{'eval'}&this[_1]&gopost('/adm/evaluate',currentURL,1)&Provide my evaluation of this resource
 1111: ENDREALRES
 1112:                 }
 1113:                 unless ($env{'request.noversionuri'} =~ m{^\Q/adm/wrapper/\E(ext|uploaded)/}) {
 1114:                     $menuitems.=(<<ENDREALRES);
 1115: s&8&2&fdbk.png&$swtext{'fdbk'}&discuss[_1]&gopost('/adm/feedback',currentURL,1)&Provide feedback messages or contribute to the course discussion about this resource
 1116: ENDREALRES
 1117:                 }
 1118: 	    }
 1119:         }
 1120: 	if ($env{'request.uri'} =~ /^\/res/) {
 1121:             unless ($got_prt) {
 1122: 	        $menuitems .= (<<ENDMENUITEMS);
 1123: s&8&3&prt.png&$swtext{'prt'}&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
 1124: ENDMENUITEMS
 1125:                 $got_prt = 1;
 1126:             }
 1127:             unless ($got_wishlist) {
 1128:                 if (($env{'user.adv'}) && (!$env{'request.enc'})) {
 1129:                     # wishlist is only available for users with access to resource-pool
 1130:                     $menuitems .= (<<ENDMENUITEMS);
 1131: s&9&1&alnk.png&$swtext{'alnk'}&linkstor[_1]&set_wishlistlink('',currentURL)&Save a link for this resource in your personal Stored Links repository&&1
 1132: ENDMENUITEMS
 1133:                     $got_wishlist = 1;
 1134:                 }
 1135:             }
 1136: 	}
 1137:         unless ($got_prt) {
 1138:             $menuitems .= (<<ENDMENUITEMS);
 1139: c&8&3
 1140: ENDMENUITEMS
 1141:         }
 1142:         unless ($got_wishlist) {
 1143:             $menuitems .= (<<ENDMENUITEMS);
 1144: c&9&1
 1145: ENDMENUITEMS
 1146:         }
 1147:         my $buttons='';
 1148:         foreach (split(/\n/,$menuitems)) {
 1149: 	    my ($command,@rest)=split(/\&/,$_);
 1150:             my $idx=10*$rest[0]+$rest[1];
 1151:             if (&hidden_button_check() eq 'yes') {
 1152:                 if ($idx == 21 ||$idx == 23) {
 1153:                     $buttons.=&switch('','',@rest);
 1154:                 } else {
 1155:                     $buttons.=&clear(@rest);
 1156:                 }
 1157:             } else {  
 1158:                 if ($command eq 's') {
 1159: 	            $buttons.=&switch('','',@rest);
 1160:                 } else {
 1161:                     $buttons.=&clear(@rest);
 1162:                 }
 1163:             }
 1164:         }
 1165:         if ($noremote) {
 1166: 	    my $addremote=0;
 1167: 	    foreach (@inlineremote) { if ($_ ne '') { $addremote=1; last;} }
 1168:             if ($addremote) {
 1169:                 my ($countdown,$buttonshide);
 1170:                 if ($env{'request.filename'} =~ /\.page$/) {
 1171:                     my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
 1172:                     if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
 1173:                         $countdown = $breadcrumb_tools{'tools'}->[0];
 1174:                     }
 1175:                     $buttonshide = $pagebuttonshide;
 1176:                 } else {
 1177:                     $countdown = &countdown_timer();
 1178:                     $buttonshide = &hidden_button_check();
 1179:                 }
 1180: 
 1181:                 &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1182: 
 1183:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1184:                     'navigation', @inlineremote[21,23]);
 1185: 
 1186:                 if ($buttonshide eq 'yes') {
 1187:                     if ($countdown) {
 1188:                         &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$countdown);
 1189:                     }
 1190:                 } else {
 1191:                     my @tools = @inlineremote[93,91,81,82,83];
 1192:                     if ($countdown) {
 1193:                         unshift(@tools,$countdown);
 1194:                     }
 1195:                     &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1196:                         'tools',@tools);
 1197: 
 1198:                     #publish button in construction space
 1199:                     if ($env{'request.state'} eq 'construct'){
 1200:                         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1201:                             'advtools', $inlineremote[63]);
 1202:                     } else {
 1203:                         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1204:                             'tools', $inlineremote[63]);
 1205:                     }
 1206:                     &advtools_crumbs(@inlineremote);
 1207:                 }
 1208:             }
 1209:             my ($topic_help,$topic_help_text);
 1210:             if ($is_const_dir == 2) {
 1211:                 if ((($ENV{'SERVER_PORT'} == 443) ||
 1212:                      ($Apache::lonnet::protocol{$Apache::lonnet::perlvar{'lonHostID'}} eq 'https')) &&
 1213:                      (&Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},'webdav'))) {
 1214:                     $topic_help = 'Authoring_WebDAV,Authoring_WebDAV_Mac_10v6,Authoring_WebDAV_Mac_10v10,'.
 1215:                                  'Authoring_WebDAV_Windows_v7,Authoring_WebDAV_Linux_Centos';
 1216:                     $topic_help_text = 'About WebDAV access';
 1217:                 }
 1218:             }
 1219:             return   &Apache::lonhtmlcommon::scripttag('', 'start')
 1220:                    . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0,'','','','',$topic_help,$topic_help_text)
 1221:                    . &Apache::lonhtmlcommon::scripttag('', 'end');
 1222: 
 1223:         } else {
 1224:             my $cstrcrumbs;
 1225:             if ($const_space) {
 1226:                 foreach (split(/\n/,$cstritems)) {
 1227:                     my ($command,@rest)=split(/\&/,$_);
 1228:                     my $idx=10*$rest[0]+$rest[1];
 1229:                     &switch('','',@rest);
 1230:                 }
 1231:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('advtools',
 1232:                                                             @inlineremote[63,61,71,72]);
 1233: 
 1234:                 $cstrcrumbs = &Apache::lonhtmlcommon::scripttag('', 'start')
 1235:                              .&Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
 1236:                              .&Apache::lonhtmlcommon::scripttag('', 'end');
 1237:             }
 1238:             my $requri=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 1239:             $requri=&Apache::lonenc::check_encrypt(&unescape($requri));
 1240:             my $cursymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 1241:             my $navstatus=&get_nav_status();
 1242:             my $clearcstr;
 1243: 
 1244:             if ($env{'user.adv'}) { $clearcstr='clearbut(6,1)'; }
 1245:             return <<ENDREGTHIS;
 1246: 
 1247: <script type="text/javascript">
 1248: // <![CDATA[
 1249: // BEGIN LON-CAPA Internal
 1250: var swmenu=null;
 1251: 
 1252:     function LONCAPAreg() {
 1253:           swmenu=$reopen;
 1254:           swmenu.clearTimeout(swmenu.menucltim);
 1255:           $timesync
 1256:           $newmail
 1257:           $buttons
 1258:           swmenu.currentURL="$requri";
 1259:           swmenu.reloadURL=swmenu.currentURL+window.location.search;
 1260:           swmenu.currentSymb="$cursymb";
 1261:           swmenu.reloadSymb="$cursymb";
 1262:           swmenu.currentStale=0;
 1263:           $navstatus
 1264:           $hwkadd
 1265:           $editbutton
 1266:     }
 1267: 
 1268:     function LONCAPAstale() {
 1269:           swmenu=$reopen
 1270:           swmenu.currentStale=1;
 1271:           if (swmenu.reloadURL!='' && swmenu.reloadURL!= null) {
 1272:              swmenu.switchbutton
 1273:              (3,1,'reload.gif','return','location','go(reloadURL)','Return to the last known location in the course sequence');
 1274:           }
 1275:           swmenu.clearbut(7,2);
 1276:           swmenu.clearbut(7,3);
 1277:           swmenu.menucltim=swmenu.setTimeout(
 1278:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
 1279:  'clearbut(9,1);clearbut(9,3);clearbut(6,3);$clearcstr',
 1280:                           2000);
 1281:       }
 1282: 
 1283: // END LON-CAPA Internal
 1284: // ]]>
 1285: </script>
 1286: 
 1287: $cstrcrumbs
 1288: ENDREGTHIS
 1289:         }
 1290:     } else {
 1291:        unless ($noremote) {
 1292: # Not registered, graphical
 1293:            return (<<ENDDONOTREGTHIS);
 1294: 
 1295: <script type="text/javascript">
 1296: // <![CDATA[
 1297: // BEGIN LON-CAPA Internal
 1298: var swmenu=null;
 1299: 
 1300:     function LONCAPAreg() {
 1301:           swmenu=$reopen
 1302:           $timesync
 1303:           swmenu.currentStale=1;
 1304:           swmenu.clearbut(2,1);
 1305:           swmenu.clearbut(2,3);
 1306:           swmenu.clearbut(8,1);
 1307:           swmenu.clearbut(8,2);
 1308:           swmenu.clearbut(8,3);
 1309:           swmenu.clearbut(9,1);
 1310:           if (swmenu.currentURL) {
 1311:              swmenu.switchbutton
 1312:               (3,1,'reload.gif','return','location','go(currentURL)');
 1313:           } else {
 1314:               swmenu.clearbut(3,1);
 1315:           }
 1316:     }
 1317: 
 1318:     function LONCAPAstale() {
 1319:     }
 1320: 
 1321: // END LON-CAPA Internal
 1322: // ]]>
 1323: </script>
 1324: ENDDONOTREGTHIS
 1325: 
 1326:         }
 1327:         return '';
 1328:     }
 1329: }
 1330: 
 1331: sub get_inline_text {
 1332:     my %text = (
 1333:                  pgrd     => 'Content Grades',
 1334:                  subm     => 'Content Submissions',
 1335:                  pparm    => 'Content Settings',
 1336:                  docs     => 'Folder/Page Content',
 1337:                  pcstr    => 'Edit',
 1338:                  prt      => 'Print',
 1339:                  alnk     => 'Stored Links',
 1340:                  anot     => 'Notes',
 1341:                  catalog  => 'Info',
 1342:                  eval     => 'Evaluate',
 1343:                  fdbk     => 'Feedback',
 1344:     );
 1345:     return %text;
 1346: }
 1347: 
 1348: sub get_rc_text {
 1349:     my %text = (
 1350:                    pgrd    => 'problem[_1]',
 1351:                    subm    => 'view sub-[_1]',
 1352:                    pparm   => 'problem[_2]',
 1353:                    pcstr   => 'edit[_1]',
 1354:                    prt     => 'prepare[_1]',
 1355:                    back    => 'backward[_1]',
 1356:                    forw    => 'forward[_1]',
 1357:                    alnk    => 'add to[_1]',
 1358:                    anot    => 'anno-[_1]',
 1359:                    catalog => 'catalog[_2]',
 1360:                    eval    => 'evaluate[_1]',
 1361:                    fdbk    => 'feedback[_1]',
 1362:     );
 1363:     return %text;
 1364: }
 1365: 
 1366: sub loadevents() {
 1367:     if ($env{'request.state'} eq 'construct' ||
 1368:         $env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
 1369:     return 'LONCAPAreg();';
 1370: }
 1371: 
 1372: sub unloadevents() {
 1373:     if ($env{'request.state'} eq 'construct' ||
 1374:         $env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
 1375:     return 'LONCAPAstale();';
 1376: }
 1377: 
 1378: sub startupremote {
 1379:     my ($lowerurl)=@_;
 1380:     unless ($env{'environment.remote'} eq 'on') {
 1381:         return ('<meta HTTP-EQUIV="Refresh" CONTENT="0.5; url='.$lowerurl.'" />');
 1382:     }
 1383: #
 1384: # The Remote actually gets launched!
 1385: #
 1386:     my $configmenu=&rawconfig();
 1387:     my $esclowerurl=&escape($lowerurl);
 1388:     my $message=&mt('"Waiting for Remote Control window to load: "+[_1]','waited');
 1389:     return(<<ENDREMOTESTARTUP);
 1390: <script type="text/javascript">
 1391: // <![CDATA[
 1392: var timestart;
 1393: function wheelswitch() {
 1394:     if (typeof(document.wheel) != 'undefined') {
 1395:         if (typeof(document.wheel.spin) != 'undefined') {
 1396:             var date=new Date();
 1397:             var waited=Math.round(30-((date.getTime()-timestart)/1000));
 1398:             document.wheel.spin.value=$message;
 1399:         }
 1400:     }
 1401:    if (window.status=='|') {
 1402:       window.status='/';
 1403:    } else {
 1404:       if (window.status=='/') {
 1405:          window.status='-';
 1406:       } else {
 1407:          if (window.status=='-') {
 1408:             window.status='\\\\';
 1409:          } else {
 1410:             if (window.status=='\\\\') { window.status='|'; }
 1411:          }
 1412:       }
 1413:    }
 1414: }
 1415: 
 1416: // ---------------------------------------------------------- The wait function
 1417: var canceltim;
 1418: function wait() {
 1419:    if ((menuloaded==1) || (tim==1)) {
 1420:       window.status='Done.';
 1421:       if (tim==0) {
 1422:          clearTimeout(canceltim);
 1423:          $configmenu
 1424:          window.location='$lowerurl';
 1425:       } else {
 1426:           window.location='/adm/remote?action=collapse&url=$esclowerurl';
 1427:       }
 1428:    } else {
 1429:       wheelswitch();
 1430:       setTimeout('wait();',200);
 1431:    }
 1432: }
 1433: 
 1434: function main() {
 1435:    canceltim=setTimeout('tim=1;',30000);
 1436:    window.status='-';
 1437:    var date=new Date();
 1438:    timestart=date.getTime();
 1439:    wait();
 1440: }
 1441: 
 1442: // ]]>
 1443: </script>
 1444: ENDREMOTESTARTUP
 1445: }
 1446: 
 1447: sub setflags() {
 1448:     return(<<ENDSETFLAGS);
 1449: <script type="text/javascript">
 1450: // <![CDATA[
 1451:     menuloaded=0;
 1452:     tim=0;
 1453: // ]]>
 1454: </script>
 1455: ENDSETFLAGS
 1456: }
 1457: 
 1458: sub maincall() {
 1459:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1460:     return(<<ENDMAINCALL);
 1461: <script type="text/javascript">
 1462: // <![CDATA[
 1463:     main();
 1464: // ]]>
 1465: </script>
 1466: ENDMAINCALL
 1467: }
 1468: 
 1469: sub load_remote_msg {
 1470:     my ($lowerurl)=@_;
 1471: 
 1472:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1473: 
 1474:     my $esclowerurl=&escape($lowerurl);
 1475:     my $link=&mt('[_1]Continue[_2] on in Inline Menu mode'
 1476:                 ,'<a href="/adm/remote?action=collapse&amp;url='.$esclowerurl.'">'
 1477:                 ,'</a>');
 1478:     return(<<ENDREMOTEFORM);
 1479: <p>
 1480: <form name="wheel">
 1481: <input name="spin" type="text" size="60" />
 1482: </form>
 1483: </p>
 1484: <p>$link</p>
 1485: ENDREMOTEFORM
 1486: }
 1487: 
 1488: sub get_menu_name {
 1489:     my $hostid = $Apache::lonnet::perlvar{'lonHostID'};
 1490:     $hostid =~ s/\W//g;
 1491:     return 'LCmenu'.$hostid;
 1492: }
 1493: 
 1494: 
 1495: sub reopenmenu {
 1496:    unless ($env{'environment.remote'} eq 'on') { return ''; }
 1497:    my $menuname = &get_menu_name();
 1498:    my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
 1499:    return('window.open('.$nothing.',"'.$menuname.'","",false);');
 1500: }
 1501: 
 1502: 
 1503: sub open {
 1504:     my $returnval='';
 1505:     unless ($env{'environment.remote'} eq 'on') {
 1506:         return
 1507:         '<script type="text/javascript">'."\n"
 1508:        .'// <![CDATA['."\n"
 1509:        .'self.name="loncapaclient";'."\n"
 1510:        .'// ]]>'."\n"
 1511:        .'</script>';
 1512:     }
 1513:     my $menuname = &get_menu_name();
 1514: 
 1515: #    unless (shift eq 'unix') {
 1516: # resizing does not work on linux because of virtual desktop sizes
 1517: #       $returnval.=(<<ENDRESIZE);
 1518: #if (window.screen) {
 1519: #    self.resizeTo(screen.availWidth-215,screen.availHeight-55);
 1520: #    self.moveTo(190,15);
 1521: #}
 1522: #ENDRESIZE
 1523: #    }
 1524:     $returnval=(<<ENDOPEN);
 1525: // <![CDATA[
 1526: window.status='Opening LON-CAPA Remote Control';
 1527: var menu=window.open("/res/adm/pages/menu.html?inhibitmenu=yes","$menuname",
 1528: "height=375,width=150,scrollbars=no,menubar=no,top=5,left=5,screenX=5,screenY=5");
 1529: self.name='loncapaclient';
 1530: // ]]>
 1531: ENDOPEN
 1532:     return '<script type="text/javascript">'.$returnval.'</script>';
 1533: }
 1534: 
 1535: sub get_editbutton {
 1536:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$forcereg,$hostname) = @_;
 1537:     my $jscall;
 1538:     if (($forceview) && ($env{'form.todocs'})) {
 1539:         my ($folderpath,$command,$navmap);
 1540:         if ($env{'request.symb'}) {
 1541:             $folderpath = &Apache::loncommon::symb_to_docspath($env{'request.symb'},\$navmap);
 1542:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
 1543:             $folderpath = $env{'form.folderpath'};
 1544:             $command = '&forcesupplement=1';
 1545:         }
 1546:         $folderpath = &escape(&HTML::Entities::encode(&escape($folderpath),'<>&"'));
 1547:         $jscall = "go('/adm/coursedocs?folderpath=$folderpath$command')";
 1548:     } else {
 1549:         my $suppanchor;
 1550:         if ($env{'form.folderpath'}) {
 1551:             $suppanchor = $env{'form.anchor'};
 1552:         }
 1553:         $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver,
 1554:                                                 $forceedit,$forcereg,$env{'request.symb'},
 1555:                                                 &escape($env{'form.folderpath'}),
 1556:                                                 &escape($env{'form.title'}),$hostname,
 1557:                                                 $env{'form.idx'},&escape($env{'form.suppurl'}),
 1558:                                                 $env{'form.todocs'},$suppanchor);
 1559:     }
 1560:     if ($jscall) {
 1561:         my $icon = 'pcstr.png';
 1562:         my $label = 'Edit';
 1563:         if ($forceview) {
 1564:             $icon = 'tolastloc.png';
 1565:             $label = 'Exit Editing';
 1566:         }
 1567:         my $infunc = 1;
 1568:         my $clearbutton;
 1569:         if ($env{'environment.remote'} eq 'on') {
 1570:             if ($cfile =~ m{^/priv/}) {
 1571:                 undef($infunc);
 1572:                 $label = 'edit';
 1573:             } else {
 1574:                 $clearbutton = 1;
 1575:             }
 1576:         }
 1577:         my $editor = &switch('','',6,1,$icon,$label,'resource[_2]',
 1578:                              $jscall,"Edit this resource",'','',$infunc);
 1579:         if ($infunc) {
 1580:             return 1;
 1581:         } elsif ($clearbutton) {
 1582:             return &clear(6,1);
 1583:         } else {
 1584:             return $editor;
 1585:         }
 1586:     }
 1587:     return;
 1588: }
 1589: 
 1590: sub prepare_functions {
 1591:     my ($resurl,$forcereg,$group,$bread_crumbs,$advtools,$docscrumbs,$hostname,$forbodytag) = @_;
 1592:     unless ($env{'request.registered'}) {
 1593:         undef(@inlineremote);
 1594:     }
 1595:     my ($cdom,$cnum,%perms,$cfile,$switchserver,$home,$forceedit,
 1596:         $forceview);
 1597: 
 1598:     if ($env{'request.course.id'}) {
 1599:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1600:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1601:         $perms{'mdc'} = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 1602:     }
 1603: 
 1604:     my $editbutton = '';
 1605:     my $viewsrcbutton = '';
 1606:     my $clientip = &Apache::lonnet::get_requestor_ip();
 1607: #
 1608: # Determine whether or not to display 'Edit' or 'View Source' icon/button
 1609: #
 1610:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 1611:         my $blocked = &Apache::loncommon::blocking_status('about',$clientip,$2,$1);
 1612:         my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1613:         ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1614:             &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1615:                 &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1616:         if (($cfile) && ($home ne '') && ($home ne 'no_host') && (!$blocked)) {
 1617:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1618:                                           $forceedit,$forceview,$forcereg);
 1619:         }
 1620:     } elsif ((!$env{'request.course.id'}) &&
 1621:              ($env{'user.author'}) && ($env{'request.filename'}) &&
 1622:              ($env{'request.role'} !~/^(aa|ca|au)/)) {
 1623: #
 1624: # Currently do not have the role of author or co-author.
 1625: # Do we have authoring privileges for the resource?
 1626: #
 1627:         my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1628:         ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1629:             &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1630:                 &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1631:         if (($cfile) && ($home ne '') && ($home ne 'no_host')) {
 1632:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1633:                                           $forceedit,$forceview,$forcereg);
 1634:         }
 1635:     } elsif ($env{'request.course.id'}) {
 1636: #
 1637: # This applies in course context
 1638: #
 1639:         if (($perms{'mdc'}) &&
 1640:             (($resurl =~ m{^/?public/$cdom/$cnum/syllabus}) ||
 1641:             ($resurl =~ m{^/?uploaded/$cdom/$cnum/portfolio/syllabus/}) || 
 1642:             (($resurl =~ m{^/?uploaded/$cdom/$cnum/default_\d+\.sequence$}) && ($env{'form.navmap'})))) {
 1643:             if ($resurl =~ m{^/}) {
 1644:                 $cfile = $resurl;
 1645:             } else {
 1646:                 $cfile = "/$resurl";
 1647:             }
 1648:             $home = &Apache::lonnet::homeserver($cnum,$cdom);
 1649:             if ($env{'form.forceedit'}) {
 1650:                 $forceview = 1;
 1651:             } else {
 1652:                 $forceedit = 1;
 1653:             }
 1654:             if ($cfile =~ m{^/uploaded/$cdom/$cnum/default_\d+\.sequence$}) {
 1655:                 my $text = 'Edit Folder';
 1656:                 &switch('','',7,4,'docs-22x22.png','Edit Folder','parms[_2]',
 1657:                         "gocmd('/adm/coursedocs','direct')",
 1658:                         'Folder/Page Content');
 1659:                 $editbutton = 1;
 1660:             } else {
 1661:                 $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1662:                                               $forceedit,$forceview,$forcereg,
 1663:                                               $hostname);
 1664:             }
 1665:         } elsif (($resurl eq '/adm/extresedit') &&
 1666:                  (($env{'form.symb'}) || ($env{'form.folderpath'}))) {
 1667:             ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1668:             &Apache::lonnet::can_edit_resource($resurl,$cnum,$cdom,$resurl,
 1669:                                                $env{'form.symb'});
 1670:             if ($cfile ne '') {
 1671:                 $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1672:                                               $forceedit,$forceview,$forcereg,
 1673:                                               $env{'form.title'},$env{'form.suppurl'});
 1674:             }
 1675:         } elsif (($resurl =~ m{^/?adm/viewclasslist$}) &&
 1676:                  (&Apache::lonnet::allowed('opa',$env{'request.course.id'}))) {
 1677:             ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1678:             &Apache::lonnet::can_edit_resource($resurl,$cnum,$cdom,$resurl,
 1679:                                                $env{'form.symb'});
 1680:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1681:                                           $forceedit,$forceview,$forcereg);
 1682:         } elsif (($resurl !~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) &&
 1683:                  ($resurl ne '/cgi-bin/printout.pl')) {
 1684:             if ($env{'request.filename'}) {
 1685:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1686:                 ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1687:                     &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1688:                         &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1689:                 if ($cfile ne '') {
 1690:                     $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1691:                                                   $forceedit,$forceview,$forcereg);
 1692:                 }
 1693:                 if ((($cfile eq '') || (!$editbutton)) &&
 1694:                     ($resurl =~ /$LONCAPA::assess_re/)) {
 1695:                     my $showurl = &Apache::lonnet::clutter($resurl);
 1696:                     my $crs_sec = $env{'request.course.id'} . (($env{'request.course.sec'} ne '')
 1697:                                                               ? "/$env{'request.course.sec'}"
 1698:                                                               : '');
 1699:                     if ((&Apache::lonnet::allowed('cre','/')) &&
 1700:                         (&Apache::lonnet::metadata($resurl,'sourceavail') eq 'open')) {
 1701:                         $viewsrcbutton = 1;
 1702:                     } elsif (&Apache::lonnet::allowed('vxc',$crs_sec)) {
 1703:                         if ($showurl =~ m{^\Q/res/$cdom/\E($match_username)/}) {
 1704:                             my $auname = $1;
 1705:                             if (($env{'request.course.adhocsrcaccess'} ne '') &&
 1706:                                 (grep(/^\Q$auname\E$/,split(/,/,$env{'request.course.adhocsrcaccess'})))) {
 1707:                                 $viewsrcbutton = 1;
 1708:                             } elsif ((&Apache::lonnet::metadata($resurl,'sourceavail') eq 'open') &&
 1709:                                      (&Apache::lonnet::allowed('bre',$crs_sec))) {
 1710:                                 $viewsrcbutton = 1;
 1711:                             }
 1712:                         }
 1713:                     }
 1714:                     if ($viewsrcbutton) {
 1715:                         &switch('','',6,1,'pcstr.png','View Source','resource[_2]','open_source()',
 1716:                                 'View source code');
 1717:                     }
 1718:                 }
 1719:             }
 1720:         }
 1721:     }
 1722: # End determination of 'Edit' icon/button display
 1723: 
 1724:     if ($env{'request.course.id'}) {
 1725: # This applies to about me page for users in a course
 1726:         if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 1727:             my ($sdom,$sname) = ($1,$2);
 1728:             unless (&Apache::lonnet::is_course($sdom,$sname)) {
 1729:                 my $blocked = &Apache::loncommon::blocking_status('about',$clientip,$sname,$sdom);
 1730:                 unless ($blocked) {
 1731:                     &switch('','',6,4,'mail-message-new-22x22.png','Message to user',
 1732:                             '',
 1733:                             "go('/adm/email?compose=individual&amp;recname=$sname&amp;recdom=$sdom')",
 1734:                                 'Send message to specific user','','',1);
 1735:                 }
 1736:             }
 1737:             my $hideprivileged = 1;
 1738:             if (&Apache::lonnet::in_course($sdom,$sname,$cdom,$cnum,undef,
 1739:                                            $hideprivileged)) {
 1740:                 foreach my $priv ('vsa','vgr','srm') {
 1741:                     $perms{$priv} = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
 1742:                     if (!$perms{$priv} && $env{'request.course.sec'} ne '') {
 1743:                         $perms{$priv} =
 1744:                             &Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}");
 1745:                     }
 1746:                 }
 1747:                 if ($perms{'vsa'}) {
 1748:                     &switch('','',6,5,'trck-22x22.png','Activity',
 1749:                             '',
 1750:                             "go('/adm/trackstudent?selected_student=$sname:$sdom')",
 1751:                             'View recent activity by this person','','',1);
 1752:                 }
 1753:                 if ($perms{'vgr'}) {
 1754:                     &switch('','',6,6,'rsrv-22x22.png','Reservations',
 1755:                             '',
 1756:                             "go('/adm/slotrequest?command=showresv&amp;origin=aboutme&amp;uname=$sname&amp;udom=$sdom')",
 1757:                             'Slot reservation history','','',1);
 1758:                 }
 1759:                 if ($perms{'srm'}) {
 1760:                     &switch('','',6,7,'contact-new-22x22.png','Records',
 1761:                             '',
 1762:                             "go('/adm/email?recordftf=retrieve&amp;recname=$sname&amp;recdom=$sdom')",
 1763:                             'Add records','','',1);
 1764:                 }
 1765:             }
 1766:         }
 1767:         if (($env{'form.folderpath'} =~ /^supplemental/) &&
 1768:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
 1769:             (($resurl =~ m{^/adm/wrapper/ext/}) ||
 1770:              ($resurl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) ||
 1771:              ($resurl =~ m{^/uploaded/$cdom/$cnum/supplemental/}) ||
 1772:              ($resurl eq '/adm/supplemental') ||
 1773:              ($resurl =~ m{^/public/$cdom/$cnum/syllabus$}) ||
 1774:              ($resurl =~ m{^/adm/$match_domain/$match_username/aboutme$}))) {
 1775:             my @folders=split('&',$env{'form.folderpath'});
 1776:             if ((@folders > 2) || ($resurl ne '/adm/supplemental')) {
 1777:                 my $suppanchor;
 1778:                 if ($resurl =~ m{^/adm/wrapper/ext/}) {
 1779:                     $suppanchor = $env{'form.anchor'};
 1780:                 }
 1781:                 my $esc_path=&escape(&HTML::Entities::encode(&escape($env{'form.folderpath'}),'<>&"'));
 1782:                 my $link = '/adm/coursedocs?command=direct&amp;forcesupplement=1&amp;supppath='.
 1783:                            "$esc_path&amp;anchor=$suppanchor";
 1784:                 if ($env{'request.use_absolute'} ne '') {
 1785:                     $link = $env{'request.use_absolute'}.$link;
 1786:                 }
 1787:                 &switch('','',7,4,'docs-22x22.png','Edit Folder','parms[_2]',
 1788:                         "location.href='$link'",'Folder/Page Content');
 1789:             }
 1790:         }
 1791:     }
 1792: 
 1793: # End checking for items for about me page for users in a course
 1794:     if ($docscrumbs) {
 1795:         &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1796:         &advtools_crumbs(@inlineremote);
 1797:         return $editbutton;
 1798:     } elsif (($env{'request.registered'}) && (!ref($forbodytag))) {
 1799:         return $editbutton || $viewsrcbutton;
 1800:     } else {
 1801:         if (ref($bread_crumbs) eq 'ARRAY') {
 1802:             if (@inlineremote > 0) {
 1803:                 if (ref($advtools) eq 'ARRAY') {
 1804:                     @{$advtools} = @inlineremote;
 1805:                 }
 1806:             }
 1807:             return;
 1808:         } elsif (@inlineremote > 0) {
 1809:             &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1810:             &advtools_crumbs(@inlineremote);
 1811:             if (ref($forbodytag)) {
 1812:                 $$forbodytag =
 1813:                     &Apache::lonhtmlcommon::scripttag('', 'start')
 1814:                    .&Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
 1815:                    .&Apache::lonhtmlcommon::scripttag('', 'end');
 1816:             }
 1817:             return;
 1818:         }
 1819:     }
 1820: }
 1821: 
 1822: sub advtools_crumbs {
 1823:     my @funcs = @_;
 1824:     if ($env{'request.noversionuri'} =~ m{^/adm/$match_domain/$match_username/aboutme$}) {
 1825:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1826:             'advtools', @funcs[61,64,65,66,67,74]);
 1827:     } elsif ($env{'request.noversionuri'} !~ m{^/adm/(navmaps|viewclasslist)(\?|$)}) {
 1828:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1829:             'advtools', @funcs[61,71,72,73,74,92]);
 1830:     } elsif ($env{'request.noversionuri'} eq '/adm/viewclasslist') {
 1831:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1832:             'advtools', $funcs[61]);
 1833:     }
 1834: }
 1835: 
 1836: # ================================================================== Raw Config
 1837: 
 1838: sub clear {
 1839:     my ($row,$col)=@_;
 1840:     if ($env{'environment.remote'} eq 'on') {
 1841:        if (($row<1) || ($row>13)) { return ''; }
 1842:        return "\n".qq(window.status+='.';swmenu.clearbut($row,$col););
 1843:     } else {
 1844:         $inlineremote[10*$row+$col]='';
 1845:         return '';
 1846:     }
 1847: }
 1848: 
 1849: # ============================================ Switch a button or create a link
 1850: # Switch acts on the javascript that is executed when a button is clicked.  
 1851: # The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
 1852: 
 1853: sub switch {
 1854:     my ($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat,$nobreak,$infunc)=@_;
 1855:     $act=~s/\$uname/$uname/g;
 1856:     $act=~s/\$udom/$udom/g;
 1857:     $top=&mt($top);
 1858:     $bot=&mt($bot);
 1859:     $desc=&mt($desc);
 1860:     my $idx=10*$row+$col;
 1861:     $category_members{$cat}.=':'.$idx;
 1862: 
 1863:     if (($env{'environment.remote'} eq 'on') && (!$infunc)) {
 1864:         if (($row<1) || ($row>13)) { return ''; }
 1865:         if ($env{'request.state'} eq 'construct') {
 1866:             my $text = $top.' '.$bot;
 1867:             $text=~s/\s*\-\s*//gs;
 1868:             my $pic = '<img alt="'.$text.'" src="'.
 1869:                       &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
 1870:                       '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
 1871:            $inlineremote[$idx] =
 1872:                '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.
 1873:                $pic.'<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
 1874:         }
 1875: # Remote
 1876:         $img=~s/\.png$/\.gif/;
 1877:         return "\n".
 1878:  qq(window.status+='.';swmenu.switchbutton($row,$col,"$img","$top","$bot","$act","$desc"););
 1879:     }
 1880: 
 1881: # Inline Menu
 1882:     if ($nobreak==2) { return ''; }
 1883:     my $text=$top.' '.$bot;
 1884:     $text=~s/\s*\-\s*//gs;
 1885: 
 1886:     my $pic=
 1887: 	   '<img alt="'.$text.'" src="'.
 1888: 	   &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
 1889: 	   '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
 1890:     if ($env{'browser.interface'} eq 'faketextual') {
 1891: # Main Menu
 1892: 	   if ($nobreak==3) {
 1893: 	       $inlineremote[$idx]="\n".
 1894: 		   '<td class="LC_menubuttons_text" align="right">'.$text.
 1895: 		   '</td><td align="left">'.
 1896: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td></tr>';
 1897: 	   } elsif ($nobreak) {
 1898: 	       $inlineremote[$idx]="\n<tr>".
 1899: 		   '<td align="left">'.
 1900: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td>
 1901:                     <td class="LC_menubuttons_text" align="left"><a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$text.'</span></a></td>';
 1902: 	   } else {
 1903: 	       $inlineremote[$idx]="\n<tr>".
 1904: 		   '<td align="left">'.
 1905: 		   '<a href="javascript:'.$act.';">'.$pic.
 1906: 		   '</a></td><td class="LC_menubuttons_text" colspan="3">'.
 1907: 		   '<a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$desc.'</span></a></td></tr>';
 1908: 	   }
 1909:     } else {
 1910: # Inline Menu
 1911:         my @tools = (93,91,81,82,83);
 1912:         unless ($env{'request.state'} eq 'construct') {
 1913:             push(@tools,63);
 1914:         }
 1915:         if (($env{'environment.icons'} eq 'iconsonly') &&
 1916:             (grep(/^$idx$/,@tools))) {
 1917:             $inlineremote[$idx] =
 1918:         '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.'</a>';
 1919:         } else {
 1920:             $inlineremote[$idx] =
 1921:        '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.
 1922:        '<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
 1923:         }
 1924:     }
 1925:     return '';
 1926: }
 1927: 
 1928: sub secondlevel {
 1929:     my $output='';
 1930:     my 
 1931:     ($uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat)=@_;
 1932:     if ($prt eq 'any') {
 1933: 	   $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1934:     } elsif ($prt=~/^r(\w+)/) {
 1935:         if ($rol eq $1) {
 1936:            $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1937:         }
 1938:     }
 1939:     return $output;
 1940: }
 1941: 
 1942: sub openmenu {
 1943:     my $menuname = &get_menu_name();
 1944:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1945:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
 1946:     return "window.open(".$nothing.",'".$menuname."');";
 1947: }
 1948: 
 1949: sub inlinemenu {
 1950:     undef(@inlineremote);
 1951:     undef(%category_members);
 1952: # calling rawconfig with "1" will evaluate mydesk.tab, even if there is no active remote control
 1953:     &rawconfig(1);
 1954:     my $output='<table><tr>';
 1955:     for (my $col=1; $col<=2; $col++) {
 1956:         $output.='<td class="LC_mainmenu_col_fieldset">';
 1957:         for (my $row=1; $row<=8; $row++) {
 1958:             foreach my $cat (keys(%category_members)) {
 1959:                if ($category_positions{$cat} ne "$col,$row") { next; }
 1960:                #$output.='<table><tr><td colspan="4" class="LC_menubuttons_category">'.&mt($category_names{$cat}).'</td></tr>';
 1961:                $output.='<div class="LC_Box LC_400Box">';
 1962: 	       $output.='<h3 class="LC_hcell">'.&mt($category_names{$cat}).'</h3>';
 1963:                $output.='<table>';
 1964:                my %active=();
 1965:                foreach my $menu_item (split(/\:/,$category_members{$cat})) {
 1966:                   if ($inlineremote[$menu_item]) {
 1967:                      $active{$menu_item}=1;
 1968:                   }
 1969:                }  
 1970:                foreach my $item (sort(keys(%active))) {
 1971:                   $output.=$inlineremote[$item];
 1972:                }
 1973:                $output.='</table>';
 1974:                $output.='</div>';
 1975:             }
 1976:          }
 1977:          $output.="</td>";
 1978:     }
 1979:     $output.="</tr></table>";
 1980:     return $output;
 1981: }
 1982: 
 1983: sub rawconfig {
 1984: #
 1985: # This evaluates mydesk.tab
 1986: # Need to add more positions and more privileges to deal with all
 1987: # menu items.
 1988: #
 1989:     my $textualoverride=shift;
 1990:     my $output='';
 1991:     if ($env{'environment.remote'} eq 'on') {
 1992:        $output.=
 1993:  "window.status='Opening Remote Control';var swmenu=".&openmenu().
 1994: "\nwindow.status='Configuring Remote Control ';";
 1995:     } else {
 1996:         unless ($textualoverride) { return ''; }
 1997:     }
 1998:     my $uname=$env{'user.name'};
 1999:     my $udom=$env{'user.domain'};
 2000:     my $adv=$env{'user.adv'};
 2001:     my $show_course=&Apache::loncommon::show_course();
 2002:     my $author=$env{'user.author'};
 2003:     my $crs='';
 2004:     my $crstype='';
 2005:     if ($env{'request.course.id'}) {
 2006:        $crs='/'.$env{'request.course.id'};
 2007:        if ($env{'request.course.sec'}) {
 2008: 	   $crs.='_'.$env{'request.course.sec'};
 2009:        }
 2010:        $crs=~s/\_/\//g;
 2011:        $crstype = &Apache::loncommon::course_type();
 2012:     }
 2013:     my $pub=($env{'request.state'} eq 'published');
 2014:     my $con=($env{'request.state'} eq 'construct');
 2015:     my $rol=$env{'request.role'};
 2016:     my $requested_domain;
 2017:     if ($rol) {
 2018:        $requested_domain = $env{'request.role.domain'};
 2019:     }
 2020:     foreach my $line (@desklines) {
 2021:         my ($row,$col,$pro,$prt,$img,$top,$bot,$act,$desc,$cat)=split(/\:/,$line);
 2022:         $prt=~s/\$uname/$uname/g;
 2023:         $prt=~s/\$udom/$udom/g;
 2024:         if ($prt =~ /\$crs/) {
 2025:             next unless ($env{'request.course.id'});
 2026:             next if ($crstype eq 'Community');
 2027:             $prt=~s/\$crs/$crs/g;
 2028:         } elsif ($prt =~ /\$cmty/) {
 2029:             next unless ($env{'request.course.id'});
 2030:             next if ($crstype ne 'Community');
 2031:             $prt=~s/\$cmty/$crs/g;
 2032:         }
 2033:         if ($prt =~ m/\$requested_domain/) {
 2034:             if ((!$requested_domain) && ($pro eq 'pbre') && ($env{'user.adv'})) {
 2035:                 $prt=~s/\$requested_domain/$env{'user.domain'}/g;
 2036:             } else {
 2037:                 $prt=~s/\$requested_domain/$requested_domain/g;
 2038:             }
 2039:         }
 2040:         if ($category_names{$cat}!~/\w/) { $cat='oth'; }
 2041:         if ($pro eq 'clear') {
 2042: 	    $output.=&clear($row,$col);
 2043:         } elsif ($pro eq 'any') {
 2044:                $output.=&secondlevel(
 2045: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2046: 	} elsif ($pro eq 'smp') {
 2047:             unless ($adv) {
 2048:                $output.=&secondlevel(
 2049:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2050:             }
 2051:         } elsif ($pro eq 'adv') {
 2052:             if ($adv) {
 2053:                $output.=&secondlevel(
 2054: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2055:             }
 2056: 	} elsif ($pro eq 'shc') {
 2057:             if ($show_course) {
 2058:                $output.=&secondlevel(
 2059:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2060:             }
 2061:         } elsif ($pro eq 'nsc') {
 2062:             if (!$show_course) {
 2063:                $output.=&secondlevel(
 2064: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2065:             }
 2066:         } elsif (($pro=~/^p(\w+)/) && ($prt)) {
 2067:             my $priv = $1;
 2068:             if ($priv =~ /^mdc(Course|Community)/) {
 2069:                 if ($crstype eq $1) {
 2070:                     $priv = 'mdc';
 2071:                 } else {
 2072:                     next;
 2073:                 }
 2074:             }
 2075:             if ((($priv eq 'bre') && (&Apache::lonnet::allowed($priv,$prt) eq 'F')) ||
 2076:                 (($priv ne 'bre') && (&Apache::lonnet::allowed($priv,$prt)))) {
 2077:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2078:             }
 2079:         } elsif ($pro eq 'course')  {
 2080:             if (($env{'request.course.fn'}) && ($crstype ne 'Community')) {
 2081:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2082: 	    }
 2083:         } elsif ($pro eq 'community')  {
 2084:             if (($env{'request.course.fn'}) && ($crstype eq 'Community')) {
 2085:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2086:             }
 2087:         } elsif ($pro =~ /^courseenv_(.*)$/) {
 2088:             my $key = $1;
 2089:             if ($crstype ne 'Community') {
 2090:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 2091:                 if ($key eq 'canuse_pdfforms') {
 2092:                     if ($env{'request.course.id'} && $coursepref eq '') {
 2093:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 2094:                         $coursepref = $domdefs{'canuse_pdfforms'};
 2095:                     }
 2096:                 }
 2097:                 if ($coursepref) { 
 2098:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2099:                 }
 2100:             }
 2101:         } elsif ($pro =~ /^communityenv_(.*)$/) {
 2102:             my $key = $1;
 2103:             if ($crstype eq 'Community') {
 2104:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 2105:                 if ($key eq 'canuse_pdfforms') {
 2106:                     if ($env{'request.course.id'} && $coursepref eq '') {
 2107:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 2108:                         $coursepref = $domdefs{'canuse_pdfforms'};
 2109:                     }
 2110:                 }
 2111:                 if ($coursepref) { 
 2112:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2113:                 }
 2114:             }
 2115:         } elsif ($pro =~ /^course_(.*)$/) {
 2116:             # Check for permissions inside of a course
 2117:             if (($env{'request.course.id'}) && ($crstype ne 'Community') && 
 2118:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 2119:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 2120:                  )) {
 2121:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2122: 	    }
 2123:         } elsif ($pro =~ /^community_(.*)$/) {
 2124:             # Check for permissions inside of a community
 2125:             if (($env{'request.course.id'}) && ($crstype eq 'Community') &&   
 2126:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 2127:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 2128:                  )) {
 2129:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 2130:             }
 2131:         } elsif ($pro eq 'author') {
 2132:             if ($author) {
 2133:                 if ((($prt eq 'rca') && ($env{'request.role'}=~/^ca/)) ||
 2134:                     (($prt eq 'raa') && ($env{'request.role'}=~/^aa/)) || 
 2135:                     (($prt eq 'rau') && ($env{'request.role'}=~/^au/))) {
 2136:                     # Check that we are on the correct machine
 2137:                     my $cadom=$requested_domain;
 2138:                     my $caname=$env{'user.name'};
 2139:                     if (($prt eq 'rca') || ($prt eq 'raa')) {
 2140: 		       ($cadom,$caname)=
 2141:                                ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 2142:                     }                       
 2143:                     $act =~ s/\$caname/$caname/g;
 2144:                     $act =~ s/\$cadom/$cadom/g;
 2145:                     my $home = &Apache::lonnet::homeserver($caname,$cadom);
 2146: 		    my $allowed=0;
 2147: 		    my @ids=&Apache::lonnet::current_machine_ids();
 2148: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 2149: 		    if ($allowed) {
 2150:                         $output.=&switch($caname,$cadom,
 2151:                                         $row,$col,$img,$top,$bot,$act,$desc,$cat);
 2152:                     }
 2153:                 }
 2154:             }
 2155:         } elsif ($pro eq 'tools') {
 2156:             my @tools = ('aboutme','blog','portfolio');
 2157:             if (grep(/^\Q$prt\E$/,@tools)) {
 2158:                 if (!&Apache::lonnet::usertools_access($env{'user.name'},
 2159:                                                        $env{'user.domain'},
 2160:                                                        $prt,undef,'tools')) {
 2161:                     $output.=&clear($row,$col);
 2162:                     next;
 2163:                 }
 2164:             } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
 2165:                 if (($prt eq 'reqcrsnsc') && ($show_course))   {
 2166:                     next;
 2167:                 }
 2168:                 if (($prt eq 'reqcrsshc') && (!$show_course)) {
 2169:                     next;
 2170:                 }
 2171:                 my $showreqcrs = &check_for_rcrs();
 2172:                 if (!$showreqcrs) {
 2173:                     $output.=&clear($row,$col);
 2174:                     next;
 2175:                 }
 2176:             }
 2177:             $prt='any';
 2178:             $output.=&secondlevel(
 2179:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 2180:         }
 2181:     }
 2182:     if ($env{'environment.remote'} eq 'on') {
 2183:         $output.="\nwindow.status='Synchronizing Time';swmenu.syncclock(1000*".time.");\nwindow.status='Remote Control Configured.';";
 2184:         if (&Apache::lonmsg::newmail()) {
 2185:             $output.='swmenu.setstatus("you have","messages");';
 2186:         }
 2187:     }
 2188:     return $output;
 2189: }
 2190: 
 2191: sub check_for_rcrs {
 2192:     my $showreqcrs = 0;
 2193:     my @reqtypes = ('official','unofficial','community','textbook');
 2194:     foreach my $type (@reqtypes) {
 2195:         if (&Apache::lonnet::usertools_access($env{'user.name'},
 2196:                                               $env{'user.domain'},
 2197:                                               $type,undef,'requestcourses')) {
 2198:             $showreqcrs = 1;
 2199:             last;
 2200:         }
 2201:     }
 2202:     if (!$showreqcrs) {
 2203:         foreach my $type (@reqtypes) {
 2204:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 2205:                 $showreqcrs = 1;
 2206:                 last;
 2207:             }
 2208:         }
 2209:     }
 2210:     return $showreqcrs;
 2211: }
 2212: 
 2213: # ======================================================================= Close
 2214: 
 2215: sub close {
 2216:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 2217:     my $menuname = &get_menu_name();
 2218:     return(<<ENDCLOSE);
 2219: <script type="text/javascript">
 2220: // <![CDATA[
 2221: window.status='Accessing Remote Control';
 2222: menu=window.open("/adm/rat/empty.html","$menuname",
 2223:                  "height=350,width=150,scrollbars=no,menubar=no");
 2224: window.status='Disabling Remote Control';
 2225: menu.active=0;
 2226: menu.autologout=0;
 2227: window.status='Closing Remote Control';
 2228: menu.close();
 2229: window.status='Done.';
 2230: // ]]>
 2231: </script>
 2232: ENDCLOSE
 2233: }
 2234: 
 2235: sub dc_popup_js {
 2236:     my %lt = &Apache::lonlocal::texthash(
 2237:                                           more => '(More ...)',
 2238:                                           less => '(Less ...)',
 2239:                                         );
 2240:     return <<"END";
 2241: 
 2242: function showCourseID() {
 2243:     document.getElementById('dccid').style.display='block';
 2244:     document.getElementById('dccid').style.textAlign='left';
 2245:     document.getElementById('dccid').style.textFace='normal';
 2246:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:hideCourseID();" class="LC_menubuttons_link">$lt{'less'}</a>';
 2247:     return;
 2248: }
 2249: 
 2250: function hideCourseID() {
 2251:     document.getElementById('dccid').style.display='none';
 2252:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:showCourseID()" class="LC_menubuttons_link">$lt{'more'}</a>';
 2253:     return;
 2254: }
 2255: 
 2256: END
 2257: 
 2258: }
 2259: 
 2260: sub countdown_toggle_js {
 2261:     return <<"END";
 2262: 
 2263: function toggleCountdown() {
 2264:     var countdownid = document.getElementById('duedatecountdown');
 2265:     var currstyle = countdownid.style.display;
 2266:     if (currstyle == 'inline') {
 2267:         countdownid.style.display = 'none';
 2268:         document.getElementById('ddcountcollapse').innerHTML='';
 2269:         document.getElementById('ddcountexpand').innerHTML='&#9668;&nbsp;';
 2270:     } else {
 2271:         countdownid.style.display = 'inline';
 2272:         document.getElementById('ddcountcollapse').innerHTML='&#9658;&nbsp;';
 2273:         document.getElementById('ddcountexpand').innerHTML='';
 2274:     }
 2275:     return;
 2276: }
 2277: 
 2278: END
 2279: }
 2280: 
 2281: # This creates a "done button" for timed events.  The confirmation box is a jQuery
 2282: # dialog widget. If the interval parameter requires a proctor key for the timed
 2283: # event to be marked done, there will also be a textbox where that can be entered.
 2284: # Clicking OK will set the value of LC_interval_done to 'true', and, if needed will
 2285: # set the value of LC_interval_done_proctorpass to the text entered in that box,
 2286: # and submit the corresponding form.
 2287: #
 2288: # The &zero_time() routine in lonhomework.pm is called when a page is rendered if
 2289: # LC_interval_done is true.
 2290: #
 2291: sub done_button_js {
 2292:     my ($type,$width,$height,$proctor,$donebuttontext) = @_;
 2293:     return unless (($type eq 'map') || ($type eq 'resource'));
 2294:     my %lt = &Apache::lonlocal::texthash(
 2295:                  title    => 'WARNING!',
 2296:                  preamble => 'You are trying to end this timed event early.',
 2297:                  map      => 'Confirming that you are done will cause the time to expire and prevent you from changing any answers in the current folder.',
 2298:                  resource => 'Confirming that you are done will cause the time to expire for this question, and prevent you from changing your answer(s).',
 2299:                  okdone   => 'Click "OK" if you are completely finished.',
 2300:                  cancel   => 'Click "Cancel" to continue working.',
 2301:                  proctor  => 'Ask a proctor to enter the key, then click "OK" if you are completely finished.',
 2302:                  ok       => 'OK',
 2303:                  exit     => 'Cancel',
 2304:                  key      => 'Key:',
 2305:                  nokey    => 'A proctor key is required',
 2306:     );
 2307:     my $shownsymb = &HTML::Entities::encode(&Apache::lonenc::check_encrypt($env{'request.symb'}));
 2308:     my $navmap = Apache::lonnavmaps::navmap->new();
 2309:     my ($missing,$tried) = (0,0);
 2310:     if (ref($navmap)) {
 2311:         my @resources=();
 2312:         if ($type eq 'map') {
 2313:             my ($mapurl,$rid,$resurl)=&Apache::lonnet::decode_symb($env{'request.symb'});
 2314:             if ($env{'request.symb'} =~ /\.page$/) {
 2315:                 @resources=$navmap->retrieveResources($resurl,sub { $_[0]->is_problem() });
 2316:             } else {
 2317:                 @resources=$navmap->retrieveResources($mapurl,sub { $_[0]->is_problem() });
 2318:             }
 2319:         } else {
 2320:             my $res = $navmap->getBySymb($env{'request.symb'});
 2321:             if (ref($res)) {
 2322:                 if ($res->is_problem()) {
 2323:                     push(@resources,$res);
 2324:                 }
 2325:             }
 2326:         }
 2327:         foreach my $res (@resources) {
 2328:             if (ref($res->parts()) eq 'ARRAY') {
 2329:                 foreach my $part (@{$res->parts()}) {
 2330:                     if (!$res->tries($part)) {
 2331:                         $missing++;
 2332:                     } else {
 2333:                         $tried++;
 2334:                     }
 2335:                 }
 2336:             }
 2337:         }
 2338:     }
 2339:     if ($missing) {
 2340:         $lt{'miss'} .= '<p class="LC_error">';
 2341:         if ($type eq 'map') {
 2342:             $lt{'miss'} .= &mt('Submissions are missing for [quant,_1,question part,question parts] in this folder.',$missing);
 2343:         } else {
 2344:             $lt{'miss'} .= &mt('Submissions are missing for [quant,_1,part] in this question.',$missing);
 2345:         }
 2346:         if ($missing > 1) {
 2347:             $lt{'miss'} .= ' '.&mt('If you confirm you are done you will be unable to submit answers for them.').'</span>';
 2348:         } else {
 2349:             $lt{'miss'} .= ' '.&mt('If you confirm you are done you will be unable to submit an answer for it.').'</p>';
 2350:         }
 2351:     }
 2352:     $donebuttontext = &HTML::Entities::encode($donebuttontext,'<>&"');
 2353:     if ($proctor) {
 2354:         if ($height !~ /^\d+$/) {
 2355:             $height = 400;
 2356:             if ($missing) {
 2357:                 $height += 60;
 2358:             }
 2359:         }
 2360:         if ($width !~ /^\d+$/) {
 2361:             $width = 400;
 2362:             if ($missing) {
 2363:                 $width += 60;
 2364:             }
 2365:         }
 2366:         return <<END;
 2367: <form method="post" name="LCdoneButton" action="">
 2368:     <input type="hidden" name="LC_interval_done" value="" />
 2369:     <input type="hidden" name="LC_interval_done_proctorpass" value="" />
 2370:     <input type="hidden" name="symb" value="$shownsymb" />
 2371:     <button id="LC_done-confirm-opener" type="button">$donebuttontext</button>
 2372: </form>
 2373: 
 2374: <div id="LC_done-confirm" title="$lt{'title'}">
 2375:   <p>$lt{'preamble'} $lt{$type}</p>
 2376:   $lt{'miss'}
 2377:   <p>$lt{'proctor'}</p>
 2378:   <form name="LCdoneButtonProctor" action="">
 2379:     <label>$lt{'key'}<input type="password" name="LC_interval_done_proctorkey" value="" /></label>
 2380:     <input type="submit" tabindex="-1" style="position:absolute; top:-1000px" />
 2381:   </form>
 2382:   <p>$lt{'cancel'}</p>
 2383: </div>
 2384: 
 2385: <script type="text/javascript">
 2386: // <![CDATA[
 2387:     \$( "#LC_done-confirm" ).dialog({ autoOpen: false });
 2388:     \$( "#LC_done-confirm-opener" ).on("click", function() {
 2389:         \$( "#LC_done-confirm" ).dialog("open");
 2390:         \$( "#LC_done-confirm" ).dialog({
 2391:             height: $height,
 2392:             width: $width,
 2393:             modal: true,
 2394:             resizable: false,
 2395:             buttons: [
 2396:                 {
 2397:                     text: "$lt{'ok'}",
 2398:                     click: function() {
 2399:                         var proctorkey = \$( '[name="LC_interval_done_proctorkey"]' )[0].value;
 2400:                         if ((proctorkey == '') || (proctorkey == null)) {
 2401:                             alert("$lt{'nokey'}");
 2402:                         } else {
 2403:                             \$( '[name="LC_interval_done"]' )[0].value = 'true';
 2404:                             \$( '[name="LC_interval_done_proctorpass"]' )[0].value = proctorkey;
 2405:                             \$( '[name="LCdoneButton"]' )[0].submit();
 2406:                         }
 2407:                     },
 2408:                 },
 2409:                 {
 2410:                     text: "$lt{'exit'}",
 2411:                     click: function() {
 2412:                         \$("#LC_done-confirm").dialog( "close" );
 2413:                     }
 2414:                 }
 2415:             ],
 2416:             close: function() {
 2417:                 \$( '[name="LC_interval_done_proctorkey"]' )[0].value = '';
 2418:             }
 2419:         });
 2420:         \$( "#LC_done-confirm" ).find( "form" ).on( "submit", function( event ) {
 2421:             event.preventDefault();
 2422:             \$( '[name="LC_interval_done"]' )[0].value = 'true';
 2423:             \$( '[name="LC_interval_done_proctorpass"]' )[0].value = \$( '[name="LC_interval_done_proctorkey"]' )[0].value;
 2424:             \$( '[name="LCdoneButton"]' )[0].submit();
 2425:         });
 2426: });
 2427: 
 2428: // ]]>
 2429: </script>
 2430: 
 2431: END
 2432:     } else {
 2433:         if ($height !~ /^\d+$/) {
 2434:             $height = 320;
 2435:             if ($missing) {
 2436:                 $height += 60;
 2437:             }
 2438:         }
 2439:         if ($width !~ /^\d+$/) {
 2440:             $width = 320;
 2441:             if ($missing) {
 2442:                 $width += 60;
 2443:             }
 2444:         }
 2445:         if ($missing) {
 2446:             $lt{'miss'} = '</p>'.$lt{'miss'}.'<p>';
 2447:         }
 2448:         return <<END;
 2449: 
 2450: <form method="post" name="LCdoneButton" action="">
 2451:     <input type="hidden" name="LC_interval_done" value="" />
 2452:     <input type="hidden" name="symb" value="$shownsymb" />
 2453:     <button id="LC_done-confirm-opener" type="button">$donebuttontext</button>
 2454: </form>
 2455: 
 2456: <div id="LC_done-confirm" title="$lt{'title'}">
 2457:     <p>$lt{'preamble'} $lt{$type} $lt{'miss'} $lt{'okdone'} $lt{'cancel'}</p>
 2458: </div>
 2459: 
 2460: <script type="text/javascript">
 2461: // <![CDATA[
 2462: \$( "#LC_done-confirm" ).dialog({ autoOpen: false });
 2463: \$( "#LC_done-confirm-opener" ).click(function() {
 2464:     \$( "#LC_done-confirm" ).dialog( "open" );
 2465:     \$( "#LC_done-confirm" ).dialog({
 2466:       resizable: false,
 2467:       height: $height,
 2468:       width: $width,
 2469:       modal: true,
 2470:       buttons: [
 2471:                  {
 2472:                     text: "$lt{'ok'}",
 2473:                     click: function() {
 2474:                         \$( this ).dialog( "close" );
 2475:                         \$( '[name="LC_interval_done"]' )[0].value = 'true';
 2476:                         \$( '[name="LCdoneButton"]' )[0].submit();
 2477:                     },
 2478:                  },
 2479:                  {
 2480:                      text: "$lt{'exit'}",
 2481:                      click: function() {
 2482:                          \$( this ).dialog( "close" );
 2483:                      },
 2484:                   },
 2485:                ],
 2486:        });
 2487: });
 2488: // ]]>
 2489: </script>
 2490: 
 2491: END
 2492:     }
 2493: }
 2494: 
 2495: sub utilityfunctions {
 2496:     my ($httphost) = @_;
 2497:     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 2498:     my $currentsymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 2499:     if ($currenturl =~ m{^/adm/wrapper/ext/}) {
 2500:         if ($env{'request.external.querystring'}) {
 2501:             $currenturl .= ($currenturl=~/\?/)?'&':'?'.$env{'request.external.querystring'};
 2502:         }
 2503:         my ($anchor) = ($env{'request.symb'} =~ /(\#[^\#]+)$/);
 2504:         if (($anchor) && ($currenturl !~ /\Q$anchor\E$/)) {
 2505:             $currenturl .= $1;
 2506:         }
 2507:     }
 2508:     $currenturl=&Apache::lonenc::check_encrypt(&unescape($currenturl));
 2509:     
 2510:     my $dc_popup_cid;
 2511:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 2512:                         $env{'course.'.$env{'request.course.id'}.
 2513:                                  '.domain'}.'/'})) {
 2514:         $dc_popup_cid = &dc_popup_js();
 2515:     }
 2516: 
 2517:     my $start_page_annotate = 
 2518:         &Apache::loncommon::start_page('Annotator',undef,
 2519: 				       {'only_body' => 1,
 2520: 					'js_ready'  => 1,
 2521: 					'bgcolor'   => '#BBBBBB',
 2522: 					'add_entries' => {
 2523: 					    'onload' => 'javascript:document.goannotate.submit();'}});
 2524: 
 2525:     my $end_page_annotate = 
 2526:         &Apache::loncommon::end_page({'js_ready' => 1});
 2527: 
 2528:     my $jumptores = &Apache::lonhtmlcommon::javascript_jumpto_resource();
 2529: 
 2530:     my $esc_url=&escape($currenturl);
 2531:     my $esc_symb=&escape($currentsymb);
 2532: 
 2533:     my $countdown = &countdown_toggle_js();
 2534: 
 2535:     my $annotateurl = '/adm/annotation';
 2536:     if ($httphost) {
 2537:         $annotateurl = '/adm/annotations';
 2538:     }
 2539:     my $hostvar = '
 2540: function setLCHost() {
 2541:     var lcHostname="";
 2542: ';
 2543:     if ($httphost =~ m{^https?\://}) {
 2544:         $hostvar .= '    var lcServer="'.$httphost.'";'."\n".
 2545:                     '    var hostReg = /^https?:\/\/([^\/]+)$/i;'."\n".
 2546:                     '    var match = hostReg.exec(lcServer);'."\n".
 2547:                     '    if (match.length) {'."\n".
 2548:                     '        if (match[1] == location.hostname) {'."\n".
 2549:                     '            lcHostname=lcServer;'."\n".
 2550:                     '        }'."\n".
 2551:                     '    }'."\n";
 2552:     }
 2553: 
 2554:     $hostvar .= '    return lcHostname;'."\n".
 2555: '}'."\n";
 2556: 
 2557: return (<<ENDUTILITY)
 2558:     $hostvar
 2559:     var currentURL=unescape("$esc_url");
 2560:     var reloadURL=unescape("$esc_url");
 2561:     var currentSymb=unescape("$esc_symb");
 2562: 
 2563: $dc_popup_cid
 2564: 
 2565: $jumptores
 2566: 
 2567: function gopost(url,postdata) {
 2568:    if (url!='') {
 2569:       var lcHostname = setLCHost();
 2570:       this.document.server.action=lcHostname+url;
 2571:       this.document.server.postdata.value=postdata;
 2572:       this.document.server.command.value='';
 2573:       this.document.server.url.value='';
 2574:       this.document.server.symb.value='';
 2575:       this.document.server.submit();
 2576:    }
 2577: }
 2578: 
 2579: function gocmd(url,cmd) {
 2580:    if (url!='') {
 2581:       var lcHostname = setLCHost();
 2582:       this.document.server.action=lcHostname+url;
 2583:       this.document.server.postdata.value='';
 2584:       this.document.server.command.value=cmd;
 2585:       this.document.server.url.value=currentURL;
 2586:       this.document.server.symb.value=currentSymb;
 2587:       this.document.server.submit();
 2588:    }
 2589: }
 2590: 
 2591: function gocstr(url,filename) {
 2592:     if (url == '/adm/cfile?action=delete') {
 2593:         this.document.cstrdelete.filename.value = filename
 2594:         this.document.cstrdelete.submit();
 2595:         return;
 2596:     }
 2597:     if (url == '/adm/printout') {
 2598:         this.document.cstrprint.postdata.value = filename
 2599:         this.document.cstrprint.curseed.value = 0;
 2600:         this.document.cstrprint.problemtype.value = 0;
 2601:         if (this.document.lonhomework) {
 2602:             if ((this.document.lonhomework.rndseed) && (this.document.lonhomework.rndseed.value != null) && (this.document.lonhomework.rndseed.value != '')) {
 2603:                 this.document.cstrprint.curseed.value = this.document.lonhomework.rndseed.value
 2604:             }
 2605:             if (this.document.lonhomework.problemtype) {
 2606: 		if (this.document.lonhomework.problemtype.value) {
 2607: 		    this.document.cstrprint.problemtype.value = 
 2608: 			this.document.lonhomework.problemtype.value;
 2609: 		} else if (this.document.lonhomework.problemtype.options) {
 2610: 		    for (var i=0; i<this.document.lonhomework.problemtype.options.length; i++) {
 2611: 			if (this.document.lonhomework.problemtype.options[i].selected) {
 2612: 			    if (this.document.lonhomework.problemtype.options[i].value != null && this.document.lonhomework.problemtype.options[i].value != '') { 
 2613: 				this.document.cstrprint.problemtype.value = this.document.lonhomework.problemtype.options[i].value
 2614: 				}
 2615: 			}
 2616: 		    }
 2617: 		}
 2618: 	    }
 2619: 	}
 2620:         this.document.cstrprint.submit();
 2621:         return;
 2622:     }
 2623:     if (url !='') {
 2624:         this.document.constspace.filename.value = filename;
 2625:         this.document.constspace.action = url;
 2626:         this.document.constspace.submit();
 2627:     }
 2628: }
 2629: 
 2630: function golist(url) {
 2631:    if (url!='' && url!= null) {
 2632:        currentURL = null;
 2633:        currentSymb= null;
 2634:        var lcHostname = setLCHost();
 2635:        top.location.href=lcHostname+url;
 2636:    }
 2637: }
 2638: 
 2639: 
 2640: 
 2641: function catalog_info(url,isMobile) {
 2642:     if (isMobile == 1) {
 2643:         openMyModal(url+'.meta?modal=1',500,400,'yes');
 2644:     } else {
 2645:         loncatinfo=window.open(url+'.meta',"LONcatInfo",'height=500,width=400,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 2646:     }
 2647: }
 2648: 
 2649: function chat_win() {
 2650:    var lcHostname = setLCHost();
 2651:    lonchat=window.open(lcHostname+'/res/adm/pages/chatroom.html',"LONchat",'height=320,width=480,resizable=yes,location=no,menubar=no,toolbar=no');
 2652: }
 2653: 
 2654: function group_chat(group) {
 2655:    var lcHostname = setLCHost();
 2656:    var url = lcHostname+'/adm/groupchat?group='+group;
 2657:    var winName = 'LONchat_'+group;
 2658:    grpchat=window.open(url,winName,'height=320,width=280,resizable=yes,location=no,menubar=no,toolbar=no');
 2659: }
 2660: 
 2661: function annotate() {
 2662:    w_Annotator_flag=1;
 2663:    annotator=window.open('','Annotator','width=365,height=265,scrollbars=0');
 2664:    annotator.document.write(
 2665:    '$start_page_annotate'
 2666:   +"<form name='goannotate' target='Annotator' method='post' "
 2667:   +"action='$annotateurl'>"
 2668:   +"<input type='hidden' name='symbnew' value='"+currentSymb+"' />"
 2669:   +"<\\/form>"
 2670:   +'$end_page_annotate');
 2671:    annotator.document.close();
 2672: }
 2673: 
 2674: function open_StoredLinks_Import(rat) {
 2675:    var newWin;
 2676:    var lcHostname = setLCHost();
 2677:    if (rat) {
 2678:        newWin = window.open(lcHostname+'/adm/wishlist?inhibitmenu=yes&mode=import&rat='+rat,
 2679:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 2680:    }
 2681:    else {
 2682:        newWin = window.open(lcHostname+'/adm/wishlist?inhibitmenu=yes&mode=import',
 2683:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 2684:    }
 2685:    newWin.focus();
 2686: }
 2687: 
 2688: function open_source() {
 2689:    sourcewin=window.open('/adm/source?inhibitmenu=yes&viewonly=1&filename='+currentURL,'LONsource',
 2690:                          'height=500,width=600,resizable=yes,location=no,menubar=no,toolbar=no,scrollbars=yes');
 2691: }
 2692: 
 2693: (function (\$) {
 2694:   \$(document).ready(function () {
 2695:     \$.single=function(a){return function(b){a[0]=b;return a}}(\$([1]));
 2696:     /*\@cc_on
 2697:       if (!window.XMLHttpRequest) {
 2698:         \$('.LC_hoverable').each(function () {
 2699:           this.attachEvent('onmouseenter', function (evt) { \$.single(evt.srcElement).addClass('hover'); });
 2700:           this.attachEvent('onmouseleave', function (evt) { \$.single(evt.srcElement).removeClass('hover'); });
 2701:         });
 2702:       }
 2703:     \@*/
 2704:   });
 2705: }(jQuery));
 2706: 
 2707: $countdown
 2708: 
 2709: ENDUTILITY
 2710: }
 2711: 
 2712: sub serverform {
 2713:     return(<<ENDSERVERFORM);
 2714: <form name="server" action="/adm/logout" method="post" target="_top">
 2715: <input type="hidden" name="postdata" value="none" />
 2716: <input type="hidden" name="command" value="none" />
 2717: <input type="hidden" name="url" value="none" />
 2718: <input type="hidden" name="symb" value="none" />
 2719: </form>
 2720: ENDSERVERFORM
 2721: }
 2722: 
 2723: sub constspaceform {
 2724:     return(<<ENDCONSTSPACEFORM);
 2725: <form name="constspace" action="/adm/logout" method="post" target="_top">
 2726: <input type="hidden" name="filename" value="" />
 2727: </form>
 2728: <form name="cstrdelete" action="/adm/cfile" method="post" target="_top">
 2729: <input type="hidden" name="action" value="delete" /> 
 2730: <input type="hidden" name="filename" value="" />
 2731: </form>
 2732: <form name="cstrprint" action="/adm/printout" target="_parent" method="post">
 2733: <input type="hidden" name="postdata" value="" />
 2734: <input type="hidden" name="curseed" value="" />
 2735: <input type="hidden" name="problemtype" value="" />
 2736: </form>
 2737: 
 2738: ENDCONSTSPACEFORM
 2739: }
 2740: 
 2741: sub get_nav_status {
 2742:     my $navstatus="swmenu.w_loncapanav_flag=";
 2743:     if ($env{'environment.remotenavmap'} eq 'on') {
 2744:         $navstatus.="1";
 2745:     } else {
 2746:         $navstatus.="-1";
 2747:     }
 2748:     return $navstatus;
 2749: }
 2750: 
 2751: sub hidden_button_check {
 2752:     if ( $env{'request.course.id'} eq ''
 2753:          || $env{'request.role.adv'} ) {
 2754: 
 2755:         return;
 2756:     }
 2757:     my $buttonshide = &Apache::lonnet::EXT('resource.0.buttonshide');
 2758:     return $buttonshide; 
 2759: }
 2760: 
 2761: sub roles_selector {
 2762:     my ($cdom,$cnum,$httphost) = @_;
 2763:     my $crstype = &Apache::loncommon::course_type();
 2764:     my $now = time;
 2765:     my (%courseroles,%seccount,%courseprivs,%roledesc);
 2766:     my $is_cc;
 2767:     my ($js,$form,$switcher,$has_opa_priv);
 2768:     my $ccrole;
 2769:     if ($crstype eq 'Community') {
 2770:         $ccrole = 'co';
 2771:     } else {
 2772:         $ccrole = 'cc';
 2773:     }
 2774:     my ($privref,$gotsymb,$destsymb);
 2775:     my $destinationurl = $ENV{'REQUEST_URI'};
 2776:     if ($destinationurl =~ /(\?|\&)symb=/) {
 2777:         $gotsymb = 1;
 2778:     } elsif ($destinationurl =~ m{^/enc/}) {
 2779:         my $plainurl = &Apache::lonenc::unencrypted($destinationurl);
 2780:         if ($plainurl =~ /(\?|\&)symb=/) {
 2781:             $gotsymb = 1;
 2782:         }
 2783:     }
 2784:     unless ($gotsymb) {
 2785:         $destsymb = &Apache::lonnet::symbread();
 2786:         if ($destsymb ne '') {
 2787:             $destsymb = &Apache::lonenc::check_encrypt($destsymb);
 2788:         }
 2789:     }
 2790:     my $reqprivs = &required_privs();
 2791:     if (ref($reqprivs) eq 'HASH') {
 2792:         my $destination = $destinationurl;
 2793:         $destination =~ s/(\?.*)$//;
 2794:         if (exists($reqprivs->{$destination})) {
 2795:             if ($reqprivs->{$destination} =~ /,/) {
 2796:                 @{$privref} = split(/,/,$reqprivs->{$destination});
 2797:             } else {
 2798:                 $privref = [$reqprivs->{$destination}];
 2799:             }
 2800:         }
 2801:     }
 2802:     if ($env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum}) {
 2803:         my ($start,$end) = split(/\./,$env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum});
 2804:         if ((($start) && ($start<0)) || 
 2805:             (($end) && ($end<$now))  ||
 2806:             (($start) && ($now<$start))) {
 2807:             $is_cc = 0;
 2808:         } else {
 2809:             $is_cc = 1;
 2810:         }
 2811:     }
 2812:     if ($is_cc) {
 2813:         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs);
 2814:     } elsif ($env{'request.role'} =~ m{^\Qcr/$cdom/$cdom-domainconfig/\E(\w+)\.\Q/$cdom/$cnum\E}) {
 2815:         &get_customadhoc_roles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,\%roledesc,$privref);
 2816:     } else {
 2817:         my %gotnosection;
 2818:         foreach my $item (keys(%env)) {
 2819:             if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
 2820:                 my $role = $1;
 2821:                 my $sec = $2;
 2822:                 next if ($role eq 'gr');
 2823:                 my ($start,$end) = split(/\./,$env{$item});
 2824:                 next if (($start && $start > $now) || ($end && $end < $now));
 2825:                 if ($sec eq '') {
 2826:                     if (!$gotnosection{$role}) {
 2827:                         $seccount{$role} ++;
 2828:                         $gotnosection{$role} = 1;
 2829:                     }
 2830:                 }
 2831:                 if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0)) {
 2832:                     my $cnumsec = $cnum;
 2833:                     if ($sec ne '') {
 2834:                         $cnumsec .= "/$sec";
 2835:                     }
 2836:                     $courseprivs{"$role./$cdom/$cnumsec./"} =
 2837:                         $env{"user.priv.$role./$cdom/$cnumsec./"};
 2838:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/"} =
 2839:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/"};
 2840:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/$cnumsec"} =
 2841:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/$cnumsec"};
 2842:                 }
 2843:                 if (ref($courseroles{$role}) eq 'ARRAY') {
 2844:                     if ($sec ne '') {
 2845:                         if (!grep(/^\Q$sec\E$/,@{$courseroles{$role}})) {
 2846:                             push(@{$courseroles{$role}},$sec);
 2847:                             $seccount{$role} ++;
 2848:                         }
 2849:                     }
 2850:                 } else {
 2851:                     @{$courseroles{$role}} = ();
 2852:                     if ($sec ne '') {
 2853:                         $seccount{$role} ++;
 2854:                         push(@{$courseroles{$role}},$sec);
 2855:                     }
 2856:                 }
 2857:             }
 2858:         }
 2859:     }
 2860:     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
 2861:     my $numdiffsec;
 2862:     if (keys(%seccount) == 1) {
 2863:         foreach my $key (keys(%seccount)) {
 2864:             $numdiffsec = $seccount{$key};
 2865:         }
 2866:     }
 2867:     if ((keys(%seccount) > 1) || ($numdiffsec > 1)) {
 2868:         my @submenu;
 2869:         $js = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,\%roledesc,$privref);
 2870:         $form = 
 2871:             '<form name="rolechooser" method="post" action="'.$httphost.'/adm/roles">'."\n".
 2872:             '  <input type="hidden" name="destinationurl" value="'.
 2873:             &HTML::Entities::encode($destinationurl).'" />'."\n".
 2874:             '  <input type="hidden" name="gotorole" value="1" />'."\n".
 2875:             '  <input type="hidden" name="selectrole" value="" />'."\n".
 2876:             '  <input type="hidden" name="switchrole" value="" />'."\n";
 2877:         if ($destsymb ne '') {
 2878:             $form .= '   <input type="hidden" name="destsymb" value="'.
 2879:                          &HTML::Entities::encode($destsymb).'" />'."\n";
 2880:         }
 2881:         $form .= '</form>'."\n";
 2882:         foreach my $role (@roles_order) {
 2883:             my $include;
 2884:             if (defined($courseroles{$role})) {
 2885:                 if ($env{'request.role'} =~ m{^\Q$role\E}) {
 2886:                     if ($seccount{$role} > 1) {
 2887:                         $include = 1;
 2888:                     } else {
 2889:                         if ($env{'user.priv.'.$env{'request.role'}."./$cdom/$cnum"} =~/opa\&([^\:]*)/) {
 2890:                             $has_opa_priv = 1;
 2891:                         }
 2892:                     }
 2893:                 } else {
 2894:                     $include = 1;
 2895:                 }
 2896:             }
 2897:             if ($include) {
 2898:                 if ($env{"user.priv.$role./$cdom/$cnum./$cdom/$cnum"} =~/opa\&([^\:]*)/) {
 2899:                     $has_opa_priv = 1;
 2900:                 }
 2901:                 push(@submenu,['javascript:adhocRole('."'$role'".')',
 2902:                                &Apache::lonnet::plaintext($role,$crstype)]);
 2903:             }
 2904:         }
 2905:         foreach my $role (sort(keys(%courseroles))) {
 2906:             if ($role =~ /^cr/) {
 2907:                 my $include;
 2908:                 if ($env{'request.role'} =~ m{^\Q$role\E}) {
 2909:                     if ($seccount{$role} > 1) {
 2910:                         $include = 1;
 2911:                     }
 2912:                 } else {
 2913:                     $include = 1;
 2914:                 }
 2915:                 if ($include) {
 2916:                     my $rolename;
 2917:                     if ($role =~ m{^cr/$cdom/$cdom\-domainconfig/(\w+)(?:/\w+|$)}) {
 2918:                         $rolename = $roledesc{$role};
 2919:                         if ($rolename eq '') {
 2920:                             $rolename = &mt('Helpdesk [_1]',$1);
 2921:                         }
 2922:                     } else {
 2923:                         $rolename = &Apache::lonnet::plaintext($role);
 2924:                     }
 2925:                     if ($env{"user.priv.$role./$cdom/$cnum./$cdom/$cnum"} =~/opa\&([^\:]*)/) {
 2926:                         $has_opa_priv = 1;
 2927:                     }
 2928:                     push(@submenu,['javascript:adhocRole('."'$role'".')',
 2929:                                    $rolename]);
 2930:                 }
 2931:             }
 2932:         }
 2933:         if (@submenu > 0) {
 2934:             $switcher = &create_submenu('','',&mt('Switch role'),\@submenu);
 2935:         }
 2936:     }
 2937:     return ($js,$form,$switcher,$has_opa_priv);
 2938: }
 2939: 
 2940: sub get_all_courseroles {
 2941:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs) = @_;
 2942:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 2943:             (ref($courseprivs) eq 'HASH')) {
 2944:         return;
 2945:     }
 2946:     my ($result,$cached) = 
 2947:         &Apache::lonnet::is_cached_new('getcourseroles',$cdom.'_'.$cnum);
 2948:     if (defined($cached)) {
 2949:         if (ref($result) eq 'HASH') {
 2950:             if ((ref($result->{'roles'}) eq 'HASH') && 
 2951:                 (ref($result->{'seccount'}) eq 'HASH') && 
 2952:                 (ref($result->{'privs'}) eq 'HASH')) {
 2953:                 %{$courseroles} = %{$result->{'roles'}};
 2954:                 %{$seccount} = %{$result->{'seccount'}};
 2955:                 %{$courseprivs} = %{$result->{'privs'}};
 2956:                 return;
 2957:             }
 2958:         }
 2959:     }
 2960:     my %gotnosection;
 2961:     my %adv_roles =
 2962:          &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
 2963:     foreach my $role (keys(%adv_roles)) {
 2964:         my ($urole,$usec) = split(/:/,$role);
 2965:         if (!$gotnosection{$urole}) {
 2966:             $seccount->{$urole} ++;
 2967:             $gotnosection{$urole} = 1;
 2968:         }
 2969:         if (ref($courseroles->{$urole}) eq 'ARRAY') {
 2970:             if ($usec ne '') {
 2971:                 if (!grep(/^Q$usec\E$/,@{$courseroles->{$urole}})) {
 2972:                     push(@{$courseroles->{$urole}},$usec);
 2973:                     $seccount->{$urole} ++;
 2974:                 }
 2975:             }
 2976:         } else {
 2977:             @{$courseroles->{$urole}} = ();
 2978:             if ($usec ne '') {
 2979:                 $seccount->{$urole} ++;
 2980:                 push(@{$courseroles->{$urole}},$usec);
 2981:             }
 2982:         }
 2983:         my $area = '/'.$cdom.'/'.$cnum;
 2984:         if ($usec ne '') {
 2985:             $area .= '/'.$usec;
 2986:         }
 2987:         if ($role =~ /^cr\//) {
 2988:             &Apache::lonnet::custom_roleprivs($courseprivs,$urole,$cdom,$cnum,$urole.'.'.$area,$area);
 2989:         } else {
 2990:             &Apache::lonnet::standard_roleprivs($courseprivs,$urole,$cdom,$urole.'.'.$area,$cnum,$area);
 2991:         }
 2992:     }
 2993:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum,['st']);
 2994:     @{$courseroles->{'st'}} = ();
 2995:     &Apache::lonnet::standard_roleprivs($courseprivs,'st',$cdom,"st./$cdom/$cnum",$cnum,"/$cdom/$cnum");
 2996:     if (keys(%sections_count) > 0) {
 2997:         push(@{$courseroles->{'st'}},keys(%sections_count));
 2998:         $seccount->{'st'} = scalar(keys(%sections_count));
 2999:     }
 3000:     $seccount->{'st'} ++; # Increment for a section-less student role.
 3001:     my $rolehash = {
 3002:                      'roles'    => $courseroles,
 3003:                      'seccount' => $seccount,
 3004:                      'privs'    => $courseprivs,
 3005:                    };
 3006:     &Apache::lonnet::do_cache_new('getcourseroles',$cdom.'_'.$cnum,$rolehash);
 3007:     return;
 3008: }
 3009: 
 3010: sub get_customadhoc_roles {
 3011:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs,$roledesc,$privref) = @_;
 3012:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 3013:             (ref($courseprivs) eq 'HASH') && (ref($roledesc) eq 'HASH')) {
 3014:         return;
 3015:     }
 3016:     my $is_helpdesk = 0;
 3017:     my $now = time;
 3018:     foreach my $role ('dh','da') {
 3019:         if ($env{"user.role.$role./$cdom/"}) {
 3020:             my ($start,$end)=split(/\./,$env{"user.role.$role./$cdom/"});
 3021:             if (!($start && ($now<$start)) && !($end && ($now>$end))) {
 3022:                 $is_helpdesk = 1;
 3023:                 last;
 3024:             }
 3025:         }
 3026:     }
 3027:     if ($is_helpdesk) {
 3028:         my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($cdom.'_'.$cnum);
 3029:         my %available;
 3030:         if (ref($possroles) eq 'ARRAY') {
 3031:             map { $available{$_} = 1; } @{$possroles};
 3032:         }
 3033:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
 3034:         if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 3035:             if (keys(%{$domdefaults{'adhocroles'}})) {
 3036:                 my $numsec = 1;
 3037:                 my @sections;
 3038:                 my ($allseclist,$cached) =
 3039:                     &Apache::lonnet::is_cached_new('courseseclist',$cdom.'_'.$cnum);
 3040:                 if (defined($cached)) {
 3041:                     if ($allseclist ne '') {
 3042:                         @sections = split(/,/,$allseclist);
 3043:                         $numsec += scalar(@sections);
 3044:                     }
 3045:                 } else {
 3046:                     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
 3047:                     @sections = sort(keys(%sections_count));
 3048:                     $numsec += scalar(@sections);
 3049:                     $allseclist = join(',',@sections);
 3050:                     &Apache::lonnet::do_cache_new('courseseclist',$cdom.'_'.$cnum,$allseclist);
 3051:                 }
 3052:                 my (%adhoc,$gotprivs);
 3053:                 my $prefix = "cr/$cdom/$cdom".'-domainconfig';
 3054:                 foreach my $role (keys(%{$domdefaults{'adhocroles'}})) {
 3055:                     next if (($role eq '') || ($role =~ /\W/));
 3056:                     $seccount->{"$prefix/$role"} = $numsec;
 3057:                     $roledesc->{"$prefix/$role"} = $description->{$role};  
 3058:                     if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0)) {
 3059:                         if (exists($env{"user.priv.$prefix/$role./$cdom/$cnum./"})) {
 3060:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./"} =
 3061:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./"};
 3062:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/"} =
 3063:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./$cdom/"};
 3064:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/$cnum"} =
 3065:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./$cdom/$cnum"};
 3066:                         } else {
 3067:                             unless ($gotprivs) {
 3068:                                 my ($adhocroles,$privscached) =
 3069:                                     &Apache::lonnet::is_cached_new('adhocroles',$cdom);
 3070:                                 if ((defined($privscached)) && (ref($adhocroles) eq 'HASH')) {
 3071:                                     %adhoc = %{$adhocroles};
 3072:                                 } else {
 3073:                                     my $confname = &Apache::lonnet::get_domainconfiguser($cdom);
 3074:                                     my %roledefs = &Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
 3075:                                     foreach my $key (keys(%roledefs)) {
 3076:                                         (undef,my $rolename) = split(/_/,$key);
 3077:                                         if ($rolename ne '') {
 3078:                                             my ($systempriv,$domainpriv,$coursepriv) = split(/\_/,$roledefs{$key});
 3079:                                             $coursepriv = &Apache::lonnet::course_adhocrole_privs($rolename,$cdom,$cnum,$coursepriv);
 3080:                                             $adhoc{$rolename} = join('_',($systempriv,$domainpriv,$coursepriv));
 3081:                                         }
 3082:                                     }
 3083:                                     &Apache::lonnet::do_cache_new('adhocroles',$cdom,\%adhoc);
 3084:                                 }
 3085:                                 $gotprivs = 1;
 3086:                             }
 3087:                             ($courseprivs->{"$prefix/$role./$cdom/$cnum./"},
 3088:                              $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/"},
 3089:                              $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/$cnum"}) =
 3090:                                  split(/\_/,$adhoc{$role});
 3091:                         }
 3092:                     }
 3093:                     if ($available{$role}) {
 3094:                         $courseroles->{"$prefix/$role"} = \@sections;
 3095:                     }
 3096:                 }
 3097:             }
 3098:         }
 3099:     }
 3100:     return;
 3101: }
 3102: 
 3103: sub jump_to_role {
 3104:     my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$roledesc,$privref) = @_;
 3105:     my %lt = &Apache::lonlocal::texthash(
 3106:                 this => 'This role has section(s) associated with it.',
 3107:                 ente => 'Enter a specific section.',
 3108:                 orlb => 'Enter a specific section, or leave blank for no section.',
 3109:                 avai => 'Available sections are:',
 3110:                 youe => 'You entered an invalid section choice:',
 3111:                 plst => 'Please try again.',
 3112:                 role => 'The role you selected is not permitted to view the current page.',
 3113:                 swit => 'Switch role, but display Main Menu page instead?',
 3114:     );
 3115:     my $js;
 3116:     if (ref($courseroles) eq 'HASH') {
 3117:         $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
 3118:               '    var numsec = new Array();'."\n".
 3119:               '    var rolesections = new Array();'."\n".
 3120:               '    var rolenames = new Array();'."\n".
 3121:               '    var roleseclist = new Array();'."\n";
 3122:         my @items = keys(%{$courseroles});
 3123:         for (my $i=0; $i<@items; $i++) {
 3124:             $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
 3125:             my ($secs,$secstr);
 3126:             if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
 3127:                 my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
 3128:                 $secs = join('","',@sections);
 3129:                 $secstr = join(', ',@sections);
 3130:             }
 3131:             $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
 3132:                    '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
 3133:                    '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
 3134:         }
 3135:     }
 3136:     my $checkroles = 0;
 3137:     if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0) && (ref($courseprivs) eq 'HASH')) {
 3138:         my %disallowed;
 3139:         foreach my $role (sort(keys(%{$courseprivs}))) {
 3140:             my $trole;
 3141:             if ($role =~ m{^(.+?)\Q./$cdom/$cnum\E}) {
 3142:                 $trole = $1;
 3143:             }
 3144:             if (($trole ne '') && ($trole ne 'cm')) {
 3145:                 $disallowed{$trole} = 1;
 3146:                 foreach my $priv (@{$privref}) { 
 3147:                     if ($courseprivs->{$role} =~ /\Q:$priv\E($|:|\&\w+)/) {
 3148:                         delete($disallowed{$trole});
 3149:                         last;
 3150:                     }
 3151:                 }
 3152:             }
 3153:         }
 3154:         if (keys(%disallowed) > 0) {
 3155:             $checkroles = 1;
 3156:             $js .= "    var disallow = new Array('".join("','",keys(%disallowed))."');\n".
 3157:                    "    var rolecheck = 1;\n";
 3158:         }
 3159:     }
 3160:     if (!$checkroles) {
 3161:         $js .=  "    var disallow = new Array();\n".
 3162:                 "    rolecheck = 0;\n";
 3163:     }
 3164:     return <<"END";
 3165: <script type="text/javascript">
 3166: //<![CDATA[
 3167: function adhocRole(newrole) {
 3168:     $js
 3169:     if (newrole == '') {
 3170:         return;
 3171:     } 
 3172:     var fullrole = newrole+'./$cdom/$cnum';
 3173:     var selidx = '';
 3174:     for (var i=0; i<rolenames.length; i++) {
 3175:         if (rolenames[i] == newrole) {
 3176:             selidx = i;
 3177:         }
 3178:     }
 3179:     if (rolecheck > 0) {
 3180:         for (var i=0; i<disallow.length; i++) {
 3181:             if (disallow[i] == newrole) {
 3182:                 if (confirm("$lt{'role'}\\n$lt{'swit'}")) {
 3183:                     document.rolechooser.destinationurl.value = '/adm/menu';
 3184:                 } else {
 3185:                     return;
 3186:                 }
 3187:             }
 3188:         }
 3189:     }
 3190:     var secok = 1;
 3191:     var secchoice = '';
 3192:     if (selidx >= 0) {
 3193:         if (numsec[selidx] > 1) {
 3194:             secok = 0;
 3195:             var numrolesec = rolesections[selidx].length;
 3196:             var msgidx = numsec[selidx] - numrolesec;
 3197:             secchoice = prompt("$lt{'this'} "+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
 3198:             if (secchoice == '') {
 3199:                 if (msgidx > 0) {
 3200:                     secok = 1;
 3201:                 }
 3202:             } else {
 3203:                 for (var j=0; j<rolesections[selidx].length; j++) {
 3204:                     if (rolesections[selidx][j] == secchoice) {
 3205:                         secok = 1;
 3206:                     }
 3207:                 }
 3208:             }
 3209:         } else {
 3210:             if (rolesections[selidx].length == 1) {
 3211:                 secchoice = rolesections[selidx][0];
 3212:             }
 3213:         }
 3214:     }
 3215:     if (secok == 1) {
 3216:         if (secchoice != '') {
 3217:             fullrole += '/'+secchoice;
 3218:         }
 3219:     } else {
 3220:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 3221:         if (secchoice != null) {
 3222:             alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
 3223:         }
 3224:         return;
 3225:     }
 3226:     if (fullrole == "$env{'request.role'}") {
 3227:         return;
 3228:     }
 3229:     itemid = retrieveIndex('gotorole');
 3230:     if (itemid != -1) {
 3231:         document.rolechooser.elements[itemid].name = fullrole;
 3232:     }
 3233:     document.rolechooser.switchrole.value = fullrole;
 3234:     document.rolechooser.selectrole.value = '1';
 3235:     document.rolechooser.submit();
 3236:     return;
 3237: }
 3238: 
 3239: function retrieveIndex(item) {
 3240:     for (var i=0;i<document.rolechooser.elements.length;i++) {
 3241:         if (document.rolechooser.elements[i].name == item) {
 3242:             return i;
 3243:         }
 3244:     }
 3245:     return -1;
 3246: }
 3247: // ]]>
 3248: </script>
 3249: END
 3250: }
 3251: 
 3252: sub required_privs {
 3253:     my $privs =  {
 3254:              '/adm/parmset'      => 'opa,vpa',
 3255:              '/adm/courseprefs'  => 'opa,vpa',
 3256:              '/adm/whatsnew'     => 'whn',
 3257:              '/adm/populate'     => 'cst,vpa,vcl',
 3258:              '/adm/trackstudent' => 'vsa',
 3259:              '/adm/statistics'   => 'mgr,vgr',
 3260:              '/adm/setblock'     => 'dcm,vcb',
 3261:              '/adm/coursedocs'   => 'mdc',
 3262:            };
 3263:     unless ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet') {
 3264:         $privs->{'/adm/classcalc'}   = 'vgr',
 3265:         $privs->{'/adm/assesscalc'}  = 'vgr',
 3266:         $privs->{'/adm/studentcalc'} = 'vgr';
 3267:     }
 3268:     return $privs;
 3269: }
 3270: 
 3271: sub countdown_timer {
 3272:     if (($env{'request.course.id'}) && ($env{'request.symb'} ne '') &&
 3273:         ($env{'request.filename'}=~/$LONCAPA::assess_re/)) {
 3274:         my ($type,$hastimeleft,$slothastime);
 3275:         my $now = time;
 3276:         if ($env{'request.filename'} =~ /\.task$/) {
 3277:             $type = 'Task';
 3278:         } else {
 3279:             $type = 'problem';
 3280:         }
 3281:         my ($status,$accessmsg,$slot_name,$slot) =
 3282:             &Apache::lonhomework::check_slot_access('0',$type);
 3283:         if ($slot_name ne '') {
 3284:             if (ref($slot) eq 'HASH') {
 3285:                 if (($slot->{'starttime'} < $now) &&
 3286:                     ($slot->{'endtime'} > $now)) {
 3287:                     $slothastime = 1;
 3288:                 }
 3289:             }
 3290:         }
 3291:         if ($status ne 'CAN_ANSWER') {
 3292:             return;
 3293:         }
 3294:         my $duedate = &Apache::lonnet::EXT("resource.0.duedate");
 3295:         my @interval=&Apache::lonnet::EXT("resource.0.interval");
 3296:         my ($timelimit,$usesdone,$donebuttontext,$proctor,$secret);
 3297:         if (@interval > 1) {
 3298:             ($timelimit,my $donesuffix) = split(/_/,$interval[0],2);
 3299:             if ($donesuffix =~ /^done\:([^\:]+)\:(.*)$/) {
 3300:                 $usesdone = 'done';
 3301:                 $donebuttontext = $1;
 3302:                 (undef,$proctor,$secret) = split(/_/,$2);
 3303:             } elsif ($donesuffix =~ /^done(|_.+)$/) {
 3304:                 $donebuttontext = &mt('Done');
 3305:                 ($usesdone,$proctor,$secret) = split(/_/,$donesuffix);
 3306:             }
 3307:             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
 3308:             if ($first_access > 0) {
 3309:                 if ($first_access+$timelimit > time) {
 3310:                     $hastimeleft = 1;
 3311:                 }
 3312:             }
 3313:         }
 3314:         if (($duedate && $duedate > time) ||
 3315:             (!$duedate && $hastimeleft) ||
 3316:             ($slot_name ne '' && $slothastime)) {
 3317:             my ($collapse,$expand,$alttxt,$title,$currdisp,$donebutton);
 3318:             if ((@interval > 1 && $hastimeleft) ||
 3319:                 ($type eq 'Task' && $slothastime)) {
 3320:                 $currdisp = 'inline';
 3321:                 $collapse = '&#9658;&nbsp;';
 3322:                 if ((@interval > 1) && ($hastimeleft)) {
 3323:                     if ($usesdone eq 'done') {
 3324:                         $donebutton = &done_button_js($interval[1],'','',$proctor,$donebuttontext);
 3325:                     }
 3326:                 }
 3327:             } else {
 3328:                 $currdisp = 'none';
 3329:                 $expand = '&#9668;&nbsp;';
 3330:             }
 3331:             unless ($env{'environment.icons'} eq 'iconsonly') {
 3332:                 $alttxt = &mt('Timer');
 3333:                 $title = $alttxt.'&nbsp;';
 3334:             }
 3335:             my $desc = &mt('Countdown to due date/time');
 3336:             return <<END;
 3337: $donebutton
 3338: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 3339: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
 3340: $collapse
 3341: </span></a>
 3342: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
 3343: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 3344: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
 3345: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
 3346: END
 3347:         }
 3348:     }
 3349:     return;
 3350: }
 3351: 
 3352: # ================================================================ Main Program
 3353: 
 3354: BEGIN {
 3355:     if (! defined($readdesk)) {
 3356:         {
 3357:             my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
 3358:             if ( CORE::open( my $config,"<$tabfile") ) {
 3359:                 while (my $configline=<$config>) {
 3360:                     $configline=(split(/\#/,$configline))[0];
 3361:                     $configline=~s/^\s+//;
 3362:                     chomp($configline);
 3363:                     if ($configline=~/^cat\:/) {
 3364:                         my @entries=split(/\:/,$configline);
 3365:                         $category_positions{$entries[2]}=$entries[1];
 3366:                         $category_names{$entries[2]}=$entries[3];
 3367:                     } elsif ($configline=~/^prim\:/) {
 3368:                         my @entries = (split(/\:/, $configline))[1..7];
 3369:                         push(@primary_menu,\@entries);
 3370:                     } elsif ($configline=~/^primsub\:/) {
 3371:                         my ($parent,@entries) = (split(/\:/, $configline))[1..5];
 3372:                         push(@{$primary_submenu{$parent}},\@entries);
 3373:                     } elsif ($configline=~/^scnd\:/) {
 3374:                         my @entries = (split(/\:/, $configline))[1..6];
 3375:                         push(@secondary_menu,\@entries);
 3376:                     } elsif ($configline=~/^scndsub\:/) {
 3377:                         my ($parent,@entries) = (split(/\:/, $configline))[1..4];
 3378:                         push(@{$secondary_submenu{$parent}},\@entries);
 3379:                     } elsif ($configline) {
 3380:                         push(@desklines,$configline);
 3381:                     }
 3382:                 }
 3383:                 CORE::close($config);
 3384:             }
 3385:         }
 3386:         $readdesk='done';
 3387:     }
 3388: }
 3389: 
 3390: 1;
 3391: __END__
 3392: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>