File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.117: download - view: text, annotated - select for diffs
Wed Mar 29 16:01:13 2023 UTC (13 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Accommodate argument changes in &selectbox() and &prettyinput() in lonmeta.pm
  rev. 1.255.

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

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