File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.315.2.5: download - view: text, annotated - select for diffs
Sat Dec 25 00:32:05 2010 UTC (13 years, 6 months ago) by raeburn
Branches: version_2_10_X
- Backport 1.340.

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

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