File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.352: download - view: text, annotated - select for diffs
Fri Oct 21 15:26:27 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Consistent wording.
- Remove phrase no longer needed.

    1: # The LearningOnline Network with CAPA
    2: # Routines to control the menu
    3: #
    4: # $Id: lonmenu.pm,v 1.352 2011/10/21 15:26:27 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 LONCAPA qw(:DEFAULT :match);
  157: use HTML::Entities();
  158: use Apache::lonwishlist();
  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:             ($escurl = $env{'request.filename'}) =~ 
  338:                 s{^/home/([^/]+)/public_html/(.*)$}{/priv/$1/$2};
  339: 
  340:             $escurl  = &escape($escurl);
  341:         }    
  342:         $menu =~ s/\[url\]/$escurl/g;
  343:         $menu =~ s/\[symb\]/$escsymb/g;
  344:     }
  345:     $menu =~ s/\[uname\]/$$author{user}/g;
  346:     $menu =~ s/\[udom\]/$$author{dom}/g;
  347: 
  348:     return "<ul id=\"LC_secondary_menu\">$menu</ul>";
  349: }
  350: 
  351: sub innerregister {
  352:     my ($forcereg,$bread_crumbs) = @_;
  353:     my $const_space = ($env{'request.state'} eq 'construct');
  354:     my $is_const_dir = 0;
  355: 
  356:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  357: 
  358:     $env{'request.registered'} = 1;
  359: 
  360:     undef(@inlineremote);
  361: 
  362:     my $resurl; 
  363:     if ( $env{'request.symb'} && $env{'request.course.id'} ) {
  364: 
  365:         (my $mapurl, my $rid, $resurl) = &Apache::lonnet::decode_symb(&Apache::lonnet::symbread());
  366:         my $coursetitle = $env{'course.'.$env{'request.course.id'}.'.description'};
  367: 
  368:         my $maptitle = &Apache::lonnet::gettitle($mapurl);
  369:         my $restitle = &Apache::lonnet::gettitle(&Apache::lonnet::symbread());
  370: 
  371: #SD
  372: #course_type only Course and Community?
  373: #
  374:         my @crumbs;
  375:         unless (($forcereg) && ($env{'request.noversionuri'} eq '/adm/navmaps')
  376:                 && ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'})) {
  377:             @crumbs = ({text  => Apache::loncommon::course_type() 
  378:                                 . ' Contents', 
  379:                         href  => "Javascript:gopost('/adm/navmaps','')"});
  380:         }
  381:         if ($mapurl ne $env{'course.'.$env{'request.course.id'}.'.url'}) { 
  382:             push(@crumbs, {text  => '...',
  383:                            no_mt => 1});
  384:         }
  385: 
  386:         push @crumbs, {text => $maptitle, no_mt => 1} if ($maptitle 
  387:                                                    && $maptitle ne 'default.sequence' 
  388:                                                    && $maptitle ne $coursetitle);
  389: 
  390:         push @crumbs, {text => $restitle, no_mt => 1} if $restitle; 
  391: 
  392:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  393:         &Apache::lonhtmlcommon::add_breadcrumb(@crumbs);
  394:     }elsif (! $const_space){
  395:         #a situation when we're looking at a resource outside of context of a 
  396:         #course or construction space (e.g. with cumulative rights)
  397:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  398:         &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  399:     }
  400: # =============================================================================
  401: # ============================ This is for URLs that actually can be registered
  402:     return '' unless ( ($env{'request.noversionuri'}!~m{^/(res/)*adm/}) 
  403:                        || $forcereg );
  404: 
  405: # -- This applies to homework problems for users with grading privileges
  406: 	my $crs='/'.$env{'request.course.id'};
  407: 	if ($env{'request.course.sec'}) {
  408: 	    $crs.='_'.$env{'request.course.sec'};
  409: 	}
  410: 	$crs=~s/\_/\//g;
  411: 
  412:         my $hwkadd='';
  413:         if ($env{'request.symb'} ne '' &&
  414: 	    $env{'request.filename'}=~/\.(problem|exam|quiz|assess|survey|form|task)$/) {
  415: 	    if (&Apache::lonnet::allowed('mgr',$crs)) {
  416: 		$hwkadd.=&switch('','',7,2,'pgrd.png','Content Grades','grades[_4]',
  417:                        "gocmd('/adm/grades','gradingmenu')",
  418:                        'Content Grades');
  419:             } elsif (&Apache::lonnet::allowed('vgr',$crs)) {
  420: 		$hwkadd.=&switch('','',7,2,'subm.png','Content Submissions','missions[_1]',
  421:                        "gocmd('/adm/grades','submission')",
  422: 		       'Content Submissions');
  423:             }
  424: 	}
  425: 	if ($env{'request.symb'} ne '' &&
  426: 	    &Apache::lonnet::allowed('opa',$crs)) {
  427: 	    $hwkadd.=&switch('','',7,3,'pparm.png','Content Settings','parms[_2]',
  428: 			     "gocmd('/adm/parmset','set')",
  429: 			     'Content Settings');
  430: 	}
  431: # -- End Homework
  432:         ###
  433:         ### Determine whether or not to display the 'cstr' button for this
  434:         ### resource
  435:         ###
  436:         my $editbutton = '';
  437:         my $noeditbutton = 1;
  438:         my ($cnum,$cdom);
  439:         if ($env{'request.course.id'}) {
  440:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  441:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  442:         }
  443:         if ($env{'user.author'}) {
  444:             if ($env{'request.role'}=~/^(aa|ca|au)/) {
  445: #
  446: # We have the role of an author
  447: #
  448:                 # Set defaults for authors
  449:                 my ($top,$bottom) = ('con-','struct');
  450:                 my $action = "go('/priv/".$env{'user.name'}."');";
  451:                 my $cadom  = $env{'request.role.domain'};
  452:                 my $caname = $env{'user.name'};
  453:                 my $desc = "Enter my construction space";
  454:                 # Set defaults for co-authors
  455:                 if ($env{'request.role'} =~ /^ca/) { 
  456:                     ($cadom,$caname)=($env{'request.role'}=~/($match_domain)\/($match_username)$/);
  457:                     ($top,$bottom) = ('co con-','struct');
  458:                     $action = "go('/priv/".$caname."');";
  459:                     $desc = "Enter construction space as co-author";
  460:                 } elsif ($env{'request.role'} =~ /^aa/) {
  461:                     ($cadom,$caname)=($env{'request.role'}=~/($match_domain)\/($match_username)$/);
  462:                     ($top,$bottom) = ('co con-','struct');
  463:                     $action = "go('/priv/".$caname."');";
  464:                     $desc = "Enter construction space as assistant co-author";
  465:                 }
  466:                 # Check that we are on the correct machine
  467:                 my $home = &Apache::lonnet::homeserver($caname,$cadom);
  468: 		my $allowed=0;
  469: 		my @ids=&Apache::lonnet::current_machine_ids();
  470: 		foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  471: 		if (!$allowed) {
  472: 		    $editbutton=&switch('','',6,1,$top,,$bottom,$action,$desc);
  473:                     $noeditbutton = 0;
  474:                 }
  475:             }
  476: #
  477: # We are an author for some stuff, but currently do not have the role of author.
  478: # Figure out if we have authoring privileges for the resource we are looking at.
  479: # This should maybe become a privilege check in lonnet
  480: #
  481:             ##
  482:             ## Determine if user can edit url.
  483:             ##
  484:             my $cfile='';
  485:             my $cfuname='';
  486:             my $cfudom='';
  487:             my $uploaded;
  488:             my $switchserver='';
  489:             my $home;
  490:             if ($env{'request.filename'}) {
  491:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
  492:                 if (defined($cnum) && defined($cdom)) {
  493:                     $uploaded = &is_course_upload($file,$cnum,$cdom);
  494:                 }
  495:                 if (!$uploaded) {
  496:                     $file=~s/^($match_domain)\/($match_username)/\/priv\/$2/;
  497:                     # Check that the user has permission to edit this resource
  498:                     ($cfuname,$cfudom)=&Apache::loncacc::constructaccess($file,$1);
  499:                     if (defined($cfudom)) {
  500: 		        $home=&Apache::lonnet::homeserver($cfuname,$cfudom);
  501: 		        my $allowed=0;
  502: 		        my @ids=&Apache::lonnet::current_machine_ids();
  503: 		        foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  504: 		        if ($allowed) {
  505:                             $cfile=$file;
  506:                         } else {
  507:                             $switchserver=$file;
  508:                         }
  509:                     }
  510:                 }
  511:             }
  512:             # Finally, turn the button on or off
  513:             if (($cfile || $switchserver) && !$const_space) {
  514:                 my $nocrsedit;
  515:                 # Suppress display where CC has switched to student role.
  516:                 if ($env{'request.course.id'}) {
  517:                     unless(&Apache::lonnet::allowed('mdc',
  518:                                                     $env{'request.course.id'})) {
  519:                         $nocrsedit = 1;
  520:                     }
  521:                 }
  522:                 if ($nocrsedit) {
  523:                     $editbutton=&clear(6,1);
  524:                 } else {
  525:                     my $bot = "go('$cfile')";
  526:                     if ($switchserver) {
  527:                         if ( $env{'request.symb'} && $env{'request.course.id'} ) {
  528:                             $cfile = '/adm/switchserver?otherserver='.$home.'&amp;role='.
  529:                                      &HTML::Entities::encode($env{'request.role'},'"<>&').'&amp;symb='.
  530:                                      &HTML::Entities::encode($env{'request.symb'},'"<>&');
  531:                             $bot = "need_switchserver('$cfile');";
  532:                         }
  533:                     }
  534:                     $editbutton=&switch
  535:                        ('','',6,1,'pcstr.png','Edit','resource[_2]',
  536:                         $bot,"Edit this resource");
  537:                     $noeditbutton = 0;
  538:                 }
  539:             } elsif ($editbutton eq '') {
  540:                 $editbutton=&clear(6,1);
  541:             }
  542:         }
  543:         if (($noeditbutton) && ($env{'request.filename'})) { 
  544:             if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  545:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
  546:                 if (defined($cnum) && defined($cdom)) {
  547:                     if (&is_course_upload($file,$cnum,$cdom)) {
  548:                         my $cfile = &edit_course_upload($file,$cnum,$cdom);
  549:                         if ($cfile) {
  550:                             $editbutton=&switch
  551:                                         ('','',6,1,'pcstr.png','Edit',
  552:                                          'resource[_2]',"go('".$cfile."');",
  553:                                          'Edit this resource');
  554:                         }
  555:                     }
  556:                 }
  557:             }
  558:         }
  559:         if ($env{'request.course.id'}) {
  560:             if ($resurl eq "public/$cdom/$cnum/syllabus") {
  561:                 if ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ /\w/) {
  562:                     if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  563:                         $editbutton=&switch('','',6,1,'pcstr.png','Edit',
  564:                                             'resource[_2]',
  565:                                             "go('/adm/courseprefs?phase=display&actions=courseinfo')",
  566:                                             'Edit this resource');
  567:                     }
  568:                 }
  569:             }
  570:         }
  571:         ###
  572:         ###
  573: # Prepare the rest of the buttons
  574:         my $menuitems;
  575:         if ($const_space) {
  576: #
  577: # We are in construction space
  578: #
  579: 	    my ($uname,$thisdisfn) =
  580: 		($env{'request.filename'}=~m|^/home/([^/]+)/public_html/(.*)|);
  581:             my $currdir = '/priv/'.$uname.'/'.$thisdisfn;
  582:             if ($currdir =~ m-/$-) {
  583:                 $is_const_dir = 1;
  584:             } else {
  585:                 $currdir =~ s|[^/]+$||;
  586: 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
  587: 		my $esc_currdir = &Apache::loncommon::escape_single($currdir);
  588: #
  589: # Probably should be in mydesk.tab
  590: #
  591:                 $menuitems=(<<ENDMENUITEMS);
  592: s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
  593: s&6&2&rtrv.png&Retrieve&version[_1]&gocstr('/adm/retrieve','/~$uname/$cleandisfn')&Retrieve old version
  594: s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/~$uname/$cleandisfn')&Publish this resource
  595: s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/~$uname/$cleandisfn')&Delete this resource
  596: s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/~$uname/$cleandisfn')&Prepare a printable document
  597: ENDMENUITEMS
  598:             }
  599:                 if (ref($bread_crumbs) eq 'ARRAY') {
  600:                     &Apache::lonhtmlcommon::clear_breadcrumbs();
  601:                     foreach my $crumb (@{$bread_crumbs}){
  602:                         &Apache::lonhtmlcommon::add_breadcrumb($crumb);
  603:                     }
  604:                 }
  605:         } elsif ( defined($env{'request.course.id'}) && 
  606: 		 $env{'request.symb'} ne '' ) {
  607: #
  608: # We are in a course and looking at a registred URL
  609: # Should probably be in mydesk.tab
  610: #
  611: 	    $menuitems=(<<ENDMENUITEMS);
  612: c&3&1
  613: s&2&1&back.png&&&gopost('/adm/flip','back:'+currentURL)&Previous content resource&&1
  614: s&2&3&forw.png&&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3
  615: c&6&3
  616: c&8&1
  617: c&8&2
  618: s&8&3&prt.png&Print&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  619: ENDMENUITEMS
  620:             if (&Apache::lonnet::allowed('bre', $env{'request.course.id'}) eq 'F' && $env{'request.uri'} =~ /^\/res/) {
  621:                 # wishlist is only available for users with access to resource-pool
  622:                 # and links can only be set for resources within the resource-pool
  623:                 $menuitems .= (<<ENDMENUITEMS);
  624: s&9&1&wishlist-link.png&Wishlist&wishlistlink[_2]&set_wishlistlink()&Set a link for this resource to wishlist&&1
  625: ENDMENUITEMS
  626:             }
  627: 
  628: my $currentURL = &Apache::loncommon::get_symb();
  629: my ($symb_old,$symb_old_enc) = &Apache::loncommon::clean_symb($currentURL);
  630: my $annotation = &Apache::loncommon::get_annotation($symb_old,$symb_old_enc);
  631: $menuitems.="s&9&3&";
  632: if(length($annotation) > 0){
  633: 	$menuitems.="anot2.png";
  634: }else{
  635: 	$menuitems.="anot.png";
  636: }
  637: $menuitems.="&Notes&&annotate()&";
  638: $menuitems.="Make notes and annotations about this resource&&1\n";
  639: 
  640:             unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio)(\?|$)/) {
  641: 		if ((!$env{'request.enc'}) && ($env{'request.noversionuri'} !~ m{^/adm/wrapper/ext/})) {
  642: 		    $menuitems.=(<<ENDREALRES);
  643: s&6&3&catalog.png&Info&info[_1]&catalog_info()&Show Metadata
  644: ENDREALRES
  645:                 }
  646: 	        $menuitems.=(<<ENDREALRES);
  647: s&8&1&eval.png&Evaluate&this[_1]&gopost('/adm/evaluate',currentURL,1)&Provide my evaluation of this resource
  648: 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
  649: ENDREALRES
  650: 	    }
  651:         }
  652: 	if ($env{'request.uri'} =~ /^\/res/) {
  653: 	    $menuitems .= (<<ENDMENUITEMS);
  654: s&8&3&prt.png&Print&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  655: ENDMENUITEMS
  656:             if (&Apache::lonnet::allowed('bre', $env{'request.course.id'}) eq 'F') {
  657:                 # wishlist is only available for users with access to resource-pool
  658:                 $menuitems .= (<<ENDMENUITEMS);
  659: s&9&1&wishlist-link.png&Wishlist&wishlistlink[_2]&set_wishlistlink()&Set a link for this resource to wishlist&&1
  660: ENDMENUITEMS
  661:             }
  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,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: # ================================================================== Raw Config
  753: 
  754: sub clear {
  755:     my ($row,$col)=@_;
  756:     $inlineremote[10*$row+$col]='';
  757:     return ''; 
  758: }
  759: 
  760: # ============================================ Switch a button or create a link
  761: # Switch acts on the javascript that is executed when a button is clicked.  
  762: # The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
  763: 
  764: sub switch {
  765:     my ($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat,$nobreak)=@_;
  766:     $act=~s/\$uname/$uname/g;
  767:     $act=~s/\$udom/$udom/g;
  768:     $top=&mt($top);
  769:     $bot=&mt($bot);
  770:     $desc=&mt($desc);
  771:     my $idx=10*$row+$col;
  772:     $category_members{$cat}.=':'.$idx;
  773: 
  774: # Inline Menu
  775:     if ($nobreak==2) { return ''; }
  776:     my $text=$top.' '.$bot;
  777:     $text=~s/\s*\-\s*//gs;
  778: 
  779:     my $pic=
  780: 	   '<img alt="'.$text.'" src="'.
  781: 	   &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
  782: 	   '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
  783:     if ($env{'browser.interface'} eq 'faketextual') {
  784: # Main Menu
  785: 	   if ($nobreak==3) {
  786: 	       $inlineremote[$idx]="\n".
  787: 		   '<td class="LC_menubuttons_text" align="right">'.$text.
  788: 		   '</td><td align="left">'.
  789: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td></tr>';
  790: 	   } elsif ($nobreak) {
  791: 	       $inlineremote[$idx]="\n<tr>".
  792: 		   '<td align="left">'.
  793: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td>
  794:                     <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>';
  795: 	   } else {
  796: 	       $inlineremote[$idx]="\n<tr>".
  797: 		   '<td align="left">'.
  798: 		   '<a href="javascript:'.$act.';">'.$pic.
  799: 		   '</a></td><td class="LC_menubuttons_text" colspan="3">'.
  800: 		   '<a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$desc.'</span></a></td></tr>';
  801: 	   }
  802:     } else {
  803: # Inline Menu
  804:       $inlineremote[$idx]=
  805:        '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.
  806:        '<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
  807:     }
  808:     return '';
  809: }
  810: 
  811: sub secondlevel {
  812:     my $output='';
  813:     my 
  814:     ($uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat)=@_;
  815:     if ($prt eq 'any') {
  816: 	   $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  817:     } elsif ($prt=~/^r(\w+)/) {
  818:         if ($rol eq $1) {
  819:            $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  820:         }
  821:     }
  822:     return $output;
  823: }
  824: 
  825: sub inlinemenu {
  826:     undef(@inlineremote);
  827:     undef(%category_members);
  828: # calling rawconfig with "1" will evaluate mydesk.tab, even if there is no active remote control
  829:     &rawconfig(1);
  830:     my $output='<table><tr>';
  831:     for (my $col=1; $col<=2; $col++) {
  832:         $output.='<td class="LC_mainmenu_col_fieldset">';
  833:         for (my $row=1; $row<=8; $row++) {
  834:             foreach my $cat (keys(%category_members)) {
  835:                if ($category_positions{$cat} ne "$col,$row") { next; }
  836:                #$output.='<table><tr><td colspan="4" class="LC_menubuttons_category">'.&mt($category_names{$cat}).'</td></tr>';
  837:                $output.='<div class="LC_Box LC_400Box">';
  838: 	       $output.='<h3 class="LC_hcell">'.&mt($category_names{$cat}).'</h3>';
  839:                $output.='<table>';
  840:                my %active=();
  841:                foreach my $menu_item (split(/\:/,$category_members{$cat})) {
  842:                   if ($inlineremote[$menu_item]) {
  843:                      $active{$menu_item}=1;
  844:                   }
  845:                }  
  846:                foreach my $item (sort(keys(%active))) {
  847:                   $output.=$inlineremote[$item];
  848:                }
  849:                $output.='</table>';
  850:                $output.='</div>';
  851:             }
  852:          }
  853:          $output.="</td>";
  854:     }
  855:     $output.="</tr></table>";
  856:     return $output;
  857: }
  858: 
  859: sub rawconfig {
  860: #
  861: # This evaluates mydesk.tab
  862: # Need to add more positions and more privileges to deal with all
  863: # menu items.
  864: #
  865:     my $textualoverride=shift;
  866:     my $output='';
  867:     return '' unless $textualoverride;
  868:     my $uname=$env{'user.name'};
  869:     my $udom=$env{'user.domain'};
  870:     my $adv=$env{'user.adv'};
  871:     my $show_course=&Apache::loncommon::show_course();
  872:     my $author=$env{'user.author'};
  873:     my $crs='';
  874:     my $crstype='';
  875:     if ($env{'request.course.id'}) {
  876:        $crs='/'.$env{'request.course.id'};
  877:        if ($env{'request.course.sec'}) {
  878: 	   $crs.='_'.$env{'request.course.sec'};
  879:        }
  880:        $crs=~s/\_/\//g;
  881:        $crstype = &Apache::loncommon::course_type();
  882:     }
  883:     my $pub=($env{'request.state'} eq 'published');
  884:     my $con=($env{'request.state'} eq 'construct');
  885:     my $rol=$env{'request.role'};
  886:     my $requested_domain = $env{'request.role.domain'};
  887:     foreach my $line (@desklines) {
  888:         my ($row,$col,$pro,$prt,$img,$top,$bot,$act,$desc,$cat)=split(/\:/,$line);
  889:         $prt=~s/\$uname/$uname/g;
  890:         $prt=~s/\$udom/$udom/g;
  891:         if ($prt =~ /\$crs/) {
  892:             next unless ($env{'request.course.id'});
  893:             next if ($crstype eq 'Community');
  894:             $prt=~s/\$crs/$crs/g;
  895:         } elsif ($prt =~ /\$cmty/) {
  896:             next unless ($env{'request.course.id'});
  897:             next if ($crstype ne 'Community');
  898:             $prt=~s/\$cmty/$crs/g;
  899:         }
  900:         $prt=~s/\$requested_domain/$requested_domain/g;
  901:         if ($category_names{$cat}!~/\w/) { $cat='oth'; }
  902:         if ($pro eq 'clear') {
  903: 	    $output.=&clear($row,$col);
  904:         } elsif ($pro eq 'any') {
  905:                $output.=&secondlevel(
  906: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  907: 	} elsif ($pro eq 'smp') {
  908:             unless ($adv) {
  909:                $output.=&secondlevel(
  910:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  911:             }
  912:         } elsif ($pro eq 'adv') {
  913:             if ($adv) {
  914:                $output.=&secondlevel(
  915: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  916:             }
  917: 	} elsif ($pro eq 'shc') {
  918:             if ($show_course) {
  919:                $output.=&secondlevel(
  920:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  921:             }
  922:         } elsif ($pro eq 'nsc') {
  923:             if (!$show_course) {
  924:                $output.=&secondlevel(
  925: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  926:             }
  927:         } elsif (($pro=~/^p(\w+)/) && ($prt)) {
  928:             my $priv = $1;
  929:             if ($priv =~ /^mdc(Course|Community)/) {
  930:                 if ($crstype eq $1) {
  931:                     $priv = 'mdc';
  932:                 } else {
  933:                     next;
  934:                 }
  935:             }
  936: 	    if (&Apache::lonnet::allowed($priv,$prt)) {
  937:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  938:             }
  939:         } elsif ($pro eq 'course')  {
  940:             if (($env{'request.course.fn'}) && ($crstype ne 'Community')) {
  941:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  942: 	    }
  943:         } elsif ($pro eq 'community')  {
  944:             if (($env{'request.course.fn'}) && ($crstype eq 'Community')) {
  945:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  946:             }
  947:         } elsif ($pro =~ /^courseenv_(.*)$/) {
  948:             my $key = $1;
  949:             if ($crstype ne 'Community') {
  950:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
  951:                 if ($key eq 'canuse_pdfforms') {
  952:                     if ($env{'request.course.id'} && $coursepref eq '') {
  953:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
  954:                         $coursepref = $domdefs{'canuse_pdfforms'};
  955:                     }
  956:                 }
  957:                 if ($coursepref) { 
  958:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  959:                 }
  960:             }
  961:         } elsif ($pro =~ /^communityenv_(.*)$/) {
  962:             my $key = $1;
  963:             if ($crstype eq 'Community') {
  964:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
  965:                 if ($key eq 'canuse_pdfforms') {
  966:                     if ($env{'request.course.id'} && $coursepref eq '') {
  967:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
  968:                         $coursepref = $domdefs{'canuse_pdfforms'};
  969:                     }
  970:                 }
  971:                 if ($coursepref) { 
  972:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  973:                 }
  974:             }
  975:         } elsif ($pro =~ /^course_(.*)$/) {
  976:             # Check for permissions inside of a course
  977:             if (($env{'request.course.id'}) && ($crstype ne 'Community') && 
  978:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
  979:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
  980:                  )) {
  981:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  982: 	    }
  983:         } elsif ($pro =~ /^community_(.*)$/) {
  984:             # Check for permissions inside of a community
  985:             if (($env{'request.course.id'}) && ($crstype eq 'Community') &&   
  986:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
  987:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
  988:                  )) {
  989:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  990:             }
  991:         } elsif ($pro eq 'author') {
  992:             if ($author) {
  993:                 if ((($prt eq 'rca') && ($env{'request.role'}=~/^ca/)) ||
  994:                     (($prt eq 'raa') && ($env{'request.role'}=~/^aa/)) || 
  995:                     (($prt eq 'rau') && ($env{'request.role'}=~/^au/))) {
  996:                     # Check that we are on the correct machine
  997:                     my $cadom=$requested_domain;
  998:                     my $caname=$env{'user.name'};
  999:                     if (($prt eq 'rca') || ($prt eq 'raa')) {
 1000: 		       ($cadom,$caname)=
 1001:                                ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 1002:                     }                       
 1003:                     $act =~ s/\$caname/$caname/g;
 1004:                     my $home = &Apache::lonnet::homeserver($caname,$cadom);
 1005: 		    my $allowed=0;
 1006: 		    my @ids=&Apache::lonnet::current_machine_ids();
 1007: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 1008: 		    if ($allowed) {
 1009:                         $output.=&switch($caname,$cadom,
 1010:                                         $row,$col,$img,$top,$bot,$act,$desc,$cat);
 1011:                     }
 1012:                 }
 1013:             }
 1014:         } elsif ($pro eq 'tools') {
 1015:             my @tools = ('aboutme','blog','portfolio');
 1016:             if (grep(/^\Q$prt\E$/,@tools)) {
 1017:                 if (!&Apache::lonnet::usertools_access($env{'user.name'},
 1018:                                                        $env{'user.domain'},
 1019:                                                        $prt,undef,'tools')) {
 1020:                     $output.=&clear($row,$col);
 1021:                     next;
 1022:                 }
 1023:             } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
 1024:                 if (($prt eq 'reqcrsnsc') && ($show_course))   {
 1025:                     next;
 1026:                 }
 1027:                 if (($prt eq 'reqcrsshc') && (!$show_course)) {
 1028:                     next;
 1029:                 }
 1030:                 my $showreqcrs = &check_for_rcrs();
 1031:                 if (!$showreqcrs) {
 1032:                     $output.=&clear($row,$col);
 1033:                     next;
 1034:                 }
 1035:             }
 1036:             $prt='any';
 1037:             $output.=&secondlevel(
 1038:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1039:         }
 1040:     }
 1041:     return $output;
 1042: }
 1043: 
 1044: sub check_for_rcrs {
 1045:     my $showreqcrs = 0;
 1046:     my @reqtypes = ('official','unofficial','community');
 1047:     foreach my $type (@reqtypes) {
 1048:         if (&Apache::lonnet::usertools_access($env{'user.name'},
 1049:                                               $env{'user.domain'},
 1050:                                               $type,undef,'requestcourses')) {
 1051:             $showreqcrs = 1;
 1052:             last;
 1053:         }
 1054:     }
 1055:     if (!$showreqcrs) {
 1056:         foreach my $type (@reqtypes) {
 1057:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 1058:                 $showreqcrs = 1;
 1059:                 last;
 1060:             }
 1061:         }
 1062:     }
 1063:     return $showreqcrs;
 1064: }
 1065: 
 1066: sub dc_popup_js {
 1067:     my %lt = &Apache::lonlocal::texthash(
 1068:                                           more => '(More ...)',
 1069:                                           less => '(Less ...)',
 1070:                                         );
 1071:     return <<"END";
 1072: 
 1073: function showCourseID() {
 1074:     document.getElementById('dccid').style.display='block';
 1075:     document.getElementById('dccid').style.textAlign='left';
 1076:     document.getElementById('dccid').style.textFace='normal';
 1077:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:hideCourseID();">$lt{'less'}</a>';
 1078:     return;
 1079: }
 1080: 
 1081: function hideCourseID() {
 1082:     document.getElementById('dccid').style.display='none';
 1083:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:showCourseID()">$lt{'more'}</a>';
 1084:     return;
 1085: }
 1086: 
 1087: END
 1088: 
 1089: }
 1090: 
 1091: sub utilityfunctions {
 1092:     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 1093:     if ($currenturl =~ m{^/adm/wrapper/ext/}
 1094:         && $env{'request.external.querystring'} ) {
 1095:             $currenturl .= ($currenturl=~/\?/)?'&':'?'.$env{'request.external.querystring'};
 1096:     }
 1097:     $currenturl=&Apache::lonenc::check_encrypt(&unescape($currenturl));
 1098:     
 1099:     my $currentsymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 1100: 
 1101:     my $dc_popup_cid;
 1102:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 1103:                         $env{'course.'.$env{'request.course.id'}.
 1104:                                  '.domain'}.'/'})) {
 1105:         $dc_popup_cid = &dc_popup_js();
 1106:     }
 1107: 
 1108:     my $start_page_annotate = 
 1109:         &Apache::loncommon::start_page('Annotator',undef,
 1110: 				       {'only_body' => 1,
 1111: 					'js_ready'  => 1,
 1112: 					'bgcolor'   => '#BBBBBB',
 1113: 					'add_entries' => {
 1114: 					    'onload' => 'javascript:document.goannotate.submit();'}});
 1115: 
 1116:     my $end_page_annotate = 
 1117:         &Apache::loncommon::end_page({'js_ready' => 1});
 1118: 
 1119:     my $confirm_switch = &mt("Editing requires switching to the resource's home server.").'\n'.
 1120:                          &mt('Switch server?');
 1121: 
 1122: 
 1123: return (<<ENDUTILITY)
 1124: 
 1125:     var currentURL="$currenturl";
 1126:     var reloadURL="$currenturl";
 1127:     var currentSymb="$currentsymb";
 1128: 
 1129: $dc_popup_cid
 1130: 
 1131: function go(url) {
 1132:    if (url!='' && url!= null) {
 1133:        currentURL = null;
 1134:        currentSymb= null;
 1135:        window.location.href=url;
 1136:    }
 1137: }
 1138: 
 1139: function need_switchserver(url) {
 1140:     if (url!='' && url!= null) {
 1141:         if (confirm("$confirm_switch")) {
 1142:             go(url); 
 1143:         }
 1144:     }
 1145:     return;
 1146: }
 1147: 
 1148: function gopost(url,postdata) {
 1149:    if (url!='') {
 1150:       this.document.server.action=url;
 1151:       this.document.server.postdata.value=postdata;
 1152:       this.document.server.command.value='';
 1153:       this.document.server.url.value='';
 1154:       this.document.server.symb.value='';
 1155:       this.document.server.submit();
 1156:    }
 1157: }
 1158: 
 1159: function gocmd(url,cmd) {
 1160:    if (url!='') {
 1161:       this.document.server.action=url;
 1162:       this.document.server.postdata.value='';
 1163:       this.document.server.command.value=cmd;
 1164:       this.document.server.url.value=currentURL;
 1165:       this.document.server.symb.value=currentSymb;
 1166:       this.document.server.submit();
 1167:    }
 1168: }
 1169: 
 1170: function gocstr(url,filename) {
 1171:     if (url == '/adm/cfile?action=delete') {
 1172:         this.document.cstrdelete.filename.value = filename
 1173:         this.document.cstrdelete.submit();
 1174:         return;
 1175:     }
 1176:     if (url == '/adm/printout') {
 1177:         this.document.cstrprint.postdata.value = filename
 1178:         this.document.cstrprint.curseed.value = 0;
 1179:         this.document.cstrprint.problemtype.value = 0;
 1180:         if (this.document.lonhomework) {
 1181:             if ((this.document.lonhomework.rndseed) && (this.document.lonhomework.rndseed.value != null) && (this.document.lonhomework.rndseed.value != '')) {
 1182:                 this.document.cstrprint.curseed.value = this.document.lonhomework.rndseed.value
 1183:             }
 1184:             if (this.document.lonhomework.problemtype) {
 1185: 		if (this.document.lonhomework.problemtype.value) {
 1186: 		    this.document.cstrprint.problemtype.value = 
 1187: 			this.document.lonhomework.problemtype.value;
 1188: 		} else if (this.document.lonhomework.problemtype.options) {
 1189: 		    for (var i=0; i<this.document.lonhomework.problemtype.options.length; i++) {
 1190: 			if (this.document.lonhomework.problemtype.options[i].selected) {
 1191: 			    if (this.document.lonhomework.problemtype.options[i].value != null && this.document.lonhomework.problemtype.options[i].value != '') { 
 1192: 				this.document.cstrprint.problemtype.value = this.document.lonhomework.problemtype.options[i].value
 1193: 				}
 1194: 			}
 1195: 		    }
 1196: 		}
 1197: 	    }
 1198: 	}
 1199:         this.document.cstrprint.submit();
 1200:         return;
 1201:     }
 1202:     if (url !='') {
 1203:         this.document.constspace.filename.value = filename;
 1204:         this.document.constspace.action = url;
 1205:         this.document.constspace.submit();
 1206:     }
 1207: }
 1208: 
 1209: function golist(url) {
 1210:    if (url!='' && url!= null) {
 1211:        currentURL = null;
 1212:        currentSymb= null;
 1213:        top.location.href=url;
 1214:    }
 1215: }
 1216: 
 1217: 
 1218: 
 1219: function catalog_info() {
 1220:    loncatinfo=window.open(window.location.pathname+'.meta',"LONcatInfo",'height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1221: }
 1222: 
 1223: function chat_win() {
 1224:    lonchat=window.open('/res/adm/pages/chatroom.html',"LONchat",'height=320,width=480,resizable=yes,location=no,menubar=no,toolbar=no');
 1225: }
 1226: 
 1227: function group_chat(group) {
 1228:    var url = '/adm/groupchat?group='+group;
 1229:    var winName = 'LONchat_'+group;
 1230:    grpchat=window.open(url,winName,'height=320,width=280,resizable=yes,location=no,menubar=no,toolbar=no');
 1231: }
 1232: 
 1233: function annotate() {
 1234:    w_Annotator_flag=1;
 1235:    annotator=window.open('','Annotator','width=365,height=265,scrollbars=0');
 1236:    annotator.document.write(
 1237:    '$start_page_annotate'
 1238:   +"<form name='goannotate' target='Annotator' method='post' "
 1239:   +"action='/adm/annotations'>"
 1240:   +"<input type='hidden' name='symbnew' value='"+currentSymb+"' />"
 1241:   +"<\\/form>"
 1242:   +'$end_page_annotate');
 1243:    annotator.document.close();
 1244: }
 1245: 
 1246: function set_wishlistlink(title, path) {
 1247:     if (!title) {
 1248:         title = document.title;
 1249:         title = title.replace(/^LON-CAPA /,'');
 1250:     }
 1251:     if (!path) {
 1252:         path = location.pathname;
 1253:     }
 1254:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
 1255:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
 1256: }
 1257: 
 1258: function open_Wishlist_Import(rat) {
 1259:    var newWin;
 1260:    if (rat) {
 1261:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import&rat='+rat,
 1262:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1263:    }
 1264:    else {
 1265:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import',
 1266:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1267:    }
 1268:    newWin.focus();
 1269: }
 1270: 
 1271: ENDUTILITY
 1272: }
 1273: 
 1274: sub serverform {
 1275:     return(<<ENDSERVERFORM);
 1276: <form name="server" action="/adm/logout" method="post" target="_top">
 1277: <input type="hidden" name="postdata" value="none" />
 1278: <input type="hidden" name="command" value="none" />
 1279: <input type="hidden" name="url" value="none" />
 1280: <input type="hidden" name="symb" value="none" />
 1281: </form>
 1282: ENDSERVERFORM
 1283: }
 1284: 
 1285: sub constspaceform {
 1286:     return(<<ENDCONSTSPACEFORM);
 1287: <form name="constspace" action="/adm/logout" method="post" target="_top">
 1288: <input type="hidden" name="filename" value="" />
 1289: </form>
 1290: <form name="cstrdelete" action="/adm/cfile" method="post" target="_top">
 1291: <input type="hidden" name="action" value="delete" /> 
 1292: <input type="hidden" name="filename" value="" />
 1293: </form>
 1294: <form name="cstrprint" action="/adm/printout" target="_parent" method="post">
 1295: <input type="hidden" name="postdata" value="" />
 1296: <input type="hidden" name="curseed" value="" />
 1297: <input type="hidden" name="problemtype" value="" />
 1298: </form>
 1299: 
 1300: ENDCONSTSPACEFORM
 1301: }
 1302: 
 1303: sub hidden_button_check {
 1304:     if ( $env{'request.course.id'} eq ''
 1305:          || $env{'request.role.adv'} ) {
 1306: 
 1307:         return;
 1308:     }
 1309:     my $buttonshide = &Apache::lonnet::EXT('resource.0.buttonshide');
 1310:     return $buttonshide; 
 1311: }
 1312: 
 1313: sub roles_selector {
 1314:     my ($cdom,$cnum) = @_;
 1315:     my $crstype = &Apache::loncommon::course_type();
 1316:     my $now = time;
 1317:     my (%courseroles,%seccount,%courseprivs);
 1318:     my $is_cc;
 1319:     my $role_selector;
 1320:     my $ccrole;
 1321:     if ($crstype eq 'Community') {
 1322:         $ccrole = 'co';
 1323:     } else {
 1324:         $ccrole = 'cc';
 1325:     }
 1326:     my $priv;
 1327:     my $destinationurl = $ENV{'REQUEST_URI'};
 1328:     my $reqprivs = &required_privs();
 1329:     if (ref($reqprivs) eq 'HASH') {
 1330:         my $destination = $destinationurl;
 1331:         $destination =~ s/(\?.*)$//;
 1332:         if (exists($reqprivs->{$destination})) {
 1333:             $priv = $reqprivs->{$destination};
 1334:         }
 1335:     }
 1336:     if ($env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum}) {
 1337:         my ($start,$end) = split(/\./,$env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum});
 1338:         
 1339:         if ((($start) && ($start<0)) || 
 1340:             (($end) && ($end<$now))  ||
 1341:             (($start) && ($now<$start))) {
 1342:             $is_cc = 0;
 1343:         } else {
 1344:             $is_cc = 1;
 1345:         }
 1346:     }
 1347:     if ($is_cc) {
 1348:         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,$priv);
 1349:     } else {
 1350:         my %gotnosection;
 1351:         foreach my $item (keys(%env)) {
 1352:             if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
 1353:                 my $role = $1;
 1354:                 my $sec = $2;
 1355:                 next if ($role eq 'gr');
 1356:                 my ($start,$end) = split(/\./,$env{$item});
 1357:                 next if (($start && $start > $now) || ($end && $end < $now));
 1358:                 if ($sec eq '') {
 1359:                     if (!$gotnosection{$role}) {
 1360:                         $seccount{$role} ++;
 1361:                         $gotnosection{$role} = 1;
 1362:                     }
 1363:                 }
 1364:                 if ($priv ne '') {
 1365:                     my $cnumsec = $cnum;
 1366:                     if ($sec ne '') {
 1367:                         $cnumsec .= "/$sec";
 1368:                     }
 1369:                     $courseprivs{"$role./$cdom/$cnumsec./"} =
 1370:                         $env{"user.priv.$role./$cdom/$cnumsec./"};
 1371:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/"} =
 1372:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/"};
 1373:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/$cnumsec"} =
 1374:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/$cnumsec"};
 1375:                 }
 1376:                 if (ref($courseroles{$role}) eq 'ARRAY') {
 1377:                     if ($sec ne '') {
 1378:                         if (!grep(/^\Q$sec\E$/,@{$courseroles{$role}})) {
 1379:                             push(@{$courseroles{$role}},$sec);
 1380:                             $seccount{$role} ++;
 1381:                         }
 1382:                     }
 1383:                 } else {
 1384:                     @{$courseroles{$role}} = ();
 1385:                     if ($sec ne '') {
 1386:                         $seccount{$role} ++;
 1387:                         push(@{$courseroles{$role}},$sec);
 1388:                     }
 1389:                 }
 1390:             }
 1391:         }
 1392:     }
 1393:     my $switchtext;
 1394:     if ($crstype eq 'Community') {
 1395:         $switchtext = &mt('Switch community role to...')
 1396:     } else {
 1397:         $switchtext = &mt('Switch course role to...')
 1398:     }
 1399:     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
 1400:     if (keys(%courseroles) > 1) {
 1401:         $role_selector = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,$priv);
 1402:         $role_selector .= '<form name="rolechooser" method="post" action="/adm/roles">
 1403:                           <select name="switchrole" onchange="javascript:adhocRole('."'switchrole'".')">';
 1404:         $role_selector .= '<option value="">'.$switchtext.'</option>';
 1405:         foreach my $role (@roles_order) {
 1406:             if (defined($courseroles{$role})) {
 1407:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role,$crstype).'</option>'; 
 1408:             }
 1409:         }
 1410:         foreach my $role (sort(keys(%courseroles))) {
 1411:             if ($role =~ /^cr/) {
 1412:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role).'</option>'; 
 1413:             }
 1414:         }
 1415:         $role_selector .= '</select>'."\n".
 1416:                '<input type="hidden" name="destinationurl" value="'.
 1417:                &HTML::Entities::encode($destinationurl).'" />'."\n".
 1418:                '<input type="hidden" name="gotorole" value="1" />'."\n".
 1419:                '<input type="hidden" name="selectrole" value="" />'."\n".
 1420:                '<input type="hidden" name="switch" value="1" />'."\n".
 1421:                '</form>';
 1422:     }
 1423:     return $role_selector;
 1424: }
 1425: 
 1426: sub get_all_courseroles {
 1427:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs) = @_;
 1428:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 1429:             (ref($courseprivs) eq 'HASH')) {
 1430:         return;
 1431:     }
 1432:     my ($result,$cached) = 
 1433:         &Apache::lonnet::is_cached_new('getcourseroles',$cdom.'_'.$cnum);
 1434:     if (defined($cached)) {
 1435:         if (ref($result) eq 'HASH') {
 1436:             if ((ref($result->{'roles'}) eq 'HASH') && 
 1437:                 (ref($result->{'seccount'}) eq 'HASH') && 
 1438:                 (ref($result->{'privs'}) eq 'HASH')) {
 1439:                 %{$courseroles} = %{$result->{'roles'}};
 1440:                 %{$seccount} = %{$result->{'seccount'}};
 1441:                 %{$courseprivs} = %{$result->{'privs'}};
 1442:                 return;
 1443:             }
 1444:         }
 1445:     }
 1446:     my %gotnosection;
 1447:     my %adv_roles =
 1448:          &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
 1449:     foreach my $role (keys(%adv_roles)) {
 1450:         my ($urole,$usec) = split(/:/,$role);
 1451:         if (!$gotnosection{$urole}) {
 1452:             $seccount->{$urole} ++;
 1453:             $gotnosection{$urole} = 1;
 1454:         }
 1455:         if (ref($courseroles->{$urole}) eq 'ARRAY') {
 1456:             if ($usec ne '') {
 1457:                 if (!grep(/^Q$usec\E$/,@{$courseroles->{$urole}})) {
 1458:                     push(@{$courseroles->{$urole}},$usec);
 1459:                     $seccount->{$urole} ++;
 1460:                 }
 1461:             }
 1462:         } else {
 1463:             @{$courseroles->{$urole}} = ();
 1464:             if ($usec ne '') {
 1465:                 $seccount->{$urole} ++;
 1466:                 push(@{$courseroles->{$urole}},$usec);
 1467:             }
 1468:         }
 1469:         my $area = '/'.$cdom.'/'.$cnum;
 1470:         if ($usec ne '') {
 1471:             $area .= '/'.$usec;
 1472:         }
 1473:         if ($role =~ /^cr\//) {
 1474:             &Apache::lonnet::custom_roleprivs($courseprivs,$urole,$cdom,$cnum,$urole.'.'.$area,$area);
 1475:         } else {
 1476:             &Apache::lonnet::standard_roleprivs($courseprivs,$urole,$cdom,$urole.'.'.$area,$cnum,$area);
 1477:         }
 1478:     }
 1479:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum,['st']);
 1480:     @{$courseroles->{'st'}} = ();
 1481:     &Apache::lonnet::standard_roleprivs($courseprivs,'st',$cdom,"st./$cdom/$cnum",$cnum,"/$cdom/$cnum");
 1482:     if (keys(%sections_count) > 0) {
 1483:         push(@{$courseroles->{'st'}},keys(%sections_count));
 1484:         $seccount->{'st'} = scalar(keys(%sections_count));
 1485:     }
 1486:     my $rolehash = {
 1487:                      'roles'    => $courseroles,
 1488:                      'seccount' => $seccount,
 1489:                      'privs'    => $courseprivs,
 1490:                    };
 1491:     &Apache::lonnet::do_cache_new('getcourseroles',$cdom.'_'.$cnum,$rolehash);
 1492:     return;
 1493: }
 1494: 
 1495: sub jump_to_role {
 1496:     my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$priv) = @_;
 1497:     my %lt = &Apache::lonlocal::texthash(
 1498:                 this => 'This role has section(s) associated with it.',
 1499:                 ente => 'Enter a specific section.',
 1500:                 orlb => 'Enter a specific section, or leave blank for no section.',
 1501:                 avai => 'Available sections are:',
 1502:                 youe => 'You entered an invalid section choice:',
 1503:                 plst => 'Please try again.',
 1504:                 role => 'The role you selected is not permitted to view the current page.',
 1505:                 swit => 'Switch role, but display Main Menu page instead?',
 1506:     );
 1507:     my $js;
 1508:     if (ref($courseroles) eq 'HASH') {
 1509:         $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
 1510:               '    var numsec = new Array();'."\n".
 1511:               '    var rolesections = new Array();'."\n".
 1512:               '    var rolenames = new Array();'."\n".
 1513:               '    var roleseclist = new Array();'."\n";
 1514:         my @items = keys(%{$courseroles});
 1515:         for (my $i=0; $i<@items; $i++) {
 1516:             $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
 1517:             my ($secs,$secstr);
 1518:             if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
 1519:                 my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
 1520:                 $secs = join('","',@sections);
 1521:                 $secstr = join(', ',@sections);
 1522:             }
 1523:             $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
 1524:                    '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
 1525:                    '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
 1526:         }
 1527:     }
 1528:     my $checkroles = 0;
 1529:     if ($priv && ref($courseprivs) eq 'HASH') {
 1530:         my (%disallowed,%allowed,@disallow);
 1531:         foreach my $role (sort(keys(%{$courseprivs}))) {
 1532:             my $trole;
 1533:             if ($role =~ m{^(.+?)\Q./$cdom/$cnum\E}) {
 1534:                 $trole = $1;
 1535:             }
 1536:             if (($trole ne '') && ($trole ne 'cm')) {
 1537:                 if ($courseprivs->{$role} =~ /\Q:$priv\E($|:|\&\w+)/) {
 1538:                     $allowed{$trole} = 1;
 1539:                 } else {
 1540:                     $disallowed{$trole} = 1;
 1541:                 }
 1542:             }
 1543:         }
 1544:         foreach my $trole (keys(%disallowed)) {
 1545:             unless ($allowed{$trole}) {
 1546:                 push(@disallow,$trole);
 1547:             }
 1548:         }
 1549:         if (@disallow > 0) {
 1550:             $checkroles = 1;
 1551:             $js .= "    var disallow = new Array('".join("','",@disallow)."');\n".
 1552:                    "    var rolecheck = 1;\n";
 1553:         }
 1554:     }
 1555:     if (!$checkroles) {
 1556:         $js .=  "    var disallow = new Array();\n".
 1557:                 "    rolecheck = 0;\n";
 1558:     }
 1559:     return <<"END";
 1560: <script type="text/javascript">
 1561: //<![CDATA[
 1562: function adhocRole(roleitem) {
 1563:     $js
 1564:     var newrole =  document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value;
 1565:     if (newrole == '') {
 1566:         return;
 1567:     } 
 1568:     var fullrole = newrole+'./$cdom/$cnum';
 1569:     var selidx = '';
 1570:     for (var i=0; i<rolenames.length; i++) {
 1571:         if (rolenames[i] == newrole) {
 1572:             selidx = i;
 1573:         }
 1574:     }
 1575:     if (rolecheck > 0) {
 1576:         for (var i=0; i<disallow.length; i++) {
 1577:             if (disallow[i] == newrole) {
 1578:                 if (confirm("$lt{'role'}\\n$lt{'swit'}")) {
 1579:                     document.rolechooser.destinationurl.value = '/adm/menu';
 1580:                 } else {
 1581:                     document.rolechooser.elements[roleitem].selectedIndex = 0;
 1582:                     return;
 1583:                 }
 1584:             }
 1585:         }
 1586:     }
 1587:     var secok = 1;
 1588:     var secchoice = '';
 1589:     if (selidx >= 0) {
 1590:         if (numsec[selidx] > 1) {
 1591:             secok = 0;
 1592:             var numrolesec = rolesections[selidx].length;
 1593:             var msgidx = numsec[selidx] - numrolesec;
 1594:             secchoice = prompt("$lt{'this'} "+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
 1595:             if (secchoice == '') {
 1596:                 if (msgidx > 0) {
 1597:                     secok = 1;
 1598:                 }
 1599:             } else {
 1600:                 for (var j=0; j<rolesections[selidx].length; j++) {
 1601:                     if (rolesections[selidx][j] == secchoice) {
 1602:                         secok = 1;
 1603:                     }
 1604:                 }
 1605:             }
 1606:         } else {
 1607:             if (rolesections[selidx].length == 1) {
 1608:                 secchoice = rolesections[selidx][0];
 1609:             }
 1610:         }
 1611:     }
 1612:     if (secok == 1) {
 1613:         if (secchoice != '') {
 1614:             fullrole += '/'+secchoice;
 1615:         }
 1616:     } else {
 1617:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 1618:         if (secchoice != null) {
 1619:             alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
 1620:         }
 1621:         return;
 1622:     }
 1623:     if (fullrole == "$env{'request.role'}") {
 1624:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 1625:         return;
 1626:     }
 1627:     itemid = retrieveIndex('gotorole');
 1628:     if (itemid != -1) {
 1629:         document.rolechooser.elements[itemid].name = fullrole;
 1630:     }
 1631:     document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = fullrole;
 1632:     document.rolechooser.selectrole.value = '1';
 1633:     document.rolechooser.submit();
 1634:     return;
 1635: }
 1636: 
 1637: function retrieveIndex(item) {
 1638:     for (var i=0;i<document.rolechooser.elements.length;i++) {
 1639:         if (document.rolechooser.elements[i].name == item) {
 1640:             return i;
 1641:         }
 1642:     }
 1643:     return -1;
 1644: }
 1645: // ]]>
 1646: </script>
 1647: END
 1648: }
 1649: 
 1650: sub required_privs {
 1651:     my $privs =  {
 1652:              '/adm/parmset'      => 'opa',
 1653:              '/adm/courseprefs'  => 'opa',
 1654:              '/adm/whatsnew'     => 'whn',
 1655:              '/adm/populate'     => 'cst',
 1656:              '/adm/trackstudent' => 'vsa',
 1657:              '/adm/statistics'   => 'vgr',
 1658:            };
 1659:     unless ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet') {
 1660:         $privs->{'/adm/classcalc'}   => 'vgr',
 1661:         $privs->{'/adm/assesscalc'}  => 'vgr',
 1662:         $privs->{'/adm/studentcalc'} => 'vgr';
 1663:     }
 1664:     return $privs;
 1665: }
 1666: 
 1667: # ================================================================ Main Program
 1668: 
 1669: BEGIN {
 1670:     if (! defined($readdesk)) {
 1671:         {
 1672:             my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
 1673:             if ( CORE::open( my $config,"<$tabfile") ) {
 1674:                 while (my $configline=<$config>) {
 1675:                     $configline=(split(/\#/,$configline))[0];
 1676:                     $configline=~s/^\s+//;
 1677:                     chomp($configline);
 1678:                     if ($configline=~/^cat\:/) {
 1679:                         my @entries=split(/\:/,$configline);
 1680:                         $category_positions{$entries[2]}=$entries[1];
 1681:                         $category_names{$entries[2]}=$entries[3];
 1682:                     } elsif ($configline=~/^prim\:/) {
 1683:                         my @entries = (split(/\:/, $configline))[1..5];
 1684:                         push @primary_menu, \@entries;
 1685:                     } elsif ($configline=~/^scnd\:/) {
 1686:                         my @entries = (split(/\:/, $configline))[1..5];
 1687:                         push @secondary_menu, \@entries; 
 1688:                     } elsif ($configline) {
 1689:                         push(@desklines,$configline);
 1690:                     }
 1691:                 }
 1692:                 CORE::close($config);
 1693:             }
 1694:         }
 1695:         $readdesk='done';
 1696:     }
 1697: }
 1698: 
 1699: 1;
 1700: __END__
 1701: 

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