Annotation of loncom/interface/lonrequestcourse.pm, revision 1.32

1.1       raeburn     1: # The LearningOnline Network
                      2: # Request a course
                      3: #
1.32    ! raeburn     4: # $Id: lonrequestcourse.pm,v 1.31 2009/09/08 13:05:00 raeburn Exp $
1.1       raeburn     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: 
1.27      raeburn    47: =item get_breadcrumbs()
                     48: 
1.12      raeburn    49: =item header()
                     50: 
                     51: =item form_elements()
                     52: 
                     53: =item onload_action()
                     54: 
                     55: =item check_can_request() 
                     56: 
                     57: =item course_types()
                     58: 
                     59: =item print_main_menu()
                     60: 
                     61: =item request_administration()
                     62: 
1.27      raeburn    63: =item close_popup_form()
                     64: 
                     65: =item get_instcode()
                     66: 
1.12      raeburn    67: =item print_request_form()
                     68: 
                     69: =item print_enrollment_menu()
                     70: 
1.27      raeburn    71: =item show_invalid_crosslists()
                     72: 
1.12      raeburn    73: =item inst_section_selector()
                     74: 
                     75: =item date_setting_table()
                     76: 
                     77: =item print_personnel_menu()
                     78: 
                     79: =item print_request_status()
                     80: 
                     81: =item print_request_logs()
                     82: 
                     83: =item print_review()
                     84: 
                     85: =item dates_from_form()
                     86: 
                     87: =item courseinfo_form()
                     88: 
                     89: =item clone_form()
                     90: 
                     91: =item clone_text()
                     92: 
                     93: =item coursecode_form()
                     94: 
                     95: =item get_course_dom()
                     96: 
                     97: =item display_navbuttons()
                     98: 
                     99: =item print_request_outcome()
                    100: 
                    101: =item get_processtype()
                    102: 
                    103: =item check_autolimit()
                    104: 
                    105: =item retrieve_settings()
                    106: 
                    107: =item get_request_settings()
                    108: 
1.27      raeburn   109: =item extract_instcode() 
                    110: 
                    111: =item generate_date_items()
                    112: 
1.1       raeburn   113: =back
                    114: 
                    115: =cut
                    116: 
                    117: package Apache::lonrequestcourse;
                    118: 
                    119: use strict;
                    120: use Apache::Constants qw(:common :http);
                    121: use Apache::lonnet;
                    122: use Apache::loncommon;
                    123: use Apache::lonlocal;
1.8       raeburn   124: use Apache::loncoursequeueadmin;
1.30      raeburn   125: use Apache::lonuserutils;
1.4       raeburn   126: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn   127: 
                    128: sub handler {
                    129:     my ($r) = @_;
1.20      raeburn   130:     &Apache::loncommon::content_type($r,'text/html');
                    131:     $r->send_http_header;
1.1       raeburn   132:     if ($r->header_only) {
                    133:         return OK;
                    134:     }
                    135: 
1.27      raeburn   136:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    137:                                             ['action','showdom','cnum','state']);
1.2       raeburn   138:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    139:     my $dom = &get_course_dom();
1.1       raeburn   140:     my $action = $env{'form.action'};
                    141:     my $state = $env{'form.state'};
1.27      raeburn   142:     my (%states,%stored);
                    143:     my ($jscript,$uname,$udom,$result,$warning);
                    144: 
                    145:     $states{'display'} = ['details'];
                    146:     $states{'view'} = ['pick_request','details','cancel','removal'];
                    147:     $states{'log'} = ['filter','display'];
                    148:     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
                    149: 
                    150:     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
                    151:         unless ($env{'form.state'} eq 'crstype') {
                    152:             unshift(@{$states{'new'}},'codepick');
                    153:         }
                    154:     }
                    155: 
                    156:     foreach my $key (keys(%states)) {
                    157:         if (ref($states{$key}) eq 'ARRAY') {
                    158:             unshift (@{$states{$key}},'crstype');
                    159:         }
                    160:     }
                    161: 
                    162:     my @invalidcrosslist;
                    163:     my %trail = (
                    164:                  crstype       => 'Course Request Action',
                    165:                  codepick      => 'Category',
                    166:                  courseinfo    => 'Description',
                    167:                  enrollment    => 'Access Dates',
                    168:                  personnel     => 'Personnel',
                    169:                  review        => 'Review',
                    170:                  process       => 'Result',
                    171:                  pick_request  => 'Display Summary',
                    172:                  details       => 'Request Details',
                    173:                  cancel        => 'Cancel Request',
                    174:                  removal       => 'Outcome',
                    175:                 );
                    176: 
                    177:     if (($env{'form.crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
                    178:         $trail{'enrollment'} = 'Enrollment';
                    179:     }
                    180: 
                    181:     my ($page,$crumb,$newinstcode,$codechk,$checkedcode) = 
                    182:         &get_breadcrumbs($dom,$action,\$state,\%states,\%trail);
1.26      raeburn   183:     if ($action eq 'display') {
                    184:         if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                    185:             my $namespace = 'courserequestqueue';
                    186:             if ($env{'form.cnum'} ne '') {
                    187:                 my $cnum = $env{'form.cnum'};
                    188:                 my $reqkey = $cnum.'_approval';
                    189:                 my $namespace = 'courserequestqueue';
                    190:                 my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                    191:                 my %queued =
                    192:                     &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                    193:                 if (ref($queued{$reqkey}) eq 'HASH') {
                    194:                     $uname = $queued{$reqkey}{'ownername'};
                    195:                     $udom  = $queued{$reqkey}{'ownerdom'};
                    196:                     if (($udom =~ /^$match_domain$/) && ($uname =~ /^$match_username$/)) {
                    197:                         $result = &retrieve_settings($dom,$cnum,$udom,$uname);
                    198:                     } else {
                    199:                         $warning = &mt('Invalid username or domain for course requestor');
                    200:                     }
                    201:                 } else {
                    202:                     $warning = &mt('No information was found for this course request.');
                    203:                 }
                    204:             } else {
                    205:                 $warning = &mt('No course request ID provided.');
                    206:             }
                    207:         } else {
                    208:             $warning = &mt('You do not have rights to view course request information.');
                    209:         }
                    210:     } elsif ((defined($state)) && (defined($action))) {
1.16      raeburn   211:         if (($action eq 'view') && ($state eq 'details')) {
                    212:             if ((defined($env{'form.showdom'})) && (defined($env{'form.cnum'}))) {
                    213:                 my $result = &retrieve_settings($env{'form.showdom'},$env{'form.cnum'});
1.2       raeburn   214:             }
1.27      raeburn   215:         } elsif ($env{'form.crstype'} eq 'official') {
                    216:             if (&Apache::lonnet::auto_run('',$dom)) {
                    217:                 if (($action eq 'new') && (($state eq 'enrollment') || 
                    218:                     ($state eq 'personnel'))) {
                    219:                     my $checkcrosslist = 0;
                    220:                     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    221:                         if ($env{'form.crosslist_'.$i}) {
                    222:                             $checkcrosslist ++;
                    223:                         }
                    224:                     }
                    225:                     if ($checkcrosslist) {
                    226:                         my %codechk;
                    227:                         my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
                    228:                         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,
                    229:                                                                  \%cat_titles,
                    230:                                                                  \%cat_order,
                    231:                                                                  \@code_order);
                    232:                         my $numtitles = scalar(@codetitles);
                    233:                         if ($numtitles) {
                    234:                             for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    235:                                 if ($env{'form.crosslist_'.$i}) {
                    236:                                     my $codecheck;
                    237:                                     my $crosslistcode = '';
                    238:                                     foreach my $item (@code_order) {
                    239:                                         $crosslistcode .= $env{'form.crosslist_'.$i.'_'.$item}; 
                    240:                                     }
                    241:                                     if ($crosslistcode ne '') { 
                    242:                                          $codechk{$i} = 
                    243:                                             &Apache::lonnet::auto_validate_instcode('',$dom,$crosslistcode);
                    244:                                     }
                    245:                                     unless ($codechk{$i} eq 'valid') {
                    246:                                         $env{'form.crosslist_'.$i} = '';
                    247:                                         push(@invalidcrosslist,$crosslistcode);
                    248:                                     } 
                    249:                                 }
                    250:                             }
                    251:                         }
                    252:                     }
                    253:                 }
                    254:             }
1.2       raeburn   255:         }
1.16      raeburn   256:         my %elements =  &form_elements($dom);
1.2       raeburn   257:         my $elementsref = {};
                    258:         if (ref($elements{$action}) eq 'HASH') {
                    259:             if (ref($elements{$action}{$state}) eq 'HASH') {
                    260:                 $elementsref = $elements{$action}{$state};
                    261:             }
                    262:         }
1.16      raeburn   263:         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
                    264:             $env{'form.clonedom'} = $dom;
                    265:         }
1.30      raeburn   266:         if ($state eq 'crstype') {
                    267:             $jscript = &mainmenu_javascript();
                    268:         } else {
                    269:             $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
                    270:         }
1.2       raeburn   271:     }
                    272: 
                    273:     if ($state eq 'personnel') {
                    274:         $jscript .= "\n".&Apache::loncommon::userbrowser_javascript();
                    275:     }
                    276: 
                    277:     my $loaditems = &onload_action($action,$state);
                    278: 
1.1       raeburn   279:     my %can_request;
                    280:     my $canreq = &check_can_request($dom,\%can_request);
                    281:     if ($action eq 'new') {
                    282:         if ($canreq) {
                    283:             if ($state eq 'crstype') {
1.3       raeburn   284:                 &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,
                    285:                                  $crumb);
1.1       raeburn   286:             } else {
1.27      raeburn   287:                 &request_administration($r,$action,$state,$page,\%states,$dom,
                    288:                                         $jscript,$loaditems,$crumb,$newinstcode,
                    289:                                         $codechk,$checkedcode,\@invalidcrosslist);
1.1       raeburn   290:             }
                    291:         } else {
1.3       raeburn   292:             $r->print(&header('Course Requests').$crumb.
1.1       raeburn   293:                       '<div class="LC_warning">'.
                    294:                       &mt('You do not have privileges to request creation of courses.').
1.2       raeburn   295:                       '</div>'.&Apache::loncommon::end_page());
1.1       raeburn   296:         }
                    297:     } elsif ($action eq 'view') {
1.10      raeburn   298:         if ($state eq 'crstype') {
1.30      raeburn   299:             &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,$crumb);
1.26      raeburn   300:         } else {
                    301:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
                    302:                                     $loaditems,$crumb);
                    303:         }
                    304:     } elsif ($action eq 'display') {
                    305:         if ($warning ne '') {
                    306:             my $args = { only_body => 1 };
                    307:             $r->print(&header('Course Requests','','',$args).$crumb.
                    308:                       '<h3>'.&mt('Course Request Details').'</h3>'.
                    309:                       '<div class="LC_warning">'.$warning.'</div>'.
                    310:                       &close_popup_form());
1.11      raeburn   311:         } else {
1.26      raeburn   312:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
1.27      raeburn   313:                                     $loaditems,$crumb,'','','','',$uname,$udom);
1.10      raeburn   314:         }
1.1       raeburn   315:     } elsif ($action eq 'log') {
1.3       raeburn   316:         &print_request_logs($jscript,$loaditems,$crumb);
1.1       raeburn   317:     } else {
1.3       raeburn   318:         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
1.1       raeburn   319:     }
                    320:     return OK;
                    321: }
                    322: 
1.30      raeburn   323: sub mainmenu_javascript {
                    324:     return <<"END";
                    325: function setType(courseForm) {
                    326:     for (var i=0; i<courseForm.crstype.length; i++) {
                    327:         if (courseForm.crstype.options[i].value == "$env{'form.crstype'}") {
                    328:             courseForm.crstype.options[i].selected = true;
                    329:         } else {
                    330:             courseForm.crstype.options[i].selected = false;
                    331:         }
                    332:     }
                    333: }
                    334: 
                    335: function setAction(courseForm) {
                    336:     for (var i=0; i<courseForm.action.length; i++) {
                    337:         if (courseForm.action.options[i].value == "$env{'form.action'}") {
                    338:             courseForm.action.options[i].selected = true;
                    339:         } else {
                    340:             courseForm.action.options[i].selected = false;
                    341:         }
                    342:     }
                    343: }
                    344: END
                    345: }
                    346: 
1.27      raeburn   347: sub get_breadcrumbs {
                    348:     my ($dom,$action,$state,$states,$trail) = @_;
                    349:     my ($crumb,$newinstcode,$codechk,$checkedcode,$numtitles);
                    350:     my $page = 0;
                    351:     if ((ref($states) eq 'HASH') && (ref($trail) eq 'HASH') && (ref($state))) {
                    352:         if (defined($action)) {
                    353:             my $done = 0;
                    354:             my $i=0;
                    355:             if (ref($states->{$action}) eq 'ARRAY') {
                    356:                 while ($i<@{$states->{$action}} && !$done) {
                    357:                     if ($states->{$action}[$i] eq $$state) {
                    358:                         $page = $i;
                    359:                         $done = 1;
                    360:                     }
                    361:                     $i++;
                    362:                 }
                    363:             }
                    364:             if ($env{'form.crstype'} eq 'official') {
                    365:                 if ($page > 1) {
                    366:                     if ($states->{$action}[$page-1] eq 'codepick') {
                    367:                         if ($env{'form.instcode'} eq '') {
                    368:                             ($newinstcode,$numtitles) = &get_instcode($dom);
                    369:                             if ($numtitles) {
                    370:                                 if ($newinstcode eq '') {
                    371:                                     $$state = 'codepick';
                    372:                                     $page --;
                    373:                                 } else {
                    374:                                     $codechk = 
                    375:                                         &Apache::lonnet::auto_validate_instcode('',
                    376:                                             $dom,$newinstcode);
                    377:                                     if ($codechk ne 'valid') {
                    378:                                         $$state = 'codepick';
                    379:                                         $page --;
                    380:                                     }
                    381:                                     $checkedcode = 1;
                    382:                                 }
                    383:                             }
                    384:                         }
                    385:                     }
                    386:                 }
                    387:             }
                    388:             for (my $i=0; $i<@{$states->{$action}}; $i++) {
                    389:                 if ($$state eq $states->{$action}[$i]) {
                    390:                     &Apache::lonhtmlcommon::add_breadcrumb(
                    391:                        {text=>"$trail->{$$state}"});
                    392:                     $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
                    393:                     last;
                    394:                 } else {
                    395:                     if (($$state eq 'process') || ($$state eq 'removal')) {
                    396:                         &Apache::lonhtmlcommon::add_breadcrumb(
                    397:                             { href => '/adm/requestcourse',
                    398:                               text => "$trail->{$states->{$action}[$i]}",
                    399:                             }
                    400:                         );
                    401:                     } else {
                    402:                         &Apache::lonhtmlcommon::add_breadcrumb(
                    403:      { href => "javascript:backPage(document.requestcrs,'$states->{$action}[$i]')",
                    404:        text => "$trail->{$states->{$action}[$i]}", }
                    405:                        );
                    406:                    }
                    407:                }
                    408:            }
                    409:         } else {
                    410:             &Apache::lonhtmlcommon::add_breadcrumb(
                    411:                     {text=>'Pick Action'});
                    412:             $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
                    413:         }
                    414:     } else {
                    415:         &Apache::lonhtmlcommon::add_breadcrumb(
                    416:                 {text=>'Pick Action'});
                    417:         $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
                    418:     }
                    419:     return ($page,$crumb,$newinstcode,$codechk,$checkedcode);
                    420: }
                    421: 
1.2       raeburn   422: sub header {
1.26      raeburn   423:     my ($bodytitle,$jscript,$loaditems,$jsextra,$args) = @_;
1.2       raeburn   424:     if ($jscript) {
1.6       raeburn   425:         $jscript = '<script type="text/javascript">'."\n".
                    426:                    '// <![CDATA['."\n".
                    427:                    $jscript."\n".'// ]]>'."\n".'</script>'."\n";
1.2       raeburn   428:     }
                    429:     if ($loaditems) {
1.26      raeburn   430:         if (ref($args) eq 'HASH') {
                    431:             my %loadhash = (
                    432:                              'add_entries' => $loaditems,
                    433:                            );
                    434:             my %arghash = (%loadhash,%{$args});
                    435:             $args = \%arghash;                  
                    436:         } else {
                    437:             $args = {'add_entries' => $loaditems,};
                    438:         }
1.3       raeburn   439:     }
1.26      raeburn   440:     return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra,$args);
1.2       raeburn   441: }
                    442: 
                    443: sub form_elements {
                    444:     my ($dom) = @_;
                    445:     my %elements =
                    446:     (
                    447:         new => {
                    448:             crstype => {
                    449:                 crstype => 'selectbox',
                    450:                 action  => 'selectbox',
1.16      raeburn   451:                 origcnum => 'hidden', 
1.2       raeburn   452:             },
                    453:             courseinfo => {
                    454:                 cdescr           => 'text',
1.13      raeburn   455:                 clonecrs         => 'text',
                    456:                 clonedom         => 'selectbox',
1.2       raeburn   457:                 datemode         => 'radio',
                    458:                 dateshift        => 'text',
                    459:             },
                    460:             enrollment  => {
1.13      raeburn   461:                 accessstart_month  => 'selectbox',
                    462:                 accessstart_hour   => 'selectbox',
                    463:                 accessend_month    => 'selectbox',
                    464:                 accessend_hour     => 'selectbox',
                    465:                 accessstart_day    => 'text',
                    466:                 accessstart_year   => 'text',
                    467:                 accessstart_minute => 'text',
                    468:                 accessstart_second => 'text',
                    469:                 accessend_day      => 'text',
                    470:                 accessend_year     => 'text',
                    471:                 accessend_minute   => 'text',
                    472:                 accessend_second   => 'text',
1.2       raeburn   473:                 no_end_date      => 'checkbox',
                    474:             },
                    475:             personnel => {
                    476:                 addperson   => 'checkbox', 
                    477:             },
1.13      raeburn   478:             review => {
                    479:                 cnum => 'hidden',
                    480:             },
1.2       raeburn   481:          },
                    482:          view => {
                    483:             crstype => {
                    484:                 crstype => 'selectbox',
                    485:                 action  => 'selectbox',
                    486:             },
                    487:          },
                    488:     );
1.13      raeburn   489:     my %servers = &Apache::lonnet::get_servers($dom,'library');
                    490:     my $numlib = keys(%servers);
                    491:     if ($numlib > 1) {
                    492:         $elements{'new'}{'courseinfo'}{'chome'} = 'selectbox';
                    493:     } else {
                    494:         $elements{'new'}{'courseinfo'}{'chome'} = 'hidden';
                    495:     }
1.2       raeburn   496:     my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
                    497:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    498:                                              \%cat_order,\@code_order);
                    499:     my $numtitles = scalar(@codetitles);
                    500:     if ($numtitles) {
                    501:         my %extras;
                    502:         $lastitem = pop(@codetitles);
                    503:         $extras{'instcode_'.$lastitem} = 'text'; 
                    504:         foreach my $item (@codetitles) {
                    505:             $extras{'instcode_'.$item} = 'selectbox';   
                    506:         }
                    507:         $elements{'new'}{'codepick'} = \%extras;
                    508:     }
                    509:     if (&Apache::lonnet::auto_run('',$dom)) {
                    510:         my %extras = (
                    511:                        sectotal           => 'hidden',
1.13      raeburn   512:                        enrollstart_month  => 'selectbox',
                    513:                        enrollstart_hour   => 'selectbox',
                    514:                        enrollend_month    => 'selectbox',
                    515:                        enrollend_hour     => 'selectbox',
                    516:                        enrollstart_day    => 'text',
                    517:                        enrollstart_year   => 'text',
                    518:                        enrollstart_minute => 'text',
                    519:                        enrollstart_second => 'text',
                    520:                        enrollend_day      => 'text',
                    521:                        enrollend_year     => 'text',
                    522:                        enrollend_minute   => 'text',
                    523:                        enrollend_second   => 'text',
1.2       raeburn   524:                        addcrosslist       => 'checkbox',
                    525:                        autoadds           => 'radio',
                    526:                        autodrops          => 'radio',
                    527:                      );
                    528:         if ($env{'form.sectotal'} > 0) {
                    529:             for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
1.31      raeburn   530:                 $extras{'sec_'.$i} = 'radio';
                    531:                 $extras{'secnum_'.$i} = 'text';
                    532:                 $extras{'loncapasec_'.$i} = 'text';
1.2       raeburn   533:             }
                    534:         }
                    535:         my $crosslisttotal = $env{'form.crosslisttotal'};
