File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.2: download - view: text, annotated - select for diffs
Mon May 14 13:58:04 2012 UTC (12 years, 2 months ago) by raeburn
Branches: version_2_11_X
- For 2.11
  - Reverse changes in 1.332, 1.335, 1.337, 1.346.
  - Wishlist not included in 2.11.0; Bookmarks retained.

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

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