File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.95.2.2.2.1: download - view: text, annotated - select for diffs
Fri Jul 17 20:39:55 2020 UTC (3 years, 9 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.95.2.2: preferred, unified
- For 2.11.2 modified
  Include changes in rev. 1.104 and 1.109:

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

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