File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.22: download - view: text, annotated - select for diffs
Mon Aug 27 15:53:40 2012 UTC (11 years, 9 months ago) by raeburn
Branches: version_2_11_X
- For 2.11.
  - Backport 1.385.

    1: # The LearningOnline Network with CAPA
    2: # Routines to control the menu
    3: #
    4: # $Id: lonmenu.pm,v 1.369.2.22 2012/08/27 15:53:40 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 XHTML for the menu
  110: that contains following links: About, Message, Roles, Help, Logout
  111: @primary_menu is filled within the BEGIN block of this module with 
  112: entries from mydesk.tab 
  113: 
  114: =item secondary_menu()
  115: 
  116: Same as primary_menu() but operates on @secondary_menu.
  117: 
  118: =item create_submenu()
  119: 
  120: Creates XHTML for unordered list of sub-menu items which belong to a
  121: particular top-level menu item. Uses hover pseudo class in css to display
  122: dropdown list when mouse hovers over top-level item. Support for IE6
  123: (no hover psuedo class) via LC_hoverable class for <li> tag for top-
  124: level item, which employs jQuery to handle behavior on mouseover.
  125: 
  126: Inputs: 4 - (a) link and (b) target for anchor href in top level item,
  127:             (c) title for text wrapped by anchor tag in top level item.
  128:             (d) reference to array of arrays of sub-menu items.
  129: 
  130: =item innerregister()
  131: 
  132: This gets called in order to register a URL in the body of the document
  133: 
  134: =item loadevents()
  135: 
  136: =item unloadevents()
  137: 
  138: =item startupremote()
  139: 
  140: =item setflags()
  141: 
  142: =item maincall()
  143: 
  144: =item load_remote_msg()
  145: 
  146: =item get_menu_name()
  147: 
  148: =item reopenmenu()
  149: 
  150: =item open()
  151: 
  152: Open the menu
  153: 
  154: =item clear()
  155: 
  156: =item switch()
  157: 
  158: Switch a button or create a link
  159: Switch acts on the javascript that is executed when a button is clicked.  
  160: The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
  161: 
  162: =item secondlevel()
  163: 
  164: =item openmenu()
  165: 
  166: =item inlinemenu()
  167: 
  168: =item rawconfig()
  169: 
  170: =item utilityfunctions()
  171: 
  172: Output from this routine is a number of javascript functions called by
  173: items in the inline menu, and in some cases items in the Main Menu page. 
  174: 
  175: =item serverform()
  176: 
  177: =item constspaceform()
  178: 
  179: =item get_nav_status()
  180: 
  181: =item hidden_button_check()
  182: 
  183: =item roles_selector()
  184: 
  185: =item jump_to_role()
  186: 
  187: =back
  188: 
  189: =cut
  190: 
  191: package Apache::lonmenu;
  192: 
  193: use strict;
  194: use Apache::lonnet;
  195: use Apache::lonhtmlcommon();
  196: use Apache::loncommon();
  197: use Apache::lonenc();
  198: use Apache::lonlocal;
  199: use Apache::lonmsg();
  200: use LONCAPA qw(:DEFAULT :match);
  201: use HTML::Entities();
  202: use Apache::lonwishlist();
  203: 
  204: use vars qw(@desklines %category_names %category_members %category_positions 
  205:             $readdesk @primary_menu %primary_submenu @secondary_menu %secondary_submenu);
  206: 
  207: my @inlineremote;
  208: 
  209: sub prep_menuitem {
  210:     my ($menuitem) = @_;
  211:     return '' unless(ref($menuitem) eq 'ARRAY');
  212:     my $link;
  213:     if ($$menuitem[1]) { # graphical Link
  214:         $link = "<img class=\"LC_noBorder\""
  215:               . " src=\"" . &Apache::loncommon::lonhttpdurl($$menuitem[1]) . "\"" 
  216:               . " alt=\"" . &mt($$menuitem[2]) . "\" />";
  217:     } else {             # textual Link
  218:         $link = &mt($$menuitem[3]);
  219:     }
  220:     return '<li><a' 
  221:            # highlighting for new messages
  222:            . ( $$menuitem[4] eq 'newmsg' ? ' class="LC_new_message"' : '') 
  223:            . qq| href="$$menuitem[0]" target="_top">$link</a></li>|;
  224: }
  225: 
  226: # primary_menu() evaluates @primary_menu and returns XHTML for the menu
  227: # that contains following links:
  228: # About, Message, Personal, Roles, Help, Logout
  229: # @primary_menu is filled within the BEGIN block of this module with 
  230: # entries from mydesk.tab
  231: sub primary_menu {
  232:     my $menu;
  233:     # each element of @primary contains following array:
  234:     # (link url, icon path, alt text, link text, condition)
  235:     my $public;
  236:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
  237:         || (($env{'user.name'} eq '') && ($env{'user.domain'} eq ''))) {
  238:         $public = 1;
  239:     }
  240:     foreach my $menuitem (@primary_menu) {
  241:         # evaluate conditions 
  242:         next if    ref($menuitem)       ne 'ARRAY';    #
  243:         next if    $$menuitem[4]        eq 'nonewmsg'  # show links depending on
  244:                 && &Apache::lonmsg::mynewmail();       # whether a new msg 
  245:         next if    $$menuitem[4]        eq 'newmsg'    # arrived or not
  246:                 && !&Apache::lonmsg::mynewmail();      # 
  247:         next if    $$menuitem[4]        !~ /public/    ##we've a public user,
  248:                 && $public;                            ##who should not see all
  249:                                                        ##links
  250:         next if    $$menuitem[4]        eq 'onlypublic'# hide links which are 
  251:                 && !$public;                           # only visible to public
  252:                                                        # users
  253:         next if    $$menuitem[4]        eq 'roles'     ##show links depending on
  254:                 && &Apache::loncommon::show_course();  ##term 'Courses' or 
  255:         next if    $$menuitem[4]        eq 'courses'   ##'Roles' wanted
  256:                 && !&Apache::loncommon::show_course(); ##
  257:         
  258:         my $title = $menuitem->[3];
  259:         if (defined($primary_submenu{$title})) {
  260:             my ($link,$target);
  261:             if ($menuitem->[0] ne '') {
  262:                 $link = $menuitem->[0];
  263:                 $target = '_top';
  264:             } else {
  265:                 $link = '#';
  266:             }
  267:             my @primsub;
  268:             if (ref($primary_submenu{$title}) eq 'ARRAY') {
  269:                 foreach my $item (@{$primary_submenu{$title}}) {
  270:                     next if (($item->[2] eq 'wishlist') && (!$env{'user.adv'})); 
  271:                     next if (($item->[2] eq 'reqcrs') && (!&check_for_rcrs()));
  272:                     next if ((($item->[2] eq 'portfolio') || 
  273:                              ($item->[2] eq 'blog')) && 
  274:                              (!&Apache::lonnet::usertools_access('','',$item->[2],
  275:                                                            undef,'tools')));
  276:                     push(@primsub,$item);
  277:                 }
  278:                 if (@primsub > 0) {
  279:                     $menu .= &create_submenu($link,$target,$title,\@primsub);
  280:                 } elsif ($link) {
  281:                     $menu .= '<li><a href="'.$link.'" target="'.$target.'">'.&mt($title).'</a></li>';
  282:                 }
  283:             }
  284:         } elsif ($$menuitem[3] eq 'Help') { # special treatment for helplink
  285:             if ($public) {
  286:                 my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  287:                 my $defdom = &Apache::lonnet::default_login_domain();
  288:                 my $to = &Apache::loncommon::build_recipient_list(undef,
  289:                                                                   'helpdeskmail',
  290:                                                                   $defdom,$origmail);
  291:                 if ($to ne '') {
  292:                     $menu .= &prep_menuitem($menuitem); 
  293:                 }
  294:             } else {
  295:                 $menu .= '<li>'.&Apache::loncommon::top_nav_help('Help').'</li>';
  296:             }
  297:         } else {
  298:             $menu .= prep_menuitem($menuitem);
  299:         }
  300:     }
  301:     $menu =~ s/\[domain\]/$env{'user.domain'}/g;
  302:     $menu =~ s/\[user\]/$env{'user.name'}/g;
  303: 
  304:     return "<ol class=\"LC_primary_menu LC_right\">$menu</ol>";
  305: }
  306: 
  307: #returns hashref {user=>'',dom=>''} containing:
  308: #   own name, domain if user is au
  309: #   name, domain of parent author if user is ca or aa
  310: #empty return if user is not an author or not on homeserver
  311: #
  312: #TODO this should probably be moved somewhere more central
  313: #since it can be used by different parts of the system
  314: sub getauthor{
  315:     return unless $env{'request.role'}=~/^(ca|aa|au)/; #nothing to do if user isn't some kind of author
  316: 
  317:                         #co- or assistent author?
  318:     my ($dom, $user) = ($env{'request.role'} =~ /^(?:ca|aa)\.\/($match_domain)\/($match_username)$/)
  319:                        ? ($1, $2) #domain, username of the parent author
  320:                        : @env{ ('request.role.domain', 'user.name') }; #own domain, username
  321: 
  322:     # current server == home server?
  323:     my $home =  &Apache::lonnet::homeserver($user,$dom);
  324:     foreach (&Apache::lonnet::current_machine_ids()){
  325:         return {user => $user, dom => $dom} if $_ eq $home;
  326:     }
  327: 
  328:     # if wrong server
  329:     return;
  330: }
  331: 
  332: sub secondary_menu {
  333:     my $menu;
  334: 
  335:     my $crstype = &Apache::loncommon::course_type();
  336:     my $crs_sec = $env{'request.course.id'} . ($env{'request.course.sec'} 
  337:                                                ? "/$env{'request.course.sec'}"
  338:                                                : '');
  339:     my $canedit       = &Apache::lonnet::allowed('mdc', $env{'request.course.id'});
  340:     my $canviewroster = $env{'course.'.$env{'request.course.id'}.'.student_classlist_view'};
  341:     my $canviewgrps   = &Apache::lonnet::allowed('vcg', $crs_sec);
  342:     my $canmodifyuser = &Apache::lonnet::allowed('cst', $crs_sec);
  343:     my $canviewwnew   = &Apache::lonnet::allowed('whn', $crs_sec);
  344:     my $canmodpara    = &Apache::lonnet::allowed('opa', $crs_sec);
  345:     my $canvgr        = &Apache::lonnet::allowed('vgr', $crs_sec);
  346:     my $canmgr        = &Apache::lonnet::allowed('mgr', $crs_sec);
  347:     my $author        = &getauthor();
  348: 
  349:     my %groups = &Apache::lonnet::get_active_groups(
  350:                      $env{'user.domain'}, $env{'user.name'},
  351:                      $env{'course.' . $env{'request.course.id'} . '.domain'},
  352:                      $env{'course.' . $env{'request.course.id'} . '.num'});
  353: 
  354:     foreach my $menuitem (@secondary_menu) {
  355:         # evaluate conditions 
  356:         next if    ref($menuitem)  ne 'ARRAY';
  357:         next if    $$menuitem[4]   ne 'always'
  358:                 && $$menuitem[4]   ne 'author'
  359:                 && !$env{'request.course.id'};
  360:         next if    $$menuitem[4]   =~ /^mdc/
  361:                 && !$canedit;
  362:         next if    $$menuitem[4]  eq 'mdcCourse'
  363:                 && ($crstype eq 'Community');
  364:         next if    $$menuitem[4]  eq 'mdcCommunity'
  365:                 && ($crstype eq 'Course');
  366:         next if    $$menuitem[4]  eq 'nvgr'
  367:                 && $canvgr;
  368:         next if    $$menuitem[4]  eq 'vgr'
  369:                 && !$canvgr;
  370:         next if    $$menuitem[4]   eq 'cst'
  371:                 && !$canmodifyuser;
  372:         next if    $$menuitem[4]   eq 'ncst'
  373:                 && ($canmodifyuser || !$canviewroster);
  374:         next if    $$menuitem[4]   eq 'mgr'
  375:                 && !$canmgr;
  376:         next if    $$menuitem[4]   eq 'nmgr'
  377:                 && $canmgr;
  378:         next if    $$menuitem[4]   eq 'whn'
  379:                 && !$canviewwnew;
  380:         next if    $$menuitem[4]   eq 'opa'
  381:                 && !$canmodpara;
  382:         next if    $$menuitem[4]   eq 'nvcg'
  383:                 && ($canviewgrps || !%groups);
  384:         next if    $$menuitem[4]    eq 'author'
  385:                 && !$author;
  386: 
  387:         my $title = $menuitem->[3];
  388:         if (defined($secondary_submenu{$title})) {
  389:             my ($link,$target);
  390:             if ($menuitem->[0] ne '') {
  391:                 $link = $menuitem->[0];
  392:                 $target = '_top';
  393:             } else {
  394:                 $link = '#';
  395:             }
  396:             my @scndsub;   
  397:             if (ref($secondary_submenu{$title}) eq 'ARRAY') {
  398:                 foreach my $item (@{$secondary_submenu{$title}}) {
  399:                     if (ref($item) eq 'ARRAY') {
  400:                         next if ($item->[2] eq 'vgr' && !$canvgr);
  401:                         next if ($item->[2] eq 'opa' && !$canmodpara);
  402:                         next if ($item->[2] eq 'cst' && !$canmodifyuser);
  403:                         next if ($item->[2] eq 'mgr' && !$canmgr);
  404:                         next if ($item->[2] eq 'vcg' && !$canviewgrps);
  405:                         push(@scndsub,$item); 
  406:                     }
  407:                 }
  408:                 if (@scndsub > 0) {
  409:                     $menu .= &create_submenu($link,$target,$title,\@scndsub);
  410:                 } elsif ($link) {
  411:                     $menu .= '<li><a href="'.$link.'" target="'.$target.'">'.&mt($title).'</a></li>';
  412:                 }
  413:             }
  414:         } elsif ($$menuitem[3] eq 'Roles' && $env{'request.course.id'}) {
  415:             # special treatment for role selector
  416:             my $roles_selector = &roles_selector(
  417:                         $env{'course.' . $env{'request.course.id'} . '.domain'},
  418:                         $env{'course.' . $env{'request.course.id'} . '.num'}  );
  419: 
  420:             $menu .= $roles_selector ? "<li style=\"padding: 0 0.8em;\">$roles_selector</li>"
  421:                                      : '';
  422:         } else {
  423:             $menu .= &prep_menuitem(\@$menuitem);
  424:         }
  425:     }
  426:     if ($menu =~ /\[url\].*\[symb\]/) {
  427:         my $escurl  = &escape( &Apache::lonenc::check_encrypt(
  428:                              $env{'request.noversionuri'}));
  429: 
  430:         my $escsymb = &escape( &Apache::lonenc::check_encrypt(
  431:                              $env{'request.symb'})); 
  432: 
  433:         if (    $env{'request.state'} eq 'construct'
  434:             and (   $env{'request.noversionuri'} eq '' 
  435:                  || !defined($env{'request.noversionuri'}))) 
  436:         {
  437:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  438:             ($escurl = $env{'request.filename'}) =~ s{^\Q$londocroot\E}{};
  439:             $escurl  = &escape($escurl);
  440:         }    
  441:         $menu =~ s/\[url\]/$escurl/g;
  442:         $menu =~ s/\[symb\]/$escsymb/g;
  443:     }
  444:     $menu =~ s/\[uname\]/$$author{user}/g;
  445:     $menu =~ s/\[udom\]/$$author{dom}/g;
  446:     if ($menu) {
  447:         $menu = "<ul id=\"LC_secondary_menu\">$menu</ul>";
  448:     }
  449:     return $menu;
  450: }
  451: 
  452: sub create_submenu {
  453:     my ($link,$target,$title,$submenu) = @_;
  454:     return unless (ref($submenu) eq 'ARRAY');
  455:     my $disptarget;
  456:     if ($target ne '') {
  457:         $disptarget = ' target="'.$target.'"';
  458:     }
  459:     my $menu = '<li class="LC_hoverable">'.
  460:                '<a href="'.$link.'"'.$disptarget.'>'. 
  461:                '<span class="LC_nobreak">'.&mt($title).
  462:                '<span class="LC_fontsize_small" style="font-weight:normal;">'.
  463:                ' &#9660;</span></span></a>'.
  464:                '<ul>';
  465:     my $count = 0;
  466:     my $numsub = scalar(@{$submenu});
  467:     foreach my $item (@{$submenu}) {
  468:         $count ++;
  469:         if (ref($item) eq 'ARRAY') {
  470:             my $borderbot;
  471:             if ($count == $numsub) {
  472:                 $borderbot = 'border-bottom:1px solid black;';
  473:             }
  474:             $menu .= '<li style="margin:0;padding:0;'.
  475:                      $borderbot.'"><a href="'.$item->[0].'">'.
  476:                      &mt($item->[1]).'</a></li>';
  477:         }
  478:     }
  479:     $menu .= '</ul></li>';
  480:     return $menu;
  481: }
  482: 
  483: sub registerurl {
  484:     my ($forcereg) = @_;
  485:     my $result = '';
  486:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  487:     my $force_title='';
  488:     if ($env{'request.state'} eq 'construct') {
  489:         $force_title=&Apache::lonxml::display_title();
  490:     }
  491:     if (($env{'environment.remote'} eq 'off') ||
  492:         ((($env{'request.publicaccess'}) ||
  493:          (!&Apache::lonnet::is_on_map(
  494:            &unescape($env{'request.noversionuri'})))) &&
  495:         (!$forcereg))) {
  496:         return
  497:         $result
  498:        .'<script type="text/javascript">'."\n"
  499:        .'// <![CDATA['."\n"
  500:        .'function LONCAPAreg(){;} function LONCAPAstale(){}'."\n"
  501:        .'// ]]>'."\n"
  502:        .'</script>'
  503:        .$force_title;
  504:     }
  505: # Graphical display after login only
  506:     if ($env{'request.registered'} && !$forcereg) { return ''; }
  507:     $result.=&innerregister($forcereg);
  508:     return $result.$force_title;
  509: }
  510: 
  511: sub innerregister {
  512:     my ($forcereg,$bread_crumbs) = @_;
  513:     my $const_space = ($env{'request.state'} eq 'construct');
  514:     my $is_const_dir = 0;
  515: 
  516:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  517: 
  518:     $env{'request.registered'} = 1;
  519: 
  520:     my $noremote = ($env{'environment.remote'} eq 'off');
  521: 
  522:     undef(@inlineremote);
  523: 
  524:     my $reopen=&Apache::lonmenu::reopenmenu();
  525: 
  526:     my $newmail='';
  527: 
  528:     if (&Apache::lonmsg::newmail() && !$noremote) {
  529:         # We have new mail and remote is up
  530:         $newmail= 'swmenu.setstatus("you have","messages");';
  531:     }
  532: 
  533:     my $resurl; 
  534:     if ( $env{'request.symb'} && $env{'request.course.id'} ) {
  535: 
  536:         (my $mapurl, my $rid, $resurl) = &Apache::lonnet::decode_symb(&Apache::lonnet::symbread());
  537:         my $coursetitle = $env{'course.'.$env{'request.course.id'}.'.description'};
  538: 
  539:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
  540:         my $restitle = &Apache::lonnet::gettitle(&Apache::lonnet::symbread());
  541: 
  542: #SD
  543: #course_type only Course and Community?
  544: #
  545:         my @crumbs;
  546:         unless (($forcereg) && ($env{'request.noversionuri'} eq '/adm/navmaps')
  547:                 && ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'})) {
  548:             @crumbs = ({text  => Apache::loncommon::course_type() 
  549:                                 . ' Contents', 
  550:                         href  => "Javascript:gopost('/adm/navmaps','')"});
  551:         }
  552:         if ($mapurl ne $env{'course.'.$env{'request.course.id'}.'.url'}) { 
  553:             push(@crumbs, {text  => '...',
  554:                            no_mt => 1});
  555:         }
  556: 
  557:         push @crumbs, {text => $maptitle, no_mt => 1} if ($maptitle 
  558:                                                    && $maptitle ne 'default.sequence' 
  559:                                                    && $maptitle ne $coursetitle);
  560: 
  561:         push @crumbs, {text => $restitle, no_mt => 1} if $restitle; 
  562: 
  563:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  564:         &Apache::lonhtmlcommon::add_breadcrumb(@crumbs);
  565:     }elsif (! $const_space){
  566:         #a situation when we're looking at a resource outside of context of a 
  567:         #course or construction space (e.g. with cumulative rights)
  568:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  569:         &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  570:     }
  571:     my $timesync   = ( $noremote ? '' : 'swmenu.syncclock(1000*'.time.');' );
  572: # =============================================================================
  573: # ============================ This is for URLs that actually can be registered
  574:     if ( ($env{'request.noversionuri'}!~m{^/(res/)*adm/})
  575:                        || ($forcereg)) {
  576: # -- This applies to homework problems for users with grading privileges
  577: 	my $crs='/'.$env{'request.course.id'};
  578: 	if ($env{'request.course.sec'}) {
  579: 	    $crs.='_'.$env{'request.course.sec'};
  580: 	}
  581: 	$crs=~s/\_/\//g;
  582: 
  583:         my %swtext;
  584:         if ($noremote) {
  585:             %swtext = &get_inline_text();
  586:         } else {
  587:             %swtext = &get_rc_text();
  588:         } 
  589:         my $hwkadd='';
  590:         if ($env{'request.symb'} ne '' &&
  591: 	    $env{'request.filename'}=~/$LONCAPA::assess_re/) {
  592: 	    if (&Apache::lonnet::allowed('mgr',$crs)) {
  593: 		$hwkadd.=&switch('','',7,2,'pgrd.png',$swtext{'pgrd'},'grades[_4]',
  594:                        "gocmd('/adm/grades','gradingmenu')",
  595:                        'Content Grades');
  596:             } elsif (&Apache::lonnet::allowed('vgr',$crs)) {
  597: 		$hwkadd.=&switch('','',7,2,'subm.png',$swtext{'subm'},'missions[_1]',
  598:                        "gocmd('/adm/grades','submission')",
  599: 		       'Content Submissions');
  600:             }
  601: 	}
  602: 	if ($env{'request.symb'} ne '' &&
  603: 	    &Apache::lonnet::allowed('opa',$crs)) {
  604: 	    $hwkadd.=&switch('','',7,3,'pparm.png',$swtext{'pparm'},'parms[_2]',
  605: 			     "gocmd('/adm/parmset','set')",
  606: 			     'Content Settings');
  607: 	}
  608:         if ($noremote) {  
  609:             if ($env{'request.symb'}=~/^uploaded/ &&
  610:                 &Apache::lonnet::allowed('mdc',$crs)) {
  611:                 $hwkadd.=&switch('','',7,4,'docs.png',$swtext{'docs'},'parms[_2]',
  612:                                  "gocmd('/adm/coursedocs','direct')",
  613:                                  'Folder/Page Content');
  614:             }
  615:         }
  616: # -- End Homework
  617:         ###
  618:         ### Determine whether or not to display the 'cstr' button for this
  619:         ### resource
  620:         ###
  621:         my $editbutton = '';
  622:         my $noeditbutton = 1;
  623:         my ($cnum,$cdom);
  624:         if ($env{'request.course.id'}) {
  625:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  626:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  627:         }
  628:         if ($env{'user.author'}) {
  629:             if ($env{'request.role'}=~/^(aa|ca|au)/) {
  630: #
  631: # We have the role of an author
  632: #
  633:                 # Set defaults for authors
  634:                 my ($top,$bottom) = ('con-','struct');
  635:                 my $action = "go('/priv/".$env{'user.domain'}.'/'.$env{'user.name'}."');";
  636:                 my $cadom  = $env{'request.role.domain'};
  637:                 my $caname = $env{'user.name'};
  638:                 my $desc = "Enter my construction space";
  639:                 # Set defaults for co-authors
  640:                 if ($env{'request.role'} =~ /^ca/) { 
  641:                     ($cadom,$caname)=($env{'request.role'}=~/($match_domain)\/($match_username)$/);
  642:                     ($top,$bottom) = ('co con-','struct');
  643:                     $action = "go('/priv/".$cadom.'/'.$caname."');";
  644:                     $desc = "Enter construction space as co-author";
  645:                 } elsif ($env{'request.role'} =~ /^aa/) {
  646:                     ($cadom,$caname)=($env{'request.role'}=~/($match_domain)\/($match_username)$/);
  647:                     ($top,$bottom) = ('co con-','struct');
  648:                     $action = "go('/priv/".$cadom.'/'.$caname."');";
  649:                     $desc = "Enter construction space as assistant co-author";
  650:                 }
  651:                 # Check that we are on the correct machine
  652:                 my $home = &Apache::lonnet::homeserver($caname,$cadom);
  653: 		my $allowed=0;
  654: 		my @ids=&Apache::lonnet::current_machine_ids();
  655: 		foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  656: 		if (!$allowed) {
  657: 		    $editbutton=&switch('','',6,1,,$top,$bottom,$action,$desc);
  658:                     $noeditbutton = 0;
  659:                 }
  660:             }
  661: #
  662: # We are an author for some stuff, but currently do not have the role of author.
  663: # Figure out if we have authoring privileges for the resource we are looking at.
  664: # This should maybe become a privilege check in lonnet
  665: #
  666:             ##
  667:             ## Determine if user can edit url.
  668:             ##
  669:             my $cfile='';
  670:             my $cfuname='';
  671:             my $cfudom='';
  672:             my $uploaded;
  673:             my $switchserver='';
  674:             my $home;
  675:             if ($env{'request.filename'}) {
  676:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
  677:                 if (defined($cnum) && defined($cdom)) {
  678:                     $uploaded = &is_course_upload($file,$cnum,$cdom);
  679:                 }
  680:                 if (!$uploaded) {
  681: 
  682:                     $file=~s{^(priv/$match_domain/$match_username)}{/$1};
  683:                     $file=~s{^($match_domain/$match_username)}{/priv/$1};
  684: 
  685:                     # Check that the user has permission to edit this resource
  686:                     my $setpriv = 1;
  687:                     ($cfuname,$cfudom)=&Apache::loncacc::constructaccess($file,$setpriv);
  688:                     if (defined($cfudom)) {
  689: 		        $home=&Apache::lonnet::homeserver($cfuname,$cfudom);
  690: 		        my $allowed=0;
  691: 		        my @ids=&Apache::lonnet::current_machine_ids();
  692: 		        foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  693: 		        if ($allowed) {
  694:                             $cfile=$file;
  695:                         } else {
  696:                             $switchserver=$file;
  697:                         }
  698:                     }
  699:                 }
  700:             }
  701:             # Finally, turn the button on or off
  702:             if (($cfile || $switchserver) && !$const_space) {
  703:                 my $nocrsedit;
  704:                 # Suppress display where CC has switched to student role.
  705:                 if ($env{'request.course.id'}) {
  706:                     unless(&Apache::lonnet::allowed('mdc',
  707:                                                     $env{'request.course.id'})) {
  708:                         $nocrsedit = 1;
  709:                     }
  710:                 }
  711:                 if ($nocrsedit) {
  712:                     $editbutton=&clear(6,1);
  713:                 } else {
  714:                     my $bot = "go('$cfile')";
  715:                     if ($switchserver) {
  716:                         if ( $env{'request.symb'} && $env{'request.course.id'} ) {
  717:                             $cfile = '/adm/switchserver?otherserver='.$home.'&amp;role='.
  718:                                      &HTML::Entities::encode($env{'request.role'},'"<>&').'&amp;symb='.
  719:                                      &HTML::Entities::encode($env{'request.symb'},'"<>&');
  720:                             $bot = "need_switchserver('$cfile');";
  721:                         }
  722:                     }
  723:                     $editbutton=&switch
  724:                        ('','',6,1,'pcstr.png',$swtext{'pcstr'},'resource[_2]',
  725:                         $bot,"Edit this resource");
  726:                     $noeditbutton = 0;
  727:                 }
  728:             } elsif ($editbutton eq '') {
  729:                 $editbutton=&clear(6,1);
  730:             }
  731:         }
  732:         if (($noeditbutton) && ($env{'request.filename'})) { 
  733:             if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  734:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
  735:                 if (defined($cnum) && defined($cdom)) {
  736:                     if (&is_course_upload($file,$cnum,$cdom)) {
  737:                         my $cfile = &edit_course_upload($file,$cnum,$cdom);
  738:                         if ($cfile) {
  739:                             $editbutton=&switch
  740:                                         ('','',6,1,'pcstr.png',$swtext{'pcstr'},
  741:                                          'resource[_2]',"go('".$cfile."');",
  742:                                          'Edit this resource');
  743:                         }
  744:                     }
  745:                 }
  746:             }
  747:         }
  748:         if ($env{'request.course.id'}) {
  749:             if ($resurl eq "public/$cdom/$cnum/syllabus") {
  750:                 if ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ /\w/) {
  751:                     if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  752:                         $editbutton=&switch('','',6,1,'pcstr.png',$swtext{'pcstr'},
  753:                                             'resource[_2]',
  754:                                             "go('/adm/courseprefs?phase=display&actions=courseinfo')",
  755:                                             'Edit this resource');
  756:                     }
  757:                 }
  758:             }
  759:         }
  760:         ###
  761:         ###
  762: # Prepare the rest of the buttons
  763:         my ($menuitems,$got_prt,$got_wishlist,$cstritems);
  764:         if ($const_space) {
  765: #
  766: # We are in construction space
  767: #
  768: 
  769:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  770: 	    my ($udom,$uname,$thisdisfn) =
  771: 		($env{'request.filename'}=~m{^\Q$londocroot/priv/\E([^/]+)/([^/]+)/(.*)$});
  772:             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
  773:             if ($currdir =~ m-/$-) {
  774:                 $is_const_dir = 1;
  775:             } else {
  776:                 $currdir =~ s|[^/]+$||;
  777: 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
  778: 		my $esc_currdir = &Apache::loncommon::escape_single($currdir);
  779: #
  780: # Probably should be in mydesk.tab
  781: #
  782:                 $menuitems=(<<ENDMENUITEMS);
  783: s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
  784: s&6&2&rtrv.png&Retrieve&version[_1]&gocstr('/adm/retrieve','/priv/$udom/$uname/$cleandisfn')&Retrieve old version
  785: s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/priv/$udom/$uname/$cleandisfn')&Publish this resource
  786: s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/priv/$udom/$uname/$cleandisfn')&Delete this resource
  787: s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/priv/$udom/$uname/$cleandisfn')&Prepare a printable document
  788: ENDMENUITEMS
  789:                 unless ($noremote) {
  790:                     $cstritems = $menuitems;
  791:                     undef($menuitems);
  792:                 }
  793:             }
  794:                 if (ref($bread_crumbs) eq 'ARRAY') {
  795:                     &Apache::lonhtmlcommon::clear_breadcrumbs();
  796:                     foreach my $crumb (@{$bread_crumbs}){
  797:                         &Apache::lonhtmlcommon::add_breadcrumb($crumb);
  798:                     }
  799:                 }
  800:         } elsif ( defined($env{'request.course.id'}) && 
  801: 		 $env{'request.symb'} ne '' ) {
  802: #
  803: # We are in a course and looking at a registered URL
  804: # Should probably be in mydesk.tab
  805: #
  806: 
  807: 	    $menuitems=(<<ENDMENUITEMS);
  808: c&3&1
  809: s&2&1&back.png&$swtext{'back'}&&gopost('/adm/flip','back:'+currentURL)&Previous content resource&&1
  810: s&2&3&forw.png&$swtext{'forw'}&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3
  811: c&6&3
  812: c&8&1
  813: c&8&2
  814: s&8&3&prt.png&$swtext{'prt'}&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  815: ENDMENUITEMS
  816:             $got_prt = 1;
  817:             if (($env{'user.adv'}) && ($env{'request.uri'} =~ /^\/res/)
  818:                 && (!$env{'request.enc'})) {
  819:                 # wishlist is only available for users with access to resource-pool
  820:                 # and links can only be set for resources within the resource-pool
  821:                 $menuitems .= (<<ENDMENUITEMS);
  822: s&9&1&alnk.png&$swtext{'alnk'}&linkstor[_1]&set_wishlistlink()&Save a link for this resource in your personal Stored Links repository&&1
  823: ENDMENUITEMS
  824:                 $got_wishlist = 1;
  825:             }
  826: 
  827: my $currentURL = &Apache::loncommon::get_symb();
  828: my ($symb_old,$symb_old_enc) = &Apache::loncommon::clean_symb($currentURL);
  829: my $annotation = &Apache::loncommon::get_annotation($symb_old,$symb_old_enc);
  830: $menuitems.="s&9&3&";
  831: if(length($annotation) > 0){
  832: 	$menuitems.="anot2.png";
  833: }else{
  834: 	$menuitems.="anot.png";
  835: }
  836: $menuitems.="&$swtext{'anot'}&tations[_1]&annotate()&";
  837: $menuitems.="Make notes and annotations about this resource&&1\n";
  838: 
  839:             unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio)(\?|$)/) {
  840: 		if ((!$env{'request.enc'}) && ($env{'request.noversionuri'} !~ m{^/adm/wrapper/ext/}) && ($env{'request.noversionuri'} !~ m{^/uploaded/$match_domain/$match_courseid/docs/})) {
  841: 		    $menuitems.=(<<ENDREALRES);
  842: s&6&3&catalog.png&$swtext{'catalog'}&info[_1]&catalog_info()&Show Metadata
  843: ENDREALRES
  844:                 }
  845:                 unless ($env{'request.noversionuri'} =~ m{^/uploaded/$match_domain/$match_courseid/docs/}) {
  846: 	            $menuitems.=(<<ENDREALRES);
  847: s&8&1&eval.png&$swtext{'eval'}&this[_1]&gopost('/adm/evaluate',currentURL,1)&Provide my evaluation of this resource
  848: ENDREALRES
  849:                 }
  850:                 $menuitems.=(<<ENDREALRES);
  851: 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
  852: ENDREALRES
  853: 	    }
  854:         }
  855: 	if ($env{'request.uri'} =~ /^\/res/) {
  856:             unless ($got_prt) {
  857: 	        $menuitems .= (<<ENDMENUITEMS);
  858: s&8&3&prt.png&$swtext{'prt'}&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  859: ENDMENUITEMS
  860:                 $got_prt = 1;
  861:             }
  862:             unless ($got_wishlist) {
  863:                 if (($env{'user.adv'}) && (!$env{'request.enc'})) {
  864:                     # wishlist is only available for users with access to resource-pool
  865:                     $menuitems .= (<<ENDMENUITEMS);
  866: s&9&1&alnk.png&$swtext{'alnk'}&linkstor[_1]&set_wishlistlink()&Save a link for this resource in your personal Stored Links repository&&1
  867: ENDMENUITEMS
  868:                     $got_wishlist = 1;
  869:                 }
  870:             }
  871: 	}
  872:         unless ($got_prt) {
  873:             $menuitems .= (<<ENDMENUITEMS);
  874: c&8&3
  875: ENDMENUITEMS
  876:         }
  877:         unless ($got_wishlist) {
  878:             $menuitems .= (<<ENDMENUITEMS);
  879: c&9&1
  880: ENDMENUITEMS
  881:         }
  882:         my $buttons='';
  883:         foreach (split(/\n/,$menuitems)) {
  884: 	    my ($command,@rest)=split(/\&/,$_);
  885:             my $idx=10*$rest[0]+$rest[1];
  886:             if (&hidden_button_check() eq 'yes') {
  887:                 if ($idx == 21 ||$idx == 23) {
  888:                     $buttons.=&switch('','',@rest);
  889:                 } else {
  890:                     $buttons.=&clear(@rest);
  891:                 }
  892:             } else {  
  893:                 if ($command eq 's') {
  894: 	            $buttons.=&switch('','',@rest);
  895:                 } else {
  896:                     $buttons.=&clear(@rest);
  897:                 }
  898:             }
  899:         }
  900:         if ($noremote) {
  901: 	    my $addremote=0;
  902: 	    foreach (@inlineremote) { if ($_ ne '') { $addremote=1; last;} }
  903:             if ($addremote) {
  904: 
  905:                 &Apache::lonhtmlcommon::clear_breadcrumb_tools();
  906: 
  907:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
  908:                     'navigation', @inlineremote[21,23]);
  909: 
  910:                 my $countdown = &countdown_timer();
  911:                 if (&hidden_button_check() eq 'yes') {
  912:                     if ($countdown) {
  913:                         &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$countdown);
  914:                     }
  915:                 } else {
  916:                     my @tools = @inlineremote[93,91,81,82,83];
  917:                     if ($countdown) {
  918:                         unshift(@tools,$countdown);
  919:                     }
  920:                     &Apache::lonhtmlcommon::add_breadcrumb_tool(
  921:                         'tools',@tools);
  922: 
  923:                     #publish button in construction space
  924:                     if ($env{'request.state'} eq 'construct'){
  925:                         &Apache::lonhtmlcommon::add_breadcrumb_tool(
  926:                             'advtools', $inlineremote[63]);
  927:                     } else {
  928:                         &Apache::lonhtmlcommon::add_breadcrumb_tool(
  929:                             'tools', $inlineremote[63]);
  930:                     }
  931:                     unless ($env{'request.noversionuri'}=~ m{^/adm/(navmaps|viewclasslist)(\?|$)}) {
  932:                         &Apache::lonhtmlcommon::add_breadcrumb_tool(
  933:                             'advtools', @inlineremote[61,71,72,73,74,92]);
  934:                     }
  935:                 }
  936:             }
  937:             return   &Apache::lonhtmlcommon::scripttag('', 'start')
  938:                    . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
  939:                    . &Apache::lonhtmlcommon::scripttag('', 'end');
  940: 
  941:         } else {
  942:             my $cstrcrumbs;
  943:             if ($const_space) {
  944:                 foreach (split(/\n/,$cstritems)) {
  945:                     my ($command,@rest)=split(/\&/,$_);
  946:                     my $idx=10*$rest[0]+$rest[1];
  947:                     &switch('','',@rest);
  948:                 }
  949:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('advtools',
  950:                                                             @inlineremote[63,61,71,72]);
  951: 
  952:                 $cstrcrumbs = &Apache::lonhtmlcommon::scripttag('', 'start')
  953:                              .&Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
  954:                              .&Apache::lonhtmlcommon::scripttag('', 'end');
  955:             }
  956:             my $requri=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
  957:             $requri=&Apache::lonenc::check_encrypt(&unescape($requri));
  958:             my $cursymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
  959:             my $navstatus=&get_nav_status();
  960:             my $clearcstr;
  961: 
  962:             if ($env{'user.adv'}) { $clearcstr='clearbut(6,1)'; }
  963:             return <<ENDREGTHIS;
  964: 
  965: <script type="text/javascript">
  966: // <![CDATA[
  967: // BEGIN LON-CAPA Internal
  968: var swmenu=null;
  969: 
  970:     function LONCAPAreg() {
  971:           swmenu=$reopen;
  972:           swmenu.clearTimeout(swmenu.menucltim);
  973:           $timesync
  974:           $newmail
  975:           $buttons
  976:           swmenu.currentURL="$requri";
  977:           swmenu.reloadURL=swmenu.currentURL+window.location.search;
  978:           swmenu.currentSymb="$cursymb";
  979:           swmenu.reloadSymb="$cursymb";
  980:           swmenu.currentStale=0;
  981:           $navstatus
  982:           $hwkadd
  983:           $editbutton
  984:     }
  985: 
  986:     function LONCAPAstale() {
  987:           swmenu=$reopen
  988:           swmenu.currentStale=1;
  989:           if (swmenu.reloadURL!='' && swmenu.reloadURL!= null) {
  990:              swmenu.switchbutton
  991:              (3,1,'reload.gif','return','location','go(reloadURL)','Return to the last known location in the course sequence');
  992:           }
  993:           swmenu.clearbut(7,2);
  994:           swmenu.clearbut(7,3);
  995:           swmenu.menucltim=swmenu.setTimeout(
  996:  'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
  997:  'clearbut(9,1);clearbut(9,3);clearbut(6,3);$clearcstr',
  998:                           2000);
  999:       }
 1000: 
 1001: // END LON-CAPA Internal
 1002: // ]]>
 1003: </script>
 1004: 
 1005: $cstrcrumbs
 1006: ENDREGTHIS
 1007:         }
 1008:     } else {
 1009:        unless ($noremote) {
 1010: # Not registered, graphical
 1011:            return (<<ENDDONOTREGTHIS);
 1012: 
 1013: <script type="text/javascript">
 1014: // <![CDATA[
 1015: // BEGIN LON-CAPA Internal
 1016: var swmenu=null;
 1017: 
 1018:     function LONCAPAreg() {
 1019:           swmenu=$reopen
 1020:           $timesync
 1021:           swmenu.currentStale=1;
 1022:           swmenu.clearbut(2,1);
 1023:           swmenu.clearbut(2,3);
 1024:           swmenu.clearbut(8,1);
 1025:           swmenu.clearbut(8,2);
 1026:           swmenu.clearbut(8,3);
 1027:           swmenu.clearbut(9,1);
 1028:           if (swmenu.currentURL) {
 1029:              swmenu.switchbutton
 1030:               (3,1,'reload.gif','return','location','go(currentURL)');
 1031:           } else {
 1032:               swmenu.clearbut(3,1);
 1033:           }
 1034:     }
 1035: 
 1036:     function LONCAPAstale() {
 1037:     }
 1038: 
 1039: // END LON-CAPA Internal
 1040: // ]]>
 1041: </script>
 1042: ENDDONOTREGTHIS
 1043: 
 1044:         }
 1045:         return '';
 1046:     }
 1047: }
 1048: 
 1049: sub get_inline_text {
 1050:     my %text = (
 1051:                  pgrd     => 'Content Grades',
 1052:                  subm     => 'Content Submissions',
 1053:                  pparm    => 'Content Settings',
 1054:                  docs     => 'Folder/Page Content',
 1055:                  pcstr    => 'Edit',
 1056:                  prt      => 'Print',
 1057:                  alnk     => 'Stored Links',
 1058:                  anot     => 'Notes',
 1059:                  catalog  => 'Info',
 1060:                  eval     => 'Evaluate',
 1061:                  fdbk     => 'Feedback',
 1062:     );
 1063:     return %text;
 1064: }
 1065: 
 1066: sub get_rc_text {
 1067:     my %text = (
 1068:                    pgrd    => 'problem[_1]',
 1069:                    subm    => 'view sub-[_1]',
 1070:                    pparm   => 'problem[_2]',
 1071:                    pcstr   => 'edit[_1]',
 1072:                    prt     => 'prepare[_1]',
 1073:                    back    => 'backward[_1]',
 1074:                    forw    => 'forward[_1]',
 1075:                    alnk    => 'add to[_1]',
 1076:                    anot    => 'anno-[_1]',
 1077:                    catalog => 'catalog[_2]',
 1078:                    eval    => 'evaluate[_1]',
 1079:                    fdbk    => 'feedback[_1]',
 1080:     );
 1081:     return %text;
 1082: }
 1083: 
 1084: sub is_course_upload {
 1085:     my ($file,$cnum,$cdom) = @_;
 1086:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 1087:     $uploadpath =~ s{^\/}{};
 1088:     if (($file =~ m{^\Q$uploadpath\E/userfiles/docs/}) ||
 1089:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/docs/})) {
 1090:         return 1;
 1091:     }
 1092:     return;
 1093: }
 1094: 
 1095: sub edit_course_upload {
 1096:     my ($file,$cnum,$cdom) = @_;
 1097:     my $cfile;
 1098:     if ($file =~/\.(htm|html|css|js|txt)$/) {
 1099:         my $ext = $1;
 1100:         my $url = &Apache::lonnet::hreflocation('',$file);
 1101:         my $home = &Apache::lonnet::homeserver($cnum,$cdom);
 1102:         my @ids=&Apache::lonnet::current_machine_ids();
 1103:         my $dest;
 1104:         if ($home && grep(/^\Q$home\E$/,@ids)) {
 1105:             $dest = $url.'?forceedit=1';
 1106:         } else {
 1107:             unless (&Apache::lonnet::get_locks()) {
 1108:                 $dest = '/adm/switchserver?otherserver='.
 1109:                         $home.'&role='.$env{'request.role'}.
 1110:                         '&url='.$url.'&forceedit=1';
 1111:             }
 1112:         }
 1113:         if ($dest) {
 1114:             $cfile = &HTML::Entities::encode($dest,'"<>&');
 1115:         }
 1116:     }
 1117:     return $cfile;
 1118: }
 1119: 
 1120: sub loadevents() {
 1121:     if ($env{'request.state'} eq 'construct' ||
 1122:         $env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
 1123:     return 'LONCAPAreg();';
 1124: }
 1125: 
 1126: sub unloadevents() {
 1127:     if ($env{'request.state'} eq 'construct' ||
 1128:         $env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
 1129:     return 'LONCAPAstale();';
 1130: }
 1131: 
 1132: sub startupremote {
 1133:     my ($lowerurl)=@_;
 1134:     unless ($env{'environment.remote'} eq 'on') {
 1135:         return ('<meta HTTP-EQUIV="Refresh" CONTENT="0.5; url='.$lowerurl.'" />');
 1136:     }
 1137: #
 1138: # The Remote actually gets launched!
 1139: #
 1140:     my $configmenu=&rawconfig();
 1141:     my $esclowerurl=&escape($lowerurl);
 1142:     my $message=&mt('"Waiting for Remote Control window to load: "+[_1]','waited');
 1143:     return(<<ENDREMOTESTARTUP);
 1144: <script type="text/javascript">
 1145: // <![CDATA[
 1146: var timestart;
 1147: function wheelswitch() {
 1148:     if (typeof(document.wheel) != 'undefined') {
 1149:         if (typeof(document.wheel.spin) != 'undefined') {
 1150:             var date=new Date();
 1151:             var waited=Math.round(30-((date.getTime()-timestart)/1000));
 1152:             document.wheel.spin.value=$message;
 1153:         }
 1154:     }
 1155:    if (window.status=='|') {
 1156:       window.status='/';
 1157:    } else {
 1158:       if (window.status=='/') {
 1159:          window.status='-';
 1160:       } else {
 1161:          if (window.status=='-') {
 1162:             window.status='\\\\';
 1163:          } else {
 1164:             if (window.status=='\\\\') { window.status='|'; }
 1165:          }
 1166:       }
 1167:    }
 1168: }
 1169: 
 1170: // ---------------------------------------------------------- The wait function
 1171: var canceltim;
 1172: function wait() {
 1173:    if ((menuloaded==1) || (tim==1)) {
 1174:       window.status='Done.';
 1175:       if (tim==0) {
 1176:          clearTimeout(canceltim);
 1177:          $configmenu
 1178:          window.location='$lowerurl';
 1179:       } else {
 1180:           window.location='/adm/remote?action=collapse&url=$esclowerurl';
 1181:       }
 1182:    } else {
 1183:       wheelswitch();
 1184:       setTimeout('wait();',200);
 1185:    }
 1186: }
 1187: 
 1188: function main() {
 1189:    canceltim=setTimeout('tim=1;',30000);
 1190:    window.status='-';
 1191:    var date=new Date();
 1192:    timestart=date.getTime();
 1193:    wait();
 1194: }
 1195: 
 1196: // ]]>
 1197: </script>
 1198: ENDREMOTESTARTUP
 1199: }
 1200: 
 1201: sub setflags() {
 1202:     return(<<ENDSETFLAGS);
 1203: <script type="text/javascript">
 1204: // <![CDATA[
 1205:     menuloaded=0;
 1206:     tim=0;
 1207: // ]]>
 1208: </script>
 1209: ENDSETFLAGS
 1210: }
 1211: 
 1212: sub maincall() {
 1213:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1214:     return(<<ENDMAINCALL);
 1215: <script type="text/javascript">
 1216: // <![CDATA[
 1217:     main();
 1218: // ]]>
 1219: </script>
 1220: ENDMAINCALL
 1221: }
 1222: 
 1223: sub load_remote_msg {
 1224:     my ($lowerurl)=@_;
 1225: 
 1226:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1227: 
 1228:     my $esclowerurl=&escape($lowerurl);
 1229:     my $link=&mt('[_1]Continue[_2] on in Inline Menu mode'
 1230:                 ,'<a href="/adm/remote?action=collapse&amp;url='.$esclowerurl.'">'
 1231:                 ,'</a>');
 1232:     return(<<ENDREMOTEFORM);
 1233: <p>
 1234: <form name="wheel">
 1235: <input name="spin" type="text" size="60" />
 1236: </form>
 1237: </p>
 1238: <p>$link</p>
 1239: ENDREMOTEFORM
 1240: }
 1241: 
 1242: sub get_menu_name {
 1243:     my $hostid = $Apache::lonnet::perlvar{'lonHostID'};
 1244:     $hostid =~ s/\W//g;
 1245:     return 'LCmenu'.$hostid;
 1246: }
 1247: 
 1248: 
 1249: sub reopenmenu {
 1250:    unless ($env{'environment.remote'} eq 'on') { return ''; }
 1251:    my $menuname = &get_menu_name();
 1252:    my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
 1253:    return('window.open('.$nothing.',"'.$menuname.'","",false);');
 1254: }
 1255: 
 1256: 
 1257: sub open {
 1258:     my $returnval='';
 1259:     unless ($env{'environment.remote'} eq 'on') {
 1260:         return
 1261:         '<script type="text/javascript">'."\n"
 1262:        .'// <![CDATA['."\n"
 1263:        .'self.name="loncapaclient";'."\n"
 1264:        .'// ]]>'."\n"
 1265:        .'</script>';
 1266:     }
 1267:     my $menuname = &get_menu_name();
 1268: 
 1269: #    unless (shift eq 'unix') {
 1270: # resizing does not work on linux because of virtual desktop sizes
 1271: #       $returnval.=(<<ENDRESIZE);
 1272: #if (window.screen) {
 1273: #    self.resizeTo(screen.availWidth-215,screen.availHeight-55);
 1274: #    self.moveTo(190,15);
 1275: #}
 1276: #ENDRESIZE
 1277: #    }
 1278:     $returnval=(<<ENDOPEN);
 1279: // <![CDATA[
 1280: window.status='Opening LON-CAPA Remote Control';
 1281: var menu=window.open("/res/adm/pages/menu.html?inhibitmenu=yes","$menuname",
 1282: "height=375,width=150,scrollbars=no,menubar=no,top=5,left=5,screenX=5,screenY=5");
 1283: self.name='loncapaclient';
 1284: // ]]>
 1285: ENDOPEN
 1286:     return '<script type="text/javascript">'.$returnval.'</script>';
 1287: }
 1288: 
 1289: 
 1290: # ================================================================== Raw Config
 1291: 
 1292: sub clear {
 1293:     my ($row,$col)=@_;
 1294:     if ($env{'environment.remote'} eq 'on') {
 1295:        if (($row<1) || ($row>13)) { return ''; }
 1296:        return "\n".qq(window.status+='.';swmenu.clearbut($row,$col););
 1297:     } else {
 1298:         $inlineremote[10*$row+$col]='';
 1299:         return '';
 1300:     }
 1301: }
 1302: 
 1303: # ============================================ Switch a button or create a link
 1304: # Switch acts on the javascript that is executed when a button is clicked.  
 1305: # The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
 1306: 
 1307: sub switch {
 1308:     my ($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat,$nobreak)=@_;
 1309:     $act=~s/\$uname/$uname/g;
 1310:     $act=~s/\$udom/$udom/g;
 1311:     $top=&mt($top);
 1312:     $bot=&mt($bot);
 1313:     $desc=&mt($desc);
 1314:     my $idx=10*$row+$col;
 1315:     $category_members{$cat}.=':'.$idx;
 1316: 
 1317:     if ($env{'environment.remote'} eq 'on') {
 1318:         if (($row<1) || ($row>13)) { return ''; }
 1319:         if ($env{'request.state'} eq 'construct') {
 1320:             my $text = $top.' '.$bot;
 1321:             $text=~s/\s*\-\s*//gs;
 1322:             my $pic = '<img alt="'.$text.'" src="'.
 1323:                       &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
 1324:                       '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
 1325:            $inlineremote[$idx] =
 1326:                '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.
 1327:                $pic.'<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
 1328:         }
 1329: # Remote
 1330:         $img=~s/\.png$/\.gif/;
 1331:         return "\n".
 1332:  qq(window.status+='.';swmenu.switchbutton($row,$col,"$img","$top","$bot","$act","$desc"););
 1333:     }
 1334: 
 1335: # Inline Menu
 1336:     if ($nobreak==2) { return ''; }
 1337:     my $text=$top.' '.$bot;
 1338:     $text=~s/\s*\-\s*//gs;
 1339: 
 1340:     my $pic=
 1341: 	   '<img alt="'.$text.'" src="'.
 1342: 	   &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
 1343: 	   '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
 1344:     if ($env{'browser.interface'} eq 'faketextual') {
 1345: # Main Menu
 1346: 	   if ($nobreak==3) {
 1347: 	       $inlineremote[$idx]="\n".
 1348: 		   '<td class="LC_menubuttons_text" align="right">'.$text.
 1349: 		   '</td><td align="left">'.
 1350: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td></tr>';
 1351: 	   } elsif ($nobreak) {
 1352: 	       $inlineremote[$idx]="\n<tr>".
 1353: 		   '<td align="left">'.
 1354: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td>
 1355:                     <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>';
 1356: 	   } else {
 1357: 	       $inlineremote[$idx]="\n<tr>".
 1358: 		   '<td align="left">'.
 1359: 		   '<a href="javascript:'.$act.';">'.$pic.
 1360: 		   '</a></td><td class="LC_menubuttons_text" colspan="3">'.
 1361: 		   '<a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$desc.'</span></a></td></tr>';
 1362: 	   }
 1363:     } else {
 1364: # Inline Menu
 1365:         my @tools = (93,91,81,82,83);
 1366:         unless ($env{'request.state'} eq 'construct') {
 1367:             push(@tools,63);
 1368:         }
 1369:         if (($env{'environment.icons'} eq 'iconsonly') &&
 1370:             (grep(/^$idx$/,@tools))) {
 1371:             $inlineremote[$idx] =
 1372:         '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.'</a>';
 1373:         } else {
 1374:             $inlineremote[$idx] =
 1375:        '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.
 1376:        '<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
 1377:         }
 1378:     }
 1379:     return '';
 1380: }
 1381: 
 1382: sub secondlevel {
 1383:     my $output='';
 1384:     my 
 1385:     ($uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat)=@_;
 1386:     if ($prt eq 'any') {
 1387: 	   $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1388:     } elsif ($prt=~/^r(\w+)/) {
 1389:         if ($rol eq $1) {
 1390:            $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1391:         }
 1392:     }
 1393:     return $output;
 1394: }
 1395: 
 1396: sub openmenu {
 1397:     my $menuname = &get_menu_name();
 1398:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1399:     my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
 1400:     return "window.open(".$nothing.",'".$menuname."');";
 1401: }
 1402: 
 1403: sub inlinemenu {
 1404:     undef(@inlineremote);
 1405:     undef(%category_members);
 1406: # calling rawconfig with "1" will evaluate mydesk.tab, even if there is no active remote control
 1407:     &rawconfig(1);
 1408:     my $output='<table><tr>';
 1409:     for (my $col=1; $col<=2; $col++) {
 1410:         $output.='<td class="LC_mainmenu_col_fieldset">';
 1411:         for (my $row=1; $row<=8; $row++) {
 1412:             foreach my $cat (keys(%category_members)) {
 1413:                if ($category_positions{$cat} ne "$col,$row") { next; }
 1414:                #$output.='<table><tr><td colspan="4" class="LC_menubuttons_category">'.&mt($category_names{$cat}).'</td></tr>';
 1415:                $output.='<div class="LC_Box LC_400Box">';
 1416: 	       $output.='<h3 class="LC_hcell">'.&mt($category_names{$cat}).'</h3>';
 1417:                $output.='<table>';
 1418:                my %active=();
 1419:                foreach my $menu_item (split(/\:/,$category_members{$cat})) {
 1420:                   if ($inlineremote[$menu_item]) {
 1421:                      $active{$menu_item}=1;
 1422:                   }
 1423:                }  
 1424:                foreach my $item (sort(keys(%active))) {
 1425:                   $output.=$inlineremote[$item];
 1426:                }
 1427:                $output.='</table>';
 1428:                $output.='</div>';
 1429:             }
 1430:          }
 1431:          $output.="</td>";
 1432:     }
 1433:     $output.="</tr></table>";
 1434:     return $output;
 1435: }
 1436: 
 1437: sub rawconfig {
 1438: #
 1439: # This evaluates mydesk.tab
 1440: # Need to add more positions and more privileges to deal with all
 1441: # menu items.
 1442: #
 1443:     my $textualoverride=shift;
 1444:     my $output='';
 1445:     if ($env{'environment.remote'} eq 'on') {
 1446:        $output.=
 1447:  "window.status='Opening Remote Control';var swmenu=".&openmenu().
 1448: "\nwindow.status='Configuring Remote Control ';";
 1449:     } else {
 1450:         unless ($textualoverride) { return ''; }
 1451:     }
 1452:     my $uname=$env{'user.name'};
 1453:     my $udom=$env{'user.domain'};
 1454:     my $adv=$env{'user.adv'};
 1455:     my $show_course=&Apache::loncommon::show_course();
 1456:     my $author=$env{'user.author'};
 1457:     my $crs='';
 1458:     my $crstype='';
 1459:     if ($env{'request.course.id'}) {
 1460:        $crs='/'.$env{'request.course.id'};
 1461:        if ($env{'request.course.sec'}) {
 1462: 	   $crs.='_'.$env{'request.course.sec'};
 1463:        }
 1464:        $crs=~s/\_/\//g;
 1465:        $crstype = &Apache::loncommon::course_type();
 1466:     }
 1467:     my $pub=($env{'request.state'} eq 'published');
 1468:     my $con=($env{'request.state'} eq 'construct');
 1469:     my $rol=$env{'request.role'};
 1470:     my $requested_domain = $env{'request.role.domain'};
 1471:     foreach my $line (@desklines) {
 1472:         my ($row,$col,$pro,$prt,$img,$top,$bot,$act,$desc,$cat)=split(/\:/,$line);
 1473:         $prt=~s/\$uname/$uname/g;
 1474:         $prt=~s/\$udom/$udom/g;
 1475:         if ($prt =~ /\$crs/) {
 1476:             next unless ($env{'request.course.id'});
 1477:             next if ($crstype eq 'Community');
 1478:             $prt=~s/\$crs/$crs/g;
 1479:         } elsif ($prt =~ /\$cmty/) {
 1480:             next unless ($env{'request.course.id'});
 1481:             next if ($crstype ne 'Community');
 1482:             $prt=~s/\$cmty/$crs/g;
 1483:         }
 1484:         $prt=~s/\$requested_domain/$requested_domain/g;
 1485:         if ($category_names{$cat}!~/\w/) { $cat='oth'; }
 1486:         if ($pro eq 'clear') {
 1487: 	    $output.=&clear($row,$col);
 1488:         } elsif ($pro eq 'any') {
 1489:                $output.=&secondlevel(
 1490: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1491: 	} elsif ($pro eq 'smp') {
 1492:             unless ($adv) {
 1493:                $output.=&secondlevel(
 1494:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1495:             }
 1496:         } elsif ($pro eq 'adv') {
 1497:             if ($adv) {
 1498:                $output.=&secondlevel(
 1499: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1500:             }
 1501: 	} elsif ($pro eq 'shc') {
 1502:             if ($show_course) {
 1503:                $output.=&secondlevel(
 1504:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1505:             }
 1506:         } elsif ($pro eq 'nsc') {
 1507:             if (!$show_course) {
 1508:                $output.=&secondlevel(
 1509: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1510:             }
 1511:         } elsif (($pro=~/^p(\w+)/) && ($prt)) {
 1512:             my $priv = $1;
 1513:             if ($priv =~ /^mdc(Course|Community)/) {
 1514:                 if ($crstype eq $1) {
 1515:                     $priv = 'mdc';
 1516:                 } else {
 1517:                     next;
 1518:                 }
 1519:             }
 1520: 	    if (&Apache::lonnet::allowed($priv,$prt)) {
 1521:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1522:             }
 1523:         } elsif ($pro eq 'course')  {
 1524:             if (($env{'request.course.fn'}) && ($crstype ne 'Community')) {
 1525:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1526: 	    }
 1527:         } elsif ($pro eq 'community')  {
 1528:             if (($env{'request.course.fn'}) && ($crstype eq 'Community')) {
 1529:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1530:             }
 1531:         } elsif ($pro =~ /^courseenv_(.*)$/) {
 1532:             my $key = $1;
 1533:             if ($crstype ne 'Community') {
 1534:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 1535:                 if ($key eq 'canuse_pdfforms') {
 1536:                     if ($env{'request.course.id'} && $coursepref eq '') {
 1537:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 1538:                         $coursepref = $domdefs{'canuse_pdfforms'};
 1539:                     }
 1540:                 }
 1541:                 if ($coursepref) { 
 1542:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1543:                 }
 1544:             }
 1545:         } elsif ($pro =~ /^communityenv_(.*)$/) {
 1546:             my $key = $1;
 1547:             if ($crstype eq 'Community') {
 1548:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 1549:                 if ($key eq 'canuse_pdfforms') {
 1550:                     if ($env{'request.course.id'} && $coursepref eq '') {
 1551:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 1552:                         $coursepref = $domdefs{'canuse_pdfforms'};
 1553:                     }
 1554:                 }
 1555:                 if ($coursepref) { 
 1556:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1557:                 }
 1558:             }
 1559:         } elsif ($pro =~ /^course_(.*)$/) {
 1560:             # Check for permissions inside of a course
 1561:             if (($env{'request.course.id'}) && ($crstype ne 'Community') && 
 1562:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 1563:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 1564:                  )) {
 1565:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1566: 	    }
 1567:         } elsif ($pro =~ /^community_(.*)$/) {
 1568:             # Check for permissions inside of a community
 1569:             if (($env{'request.course.id'}) && ($crstype eq 'Community') &&   
 1570:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 1571:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 1572:                  )) {
 1573:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1574:             }
 1575:         } elsif ($pro eq 'author') {
 1576:             if ($author) {
 1577:                 if ((($prt eq 'rca') && ($env{'request.role'}=~/^ca/)) ||
 1578:                     (($prt eq 'raa') && ($env{'request.role'}=~/^aa/)) || 
 1579:                     (($prt eq 'rau') && ($env{'request.role'}=~/^au/))) {
 1580:                     # Check that we are on the correct machine
 1581:                     my $cadom=$requested_domain;
 1582:                     my $caname=$env{'user.name'};
 1583:                     if (($prt eq 'rca') || ($prt eq 'raa')) {
 1584: 		       ($cadom,$caname)=
 1585:                                ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 1586:                     }                       
 1587:                     $act =~ s/\$caname/$caname/g;
 1588:                     $act =~ s/\$cadom/$cadom/g;
 1589:                     my $home = &Apache::lonnet::homeserver($caname,$cadom);
 1590: 		    my $allowed=0;
 1591: 		    my @ids=&Apache::lonnet::current_machine_ids();
 1592: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 1593: 		    if ($allowed) {
 1594:                         $output.=&switch($caname,$cadom,
 1595:                                         $row,$col,$img,$top,$bot,$act,$desc,$cat);
 1596:                     }
 1597:                 }
 1598:             }
 1599:         } elsif ($pro eq 'tools') {
 1600:             my @tools = ('aboutme','blog','portfolio');
 1601:             if (grep(/^\Q$prt\E$/,@tools)) {
 1602:                 if (!&Apache::lonnet::usertools_access($env{'user.name'},
 1603:                                                        $env{'user.domain'},
 1604:                                                        $prt,undef,'tools')) {
 1605:                     $output.=&clear($row,$col);
 1606:                     next;
 1607:                 }
 1608:             } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
 1609:                 if (($prt eq 'reqcrsnsc') && ($show_course))   {
 1610:                     next;
 1611:                 }
 1612:                 if (($prt eq 'reqcrsshc') && (!$show_course)) {
 1613:                     next;
 1614:                 }
 1615:                 my $showreqcrs = &check_for_rcrs();
 1616:                 if (!$showreqcrs) {
 1617:                     $output.=&clear($row,$col);
 1618:                     next;
 1619:                 }
 1620:             }
 1621:             $prt='any';
 1622:             $output.=&secondlevel(
 1623:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1624:         }
 1625:     }
 1626:     if ($env{'environment.remote'} eq 'on') {
 1627:         $output.="\nwindow.status='Synchronizing Time';swmenu.syncclock(1000*".time.");\nwindow.status='Remote Control Configured.';";
 1628:         if (&Apache::lonmsg::newmail()) {
 1629:             $output.='swmenu.setstatus("you have","messages");';
 1630:         }
 1631:     }
 1632:     return $output;
 1633: }
 1634: 
 1635: sub check_for_rcrs {
 1636:     my $showreqcrs = 0;
 1637:     my @reqtypes = ('official','unofficial','community');
 1638:     foreach my $type (@reqtypes) {
 1639:         if (&Apache::lonnet::usertools_access($env{'user.name'},
 1640:                                               $env{'user.domain'},
 1641:                                               $type,undef,'requestcourses')) {
 1642:             $showreqcrs = 1;
 1643:             last;
 1644:         }
 1645:     }
 1646:     if (!$showreqcrs) {
 1647:         foreach my $type (@reqtypes) {
 1648:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 1649:                 $showreqcrs = 1;
 1650:                 last;
 1651:             }
 1652:         }
 1653:     }
 1654:     return $showreqcrs;
 1655: }
 1656: 
 1657: # ======================================================================= Close
 1658: 
 1659: sub close {
 1660:     unless ($env{'environment.remote'} eq 'on') { return ''; }
 1661:     my $menuname = &get_menu_name();
 1662:     return(<<ENDCLOSE);
 1663: <script type="text/javascript">
 1664: // <![CDATA[
 1665: window.status='Accessing Remote Control';
 1666: menu=window.open("/adm/rat/empty.html","$menuname",
 1667:                  "height=350,width=150,scrollbars=no,menubar=no");
 1668: window.status='Disabling Remote Control';
 1669: menu.active=0;
 1670: menu.autologout=0;
 1671: window.status='Closing Remote Control';
 1672: menu.close();
 1673: window.status='Done.';
 1674: // ]]>
 1675: </script>
 1676: ENDCLOSE
 1677: }
 1678: 
 1679: sub dc_popup_js {
 1680:     my %lt = &Apache::lonlocal::texthash(
 1681:                                           more => '(More ...)',
 1682:                                           less => '(Less ...)',
 1683:                                         );
 1684:     return <<"END";
 1685: 
 1686: function showCourseID() {
 1687:     document.getElementById('dccid').style.display='block';
 1688:     document.getElementById('dccid').style.textAlign='left';
 1689:     document.getElementById('dccid').style.textFace='normal';
 1690:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:hideCourseID();" class="LC_menubuttons_link">$lt{'less'}</a>';
 1691:     return;
 1692: }
 1693: 
 1694: function hideCourseID() {
 1695:     document.getElementById('dccid').style.display='none';
 1696:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:showCourseID()" class="LC_menubuttons_link">$lt{'more'}</a>';
 1697:     return;
 1698: }
 1699: 
 1700: END
 1701: 
 1702: }
 1703: 
 1704: sub countdown_toggle_js {
 1705:     return <<"END";
 1706: 
 1707: function toggleCountdown() {
 1708:     var countdownid = document.getElementById('duedatecountdown');
 1709:     var currstyle = countdownid.style.display;
 1710:     if (currstyle == 'inline') {
 1711:         countdownid.style.display = 'none';
 1712:         document.getElementById('ddcountcollapse').innerHTML='';
 1713:         document.getElementById('ddcountexpand').innerHTML='&#9668;&nbsp;';
 1714:     } else {
 1715:         countdownid.style.display = 'inline';
 1716:         document.getElementById('ddcountcollapse').innerHTML='&#9658;&nbsp;';
 1717:         document.getElementById('ddcountexpand').innerHTML='';
 1718:     }
 1719:     return;
 1720: }
 1721: 
 1722: END
 1723: }
 1724: 
 1725: sub utilityfunctions {
 1726:     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 1727:     if ($currenturl =~ m{^/adm/wrapper/ext/}
 1728:         && $env{'request.external.querystring'} ) {
 1729:             $currenturl .= ($currenturl=~/\?/)?'&':'?'.$env{'request.external.querystring'};
 1730:     }
 1731:     $currenturl=&Apache::lonenc::check_encrypt(&unescape($currenturl));
 1732:     
 1733:     my $currentsymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 1734: 
 1735:     my $dc_popup_cid;
 1736:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 1737:                         $env{'course.'.$env{'request.course.id'}.
 1738:                                  '.domain'}.'/'})) {
 1739:         $dc_popup_cid = &dc_popup_js();
 1740:     }
 1741: 
 1742:     my $start_page_annotate = 
 1743:         &Apache::loncommon::start_page('Annotator',undef,
 1744: 				       {'only_body' => 1,
 1745: 					'js_ready'  => 1,
 1746: 					'bgcolor'   => '#BBBBBB',
 1747: 					'add_entries' => {
 1748: 					    'onload' => 'javascript:document.goannotate.submit();'}});
 1749: 
 1750:     my $end_page_annotate = 
 1751:         &Apache::loncommon::end_page({'js_ready' => 1});
 1752: 
 1753:     my $confirm_switch = &mt("Editing requires switching to the resource's home server.").'\n'.
 1754:                          &mt('Switch server?');
 1755: 
 1756:     my $esc_url=&escape($currenturl);
 1757:     my $esc_symb=&escape($currentsymb);
 1758: 
 1759:     my $countdown = &countdown_toggle_js();
 1760: 
 1761: return (<<ENDUTILITY)
 1762: 
 1763:     var currentURL=unescape("$esc_url");
 1764:     var reloadURL=unescape("$esc_url");
 1765:     var currentSymb=unescape("$esc_symb");
 1766: 
 1767: $dc_popup_cid
 1768: 
 1769: function go(url) {
 1770:    if (url!='' && url!= null) {
 1771:        currentURL = null;
 1772:        currentSymb= null;
 1773:        window.location.href=url;
 1774:    }
 1775: }
 1776: 
 1777: function need_switchserver(url) {
 1778:     if (url!='' && url!= null) {
 1779:         if (confirm("$confirm_switch")) {
 1780:             go(url); 
 1781:         }
 1782:     }
 1783:     return;
 1784: }
 1785: 
 1786: function gopost(url,postdata) {
 1787:    if (url!='') {
 1788:       this.document.server.action=url;
 1789:       this.document.server.postdata.value=postdata;
 1790:       this.document.server.command.value='';
 1791:       this.document.server.url.value='';
 1792:       this.document.server.symb.value='';
 1793:       this.document.server.submit();
 1794:    }
 1795: }
 1796: 
 1797: function gocmd(url,cmd) {
 1798:    if (url!='') {
 1799:       this.document.server.action=url;
 1800:       this.document.server.postdata.value='';
 1801:       this.document.server.command.value=cmd;
 1802:       this.document.server.url.value=currentURL;
 1803:       this.document.server.symb.value=currentSymb;
 1804:       this.document.server.submit();
 1805:    }
 1806: }
 1807: 
 1808: function gocstr(url,filename) {
 1809:     if (url == '/adm/cfile?action=delete') {
 1810:         this.document.cstrdelete.filename.value = filename
 1811:         this.document.cstrdelete.submit();
 1812:         return;
 1813:     }
 1814:     if (url == '/adm/printout') {
 1815:         this.document.cstrprint.postdata.value = filename
 1816:         this.document.cstrprint.curseed.value = 0;
 1817:         this.document.cstrprint.problemtype.value = 0;
 1818:         if (this.document.lonhomework) {
 1819:             if ((this.document.lonhomework.rndseed) && (this.document.lonhomework.rndseed.value != null) && (this.document.lonhomework.rndseed.value != '')) {
 1820:                 this.document.cstrprint.curseed.value = this.document.lonhomework.rndseed.value
 1821:             }
 1822:             if (this.document.lonhomework.problemtype) {
 1823: 		if (this.document.lonhomework.problemtype.value) {
 1824: 		    this.document.cstrprint.problemtype.value = 
 1825: 			this.document.lonhomework.problemtype.value;
 1826: 		} else if (this.document.lonhomework.problemtype.options) {
 1827: 		    for (var i=0; i<this.document.lonhomework.problemtype.options.length; i++) {
 1828: 			if (this.document.lonhomework.problemtype.options[i].selected) {
 1829: 			    if (this.document.lonhomework.problemtype.options[i].value != null && this.document.lonhomework.problemtype.options[i].value != '') { 
 1830: 				this.document.cstrprint.problemtype.value = this.document.lonhomework.problemtype.options[i].value
 1831: 				}
 1832: 			}
 1833: 		    }
 1834: 		}
 1835: 	    }
 1836: 	}
 1837:         this.document.cstrprint.submit();
 1838:         return;
 1839:     }
 1840:     if (url !='') {
 1841:         this.document.constspace.filename.value = filename;
 1842:         this.document.constspace.action = url;
 1843:         this.document.constspace.submit();
 1844:     }
 1845: }
 1846: 
 1847: function golist(url) {
 1848:    if (url!='' && url!= null) {
 1849:        currentURL = null;
 1850:        currentSymb= null;
 1851:        top.location.href=url;
 1852:    }
 1853: }
 1854: 
 1855: 
 1856: 
 1857: function catalog_info() {
 1858:    openMyModal(window.location.pathname+'.meta',500,400,'yes');
 1859: }
 1860: 
 1861: function chat_win() {
 1862:    lonchat=window.open('/res/adm/pages/chatroom.html',"LONchat",'height=320,width=480,resizable=yes,location=no,menubar=no,toolbar=no');
 1863: }
 1864: 
 1865: function group_chat(group) {
 1866:    var url = '/adm/groupchat?group='+group;
 1867:    var winName = 'LONchat_'+group;
 1868:    grpchat=window.open(url,winName,'height=320,width=280,resizable=yes,location=no,menubar=no,toolbar=no');
 1869: }
 1870: 
 1871: function annotate() {
 1872:    w_Annotator_flag=1;
 1873:    annotator=window.open('','Annotator','width=365,height=265,scrollbars=0');
 1874:    annotator.document.write(
 1875:    '$start_page_annotate'
 1876:   +"<form name='goannotate' target='Annotator' method='post' "
 1877:   +"action='/adm/annotations'>"
 1878:   +"<input type='hidden' name='symbnew' value='"+currentSymb+"' />"
 1879:   +"<\\/form>"
 1880:   +'$end_page_annotate');
 1881:    annotator.document.close();
 1882: }
 1883: 
 1884: function open_StoredLinks_Import(rat) {
 1885:    var newWin;
 1886:    if (rat) {
 1887:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import&rat='+rat,
 1888:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1889:    }
 1890:    else {
 1891:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import',
 1892:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1893:    }
 1894:    newWin.focus();
 1895: }
 1896: 
 1897: (function (\$) {
 1898:   \$(document).ready(function () {
 1899:     \$.single=function(a){return function(b){a[0]=b;return a}}(\$([1]));
 1900:     /*\@cc_on
 1901:       if (!window.XMLHttpRequest) {
 1902:         \$('.LC_hoverable').each(function () {
 1903:           this.attachEvent('onmouseenter', function (evt) { \$.single(evt.srcElement).addClass('hover'); });
 1904:           this.attachEvent('onmouseleave', function (evt) { \$.single(evt.srcElement).removeClass('hover'); });
 1905:         });
 1906:       }
 1907:     \@*/
 1908:   });
 1909: }(jQuery));
 1910: 
 1911: $countdown
 1912: 
 1913: ENDUTILITY
 1914: }
 1915: 
 1916: sub serverform {
 1917:     return(<<ENDSERVERFORM);
 1918: <form name="server" action="/adm/logout" method="post" target="_top">
 1919: <input type="hidden" name="postdata" value="none" />
 1920: <input type="hidden" name="command" value="none" />
 1921: <input type="hidden" name="url" value="none" />
 1922: <input type="hidden" name="symb" value="none" />
 1923: </form>
 1924: ENDSERVERFORM
 1925: }
 1926: 
 1927: sub constspaceform {
 1928:     return(<<ENDCONSTSPACEFORM);
 1929: <form name="constspace" action="/adm/logout" method="post" target="_top">
 1930: <input type="hidden" name="filename" value="" />
 1931: </form>
 1932: <form name="cstrdelete" action="/adm/cfile" method="post" target="_top">
 1933: <input type="hidden" name="action" value="delete" /> 
 1934: <input type="hidden" name="filename" value="" />
 1935: </form>
 1936: <form name="cstrprint" action="/adm/printout" target="_parent" method="post">
 1937: <input type="hidden" name="postdata" value="" />
 1938: <input type="hidden" name="curseed" value="" />
 1939: <input type="hidden" name="problemtype" value="" />
 1940: </form>
 1941: 
 1942: ENDCONSTSPACEFORM
 1943: }
 1944: 
 1945: sub get_nav_status {
 1946:     my $navstatus="swmenu.w_loncapanav_flag=";
 1947:     if ($env{'environment.remotenavmap'} eq 'on') {
 1948:         $navstatus.="1";
 1949:     } else {
 1950:         $navstatus.="-1";
 1951:     }
 1952:     return $navstatus;
 1953: }
 1954: 
 1955: sub hidden_button_check {
 1956:     if ( $env{'request.course.id'} eq ''
 1957:          || $env{'request.role.adv'} ) {
 1958: 
 1959:         return;
 1960:     }
 1961:     my $buttonshide = &Apache::lonnet::EXT('resource.0.buttonshide');
 1962:     return $buttonshide; 
 1963: }
 1964: 
 1965: sub roles_selector {
 1966:     my ($cdom,$cnum) = @_;
 1967:     my $crstype = &Apache::loncommon::course_type();
 1968:     my $now = time;
 1969:     my (%courseroles,%seccount,%courseprivs);
 1970:     my $is_cc;
 1971:     my $role_selector;
 1972:     my $ccrole;
 1973:     if ($crstype eq 'Community') {
 1974:         $ccrole = 'co';
 1975:     } else {
 1976:         $ccrole = 'cc';
 1977:     }
 1978:     my $priv;
 1979:     my $destinationurl = $ENV{'REQUEST_URI'};
 1980:     my $reqprivs = &required_privs();
 1981:     if (ref($reqprivs) eq 'HASH') {
 1982:         my $destination = $destinationurl;
 1983:         $destination =~ s/(\?.*)$//;
 1984:         if (exists($reqprivs->{$destination})) {
 1985:             $priv = $reqprivs->{$destination};
 1986:         }
 1987:     }
 1988:     if ($env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum}) {
 1989:         my ($start,$end) = split(/\./,$env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum});
 1990:         
 1991:         if ((($start) && ($start<0)) || 
 1992:             (($end) && ($end<$now))  ||
 1993:             (($start) && ($now<$start))) {
 1994:             $is_cc = 0;
 1995:         } else {
 1996:             $is_cc = 1;
 1997:         }
 1998:     }
 1999:     if ($is_cc) {
 2000:         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,$priv);
 2001:     } else {
 2002:         my %gotnosection;
 2003:         foreach my $item (keys(%env)) {
 2004:             if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
 2005:                 my $role = $1;
 2006:                 my $sec = $2;
 2007:                 next if ($role eq 'gr');
 2008:                 my ($start,$end) = split(/\./,$env{$item});
 2009:                 next if (($start && $start > $now) || ($end && $end < $now));
 2010:                 if ($sec eq '') {
 2011:                     if (!$gotnosection{$role}) {
 2012:                         $seccount{$role} ++;
 2013:                         $gotnosection{$role} = 1;
 2014:                     }
 2015:                 }
 2016:                 if ($priv ne '') {
 2017:                     my $cnumsec = $cnum;
 2018:                     if ($sec ne '') {
 2019:                         $cnumsec .= "/$sec";
 2020:                     }
 2021:                     $courseprivs{"$role./$cdom/$cnumsec./"} =
 2022:                         $env{"user.priv.$role./$cdom/$cnumsec./"};
 2023:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/"} =
 2024:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/"};
 2025:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/$cnumsec"} =
 2026:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/$cnumsec"};
 2027:                 }
 2028:                 if (ref($courseroles{$role}) eq 'ARRAY') {
 2029:                     if ($sec ne '') {
 2030:                         if (!grep(/^\Q$sec\E$/,@{$courseroles{$role}})) {
 2031:                             push(@{$courseroles{$role}},$sec);
 2032:                             $seccount{$role} ++;
 2033:                         }
 2034:                     }
 2035:                 } else {
 2036:                     @{$courseroles{$role}} = ();
 2037:                     if ($sec ne '') {
 2038:                         $seccount{$role} ++;
 2039:                         push(@{$courseroles{$role}},$sec);
 2040:                     }
 2041:                 }
 2042:             }
 2043:         }
 2044:     }
 2045:     my $switchtext;
 2046:     if ($crstype eq 'Community') {
 2047:         $switchtext = &mt('Switch community role to...')
 2048:     } else {
 2049:         $switchtext = &mt('Switch course role to...')
 2050:     }
 2051:     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
 2052:     if (keys(%courseroles) > 1) {
 2053:         $role_selector = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,$priv);
 2054:         $role_selector .= '<form name="rolechooser" method="post" action="/adm/roles">
 2055:                           <select name="switchrole" onchange="javascript:adhocRole('."'switchrole'".')">';
 2056:         $role_selector .= '<option value="">'.$switchtext.'</option>';
 2057:         foreach my $role (@roles_order) {
 2058:             if (defined($courseroles{$role})) {
 2059:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role,$crstype).'</option>'; 
 2060:             }
 2061:         }
 2062:         foreach my $role (sort(keys(%courseroles))) {
 2063:             if ($role =~ /^cr/) {
 2064:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role).'</option>'; 
 2065:             }
 2066:         }
 2067:         $role_selector .= '</select>'."\n".
 2068:                '<input type="hidden" name="destinationurl" value="'.
 2069:                &HTML::Entities::encode($destinationurl).'" />'."\n".
 2070:                '<input type="hidden" name="gotorole" value="1" />'."\n".
 2071:                '<input type="hidden" name="selectrole" value="" />'."\n".
 2072:                '<input type="hidden" name="switch" value="1" />'."\n".
 2073:                '</form>';
 2074:     }
 2075:     return $role_selector;
 2076: }
 2077: 
 2078: sub get_all_courseroles {
 2079:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs) = @_;
 2080:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 2081:             (ref($courseprivs) eq 'HASH')) {
 2082:         return;
 2083:     }
 2084:     my ($result,$cached) = 
 2085:         &Apache::lonnet::is_cached_new('getcourseroles',$cdom.'_'.$cnum);
 2086:     if (defined($cached)) {
 2087:         if (ref($result) eq 'HASH') {
 2088:             if ((ref($result->{'roles'}) eq 'HASH') && 
 2089:                 (ref($result->{'seccount'}) eq 'HASH') && 
 2090:                 (ref($result->{'privs'}) eq 'HASH')) {
 2091:                 %{$courseroles} = %{$result->{'roles'}};
 2092:                 %{$seccount} = %{$result->{'seccount'}};
 2093:                 %{$courseprivs} = %{$result->{'privs'}};
 2094:                 return;
 2095:             }
 2096:         }
 2097:     }
 2098:     my %gotnosection;
 2099:     my %adv_roles =
 2100:          &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
 2101:     foreach my $role (keys(%adv_roles)) {
 2102:         my ($urole,$usec) = split(/:/,$role);
 2103:         if (!$gotnosection{$urole}) {
 2104:             $seccount->{$urole} ++;
 2105:             $gotnosection{$urole} = 1;
 2106:         }
 2107:         if (ref($courseroles->{$urole}) eq 'ARRAY') {
 2108:             if ($usec ne '') {
 2109:                 if (!grep(/^Q$usec\E$/,@{$courseroles->{$urole}})) {
 2110:                     push(@{$courseroles->{$urole}},$usec);
 2111:                     $seccount->{$urole} ++;
 2112:                 }
 2113:             }
 2114:         } else {
 2115:             @{$courseroles->{$urole}} = ();
 2116:             if ($usec ne '') {
 2117:                 $seccount->{$urole} ++;
 2118:                 push(@{$courseroles->{$urole}},$usec);
 2119:             }
 2120:         }
 2121:         my $area = '/'.$cdom.'/'.$cnum;
 2122:         if ($usec ne '') {
 2123:             $area .= '/'.$usec;
 2124:         }
 2125:         if ($role =~ /^cr\//) {
 2126:             &Apache::lonnet::custom_roleprivs($courseprivs,$urole,$cdom,$cnum,$urole.'.'.$area,$area);
 2127:         } else {
 2128:             &Apache::lonnet::standard_roleprivs($courseprivs,$urole,$cdom,$urole.'.'.$area,$cnum,$area);
 2129:         }
 2130:     }
 2131:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum,['st']);
 2132:     @{$courseroles->{'st'}} = ();
 2133:     &Apache::lonnet::standard_roleprivs($courseprivs,'st',$cdom,"st./$cdom/$cnum",$cnum,"/$cdom/$cnum");
 2134:     if (keys(%sections_count) > 0) {
 2135:         push(@{$courseroles->{'st'}},keys(%sections_count));
 2136:         $seccount->{'st'} = scalar(keys(%sections_count));
 2137:     }
 2138:     my $rolehash = {
 2139:                      'roles'    => $courseroles,
 2140:                      'seccount' => $seccount,
 2141:                      'privs'    => $courseprivs,
 2142:                    };
 2143:     &Apache::lonnet::do_cache_new('getcourseroles',$cdom.'_'.$cnum,$rolehash);
 2144:     return;
 2145: }
 2146: 
 2147: sub jump_to_role {
 2148:     my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$priv) = @_;
 2149:     my %lt = &Apache::lonlocal::texthash(
 2150:                 this => 'This role has section(s) associated with it.',
 2151:                 ente => 'Enter a specific section.',
 2152:                 orlb => 'Enter a specific section, or leave blank for no section.',
 2153:                 avai => 'Available sections are:',
 2154:                 youe => 'You entered an invalid section choice:',
 2155:                 plst => 'Please try again.',
 2156:                 role => 'The role you selected is not permitted to view the current page.',
 2157:                 swit => 'Switch role, but display Main Menu page instead?',
 2158:     );
 2159:     my $js;
 2160:     if (ref($courseroles) eq 'HASH') {
 2161:         $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
 2162:               '    var numsec = new Array();'."\n".
 2163:               '    var rolesections = new Array();'."\n".
 2164:               '    var rolenames = new Array();'."\n".
 2165:               '    var roleseclist = new Array();'."\n";
 2166:         my @items = keys(%{$courseroles});
 2167:         for (my $i=0; $i<@items; $i++) {
 2168:             $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
 2169:             my ($secs,$secstr);
 2170:             if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
 2171:                 my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
 2172:                 $secs = join('","',@sections);
 2173:                 $secstr = join(', ',@sections);
 2174:             }
 2175:             $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
 2176:                    '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
 2177:                    '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
 2178:         }
 2179:     }
 2180:     my $checkroles = 0;
 2181:     if ($priv && ref($courseprivs) eq 'HASH') {
 2182:         my (%disallowed,%allowed,@disallow);
 2183:         foreach my $role (sort(keys(%{$courseprivs}))) {
 2184:             my $trole;
 2185:             if ($role =~ m{^(.+?)\Q./$cdom/$cnum\E}) {
 2186:                 $trole = $1;
 2187:             }
 2188:             if (($trole ne '') && ($trole ne 'cm')) {
 2189:                 if ($courseprivs->{$role} =~ /\Q:$priv\E($|:|\&\w+)/) {
 2190:                     $allowed{$trole} = 1;
 2191:                 } else {
 2192:                     $disallowed{$trole} = 1;
 2193:                 }
 2194:             }
 2195:         }
 2196:         foreach my $trole (keys(%disallowed)) {
 2197:             unless ($allowed{$trole}) {
 2198:                 push(@disallow,$trole);
 2199:             }
 2200:         }
 2201:         if (@disallow > 0) {
 2202:             $checkroles = 1;
 2203:             $js .= "    var disallow = new Array('".join("','",@disallow)."');\n".
 2204:                    "    var rolecheck = 1;\n";
 2205:         }
 2206:     }
 2207:     if (!$checkroles) {
 2208:         $js .=  "    var disallow = new Array();\n".
 2209:                 "    rolecheck = 0;\n";
 2210:     }
 2211:     return <<"END";
 2212: <script type="text/javascript">
 2213: //<![CDATA[
 2214: function adhocRole(roleitem) {
 2215:     $js
 2216:     var newrole =  document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value;
 2217:     if (newrole == '') {
 2218:         return;
 2219:     } 
 2220:     var fullrole = newrole+'./$cdom/$cnum';
 2221:     var selidx = '';
 2222:     for (var i=0; i<rolenames.length; i++) {
 2223:         if (rolenames[i] == newrole) {
 2224:             selidx = i;
 2225:         }
 2226:     }
 2227:     if (rolecheck > 0) {
 2228:         for (var i=0; i<disallow.length; i++) {
 2229:             if (disallow[i] == newrole) {
 2230:                 if (confirm("$lt{'role'}\\n$lt{'swit'}")) {
 2231:                     document.rolechooser.destinationurl.value = '/adm/menu';
 2232:                 } else {
 2233:                     document.rolechooser.elements[roleitem].selectedIndex = 0;
 2234:                     return;
 2235:                 }
 2236:             }
 2237:         }
 2238:     }
 2239:     var secok = 1;
 2240:     var secchoice = '';
 2241:     if (selidx >= 0) {
 2242:         if (numsec[selidx] > 1) {
 2243:             secok = 0;
 2244:             var numrolesec = rolesections[selidx].length;
 2245:             var msgidx = numsec[selidx] - numrolesec;
 2246:             secchoice = prompt("$lt{'this'} "+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
 2247:             if (secchoice == '') {
 2248:                 if (msgidx > 0) {
 2249:                     secok = 1;
 2250:                 }
 2251:             } else {
 2252:                 for (var j=0; j<rolesections[selidx].length; j++) {
 2253:                     if (rolesections[selidx][j] == secchoice) {
 2254:                         secok = 1;
 2255:                     }
 2256:                 }
 2257:             }
 2258:         } else {
 2259:             if (rolesections[selidx].length == 1) {
 2260:                 secchoice = rolesections[selidx][0];
 2261:             }
 2262:         }
 2263:     }
 2264:     if (secok == 1) {
 2265:         if (secchoice != '') {
 2266:             fullrole += '/'+secchoice;
 2267:         }
 2268:     } else {
 2269:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 2270:         if (secchoice != null) {
 2271:             alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
 2272:         }
 2273:         return;
 2274:     }
 2275:     if (fullrole == "$env{'request.role'}") {
 2276:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 2277:         return;
 2278:     }
 2279:     itemid = retrieveIndex('gotorole');
 2280:     if (itemid != -1) {
 2281:         document.rolechooser.elements[itemid].name = fullrole;
 2282:     }
 2283:     document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = fullrole;
 2284:     document.rolechooser.selectrole.value = '1';
 2285:     document.rolechooser.submit();
 2286:     return;
 2287: }
 2288: 
 2289: function retrieveIndex(item) {
 2290:     for (var i=0;i<document.rolechooser.elements.length;i++) {
 2291:         if (document.rolechooser.elements[i].name == item) {
 2292:             return i;
 2293:         }
 2294:     }
 2295:     return -1;
 2296: }
 2297: // ]]>
 2298: </script>
 2299: END
 2300: }
 2301: 
 2302: sub required_privs {
 2303:     my $privs =  {
 2304:              '/adm/parmset'      => 'opa',
 2305:              '/adm/courseprefs'  => 'opa',
 2306:              '/adm/whatsnew'     => 'whn',
 2307:              '/adm/populate'     => 'cst',
 2308:              '/adm/trackstudent' => 'vsa',
 2309:              '/adm/statistics'   => 'vgr',
 2310:              '/adm/setblock'     => 'dcm',
 2311:              '/adm/coursedocs'   => 'mdc',
 2312:            };
 2313:     unless ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet') {
 2314:         $privs->{'/adm/classcalc'}   = 'vgr',
 2315:         $privs->{'/adm/assesscalc'}  = 'vgr',
 2316:         $privs->{'/adm/studentcalc'} = 'vgr';
 2317:     }
 2318:     return $privs;
 2319: }
 2320: 
 2321: sub countdown_timer {
 2322:     if (($env{'request.course.id'}) && ($env{'request.symb'} ne '') &&
 2323:         ($env{'request.filename'}=~/$LONCAPA::assess_re/) &&
 2324:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
 2325:         my $duedate = &Apache::lonnet::EXT("resource.0.duedate");
 2326:         my @interval=&Apache::lonnet::EXT("resource.0.interval");
 2327:         my $hastimeleft;
 2328:         if (@interval > 1) {
 2329:             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
 2330:             if ($first_access > 0) {
 2331:                 if ($first_access+$interval[0] > time) {
 2332:                     $hastimeleft = 1;
 2333:                 }
 2334:             }
 2335:         }
 2336:         if (($duedate && $duedate > time) ||
 2337:             (!$duedate && $hastimeleft)) {
 2338:             my ($collapse,$expand,$alttxt,$title,$currdisp);
 2339:             if (@interval > 1 && $hastimeleft) {
 2340:                 $currdisp = 'inline';
 2341:                 $collapse = '&#9658;&nbsp;';
 2342:             } else {
 2343:                 $currdisp = 'none';
 2344:                 $expand = '&#9668;&nbsp;';
 2345:             }
 2346:             unless ($env{'environment.icons'} eq 'iconsonly') {
 2347:                 $alttxt = &mt('Timer');
 2348:                 $title = $alttxt.'&nbsp;';
 2349:             }
 2350:             my $desc = &mt('Countdown to due date/time');
 2351:             return <<END;
 2352: 
 2353: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 2354: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
 2355: $collapse
 2356: </span></a>
 2357: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
 2358: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 2359: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
 2360: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
 2361: END
 2362:         }
 2363:     }
 2364:     return;
 2365: }
 2366: 
 2367: # ================================================================ Main Program
 2368: 
 2369: BEGIN {
 2370:     if (! defined($readdesk)) {
 2371:         {
 2372:             my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
 2373:             if ( CORE::open( my $config,"<$tabfile") ) {
 2374:                 while (my $configline=<$config>) {
 2375:                     $configline=(split(/\#/,$configline))[0];
 2376:                     $configline=~s/^\s+//;
 2377:                     chomp($configline);
 2378:                     if ($configline=~/^cat\:/) {
 2379:                         my @entries=split(/\:/,$configline);
 2380:                         $category_positions{$entries[2]}=$entries[1];
 2381:                         $category_names{$entries[2]}=$entries[3];
 2382:                     } elsif ($configline=~/^prim\:/) {
 2383:                         my @entries = (split(/\:/, $configline))[1..5];
 2384:                         push @primary_menu, \@entries;
 2385:                     } elsif ($configline=~/^primsub\:/) {
 2386:                         my ($parent,@entries) = (split(/\:/, $configline))[1..4];
 2387:                         push (@{$primary_submenu{$parent}},\@entries);
 2388:                     } elsif ($configline=~/^scnd\:/) {
 2389:                         my @entries = (split(/\:/, $configline))[1..5];
 2390:                         push @secondary_menu, \@entries; 
 2391:                     } elsif ($configline=~/^scndsub\:/) {
 2392:                         my ($parent,@entries) = (split(/\:/, $configline))[1..4];
 2393:                         push (@{$secondary_submenu{$parent}},\@entries);
 2394:                     } elsif ($configline) {
 2395:                         push(@desklines,$configline);
 2396:                     }
 2397:                 }
 2398:                 CORE::close($config);
 2399:             }
 2400:         }
 2401:         $readdesk='done';
 2402:     }
 2403: }
 2404: 
 2405: 1;
 2406: __END__
 2407: 

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