File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.364: download - view: text, annotated - select for diffs
Wed Nov 30 18:30:32 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
- Typos (=> should be = in this context).

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

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