File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.369.2.4: download - view: text, annotated - select for diffs
Mon May 21 16:25:37 2012 UTC (12 years ago) by raeburn
Branches: version_2_11_X
- For 2.11
  - backport 1.372.

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

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