File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.85: download - view: text, annotated - select for diffs
Mon Jun 16 01:42:22 2014 UTC (9 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0, HEAD
- Add missing subroutine: &textbook_request_disabled()
- In &get_breadcrumbs() sanity checking -- $states->{$action} needs to be
  a ref to an ARRAY.

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

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