1.16      raeburn   536:         if ($env{'form.addcrosslist'}) {
                    537:             $crosslisttotal ++;
                    538:         }
1.24      raeburn   539:         if (!$crosslisttotal) {
1.2       raeburn   540:             $crosslisttotal = 1;
                    541:         }
1.27      raeburn   542: 
1.24      raeburn   543:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    544:             if ($numtitles) {
                    545:                 $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
                    546:             }
                    547:             if (@codetitles > 0) {
                    548:                 foreach my $item (@codetitles) {
                    549:                     $extras{'crosslist_'.$i.'_'.$item} = 'selectbox';
1.2       raeburn   550:                 }
                    551:             }
1.24      raeburn   552:             $extras{'crosslist_'.$i} = 'checkbox';
                    553:             $extras{'crosslist_'.$i.'_instsec'} = 'text',
                    554:             $extras{'crosslist_'.$i.'_lcsec'} = 'text',
1.2       raeburn   555:         }
                    556:         my %mergedhash = (%{$elements{'new'}{'enrollment'}},%extras);
                    557:         %{$elements{'new'}{'enrollment'}} = %mergedhash;
                    558:     }
                    559:     my %people;
                    560:     my $persontotal = $env{'form.persontotal'};
1.16      raeburn   561:     if ($env{'form.addperson'}) {
                    562:         $persontotal ++;
                    563:     }
                    564:     if ((!defined($persontotal)) || (!$persontotal)) {
1.2       raeburn   565:         $persontotal = 1;
                    566:     }
                    567:     for (my $i=0; $i<$persontotal; $i++) {
1.13      raeburn   568:         $people{'person_'.$i.'_uname'}     = 'text',
                    569:         $people{'person_'.$i.'_dom'}       = 'selectbox',
                    570:         $people{'person_'.$i.'_hidedom'}   = 'hidden',
                    571:         $people{'person_'.$i.'_firstname'} = 'text',
                    572:         $people{'person_'.$i.'_lastname'}  = 'text',
                    573:         $people{'person_'.$i.'_emailaddr'} = 'text',
                    574:         $people{'person_'.$i.'_role'}      = 'selectbox',
                    575:         $people{'person_'.$i.'_sec'}       = 'selectbox',
                    576:         $people{'person_'.$i.'_newsec'}    = 'text',
1.2       raeburn   577:     }
                    578:     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
                    579:     %{$elements{'new'}{'personnel'}} = %personnelhash;
                    580:     return %elements;
                    581: }
                    582: 
                    583: sub onload_action {
                    584:     my ($action,$state) = @_;
                    585:     my %loaditems;
                    586:     if (($action eq 'new') || ($action eq 'view')) {
1.30      raeburn   587:         if ($state eq 'crstype') {
                    588:             $loaditems{'onload'} = 'javascript:setAction(document.mainmenu_action);javascript:setType(document.mainmenu_coursetype)';
                    589:         } else {
                    590:             $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
                    591:         }
1.2       raeburn   592:     }
                    593:     return \%loaditems;
                    594: }
                    595: 
1.1       raeburn   596: sub check_can_request {
                    597:     my ($dom,$can_request) = @_;
                    598:     my $canreq = 0;
1.4       raeburn   599:     my ($types,$typename) = &course_types();
1.19      raeburn   600:     my @options = ('approval','validate','autolimit');
                    601:     my $optregex = join('|',@options);
1.4       raeburn   602:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                    603:         foreach my $type (@{$types}) {
1.1       raeburn   604:             if (&Apache::lonnet::usertools_access($env{'user.name'},
                    605:                                                   $env{'user.domain'},
                    606:                                                   $type,undef,'requestcourses')) {
                    607:                 $canreq ++;
                    608:                 if ($dom eq $env{'user.domain'}) {
                    609:                     $can_request->{$type} = 1;
                    610:                 }
                    611:             }
                    612:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                    613:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                    614:                 if (@curr > 0) {
                    615:                     $canreq ++;
                    616:                     unless ($dom eq $env{'user.domain'}) {
1.19      raeburn   617:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
1.1       raeburn   618:                             $can_request->{$type} = 1;
                    619:                         }
                    620:                     }
                    621:                 }
                    622:             }
                    623:         }
                    624:     }
                    625:     return $canreq;
                    626: }
                    627: 
1.4       raeburn   628: sub course_types {
                    629:     my @types = ('official','unofficial','community');
                    630:     my %typename = (
                    631:                          official   => 'Official course',
                    632:                          unofficial => 'Unofficial course',
                    633:                          community  => 'Community',
                    634:                     );
                    635:     return (\@types,\%typename);
                    636: }
                    637: 
                    638: 
1.1       raeburn   639: sub print_main_menu {
1.3       raeburn   640:     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb) = @_;
1.4       raeburn   641:     my ($types,$typename) = &course_types();
1.1       raeburn   642:     my $onchange;
                    643:     unless ($env{'form.interface'} eq 'textual') {
                    644:         $onchange = 1;
                    645:     }
                    646: 
1.2       raeburn   647:     my $nextstate_setter = "\n";
                    648:     if (ref($states) eq 'HASH') {
                    649:         foreach my $key (keys(%{$states})) {
                    650:             if (ref($states->{$key}) eq 'ARRAY') {
                    651:                 $nextstate_setter .= 
                    652: "             if (actionchoice == '$key') {
                    653:                   nextstate = '".$states->{$key}[1]."';
                    654:              }
                    655: ";
                    656:             }
                    657:         }
                    658:     }
1.1       raeburn   659: 
1.2       raeburn   660:     my $js = <<"END";
1.1       raeburn   661: 
1.2       raeburn   662: function nextPage(formname) {
1.27      raeburn   663:     var crschoice = document.mainmenu_coursetype.crstype.value;
                    664:     var actionchoice = document.mainmenu_action.action.value;
1.2       raeburn   665:     if (check_can_request(crschoice,actionchoice) == true) {
                    666:         if ((actionchoice == 'new') && (crschoice == 'official')) {
                    667:             nextstate = 'codepick';
                    668:         } else {
                    669: $nextstate_setter 
1.27      raeburn   670:         }
                    671:         formname.crstype.value = crschoice;
                    672:         formname.action.value = actionchoice; 
1.1       raeburn   673:         formname.state.value= nextstate;
                    674:         formname.submit();
                    675:     }
                    676:     return;
                    677: }
                    678: 
1.2       raeburn   679: function check_can_request(crschoice,actionchoice) {
1.1       raeburn   680:     var official = '';
                    681:     var unofficial = '';
                    682:     var community = '';    
                    683: END
                    684: 
                    685:     foreach my $item (keys(%{$can_request})) {
                    686:             $js .= " 
                    687:         $item = 1;
                    688: ";
                    689:     }
                    690:     my %lt = &Apache::lonlocal::texthash(
                    691:         official => 'You are not permitted to request creation of an official course in this domain.',
                    692:         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
                    693:         community => 'You are not permitted to request creation of a community this domain.',
                    694:         all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
                    695:     ); 
                    696:     $js .= <<END;
                    697:     if (crschoice == 'official') {
                    698:         if (official != 1) {
                    699:             alert("$lt{'official'}");
                    700:             return false;
                    701:         }
                    702:     } else {
                    703:         if (crschoice == 'unofficial') {
                    704:             if (unofficial != 1) {
                    705:                 alert("$lt{'unofficial'}");
                    706:                 return false;
                    707:             }
                    708:         } else {
                    709:             if (crschoice == 'community') {
                    710:                 if (community != 1) {
                    711:                     alert("$lt{'community'}");
                    712:                     return false;
                    713:                 }
                    714:             } else {
                    715:                 if (actionchoice == 'new') {
                    716:                     alert("$lt{'all'}");
                    717:                     return false;
                    718:                 }               
                    719:             }
                    720:         }
                    721:     }
                    722:     return true;
                    723: }
                    724: 
                    725: END
                    726: 
1.27      raeburn   727:     $r->print(&header('Course Requests',$js.$jscript,$loaditems).$crumb.'<div>'.
                    728:               &Apache::lonhtmlcommon::start_pick_box().
                    729:               &Apache::lonhtmlcommon::row_title(&mt('Course Domain')).
1.1       raeburn   730:               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
                    731:               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange));
                    732:     if (!$onchange) {
                    733:         $r->print('&nbsp;<input type="submit" name="godom" value="'.
                    734:                    &mt('Change').'" />');
                    735:     }
1.27      raeburn   736:     $r->print('</form>'.&Apache::lonhtmlcommon::row_closure());
1.1       raeburn   737: 
1.2       raeburn   738:     my $formname = 'requestcrs';
1.1       raeburn   739:     my $nexttext = &mt('Next');
1.27      raeburn   740:     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Action')).'
                    741: <form name="mainmenu_action" method="post" action=""> 
1.1       raeburn   742: <select size="1" name="action" >
1.2       raeburn   743:  <option value="new">'.&mt('New request').'</option>
1.1       raeburn   744:  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
                    745:  <option value="log">'.&mt('View request history').'</option>
1.27      raeburn   746: </select></form>'.
                    747:               &Apache::lonhtmlcommon::row_closure(1).
                    748:               &Apache::lonhtmlcommon::row_title(&mt('Course Type')).'
                    749: <form name="mainmenu_coursetype" method="post" action="">
1.1       raeburn   750: <select size="1" name="crstype">
1.4       raeburn   751:  <option value="any">'.&mt('All types').'</option>');
                    752:     if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
                    753:         foreach my $type (@{$types}) {
                    754:             my $selected = '';
                    755:             if ($type eq 'official') {
                    756:                 $selected = ' selected="selected"';
                    757:             }
                    758:             $r->print('<option value="'.$type.'"'.$selected.'>'.$typename->{$type}.
                    759:                       '</option>'."\n");
                    760:         }
                    761:     }
1.27      raeburn   762:     $r->print('</select></form>'."\n".
                    763:               &Apache::lonhtmlcommon::row_closure(1)."\n".
                    764:               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n".
                    765:               '<div><form name="'.$formname.'" method="post" action="">'."\n".
                    766:               '<input type="hidden" name="state" value="crstype" />'."\n".
                    767:               '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
                    768:               '<input type="hidden" name="crstype" value="" />'."\n".
                    769:               '<input type="hidden" name="action" value="" />'."\n".
                    770:               '<input type="button" name="next" value="'.$nexttext.
                    771:               '" onclick="javascript:nextPage(document.'.$formname.')" />'."\n".
                    772:               '</form></div>');
1.1       raeburn   773:     $r->print(&Apache::loncommon::end_page());
                    774:     return;
                    775: }
                    776: 
                    777: sub request_administration {
1.27      raeburn   778:     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb,
                    779:         $newinstcode,$codechk,$checkedcode,$invalidcrosslist,$uname,$udom) = @_;
1.2       raeburn   780:     my $js;
1.16      raeburn   781:     if (($action eq 'new') || (($action eq 'view') && ($state eq 'pick_request'))) {
1.2       raeburn   782:         $js =  <<END;
1.1       raeburn   783: 
                    784: function nextPage(formname,nextstate) {
                    785:     formname.state.value= nextstate;
                    786:     formname.submit();
                    787: }
1.16      raeburn   788: 
                    789: END
                    790:     }
                    791:     if (($action eq 'new') || ($action eq 'view')) {
                    792:         $js .= <<END;   
                    793: 
1.1       raeburn   794: function backPage(formname,prevstate) {
                    795:     formname.state.value = prevstate;
                    796:     formname.submit();
                    797: }
                    798: 
                    799: END
1.2       raeburn   800:     }
                    801:     if ($action eq 'new') {
                    802:         my $jsextra;
1.31      raeburn   803:         if ($state eq 'courseinfo') {
1.2       raeburn   804:             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
1.31      raeburn   805:         } elsif ($state eq 'enrollment') {
                    806:             if (($env{'form.crstype'} eq 'official') && 
                    807:                 (&Apache::lonnet::auto_run('',$dom))) {
                    808:                 $js .= "\n".&section_check_javascript()."\n".&enrollment_lcsec_js();
                    809:             }
                    810:         } elsif ($state eq 'personnel') {
                    811:             $js .= "\n".&section_check_javascript()."\n".&personnel_lcsec_js();
1.1       raeburn   812:         }
1.3       raeburn   813:         $r->print(&header('Request a course',$js.$jscript,$loaditems,$jsextra).$crumb);
1.27      raeburn   814:         &print_request_form($r,$action,$state,$page,$states,$dom,$newinstcode,
                    815:                             $codechk,$checkedcode,$invalidcrosslist);
1.2       raeburn   816:     } elsif ($action eq 'view') {
1.16      raeburn   817:         my $jsextra;
                    818:         my $formname = 'requestcrs';
                    819:         my $prev = $states->{$action}[$page-1];
                    820:         my $next = $states->{$action}[$page+1];
                    821:         if ($state eq 'pick_request') {
                    822:             $next = $states->{$action}[$page+1];
                    823:             $jsextra = &viewrequest_javascript($formname,$next);
                    824:         } elsif ($state eq 'details') {
                    825:             $jsextra = &viewdetails_javascript($formname);
                    826: 
                    827:         } elsif ($state eq 'cancel') {
                    828:             $jsextra = &viewcancel_javascript($formname);
                    829:         }
                    830:         $r->print(&header('Manage course requests',$js.$jscript.$jsextra,$loaditems).
                    831:                   $crumb);
                    832:         my $form = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />';
1.11      raeburn   833:         if ($state eq 'pick_request') {
1.16      raeburn   834:             $r->print('<h3>'.&mt('Pending course requests').'</h3><div>'."\n".$form."\n".
1.30      raeburn   835:                       &print_request_status($dom).'</form></div>');
1.16      raeburn   836:         } elsif ($state eq 'details') {
                    837:             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
                    838:             my $origcnum = $env{'form.cnum'};
                    839:             if ($origcnum eq '') {
                    840:                 $origcnum = $env{'form.origcnum'};   
                    841:             }
                    842:             if ($env{'form.crstype'} eq 'official') {
                    843:                 &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    844:                                                          \%cat_order,\@code_order);
                    845:             }
                    846:             $r->print('<h3>'.&mt('Course Request Details').'</h3><div>'."\n".$form."\n".
1.26      raeburn   847:                       &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
1.16      raeburn   848:                                     \@code_order)."\n".
                    849:                       '<input name="origcnum" value="'.$origcnum.'" type="hidden" />'."\n");
                    850:             my @excluded = &get_excluded_elements($dom,$states,'new','review');
                    851:             push(@excluded,'origcnum');
                    852:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
                    853:             my $other = 'modify';
                    854:             my %navtxt = &Apache::lonlocal::texthash (
                    855:                                                       prev => 'Back',
                    856:                                                       other => 'Modify Request',
                    857:                                                       next => 'Cancel Request',
                    858:                                                      );
1.31      raeburn   859:             &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
                    860:                                 $navtxt{'next'},$state,$other,$navtxt{'other'});
1.16      raeburn   861:             $r->print('</form>');
                    862:         } elsif ($state eq 'cancel') {
                    863:             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
                    864:             $r->print('<h3>'.&mt('Cancel course request').'</h3><div>'."\n".$form."\n".
                    865:                       $output);
                    866:             my @excluded = &get_excluded_elements($dom,$states,'view','cancel');
                    867:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
                    868:             my %navtxt = &Apache::lonlocal::texthash (
                    869:                                                       prev => 'Back',
                    870:                                                       next => 'Confirm Cancellation',
                    871:                                                      );
                    872:             if ($result eq 'ok') {
1.31      raeburn   873:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
1.16      raeburn   874:                                     $navtxt{'next'},$state);
                    875:             } else {
1.31      raeburn   876:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},undef,
                    877:                                     '',$state);
1.16      raeburn   878:             }
                    879:             $r->print('</form>');
                    880:         } elsif ($state eq 'removal') {
                    881:             my $cnum = $env{'form.origcnum'};
                    882:             my $statuskey = 'status:'.$dom.':'.$cnum;
                    883:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
                    884:                                                    $env{'user.domain'},$env{'user.name'});
                    885:             my $currstatus = $userreqhash{$statuskey};
                    886:             my ($result,$error); 
                    887:             if (($currstatus eq 'approval') || ($currstatus eq 'pending')) { 
                    888:                 my %status = (
                    889:                                  $statuskey => 'cancelled',
                    890:                              );
                    891:                 my $statusresult = &Apache::lonnet::put('courserequests',\%status);
                    892:                 if ($statusresult eq 'ok') {
                    893:                     my $delresult = 
                    894:                         &Apache::lonnet::del_dom('courserequestqueue',
                    895:                                                  [$cnum.'_'.$currstatus],$dom);
                    896:                     if ($delresult eq 'ok') {
                    897:                         $result = 'ok';
                    898:                     } else {
                    899:                         $error = &mt('An error occurred when updating the pending requests queue: [_1]',$delresult);
                    900:                     }
                    901:                 } else {
                    902:                     $error = &mt("An error occurred when updating the status of this request in the requestor's records: [_1]",$statusresult);
                    903:                 }
                    904:             } else {
                    905:                 $error = &mt('The current status of this request could not be verified as pending approval/institutional action.');  
                    906:             }
                    907:             $r->print('<h3>'.&mt('Request Cancellation').'</h3><div>'."\n".$form."\n".
                    908:                       '<input type="hidden" name="state" value="'.$state.'" />'."\n".
                    909:                       '<input type="hidden" name="action" value="'.$action.'" />'."\n".
                    910:                       '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
                    911:                       '<input type="hidden" name="orignum" value="'.$cnum.'" />'."\n");
                    912:             if ($result eq 'ok') {
                    913:                 $r->print(&mt('Your course request has been cancelled.'));
                    914:             } else {
                    915:                 $r->print('<div class="LC_error">'.
                    916:                           &mt('The request cancellation process was not complete.').
                    917:                           '<br />'.$error.'</div>');
                    918:             }
                    919:             $r->print('</form>');
1.11      raeburn   920:         }
1.26      raeburn   921:     } elsif ($action eq 'display') {
                    922:         my $formname = 'requestcrs';
                    923:         my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
                    924:         if ($env{'form.crstype'} eq 'official') {
                    925:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    926:                                                      \%cat_order,\@code_order);
                    927:         }
                    928:         $r->print(&header('Course Request','','','',{ 'only_body' => 1}).
                    929:                   $crumb."\n".'<h3>'.&mt('Course Request Details').'</h3>'.
                    930:                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
                    931:                                 \@code_order,$uname,$udom)."\n".'</div>'.
                    932:                   &close_popup_form());
1.1       raeburn   933:     } elsif ($action eq 'log') {
1.11      raeburn   934:         $r->print(&coursereq_log('View request log',$jscript,$loaditems).$crumb);
1.1       raeburn   935:     }
1.2       raeburn   936:     $r->print(&Apache::loncommon::end_page());
1.1       raeburn   937:     return;
                    938: }
                    939: 
