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

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

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