File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.83: download - view: text, annotated - select for diffs
Wed May 14 18:20:09 2014 UTC (10 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support display of queued "textbook" requests awaiting approval by DC.

    1: # The LearningOnline Network
    2: # Request a course
    3: #
    4: # $Id: lonrequestcourse.pm,v 1.83 2014/05/14 18:20:09 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::lonrequestcourse.pm
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Allows users to request creation of new courses.
   37: 
   38: This is part of the LearningOnline Network with CAPA project
   39: described at http://www.lon-capa.org.
   40: 
   41: =head1 SUBROUTINES
   42: 
   43: =over
   44: 
   45: =item handler()
   46: 
   47: =item get_breadcrumbs()
   48: 
   49: =item header()
   50: 
   51: =item form_elements()
   52: 
   53: =item onload_action()
   54: 
   55: =item print_main_menu()
   56: 
   57: =item request_administration()
   58: 
   59: =item close_popup_form()
   60: 
   61: =item get_instcode()
   62: 
   63: =item print_request_form()
   64: 
   65: =item print_enrollment_menu()
   66: 
   67: =item show_invalid_crosslists()
   68: 
   69: =item inst_section_selector()
   70: 
   71: =item date_setting_table()
   72: 
   73: =item print_personnel_menu()
   74: 
   75: =item print_request_status()
   76: 
   77: =item print_request_logs()
   78: 
   79: =item print_review()
   80: 
   81: =item dates_from_form()
   82: 
   83: =item courseinfo_form()
   84: 
   85: =item clone_form()
   86: 
   87: =item clone_text()
   88: 
   89: =item coursecode_form()
   90: 
   91: =item get_course_dom()
   92: 
   93: =item display_navbuttons()
   94: 
   95: =item print_request_outcome()
   96: 
   97: =item check_autolimit()
   98: 
   99: =item retrieve_settings()
  100: 
  101: =item get_request_settings()
  102: 
  103: =item extract_instcode() 
  104: 
  105: =item generate_date_items()
  106: 
  107: =back
  108: 
  109: =cut
  110: 
  111: package Apache::lonrequestcourse;
  112: 
  113: use strict;
  114: use Apache::Constants qw(:common :http);
  115: use Apache::lonnet;
  116: use Apache::loncommon;
  117: use Apache::lonlocal;
  118: use Apache::loncoursequeueadmin;
  119: use Apache::lonuserutils;
  120: use LONCAPA qw(:DEFAULT :match);
  121: 
  122: sub handler {
  123:     my ($r) = @_;
  124:     &Apache::loncommon::content_type($r,'text/html');
  125:     $r->send_http_header;
  126:     if ($r->header_only) {
  127:         return OK;
  128:     }
  129: 
  130:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  131:         ['action','showdom','cnum','state','crstype','queue','tabs']);
  132:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  133:     my $dom = &get_course_dom();
  134:     my $action = $env{'form.action'};
  135:     my $state = $env{'form.state'};
  136:     my (%states,%stored);
  137:     my ($jscript,$uname,$udom,$result,$warning,$showcredits,$instcredits,%can_request,
  138:         %request_domains,@incdoms);
  139:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
  140:     if ($domdefs{'officialcredits'} || $domdefs{'unofficialcredits'} || $domdefs{'textbookcredits'}) {
  141:         $showcredits = 1;
  142:     }
  143: 
  144:     my $canreq =
  145:         &Apache::lonnet::check_can_request($dom,\%can_request,\%request_domains);
  146: 
  147:     foreach my $item (keys(%request_domains)) {
  148:         if (ref($request_domains{$item}) eq 'ARRAY') {
  149:             foreach my $possdom (@{$request_domains{$item}}) {
  150:                 unless(grep(/^\Q$possdom\E$/,@incdoms)) {
  151:                     push(@incdoms,$possdom);
  152:                 }
  153:             }
  154:         }
  155:     }
  156: 
  157:     if ($canreq) {
  158:         if (($env{'form.crstype'} eq 'textbook') || 
  159:             (scalar(keys(%can_request)) == 1) && ($can_request{'textbook'})) {
  160:             my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
  161:             if ($action eq 'log') {
  162:                 my $usetabs;
  163:                 if ((scalar(keys(%can_request)) == 1) && ($can_request{'textbook'})) {
  164:                     $usetabs = 1;
  165:                 } elsif ($env{'form.tabs'} eq 'on') {
  166:                     $usetabs = 1;
  167:                 }
  168:                 &Apache::lonhtmlcommon::add_breadcrumb({text=>'Course Request'});
  169:                 my $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
  170:                 &print_request_logs($r,$dom,undef,undef,$crumb,$usetabs);
  171:             } elsif ($action eq 'process') {
  172:                 if ($can_request{'textbook'}) {
  173:                     &process_textbook_request($r,$dom,$action,\%domdefs,\%domconfig,\%can_request);
  174:                 } else {
  175:                     &textbook_request_disabled($r,$dom,\%can_request);
  176:                 }
  177:             } elsif ($action eq 'display') {
  178:                my ($uname,$udom,$result,$warning) = &domcoord_display($dom);
  179:                if ($warning ne '') {
  180:                    my $args = { only_body => 1 };
  181:                    $r->print(&header('Course/Community Requests','','' ,'',$args).
  182:                              '<h3>'.&mt('Course/Community Request Details').'</h3>'.
  183:                              '<div class="LC_warning">'.$warning.'</div>'.
  184:                              &close_popup_form());
  185:                 } else {
  186:                     $states{'display'} = ['details'];
  187:                     my $loaditems = &onload_action($action,$state);
  188:                     my $page = 0;
  189:                     &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
  190:                                             $loaditems,'','','','','',$showcredits,'','',
  191:                                             $uname,$udom);
  192:                 }
  193:             } else {
  194:                 if ($can_request{'textbook'}) {
  195:                     &print_textbook_form($r,$dom,\@incdoms,\%domdefs,$domconfig{'requestcourses'},\%can_request);
  196:                 } else {
  197:                     &textbook_request_disabled($r,$dom,\%can_request);
  198:                 }
  199:             }
  200:             return OK;
  201:         }
  202:     }
  203: 
  204:     $states{'display'} = ['details'];
  205:     $states{'view'} = ['pick_request','details','cancel','removal'];
  206:     $states{'log'} = ['display'];
  207:     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
  208: 
  209:     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
  210:         unless ($env{'form.state'} eq 'crstype') {
  211:             unshift(@{$states{'new'}},'codepick');
  212:         }
  213:     }
  214: 
  215:     if (($action eq 'new') && (&Apache::loncoursequeueadmin::author_prompt())) {
  216:         if (ref($states{$action}) eq 'ARRAY') {
  217:             push(@{$states{$action}},'reqauthor');
  218:         }
  219:     }
  220: 
  221:     foreach my $key (keys(%states)) {
  222:         if (ref($states{$key}) eq 'ARRAY') {
  223:             unshift (@{$states{$key}},'crstype');
  224:         }
  225:     }
  226: 
  227:     my @invalidcrosslist;
  228:     my %trail = (
  229:                  crstype       => 'Pick Action',
  230:                  codepick      => 'Category',
  231:                  courseinfo    => 'Description',
  232:                  enrollment    => 'Access Dates',
  233:                  personnel     => 'Personnel',
  234:                  review        => 'Review',
  235:                  process       => 'Result',
  236:                  reqauthor     => 'Authoring Space Result',
  237:                  pick_request  => 'Display Summary',
  238:                  details       => 'Request Details',
  239:                  cancel        => 'Cancel Request',
  240:                  removal       => 'Outcome',
  241:                  display       => 'Request Logs',
  242:                 );
  243: 
  244:     if (($env{'form.crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
  245:         $trail{'enrollment'} = 'Enrollment';
  246:     }
  247: 
  248:     my ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description) =
  249:         &get_breadcrumbs($dom,$action,\$state,\%states,\%trail);
  250:     if ($action eq 'display') {
  251:         ($uname,$udom,$result,$warning) = &domcoord_display($dom);
  252:     } elsif ((defined($state)) && (defined($action))) {
  253:         if (($action eq 'view') && ($state eq 'details')) {
  254:             if ((defined($env{'form.showdom'})) && (defined($env{'form.cnum'}))) {
  255:                 my $result = &retrieve_settings($env{'form.showdom'},$env{'form.cnum'});
  256:             }
  257:         } elsif ($env{'form.crstype'} eq 'official') {
  258:             if (&Apache::lonnet::auto_run('',$dom)) {
  259:                 if (($action eq 'new') && (($state eq 'enrollment') || 
  260:                     ($state eq 'personnel'))) {
  261:                     my $checkcrosslist = 0;
  262:                     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  263:                         if ($env{'form.crosslist_'.$i}) {
  264:                             $checkcrosslist ++;
  265:                         }
  266:                     }
  267:                     if ($checkcrosslist) {
  268:                         my %codechk;
  269:                         my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
  270:                         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,
  271:                                                                  \%cat_titles,
  272:                                                                  \%cat_order,
  273:                                                                  \@code_order);
  274:                         my $numtitles = scalar(@codetitles);
  275:                         if ($numtitles) {
  276:                             for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  277:                                 if ($env{'form.crosslist_'.$i}) {
  278:                                     my $codecheck;
  279:                                     my $crosslistcode = '';
  280:                                     foreach my $item (@code_order) {
  281:                                         $crosslistcode .= $env{'form.crosslist_'.$i.'_'.$item}; 
  282:                                     }
  283:                                     if ($crosslistcode ne '') { 
  284:                                          ($codechk{$i}, my $rest) = 
  285:                                             &Apache::lonnet::auto_validate_instcode('',$dom,$crosslistcode);
  286:                                     }
  287:                                     unless ($codechk{$i} eq 'valid') {
  288:                                         $env{'form.crosslist_'.$i} = '';
  289:                                         push(@invalidcrosslist,$crosslistcode);
  290:                                     } 
  291:                                 }
  292:                             }
  293:                         }
  294:                     }
  295:                 }
  296:             }
  297:         }
  298:         (my $elements,$instcredits) = &form_elements($dom,$showcredits);
  299:         my $elementsref = {};
  300:         if ((ref($elements) eq 'HASH') && (ref($elements->{$action}) eq 'HASH')) {
  301:             if (ref($elements->{$action}{$state}) eq 'HASH') {
  302:                 $elementsref = $elements->{$action}{$state};
  303:             }
  304:         }
  305:         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
  306:             $env{'form.clonedom'} = $dom;
  307:         }
  308:         if ($state eq 'crstype') {
  309:             $jscript = &mainmenu_javascript();
  310:         } else {
  311:             $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
  312:             if ($state eq 'courseinfo') {
  313:                 $jscript .= &cloning_javascript();
  314:             }
  315:         }
  316:     }
  317: 
  318:     if ($state eq 'personnel') {
  319:         $jscript .= "\n".&Apache::loncommon::userbrowser_javascript();
  320:     }
  321: 
  322:     my $loaditems = &onload_action($action,$state);
  323: 
  324:     if ($action eq 'new') {
  325:         if ($canreq) {
  326:             if ($state eq 'crstype') {
  327:                 &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,
  328:                                  $crumb,\@incdoms);
  329:             } else {
  330:                 &request_administration($r,$action,$state,$page,\%states,$dom,
  331:                                         $jscript,$loaditems,$crumb,$newinstcode,
  332:                                         $codechk,$checkedcode,$description,
  333:                                         $showcredits,$instcredits,\@invalidcrosslist);
  334:             }
  335:         } else {
  336:             $r->print(&header('Course/Community Requests').$crumb.
  337:                       '<div class="LC_warning">'.
  338:                       &mt('You do not have privileges to request creation of courses or communities.').
  339:                       '</div>'.&Apache::loncommon::end_page());
  340:         }
  341:     } elsif ($action eq 'view') {
  342:         if ($state eq 'crstype') {
  343:             &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,$crumb,\@incdoms);
  344:         } else {
  345:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
  346:                                     $loaditems,$crumb,'','','','',$showcredits);
  347:         }
  348:     } elsif ($action eq 'display') {
  349:         if ($warning ne '') {
  350:             my $args = { only_body => 1 };
  351:             $r->print(&header('Course/Community Requests','','' ,'',$args).$crumb.
  352:                       '<h3>'.&mt('Course/Community Request Details').'</h3>'.
  353:                       '<div class="LC_warning">'.$warning.'</div>'.
  354:                       &close_popup_form());
  355:         } else {
  356:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
  357:                                     $loaditems,$crumb,'','','','',$showcredits,'','',
  358:                                     $uname,$udom);
  359:         }
  360:     } elsif ($action eq 'log') {
  361:         if ($state eq 'crstype') {
  362:             &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb,\@incdoms);
  363:         } else {
  364:             $jscript .= <<ENDJS;
  365: 
  366: function backPage(formname,prevstate) {
  367:     formname.state.value = prevstate;
  368:     formname.submit();
  369: }
  370: 
  371: function setPage(formname) {
  372:     formname.page.value = '1';
  373:     return;
  374: }
  375: 
  376: ENDJS
  377:             &print_request_logs($r,$dom,$jscript,$loaditems,$crumb,\%can_request);
  378:         }
  379:     } else {
  380:         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb,\@incdoms);
  381:     }
  382:     return OK;
  383: }
  384: 
  385: sub mainmenu_javascript {
  386:     return <<"END";
  387: function setType(courseForm) {
  388:     for (var i=0; i<courseForm.crstype.length; i++) {
  389:         if (courseForm.crstype.options[i].value == "$env{'form.crstype'}") {
  390:             courseForm.crstype.options[i].selected = true;
  391:         } else {
  392:             courseForm.crstype.options[i].selected = false;
  393:         }
  394:     }
  395: }
  396: 
  397: function setAction(courseForm) {
  398:     for (var i=0; i<courseForm.action.length; i++) {
  399:         if (courseForm.action.options[i].value == "$env{'form.action'}") {
  400:             courseForm.action.options[i].selected = true;
  401:         } else {
  402:             courseForm.action.options[i].selected = false;
  403:         }
  404:     }
  405: }
  406: END
  407: }
  408: 
  409: sub cloning_javascript {
  410:     return <<"END";
  411: function setCloneDisplay(courseForm) {
  412:     if (courseForm.cloning.length > 1) {    
  413:         for (var i=0; i<courseForm.cloning.length; i++) {
  414:             if (courseForm.cloning[i].checked) {
  415:                 if (courseForm.cloning[i].value == 1) {
  416:                     document.getElementById('cloneoptions').style.display="block";;
  417:                 }
  418:             }
  419:         }
  420:     }
  421: }
  422: END
  423: }
  424: 
  425: sub get_breadcrumbs {
  426:     my ($dom,$action,$state,$states,$trail) = @_;
  427:     my ($crumb,$newinstcode,$codechk,$checkedcode,$numtitles,$description);
  428:     my $page = 0;
  429:     if ((ref($states) eq 'HASH') && (ref($trail) eq 'HASH') && (ref($state))) {
  430:         if (defined($action)) {
  431:             my $done = 0;
  432:             my $i=0;
  433:             if (ref($states->{$action}) eq 'ARRAY') {
  434:                 while ($i<@{$states->{$action}} && !$done) {
  435:                     if ($states->{$action}[$i] eq $$state) {
  436:                         $page = $i;
  437:                         $done = 1;
  438:                     }
  439:                     $i++;
  440:                 }
  441:             }
  442:             if ($env{'form.crstype'} eq 'official') {
  443:                 if ($page > 1) {
  444:                     if ($states->{$action}[$page-1] eq 'codepick') {
  445:                         if ($env{'form.instcode'} eq '') {
  446:                             ($newinstcode,$numtitles) = &get_instcode($dom);
  447:                             if ($numtitles) {
  448:                                 if ($newinstcode eq '') {
  449:                                     $$state = 'codepick';
  450:                                     $page --;
  451:                                 } else {
  452:                                     ($codechk,$description) = 
  453:                                         &Apache::lonnet::auto_validate_instcode('',
  454:                                             $dom,$newinstcode);
  455:                                     if ($codechk ne 'valid') {
  456:                                         $$state = 'codepick';
  457:                                         $page --;
  458:                                     }
  459:                                     $checkedcode = 1;
  460:                                 }
  461:                             }
  462:                         }
  463:                     }
  464:                 }
  465:             }
  466:             for (my $i=0; $i<@{$states->{$action}}; $i++) {
  467:                 if ($$state eq $states->{$action}[$i]) {
  468:                     &Apache::lonhtmlcommon::add_breadcrumb(
  469:                        {text=>"$trail->{$$state}"});
  470:                     $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  471:                     last;
  472:                 } else {
  473:                     if (($$state eq 'process') || ($$state eq 'removal') || ($$state eq 'reqauthor')) {
  474:                         &Apache::lonhtmlcommon::add_breadcrumb(
  475:                             { href => '/adm/requestcourse',
  476:                               text => "$trail->{$states->{$action}[$i]}",
  477:                             }
  478:                         );
  479:                     } else {
  480:                         &Apache::lonhtmlcommon::add_breadcrumb(
  481:      { href => "javascript:backPage(document.requestcrs,'$states->{$action}[$i]')",
  482:        text => "$trail->{$states->{$action}[$i]}", }
  483:                        );
  484:                    }
  485:                }
  486:            }
  487:         } else {
  488:             &Apache::lonhtmlcommon::add_breadcrumb(
  489:                     {text=>'Pick Action'});
  490:             $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  491:         }
  492:     } else {
  493:         &Apache::lonhtmlcommon::add_breadcrumb(
  494:                 {text=>'Pick Action'});
  495:         $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  496:     }
  497:     return ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description);
  498: }
  499: 
  500: sub header {
  501:     my ($bodytitle,$jscript,$loaditems,$jsextra,$args) = @_;
  502:     if ($jscript) {
  503:         $jscript = '<script type="text/javascript">'."\n".
  504:                    '// <![CDATA['."\n".
  505:                    $jscript."\n".'// ]]>'."\n".'</script>'."\n";
  506:     }
  507:     if ($loaditems) {
  508:         if (ref($args) eq 'HASH') {
  509:             my %loadhash = (
  510:                              'add_entries' => $loaditems,
  511:                            );
  512:             my %arghash = (%loadhash,%{$args});
  513:             $args = \%arghash;                  
  514:         } else {
  515:             $args = {'add_entries' => $loaditems,};
  516:         }
  517:     }
  518:     return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra,$args);
  519: }
  520: 
  521: sub form_elements {
  522:     my ($dom,$showcredits) = @_;
  523:     my $instcredits;
  524:     my %elements =
  525:     (
  526:         new => {
  527:             crstype => {
  528:                 crstype => 'selectbox',
  529:                 action  => 'selectbox',
  530:                 origcnum => 'hidden', 
  531:             },
  532:             courseinfo => {
  533:                 cdescr           => 'text',
  534:                 cloning          => 'radio', 
  535:                 clonecrs         => 'text',
  536:                 clonedom         => 'selectbox',
  537:                 datemode         => 'radio',
  538:                 dateshift        => 'text',
  539:             },
  540:             enrollment  => {
  541:                 accessstart_month  => 'selectbox',
  542:                 accessstart_hour   => 'selectbox',
  543:                 accessend_month    => 'selectbox',
  544:                 accessend_hour     => 'selectbox',
  545:                 accessstart_day    => 'text',
  546:                 accessstart_year   => 'text',
  547:                 accessstart_minute => 'text',
  548:                 accessstart_second => 'text',
  549:                 accessend_day      => 'text',
  550:                 accessend_year     => 'text',
  551:                 accessend_minute   => 'text',
  552:                 accessend_second   => 'text',
  553:                 no_end_date      => 'checkbox',
  554:             },
  555:             personnel => {
  556:                 addperson   => 'checkbox', 
  557:             },
  558:             review => {
  559:                 cnum => 'hidden',
  560:             },
  561:          },
  562:          view => {
  563:             crstype => {
  564:                 crstype => 'selectbox',
  565:                 action  => 'selectbox',
  566:             },
  567:          },
  568:     );
  569:     my %servers = &Apache::lonnet::get_servers($dom,'library');
  570:     my $numlib = keys(%servers);
  571:     if ($numlib > 1) {
  572:         $elements{'new'}{'courseinfo'}{'chome'} = 'selectbox';
  573:     } else {
  574:         $elements{'new'}{'courseinfo'}{'chome'} = 'hidden';
  575:     }
  576:     my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
  577:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
  578:                                              \%cat_order,\@code_order);
  579:     my $numtitles = scalar(@codetitles);
  580:     if ($numtitles) {
  581:         my %extras;
  582:         $lastitem = pop(@codetitles);
  583:         $extras{'instcode_'.$lastitem} = 'text'; 
  584:         foreach my $item (@codetitles) {
  585:             $extras{'instcode_'.$item} = 'selectbox';   
  586:         }
  587:         $elements{'new'}{'codepick'} = \%extras;
  588:     }
  589:     if (&Apache::lonnet::auto_run('',$dom)) {
  590:         my %extras = (
  591:                        enrollstart_month  => 'selectbox',
  592:                        enrollstart_hour   => 'selectbox',
  593:                        enrollend_month    => 'selectbox',
  594:                        enrollend_hour     => 'selectbox',
  595:                        enrollstart_day    => 'text',
  596:                        enrollstart_year   => 'text',
  597:                        enrollstart_minute => 'text',
  598:                        enrollstart_second => 'text',
  599:                        enrollend_day      => 'text',
  600:                        enrollend_year     => 'text',
  601:                        enrollend_minute   => 'text',
  602:                        enrollend_second   => 'text',
  603:                        addcrosslist       => 'checkbox',
  604:                        autoadds           => 'radio',
  605:                        autodrops          => 'radio',
  606:         );
  607:         my ($instcode,$titlescount) = &get_instcode($dom);
  608:         if ($instcode) {
  609:             my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
  610:             if (@sections) {
  611:                 $extras{'sectotal'} = 'hidden';
  612:                 if ($env{'form.sectotal'} > 0) {
  613:                     for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
  614:                         $extras{'sec_'.$i} = 'radio';
  615:                         $extras{'secnum_'.$i} = 'text';
  616:                         $extras{'loncapasec_'.$i} = 'text';
  617:                     }
  618:                 }
  619:             } else {
  620:                 $extras{'addsection'} = 'checkbox';
  621:                 my $sectotal = $env{'form.sectotal'};
  622:                 if ($env{'form.addsection'}) {
  623:                     $sectotal ++;
  624:                 }
  625:                 for (my $i=0; $i<$sectotal; $i++) {
  626:                     $extras{'sec_'.$i} = 'checkbox';
  627:                     $extras{'secnum_'.$i} = 'text',
  628:                     $extras{'loncapasec_'.$i} = 'text',
  629:                 }
  630:             }
  631:             (my $outcome,my $desc,$instcredits) = 
  632:                 &Apache::lonnet::auto_validate_instcode(undef,$dom,$instcode);
  633:             if ($showcredits && $instcredits eq '') {
  634:                 $extras{'coursecredits'} = 'text';
  635:             }
  636:         } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
  637:             if ($showcredits) {
  638:                 $extras{'coursecredits'} = 'text';
  639:             }
  640:         }
  641:         my $crosslisttotal = $env{'form.crosslisttotal'};
  642:         if ($env{'form.addcrosslist'}) {
  643:             $crosslisttotal ++;
  644:         }
  645:         if (!$crosslisttotal) {
  646:             $crosslisttotal = 1;
  647:         }
  648:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  649:             if ($numtitles) {
  650:                 $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
  651:             }
  652:             if (@codetitles > 0) {
  653:                 foreach my $item (@codetitles) {
  654:                     $extras{'crosslist_'.$i.'_'.$item} = 'selectbox';
  655:                 }
  656:             }
  657:             $extras{'crosslist_'.$i} = 'checkbox';
  658:             $extras{'crosslist_'.$i.'_instsec'} = 'text',
  659:             $extras{'crosslist_'.$i.'_lcsec'} = 'text',
  660:         }
  661:         my %mergedhash = (%{$elements{'new'}{'enrollment'}},%extras);
  662:         %{$elements{'new'}{'enrollment'}} = %mergedhash;
  663:     }
  664:     my %people;
  665:     my $persontotal = $env{'form.persontotal'};
  666:     if ($env{'form.addperson'}) {
  667:         $persontotal ++;
  668:     }
  669:     if ((!defined($persontotal)) || (!$persontotal)) {
  670:         $persontotal = 1;
  671:     }
  672:     for (my $i=0; $i<$persontotal; $i++) {
  673:         $people{'person_'.$i.'_uname'}     = 'text',
  674:         $people{'person_'.$i.'_dom'}       = 'selectbox',
  675:         $people{'person_'.$i.'_hidedom'}   = 'hidden',
  676:         $people{'person_'.$i.'_firstname'} = 'text',
  677:         $people{'person_'.$i.'_lastname'}  = 'text',
  678:         $people{'person_'.$i.'_emailaddr'} = 'text',
  679:         $people{'person_'.$i.'_role'}      = 'selectbox',
  680:         $people{'person_'.$i.'_sec'}       = 'selectbox',
  681:         $people{'person_'.$i.'_newsec'}    = 'text',
  682:     }
  683:     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
  684:     %{$elements{'new'}{'personnel'}} = %personnelhash;
  685:     return (\%elements,$instcredits);;
  686: }
  687: 
  688: sub onload_action {
  689:     my ($action,$state) = @_;
  690:     my %loaditems;
  691:     if (($action eq 'new') || ($action eq 'view')) {
  692:         if ($state eq 'crstype') {
  693:             $loaditems{'onload'} = 'javascript:setAction(document.mainmenu_action);javascript:setType(document.mainmenu_coursetype)';
  694:         } else {
  695:             $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs);';
  696:         }
  697:         if ($state eq 'courseinfo') {
  698:             $loaditems{'onload'} .= 'javascript:setCloneDisplay(document.requestcrs);';
  699:         }
  700:     }
  701:     return \%loaditems;
  702: }
  703: 
  704: sub print_main_menu {
  705:     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb,$incdoms) = @_;
  706:     my ($types,$typename) = &Apache::loncommon::course_types();
  707:     my $onchange = 'this.form.submit()';
  708:     my $nextstate_setter = "\n";
  709:     if (ref($states) eq 'HASH') {
  710:         foreach my $key (keys(%{$states})) {
  711:             if (ref($states->{$key}) eq 'ARRAY') {
  712:                 $nextstate_setter .= 
  713: "             if (actionchoice == '$key') {
  714:                   nextstate = '".$states->{$key}[1]."';
  715:              }
  716: ";
  717:             }
  718:         }
  719:     }
  720: 
  721:     my $js = <<"END";
  722: 
  723: function nextPage(formname) {
  724:     var crschoice = document.mainmenu_coursetype.crstype.value;
  725:     var actionchoice = document.mainmenu_action.action.value;
  726:     if (check_can_request(crschoice,actionchoice) == true) {
  727:         if ((actionchoice == 'new') && (crschoice == 'official')) {
  728:             nextstate = 'codepick';
  729:         } else {
  730: $nextstate_setter 
  731:         }
  732:         formname.crstype.value = crschoice;
  733:         formname.action.value = actionchoice; 
  734:         formname.state.value= nextstate;
  735:         formname.submit();
  736:     }
  737:     return;
  738: }
  739: 
  740: function check_can_request(crschoice,actionchoice) {
  741:     var official = '';
  742:     var unofficial = '';
  743:     var community = '';
  744:     var textbook = '';
  745: END
  746:     if (ref($can_request) eq 'HASH') {
  747:         foreach my $item (keys(%{$can_request})) {
  748:                 $js .= " 
  749:         $item = 1;
  750: ";
  751:         }
  752:     }
  753:     my %lt = &Apache::lonlocal::texthash(
  754:         official => 'You are not permitted to request creation of an official course in this domain.',
  755:         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
  756:         community => 'You are not permitted to request creation of a community in this domain.',
  757:         textbook => 'You are not permitted to request creation of a textbook course in this domain',
  758:         all => 'You must choose a specific course type when making a new course request.',
  759:         allt => '"All types" is not allowed.',
  760:     ); 
  761:     $js .= <<END;
  762:     if (crschoice == 'official') {
  763:         if (official != 1) {
  764:             alert("$lt{'official'}");
  765:             return false;
  766:         }
  767:     } else {
  768:         if (crschoice == 'unofficial') {
  769:             if (unofficial != 1) {
  770:                 alert("$lt{'unofficial'}");
  771:                 return false;
  772:             }
  773:         } else {
  774:             if (crschoice == 'community') {
  775:                 if (community != 1) {
  776:                     alert("$lt{'community'}");
  777:                     return false;
  778:                 }
  779:             } else {
  780:                 if (crschoice == 'textbook') {
  781:                     if (textbook != 1) {
  782:                         alert("$lt{'community'}");
  783:                         return false;
  784:                     }
  785:                 } else {
  786:                     if (actionchoice == 'new') {
  787:                         alert('$lt{'all'}'+'\\n'+'$lt{'allt'}');
  788:                         return false;
  789:                     }
  790:                 }
  791:             }
  792:         }
  793:     }
  794:     return true;
  795: }
  796: END
  797:     my ($pagetitle,$pageinfo,$domaintitle,$earlyout);
  798:     if (ref($can_request) eq 'HASH') {
  799:         if (($can_request->{'official'}) || ($can_request->{'unofficial'}) || $can_request->{'textbook'}) {
  800:             if ($can_request->{'community'}) {
  801:                 $pagetitle = 'Course/Community Requests';
  802:                 $pageinfo = &mt('Request creation of a new course or community, or review your pending requests.');
  803:                 $domaintitle = &mt('Course/Community Domain');
  804:             } else {
  805:                 $pagetitle = 'Course Requests';
  806:                 $pageinfo = &mt('Request creation of a new course, or review your pending course requests.');
  807:                 $domaintitle = &mt('Course Domain');
  808:             }
  809:         } elsif ($can_request->{'community'}) {
  810:             $pagetitle = 'Community Requests';
  811:             $pageinfo = &mt('Request creation of a new course, or review your pending requests.');
  812:             $domaintitle = &mt('Community Domain');
  813:         } elsif ((ref($incdoms) eq 'ARRAY') && ((@{$incdoms} > 1) ||
  814:                  ((@{$incdoms} == 1) && ($incdoms->[0] ne $dom)))) {
  815:             $pagetitle = 'Course/Community Requests';
  816:             $pageinfo = &mt('You do not have rights to request creation of courses in this domain; please choose a different domain.');
  817:             $domaintitle = &mt('Course/Community Domain');
  818:         } else {
  819:             $pagetitle = 'Course/Community Requests';
  820:             $pageinfo  = &mt('You do not have rights to request creation of courses or communities.');
  821:             $earlyout = 1;
  822:         }
  823:     }
  824:     $r->print(&header($pagetitle,$js.$jscript,$loaditems).$crumb.
  825:              '<p>'.$pageinfo.'</p>');
  826:     if ($earlyout) {
  827:         $r->print(&Apache::loncommon::end_page());
  828:         return;
  829:     }
  830:     $r->print('<div>'.
  831:               &Apache::lonhtmlcommon::start_pick_box().
  832:               &Apache::lonhtmlcommon::row_title($domaintitle).
  833:               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
  834:               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange,$incdoms));
  835:     if (!$onchange) {
  836:         $r->print('&nbsp;<input type="submit" name="godom" value="'.
  837:                    &mt('Change').'" />');
  838:     }
  839:     unless ((ref($can_request) eq 'HASH') && (keys(%{$can_request}) > 0)) {
  840:         $r->print('</form>'.&Apache::lonhtmlcommon::row_closure(1)."\n".
  841:                   &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n".
  842:                   &Apache::loncommon::end_page());
  843:         return;
  844:     }
  845:     $r->print('</form>'.&Apache::lonhtmlcommon::row_closure());
  846:     my $formname = 'requestcrs';
  847:     my $nexttext = &mt('Next');
  848:     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Action')).'
  849: <form name="mainmenu_action" method="post" action=""> 
  850: <select size="1" name="action" >
  851:  <option value="new">'.&mt('New request').'</option>
  852:  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
  853:  <option value="log">'.&mt('View request history').'</option>
  854: </select></form>'.
  855:               &Apache::lonhtmlcommon::row_closure().
  856:               &Apache::lonhtmlcommon::row_title(&mt('Type')).'
  857: <form name="mainmenu_coursetype" method="post" action="">
  858: <select size="1" name="crstype">');
  859:     if (ref($can_request) eq 'HASH') {
  860:         if (keys(%{$can_request}) > 1) {
  861:             $r->print(' <option value="any">'.&mt('All types').'</option>');
  862:         }
  863:         if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
  864:             foreach my $type (@{$types}) {
  865:                 next unless($can_request->{$type});
  866:                 my $selected = '';
  867:                 if ($env{'form.crstype'} eq '') {
  868:                     if ($type eq 'official') {
  869:                         $selected = ' selected="selected"';
  870:                     }
  871:                 } else {
  872:                     if ($type eq $env{'form.crstype'}) {
  873:                         $selected = ' selected="selected"';
  874:                     }
  875:                 }
  876:                 $r->print('<option value="'.$type.'"'.$selected.'>'.&mt($typename->{$type}).
  877:                           '</option>'."\n");
  878:             }
  879:         }
  880:     }
  881:     $r->print('</select></form>'."\n".
  882:               &Apache::lonhtmlcommon::row_closure(1)."\n".
  883:               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n".
  884:               '<div><form name="'.$formname.'" method="post" action="/adm/requestcourse">'."\n".
  885:               '<input type="hidden" name="state" value="crstype" />'."\n".
  886:               '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
  887:               '<input type="hidden" name="crstype" value="" />'."\n".
  888:               '<input type="hidden" name="action" value="" />'."\n".
  889:               '<input type="button" name="next" value="'.$nexttext.
  890:               '" onclick="javascript:nextPage(document.'.$formname.')" />'."\n".
  891:               '</form></div>');
  892:     $r->print(&Apache::loncommon::end_page());
  893:     return;
  894: }
  895: 
  896: sub request_administration {
  897:     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb,
  898:         $newinstcode,$codechk,$checkedcode,$description,$showcredits,
  899:         $instcredits,$invalidcrosslist,$uname,$udom) = @_;
  900:     my $js;
  901:     if (($action eq 'new') || (($action eq 'view') && ($state eq 'pick_request'))) {
  902:         $js =  <<END;
  903: 
  904: function nextPage(formname,nextstate) {
  905:     formname.state.value= nextstate;
  906:     formname.submit();
  907: }
  908: 
  909: END
  910:     }
  911:     if (($action eq 'new') || ($action eq 'view')) {
  912:         $js .= <<END;   
  913: 
  914: function backPage(formname,prevstate) {
  915:     formname.state.value = prevstate;
  916:     formname.submit();
  917: }
  918: 
  919: END
  920:     }
  921:     if ($action eq 'new') {
  922:         my $jsextra;
  923:         if (($state eq 'courseinfo') || ($state eq 'codepick')) {
  924:             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
  925:         } elsif ($state eq 'enrollment') {
  926:             if (($env{'form.crstype'} eq 'official') && 
  927:                 (&Apache::lonnet::auto_run('',$dom))) {
  928:                 $js .= "\n".&section_check_javascript()."\n".&enrollment_lcsec_js();
  929:             }
  930:         } elsif ($state eq 'personnel') {
  931:             $js .= "\n".&section_check_javascript()."\n".&personnel_lcsec_js();
  932:         }
  933:         my $title;
  934:         if ($env{'form.crstype'} eq 'community') {
  935:             $title = 'Request a community';
  936:         } else {
  937:             $title = 'Request a course';
  938:         }
  939:         $r->print(&header($title,$js.$jscript,$loaditems,$jsextra).$crumb);
  940:         &print_request_form($r,$action,$state,$page,$states,$dom,$newinstcode,
  941:                             $codechk,$checkedcode,$description,$showcredits,
  942:                             $instcredits,$invalidcrosslist);
  943:     } elsif ($action eq 'view') {
  944:         my $jsextra;
  945:         my $formname = 'requestcrs';
  946:         my $prev = $states->{$action}[$page-1];
  947:         my $next = $states->{$action}[$page+1];
  948:         if ($state eq 'pick_request') {
  949:             $next = $states->{$action}[$page+1];
  950:             $jsextra = &viewrequest_javascript($formname,$next);
  951:         } elsif ($state eq 'details') {
  952:             $jsextra = &viewdetails_javascript($formname);
  953: 
  954:         } elsif ($state eq 'cancel') {
  955:             $jsextra = &viewcancel_javascript($formname);
  956:         }
  957:         my $title;
  958:         if ($env{'form.crstype'} eq 'community') {
  959:             $title = 'Manage community requests';
  960:         } else {
  961:             $title = 'Manage course requests';
  962:         }
  963:         $r->print(&header($title,$js.$jscript.$jsextra,$loaditems).$crumb);
  964:         my $form = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />';
  965:         if ($state eq 'pick_request') {
  966:             my $title;
  967:             if ($env{'form.crstype'} eq 'community') {
  968:                 $title = &mt('Pending community requests');
  969:             } elsif ($env{'form.crstype'} eq 'official') {
  970:                 $title = &mt('Pending requests for official courses');
  971:             } elsif ($env{'form.crstype'} eq 'unofficial') {
  972:                 $title = &mt('Pending requests for unofficial courses');
  973:             } elsif ($env{'form.crstype'} eq 'textbook') {
  974:                 $title = &mt('Pending requests for textbook courses');
  975:             } else {
  976:                 $title = &mt('Pending course/community requests'); 
  977:             }
  978:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
  979:                       &print_request_status($dom,$action).'</form></div>');
  980:         } elsif ($state eq 'details') {
  981:             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
  982:             my $origcnum = $env{'form.cnum'};
  983:             if ($origcnum eq '') {
  984:                 $origcnum = $env{'form.origcnum'};   
  985:             }
  986:             if ($env{'form.crstype'} eq 'official') {
  987:                 &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
  988:                                                          \%cat_order,\@code_order);
  989:             }
  990:             my $title;
  991:             if ($env{'form.crstype'} eq 'community') {
  992:                 $title = &mt('Community Request Details');
  993:             } else {
  994:                 $title = &mt('Course Request Details');
  995:             }
  996:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
  997:                       &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
  998:                                     \@code_order,'','','','',$instcredits)."\n".
  999:                       '<input name="origcnum" value="'.$origcnum.'" type="hidden" />'."\n");
 1000:             my @excluded = &get_excluded_elements($dom,$states,'new','review',
 1001:                                                   $showcredits);
 1002:             push(@excluded,'origcnum');
 1003:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
 1004:             my $other = 'modify';
 1005:             my %navtxt = &Apache::lonlocal::texthash (
 1006:                                                       prev => 'Back',
 1007:                                                       other => 'Modify Request',
 1008:                                                       next => 'Cancel Request',
 1009:                                                      );
 1010:             &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
 1011:                                 $navtxt{'next'},$state,$other,$navtxt{'other'});
 1012:             $r->print('</form>');
 1013:         } elsif ($state eq 'cancel') {
 1014:             my $title;
 1015:             if ($env{'form.crstype'} eq 'community') {
 1016:                 $title = &mt('Cancel community request');
 1017:             } else {
 1018:                 $title = &mt('Cancel course request');
 1019:             }
 1020:             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
 1021:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
 1022:                       $output);
 1023:             my @excluded = &get_excluded_elements($dom,$states,'view','cancel',
 1024:                                                   $showcredits);
 1025:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
 1026:             my %navtxt = &Apache::lonlocal::texthash (
 1027:                                                       prev => 'Back',
 1028:                                                       next => 'Confirm Cancellation',
 1029:                                                      );
 1030:             if ($result eq 'ok') {
 1031:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
 1032:                                     $navtxt{'next'},$state);
 1033:             } else {
 1034:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},undef,
 1035:                                     '',$state);
 1036:             }
 1037:             $r->print('</form>');
 1038:         } elsif ($state eq 'removal') {
 1039:             my $cnum = $env{'form.origcnum'};
 1040:             my $statuskey = 'status:'.$dom.':'.$cnum;
 1041:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
 1042:                                                    $env{'user.domain'},$env{'user.name'});
 1043:             my $currstatus = $userreqhash{$statuskey};
 1044:             my ($result,$error); 
 1045:             if (($currstatus eq 'approval') || ($currstatus eq 'pending')) { 
 1046:                 my %status = (
 1047:                                  $statuskey => 'cancelled',
 1048:                              );
 1049:                 my $statusresult = &Apache::lonnet::put('courserequests',\%status);
 1050:                 if ($statusresult eq 'ok') {
 1051:                     my $delresult = 
 1052:                         &Apache::lonnet::del_dom('courserequestqueue',
 1053:                                                  [$cnum.'_'.$currstatus],$dom);
 1054:                     if ($delresult eq 'ok') {
 1055:                         $result = 'ok';
 1056:                     } else {
 1057:                         $error = &mt('An error occurred when updating the pending requests queue: [_1]',$delresult);
 1058:                     }
 1059:                 } else {
 1060:                     $error = &mt("An error occurred when updating the status of this request in the requestor's records: [_1]",$statusresult);
 1061:                 }
 1062:             } else {
 1063:                 $error = &mt('The current status of this request could not be verified as pending approval/institutional action.');  
 1064:             }
 1065:             $r->print('<h3>'.&mt('Request Cancellation').'</h3><div>'."\n".$form."\n".
 1066:                       '<input type="hidden" name="state" value="'.$state.'" />'."\n".
 1067:                       '<input type="hidden" name="action" value="'.$action.'" />'."\n".
 1068:                       '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
 1069:                       '<input type="hidden" name="orignum" value="'.$cnum.'" />'."\n");
 1070:             if ($result eq 'ok') {
 1071:                 if ($env{'form.crstype'} eq 'community') {
 1072:                     $r->print(&mt('Your community request has been cancelled.'));
 1073:                 } else {
 1074:                     $r->print(&mt('Your course request has been cancelled.'));
 1075:                 }
 1076:             } else {
 1077:                 $r->print('<div class="LC_error">'.
 1078:                           &mt('The request cancellation process was not complete.').
 1079:                           '<br />'.$error.'</div>');
 1080:             }
 1081:             $r->print('</form>');
 1082:         }
 1083:     } elsif ($action eq 'display') {
 1084:         my $formname = 'requestcrs';
 1085:         my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
 1086:         if ($env{'form.crstype'} eq 'official') {
 1087:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1088:                                                      \%cat_order,\@code_order);
 1089:         }
 1090:         my ($title,$header);
 1091:         if ($env{'form.crstype'} eq 'community') {
 1092:             $title = 'Community Request';
 1093:             $header = &mt('Community Request');
 1094:         } else {
 1095:             $title = 'Course Request';
 1096:             $header = &mt('Course Request');
 1097:         }
 1098:         $r->print(&header($title,'','','',{ 'only_body' => 1}).
 1099:                   $crumb."\n".'<h3>'.$header.'</h3>'.
 1100:                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
 1101:                                 \@code_order,$uname,$udom,'','',$instcredits)."\n".
 1102:                   '</div>'.
 1103:                   &close_popup_form());
 1104:     }
 1105:     $r->print(&Apache::loncommon::end_page());
 1106:     return;
 1107: }
 1108: 
 1109: sub domcoord_display {
 1110:     my ($dom) = @_;
 1111:     my ($uname,$udom,$result,$warning);
 1112:     if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
 1113:         if ($env{'form.cnum'} ne '') {
 1114:             my $cnum = $env{'form.cnum'};
 1115:             my $queue = $env{'form.queue'};
 1116:             my $reqkey = $cnum.'_'.$queue;
 1117:             my $namespace = 'courserequestqueue';
 1118:             my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
 1119:             my %queued =
 1120:                 &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
 1121:             if (ref($queued{$reqkey}) eq 'HASH') {
 1122:                 $uname = $queued{$reqkey}{'ownername'};
 1123:                 $udom  = $queued{$reqkey}{'ownerdom'};
 1124:                 if (($udom =~ /^$match_domain$/) && ($uname =~ /^$match_username$/)) {
 1125:                     $result = &retrieve_settings($dom,$cnum,$udom,$uname);
 1126:                 } else {
 1127:                     if ($env{'form.crstype'} eq 'community') {
 1128:                         $warning = &mt('Invalid username or domain for community requestor');
 1129:                     } else {
 1130:                         $warning = &mt('Invalid username or domain for course requestor');
 1131:                     }
 1132:                 }
 1133:             } else {
 1134:                 if ($env{'form.crstype'} eq 'community') {
 1135:                     $warning = &mt('No information was found for this community request.');
 1136:                 } else {
 1137:                     $warning = &mt('No information was found for this course request.');
 1138:                 }
 1139:             }
 1140:         } else {
 1141:             $warning = &mt('No course request ID provided.');
 1142:         }
 1143:     } else {
 1144:         if ($env{'form.crstype'} eq 'any') {
 1145:             $warning = &mt('You do not have rights to view course or community request information.');
 1146:         } elsif ($env{'form.crstype'} eq 'community') {
 1147:             $warning = &mt('You do not have rights to view community request information.');
 1148:         } else {
 1149:             $warning = &mt('You do not have rights to view course request information.');
 1150:         }
 1151:     }
 1152:     return ($uname,$udom,$result,$warning);
 1153: }
 1154: 
 1155: sub enrollment_lcsec_js {
 1156:     my %alerts = &section_check_alerts();
 1157:     my $secname = $alerts{'badsec'};
 1158:     my $secnone = $alerts{'reserved'};
 1159:     my $output = '
 1160: function validateEnrollSections(formname,nextstate) {
 1161:     var badsectotal = 0;
 1162:     var reservedtotal = 0;
 1163:     var secTest = "";
 1164: ';
 1165:     for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 1166:         $output .= "
 1167:     var selSec = 0;
 1168:     for (var j=0; j<document.requestcrs.sec_".$i.".length; j++) {
 1169:         if (document.requestcrs.sec_".$i."[j].checked) {
 1170:             selSec = document.requestcrs.sec_".$i."[j].value;
 1171:         }
 1172:         if (selSec == 1) {
 1173:             secTest = validsection(document.requestcrs.loncapasec_".$i.");
 1174:             if (secTest == 'badsec') {
 1175:                 badsectotal++;
 1176:             }
 1177:             if (secTest == 'reserved') {
 1178:                 reservedtotal++;
 1179:             }
 1180:         }
 1181:     }
 1182: ";
 1183:     }
 1184:     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
 1185:         $output .= "
 1186:     if (document.requestcrs.crosslist_".$i.".checked) {
 1187:         secTest = validsection(document.requestcrs.crosslist_".$i."_lcsec);
 1188:         if (secTest == 'badsec') {
 1189:             badsectotal++;
 1190:         }
 1191:         if (secTest == 'reserved') {
 1192:             reservedtotal++;
 1193:         }
 1194:     }
 1195: ";
 1196:     }
 1197:     $output .= "
 1198:     if (badsectotal>0) {
 1199:         alert('$secname');
 1200:         return false;
 1201:     }
 1202:     if (reservedtotal>0) {
 1203:         alert('$secnone');
 1204:         return false;
 1205:     }
 1206:     formname.state.value= nextstate;
 1207:     formname.submit();
 1208:     return;
 1209: }
 1210: ";
 1211:     return $output;
 1212: }
 1213: 
 1214: sub personnel_lcsec_js {
 1215:     my %alerts = &section_check_alerts();
 1216:     my $secname = $alerts{'badsec'}.'\\n'.$alerts{'separate'};
 1217:     my $secnone = $alerts{'reserved'};
 1218:     my $output = '
 1219: function validatePersonnelSections(formname,nextstate) {
 1220:     var badsectotal = 0;
 1221:     var reservedtotal = 0;
 1222:     var secTest = "";
 1223: ';
 1224:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1225:         $output .= "
 1226:     if (document.requestcrs.person_".$i."_uname.value != '') {
 1227:         secTest = validsection(document.requestcrs.person_".$i."_newsec,'1');
 1228:         if (secTest == 'badsec') {
 1229:             badsectotal++; 
 1230:         }
 1231:         if (secTest == 'reserved') {
 1232:             reservedtotal++;
 1233:         }
 1234:     }
 1235: ";
 1236:     }
 1237:     $output .= "
 1238:     if (badsectotal > 0) {
 1239:         alert('$secname');
 1240:         return false;
 1241:     } else {
 1242:         if (reservedtotal > 0) {
 1243:             alert('$secnone');
 1244:             return false;
 1245:         }
 1246:     }
 1247:     formname.state.value = nextstate;
 1248:     formname.submit();
 1249:     return;
 1250: }
 1251: ";
 1252:     return $output;
 1253: }
 1254: 
 1255: sub section_check_alerts {
 1256:     my %lt = 
 1257:         &Apache::lonlocal::texthash(
 1258:             reserved => "You need to change one or more LON-CAPA section names - none is a reserved word in the system, and may not be used.",
 1259:             badsec => 'You need to change one or more LON-CAPA section names - names may only contain letters or numbers.',
 1260:             separate => 'Separate multiple sections with a comma.'
 1261:         );
 1262:     return %lt;
 1263: }
 1264: 
 1265: sub section_check_javascript {
 1266:     return <<"END";
 1267: function validsection(field,mult) {
 1268:     var str = field.value;
 1269:     var badsec=0;
 1270:     var reserved=0;
 1271:     if (window.RegExp) {
 1272:         var badsecnum=0;
 1273:         var reservednum=0;
 1274:         var pattern=/[^a-zA-Z0-9]/; 
 1275:         str = str.replace(/(^\\s*)|(\\s*\$)/gi,"");
 1276:         str = str.replace(/[ ]{2,}/gi," ");
 1277:         if (mult == '1') {
 1278:             var sections = new Array();
 1279:             sections = str.split(/\\s*[\\s,;:]\\s*/);
 1280:             var i;
 1281:             for (i=0; i<sections.length; i++) {
 1282:                 if ((sections[i] != '') && (sections[i] != undefined) && (sections[i] != null)) {
 1283:                     if (pattern.test(sections[i])) {
 1284:                         badsecnum++;
 1285:                     } else {
 1286:                         if (sections[i] == 'none') {
 1287:                             reservednum++;
 1288:                         }
 1289:                     }
 1290:                 }
 1291:             }
 1292:         } else {
 1293:             if ((str != '') && (str != undefined) && (str != null)) {
 1294:                 if (pattern.test(str)) {
 1295:                     badsecnum++;
 1296:                 } else {
 1297:                     if (str == 'none') {
 1298:                         reservednum++;
 1299:                     }
 1300:                 }
 1301:             }
 1302:         }
 1303:         if (badsecnum > 0) {
 1304:             return 'badsec';
 1305:         }
 1306:         if (reservednum > 0) {
 1307:             return 'reserved';
 1308:         }
 1309:     }
 1310:     return;
 1311: }
 1312: END
 1313: }
 1314: 
 1315: sub close_popup_form {
 1316:     my $close= &mt('Close Window');
 1317:     return << "END";
 1318: <p><form name="displayreq" action="" method="post">
 1319: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
 1320: </form></p>
 1321: END
 1322: }
 1323: 
 1324: sub get_instcode {
 1325:     my ($dom) = @_;
 1326:     my ($instcode,$numtitles);
 1327:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
 1328:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1329:                                              \%cat_order,\@code_order);
 1330:     $numtitles = scalar(@codetitles);
 1331:     if (@code_order > 0) {
 1332:         my $message;
 1333:         foreach my $item (@code_order) {
 1334:             $instcode .= $env{'form.instcode_'.$item};
 1335:         }
 1336:     }
 1337:     return ($instcode,$numtitles);
 1338: }
 1339: 
 1340: sub print_request_form {
 1341:     my ($r,$action,$state,$page,$states,$dom,$newinstcode,$codechk,$checkedcode,
 1342:         $description,$showcredits,$instcredits,$invalidcrosslist) = @_;
 1343:     my $formname = 'requestcrs';
 1344:     my ($next,$prev,$message,$output,$codepicker,$crstype);
 1345:     $prev = $states->{$action}[$page-1];
 1346:     $next = $states->{$action}[$page+1];
 1347:     my %navtxt = &Apache::lonlocal::texthash (
 1348:                                                prev => 'Back',
 1349:                                                next => 'Next',
 1350:                                              );
 1351:     $crstype = $env{'form.crstype'};
 1352:     $r->print('<br /><form name="'.$formname.'" method="post" action="/adm/requestcourse">');
 1353:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk,
 1354:         @disallowed);
 1355:     if ($crstype eq 'official') {
 1356:         if ($env{'form.instcode'} ne '') {
 1357:             $instcode = $env{'form.instcode'};
 1358:         } elsif ($newinstcode ne '') {
 1359:             $instcode = $newinstcode;
 1360:         }
 1361:         if ($checkedcode) {
 1362:             if ($codechk eq 'valid') {
 1363:                 $message = '<div class="LC_info">'.
 1364:                            &mt('The chosen course category [_1] is valid.','<b>'.
 1365:                            $instcode.'</b>').
 1366:                            '<input type="hidden" name="instcode" value="'.
 1367:                            $instcode.'" /></div>';
 1368:             } else {
 1369:                 $message = '<div class="LC_warning">'.
 1370:                             &mt('No course was found matching your choice of institutional course category.');
 1371:                 if ($codechk ne '') {
 1372:                     $message .= '<br />'.$codechk;
 1373:                 }
 1374:                 $message .= '</div>';
 1375:                 $prev = 'crstype';
 1376:             }
 1377:             $r->print($message);
 1378:         }
 1379:     }
 1380:     if ($prev eq 'crstype') {
 1381:         if ($crstype eq 'official') {
 1382:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1383:                                                      \%cat_order,\@code_order);
 1384:         }
 1385:         if (@code_order > 0) {
 1386:             $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
 1387:                                            \%cat_titles,\%cat_order);
 1388:             if ($codepicker) {
 1389:                 $r->print(&mt('Specify the course to be created.').
 1390:                           '<div>'.&Apache::lonhtmlcommon::start_pick_box().
 1391:                           $codepicker.
 1392:                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
 1393:             } else {
 1394:                 $next = $states->{$action}[$page+2];
 1395:                 $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
 1396:             }
 1397:         } else {
 1398:             if ($crstype eq 'official') {
 1399:                 $next = $states->{$action}[$page+2];
 1400:             }
 1401:             $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
 1402:         }
 1403:     } elsif ($prev eq 'codepick') {
 1404:         if ($instcode eq '') {
 1405:             $prev = $states->{$action}[$page-2];
 1406:         }
 1407:         $r->print(&courseinfo_form($dom,$formname,$crstype,$next,$description));
 1408:     } elsif ($state eq 'enrollment') {
 1409:         if ($crstype eq 'official') {
 1410:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1411:                                                      \%cat_order,\@code_order);
 1412:         }
 1413:         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
 1414:                                          \%cat_titles,\%cat_order,\@code_order,
 1415:                                          $showcredits,$instcredits,$invalidcrosslist));
 1416:     } elsif ($state eq 'personnel') {
 1417:         $r->print(&print_personnel_menu($dom,$formname,$crstype,$invalidcrosslist));
 1418:     } elsif ($state eq 'review') {
 1419:         my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg);
 1420:         my $now = time;
 1421:         for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1422:             my $personname = $env{'form.person_'.$i.'_uname'};
 1423:             my $persondom = $env{'form.person_'.$i.'_dom'};
 1424:             if (($personname =~ /^$match_username$/) && 
 1425:                 ($persondom =~ /^$match_domain$/)) {
 1426:                 if (&Apache::lonnet::domain($persondom)) {
 1427:                     my $personhome = 
 1428:                         &Apache::lonnet::homeserver($personname,$persondom);
 1429:                     if ($personhome eq 'no_host') {
 1430:                         if ($persondom ne $dom) {
 1431:                             my $skipuser = 1;
 1432:                             if ($env{'user.role.dc./'.$persondom.'/'}) {
 1433:                                 my ($start,$end) = split('.',$env{'user.role.dc./'.$persondom.'/'});
 1434:                                 if (((!$start) || ($start < $now)) && 
 1435:                                     ((!$end) || ($end > $now))) {
 1436:                                     $skipuser = 0;
 1437:                                 }
 1438:                             }
 1439:                             if ($skipuser) {
 1440:                                 push(@disallowed,$i);
 1441:                                 $disallowmsg{$i} = &mt('[_1] was excluded because new users need to be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
 1442:                                 next;
 1443:                             }
 1444:                         }
 1445:                         my $usertype = &get_usertype($persondom,$personname,\%curr_rules,\%got_rules);
 1446:                         if (&Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype)) {
 1447:                             my ($allowed,$msg,$authtype,$authparam) = 
 1448:                                 &check_newuser_rules($persondom,$personname,
 1449:                                     \%alerts,\%rulematch,\%inst_results,
 1450:                                     \%curr_rules,\%got_rules);
 1451:                             if ($allowed) {
 1452:                                 my %domdefaults = &Apache::lonnet::get_domain_defaults($persondom);
 1453:                                 if ($usertype eq 'official') {
 1454:                                     if ($authtype eq '') {
 1455:                                         $authtype = $domdefaults{'auth_def'};
 1456:                                         $authparam = $domdefaults{'auth_arg_def'};
 1457:                                     }
 1458:                                 } elsif ($usertype eq 'unofficial') {
 1459:                                     if ($authtype eq '') {
 1460:                                         $authtype = 'internal';
 1461:                                         $authparam = '';
 1462:                                     }
 1463:                                 } else {
 1464:                                     $authtype = $domdefaults{'auth_def'};
 1465:                                     $authparam = $domdefaults{'auth_arg_def'};
 1466:                                 }
 1467:                                 if (($authtype eq '') ||
 1468:                                     (($authtype =~/^krb/) && ($authparam eq ''))) {
 1469:                                     push(@disallowed,$i);
 1470:                                     $disallowmsg{$i} = &mt('[_1] was excluded because institutional information is incomplete for this new user.','<tt>'.$personname.':'.$persondom.'</tt>');
 1471:                                     next;
 1472:                                 }
 1473:                                 if (ref($inst_results{$personname.':'.$persondom}) eq 'HASH') {
 1474:                                     if ($inst_results{$personname.':'.$persondom}{'lastname'} ne '') {
 1475:                                         $env{'form.person_'.$i.'_lastname'} = $inst_results{$personname.':'.$persondom}{'lastname'};
 1476:                                     }
 1477:                                     if ($inst_results{$personname.':'.$persondom}{'firstname'} ne '') {
 1478:                                         $env{'form.person_'.$i.'_firstname'} = $inst_results{$personname.':'.$persondom}{'firstname'};
 1479:                                     }
 1480:                                     if ($inst_results{$personname.':'.$persondom}{'permanentemail'} ne '') {
 1481:                                         $env{'form.person_'.$i.'_emailaddr'} = $inst_results{$personname.':'.$persondom}{'permanentemail'};
 1482:                                     }
 1483:                                 }
 1484:                             } else {
 1485:                                 push(@disallowed,$i);
 1486:                                 $disallowmsg{$i} = &mt('[_1] was excluded because the username violated format rules for the domain','<tt>'.$personname.':'.$persondom.'</tt>');  
 1487:                             }
 1488:                         } else {
 1489:                             push(@disallowed,$i);
 1490:                             $disallowmsg{$i} = &mt('[_1] was excluded because you may not request new users in the domain','<tt>'.$personname.':'.$persondom.'</tt>');
 1491:                         }
 1492:                     } else {
 1493:                         my %userenv = 
 1494:                             &Apache::lonnet::userenvironment($persondom,$personname,'lastname','firstname','permanentemail');
 1495:                         if ($env{'form.person_'.$i.'_lastname'} eq '') {
 1496:                             $env{'form.person_'.$i.'_lastname'} = $userenv{'lastname'};
 1497:                         }
 1498:                         if ($env{'form.person_'.$i.'_firstname'} eq '') {
 1499:                             $env{'form.person_'.$i.'_firstname'} = $userenv{'firstname'};
 1500:                         }
 1501:                         if ($env{'form.person_'.$i.'_emailaddr'} eq '') {
 1502:                             $env{'form.person_'.$i.'_emailaddr'} = $userenv{'permanentemail'};
 1503:                         }
 1504:                     }
 1505:                 } elsif ($personname ne '') {
 1506:                     push(@disallowed,$i);
 1507:                     $disallowmsg{$i} = &mt('[_1] was excluded because the domain is invalid','<tt>'.$personname.':'.$persondom.'</tt>');
 1508:                 }
 1509:             } elsif ($personname ne '') {
 1510:                 push(@disallowed,$i);
 1511:                 $disallowmsg{$i} = &mt('[_1] was excluded because the username or domain is invalid.','<tt>'.$personname.':'.$persondom.'</tt>');
 1512:             }
 1513:         }
 1514:         my $cnum;
 1515:         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
 1516:             $cnum = $env{'form.origcnum'};
 1517:         } else {
 1518:             my $gentype = 'Course';
 1519:             if ($crstype eq 'community') {
 1520:                 $gentype = 'Community';
 1521:             }
 1522:             $cnum = &Apache::lonnet::generate_coursenum($dom,$gentype);
 1523:         }
 1524:         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1525:                                                  \%cat_order,\@code_order);
 1526:         if ($crstype eq 'community') {
 1527:             $r->print('<h3>'.&mt('Review community request details before submission').'</h3>');
 1528:         } else {
 1529:             $r->print('<h3>'.&mt('Review course request details before submission').'</h3>');
 1530:         }
 1531:         $r->print(&print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg,$instcredits).
 1532:                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
 1533:         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 1534:                                                      $env{'user.domain'});
 1535:         my $postprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'review',$env{'user.name'},
 1536:                                                               $env{'user.domain'},$fullname,$env{'form.cdescr'});
 1537:         if (ref($postprocess) eq 'HASH') {
 1538:             if ($postprocess->{'reviewweb'}) {
 1539:                 $r->print($postprocess->{'reviewweb'});
 1540:             }
 1541:         }
 1542:         if ($crstype eq 'community') {
 1543:             $navtxt{'next'} = &mt('Submit community request');
 1544:         } else {
 1545:             $navtxt{'next'} = &mt('Submit course request');
 1546:         }
 1547:     }  elsif ($state eq 'process') {
 1548:         if ($crstype eq 'official') {
 1549:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1550:                                                      \%cat_order,\@code_order);
 1551:         }
 1552:         my $lonhost = $r->dir_config('lonHostID');
 1553:         my ($storeresult,$result) = &print_request_outcome($lonhost,$dom,\@codetitles,
 1554:                                                            \@code_order,$instcredits);
 1555:         $r->print($result);
 1556:         if (($storeresult eq 'ok') || ($storeresult eq 'created')) {
 1557:             if ($storeresult eq 'ok') {
 1558:                 $r->print('<p><a href="/adm/requestcourse?action=view&state=details&showdom='.$dom.'&cnum='. $env{'form.cnum'}.'">'.
 1559:                           &mt('Modify this request').'</a>'.('&nbsp;'x4).
 1560:                           '<a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
 1561:             }
 1562:             if (&Apache::loncoursequeueadmin::author_prompt()) {
 1563:                 &print_author_prompt($r,$env{'form.action'},$env{'form.cnum'},$env{'form.showdom'},
 1564:                                      $env{'form.crstype'},$storeresult);
 1565:             } elsif ($storeresult eq 'created') {
 1566:                 $r->print('<p><a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
 1567:             }
 1568:         }
 1569:     } elsif ($state eq 'reqauthor') {
 1570:         my ($result,@links); 
 1571:         if ($env{'form.requestauthor'}) {
 1572:             $r->print(&Apache::loncoursequeueadmin::process_reqauthor(\$result));
 1573:             if ($result eq 'created') {
 1574:                 my $role = 'au';
 1575:                 my $spec = "$role./$env{'form.showdom'}/";
 1576:                 push(@links,&mt('Enter your Authoring Space with role: [_1]',
 1577:                                 '<a href="/adm/roles?selectrole=1&amp;'.$spec.'=1">'.
 1578:                                 &Apache::lonnet::plaintext($role).'</a>'));
 1579:             }
 1580:         }
 1581:         if (($env{'form.disposition'} eq 'created') &&
 1582:             ($env{'form.cnum'} =~ /^$match_courseid$/) &&
 1583:             ($env{'form.showdom'} =~ /^$match_domain$/)) {
 1584:             my ($spec,$area,$role,$type);
 1585:             my $role = 'cc';
 1586:             my $spec = "$role./$env{'form.showdom'}/$env{'form.cnum'}";
 1587:             my $type = 'Course';
 1588:             if ($env{'form.crstype'} eq 'community') {
 1589:                 $type = 'Community';
 1590:             }
 1591:             my $showrole = &Apache::lonnet::plaintext($role,$type);
 1592:             unshift(@links,&mt('Enter new course with role: [_1]',
 1593:                                '<a href="/adm/roles?selectrole=1&amp;'.$spec.'=1">'.$showrole.'</a>'));
 1594:         }
 1595:         if (@links > 1) {
 1596:             $r->print(&mt('New roles will be listed on your [_1]Roles[_2] page.',
 1597:                           '<a href="/adm/roles">','</a>').'&nbsp'.&mt('Choose a role:').
 1598:                        '<ul>');
 1599:             foreach my $link (@links) {
 1600:                 $r->print('<li>'.$link.'</li>');
 1601:             }
 1602:             $r->print('</ul>');
 1603:         } elsif (@links == 1) {
 1604:             $r->print('<p>'.$links[0].'</p>');
 1605:         }
 1606:     }
 1607:     my @excluded = &get_excluded_elements($dom,$states,$action,$state,$showcredits);
 1608:     if ($state eq 'personnel') {
 1609:         push(@excluded,'persontotal');
 1610:     }
 1611:     if ($state eq 'review') {
 1612:         if (@disallowed > 0) {
 1613:             my @items = qw(uname dom lastname firstname emailaddr hidedom role newsec);
 1614:             my @currsecs = &current_lc_sections();
 1615:             if (@currsecs) {
 1616:                 push(@items,'sec');
 1617:             }
 1618:             my $count = 0;
 1619:             for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1620:                 unless ($env{'form.person_'.$i.'_uname'} eq '') {
 1621:                     if (grep(/^$i$/,@disallowed)) {
 1622:                         foreach my $item (@items) {
 1623:                             $env{'form.person_'.$i.'_'.$item} = '';
 1624:                         }
 1625:                     } else { 
 1626:                         foreach my $item (@items) {
 1627:                             $env{'form.person_'.$count.'_'.$item} = $env{'form.person_'.$i.'_'.$item};
 1628:                         }
 1629:                     }
 1630:                 }
 1631:                 $count ++;
 1632:             }
 1633:             $env{'form.persontotal'} = $count;
 1634:         }
 1635:     }
 1636:     if ($state eq 'enrollment') {
 1637:         push(@excluded,('sectotal','crosslisttotal'));
 1638:     }
 1639:     if (($state eq 'process') || ($state eq 'reqauthor')) {
 1640:         $r->print('</form>');
 1641:     } else {
 1642:         $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
 1643:         &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
 1644:                             $navtxt{'next'},$state);
 1645:     }
 1646:     return;
 1647: }
 1648: 
 1649: sub print_author_prompt {
 1650:     my ($r,$action,$cnum,$showdom,$crstype,$storeresult) = @_;
 1651:     $r->print('<h3>'.&mt('Access to Authoring Space').'</h3>'.
 1652:               '<p>'.
 1653:               &mt('Although assessment items can be created directly inside a course, such items only use part of the assessment capabilities of LON-CAPA.').
 1654:               '<br />'.
 1655:               &mt('By contrast, items created in Authoring Space, then imported into a course, can use all of the features of the assessment engine.').'</p>'.
 1656:               '<p>'.&mt('Request Authoring Space access now?').
 1657:               '<span class="LC_nobreak">&nbsp;'.
 1658:               '<label><input type="radio" name="requestauthor" value="1" />'.&mt('Yes').'</label>'.
 1659:               ('&nbsp;'x2).
 1660:               '<label><input type="radio" name="requestauthor" value="0" checked="checked"/>'.&mt('No').'</label>'.
 1661:               '</span></p>'.
 1662:               '<input type="submit" name="newauthor" value="'.&mt('Submit authoring request').'" />'.
 1663:               '<input type="hidden" name="state" value="reqauthor" />'.
 1664:               '<input type="hidden" name="action" value="'.$action.'" />'.
 1665:               '<input type="hidden" name="cnum" value="'.$cnum.'" />'.
 1666:               '<input type="hidden" name="showdom" value="'.$showdom.'" />'.
 1667:               '<input type="hidden" name="crstype" value="'.$crstype.'" />'.
 1668:               '<input type="hidden" name="disposition" value="'.$storeresult.'" />'.
 1669:               '<br />');
 1670: }    
 1671: 
 1672: sub get_usertype {
 1673:     my ($persondom,$personname,$curr_rules,$got_rules) = @_;
 1674:     my ($rules,$ruleorder) =
 1675:         &Apache::lonnet::inst_userrules($persondom,'username');
 1676:     my $usertype = &Apache::lonuserutils::check_usertype($persondom,$personname,
 1677:                                                          $rules,$curr_rules,$got_rules);
 1678:     return $usertype;
 1679: }
 1680: 
 1681: sub check_newuser_rules {
 1682:     my ($persondom,$personname,$alerts,$rulematch,$inst_results,$curr_rules,
 1683:         $got_rules) = @_;
 1684:     my $allowed = 1;
 1685:     my $newuser = 1;
 1686:     my ($checkhash,$userchkmsg,$authtype,$authparam);
 1687:     my $checks = { 'username' => 1 };
 1688:     $checkhash->{$personname.':'.$persondom} = { 'newuser' => $newuser };
 1689:     &Apache::loncommon::user_rule_check($checkhash,$checks,$alerts,$rulematch,
 1690:                                         $inst_results,$curr_rules,$got_rules);
 1691:     if (ref($alerts->{'username'}) eq 'HASH') {
 1692:         if (ref($alerts->{'username'}{$persondom}) eq 'HASH') {
 1693:             my $domdesc =
 1694:                 &Apache::lonnet::domain($persondom,'description');
 1695:             if ($alerts->{'username'}{$persondom}{$personname}) {
 1696:                 if (ref($curr_rules->{$persondom}) eq 'HASH') {
 1697:                     $userchkmsg =
 1698:                         &Apache::loncommon::instrule_disallow_msg('username',
 1699:                                                                   $domdesc,1).
 1700:                         &Apache::loncommon::user_rule_formats($persondom,
 1701:                             $domdesc,$curr_rules->{$persondom}{'username'},
 1702:                             'username');
 1703:                 }
 1704:                 $allowed = 0;
 1705:             }
 1706:         }
 1707:     }
 1708:     if ($allowed) {
 1709:         if (ref($rulematch) eq 'HASH') {
 1710:             if (ref($rulematch->{$personname.':'.$persondom}) eq 'HASH') {
 1711:                 my $matchedrule = $rulematch->{$personname.':'.$persondom}{'username'};
 1712:                 my ($rules,$ruleorder) =
 1713:                     &Apache::lonnet::inst_userrules($persondom,'username');
 1714:                 if (ref($rules) eq 'HASH') {
 1715:                     if (ref($rules->{$matchedrule}) eq 'HASH') {
 1716:                         $authtype = $rules->{$matchedrule}{'authtype'};
 1717:                         $authparam = $rules->{$matchedrule}{'authparm'};
 1718:                     }
 1719:                 }
 1720:             }
 1721:         }
 1722:     }
 1723:     return ($allowed,$userchkmsg,$authtype,$authparam);
 1724: }
 1725: 
 1726: sub get_excluded_elements {
 1727:     my ($dom,$states,$action,$state,$showcredits) = @_;
 1728:     my @excluded = ('counter');
 1729:     my ($elements,$instcredits) = &form_elements($dom,$showcredits);
 1730:     if (ref($states) eq 'HASH') {
 1731:         if (ref($states->{$action}) eq 'ARRAY') {
 1732:             my @items = @{$states->{$action}};
 1733:             my $numitems = scalar(@items);
 1734:             if ($numitems) {
 1735:                 for (my $i=$numitems-1; $i>=0; $i--) {
 1736:                     if ((ref($elements) eq 'HASH') && 
 1737:                         (ref($elements->{$action}) eq 'HASH')) {
 1738:                         if (ref($elements->{$action}{$items[$i]}) eq 'HASH') {
 1739:                             foreach my $key (keys(%{$elements->{$action}{$items[$i]}})) {
 1740:                                 push(@excluded,$key);
 1741:                             }
 1742:                         }
 1743:                     }
 1744:                     last if ($items[$i] eq $state);
 1745:                 }
 1746:             }
 1747:         }
 1748:     }
 1749:     if (grep(/^instcode_/,@excluded)) {
 1750:         push(@excluded,'instcode');
 1751:     }
 1752:     return @excluded;
 1753: }
 1754: 
 1755: sub print_enrollment_menu {
 1756:     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order,
 1757:         $showcredits,$instcredits,$invalidcrosslist) =@_;
 1758:     my ($sections,$autoenroll,$access_dates,$output,$hasauto,$hascredits,
 1759:         $creditsrow,$domdefcredits);
 1760:     my $starttime = time;
 1761:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
 1762: 
 1763:     my %accesstitles = (
 1764:                           'start' => 'Default start access',
 1765:                            'end'   => 'Default end access',
 1766:                        );
 1767:     my %enrolltitles = (
 1768:                            'start' => 'Start auto-enrollment',
 1769:                            'end'   => 'End auto-enrollment',
 1770:                        );
 1771:     if ($showcredits) {
 1772:         unless ($env{'form.crstype'} eq 'community') {
 1773:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 1774:             $domdefcredits = $domdefs{$env{'form.crstype'}.'credits'};
 1775:         }
 1776:     }
 1777:     if ($env{'form.crstype'} eq 'official') {
 1778:         if (&Apache::lonnet::auto_run('',$dom)) {
 1779:             $output = &show_invalid_crosslists($invalidcrosslist);
 1780:             my ($section_form,$crosslist_form);
 1781:             if ($instcode ne '') {
 1782:                 $section_form = &inst_section_selector($dom,$instcode);
 1783:                 if ($section_form eq '') {
 1784:                     my $sectotal = $env{'form.sectotal'};
 1785:                     if (!$sectotal) {
 1786:                         $sectotal = 1;
 1787:                     }
 1788:                     if ($env{'form.addsection'}) {
 1789:                         $sectotal ++;
 1790:                     }
 1791:                     for (my $i=0; $i<$sectotal; $i++) {
 1792:                         $section_form .= &sections_form($dom,$instcode,$i);
 1793:                     }
 1794:                     if ($section_form) {
 1795:                         $section_form .=
 1796:                     &Apache::lonhtmlcommon::row_title(&mt('Add another')).
 1797:                     '<input name="sectotal" type="hidden" value="'.$sectotal.'" />'.
 1798:                     '<input name="addsection" type="checkbox" value="'.$sectotal.'"'.
 1799:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
 1800:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
 1801:                     }
 1802:                 }
 1803:             }
 1804:             if ($section_form) {
 1805:                 $sections = &Apache::lonhtmlcommon::row_headline().
 1806:                             '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Sections').
 1807:                             '&nbsp;'.&mt('Sections for auto-enrollment').'</h3>'.
 1808:                             &Apache::lonhtmlcommon::row_closure(1).
 1809:                             $section_form;
 1810:             }
 1811:             my $crosslisttotal = $env{'form.crosslisttotal'};
 1812:             if (!$crosslisttotal) {
 1813:                 $crosslisttotal = 1;
 1814:             }
 1815:             if ($env{'form.addcrosslist'}) {
 1816:                 $crosslisttotal ++;
 1817:             }
 1818:             for (my $i=0; $i<$crosslisttotal; $i++) {
 1819:                 $crosslist_form .= &coursecode_form($dom,'crosslist',$codetitles, 
 1820:                                                   $cat_titles,$cat_order,$i);
 1821:             }
 1822:             if ($crosslist_form) { 
 1823:                 $crosslist_form .= 
 1824:                     &Apache::lonhtmlcommon::row_title(&mt('Add another')).
 1825:                     '<input name="crosslisttotal" type="hidden" value="'.$crosslisttotal.'" />'.
 1826:                     '<input name="addcrosslist" type="checkbox" value="'.$crosslisttotal.'"'.
 1827:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
 1828:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
 1829:                 $sections .=  &Apache::lonhtmlcommon::row_headline.
 1830:                               '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Crosslist').'&nbsp;'.&mt('Crosslisted courses for auto-enrollment').'</h3>'.
 1831:                               &Apache::lonhtmlcommon::row_closure(1).
 1832:                               $crosslist_form;
 1833:             }
 1834:             $hasauto = 1;
 1835:             $autoenroll = 
 1836:                 &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic('Course_Request_Autoadd').'&nbsp;'.&mt('Add registered students automatically')).
 1837:                 '<span class="LC_nobreak"><label>'.
 1838:                 '<input type="radio" name="autoadds" value="1">'.
 1839:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
 1840:                 '<input type="radio" name="autoadds" value="0" checked="checked">'.
 1841:                 &mt('No').'</label></span>'.
 1842:                 &Apache::lonhtmlcommon::row_closure(1).
 1843:                 &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic('Course_Request_Autodrop').'&nbsp;'.&mt('Drop unregistered students automatically')).
 1844:                 '<span class="LC_nobreak"><label>'.
 1845:                 '<input type="radio" name="autodrops" value="1">'.
 1846:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
 1847:                 '<input type="radio" name="autodrops" value="0" checked="checked">'.
 1848:                 &mt('No').'</label></span>'. 
 1849:                 &Apache::lonhtmlcommon::row_closure(1).
 1850:                 &date_setting_table($starttime,$endtime,$formname,'enroll',
 1851:                                     $hasauto,undef,%enrolltitles);
 1852:             if ($showcredits) {
 1853:                 if ($instcredits) {
 1854:                     $creditsrow = &mt('[quant,_1,credit]',$instcredits);
 1855:                 } else {
 1856:                     $creditsrow = '<span class="LC_nobreak">'.
 1857:                                   '<input type="text" size="3" name="coursecredits"'.
 1858:                                   ' value="'.$domdefcredits.'" />';
 1859:                 }
 1860:                 $hascredits = 1;
 1861:             }
 1862:         }
 1863:     } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
 1864:         if ($showcredits) {
 1865:             $creditsrow = '<span class="LC_nobreak">'.
 1866:                           '<input type="text" size="3" name="coursecredits"'.
 1867:                           ' value="'.$domdefcredits.'"/>';
 1868:             $hascredits = 1;
 1869:         }
 1870:     }
 1871:     my $access_dates = 
 1872:         &date_setting_table($starttime,$endtime,$formname,'access',$hasauto,
 1873:                             $hascredits,%accesstitles);
 1874:     $output .= &Apache::lonhtmlcommon::start_pick_box();
 1875:     if ($sections) {
 1876:         $output .=  $sections;
 1877:     }
 1878:     if ($autoenroll) {
 1879:         $output .= &Apache::lonhtmlcommon::row_headline('Auto-enroll').
 1880:                    '<h3>'.&mt('Auto-enrollment settings').'</h3>'.
 1881:                    &Apache::lonhtmlcommon::row_closure(1).
 1882:                    $autoenroll;
 1883:     }
 1884:     if ($access_dates) {
 1885:         my $header = &mt('Access dates for students');
 1886:         if ($env{'form.crstype'} eq 'community') {
 1887:             $header = &mt('Access dates for community members');
 1888:         }
 1889:         $output .= &Apache::lonhtmlcommon::row_headline('Access').
 1890:                    '<h3>'.$header.'</h3>'.
 1891:                    &Apache::lonhtmlcommon::row_closure(1).
 1892:                    $access_dates;
 1893:     }
 1894:     if ($creditsrow) {
 1895:         $output .= &Apache::lonhtmlcommon::row_headline('Credits').
 1896:                    '<h3>'.&mt('Credits earned by students').'</h3>'.
 1897:                    &Apache::lonhtmlcommon::row_closure(1).
 1898:                    &Apache::lonhtmlcommon::row_title(&mt('Default credits')).
 1899:                    $creditsrow.
 1900:                    &Apache::lonhtmlcommon::row_closure(1);
 1901:     }
 1902:     return '<div>'.&Apache::lonhtmlcommon::start_pick_box().$output.
 1903:            &Apache::lonhtmlcommon::end_pick_box().'</div>';
 1904: }
 1905: 
 1906: sub show_invalid_crosslists {
 1907:     my ($invalidcrosslist) = @_;
 1908:     my $output;
 1909:     if (ref($invalidcrosslist) eq 'ARRAY') {
 1910:         if (@{$invalidcrosslist} > 0) {
 1911:             $output = '<div class="LC_warning">'.
 1912:                       &mt('The following crosslisted courses were invalid:').'<ul>';
 1913:             foreach my $item (@{$invalidcrosslist}) {
 1914:                 $output .= '<li>'.$item.'</li>';
 1915:             }
 1916:             $output .= '</ul></div><br />';
 1917:         }
 1918:     }
 1919:     return $output;
 1920: }
 1921: 
 1922: 
 1923: sub inst_section_selector {
 1924:     my ($dom,$instcode) = @_;
 1925:     my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
 1926:     my $sectotal = scalar(@sections);
 1927:     my $output;
 1928:     if ($sectotal) {
 1929:         $output .=  &Apache::lonhtmlcommon::row_title(&mt('Sections of [_1]',$instcode)).
 1930:                     &Apache::loncommon::start_data_table().
 1931:                     &Apache::loncommon::start_data_table_row().
 1932:                     '<th>'.&mt('Include?').'<input type="hidden" name="sectotal" '.
 1933:                     'value="'.$sectotal.'"  /></th>'.
 1934:                     '<th>'.&mt('Institutional Section').'</th>'.
 1935:                     '<th>'.&Apache::loncommon::help_open_topic('Course_Request_LCSection').
 1936:                           '&nbsp;'.&mt('LON-CAPA section').'</th>'.
 1937:                     &Apache::loncommon::end_data_table_row();
 1938:         for (my $i=0; $i<@sections; $i++) {
 1939:             my $colflag = $i%2;
 1940:             my $secon = ' checked="checked"';
 1941:             my $secoff = '';
 1942:             if ($env{'form.origcnum'}) {
 1943:                 $secoff = $secon;
 1944:                 $secon='';
 1945:             }
 1946:             $output .= &Apache::loncommon::start_data_table_row().
 1947:                        '<td><label><input type="radio" name="sec_'.$i.
 1948:                        '"'.$secon.' value="1" />'.&mt('Yes').'</label>'.
 1949:                        ('&nbsp;'x2).'<label><input type="radio" name="sec_'.$i.
 1950:                         '"'.$secoff.' value="0" />'.&mt('No').'</label></td>'.
 1951:                        '<td align="center">'.$sections[$i].
 1952:                        '<input type="hidden" name="secnum_'.$i.'" value="'.
 1953:                        $sections[$i].'" /></td>'.
 1954:                        '<td><input type="text" size="10" name="loncapasec_'.$i.
 1955:                        '" value="'.$sections[$i].'" /></td>'.
 1956:                        &Apache::loncommon::end_data_table_row();
 1957:         }
 1958:         $output .= &Apache::loncommon::end_data_table().
 1959:                    &Apache::lonhtmlcommon::row_closure();
 1960:     }
 1961:     return $output;
 1962: }
 1963: 
 1964: sub date_setting_table {
 1965:     my ($starttime,$endtime,$formname,$prefix,$hasauto,$hascredits,%datetitles)=@_;
 1966:     my ($perpetual,$table);
 1967:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'start',
 1968:                                                         $starttime,'','','',1,'','','',1);
 1969:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'end',
 1970:                                                       $endtime,'','','',1,'','','',1);
 1971:     my $closure = '';
 1972:     if ($prefix eq 'access') {
 1973:         $perpetual = ' <span class="LC_nobreak"><label>'.
 1974:                      '<input type="checkbox" name="no_end_date" />'.
 1975:                      &mt('No end date').'</label></span>';
 1976:         unless ($hascredits) {
 1977:             $closure = '1';
 1978:         }
 1979:     }
 1980: 
 1981:     my %help_item = (
 1982:                       access => {
 1983:                                   start => 'Course_Request_Access_Start', 
 1984:                                   end   => 'Course_Request_Access_End',
 1985:                                 },
 1986:                       enroll => {
 1987:                                   start => 'Course_Request_Enroll_Start',
 1988:                                   end   => 'Course_Request_Enroll_End',
 1989:                                 },
 1990:                      );
 1991:     if ($hasauto) {
 1992:         $help_item{'access'}{'start'} = 'Course_Request_RegAccess_Start';
 1993:         $help_item{'access'}{'end'}   = 'Course_Request_RegAccess_End';
 1994:     }
 1995: 
 1996:     $table = &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'start'}).
 1997:              '&nbsp;'.&mt($datetitles{'start'})).$startform.
 1998:              &Apache::lonhtmlcommon::row_closure(1).
 1999:              &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'end'}).
 2000:              '&nbsp;'.&mt($datetitles{'end'})).$endform.$perpetual.
 2001:              &Apache::lonhtmlcommon::row_closure($closure);
 2002:     return $table;
 2003: }
 2004: 
 2005: sub print_personnel_menu {
 2006:     my ($dom,$formname,$crstype,$invalidcrosslist) = @_;
 2007:     my $output;
 2008:     if ($crstype eq 'official') {
 2009:         if (&Apache::lonnet::auto_run('',$dom)) {
 2010:             $output .= &show_invalid_crosslists($invalidcrosslist);
 2011:         }  
 2012:     }
 2013:     $output .= '<div>'.&Apache::lonhtmlcommon::start_pick_box();
 2014:     my $persontotal = $env{'form.persontotal'};
 2015:     if ((!defined($persontotal)) || (!$persontotal)) {
 2016:         $persontotal = 1;
 2017:     }
 2018:     if ($env{'form.addperson'}) {
 2019:         $persontotal ++;
 2020:     }
 2021:     my @items = ('uname','dom','lastname','firstname','emailaddr','hidedom');
 2022: 
 2023:     my $type = 'Course';
 2024:     if ($crstype eq 'community') {
 2025:         $type = 'Community';
 2026:     }
 2027:     my $roleoptions;
 2028:     my @roles = &Apache::lonuserutils::roles_by_context('course','',$type);
 2029:     foreach my $role (@roles) {
 2030:         my $plrole = &Apache::lonnet::plaintext($role,$type);
 2031:         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
 2032:     }
 2033:     my %customroles=&Apache::lonuserutils::my_custom_roles();
 2034:     if (keys(%customroles) > 0) {
 2035:         foreach my $cust (sort(keys(%customroles))) {
 2036:             my $custrole="cr/$env{'user.domain'}/$env{'user.name'}/$cust";
 2037:             $roleoptions .= '  <option value="'.$custrole.'">'.$cust.'</option>'."\n";
 2038:         }
 2039:     }
 2040: 
 2041:     my @currsecs = &current_lc_sections();
 2042: 
 2043:     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
 2044:     if (@currsecs) {
 2045:         my $existsize = scalar(@currsecs);
 2046:         if ($existsize > 3) {
 2047:             $existsize = 3;
 2048:         }
 2049:         if ($existsize > 1) {
 2050:             $existmult = ' multiple="multiple" size="'.$existsize.'" ';
 2051:         }
 2052:         @currsecs = sort { $a <=> $b } (@currsecs);
 2053:         $existtitle = &mt('Official').':&nbsp;';
 2054:         $existops = '<option value="">'.&mt('None').'</option>';
 2055:         foreach my $sec (@currsecs) {
 2056:             $existops .= '<option value="'.$sec.'">'.$sec.'</option>'."\n";
 2057:         }
 2058:         $seccolspan = ' colspan="2"';
 2059:         $newtitle = &mt('Other').':&nbsp;';
 2060:     }
 2061: 
 2062:     if ($persontotal) {
 2063:         my %lt = &Apache::lonlocal::texthash(
 2064:             community => 'Requestor is automatically assigned Coordinator role.',
 2065:             official => 'Requestor is automatically assigned Course Coordinator role.',
 2066:         );
 2067:         $lt{'unofficial'} = $lt{'official'};
 2068:         $lt{'textbook'} = $lt{'textbook'};
 2069:         $output .= &Apache::lonhtmlcommon::row_headline().
 2070:                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Personnel').'&nbsp;'.$lt{$crstype}.' '.&mt('Include other personnel?').'</h3>';
 2071:     }
 2072:     for (my $i=0; $i<$persontotal; $i++) { 
 2073:         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
 2074:         my $linkargstr = join("','",@linkargs);
 2075:         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value="" size="20" />';
 2076:         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
 2077:                        "'person_".$i."_hidedom','person_".$i."_uname'".');';
 2078:         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
 2079:                                                             1,$onchange).
 2080:                         '<input type="hidden" name="person_'.$i.'_hidedom" value="" />';
 2081:         my %form_elems;
 2082:         foreach my $item (@items) {
 2083:             next if (($item eq 'dom') || ($item eq 'uname') || ($item eq 'hidedom'));
 2084:             $form_elems{$item} = '<input type="text" name="person_'.$i.'_'.$item.'" '.
 2085:                                  'value="" readonly="readonly" />';
 2086:         }
 2087:         my $roleselector = '<select name="person_'.$i.'_role">'."\n".
 2088:                            $roleoptions.'</select>';
 2089:         my $sectionselector;
 2090:         if (@currsecs) {
 2091:             $sectionselector = $existtitle.'<select name="person_'.$i.'_sec"'.
 2092:                                $existmult.'>'."\n".$existops.'</select>'.('&nbsp;' x3);
 2093:         }
 2094:         $sectionselector .= $newtitle.
 2095:             '<input type="text" name="person_'.$i.'_newsec" size="15" value="" />'."\n";
 2096:         my $usersrchlinktxt = &mt('Search for user');
 2097:         my $usersrchlink =  &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
 2098:                             $usersrchlinktxt);
 2099:         my $userchklinktxt = &mt('Check username');
 2100:         my $userchklink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
 2101:                             $userchklinktxt,'checkusername');
 2102:         $output .= 
 2103:             &Apache::lonhtmlcommon::row_title(&mt('Additional Personnel')).
 2104:             '<table><tr><td align="center" valign="middle"><b>'.$usersrchlink.'</b></td>'."\n".
 2105:             '<td align="left" valign="top" colspan="2"><span class="LC_nobreak">'.
 2106:             &mt('Username').':&nbsp;'.$uname_form.'&nbsp;'.$userchklink.'</span><br />'."\n".
 2107:             '<span class="LC_nobreak">'.&mt('Domain').':&nbsp;'.$udom_form.'</span></td>'.
 2108:             '</tr>'."\n".'<tr>'.
 2109:             '<td align="center" valign="top">'.&mt('First Name').'<br />'.$form_elems{'firstname'}.'</td>'."\n".
 2110:             '<td align="center" valign="top">'.&mt('Last Name').'<br />'.$form_elems{'lastname'}.'</td>'."\n".
 2111:             '<td align="center" valign="top">'.&mt('E-mail').'<br />'.$form_elems{'emailaddr'}.'</td></tr>'."\n".
 2112:             '<tr><td align="center" valign="top">'.&Apache::loncommon::help_open_topic('Course_Roles').'&nbsp;'.&mt('Role').'<br />'.$roleselector.'</td>'."\n".
 2113:             '<td'.$seccolspan.' align="center" valign="top">'.
 2114:             &Apache::loncommon::help_open_topic('Course_Request_Rolesection').'&nbsp;'.&mt('LON-CAPA Section(s)').'<br />'.$sectionselector.'</td>'."\n".
 2115:             '</tr></table>'.&Apache::lonhtmlcommon::row_closure();
 2116:     }
 2117:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Add another')).
 2118:                '<input name="persontotal" type="hidden" value="'.$persontotal.'" />'.
 2119:                '<input name="addperson" type="checkbox" value="'.$persontotal.'"'.
 2120:                ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
 2121:                "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1).
 2122:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
 2123:     if ($crstype eq 'community') {
 2124:         $output .= '<p>'.&mt('You may also add users later, once the community has been created, by using the "Manage community users" link, accessible from the "Main Menu".').'</p>';
 2125:     } else {
 2126:         $output .= '<p>'.&mt('You may also add users later, once the course has been created, by using the "Manage course users" link, accessible from the "Main Menu".').'</p>';
 2127:     }
 2128:     return $output;
 2129: }
 2130: 
 2131: sub current_lc_sections {
 2132:     my @currsecs;
 2133:     if ($env{'form.sectotal'}) {
 2134:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 2135:             if ($env{'form.sec_'.$i}) { 
 2136:                 if (defined($env{'form.loncapasec_'.$i})) {
 2137:                     my $lcsec = $env{'form.loncapasec_'.$i};
 2138:                     unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
 2139:                         push(@currsecs,$lcsec);
 2140:                     }
 2141:                 }
 2142:             }
 2143:         }
 2144:     }
 2145:     return @currsecs;
 2146: }
 2147: 
 2148: sub sorted_request_history {
 2149:     my ($dom,$action,$curr_req) = @_;
 2150:     my ($after,$before,$statusfilter,$crstypefilter);
 2151:     if ($env{'form.status'} ne '') {
 2152:         $statusfilter = $env{'form.status'};
 2153:     }
 2154:     if ($env{'form.crstype'} ne '') {
 2155:         $crstypefilter = $env{'form.crstype'};
 2156:     }
 2157:     if (ref($curr_req) eq 'HASH') {
 2158:         $after = $curr_req->{'requested_after_date'},
 2159:         $before = $curr_req->{'requested_before_date'};
 2160:         $statusfilter = $curr_req->{'status'};
 2161:         $crstypefilter = $curr_req->{'crstype'};
 2162:     }
 2163:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
 2164:                                            $env{'user.name'},'^status:'.$dom);
 2165:     my %queue_by_date;
 2166:     my ($types,$typenames) = &Apache::loncommon::course_types();
 2167:     foreach my $key (keys(%statusinfo)) {
 2168:         if ($action eq 'view') {
 2169:             next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending'));
 2170:         } else {
 2171:             next unless (($statusfilter eq 'any') ||
 2172:                           ($statusfilter eq $statusinfo{$key}));
 2173:         }
 2174:         (undef,my($cdom,$cnum)) = split(':',$key);
 2175:         next if ($cdom ne $dom);   
 2176:         my $requestkey = $cdom.'_'.$cnum;
 2177:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 2178:             my %history = &Apache::lonnet::restore($requestkey,'courserequests',
 2179:                                                    $env{'user.domain'},$env{'user.name'});
 2180:             my $entry;
 2181:             my $reqtime = $history{'reqtime'};
 2182:             my $lastupdate = $history{'timestamp'};
 2183:             my $crstype = $history{'crstype'};
 2184:             my $disposition = $history{'disposition'};
 2185:             my $status = $history{'status'};
 2186:             my $uniquecode = $history{'code'}; 
 2187:             if ($action eq 'view') {
 2188:                 next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
 2189:             } else {
 2190:                 next if (($reqtime < $after) || ($reqtime > $before));
 2191:             }
 2192:             next unless (($crstypefilter eq 'any') || 
 2193:                          ($crstypefilter eq $crstype));
 2194:             if ($action eq 'view') {
 2195:                 next unless (($disposition eq 'approval') || 
 2196:                              ($disposition eq 'pending'));
 2197:             }
 2198:             if (ref($history{'details'}) eq 'HASH') {
 2199:                 $entry = $requestkey.':'.$crstype.':'.
 2200:                          &escape($history{'details'}{'cdescr'});
 2201:                 if ($action eq 'log') {
 2202:                     $entry .= ':'.$uniquecode.':'.$lastupdate.':';
 2203:                     if ($statusinfo{$key} ne '') {
 2204:                         $entry .= $statusinfo{$key};
 2205:                     } elsif ($status ne '') {
 2206:                         $entry .= $status;
 2207:                     } else {
 2208:                         $entry .= $disposition;
 2209:                     }
 2210:                 }
 2211:                 if ($crstype eq 'official') {
 2212:                     $entry .= ':'.&escape($history{'details'}{'instcode'}); 
 2213:                 }
 2214:             }
 2215:             if ($entry ne '') {
 2216:                 if (exists($queue_by_date{$reqtime})) {
 2217:                     if (ref($queue_by_date{$reqtime}) eq 'ARRAY') {
 2218:                         push(@{$queue_by_date{$reqtime}},$entry);
 2219:                     }
 2220:                 } else {
 2221:                     @{$queue_by_date{$reqtime}} = ($entry);
 2222:                 }
 2223:             }
 2224:         }
 2225:     }
 2226:     return %queue_by_date;
 2227: }
 2228: 
 2229: sub print_request_status {
 2230:     my ($dom,$action) = @_;
 2231:     my %queue_by_date = &sorted_request_history($dom,$action);
 2232:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
 2233:     my $formname = 'requestcrs';
 2234:     my ($types,$typenames) = &Apache::loncommon::course_types();
 2235:     my $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 2236: 
 2237:               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
 2238:               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n".
 2239:               '<input type="hidden" name="showdom" value="" />'."\n".
 2240:               '<input type="hidden" name="cnum" value="" />'."\n";
 2241:     if (@sortedtimes > 0) {
 2242:         my $desctitle;
 2243:         if ($env{'form.crstype'} eq 'any') {
 2244:             $desctitle = &mt('Course/Community Description')
 2245:         } elsif ($env{'form.crstype'} eq 'community') {
 2246:             $desctitle = &mt('Community Description')
 2247:         } else {
 2248:             $desctitle = &mt('Course Description');
 2249:         }
 2250:         $output .= &Apache::loncommon::start_data_table().
 2251:                    &Apache::loncommon::start_data_table_header_row().
 2252:                    '<th>'.&mt('Action').'</th>'.
 2253:                    '<th>'.$desctitle.'</th>'.
 2254:                    '<th>'.&mt('Domain').'</th>';
 2255:         if ($env{'form.crstype'} eq 'any') {
 2256:             $output .= '<th>'.&mt('Type').'</th>';
 2257:         }
 2258:         if (($env{'form.crstype'} eq 'any') || ($env{'form.crstype'} eq 'official')) {
 2259:             $output .= '<th>'.&mt('Institutional Code').'</th>';
 2260:         }
 2261:         $output .= '<th>'.&mt('Date requested').'</th>'.
 2262:                    &Apache::loncommon::end_data_table_header_row();
 2263:         my $count = 0;
 2264:         foreach my $item (@sortedtimes) {
 2265:             my $showtime = &Apache::lonlocal::locallocaltime($item);
 2266:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
 2267:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
 2268:                     my ($key,$type,$desc,$instcode) = split(':',$request);
 2269:                     my ($cdom,$cnum) = split('_',$key);
 2270:                     $output .= &Apache::loncommon::start_data_table_row().
 2271:      '<td><input type="button" value="'.&mt('Select').'" onclick="javascript:chooseRequest('."'$cdom','$cnum'".')" /></td>'.
 2272:      '<td>'.&unescape($desc).'</td>'.
 2273:      '<td>'.$cdom.'</td>';
 2274:                     if ($env{'form.crstype'} eq 'any') {
 2275:                         my $typename;
 2276:                         if (ref($typenames) eq 'HASH') {
 2277:                             $typename = &mt($typenames->{$type});
 2278:                         }
 2279:                         if ($typename eq '') {
 2280:                             $typename = &mt('Unknown type');
 2281:                         }
 2282:                         $output .= '<td>'.$typename.'</td>';
 2283:                     }
 2284:                     if (($env{'form.crstype'} eq 'any') || 
 2285:                         ($env{'form.crstype'} eq 'official')) {
 2286:                         my $showinstcode;
 2287:                         if ($type eq 'official') {
 2288:                             $showinstcode = &unescape($instcode);
 2289:                         } else {
 2290:                             $showinstcode = &mt('Not applicable');
 2291:                         }
 2292:                         $output .= '<td>'.$showinstcode.'</td>';
 2293:                     }
 2294:                     $output .= '<td>'.$showtime.'</td>'.
 2295:                                &Apache::loncommon::end_data_table_row();
 2296:                 }
 2297:             }
 2298:         }
 2299:         $output .= &Apache::loncommon::end_data_table();
 2300:     } else {
 2301:         if ($env{'form.crstype'} eq 'any') {
 2302: $output .= '<div>'.&mt('You have no matching course or community requests awaiting approval by a Domain Coordinator or held in a queue pending administrative action at your institution.').'</div>';
 2303:         } elsif ($env{'form.crstype'} eq 'community') {
 2304:             $output .= '<div>'.&mt('You have no matching community requests awaiting approval by a Domain Coordinator or held in a queue pending administrative action at your institution.').'</div>';
 2305:         } else {
 2306:             $output .= '<div>'.&mt('You have no matching course requests awaiting approval by a Domain Coordinator or held in a queue pending administrative action at your institution.').'</div>';
 2307:         }
 2308:     }
 2309:     $output .= '
 2310: <br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />';
 2311:     return $output;
 2312: }
 2313: 
 2314: sub print_cancel_request {
 2315:     my ($dom,$cnum) = @_;
 2316:     my $requestkey = $dom.'_'.$cnum;
 2317:     my ($result,$output);
 2318:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 2319:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',
 2320:                                                $env{'user.domain'},$env{'user.name'});
 2321:         my $timestamp = $history{'reqtime'};
 2322:         my $crstype = $history{'crstype'};
 2323:         my $status = $history{'status'};
 2324:         if (($status eq 'cancelled') || ($status eq 'created')) { 
 2325:             if ($status eq 'cancelled') {
 2326:                 $output = &mt('This request has already been cancelled.');
 2327:             } elsif ($status eq 'created') {
 2328:                 $output = &mt('This request has already been processed, and a course created.');
 2329:             }
 2330:             $output = &mt('No further action will be taken');
 2331:         } elsif (ref($history{'details'}) eq 'HASH') {
 2332:             my ($types,$typename) = &Apache::loncommon::course_types();
 2333:             my $showtype = $crstype;
 2334:             if (defined($typename->{$crstype})) {
 2335:                 $showtype = $typename->{$crstype}; 
 2336:             }
 2337:             $output = '<p>'.&Apache::loncommon::start_data_table().
 2338:                       &Apache::loncommon::start_data_table_header_row().
 2339:                       '<th>'.&mt('Description').'</th><th>'.&mt('Requested').'</th>'.
 2340:                       '<th>'.&mt('Type').'</th>'.
 2341:                       &Apache::loncommon::end_data_table_header_row().
 2342:                       &Apache::loncommon::start_data_table_row().
 2343:                       '<td>'.$history{details}{'cdescr'}.'</td><td>'.
 2344:                       &Apache::lonlocal::locallocaltime($timestamp).'</td>'.
 2345:                       '<td>'.$showtype.'</td>'.
 2346:                       &Apache::loncommon::end_data_table_row().
 2347:                       &Apache::loncommon::end_data_table().
 2348:                       '<br /><div class="LC_warning">';
 2349:             if ($crstype eq 'community') {
 2350:                 $output .= &mt('Cancelling the request will remove it from the queue of pending community requests').'</div>';
 2351:             } else {
 2352:                 $output .= &mt('Cancelling the request will remove it from the queue of pending course requests').'</div>';
 2353:             }
 2354:             $result = 'ok';
 2355:         } else {
 2356:             $output = '<div class="LC_error">'.&mt('No record exists for the course ID').'</div>';
 2357:         }
 2358:     } else {
 2359:         $output = '<div class="LC_error">'.&mt('Invalid course ID').'</div>';
 2360:     }
 2361:     return ($result,$output);
 2362: }
 2363: 
 2364: sub viewrequest_javascript {
 2365:     my ($formname,$next) = @_;
 2366:     return <<"ENDJS";
 2367: 
 2368: function chooseRequest(cdom,cnum) {
 2369:     document.$formname.showdom.value = cdom;
 2370:     document.$formname.cnum.value = cnum;
 2371:     nextPage(document.$formname,'$next');
 2372: }
 2373: 
 2374: ENDJS
 2375: }
 2376: 
 2377: sub viewdetails_javascript {
 2378:     my ($formname) = @_;
 2379:     return << "ENDJS";
 2380: 
 2381: function nextPage(formname,nextstate) {
 2382:     if (nextstate == "modify") { 
 2383:         formname.state.value = "personnel";
 2384:         formname.action.value = "new";
 2385:     } else {
 2386:         formname.state.value = nextstate;
 2387:     }
 2388:     formname.submit();
 2389: }
 2390: 
 2391: function backPage(formname,prevstate) {
 2392:     formname.state.value = prevstate;
 2393:     formname.submit();
 2394: }
 2395: 
 2396: ENDJS
 2397: }
 2398: 
 2399: sub viewcancel_javascript {
 2400:     my $alert = &mt('Are you sure you want to cancel this request?').'\\n'.
 2401:                 &mt('Your request will be removed.');
 2402:     return << "ENDJS";
 2403: function nextPage(formname,nextstate) {
 2404:     if (confirm('$alert')) {
 2405:         formname.state.value = nextstate;
 2406:         formname.submit();
 2407:     }
 2408:     return;
 2409: }
 2410: 
 2411: ENDJS
 2412: }
 2413: 
 2414: sub print_request_logs {
 2415:     my ($r,$dom,$jscript,$loaditems,$crumb,$usetabs) = @_;
 2416:     my $title;
 2417:     if ($env{'form.crstype'} eq 'community') {
 2418:         $title = 'Community Request Logs';
 2419:     } elsif ($env{'form.crstype'} eq 'any') {
 2420:         $title = 'Course/Community Request Logs';
 2421:     } else {
 2422:         $title = 'Course Request Logs';
 2423:     }
 2424:     $r->print(&header($title,$jscript,$loaditems).$crumb);
 2425:     if ($usetabs) {
 2426:         &startContentScreen($r,'textbooklogs');
 2427:     }
 2428:     my $formname = 'requestcrs';
 2429:     $r->print('<form action="/adm/requestcourse" method="post" name="'.$formname.'" onsubmit="javascript:setPage(this);">'."\n".
 2430:               '<input type="hidden" name="action" value="log" />'."\n".
 2431:               '<input type="hidden" name="state" value="display" />'."\n");
 2432:     # set defaults
 2433:     my $now = time();
 2434:     my $defstart = $now - (7*24*3600); #7 days ago
 2435:     my %defaults = (
 2436:                      page                => '1',
 2437:                      show                => '10',
 2438:                      crstype             => 'any',
 2439:                      status              => 'any',
 2440:                      requested_before_date => $now,
 2441:                      requested_after_date  => $defstart,
 2442:                    );
 2443:     my ($types,$typenames) = &Apache::loncommon::course_types();
 2444:     my $more_records = 0;
 2445:     my %curr;
 2446:     foreach my $item ('show','page','crstype','status') {
 2447:         $curr{$item} = $env{'form.'.$item};
 2448:     }
 2449:     $curr{'requested_after_date'} = &Apache::lonhtmlcommon::get_date_from_form('requested_after_date');
 2450:     $curr{'requested_before_date'} = &Apache::lonhtmlcommon::get_date_from_form('requested_before_date');
 2451:     foreach my $key (keys(%defaults)) {
 2452:         if ($curr{$key} eq '') {
 2453:             $curr{$key} = $defaults{$key};
 2454:         }
 2455:     }
 2456:     my ($statuses,$statusnames) = &reqstatus_names($curr{'crstype'});
 2457:     $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
 2458:               &requestlog_display_filter($formname,\%curr));
 2459:     my %queue_by_date = &sorted_request_history($dom,$env{'form.action'},\%curr);
 2460:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
 2461:     my $showntablehdr = 0;
 2462:     my $tablehdr = &Apache::loncommon::start_data_table().
 2463:                    &Apache::loncommon::start_data_table_header_row().
 2464:                    '<th>&nbsp;</th><th>'.&mt('Request Date').'</th>'.
 2465:                    '<th>'.&mt('Description').'</th>';
 2466:     if ($curr{'crstype'} eq 'any') {
 2467:         $tablehdr .= '<th>'.&mt('Course Type').'</th>';
 2468:     }
 2469:     if (($curr{'crstype'} eq 'official') || ($curr{'crstype'} eq 'any')) {
 2470:         $tablehdr .= '<th>'.&mt('Institutional Code').'</th>';
 2471:     }
 2472:     my $showuniquecode;
 2473:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
 2474:     if (($curr{'status'} eq 'any') || ($curr{'status'} eq 'created')) {
 2475:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2476:             if (ref($domconfig{'requestcourses'}{'uniquecode'}) eq 'HASH') {
 2477:                 if ($curr{'crstype'} eq 'any') {
 2478:                     my @types = qw(official unofficial community textbook);
 2479:                     foreach my $type (@types) {
 2480:                         if ($domconfig{'requestcourses'}{'uniquecode'}{$type}) {
 2481:                             $showuniquecode = 1;
 2482:                             last;
 2483:                         }
 2484:                     }
 2485:                 } elsif ($domconfig{'requestcourses'}{'uniquecode'}{$curr{'crstype'}}) {
 2486:                     $showuniquecode = 1;
 2487:                 }
 2488:             }
 2489:         }
 2490:     }
 2491:     if ($showuniquecode) {
 2492:         $tablehdr .= '<th>'.&mt('Unique Code').'</th>';    
 2493:     }
 2494:     if ($curr{'status'} eq 'any') {
 2495:         $tablehdr .= '<th>'.&mt('Status').'</th>';
 2496:     } elsif ($curr{'status'} eq 'created') {
 2497:         $tablehdr .= '<th>'.&mt('Creation Date').'</th>';
 2498:     } elsif ($curr{'status'} eq 'cancelled') {
 2499:         $tablehdr .= '<th>'.&mt('Cancellation Date').'</th>';
 2500:     } elsif ($curr{'status'} eq 'rejected') {
 2501:         $tablehdr .= '<th>'.&mt('Rejection Date').'</th>';
 2502:     }
 2503:     $tablehdr .= &Apache::loncommon::end_data_table_header_row();
 2504:     my ($minshown,$maxshown);
 2505:     $minshown = 1;
 2506:     my $count = 0;
 2507:     if ($curr{'show'} ne &mt('all')) {
 2508:         $maxshown = $curr{'page'} * $curr{'show'};
 2509:         if ($curr{'page'} > 1) {
 2510:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
 2511:         }
 2512:     }
 2513:     my $norecords;
 2514:     if (@sortedtimes > 0) {
 2515:         foreach my $item (@sortedtimes) {
 2516:             if ($curr{'show'} ne &mt('all')) {
 2517:                 if ($count >= $curr{'page'} * $curr{'show'}) {
 2518:                     $more_records = 1;
 2519:                     last;
 2520:                 }
 2521:             }
 2522:             $count ++;
 2523:             next if ($count < $minshown);
 2524:             if (!$showntablehdr) {
 2525:                 $r->print($tablehdr);
 2526:                 $showntablehdr = 1;
 2527:             }
 2528:             my $showtime = &Apache::lonlocal::locallocaltime($item);
 2529:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
 2530:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
 2531:                     my ($key,$crstype,$desc,$uniquecode,$timestamp,$status,$instcode) = split(':',$request);
 2532:                     my ($cdom,$cnum) = split('_',$key);
 2533:                     my $output = &Apache::loncommon::start_data_table_row().
 2534:      '<td>'.$count.'</td>'.
 2535:      '<td>'.$showtime.'</td>'.
 2536:      '<td>'.&unescape($desc).'</td>';
 2537:                     if ($curr{'crstype'} eq 'any') {
 2538:                         my $typename;
 2539:                         if (ref($typenames) eq 'HASH') {
 2540:                             $typename = &mt($typenames->{$crstype});
 2541:                         }
 2542:                         if ($typename eq '') {
 2543:                             $typename = &mt('Unknown type');
 2544:                         }
 2545:                         $output .= '<td>'.$typename.'</td>';
 2546:                     }
 2547:                     if (($curr{'crstype'} eq 'any') ||
 2548:                         ($curr{'crstype'} eq 'official')) {
 2549:                         my $showinstcode;
 2550:                         if ($crstype eq 'official') {
 2551:                             $showinstcode = &unescape($instcode);
 2552:                         } else {
 2553:                             $showinstcode = &mt('Not applicable');
 2554:                         }
 2555:                         $output .= '<td>'.$showinstcode.'</td>';
 2556:                     }
 2557:                     if ($showuniquecode) {
 2558:                         if ($status eq 'created') {
 2559:                             $output .= '<td>'.$uniquecode.'</td>';
 2560:                         } else {
 2561:                             $output .= '<td>'.&mt('Not applicable').'</td>';
 2562:                         }    
 2563:                     }
 2564:                     if ($curr{'status'} eq 'any') {
 2565:                         my $statusname = &mt('Unknown status');
 2566:                         if (ref($statusnames) eq 'HASH') {
 2567:                             if ($statusnames->{$status} ne '') {
 2568:                                 $statusname = $statusnames->{$status};
 2569:                             }
 2570:                         }
 2571:                         if (($status eq 'created') || ($status eq 'cancelled') ||
 2572:                             ($status eq 'rejected')) {
 2573:                             $statusname .= ' '.&Apache::lonlocal::locallocaltime($timestamp);
 2574:                         }
 2575:                         $output .= '<td>'.$statusname.'</td>';
 2576:                     } elsif (($status eq 'created') || ($status eq 'cancelled') ||
 2577:                              ($status eq 'rejected')) {
 2578:                          $output .= '<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>';
 2579:                     }
 2580:                     $output .= &Apache::loncommon::end_data_table_row();
 2581:                     $r->print($output);
 2582:                 }
 2583:             }
 2584:         }
 2585:         if ($showntablehdr) {  
 2586:             $r->print(&Apache::loncommon::end_data_table());
 2587:             if (($curr{'page'} > 1) || ($more_records)) {
 2588:                 $r->print('<table><tr>');
 2589:                 if ($curr{'page'} > 1) {
 2590:                     $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
 2591:                 }
 2592:                 if ($more_records) {
 2593:                     $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
 2594:                 }
 2595:                 $r->print('</tr></table>');
 2596:                 $r->print(<<"ENDSCRIPT");
 2597: <script type="text/javascript">
 2598: // <![CDATA[
 2599: function chgPage(caller) {
 2600:     if (caller == 'previous') {
 2601:         document.$formname.page.value --;
 2602:     }
 2603:     if (caller == 'next') {
 2604:         document.$formname.page.value ++;
 2605:     }
 2606:     document.$formname.submit();
 2607:     return;
 2608: }
 2609: // ]]>
 2610: </script>
 2611: ENDSCRIPT
 2612:             }
 2613:         } else {
 2614:             $norecords = 1;
 2615:         }
 2616:     } else {
 2617:         $norecords = 1;
 2618:     }
 2619:     if ($norecords) {
 2620:         $r->print('<p class="LC_info">'.
 2621:                   &mt('There are no records to display').
 2622:                   '</p>');
 2623:     }
 2624:     if ($usetabs) {
 2625:         $r->print('<input type="hidden" name="tabs" value="on" />');
 2626:     }
 2627:     $r->print('</form>');
 2628:     if ($usetabs) {
 2629:         &endContentScreen($r);
 2630:     }
 2631:     $r->print(&Apache::loncommon::end_page());
 2632:     return;
 2633: }
 2634: 
 2635: sub reqstatus_names {
 2636:     my ($crstype) = @_;
 2637:     my @statuses = qw(created approval pending rejected cancelled);
 2638:     my %statusnames =
 2639:             &Apache::lonlocal::texthash (
 2640:                         created   => 'Created',
 2641:                         approval  => 'Queued pending approval',
 2642:                         pending   => 'Queued pending validation',
 2643:                         rejected  => 'Request rejected',
 2644:                         cancelled => 'Request cancelled',
 2645:             );
 2646:     if (($crstype eq 'official') || ($crstype eq 'unofficial') || ($crstype eq 'textbook')) {
 2647:         $statusnames{'created'} = &mt('Course created');
 2648:     } elsif ($crstype eq 'community') {
 2649:         $statusnames{'created'} = &mt('Community created');
 2650:     }
 2651:     return (\@statuses,\%statusnames);
 2652: }
 2653: 
 2654: sub requestlog_display_filter {
 2655:     my ($formname,$curr) = @_;
 2656:     my $nolink = 1;
 2657:     my $output = '<table><tr><td valign="top">'.
 2658:                  '<span class="LC_nobreak"><b>'.&mt('Records/page:').'</b></span><br />'.
 2659:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
 2660:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
 2661:                  '</td><td>&nbsp;&nbsp;</td>';
 2662:     my $startform =
 2663:         &Apache::lonhtmlcommon::date_setter($formname,'requested_after_date',
 2664:                                             $curr->{'requested_after_date'},undef,
 2665:                                             undef,undef,undef,undef,undef,undef,$nolink);
 2666:     my $endform =
 2667:         &Apache::lonhtmlcommon::date_setter($formname,'requested_before_date',
 2668:                                             $curr->{'requested_before_date'},undef,
 2669:                                             undef,undef,undef,undef,undef,undef,$nolink);
 2670:     $output .= '<td valign="top"><b>'.&mt('Window during which course/community was requested:').'</b><br />'.
 2671:                '<table><tr><td>'.&mt('After:').
 2672:                '</td><td>'.$startform.'</td></tr>'.
 2673:                '<tr><td>'.&mt('Before:').'</td>'.
 2674:                '<td>'.$endform.'</td></tr></table>'.
 2675:                '</td>'.
 2676:                '<td>&nbsp;&nbsp;</td>';
 2677:     my ($types,$typenames) = &Apache::loncommon::course_types();
 2678:     if (ref($types) eq 'ARRAY') {
 2679:         if (@{$types} > 1) {
 2680:             $output .= '<td valign="top"><b>'.
 2681:                        &mt('Course Type:').'</b><br /><select name="crstype">';
 2682:             my $selstr = '';
 2683:             if ($curr->{'crstype'} eq 'any') {
 2684:                 $selstr = ' selected="selected"';
 2685:             }
 2686:             $output .= '<option value="any"'.$selstr.'>'.&mt('All types').'</option>'."\n";
 2687:             foreach my $crstype (@{$types}) {
 2688:                 my $selstr = '';
 2689:                 if ($curr->{'crstype'} eq $crstype) {
 2690:                     $selstr = ' selected="selected"';
 2691:                 }
 2692:                 my $typename = $crstype;
 2693:                 if (ref($typenames) eq 'HASH') {
 2694:                     if ($typenames->{$crstype} ne '') {
 2695:                         $typename = $typenames->{$crstype};
 2696:                     }
 2697:                 }
 2698:                 $output .= '<option value="'.$crstype.'"'.$selstr.'>'.$typename.'</option>'."\n";
 2699:             }
 2700:             $output .= '</select></td>';
 2701:         }
 2702:     }
 2703:     my ($statuses,$statusnames) = &reqstatus_names($curr->{'crstype'});
 2704:     if (ref($statuses) eq 'ARRAY') {
 2705:         if (@{$statuses} > 1) {
 2706:             $output .= '<td valign="top"><b>'.
 2707:                        &mt('Request Status:').'</b><br /><select name="status">';
 2708:             my $selstr = '';
 2709:             if ($curr->{'status'} eq 'any') {
 2710:                 $selstr = ' selected="selected"';
 2711:             }
 2712:             $output .= '<option value="any"'.$selstr.'>'.&mt('Any status').'</option>'."\n";
 2713:             foreach my $status (@{$statuses}) {
 2714:                 my $selstr = '';
 2715:                 if ($curr->{'status'} eq $status) {
 2716:                     $selstr = ' selected="selected"';
 2717:                 }
 2718:                 my $statusname = $status;
 2719:                 if (ref($statusnames) eq 'HASH') {
 2720:                     if ($statusnames->{$status} ne '') {
 2721:                         $statusname = $statusnames->{$status};
 2722:                     }
 2723:                 }
 2724:                 $output .= '<option value="'.$status.'"'.$selstr.'>'.$statusname.'</option>'."\n";
 2725:             }
 2726:             $output .= '</select></td>';
 2727:         }
 2728:     }
 2729:     $output .= '</tr></table>';
 2730: 
 2731:     # Update Display button
 2732:     $output .= '<p>'.
 2733:                '<input type="submit" value="'.&mt('Update Display').'" />'.
 2734:                '</p><hr />';
 2735:     return $output;
 2736: }
 2737: 
 2738: sub print_review {
 2739:     my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
 2740:         $disallowed,$disallowmsg,$instcredits) = @_;
 2741:     my ($types,$typename) = &Apache::loncommon::course_types();
 2742:     my ($owner,$ownername,$owneremail);
 2743:     if ($uname eq '' || $udom eq '') {
 2744:         $uname = $env{'user.name'};
 2745:         $udom = $env{'user.domain'};
 2746:     }
 2747:     $owner = $uname.':'.$udom;
 2748:     $ownername = &Apache::loncommon::plainname($uname,$udom,'first');
 2749:     my %emails = &Apache::loncommon::getemails($uname,$udom);
 2750:     foreach my $email ('permanentemail','critnotification','notification') {
 2751:         $owneremail = $emails{$email};
 2752:         last if ($owneremail ne '');
 2753:     }
 2754:     my ($inst_headers,$inst_values,$crstypename,$enroll_headers,$enroll_values,
 2755:         $section_headers,$section_values,$personnel_headers,$personnel_values);
 2756: 
 2757:     $crstypename = $env{'form.crstype'};
 2758:     if (ref($typename) eq 'HASH') {
 2759:         unless ($typename->{$env{'form.crstype'}} eq '') {
 2760:             $crstypename = &mt($typename->{$env{'form.crstype'}});
 2761:         }
 2762:     }
 2763:     my $category = 'Course';
 2764:     if ($env{'form.crstype'} eq 'community') {
 2765:         $category = 'Community';
 2766:     }
 2767: 
 2768:     $inst_headers = '<th>'.&mt('Description').'</th><th>'.&mt('Type').'</th>';
 2769:     $inst_values = '<td>'.$env{'form.cdescr'}.'</td><td>'.$crstypename.'</td>';
 2770: 
 2771:     my $enrollrow_title = &mt('Default Access Dates').'<br />'.
 2772:                           '('.&Apache::lonnet::plaintext('st',$category).')';
 2773:     if ($env{'form.crstype'} eq 'official') {
 2774:         if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH')) {
 2775:             foreach my $title (@{$codetitles}) {
 2776:                 if ($env{'form.instcode_'.$title} ne '') {
 2777:                     $inst_headers .= '<th>'.$title.'</th>';
 2778:                     my $longitem = $env{'form.instcode_'.$title};
 2779:                     if (ref($cat_titles->{$title}) eq 'HASH') {
 2780:                         if ($cat_titles->{$title}{$env{'form.instcode_'.$title}} ne '') {
 2781:                             $longitem = $cat_titles->{$title}{$env{'form.instcode_'.$title}};
 2782:                         }
 2783:                     }
 2784:                     $inst_values .= '<td>'.$longitem.'</td>';
 2785:                 }
 2786:             }
 2787:         }
 2788:         $inst_headers .= '<th>'.&mt('Credits').'</th>';
 2789:         if ($instcredits) {
 2790:             $inst_values .= '<td>'.$instcredits.'</td>';
 2791:         } else {
 2792:             $inst_values .= '<td>'.$env{'form.coursecredits'}.'</td>';
 2793:         }
 2794:         if (&Apache::lonnet::auto_run('',$dom)) {
 2795:             $enrollrow_title = &mt('Enrollment');
 2796:             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
 2797:                               '<th>'.&mt('Automatic Drops').'</th>'.
 2798:                               '<th>'.&mt('Enrollment Starts').'</th>'.
 2799:                               '<th>'.&mt('Enrollment Ends').'</th>';
 2800:             $section_headers = '<th>'.&mt('Sections').'</th>'.
 2801:                                '<th>'.&mt('Crosslistings').'</th>';
 2802: 
 2803:             my ($enrollstart,$enrollend) = &dates_from_form('enrollstart','enrollend');
 2804:             my @autoroster = (&mt('No'),&mt('Yes'));
 2805:             $enroll_values = '<td>'.$autoroster[$env{'form.autoadds'}].'</td>'.
 2806:                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
 2807:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollstart).'</td>'.
 2808:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollend).'</td>';
 2809:             $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
 2810:                               &mt('Institutional section').'</th>'.
 2811:                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
 2812:             my $secinfo;
 2813:             if ($env{'form.sectotal'} > 0) {
 2814:                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 2815:                     if ($env{'form.sec_'.$i}) {
 2816:                         $secinfo .= '<tr><td>'.$env{'form.secnum_'.$i}.'</td><td>';
 2817:                         if ($env{'form.loncapasec_'.$i} ne '') {
 2818:                             $secinfo .= $env{'form.loncapasec_'.$i};
 2819:                         } else {
 2820:                             $secinfo .= &mt('None');
 2821:                         }
 2822:                         $secinfo .= '</td></tr>';
 2823:                     }
 2824:                 }
 2825:             }
 2826:             if ($secinfo eq '') {
 2827:                 $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
 2828:             }
 2829:             $section_values .= $secinfo.'</table></td><td>'.
 2830:                                '<table class="LC_innerpickbox"><tr><th>'.
 2831:                                &mt('Institutional course/section').'</th>'.
 2832:                                '<th>'.&mt('LON-CAPA section').'</th></tr>';
 2833:             my $xlistinfo;
 2834:             my $crosslisttotal = $env{'form.crosslisttotal'};
 2835:             if (!$crosslisttotal) {
 2836:                 $crosslisttotal = 1;
 2837:             }
 2838:             for (my $i=0; $i<$crosslisttotal; $i++) {
 2839:                 if ($env{'form.crosslist_'.$i}) {
 2840:                     $xlistinfo .= '<tr><td>';
 2841:                     if (ref($code_order) eq 'ARRAY') {
 2842:                         if (@{$code_order} > 0) {
 2843:                             foreach my $item (@{$code_order}) {
 2844:                                 $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
 2845:                             }
 2846:                         }
 2847:                     }
 2848:                     $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'}.'</td><td>';
 2849:                     if ($env{'form.crosslist_'.$i.'_lcsec'}) {
 2850:                         $xlistinfo .= $env{'form.crosslist_'.$i.'_lcsec'};
 2851:                     } else {
 2852:                         $xlistinfo .= &mt('None');
 2853:                     }
 2854:                     $xlistinfo .= '</td></tr>';
 2855:                 }
 2856:             }
 2857:             if ($xlistinfo eq '') {
 2858:                 $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
 2859:             }
 2860:             $section_values .= $xlistinfo;
 2861:         }
 2862:         $section_values .= '</table></td>';
 2863:     } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
 2864:         $inst_headers .= '<th>'.&mt('Credits').'</th>';
 2865:         $inst_values .= '<td>'.$env{'form.coursecredits'}.'</td>';
 2866:     }
 2867: 
 2868:     my %ctxt = &clone_text();
 2869:     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
 2870:     if (($env{'form.cloning'}) &&
 2871:         ($env{'form.clonecrs'} =~ /^$match_name$/) && 
 2872:         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
 2873:         my $canclone = &Apache::loncoursequeueadmin::can_clone_course($uname,
 2874:                            $udom,$env{'form.clonecrs'},$env{'form.clonedom'},
 2875:                            $env{'form.crstype'});
 2876:         if ($canclone) {
 2877:             my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
 2878:                               $env{'form.clonecrs'},('description','internal.coursecode'));
 2879:             if (keys(%courseenv) > 0) {
 2880:                 $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
 2881:                 $inst_values .= '<td>'.$courseenv{'description'}.'&nbsp;';
 2882:                 my $cloneinst = $courseenv{'internal.coursecode'};
 2883:                 if ($cloneinst ne '') {
 2884:                     $inst_values .= $cloneinst.' '.&mt('in').' '.$env{'form.clonedom'};
 2885:                 } else {
 2886:                     $inst_values .= &mt('from').' '.$env{'form.clonedom'};
 2887:                 }
 2888:                 $inst_values .= '</td><td>';
 2889:                 if ($env{'form.datemode'} eq 'preserve') {
 2890:                     $inst_values .= $ctxt{'prd'}; 
 2891:                 } elsif ($env{'form.datemode'} eq 'shift') {
 2892:                     $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
 2893:                 } else {
 2894:                     $inst_values .= $ctxt{'ncd'};
 2895:                 }
 2896:                 $inst_values .= '</td>';
 2897:              } else {
 2898:                  $inst_values .= '<td>'.&mt('Unknown').'</td>';
 2899:              }
 2900:          } else {
 2901:              $inst_values .= '<td>'.&mt('Not permitted'),'</td>';
 2902:          }
 2903:     } else {
 2904:         $inst_values .= '<td>'.&mt('None').'</td>';
 2905:     }
 2906:     $enroll_headers .= '<th>'.&mt('Access Starts').'</th>'.
 2907:                        '<th>'.&mt('Access Ends').'</th>';
 2908:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
 2909:     $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessstart).'</td>';
 2910:     if ($accessend == 0) {
 2911:         $enroll_values .= '<td>'.&mt('No end date').'</td>';
 2912:     } else {
 2913:         $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessend).'</td>';
 2914:     }
 2915: 
 2916:     my $container = 'Course';
 2917:     my $ccrole = 'cc';
 2918:     if ($env{'form.crstype'} eq 'community') {
 2919:         $container = 'Community';
 2920:         $ccrole = 'co';
 2921:     }
 2922: 
 2923:     $personnel_headers = '<th>'.&mt('Name').'</th><th>'.&mt('Username:Domain').
 2924:                          '</th><th>'.&mt('Role').'</th><th>'.&mt('LON-CAPA Sections').
 2925:                          '</th>';
 2926: 
 2927:     $personnel_values .= '<tr><td>'.$ownername.'</td><td>'.$owner.'</td>'.
 2928:                          '<td>'.&Apache::lonnet::plaintext($ccrole,$container).'</td>'.
 2929:                          '<td>'.&mt('None').'</td></tr>';
 2930:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 2931:         if ($env{'form.person_'.$i.'_uname'} ne '') {
 2932:             if (ref($disallowed) eq 'ARRAY') {
 2933:                 next if (grep(/^$i$/,@{$disallowed}));
 2934:             } 
 2935:             my @officialsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
 2936:             my @allsecs;
 2937:             foreach my $sec (@officialsecs) {
 2938:                 next unless ($sec =~ /\w/);
 2939:                 next if ($sec =~ /\W/);
 2940:                 next if ($sec eq 'none');
 2941:                 push(@allsecs,$sec);
 2942:             }
 2943:             my $newsec = $env{'form.person_'.$i.'_newsec'};
 2944:             $newsec =~ s/^\s+//;
 2945:             $newsec =~s/\s+$//;
 2946:             my @newsecs = split(/\s*[\s,;:]\s*/,$newsec);
 2947:             foreach my $sec (@newsecs) {
 2948:                 next unless ($sec =~ /\w/); 
 2949:                 next if ($sec =~ /\W/);
 2950:                 next if ($sec eq 'none');
 2951:                 if ($sec ne '') {
 2952:                     unless (grep(/^\Q$sec\E$/,@allsecs)) {
 2953:                         push(@allsecs,$sec); 
 2954:                     }
 2955:                 }
 2956:             }
 2957:             my $showsec;
 2958:             if (@allsecs) {
 2959:                 $showsec = join(', ',@allsecs);
 2960:             }
 2961:             if ($showsec eq '') {
 2962:                 $showsec = &mt('None');
 2963:             }
 2964:             if ($env{'form.person_'.$i.'_role'} eq $ccrole) {
 2965:                 $showsec = &mt('None');
 2966:             }
 2967:             my $role = $env{'form.person_'.$i.'_role'}; 
 2968:             $personnel_values .= 
 2969:                 '<tr><td>'.$env{'form.person_'.$i.'_firstname'}.' '.
 2970:                 $env{'form.person_'.$i.'_lastname'}.'</td>'.
 2971:                 '<td>'.$env{'form.person_'.$i.'_uname'}.':'.
 2972:                 $env{'form.person_'.$i.'_dom'}.'</td>'.
 2973:                 '<td>'.&Apache::lonnet::plaintext($role,$container).'</td>'.
 2974:                 '<td>'.$showsec.'</td></tr>';
 2975:         }
 2976:     }
 2977:     my $output;
 2978:     if (ref($disallowed) eq 'ARRAY') {
 2979:         if (@{$disallowed} > 0) {
 2980:             if (ref($disallowmsg) eq 'HASH') {
 2981:                 $output = '<p class="LC_warning">'.
 2982:                           &mt('Not all requested personnel could be included.').'<ul>'; 
 2983:                 foreach my $item (@{$disallowed}) {
 2984:                     $output .= '<li>'.$disallowmsg->{$item}.'</li>';
 2985:                 }
 2986:                 $output .= '</ul></p>';
 2987:             }
 2988:         }
 2989:     }
 2990:     $output .=    '<div>'.&Apache::lonhtmlcommon::start_pick_box().
 2991:                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
 2992:                   '<table class="LC_innerpickbox"><tr>'.
 2993:                   '<th>'.&mt('Name').'</th>'.
 2994:                   '<th>'.&mt('Username:Domain').'</th>'.
 2995:                   '<th>'.&mt('E-mail address').'</th>'.
 2996:                   '</tr><tr>'."\n".
 2997:                   '<td>'.$ownername.'</td><td>'.$owner.'</td>'.
 2998:                   '<td>'.$owneremail.'</td>'.
 2999:                   '</tr></table>'."\n".
 3000:                   &Apache::lonhtmlcommon::row_closure().
 3001:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
 3002:                   '<table class="LC_innerpickbox"><tr>'.$inst_headers.'</tr>'."\n".
 3003:                   '<tr>'.$inst_values.'</tr></table>'."\n".
 3004:                   &Apache::lonhtmlcommon::row_closure().
 3005:                   &Apache::lonhtmlcommon::row_title($enrollrow_title).
 3006:                   '<table class="LC_innerpickbox"><tr>'.$enroll_headers.'</tr>'."\n".
 3007:                   '<tr>'.$enroll_values.'</tr></table>'."\n".
 3008:                   &Apache::lonhtmlcommon::row_closure();
 3009:     if ($section_headers ne '') {
 3010:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Sections')).
 3011:                    '<table class="LC_innerpickbox"><tr>'.$section_headers.'</tr>'."\n".
 3012:                    '<tr>'.$section_values.'</tr></table>'."\n".
 3013:                    &Apache::lonhtmlcommon::row_closure();
 3014:     }
 3015:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Personnel')).
 3016:                '<table class="LC_innerpickbox"><tr>'.$personnel_headers.'</tr>'."\n".
 3017:                $personnel_values.'</table>'."\n".
 3018:                &Apache::lonhtmlcommon::row_closure(1).
 3019:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
 3020:     return $output;
 3021: }
 3022: 
 3023: sub dates_from_form {
 3024:     my ($startname,$endname) = @_;
 3025:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
 3026:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
 3027:     if ($endname eq 'accessend') {
 3028:         if (exists($env{'form.no_end_date'}) ) {
 3029:             $enddate = 0;
 3030:         }
 3031:     }
 3032:     return ($startdate,$enddate);
 3033: }
 3034: 
 3035: sub courseinfo_form {
 3036:     my ($dom,$formname,$crstype,$next,$description) = @_;
 3037:     my %lt = &Apache::lonlocal::texthash(
 3038:                 official => 'You must provide a (brief) course description.',
 3039:                 community => 'You must provide a (brief) community description.'
 3040:              );
 3041:     $lt{'unofficial'} = $lt{'official'};
 3042:     $lt{'textbook'} = $lt{'official'};
 3043:     my $js_validate = <<"ENDJS";
 3044: <script type="text/javascript">
 3045: // <![CDATA['
 3046: 
 3047: function validateForm() {
 3048:     if ((document.$formname.cdescr.value == "")  || (document.$formname.cdescr.value == "undefined")) {
 3049:         alert('$lt{$crstype}');
 3050:         return;
 3051:     }
 3052:     nextPage(document.$formname,'$next');
 3053: }
 3054: 
 3055: function toggleCloning() {
 3056:     var willclone;
 3057:     if (document.$formname.cloning.length > 1) {
 3058:         for (var i=0; i<document.$formname.cloning.length; i++) {
 3059:             if (document.$formname.cloning[i].checked) {
 3060:                 willclone = document.$formname.cloning[i].value;
 3061:             }
 3062:         }
 3063:     }
 3064:     if (willclone == 1) {
 3065:         document.getElementById('cloneoptions').style.display="block";
 3066:     } else {
 3067:         document.getElementById('cloneoptions').style.display="none";
 3068:         document.$formname.clonecrs.value = '';
 3069:     }
 3070: }
 3071: 
 3072: // ]]
 3073: </script>
 3074: 
 3075: ENDJS
 3076:     my $title = &mt('Brief Course Description');
 3077:     my $clonetitle = &mt('Clone content and settings from an existing course?');
 3078:     if ($crstype eq 'community') {
 3079:         $title = &mt('Brief Community Description');
 3080:         $clonetitle = &mt('Clone content and settings from an existing community?');
 3081:     }
 3082:     my $output .= $js_validate."\n".&Apache::lonhtmlcommon::start_pick_box().
 3083:                   &Apache::lonhtmlcommon::row_headline().
 3084:                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Description').'&nbsp;'.$title.'</h3>'.
 3085:                   &Apache::lonhtmlcommon::row_closure(1).
 3086:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
 3087:                  '<input type="text" size="60" name="cdescr" value="'.$description.'" />';
 3088:     my ($home_server_pick,$numlib) =
 3089:         &Apache::loncommon::home_server_form_item($dom,'chome',
 3090:                                                   'default','hide');
 3091:     if ($numlib > 1) {
 3092:         $output .= &Apache::lonhtmlcommon::row_closure().
 3093:                    &Apache::lonhtmlcommon::row_title(&mt('Home Server for Course'));
 3094:     }
 3095:     $output .= $home_server_pick.
 3096:                &Apache::lonhtmlcommon::row_closure().
 3097:                &Apache::lonhtmlcommon::row_headline().
 3098:                '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Clone').'&nbsp;'.$clonetitle.
 3099:                &Apache::lonhtmlcommon::row_closure(1).
 3100:                &Apache::lonhtmlcommon::row_title(&mt('Clone?')).
 3101:                '<label><input type="radio" name="cloning" value="1" '.
 3102:                'onclick="javascript:toggleCloning()" />'.
 3103:                &mt('Yes').('&nbsp;'x2).'</label><label>'.
 3104:                '<input type="radio" name="cloning" value="0" checked="checked" '.
 3105:                'onclick="javascript:toggleCloning()" />'.&mt('No').'</label>'.
 3106:                '</h3>'.
 3107:                &Apache::lonhtmlcommon::row_closure(1).
 3108:                &Apache::lonhtmlcommon::row_headline().
 3109:                '<div id="cloneoptions" style="display: none" >'.
 3110:                &Apache::lonhtmlcommon::start_pick_box().
 3111:                &clone_form($dom,$formname,$crstype).
 3112:                &Apache::lonhtmlcommon::end_pick_box().'</div>'.
 3113:                &Apache::lonhtmlcommon::end_pick_box()."\n";
 3114:     return $output;
 3115: }
 3116: 
 3117: sub clone_form {
 3118:     my ($dom,$formname,$crstype) = @_;
 3119:     my $type = 'Course';
 3120:     if ($crstype eq 'community') {
 3121:         $type = 'Community';
 3122:     }
 3123:     my %lt = &clone_text();
 3124:     my $output .= 
 3125:         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'<label>'.
 3126:         &Apache::loncommon::select_dom_form($dom,'clonedom').'</label>'.
 3127:         &Apache::lonhtmlcommon::row_closure(1).
 3128:         &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
 3129:         '<input type="text" size="25" name="clonecrs" value="" onfocus="this.blur();opencrsbrowser('."'$formname','clonecrs','clonedom','','','','','$type'".')" />'.
 3130:         '</label>&nbsp;'.
 3131:         &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type).
 3132:         &Apache::lonhtmlcommon::row_closure(1).
 3133:         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
 3134:         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
 3135:         '</label><br /><label>'.
 3136:         '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
 3137:         '</label><br /><label>'.
 3138:         '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
 3139:         $lt{'shd'}.'</label>'.
 3140:         '<input type="text" size="5" name="dateshift" value="365" />'.
 3141:         &Apache::lonhtmlcommon::row_closure(1);
 3142:     return $output;
 3143: }
 3144: 
 3145: sub clone_text {
 3146:     return &Apache::lonlocal::texthash(
 3147:                'cid'  => 'Course ID',
 3148:                'dmn'  => 'Domain',
 3149:                'dsh'  => 'Date Shift',
 3150:                'ncd'  => 'Do not clone date parameters',
 3151:                'prd'  => 'Clone date parameters as-is',
 3152:                'shd'  => 'Shift date parameters by number of days',
 3153:         );
 3154: }
 3155: 
 3156: sub coursecode_form {
 3157:     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
 3158:     my $output;
 3159:     my %rowtitle = &Apache::lonlocal::texthash (
 3160:                     instcode  => 'Course Category',
 3161:                     crosslist => 'Cross Listed Course',
 3162:                    );
 3163:     my %helpitem = ( 
 3164:                      instcode => 'Course_Request_Category',
 3165:                    );
 3166:     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 3167:         (ref($cat_order))) {
 3168:         my ($sel,$instsec,$lcsec);
 3169:         $sel = $context;
 3170:         if ($context eq 'crosslist') {
 3171:             $sel .= '_'.$num;
 3172:             $instsec = &mt('Institutional section').'<br />'.
 3173:                        '<input type="text" size="10" name="'.$sel.'_instsec" />';
 3174:             $lcsec = &mt('LON-CAPA section').'<br />'.
 3175:                      '<input type="text" size="10" name="'.$sel.'_lcsec" />';
 3176:         }
 3177:         if (@{$codetitles} > 0) {
 3178:             my $lastitem = pop(@{$codetitles});
 3179:             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
 3180:             if (@{$codetitles} > 0) {
 3181:                 my $helplink;
 3182:                 if (defined($helpitem{$context})) {
 3183:                     $helplink = &Apache::loncommon::help_open_topic($helpitem{$context}).'&nbsp;';
 3184:                 }
 3185:                 $output = &Apache::lonhtmlcommon::row_title($helplink.$rowtitle{$context}).
 3186:                           '<table><tr>';
 3187:                 if ($context eq 'crosslist') {
 3188:                     $output .= '<td>'.&mt('Include?').'<br />'.
 3189:                                '<input type="checkbox" name="'.$sel.'" value="1" /></td>';
 3190:                 }
 3191:                 foreach my $title (@{$codetitles}) {
 3192:                     if (ref($cat_order->{$title}) eq 'ARRAY') {
 3193:                         if (@{$cat_order->{$title}} > 0) {
 3194:                             $output .= '<td align="center">'.$title.'<br />'."\n".
 3195:                                        '<select name="'.$sel.'_'.$title.'">'."\n".
 3196:                                        ' <option value="" selected="selected">'.
 3197:                                        &mt('Select').'</option>'."\n";
 3198:                             foreach my $item (@{$cat_order->{$title}}) {
 3199:                                 my $longitem = $item;
 3200:                                 if (ref($cat_titles->{$title}) eq 'HASH') {
 3201:                                     if ($cat_titles->{$title}{$item} ne '') {
 3202:                                         $longitem = $cat_titles->{$title}{$item};
 3203:                                     }
 3204:                                 }
 3205:                                 $output .= '<option value="'.$item.'">'.$longitem.
 3206:                                            '</option>'."\n";
 3207:                             }
 3208:                         }
 3209:                         $output .= '</select></td>'."\n";
 3210:                     }
 3211:                 }
 3212:                 if ($context eq 'crosslist') {
 3213:                     $output .= '<td align="center">'.$lastitem.'<br />'."\n".
 3214:                                $lastinput.'</td><td align="center">'.$instsec.'</td>'.
 3215:                                '<td align="center">'.$lcsec.'</td></tr></table>';
 3216:                 } else {
 3217:                     $output .= '</tr></table>'.
 3218:                                &Apache::lonhtmlcommon::row_closure().
 3219:                                &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
 3220:                                $lastinput;
 3221:                 }
 3222:             } else {
 3223:                 if ($context eq 'crosslist') {
 3224:                     $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
 3225:                                '<table><tr>'.
 3226:                                '<td align="center">'.$lastitem.'<br />'.$lastinput.'</td>'.
 3227:                                '<td align="center">'.$instsec.'</td><td>'.$lcsec.'</td>'.
 3228:                                '</tr></table>';
 3229:                 } else { 
 3230:                     $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
 3231:                                $lastinput;
 3232:                 }
 3233:             }
 3234:             $output .=  &Apache::lonhtmlcommon::row_closure(1);
 3235:             push(@$codetitles,$lastitem);    
 3236:         } elsif ($context eq 'crosslist') {
 3237:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
 3238:                        '<table><tr><td align="center">'.
 3239:                        '<span class="LC_nobreak">'.&mt('Include?').
 3240:                        '<input type="checkbox" name="'.$sel.'" value="1" /></span>'.
 3241:                        '</td><td align="center">'.&mt('Institutional ID').'<br />'.
 3242:                        '<input type="text" size="10" name="'.$sel.'_instsec" />'.
 3243:                        '</td><td align="center">'.$lcsec.'</td></tr></table>'.
 3244:                        &Apache::lonhtmlcommon::row_closure(1);
 3245:         }
 3246:     }
 3247:     return $output;
 3248: }
 3249: 
 3250: sub sections_form {
 3251:     my ($dom,$instcode,$num) = @_;
 3252:     my $rowtitle;
 3253:     if ($instcode eq '') {
 3254:         $rowtitle = &mt('Sections');
 3255:     } else {
 3256:         $rowtitle = &mt('Sections of [_1]',$instcode);
 3257:     }
 3258:     return &Apache::lonhtmlcommon::row_title($rowtitle).
 3259:            '<table><tr><td align="center">'.
 3260:            '<span class="LC_nobreak">'.&mt('Include?').
 3261:            '<input type="checkbox" name="sec_'.$num.'" value="1" /></span>'.
 3262:            '</td><td align="center">'.&mt('Institutional section').'<br />'.
 3263:            '<input type="text" size="10" name="secnum_'.$num.'" />'.
 3264:            '</td><td align="center">'.&mt('LON-CAPA section').'<br />'.
 3265:            '<input type="text" size="10" name="loncapasec_'.$num.'" />'.
 3266:            '</td></tr></table>'.
 3267:            &Apache::lonhtmlcommon::row_closure(1);
 3268: }
 3269: 
 3270: sub get_course_dom {
 3271:     my $codedom = &Apache::lonnet::default_login_domain();
 3272:     if ($env{'form.showdom'} ne '') {
 3273:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
 3274:             return $env{'form.showdom'};
 3275:         }
 3276:     }
 3277:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
 3278:         my ($types,$typename) = &Apache::loncommon::course_types();
 3279:         if (ref($types) eq 'ARRAY') {
 3280:             foreach my $type (@{$types}) {
 3281:                 if (&Apache::lonnet::usertools_access($env{'user.name'},
 3282:                                                       $env{'user.domain'},$type,
 3283:                                                       undef,'requestcourses')) {
 3284:                     return $env{'user.domain'};
 3285:                 }
 3286:             }
 3287:             my @possible_doms;
 3288:             foreach my $type (@{$types}) {
 3289:                 my $dom_str = $env{'environment.reqcrsotherdom.'.$type};
 3290:                 if ($dom_str ne '') {
 3291:                     my @domains = split(',',$dom_str);
 3292:                     foreach my $entry (@domains) {
 3293:                         my ($extdom,$extopt) = split(':',$entry);
 3294:                         if ($extdom eq $env{'request.role.domain'}) {
 3295:                             return $extdom;
 3296:                         } 
 3297:                         unless(grep(/^\Q$extdom\E$/,@possible_doms)) {
 3298:                             push(@possible_doms,$extdom);
 3299:                         }
 3300:                     }
 3301:                 }
 3302:             }
 3303:             if (@possible_doms) {
 3304:                 @possible_doms = sort(@possible_doms);
 3305:                 return $possible_doms[0];
 3306:             }
 3307:         }
 3308:         $codedom = $env{'user.domain'};
 3309:         if ($env{'request.role.domain'} ne '') {
 3310:             $codedom = $env{'request.role.domain'};
 3311:         }
 3312:     }
 3313:     return $codedom;
 3314: }
 3315: 
 3316: sub display_navbuttons {
 3317:     my ($r,$dom,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
 3318:     $r->print('<div class="LC_navbuttons">');
 3319:     if ($prev) {
 3320:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
 3321:                   'onclick="javascript:backPage('."document.$formname,'$prev'".')"/>'.
 3322:                   ('&nbsp;'x3));
 3323:     } elsif ($prevtext) {
 3324:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
 3325:                   'onclick="javascript:history.back()"/>'.('&nbsp;'x3));
 3326:     }
 3327:     if ($state eq 'details') {
 3328:         $r->print(' <input type="button" name="other" value="'.$othertext.'" '.
 3329:                   'onclick="javascript:nextPage('."document.$formname,'$other'".
 3330:                   ')" />');
 3331:     }
 3332:     my $gotnext;
 3333:     if ($state eq 'courseinfo') {
 3334:         $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 3335:                   'onclick="javascript:validateForm();" />');
 3336:         $gotnext = 1;
 3337:     } elsif ($state eq 'enrollment') {
 3338:         if (($env{'form.crstype'} eq 'official') && 
 3339:             (&Apache::lonnet::auto_run('',$dom))) {
 3340:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 3341:                       'onclick="javascript:validateEnrollSections('."document.$formname,'$next'".');" />');
 3342:                 $gotnext = 1;
 3343:         }
 3344:     } elsif ($state eq 'personnel') {
 3345:         if ($env{'form.persontotal'} > 0) { 
 3346:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 3347:                       'onclick="javascript:validatePersonnelSections('."document.$formname,'$next'".');" />');
 3348:             $gotnext = 1;
 3349:         }
 3350:     }
 3351:     unless ($gotnext) {
 3352:         if ($next) {
 3353:             $r->print('
 3354:                       <input type="button" name="next" value="'.$nexttext.'" '.
 3355:       'onclick="javascript:nextPage('."document.$formname,'$next'".')" />');
 3356:         }
 3357:     }
 3358:     $r->print('</div>');
 3359: }
 3360: 
 3361: sub print_request_outcome {
 3362:     my ($lonhost,$dom,$codetitles,$code_order,$instcredits) = @_;
 3363:     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
 3364:         %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,
 3365:         $uniquecode);
 3366:     my $sectotal = $env{'form.sectotal'};
 3367:     my $crosslisttotal = 0;
 3368:     $cnum = $env{'form.cnum'};
 3369:     unless ($cnum =~ /^$match_courseid$/) {
 3370:         $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
 3371:         return $output;
 3372:     }
 3373:     $crstype = $env{'form.crstype'};
 3374:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
 3375:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 3376:         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
 3377:             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
 3378:         }
 3379:         if (ref($domconfig{'requestcourses'}{'uniquecode'}) eq 'HASH') {
 3380:             $uniquecode = $domconfig{'requestcourses'}{'uniquecode'}{$crstype};
 3381:         }
 3382:     }
 3383:     $now = time;
 3384:     my $ccrole = 'cc';
 3385:     if ($crstype eq 'community') {
 3386:         $ccrole = 'co';
 3387:     }
 3388:     my @instsections;
 3389:     if ($crstype eq 'official') {
 3390:         if (&Apache::lonnet::auto_run('',$dom)) {
 3391:             ($enrollstart,$enrollend)=&dates_from_form('enrollstart','enrollend');
 3392:         }
 3393:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 3394:             if ($env{'form.sec_'.$i}) {
 3395:                 if ($env{'form.secnum_'.$i} ne '') {
 3396:                     my $sec = $env{'form.secnum_'.$i};
 3397:                     $sections{$i}{'inst'} = $sec;
 3398:                     if (($sec ne '') && (!grep(/^\Q$sec\E$/,@instsections))) {
 3399:                         push(@instsections,$sec);
 3400:                     }
 3401:                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
 3402:                     $sections{$i}{'loncapa'} =~ s/\W//g;
 3403:                     if ($sections{$i}{'loncapa'} eq 'none') {
 3404:                         $sections{$i}{'loncapa'} = '';
 3405:                     }
 3406:                 }
 3407:             }
 3408:         }
 3409:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
 3410:             if ($env{'form.crosslist_'.$i}) {
 3411:                 my $xlistinfo = '';
 3412:                 if (ref($code_order) eq 'ARRAY') {
 3413:                     if (@{$code_order} > 0) {
 3414:                         foreach my $item (@{$code_order}) {
 3415:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
 3416:                         }
 3417:                     }
 3418:                 }
 3419:                 $crosslistings{$i}{'instcode'} = $xlistinfo;
 3420:                 if ($xlistinfo ne '') {
 3421:                     $crosslisttotal ++;
 3422:                 }
 3423:                 $crosslistings{$i}{'instsec'} = $env{'form.crosslist_'.$i.'_instsec'}; 
 3424:                 $crosslistings{$i}{'loncapa'} = $env{'form.crosslist_'.$i.'_lcsec'};
 3425:             }
 3426:         }
 3427:     } else {
 3428:         $enrollstart = '';
 3429:         $enrollend = '';
 3430:     }
 3431:     my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg,%skipped);
 3432:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 3433:         my $uname = $env{'form.person_'.$i.'_uname'};
 3434:         my $udom = $env{'form.person_'.$i.'_dom'};
 3435:         if (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
 3436:             if (&Apache::lonnet::domain($udom) ne '') {
 3437:                 unless (ref($personnel{$uname.':'.$udom}) eq 'HASH') {
 3438:                     $personnel{$uname.':'.$udom} = {
 3439:                              firstname    => $env{'form.person_'.$i.'_firstname'},
 3440:                              lastname     => $env{'form.person_'.$i.'_lastname'},
 3441:                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
 3442:                                                    };
 3443:                     if (&Apache::lonnet::homeserver($uname,$udom) eq 'no_host') {
 3444:                         my $usertype = &get_usertype($udom,$uname,\%curr_rules,\%got_rules);
 3445:                         if (&Apache::lonuserutils::can_create_user($udom,'requestcrs',$usertype)) {
 3446:                             my ($allowed,$msg,$authtype,$authparam) =
 3447:                                 &check_newuser_rules($udom,$uname,\%alerts,\%rulematch,
 3448:                                                      \%inst_results,\%curr_rules,\%got_rules);
 3449:                             if ($allowed) {
 3450:                                 my %domdefaults = &Apache::lonnet::get_domain_defaults($udom);
 3451:                                 if ($usertype eq 'official') {
 3452:                                     if ($authtype eq '') {
 3453:                                         $authtype = $domdefaults{'auth_def'};
 3454:                                         $authparam = $domdefaults{'auth_arg_def'};
 3455:                                     } else {
 3456:                                         if ($authtype eq 'loc') {
 3457:                                             $authtype = 'localauth';
 3458:                                         } elsif ($authtype eq 'int') {
 3459:                                             $authtype = 'internal';
 3460:                                         }
 3461:                                         if ($authtype !~ /^(krb4|krb5|internal|localauth)$/) {
 3462:                                             $authtype = $domdefaults{'auth_def'};
 3463:                                             $authparam = $domdefaults{'auth_arg_def'};
 3464:                                         }
 3465:                                     }
 3466:                                 } elsif ($usertype eq 'unofficial') {
 3467:                                     if ($authtype eq '') {
 3468:                                         $authtype = 'internal';
 3469:                                         $authparam = '';
 3470:                                     }
 3471:                                 } else {
 3472:                                     $authtype = $domdefaults{'auth_def'};
 3473:                                     $authparam = $domdefaults{'auth_arg_def'};
 3474:                                 }
 3475:                                 if (($authtype eq '') ||
 3476:                                     (($authtype =~/^krb(4|5)$/) && ($authparam eq '')) ||
 3477:                                     ($authtype !~ /^(krb4|krb5|internal|localauth)$/)) {
 3478:                                     $skipped{$uname.':'.$udom} = 1;
 3479:                                     next;
 3480:                                 } else {
 3481:                                     $personnel{$uname.':'.$udom}{'authtype'} = $authtype;
 3482:                                     $personnel{$uname.':'.$udom}{'autharg'} = $authparam;
 3483:                                 }
 3484:                             } else {
 3485:                                 $skipped{$uname.':'.$udom} = 1;
 3486:                                 next;
 3487:                             }
 3488:                         } else {
 3489:                             $skipped{$uname.':'.$udom} = 1;
 3490:                             next;
 3491:                         }
 3492:                     }
 3493:                 }
 3494:                 my $role = $env{'form.person_'.$i.'_role'};
 3495:                 unless ($role eq '') {
 3496:                     if (ref($personnel{$uname.':'.$udom}{'roles'}) eq 'ARRAY') {
 3497:                         my @curr_roles = @{$personnel{$uname.':'.$udom}{'roles'}};
 3498:                         unless (grep(/^\Q$role\E$/,@curr_roles)) {
 3499:                             push(@{$personnel{$uname.':'.$udom}{'roles'}},$role);
 3500:                         }
 3501:                     } else {
 3502:                         @{$personnel{$uname.':'.$udom}{'roles'}} = ($role);
 3503:                     }
 3504:                     if ($role eq $ccrole) {
 3505:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
 3506:                     } else {
 3507:                         my @currsec = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
 3508:                         my @allsecs;
 3509:                         foreach my $sec (@currsec) {
 3510:                             next unless ($sec =~ /\w/);
 3511:                             next if ($sec =~ /\W/);
 3512:                             next if ($sec eq 'none');
 3513:                             push(@allsecs,$sec);
 3514:                         }
 3515:                         my $newsec = $env{'form.person_'.$i.'_newsec'};
 3516:                         $newsec =~ s/^\s+//;
 3517:                         $newsec =~s/\s+$//;
 3518:                         my @newsecs = split(/[\s,;]+/,$newsec);
 3519:                         foreach my $sec (@newsecs) {
 3520:                             next if ($sec =~ /\W/);
 3521:                             next if ($sec eq 'none');
 3522:                             if ($sec ne '') {
 3523:                                 unless (grep(/^\Q$sec\E$/,@allsecs)) {
 3524:                                     push(@allsecs,$sec);
 3525:                                 }
 3526:                             }
 3527:                         }
 3528:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @allsecs;
 3529:                     }
 3530:                 }
 3531:             } else {
 3532:                 push(@missingdom,$uname.':'.$udom);
 3533:             }
 3534:         } else {
 3535:             push(@baduname,$uname.':'.$udom);
 3536:         }
 3537:     }
 3538:     if (keys(%skipped)) {
 3539:         foreach my $key (keys(%skipped)) {
 3540:             delete($personnel{$key}); 
 3541:         }
 3542:     }
 3543:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
 3544:     my $autodrops = 0;
 3545:     if ($env{'form.autodrops'}) {
 3546:         $autodrops = $env{'form.autodrops'}; 
 3547:     }
 3548:     my $autoadds = 0;
 3549:     if ($env{'form.autoadds'}) {
 3550:         $autoadds = $env{'form.autoadds'};
 3551:     }
 3552:     my $instcode = '';
 3553:     if (exists($env{'form.instcode'})) {
 3554:         $instcode = $env{'form.instcode'};
 3555:     }
 3556:     my $credits;
 3557:     if ($instcredits) {
 3558:         $credits = $instcredits;
 3559:     } elsif (exists($env{'form.coursecredits'})) {
 3560:         $credits = $env{'form.coursecredits'};
 3561:     }
 3562:     my $clonecrs = '';
 3563:     my $clonedom = '';
 3564:     if (($env{'form.cloning'}) &&
 3565:         ($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
 3566:         ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
 3567:         my $clonehome = &Apache::lonnet::homeserver($env{'form.clonecrs'},
 3568:                                                     $env{'form.clonedom'});
 3569:         if ($clonehome ne 'no_host') {  
 3570:             my $canclone =  
 3571:                 &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
 3572:                         $env{'user.domain'},$env{'form.clonecrs'},$env{'form.clonedom'},
 3573:                         $crstype);
 3574:             if ($canclone) {
 3575:                 $clonecrs = $env{'form.clonecrs'};
 3576:                 $clonedom = $env{'form.clonedom'};
 3577:             }
 3578:         }
 3579:     }
 3580:     my $details = {
 3581:                     owner          => $env{'user.name'},
 3582:                     domain         => $env{'user.domain'}, 
 3583:                     cdom           => $dom,
 3584:                     cnum           => $cnum,
 3585:                     coursehome     => $env{'form.chome'},
 3586:                     cdescr         => $env{'form.cdescr'},
 3587:                     crstype        => $env{'form.crstype'},
 3588:                     instcode       => $instcode,
 3589:                     defaultcredits => $credits, 
 3590:                     uniquecode     => $uniquecode,
 3591:                     clonedom       => $clonedom,
 3592:                     clonecrs       => $clonecrs,
 3593:                     datemode       => $env{'form.datemode'},
 3594:                     dateshift      => $env{'form.dateshift'},
 3595:                     sectotal       => $sectotal,
 3596:                     sections       => \%sections,
 3597:                     crosslisttotal => $crosslisttotal,
 3598:                     crosslists     => \%crosslistings,
 3599:                     autoadds       => $autoadds,
 3600:                     autodrops      => $autodrops,
 3601:                     enrollstart    => $enrollstart,
 3602:                     enrollend      => $enrollend,
 3603:                     accessstart    => $accessstart,
 3604:                     accessend      => $accessend,
 3605:                     personnel      => \%personnel,
 3606:                   };
 3607:     my ($result,$output) = &process_request($lonhost,$dom,$cnum,$crstype,$now,$details,$instcode,
 3608:                                             $req_notifylist,\@instsections,\%domconfig);
 3609:     return ($result,$output);
 3610: }
 3611:     
 3612: sub process_request {
 3613:     my ($lonhost,$dom,$cnum,$crstype,$now,$details,$instcode,$req_notifylist,$instsections,
 3614:         $domconfig) = @_; 
 3615:     my (@inststatuses,$storeresult,$creationresult,$output);
 3616:     my $val = 
 3617:         &Apache::loncoursequeueadmin::get_processtype('course',$env{'user.name'},
 3618:                                                       $env{'user.domain'},$env{'user.adv'},
 3619:                                                       $dom,$crstype,\@inststatuses,$domconfig);
 3620:     if ($val eq '') {
 3621:         if ($crstype eq 'official') {
 3622:             $output = &mt('You are not permitted to request creation of official courses.');
 3623:         } elsif ($crstype eq 'unofficial') {
 3624:             $output = &mt('You are not permitted to request creation of unofficial courses.');
 3625:         } elsif ($crstype eq 'community') {
 3626:             $output = &mt('You are not permitted to request creation of communities');
 3627:         } elsif ($crstype eq 'textbook') {
 3628:             $output = &mt('You are not permitted to request creation of textbook courses');
 3629:         } else {
 3630:             $output = &mt('Unrecognized course type: [_1]',$crstype);
 3631:         }
 3632:         $storeresult = 'notpermitted'; 
 3633:     } else {
 3634:         my ($disposition,$message,$reqstatus,$coursedesc);
 3635:         my %reqhash = (
 3636:                         reqtime   => $now,
 3637:                         crstype   => $crstype,
 3638:                         details   => $details,
 3639:                       );
 3640:         my $requestkey = $dom.'_'.$cnum;
 3641:         my $validationerror;
 3642:         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 3643:                                                      $env{'user.domain'});
 3644:         if (ref($details) eq 'HASH') {
 3645:             $coursedesc = $details->{'cdescr'};
 3646:         }
 3647:         if ($val eq 'autolimit=') {
 3648:             $disposition = 'process';
 3649:         } elsif ($val =~ /^autolimit=(\d+)$/) {
 3650:             my $limit = $1;
 3651:             $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
 3652:                                             $dom,$crstype,$limit,\$message);
 3653:         } elsif ($val eq 'validate') {
 3654:             my ($inststatuslist,$validationchk,$validation,%custominfo);
 3655:             if (@inststatuses > 0) {
 3656:                 $inststatuslist = join(',',@inststatuses);
 3657:             }
 3658:             my $instseclist;
 3659:             if (ref($instsections) eq 'ARRAY') {
 3660:                 if (@{$instsections} > 0) {
 3661:                     $instseclist = join(',',@{$instsections});
 3662:                 }
 3663:             }
 3664:             my $preprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'prevalidate',$env{'user.name'},
 3665:                                                                  $env{'user.domain'},$fullname,$coursedesc);
 3666:             if (ref($preprocess) eq 'HASH') {
 3667:                 if (ref($preprocess->{'formitems'}) eq 'HASH') {
 3668:                     foreach my $key (keys(%{$preprocess->{'formitems'}})) {
 3669:                         if ($preprocess->{'formitems'}->{$key} eq 'multiple') {
 3670:                             if (exists($env{'form.'.$key})) {
 3671:                                 @{$custominfo{$key}} = &Apache::loncommon::get_env_multiple($env{'form.'.$key});
 3672:                             }
 3673:                         } else {
 3674:                             if (exists($env{'form.'.$key})) {
 3675:                                 $custominfo{$key} = $env{'form.'.$key};
 3676:                             }
 3677:                         }
 3678:                     }
 3679:                 }
 3680:             }
 3681:             $validationchk = 
 3682:                 &Apache::lonnet::auto_courserequest_validation($dom,
 3683:                     $env{'user.name'}.':'.$env{'user.domain'},$crstype,
 3684:                     $inststatuslist,$instcode,$instseclist,\%custominfo);
 3685:             if ($validationchk =~ /:/) {
 3686:                 ($validation,$message) = split(':',$validationchk);
 3687:             } else {
 3688:                 $validation = $validationchk;
 3689:             }
 3690:             if ($validation =~ /^error(.*)$/) {
 3691:                 $disposition = 'approval';
 3692:                 $validationerror = $1;
 3693:             } else {
 3694:                 $disposition = $validation;
 3695:             }
 3696:         } else {
 3697:             $disposition = 'approval';
 3698:         }
 3699:         $reqhash{'disposition'} = $disposition;
 3700:         $reqstatus = $disposition;
 3701:         my ($modified,$queued,$coursedesc,$token,%customitems);
 3702:         unless ($disposition eq 'rejected') {
 3703:             my $inprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'process',$env{'user.name'},
 3704:                                                                 $env{'user.domain'},$fullname,$coursedesc);
 3705:             if (ref($inprocess) eq 'HASH') {
 3706:                 if (ref($inprocess->{'formitems'}) eq 'HASH') {
 3707:                     foreach my $key (keys(%{$inprocess->{'formitems'}})) {
 3708:                         if ($inprocess->{'formitems'}->{$key} eq 'multiple') {
 3709:                             if (exists($env{'form.'.$key})) {
 3710:                                 @{$customitems{$key}} = &Apache::loncommon::get_env_multiple($env{'form.'.$key});
 3711:                             }
 3712:                         } else {
 3713:                             if (exists($env{'form.'.$key})) {
 3714:                                 $customitems{$key} = $env{'form.'.$key};
 3715:                                 $reqhash{'custom'}{$key} = $customitems{$key};
 3716:                             }
 3717:                         }
 3718:                     }
 3719:                 }
 3720:             }
 3721:         }
 3722:         if ($disposition eq 'rejected') {
 3723:             if ($crstype eq 'community') {
 3724:                 $output = &mt('Your community request was rejected.');
 3725:             } else {
 3726:                 $output = &mt('Your course request was rejected.');
 3727:             }
 3728:             if ($message) {
 3729:                 $output .= '<div class="LC_warning">'.$message.'</div>';
 3730:             }
 3731:             $storeresult = 'rejected';
 3732:         } elsif ($disposition eq 'process') {
 3733:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 3734:             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles,$code);
 3735:             my $type = 'Course';
 3736:             if ($crstype eq 'community') {
 3737:                 $type = 'Community';
 3738:             }
 3739:             my @roles = &Apache::lonuserutils::roles_by_context('course','',$type);
 3740:             foreach my $role (@roles) {
 3741:                 $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
 3742:             }
 3743:             my ($result,$postprocess) = &Apache::loncoursequeueadmin::course_creation($dom,$cnum,
 3744:                                           'autocreate',$details,\$logmsg,\$newusermsg,\$addresult,
 3745:                                           \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles,
 3746:                                           \$code,\%customitems);
 3747:             if ($result eq 'created') {
 3748:                 $disposition = 'created';
 3749:                 $reqstatus = 'created';
 3750:                 my $role_result = &update_requestors_roles($dom,$cnum,$crstype,$details,
 3751:                                                            \%longroles);
 3752:                 if ($crstype eq 'community') {
 3753:                     $output = '<p>'.&mt('Your community request has been processed and the community has been created.');
 3754:                 } else {
 3755:                     $output = '<p>'.&mt('Your course request has been processed and the course has been created.');
 3756:                 }
 3757:                 if (($code) || ((ref($postprocess) eq 'HASH') && 
 3758:                                 (($postprocess->{'createdweb'}) || ($postprocess->{'createdmsg'})))) {
 3759:                     $output .= &notification_information($disposition,$env{'user.name'}.':'.$env{'user.domain'},
 3760:                                                          $dom,$cnum,$now,$code,$postprocess);
 3761:                 }
 3762:                 if ($code) {
 3763:                     $reqhash{'code'} = $code;
 3764:                 }
 3765:                 if (ref($postprocess) eq 'HASH') {
 3766:                     if (ref($postprocess->{'createdactions'}) eq 'HASH') {
 3767:                         if (ref($postprocess->{'createdactions'}{'environment'}) eq 'HASH') {
 3768:                             &Apache::loncoursequeueadmin::postprocess_crsenv($dom,$cnum,
 3769:                                                                              $postprocess->{'createdactions'}{'environment'});
 3770:                         }
 3771:                     }
 3772:                 }
 3773:                 $output .= '<br />'.$role_result.'</p>';
 3774:                 $creationresult = 'created';
 3775:             } else {
 3776:                 $output = '<span class="LC_error">';
 3777:                 if ($crstype eq 'community') {
 3778:                     $output .= &mt('An error occurred when processing your community request.');
 3779:                 } else {
 3780:                     $output .= &mt('An error occurred when processing your course request.');
 3781:                 }
 3782:                 $output .= '<br />'.
 3783:                            &mt('You may want to review the request details and submit the request again.').
 3784:                           '</span>';
 3785:                 $creationresult = 'error';
 3786:             }
 3787:         } else {
 3788:             my $requestid = $cnum.'_'.$disposition;
 3789:             my $request = {
 3790:                             $requestid => {
 3791:                                             timestamp   => $now,
 3792:                                             crstype     => $crstype,
 3793:                                             ownername   => $env{'user.name'},
 3794:                                             ownerdom    => $env{'user.domain'},
 3795:                                             description => $env{'form.cdescr'},
 3796:                                             lonhost     => $lonhost,
 3797:                                           },
 3798:                           };
 3799:             if ($crstype eq 'official') {
 3800:                 $request->{$requestid}->{'instcode'} = $instcode;
 3801:             }
 3802:             my $statuskey = 'status:'.$dom.':'.$cnum;
 3803:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
 3804:                                                    $env{'user.domain'},$env{'user.name'});
 3805:             if ($userreqhash{$statuskey} ne '') {
 3806:                 $modified = 1;
 3807:                 my $uname = &Apache::lonnet::get_domainconfiguser($dom);
 3808:                 my %queuehash = &Apache::lonnet::get('courserequestqueue',
 3809:                                                      [$cnum.'_approval',
 3810:                                                       $cnum.'_pending'],$dom,$uname);
 3811:                 if (($queuehash{$cnum.'_approval'} ne '') || 
 3812:                     ($queuehash{$cnum.'_pending'} ne '')) {
 3813:                     $queued = 1;
 3814:                     if (ref($queuehash{$cnum.'_pending'}) eq 'HASH') {
 3815:                         $token = $queuehash{$cnum.'_pending'}{'token'};
 3816:                     } 
 3817:                 }
 3818:             }
 3819:             unless ($queued) {
 3820:                 if (($disposition eq 'pending') && ($crstype ne 'official')) {
 3821:                     my %reqinfo = (
 3822:                                     $cnum.':'.$dom => $now.':'.$env{'user.name'}.':'.$env{'user.domain'},
 3823:                                   );
 3824:                      $token = &Apache::lonnet::tmpput(\%reqinfo,$lonhost);
 3825:                      $request->{$requestid}->{'token'} = $token;
 3826:                 }
 3827:                 my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
 3828:                                                             $dom);
 3829:                 if ($putresult eq 'ok') {
 3830:                     if ($crstype eq 'community') {
 3831:                         $output .= &mt('Your community request has been recorded.');
 3832:                     } else {
 3833:                         $output .= &mt('Your course request has been recorded.') 
 3834:                     }
 3835:                     unless ($disposition eq 'pending') { 
 3836:                         $output .= '<br />'.
 3837:                                    &notification_information($disposition,$req_notifylist,
 3838:                                                              $dom,$cnum,$now);
 3839:                     }
 3840:                 } else {
 3841:                     $reqstatus = 'domainerror';
 3842:                     $reqhash{'disposition'} = $disposition;
 3843:                     my $warning = &mt('An error occurred saving your request in the pending requests queue.');
 3844:                     $output = '<span class"LC_warning">'.$warning.'</span><br />';
 3845:                 }
 3846:             }
 3847:         }
 3848:         ($storeresult,my $updateresult) = 
 3849:             &Apache::loncoursequeueadmin::update_coursereq_status(\%reqhash,$dom,
 3850:                 $cnum,$reqstatus,'request',$env{'user.domain'},$env{'user.name'});
 3851:         if ($storeresult eq 'ok') {
 3852:             my $postprocess;
 3853:             if (($disposition eq 'approval') || ($disposition eq 'pending')) {
 3854:                 my $updateaction = $disposition;
 3855:                 if ($disposition eq 'approval') {
 3856:                     $updateaction = 'queued';
 3857:                 }
 3858:                 my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 3859:                                                              $env{'user.domain'});
 3860:                 $postprocess =
 3861:                     &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,$updateaction,$env{'user.name'},
 3862:                                                         $env{'user.domain'},$fullname,$env{'form.cdescr'});
 3863:             }
 3864:             if ($modified && $queued) {
 3865:                 if ($crstype eq 'community') {
 3866:                     $output .= '<p>'.&mt('Your community request has been updated').'</p>';
 3867:                 } else {
 3868:                     $output .= '<p>'.&mt('Your course request has been updated').'</p>';
 3869:                 }
 3870:                 if ($disposition eq 'approval') {
 3871:                     $output .= &notification_information($disposition,$req_notifylist,$dom,$cnum,$now);
 3872:                 }
 3873:             }
 3874:             if ($disposition eq 'approval') {
 3875:                 if ((ref($postprocess) eq 'HASH') && 
 3876:                     ((ref($postprocess->{'queuedmsg'}) eq 'HASH') || ($postprocess->{'queuedweb'}))) { 
 3877:                     &notification_information($disposition,undef,$dom,$cnum,$now,undef,$postprocess);
 3878:                 }
 3879:             } elsif ($disposition eq 'pending') {
 3880:                 my $pendingform;
 3881:                 if ($crstype ne 'official') {
 3882:                     $pendingform = &pending_validation_form($dom,$cnum,$crstype,$now,$token,
 3883:                                                             $lonhost,$env{'form.cdescr'});
 3884:                 }
 3885:                 if ($pendingform) {
 3886:                     $output .= $pendingform;
 3887:                 } else { 
 3888:                     $output .= &notification_information($disposition,undef,$dom,$cnum,$now,undef,$postprocess);
 3889:                 }
 3890:             }
 3891:         }
 3892:         if ($validationerror ne '') {
 3893:             $output .= '<p class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: [_1].',$validationerror).'</p>';
 3894:         }
 3895:         if ($updateresult) {
 3896:             $output .= $updateresult;
 3897:         }
 3898:     }
 3899:     if ($creationresult ne '') {
 3900:         return ($creationresult,$output);
 3901:     } else {
 3902:         return ($storeresult,$output);
 3903:     }
 3904: }
 3905: 
 3906: sub update_requestors_roles {
 3907:     my ($dom,$cnum,$crstype,$details,$longroles) = @_;
 3908:     my $now = time;
 3909:     my ($active,$future,$numactive,$numfuture,$output);
 3910:     my $owner = $env{'user.name'}.':'.$env{'user.domain'};
 3911:     if (ref($details) eq 'HASH') {
 3912:         if (ref($details->{'personnel'}) eq 'HASH') {
 3913:             my $ccrole = 'cc';
 3914:             if ($crstype eq 'community') {
 3915:                 $ccrole = 'co';
 3916:             }
 3917:             unless (ref($details->{'personnel'}{$owner}) eq 'HASH') {
 3918:                 $details->{'personnel'}{$owner} = {
 3919:                                                     'roles' => [$ccrole],
 3920:                                                     $ccrole => { 'usec' => [] },
 3921:                                                   };
 3922:             }
 3923:             my @roles;
 3924:             if (ref($details->{'personnel'}{$owner}{'roles'}) eq 'ARRAY') {
 3925:                 @roles = sort(@{$details->{'personnel'}{$owner}{'roles'}});
 3926:                 unless (grep(/^\Q$ccrole\E$/,@roles)) {
 3927:                     push(@roles,$ccrole);
 3928:                 }
 3929:             } else {
 3930:                 @roles = ($ccrole);
 3931:             }
 3932:             foreach my $role (@roles) {
 3933:                 my $refresh=$env{'user.refresh.time'};
 3934:                 if ($refresh eq '') {
 3935:                     $refresh = $env{'user.login.time'};
 3936:                 }
 3937:                 if ($refresh eq '') {
 3938:                     $refresh = $now;
 3939:                 }
 3940:                 my $start = $refresh-1;
 3941:                 my $end = '0';
 3942:                 if ($role eq 'st') {
 3943:                     if ($details->{'accessstart'} ne '') {
 3944:                         $start = $details->{'accessstart'};
 3945:                     }
 3946:                     if ($details->{'accessend'} ne '') {
 3947:                         $end = $details->{'accessend'};
 3948:                     }
 3949:                 }
 3950:                 my @usecs;
 3951:                 if ($role ne $ccrole) {
 3952:                     if (ref($details->{'personnel'}{$owner}{$role}{'usec'}) eq 'ARRAY') {
 3953:                         @usecs = @{$details->{'personnel'}{$owner}{$role}{'usec'}};
 3954:                     }
 3955:                 } 
 3956:                 if ($role eq 'st') {
 3957:                     if (@usecs > 1) {
 3958:                         my $firstsec = $usecs[0];
 3959:                         @usecs = ($firstsec);
 3960:                     }
 3961:                 }
 3962:                 if (@usecs == 0) {
 3963:                     push(@usecs,'');
 3964:                 }
 3965:                 foreach my $usec (@usecs) {
 3966:                     my (%userroles,%newrole,%newgroups,$spec,$area);
 3967:                     my $area = '/'.$dom.'/'.$cnum;
 3968:                     my $spec = $role.'.'.$area;
 3969:                     if ($usec ne '') {
 3970:                        $spec .= '/'.$usec;
 3971:                        $area .= '/'.$usec;
 3972:                     }
 3973:                     if ($role =~ /^cr\//) {
 3974:                         &Apache::lonnet::custom_roleprivs(\%newrole,$role,$dom,
 3975:                                                           $cnum,$spec,$area);
 3976:                     } else {
 3977:                         &Apache::lonnet::standard_roleprivs(\%newrole,$role,$dom,
 3978:                                                             $spec,$cnum,$area);
 3979:                     }
 3980:                     &Apache::lonnet::set_userprivs(\%userroles,\%newrole,
 3981:                                                    \%newgroups);
 3982:                     $userroles{'user.role.'.$spec} = $start.'.'.$end;
 3983:                     &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
 3984:                     if (($end == 0) || ($end > $now)) {
 3985:                         my $showrole = $role;
 3986:                         if ($role =~ /^cr\//) {
 3987:                             $showrole = &Apache::lonnet::plaintext($role,$crstype);
 3988:                         } elsif (ref($longroles) eq 'HASH') {
 3989:                             if ($longroles->{$role} ne '') {
 3990:                                 $showrole = $longroles->{$role};
 3991:                             }
 3992:                         }
 3993:                         if ($start <= $now) {
 3994:                             $active .= '<li><a href="/adm/roles?selectrole=1&'.
 3995:                                        $spec.'=1">'.$showrole;
 3996:                             if ($usec ne '') {
 3997:                                 $active .= ' - '.&mt('section:').' '.$usec; 
 3998:                             }
 3999:                             $active .= '</a></li>';
 4000:                             $numactive ++;
 4001:                         } else { 
 4002:                             $future .= '<li>'.$showrole;
 4003:                             if ($usec ne '') {
 4004:                                 $future .= ' - '.&mt('section:').' '.$usec;
 4005:                             }
 4006:                             $future .= '</li>';
 4007:                             $numfuture ++;
 4008:                         }
 4009:                     }
 4010:                 }
 4011:             }
 4012:         }
 4013:     }
 4014:     if ($active) {
 4015:         if ($numactive == 1) {
 4016:             if ($crstype eq 'Community') {
 4017:                 $output = &mt('Use the following link to enter the community:');
 4018:             } else {
 4019:                 $output = &mt('Use the following link to enter the course:'); 
 4020:             }
 4021:         } else {
 4022:             if ($crstype eq 'Community') {
 4023:                 $output = &mt('Use the following links to your new roles to enter the community:');
 4024:             } else {
 4025:                 $output = &mt('Use the following links to your new roles to enter the course:');
 4026:             }
 4027:         }
 4028:         $output .= ' <ul>'.$active.'</ul><br />';
 4029:     }
 4030:     if ($future) {
 4031:         if ($crstype eq 'Community') {
 4032:             $output .= &mt('The following community [quant,_1,role] will become available for selection from your [_2]roles page[_3], once the default student access start date - [_4] - has been reached:',$numfuture,'<a href="/adm/roles">','</a>',&Apache::lonlocal::locallocaltime($details->{'accessstart'}))
 4033:         } else {
 4034:             $output .= &mt('The following course [quant,_1,role] will become available for selection from your [_2]roles page[_3], once the default student access start date - [_4] - has been reached:',$numfuture,'<a href="/adm/roles">','</a>',&Apache::lonlocal::locallocaltime($details->{'accessstart'}));
 4035:         }
 4036:         $output .= ' <ul>'.$future.'</ul>';
 4037:     }
 4038:     return $output;
 4039: }
 4040: 
 4041: sub notification_information {
 4042:     my ($disposition,$req_notifylist,$dom,$cnum,$now,$code,$postprocess) = @_;
 4043:     my %emails = &Apache::loncommon::getemails();
 4044:     my $address;
 4045:     if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
 4046:         $address = $emails{'permanentemail'};
 4047:         if ($address eq '') {
 4048:             $address = $emails{'notification'};
 4049:         }
 4050:     }
 4051:     my $output;
 4052:     if ($disposition eq 'approval') {
 4053:         $output .= &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
 4054:                    &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
 4055:         if ($address ne '') {
 4056:             $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
 4057:         }
 4058:         if ($req_notifylist) {
 4059:             my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 4060:                                                          $env{'user.domain'});
 4061:             my $sender = $env{'user.name'}.':'.$env{'user.domain'};
 4062:             &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,"$fullname ($env{'user.name'}:$env{'user.domain'})",
 4063:                                                                       'undef',$env{'form.cdescr'},$now,'coursereq',$sender);
 4064:         }
 4065:         if (ref($postprocess) eq 'HASH') {
 4066:             if (ref($postprocess->{'queuedmsg'}) eq 'ARRAY') {
 4067:                 if (scalar(@{$postprocess->{'queuedmsg'}}) > 0) {
 4068:                     my $recipient = $env{'user.name'}.':'.$env{'user.domain'};
 4069:                     my $sender = $recipient;
 4070:                     my $addmsg = [];
 4071:                     foreach my $item (@{$postprocess->{'queuedmsg'}}) {
 4072:                         if (ref($item) eq 'HASH') {
 4073:                             if ($item->{'mt'} ne '') {
 4074:                                 push(@{$addmsg},$item);
 4075:                             }
 4076:                         }
 4077:                     }
 4078:                     if (scalar(@{$addmsg}) > 0) {
 4079:                         &Apache::loncoursequeueadmin::send_selfserve_notification($recipient,$addmsg,undef,
 4080:                                                                                   $env{'form.cdescr'},$now,
 4081:                                                                                   'queuedcrsreq',$sender);
 4082:                     }
 4083:                 }
 4084:             }
 4085:             if ($postprocess->{'queuedweb'}) {
 4086:                 $output .= $postprocess->{'queuedweb'};
 4087:             }
 4088:         }
 4089:     } elsif ($disposition eq 'pending') {
 4090:         my $pending_default = '<div class="LC_info">'.
 4091: &mt('Your request has been placed in a queue pending administrative action.').'<br />'.
 4092: &mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
 4093: &mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
 4094:                        '</div>';
 4095:         if (ref($postprocess) eq 'HASH') {
 4096:             if ($postprocess->{'pendingweb'}) {
 4097:                 $output .= $postprocess->{'pendingweb'};
 4098:             } else {
 4099:                 $output .= $pending_default;
 4100:             }
 4101:         } else {
 4102:             $output .= $pending_default;
 4103:         }
 4104:     } elsif ($disposition eq 'created') {
 4105:         if (($code) || ((ref($postprocess) eq 'HASH') && 
 4106:             ((ref($postprocess->{'createdmsg'}) eq 'ARRAY') || ($postprocess->{'createdweb'})))) {
 4107:             my $addmsg = [];
 4108:             my $recipient = $env{'user.name'}.':'.$env{'user.domain'};
 4109:             my $sender = $recipient;
 4110:             if ($code) {
 4111:                 push(@{$addmsg},{
 4112:                                   mt   => 'Students can automatically select your course: "[_1]" by entering this code: [_2]',
 4113:                                   args => [$env{'form.cdescr'},$code],
 4114:                                 });
 4115:                 $output .= '<p>'.
 4116:                            &mt('Students can automatically select your course by entering this code: [_1].','<b>'.$code.'</b>').
 4117:                            '<br />'.
 4118:                            &mt('A message has been sent to your LON-CAPA account with this information.');
 4119:                 if ($address ne '') {
 4120:                     $output.= '<br />'.&mt('An e-mail has also been sent to: [_1] with this code.',$address);
 4121:                 }
 4122:                 $output .= '</p>';
 4123:             }
 4124:             if (ref($postprocess) eq 'HASH') {
 4125:                 if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
 4126:                     foreach my $item (@{$postprocess->{'createdmsg'}}) {
 4127:                         if (ref($item) eq 'HASH') {
 4128:                             if ($item->{'mt'} ne '') {  
 4129:                                 push(@{$addmsg},$item);
 4130:                             }
 4131:                         }
 4132:                     }
 4133:                 }
 4134:                 if ($postprocess->{'createdweb'}) {
 4135:                     $output .= $postprocess->{'createdweb'}
 4136:                 }
 4137:             }
 4138:             if (scalar(@{$addmsg}) > 0) {
 4139:                 my $type = 'createdcrsreq';
 4140:                 if ($code) {
 4141:                     $type = 'uniquecode';
 4142:                 }
 4143:                 &Apache::loncoursequeueadmin::send_selfserve_notification($recipient,$addmsg,$dom.'_'.$cnum,$env{'form.cdescr'},
 4144:                                                                           $now,$type,$sender);
 4145:             }
 4146:         }
 4147:     } else {
 4148:         $output .= '<div class="LC_warning">'.
 4149:                    &mt('Your request status is: [_1].',$disposition).
 4150:                    '</div>';
 4151:     }
 4152:     return $output;
 4153: }
 4154: 
 4155: sub pending_validation_form {
 4156:     my ($cdom,$cnum,$crstype,$now,$token,$lonhost,$cdesc) = @_;
 4157:     my $output;
 4158:     my %postvalues = (
 4159:                       'owner'      => $env{'user.name'}.':'.$env{'user.domain'},
 4160:                       'course'     => $cdom.'_'.$cnum,
 4161:                       'coursetype' => $crstype,
 4162:                      );
 4163:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
 4164: 
 4165:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 4166:         my ($url,$buttontext,$code,@fields);
 4167:         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 4168:             $postvalues{'description'} = $cdesc;
 4169:             $url = $domconfig{'requestcourses'}{'validation'}{'url'};
 4170:             if (ref($domconfig{'requestcourses'}{'validation'}{'fields'}) eq 'ARRAY') {
 4171:                 @fields = @{$domconfig{'requestcourses'}{'validation'}{'fields'}};
 4172:             }
 4173:             $buttontext = $domconfig{'requestcourses'}{'validation'}{'button'};
 4174:             $output .= $domconfig{'requestcourses'}{'validation'}{'markup'};
 4175:             if (($url =~ m{^(https?\://|/)}) && (@fields > 0)) {
 4176:                 $output .= '<form name="crsreqvalidation" action="'.$url.'" method="post">'."\n";
 4177:                 foreach my $field (@fields) {
 4178:                     if ($postvalues{$field}) {
 4179:                         $output .= '<input type="hidden" name="'.$field.'" value="'.$postvalues{$field}.'" />'."\n";
 4180:                     }
 4181:                 }
 4182:                 if ($buttontext eq '') {
 4183:                     if ($crstype eq 'community') {
 4184:                         $buttontext = &mt('Create community');
 4185:                     } else {
 4186:                         $buttontext = &mt('Create course');
 4187:                     }
 4188:                 }
 4189:                 my $protocol = $Apache::lonnet::protocol{$lonhost};
 4190:                 $protocol = 'http' if ($protocol ne 'https');
 4191:                 my $crscreator = $protocol.'://'.&Apache::lonnet::hostname($lonhost).'/cgi-bin/createpending.pl';
 4192:                 $output .= '<input type="hidden" name="crscreator" value="'.$crscreator.'" />'."\n".
 4193:                            '<input type="hidden" name="token" value="'.$token.'" />'."\n".
 4194:                            '<input type="submit" name="validate" value="'.$buttontext.'" />'."\n".
 4195:                            '</form>'."\n";
 4196:             }
 4197:         }
 4198:     }
 4199:     return $output;
 4200: }
 4201: 
 4202: sub check_autolimit {
 4203:     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
 4204:     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
 4205:                        'userroles',['active','future'],['cc','co'],[$dom]);
 4206:     my ($types,$typename) = &Apache::loncommon::course_types();
 4207:     my %requests = &Apache::lonnet::dumpstore('courserequests',$udom,$uname);
 4208:     my $count = 0;
 4209:     foreach my $key (keys(%requests)) {
 4210:         my ($cdom,$cnum) = split('_',$key);
 4211:         if (ref($requests{$key}) eq 'HASH') {
 4212:             next if ($requests{$key}{'crstype'} ne $crstype);
 4213:             if (($crstype eq 'community') && 
 4214:                 (exists($crsroles{$cnum.':'.$cdom.':co'}))) {
 4215:                 $count ++;
 4216:             } elsif ((($crstype eq 'official') || ($crstype eq 'unofficial') || ($crstype eq 'textbook')) &&
 4217:                      (exists($crsroles{$cnum.':'.$cdom.':cc'}))) {
 4218:                 $count ++;
 4219:             }
 4220:         }
 4221:     }
 4222:     if ($count < $limit) {
 4223:         return 'process';
 4224:     } else {
 4225:         if (ref($typename) eq 'HASH') {
 4226:             if ($crstype eq 'community') {
 4227:                 $$message = &mt('Your request has not been processed because you have reached the limit for the number of communities.').
 4228:                             '<br />'.&mt("Your limit is [_1].",$limit);
 4229:             } else {
 4230:                 $$message = &mt('Your request has not been processed because you have reached the limit for the number of courses of this type.').
 4231:                             '<br />'.&mt("Your $typename->{$crstype} limit is [_1].",$limit);
 4232:             }
 4233:         }
 4234:         return 'rejected';
 4235:     }
 4236:     return;
 4237: }
 4238: 
 4239: sub retrieve_settings {
 4240:     my ($dom,$cnum,$udom,$uname) = @_;
 4241:     if ($udom eq '' || $uname eq '') {
 4242:         $udom = $env{'user.domain'};
 4243:         $uname = $env{'user.name'};
 4244:     }
 4245:     my ($result,%reqinfo) = &get_request_settings($dom,$cnum,$udom,$uname);
 4246:     if ($result eq 'ok') {
 4247:         if (($udom eq $reqinfo{'domain'}) &&  ($uname eq $reqinfo{'owner'})) {
 4248:             $env{'form.chome'} = $reqinfo{'coursehome'};
 4249:             $env{'form.cdescr'} = $reqinfo{'cdescr'};
 4250:             $env{'form.crstype'} = $reqinfo{'crstype'}; 
 4251:             &generate_date_items($reqinfo{'accessstart'},'accessstart');
 4252:             &generate_date_items($reqinfo{'accessend'},'accessend');
 4253:             if ($reqinfo{'accessend'} == 0) {
 4254:                 $env{'form.no_end_date'} = 1;
 4255:             }
 4256:             if (($reqinfo{'crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
 4257:                 &generate_date_items($reqinfo{'enrollstart'},'enrollstart');
 4258:                 &generate_date_items($reqinfo{'enrollend'},'enrollend');
 4259:             }
 4260:             $env{'form.clonecrs'} = $reqinfo{'clonecrs'};
 4261:             $env{'form.clonedom'} = $reqinfo{'clonedom'};
 4262:             if (($reqinfo{'clonecrs'} ne '') && ($reqinfo{'clonedom'} ne '')) {
 4263:                 $env{'form.cloning'} = 1;
 4264:             }
 4265:             $env{'form.datemode'} = $reqinfo{'datemode'};
 4266:             $env{'form.dateshift'} = $reqinfo{'dateshift'};
 4267:             if ($reqinfo{'crstype'} eq 'official') {
 4268:                 $env{'form.autoadds'} = $reqinfo{'autoadds'};
 4269:                 $env{'form.autodrops'} = $reqinfo{'autodrops'};
 4270:                 if ($reqinfo{'instcode'} ne '') { 
 4271:                     $env{'form.sectotal'} = $reqinfo{'sectotal'};
 4272:                     $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
 4273:                     $env{'form.instcode'} = $reqinfo{'instcode'};
 4274:                     my $crscode = { 
 4275:                                     $cnum => $reqinfo{'instcode'},
 4276:                                   };
 4277:                     &extract_instcode($dom,'instcode',$crscode,$cnum);
 4278:                     (undef,undef,my $instcredits) =
 4279:                         &Apache::lonnet::auto_validate_instcode(undef,$dom,
 4280:                                                                 $reqinfo{'instcode'});
 4281:                     if ($instcredits ne $reqinfo{'defaultcredits'}) {
 4282:                         $env{'form.coursecredits'} = $reqinfo{'defaultcredits'};
 4283:                     }
 4284:                 }
 4285:             } elsif (($reqinfo{'crstype'} eq 'unofficial') || ($reqinfo{'crstype'} eq 'textbook')) {
 4286:                 $env{'form.coursecredits'} = $reqinfo{'defaultcredits'};
 4287:             }
 4288:             my @currsec;
 4289:             if (ref($reqinfo{'sections'}) eq 'HASH') {
 4290:                 foreach my $i (sort(keys(%{$reqinfo{'sections'}}))) {
 4291:                     if (ref($reqinfo{'sections'}{$i}) eq 'HASH') {
 4292:                         my $sec = $reqinfo{'sections'}{$i}{'inst'};
 4293:                         $env{'form.secnum_'.$i} = $sec;
 4294:                         $env{'form.sec_'.$i} = '1';
 4295:                         if (!grep(/^\Q$sec\E$/,@currsec)) {
 4296:                             push(@currsec,$sec);
 4297:                         }
 4298:                         $env{'form.loncapasec_'.$i} = $reqinfo{'sections'}{$i}{'loncapa'};
 4299:                     }
 4300:                 }
 4301:             }
 4302:             if (ref($reqinfo{'crosslists'}) eq 'HASH') {
 4303:                 foreach my $i (sort(keys(%{$reqinfo{'crosslists'}}))) {
 4304:                     if (ref($reqinfo{'crosslists'}{$i}) eq 'HASH') {
 4305:                         $env{'form.crosslist_'.$i} = '1';
 4306:                         $env{'form.crosslist_'.$i.'_instsec'} = $reqinfo{'crosslists'}{$i}{'instsec'};
 4307:                         $env{'form.crosslist_'.$i.'_lcsec'} = $reqinfo{'crosslists'}{$i}{'loncapa'};
 4308:                         if ($reqinfo{'crosslists'}{$i}{'instcode'} ne '') {
 4309:                             my $key = $cnum.$i; 
 4310:                             my $crscode = {
 4311:                                               $key => $reqinfo{'crosslists'}{$i}{'instcode'},
 4312:                                           };
 4313:                             &extract_instcode($dom,'crosslist',$crscode,$key,$i);
 4314:                         }
 4315:                     }
 4316:                 }
 4317:             }
 4318:             if (ref($reqinfo{'personnel'}) eq 'HASH') {
 4319:                 my $i = 0;
 4320:                 foreach my $user (sort(keys(%{$reqinfo{'personnel'}}))) {
 4321:                     my ($uname,$udom) = split(':',$user);
 4322:                     if (ref($reqinfo{'personnel'}{$user}) eq 'HASH') {
 4323:                         if (ref($reqinfo{'personnel'}{$user}{'roles'}) eq 'ARRAY') {
 4324:                             foreach my $role (sort(@{$reqinfo{'personnel'}{$user}{'roles'}})) {
 4325:                                 $env{'form.person_'.$i.'_role'} = $role;
 4326:                                 $env{'form.person_'.$i.'_firstname'} = $reqinfo{'personnel'}{$user}{'firstname'};
 4327:                                 $env{'form.person_'.$i.'_lastname'} = $reqinfo{'personnel'}{$user}{'lastname'}; ;
 4328:                                 $env{'form.person_'.$i.'_emailaddr'} = $reqinfo{'personnel'}{$user}{'emailaddr'};
 4329:                                 $env{'form.person_'.$i.'_uname'} = $uname;
 4330:                                 $env{'form.person_'.$i.'_dom'} = $udom;
 4331:                                 if (ref($reqinfo{'personnel'}{$user}{$role}) eq 'HASH') {
 4332:                                     if (ref($reqinfo{'personnel'}{$user}{$role}{'usec'}) eq 'ARRAY') {
 4333:                                         my @usecs = @{$reqinfo{'personnel'}{$user}{$role}{'usec'}};
 4334:                                         my @newsecs;
 4335:                                         if (@usecs > 0) {
 4336:                                             foreach my $sec (@usecs) {
 4337:                                                 if (grep(/^\Q$sec\E/,@currsec)) {
 4338:                                                     $env{'form.person_'.$i.'_sec'} = $sec;
 4339:                                                 } else {
 4340:                                                     push(@newsecs,$sec);
 4341:                                                 }
 4342:                                             }
 4343:                                         }
 4344:                                         if (@newsecs > 0) {
 4345:                                             $env{'form.person_'.$i.'_newsec'} = join(',',@newsecs); 
 4346:                                         }
 4347:                                     }
 4348:                                 }
 4349:                                 $i ++;
 4350:                             }
 4351:                         }
 4352:                     }
 4353:                 }
 4354:                 $env{'form.persontotal'} = $i;
 4355:             }
 4356:         }
 4357:     }
 4358:     return $result;
 4359: }
 4360: 
 4361: sub get_request_settings {
 4362:     my ($dom,$cnum,$udom,$uname) = @_;
 4363:     my $requestkey = $dom.'_'.$cnum;
 4364:     my ($result,%reqinfo);
 4365:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 4366:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',$udom,$uname);
 4367:         my $disposition = $history{'disposition'};
 4368:         if (($disposition eq 'approval') || ($disposition eq 'pending')) { 
 4369:             if (ref($history{'details'}) eq 'HASH') {
 4370:                 %reqinfo = %{$history{'details'}};
 4371:                 $result = 'ok';
 4372:             } else {
 4373:                 $result = 'nothash';
 4374:             }
 4375:         } else {
 4376:             $result = 'notqueued';
 4377:         }
 4378:     } else {
 4379:         $result = 'invalid';
 4380:     }
 4381:     return ($result,%reqinfo);
 4382: }
 4383: 
 4384: sub extract_instcode {
 4385:     my ($cdom,$element,$crscode,$crskey,$counter) = @_;
 4386:     my (%codes,@codetitles,%cat_titles,%cat_order);
 4387:     if (&Apache::lonnet::auto_instcode_format('requests',$cdom,$crscode,\%codes,
 4388:                                               \@codetitles,\%cat_titles,
 4389:                                               \%cat_order) eq 'ok') {
 4390:         if (ref($codes{$crskey}) eq 'HASH') {
 4391:             if (@codetitles > 0) {
 4392:                 my $sel = $element;
 4393:                 if ($element eq 'crosslist') {
 4394:                     $sel .= '_'.$counter;
 4395:                 }
 4396:                 foreach my $title (@codetitles) {
 4397:                     $env{'form.'.$sel.'_'.$title} = $codes{$crskey}{$title};
 4398:                 }
 4399:             }
 4400:         }
 4401:     }
 4402:     return;
 4403: }
 4404: 
 4405: sub generate_date_items {
 4406:     my ($currentval,$item) = @_;
 4407:     if ($currentval =~ /\d+/) {
 4408:         my ($tzname,$sec,$min,$hour,$mday,$month,$year) = 
 4409:             &Apache::lonhtmlcommon::get_timedates($currentval);
 4410:         $env{'form.'.$item.'_day'} = $mday;
 4411:         $env{'form.'.$item.'_month'} = $month+1;
 4412:         $env{'form.'.$item.'_year'} = $year;
 4413:     }
 4414:     return;
 4415: }
 4416: 
 4417: sub print_textbook_form {
 4418:     my ($r,$dom,$incdoms,$domdefs,$settings,$can_request) = @_;
 4419:     my (%prefab,%ordered,%numprefab);
 4420:     my $crstype = 'textbook';
 4421: #
 4422: #  Retrieve list of prefabricated courses (textbook courses and templates) cloneable by user
 4423: #
 4424:     foreach my $type ('textbooks','templates') {
 4425:         $numprefab{$type} = 0;
 4426:         if (ref($settings) eq 'HASH') {
 4427:             $prefab{$type} = $settings->{$type};
 4428:             if (ref($prefab{$type}) eq 'HASH') {
 4429:                 foreach my $item (keys(%{$prefab{$type}})) {
 4430:                     my ($clonedom,$clonecrs) = split(/_/,$item);
 4431:                     if (ref($prefab{$type}{$item}) eq 'HASH') {
 4432:                         if (&Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
 4433:                                           $env{'user.domain'},$clonecrs,$clonedom,$crstype)) {
 4434: 
 4435:                             my $num = $prefab{$type}{$item}{'order'};
 4436:                             $ordered{$type}{$num} = $item;
 4437:                             $numprefab{$type} ++;
 4438:                         }
 4439:                     }
 4440:                 }
 4441:             }
 4442:         }
 4443:     }
 4444: 
 4445: #
 4446: # Check if domain has multiple library servers
 4447: #
 4448:     my ($home_server_pick,$numlib) =
 4449:         &Apache::loncommon::home_server_form_item($dom,'chome',
 4450:                                                   'default','hide');
 4451:     if ($numlib > 1) {
 4452:         $home_server_pick = &mt('Home Server for Course').': '.$home_server_pick.'<br />';
 4453:     }
 4454: 
 4455: #
 4456: # Retrieve information about courses owned by user, or in which user has an active or future 
 4457: # Course Coordinator role 
 4458: #
 4459:     my $numcurrent;
 4460:     my %cloneable = &Apache::lonnet::courseiddump($dom,'.',1,'.',$env{'user.name'}.':'.$env{'user.domain'},
 4461:                                               undef,undef,undef,'Course');
 4462:     my %ccroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
 4463:                                                 ['active','future'],['cc']);
 4464:     foreach my $role (keys(%ccroles)) {
 4465:         my ($cnum,$cdom,$rest) = split(/:/,$role,3);
 4466:         unless (exists($cloneable{$cdom.'_'.$cnum})) {
 4467:             my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum,{'one_time' => 1});
 4468:             $cloneable{$cdom.'_'.$cnum} = \%courseinfo;
 4469:         }
 4470:     }
 4471: 
 4472:     my $numcurrent = scalar(keys(%cloneable));
 4473: 
 4474:     my $jscript = &textbook_request_javascript(\%numprefab,$numcurrent);
 4475:     my %loaditems;
 4476:     $loaditems{'onload'} = 'javascript:uncheckAllRadio();';
 4477:     $r->print(&header('Course Request',$jscript,\%loaditems));
 4478: 
 4479:     if (ref($can_request) eq 'HASH') {
 4480:         unless ((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) {
 4481:             &Apache::lonhtmlcommon::add_breadcrumb(
 4482:                 { href => '/adm/requestcourse',
 4483:                   text => 'Pick action',
 4484:                 });
 4485:         }
 4486:     }
 4487:     &Apache::lonhtmlcommon::add_breadcrumb({text=>'Course Request'});
 4488:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
 4489: 
 4490:     &startContentScreen($r,'textbookrequests');
 4491: #
 4492: # Show domain selector form, if required.
 4493: #
 4494:     if (@{$incdoms} > 1) {
 4495:         my $onchange = 'this.form.submit()';
 4496:         $r->print('<form name="domforcourse" method="post" action="/adm/requestcourse">'.
 4497:                   '<div><fieldset><legend>'.&mt('Domain').'</legend>'.
 4498:                   &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange,$incdoms).
 4499:                   '</fieldset></form>');
 4500:     }
 4501: 
 4502: #
 4503: # Course request form
 4504: #
 4505: 
 4506: #
 4507: # Course Title
 4508: #
 4509:     $r->print('<form name="requestcourse" method="post" action="/adm/requestcourse" onsubmit="return validTextbookReq();">'.
 4510:               '<div>'.
 4511:               '<fieldset>'.
 4512:               '<legend>'.&mt('Course Information').'</legend>'.
 4513:               '<span class="LC_nobreak">'.&mt('Title').':&nbsp'.
 4514:               '<input type="text" size="60" name="cdescr" value="" /></span><br />'.
 4515:               $home_server_pick.'<br /></fieldset>'.
 4516:               '</div>');
 4517: 
 4518: #
 4519: # Content source selection, if more than one available
 4520: #
 4521:     if (keys(%cloneable) || keys(%ordered)) {
 4522:         $r->print('<div>'. 
 4523:                   '<fieldset><legend>'.&mt('Course Content').'</legend>');
 4524:         if (keys(%ordered)) {
 4525:             if (ref($ordered{'textbooks'}) eq 'HASH') {
 4526:                 $r->print('<span class="LC_nobreak"><label>'.
 4527:                           '<input type="radio" name="cloning" value="textbook" onclick="javascript:cloneChoice();" />'.
 4528:                           &mt('Load textbook content').'</span>'.('&nbsp;'x2).' ');
 4529:             }
 4530:             if (ref($ordered{'templates'}) eq 'HASH') {
 4531:                 $r->print('<span class="LC_nobreak"><label>'.
 4532:                           '<input type="radio" name="cloning" value="template" onclick="javascript:cloneChoice();" />'.
 4533:                           &mt('Load pre-existing template').'</span>'.('&nbsp;'x2).' ');
 4534:             }
 4535:         }
 4536:         if (keys(%cloneable)) {
 4537:             $r->print('<span class="LC_nobreak"><label>'.
 4538:                       '<input type="radio" name="cloning" value="existing" onclick="javascript:cloneChoice();" />'.
 4539:                       &mt('Copy one of your courses').'</label></span>'.('&nbsp;'x2).' ');
 4540:         }
 4541:         $r->print('<span class="LC_nobreak"><label>'.
 4542:                   '<input type="radio" name="cloning" value="none" checked="checked" onclick="javascript:cloneChoice();" />'.
 4543:                   &mt('Empty course shell').'</label></span>');
 4544:     } else {
 4545:         $r->print('<input type="hidden" name="cloning" value="none" />');
 4546:     }
 4547: 
 4548: #
 4549: # Table of cloneable textbook courses
 4550: #
 4551:     if (keys(%ordered)) {
 4552:         foreach my $type ('textbooks','templates') {
 4553:             my $divid = 'showtextbook';
 4554:             my $radioid = 'book';
 4555:             if ($type eq 'templates') {
 4556:                 $divid = 'showtemplate'; 
 4557:                 $radioid = 'template';
 4558:             }
 4559:             if (ref($ordered{$type}) eq 'HASH') {
 4560:                 $r->print('<div id="'.$divid.'" style="display:none">'.
 4561:                           &Apache::loncommon::start_data_table().
 4562:                           &Apache::loncommon::start_data_table_header_row().
 4563:                           '<th>'.&mt('Title').'</th>');
 4564:                 if ($type eq 'textbooks') {
 4565:                     $r->print('<th>'.&mt('Author(s)').'</th>');
 4566:                 }
 4567:                 $r->print('<th>'.&mt('Subject').'</th>');
 4568:                 if ($type eq 'textbooks') {
 4569:                     $r->print('<th>'.&mt('Publisher').'</th>'.
 4570:                               '<th>'.&mt('Book').'</th>');
 4571:                 }
 4572:                 $r->print(&Apache::loncommon::end_data_table_header_row());
 4573:                 my @items = sort { $a <=> $b } keys(%{$ordered{$type}});
 4574:                 foreach my $num (@items) {
 4575:                     my $item = $ordered{$type}{$num};
 4576:                     my $cleantitle=&HTML::Entities::encode($prefab{$type}{$item}{'title'},'<>&"');
 4577:                     $cleantitle=~s/'/\\'/g;
 4578:                     $cleantitle =~ s/^\s+//;
 4579:                     $r->print(&Apache::loncommon::start_data_table_row().
 4580:                               '<td><label><input type="radio" name="'.$radioid.'" value="'.$item.'" />'.
 4581:                               $cleantitle.'</label></td>');
 4582:                     if ($type eq 'textbooks') {
 4583:                          $r->print('<td>'.$prefab{$type}{$item}{'author'}.'</td>');
 4584:                     }
 4585:                     $r->print('<td>'.$prefab{$type}{$item}{'subject'}.'</td>');
 4586:                     if ($type eq 'textbooks') {
 4587:                         $r->print('<td>'.$prefab{$type}{$item}{'publisher'}.'</td>'.
 4588:                                   '<td><img border="0" src="'.$prefab{$type}{$item}{'image'}.
 4589:                                   '" alt="'.$cleantitle.'" /></td>');
 4590:                     }
 4591:                     $r->print(&Apache::loncommon::end_data_table_row());
 4592:                 }
 4593:                 $r->print(&Apache::loncommon::end_data_table().
 4594:                           '</div>');
 4595:             }
 4596:         }
 4597:     }
 4598: 
 4599: #
 4600: # Table of user's current courses (owner and/or course coordinator)
 4601: #
 4602:     if (keys(%cloneable)) {
 4603:         my %lt = &clone_text();
 4604:         $r->print('<div id="showexisting" style="display:none">'.
 4605:                   &Apache::loncommon::start_data_table().
 4606:                   &Apache::loncommon::start_data_table_header_row().
 4607:                   '<th>'.&mt('Title').'</th>'.
 4608:                   '<th>'.&mt('Owner/co-owner(s)').'</th>'.
 4609:                   &Apache::loncommon::end_data_table_header_row());
 4610:         my %allownernames;
 4611:         my %sortbytitle; 
 4612:         foreach my $cid (sort(keys(%cloneable))) {
 4613:             if (ref($cloneable{$cid}) eq 'HASH') {
 4614:                 my $cdesc = $cloneable{$cid}{'description'};
 4615:                 $cdesc =~ s/`/'/g;
 4616:                 if ($cdesc ne '') {
 4617:                     push(@{$sortbytitle{$cdesc}},$cid);
 4618:                 }
 4619:             }
 4620:          }
 4621:          foreach my $title (sort(keys(%sortbytitle))) {
 4622:              if (ref($sortbytitle{$title}) eq 'ARRAY') {
 4623:                 foreach my $cid (sort(@{$sortbytitle{$title}})) {
 4624:                     my $cleantitle=&HTML::Entities::encode($title,'<>&"');
 4625:                     $cleantitle=~s/'/\\'/g;
 4626:                     $cleantitle =~ s/^\s+//;
 4627:                     my ($namestr,@owners,%ownernames);
 4628:                     my $singleowner = $cloneable{$cid}{'internal.courseowner'};
 4629:                     push(@owners,$singleowner);
 4630:                     if ($cloneable{$cid}{'co-owners'} ne '') {
 4631:                         foreach my $item (split(/,/,$cloneable{$cid}{'internal.co-owners'})) {
 4632:                             push(@owners,$item);
 4633:                         }
 4634:                     }
 4635:                     foreach my $owner (@owners) {
 4636:                         my ($ownername,$ownerdom);
 4637:                         if ($owner =~ /:/) {
 4638:                             ($ownername,$ownerdom) = split(/:/,$owner);
 4639:                         } else {
 4640:                             $ownername = $owner;
 4641:                             if ($owner ne '') {
 4642:                                 $ownerdom = $dom;
 4643:                             }
 4644:                         }
 4645:                         if ($ownername ne '' && $ownerdom ne '') {
 4646:                             if (exists($allownernames{$ownername.':'.$ownerdom})) {
 4647:                                 $ownernames{$ownername.':'.$ownerdom} = $allownernames{$ownername.':'.$ownerdom}; 
 4648:                             } else {
 4649:                                 my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
 4650:                                 $ownernames{$ownername.':'.$ownerdom} = \%namehash;
 4651:                                 $allownernames{$ownername.':'.$ownerdom} = $ownernames{$ownername.':'.$ownerdom};
 4652:                             }
 4653:                         }
 4654:                     }
 4655:                     my @lastnames;
 4656:                     foreach my $owner (keys(%ownernames)) {
 4657:                         if (ref($ownernames{$owner}) eq 'HASH') {
 4658:                             push(@lastnames,$ownernames{$owner}{'lastname'});
 4659:                         }
 4660:                     }
 4661:                     if (@lastnames) {
 4662:                         $namestr = join(', ',sort(@lastnames));
 4663:                     }
 4664:                     $r->print(&Apache::loncommon::start_data_table_row().
 4665:                               '<td><label><input type="radio" name="owned" value="'.$cid.'" />'.
 4666:                               '&nbsp'.$cleantitle.'</label></td>'.
 4667:                               '<td>'.$namestr.'</td>'.
 4668:                               &Apache::loncommon::end_data_table_row());
 4669:                 }
 4670:             }
 4671:         }
 4672:         $r->print(&Apache::loncommon::end_data_table().
 4673:               '<p><input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
 4674:               '</label><br /><label>'.
 4675:               '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
 4676:               '</label><br /><label>'.
 4677:               '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
 4678:               $lt{'shd'}.'</label>'.
 4679:               '<input type="text" size="5" name="dateshift" value="365" />'.
 4680:               '</div>');
 4681:     }
 4682: #
 4683: # End of content selector
 4684: #
 4685:     if (keys(%cloneable) || keys(%ordered)) {
 4686:         $r->print('</fieldset></div>');
 4687:     }
 4688: 
 4689:     my %accesstitles = (
 4690:                           'start' => 'Default start access',
 4691:                            'end'   => 'Default end access',
 4692:                        );
 4693:     my %help_item = (
 4694:                        start => 'Course_Request_Access_Start',
 4695:                        end   => 'Course_Request_Access_End',
 4696:                     );
 4697:     my $starttime = time;
 4698:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
 4699:     my $startform = &Apache::lonhtmlcommon::date_setter('requestcourse','accessstart',
 4700:                                                         $starttime,'','','',1,'','','',1);
 4701:     my $endform = &Apache::lonhtmlcommon::date_setter('requestcourse','accessend',
 4702:                                                       $endtime,'','','',1,'','','',1);
 4703: #
 4704: # Set default start and end dates for student access
 4705: # 
 4706:     $r->print('<div>'.
 4707:               '<fieldset><legend>'.&mt('Student Access Dates').'</legend>'.
 4708:               &Apache::loncommon::help_open_topic($help_item{'start'}).
 4709:               '&nbsp;'.&mt($accesstitles{'start'}).$startform.'<br />'.
 4710:               &Apache::loncommon::help_open_topic($help_item{'end'}).
 4711:               '&nbsp;'.&mt($accesstitles{'end'}).$endform.'<br /></div>');
 4712: 
 4713: #
 4714: # Display any custom fields for this course type
 4715: #
 4716:     my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 4717:                                                  $env{'user.domain'});
 4718:     my $postprocess = &Apache::lonnet::auto_crsreq_update($dom,undef,$crstype,'review',
 4719:                                                           $env{'user.name'},
 4720:                                                           $env{'user.domain'},$fullname);
 4721:     if (ref($postprocess) eq 'HASH') {
 4722:         if ($postprocess->{'reviewweb'}) {
 4723:             $r->print($postprocess->{'reviewweb'});
 4724:         }
 4725:     }
 4726: 
 4727: #
 4728: # Submit button
 4729: #
 4730:     $r->print('<input type="hidden" name="crstype" value="textbook" />'.
 4731:               '<input type="hidden" name="action" value="process" />'.
 4732:               '<input type="submit" value="'.&mt('Create course').'" />');
 4733: 
 4734: #
 4735: # End request form
 4736: #
 4737:     $r->print('</form>');
 4738:     &endContentScreen($r).
 4739:     $r->print(&Apache::loncommon::end_page());
 4740:     return;
 4741: }
 4742: 
 4743: sub process_textbook_request {
 4744:     my ($r,$dom,$action,$domdefs,$domconfig,$can_request) = @_;
 4745:     my ($uniquecode,$req_notifylist);
 4746:     my $crstype = 'textbook';
 4747:     if (ref($domconfig) eq 'HASH') {
 4748:         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
 4749:             if (ref($domconfig->{'requestcourses'}{'notify'}) eq 'HASH') {
 4750:                 $req_notifylist = $domconfig->{'requestcourses'}{'notify'}{'approval'};
 4751:             }
 4752:             if (ref($domconfig->{'requestcourses'}{'uniquecode'}) eq 'HASH') {
 4753:                 $uniquecode = $domconfig->{'requestcourses'}{'uniquecode'}{$crstype};
 4754:             }
 4755:         }
 4756:     }
 4757:     my $now = time;
 4758:     my $reqtype = $env{'form.cloning'};
 4759:     my (@inststatuses,$storeresult,$creationresult);
 4760:     my $cnum = &Apache::lonnet::generate_coursenum($dom,'Course');
 4761:     my ($clonefrom,$clonedom,$clonecrs);
 4762:     if ($reqtype eq 'textbook') {
 4763:         $clonefrom = $env{'form.book'};
 4764:     } elsif ($reqtype eq 'template') {
 4765:         $clonefrom = $env{'form.template'};
 4766:     } elsif ($reqtype eq 'existing') {
 4767:         $clonefrom = $env{'form.owned'};
 4768:     }
 4769:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
 4770:     if ($clonefrom) {
 4771:         ($clonedom,$clonecrs) = split(/_/,$clonefrom);
 4772:         if (&Apache::lonnet::homeserver($clonecrs,$clonedom) ne 'no_host') {
 4773:             my $canclone =
 4774:                 &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
 4775:                             $env{'user.domain'},$clonecrs,$clonedom,$crstype);
 4776:             unless ($canclone) {
 4777:                 undef($clonecrs);
 4778:                 undef($clonedom);
 4779:             }
 4780:         } else {
 4781:             undef($clonecrs);
 4782:             undef($clonedom);
 4783:         }
 4784:     }
 4785:     $r->print(&header('Course Creation'));
 4786: 
 4787:     if (ref($can_request) eq 'HASH') {
 4788:         unless ((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) {
 4789:             &Apache::lonhtmlcommon::add_breadcrumb(
 4790:                 { href => '/adm/requestcourse',
 4791:                   text => 'Pick action',
 4792:                 });
 4793:         }
 4794:     }
 4795:     &Apache::lonhtmlcommon::add_breadcrumb(
 4796:                                            { href => '/adm/requestcourse',
 4797:                                              text => "Create Course",
 4798:                                            }
 4799:                                           );
 4800:     &Apache::lonhtmlcommon::add_breadcrumb({text=>'Request Processed'});
 4801:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
 4802:     &startContentScreen($r,'textbookrequests');
 4803: 
 4804:     my $details = {
 4805:                     owner          => $env{'user.name'},
 4806:                     domain         => $env{'user.domain'},
 4807:                     cdom           => $dom,
 4808:                     cnum           => $cnum,
 4809:                     coursehome     => $env{'form.chome'},
 4810:                     cdescr         => $env{'form.cdescr'},
 4811:                     crstype        => $crstype,
 4812:                     uniquecode     => $uniquecode,
 4813:                     clonedom       => $clonedom,
 4814:                     clonecrs       => $clonecrs,
 4815:                     accessstart    => $accessstart,
 4816:                     accessend      => $accessend,
 4817:                     personnel      => {},
 4818:                   };
 4819:     if ($reqtype eq 'existing') {
 4820:         $details->{datemode} = $env{'form.datemode'};
 4821:         $details->{dateshift} = $env{'form.dateshift'};
 4822:     }
 4823:     my $lonhost = $r->dir_config('lonHostID');
 4824:     my ($result,$output) = &process_request($lonhost,$dom,$cnum,$crstype,$now,$details,'',
 4825:                                             $req_notifylist,[],$domconfig);
 4826:     $r->print($output);
 4827:     if (&Apache::loncoursequeueadmin::author_prompt()) {
 4828:         &print_author_prompt($r,$action,$cnum,$dom,$crstype,$result);
 4829:     } elsif ($result eq 'created') {
 4830:         $r->print('<p><a href="/adm/requestcourse">'.&mt('Create another course').'</a></p>');
 4831:     }
 4832:     &endContentScreen($r);
 4833:     $r->print(&Apache::loncommon::end_page());
 4834: }
 4835: 
 4836: sub textbook_request_javascript {
 4837:     my ($numprefab,$numcurrent) = @_;
 4838:     return unless (ref($numprefab) eq 'HASH');
 4839:     return if (!$numprefab->{'textbooks'} && !$numprefab->{'templates'} && !$numcurrent);
 4840:     my %lt = &Apache::lonlocal::texthash(
 4841:                  choose   => 'Please select a content option.',
 4842:                  textbook => 'Please select a textbook, or choose a different option.',
 4843:                  template => 'Please select a template, or choose a different option.',        
 4844:                  existing => 'Please select one of your existing courses to copy, or choose a different option.',
 4845:                  title    => 'Please enter a course title.',
 4846:              );
 4847:     return <<"ENDSCRIPT";
 4848: function cloneChoice() {
 4849:     if (document.requestcourse.cloning) {
 4850:         var radioLength = document.requestcourse.cloning.length;
 4851:         if (radioLength == undefined) {
 4852:             var val = document.requestcourse.cloning.value;
 4853:             if ((val == 'textbook') || (val == 'template') || (val == 'existing')) {
 4854:                 var elem = document.getElementById('show'+val);
 4855:                 if (document.requestcourse.cloning.checked) {
 4856:                     elem.style.display = 'block';
 4857:                 } else {
 4858:                     uncheckRadio(val);
 4859:                     elem.style.display = 'none';
 4860:                 }
 4861:             }
 4862:         } else {
 4863:             for (var i=0; i<radioLength; i++) {
 4864:                 var val = document.requestcourse.cloning[i].value;
 4865:                 if ((val == 'textbook') || (val == 'template') || (val == 'existing')) {
 4866:                     var elem = document.getElementById('show'+val);
 4867:                     if (document.requestcourse.cloning[i].checked) {
 4868:                         elem.style.display = 'block';
 4869:                     }  else {
 4870:                         if (val == 'textbook') {
 4871:                             uncheckRadio('book');
 4872:                         }
 4873:                         if (val == 'template') {
 4874:                             uncheckRadio('template');
 4875:                         }
 4876:                         if (val == 'existing') {
 4877:                             uncheckRadio('owned');
 4878:                         }
 4879:                         elem.style.display = 'none';
 4880:                     }
 4881:                 }
 4882:             }
 4883:         }
 4884:     }
 4885:     return;
 4886: }
 4887: 
 4888: function uncheckRadio(radioGroupName) {
 4889:     var group = document.getElementsByName(radioGroupName);
 4890:     var radioLength = group.length;
 4891:     if (radioLength == undefined) {
 4892:         group.checked = false;
 4893:     } else {
 4894:         for (var i=0; i<radioLength; i++) {
 4895:             group[i].checked = false;
 4896:         }
 4897:     }
 4898:     return;
 4899: }
 4900: 
 4901: function uncheckAllRadio() {
 4902:     uncheckRadio('cloning');
 4903:     var numbook = $numprefab->{'textbooks'};
 4904:     var numtemplate = $numprefab->{'templates'};
 4905:     var numcurrent = $numcurrent;
 4906:     if (numbook > 0) {
 4907:         uncheckRadio('textbook'); 
 4908:     }
 4909:     if (nutemplate > 0) {
 4910:         uncheckRadio('template');
 4911:     }m
 4912:     if (numcurrent > 0) {
 4913:         uncheckRadio('existing');
 4914:     }
 4915:     return;
 4916: }
 4917: 
 4918: function validTextbookReq() {
 4919:     if (document.requestcourse.cloning) {
 4920:         var cloneChoice = 0;
 4921:         var radioLength = document.requestcourse.cloning.length;
 4922:         if (radioLength == undefined) {
 4923:             if (document.requestcourse.cloning.checked == false) {
 4924:                 alert("$lt{'choose'}");
 4925:                 return false;
 4926:             } else {
 4927:                 cloneChoice = document.requestcourse.cloning.value;
 4928:             }
 4929:         } else {
 4930:             for (var i=0; i<radioLength; i++) {
 4931:                 if (document.requestcourse.cloning[i].checked) {
 4932:                     cloneChoice = document.requestcourse.cloning[i].value;
 4933:                     break;
 4934:                 }
 4935:             }
 4936:             if (cloneChoice == 0) {
 4937:                 alert("$lt{'choose'}");
 4938:                 return false;
 4939:             }
 4940:         }
 4941:         var group;
 4942:         if ((cloneChoice == 'textbook') || (cloneChoice == 'template') || (cloneChoice == 'existing')) {
 4943:             var group;
 4944:             if (cloneChoice == 'textbook') {
 4945:                 group = document.getElementsByName('book');
 4946:             } else {
 4947:                 if (cloneChoice == 'template') {
 4948:                     group = document.getElementsByName('template');
 4949:                 } else {
 4950:                     group = document.getElementsByName('owned');
 4951:                 }
 4952:             }
 4953:             var groupLength = group.length;
 4954:             var chosen = 0;
 4955:             if (groupLength == undefined) {
 4956:                 if (group.checked) {
 4957:                     chosen = 1;
 4958:                 }
 4959:             } else {
 4960:                 for (var j=0; j<groupLength; j++) {
 4961:                     if (group[j].checked) {
 4962:                         chosen = 1;
 4963:                         break;
 4964:                     }
 4965:                 }
 4966:            }
 4967:            if (chosen == 0) {
 4968:                if (cloneChoice == 'textbook') {
 4969:                    alert("$lt{'textbook'}");
 4970:                } else {
 4971:                    if (cloneChoice == 'template') {
 4972:                        alert("$lt{'template'}");
 4973:                    } else {
 4974:                        alert("$lt{'existing'}");
 4975:                    }
 4976:                }
 4977:                return false;
 4978:             }
 4979:         }
 4980:     }
 4981:     if (document.requestcourse.cdescr.value == '') {
 4982:         alert("$lt{'title'}");
 4983:         return false;
 4984:     }
 4985:     return true;
 4986: }
 4987:  
 4988: ENDSCRIPT
 4989: 
 4990: }
 4991: 
 4992: sub startContentScreen {
 4993:     my ($r,$mode)=@_;
 4994:     $r->print("\n".'<ul class="LC_TabContentBigger" id="textbookreq">'."\n");
 4995:     $r->print('<li'.(($mode eq 'textbookrequests')?' class="active"':'').'><a href="/adm/requestcourse"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Request a Course').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n");
 4996:     $r->print('<li'.(($mode eq 'textbooklogs')?' class="active"':'').'><a href="/adm/requestcourse?action=log&amp;crstype=textbook&amp;tabs=on"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Course Request History').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n");
 4997:     $r->print("\n".'</ul>'."\n");
 4998:     $r->print('<div class="LC_Box" style="clear:both;margin:0;"><div id="mainbox" style="margin:0 0;padding:0 0;"><div class="LC_ContentBox" id="maincontentbox" style="display: block;">');
 4999: }
 5000: 
 5001: sub endContentScreen {
 5002:    my ($r)=@_;
 5003:    $r->print('</div></div></div>');
 5004: }
 5005:    
 5006: 1;
 5007: 

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