1.31      raeburn   940: sub enrollment_lcsec_js {
                    941:     my %alerts = &section_check_alerts();
                    942:     my $secname = $alerts{'badsec'};
                    943:     my $secnone = $alerts{'reserved'};
                    944:     my $output = '
                    945: function validateEnrollSections(formname,nextstate) {
                    946:     var badsectotal = 0;
                    947:     var reservedtotal = 0;
                    948:     var secTest = "";
                    949: ';
                    950:     for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                    951:         $output .= "
                    952:     var selSec = 0;
                    953:     for (var j=0; j<document.requestcrs.sec_".$i.".length; j++) {
                    954:         if (document.requestcrs.sec_".$i."[j].checked) {
                    955:             selSec = document.requestcrs.sec_".$i."[j].value;
                    956:         }
                    957:         if (selSec == 1) {
                    958:             secTest = validsection(document.requestcrs.loncapasec_".$i.");
                    959:             if (secTest == 'badsec') {
                    960:                 badsectotal++;
                    961:             }
                    962:             if (secTest == 'reserved') {
                    963:                 reservedtotal++;
                    964:             }
                    965:         }
                    966:     }
                    967: ";
                    968:     }
                    969:     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    970:         $output .= "
                    971:     if (document.requestcrs.crosslist_".$i.".checked) {
                    972:         secTest = validsection(document.requestcrs.crosslist_".$i."_lcsec);
                    973:         if (secTest == 'badsec') {
                    974:             badsectotal++;
                    975:         }
                    976:         if (secTest == 'reserved') {
                    977:             reservedtotal++;
                    978:         }
                    979:     }
                    980: ";
                    981:     }
                    982:     $output .= "
                    983:     if (badsectotal>0) {
                    984:         alert('$secname');
                    985:         return false;
                    986:     }
                    987:     if (reservedtotal>0) {
                    988:         alert('$secnone');
                    989:         return false;
                    990:     }
                    991:     formname.state.value= nextstate;
                    992:     formname.submit();
                    993:     return;
                    994: }
                    995: ";
                    996:     return $output;
                    997: }
                    998: 
                    999: sub personnel_lcsec_js {
                   1000:     my %alerts = &section_check_alerts();
                   1001:     my $secname = $alerts{'badsec'}.'\\n'.$alerts{'separate'};
                   1002:     my $secnone = $alerts{'reserved'};
                   1003:     my $output = '
                   1004: function validatePersonnelSections(formname,nextstate) {
                   1005:     var badsectotal = 0;
                   1006:     var reservedtotal = 0;
                   1007:     var secTest = "";
                   1008: ';
                   1009:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1010:         $output .= "
                   1011:     if (document.requestcrs.person_".$i."_uname.value != '') {
                   1012:         secTest = validsection(document.requestcrs.person_".$i."_newsec,'1');
                   1013:         if (secTest == 'badsec') {
                   1014:             badsectotal++; 
                   1015:         }
                   1016:         if (secTest == 'reserved') {
                   1017:             reservedtotal++;
                   1018:         }
                   1019:     }
                   1020: ";
                   1021:     }
                   1022:     $output .= "
                   1023:     if (badsectotal > 0) {
                   1024:         alert('$secname');
                   1025:         return false;
                   1026:     } else {
                   1027:         if (reservedtotal > 0) {
                   1028:             alert('$secnone');
                   1029:             return false;
                   1030:         }
                   1031:     }
                   1032:     formname.state.value = nextstate;
                   1033:     formname.submit();
                   1034:     return;
                   1035: }
                   1036: ";
                   1037:     return $output;
                   1038: }
                   1039: 
                   1040: sub section_check_alerts {
                   1041:     my %lt = 
                   1042:         &Apache::lonlocal::texthash(
                   1043:             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.",
                   1044:             badsec => 'You need to change one or more LON-CAPA section names - names may only contain letters or numbers.',
                   1045:             separate => 'Separate multiple sections with a comma.'
                   1046:         );
                   1047:     return %lt;
                   1048: }
                   1049: 
                   1050: sub section_check_javascript {
                   1051:     return <<"END";
                   1052: function validsection(field,mult) {
                   1053:     var str = field.value;
                   1054:     var badsec=0;
                   1055:     var reserved=0;
                   1056:     if (window.RegExp) {
                   1057:         var badsecnum=0;
                   1058:         var reservednum=0;
                   1059:         var pattern=/[^a-zA-Z0-9]/; 
                   1060:         str = str.replace(/(^\\s*)|(\\s*\$)/gi,"");
                   1061:         str = str.replace(/[ ]{2,}/gi," ");
                   1062:         if (mult == '1') {
                   1063:             var sections = new Array();
                   1064:             sections = str.split(/\\s*[\\s,;:]\\s*/);
                   1065:             var i;
                   1066:             for (i=0; i<sections.length; i++) {
                   1067:                 if ((sections[i] != '') && (sections[i] != undefined) && (sections[i] != null)) {
                   1068:                     if (pattern.test(sections[i])) {
                   1069:                         badsecnum++;
                   1070:                     } else {
                   1071:                         if (sections[i] == 'none') {
                   1072:                             reservednum++;
                   1073:                         }
                   1074:                     }
                   1075:                 }
                   1076:             }
                   1077:         } else {
                   1078:             if ((str != '') && (str != undefined) && (str != null)) {
                   1079:                 if (pattern.test(str)) {
                   1080:                     badsecnum++;
                   1081:                 } else {
                   1082:                     if (str == 'none') {
                   1083:                         reservednum++;
                   1084:                     }
                   1085:                 }
                   1086:             }
                   1087:         }
                   1088:         if (badsecnum > 0) {
                   1089:             return 'badsec';
                   1090:         }
                   1091:         if (reservednum > 0) {
                   1092:             return 'reserved';
                   1093:         }
                   1094:     }
                   1095:     return;
                   1096: }
                   1097: END
                   1098: }
                   1099: 
1.26      raeburn  1100: sub close_popup_form {
                   1101:     my $close= &mt('Close Window');
                   1102:     return << "END";
                   1103: <p><form name="displayreq" action="" method="post">
                   1104: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   1105: </form></p>
                   1106: END
                   1107: }
                   1108: 
1.27      raeburn  1109: sub get_instcode {
                   1110:     my ($dom) = @_;
                   1111:     my ($instcode,$numtitles);
                   1112:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
                   1113:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                   1114:                                              \%cat_order,\@code_order);
                   1115:     $numtitles = scalar(@codetitles);
                   1116:     if (@code_order > 0) {
                   1117:         my $message;
                   1118:         foreach my $item (@code_order) {
                   1119:             $instcode .= $env{'form.instcode_'.$item};
                   1120:         }
                   1121:     }
                   1122:     return ($instcode,$numtitles);
                   1123: }
                   1124: 
1.1       raeburn  1125: sub print_request_form {
1.27      raeburn  1126:     my ($r,$action,$state,$page,$states,$dom,$newinstcode,$codechk,$checkedcode,
                   1127:         $invalidcrosslist) = @_;
1.1       raeburn  1128:     my $formname = 'requestcrs';
1.2       raeburn  1129:     my ($next,$prev,$message,$output,$codepicker,$crstype);
                   1130:     $prev = $states->{$action}[$page-1];
                   1131:     $next = $states->{$action}[$page+1];
1.4       raeburn  1132:     my %navtxt = &Apache::lonlocal::texthash (
1.10      raeburn  1133:                                                prev => 'Back',
1.4       raeburn  1134:                                                next => 'Next',
                   1135:                                              );
1.2       raeburn  1136:     $crstype = $env{'form.crstype'};
1.1       raeburn  1137:     $r->print('<form name="'.$formname.'" method="post" action="/adm/requestcourse">');
1.30      raeburn  1138:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk,
                   1139:         @disallowed);
1.1       raeburn  1140:     if ($crstype eq 'official') {
1.2       raeburn  1141:         if ($env{'form.instcode'} ne '') {
                   1142:             $instcode = $env{'form.instcode'};
1.27      raeburn  1143:         } elsif ($newinstcode ne '') {
                   1144:             $instcode = $newinstcode;
1.2       raeburn  1145:         }
1.27      raeburn  1146:         if ($checkedcode) {
                   1147:             if ($codechk eq 'valid') {
                   1148:                 $message = '<div class="LC_info">'.
                   1149:                            &mt('The chosen course category [_1] is valid.','<b>'.
                   1150:                            $instcode.'</b>').
                   1151:                            '<input type="hidden" name="instcode" value="'.
                   1152:                            $instcode.'" /></div>';
1.2       raeburn  1153:             } else {
                   1154:                 $message = '<div class="LC_warning">'.
1.27      raeburn  1155:                             &mt('No course was found matching your choice of institutional course category.');
                   1156:                 if ($codechk ne '') {
                   1157:                     $message .= '<br />'.$codechk;
                   1158:                 }
                   1159:                 $message .= '</div>';
1.2       raeburn  1160:                 $prev = 'crstype';
                   1161:             }
                   1162:             $r->print($message);
1.1       raeburn  1163:         }
1.2       raeburn  1164:     }
                   1165:     if ($prev eq 'crstype') {
1.4       raeburn  1166:         if ($crstype eq 'official') {
                   1167:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                   1168:                                                      \%cat_order,\@code_order);
                   1169:         }
1.2       raeburn  1170:         if (@code_order > 0) {
1.1       raeburn  1171:             $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
                   1172:                                            \%cat_titles,\%cat_order);
1.2       raeburn  1173:             if ($codepicker) {
                   1174:                 $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box().$codepicker.
                   1175:                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
                   1176:             } else {
1.20      raeburn  1177:                 $next = $states->{$action}[$page+2];
1.15      raeburn  1178:                 $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.2       raeburn  1179:             }
                   1180:         } else {
1.20      raeburn  1181:             if ($crstype eq 'official') {
                   1182:                 $next = $states->{$action}[$page+2];
                   1183:             }
1.15      raeburn  1184:             $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.1       raeburn  1185:         }
1.2       raeburn  1186:     } elsif ($prev eq 'codepick') {
1.27      raeburn  1187:         if ($instcode eq '') {
1.20      raeburn  1188:             $prev = $states->{$action}[$page-2];
                   1189:         }
1.15      raeburn  1190:         $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.2       raeburn  1191:     } elsif ($state eq 'enrollment') {
1.4       raeburn  1192:         if ($crstype eq 'official') {
                   1193:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                   1194:                                                      \%cat_order,\@code_order);
                   1195:         }
1.2       raeburn  1196:         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
1.27      raeburn  1197:                                          \%cat_titles,\%cat_order,\@code_order,
                   1198:                                          $invalidcrosslist));
1.2       raeburn  1199:     } elsif ($state eq 'personnel') {
1.27      raeburn  1200:         $r->print(&print_personnel_menu($dom,$formname,$crstype,$invalidcrosslist));
1.4       raeburn  1201:     } elsif ($state eq 'review') {
1.30      raeburn  1202:         my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg);
                   1203:         my $now = time;
                   1204:         for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1205:             my $personname = $env{'form.person_'.$i.'_uname'};
                   1206:             my $persondom = $env{'form.person_'.$i.'_dom'};
                   1207:             if (($personname =~ /^$match_username$/) && 
                   1208:                 ($persondom =~ /^$match_domain$/)) {
                   1209:                 if (&Apache::lonnet::domain($persondom)) {
                   1210:                     my $personhome = 
                   1211:                         &Apache::lonnet::homeserver($personname,$persondom);
                   1212:                     if ($personhome eq 'no_host') {
                   1213:                         if ($persondom ne $dom) {
                   1214:                             my $skipuser = 1;
                   1215:                             if ($env{'user.role.dc./'.$persondom.'/'}) {
                   1216:                                 my ($start,$end) = split('.',$env{'user.role.dc./'.$persondom.'/'});
                   1217:                                 if (((!$start) || ($start < $now)) && 
                   1218:                                     ((!$end) || ($end > $now))) {
                   1219:                                     $skipuser = 0;
                   1220:                                 }
                   1221:                             }
                   1222:                             if ($skipuser) {
                   1223:                                 push(@disallowed,$i);
                   1224:                                 $disallowmsg{$i} = &mt('[_1] was excluded because new users need be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
                   1225:                                 next;
                   1226:                             }
                   1227:                         }
                   1228:                         if (&get_cancreate_status($persondom,$personname,$dom)) {
                   1229:                             my ($allowed,$msg) = 
                   1230:                                 &check_newuser_rules($persondom,$personname,
                   1231:                                     \%alerts,\%rulematch,\%inst_results,
                   1232:                                     \%curr_rules,\%got_rules);
                   1233:                             if ($allowed) {
                   1234:                                 if (ref($inst_results{$personname.':'.$persondom}) eq 'HASH') {
                   1235:                                     if ($inst_results{$personname.':'.$persondom}{'lastname'} ne '') {
                   1236:                                         $env{'form.person_'.$i.'_lastname'} = $inst_results{$personname.':'.$persondom}{'lastname'};
                   1237:                                     }
                   1238:                                     if ($inst_results{$personname.':'.$persondom}{'firstname'} ne '') {
                   1239:                                         $env{'form.person_'.$i.'_firstname'} = $inst_results{$personname.':'.$persondom}{'firstname'};
                   1240:                                     }
                   1241:                                     if ($inst_results{$personname.':'.$persondom}{'permanentemail'} ne '') {
                   1242:                                         $env{'form.person_'.$i.'_emailaddr'} = $inst_results{$personname.':'.$persondom}{'permanentemail'};
                   1243:                                     }
                   1244:                                 }
                   1245:                             } else {
                   1246:                                 push(@disallowed,$i);
                   1247:                                 $disallowmsg{$i} = &mt('[_1] was excluded because the username violated format rules for the domain','<tt>'.$personname.':'.$persondom.'</tt>');  
                   1248:                             }
                   1249:                         } else {
                   1250:                             push(@disallowed,$i);
                   1251:                             $disallowmsg{$i} = &mt('[_1] was excluded because you may not request new users in the domain','<tt>'.$personname.':'.$persondom.'</tt>');
                   1252:                         }
                   1253:                     } else {
                   1254:                         my %userenv = 
                   1255:                             &Apache::lonnet::userenvironment($persondom,$personname,'lastname','firstname','permanentemail');
                   1256:                         if ($env{'form.person_'.$i.'_lastname'} eq '') {
                   1257:                             $env{'form.person_'.$i.'_lastname'} = $userenv{'lastname'};
                   1258:                         }
                   1259:                         if ($env{'form.person_'.$i.'_firstname'} eq '') {
                   1260:                             $env{'form.person_'.$i.'_firstname'} = $userenv{'firstname'};
                   1261:                         }
                   1262:                         if ($env{'form.person_'.$i.'_emailaddr'} eq '') {
                   1263:                             $env{'form.person_'.$i.'_emailaddr'} = $userenv{'permanentemail'};
                   1264:                         }
                   1265:                     }
                   1266:                 } elsif ($personname ne '') {
                   1267:                     push(@disallowed,$i);
                   1268:                     $disallowmsg{$i} = &mt('[_1] was excluded because the domain is invalid','<tt>'.$personname.':'.$persondom.'</tt>');
                   1269:                 }
                   1270:             } elsif ($personname ne '') {
                   1271:                 push(@disallowed,$i);
                   1272:                 $disallowmsg{$i} = &mt('[_1] was excluded because the username or domain is invalid.','<tt>'.$personname.':'.$persondom.'</tt>');
                   1273:             }
                   1274:         }
1.16      raeburn  1275:         my $cnum;
                   1276:         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
                   1277:             $cnum = $env{'form.origcnum'};
                   1278:         } else {
                   1279:             $cnum = &Apache::lonnet::generate_coursenum($dom);
                   1280:         }
1.4       raeburn  1281:         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                   1282:                                                  \%cat_order,\@code_order);
1.27      raeburn  1283:         $r->print('<h3>'.&mt('Review course request details before submission').'</h3>'.
1.30      raeburn  1284:                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg).
1.16      raeburn  1285:                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
1.4       raeburn  1286:         $navtxt{'next'} = &mt('Submit course request');
1.10      raeburn  1287:     }  elsif ($state eq 'process') {
                   1288:         if ($crstype eq 'official') {
                   1289:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                   1290:                                                      \%cat_order,\@code_order);
                   1291:         }
1.27      raeburn  1292:         my ($storeresult,$result) = &print_request_outcome($dom,\@codetitles,
                   1293:                                                            \@code_order);
1.13      raeburn  1294:         $r->print($result);
1.27      raeburn  1295:         if (($storeresult eq 'ok') || ($storeresult eq 'created')) {
                   1296:             $r->print('<p>');
                   1297:             if ($storeresult eq 'ok') {
                   1298:                 $r->print('<a href="/adm/requestcourse?action=view&state=details&showdom='.$dom.'&cnum='. $env{'form.cnum'}.'">'.
                   1299:                           &mt('Modify this request').'</a>'.('&nbsp;'x4));
                   1300:             }
                   1301:             $r->print('<a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
                   1302:             return;
                   1303:         }
1.1       raeburn  1304:     }
1.16      raeburn  1305:     my @excluded = &get_excluded_elements($dom,$states,$action,$state);
1.24      raeburn  1306:     if ($state eq 'personnel') {
                   1307:         push(@excluded,'persontotal');
                   1308:     }
1.30      raeburn  1309:     if ($state eq 'review') {
                   1310:         if (@disallowed > 0) {
                   1311:             my @items = qw(uname dom lastname firstname emailaddr hidedom role newsec);
                   1312:             my @currsecs = &current_lc_sections();
                   1313:             if (@currsecs) {
                   1314:                 push(@items,'sec');
                   1315:             }
                   1316:             my $count = 0;
                   1317:             for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1318:                 unless ($env{'form.person_'.$i.'_uname'} eq '') {
                   1319:                     if (grep(/^$i$/,@disallowed)) {
                   1320:                         foreach my $item (@items) {
                   1321:                             $env{'form.person_'.$i.'_'.$item} = '';
                   1322:                         }
                   1323:                     } else { 
                   1324:                         foreach my $item (@items) {
                   1325:                             $env{'form.person_'.$count.'_'.$item} = $env{'form.person_'.$i.'_'.$item};
                   1326:                         }
                   1327:                     }
                   1328:                 }
                   1329:                 $count ++;
                   1330:             }
                   1331:             $env{'form.persontotal'} = $count;
                   1332:              
                   1333:         }
                   1334:     }
1.27      raeburn  1335:     if ($state eq 'enrollment') {
                   1336:         push(@excluded,'crosslisttotal');
                   1337:     }
1.16      raeburn  1338:     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
1.31      raeburn  1339:     &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
                   1340:                         $navtxt{'next'},$state);
1.16      raeburn  1341:     return;
                   1342: }
                   1343: 
1.30      raeburn  1344: sub get_cancreate_status {
                   1345:     my ($persondom,$personname,$dom) = @_;
                   1346:     my ($rules,$ruleorder) =
                   1347:         &Apache::lonnet::inst_userrules($persondom,'username');
                   1348:     my $usertype = &Apache::lonuserutils::check_usertype($persondom,$personname,
                   1349:                                                          $rules);
                   1350:     return &Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype);
                   1351: }
                   1352: 
                   1353: sub check_newuser_rules {
                   1354:     my ($persondom,$personname,$alerts,$rulematch,$inst_results,$curr_rules,
                   1355:         $got_rules) = @_;
                   1356:     my $allowed = 1;
                   1357:     my $newuser = 1;
                   1358:     my ($checkhash,$userchkmsg);
                   1359:     my $checks = { 'username' => 1 };
                   1360:     $checkhash->{$personname.':'.$persondom} = { 'newuser' => $newuser };
                   1361:     &Apache::loncommon::user_rule_check($checkhash,$checks,$alerts,$rulematch,
                   1362:                                         $inst_results,$curr_rules,$got_rules);
                   1363:     if (ref($alerts->{'username'}) eq 'HASH') {
                   1364:         if (ref($alerts->{'username'}{$persondom}) eq 'HASH') {
                   1365:             my $domdesc =
                   1366:                 &Apache::lonnet::domain($persondom,'description');
                   1367:             if ($alerts->{'username'}{$persondom}{$personname}) {
                   1368:                 if (ref($curr_rules->{$persondom}) eq 'HASH') {
                   1369:                     $userchkmsg =
                   1370:                         &Apache::loncommon::instrule_disallow_msg('username',
                   1371:                                                                   $domdesc,1).
                   1372:                         &Apache::loncommon::user_rule_formats($persondom,
                   1373:                             $domdesc,$curr_rules->{$persondom}{'username'},
                   1374:                             'username');
                   1375:                 }
                   1376:                 $allowed = 0;
                   1377:             }
                   1378:         }
                   1379:     }
                   1380:     return ($allowed,$userchkmsg);
                   1381: }
                   1382: 
