File:  [LON-CAPA] / loncom / interface / lonmenu.pm
Revision 1.481: download - view: text, annotated - select for diffs
Sat Oct 7 21:07:17 2017 UTC (6 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- "View source" link in Functions bar for published assessment items in a
  course, where user does not have editing privileges, but does have vxc
  priv in course, and system-level bre priv, and item publised source open.

    1: # The LearningOnline Network with CAPA
    2: # Routines to control the menu
    3: #
    4: # $Id: lonmenu.pm,v 1.481 2017/10/07 21:07:17 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 a two item array, 
  110: with the array elements containing XHTML for the left and right sides of 
  111: the menu that contains the following links: About, Message, Roles, Help, Logout 
  112: @primary_menu is filled within the BEGIN block of this module with 
  113: entries from mydesk.tab
  114: 
  115: =item secondary_menu()
  116: 
  117: Same as primary_menu() but operates on @secondary_menu.
  118: 
  119: =item create_submenu()
  120: 
  121: Creates XHTML for unordered list of sub-menu items which belong to a 
  122: particular top-level menu item. Uses hover pseudo class in css to display
  123: dropdown list when mouse hovers over top-level item. Support for IE6 
  124: (no hover psuedo class) via LC_hoverable class for <li> tag for top-
  125: level item, which employs jQuery to handle behavior on mouseover.
  126: 
  127: Inputs: 6 - (a) link and (b) target for anchor href in top level item,
  128:             (c) title for text wrapped by anchor tag in top level item,
  129:             (d) reference to array of arrays of sub-menu items,
  130:             (e) boolean to indicate whether to call &mt() to translate 
  131:                 name of menu item,
  132:             (f) optional class for <li> element in primary menu, for which
  133:                 sub menu is being generated.
  134: 
  135:  The underlying datastructure used in (d) contains data from mydesk.tab.
  136:  It consists of an array which has an array for each item appearing in
  137:  the menu (e.g. [["link", "title", "condition"]] for a single-item menu).
  138:  create_submenu() supports also the creation of XHTML for nested dropdown
  139:  menus represented by unordered lists. This is done by replacing the
  140:  scalar used for the link with an arrayreference containing the menuitems
  141:  for the nested menu. This can be done recursively so that the next menu
  142:  may also contain nested submenus.
  143: 
  144:  Example:
  145:  [											# begin of datastructure
  146: 	["/home/", "Home", "condition1"], 		# 1st item of the 1st layer menu
  147: 	[										# 2nd item of the 1st layer menu
  148: 		[									# anon. array for nested menu
  149: 			["/path1", "Path1", undef], 	# 1st item of the 2nd layer menu
  150: 			["/path2", "Path2", undef], 	# 2nd item of the 2nd layer menu
  151: 			[								# 3rd item of the 2nd layer menu
  152: 				[[...], [...], ..., [...]],	# containing another menu layer
  153: 				"Sub-Sub-Menu",				# title for this container
  154: 				undef
  155: 			]
  156: 		],									# end of array/nested menu
  157: 		"Sub-Menu",							# title for the container item
  158: 		undef
  159: 	]										# end of 2nd item of the 1st layer menu
  160: ]
  161: 
  162: =item innerregister()
  163: 
  164: This gets called in order to register a URL in the body of the document
  165: 
  166: =item clear()
  167: 
  168: =item switch()
  169: 
  170: Switch a button or create a link
  171: Switch acts on the javascript that is executed when a button is clicked.  
  172: The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
  173: 
  174: =item secondlevel()
  175: 
  176: =item openmenu()
  177: 
  178: =item inlinemenu()
  179: 
  180: =item rawconfig()
  181: 
  182: =item utilityfunctions()
  183: 
  184: Output from this routine is a number of javascript functions called by
  185: items in the inline menu, and in some cases items in the Main Menu page. 
  186: 
  187: =item serverform()
  188: 
  189: =item constspaceform()
  190: 
  191: =item get_nav_status()
  192: 
  193: =item hidden_button_check()
  194: 
  195: =item roles_selector()
  196: 
  197: =item jump_to_role()
  198: 
  199: =back
  200: 
  201: =cut
  202: 
  203: package Apache::lonmenu;
  204: 
  205: use strict;
  206: use Apache::lonnet;
  207: use Apache::lonhtmlcommon();
  208: use Apache::loncommon();
  209: use Apache::lonenc();
  210: use Apache::lonlocal;
  211: use Apache::lonmsg();
  212: use LONCAPA qw(:DEFAULT :match);
  213: use HTML::Entities();
  214: use Apache::lonwishlist();
  215: 
  216: use vars qw(@desklines %category_names %category_members %category_positions 
  217:             $readdesk @primary_menu %primary_submenu @secondary_menu);
  218: 
  219: my @inlineremote;
  220: 
  221: sub prep_menuitem {
  222:     my ($menuitem) = @_;
  223:     return '' unless(ref($menuitem) eq 'ARRAY');
  224:     my $link;
  225:     if ($$menuitem[1]) { # graphical Link
  226:         $link = "<img class=\"LC_noBorder\""
  227:               . " src=\"" . &Apache::loncommon::lonhttpdurl($$menuitem[1]) . "\"" 
  228:               . " alt=\"" . &mt($$menuitem[2]) . "\" />";
  229:     } else {             # textual Link
  230:         $link = &mt($$menuitem[3]);
  231:     }
  232:     return '<li><a' 
  233:            # highlighting for new messages
  234:            . ( $$menuitem[4] eq 'newmsg' ? ' class="LC_new_message"' : '') 
  235:            . qq| href="$$menuitem[0]" target="_top">$link</a></li>|;
  236: }
  237: 
  238: # primary_menu() evaluates @primary_menu and returns a two item array,
  239: # with the array elements containing XHTML for the left and right sides of 
  240: # the menu that contains the following links:
  241: # Personal, About, Message, Roles, Help, Logout
  242: # @primary_menu is filled within the BEGIN block of this module with 
  243: # entries from mydesk.tab
  244: sub primary_menu {
  245:     my ($crstype) = @_;
  246:     my (%menu);
  247:     # each element of @primary contains following array:
  248:     # (link url, icon path, alt text, link text, condition, position)
  249:     my $public;
  250:     if ((($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public'))
  251:         || (($env{'user.name'} eq '') && ($env{'user.domain'} eq ''))) {
  252:         $public = 1;
  253:     }
  254:     my $rolecount;
  255:     if (($crstype eq 'Placement') && (!$env{'request.role.adv'})) {
  256:         my $update=$env{'user.update.time'};
  257:         if (!$update) {
  258:             $update = $env{'user.login.time'};
  259:         }
  260:         my %roles_in_env;
  261:         $rolecount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
  262:     }
  263:     foreach my $menuitem (@primary_menu) {
  264:         # evaluate conditions 
  265:         next if    ref($menuitem)       ne 'ARRAY';    #
  266:         next if    $$menuitem[4]        eq 'nonewmsg'  # show links depending on
  267:                 && &Apache::lonmsg::mynewmail();       # whether a new msg 
  268:         next if    $$menuitem[4]        eq 'newmsg'    # arrived or not
  269:                 && !&Apache::lonmsg::mynewmail();      # 
  270:         next if    $$menuitem[4]        !~ /public/    ##we've a public user,
  271:                 && $public;                            ##who should not see all
  272:                                                        ##links
  273:         next if    $$menuitem[4]        eq 'onlypublic'# hide links which are 
  274:                 && !$public;                           # only visible to public
  275:                                                        # users
  276:         next if    $$menuitem[4]        eq 'roles'     ##show links depending on
  277:                 && &Apache::loncommon::show_course();  ##term 'Courses' or 
  278:         next if    $$menuitem[4]        eq 'courses'   ##'Roles' wanted
  279:                 && !&Apache::loncommon::show_course(); ##
  280:         my $title = $menuitem->[3];
  281:         if (($crstype eq 'Placement') && (!$env{'request.role.adv'})) {
  282:             if ($menuitem->[4] eq 'courses') {
  283:                 next unless ($rolecount>1);
  284:             } else {
  285:                 next unless (($title eq 'Personal') || ($title eq 'Logout'));
  286:             }
  287:         }
  288:         my $position = $menuitem->[5];
  289:         if ($position eq '') {
  290:             $position = 'right';
  291:         }
  292:         if (defined($primary_submenu{$title})) {
  293:             my ($link,$target);
  294:             if ($menuitem->[0] ne '') {
  295:                 $link = $menuitem->[0];
  296:                 $target = '_top';
  297:             } else {
  298:                 $link = '#';
  299:             }
  300:             my @primsub;
  301:             if (ref($primary_submenu{$title}) eq 'ARRAY') {
  302:                 foreach my $item (@{$primary_submenu{$title}}) {
  303:                     next if (($crstype eq 'Placement') && (!$env{'request.role.adv'}));
  304:                     next if (($item->[2] eq 'wishlist') && (!$env{'user.adv'}));
  305:                     next if ((($item->[2] eq 'portfolio') ||
  306:                              ($item->[2] eq 'blog')) &&
  307:                              (!&Apache::lonnet::usertools_access('','',$item->[2],
  308:                                                            undef,'tools')));
  309:                     push(@primsub,$item);
  310:                 }
  311:                 if ($title eq 'Personal' && $env{'user.name'} && $env{'user.domain'} ) {
  312:                     $title = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
  313:                 } else {
  314:                     $title = &mt($title);
  315:                 }
  316:                 if (@primsub > 0) {
  317:                     $menu{$position} .= &create_submenu($link,$target,$title,\@primsub,1);
  318:                 } elsif ($link) {
  319:                     $menu{$position} .= '<li><a href="'.$link.'" target="'.$target.'">'.$title.'</a></li>';
  320:                 }
  321:             }
  322:         } elsif ($$menuitem[3] eq 'Help') { # special treatment for helplink
  323:             next if ($crstype eq 'Placement'); 
  324:             if ($public) {
  325:                 my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  326:                 my $defdom = &Apache::lonnet::default_login_domain();
  327:                 my $to = &Apache::loncommon::build_recipient_list(undef,
  328:                                                                   'helpdeskmail',
  329:                                                                   $defdom,$origmail);
  330:                 if ($to ne '') {
  331:                     $menu{$position} .= &prep_menuitem($menuitem); 
  332:                 }
  333:             } else {
  334:                 $menu{$position} .= '<li>'.&Apache::loncommon::top_nav_help('Help').'</li>';
  335:             }
  336:         } else {
  337:             $menu{$position} .= prep_menuitem($menuitem);
  338:         }
  339:     }
  340:     my @output = ('','');
  341:     if ($menu{'left'} ne '') {
  342:         $output[0] = "<ol class=\"LC_primary_menu LC_floatleft\">$menu{'left'}</ol>";
  343:     }
  344:     if ($menu{'right'} ne '') {
  345:         $output[1] = "<ol class=\"LC_primary_menu LC_floatright LC_right\">$menu{'right'}</ol>";
  346:     }
  347:     return @output;
  348: }
  349: 
  350: #returns hashref {user=>'',dom=>''} containing:
  351: #   own name, domain if user is au
  352: #   name, domain of parent author if user is ca or aa
  353: #empty return if user is not an author or not on homeserver
  354: #
  355: #TODO this should probably be moved somewhere more central
  356: #since it can be used by different parts of the system
  357: sub getauthor{
  358:     return unless $env{'request.role'}=~/^(ca|aa|au)/; #nothing to do if user isn't some kind of author
  359: 
  360:                         #co- or assistent author?
  361:     my ($dom, $user) = ($env{'request.role'} =~ /^(?:ca|aa)\.\/($match_domain)\/($match_username)$/)
  362:                        ? ($1, $2) #domain, username of the parent author
  363:                        : @env{ ('request.role.domain', 'user.name') }; #own domain, username
  364: 
  365:     # current server == home server?
  366:     my $home =  &Apache::lonnet::homeserver($user,$dom);
  367:     foreach (&Apache::lonnet::current_machine_ids()){
  368:         return {user => $user, dom => $dom} if $_ eq $home;
  369:     }
  370: 
  371:     # if wrong server
  372:     return;
  373: }
  374: 
  375: sub secondary_menu {
  376:     my ($httphost) = @_;
  377:     my $menu;
  378: 
  379:     my $crstype = &Apache::loncommon::course_type();
  380:     my $crs_sec = $env{'request.course.id'} . ($env{'request.course.sec'} 
  381:                                                ? "/$env{'request.course.sec'}"
  382:                                                : '');
  383:     my $canedit       = &Apache::lonnet::allowed('mdc', $env{'request.course.id'});
  384:     my $canvieweditor = &Apache::lonnet::allowed('cev', $env{'request.course.id'});
  385:     my $canviewroster = $env{'course.'.$env{'request.course.id'}.'.student_classlist_view'};
  386:     if ($canviewroster eq 'disabled') {
  387:         undef($canviewroster);
  388:     }
  389:     my $canviewgrps   = &Apache::lonnet::allowed('vcg', $crs_sec); 
  390:     my $canmodifyuser = &Apache::lonnet::allowed('cst', $crs_sec);
  391:     my $canviewusers  = &Apache::lonnet::allowed('vcl', $crs_sec); 
  392:     my $canviewwnew   = &Apache::lonnet::allowed('whn', $crs_sec); 
  393:     my $canviewpara   = &Apache::lonnet::allowed('vpa', $crs_sec);
  394:     my $canmodpara    = &Apache::lonnet::allowed('opa', $crs_sec);
  395:     my $canvgr        = &Apache::lonnet::allowed('vgr', $crs_sec);
  396:     my $canmgr        = &Apache::lonnet::allowed('mgr', $crs_sec); 
  397:     my $author        = &getauthor();
  398: 
  399:     my ($cdom,$cnum,$showsyllabus,$showfeeds,$showresv,$grouptools);
  400:     $grouptools = 0;
  401:     if ($env{'request.course.id'}) {
  402:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  403:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  404:         if ($canedit || $canvieweditor)  {
  405:             $showsyllabus = 1;
  406:             $showfeeds = 1;
  407:         } else {
  408:             unless (&Apache::lonnet::is_on_map("public/$cdom/$cnum/syllabus")) {
  409:                 if (($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'}) ||
  410:                     ($env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) ||
  411:                     ($env{'course.'.$env{'request.course.id'}.'.updatedsyllabus'}) ||
  412:                     ($env{'request.course.syllabustime'})) {
  413:                     $showsyllabus = 1;
  414:                 }
  415:             }
  416:             if ($env{'request.course.feeds'}) {
  417:                 $showfeeds = 1;
  418:             }
  419:         }
  420:         unless ($canmgr || $canvgr) {
  421:             my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
  422:             if (keys(%slots) > 0) {
  423:                 $showresv = 1;
  424:             }
  425:         }
  426:         my %groups = &Apache::lonnet::get_active_groups(
  427:                      $env{'user.domain'}, $env{'user.name'},$cdom,$cnum);
  428:         if (%groups) {
  429:             foreach my $group (keys(%groups)) {
  430:                 my @privs = split(/:/,$env{"user.priv.$env{'request.role'}./$cdom/$cnum/$group"});
  431:                 shift(@privs);
  432:                 if (@privs) {
  433:                     $grouptools ++;
  434:                 }
  435:             }
  436:         }
  437:     }
  438: 
  439:     my ($canmodifycoauthor); 
  440:     if ($env{'request.role'} eq "au./$env{'user.domain'}/") {
  441:         my $extent = "$env{'user.domain'}/$env{'user.name'}";
  442:         if ((&Apache::lonnet::allowed('cca',$extent)) ||
  443:             (&Apache::lonnet::allowed('caa',$extent))) {
  444:             $canmodifycoauthor = 1;
  445:         }
  446:     }
  447:     my ($roleswitcher_js,$roleswitcher_form);
  448: 
  449:     foreach my $menuitem (@secondary_menu) {
  450:         # evaluate conditions 
  451:         next if    ref($menuitem)  ne 'ARRAY';
  452:         next if (($crstype eq 'Placement') && ($$menuitem[3] ne 'Roles') && (!$env{'request.role.adv'}));
  453:         next if    $$menuitem[4]   ne 'always'
  454:                 && ($$menuitem[4]   ne 'author' && $$menuitem[4] ne 'cca')
  455:                 && !$env{'request.course.id'};
  456:         next if    $$menuitem[4]   =~ /^crsedit/
  457:                 && (!$canedit && !$canvieweditor);
  458:         next if    $$menuitem[4]  eq 'nvgr'
  459:                 && $canvgr;
  460:         next if    $$menuitem[4]  eq 'vgr'
  461:                 && !$canvgr;
  462:         next if    $$menuitem[4]   eq 'viewusers'
  463:                 && !$canmodifyuser && !$canviewusers;
  464:         next if    $$menuitem[4]   eq 'noviewusers'
  465:                 && ($canmodifyuser || $canviewusers || !$canviewroster);
  466:         next if    $$menuitem[4]   eq 'mgr'
  467:                 && !$canmgr;
  468:         next if    $$menuitem[4]   eq 'showresv'
  469:                 && !$showresv;
  470:         next if    $$menuitem[4]   eq 'whn'
  471:                 && !$canviewwnew;
  472:         next if    $$menuitem[4]   eq 'params'
  473:                 && (!$canmodpara && !$canviewpara);
  474:         next if    $$menuitem[4]   =~ /showgroups$/
  475:                 && !$canviewgrps
  476:                 && !$grouptools;
  477:         next if    $$menuitem[4]   eq 'showsyllabus'
  478:                 && !$showsyllabus;
  479:         next if    $$menuitem[4]   eq 'showfeeds'
  480:                 && !$showfeeds;
  481:         next if    $$menuitem[4]    eq 'author'
  482:                 && !$author;
  483:         next if    $$menuitem[4]    eq 'cca'
  484:                 && !$canmodifycoauthor;
  485: 
  486:         if ($$menuitem[3] eq 'Roles' && $env{'request.course.id'}) {
  487:             # special treatment for role selector
  488:             ($roleswitcher_js,$roleswitcher_form,my $switcher) =
  489:                 &roles_selector(
  490:                         $env{'course.' . $env{'request.course.id'} . '.domain'},
  491:                         $env{'course.' . $env{'request.course.id'} . '.num'},
  492:                         $httphost
  493:                 );
  494:             $menu .= $switcher;
  495:         } else {
  496:             if ($$menuitem[3] eq 'Syllabus' && $env{'request.course.id'}) {
  497:                 my $url = $$menuitem[0];
  498:                 $url =~ s{\[cdom\]/\[cnum\]}{$cdom/$cnum};
  499:                 if (&Apache::lonnet::is_on_map($url)) {
  500:                     unless ($$menuitem[0] =~ /(\?|\&)register=1/) {
  501:                         $$menuitem[0] .= (($$menuitem[0]=~/\?/)? '&' : '?').'register=1';
  502:                     }
  503:                 } else {
  504:                     $$menuitem[0] =~ s{\&?register=1}{};
  505:                 }
  506:                 if ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://}) {
  507:                     if (($ENV{'SERVER_PORT'} == 443) || ($env{'request.use_absolute'} =~ m{^https://})) {
  508:                         unless ($$menuitem[0] =~ m{^https?://}) {
  509:                             $$menuitem[0] = 'http://'.$ENV{'SERVER_NAME'}.$$menuitem[0];
  510:                         }
  511:                         unless ($$menuitem[0] =~ /(\&|\?)usehttp=1/) {
  512:                             $$menuitem[0] .= (($$menuitem[0]=~/\?/) ? '&' : '?').'usehttp=1';
  513:                         }
  514:                     }
  515:                 }
  516:                 $$menuitem[0] = &HTML::Entities::encode($$menuitem[0],'&<>"');
  517:             }
  518:             $menu .= &prep_menuitem(\@$menuitem);
  519:         }
  520:     }
  521:     if ($menu =~ /\[url\].*\[symb\]/) {
  522:         my $escurl  = &escape( &Apache::lonenc::check_encrypt(
  523:                              $env{'request.noversionuri'}));
  524: 
  525:         my $escsymb = &escape( &Apache::lonenc::check_encrypt(
  526:                              $env{'request.symb'})); 
  527: 
  528:         if (    $env{'request.state'} eq 'construct'
  529:             and (   $env{'request.noversionuri'} eq '' 
  530:                  || !defined($env{'request.noversionuri'}))) 
  531:         {
  532:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  533:             ($escurl = $env{'request.filename'}) =~ s{^\Q$londocroot\E}{};
  534:             $escurl  = &escape($escurl);
  535:         }
  536:         $menu =~ s/\[url\]/$escurl/g;
  537:         $menu =~ s/\[symb\]/$escsymb/g;
  538:     }
  539:     $menu =~ s/\[uname\]/$$author{user}/g;
  540:     $menu =~ s/\[udom\]/$$author{dom}/g;
  541:     if ($showsyllabus || $showfeeds) {
  542:         $menu =~ s/\[cnum\]/$cnum/g;
  543:         $menu =~ s/\[cdom\]/$cdom/g;
  544:     }
  545:     if ($menu) {
  546:         $menu = "<ul id=\"LC_secondary_menu\">$menu</ul>";
  547:     }
  548:     if ($roleswitcher_form) {
  549:         $menu .= "\n$roleswitcher_js\n$roleswitcher_form";
  550:     }
  551:     return $menu;
  552: }
  553: 
  554: sub create_submenu {
  555:     my ($link,$target,$title,$submenu,$translate,$addclass) = @_;
  556:     return unless (ref($submenu) eq 'ARRAY');
  557:     my $disptarget;
  558:     if ($target ne '') {
  559:         $disptarget = ' target="'.$target.'"';
  560:     }
  561:     my $menu = '<li class="LC_hoverable '.$addclass.'">'.
  562:                '<a href="'.$link.'"'.$disptarget.'>'.
  563:                '<span class="LC_nobreak">'.$title.
  564:                '<span class="LC_fontsize_small" style="font-weight:normal;">'.
  565:                ' &#9660;</span></span></a>'.
  566:                '<ul>';
  567: 
  568:     # $link and $title are only used in the initial string written in $menu
  569:     # as seen above, not needed for nested submenus
  570:     $menu .= &build_submenu($target, $submenu, $translate, '1');
  571:     $menu .= '</ul></li>';
  572: 
  573:     return $menu;
  574: }
  575: 
  576: # helper routine for create_submenu
  577: # build the dropdown (and nested submenus) recursively
  578: # see perldoc create_submenu documentation for further information
  579: sub build_submenu {
  580:     my ($target, $submenu, $translate, $first_level) = @_; 
  581:     unless (@{$submenu}) {
  582:         return '';
  583:     }
  584: 
  585:     my $menu = '';
  586:     my $count = 0;
  587:     my $numsub = scalar(@{$submenu});
  588:     foreach my $item (@{$submenu}) {
  589:         $count ++;
  590:         if (ref($item) eq 'ARRAY') {
  591:             my $href = $item->[0];
  592:             my $bordertop;
  593:             my $borderbot;
  594:             my $title;
  595: 
  596:             if ($translate) {
  597:                  $title = &mt($item->[1]);
  598:             } else {
  599:                 $title = $item->[1];
  600:             }
  601: 
  602:             if ($count == 1 && !$first_level) {
  603:                 $bordertop = 'border-top: 1px solid black;';
  604:             }
  605:             if ($count == $numsub) {
  606:                 $borderbot = 'border-bottom: 1px solid black;';
  607:             }
  608: 
  609:             # href is a reference to another submenu
  610:             if (ref($href) eq 'ARRAY') {
  611:                 $menu .= '<li style="margin:0;padding:0;'.$bordertop . $borderbot . '">';
  612:                 $menu .= '<p><span class="LC_primary_menu_innertitle">'
  613: 					. $title . '</span><span class="LC_primary_menu_innerarrow">&#9654;</span></p>';
  614:                 $menu .= '<ul>';
  615:                 $menu .= &build_submenu($target, $href, $translate);
  616:                 $menu .= '</ul>';
  617:                 $menu .= '</li>';    
  618:             } else {    # href is the actual hyperlink and does not represent another submenu
  619:                         # for the current menu title
  620:                 if ($href =~ /(aboutme|rss\.html)$/) {
  621:                     next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne ''));
  622:                     $href =~ s/\[domain\]/$env{'user.domain'}/g;
  623:                     $href =~ s/\[user\]/$env{'user.name'}/g;
  624:                 }
  625:                 unless (($href eq '') || ($href =~ /^\#/)) {
  626:                     $target = ' target="_top"';
  627:                 }
  628: 
  629:                 $menu .= '<li style="margin:0;padding:0;'. $bordertop . $borderbot .'">';
  630:                 $menu .= '<a href="'.$href.'"'.$target.'>' .  $title . '</a>';
  631:                 $menu .= '</li>';
  632:             }
  633:         }
  634:     }
  635:     return $menu;
  636: }
  637: 
  638: sub innerregister {
  639:     my ($forcereg,$bread_crumbs,$group,$pagebuttonshide,$hostname) = @_;
  640:     my $const_space = ($env{'request.state'} eq 'construct');
  641:     my $is_const_dir = 0;
  642: 
  643:     if ($env{'request.noversionuri'} =~ m{^/res/adm/pages/}) { return ''; }
  644: 
  645:     $env{'request.registered'} = 1;
  646: 
  647:     undef(@inlineremote);
  648: 
  649:     my ($mapurl,$resurl,$crstype,$navmap);
  650: 
  651:     if ($env{'request.course.id'}) {
  652: #
  653: #course_type:  Course, Community, or Placement
  654: #
  655:         $crstype = &Apache::loncommon::course_type();
  656:         if ($env{'request.symb'}) {
  657:             my $ignorenull;
  658:             unless ($env{'request.noversionuri'} eq '/adm/navmaps') {
  659:                 $ignorenull = 1;
  660:             }
  661:             my $symb = &Apache::lonnet::symbread('','',$ignorenull);
  662:             ($mapurl, my $rid, $resurl) = &Apache::lonnet::decode_symb($symb);
  663:             my $coursetitle = $env{'course.'.$env{'request.course.id'}.'.description'};
  664: 
  665:             my $maptitle = &Apache::lonnet::gettitle($mapurl);
  666:             my $restitle = &Apache::lonnet::gettitle($symb);
  667:             my (@crumbs,@mapcrumbs);
  668:             if (($env{'request.noversionuri'} ne '/adm/navmaps') && ($mapurl ne '') &&
  669:                 (!(($crstype eq 'Placement') && !$env{'request.role.adv'}))) {
  670:                 $navmap = Apache::lonnavmaps::navmap->new();
  671:                 if (ref($navmap)) {
  672:                     @mapcrumbs = $navmap->recursed_crumbs($mapurl,$restitle);
  673:                 }
  674:             }
  675:             unless (($forcereg) &&
  676:                     ($env{'request.noversionuri'} eq '/adm/navmaps') &&
  677:                     ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'}) ||
  678:                     (($crstype eq 'Placement') && (!$env{'request.role.adv'}))) {
  679:                 @crumbs = ({text  => $crstype.' Contents', 
  680:                             href  => "Javascript:gopost('/adm/navmaps','')"});
  681:             }
  682:             if ($mapurl ne $env{'course.'.$env{'request.course.id'}.'.url'}) { 
  683:                 if (@mapcrumbs) {
  684:                     push(@crumbs,@mapcrumbs);
  685:                 } elsif (!(($crstype eq 'Placement') && (!$env{'request.role.adv'}))) {
  686:                     push(@crumbs, {text  => '...',
  687:                                    no_mt => 1});
  688:                 }
  689:             }
  690: 
  691:             unless ((($crstype eq 'Placement') && (!$env{'request.role.adv'})) || (@mapcrumbs) ||
  692:                     (!$maptitle) || ($maptitle eq 'default.sequence') ||
  693:                     ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'})) {
  694:                 push @crumbs, {text => $maptitle, no_mt => 1, href => $mapurl};
  695:             }
  696:             if ($restitle && !@mapcrumbs) {
  697:                 push(@crumbs,{text => $restitle, no_mt => 1});
  698:             }
  699:             my @tools;
  700:             if ($env{'request.filename'} =~ /\.page$/) {
  701:                 my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
  702:                 if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
  703:                     @tools = @{$breadcrumb_tools{'tools'}};
  704:                 }
  705:             }
  706:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  707:             &Apache::lonhtmlcommon::add_breadcrumb(@crumbs);
  708:             if (@tools) {
  709:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',@tools);
  710:             }
  711:         } else {
  712:             $resurl = $env{'request.noversionuri'};
  713:             my $courseurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
  714:             my $title = &mt('View Resource');
  715:             if ($resurl =~ m{^\Q/uploaded$courseurl/supplemental/\E(default|\d+)/}) {
  716:                 &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['folderpath','title']);
  717:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  718:                 if ($env{'form.title'}) {
  719:                     $title = $env{'form.title'};
  720:                 }
  721:                 my $trail;
  722:                 if ($env{'form.folderpath'}) {
  723:                     &prepare_functions($resurl,$forcereg,$group,undef,undef,1,$hostname);
  724:                     ($trail) =
  725:                         &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  726:                 } else {
  727:                     &Apache::lonhtmlcommon::add_breadcrumb(
  728:                     {text  => "Supplemental $crstype Content",
  729:                      href  => "javascript:gopost('/adm/supplemental','')"});
  730:                     $title = &mt('View Resource');
  731:                     ($trail) = 
  732:                         &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  733:                 }
  734:                 return $trail;
  735:             } elsif ($resurl =~ m{^\Q/uploaded$courseurl/portfolio/syllabus/}) {
  736:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  737:                 &prepare_functions('/public'.$courseurl."/syllabus",
  738:                                    $forcereg,$group,undef,undef,1,$hostname);
  739:                 $title = &mt('Syllabus File');
  740:                 my ($trail) =
  741:                     &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1,$hostname);
  742:                 return $trail;
  743:             }
  744:             unless ($env{'request.state'} eq 'construct') {
  745:                 &Apache::lonhtmlcommon::clear_breadcrumbs();
  746:                 &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  747:             }
  748:         }
  749:     } elsif (! $const_space){
  750:         #a situation when we're looking at a resource outside of context of a 
  751:         #course or construction space (e.g. with cumulative rights)
  752:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  753:         unless ($env{'request.noversionuri'} =~ m{^/adm/$match_domain/$match_username/aboutme$}) {
  754:             &Apache::lonhtmlcommon::add_breadcrumb({text => 'View Resource'});
  755:         }
  756:     }
  757: # =============================================================================
  758: # ============================ This is for URLs that actually can be registered
  759:     return '' unless ( ($env{'request.noversionuri'}!~m{^/(res/)*adm/}) 
  760:                        || $forcereg );
  761:     my ($cdom,$cnum,%perms,$cfile,$switchserver,$home,$forceedit,
  762:         $forceview,$editbutton);
  763:     if (($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) ||
  764:         ($env{'request.role'} !~/^(aa|ca|au)/)) {
  765:         $editbutton = &prepare_functions($resurl,$forcereg,$group,'','','',$hostname);
  766:     }
  767:     if ($editbutton eq '') {
  768:         $editbutton = &clear(6,1);
  769:     }
  770: 
  771: #
  772: # This applies in course context
  773: #
  774:     if ($env{'request.course.id'}) {
  775:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  776:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  777:         $perms{'mdc'} = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  778:         $perms{'cev'} = &Apache::lonnet::allowed('cev',$env{'request.course.id'});
  779:         my @privs;
  780:         if ($env{'request.symb'} ne '') {
  781:              if ($env{'request.filename'}=~/$LONCAPA::assess_re/) {
  782:                  push(@privs,('mgr','vgr'));
  783:              }
  784:              push(@privs,('opa','vpa'));
  785:         }
  786:         foreach my $priv (@privs) {
  787:             $perms{$priv} = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
  788:             if (!$perms{$priv} && $env{'request.course.sec'} ne '') {
  789:                 $perms{$priv} = 
  790:                     &Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}");
  791:             }
  792:         }
  793: #
  794: # Determine whether or not to show Grades and Submissions buttons
  795: #
  796:         if ($env{'request.symb'} ne '' &&
  797:             $env{'request.filename'}=~/$LONCAPA::assess_re/) {
  798:             if ($perms{'mgr'}) {
  799:                 &switch('','',7,2,'pgrd.png','Content Grades','grades[_4]',
  800:                         "gocmd('/adm/grades','gradingmenu')",
  801:                         'Content Grades');
  802:             } elsif ($perms{'vgr'}) {
  803:                 &switch('','',7,2,'subm.png','Content Submissions','missions[_1]',
  804:                         "gocmd('/adm/grades','submission')",
  805:                         'Content Submissions');
  806:              }
  807:         }
  808:         if (($env{'request.symb'} ne '') && (($perms{'opa'}) || ($perms{'vpa'}))) {
  809:             &switch('','',7,3,'pparm.png','Content Settings','parms[_2]',
  810:                     "gocmd('/adm/parmset','set')",
  811:                     'Content Settings');
  812: 	}
  813: # End grades/submissions check
  814: 
  815: #
  816: # This applies to items inside a folder/page modifiable in the course.
  817: #
  818:         if (($env{'request.symb'}=~/^uploaded/) && (($perms{'mdc'}) || ($perms{'cev'}))) {
  819:             my $text = 'Edit Folder';
  820:             if (($mapurl =~ /\.page$/) ||
  821:                 ($env{'request.symb'}=~
  822:                      m{uploaded/$cdom/$cnum/default_\d+\.page$}))  {
  823:                 $text = 'Edit Page';
  824:             }
  825:             &switch('','',7,4,'docs-22x22.png',$text,'parms[_2]',
  826:                     "gocmd('/adm/coursedocs','direct')",
  827:                     'Folder/Page Content');
  828:         }
  829: # End modifiable folder/page container check
  830:     }
  831: # End course context
  832: 
  833: # Prepare the rest of the buttons
  834:         my ($menuitems,$got_prt,$got_wishlist);
  835:         if ($const_space) {
  836: #
  837: # We are in construction space
  838: #
  839: 
  840:             my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  841: 	    my ($udom,$uname,$thisdisfn) =
  842: 		($env{'request.filename'}=~m{^\Q$londocroot/priv/\E([^/]+)/([^/]+)/(.*)$});
  843:             my $currdir = '/priv/'.$udom.'/'.$uname.'/'.$thisdisfn;
  844:             if ($currdir =~ m-/$-) {
  845:                 $is_const_dir = 1;
  846:                 if ($thisdisfn eq '') {
  847:                     unless (($env{'request.course.id'}) && 
  848:                             ($env{'course.'.$env{'request.course.id'}.'.num'} eq $uname) &&
  849:                             ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom)) { 
  850:                         $is_const_dir = 2;
  851:                     }
  852:                 }
  853:             } else {
  854:                 $currdir =~ s|[^/]+$||;
  855: 		my $cleandisfn = &Apache::loncommon::escape_single($thisdisfn);
  856: 		my $esc_currdir = &Apache::loncommon::escape_single($currdir);
  857: #
  858: # Probably should be in mydesk.tab
  859: #
  860:                 $menuitems=(<<ENDMENUITEMS);
  861: s&6&1&list.png&Directory&dir[_1]&golist('$esc_currdir')&List current directory
  862: s&6&2&rtrv.png&Retrieve&version[_1]&gocstr('/adm/retrieve','/priv/$udom/$uname/$cleandisfn')&Retrieve old version
  863: s&6&3&pub.png&Publish&resource[_3]&gocstr('/adm/publish','/priv/$udom/$uname/$cleandisfn')&Publish this resource
  864: s&7&1&del.png&Delete&resource[_2]&gocstr('/adm/cfile?action=delete','/priv/$udom/$uname/$cleandisfn')&Delete this resource
  865: s&7&2&prt.png&Print&printout[_1]&gocstr('/adm/printout','/priv/$udom/$uname/$cleandisfn')&Prepare a printable document
  866: ENDMENUITEMS
  867:             }
  868:                 if (ref($bread_crumbs) eq 'ARRAY') {
  869:                     &Apache::lonhtmlcommon::clear_breadcrumbs();
  870:                     foreach my $crumb (@{$bread_crumbs}){
  871:                         &Apache::lonhtmlcommon::add_breadcrumb($crumb);
  872:                     }
  873:                 }
  874:         } elsif ( defined($env{'request.course.id'}) && 
  875: 		 $env{'request.symb'} ne '' ) {
  876: #
  877: # We are in a course and looking at a registered URL
  878: # Should probably be in mydesk.tab
  879: #
  880:             $menuitems = "c&3&1";
  881:             if (($crstype ne 'Placement') || ($env{'request.role.adv'})) {
  882:                 $menuitems.="
  883: s&2&1&back.png&&&gopost('/adm/flip','back:'+currentURL)&Previous content resource&&1
  884: s&2&3&forw.png&&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3";
  885:             } else {
  886: # Suppress display of backward arrow for Placement Tests
  887: # Suppress display of forward arrow for Placement Tests if this is the last resource.
  888:                 my $showforw = 1;
  889:                 if ($env{'request.symb'}) {
  890:                     my $navmap = Apache::lonnavmaps::navmap->new();
  891:                     if (ref($navmap)) {
  892:                         if (&Apache::lonplacementtest::is_lastres($env{'request.symb'},$navmap)) {
  893:                             $showforw = 0;
  894:                         }
  895:                     }
  896:                 }
  897:                 if ($showforw) {
  898:                     $menuitems.="
  899: s&2&3&forw.png&&&gopost('/adm/flip','forward:'+currentURL)&Next content resource&&3";
  900:                 }
  901:             }
  902: 	    $menuitems .= (<<ENDMENUITEMS);
  903: 
  904: c&6&3
  905: c&8&1
  906: c&8&2
  907: s&8&3&prt.png&Print&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  908: ENDMENUITEMS
  909:             $got_prt = 1;
  910:             if (($env{'user.adv'}) && ($env{'request.uri'} =~ /^\/res/)
  911:                 && (!$env{'request.enc'})) {
  912:                 my ($cnum,$cdom) = &Apache::loncommon::crsauthor_url($env{'request.uri'});
  913:                 unless ($cnum) {
  914:                     # wishlist is only available for users with access to resource-pool
  915:                     # and links can only be set for resources within the resource-pool
  916:                     $menuitems .= (<<ENDMENUITEMS);
  917: s&9&1&wishlist-link.png&Stored Links&wishlistlink[_2]&set_wishlistlink()&Save a link for this resource in my personal Stored Links repository&&1
  918: ENDMENUITEMS
  919:                     $got_wishlist = 1;
  920:                 }
  921:             }
  922: 
  923: my $currentURL = &Apache::loncommon::get_symb();
  924: my ($symb_old,$symb_old_enc) = &Apache::loncommon::clean_symb($currentURL);
  925: my $annotation = &Apache::loncommon::get_annotation($symb_old,$symb_old_enc);
  926: $menuitems.="s&9&3&";
  927: if(length($annotation) > 0){
  928: 	$menuitems.="anot2.png";
  929: }else{
  930: 	$menuitems.="anot.png";
  931: }
  932: $menuitems.="&Notes&&annotate()&";
  933: $menuitems.="Make notes and annotations about this resource&&1\n";
  934: my $is_mobile;
  935: if ($env{'browser.mobile'}) {
  936:     $is_mobile = 1;
  937: }
  938: 
  939:             unless ($env{'request.noversionuri'}=~/\/(bulletinboard|smppg|navmaps|syllabus|aboutme|viewclasslist|portfolio|ext\.tool)(\?|$)/) {
  940: 		if ((!$env{'request.enc'}) && ($env{'request.noversionuri'} !~ m{^/adm/wrapper/ext/}) && ($env{'request.noversionuri'} !~ m{^/uploaded/$match_domain/$match_courseid/docs/})) {
  941: 		    $menuitems.=(<<ENDREALRES);
  942: s&6&3&catalog.png&Info&info[_1]&catalog_info('$is_mobile')&Show Metadata
  943: ENDREALRES
  944:                 }
  945:                 unless (($env{'request.noversionuri'} =~ m{^/uploaded/$match_domain/$match_courseid/docs/}) ||
  946:                         ($env{'request.noversionuri'} =~ m{^\Q/adm/wrapper/\E(ext|uploaded)/})) {  
  947:                     $menuitems.=(<<ENDREALRES);
  948: s&8&1&eval.png&Evaluate&this[_1]&gopost('/adm/evaluate',currentURL,1)&Provide my evaluation of this resource
  949: ENDREALRES
  950:                 }
  951:                 unless ($env{'request.noversionuri'} =~ m{^\Q/adm/wrapper/\E(ext|uploaded)/}) {
  952:                     $menuitems.=(<<ENDREALRES);
  953: 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
  954: ENDREALRES
  955:                 }
  956: 	    }
  957:         }
  958: 	if ($env{'request.uri'} =~ /^\/res/) {
  959:             unless ($got_prt) {
  960: 	        $menuitems .= (<<ENDMENUITEMS);
  961: s&8&3&prt.png&Print&printout[_1]&gopost('/adm/printout',currentURL)&Prepare a printable document
  962: ENDMENUITEMS
  963:                 $got_prt = 1;
  964:             }
  965:             unless ($got_wishlist) {
  966:                 if (($env{'user.adv'}) && (!$env{'request.enc'})) {
  967:                     # wishlist is only available for users with access to resource-pool
  968:                     $menuitems .= (<<ENDMENUITEMS);
  969: 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
  970: ENDMENUITEMS
  971:                     $got_wishlist = 1;
  972:                 }
  973: 	    }
  974:         }
  975:         my $buttons='';
  976:         foreach (split(/\n/,$menuitems)) {
  977: 	    my ($command,@rest)=split(/\&/,$_);
  978:             my $idx=10*$rest[0]+$rest[1];
  979:             if (&hidden_button_check() eq 'yes') {
  980:                 if ($idx == 21 ||$idx == 23) {
  981:                     $buttons.=&switch('','',@rest);
  982:                 } else {
  983:                     $buttons.=&clear(@rest);
  984:                 }
  985:             } else {  
  986:                 if ($command eq 's') {
  987: 	            $buttons.=&switch('','',@rest);
  988:                 } else {
  989:                     $buttons.=&clear(@rest);
  990:                 }
  991:             }
  992:         }
  993:         my $showprogress;
  994:         if (($crstype eq 'Placement') && (!$env{'request.role.adv'})) {
  995:             $showprogress = &placement_progress();
  996:         }
  997: 
  998: 	my $addremote=0;
  999: 	foreach (@inlineremote) { if ($_ ne '') { $addremote=1; last;} }
 1000: 
 1001:     if ($addremote) {
 1002:         my ($countdown,$buttonshide);
 1003:         if ($env{'request.filename'} =~ /\.page$/) {
 1004:             my %breadcrumb_tools = &Apache::lonhtmlcommon::current_breadcrumb_tools();
 1005:             if (ref($breadcrumb_tools{'tools'}) eq 'ARRAY') {
 1006:                 $countdown = $breadcrumb_tools{'tools'}->[0];
 1007:             }
 1008:             $buttonshide = $pagebuttonshide;
 1009:         } else {
 1010:             $countdown = &countdown_timer();
 1011:             $buttonshide = &hidden_button_check();
 1012:         }
 1013:         &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1014: 
 1015:             &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1016:                 'navigation', @inlineremote[21,23]);
 1017: 
 1018:         if ($buttonshide eq 'yes') {
 1019:             if ($countdown) {
 1020:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$countdown);
 1021:             }
 1022:             if ($showprogress) {
 1023:                 &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$showprogress);
 1024:             }
 1025:         } else {
 1026:             my @tools = @inlineremote[93,91,81,82,83];
 1027:             if ($countdown) {
 1028:                 unshift(@tools,$countdown);
 1029:             }
 1030:             &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1031:                 'tools',@tools);
 1032: 
 1033:             #publish button in construction space
 1034:             if ($env{'request.state'} eq 'construct'){
 1035:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1036:                      'advtools', $inlineremote[63]);
 1037:             } else {
 1038:                 &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1039:                      'tools', $inlineremote[63]);
 1040:             }
 1041:             &advtools_crumbs(@inlineremote);
 1042:         }
 1043:     } else {
 1044:         if ($showprogress) {
 1045:             &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$showprogress);
 1046:         }
 1047:     }
 1048:     my ($topic_help,$topic_help_text);
 1049:     if ($is_const_dir == 2) {
 1050:         if ((($ENV{'SERVER_PORT'} == 443) || 
 1051:              ($Apache::lonnet::protocol{$Apache::lonnet::perlvar{'lonHostID'}} eq 'https')) && 
 1052:             (&Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},'webdav'))) {
 1053:             $topic_help = 'Authoring_WebDAV,Authoring_WebDAV_Mac_10v6,Authoring_WebDAV_Mac_10v10,'.
 1054:                           'Authoring_WebDAV_Windows_v7,Authoring_WebDAV_Linux_Centos';
 1055:             $topic_help_text = 'About WebDAV access';
 1056:         }
 1057:     }
 1058:     return   &Apache::lonhtmlcommon::scripttag('', 'start')
 1059:            . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0,'','','','',$topic_help,$topic_help_text)
 1060:            . &Apache::lonhtmlcommon::scripttag('', 'end');
 1061: }
 1062: 
 1063: sub get_editbutton {
 1064:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$forcereg,$hostname) = @_;
 1065:     my $jscall;
 1066:     if (($forceview) && ($env{'form.todocs'})) {
 1067:         my ($folderpath,$command,$navmap);
 1068:         if ($env{'request.symb'}) {
 1069:             $folderpath = &Apache::loncommon::symb_to_docspath($env{'request.symb'},\$navmap);
 1070:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
 1071:             $folderpath = $env{'form.folderpath'};
 1072:             $command = '&forcesupplement=1';
 1073:         }
 1074:         $folderpath = &escape(&HTML::Entities::encode(&escape($folderpath),'<>&"'));
 1075:         $jscall = "go('/adm/coursedocs?folderpath=$folderpath$command')";
 1076:     } else {
 1077:         my $suppanchor;
 1078:         if ($env{'form.folderpath'}) {
 1079:             $suppanchor = $env{'form.anchor'};
 1080:         }
 1081:         $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver,
 1082:                                                 $forceedit,$forcereg,$env{'request.symb'},
 1083:                                                 &escape($env{'form.folderpath'}),
 1084:                                                 &escape($env{'form.title'}),$hostname,
 1085:                                                 $env{'form.idx'},&escape($env{'form.suppurl'}),
 1086:                                                 $env{'form.todocs'},$suppanchor);
 1087:     }
 1088:     if ($jscall) {
 1089:         my $icon = 'pcstr.png';
 1090:         my $label = 'Edit';
 1091:         if ($forceview) {
 1092:             $icon = 'tolastloc.png';
 1093:             $label = 'Exit Editing';
 1094:         }
 1095:         &switch('','',6,1,$icon,$label,'resource[_2]',
 1096:                 $jscall,"Edit this resource");
 1097:         return 1;
 1098:     }
 1099:     return;
 1100: }
 1101: 
 1102: sub prepare_functions {
 1103:     my ($resurl,$forcereg,$group,$bread_crumbs,$advtools,$docscrumbs,$hostname) = @_;
 1104:     unless ($env{'request.registered'}) {
 1105:         undef(@inlineremote);
 1106:     }
 1107:     my ($cdom,$cnum,%perms,$cfile,$switchserver,$home,$forceedit,
 1108:         $forceview);
 1109: 
 1110:     if ($env{'request.course.id'}) {
 1111:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1112:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1113:         $perms{'mdc'} = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 1114:     }
 1115: 
 1116:     my $editbutton = '';
 1117:     my $viewsrcbutton = '';
 1118: #
 1119: # Determine whether or not to display 'Edit' or 'View Source' icon/button
 1120: #
 1121:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 1122:         my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1123:         ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1124:             &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1125:                 &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1126:         if (($cfile) && ($home ne '') && ($home ne 'no_host')) {
 1127:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1128:                                           $forceedit,$forceview,$forcereg);
 1129:         }
 1130:     } elsif ((!$env{'request.course.id'}) &&
 1131:              ($env{'user.author'}) && ($env{'request.filename'}) &&
 1132:              ($env{'request.role'} !~/^(aa|ca|au)/)) {
 1133: #
 1134: # Currently do not have the role of author or co-author.
 1135: # Do we have authoring privileges for the resource?
 1136: #
 1137:         my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1138:         ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1139:             &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1140:                 &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1141:         if (($cfile) && ($home ne '') && ($home ne 'no_host')) {
 1142:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1143:                                           $forceedit,$forceview,$forcereg);
 1144:         }
 1145:     } elsif ($env{'request.course.id'}) {
 1146: #
 1147: # This applies in course context
 1148: #
 1149:         if (($perms{'mdc'}) &&
 1150:             (($resurl =~ m{^/?public/$cdom/$cnum/syllabus}) ||
 1151:              ($resurl =~ m{^/?uploaded/$cdom/$cnum/portfolio/syllabus/}) ||
 1152:              (($resurl =~ m{^/?uploaded/$cdom/$cnum/default_\d+\.sequence$}) && ($env{'form.navmap'})))) {
 1153:             if ($resurl =~ m{^/}) {
 1154:                 $cfile = $resurl;
 1155:             } else {
 1156:                 $cfile = "/$resurl";
 1157:             }
 1158:             $home = &Apache::lonnet::homeserver($cnum,$cdom);
 1159:             if ($env{'form.forceedit'}) {
 1160:                 $forceview = 1;
 1161:             } else {
 1162:                 $forceedit = 1;
 1163:             }
 1164:             if ($cfile =~ m{^/uploaded/$cdom/$cnum/default_\d+\.sequence$}) {
 1165:                 my $text = 'Edit Folder';
 1166:                 &switch('','',7,4,'docs-22x22.png','Edit Folder','parms[_2]',
 1167:                         "gocmd('/adm/coursedocs','direct')",
 1168:                         'Folder/Page Content');
 1169:                 $editbutton = 1;
 1170:             } else {
 1171:                 $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1172:                                               $forceedit,$forceview,$forcereg,
 1173:                                               $hostname);
 1174:             }
 1175:         } elsif (($resurl eq '/adm/extresedit') &&
 1176:                  (($env{'form.symb'}) || ($env{'form.folderpath'}))) {
 1177:             ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1178:             &Apache::lonnet::can_edit_resource($resurl,$cnum,$cdom,$resurl,
 1179:                                                $env{'form.symb'});
 1180:             if ($cfile ne '') {
 1181:                 $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1182:                                               $forceedit,$forceview,$forcereg);
 1183:             }
 1184:         } elsif (($resurl =~ m{^/?adm/viewclasslist$}) &&
 1185:                  (&Apache::lonnet::allowed('opa',$env{'request.course.id'}))) {
 1186:             ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1187:             &Apache::lonnet::can_edit_resource($resurl,$cnum,$cdom,$resurl,
 1188:                                                $env{'form.symb'});
 1189:             $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1190:                                           $forceedit,$forceview,$forcereg);
 1191:         } elsif (($resurl !~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) &&
 1192:                  ($resurl ne '/cgi-bin/printout.pl')) {
 1193:             if ($env{'request.filename'}) {
 1194:                 my $file=&Apache::lonnet::declutter($env{'request.filename'});
 1195:                 ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1196:                     &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,
 1197:                         &Apache::lonnet::clutter($resurl),$env{'request.symb'},$group);
 1198:                 if ($cfile ne '') {
 1199:                     $editbutton = &get_editbutton($cfile,$home,$switchserver,
 1200:                                                   $forceedit,$forceview,$forcereg,
 1201:                                                   $hostname);
 1202:                 }
 1203:                 if ((($cfile eq '') || (!$editbutton)) &&
 1204:                     ($resurl =~ /$LONCAPA::assess_re/)) {
 1205:                     my $showurl = &Apache::lonnet::clutter($resurl);
 1206:                     if ((&Apache::lonnet::allowed('cre','/')) &&
 1207:                         (&Apache::lonnet::metadata($resurl,'sourceavail') eq 'open')) {
 1208:                         $viewsrcbutton = 1;
 1209:                     } elsif (&Apache::lonnet::allowed('vxc',$env{'request.course.id'})) {
 1210:                         if ($showurl =~ m{^\Q/res/$cdom/\E($match_username)/}) {
 1211:                             my $auname = $1;
 1212:                             if (($env{'request.course.adhocsrcaccess'} ne '') &&
 1213:                                 (grep(/^\Q$auname\E$/,split(/,/,$env{'request.course.adhocsrcaccess'})))) {
 1214:                                 $viewsrcbutton = 1;
 1215:                             } elsif ((&Apache::lonnet::metadata($resurl,'sourceavail') eq 'open') &&
 1216:                                      (&Apache::lonnet::allowed('bre','/'))) {
 1217:                                 $viewsrcbutton = 1;
 1218:                             }
 1219:                         }
 1220:                     }
 1221:                     if ($viewsrcbutton) {
 1222:                         &switch('','',6,1,'pcstr.png','View Source','resource[_2]','open_source()',
 1223:                                 'View source code');
 1224:                     }
 1225:                 }
 1226:             }
 1227:         }
 1228:     }
 1229: # End determination of 'Edit' icon/button display
 1230: 
 1231:     if ($env{'request.course.id'}) {
 1232: # This applies to about me page for users in a course
 1233:         if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 1234:             my ($sdom,$sname) = ($1,$2);
 1235:             unless (&Apache::lonnet::is_course($sdom,$sname)) {
 1236:                 &switch('','',6,4,'mail-message-new-22x22.png','Message to user',
 1237:                         '',
 1238:                         "go('/adm/email?compose=individual&amp;recname=$sname&amp;recdom=$sdom')",
 1239:                             'Send message to specific user');
 1240:             }
 1241:             my $hideprivileged = 1;
 1242:             if (&Apache::lonnet::in_course($sdom,$sname,$cdom,$cnum,undef,
 1243:                                            $hideprivileged)) {
 1244:                 foreach my $priv ('vsa','vgr','srm') {
 1245:                     $perms{$priv} = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
 1246:                     if (!$perms{$priv} && $env{'request.course.sec'} ne '') {
 1247:                         $perms{$priv} =
 1248:                             &Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}");
 1249:                     }
 1250:                 }
 1251:                 if ($perms{'vsa'}) {
 1252:                     &switch('','',6,5,'trck-22x22.png','Activity',
 1253:                             '',
 1254:                             "go('/adm/trackstudent?selected_student=$sname:$sdom')",
 1255:                             'View recent activity by this person');
 1256:                 }
 1257:                 if ($perms{'vgr'}) {
 1258:                     &switch('','',6,6,'rsrv-22x22.png','Reservations',
 1259:                             '',
 1260:                             "go('/adm/slotrequest?command=showresv&amp;origin=aboutme&amp;uname=$sname&amp;udom=$sdom')",
 1261:                             'Slot reservation history');
 1262:                 }
 1263:                 if ($perms{'srm'}) {
 1264:                     &switch('','',6,7,'contact-new-22x22.png','Records',
 1265:                             '',
 1266:                             "go('/adm/email?recordftf=retrieve&amp;recname=$sname&amp;recdom=$sdom')",
 1267:                             'Add records');
 1268:                 }
 1269:             }
 1270:         }
 1271:         if (($env{'form.folderpath'} =~ /^supplemental/) &&
 1272:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
 1273:             (($resurl =~ m{^/adm/wrapper/ext/}) ||
 1274:              ($resurl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) ||
 1275:              ($resurl =~ m{^/uploaded/$cdom/$cnum/supplemental/}) ||
 1276:              ($resurl eq '/adm/supplemental') ||
 1277:              ($resurl =~ m{^/public/$cdom/$cnum/syllabus$}) ||
 1278:              ($resurl =~ m{^/adm/$match_domain/$match_username/aboutme$}))) {
 1279:             my @folders=split('&',$env{'form.folderpath'});
 1280:             if ((@folders > 2) || ($resurl ne '/adm/supplemental')) {
 1281:                 my $suppanchor;
 1282:                 if ($resurl =~ m{^/adm/wrapper/ext/}) {
 1283:                     $suppanchor = $env{'form.anchor'};
 1284:                 }
 1285:                 my $esc_path=&escape(&HTML::Entities::encode(&escape($env{'form.folderpath'}),'<>&"'));
 1286:                 my $link = '/adm/coursedocs?command=direct&amp;forcesupplement=1&amp;supppath='.
 1287:                            "$esc_path&amp;anchor=$suppanchor";
 1288:                 if ($env{'request.use_absolute'} ne '') {
 1289:                     $link = $env{'request.use_absolute'}.$link;
 1290:                 }
 1291:                 &switch('','',7,4,'docs-22x22.png','Edit Folder','parms[_2]',
 1292:                         "location.href='$link'",'Folder/Page Content');
 1293:             }
 1294:         }
 1295:     }
 1296: 
 1297: # End checking for items for about me page for users in a course
 1298:     if ($docscrumbs) {
 1299:         &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1300:         &advtools_crumbs(@inlineremote);
 1301:         return $editbutton;
 1302:     } elsif ($env{'request.registered'}) {
 1303:         return $editbutton || $viewsrcbutton;
 1304:     } else {
 1305:         if (ref($bread_crumbs) eq 'ARRAY') {
 1306:             if (@inlineremote > 0) {
 1307:                 if (ref($advtools) eq 'ARRAY') {
 1308:                     @{$advtools} = @inlineremote;
 1309:                 }
 1310:             }
 1311:             return;
 1312:         } elsif (@inlineremote > 0) {
 1313:             &Apache::lonhtmlcommon::clear_breadcrumb_tools();
 1314:             &advtools_crumbs(@inlineremote);
 1315:             return   &Apache::lonhtmlcommon::scripttag('', 'start')
 1316:                    . &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0)
 1317:                    . &Apache::lonhtmlcommon::scripttag('', 'end');
 1318:         }
 1319:     }
 1320: }
 1321: 
 1322: sub advtools_crumbs {
 1323:     my @funcs = @_;
 1324:     if ($env{'request.noversionuri'} =~ m{^/adm/$match_domain/$match_username/aboutme$}) {
 1325:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1326:             'advtools', @funcs[61,64,65,66,67,74]);
 1327:     } elsif ($env{'request.noversionuri'} !~ m{^/adm/(navmaps|viewclasslist)(\?|$)}) {
 1328:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1329:             'advtools', @funcs[61,71,72,73,74,92]);
 1330:     } elsif ($env{'request.noversionuri'} eq '/adm/viewclasslist') {
 1331:         &Apache::lonhtmlcommon::add_breadcrumb_tool(
 1332:             'advtools', $funcs[61]);
 1333:     }
 1334:     return;
 1335: }
 1336: 
 1337: # ================================================================== Raw Config
 1338: 
 1339: sub clear {
 1340:     my ($row,$col)=@_;
 1341:     $inlineremote[10*$row+$col]='';
 1342:     return ''; 
 1343: }
 1344: 
 1345: # ============================================ Switch a button or create a link
 1346: # Switch acts on the javascript that is executed when a button is clicked.  
 1347: # The javascript is usually similar to "go('/adm/roles')" or "cstrgo(..)".
 1348: 
 1349: sub switch {
 1350:     my ($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat,$nobreak)=@_;
 1351:     $act=~s/\$uname/$uname/g;
 1352:     $act=~s/\$udom/$udom/g;
 1353:     $top=&mt($top);
 1354:     $bot=&mt($bot);
 1355:     $desc=&mt($desc);
 1356:     my $idx=10*$row+$col;
 1357:     $category_members{$cat}.=':'.$idx;
 1358: 
 1359: # Inline Menu
 1360:     if ($nobreak==2) { return ''; }
 1361:     my $text=$top.' '.$bot;
 1362:     $text=~s/\s*\-\s*//gs;
 1363: 
 1364:     my $pic=
 1365: 	   '<img alt="'.$text.'" src="'.
 1366: 	   &Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$img).
 1367: 	   '" align="'.($nobreak==3?'right':'left').'" class="LC_icon" />';
 1368:     if ($env{'browser.interface'} eq 'faketextual') {
 1369: # Main Menu
 1370: 	   if ($nobreak==3) {
 1371: 	       $inlineremote[$idx]="\n".
 1372: 		   '<td class="LC_menubuttons_text" align="right">'.$text.
 1373: 		   '</td><td align="left">'.
 1374: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td></tr>';
 1375: 	   } elsif ($nobreak) {
 1376: 	       $inlineremote[$idx]="\n<tr>".
 1377: 		   '<td align="left">'.
 1378: 		   '<a href="javascript:'.$act.';">'.$pic.'</a></td>
 1379:                     <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>';
 1380: 	   } else {
 1381: 	       $inlineremote[$idx]="\n<tr>".
 1382: 		   '<td align="left">'.
 1383: 		   '<a href="javascript:'.$act.';">'.$pic.
 1384: 		   '</a></td><td class="LC_menubuttons_text" colspan="3">'.
 1385: 		   '<a class="LC_menubuttons_link" href="javascript:'.$act.';"><span class="LC_menubuttons_inline_text">'.$desc.'</span></a></td></tr>';
 1386: 	   }
 1387:     } else {
 1388: # Inline Menu
 1389:         my @tools = (93,91,81,82,83);
 1390:         unless ($env{'request.state'} eq 'construct') {
 1391:             push(@tools,63);
 1392:         }
 1393:         if (($env{'environment.icons'} eq 'iconsonly') && 
 1394:             (grep(/^$idx$/,@tools))) {
 1395:             $inlineremote[$idx] =
 1396:         '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.'</a>';
 1397:         } else {
 1398:             $inlineremote[$idx] =
 1399:        '<a title="'.$desc.'" class="LC_menubuttons_link" href="javascript:'.$act.';">'.$pic.
 1400:        '<span class="LC_menubuttons_inline_text">'.$top.'&nbsp;</span></a>';
 1401:         }
 1402:     }
 1403:     return '';
 1404: }
 1405: 
 1406: sub secondlevel {
 1407:     my $output='';
 1408:     my 
 1409:     ($uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat)=@_;
 1410:     if ($prt eq 'any') {
 1411: 	   $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1412:     } elsif ($prt=~/^r(\w+)/) {
 1413:         if ($rol eq $1) {
 1414:            $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1415:         }
 1416:     }
 1417:     return $output;
 1418: }
 1419: 
 1420: sub inlinemenu {
 1421:     undef(@inlineremote);
 1422:     undef(%category_members);
 1423: # calling rawconfig with "1" will evaluate mydesk.tab, even if there is no active remote control
 1424:     &rawconfig(1);
 1425:     my $output='<table><tr>';
 1426:     for (my $col=1; $col<=2; $col++) {
 1427:         $output.='<td class="LC_mainmenu_col_fieldset">';
 1428:         for (my $row=1; $row<=8; $row++) {
 1429:             foreach my $cat (keys(%category_members)) {
 1430:                if ($category_positions{$cat} ne "$col,$row") { next; }
 1431:                #$output.='<table><tr><td colspan="4" class="LC_menubuttons_category">'.&mt($category_names{$cat}).'</td></tr>';
 1432:                $output.='<div class="LC_Box LC_400Box">';
 1433: 	       $output.='<h3 class="LC_hcell">'.&mt($category_names{$cat}).'</h3>';
 1434:                $output.='<table>';
 1435:                my %active=();
 1436:                foreach my $menu_item (split(/\:/,$category_members{$cat})) {
 1437:                   if ($inlineremote[$menu_item]) {
 1438:                      $active{$menu_item}=1;
 1439:                   }
 1440:                }  
 1441:                foreach my $item (sort(keys(%active))) {
 1442:                   $output.=$inlineremote[$item];
 1443:                }
 1444:                $output.='</table>';
 1445:                $output.='</div>';
 1446:             }
 1447:          }
 1448:          $output.="</td>";
 1449:     }
 1450:     $output.="</tr></table>";
 1451:     return $output;
 1452: }
 1453: 
 1454: sub rawconfig {
 1455: #
 1456: # This evaluates mydesk.tab
 1457: # Need to add more positions and more privileges to deal with all
 1458: # menu items.
 1459: #
 1460:     my $textualoverride=shift;
 1461:     my $output='';
 1462:     return '' unless $textualoverride;
 1463:     my $uname=$env{'user.name'};
 1464:     my $udom=$env{'user.domain'};
 1465:     my $adv=$env{'user.adv'};
 1466:     my $show_course=&Apache::loncommon::show_course();
 1467:     my $author=$env{'user.author'};
 1468:     my $crs='';
 1469:     my $crstype='';
 1470:     if ($env{'request.course.id'}) {
 1471:        $crs='/'.$env{'request.course.id'};
 1472:        if ($env{'request.course.sec'}) {
 1473: 	   $crs.='_'.$env{'request.course.sec'};
 1474:        }
 1475:        $crs=~s/\_/\//g;
 1476:        $crstype = &Apache::loncommon::course_type();
 1477:     }
 1478:     my $pub=($env{'request.state'} eq 'published');
 1479:     my $con=($env{'request.state'} eq 'construct');
 1480:     my $rol=$env{'request.role'};
 1481:     my $requested_domain;
 1482:     if ($rol) {
 1483:        $requested_domain = $env{'request.role.domain'};
 1484:     }
 1485:     foreach my $line (@desklines) {
 1486:         my ($row,$col,$pro,$prt,$img,$top,$bot,$act,$desc,$cat)=split(/\:/,$line);
 1487:         $prt=~s/\$uname/$uname/g;
 1488:         $prt=~s/\$udom/$udom/g;
 1489:         if ($prt =~ /\$crs/) {
 1490:             next unless ($env{'request.course.id'});
 1491:             next if ($crstype eq 'Community');
 1492:             $prt=~s/\$crs/$crs/g;
 1493:         } elsif ($prt =~ /\$cmty/) {
 1494:             next unless ($env{'request.course.id'});
 1495:             next if ($crstype ne 'Community');
 1496:             $prt=~s/\$cmty/$crs/g;
 1497:         }
 1498:         if ($prt =~ m/\$requested_domain/) {
 1499:             if ((!$requested_domain) && ($pro eq 'pbre') && ($env{'user.adv'})) {
 1500:                 $prt=~s/\$requested_domain/$env{'user.domain'}/g;
 1501:             } else {
 1502:                 $prt=~s/\$requested_domain/$requested_domain/g;
 1503:             }
 1504:         }
 1505:         if ($category_names{$cat}!~/\w/) { $cat='oth'; }
 1506:         if ($pro eq 'clear') {
 1507: 	    $output.=&clear($row,$col);
 1508:         } elsif ($pro eq 'any') {
 1509:                $output.=&secondlevel(
 1510: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1511: 	} elsif ($pro eq 'smp') {
 1512:             unless ($adv) {
 1513:                $output.=&secondlevel(
 1514:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1515:             }
 1516:         } elsif ($pro eq 'adv') {
 1517:             if ($adv) {
 1518:                $output.=&secondlevel(
 1519: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1520:             }
 1521: 	} elsif ($pro eq 'shc') {
 1522:             if ($show_course) {
 1523:                $output.=&secondlevel(
 1524:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1525:             }
 1526:         } elsif ($pro eq 'nsc') {
 1527:             if (!$show_course) {
 1528:                $output.=&secondlevel(
 1529: 	  $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1530:             }
 1531:         } elsif (($pro=~/^p(\w+)/) && ($prt)) {
 1532:             my $priv = $1;
 1533:             if ($priv =~ /^mdc(Course|Community)/) {
 1534:                 if ($crstype eq $1) {
 1535:                     $priv = 'mdc';
 1536:                 } else {
 1537:                     next;
 1538:                 }
 1539:             }
 1540:             if ((($priv eq 'bre') && (&Apache::lonnet::allowed($priv,$prt) eq 'F')) ||
 1541:                 (($priv ne 'bre') && (&Apache::lonnet::allowed($priv,$prt)))) {
 1542:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1543:             }
 1544:         } elsif ($pro eq 'course')  {
 1545:             if (($env{'request.course.fn'}) && ($crstype ne 'Community')) {
 1546:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1547: 	    }
 1548:         } elsif ($pro eq 'community')  {
 1549:             if (($env{'request.course.fn'}) && ($crstype eq 'Community')) {
 1550:                $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1551:             }
 1552:         } elsif ($pro =~ /^courseenv_(.*)$/) {
 1553:             my $key = $1;
 1554:             if ($crstype ne 'Community') {
 1555:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 1556:                 if ($key eq 'canuse_pdfforms') {
 1557:                     if ($env{'request.course.id'} && $coursepref eq '') {
 1558:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 1559:                         $coursepref = $domdefs{'canuse_pdfforms'};
 1560:                     }
 1561:                 }
 1562:                 if ($coursepref) { 
 1563:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1564:                 }
 1565:             }
 1566:         } elsif ($pro =~ /^communityenv_(.*)$/) {
 1567:             my $key = $1;
 1568:             if ($crstype eq 'Community') {
 1569:                 my $coursepref = $env{'course.'.$env{'request.course.id'}.'.'.$key};
 1570:                 if ($key eq 'canuse_pdfforms') {
 1571:                     if ($env{'request.course.id'} && $coursepref eq '') {
 1572:                         my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
 1573:                         $coursepref = $domdefs{'canuse_pdfforms'};
 1574:                     }
 1575:                 }
 1576:                 if ($coursepref) { 
 1577:                     $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1578:                 }
 1579:             }
 1580:         } elsif ($pro =~ /^course_(.*)$/) {
 1581:             # Check for permissions inside of a course
 1582:             if (($env{'request.course.id'}) && ($crstype ne 'Community') && 
 1583:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 1584:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 1585:                  )) {
 1586:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1587: 	    }
 1588:         } elsif ($pro =~ /^community_(.*)$/) {
 1589:             # Check for permissions inside of a community
 1590:             if (($env{'request.course.id'}) && ($crstype eq 'Community') &&   
 1591:                 (&Apache::lonnet::allowed($1,$env{'request.course.id'}.
 1592:             ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
 1593:                  )) {
 1594:                 $output.=&switch($uname,$udom,$row,$col,$img,$top,$bot,$act,$desc,$cat);
 1595:             }
 1596:         } elsif ($pro eq 'author') {
 1597:             if ($author) {
 1598:                 if ((($prt eq 'rca') && ($env{'request.role'}=~/^ca/)) ||
 1599:                     (($prt eq 'raa') && ($env{'request.role'}=~/^aa/)) || 
 1600:                     (($prt eq 'rau') && ($env{'request.role'}=~/^au/))) {
 1601:                     # Check that we are on the correct machine
 1602:                     my $cadom=$requested_domain;
 1603:                     my $caname=$env{'user.name'};
 1604:                     if (($prt eq 'rca') || ($prt eq 'raa')) {
 1605: 		       ($cadom,$caname)=
 1606:                                ($env{'request.role'}=~/($match_domain)\/($match_username)$/);
 1607:                     }                       
 1608:                     $act =~ s/\$caname/$caname/g;
 1609:                     $act =~ s/\$cadom/$cadom/g;
 1610:                     my $home = &Apache::lonnet::homeserver($caname,$cadom);
 1611: 		    my $allowed=0;
 1612: 		    my @ids=&Apache::lonnet::current_machine_ids();
 1613: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 1614: 		    if ($allowed) {
 1615:                         $output.=&switch($caname,$cadom,
 1616:                                         $row,$col,$img,$top,$bot,$act,$desc,$cat);
 1617:                     }
 1618:                 }
 1619:             }
 1620:         } elsif ($pro eq 'tools') {
 1621:             my @tools = ('aboutme','blog','portfolio');
 1622:             if (grep(/^\Q$prt\E$/,@tools)) {
 1623:                 if (!&Apache::lonnet::usertools_access($env{'user.name'},
 1624:                                                        $env{'user.domain'},
 1625:                                                        $prt,undef,'tools')) {
 1626:                     $output.=&clear($row,$col);
 1627:                     next;
 1628:                 }
 1629:             } elsif (($prt eq 'reqcrsnsc') || ($prt eq 'reqcrsshc')) {
 1630:                 if (($prt eq 'reqcrsnsc') && ($show_course))   {
 1631:                     next;
 1632:                 }
 1633:                 if (($prt eq 'reqcrsshc') && (!$show_course)) {
 1634:                     next;
 1635:                 }
 1636:                 my $showreqcrs = &check_for_rcrs();
 1637:                 if (!$showreqcrs) {
 1638:                     $output.=&clear($row,$col);
 1639:                     next;
 1640:                 }
 1641:             }
 1642:             $prt='any';
 1643:             $output.=&secondlevel(
 1644:           $uname,$udom,$rol,$crs,$pub,$con,$row,$col,$prt,$img,$top,$bot,$act,$desc,$cat);
 1645:         }
 1646:     }
 1647:     return $output;
 1648: }
 1649: 
 1650: sub check_for_rcrs {
 1651:     my $showreqcrs = 0;
 1652:     my @reqtypes = ('official','unofficial','community','textbook','placement');
 1653:     foreach my $type (@reqtypes) {
 1654:         if (&Apache::lonnet::usertools_access($env{'user.name'},
 1655:                                               $env{'user.domain'},
 1656:                                               $type,undef,'requestcourses')) {
 1657:             $showreqcrs = 1;
 1658:             last;
 1659:         }
 1660:     }
 1661:     if (!$showreqcrs) {
 1662:         foreach my $type (@reqtypes) {
 1663:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
 1664:                 $showreqcrs = 1;
 1665:                 last;
 1666:             }
 1667:         }
 1668:     }
 1669:     return $showreqcrs;
 1670: }
 1671: 
 1672: sub dc_popup_js {
 1673:     my %lt = &Apache::lonlocal::texthash(
 1674:                                           more => '(More ...)',
 1675:                                           less => '(Less ...)',
 1676:                                         );
 1677:     return <<"END";
 1678: 
 1679: function showCourseID() {
 1680:     document.getElementById('dccid').style.display='block';
 1681:     document.getElementById('dccid').style.textAlign='left';
 1682:     document.getElementById('dccid').style.textFace='normal';
 1683:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:hideCourseID();" class="LC_menubuttons_link">$lt{'less'}</a>';
 1684:     return;
 1685: }
 1686: 
 1687: function hideCourseID() {
 1688:     document.getElementById('dccid').style.display='none';
 1689:     document.getElementById('dccidtext').innerHTML ='<a href="javascript:showCourseID()" class="LC_menubuttons_link">$lt{'more'}</a>';
 1690:     return;
 1691: }
 1692: 
 1693: END
 1694: 
 1695: }
 1696: 
 1697: sub countdown_toggle_js {
 1698:     return <<"END";
 1699: 
 1700: function toggleCountdown() {
 1701:     var countdownid = document.getElementById('duedatecountdown');
 1702:     var currstyle = countdownid.style.display;
 1703:     if (currstyle == 'inline') {
 1704:         countdownid.style.display = 'none';
 1705:         document.getElementById('ddcountcollapse').innerHTML='';
 1706:         document.getElementById('ddcountexpand').innerHTML='&#9668;&nbsp;';
 1707:     } else {
 1708:         countdownid.style.display = 'inline';
 1709:         document.getElementById('ddcountcollapse').innerHTML='&#9658;&nbsp;';
 1710:         document.getElementById('ddcountexpand').innerHTML='';
 1711:     }
 1712:     return;
 1713: }
 1714: 
 1715: END
 1716: }
 1717: 
 1718: # This creates a "done button" for timed events.  The confirmation box is a jQuery
 1719: # dialog widget. If the interval parameter requires a proctor key for the timed 
 1720: # event to be marked done, there will also be a textbox where that can be entered. 
 1721: # Clicking OK will set the value of LC_interval_done to 'true', and, if needed will 
 1722: # set the value of LC_interval_done_proctorpass to the text entered in that box, 
 1723: # and submit the corresponding form.
 1724: # 
 1725: # The &zero_time() routine in lonhomework.pm is called when a page is rendered if
 1726: # LC_interval_done is true.
 1727: #
 1728: sub done_button_js {
 1729:     my ($type,$width,$height,$proctor,$donebuttontext) = @_;
 1730:     return unless (($type eq 'map') || ($type eq 'resource'));
 1731:     my %lt = &Apache::lonlocal::texthash(
 1732:                  title    => 'WARNING!',
 1733:                  preamble => 'You are trying to end this timed event early.',
 1734:                  map      => 'Confirming that you are done will cause the time to expire and prevent you from changing any answers in the current folder.',
 1735:                  resource => 'Confirming that you are done will cause the time to expire for this question, and prevent you from changing your answer(s).', 
 1736:                  okdone   => 'Click "OK" if you are completely finished.',
 1737:                  cancel   => 'Click "Cancel" to continue working.',
 1738:                  proctor  => 'Ask a proctor to enter the key, then click "OK" if you are completely finished.',
 1739:                  ok       => 'OK',
 1740:                  exit     => 'Cancel',
 1741:                  key      => 'Key:',
 1742:                  nokey    => 'A proctor key is required', 
 1743:     );
 1744:     my $navmap = Apache::lonnavmaps::navmap->new(); 
 1745:     my ($missing,$tried) = (0,0);
 1746:     if (ref($navmap)) {
 1747:         my @resources=();
 1748:         if ($type eq 'map') {
 1749:             my ($mapurl,$rid,$resurl)=&Apache::lonnet::decode_symb($env{'request.symb'});
 1750:             @resources=$navmap->retrieveResources($mapurl,sub { $_[0]->is_problem() });
 1751:         } else {
 1752:             my $res = $navmap->getBySymb($env{'request.symb'});
 1753:             if (ref($res)) {
 1754:                 if ($res->is_problem()) {
 1755:                     push(@resources,$res);
 1756:                 }
 1757:             }
 1758:         }
 1759:         foreach my $res (@resources) {
 1760:             if (ref($res->parts()) eq 'ARRAY') {
 1761:                 foreach my $part (@{$res->parts()}) {
 1762:                     if (!$res->tries($part)) {
 1763:                         $missing++;
 1764:                     } else {
 1765:                         $tried++;
 1766:                     }
 1767:                 }
 1768:             }
 1769:         }
 1770:     }
 1771:     if ($missing) {
 1772:         $lt{'miss'} .= '<p class="LC_error">';
 1773:         if ($type eq 'map') {
 1774:             $lt{'miss'} .= &mt('Submissions are missing for [quant,_1,question part,question parts] in this folder.',$missing);
 1775:         } else {
 1776:             $lt{'miss'} .= &mt('Submissions are missing for [quant,_1,part] in this question.',$missing);
 1777:         }
 1778:         if ($missing > 1) {
 1779:             $lt{'miss'} .= ' '.&mt('If you confirm you are done you will be unable to submit answers for them.').'</span>';
 1780:         } else {
 1781:             $lt{'miss'} .= ' '.&mt('If you confirm you are done you will be unable to submit an answer for it.').'</p>';
 1782:         }
 1783:     }
 1784:     $donebuttontext = &HTML::Entities::encode($donebuttontext,'<>&"');
 1785:     if ($proctor) {
 1786:         if ($height !~ /^\d+$/) {
 1787:             $height = 400;
 1788:             if ($missing) {
 1789:                 $height += 60; 
 1790:             }
 1791:         }
 1792:         if ($width !~ /^\d+$/) {
 1793:             $width = 400;
 1794:             if ($missing) {
 1795:                 $width += 60;
 1796:             }
 1797:         }
 1798:         return <<END;
 1799: <form method="post" name="LCdoneButton" action="">
 1800:     <input type="hidden" name="LC_interval_done" value="" />
 1801:     <input type="hidden" name="LC_interval_done_proctorpass" value="" />
 1802:     <button id="LC_done-confirm-opener" type="button">$donebuttontext</button>
 1803: </form>
 1804: 
 1805: <div id="LC_done-confirm" title="$lt{'title'}">
 1806:   <p>$lt{'preamble'} $lt{$type}</p>
 1807:   $lt{'miss'}
 1808:   <p>$lt{'proctor'}</p>
 1809:   <form name="LCdoneButtonProctor" action="">
 1810:     <label>$lt{'key'}<input type="password" name="LC_interval_done_proctorkey" value="" /></label>
 1811:     <input type="submit" tabindex="-1" style="position:absolute; top:-1000px" />
 1812:   </form>
 1813:   <p>$lt{'cancel'}</p>
 1814: </div>
 1815: 
 1816: <script type="text/javascript">
 1817: // <![CDATA[
 1818:     \$( "#LC_done-confirm" ).dialog({ autoOpen: false });
 1819:     \$( "#LC_done-confirm-opener" ).on("click", function() {
 1820:         \$( "#LC_done-confirm" ).dialog("open");
 1821:         \$( "#LC_done-confirm" ).dialog({
 1822:             height: $height,
 1823:             width: $width,
 1824:             modal: true,
 1825:             resizable: false,
 1826:             buttons: [
 1827:                 {
 1828:                     text: "$lt{'ok'}",
 1829:                     click: function() {
 1830:                         var proctorkey = \$( '[name="LC_interval_done_proctorkey"]' )[0].value;
 1831:                         if ((proctorkey == '') || (proctorkey == null)) {
 1832:                             alert("$lt{'nokey'}"); 
 1833:                         } else { 
 1834:                             \$( '[name="LC_interval_done"]' )[0].value = 'true';
 1835:                             \$( '[name="LC_interval_done_proctorpass"]' )[0].value = proctorkey;
 1836:                             \$( '[name="LCdoneButton"]' )[0].submit();
 1837:                         }
 1838:                     },
 1839:                 },
 1840:                 {
 1841:                     text: "$lt{'exit'}",
 1842:                     click: function() {
 1843:                         \$("#LC_done-confirm").dialog( "close" );
 1844:                     }
 1845:                 }
 1846:             ],
 1847:             close: function() {
 1848:                 \$( '[name="LC_interval_done_proctorkey"]' )[0].value = '';
 1849:             }
 1850:         });
 1851:         \$( "#LC_done-confirm" ).find( "form" ).on( "submit", function( event ) {
 1852:             event.preventDefault();
 1853:             \$( '[name="LC_interval_done"]' )[0].value = 'true';
 1854:             \$( '[name="LC_interval_done_proctorpass"]' )[0].value = \$( '[name="LC_interval_done_proctorkey"]' )[0].value;
 1855:             \$( '[name="LCdoneButton"]' )[0].submit();
 1856:         });
 1857: });
 1858: 
 1859: // ]]>
 1860: </script>
 1861: 
 1862: END
 1863:     } else {
 1864:         if ($height !~ /^\d+$/) {
 1865:             $height = 320;
 1866:             if ($missing) {
 1867:                 $height += 60;
 1868:             }
 1869:         }
 1870:         if ($width !~ /^\d+$/) {
 1871:             $width = 320;
 1872:             if ($missing) {
 1873:                 $width += 60;
 1874:             }
 1875:         }
 1876:         if ($missing) {
 1877:             $lt{'miss'} = '</p>'.$lt{'miss'}.'<p>';
 1878:         }
 1879:         return <<END;
 1880: 
 1881: <form method="post" name="LCdoneButton" action="">
 1882:     <input type="hidden" name="LC_interval_done" value="" />
 1883:     <button id="LC_done-confirm-opener" type="button">$donebuttontext</button>
 1884: </form>
 1885: 
 1886: <div id="LC_done-confirm" title="$lt{'title'}">
 1887:     <p>$lt{'preamble'} $lt{$type} $lt{'miss'} $lt{'okdone'} $lt{'cancel'}</p>
 1888: </div>
 1889: 
 1890: <script type="text/javascript">
 1891: // <![CDATA[
 1892: \$( "#LC_done-confirm" ).dialog({ autoOpen: false });
 1893: \$( "#LC_done-confirm-opener" ).click(function() {
 1894:     \$( "#LC_done-confirm" ).dialog( "open" );
 1895:     \$( "#LC_done-confirm" ).dialog({
 1896:       resizable: false,
 1897:       height: $height,
 1898:       width: $width,
 1899:       modal: true,
 1900:       buttons: [
 1901:                  {
 1902:                     text: "$lt{'ok'}",
 1903:                     click: function() {
 1904:                         \$( this ).dialog( "close" );
 1905:                         \$( '[name="LC_interval_done"]' )[0].value = 'true';
 1906:                         \$( '[name="LCdoneButton"]' )[0].submit();
 1907:                     },
 1908:                  },
 1909:                  {
 1910:                      text: "$lt{'exit'}",
 1911:                      click: function() {
 1912:                          \$( this ).dialog( "close" );
 1913:                      },
 1914:                   },
 1915:                ],
 1916:        });
 1917: });
 1918: // ]]>
 1919: </script>
 1920: 
 1921: END
 1922:     }
 1923: }
 1924: 
 1925: sub utilityfunctions {
 1926:     my ($httphost) = @_;
 1927:     my $currenturl=&Apache::lonnet::clutter(&Apache::lonnet::fixversion((split(/\?/,$env{'request.noversionuri'}))[0]));
 1928:     if ($currenturl =~ m{^/adm/wrapper/ext/}
 1929:         && $env{'request.external.querystring'} ) {
 1930:             $currenturl .= ($currenturl=~/\?/)?'&':'?'.$env{'request.external.querystring'};
 1931:     }
 1932:     $currenturl=&Apache::lonenc::check_encrypt(&unescape($currenturl));
 1933:     
 1934:     my $currentsymb=&Apache::lonenc::check_encrypt($env{'request.symb'});
 1935: 
 1936:     my $dc_popup_cid;
 1937:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
 1938:                         $env{'course.'.$env{'request.course.id'}.
 1939:                                  '.domain'}.'/'})) {
 1940:         $dc_popup_cid = &dc_popup_js();
 1941:     }
 1942: 
 1943:     my $start_page_annotate = 
 1944:         &Apache::loncommon::start_page('Annotator',undef,
 1945: 				       {'only_body' => 1,
 1946: 					'js_ready'  => 1,
 1947: 					'bgcolor'   => '#BBBBBB',
 1948: 					'add_entries' => {
 1949: 					    'onload' => 'javascript:document.goannotate.submit();'}});
 1950: 
 1951:     my $end_page_annotate = 
 1952:         &Apache::loncommon::end_page({'js_ready' => 1});
 1953: 
 1954:     my $jumptores = &Apache::lonhtmlcommon::javascript_jumpto_resource();
 1955: 
 1956:     my $esc_url=&escape($currenturl);
 1957:     my $esc_symb=&escape($currentsymb);
 1958: 
 1959:     my $countdown = &countdown_toggle_js();
 1960: 
 1961:     my $hostvar = '
 1962: function setLCHost() {
 1963:     var lcHostname="";
 1964: ';
 1965:     if ($httphost =~ m{^https?\://}) {
 1966:         $hostvar .= '    var lcServer="'.$httphost.'";'."\n".
 1967:                     '    var hostReg = /^https?:\/\/([^\/]+)$/i;'."\n".
 1968:                     '    var match = hostReg.exec(lcServer);'."\n".
 1969:                     '    if (match.length) {'."\n".
 1970:                     '        if (match[1] == location.hostname) {'."\n".
 1971:                     '            lcHostname=lcServer;'."\n".
 1972:                     '        }'."\n".
 1973:                     '    }'."\n";
 1974:     }
 1975:     
 1976:     $hostvar .= '    return lcHostname;'."\n".
 1977: '}'."\n";
 1978: 
 1979: return (<<ENDUTILITY)
 1980:     $hostvar
 1981:     var currentURL=unescape("$esc_url");
 1982:     var reloadURL=unescape("$esc_url");
 1983:     var currentSymb=unescape("$esc_symb");
 1984: 
 1985: $dc_popup_cid
 1986: 
 1987: $jumptores
 1988: 
 1989: function gopost(url,postdata) {
 1990:    if (url!='') {
 1991:       var lcHostname = setLCHost();
 1992:       this.document.server.action=lcHostname+url;
 1993:       this.document.server.postdata.value=postdata;
 1994:       this.document.server.command.value='';
 1995:       this.document.server.url.value='';
 1996:       this.document.server.symb.value='';
 1997:       this.document.server.submit();
 1998:    }
 1999: }
 2000: 
 2001: function gocmd(url,cmd) {
 2002:    if (url!='') {
 2003:       var lcHostname = setLCHost();
 2004:       this.document.server.action=lcHostname+url;
 2005:       this.document.server.postdata.value='';
 2006:       this.document.server.command.value=cmd;
 2007:       this.document.server.url.value=currentURL;
 2008:       this.document.server.symb.value=currentSymb;
 2009:       this.document.server.submit();
 2010:    }
 2011: }
 2012: 
 2013: function gocstr(url,filename) {
 2014:     if (url == '/adm/cfile?action=delete') {
 2015:         this.document.cstrdelete.filename.value = filename
 2016:         this.document.cstrdelete.submit();
 2017:         return;
 2018:     }
 2019:     if (url == '/adm/printout') {
 2020:         this.document.cstrprint.postdata.value = filename
 2021:         this.document.cstrprint.curseed.value = 0;
 2022:         this.document.cstrprint.problemtype.value = 0;
 2023:         if (this.document.lonhomework) {
 2024:             if ((this.document.lonhomework.rndseed) && (this.document.lonhomework.rndseed.value != null) && (this.document.lonhomework.rndseed.value != '')) {
 2025:                 this.document.cstrprint.curseed.value = this.document.lonhomework.rndseed.value
 2026:             }
 2027:             if (this.document.lonhomework.problemtype) {
 2028: 		if (this.document.lonhomework.problemtype.value) {
 2029: 		    this.document.cstrprint.problemtype.value = 
 2030: 			this.document.lonhomework.problemtype.value;
 2031: 		} else if (this.document.lonhomework.problemtype.options) {
 2032: 		    for (var i=0; i<this.document.lonhomework.problemtype.options.length; i++) {
 2033: 			if (this.document.lonhomework.problemtype.options[i].selected) {
 2034: 			    if (this.document.lonhomework.problemtype.options[i].value != null && this.document.lonhomework.problemtype.options[i].value != '') { 
 2035: 				this.document.cstrprint.problemtype.value = this.document.lonhomework.problemtype.options[i].value
 2036: 				}
 2037: 			}
 2038: 		    }
 2039: 		}
 2040: 	    }
 2041: 	}
 2042:         this.document.cstrprint.submit();
 2043:         return;
 2044:     }
 2045:     if (url !='') {
 2046:         this.document.constspace.filename.value = filename;
 2047:         this.document.constspace.action = url;
 2048:         this.document.constspace.submit();
 2049:     }
 2050: }
 2051: 
 2052: function golist(url) {
 2053:    if (url!='' && url!= null) {
 2054:        currentURL = null;
 2055:        currentSymb= null;
 2056:        var lcHostname = setLCHost();
 2057:        top.location.href=lcHostname+url;
 2058:    }
 2059: }
 2060: 
 2061: 
 2062: 
 2063: function catalog_info(isMobile) {
 2064:     if (isMobile == 1) {
 2065:         openMyModal(window.location.pathname+'.meta?modal=1',500,400,'yes');
 2066:     } else {
 2067:         loncatinfo=window.open(window.location.pathname+'.meta',"LONcatInfo",'height=500,width=400,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 2068:     }
 2069: }
 2070: 
 2071: function chat_win() {
 2072:    var lcHostname = setLCHost();
 2073:    lonchat=window.open(lcHostname+'/res/adm/pages/chatroom.html',"LONchat",'height=320,width=480,resizable=yes,location=no,menubar=no,toolbar=no');
 2074: }
 2075: 
 2076: function group_chat(group) {
 2077:    var lcHostname = setLCHost();
 2078:    var url = lcHostname+'/adm/groupchat?group='+group;
 2079:    var winName = 'LONchat_'+group;
 2080:    grpchat=window.open(url,winName,'height=320,width=280,resizable=yes,location=no,menubar=no,toolbar=no');
 2081: }
 2082: 
 2083: function annotate() {
 2084:    w_Annotator_flag=1;
 2085:    annotator=window.open('','Annotator','width=365,height=265,scrollbars=0');
 2086:    annotator.document.write(
 2087:    '$start_page_annotate'
 2088:   +"<form name='goannotate' target='Annotator' method='post' "
 2089:   +"action='/adm/annotations'>"
 2090:   +"<input type='hidden' name='symbnew' value='"+currentSymb+"' />"
 2091:   +"<\\/form>"
 2092:   +'$end_page_annotate');
 2093:    annotator.document.close();
 2094: }
 2095: 
 2096: function open_StoredLinks_Import(rat) {
 2097:    var newWin;
 2098:    var lcHostname = setLCHost();
 2099:    if (rat) {
 2100:        newWin = window.open(lcHostname+'/adm/wishlist?inhibitmenu=yes&mode=import&rat='+rat,
 2101:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 2102:    }
 2103:    else {
 2104:        newWin = window.open(lcHostname+'/adm/wishlist?inhibitmenu=yes&mode=import',
 2105:                             'wishlistImport','scrollbars=1,resizable=1,menubar=0');
 2106:    }
 2107:    newWin.focus();
 2108: }
 2109: 
 2110: function open_source() {
 2111:    var url = escape(window.location.pathname);
 2112:    sourcewin=window.open('/adm/source?inhibitmenu=yes&viewonly=1&filename='+url,'LONsource',
 2113:                          'height=500,width=600,resizable=yes,location=no,menubar=no,toolbar=no,scrollbars=yes');
 2114: }
 2115: 
 2116: (function (\$) {
 2117:   \$(document).ready(function () {
 2118:     \$.single=function(a){return function(b){a[0]=b;return a}}(\$([1]));
 2119:     /*\@cc_on
 2120:       if (!window.XMLHttpRequest) {
 2121:         \$('.LC_hoverable').each(function () {
 2122:           this.attachEvent('onmouseenter', function (evt) { \$.single(evt.srcElement).addClass('hover'); });
 2123:           this.attachEvent('onmouseleave', function (evt) { \$.single(evt.srcElement).removeClass('hover'); });
 2124:         });
 2125:       }
 2126:     \@*/
 2127:   });
 2128: }(jQuery));
 2129: 
 2130: $countdown
 2131: 
 2132: ENDUTILITY
 2133: }
 2134: 
 2135: sub serverform {
 2136:     return(<<ENDSERVERFORM);
 2137: <form name="server" action="/adm/logout" method="post" target="_top">
 2138: <input type="hidden" name="postdata" value="none" />
 2139: <input type="hidden" name="command" value="none" />
 2140: <input type="hidden" name="url" value="none" />
 2141: <input type="hidden" name="symb" value="none" />
 2142: </form>
 2143: ENDSERVERFORM
 2144: }
 2145: 
 2146: sub constspaceform {
 2147:     return(<<ENDCONSTSPACEFORM);
 2148: <form name="constspace" action="/adm/logout" method="post" target="_top">
 2149: <input type="hidden" name="filename" value="" />
 2150: </form>
 2151: <form name="cstrdelete" action="/adm/cfile" method="post" target="_top">
 2152: <input type="hidden" name="action" value="delete" /> 
 2153: <input type="hidden" name="filename" value="" />
 2154: </form>
 2155: <form name="cstrprint" action="/adm/printout" target="_parent" method="post">
 2156: <input type="hidden" name="postdata" value="" />
 2157: <input type="hidden" name="curseed" value="" />
 2158: <input type="hidden" name="problemtype" value="" />
 2159: </form>
 2160: 
 2161: ENDCONSTSPACEFORM
 2162: }
 2163: 
 2164: sub hidden_button_check {
 2165:     if ( $env{'request.course.id'} eq ''
 2166:          || $env{'request.role.adv'} ) {
 2167: 
 2168:         return;
 2169:     }
 2170:     my $buttonshide = &Apache::lonnet::EXT('resource.0.buttonshide');
 2171:     return $buttonshide; 
 2172: }
 2173: 
 2174: sub roles_selector {
 2175:     my ($cdom,$cnum,$httphost) = @_;
 2176:     my $crstype = &Apache::loncommon::course_type();
 2177:     my $now = time;
 2178:     my (%courseroles,%seccount,%courseprivs,%roledesc);
 2179:     my $is_cc;
 2180:     my ($js,$form,$switcher);
 2181:     my $ccrole;
 2182:     if ($crstype eq 'Community') {
 2183:         $ccrole = 'co';
 2184:     } else {
 2185:         $ccrole = 'cc';
 2186:     }
 2187:     my ($privref,$gotsymb,$destsymb);
 2188:     my $destinationurl = $ENV{'REQUEST_URI'};
 2189:     if ($destinationurl =~ /(\?|\&)symb=/) {
 2190:         $gotsymb = 1;
 2191:     } elsif ($destinationurl =~ m{^/enc/}) {
 2192:         my $plainurl = &Apache::lonenc::unencrypted($destinationurl);
 2193:         if ($plainurl =~ /(\?|\&)symb=/) {
 2194:             $gotsymb = 1;
 2195:         }
 2196:     }
 2197:     unless ($gotsymb) {
 2198:         $destsymb = &Apache::lonnet::symbread();
 2199:         if ($destsymb ne '') {
 2200:             $destsymb = &Apache::lonenc::check_encrypt($destsymb);
 2201:         }
 2202:     }
 2203:     my $reqprivs = &required_privs();
 2204:     if (ref($reqprivs) eq 'HASH') {
 2205:         my $destination = $destinationurl;
 2206:         $destination =~ s/(\?.*)$//;
 2207:         if (exists($reqprivs->{$destination})) {
 2208:             if ($reqprivs->{$destination} =~ /,/) {
 2209:                 @{$privref} = split(/,/,$reqprivs->{$destination});
 2210:             } else { 
 2211:                 $privref = [$reqprivs->{$destination}];
 2212:             }
 2213:         }
 2214:     }
 2215:     if ($env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum}) {
 2216:         my ($start,$end) = split(/\./,$env{'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum});
 2217:         if ((($start) && ($start<0)) || 
 2218:             (($end) && ($end<$now))  ||
 2219:             (($start) && ($now<$start))) {
 2220:             $is_cc = 0;
 2221:         } else {
 2222:             $is_cc = 1;
 2223:         }
 2224:     }
 2225:     if ($is_cc) {
 2226:         &get_all_courseroles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs);
 2227:     } elsif ($env{'request.role'} =~ m{^\Qcr/$cdom/$cdom-domainconfig/\E(\w+)\.\Q/$cdom/$cnum\E}) {
 2228:         &get_customadhoc_roles($cdom,$cnum,\%courseroles,\%seccount,\%courseprivs,\%roledesc,$privref);
 2229:     } else {
 2230:         my %gotnosection;
 2231:         foreach my $item (keys(%env)) {
 2232:             if ($item =~ m-^user\.role\.([^.]+)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
 2233:                 my $role = $1;
 2234:                 my $sec = $2;
 2235:                 next if ($role eq 'gr');
 2236:                 my ($start,$end) = split(/\./,$env{$item});
 2237:                 next if (($start && $start > $now) || ($end && $end < $now));
 2238:                 if ($sec eq '') {
 2239:                     if (!$gotnosection{$role}) {
 2240:                         $seccount{$role} ++;
 2241:                         $gotnosection{$role} = 1;
 2242:                     }
 2243:                 }
 2244:                 if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0)) {
 2245:                     my $cnumsec = $cnum;
 2246:                     if ($sec ne '') {
 2247:                         $cnumsec .= "/$sec";
 2248:                     }
 2249:                     $courseprivs{"$role./$cdom/$cnumsec./"} =
 2250:                         $env{"user.priv.$role./$cdom/$cnumsec./"};
 2251:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/"} =
 2252:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/"};
 2253:                     $courseprivs{"$role./$cdom/$cnumsec./$cdom/$cnumsec"} =
 2254:                         $env{"user.priv.$role./$cdom/$cnumsec./$cdom/$cnumsec"};
 2255:                 }
 2256:                 if (ref($courseroles{$role}) eq 'ARRAY') {
 2257:                     if ($sec ne '') {
 2258:                         if (!grep(/^\Q$sec\E$/,@{$courseroles{$role}})) {
 2259:                             push(@{$courseroles{$role}},$sec);
 2260:                             $seccount{$role} ++;
 2261:                         }
 2262:                     }
 2263:                 } else {
 2264:                     @{$courseroles{$role}} = ();
 2265:                     if ($sec ne '') {
 2266:                         $seccount{$role} ++;
 2267:                         push(@{$courseroles{$role}},$sec);
 2268:                     }
 2269:                 }
 2270:             }
 2271:         }
 2272:     }
 2273:     my @roles_order = ($ccrole,'in','ta','ep','ad','st');
 2274:     my $numdiffsec;
 2275:     if (keys(%seccount) == 1) {
 2276:         foreach my $key (keys(%seccount)) {
 2277:             $numdiffsec = $seccount{$key};
 2278:         }
 2279:     }
 2280:     if ((keys(%seccount) > 1) || ($numdiffsec > 1)) {
 2281:         my @submenu;
 2282:         $js = &jump_to_role($cdom,$cnum,\%seccount,\%courseroles,\%courseprivs,\%roledesc,$privref);
 2283:         $form = 
 2284:             '<form name="rolechooser" method="post" action="'.$httphost.'/adm/roles">'."\n".
 2285:             '  <input type="hidden" name="destinationurl" value="'.
 2286:             &HTML::Entities::encode($destinationurl).'" />'."\n".
 2287:             '  <input type="hidden" name="gotorole" value="1" />'."\n".
 2288:             '  <input type="hidden" name="selectrole" value="" />'."\n".
 2289:             '  <input type="hidden" name="switchrole" value="" />'."\n";
 2290:         if ($destsymb ne '') {
 2291:             $form .= '  <input type="hidden" name="destsymb" value="'.
 2292:                         &HTML::Entities::encode($destsymb).'" />'."\n";
 2293:         }
 2294:         $form .= '</form>'."\n";
 2295:         foreach my $role (@roles_order) {
 2296:             my $include;
 2297:             if (defined($courseroles{$role})) {
 2298:                 if ($env{'request.role'} =~ m{^\Q$role\E}) {
 2299:                     if ($seccount{$role} > 1) {
 2300:                         $include = 1;
 2301:                     }
 2302:                 } else {
 2303:                     $include = 1;
 2304:                 }
 2305:             }
 2306:             if ($include) {
 2307:                 push(@submenu,['javascript:adhocRole('."'$role'".')',
 2308:                                &Apache::lonnet::plaintext($role,$crstype)]);
 2309:             }
 2310:         }
 2311:         foreach my $role (sort(keys(%courseroles))) {
 2312:             if ($role =~ /^cr/) {
 2313:                 my $include;
 2314:                 if ($env{'request.role'} =~ m{^\Q$role\E}) {
 2315:                     if ($seccount{$role} > 1) {
 2316:                         $include = 1;
 2317:                     }
 2318:                 } else {
 2319:                     $include = 1; 
 2320:                 }
 2321:                 if ($include) {
 2322:                     my $rolename;
 2323:                     if ($role =~ m{^cr/$cdom/$cdom\-domainconfig/(\w+)(?:/\w+|$)}) {
 2324:                         $rolename = $roledesc{$role};
 2325:                         if ($rolename eq '') {
 2326:                             $rolename = &mt('Helpdesk [_1]',$1);
 2327:                         }
 2328:                     } else {
 2329:                         $rolename = &Apache::lonnet::plaintext($role);
 2330:                     }
 2331:                     push(@submenu,['javascript:adhocRole('."'$role'".')',
 2332:                                    $rolename]);
 2333:                 }
 2334:             }
 2335:         }
 2336:         if (@submenu > 0) {
 2337:             $switcher = &create_submenu('','',&mt('Switch role'),\@submenu);
 2338:         }
 2339:     }
 2340:     return ($js,$form,$switcher);
 2341: }
 2342: 
 2343: sub get_all_courseroles {
 2344:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs) = @_;
 2345:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 2346:             (ref($courseprivs) eq 'HASH')) {
 2347:         return;
 2348:     }
 2349:     my ($result,$cached) = 
 2350:         &Apache::lonnet::is_cached_new('getcourseroles',$cdom.'_'.$cnum);
 2351:     if (defined($cached)) {
 2352:         if (ref($result) eq 'HASH') {
 2353:             if ((ref($result->{'roles'}) eq 'HASH') && 
 2354:                 (ref($result->{'seccount'}) eq 'HASH') && 
 2355:                 (ref($result->{'privs'}) eq 'HASH')) {
 2356:                 %{$courseroles} = %{$result->{'roles'}};
 2357:                 %{$seccount} = %{$result->{'seccount'}};
 2358:                 %{$courseprivs} = %{$result->{'privs'}};
 2359:                 return;
 2360:             }
 2361:         }
 2362:     }
 2363:     my %gotnosection;
 2364:     my %adv_roles =
 2365:          &Apache::lonnet::get_course_adv_roles($env{'request.course.id'},1);
 2366:     foreach my $role (keys(%adv_roles)) {
 2367:         my ($urole,$usec) = split(/:/,$role);
 2368:         if (!$gotnosection{$urole}) {
 2369:             $seccount->{$urole} ++;
 2370:             $gotnosection{$urole} = 1;
 2371:         }
 2372:         if (ref($courseroles->{$urole}) eq 'ARRAY') {
 2373:             if ($usec ne '') {
 2374:                 if (!grep(/^Q$usec\E$/,@{$courseroles->{$urole}})) {
 2375:                     push(@{$courseroles->{$urole}},$usec);
 2376:                     $seccount->{$urole} ++;
 2377:                 }
 2378:             }
 2379:         } else {
 2380:             @{$courseroles->{$urole}} = ();
 2381:             if ($usec ne '') {
 2382:                 $seccount->{$urole} ++;
 2383:                 push(@{$courseroles->{$urole}},$usec);
 2384:             }
 2385:         }
 2386:         my $area = '/'.$cdom.'/'.$cnum;
 2387:         if ($usec ne '') {
 2388:             $area .= '/'.$usec;
 2389:         }
 2390:         if ($role =~ /^cr\//) {
 2391:             &Apache::lonnet::custom_roleprivs($courseprivs,$urole,$cdom,$cnum,$urole.'.'.$area,$area);
 2392:         } else {
 2393:             &Apache::lonnet::standard_roleprivs($courseprivs,$urole,$cdom,$urole.'.'.$area,$cnum,$area);
 2394:         }
 2395:     }
 2396:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum,['st']);
 2397:     @{$courseroles->{'st'}} = ();
 2398:     &Apache::lonnet::standard_roleprivs($courseprivs,'st',$cdom,"st./$cdom/$cnum",$cnum,"/$cdom/$cnum");
 2399:     if (keys(%sections_count) > 0) {
 2400:         push(@{$courseroles->{'st'}},keys(%sections_count));
 2401:         $seccount->{'st'} = scalar(keys(%sections_count));
 2402:     }
 2403:     $seccount->{'st'} ++; # Increment for a section-less student role.  
 2404:     my $rolehash = {
 2405:                      'roles'    => $courseroles,
 2406:                      'seccount' => $seccount,
 2407:                      'privs'    => $courseprivs,
 2408:                    };
 2409:     &Apache::lonnet::do_cache_new('getcourseroles',$cdom.'_'.$cnum,$rolehash);
 2410:     return;
 2411: }
 2412: 
 2413: sub get_customadhoc_roles {
 2414:     my ($cdom,$cnum,$courseroles,$seccount,$courseprivs,$roledesc,$privref) = @_;
 2415:     unless ((ref($courseroles) eq 'HASH') && (ref($seccount) eq 'HASH') &&
 2416:             (ref($courseprivs) eq 'HASH') && (ref($roledesc) eq 'HASH')) {
 2417:         return;
 2418:     }
 2419:     my $is_helpdesk = 0;
 2420:     my $now = time;
 2421:     foreach my $role ('dh','da') {
 2422:         if ($env{"user.role.$role./$cdom/"}) {
 2423:             my ($start,$end)=split(/\./,$env{"user.role.$role./$cdom/"});
 2424:             if (!($start && ($now<$start)) && !($end && ($now>$end))) {
 2425:                 $is_helpdesk = 1;
 2426:                 last;
 2427:             }
 2428:         }
 2429:     }
 2430:     if ($is_helpdesk) {
 2431:         my ($possroles,$description) = &Apache::lonnet::get_my_adhocroles($cdom.'_'.$cnum);
 2432:         my %available;
 2433:         if (ref($possroles) eq 'ARRAY') {
 2434:             map { $available{$_} = 1; } @{$possroles};
 2435:         }
 2436:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
 2437:         if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 2438:             if (keys(%{$domdefaults{'adhocroles'}})) {
 2439:                 my $numsec = 1;
 2440:                 my @sections;
 2441:                 my ($allseclist,$cached) =
 2442:                     &Apache::lonnet::is_cached_new('courseseclist',$cdom.'_'.$cnum);
 2443:                 if (defined($cached)) {
 2444:                     if ($allseclist ne '') {
 2445:                         @sections = split(/,/,$allseclist);
 2446:                         $numsec += scalar(@sections);
 2447:                     }
 2448:                 } else {
 2449:                     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
 2450:                     @sections = sort(keys(%sections_count));
 2451:                     $numsec += scalar(@sections);
 2452:                     $allseclist = join(',',@sections);
 2453:                     &Apache::lonnet::do_cache_new('courseseclist',$cdom.'_'.$cnum,$allseclist);
 2454:                 }
 2455:                 my (%adhoc,$gotprivs);
 2456:                 my $prefix = "cr/$cdom/$cdom".'-domainconfig';
 2457:                 foreach my $role (keys(%{$domdefaults{'adhocroles'}})) {
 2458:                     next if (($role eq '') || ($role =~ /\W/));
 2459:                     $seccount->{"$prefix/$role"} = $numsec;
 2460:                     $roledesc->{"$prefix/$role"} = $description->{$role};
 2461:                     if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0)) {
 2462:                         if (exists($env{"user.priv.$prefix/$role./$cdom/$cnum./"})) {
 2463:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./"} =
 2464:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./"};
 2465:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/"} =
 2466:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./$cdom/"};
 2467:                             $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/$cnum"} =
 2468:                                 $env{"user.priv.$prefix/$role./$cdom/$cnum./$cdom/$cnum"};
 2469:                         } else {
 2470:                             unless ($gotprivs) {
 2471:                                 my ($adhocroles,$privscached) =
 2472:                                     &Apache::lonnet::is_cached_new('adhocroles',$cdom);
 2473:                                 if ((defined($privscached)) && (ref($adhocroles) eq 'HASH')) {
 2474:                                     %adhoc = %{$adhocroles};
 2475:                                 } else {
 2476:                                     my $confname = &Apache::lonnet::get_domainconfiguser($cdom);
 2477:                                     my %roledefs = &Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
 2478:                                     foreach my $key (keys(%roledefs)) {
 2479:                                         (undef,my $rolename) = split(/_/,$key);
 2480:                                         if ($rolename ne '') {
 2481:                                             my ($systempriv,$domainpriv,$coursepriv) = split(/\_/,$roledefs{$key});
 2482:                                             $coursepriv = &Apache::lonnet::course_adhocrole_privs($rolename,$cdom,$cnum,$coursepriv);
 2483:                                             $adhoc{$rolename} = join('_',($systempriv,$domainpriv,$coursepriv));
 2484:                                         }
 2485:                                     }
 2486:                                     &Apache::lonnet::do_cache_new('adhocroles',$cdom,\%adhoc);
 2487:                                 }
 2488:                                 $gotprivs = 1;
 2489:                             }
 2490:                             ($courseprivs->{"$prefix/$role./$cdom/$cnum./"},
 2491:                              $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/"},
 2492:                              $courseprivs->{"$prefix/$role./$cdom/$cnum./$cdom/$cnum"}) =
 2493:                                  split(/\_/,$adhoc{$role});
 2494:                         }
 2495:                     }
 2496:                     if ($available{$role}) {
 2497:                         $courseroles->{"$prefix/$role"} = \@sections;
 2498:                     }
 2499:                 }
 2500:             }
 2501:         }
 2502:     }
 2503:     return;
 2504: }
 2505: 
 2506: sub jump_to_role {
 2507:     my ($cdom,$cnum,$seccount,$courseroles,$courseprivs,$roledesc,$privref) = @_;
 2508:     my %lt = &Apache::lonlocal::texthash(
 2509:                 this => 'This role has section(s) associated with it.',
 2510:                 ente => 'Enter a specific section.',
 2511:                 orlb => 'Enter a specific section, or leave blank for no section.',
 2512:                 avai => 'Available sections are:',
 2513:                 youe => 'You entered an invalid section choice:',
 2514:                 plst => 'Please try again.',
 2515:                 role => 'The role you selected is not permitted to view the current page.',
 2516:                 swit => 'Switch role, but display Main Menu page instead?',
 2517:     );
 2518:     my $js;
 2519:     if (ref($courseroles) eq 'HASH') {
 2520:         $js = '    var secpick = new Array("'.$lt{'ente'}.'","'.$lt{'orlb'}.'");'."\n". 
 2521:               '    var numsec = new Array();'."\n".
 2522:               '    var rolesections = new Array();'."\n".
 2523:               '    var rolenames = new Array();'."\n".
 2524:               '    var roleseclist = new Array();'."\n";
 2525:         my @items = keys(%{$courseroles});
 2526:         for (my $i=0; $i<@items; $i++) {
 2527:             $js .= '    rolenames['.$i.'] = "'.$items[$i].'";'."\n";
 2528:             my ($secs,$secstr);
 2529:             if (ref($courseroles->{$items[$i]}) eq 'ARRAY') {
 2530:                 my @sections = sort { $a <=> $b } @{$courseroles->{$items[$i]}};
 2531:                 $secs = join('","',@sections);
 2532:                 $secstr = join(', ',@sections);
 2533:             }
 2534:             $js .= '    rolesections['.$i.'] = new Array("'.$secs.'");'."\n".
 2535:                    '    roleseclist['.$i.'] = "'.$secstr.'";'."\n".
 2536:                    '    numsec['.$i.'] = "'.$seccount->{$items[$i]}.'";'."\n";
 2537:         }
 2538:     }
 2539:     my $checkroles = 0;
 2540:     if ((ref($privref) eq 'ARRAY') && (@{$privref} > 0) && (ref($courseprivs) eq 'HASH')) {
 2541:         my %disallowed;
 2542:         foreach my $role (sort(keys(%{$courseprivs}))) {
 2543:             my $trole;
 2544:             if ($role =~ m{^(.+?)\Q./$cdom/$cnum\E}) {
 2545:                 $trole = $1;
 2546:             }
 2547:             if (($trole ne '') && ($trole ne 'cm')) {
 2548:                 $disallowed{$trole} = 1;
 2549:                 foreach my $priv (@{$privref}) { 
 2550:                     if ($courseprivs->{$role} =~ /\Q:$priv\E($|:|\&\w+)/) {
 2551:                         delete($disallowed{$trole});
 2552:                         last;
 2553:                     }
 2554:                 }
 2555:             }
 2556:         }
 2557:         if (keys(%disallowed) > 0) {
 2558:             $checkroles = 1;
 2559:             $js .= "    var disallow = new Array('".join("','",keys(%disallowed))."');\n".
 2560:                    "    var rolecheck = 1;\n";
 2561:         }
 2562:     }
 2563:     if (!$checkroles) {
 2564:         $js .=  "    var disallow = new Array();\n".
 2565:                 "    rolecheck = 0;\n";
 2566:     }
 2567:     return <<"END";
 2568: <script type="text/javascript">
 2569: //<![CDATA[
 2570: function adhocRole(newrole) {
 2571:     $js
 2572:     if (newrole == '') {
 2573:         return;
 2574:     } 
 2575:     var fullrole = newrole+'./$cdom/$cnum';
 2576:     var selidx = '';
 2577:     for (var i=0; i<rolenames.length; i++) {
 2578:         if (rolenames[i] == newrole) {
 2579:             selidx = i;
 2580:         }
 2581:     }
 2582:     if (rolecheck > 0) {
 2583:         for (var i=0; i<disallow.length; i++) {
 2584:             if (disallow[i] == newrole) {
 2585:                 if (confirm("$lt{'role'}\\n$lt{'swit'}")) {
 2586:                     document.rolechooser.destinationurl.value = '/adm/menu';
 2587:                 } else {
 2588:                     return;
 2589:                 }
 2590:             }
 2591:         }
 2592:     }
 2593:     var secok = 1;
 2594:     var secchoice = '';
 2595:     if (selidx >= 0) {
 2596:         if (numsec[selidx] > 1) {
 2597:             secok = 0;
 2598:             var numrolesec = rolesections[selidx].length;
 2599:             var msgidx = numsec[selidx] - numrolesec;
 2600:             secchoice = prompt("$lt{'this'} "+secpick[msgidx]+"\\n$lt{'avai'} "+roleseclist[selidx],"");
 2601:             if (secchoice == '') {
 2602:                 if (msgidx > 0) {
 2603:                     secok = 1;
 2604:                 }
 2605:             } else {
 2606:                 for (var j=0; j<rolesections[selidx].length; j++) {
 2607:                     if (rolesections[selidx][j] == secchoice) {
 2608:                         secok = 1;
 2609:                     }
 2610:                 }
 2611:             }
 2612:         } else {
 2613:             if (rolesections[selidx].length == 1) {
 2614:                 secchoice = rolesections[selidx][0];
 2615:             }
 2616:         }
 2617:     }
 2618:     if (secok == 1) {
 2619:         if (secchoice != '') {
 2620:             fullrole += '/'+secchoice;
 2621:         }
 2622:     } else {
 2623:         if (secchoice != null) {
 2624:             alert("$lt{'youe'} \\""+secchoice+"\\".\\n $lt{'plst'}");
 2625:         }
 2626:         return;
 2627:     }
 2628:     if (fullrole == "$env{'request.role'}") {
 2629:         return;
 2630:     }
 2631:     itemid = retrieveIndex('gotorole');
 2632:     if (itemid != -1) {
 2633:         document.rolechooser.elements[itemid].name = fullrole;
 2634:     }
 2635:     document.rolechooser.switchrole.value = fullrole;
 2636:     document.rolechooser.selectrole.value = '1';
 2637:     document.rolechooser.submit();
 2638:     return;
 2639: }
 2640: 
 2641: function retrieveIndex(item) {
 2642:     for (var i=0;i<document.rolechooser.elements.length;i++) {
 2643:         if (document.rolechooser.elements[i].name == item) {
 2644:             return i;
 2645:         }
 2646:     }
 2647:     return -1;
 2648: }
 2649: // ]]>
 2650: </script>
 2651: END
 2652: }
 2653: 
 2654: sub required_privs {
 2655:     my $privs =  {
 2656:              '/adm/parmset'      => 'opa,vpa',
 2657:              '/adm/courseprefs'  => 'opa,vpa',
 2658:              '/adm/whatsnew'     => 'whn',
 2659:              '/adm/populate'     => 'cst,vpa,vcl',
 2660:              '/adm/trackstudent' => 'vsa',
 2661:              '/adm/statistics'   => 'mgr,vgr',
 2662:              '/adm/setblock'     => 'dcm,vcb',
 2663:              '/adm/coursedocs'   => 'mdc',
 2664:            };
 2665:     unless ($env{'course.'.$env{'request.course.id'}.'.grading'} eq 'spreadsheet') {
 2666:         $privs->{'/adm/classcalc'}   = 'vgr',
 2667:         $privs->{'/adm/assesscalc'}  = 'vgr',
 2668:         $privs->{'/adm/studentcalc'} = 'vgr';
 2669:     }
 2670:     return $privs;
 2671: }
 2672: 
 2673: sub countdown_timer {
 2674:     if (($env{'request.course.id'}) && ($env{'request.symb'} ne '') &&
 2675:         ($env{'request.filename'}=~/$LONCAPA::assess_re/)) {
 2676:         my ($type,$hastimeleft,$slothastime);
 2677:         my $now = time;
 2678:         if ($env{'request.filename'} =~ /\.task$/) {
 2679:             $type = 'Task';
 2680:         } else {
 2681:             $type = 'problem';
 2682:         }
 2683:         my ($status,$accessmsg,$slot_name,$slot) =
 2684:             &Apache::lonhomework::check_slot_access('0',$type);
 2685:         if ($slot_name ne '') {
 2686:             if (ref($slot) eq 'HASH') {
 2687:                 if (($slot->{'starttime'} < $now) &&
 2688:                     ($slot->{'endtime'} > $now)) {
 2689:                     $slothastime = 1;
 2690:                 }
 2691:             }
 2692:         }
 2693:         if ($status ne 'CAN_ANSWER') {
 2694:             return;
 2695:         }
 2696:         my $duedate = &Apache::lonnet::EXT("resource.0.duedate");
 2697:         my @interval=&Apache::lonnet::EXT("resource.0.interval");
 2698:         my ($timelimit,$usesdone,$donebuttontext,$proctor,$secret);
 2699:         if (@interval > 1) {
 2700:             ($timelimit,my $donesuffix) = split(/_/,$interval[0],2);
 2701:             if ($donesuffix =~ /^done\:([^\:]+)\:(.*)$/) {
 2702:                 $usesdone = 'done';
 2703:                 $donebuttontext = $1;
 2704:                 (undef,$proctor,$secret) = split(/_/,$2);
 2705:             } elsif ($donesuffix =~ /^done(|_.+)$/) {
 2706:                 $donebuttontext = &mt('Done');
 2707:                 ($usesdone,$proctor,$secret) = split(/_/,$donesuffix);
 2708:             }
 2709:             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
 2710:             if ($first_access > 0) {
 2711:                 if ($first_access+$timelimit > time) {
 2712:                     $hastimeleft = 1;
 2713:                 }
 2714:             }
 2715:         }
 2716:         if (($duedate && $duedate > time) ||
 2717:             (!$duedate && $hastimeleft) ||
 2718:             ($slot_name ne '' && $slothastime)) {
 2719:             my ($collapse,$expand,$alttxt,$title,$currdisp,$donebutton);
 2720:             if ((@interval > 1 && $hastimeleft) ||
 2721:                 ($type eq 'Task' && $slothastime)) {
 2722:                 $currdisp = 'inline';
 2723:                 $collapse = '&#9658;&nbsp;';
 2724:                 if ((@interval > 1) && ($hastimeleft)) {
 2725:                     if ($usesdone eq 'done') {
 2726:                         $donebutton = &done_button_js($interval[1],'','',$proctor,$donebuttontext);
 2727:                     }
 2728:                 }
 2729:             } else {
 2730:                 $currdisp = 'none';
 2731:                 $expand = '&#9668;&nbsp;';
 2732:             }
 2733:             unless ($env{'environment.icons'} eq 'iconsonly') {
 2734:                 $alttxt = &mt('Timer');
 2735:                 $title = $alttxt.'&nbsp;';
 2736:             }
 2737:             my $desc = &mt('Countdown to due date/time');
 2738: 
 2739:             return <<END;
 2740: $donebutton
 2741: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 2742: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
 2743: $collapse
 2744: </span></a>
 2745: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
 2746: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 2747: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
 2748: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
 2749: END
 2750:         }
 2751:     }
 2752:     return;
 2753: }
 2754: 
 2755: sub placement_progress {
 2756:     my ($totalpoints,$incomplete) = &Apache::lonplacementtest::check_completion(undef,undef,1);
 2757:     my $complete = 100 - $incomplete;
 2758:     return '<span class="LC_placement_prog">'.
 2759:            &mt('Test is [_1]% complete',$complete).'</span>';
 2760: }
 2761: 
 2762: # ================================================================ Main Program
 2763: 
 2764: BEGIN {
 2765:     if (! defined($readdesk)) {
 2766:         {
 2767:             my $tabfile = $Apache::lonnet::perlvar{'lonTabDir'}.'/mydesk.tab';
 2768:             if ( CORE::open( my $config,"<$tabfile") ) {
 2769:                 while (my $configline=<$config>) {
 2770:                     $configline=(split(/\#/,$configline))[0];
 2771:                     $configline=~s/^\s+//;
 2772:                     chomp($configline);
 2773:                     if ($configline=~/^cat\:/) {
 2774:                         my @entries=split(/\:/,$configline);
 2775:                         $category_positions{$entries[2]}=$entries[1];
 2776:                         $category_names{$entries[2]}=$entries[3];
 2777:                     } elsif ($configline=~/^prim\:/) {
 2778:                         my @entries = (split(/\:/, $configline))[1..6];
 2779:                         push(@primary_menu,\@entries);
 2780:                     } elsif ($configline=~/^primsub\:/) {
 2781:                         my ($parent,@entries) = (split(/\:/, $configline))[1..4];
 2782:                         push(@{$primary_submenu{$parent}},\@entries);
 2783:                     } elsif ($configline=~/^scnd\:/) {
 2784:                         my @entries = (split(/\:/, $configline))[1..5];
 2785:                         push(@secondary_menu,\@entries);
 2786:                     } elsif ($configline) {
 2787:                         push(@desklines,$configline);
 2788:                     }
 2789:                 }
 2790:                 CORE::close($config);
 2791:             }
 2792:         }
 2793:         $readdesk='done';
 2794:     }
 2795: }
 2796: 
 2797: 1;
 2798: __END__
 2799: 

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