File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.19: download - view: text, annotated - select for diffs
Mon Aug 6 03:38:08 2012 UTC (11 years, 10 months ago) by raeburn
Branches: version_2_11_X
- For 2.11.
  - Remote Control retained.
  - Text used in $menuitems string for each button/icon determined based on
    context -- use &get_inline_text() for inline menu; &get_rc_text() for
    Remote Control so LCD display in Remote functions correctly.

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

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