File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.372: download - view: text, annotated - select for diffs
Mon May 21 16:24:14 2012 UTC (12 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Use javascript to support dropdown in primary menu in IE 6
  (no :hover pseudo-class).

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

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