File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.6: download - view: text, annotated - select for diffs
Thu May 24 23:36:17 2012 UTC (12 years ago) by raeburn
Branches: version_2_11_X
- For 2.11
  - Backport 1.375.
  - Use &create_submenu() to generate XHTML for pure css dropdown list
    for secondary menu items.

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

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