1.16      raeburn  1383: sub get_excluded_elements {
                   1384:     my ($dom,$states,$action,$state) = @_;
1.2       raeburn  1385:     my @excluded = ('counter');
                   1386:     my %elements = &form_elements($dom);
                   1387:     if (ref($states) eq 'HASH') {
                   1388:         if (ref($states->{$action}) eq 'ARRAY') {
                   1389:             my @items = @{$states->{$action}};
                   1390:             my $numitems = scalar(@items);
                   1391:             if ($numitems) {
                   1392:                 for (my $i=$numitems-1; $i>=0; $i--) {
                   1393:                     if (ref($elements{$action}) eq 'HASH') {
                   1394:                         if (ref($elements{$action}{$items[$i]}) eq 'HASH') {
1.16      raeburn  1395:                             foreach my $key (keys(%{$elements{$action}{$items[$i]}})) {
1.2       raeburn  1396:                                 push(@excluded,$key);
                   1397:                             }
                   1398:                         }
                   1399:                     }
                   1400:                     last if ($items[$i] eq $state);
                   1401:                 }
                   1402:             }
                   1403:         }
                   1404:     }
                   1405:     if (grep(/^instcode_/,@excluded)) {
                   1406:         push(@excluded,'instcode');
1.1       raeburn  1407:     }
1.16      raeburn  1408:     return @excluded;
1.1       raeburn  1409: }
                   1410: 
1.2       raeburn  1411: sub print_enrollment_menu {
1.27      raeburn  1412:     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order,
                   1413:         $invalidcrosslist) =@_;
                   1414:     my ($sections,$autoenroll,$access_dates,$output);
1.2       raeburn  1415:     my $starttime = time;
                   1416:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                   1417: 
                   1418:     my %accesstitles = (
                   1419:                           'start' => 'Default start access',
1.16      raeburn  1420:                            'end'   => 'Default end access',
1.2       raeburn  1421:                        );
                   1422:     my %enrolltitles = (
                   1423:                            'start' => 'Start auto-enrollment',
                   1424:                            'end'   => 'End auto-enrollment',
                   1425:                        );
                   1426:     if ($env{'form.crstype'} eq 'official') {
                   1427:         if (&Apache::lonnet::auto_run('',$dom)) {
1.27      raeburn  1428:             $output = &show_invalid_crosslists($invalidcrosslist);
                   1429:             my ($section_form,$crosslist_form);
1.2       raeburn  1430:             $section_form = &inst_section_selector($dom,$instcode);
1.27      raeburn  1431:             if ($section_form) {
                   1432:                 $sections = &Apache::lonhtmlcommon::row_headline().
                   1433:                             '<h3>'.&mt('Sections for auto-enrollment').'</h3>'.
                   1434:                             &Apache::lonhtmlcommon::row_closure(1).
                   1435:                             $section_form;
                   1436:             }
1.2       raeburn  1437:             my $crosslisttotal = $env{'form.crosslisttotal'};
1.24      raeburn  1438:             if (!$crosslisttotal) {
1.2       raeburn  1439:                 $crosslisttotal = 1;
                   1440:             }
                   1441:             if ($env{'form.addcrosslist'}) {
                   1442:                 $crosslisttotal ++;
                   1443:             }
                   1444:             for (my $i=0; $i<$crosslisttotal; $i++) {
                   1445:                 $crosslist_form .= &coursecode_form($dom,'crosslist',$codetitles, 
                   1446:                                                   $cat_titles,$cat_order,$i);
                   1447:             }
                   1448:             if ($crosslist_form) { 
                   1449:                 $crosslist_form .= 
                   1450:                     &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                   1451:                     '<input name="crosslisttotal" type="hidden" value="'.$crosslisttotal.'" />'.
                   1452:                     '<input name="addcrosslist" type="checkbox" value="'.$crosslisttotal.'"'.
                   1453:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
1.27      raeburn  1454:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
                   1455:                 $sections .=  &Apache::lonhtmlcommon::row_headline.
                   1456:                               '<h3>'.&mt('Crosslisted courses for auto-enrollment').'</h3>'.
                   1457:                               &Apache::lonhtmlcommon::row_closure(1).
                   1458:                               $crosslist_form;
1.2       raeburn  1459:             }
1.27      raeburn  1460:             $autoenroll = 
1.2       raeburn  1461:                 &Apache::lonhtmlcommon::row_title(&mt('Add registered students automatically')).
                   1462:                 '<span class="LC_nobreak"><label>'.
                   1463:                 '<input type="radio" name="autoadds" value="1">'.
                   1464:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                   1465:                 '<input type="radio" name="autoadds" value="0" checked="checked">'.
                   1466:                 &mt('No').'</label></span>'.
1.27      raeburn  1467:                 &Apache::lonhtmlcommon::row_closure(1).
1.2       raeburn  1468:                 &Apache::lonhtmlcommon::row_title(&mt('Drop unregistered students automatically')).
                   1469:                 '<span class="LC_nobreak"><label>'.
                   1470:                 '<input type="radio" name="autodrops" value="1">'.
                   1471:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                   1472:                 '<input type="radio" name="autodrops" value="0" checked="checked">'.
                   1473:                 &mt('No').'</label></span>'. 
1.27      raeburn  1474:                 &Apache::lonhtmlcommon::row_closure(1).
1.2       raeburn  1475:                 &date_setting_table($starttime,$endtime,$formname,'enroll',%enrolltitles);
                   1476:         }
                   1477:     }
1.27      raeburn  1478:     my $access_dates = 
1.2       raeburn  1479:         &date_setting_table($starttime,$endtime,$formname,'access',%accesstitles);
1.27      raeburn  1480:     $output .= &Apache::lonhtmlcommon::start_pick_box();
                   1481:     if ($sections) {
                   1482:         $output .=  $sections;
                   1483:     }
                   1484:     if ($autoenroll) {
                   1485:         $output .= &Apache::lonhtmlcommon::row_headline('Auto-enroll').
                   1486:                    '<h3>'.&mt('Auto-enrollment settings').'</h3>'.
                   1487:                    &Apache::lonhtmlcommon::row_closure(1).
                   1488:                    $autoenroll;
                   1489:     }
                   1490:     if ($access_dates) {
                   1491:         my $header = &mt('Access dates for students');
                   1492:         if ($env{'form.crstype'} eq 'community') {
                   1493:             $header = &mt('Access dates for community members');
                   1494:         }
                   1495:         $output .= &Apache::lonhtmlcommon::row_headline('Access').
                   1496:                    '<h3>'.$header.'</h3>'.
                   1497:                    &Apache::lonhtmlcommon::row_closure(1).
                   1498:                    $access_dates
1.2       raeburn  1499:     }
1.27      raeburn  1500:     return '<div>'.&Apache::lonhtmlcommon::start_pick_box().$output.
                   1501:            &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.2       raeburn  1502: }
                   1503: 
1.27      raeburn  1504: sub show_invalid_crosslists {
                   1505:     my ($invalidcrosslist) = @_;
                   1506:     my $output;
                   1507:     if (ref($invalidcrosslist) eq 'ARRAY') {
                   1508:         if (@{$invalidcrosslist} > 0) {
                   1509:             $output = '<div class="LC_warning">'.
                   1510:                       &mt('The following crosslisted courses were invalid:').'<ul>';
                   1511:             foreach my $item (@{$invalidcrosslist}) {
                   1512:                 $output .= '<li>'.$item.'</li>';
                   1513:             }
                   1514:             $output .= '</ul></div><br />';
                   1515:         }
                   1516:     }
                   1517:     return $output;
                   1518: }
                   1519: 
                   1520: 
1.1       raeburn  1521: sub inst_section_selector {
1.2       raeburn  1522:     my ($dom,$instcode) = @_;
                   1523:     my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
                   1524:     my $sectotal = scalar(@sections);
1.1       raeburn  1525:     my $output;
1.2       raeburn  1526:     if ($sectotal) {
1.27      raeburn  1527:         $output .=  &Apache::lonhtmlcommon::row_title(&mt('Sections of [_1]',$instcode)).
1.2       raeburn  1528:                     &Apache::loncommon::start_data_table().
                   1529:                     &Apache::loncommon::start_data_table_row().
                   1530:                     '<th>'.&mt('Include?').'<input type="hidden" name="sectotal" '.
1.24      raeburn  1531:                     'value="'.$sectotal.'"  /></th>'.
1.2       raeburn  1532:                     '<th>'.&mt('Institutional Section').'</th>'.
                   1533:                     '<th>'.&mt('LON-CAPA section').'</th>'.
                   1534:                     &Apache::loncommon::end_data_table_row();
                   1535:         for (my $i=0; $i<@sections; $i++) {
1.1       raeburn  1536:             my $colflag = $i%2;
1.31      raeburn  1537:             my $secon = ' checked="checked"';
                   1538:             my $secoff = '';
1.24      raeburn  1539:             if ($env{'form.origcnum'}) {
1.31      raeburn  1540:                 $secoff = $secon;
                   1541:                 $secon='';
1.24      raeburn  1542:             }
1.1       raeburn  1543:             $output .= &Apache::loncommon::start_data_table_row().
1.31      raeburn  1544:                        '<td><label><input type="radio" name="sec_'.$i.
                   1545:                        '"'.$secon.' value="1" />'.&mt('Yes').'</label>'.
                   1546:                        ('&nbsp;'x2).'<label><input type="radio" name="sec_'.$i.
                   1547:                         '"'.$secoff.' value="0" />'.&mt('No').'</label></td>'.
                   1548:                        '<td align="center">'.$sections[$i].
1.1       raeburn  1549:                        '<input type="hidden" name="secnum_'.$i.'" value="'.
1.2       raeburn  1550:                        $sections[$i].'" /></td>'.
1.1       raeburn  1551:                        '<td><input type="text" size="10" name="loncapasec_'.$i.
1.2       raeburn  1552:                        '" value="'.$sections[$i].'" /></td>'.
1.1       raeburn  1553:                        &Apache::loncommon::end_data_table_row();
                   1554:         }
1.2       raeburn  1555:         $output .= &Apache::loncommon::end_data_table().
                   1556:                    &Apache::lonhtmlcommon::row_closure();
1.1       raeburn  1557:     }
                   1558:     return $output;
                   1559: }
                   1560: 
1.2       raeburn  1561: sub date_setting_table {
1.14      raeburn  1562:     my ($starttime,$endtime,$formname,$prefix,%datetitles) = @_;
1.2       raeburn  1563:     my ($perpetual,$table);
1.14      raeburn  1564:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'start',
1.2       raeburn  1565:                                                         $starttime,'','','',1,'','','',1);
1.14      raeburn  1566:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'end',
1.2       raeburn  1567:                                                       $endtime,'','','',1,'','','',1);
1.27      raeburn  1568:     my $closure = '';
1.14      raeburn  1569:     if ($prefix eq 'access') {
1.2       raeburn  1570:         $perpetual = ' <span class="LC_nobreak"><label>'.
                   1571:                      '<input type="checkbox" name="no_end_date" />'.
                   1572:                      &mt('No end date').'</label></span>';
1.27      raeburn  1573:         $closure = '1';
1.2       raeburn  1574:     }
                   1575:     $table = &Apache::lonhtmlcommon::row_title($datetitles{'start'}).
                   1576:              $startform.
1.27      raeburn  1577:              &Apache::lonhtmlcommon::row_closure(1).
1.2       raeburn  1578:              &Apache::lonhtmlcommon::row_title($datetitles{'end'}).
                   1579:              $endform.$perpetual.
1.27      raeburn  1580:              &Apache::lonhtmlcommon::row_closure($closure);
1.2       raeburn  1581:     return $table;
                   1582: }
                   1583: 
                   1584: sub print_personnel_menu {
1.27      raeburn  1585:     my ($dom,$formname,$crstype,$invalidcrosslist) = @_;
                   1586:     my $output;
                   1587:     if ($crstype eq 'official') {
                   1588:         if (&Apache::lonnet::auto_run('',$dom)) {
                   1589:             $output .= &show_invalid_crosslists($invalidcrosslist);
                   1590:         }  
                   1591:     }
                   1592:     $output .= '<div>'.&Apache::lonhtmlcommon::start_pick_box();
1.2       raeburn  1593:     my $persontotal = $env{'form.persontotal'};
1.16      raeburn  1594:     if ((!defined($persontotal)) || (!$persontotal)) {
1.2       raeburn  1595:         $persontotal = 1;
                   1596:     }
                   1597:     if ($env{'form.addperson'}) {
                   1598:         $persontotal ++;
                   1599:     }
1.13      raeburn  1600:     my @items = ('uname','dom','lastname','firstname','emailaddr','hidedom');
1.2       raeburn  1601: 
                   1602:     my $roleoptions;
                   1603:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.8       raeburn  1604:     my $type = 'Course';
                   1605:     if ($crstype eq 'community') {
                   1606:         $type = 'Community';
                   1607:     }
1.2       raeburn  1608:     foreach my $role (@roles) {
1.28      raeburn  1609:         my $plrole = &Apache::lonnet::plaintext($role,$type);
1.2       raeburn  1610:         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1611:     }
                   1612:     my %customroles=&Apache::lonuserutils::my_custom_roles();
                   1613:     if (keys(%customroles) > 0) {
                   1614:         foreach my $cust (sort(keys(%customroles))) {
1.28      raeburn  1615:             my $custrole="cr/$env{'user.domain'}/$env{'user.name'}/$cust";
1.2       raeburn  1616:             $roleoptions .= '  <option value="'.$custrole.'">'.$cust.'</option>'."\n";
                   1617:         }
                   1618:     }
                   1619: 
1.30      raeburn  1620:     my @currsecs = &current_lc_sections();
1.2       raeburn  1621: 
                   1622:     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
                   1623:     if (@currsecs) {
                   1624:         my $existsize = scalar(@currsecs);
                   1625:         if ($existsize > 3) {
                   1626:             $existsize = 3;
                   1627:         }
                   1628:         if ($existsize > 1) {
                   1629:             $existmult = ' multiple="multiple" size="'.$existsize.'" ';
                   1630:         }
                   1631:         @currsecs = sort { $a <=> $b } (@currsecs);
                   1632:         $existtitle = &mt('Official').':&nbsp;';
                   1633:         $existops = '<option value="">'.&mt('None').'</option>';
                   1634:         foreach my $sec (@currsecs) {
                   1635:             $existops .= '<option value="'.$sec.'">'.$sec.'</option>'."\n";
                   1636:         }
                   1637:         $seccolspan = ' colspan="2"';
                   1638:         $newtitle = &mt('Other').':&nbsp;';
                   1639:     }
                   1640: 
1.27      raeburn  1641:     if ($persontotal) {
1.32    ! raeburn  1642:         my %lt = &Apache::lonlocal::texthash(
        !          1643:             community => 'Requestor is automatically assigned Coordinator role.',
        !          1644:             official => 'Requestor is automatically assigned Course Coordinator role.',
        !          1645:         );
        !          1646:         $lt{'unofficial'} = $lt{'official'};
1.27      raeburn  1647:         $output .= &Apache::lonhtmlcommon::row_headline().
1.32    ! raeburn  1648:                   '<h3>'.$lt{$crstype}.' '.&mt('Include other personnel?').'</h3>';
1.27      raeburn  1649:     }
1.2       raeburn  1650:     for (my $i=0; $i<$persontotal; $i++) { 
                   1651:         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
                   1652:         my $linkargstr = join("','",@linkargs);
1.29      raeburn  1653:         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value="" size="20" />';
1.2       raeburn  1654:         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
1.29      raeburn  1655:                        "'person_".$i."_hidedom','person_".$i."_uname'".');';
1.2       raeburn  1656:         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
                   1657:                                                             1,$onchange).
1.29      raeburn  1658:                         '<input type="hidden" name="person_'.$i.'_hidedom" value="" />';
1.2       raeburn  1659:         my %form_elems;
                   1660:         foreach my $item (@items) {
                   1661:             next if (($item eq 'dom') || ($item eq 'uname') || ($item eq 'hidedom'));
                   1662:             $form_elems{$item} = '<input type="text" name="person_'.$i.'_'.$item.'" '.
                   1663:                                  'value="" readonly="readonly" />';
                   1664:         }
                   1665:         my $roleselector = '<select name="person_'.$i.'_role">'."\n".
                   1666:                            $roleoptions.'</select>';
                   1667:         my $sectionselector;
                   1668:         if (@currsecs) {
                   1669:             $sectionselector = $existtitle.'<select name="person_'.$i.'_sec"'.
                   1670:                                $existmult.'>'."\n".$existops.'</select>'.('&nbsp;' x3);
                   1671:         }
                   1672:         $sectionselector .= $newtitle.
1.14      raeburn  1673:             '<input type="text" name="person_'.$i.'_newsec" size="15" value="" />'."\n";
1.29      raeburn  1674:         my $usersrchlinktxt = &mt('Search for user');
                   1675:         my $usersrchlink =  &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
                   1676:                             $usersrchlinktxt);
                   1677:         my $userchklinktxt = &mt('Check username');
                   1678:         my $userchklink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
                   1679:                             $userchklinktxt,'checkusername');
1.2       raeburn  1680:         $output .= 
1.29      raeburn  1681:             &Apache::lonhtmlcommon::row_title(&mt('Additional Personnel')).
                   1682:             '<table><tr><td align="center" valign="middle"><b>'.$usersrchlink.'</b></td>'."\n".
                   1683:             '<td align="left" valign="top" colspan="2"><span class="LC_nobreak">'.
                   1684:             &mt('Username').':&nbsp;'.$uname_form.'&nbsp;'.$userchklink.'</span><br />'."\n".
                   1685:             '<span class="LC_nobreak">'.&mt('Domain').':&nbsp;'.$udom_form.'</span></td>'.
                   1686:             '</tr>'."\n".'<tr>'.
1.13      raeburn  1687:             '<td align="center" valign="top">'.&mt('First Name').'<br />'.$form_elems{'firstname'}.'</td>'."\n".
                   1688:             '<td align="center" valign="top">'.&mt('Last Name').'<br />'.$form_elems{'lastname'}.'</td>'."\n".
                   1689:             '<td align="center" valign="top">'.&mt('E-mail').'<br />'.$form_elems{'emailaddr'}.'</td></tr>'."\n".
1.2       raeburn  1690:             '<tr><td align="center" valign="top">'.&mt('Role').'<br />'.$roleselector.'</td>'."\n".
                   1691:             '<td'.$seccolspan.' align="center" valign="top">'.&mt('Section(s)').'<br />'.$sectionselector.'</td>'."\n".
                   1692:             '</tr></table>'.&Apache::lonhtmlcommon::row_closure();
                   1693:     }
                   1694:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                   1695:                '<input name="persontotal" type="hidden" value="'.$persontotal.'" />'.
                   1696:                '<input name="addperson" type="checkbox" value="'.$persontotal.'"'.
                   1697:                ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                   1698:                "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1).
                   1699:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
                   1700:     return $output;
                   1701: }
                   1702: 
1.30      raeburn  1703: sub current_lc_sections {
                   1704:     my @currsecs;
                   1705:     if ($env{'form.sectotal'}) {
                   1706:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
1.31      raeburn  1707:             if ($env{'form.sec_'.$i}) { 
                   1708:                 if (defined($env{'form.loncapasec_'.$i})) {
                   1709:                     my $lcsec = $env{'form.loncapasec_'.$i};
                   1710:                     unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
                   1711:                         push(@currsecs,$lcsec);
                   1712:                     }
1.30      raeburn  1713:                 }
                   1714:             }
                   1715:         }
                   1716:     }
                   1717:     return @currsecs;
                   1718: }
                   1719: 
