File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.382: download - view: text, annotated - select for diffs
Thu Jul 5 19:22:47 2012 UTC (11 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- No evaluate or catalog_info icons/links in inline menu for documents
  uploaded directly to a course.

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

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