File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.361: download - view: text, annotated - select for diffs
Thu Nov 10 21:15:55 2011 UTC (12 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Declare use of Apache::lonmsg().

    1: # The LearningOnline Network with CAPA
    2: # Routines to control the menu
    3: #
    4: # $Id: lonmenu.pm,v 1.361 2011/11/10 21:15:55 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: use Apache::lonwishlist();
  160: 
  161: use vars qw(@desklines %category_names %category_members %category_positions 
  162:             $readdesk @primary_menu @secondary_menu);
  163: 
  164: my @inlineremote;
  165: 
  166: sub prep_menuitem {
  167:     my ($menuitem) = @_;
  168:     return '' unless(ref($menuitem) eq 'ARRAY');
  169:     my $link;
  170:     if ($$menuitem[1]) { # graphical Link
  171:         $link = "<img class=\"LC_noBorder\""
  172:               . " src=\"" . &Apache::loncommon::lonhttpdurl($$menuitem[1]) . "\"" 
  173:               . " alt=\"" . &mt($$menuitem[2]) . "\" />";
  174:     } else {             # textual Link
  175:         $link = &mt($$menuitem[3]);
  176:     }
  177:     return '<li><a' 
  178:            # highlighting for new messages
  179:            . ( $$menuitem[4] eq 'newmsg' ? ' class="LC_new_message"' : '') 
  180:            . qq| href="$$menuitem[0]" target="_top">$link</a></li>|;
  181: }
  182: 
  183: # primary_menu() evaluates @primary_menu and returns XHTML for the menu
  184: # that contains following links:
  185: # About, Message, Roles, Help, Logout
  186: # @primary_menu is filled within the BEGIN block of this module with 
  187: # entries from mydesk.tab
  188: sub primary_menu {
  189:     my $menu;
  190:     # each element of @primary contains following array:
  191:     # (link url, icon path, alt text, link text, condition)
  192:     my $public;
  193:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
  194:         || (($env{'user.name'} eq '') && ($env{'user.domain'} eq ''))) {
  195:         $public = 1;
  196:     }
  197:     foreach my $menuitem (@primary_menu) {
  198:         # evaluate conditions 
  199:         next if    ref($menuitem)       ne 'ARRAY';    #
  200:         next if    $$menuitem[4]        eq 'nonewmsg'  # show links depending on
  201:                 && &Apache::lonmsg::mynewmail();       # whether a new msg 
  202:         next if    $$menuitem[4]        eq 'newmsg'    # arrived or not
  203:                 && !&Apache::lonmsg::mynewmail();      # 
  204:         next if    $$menuitem[4]        !~ /public/    ##we've a public user,
  205:                 && $public;                            ##who should not see all
  206:                                                        ##links
  207:         next if    $$menuitem[4]        eq 'onlypublic'# hide links which are 
  208:                 && !$public;                           # only visible to public
  209:                                                        # users
  210:         next if    $$menuitem[4]        eq 'roles'     ##show links depending on
  211:                 && &Apache::loncommon::show_course();  ##term 'Courses' or 
  212:         next if    $$menuitem[4]        eq 'courses'   ##'Roles' wanted
  213:                 && !&Apache::loncommon::show_course(); ##
  214:         
  215:             
  216:         if ($$menuitem[3] eq 'Help') { # special treatment for helplink
  217:             if ($public) {
  218:                 my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  219:                 my $defdom = &Apache::lonnet::default_login_domain();
  220:                 my $to = &Apache::loncommon::build_recipient_list(undef,
  221:                                                                   'helpdeskmail',
  222:                                                                   $defdom,$origmail);
  223:                 if ($to ne '') {
  224:                     $menu .= &prep_menuitem($menuitem); 
  225:                 }
  226:             } else {
  227:                 $menu .= '<li>'.&Apache::loncommon::top_nav_help('Help').'</li>';
  228:             }
  229:         } else {
  230:             $menu .= prep_menuitem($menuitem);
  231:         }
  232:     }
  233:     $menu =~ s/\[domain\]/$env{'user.domain'}/g;
  234:     $menu =~ s/\[user\]/$env{'user.name'}/g;
  235: 
  236:     return "<ol class=\"LC_primary_menu LC_right\">$menu</ol>";
  237: }
  238: 
  239: #returns hashref {user=>'',dom=>''} containing:
  240: #   own name, domain if user is au
  241: #   name, domain of parent author if user is ca or aa
  242: #empty return if user is not an author or not on homeserver
  243: #
  244: #TODO this should probably be moved somewhere more central
  245: #since it can be used by different parts of the system
  246: sub getauthor{
  247:     return unless $env{'request.role'}=~/^(ca|aa|au)/; #nothing to do if user isn't some kind of author
  248: 
  249:                         #co- or assistent author?
  250:     my ($dom, $user) = ($env{'request.role'} =~ /^(?:ca|aa)\.\/($match_domain)\/($match_username)$/)
  251:                        ? ($1, $2) #domain, username of the parent author
  252:                        : @env{ ('request.role.domain', 'user.name') }; #own domain, username
  253: 
  254:     # current server == home server?
  255:     my $home =  &Apache::lonnet::homeserver($user,$dom);
  256:     foreach (&Apache::lonnet::current_machine_ids()){
  257:         return {user => $user, dom => $dom} if $_ eq $home;
  258:     }
  259: 
  260:     # if wrong server
  261:     return;
  262: }
  263: 
  264: sub secondary_menu {
  265:     my $menu;
  266: 
  267:     my $crstype = &Apache::loncommon::course_type();
  268:     my $crs_sec = $env{'request.course.id'} . ($env{'request.course.sec'} 
  269:                                                ? "/$env{'request.course.sec'}"
  270:                                                : '');
  271:     my $canedit       = &Apache::lonnet::allowed('mdc', $env{'request.course.id'});
  272:     my $canviewgrps   = &Apache::lonnet::allowed('vcg', $crs_sec); 
  273:     my $canmodifyuser = &Apache::lonnet::allowed('cst', $crs_sec); 
  274:     my $canviewwnew   = &Apache::lonnet::allowed('whn', $crs_sec); 
  275:     my $canmodpara    = &Apache::lonnet::allowed('opa', $crs_sec);
  276:     my $canvgr        = &Apache::lonnet::allowed('vgr', $crs_sec);
  277:     my $canmgr        = &Apache::lonnet::allowed('mgr', $crs_sec); 
  278:     my $author        = &getauthor();
  279: 
  280:     my %groups = &Apache::lonnet::get_active_groups(
  281:                      $env{'user.domain'}, $env{'user.name'},
  282:                      $env{'course.' . $env{'request.course.id'} . '.domain'},
  283:                      $env{'course.' . $env{'request.course.id'} . '.num'});
  284: 
  285:     foreach my $menuitem (@secondary_menu) {
  286:         # evaluate conditions 
  287:         next if    ref($menuitem)  ne 'ARRAY';
  288:         next if    $$menuitem[4]   ne 'always'
  289:                 && $$menuitem[4]   ne 'author'
  290:                 && !$env{'request.course.id'};
  291:         next if    $$menuitem[4]   =~ /^mdc/
  292:                 && !$canedit;
  293:         next if    $$menuitem[4]  eq 'nvgr'
  294:                 && $canvgr;
  295:         next if    $$menuitem[4]  eq 'vgr'
  296:                 && !$canvgr;
  297:         next if    $$menuitem[4]   eq 'cst'
  298:                 && !$canmodifyuser;
  299:         next if    $$menuitem[4]   eq 'ncst'
  300:                 && $canmodifyuser;
  301:         next if    $$menuitem[4]   eq 'mgr'
  302:                 && !$canmgr;
  303:         next if    $$menuitem[4]   eq 'nmgr'
  304:                 && $canmgr;
  305:         next if    $$menuitem[4]   eq 'whn'
  306:                 && !$canviewwnew;
  307:         next if    $$menuitem[4]   eq 'opa'
  308:                 && !$canmodpara;
  309:         next if    $$menuitem[4]   =~ /showgroups$/
  310:                 && !$canviewgrps
  311:                 && !%groups;
  312:         next if    $$menuitem[4]    eq 'author'
  313:                 && !$author;
  314: 
  315:         if ($$menuitem[3] eq 'Roles' && $env{'request.course.id'}) {
  316:             # special treatment for role selector
  317:             my $roles_selector = &roles_selector(
  318:                         $env{'course.' . $env{'request.course.id'} . '.domain'},
  319:                         $env{'course.' . $env{'request.course.id'} . '.num'}  );
  320: 
  321:             $menu .= $roles_selector ? "<li>$roles_selector</li>"
  322:                                      : '';
  323:         } else {
  324:             $menu .= &prep_menuitem(\@$menuitem);
  325:         }
  326:     }
  327:     if ($menu =~ /\[url\].*\[symb\]/) {
  328:         my $escurl  = &escape( &Apache::lonenc::check_encrypt(
  329:                              $env{'request.noversionuri'}));
  330: 
  331:         my $escsymb = &escape( &Apache::lonenc::check_encrypt(
  332:                              $env{'request.symb'})); 
  333: 
  334:         if (    $env{'request.state'} eq 'construct'
  335:             and (   $env{'request.noversionuri'} eq '' 
  336:                  || !defined($env{'request.noversionuri'}))) 
  337:         {
  338:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  339:             ($escurl = $env{'request.filename'}) =~ s{^\Q$londocroot\E}{};
  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'}=~/$LONCAPA::assess_re/) {
  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.domain'}.'/'.$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/".$cadom.'/'.$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/".$cadom.'/'.$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: 
  497:                     $file=~s{^(priv/$match_domain/$match_username)}{/$1};
  498:                     $file=~s{^($match_domain/$match_username)}{/priv/$1};
  499: 
  500:                     # Check that the user has permission to edit this resource
  501:                     my $setpriv = 1;
  502:                     ($cfuname,$cfudom)=&Apache::loncacc::constructaccess($file,$setpriv);
  503:                     if (defined($cfudom)) {
  504: 		        $home=&Apache::lonnet::homeserver($cfuname,$cfudom);
  505: 		        my $allowed=0;
  506: 		        my @ids=&Apache::lonnet::current_machine_ids();
  507: 		        foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  508: 		        if ($allowed) {
  509:                             $cfile=$file;
  510:                         } else {
  511:                             $switchserver=$file;
  512:                         }
  513:                     }
  514:                 }
  515:             }
  516:             # Finally, turn the button on or off
  517:             if (($cfile || $switchserver) && !$const_space) {
  518:                 my $nocrsedit;
  519:                 # Suppress display where CC has switched to student role.
  520:                 if ($env{'request.course.id'}) {
  521:                     unless(&Apache::lonnet::allowed('mdc',
  522:                                                     $env{'request.course.id'})) {
  523:                         $nocrsedit = 1;
  524:                     }
  525:                 }
  526:                 if ($nocrsedit) {
  527:                     $editbutton=&clear(6,1);
  528:                 } else {
  529:                     my $bot = "go('$cfile')";
  530:                     if ($switchserver) {
  531:                         if ( $env{'request.symb'} && $env{'request.course.id'} ) {
  532:                             $cfile = '/adm/switchserver?otherserver='.$home.'&amp;role='.
  533:                                      &HTML::Entities::encode($env{'request.role'},'"<>&').'&amp;symb='.
  534:                                      &HTML::Entities::encode($env{'request.symb'},'"<>&');
  535:                             $bot = "need_switchserver('$cfile');";
  536:                         }
  537:                     }
  538:                     $editbutton=&switch
  539:                        ('','',6,1,'pcstr.png','Edit','resource[_2]',
  540:                         $bot,"Edit this resource");
  541:                     $noeditbutton = 0;
  542:                 }
  543:             } elsif ($editbutton eq '') {
  544:                 $editbutton=&clear(6,1);
  545:             }
  546:         }
  547:         if (($noeditbutton) && ($env{'request.filename'})) { 
  548:             if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  549:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
  550:                 if (defined($cnum) && defined($cdom)) {
  551:                     if (&is_course_upload($file,$cnum,$cdom)) {
  552:                         my $cfile = &edit_course_upload($file,$cnum,$cdom);
  553:                         if ($cfile) {
  554:                             $editbutton=&switch
  555:                                         ('','',6,1,'pcstr.png','Edit',
  556:                                          'resource[_2]',"go('".$cfile."');",
  557:                                          'Edit this resource');
  558:                         }
  559:                     }
  560:                 }
  561:             }
  562:         }
  563:         if ($env{'request.course.id'}) {
  564:             if ($resurl eq "public/$cdom/$cnum/syllabus") {
  565:                 if ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ /\w/) {
  566:                     if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  567:                         $editbutton=&switch('','',6,1,'pcstr.png','Edit',
  568:                                             'resource[_2]',
  569:                                             "go('/adm/courseprefs?phase=display&actions=courseinfo')",
  570:                                             'Edit this resource');
  571:                     }
  572:                 }
  573:             }
  574:         }
  575:         ###
  576:         ###
  577: # Prepare the rest of the buttons
  578:         my $menuitems;
  579:         if ($const_space) {
  580: #
  581: # We are in construction space
  582: #
  583: 
  584:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  585: 	    my ($udom,$uname,$thisdisfn) =
  586: 		($env{'request.filename'}=~m{^\Q$londocroot/priv/\E([^/]+)/([^/]+)/(.*)$});
  587:             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
  588:             if ($currdir =~ m-/$-) {
  589:                 $is_const_dir = 1;
  590:             } else {
  591:                 $currdir =~ s|[^/]+$||;
  592: 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
  593: 		my $esc_currdir = &Apache::loncommon::escape_single($currdir);
  594: #
  595: # Probably should be in mydesk.tab
  596: #
  597:                 $menuitems=(<<ENDMENUITEMS);
  598: s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
  599: s&6&2&rtrv.png&Retrieve&version[_1]&gocstr('/adm/retrieve','/priv/$udom/$uname/$cleandisfn')&Retrieve old version
  600: s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/priv/$udom/$uname/$cleandisfn')&Publish this resource
  601: s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/priv/$udom/$uname/$cleandisfn')&Delete this resource
  602: s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/priv/$udom/$uname/$cleandisfn')&Prepare a printable document
  603: ENDMENUITEMS
  604:             }
  605:                 if (ref($bread_crumbs) eq 'ARRAY') {
  606:                     &Apache::lonhtmlcommon::clear_breadcrumbs();
  607:                     foreach my $crumb (@{$bread_crumbs}){
  608:                         &Apache::lonhtmlcommon::add_breadcrumb($crumb);
  609:                     }
  610:                 }
  611:         } elsif ( defined($env{'request.course.id'}) && 
  612: 		 $env{'request.symb'} ne '' ) {
  613: #
  614: # We are in a course and looking at a registred URL
  615: # Should probably be in mydesk.tab
  616: #
  617: 	    $menuitems=(<<ENDMENUITEMS);
  618: c&3&1
  619: s&2&1&back.png&&&gopost('/adm/flip','back:'+currentURL)&Previous content resource&&1
  620: s&2&3&forw.png&&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3
  621: c&6&3
  622: c&8&1
  623: c&8&2
  624: s&8&3&prt.png&Print&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  625: ENDMENUITEMS
  626:             if (&Apache::lonnet::allowed('bre', $env{'request.course.id'}) eq 'F' && $env{'request.uri'} =~ /^\/res/) {
  627:                 # wishlist is only available for users with access to resource-pool
  628:                 # and links can only be set for resources within the resource-pool
  629:                 $menuitems .= (<<ENDMENUITEMS);
  630: s&9&1&wishlist-link.png&Wishlist&wishlistlink[_2]&set_wishlistlink()&Set a link for this resource to wishlist&&1
  631: ENDMENUITEMS
  632:             }
  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:             if (&Apache::lonnet::allowed('bre', $env{'request.course.id'}) eq 'F') {
  663:                 # wishlist is only available for users with access to resource-pool
  664:                 $menuitems .= (<<ENDMENUITEMS);
  665: s&9&1&wishlist-link.png&Wishlist&wishlistlink[_2]&set_wishlistlink()&Set a link for this resource to wishlist&&1
  666: ENDMENUITEMS
  667:             }
  668: 	}
  669:         my $buttons='';
  670:         foreach (split(/\n/,$menuitems)) {
  671: 	    my ($command,@rest)=split(/\&/,$_);
  672:             my $idx=10*$rest[0]+$rest[1];
  673:             if (&hidden_button_check() eq 'yes') {
  674:                 if ($idx == 21 ||$idx == 23) {
  675:                     $buttons.=&switch('','',@rest);
  676:                 } else {
  677:                     $buttons.=&clear(@rest);
  678:                 }
  679:             } else {  
  680:                 if ($command eq 's') {
  681: 	            $buttons.=&switch('','',@rest);
  682:                 } else {
  683:                     $buttons.=&clear(@rest);
  684:                 }
  685:             }
  686:         }
  687: 
  688: 	    my $addremote=0;
  689: 	    foreach (@inlineremote) { if ($_ ne '') { $addremote=1; last;} }
  690:     if ($addremote) {
  691: 
  692:         &Apache::lonhtmlcommon::clear_breadcrumb_tools();
  693: 
  694:             &Apache::lonhtmlcommon::add_breadcrumb_tool(
  695:                 'navigation', @inlineremote[21,23]);
  696: 
  697:         if(hidden_button_check() ne 'yes') {
  698:             &Apache::lonhtmlcommon::add_breadcrumb_tool(
  699:                 'tools', @inlineremote[93,91,81,82,83]);
  700: 
  701:             #publish button in construction space
  702:             if ($env{'request.state'} eq 'construct'){
  703:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
  704:                      'advtools', $inlineremote[63]);
  705:             } else {
  706:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
  707:                      'tools', $inlineremote[63]);
  708:             }
  709:             
  710:             unless ($env{'request.noversionuri'}=~ m{^/adm/(navmaps|viewclasslist)(\?|$)}) {
  711:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
  712:                     'advtools', @inlineremote[61,71,72,73,92]);
  713:             }
  714:         }
  715:     }
  716: 
  717:     return   &Apache::lonhtmlcommon::scripttag('', 'start')
  718:            . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
  719:            . &Apache::lonhtmlcommon::scripttag('', 'end');
  720: }
  721: 
  722: sub is_course_upload {
  723:     my ($file,$cnum,$cdom) = @_;
  724:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
  725:     $uploadpath =~ s{^\/}{};
  726:     if (($file =~ m{^\Q$uploadpath\E/userfiles/docs/}) ||
  727:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/docs/})) {
  728:         return 1;
  729:     }
  730:     return;
  731: }
  732: 
  733: sub edit_course_upload {
  734:     my ($file,$cnum,$cdom) = @_;
  735:     my $cfile;
  736:     if ($file =~/\.(htm|html|css|js|txt)$/) {
  737:         my $ext = $1;
  738:         my $url = &Apache::lonnet::hreflocation('',$file);
  739:         my $home = &Apache::lonnet::homeserver($cnum,$cdom);
  740:         my @ids=&Apache::lonnet::current_machine_ids();
  741:         my $dest;
  742:         if ($home && grep(/^\Q$home\E$/,@ids)) {
  743:             $dest = $url.'?forceedit=1';
  744:         } else {
  745:             unless (&Apache::lonnet::get_locks()) {
  746:                 $dest = '/adm/switchserver?otherserver='.
  747:                         $home.'&role='.$env{'request.role'}.
  748:                         '&url='.$url.'&forceedit=1';
  749:             }
  750:         }
  751:         if ($dest) {
  752:             $cfile = &HTML::Entities::encode($dest,'"<>&');
  753:         }
  754:     }
  755:     return $cfile;
  756: }
  757: 
  758: # ================================================================== Raw Config
  759: 
  760: sub clear {
  761:     my ($row,$col)=@_;
  762:     $inlineremote[10*$row+$col]='';
  763:     return ''; 
  764: }
  765: 
  766: # ============================================ Switch a button or create a link
  767: # Switch acts on the javascript that is executed when a button is clicked.  
  768: # The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
  769: 
  770: sub switch {
  771:     my ($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat,$nobreak)=@_;
  772:     $act=~s/\$uname/$uname/g;
  773:     $act=~s/\$udom/$udom/g;
  774:     $top=&mt($top);
  775:     $bot=&mt($bot);
  776:     $desc=&mt($desc);
  777:     my $idx=10*$row+$col;
  778:     $category_members{$cat}.=':'.$idx;
  779: 
  780: # Inline Menu
  781:     if ($nobreak==2) { return ''; }
  782:     my $text=$top.' '.$bot;
  783:     $text=~s/\s*\-\s*//gs;
  784: 
  785:     my $pic=
  786: 	   '<img alt="'.$text.'" src="'.
  787: 	   &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
  788: 	   '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
  789:     if ($env{'browser.interface'} eq 'faketextual') {
  790: # Main Menu
  791: 	   if ($nobreak==3) {
  792: 	       $inlineremote[$idx]="\n".
  793: 		   '<td class="LC_menubuttons_text" align="right">'.$text.
  794: 		   '</td><td align="left">'.
  795: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td></tr>';
  796: 	   } elsif ($nobreak) {
  797: 	       $inlineremote[$idx]="\n<tr>".
  798: 		   '<td align="left">'.
  799: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td>
  800:                     <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>';
  801: 	   } else {
  802: 	       $inlineremote[$idx]="\n<tr>".
  803: 		   '<td align="left">'.
  804: 		   '<a href="javascript:'.$act.';">'.$pic.
  805: 		   '</a></td><td class="LC_menubuttons_text" colspan="3">'.
  806: 		   '<a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$desc.'</span></a></td></tr>';
  807: 	   }
  808:     } else {
  809: # Inline Menu
  810:       $inlineremote[$idx]=
  811:        '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.
  812:        '<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
  813:     }
  814:     return '';
  815: }
  816: 
  817: sub secondlevel {
  818:     my $output='';
  819:     my 
  820:     ($uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat)=@_;
  821:     if ($prt eq 'any') {
  822: 	   $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  823:     } elsif ($prt=~/^r(\w+)/) {
  824:         if ($rol eq $1) {
  825:            $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  826:         }
  827:     }
  828:     return $output;
  829: }
  830: 
  831: sub inlinemenu {
  832:     undef(@inlineremote);
  833:     undef(%category_members);
  834: # calling rawconfig with "1" will evaluate mydesk.tab, even if there is no active remote control
  835:     &rawconfig(1);
  836:     my $output='<table><tr>';
  837:     for (my $col=1; $col<=2; $col++) {
  838:         $output.='<td class="LC_mainmenu_col_fieldset">';
  839:         for (my $row=1; $row<=8; $row++) {
  840:             foreach my $cat (keys(%category_members)) {
  841:                if ($category_positions{$cat} ne "$col,$row") { next; }
  842:                #$output.='<table><tr><td colspan="4" class="LC_menubuttons_category">'.&mt($category_names{$cat}).'</td></tr>';
  843:                $output.='<div class="LC_Box LC_400Box">';
  844: 	       $output.='<h3 class="LC_hcell">'.&mt($category_names{$cat}).'</h3>';
  845:                $output.='<table>';
  846:                my %active=();
  847:                foreach my $menu_item (split(/\:/,$category_members{$cat})) {
  848:                   if ($inlineremote[$menu_item]) {
  849:                      $active{$menu_item}=1;
  850:                   }
  851:                }  
  852:                foreach my $item (sort(keys(%active))) {
  853:                   $output.=$inlineremote[$item];
  854:                }
  855:                $output.='</table>';
  856:                $output.='</div>';
  857:             }
  858:          }
  859:          $output.="</td>";
  860:     }
  861:     $output.="</tr></table>";
  862:     return $output;
  863: }
  864: 
  865: sub rawconfig {
  866: #
  867: # This evaluates mydesk.tab
  868: # Need to add more positions and more privileges to deal with all
  869: # menu items.
  870: #
  871:     my $textualoverride=shift;
  872:     my $output='';
  873:     return '' unless $textualoverride;
  874:     my $uname=$env{'user.name'};
  875:     my $udom=$env{'user.domain'};
  876:     my $adv=$env{'user.adv'};
  877:     my $show_course=&Apache::loncommon::show_course();
  878:     my $author=$env{'user.author'};
  879:     my $crs='';
  880:     my $crstype='';
  881:     if ($env{'request.course.id'}) {
  882:        $crs='/'.$env{'request.course.id'};
  883:        if ($env{'request.course.sec'}) {
  884: 	   $crs.='_'.$env{'request.course.sec'};
  885:        }
  886:        $crs=~s/\_/\//g;
  887:        $crstype = &Apache::loncommon::course_type();
  888:     }
  889:     my $pub=($env{'request.state'} eq 'published');
  890:     my $con=($env{'request.state'} eq 'construct');
  891:     my $rol=$env{'request.role'};
  892:     my $requested_domain = $env{'request.role.domain'};
  893:     foreach my $line (@desklines) {
  894:         my ($row,$col,$pro,$prt,$img,$top,$bot,$act,$desc,$cat)=split(/\:/,$line);
  895:         $prt=~s/\$uname/$uname/g;
  896:         $prt=~s/\$udom/$udom/g;
  897:         if ($prt =~ /\$crs/) {
  898:             next unless ($env{'request.course.id'});
  899:             next if ($crstype eq 'Community');
  900:             $prt=~s/\$crs/$crs/g;
  901:         } elsif ($prt =~ /\$cmty/) {
  902:             next unless ($env{'request.course.id'});
  903:             next if ($crstype ne 'Community');
  904:             $prt=~s/\$cmty/$crs/g;
  905:         }
  906:         $prt=~s/\$requested_domain/$requested_domain/g;
  907:         if ($category_names{$cat}!~/\w/) { $cat='oth'; }
  908:         if ($pro eq 'clear') {
  909: 	    $output.=&clear($row,$col);
  910:         } elsif ($pro eq 'any') {
  911:                $output.=&secondlevel(
  912: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  913: 	} elsif ($pro eq 'smp') {
  914:             unless ($adv) {
  915:                $output.=&secondlevel(
  916:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  917:             }
  918:         } elsif ($pro eq 'adv') {
  919:             if ($adv) {
  920:                $output.=&secondlevel(
  921: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  922:             }
  923: 	} elsif ($pro eq 'shc') {
  924:             if ($show_course) {
  925:                $output.=&secondlevel(
  926:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  927:             }
  928:         } elsif ($pro eq 'nsc') {
  929:             if (!$show_course) {
  930:                $output.=&secondlevel(
  931: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
  932:             }
  933:         } elsif (($pro=~/^p(\w+)/) && ($prt)) {
  934:             my $priv = $1;
  935:             if ($priv =~ /^mdc(Course|Community)/) {
  936:                 if ($crstype eq $1) {
  937:                     $priv = 'mdc';
  938:                 } else {
  939:                     next;
  940:                 }
  941:             }
  942: 	    if (&Apache::lonnet::allowed($priv,$prt)) {
  943:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  944:             }
  945:         } elsif ($pro eq 'course')  {
  946:             if (($env{'request.course.fn'}) && ($crstype ne 'Community')) {
  947:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  948: 	    }
  949:         } elsif ($pro eq 'community')  {
  950:             if (($env{'request.course.fn'}) && ($crstype eq 'Community')) {
  951:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  952:             }
  953:         } elsif ($pro =~ /^courseenv_(.*)$/) {
  954:             my $key = $1;
  955:             if ($crstype ne 'Community') {
  956:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
  957:                 if ($key eq 'canuse_pdfforms') {
  958:                     if ($env{'request.course.id'} && $coursepref eq '') {
  959:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
  960:                         $coursepref = $domdefs{'canuse_pdfforms'};
  961:                     }
  962:                 }
  963:                 if ($coursepref) { 
  964:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  965:                 }
  966:             }
  967:         } elsif ($pro =~ /^communityenv_(.*)$/) {
  968:             my $key = $1;
  969:             if ($crstype eq 'Community') {
  970:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
  971:                 if ($key eq 'canuse_pdfforms') {
  972:                     if ($env{'request.course.id'} && $coursepref eq '') {
  973:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
  974:                         $coursepref = $domdefs{'canuse_pdfforms'};
  975:                     }
  976:                 }
  977:                 if ($coursepref) { 
  978:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  979:                 }
  980:             }
  981:         } elsif ($pro =~ /^course_(.*)$/) {
  982:             # Check for permissions inside of a course
  983:             if (($env{'request.course.id'}) && ($crstype ne 'Community') && 
  984:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
  985:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
  986:                  )) {
  987:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  988: 	    }
  989:         } elsif ($pro =~ /^community_(.*)$/) {
  990:             # Check for permissions inside of a community
  991:             if (($env{'request.course.id'}) && ($crstype eq 'Community') &&   
  992:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
  993:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
  994:                  )) {
  995:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
  996:             }
  997:         } elsif ($pro eq 'author') {
  998:             if ($author) {
  999:                 if ((($prt eq 'rca') && ($env{'request.role'}=~/^ca/)) ||
 1000:                     (($prt eq 'raa') && ($env{'request.role'}=~/^aa/)) || 
 1001:                     (($prt eq 'rau') && ($env{'request.role'}=~/^au/))) {
 1002:                     # Check that we are on the correct machine
 1003:                     my $cadom=$requested_domain;
 1004:                     my $caname=$env{'user.name'};
 1005:                     if (($prt eq 'rca') || ($prt eq 'raa')) {
 1006: 		       ($cadom,$caname)=
 1007:                                ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 1008:                     }                       
 1009:                     $act =~ s/\$caname/$caname/g;
 1010:                     $act =~ s/\$cadom/$cadom/g;
 1011:                     my $home = &Apache::lonnet::homeserver($caname,$cadom);
 1012: 		    my $allowed=0;
 1013: 		    my @ids=&Apache::lonnet::current_machine_ids();
 1014: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 1015: 		    if ($allowed) {
 1016:                         $output.=&switch($caname,$cadom,
 1017:                                         $row,$col,$img,$top,$bot,$act,$desc,$cat);
 1018:                     }
 1019:                 }
 1020:             }
 1021:         } elsif ($pro eq 'tools') {
 1022:             my @tools = ('aboutme','blog','portfolio');
 1023:             if (grep(/^\Q$prt\E$/,@tools)) {
 1024:                 if (!&Apache::lonnet::usertools_access($env{'user.name'},
 1025:                                                        $env{'user.domain'},
 1026:                                                        $prt,undef,'tools')) {
 1027:                     $output.=&clear($row,$col);
 1028:                     next;
 1029:                 }
 1030:             } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
 1031:                 if (($prt eq 'reqcrsnsc') && ($show_course))   {
 1032:                     next;
 1033:                 }
 1034:                 if (($prt eq 'reqcrsshc') && (!$show_course)) {
 1035:                     next;
 1036:                 }
 1037:                 my $showreqcrs = &check_for_rcrs();
 1038:                 if (!$showreqcrs) {
 1039:                     $output.=&clear($row,$col);
 1040:                     next;
 1041:                 }
 1042:             }
 1043:             $prt='any';
 1044:             $output.=&secondlevel(
 1045:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1046:         }
 1047:     }
 1048:     return $output;
 1049: }
 1050: 
 1051: sub check_for_rcrs {
 1052:     my $showreqcrs = 0;
 1053:     my @reqtypes = ('official','unofficial','community');
 1054:     foreach my $type (@reqtypes) {
 1055:         if (&Apache::lonnet::usertools_access($env{'user.name'},
 1056:                                               $env{'user.domain'},
 1057:                                               $type,undef,'requestcourses')) {
 1058:             $showreqcrs = 1;
 1059:             last;
 1060:         }
 1061:     }
 1062:     if (!$showreqcrs) {
 1063:         foreach my $type (@reqtypes) {
 1064:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 1065:                 $showreqcrs = 1;
 1066:                 last;
 1067:             }
 1068:         }
 1069:     }
 1070:     return $showreqcrs;
 1071: }
 1072: 
 1073: sub dc_popup_js {
 1074:     my %lt = &Apache::lonlocal::texthash(
 1075:                                           more => '(More ...)',
 1076:                                           less => '(Less ...)',
 1077:                                         );
 1078:     return <<"END";
 1079: 
 1080: function showCourseID() {
 1081:     document.getElementById('dccid').style.display='block';
 1082:     document.getElementById('dccid').style.textAlign='left';
 1083:     document.getElementById('dccid').style.textFace='normal';
 1084:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:hideCourseID();">$lt{'less'}</a>';
 1085:     return;
 1086: }
 1087: 
 1088: function hideCourseID() {
 1089:     document.getElementById('dccid').style.display='none';
 1090:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:showCourseID()">$lt{'more'}</a>';
 1091:     return;
 1092: }
 1093: 
 1094: END
 1095: 
 1096: }
 1097: 
 1098: sub utilityfunctions {
 1099:     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 1100:     if ($currenturl =~ m{^/adm/wrapper/ext/}
 1101:         && $env{'request.external.querystring'} ) {
 1102:             $currenturl .= ($currenturl=~/\?/)?'&':'?'.$env{'request.external.querystring'};
 1103:     }
 1104:     $currenturl=&Apache::lonenc::check_encrypt(&unescape($currenturl));
 1105:     
 1106:     my $currentsymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 1107: 
 1108:     my $dc_popup_cid;
 1109:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 1110:                         $env{'course.'.$env{'request.course.id'}.
 1111:                                  '.domain'}.'/'})) {
 1112:         $dc_popup_cid = &dc_popup_js();
 1113:     }
 1114: 
 1115:     my $start_page_annotate = 
 1116:         &Apache::loncommon::start_page('Annotator',undef,
 1117: 				       {'only_body' => 1,
 1118: 					'js_ready'  => 1,
 1119: 					'bgcolor'   => '#BBBBBB',
 1120: 					'add_entries' => {
 1121: 					    'onload' => 'javascript:document.goannotate.submit();'}});
 1122: 
 1123:     my $end_page_annotate = 
 1124:         &Apache::loncommon::end_page({'js_ready' => 1});
 1125: 
 1126:     my $confirm_switch = &mt("Editing requires switching to the resource's home server.").'\n'.
 1127:                          &mt('Switch server?');
 1128: 
 1129: 
 1130: return (<<ENDUTILITY)
 1131: 
 1132:     var currentURL="$currenturl";
 1133:     var reloadURL="$currenturl";
 1134:     var currentSymb="$currentsymb";
 1135: 
 1136: $dc_popup_cid
 1137: 
 1138: function go(url) {
 1139:    if (url!='' && url!= null) {
 1140:        currentURL = null;
 1141:        currentSymb= null;
 1142:        window.location.href=url;
 1143:    }
 1144: }
 1145: 
 1146: function need_switchserver(url) {
 1147:     if (url!='' && url!= null) {
 1148:         if (confirm("$confirm_switch")) {
 1149:             go(url); 
 1150:         }
 1151:     }
 1152:     return;
 1153: }
 1154: 
 1155: function gopost(url,postdata) {
 1156:    if (url!='') {
 1157:       this.document.server.action=url;
 1158:       this.document.server.postdata.value=postdata;
 1159:       this.document.server.command.value='';
 1160:       this.document.server.url.value='';
 1161:       this.document.server.symb.value='';
 1162:       this.document.server.submit();
 1163:    }
 1164: }
 1165: 
 1166: function gocmd(url,cmd) {
 1167:    if (url!='') {
 1168:       this.document.server.action=url;
 1169:       this.document.server.postdata.value='';
 1170:       this.document.server.command.value=cmd;
 1171:       this.document.server.url.value=currentURL;
 1172:       this.document.server.symb.value=currentSymb;
 1173:       this.document.server.submit();
 1174:    }
 1175: }
 1176: 
 1177: function gocstr(url,filename) {
 1178:     if (url == '/adm/cfile?action=delete') {
 1179:         this.document.cstrdelete.filename.value = filename
 1180:         this.document.cstrdelete.submit();
 1181:         return;
 1182:     }
 1183:     if (url == '/adm/printout') {
 1184:         this.document.cstrprint.postdata.value = filename
 1185:         this.document.cstrprint.curseed.value = 0;
 1186:         this.document.cstrprint.problemtype.value = 0;
 1187:         if (this.document.lonhomework) {
 1188:             if ((this.document.lonhomework.rndseed) && (this.document.lonhomework.rndseed.value != null) && (this.document.lonhomework.rndseed.value != '')) {
 1189:                 this.document.cstrprint.curseed.value = this.document.lonhomework.rndseed.value
 1190:             }
 1191:             if (this.document.lonhomework.problemtype) {
 1192: 		if (this.document.lonhomework.problemtype.value) {
 1193: 		    this.document.cstrprint.problemtype.value = 
 1194: 			this.document.lonhomework.problemtype.value;
 1195: 		} else if (this.document.lonhomework.problemtype.options) {
 1196: 		    for (var i=0; i<this.document.lonhomework.problemtype.options.length; i++) {
 1197: 			if (this.document.lonhomework.problemtype.options[i].selected) {
 1198: 			    if (this.document.lonhomework.problemtype.options[i].value != null && this.document.lonhomework.problemtype.options[i].value != '') { 
 1199: 				this.document.cstrprint.problemtype.value = this.document.lonhomework.problemtype.options[i].value
 1200: 				}
 1201: 			}
 1202: 		    }
 1203: 		}
 1204: 	    }
 1205: 	}
 1206:         this.document.cstrprint.submit();
 1207:         return;
 1208:     }
 1209:     if (url !='') {
 1210:         this.document.constspace.filename.value = filename;
 1211:         this.document.constspace.action = url;
 1212:         this.document.constspace.submit();
 1213:     }
 1214: }
 1215: 
 1216: function golist(url) {
 1217:    if (url!='' && url!= null) {
 1218:        currentURL = null;
 1219:        currentSymb= null;
 1220:        top.location.href=url;
 1221:    }
 1222: }
 1223: 
 1224: 
 1225: 
 1226: function catalog_info() {
 1227:    loncatinfo=window.open(window.location.pathname+'.meta',"LONcatInfo",'height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1228: }
 1229: 
 1230: function chat_win() {
 1231:    lonchat=window.open('/res/adm/pages/chatroom.html',"LONchat",'height=320,width=480,resizable=yes,location=no,menubar=no,toolbar=no');
 1232: }
 1233: 
 1234: function group_chat(group) {
 1235:    var url = '/adm/groupchat?group='+group;
 1236:    var winName = 'LONchat_'+group;
 1237:    grpchat=window.open(url,winName,'height=320,width=280,resizable=yes,location=no,menubar=no,toolbar=no');
 1238: }
 1239: 
 1240: function annotate() {
 1241:    w_Annotator_flag=1;
 1242:    annotator=window.open('','Annotator','width=365,height=265,scrollbars=0');
 1243:    annotator.document.write(
 1244:    '$start_page_annotate'
 1245:   +"<form name='goannotate' target='Annotator' method='post' "
 1246:   +"action='/adm/annotations'>"
 1247:   +"<input type='hidden' name='symbnew' value='"+currentSymb+"' />"
 1248:   +"<\\/form>"
 1249:   +'$end_page_annotate');
 1250:    annotator.document.close();
 1251: }
 1252: 
 1253: function set_wishlistlink(title, path) {
 1254:     if (!title) {
 1255:         title = document.title;
 1256:         title = title.replace(/^LON-CAPA /,'');
 1257:     }
 1258:     if (!path) {
 1259:         path = location.pathname;
 1260:     }
 1261:     Win = window.open('/adm/wishlist?mode=newLink&setTitle='+title+'&setPath='+path,
 1262:                       'wishlistNewLink','width=560,height=350,scrollbars=0');
 1263: }
 1264: 
 1265: function open_Wishlist_Import(rat) {
 1266:    var newWin;
 1267:    if (rat) {
 1268:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import&rat='+rat,
 1269:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1270:    }
 1271:    else {
 1272:        newWin = window.open('/adm/wishlist?inhibitmenu=yes&mode=import',
 1273:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 1274:    }
 1275:    newWin.focus();
 1276: }
 1277: 
 1278: ENDUTILITY
 1279: }
 1280: 
 1281: sub serverform {
 1282:     return(<<ENDSERVERFORM);
 1283: <form name="server" action="/adm/logout" method="post" target="_top">
 1284: <input type="hidden" name="postdata" value="none" />
 1285: <input type="hidden" name="command" value="none" />
 1286: <input type="hidden" name="url" value="none" />
 1287: <input type="hidden" name="symb" value="none" />
 1288: </form>
 1289: ENDSERVERFORM
 1290: }
 1291: 
 1292: sub constspaceform {
 1293:     return(<<ENDCONSTSPACEFORM);
 1294: <form name="constspace" action="/adm/logout" method="post" target="_top">
 1295: <input type="hidden" name="filename" value="" />
 1296: </form>
 1297: <form name="cstrdelete" action="/adm/cfile" method="post" target="_top">
 1298: <input type="hidden" name="action" value="delete" /> 
 1299: <input type="hidden" name="filename" value="" />
 1300: </form>
 1301: <form name="cstrprint" action="/adm/printout" target="_parent" method="post">
 1302: <input type="hidden" name="postdata" value="" />
 1303: <input type="hidden" name="curseed" value="" />
 1304: <input type="hidden" name="problemtype" value="" />
 1305: </form>
 1306: 
 1307: ENDCONSTSPACEFORM
 1308: }
 1309: 
 1310: sub hidden_button_check {
 1311:     if ( $env{'request.course.id'} eq ''
 1312:          || $env{'request.role.adv'} ) {
 1313: 
 1314:         return;
 1315:     }
 1316:     my $buttonshide = &Apache::lonnet::EXT('resource.0.buttonshide');
 1317:     return $buttonshide; 
 1318: }
 1319: 
 1320: sub roles_selector {
 1321:     my ($cdom,$cnum) = @_;
 1322:     my $crstype = &Apache::loncommon::course_type();
 1323:     my $now = time;
 1324:     my (%courseroles,%seccount,%courseprivs);
 1325:     my $is_cc;
 1326:     my $role_selector;
 1327:     my $ccrole;
 1328:     if ($crstype eq 'Community') {
 1329:         $ccrole = 'co';
 1330:     } else {
 1331:         $ccrole = 'cc';
 1332:     }
 1333:     my $priv;
 1334:     my $destinationurl = $ENV{'REQUEST_URI'};
 1335:     my $reqprivs = &required_privs();
 1336:     if (ref($reqprivs) eq 'HASH') {
 1337:         my $destination = $destinationurl;
 1338:         $destination =~ s/(\?.*)$//;
 1339:         if (exists($reqprivs->{$destination})) {
 1340:             $priv = $reqprivs->{$destination};
 1341:         }
 1342:     }
 1343:     if ($env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum}) {
 1344:         my ($start,$end) = split(/\./,$env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum});
 1345:         
 1346:         if ((($start) && ($start<0)) || 
 1347:             (($end) && ($end<$now))  ||
 1348:             (($start) && ($now<$start))) {
 1349:             $is_cc = 0;
 1350:         } else {
 1351:             $is_cc = 1;
 1352:         }
 1353:     }
 1354:     if ($is_cc) {
 1355:         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,$priv);
 1356:     } else {
 1357:         my %gotnosection;
 1358:         foreach my $item (keys(%env)) {
 1359:             if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
 1360:                 my $role = $1;
 1361:                 my $sec = $2;
 1362:                 next if ($role eq 'gr');
 1363:                 my ($start,$end) = split(/\./,$env{$item});
 1364:                 next if (($start && $start > $now) || ($end && $end < $now));
 1365:                 if ($sec eq '') {
 1366:                     if (!$gotnosection{$role}) {
 1367:                         $seccount{$role} ++;
 1368:                         $gotnosection{$role} = 1;
 1369:                     }
 1370:                 }
 1371:                 if ($priv ne '') {
 1372:                     my $cnumsec = $cnum;
 1373:                     if ($sec ne '') {
 1374:                         $cnumsec .= "/$sec";
 1375:                     }
 1376:                     $courseprivs{"$role./$cdom/$cnumsec./"} =
 1377:                         $env{"user.priv.$role./$cdom/$cnumsec./"};
 1378:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/"} =
 1379:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/"};
 1380:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/$cnumsec"} =
 1381:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/$cnumsec"};
 1382:                 }
 1383:                 if (ref($courseroles{$role}) eq 'ARRAY') {
 1384:                     if ($sec ne '') {
 1385:                         if (!grep(/^\Q$sec\E$/,@{$courseroles{$role}})) {
 1386:                             push(@{$courseroles{$role}},$sec);
 1387:                             $seccount{$role} ++;
 1388:                         }
 1389:                     }
 1390:                 } else {
 1391:                     @{$courseroles{$role}} = ();
 1392:                     if ($sec ne '') {
 1393:                         $seccount{$role} ++;
 1394:                         push(@{$courseroles{$role}},$sec);
 1395:                     }
 1396:                 }
 1397:             }
 1398:         }
 1399:     }
 1400:     my $switchtext;
 1401:     if ($crstype eq 'Community') {
 1402:         $switchtext = &mt('Switch community role to...')
 1403:     } else {
 1404:         $switchtext = &mt('Switch course role to...')
 1405:     }
 1406:     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
 1407:     if (keys(%courseroles) > 1) {
 1408:         $role_selector = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,$priv);
 1409:         $role_selector .= '<form name="rolechooser" method="post" action="/adm/roles">
 1410:                           <select name="switchrole" onchange="javascript:adhocRole('."'switchrole'".')">';
 1411:         $role_selector .= '<option value="">'.$switchtext.'</option>';
 1412:         foreach my $role (@roles_order) {
 1413:             if (defined($courseroles{$role})) {
 1414:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role,$crstype).'</option>'; 
 1415:             }
 1416:         }
 1417:         foreach my $role (sort(keys(%courseroles))) {
 1418:             if ($role =~ /^cr/) {
 1419:                 $role_selector .= "\n".'<option value="'.$role.'">'.&Apache::lonnet::plaintext($role).'</option>'; 
 1420:             }
 1421:         }
 1422:         $role_selector .= '</select>'."\n".
 1423:                '<input type="hidden" name="destinationurl" value="'.
 1424:                &HTML::Entities::encode($destinationurl).'" />'."\n".
 1425:                '<input type="hidden" name="gotorole" value="1" />'."\n".
 1426:                '<input type="hidden" name="selectrole" value="" />'."\n".
 1427:                '<input type="hidden" name="switch" value="1" />'."\n".
 1428:                '</form>';
 1429:     }
 1430:     return $role_selector;
 1431: }
 1432: 
 1433: sub get_all_courseroles {
 1434:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs) = @_;
 1435:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 1436:             (ref($courseprivs) eq 'HASH')) {
 1437:         return;
 1438:     }
 1439:     my ($result,$cached) = 
 1440:         &Apache::lonnet::is_cached_new('getcourseroles',$cdom.'_'.$cnum);
 1441:     if (defined($cached)) {
 1442:         if (ref($result) eq 'HASH') {
 1443:             if ((ref($result->{'roles'}) eq 'HASH') && 
 1444:                 (ref($result->{'seccount'}) eq 'HASH') && 
 1445:                 (ref($result->{'privs'}) eq 'HASH')) {
 1446:                 %{$courseroles} = %{$result->{'roles'}};
 1447:                 %{$seccount} = %{$result->{'seccount'}};
 1448:                 %{$courseprivs} = %{$result->{'privs'}};
 1449:                 return;
 1450:             }
 1451:         }
 1452:     }
 1453:     my %gotnosection;
 1454:     my %adv_roles =
 1455:          &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
 1456:     foreach my $role (keys(%adv_roles)) {
 1457:         my ($urole,$usec) = split(/:/,$role);
 1458:         if (!$gotnosection{$urole}) {
 1459:             $seccount->{$urole} ++;
 1460:             $gotnosection{$urole} = 1;
 1461:         }
 1462:         if (ref($courseroles->{$urole}) eq 'ARRAY') {
 1463:             if ($usec ne '') {
 1464:                 if (!grep(/^Q$usec\E$/,@{$courseroles->{$urole}})) {
 1465:                     push(@{$courseroles->{$urole}},$usec);
 1466:                     $seccount->{$urole} ++;
 1467:                 }
 1468:             }
 1469:         } else {
 1470:             @{$courseroles->{$urole}} = ();
 1471:             if ($usec ne '') {
 1472:                 $seccount->{$urole} ++;
 1473:                 push(@{$courseroles->{$urole}},$usec);
 1474:             }
 1475:         }
 1476:         my $area = '/'.$cdom.'/'.$cnum;
 1477:         if ($usec ne '') {
 1478:             $area .= '/'.$usec;
 1479:         }
 1480:         if ($role =~ /^cr\//) {
 1481:             &Apache::lonnet::custom_roleprivs($courseprivs,$urole,$cdom,$cnum,$urole.'.'.$area,$area);
 1482:         } else {
 1483:             &Apache::lonnet::standard_roleprivs($courseprivs,$urole,$cdom,$urole.'.'.$area,$cnum,$area);
 1484:         }
 1485:     }
 1486:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum,['st']);
 1487:     @{$courseroles->{'st'}} = ();
 1488:     &Apache::lonnet::standard_roleprivs($courseprivs,'st',$cdom,"st./$cdom/$cnum",$cnum,"/$cdom/$cnum");
 1489:     if (keys(%sections_count) > 0) {
 1490:         push(@{$courseroles->{'st'}},keys(%sections_count));
 1491:         $seccount->{'st'} = scalar(keys(%sections_count));
 1492:     }
 1493:     my $rolehash = {
 1494:                      'roles'    => $courseroles,
 1495:                      'seccount' => $seccount,
 1496:                      'privs'    => $courseprivs,
 1497:                    };
 1498:     &Apache::lonnet::do_cache_new('getcourseroles',$cdom.'_'.$cnum,$rolehash);
 1499:     return;
 1500: }
 1501: 
 1502: sub jump_to_role {
 1503:     my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$priv) = @_;
 1504:     my %lt = &Apache::lonlocal::texthash(
 1505:                 this => 'This role has section(s) associated with it.',
 1506:                 ente => 'Enter a specific section.',
 1507:                 orlb => 'Enter a specific section, or leave blank for no section.',
 1508:                 avai => 'Available sections are:',
 1509:                 youe => 'You entered an invalid section choice:',
 1510:                 plst => 'Please try again.',
 1511:                 role => 'The role you selected is not permitted to view the current page.',
 1512:                 swit => 'Switch role, but display Main Menu page instead?',
 1513:     );
 1514:     my $js;
 1515:     if (ref($courseroles) eq 'HASH') {
 1516:         $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
 1517:               '    var numsec = new Array();'."\n".
 1518:               '    var rolesections = new Array();'."\n".
 1519:               '    var rolenames = new Array();'."\n".
 1520:               '    var roleseclist = new Array();'."\n";
 1521:         my @items = keys(%{$courseroles});
 1522:         for (my $i=0; $i<@items; $i++) {
 1523:             $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
 1524:             my ($secs,$secstr);
 1525:             if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
 1526:                 my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
 1527:                 $secs = join('","',@sections);
 1528:                 $secstr = join(', ',@sections);
 1529:             }
 1530:             $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
 1531:                    '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
 1532:                    '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
 1533:         }
 1534:     }
 1535:     my $checkroles = 0;
 1536:     if ($priv && ref($courseprivs) eq 'HASH') {
 1537:         my (%disallowed,%allowed,@disallow);
 1538:         foreach my $role (sort(keys(%{$courseprivs}))) {
 1539:             my $trole;
 1540:             if ($role =~ m{^(.+?)\Q./$cdom/$cnum\E}) {
 1541:                 $trole = $1;
 1542:             }
 1543:             if (($trole ne '') && ($trole ne 'cm')) {
 1544:                 if ($courseprivs->{$role} =~ /\Q:$priv\E($|:|\&\w+)/) {
 1545:                     $allowed{$trole} = 1;
 1546:                 } else {
 1547:                     $disallowed{$trole} = 1;
 1548:                 }
 1549:             }
 1550:         }
 1551:         foreach my $trole (keys(%disallowed)) {
 1552:             unless ($allowed{$trole}) {
 1553:                 push(@disallow,$trole);
 1554:             }
 1555:         }
 1556:         if (@disallow > 0) {
 1557:             $checkroles = 1;
 1558:             $js .= "    var disallow = new Array('".join("','",@disallow)."');\n".
 1559:                    "    var rolecheck = 1;\n";
 1560:         }
 1561:     }
 1562:     if (!$checkroles) {
 1563:         $js .=  "    var disallow = new Array();\n".
 1564:                 "    rolecheck = 0;\n";
 1565:     }
 1566:     return <<"END";
 1567: <script type="text/javascript">
 1568: //<![CDATA[
 1569: function adhocRole(roleitem) {
 1570:     $js
 1571:     var newrole =  document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value;
 1572:     if (newrole == '') {
 1573:         return;
 1574:     } 
 1575:     var fullrole = newrole+'./$cdom/$cnum';
 1576:     var selidx = '';
 1577:     for (var i=0; i<rolenames.length; i++) {
 1578:         if (rolenames[i] == newrole) {
 1579:             selidx = i;
 1580:         }
 1581:     }
 1582:     if (rolecheck > 0) {
 1583:         for (var i=0; i<disallow.length; i++) {
 1584:             if (disallow[i] == newrole) {
 1585:                 if (confirm("$lt{'role'}\\n$lt{'swit'}")) {
 1586:                     document.rolechooser.destinationurl.value = '/adm/menu';
 1587:                 } else {
 1588:                     document.rolechooser.elements[roleitem].selectedIndex = 0;
 1589:                     return;
 1590:                 }
 1591:             }
 1592:         }
 1593:     }
 1594:     var secok = 1;
 1595:     var secchoice = '';
 1596:     if (selidx >= 0) {
 1597:         if (numsec[selidx] > 1) {
 1598:             secok = 0;
 1599:             var numrolesec = rolesections[selidx].length;
 1600:             var msgidx = numsec[selidx] - numrolesec;
 1601:             secchoice = prompt("$lt{'this'} "+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
 1602:             if (secchoice == '') {
 1603:                 if (msgidx > 0) {
 1604:                     secok = 1;
 1605:                 }
 1606:             } else {
 1607:                 for (var j=0; j<rolesections[selidx].length; j++) {
 1608:                     if (rolesections[selidx][j] == secchoice) {
 1609:                         secok = 1;
 1610:                     }
 1611:                 }
 1612:             }
 1613:         } else {
 1614:             if (rolesections[selidx].length == 1) {
 1615:                 secchoice = rolesections[selidx][0];
 1616:             }
 1617:         }
 1618:     }
 1619:     if (secok == 1) {
 1620:         if (secchoice != '') {
 1621:             fullrole += '/'+secchoice;
 1622:         }
 1623:     } else {
 1624:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 1625:         if (secchoice != null) {
 1626:             alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
 1627:         }
 1628:         return;
 1629:     }
 1630:     if (fullrole == "$env{'request.role'}") {
 1631:         document.rolechooser.elements[roleitem].selectedIndex = 0;
 1632:         return;
 1633:     }
 1634:     itemid = retrieveIndex('gotorole');
 1635:     if (itemid != -1) {
 1636:         document.rolechooser.elements[itemid].name = fullrole;
 1637:     }
 1638:     document.rolechooser.elements[roleitem].options[document.rolechooser.elements[roleitem].selectedIndex].value = fullrole;
 1639:     document.rolechooser.selectrole.value = '1';
 1640:     document.rolechooser.submit();
 1641:     return;
 1642: }
 1643: 
 1644: function retrieveIndex(item) {
 1645:     for (var i=0;i<document.rolechooser.elements.length;i++) {
 1646:         if (document.rolechooser.elements[i].name == item) {
 1647:             return i;
 1648:         }
 1649:     }
 1650:     return -1;
 1651: }
 1652: // ]]>
 1653: </script>
 1654: END
 1655: }
 1656: 
 1657: sub required_privs {
 1658:     my $privs =  {
 1659:              '/adm/parmset'      => 'opa',
 1660:              '/adm/courseprefs'  => 'opa',
 1661:              '/adm/whatsnew'     => 'whn',
 1662:              '/adm/populate'     => 'cst',
 1663:              '/adm/trackstudent' => 'vsa',
 1664:              '/adm/statistics'   => 'vgr',
 1665:            };
 1666:     unless ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet') {
 1667:         $privs->{'/adm/classcalc'}   => 'vgr',
 1668:         $privs->{'/adm/assesscalc'}  => 'vgr',
 1669:         $privs->{'/adm/studentcalc'} => 'vgr';
 1670:     }
 1671:     return $privs;
 1672: }
 1673: 
 1674: # ================================================================ Main Program
 1675: 
 1676: BEGIN {
 1677:     if (! defined($readdesk)) {
 1678:         {
 1679:             my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
 1680:             if ( CORE::open( my $config,"<$tabfile") ) {
 1681:                 while (my $configline=<$config>) {
 1682:                     $configline=(split(/\#/,$configline))[0];
 1683:                     $configline=~s/^\s+//;
 1684:                     chomp($configline);
 1685:                     if ($configline=~/^cat\:/) {
 1686:                         my @entries=split(/\:/,$configline);
 1687:                         $category_positions{$entries[2]}=$entries[1];
 1688:                         $category_names{$entries[2]}=$entries[3];
 1689:                     } elsif ($configline=~/^prim\:/) {
 1690:                         my @entries = (split(/\:/, $configline))[1..5];
 1691:                         push @primary_menu, \@entries;
 1692:                     } elsif ($configline=~/^scnd\:/) {
 1693:                         my @entries = (split(/\:/, $configline))[1..5];
 1694:                         push @secondary_menu, \@entries; 
 1695:                     } elsif ($configline) {
 1696:                         push(@desklines,$configline);
 1697:                     }
 1698:                 }
 1699:                 CORE::close($config);
 1700:             }
 1701:         }
 1702:         $readdesk='done';
 1703:     }
 1704: }
 1705: 
 1706: 1;
 1707: __END__
 1708: 

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