1.1       raeburn  1720: sub print_request_status {
1.11      raeburn  1721:     my ($dom) = @_;
1.14      raeburn  1722:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                   1723:                                            $env{'user.name'},'^status:'.$dom);
                   1724:     my ($output,$formname,%queue_by_date,%typenames);
                   1725:     if ($env{'form.crstype'} eq 'any') {
                   1726:         %typenames = &Apache::lonlocal::texthash (
                   1727:                                                    official   => 'Official course',
                   1728:                                                    unofficial => 'Unofficial course',
                   1729:                                                    community  => 'Community',
                   1730:                                                  );
                   1731:     }
                   1732:     foreach my $key (keys(%statusinfo)) {
                   1733:         if (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending')) { 
                   1734:             (undef,my($cdom,$cnum)) = split(':',$key);
                   1735:             next if ($cdom ne $dom);   
                   1736:             my $requestkey = $cdom.'_'.$cnum;
                   1737:             if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   1738:                 my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   1739:                                                        $env{'user.domain'},$env{'user.name'});
                   1740:                 my $entry;
                   1741:                 my $timestamp = $history{'reqtime'};
                   1742:                 my $crstype = $history{'crstype'};
                   1743:                 my $disposition = $history{'disposition'};
                   1744:                 next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
                   1745:                 next unless (($env{'form.crstype'} eq 'any') || 
                   1746:                             ($env{'form.crstype'} eq $crstype));
                   1747:                 next unless (($disposition eq 'approval') || 
                   1748:                              ($disposition eq 'pending'));
                   1749:                 if (ref($history{'details'}) eq 'HASH') {
                   1750:                     $entry = $requestkey.':'.$crstype.':'.
                   1751:                              &escape($history{'details'}{'cdescr'});
                   1752:                     if ($crstype eq 'official') {
                   1753:                         $entry .= ':'.&escape($history{'details'}{'instcode'}); 
                   1754:                     }
                   1755:                 }
                   1756:                 if ($entry ne '') {
                   1757:                     if (exists($queue_by_date{$timestamp})) {
                   1758:                         if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                   1759:                             push(@{$queue_by_date{$timestamp}},$entry);
                   1760:                         }
                   1761:                     } else {
                   1762:                         @{$queue_by_date{$timestamp}} = ($entry);
1.10      raeburn  1763:                     }
                   1764:                 }
                   1765:             }
                   1766:         }
                   1767:     }
1.11      raeburn  1768:     $formname = 'requestcrs';
1.10      raeburn  1769:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
1.16      raeburn  1770:     $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                   1771: 
1.10      raeburn  1772:               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
1.16      raeburn  1773:               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n".
                   1774:               '<input type="hidden" name="showdom" value="" />'."\n".
                   1775:               '<input type="hidden" name="cnum" value="" />'."\n";
1.10      raeburn  1776:     if (@sortedtimes > 0) {
                   1777:         $output .= &Apache::loncommon::start_data_table().
                   1778:                    &Apache::loncommon::start_data_table_header_row().
                   1779:                    '<th>'.&mt('Action').'</th>'.
1.14      raeburn  1780:                    '<th>'.&mt('Course Description').'</th>'.
                   1781:                    '<th>'.&mt('Domain').'</th>';
                   1782:         if ($env{'form.crstype'} eq 'any') {
1.10      raeburn  1783:             $output .= '<th>'.&mt('Type').'</th>';
                   1784:         }
1.14      raeburn  1785:         if (($env{'form.crstype'} eq 'any') || ($env{'form.crstype'} eq 'official')) {
1.10      raeburn  1786:             $output .= '<th>'.&mt('Institutional Code').'</th>';
                   1787:         }
                   1788:         $output .= '<th>'.&mt('Date requested').'</th>'.
                   1789:                    &Apache::loncommon::end_data_table_header_row();
                   1790:         my $count = 0;
                   1791:         foreach my $item (@sortedtimes) {
                   1792:             my $showtime = &Apache::lonlocal::locallocaltime($item);
                   1793:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
                   1794:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
                   1795:                     my ($key,$type,$desc,$instcode) = split(':',$request);
                   1796:                     my ($cdom,$cnum) = split('_',$key);
                   1797:                     $output .= &Apache::loncommon::start_data_table_row().
1.16      raeburn  1798:      '<td><input type="button" value="'.&mt('Select').'" onclick="javascript:chooseRequest('."'$cdom','$cnum'".')" /></td>'.
1.14      raeburn  1799:      '<td>'.&unescape($desc).'</td>'.
                   1800:      '<td>'.$cdom.'</td>';
                   1801:                     if ($env{'form.crstype'} eq 'any') {
                   1802:                         my $typename = $typenames{$type};
                   1803:                         if ($typename eq '') {
                   1804:                             $typename = &mt('Unknown type');
                   1805:                         }
                   1806:                         $output .= '<td>'.$typename.'</td>';
1.10      raeburn  1807:                     }
1.14      raeburn  1808:                     if (($env{'form.crstype'} eq 'any') || 
1.10      raeburn  1809:                         ($env{'form.crstype'} eq 'official')) {
1.14      raeburn  1810:                         my $showinstcode;
                   1811:                         if ($type eq 'official') {
                   1812:                             $showinstcode = &unescape($instcode);
                   1813:                         } else {
                   1814:                             $showinstcode = &mt('Not applicable');
                   1815:                         }
                   1816:                         $output .= '<td>'.$showinstcode.'</td>';
1.10      raeburn  1817:                     }
                   1818:                     $output .= '<td>'.$showtime.'</td>'.
                   1819:                                &Apache::loncommon::end_data_table_row();
                   1820:                 }
                   1821:             }
                   1822:         }
                   1823:         $output .= &Apache::loncommon::end_data_table();
                   1824:     } else {
1.11      raeburn  1825:         $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>';
1.10      raeburn  1826:     }
                   1827:     $output .= '
1.30      raeburn  1828: <br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />';
1.10      raeburn  1829:     return $output;
1.1       raeburn  1830: }
                   1831: 
1.16      raeburn  1832: sub print_cancel_request {
                   1833:     my ($dom,$cnum) = @_;
                   1834:     my $requestkey = $dom.'_'.$cnum;
                   1835:     my ($result,$output);
                   1836:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   1837:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   1838:                                                $env{'user.domain'},$env{'user.name'});
                   1839:         my $timestamp = $history{'reqtime'};
                   1840:         my $crstype = $history{'crstype'};
                   1841:         my $status = $history{'status'};
                   1842:         if (($status eq 'cancelled') || ($status eq 'created')) { 
                   1843:             if ($status eq 'cancelled') {
                   1844:                 $output = &mt('This request has already been cancelled.');
                   1845:             } elsif ($status eq 'created') {
                   1846:                 $output = &mt('This request has already been processed, and a course created.');
                   1847:             }
                   1848:             $output = &mt('No further action will be taken');
                   1849:         } elsif (ref($history{'details'}) eq 'HASH') {
                   1850:             my ($types,$typename) = &course_types();
                   1851:             my $showtype = $crstype;
                   1852:             if (defined($typename->{$crstype})) {
                   1853:                 $showtype = $typename->{$crstype}; 
                   1854:             }
                   1855:             $output = '<p>'.&Apache::loncommon::start_data_table().
                   1856:                       &Apache::loncommon::start_data_table_header_row().
                   1857:                       '<th>'.&mt('Description').'</th><th>'.&mt('Requested').'</th>'.
                   1858:                       '<th>'.&mt('Type').'</th>'.
                   1859:                       &Apache::loncommon::end_data_table_header_row().
                   1860:                       &Apache::loncommon::start_data_table_row().
                   1861:                       '<td>'.$history{details}{'cdescr'}.'</td><td>'.
                   1862:                       &Apache::lonlocal::locallocaltime($timestamp).'</td>'.
                   1863:                       '<td>'.$showtype.'</td>'.
                   1864:                       &Apache::loncommon::end_data_table_row().
                   1865:                       &Apache::loncommon::end_data_table().
                   1866:                       '<br /><div class="LC_warning">'.
                   1867:                       &mt('Cancelling the request will remove it from the queue of pending course requests').'</div>';
                   1868:             $result = 'ok';
                   1869:         } else {
                   1870:             $output = '<div class="LC_error">'.&mt('No record exists for the course ID').'</div>';
                   1871:         }
                   1872:     } else {
                   1873:         $output = '<div class="LC_error">'.&mt('Invalid course ID').'</div>';
                   1874:     }
                   1875:     return ($result,$output);
                   1876: }
                   1877: 
                   1878: sub viewrequest_javascript {
                   1879:     my ($formname,$next) = @_;
                   1880:     return <<"ENDJS";
                   1881: 
                   1882: function chooseRequest(cdom,cnum) {
                   1883:     document.$formname.showdom.value = cdom;
                   1884:     document.$formname.cnum.value = cnum;
                   1885:     nextPage(document.$formname,'$next');
                   1886: }
                   1887: 
                   1888: ENDJS
                   1889: }
                   1890: 
                   1891: sub viewdetails_javascript {
                   1892:     my ($formname) = @_;
                   1893:     return << "ENDJS";
                   1894: 
                   1895: function nextPage(formname,nextstate) {
                   1896:     if (nextstate == "modify") { 
                   1897:         formname.state.value = "personnel";
                   1898:         formname.action.value = "new";
                   1899:     } else {
                   1900:         formname.state.value = nextstate;
                   1901:     }
                   1902:     formname.submit();
                   1903: }
                   1904: 
                   1905: function backPage(formname,prevstate) {
                   1906:     formname.state.value = prevstate;
                   1907:     formname.submit();
                   1908: }
                   1909: 
                   1910: ENDJS
                   1911: }
                   1912: 
                   1913: sub viewcancel_javascript {
                   1914:     my $alert = &mt('Are you sure you want to cancel this request?\\n'.
                   1915:                     'Your request will be removed.');
                   1916:     return << "ENDJS";
                   1917: function nextPage(formname,nextstate) {
                   1918:     if (confirm('$alert')) {
                   1919:         formname.state.value = nextstate;
                   1920:         formname.submit();
                   1921:     }
                   1922:     return;
                   1923: }
                   1924: 
                   1925: ENDJS
                   1926: }
                   1927: 
1.1       raeburn  1928: sub print_request_logs {
1.10      raeburn  1929:     my ($jscript,$loaditems,$crumb) = @_;
1.1       raeburn  1930:     return;
                   1931: }
                   1932: 
                   1933: sub print_review {
1.30      raeburn  1934:     my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
                   1935:         $disallowed,$disallowmsg) = @_;
1.4       raeburn  1936:     my ($types,$typename) = &course_types();
                   1937:     my ($owner,$ownername,$owneremail);
1.26      raeburn  1938:     if ($uname eq '' || $udom eq '') {
                   1939:         $uname = $env{'user.name'};
                   1940:         $udom = $env{'user.domain'};
                   1941:     }
                   1942:     $owner = $uname.':'.$udom;
                   1943:     $ownername = &Apache::loncommon::plainname($uname,$udom,'first');
                   1944:     my %emails = &Apache::loncommon::getemails($uname,$udom);
1.4       raeburn  1945:     foreach my $email ('permanentemail','critnotification','notification') {
                   1946:         $owneremail = $emails{$email};
                   1947:         last if ($owneremail ne '');
                   1948:     }
                   1949:     my ($inst_headers,$inst_values,$crstypename,$enroll_headers,$enroll_values,
                   1950:         $section_headers,$section_values,$personnel_headers,$personnel_values);
                   1951: 
                   1952:     $crstypename = $env{'form.crstype'};
                   1953:     if (ref($typename) eq 'HASH') {
                   1954:         unless ($typename->{$env{'form.crstype'}} eq '') {
                   1955:             $crstypename = $typename->{$env{'form.crstype'}};
                   1956:         }
                   1957:     }
1.16      raeburn  1958:     my $category = 'Course';
                   1959:     if ($env{'form.crstype'} eq 'community') {
                   1960:         $category = 'Community';
                   1961:     }
1.4       raeburn  1962: 
                   1963:     $inst_headers = '<th>'.&mt('Description').'</th><th>'.&mt('Type').'</th>';
                   1964:     $inst_values = '<td>'.$env{'form.cdescr'}.'</td><td>'.$crstypename.'</td>';
                   1965: 
1.16      raeburn  1966:     my $enrollrow_title = &mt('Default Access Dates').'<br />'.
                   1967:                           '('.&Apache::lonnet::plaintext('st',$category).')';
1.4       raeburn  1968:     if ($env{'form.crstype'} eq 'official') {
                   1969:         if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH')) {
                   1970:             foreach my $title (@{$codetitles}) {
                   1971:                 if ($env{'form.instcode_'.$title} ne '') {
                   1972:                     $inst_headers .= '<th>'.$title.'</th>';
                   1973:                     my $longitem = $env{'form.instcode_'.$title};
                   1974:                     if (ref($cat_titles->{$title}) eq 'HASH') {
                   1975:                         if ($cat_titles->{$title}{$env{'form.instcode_'.$title}} ne '') {
                   1976:                             $longitem = $cat_titles->{$title}{$env{'form.instcode_'.$title}};
                   1977:                         }
                   1978:                     }
                   1979:                     $inst_values .= '<td>'.$longitem.'</td>';
                   1980:                 }
                   1981:             }
                   1982:         }
                   1983:         if (&Apache::lonnet::auto_run('',$dom)) {
1.16      raeburn  1984:             $enrollrow_title = &mt('Enrollment');
1.4       raeburn  1985:             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
                   1986:                               '<th>'.&mt('Automatic Drops').'</th>'.
                   1987:                               '<th>'.&mt('Enrollment Starts').'</th>'.
                   1988:                               '<th>'.&mt('Enrollment Ends').'</th>';
                   1989:             $section_headers = '<th>'.&mt('Sections').'</th>'.
                   1990:                                '<th>'.&mt('Crosslistings').'</th>';
                   1991: 
1.13      raeburn  1992:             my ($enrollstart,$enrollend) = &dates_from_form('enrollstart','enrollend');
1.4       raeburn  1993:             my @autoroster = (&mt('No'),&mt('Yes'));
                   1994:             $enroll_values = '<td>'.$autoroster[$env{'form.autoadds'}].'</td>'.
                   1995:                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
1.13      raeburn  1996:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollstart).'</td>'.
                   1997:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollend).'</td>';
1.6       raeburn  1998:             $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
                   1999:                               &mt('Institutional section').'</th>'.
                   2000:                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  2001:             my $secinfo;
1.4       raeburn  2002:             if ($env{'form.sectotal'} > 0) {
                   2003:                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   2004:                     if ($env{'form.sec_'.$i}) {
1.5       raeburn  2005:                         $secinfo .= '<tr><td>'.$env{'form.secnum_'.$i}.'</td><td>';
1.4       raeburn  2006:                         if ($env{'form.loncapasec_'.$i} ne '') {
1.5       raeburn  2007:                             $secinfo .= $env{'form.loncapasec_'.$i};
                   2008:                         } else {
                   2009:                             $secinfo .= &mt('None');
1.4       raeburn  2010:                         }
1.5       raeburn  2011:                         $secinfo .= '</td></tr>';
1.4       raeburn  2012:                     }
                   2013:                 }
                   2014:             }
1.6       raeburn  2015:             if ($secinfo eq '') {
                   2016:                 $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  2017:             }
1.6       raeburn  2018:             $section_values .= $secinfo.'</table></td><td>'.
                   2019:                                '<table class="LC_innerpickbox"><tr><th>'.
                   2020:                                &mt('Institutional course/section').'</th>'.
                   2021:                                '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  2022:             my $xlistinfo;
1.24      raeburn  2023:             my $crosslisttotal = $env{'form.crosslisttotal'};
                   2024:             if (!$crosslisttotal) {
                   2025:                 $crosslisttotal = 1;
                   2026:             }
                   2027:             for (my $i=0; $i<$crosslisttotal; $i++) {
                   2028:                 if ($env{'form.crosslist_'.$i}) {
                   2029:                     $xlistinfo .= '<tr><td>';
                   2030:                     if (ref($code_order) eq 'ARRAY') {
                   2031:                         if (@{$code_order} > 0) {
                   2032:                             foreach my $item (@{$code_order}) {
                   2033:                                 $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
1.4       raeburn  2034:                             }
                   2035:                         }
                   2036:                     }
1.24      raeburn  2037:                     $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'}.'</td><td>';
                   2038:                     if ($env{'form.crosslist_'.$i.'_lcsec'}) {
                   2039:                         $xlistinfo .= $env{'form.crosslist_'.$i.'_lcsec'};
                   2040:                     } else {
                   2041:                         $xlistinfo .= &mt('None');
                   2042:                     }
                   2043:                     $xlistinfo .= '</td></tr>';
1.4       raeburn  2044:                 }
                   2045:             }
1.6       raeburn  2046:             if ($xlistinfo eq '') {
                   2047:                 $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  2048:             }
1.24      raeburn  2049:             $section_values .= $xlistinfo;
1.4       raeburn  2050:         }
1.24      raeburn  2051:         $section_values .= '</table></td>';
1.4       raeburn  2052:     }
                   2053: 
                   2054:     my %ctxt = &clone_text();
                   2055:     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
1.13      raeburn  2056:     if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
                   2057:         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
1.15      raeburn  2058:         my $canclone = &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
                   2059:                            $env{'user.domain'},$env{'form.clonecrs'}, $env{'form.clonedom'});
                   2060:         if ($canclone) {
                   2061:             my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
                   2062:                               $env{'form.clonecrs'},('description','internal.coursecode'));
                   2063:             if (keys(%courseenv) > 0) {
                   2064:                 $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
                   2065:                 $inst_values .= '<td>'.$courseenv{'description'}.'&nbsp;';
                   2066:                 my $cloneinst = $courseenv{'internal.coursecode'};
                   2067:                 if ($cloneinst ne '') {
1.18      raeburn  2068:                     $inst_values .= $cloneinst.' '.&mt('in').' '.$env{'form.clonedom'};
1.15      raeburn  2069:                 } else {
1.18      raeburn  2070:                     $inst_values .= &mt('from').' '.$env{'form.clonedom'};
1.15      raeburn  2071:                 }
                   2072:                 $inst_values .= '</td><td>';
                   2073:                 if ($env{'form.datemode'} eq 'preserve') {
1.16      raeburn  2074:                     $inst_values .= $ctxt{'prd'}; 
1.15      raeburn  2075:                 } elsif ($env{'form.datemode'} eq 'shift') {
                   2076:                     $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
                   2077:                 } else {
                   2078:                     $inst_values .= $ctxt{'ncd'};
                   2079:                 }
                   2080:                 $inst_values .= '</td>';
                   2081:              } else {
                   2082:                  $inst_values .= '<td>'.&mt('Unknown').'</td>';
                   2083:              }
                   2084:          } else {
                   2085:              $inst_values .= '<td>'.&mt('Not permitted'),'</td>';
                   2086:          }
1.4       raeburn  2087:     } else {
                   2088:         $inst_values .= '<td>'.&mt('None').'</td>';
                   2089:     }
                   2090:     $enroll_headers .= '<th>'.&mt('Access Starts').'</th>'.
                   2091:                        '<th>'.&mt('Access Ends').'</th>';
