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

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

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