File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.378: download - view: text, annotated - select for diffs
Mon May 28 13:09:16 2012 UTC (12 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 5891
  - countdown moved to first item in right breadcrumbs.
  - added "stopwatch" icon.
  - added ability to toggle hiding/display of countdown.
  - default is to hide unless timed interval parameter is in effect.
  - "Submit Early" message only displayed ~ 5 mins before due date.
  - "Past Due" text replaces "Submit Early" when time left is zero.

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

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