1.13      raeburn  2092:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
                   2093:     $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessstart).'</td>';
                   2094:     if ($accessend == 0) {
1.4       raeburn  2095:         $enroll_values .= '<td>'.&mt('No end date').'</td>';
                   2096:     } else {
1.13      raeburn  2097:         $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessend).'</td>';
1.4       raeburn  2098:     }
                   2099: 
                   2100:     my $container = 'Course';
                   2101:     if ($env{'form.crstype'} eq 'community') {
                   2102:         $container = 'Community';
                   2103:     }
                   2104: 
                   2105:     $personnel_headers = '<th>'.&mt('Name').'</th><th>'.&mt('Username:Domain').
                   2106:                          '</th><th>'.&mt('Role').'</th><th>'.&mt('LON-CAPA Sections').
                   2107:                          '</th>';
                   2108:     $personnel_values .= '<tr><td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   2109:                          '<td>'.&Apache::lonnet::plaintext('cc',$container).'</td>'.
                   2110:                          '<td>'.&mt('None').'</td></tr>';
                   2111:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   2112:         if ($env{'form.person_'.$i.'_uname'} ne '') {
1.30      raeburn  2113:             if (ref($disallowed) eq 'ARRAY') {
                   2114:                 next if (grep(/^$i$/,@{$disallowed}));
                   2115:             } 
1.31      raeburn  2116:             my @officialsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
                   2117:             my @allsecs;
                   2118:             foreach my $sec (@officialsecs) {
                   2119:                 next unless ($sec =~ /\w/);
                   2120:                 next if ($sec =~ /\W/);
                   2121:                 next if ($sec eq 'none');
                   2122:                 push(@allsecs,$sec);
                   2123:             }
1.14      raeburn  2124:             my $newsec = $env{'form.person_'.$i.'_newsec'};
                   2125:             $newsec =~ s/^\s+//;
                   2126:             $newsec =~s/\s+$//;
1.31      raeburn  2127:             my @newsecs = split(/\s*[\s,;:]\s*/,$newsec);
1.14      raeburn  2128:             foreach my $sec (@newsecs) {
1.31      raeburn  2129:                 next unless ($sec =~ /\w/); 
1.14      raeburn  2130:                 next if ($sec =~ /\W/);
1.31      raeburn  2131:                 next if ($sec eq 'none');
1.14      raeburn  2132:                 if ($sec ne '') {
                   2133:                     unless (grep(/^\Q$sec\E$/,@allsecs)) {
                   2134:                         push(@allsecs,$sec); 
                   2135:                     }
                   2136:                 }
                   2137:             }
1.24      raeburn  2138:             my $showsec;
1.14      raeburn  2139:             if (@allsecs) {
                   2140:                 $showsec = join(', ',@allsecs);
                   2141:             }
1.24      raeburn  2142:             if ($showsec eq '') {
                   2143:                 $showsec = &mt('None');
                   2144:             }
                   2145:             if ($env{'form.person_'.$i.'_role'} eq 'cc') {
                   2146:                 $showsec = &mt('None');
                   2147:             }
1.28      raeburn  2148:             my $role = $env{'form.person_'.$i.'_role'}; 
1.4       raeburn  2149:             $personnel_values .= 
1.13      raeburn  2150:                 '<tr><td>'.$env{'form.person_'.$i.'_firstname'}.' '.
                   2151:                 $env{'form.person_'.$i.'_lastname'}.'</td>'.
1.4       raeburn  2152:                 '<td>'.$env{'form.person_'.$i.'_uname'}.':'.
                   2153:                 $env{'form.person_'.$i.'_dom'}.'</td>'.
1.28      raeburn  2154:                 '<td>'.&Apache::lonnet::plaintext($role,$container).'</td>'.
1.14      raeburn  2155:                 '<td>'.$showsec.'</td></tr>';
1.4       raeburn  2156:         }
                   2157:     }
1.30      raeburn  2158:     my $output;
                   2159:     if (ref($disallowed) eq 'ARRAY') {
                   2160:         if (@{$disallowed} > 0) {
                   2161:             if (ref($disallowmsg) eq 'HASH') {
                   2162:                 $output = '<p class="LC_warning">'.
                   2163:                           &mt('Not all requested personnel could be included.').'<ul>'; 
                   2164:                 foreach my $item (@{$disallowed}) {
                   2165:                     $output .= '<li>'.$disallowmsg->{$item}.'</li>';
                   2166:                 }
                   2167:                 $output .= '</ul></p>';
                   2168:             }
                   2169:         }
                   2170:     }
                   2171:     $output .=    '<div>'.&Apache::lonhtmlcommon::start_pick_box().
1.4       raeburn  2172:                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
1.6       raeburn  2173:                   '<table class="LC_innerpickbox"><tr>'.
1.4       raeburn  2174:                   '<th>'.&mt('Name').'</th>'.
                   2175:                   '<th>'.&mt('Username:Domain').'</th>'.
                   2176:                   '<th>'.&mt('E-mail address').'</th>'.
                   2177:                   '</tr><tr>'."\n".
                   2178:                   '<td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   2179:                   '<td>'.$owneremail.'</td>'.
                   2180:                   '</tr></table>'."\n".
                   2181:                   &Apache::lonhtmlcommon::row_closure().
1.5       raeburn  2182:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
1.4       raeburn  2183:                   '<table class="LC_innerpickbox"><tr>'.$inst_headers.'</tr>'."\n".
                   2184:                   '<tr>'.$inst_values.'</tr></table>'."\n".
                   2185:                   &Apache::lonhtmlcommon::row_closure().
1.16      raeburn  2186:                   &Apache::lonhtmlcommon::row_title($enrollrow_title).
1.4       raeburn  2187:                   '<table class="LC_innerpickbox"><tr>'.$enroll_headers.'</tr>'."\n".
                   2188:                   '<tr>'.$enroll_values.'</tr></table>'."\n".
                   2189:                   &Apache::lonhtmlcommon::row_closure();
                   2190:     if ($section_headers ne '') {
                   2191:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Sections')).
                   2192:                    '<table class="LC_innerpickbox"><tr>'.$section_headers.'</tr>'."\n".
                   2193:                    '<tr>'.$section_values.'</tr></table>'."\n".
                   2194:                    &Apache::lonhtmlcommon::row_closure();
                   2195:     }
                   2196:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Personnel')).
                   2197:                '<table class="LC_innerpickbox"><tr>'.$personnel_headers.'</tr>'."\n".
                   2198:                $personnel_values.'</table>'."\n".
                   2199:                &Apache::lonhtmlcommon::row_closure(1).
1.26      raeburn  2200:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.4       raeburn  2201:     return $output;
                   2202: }
                   2203: 
                   2204: sub dates_from_form {
                   2205:     my ($startname,$endname) = @_;
                   2206:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   2207:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.13      raeburn  2208:     if ($endname eq 'accessend') {
1.4       raeburn  2209:         if (exists($env{'form.no_end_date'}) ) {
                   2210:             $enddate = 0;
                   2211:         }
                   2212:     }
                   2213:     return ($startdate,$enddate);
1.1       raeburn  2214: }
                   2215: 
                   2216: sub courseinfo_form {
1.15      raeburn  2217:     my ($dom,$formname,$crstype,$next) = @_;
1.32    ! raeburn  2218:     my %lt = &Apache::lonlocal::texthash(
        !          2219:                 official => 'You must provide a (brief) course description.',
        !          2220:                 community => 'You must provide a (brief) community description.'
        !          2221:              );
        !          2222:     $lt{'unofficial'} = $lt{'official'};
1.15      raeburn  2223:     my $js_validate = <<"ENDJS";
                   2224: <script type="text/javascript">
                   2225: // <![CDATA['
                   2226: 
                   2227: function validateForm() {
                   2228:     if ((document.$formname.cdescr.value == "")  || (document.$formname.cdescr.value == "undefined")) {
1.32    ! raeburn  2229:         alert('$lt{$crstype}');
1.15      raeburn  2230:         return;
                   2231:     }
                   2232:     nextPage(document.$formname,'$next');
                   2233: }
                   2234: // ]]
                   2235: </script>
                   2236: 
                   2237: ENDJS
1.27      raeburn  2238:     my $title = &mt('Brief Course Description');
                   2239:     if ($crstype eq 'community') {
                   2240:         $title = &mt('Brief Community Description');
                   2241:     }
1.15      raeburn  2242:     my $output .= $js_validate."\n".'<div>'.&Apache::lonhtmlcommon::start_pick_box().
1.27      raeburn  2243:                   &Apache::lonhtmlcommon::row_headline().
                   2244:                   '<h3>'.$title.'</h3>'.
                   2245:                   &Apache::lonhtmlcommon::row_closure(1).
                   2246:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
1.13      raeburn  2247:                  '<input type="text" size="40" name="cdescr" />';
                   2248:     my ($home_server_pick,$numlib) =
                   2249:         &Apache::loncommon::home_server_form_item($dom,'chome',
                   2250:                                                   'default','hide');
                   2251:     if ($numlib > 1) {
                   2252:         $output .= &Apache::lonhtmlcommon::row_closure().
1.16      raeburn  2253:                    &Apache::lonhtmlcommon::row_title(&mt('Home Server for Course'));
1.13      raeburn  2254:     }
                   2255:     $output .= $home_server_pick.
1.27      raeburn  2256:                &Apache::lonhtmlcommon::row_closure().
                   2257:                &Apache::lonhtmlcommon::row_headline().
                   2258:                '<h3>'.&mt('Clone content and settings from an existing course?').'</h3>'. 
1.13      raeburn  2259:                &Apache::lonhtmlcommon::row_closure(1).
1.27      raeburn  2260:                &clone_form($dom,$formname,$crstype).
                   2261:                &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
1.1       raeburn  2262:     return $output;
                   2263: }
                   2264: 
                   2265: sub clone_form {
                   2266:     my ($dom,$formname,$crstype) = @_;
                   2267:     my $type = 'Course';
                   2268:     if ($crstype eq 'community') {
                   2269:         $type = 'Community';
                   2270:     }
1.13      raeburn  2271:     my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedom').
                   2272:                     &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type);
1.4       raeburn  2273:     my %lt = &clone_text();
1.2       raeburn  2274:     my $output .= 
                   2275:         &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
1.29      raeburn  2276:         '<input type="text" size="25" name="clonecrs" value=""  />'.
1.2       raeburn  2277:         '</label>'.&Apache::lonhtmlcommon::row_closure(1).'<label>'.
                   2278:         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'</label>'.
1.27      raeburn  2279:         $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure(1).
1.2       raeburn  2280:         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
                   2281:         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
                   2282:         '</label><br /><label>'.
                   2283:         '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
                   2284:         '</label><br /><label>'.
                   2285:         '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
                   2286:         $lt{'shd'}.'</label>'.
                   2287:         '<input type="text" size="5" name="dateshift" value="365" />'.
1.27      raeburn  2288:         &Apache::lonhtmlcommon::row_closure(1);
1.1       raeburn  2289:     return $output;
                   2290: }
                   2291: 
1.16      raeburn  2292: sub clone_text {
1.4       raeburn  2293:     return &Apache::lonlocal::texthash(
                   2294:                'cid'  => 'Course ID',
                   2295:                'dmn'  => 'Domain',
                   2296:                'dsh'  => 'Date Shift',
                   2297:                'ncd'  => 'Do not clone date parameters',
                   2298:                'prd'  => 'Clone date parameters as-is',
                   2299:                'shd'  => 'Shift date parameters by number of days',
                   2300:         );
                   2301: }
                   2302: 
1.1       raeburn  2303: sub coursecode_form {
1.2       raeburn  2304:     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
1.1       raeburn  2305:     my $output;
1.2       raeburn  2306:     my %rowtitle = (
                   2307:                     instcode  => 'Course Category',
                   2308:                     crosslist => 'Cross Listed Course',
                   2309:                    );
1.1       raeburn  2310:     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   2311:         (ref($cat_order))) {
1.2       raeburn  2312:         my ($sel,$instsec,$lcsec);
                   2313:         $sel = $context;
                   2314:         if ($context eq 'crosslist') {
                   2315:             $sel .= '_'.$num;
                   2316:             $instsec = &mt('Institutional section').'<br />'.
                   2317:                        '<input type="text" size="10" name="'.$sel.'_instsec" />';
                   2318:             $lcsec = &mt('LON-CAPA section').'<br />'.
                   2319:                      '<input type="text" size="10" name="'.$sel.'_lcsec" />';
                   2320:         }
1.1       raeburn  2321:         if (@{$codetitles} > 0) {
                   2322:             my $lastitem = pop(@{$codetitles});
1.2       raeburn  2323:             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
1.1       raeburn  2324:             if (@{$codetitles} > 0) {
1.2       raeburn  2325:                 $output = &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   2326:                           '<table><tr>';
1.1       raeburn  2327:                 if ($context eq 'crosslist') {
1.2       raeburn  2328:                     $output .= '<td>'.&mt('Include?').'<br />'.
                   2329:                                '<input type="checkbox" name="'.$sel.'" value="1" /></td>';
1.1       raeburn  2330:                 }
                   2331:                 foreach my $title (@{$codetitles}) {
                   2332:                     if (ref($cat_order->{$title}) eq 'ARRAY') {
                   2333:                         if (@{$cat_order->{$title}} > 0) {
                   2334:                             $output .= '<td align="center">'.$title.'<br />'."\n".
                   2335:                                        '<select name="'.$sel.'_'.$title.'">'."\n".
                   2336:                                        ' <option value="" selected="selected">'.
                   2337:                                        &mt('Select').'</option>'."\n";
                   2338:                             foreach my $item (@{$cat_order->{$title}}) {
                   2339:                                 my $longitem = $item;
                   2340:                                 if (ref($cat_titles->{$title}) eq 'HASH') {
                   2341:                                     if ($cat_titles->{$title}{$item} ne '') {
                   2342:                                         $longitem = $cat_titles->{$title}{$item};
                   2343:                                     }
                   2344:                                 }
                   2345:                                 $output .= '<option value="'.$item.'">'.$longitem.
                   2346:                                            '</option>'."\n";
                   2347:                             }
                   2348:                         }
                   2349:                         $output .= '</select></td>'."\n";
                   2350:                     }
                   2351:                 }
                   2352:                 if ($context eq 'crosslist') {
                   2353:                     $output .= '<td align="center">'.$lastitem.'<br />'."\n".
1.2       raeburn  2354:                                $lastinput.'</td><td align="center">'.$instsec.'</td>'.
                   2355:                                '<td align="center">'.$lcsec.'</td></tr></table>';
1.1       raeburn  2356:                 } else {
                   2357:                     $output .= '</tr></table>'.
                   2358:                                &Apache::lonhtmlcommon::row_closure().
                   2359:                                &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   2360:                                $lastinput;
                   2361:                 }
                   2362:             } else {
                   2363:                 if ($context eq 'crosslist') {
                   2364:                     $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
1.2       raeburn  2365:                                '<table><tr>'.
                   2366:                                '<td align="center">'.$lastitem.'<br />'.$lastinput.'</td>'.
                   2367:                                '<td align="center">'.$instsec.'</td><td>'.$lcsec.'</td>'.
                   2368:                                '</tr></table>';
1.1       raeburn  2369:                 } else { 
                   2370:                     $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   2371:                                $lastinput;
                   2372:                 }
                   2373:             }
1.2       raeburn  2374:             $output .=  &Apache::lonhtmlcommon::row_closure(1);
                   2375:             push(@$codetitles,$lastitem);    
                   2376:         } elsif ($context eq 'crosslist') {
                   2377:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   2378:                        '<table><tr><td align="center">'.
                   2379:                        '<span class="LC_nobreak">'.&mt('Include?').
                   2380:                        '<input type="checkbox" name="'.$sel.'" value="1" /></span>'.
                   2381:                        '</td><td align="center">'.&mt('Institutional ID').'<br />'.
                   2382:                        '<input type="text" size="10" name="'.$sel.'_instsec" />'.
                   2383:                        '</td><td align="center">'.$lcsec.'</td></tr></table>'.
                   2384:                        &Apache::lonhtmlcommon::row_closure(1);
1.1       raeburn  2385:         }
                   2386:     }
                   2387:     return $output;
                   2388: }
                   2389: 
                   2390: sub get_course_dom {
                   2391:     my $codedom = &Apache::lonnet::default_login_domain();
1.19      raeburn  2392:     if ($env{'form.showdom'} ne '') {
                   2393:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
                   2394:             return $env{'form.showdom'};
                   2395:         }
                   2396:     }
1.1       raeburn  2397:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
1.19      raeburn  2398:         my ($types,$typename) = &course_types();
                   2399:         if (ref($types) eq 'ARRAY') {
                   2400:             foreach my $type (@{$types}) {
                   2401:                 if (&Apache::lonnet::usertools_access($env{'user.name'},
                   2402:                                                       $env{'user.domain'},$type,
                   2403:                                                       undef,'requestcourses')) {
                   2404:                     return $env{'user.domain'};
                   2405:                 }
                   2406:             }
                   2407:             my @possible_doms;
                   2408:             foreach my $type (@{$types}) {
                   2409:                 my $dom_str = $env{'environment.reqcrsotherdom.'.$type};
                   2410:                 if ($dom_str ne '') {
                   2411:                     my @domains = split(',',$dom_str);
                   2412:                     foreach my $entry (@domains) {
                   2413:                         my ($extdom,$extopt) = split(':',$entry);
                   2414:                         if ($extdom eq $env{'request.role.domain'}) {
                   2415:                             return $extdom;
                   2416:                         } 
                   2417:                         unless(grep(/^\Q$extdom\E$/,@possible_doms)) {
                   2418:                             push(@possible_doms,$extdom);
                   2419:                         }
                   2420:                     }
                   2421:                 }
                   2422:             }
                   2423:             if (@possible_doms) {
                   2424:                 @possible_doms = sort(@possible_doms);
                   2425:                 return $possible_doms[0];
                   2426:             }
                   2427:         }
1.1       raeburn  2428:         $codedom = $env{'user.domain'};
                   2429:         if ($env{'request.role.domain'} ne '') {
                   2430:             $codedom = $env{'request.role.domain'};
                   2431:         }
                   2432:     }
                   2433:     return $codedom;
                   2434: }
                   2435: 
                   2436: sub display_navbuttons {
1.31      raeburn  2437:     my ($r,$dom,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
1.1       raeburn  2438:     $r->print('<div class="LC_navbuttons">');
                   2439:     if ($prev) {
1.16      raeburn  2440:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
1.31      raeburn  2441:                   'onclick="javascript:backPage('."document.$formname,'$prev'".')"/>'.
1.16      raeburn  2442:                   ('&nbsp;'x3));
1.1       raeburn  2443:     } elsif ($prevtext) {
1.16      raeburn  2444:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
                   2445:                   'onclick="javascript:history.back()"/>'.('&nbsp;'x3));
                   2446:     }
                   2447:     if ($state eq 'details') {
                   2448:         $r->print(' <input type="button" name="other" value="'.$othertext.'" '.
1.31      raeburn  2449:                   'onclick="javascript:nextPage('."document.$formname,'$other'".
1.16      raeburn  2450:                   ')" />');
1.1       raeburn  2451:     }
1.31      raeburn  2452:     my $gotnext;
1.15      raeburn  2453:     if ($state eq 'courseinfo') {
1.16      raeburn  2454:         $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
                   2455:                   'onclick="javascript:validateForm();" />');
1.31      raeburn  2456:         $gotnext = 1;
                   2457:     } elsif ($state eq 'enrollment') {
                   2458:         if (($env{'form.crstype'} eq 'official') && 
                   2459:             (&Apache::lonnet::auto_run('',$dom))) {
                   2460:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
                   2461:                       'onclick="javascript:validateEnrollSections('."document.$formname,'$next'".');" />');
                   2462:                 $gotnext = 1;
                   2463:         }
                   2464:     } elsif ($state eq 'personnel') {
                   2465:         if ($env{'form.persontotal'} > 0) { 
                   2466:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
                   2467:                       'onclick="javascript:validatePersonnelSections('."document.$formname,'$next'".');" />');
                   2468:             $gotnext = 1;
                   2469:         }
                   2470:     }
                   2471:     unless ($gotnext) {
                   2472:         if ($next) {
                   2473:             $r->print('
                   2474:                       <input type="button" name="next" value="'.$nexttext.'" '.
                   2475:       'onclick="javascript:nextPage('."document.$formname,'$next'".')" />');
                   2476:         }
1.1       raeburn  2477:     }
                   2478:     $r->print('</div>');
                   2479: }
                   2480: 
                   2481: sub print_request_outcome {
1.10      raeburn  2482:     my ($dom,$codetitles,$code_order) = @_;
1.13      raeburn  2483:     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
1.10      raeburn  2484:         %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,);
1.24      raeburn  2485:     my $sectotal = $env{'form.sectotal'};
                   2486:     my $crosslisttotal = 0;
1.10      raeburn  2487:     $cnum = $env{'form.cnum'};
1.8       raeburn  2488:     unless ($cnum =~ /^$match_courseid$/) {
                   2489:         $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
                   2490:         return $output;
                   2491:     }
1.11      raeburn  2492: 
1.10      raeburn  2493:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
1.9       raeburn  2494:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   2495:         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
                   2496:             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
                   2497:         }
                   2498:     }
1.10      raeburn  2499:     $now = time;
                   2500:     $crstype = $env{'form.crstype'};
1.17      raeburn  2501:     my @instsections;
1.8       raeburn  2502:     if ($crstype eq 'official') {
                   2503:         if (&Apache::lonnet::auto_run('',$dom)) {
1.13      raeburn  2504:             ($enrollstart,$enrollend)=&dates_from_form('enrollstart','enrollend');
1.8       raeburn  2505:         }
1.10      raeburn  2506:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   2507:             if ($env{'form.sec_'.$i}) {
                   2508:                 if ($env{'form.secnum_'.$i} ne '') {
1.17      raeburn  2509:                     my $sec = $env{'form.secnum_'.$i};
                   2510:                     $sections{$i}{'inst'} = $sec;
                   2511:                     if (($sec ne '') && (!grep(/^\Q$sec\E$/,@instsections))) {
                   2512:                         push(@instsections,$sec);
                   2513:                     }
1.13      raeburn  2514:                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
1.10      raeburn  2515:                 }
                   2516:             }
                   2517:         }
                   2518:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                   2519:             if ($env{'form.crosslist_'.$i}) {
                   2520:                 my $xlistinfo = '';
                   2521:                 if (ref($code_order) eq 'ARRAY') {
                   2522:                     if (@{$code_order} > 0) {
                   2523:                         foreach my $item (@{$code_order}) {
                   2524:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
                   2525:                         }
                   2526:                     }
                   2527:                 }
1.22      raeburn  2528:                 $crosslistings{$i}{'instcode'} = $xlistinfo;
1.24      raeburn  2529:                 if ($xlistinfo ne '') {
                   2530:                     $crosslisttotal ++;
                   2531:                 }
1.22      raeburn  2532:                 $crosslistings{$i}{'instsec'} = $env{'form.crosslist_'.$i.'_instsec'}; 
1.13      raeburn  2533:                 $crosslistings{$i}{'loncapa'} = $env{'form.crosslist_'.$i.'_lcsec'};
1.10      raeburn  2534:             }
                   2535:         }
1.14      raeburn  2536:     } else {
                   2537:         $enrollstart = '';
                   2538:         $enrollend = '';
1.10      raeburn  2539:     }
                   2540:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   2541:         my $uname = $env{'form.person_'.$i.'_uname'};
1.16      raeburn  2542:         my $udom = $env{'form.person_'.$i.'_dom'};
1.10      raeburn  2543:         if (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
                   2544:             if (&Apache::lonnet::domain($udom) ne '') {
1.13      raeburn  2545:                 unless (ref($personnel{$uname.':'.$udom}) eq 'HASH') {
                   2546:                     $personnel{$uname.':'.$udom} = {
                   2547:                              firstname    => $env{'form.person_'.$i.'_firstname'},
                   2548:                              lastname     => $env{'form.person_'.$i.'_lastname'},
                   2549:                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
                   2550:                                                    };
                   2551:                 }
                   2552:                 my $role = $env{'form.person_'.$i.'_role'};
                   2553:                 unless ($role eq '') {
1.16      raeburn  2554:                     if (ref($personnel{$uname.':'.$udom}{'roles'}) eq 'ARRAY') {
1.13      raeburn  2555:                         my @curr_roles = @{$personnel{$uname.':'.$udom}{'roles'}};
                   2556:                         unless (grep(/^\Q$role\E$/,@curr_roles)) {
                   2557:                             push(@{$personnel{$uname.':'.$udom}{'roles'}},$role);
                   2558:                         }
                   2559:                     } else {
                   2560:                         @{$personnel{$uname.':'.$udom}{'roles'}} = ($role);
                   2561:                     }
                   2562:                     if ($role eq 'cc') {
                   2563:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
                   2564:                     } else {
1.14      raeburn  2565:                         my @currsec = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
1.31      raeburn  2566:                         my @allsecs;
                   2567:                         foreach my $sec (@currsec) {
                   2568:                             next unless ($sec =~ /\w/);
                   2569:                             next if ($sec =~ /\W/);
                   2570:                             next if ($sec eq 'none');
                   2571:                             push(@allsecs,$sec);
                   2572:                         }
1.14      raeburn  2573:                         my $newsec = $env{'form.person_'.$i.'_newsec'};
                   2574:                         $newsec =~ s/^\s+//;
                   2575:                         $newsec =~s/\s+$//;
                   2576:                         my @newsecs = split(/[\s,;]+/,$newsec);
                   2577:                         foreach my $sec (@newsecs) {
                   2578:                             next if ($sec =~ /\W/);
1.31      raeburn  2579:                             next if ($sec eq 'none');
1.14      raeburn  2580:                             if ($sec ne '') {
1.31      raeburn  2581:                                 unless (grep(/^\Q$sec\E$/,@allsecs)) {
                   2582:                                     push(@allsecs,$sec);
1.13      raeburn  2583:                                 }
                   2584:                             }
                   2585:                         }
1.31      raeburn  2586:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @allsecs;
1.13      raeburn  2587:                     }
                   2588:                 }
1.10      raeburn  2589:             } else {
                   2590:                 push(@missingdom,$uname.':'.$udom);
                   2591:             }
                   2592:         } else {
                   2593:             push(@baduname,$uname.':'.$udom);
                   2594:         }
1.8       raeburn  2595:     }
1.13      raeburn  2596:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
1.14      raeburn  2597:     my $autodrops = 0;
                   2598:     if ($env{'form.autodrops'}) {
                   2599:         $autodrops = $env{'form.autodrops'}; 
                   2600:     }
                   2601:     my $autoadds = 0;
                   2602:     if ($env{'form.autoadds'}) {
                   2603:         $autodrops = $env{'form.autoadds'};
                   2604:     }
                   2605:     if ($env{'form.autoadds'}) {
                   2606:         $autodrops = $env{'form.autoadds'};
                   2607:     }
                   2608:     my $instcode = '';
                   2609:     if (exists($env{'form.instcode'})) {
                   2610:         $instcode = $env{'form.instcode'};
                   2611:     }
1.15      raeburn  2612:     my $clonecrs = '';
                   2613:     my $clonedom = '';
                   2614:     if (($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
                   2615:         ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
1.16      raeburn  2616:         my $clonehome = &Apache::lonnet::homeserver($env{'form.clonecrs'},
                   2617:                                                     $env{'form.clonedom'});
1.15      raeburn  2618:         if ($clonehome ne 'no_host') {  
1.16      raeburn  2619:             my $canclone =  
                   2620:                 &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
                   2621:                         $env{'user.domain'},$env{'form.clonecrs'}, $env{'form.clonedom'});
1.15      raeburn  2622:             if ($canclone) {
                   2623:                 $clonecrs = $env{'form.clonecrs'};
                   2624:                 $clonedom = $env{'form.clonedom'};
                   2625:             }
                   2626:         }
                   2627:     }
1.8       raeburn  2628:     my $details = {
1.10      raeburn  2629:                     owner          => $env{'user.name'},
                   2630:                     domain         => $env{'user.domain'}, 
                   2631:                     cdom           => $dom,
1.11      raeburn  2632:                     cnum           => $cnum,
1.13      raeburn  2633:                     coursehome     => $env{'form.chome'},
                   2634:                     cdescr         => $env{'form.cdescr'},
1.10      raeburn  2635:                     crstype        => $env{'form.crstype'},
1.14      raeburn  2636:                     instcode       => $instcode,
1.15      raeburn  2637:                     clonedom       => $clonedom,
                   2638:                     clonecrs       => $clonecrs,
1.10      raeburn  2639:                     datemode       => $env{'form.datemode'},
1.14      raeburn  2640:                     dateshift      => $env{'form.dateshift'},
                   2641:                     sectotal       => $sectotal,
1.10      raeburn  2642:                     sections       => \%sections,
1.14      raeburn  2643:                     crosslisttotal => $crosslisttotal,
1.13      raeburn  2644:                     crosslists     => \%crosslistings,
1.14      raeburn  2645:                     autoadds       => $autoadds,
                   2646:                     autodrops      => $autodrops,
1.13      raeburn  2647:                     enrollstart    => $enrollstart,
                   2648:                     enrollend      => $enrollend,
                   2649:                     accessstart    => $accessstart,
                   2650:                     accessend      => $accessend,
1.10      raeburn  2651:                     personnel      => \%personnel,
1.8       raeburn  2652:                   };
1.27      raeburn  2653:     my (@inststatuses,$storeresult,$creationresult);
1.9       raeburn  2654:     my $val = &get_processtype($dom,$crstype,\@inststatuses,\%domconfig);
1.8       raeburn  2655:     if ($val eq '') {
                   2656:         if ($crstype eq 'official') {
1.19      raeburn  2657:             $output = &mt('You are not permitted to request creation of official courses.');
1.8       raeburn  2658:         } elsif ($crstype eq 'unofficial') {
1.19      raeburn  2659:             $output = &mt('You are not permitted to request creation of unofficial courses.');
1.8       raeburn  2660:         } elsif ($crstype eq 'community') {
                   2661:             $output = &mt('You are not permitted to request creation of communities');
                   2662:         } else {
                   2663:             $output = &mt('Unrecognized course type: [_1]',$crstype);
                   2664:         }
1.27      raeburn  2665:         $storeresult = 'notpermitted'; 
1.8       raeburn  2666:     } else {
1.14      raeburn  2667:         my ($disposition,$message,$reqstatus);
1.8       raeburn  2668:         my %reqhash = (
1.14      raeburn  2669:                         reqtime   => $now,
1.10      raeburn  2670:                         crstype   => $crstype,
                   2671:                         details   => $details,
1.8       raeburn  2672:                       );
                   2673:         my $requestkey = $dom.'_'.$cnum;
1.17      raeburn  2674:         my $validationerror;
1.10      raeburn  2675:         if ($val eq 'autolimit=') {
                   2676:             $disposition = 'process';
                   2677:         } elsif ($val =~ /^autolimit=(\d+)$/) {
                   2678:             my $limit = $1;
1.8       raeburn  2679:             $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
1.10      raeburn  2680:                                             $dom,$crstype,$limit,\$message);
1.8       raeburn  2681:         } elsif ($val eq 'validate') {
1.21      raeburn  2682:             my ($inststatuslist,$validationchk,$validation);
1.17      raeburn  2683:             if (@inststatuses > 0) {
                   2684:                 $inststatuslist = join(',',@inststatuses);
                   2685:             }
                   2686:             my $instseclist;
                   2687:             if (@instsections > 0) {
                   2688:                 $instseclist = join(',',@instsections);
                   2689:             }
1.21      raeburn  2690:             $validationchk = 
                   2691:                 &Apache::lonnet::auto_courserequest_validation($dom,
                   2692:                     $env{'user.name'}.':'.$env{'user.domain'},$crstype,
                   2693:                     $inststatuslist,$instcode,$instseclist);
                   2694:             if ($validationchk =~ /:/) {
                   2695:                 ($validation,$message) = split(':',$validationchk);
                   2696:             } else {
                   2697:                 $validation = $validationchk;
                   2698:             }
                   2699:             if ($validation =~ /^error(.*)$/) {
1.17      raeburn  2700:                 $disposition = 'approval';
                   2701:                 $validationerror = $1;
1.23      raeburn  2702:             } else {
                   2703:                 $disposition = $validation;
1.17      raeburn  2704:             }
1.8       raeburn  2705:         } else {
                   2706:             $disposition = 'approval';
                   2707:         }
1.14      raeburn  2708:         $reqhash{'disposition'} = $disposition;
                   2709:         $reqstatus = $disposition;
1.16      raeburn  2710:         my ($modified,$queued);
1.8       raeburn  2711:         if ($disposition eq 'rejected') {
                   2712:             $output = &mt('Your course request was rejected.');
                   2713:             if ($message) {
                   2714:                 $output .= '<div class="LC_warning">'.$message.'</div>';
                   2715:             }
1.27      raeburn  2716:             $storeresult = 'rejected';
1.8       raeburn  2717:         } elsif ($disposition eq 'process') {
1.14      raeburn  2718:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
                   2719:             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles);
                   2720:             my @roles = &Apache::lonuserutils::roles_by_context('course');
1.8       raeburn  2721:             my $type = 'Course';
                   2722:             if ($crstype eq 'community') {
                   2723:                 $type = 'Community';
                   2724:             }
                   2725:             foreach my $role (@roles) {
                   2726:                 $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
                   2727:             }
1.14      raeburn  2728:             my $result = &Apache::loncoursequeueadmin::course_creation($dom,$cnum,
                   2729:                                    'autocreate',$details,\$logmsg,\$newusermsg,\$addresult,
                   2730:                                    \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
                   2731:             if ($result eq 'created') {
1.8       raeburn  2732:                 $disposition = 'created';
1.14      raeburn  2733:                 $reqstatus = 'created';
1.28      raeburn  2734:                 my $role_result = &update_requestors_roles($dom,$cnum,$crstype,$details,
                   2735:                                                            \%longroles);
                   2736:                 $output = '<p>'.&mt('Your course request has been processed and the course has been created.').
                   2737:                           '<br />'.$role_result.'</p>';
1.27      raeburn  2738:                 $creationresult = 'created';
1.8       raeburn  2739:             } else {
1.14      raeburn  2740:                 $output = '<span class="LC_error">'.
                   2741:                           &mt('An error occurred when processing your course request.').
                   2742:                           '<br />'.
                   2743:                           &mt('You may want to review the request details and submit the request again.').
                   2744:                           '</span>';
1.27      raeburn  2745:                 $creationresult = 'error';
1.8       raeburn  2746:             }
                   2747:         } else {
                   2748:             my $requestid = $cnum.'_'.$disposition;
                   2749:             my $request = { 
                   2750:                             $requestid => {
                   2751:                                             timestamp   => $now,
                   2752:                                             crstype     => $crstype,
                   2753:                                             ownername   => $env{'user.name'},
                   2754:                                             ownerdom    => $env{'user.domain'},
1.13      raeburn  2755:                                             description => $env{'form.cdescr'}, 
1.8       raeburn  2756:                                           },
                   2757:                           };
1.16      raeburn  2758:             my $statuskey = 'status:'.$dom.':'.$cnum;
                   2759:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
                   2760:                                                    $env{'user.domain'},$env{'user.name'});
1.17      raeburn  2761:             if ($userreqhash{$statuskey} ne '') {
1.16      raeburn  2762:                 $modified = 1;
1.25      raeburn  2763:                 my $uname = &Apache::lonnet::get_domainconfiguser($dom);
                   2764:                 my %queuehash = &Apache::lonnet::get('courserequestqueue',
                   2765:                                                      [$cnum.'_approval',
                   2766:                                                       $cnum.'_pending'],$dom,$uname);
1.17      raeburn  2767:                 if (($queuehash{$cnum.'_approval'} ne '') || 
                   2768:                     ($queuehash{$cnum.'_pending'} ne '')) {
1.16      raeburn  2769:                     $queued = 1;
                   2770:                 }
                   2771:             }
                   2772:             unless ($queued) {
                   2773:                 my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
                   2774:                                                             $dom);
                   2775:                 if ($putresult eq 'ok') {
                   2776:                     $output = &mt('Your course request has been recorded.').'<br />'.
                   2777:                               &notification_information($disposition,$req_notifylist,
                   2778:                                                         $cnum,$now);
1.8       raeburn  2779:                 } else {
1.16      raeburn  2780:                     $reqstatus = 'domainerror';
                   2781:                     $reqhash{'disposition'} = $disposition;
                   2782:                     my $warning = &mt('An error occurred saving your request in the pending requests queue.');
                   2783:                     $output = '<span class"LC_warning">'.$warning.'</span><br />';
1.8       raeburn  2784:                 }
                   2785:             }
                   2786:         }
1.27      raeburn  2787:         my ($statusresult);
1.10      raeburn  2788:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2789:             $storeresult = &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                   2790:                                                            'courserequests');
1.14      raeburn  2791:             if ($storeresult eq 'ok') {
                   2792:                 my %status = (
                   2793:                                  'status:'.$dom.':'.$cnum => $reqstatus,
                   2794:                              );
1.27      raeburn  2795:                 $statusresult = &Apache::lonnet::put('courserequests',\%status);
1.14      raeburn  2796:             }
1.10      raeburn  2797:         } else {
                   2798:             $storeresult = 'error: invalid requestkey format'; 
                   2799:         }
1.8       raeburn  2800:         if ($storeresult ne 'ok') {
1.13      raeburn  2801:             $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the details of your request: [_1].',$storeresult).'</span><br />';
                   2802:             &Apache::lonnet::logthis("Error saving course request - $requestkey for $env{'user.name'}:$env{'user.domain'} - $storeresult");
1.14      raeburn  2803:         } elsif ($statusresult ne 'ok') {
1.28      raeburn  2804:             $output .= '<span class="LC_warning">'.&mt('An error occurred saving a record of the status of your request: [_1].',$statusresult).'</span><br />';
1.14      raeburn  2805:             &Apache::lonnet::logthis("Error saving course request status for  $requestkey (for $env{'user.name'}:$env{'user.domain'}) - $statusresult");
1.8       raeburn  2806:         }
1.16      raeburn  2807:         if ($modified && $queued && $storeresult eq 'ok') {
                   2808:             $output .= '<p>'.&mt('Your course request has been updated').'</p>'.
                   2809:                        &notification_information($disposition,$req_notifylist,$cnum,$now);
                   2810:         }
1.17      raeburn  2811:         if ($validationerror ne '') {
1.19      raeburn  2812:             $output .= '<span class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: [_1].',$validationerror).'</p>';
1.17      raeburn  2813:         }
1.16      raeburn  2814:     }
1.27      raeburn  2815:     if ($creationresult ne '') {
                   2816:         return ($creationresult,$output);
                   2817:     } else {
                   2818:         return ($storeresult,$output);
                   2819:     }
1.16      raeburn  2820: }
                   2821: 
1.28      raeburn  2822: sub update_requestors_roles {
                   2823:     my ($dom,$cnum,$crstype,$details,$longroles) = @_;
                   2824:     my $now = time;
                   2825:     my ($active,$future,$numactive,$numfuture,$output);
                   2826:     my $owner = $env{'user.name'}.':'.$env{'user.domain'};
                   2827:     if (ref($details) eq 'HASH') {
                   2828:         if (ref($details->{'personnel'}) eq 'HASH') {
                   2829:             if (ref($details->{'personnel'}{$owner}) eq 'HASH') {
                   2830:                 my @roles;
                   2831:                 if (ref($details->{'personnel'}{$owner}{'roles'}) eq 'ARRAY') {
                   2832:                     @roles = sort(@{$details->{'personnel'}{$owner}{'roles'}});
                   2833:                     unless (grep(/^cc$/,@roles)) {
                   2834:                         push(@roles,'cc');
                   2835:                     }
                   2836:                 } else {
                   2837:                     @roles = ('cc');
                   2838:                 }
                   2839:                 foreach my $role (@roles) {
                   2840:                     my $start = $now;
                   2841:                     my $end = '0';
                   2842:                     if ($role eq 'st') {
                   2843:                         if ($details->{'accessstart'} ne '') {
                   2844:                             $start = $details->{'accessstart'};
                   2845:                         }
                   2846:                         if ($details->{'accessend'} ne '') {
                   2847:                             $end = $details->{'accessend'};
                   2848:                         }
                   2849:                     }
                   2850:                     my @usecs;
                   2851:                     if ($role ne 'cc') {
                   2852:                         if (ref($details->{'personnel'}{$owner}{$role}{'usec'}) eq 'ARRAY') {
                   2853:                             @usecs = @{$details->{'personnel'}{$owner}{$role}{'usec'}};
                   2854:                         }
                   2855:                     } 
                   2856:                     if ($role eq 'st') {
                   2857:                         if (@usecs > 1) {
                   2858:                             my $firstsec = $usecs[0];
                   2859:                             @usecs = ($firstsec);
                   2860:                         }
                   2861:                     }
                   2862:                     if (@usecs == 0) {
                   2863:                         push(@usecs,'');
                   2864:                     }
                   2865:                     foreach my $usec (@usecs) {
                   2866:                         my (%userroles,%newrole,%newgroups,$spec,$area);
                   2867:                         my $area = '/'.$dom.'/'.$cnum;
                   2868:                         my $spec = $role.'.'.$area;
                   2869:                         if ($usec ne '') {
                   2870:                            $spec .= '/'.$usec;
                   2871:                            $area .= '/'.$usec;
                   2872:                         }
                   2873:                         if ($role =~ /^cr\//) {
                   2874:                             &Apache::lonnet::custom_roleprivs(\%newrole,$role,$dom,
                   2875:                                                               $cnum,$spec,$area);
                   2876:                         } else {
                   2877:                             &Apache::lonnet::standard_roleprivs(\%newrole,$role,$dom,
                   2878:                                                                 $spec,$cnum,$area);
                   2879:                         }
                   2880:                         &Apache::lonnet::set_userprivs(\%userroles,\%newrole,
                   2881:                                                        \%newgroups);
                   2882:                         $userroles{'user.role.'.$spec} = $start.'.'.$end;
                   2883:                         &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
                   2884:                         if (($end == 0) || ($end > $now)) {
                   2885:                             my $showrole = $role;
                   2886:                             if ($role =~ /^cr\//) {
                   2887:                                 $showrole = &Apache::lonnet::plaintext($role,$crstype);
                   2888:                             } elsif (ref($longroles) eq 'HASH') {
                   2889:                                 if ($longroles->{$role} ne '') {
                   2890:                                     $showrole = $longroles->{$role};
                   2891:                                 }
                   2892:                             }
                   2893:                             if ($start <= $now) {
                   2894:                                 $active .= '<li><a href="/adm/roles?selectrole=1&'.                                             $spec.'=1">'.$showrole;
                   2895:                                 if ($usec ne '') {
                   2896:                                     $active .= ' - '.&mt('section:').' '.$usec; 
                   2897:                                 }
                   2898:                                 $active .= '</a></li>';
                   2899:                                 $numactive ++;
                   2900:                             } else { 
                   2901:                                 $future .= '<li>'.$showrole;
                   2902:                                 if ($usec ne '') {
                   2903:                                     $future .= ' - '.&mt('section:').' '.$usec;
                   2904:                                 }
                   2905:                                 $future .= '</li>';
                   2906:                                 $numfuture ++;
                   2907:                             }
                   2908:                         }
                   2909:                     }
                   2910:                 }
                   2911:             }
                   2912:         }
                   2913:     }
                   2914:     if ($active) {
                   2915:         if ($numactive == 1) {
                   2916:             $output = &mt('Use the following link to enter the course:'); 
                   2917:         } else {
                   2918:             $output = &mt('Use the following links to your new roles to enter the course:');
                   2919:         }
                   2920:         $output .= ' <ul>'.$active.'</ul><br />';
                   2921:     }
                   2922:     if ($future) {
                   2923:         $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'})).
                   2924:                    ' <ul>'.$future.'</ul>';
                   2925:     }
                   2926:     return $output;
                   2927: }
                   2928: 
1.16      raeburn  2929: sub notification_information {
                   2930:     my ($disposition,$req_notifylist,$cnum,$now) = @_;
                   2931:     my %emails = &Apache::loncommon::getemails();
                   2932:     my $address;
                   2933:     if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                   2934:         $address = $emails{'permanentemail'};
                   2935:         if ($address eq '') {
                   2936:             $address = $emails{'notification'};
                   2937:         }
                   2938:     }
                   2939:     my $output;
                   2940:     if ($disposition eq 'approval') {
                   2941:         $output .= &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
                   2942:                    &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
                   2943:         if ($address ne '') {
                   2944:             $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                   2945:         }
                   2946:         if ($req_notifylist) {
                   2947:             my $fullname = &Apache::loncommon::plainname($env{'user.name'},
                   2948:                                                                      $env{'user.domain'});
                   2949:             my $sender = $env{'user.name'}.':'.$env{'user.domain'};
                   2950:             &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,"$fullname ($env{'user.name'}:$env{'user.domain'})",$cnum,$env{'form.cdescr'},$now,'coursereq',$sender);
                   2951:         }
1.17      raeburn  2952:     } elsif ($disposition eq 'pending') {
1.16      raeburn  2953:         $output .= '<div class="LC_info">'.
                   2954: &mt('Your request has been placed in a queue pending administrative action.').'<br />'.
                   2955: &mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
                   2956: &mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
                   2957:                    '</div>';
1.17      raeburn  2958:     } else {
                   2959:         $output .= '<div class="LC_warning">'.
                   2960:                    &mt('Your request status is: [_1].',$disposition). 
                   2961:                    '</div>'
1.8       raeburn  2962:     }
                   2963:     return $output;
                   2964: }
                   2965: 
                   2966: sub get_processtype {
1.9       raeburn  2967:     my ($dom,$crstype,$inststatuses,$domconfig) = @_;
                   2968:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
1.8       raeburn  2969:     my (%userenv,%settings,$val);
1.19      raeburn  2970:     my @options = ('autolimit','validate','approval');
1.8       raeburn  2971:     if ($dom eq $env{'user.domain'}) {
                   2972:         %userenv = 
                   2973:             &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
                   2974:                 'requestcourses.'.$crstype,'inststatus');
                   2975:         if ($userenv{'requestcourses.'.$crstype}) {
                   2976:             $val = $userenv{'requestcourses.'.$crstype};
                   2977:             @{$inststatuses} = ('_custom_');
                   2978:         } else {
                   2979:             my ($task,%alltasks);
1.9       raeburn  2980:             if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
                   2981:                 %settings = %{$domconfig->{'requestcourses'}};
1.8       raeburn  2982:                 if (ref($settings{$crstype}) eq 'HASH') {
1.23      raeburn  2983:                     if (($env{'user.adv'}) && ($settings{$crstype}{'_LC_adv'} ne '')) {
1.8       raeburn  2984:                         $val = $settings{$crstype}{'_LC_adv'};
                   2985:                         @{$inststatuses} = ('_LC_adv_');
                   2986:                     } else {
                   2987:                         if ($userenv{'inststatus'} ne '') {
                   2988:                             @{$inststatuses} = split(',',$userenv{'inststatus'});
                   2989:                         } else {
1.13      raeburn  2990:                             @{$inststatuses} = ('default');
1.8       raeburn  2991:                         }
                   2992:                         foreach my $status (@{$inststatuses}) {
                   2993:                             if (exists($settings{$crstype}{$status})) {
                   2994:                                 my $value = $settings{$crstype}{$status};
                   2995:                                 next unless ($value); 
                   2996:                                 unless (exists($alltasks{$value})) {
                   2997:                                     if (ref($alltasks{$value}) eq 'ARRAY') {
                   2998:                                         unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                   2999:                                             push(@{$alltasks{$value}},$status); 
                   3000:                                         }
                   3001:                                     } else {
                   3002:                                         @{$alltasks{$value}} = ($status);
                   3003:                                     }
                   3004:                                 }
                   3005:                             }
                   3006:                         }
                   3007:                         my $maxlimit = 0;
1.13      raeburn  3008:                         
1.8       raeburn  3009:                         foreach my $key (sort(keys(%alltasks))) {
                   3010:                             if ($key =~ /^autolimit=(\d*)$/) {
                   3011:                                 if ($1 eq '') {
                   3012:                                     $val ='autolimit=';
                   3013:                                     last;
                   3014:                                 } elsif ($1 > $maxlimit) {
                   3015:                                     $maxlimit = $1; 
                   3016:                                 }
                   3017:                             }
                   3018:                         }
                   3019:                         if ($maxlimit) {
                   3020:                             $val = 'autolimit='.$maxlimit;
                   3021:                         } else {
                   3022:                             foreach my $option (@options) {
                   3023:                                 if ($alltasks{$option}) {
                   3024:                                     $val = $option;
                   3025:                                     last;  
                   3026:                                 }
                   3027:                             }
                   3028:                         }
                   3029:                     }
                   3030:                 }
                   3031:             }
                   3032:         }
                   3033:     } else {
                   3034:         %userenv = &Apache::lonnet::userenvironment($env{'user.domain'},
                   3035:                       $env{'user.name'},'reqcrsotherdom.'.$env{'form.crstype'});
1.19      raeburn  3036:         if ($userenv{'reqcrsotherdom.'.$crstype}) {
                   3037:             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
                   3038:             my $optregex = join('|',@options);
                   3039:             foreach my $item (@doms) {
                   3040:                 my ($extdom,$extopt) = split(':',$item);
                   3041:                 if ($extdom eq $dom) {  
                   3042:                     if ($extopt =~ /^($optregex)(=?\d*)$/) {
                   3043:                         $val = $1.$2;
                   3044:                     }
                   3045:                     last;
                   3046:                 }
1.8       raeburn  3047:             }
                   3048:             @{$inststatuses} = ('_external_');
                   3049:         }
                   3050:     }
                   3051:     return $val;
                   3052: }
                   3053: 
                   3054: sub check_autolimit {
1.10      raeburn  3055:     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
                   3056:     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
                   3057:                                         'userroles',['active','future'],['cc'],[$dom]);
                   3058:     my ($types,$typename) = &course_types();
                   3059:     my %requests = &Apache::lonnet::dumpstore('courserequests',$udom,$uname);
                   3060:     my %count;
                   3061:     if (ref($types) eq 'ARRAY') {
                   3062:         foreach my $type (@{$types}) {
                   3063:             $count{$type} = 0;
                   3064:         }
                   3065:     }
                   3066:     foreach my $key (keys(%requests)) {
                   3067:         my ($cdom,$cnum) = split('_',$key);
                   3068:         if (exists($crsroles{$cnum.':'.$cdom.':cc'})) {
                   3069:             if (ref($requests{$key}) eq 'HASH') {
                   3070:                 my $type = $requests{$key}{'crstype'};
                   3071:                 if ($type =~ /^official|unofficial|community$/) {
                   3072:                     $count{$type} ++;
                   3073:                 }
                   3074:             }
                   3075:         }
                   3076:     }
                   3077:     if ($count{$crstype} < $limit) {
                   3078:         return 'process';
                   3079:     } else {
                   3080:         if (ref($typename) eq 'HASH') {
                   3081:             $$message = &mt('Your request has not been processed because you have reached the limit for the number of courses of this type.').'<br />'.&mt("Your $typename->{$crstype} limit is [_1].",$limit);
                   3082:         }
                   3083:         return 'rejected';
                   3084:     }
1.1       raeburn  3085:     return;
                   3086: }
                   3087: 
1.2       raeburn  3088: sub retrieve_settings {
1.26      raeburn  3089:     my ($dom,$cnum,$udom,$uname) = @_;
                   3090:     if ($udom eq '' || $uname eq '') {
                   3091:         $udom = $env{'user.domain'};
                   3092:         $uname = $env{'user.name'};
                   3093:     }
                   3094:     my ($result,%reqinfo) = &get_request_settings($dom,$cnum,$udom,$uname);
1.16      raeburn  3095:     if ($result eq 'ok') {
1.26      raeburn  3096:         if (($udom eq $reqinfo{'domain'}) &&  ($uname eq $reqinfo{'owner'})) {
1.16      raeburn  3097:             $env{'form.chome'} = $reqinfo{'coursehome'};
                   3098:             $env{'form.cdescr'} = $reqinfo{'cdescr'};
                   3099:             $env{'form.crstype'} = $reqinfo{'crstype'}; 
                   3100:             &generate_date_items($reqinfo{'accessstart'},'accessstart');
                   3101:             &generate_date_items($reqinfo{'accessend'},'accessend');
                   3102:             if ($reqinfo{'accessend'} == 0) {
                   3103:                 $env{'form.no_end_date'} = 1;
                   3104:             }
                   3105:             if (($reqinfo{'crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
                   3106:                 &generate_date_items($reqinfo{'enrollstart'},'enrollstart');
                   3107:                 &generate_date_items($reqinfo{'enrollend'},'enrollend');
                   3108:             }
                   3109:             $env{'form.clonecrs'} = $reqinfo{'clonecrs'};
                   3110:             $env{'form.clonedom'} = $reqinfo{'clonedom'};
                   3111:             $env{'form.datemode'} = $reqinfo{'datemode'};
                   3112:             $env{'form.dateshift'} = $reqinfo{'dateshift'};
                   3113:             if (($reqinfo{'crstype'} eq 'official') && ($reqinfo{'instcode'} ne '')) { 
                   3114:                  $env{'form.sectotal'} = $reqinfo{'sectotal'};
                   3115:                  $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
                   3116:                  $env{'form.autoadds'} = $reqinfo{'autoadds'};
                   3117:                  $env{'form.autdrops'} = $reqinfo{'autodrops'};
                   3118:                  $env{'form.instcode'} = $reqinfo{'instcode'};
1.24      raeburn  3119:                  my $crscode = { 
                   3120:                                  $cnum => $reqinfo{'instcode'},
                   3121:                                };
                   3122:                  &extract_instcode($dom,'instcode',$crscode,$cnum);
1.16      raeburn  3123:             }
                   3124:             my @currsec;
                   3125:             if (ref($reqinfo{'sections'}) eq 'HASH') {
                   3126:                 foreach my $i (sort(keys(%{$reqinfo{'sections'}}))) {
                   3127:                     if (ref($reqinfo{'sections'}{$i}) eq 'HASH') {
1.24      raeburn  3128:                         my $sec = $reqinfo{'sections'}{$i}{'inst'};
1.16      raeburn  3129:                         $env{'form.secnum_'.$i} = $sec;
1.24      raeburn  3130:                         $env{'form.sec_'.$i} = '1';
1.16      raeburn  3131:                         if (!grep(/^\Q$sec\E$/,@currsec)) {
                   3132:                             push(@currsec,$sec);
                   3133:                         }
                   3134:                         $env{'form.loncapasec_'.$i} = $reqinfo{'sections'}{$i}{'loncapa'};
                   3135:                     }
                   3136:                 }
                   3137:             }
1.24      raeburn  3138:             if (ref($reqinfo{'crosslists'}) eq 'HASH') {
                   3139:                 foreach my $i (sort(keys(%{$reqinfo{'crosslists'}}))) {
                   3140:                     if (ref($reqinfo{'crosslists'}{$i}) eq 'HASH') {
                   3141:                         $env{'form.crosslist_'.$i} = '1';
                   3142:                         $env{'form.crosslist_'.$i.'_instsec'} = $reqinfo{'crosslists'}{$i}{'instsec'};
                   3143:                         $env{'form.crosslist_'.$i.'_lcsec'} = $reqinfo{'crosslists'}{$i}{'loncapa'};
                   3144:                         if ($reqinfo{'crosslists'}{$i}{'instcode'} ne '') {
                   3145:                             my $key = $cnum.$i; 
                   3146:                             my $crscode = {
                   3147:                                               $key => $reqinfo{'crosslists'}{$i}{'instcode'},
                   3148:                                           };
                   3149:                             &extract_instcode($dom,'crosslist',$crscode,$key,$i);
                   3150:                         }
1.16      raeburn  3151:                     }
                   3152:                 }
                   3153:             }
                   3154:             if (ref($reqinfo{'personnel'}) eq 'HASH') {
                   3155:                 my $i = 0;
                   3156:                 foreach my $user (sort(keys(%{$reqinfo{'personnel'}}))) {
                   3157:                     my ($uname,$udom) = split(':',$user);
                   3158:                     if (ref($reqinfo{'personnel'}{$user}) eq 'HASH') {
                   3159:                         if (ref($reqinfo{'personnel'}{$user}{'roles'}) eq 'ARRAY') {
                   3160:                             foreach my $role (sort(@{$reqinfo{'personnel'}{$user}{'roles'}})) {
                   3161:                                 $env{'form.person_'.$i.'_role'} = $role;
                   3162:                                 $env{'form.person_'.$i.'_firstname'} = $reqinfo{'personnel'}{$user}{'firstname'};
                   3163:                                 $env{'form.person_'.$i.'_lastname'} = $reqinfo{'personnel'}{$user}{'lastname'}; ;
                   3164:                                 $env{'form.person_'.$i.'_emailaddr'} = $reqinfo{'personnel'}{$user}{'emailaddr'};
                   3165:                                 $env{'form.person_'.$i.'_uname'} = $uname;
                   3166:                                 $env{'form.person_'.$i.'_dom'} = $udom;
                   3167:                                 if (ref($reqinfo{'personnel'}{$user}{$role}) eq 'HASH') {
                   3168:                                     if (ref($reqinfo{'personnel'}{$user}{$role}{'usec'}) eq 'ARRAY') {
                   3169:                                         my @usecs = @{$reqinfo{'personnel'}{$user}{$role}{'usec'}};
                   3170:                                         my @newsecs;
                   3171:                                         if (@usecs > 0) {
                   3172:                                             foreach my $sec (@usecs) {
                   3173:                                                 if (grep(/^\Q$sec\E/,@currsec)) {
                   3174:                                                     $env{'form.person_'.$i.'_sec'} = $sec;
                   3175:                                                 } else {
1.20      raeburn  3176:                                                     push(@newsecs,$sec);
1.16      raeburn  3177:                                                 }
                   3178:                                             }
                   3179:                                         }
                   3180:                                         if (@newsecs > 0) {
                   3181:                                             $env{'form.person_'.$i.'_newsec'} = join(',',@newsecs); 
                   3182:                                         }
                   3183:                                     }
                   3184:                                 }
                   3185:                                 $i ++;
                   3186:                             }
                   3187:                         }
                   3188:                     }
                   3189:                 }
                   3190:                 $env{'form.persontotal'} = $i;
                   3191:             }
                   3192:         }
                   3193:     }
                   3194:     return $result;
                   3195: }
                   3196: 
                   3197: sub get_request_settings {
1.26      raeburn  3198:     my ($dom,$cnum,$udom,$uname) = @_;
1.16      raeburn  3199:     my $requestkey = $dom.'_'.$cnum;
                   3200:     my ($result,%reqinfo);
                   3201:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
1.26      raeburn  3202:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',$udom,$uname);
1.16      raeburn  3203:         my $disposition = $history{'disposition'};
                   3204:         if (($disposition eq 'approval') || ($disposition eq 'pending')) { 
                   3205:             if (ref($history{'details'}) eq 'HASH') {
                   3206:                 %reqinfo = %{$history{'details'}};
                   3207:                 $result = 'ok';
                   3208:             } else {
                   3209:                 $result = 'nothash';
                   3210:             }
                   3211:         } else {
                   3212:             $result = 'notqueued';
                   3213:         }
                   3214:     } else {
                   3215:         $result = 'invalid';
                   3216:     }
                   3217:     return ($result,%reqinfo);
                   3218: }
1.2       raeburn  3219: 
1.16      raeburn  3220: sub extract_instcode {
1.24      raeburn  3221:     my ($cdom,$element,$crscode,$crskey,$counter) = @_;
1.16      raeburn  3222:     my (%codes,@codetitles,%cat_titles,%cat_order);
1.24      raeburn  3223:     if (&Apache::lonnet::auto_instcode_format('requests',$cdom,$crscode,\%codes,
                   3224:                                               \@codetitles,\%cat_titles,
                   3225:                                               \%cat_order) eq 'ok') {
                   3226:         if (ref($codes{$crskey}) eq 'HASH') {
1.16      raeburn  3227:             if (@codetitles > 0) {
                   3228:                 my $sel = $element;
                   3229:                 if ($element eq 'crosslist') {
                   3230:                     $sel .= '_'.$counter;
                   3231:                 }
                   3232:                 foreach my $title (@codetitles) {
1.24      raeburn  3233:                     $env{'form.'.$sel.'_'.$title} = $codes{$crskey}{$title};
1.16      raeburn  3234:                 }
                   3235:             }
                   3236:         }
                   3237:     }
                   3238:     return;
1.2       raeburn  3239: }
                   3240: 
1.16      raeburn  3241: sub generate_date_items {
                   3242:     my ($currentval,$item) = @_;
                   3243:     if ($currentval =~ /\d+/) {
                   3244:         my ($tzname,$sec,$min,$hour,$mday,$month,$year) = 
                   3245:             &Apache::lonhtmlcommon::get_timedates($currentval);
                   3246:         $env{'form.'.$item.'_day'} = $mday;
                   3247:         $env{'form.'.$item.'_month'} = $month+1;
                   3248:         $env{'form.'.$item.'_year'} = $year;
                   3249:     }
                   3250:     return;
1.2       raeburn  3251: }
                   3252: 
1.1       raeburn  3253: 1;
                   3254: 

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