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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Request a course
                      3: #
1.18    ! raeburn     4: # $Id: lonrequestcourse.pm,v 1.17 2009/08/18 20:06:51 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.12      raeburn    47: =item header()
                     48: 
                     49: =item form_elements()
                     50: 
                     51: =item onload_action()
                     52: 
                     53: =item check_can_request() 
                     54: 
                     55: =item course_types()
                     56: 
                     57: =item print_main_menu()
                     58: 
                     59: =item request_administration()
                     60: 
                     61: =item print_request_form()
                     62: 
                     63: =item print_enrollment_menu()
                     64: 
                     65: =item inst_section_selector()
                     66: 
                     67: =item date_setting_table()
                     68: 
                     69: =item print_personnel_menu()
                     70: 
                     71: =item print_request_status()
                     72: 
                     73: =item print_request_logs()
                     74: 
                     75: =item print_review()
                     76: 
                     77: =item dates_from_form()
                     78: 
                     79: =item courseinfo_form()
                     80: 
                     81: =item clone_form()
                     82: 
                     83: =item clone_text()
                     84: 
                     85: =item coursecode_form()
                     86: 
                     87: =item get_course_dom()
                     88: 
                     89: =item display_navbuttons()
                     90: 
                     91: =item print_request_outcome()
                     92: 
                     93: =item get_processtype()
                     94: 
                     95: =item check_autolimit()
                     96: 
                     97: =item build_batchcreatehash()
                     98: 
                     99: =item retrieve_settings()
                    100: 
                    101: =item get_request_settings()
                    102: 
1.1       raeburn   103: =back
                    104: 
                    105: =cut
                    106: 
                    107: package Apache::lonrequestcourse;
                    108: 
                    109: use strict;
                    110: use Apache::Constants qw(:common :http);
                    111: use Apache::lonnet;
                    112: use Apache::loncommon;
                    113: use Apache::lonlocal;
1.8       raeburn   114: use Apache::loncoursequeueadmin;
1.4       raeburn   115: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn   116: 
                    117: sub handler {
                    118:     my ($r) = @_;
                    119:     if ($r->header_only) {
                    120:         &Apache::loncommon::content_type($r,'text/html');
                    121:         $r->send_http_header;
                    122:         return OK;
                    123:     }
                    124:     &Apache::loncommon::content_type($r,'text/html');
                    125:     $r->send_http_header;
                    126: 
1.2       raeburn   127:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    128:     my $dom = &get_course_dom();
1.1       raeburn   129:     my $action = $env{'form.action'};
                    130:     my $state = $env{'form.state'};
1.2       raeburn   131:     my %stored;
                    132:     my $jscript;
                    133:     if ((defined($state)) && (defined($action))) {
1.16      raeburn   134:         if (($action eq 'view') && ($state eq 'details')) {
                    135:             if ((defined($env{'form.showdom'})) && (defined($env{'form.cnum'}))) {
                    136:                 my $result = &retrieve_settings($env{'form.showdom'},$env{'form.cnum'});
1.2       raeburn   137:             }
                    138:         }
1.16      raeburn   139:         my %elements =  &form_elements($dom);
1.2       raeburn   140:         my $elementsref = {};
                    141:         if (ref($elements{$action}) eq 'HASH') {
                    142:             if (ref($elements{$action}{$state}) eq 'HASH') {
                    143:                 $elementsref = $elements{$action}{$state};
                    144:             }
                    145:         }
1.16      raeburn   146:         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
                    147:             $env{'form.clonedom'} = $dom;
                    148:         }
1.2       raeburn   149:         $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
                    150:     }
                    151: 
                    152:     if ($state eq 'personnel') {
                    153:         $jscript .= "\n".&Apache::loncommon::userbrowser_javascript();
                    154:     }
                    155: 
                    156:     my $loaditems = &onload_action($action,$state);
                    157: 
                    158:     my %states;
1.16      raeburn   159:     $states{'view'} = ['pick_request','details','cancel','removal'];
1.2       raeburn   160:     $states{'log'} = ['filter','display'];
                    161:     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
                    162:     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
                    163:         unless ($env{'form.state'} eq 'crstype') {
                    164:             unshift (@{$states{'new'}},'codepick');
                    165:         }
                    166:     }
                    167: 
                    168:     foreach my $key (keys(%states)) {
                    169:         if (ref($states{$key}) eq 'ARRAY') {
                    170:             unshift (@{$states{$key}},'crstype');
                    171:         }
                    172:     }
                    173: 
1.3       raeburn   174:     my %trail = (
1.10      raeburn   175:                  crstype       => 'Course Request Action',
                    176:                  codepick      => 'Category',
                    177:                  courseinfo    => 'Description',
1.16      raeburn   178:                  enrollment    => 'Access Dates',
1.10      raeburn   179:                  personnel     => 'Personnel',
                    180:                  review        => 'Review',
                    181:                  process       => 'Result',
                    182:                  pick_request  => 'Display Summary',
1.16      raeburn   183:                  details       => 'Request Details',
                    184:                  cancel        => 'Cancel Request',
                    185:                  removal       => 'Outcome',
1.3       raeburn   186:                 );
                    187: 
1.16      raeburn   188:     if (($env{'form.crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
                    189:         $trail{'enrollment'} = 'Enrollment';
                    190:     } 
                    191: 
1.2       raeburn   192:     my $page = 0;
1.3       raeburn   193:     my $crumb;
1.2       raeburn   194:     if (defined($action)) {
                    195:         my $done = 0;
                    196:         my $i=0;
                    197:         if (ref($states{$action}) eq 'ARRAY') {
                    198:             while ($i<@{$states{$action}} && !$done) {
                    199:                 if ($states{$action}[$i] eq $state) {
                    200:                     $page = $i;
                    201:                     $done = 1;
                    202:                 }
                    203:                 $i++;
                    204:             }
                    205:         }
1.3       raeburn   206:         for (my $i=0; $i<@{$states{$action}}; $i++) {
                    207:             if ($state eq $states{$action}[$i]) {
                    208:                 &Apache::lonhtmlcommon::add_breadcrumb(
                    209:                    {text=>"$trail{$state}"});
                    210:                 $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'); 
                    211:                 last;
                    212:             } else {
                    213:                 if (($state eq 'process') && ($i > 0)) {
                    214:                     &Apache::lonhtmlcommon::add_breadcrumb(
                    215:     {href=>"javascript:backPage(document.requestcrs,'$states{$action}[0]')",
                    216:      text=>"$trail{$states{$action}[$i]}"});
                    217:                 } else {
                    218:                     &Apache::lonhtmlcommon::add_breadcrumb(
                    219:      {href=>"javascript:backPage(document.requestcrs,'$states{$action}[$i]')",
                    220:       text=>"$trail{$states{$action}[$i]}"});
                    221:                 }
                    222:             }
                    223:         }
                    224:     } else {
                    225:         &Apache::lonhtmlcommon::add_breadcrumb(
                    226:                 {text=>'Pick Action'});
                    227:         $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
1.2       raeburn   228:     }
                    229: 
1.1       raeburn   230:     my %can_request;
                    231:     my $canreq = &check_can_request($dom,\%can_request);
                    232:     if ($action eq 'new') {
                    233:         if ($canreq) {
                    234:             if ($state eq 'crstype') {
1.3       raeburn   235:                 &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,
                    236:                                  $crumb);
1.1       raeburn   237:             } else {
1.2       raeburn   238:                 &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
1.3       raeburn   239:                                         $loaditems,$crumb);
1.1       raeburn   240:             }
                    241:         } else {
1.3       raeburn   242:             $r->print(&header('Course Requests').$crumb.
1.1       raeburn   243:                       '<div class="LC_warning">'.
                    244:                       &mt('You do not have privileges to request creation of courses.').
1.2       raeburn   245:                       '</div>'.&Apache::loncommon::end_page());
1.1       raeburn   246:         }
                    247:     } elsif ($action eq 'view') {
1.10      raeburn   248:         if ($state eq 'crstype') {
1.11      raeburn   249:            &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
                    250:         } else {
                    251:            &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
                    252:                                    $loaditems,$crumb);
1.10      raeburn   253:         }
1.1       raeburn   254:     } elsif ($action eq 'log') {
1.3       raeburn   255:         &print_request_logs($jscript,$loaditems,$crumb);
1.1       raeburn   256:     } else {
1.3       raeburn   257:         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
1.1       raeburn   258:     }
                    259:     return OK;
                    260: }
                    261: 
1.2       raeburn   262: sub header {
                    263:     my ($bodytitle,$jscript,$loaditems,$jsextra) = @_;
                    264:     if ($jscript) {
1.6       raeburn   265:         $jscript = '<script type="text/javascript">'."\n".
                    266:                    '// <![CDATA['."\n".
                    267:                    $jscript."\n".'// ]]>'."\n".'</script>'."\n";
1.2       raeburn   268:     }
                    269:     if ($loaditems) {
1.3       raeburn   270:         $loaditems = {'add_entries' => $loaditems,};
                    271:         return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra,$loaditems);
                    272:     } else {
                    273:         return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra);
                    274:     }
1.2       raeburn   275: }
                    276: 
                    277: sub form_elements {
                    278:     my ($dom) = @_;
                    279:     my %elements =
                    280:     (
                    281:         new => {
                    282:             crstype => {
                    283:                 crstype => 'selectbox',
                    284:                 action  => 'selectbox',
1.16      raeburn   285:                 origcnum => 'hidden', 
1.2       raeburn   286:             },
                    287:             courseinfo => {
                    288:                 cdescr           => 'text',
1.13      raeburn   289:                 clonecrs         => 'text',
                    290:                 clonedom         => 'selectbox',
1.2       raeburn   291:                 datemode         => 'radio',
                    292:                 dateshift        => 'text',
                    293:             },
                    294:             enrollment  => {
1.13      raeburn   295:                 accessstart_month  => 'selectbox',
                    296:                 accessstart_hour   => 'selectbox',
                    297:                 accessend_month    => 'selectbox',
                    298:                 accessend_hour     => 'selectbox',
                    299:                 accessstart_day    => 'text',
                    300:                 accessstart_year   => 'text',
                    301:                 accessstart_minute => 'text',
                    302:                 accessstart_second => 'text',
                    303:                 accessend_day      => 'text',
                    304:                 accessend_year     => 'text',
                    305:                 accessend_minute   => 'text',
                    306:                 accessend_second   => 'text',
1.2       raeburn   307:                 no_end_date      => 'checkbox',
                    308:             },
                    309:             personnel => {
                    310:                 addperson   => 'checkbox', 
                    311:             },
1.13      raeburn   312:             review => {
                    313:                 cnum => 'hidden',
                    314:             },
1.2       raeburn   315:          },
                    316:          view => {
                    317:             crstype => {
                    318:                 crstype => 'selectbox',
                    319:                 action  => 'selectbox',
                    320:             },
                    321:          },
                    322:     );
1.13      raeburn   323:     my %servers = &Apache::lonnet::get_servers($dom,'library');
                    324:     my $numlib = keys(%servers);
                    325:     if ($numlib > 1) {
                    326:         $elements{'new'}{'courseinfo'}{'chome'} = 'selectbox';
                    327:     } else {
                    328:         $elements{'new'}{'courseinfo'}{'chome'} = 'hidden';
                    329:     }
1.2       raeburn   330:     my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
                    331:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    332:                                              \%cat_order,\@code_order);
                    333:     my $numtitles = scalar(@codetitles);
                    334:     if ($numtitles) {
                    335:         my %extras;
                    336:         $lastitem = pop(@codetitles);
                    337:         $extras{'instcode_'.$lastitem} = 'text'; 
                    338:         foreach my $item (@codetitles) {
                    339:             $extras{'instcode_'.$item} = 'selectbox';   
                    340:         }
                    341:         $elements{'new'}{'codepick'} = \%extras;
                    342:     }
                    343:     if (&Apache::lonnet::auto_run('',$dom)) {
                    344:         my %extras = (
                    345:                        sectotal           => 'hidden',
1.13      raeburn   346:                        enrollstart_month  => 'selectbox',
                    347:                        enrollstart_hour   => 'selectbox',
                    348:                        enrollend_month    => 'selectbox',
                    349:                        enrollend_hour     => 'selectbox',
                    350:                        enrollstart_day    => 'text',
                    351:                        enrollstart_year   => 'text',
                    352:                        enrollstart_minute => 'text',
                    353:                        enrollstart_second => 'text',
                    354:                        enrollend_day      => 'text',
                    355:                        enrollend_year     => 'text',
                    356:                        enrollend_minute   => 'text',
                    357:                        enrollend_second   => 'text',
1.2       raeburn   358:                        crosslisttotal     => 'hidden',
                    359:                        addcrosslist       => 'checkbox',
                    360:                        autoadds           => 'radio',
                    361:                        autodrops          => 'radio',
                    362:                      );
                    363:         if ($env{'form.sectotal'} > 0) {
                    364:             for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                    365:                 $extras{'sec_'.$i} = 'checkbox',
                    366:                 $extras{'secnum_'.$i} = 'text',
                    367:                 $extras{'loncapasec_'.$i} = 'checkbox',
                    368:             }
                    369:         }
                    370:         my $crosslisttotal = $env{'form.crosslisttotal'};
1.16      raeburn   371:         if ($env{'form.addcrosslist'}) {
                    372:             $crosslisttotal ++;
                    373:         }
1.2       raeburn   374:         if (!defined($crosslisttotal)) {
                    375:             $crosslisttotal = 1;
                    376:         }
                    377:         if ($crosslisttotal > 0) {
                    378:             for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    379:                 if ($numtitles) {
                    380:                     $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
                    381:                 }
                    382:                 if (@codetitles > 0) {
                    383:                     foreach my $item (@codetitles) {
                    384:                         $extras{'crosslist_'.$i.'_'.$item} = 'selectbox';
                    385:                     }
                    386:                 }
                    387:                 $extras{'crosslist_'.$i} = 'checkbox';
                    388:                 $extras{'crosslist_'.$i.'_instsec'} = 'text',
                    389:                 $extras{'crosslist_'.$i.'_lcsec'} = 'text',
                    390:             }
                    391:         }
                    392:         my %mergedhash = (%{$elements{'new'}{'enrollment'}},%extras);
                    393:         %{$elements{'new'}{'enrollment'}} = %mergedhash;
                    394:     }
                    395:     my %people;
                    396:     my $persontotal = $env{'form.persontotal'};
1.16      raeburn   397:     if ($env{'form.addperson'}) {
                    398:         $persontotal ++;
                    399:     }
                    400:     if ((!defined($persontotal)) || (!$persontotal)) {
1.2       raeburn   401:         $persontotal = 1;
                    402:     }
                    403:     for (my $i=0; $i<$persontotal; $i++) {
1.13      raeburn   404:         $people{'person_'.$i.'_uname'}     = 'text',
                    405:         $people{'person_'.$i.'_dom'}       = 'selectbox',
                    406:         $people{'person_'.$i.'_hidedom'}   = 'hidden',
                    407:         $people{'person_'.$i.'_firstname'} = 'text',
                    408:         $people{'person_'.$i.'_lastname'}  = 'text',
                    409:         $people{'person_'.$i.'_emailaddr'} = 'text',
                    410:         $people{'person_'.$i.'_role'}      = 'selectbox',
                    411:         $people{'person_'.$i.'_sec'}       = 'selectbox',
                    412:         $people{'person_'.$i.'_newsec'}    = 'text',
1.2       raeburn   413:     }
                    414:     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
                    415:     %{$elements{'new'}{'personnel'}} = %personnelhash;
                    416:     return %elements;
                    417: }
                    418: 
                    419: sub onload_action {
                    420:     my ($action,$state) = @_;
                    421:     my %loaditems;
                    422:     if (($action eq 'new') || ($action eq 'view')) {
                    423:         $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
                    424:     }
                    425:     return \%loaditems;
                    426: }
                    427: 
1.1       raeburn   428: sub check_can_request {
                    429:     my ($dom,$can_request) = @_;
                    430:     my $canreq = 0;
1.4       raeburn   431:     my ($types,$typename) = &course_types();
                    432:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                    433:         foreach my $type (@{$types}) {
1.1       raeburn   434:             if (&Apache::lonnet::usertools_access($env{'user.name'},
                    435:                                                   $env{'user.domain'},
                    436:                                                   $type,undef,'requestcourses')) {
                    437:                 $canreq ++;
                    438:                 if ($dom eq $env{'user.domain'}) {
                    439:                     $can_request->{$type} = 1;
                    440:                 }
                    441:             }
                    442:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                    443:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                    444:                 if (@curr > 0) {
                    445:                     $canreq ++;
                    446:                     unless ($dom eq $env{'user.domain'}) {
                    447:                         if (grep(/^\Q$dom\E$/,@curr)) {
                    448:                             $can_request->{$type} = 1;
                    449:                         }
                    450:                     }
                    451:                 }
                    452:             }
                    453:         }
                    454:     }
                    455:     return $canreq;
                    456: }
                    457: 
1.4       raeburn   458: sub course_types {
                    459:     my @types = ('official','unofficial','community');
                    460:     my %typename = (
                    461:                          official   => 'Official course',
                    462:                          unofficial => 'Unofficial course',
                    463:                          community  => 'Community',
                    464:                     );
                    465:     return (\@types,\%typename);
                    466: }
                    467: 
                    468: 
1.1       raeburn   469: sub print_main_menu {
1.3       raeburn   470:     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb) = @_;
1.4       raeburn   471:     my ($types,$typename) = &course_types();
1.1       raeburn   472:     my $onchange;
                    473:     unless ($env{'form.interface'} eq 'textual') {
                    474:         $onchange = 1;
                    475:     }
                    476: 
1.2       raeburn   477:     my $nextstate_setter = "\n";
                    478:     if (ref($states) eq 'HASH') {
                    479:         foreach my $key (keys(%{$states})) {
                    480:             if (ref($states->{$key}) eq 'ARRAY') {
                    481:                 $nextstate_setter .= 
                    482: "             if (actionchoice == '$key') {
                    483:                   nextstate = '".$states->{$key}[1]."';
                    484:              }
                    485: ";
                    486:             }
                    487:         }
                    488:     }
1.1       raeburn   489: 
1.2       raeburn   490:     my $js = <<"END";
1.1       raeburn   491: 
1.2       raeburn   492: function nextPage(formname) {
                    493:     var crschoice = document.requestcrs.crstype.value;
                    494:     var actionchoice = document.requestcrs.action.value;
                    495:     if (check_can_request(crschoice,actionchoice) == true) {
                    496:         if ((actionchoice == 'new') && (crschoice == 'official')) {
                    497:             nextstate = 'codepick';
                    498:         } else {
                    499: $nextstate_setter 
                    500:         } 
1.1       raeburn   501:         formname.state.value= nextstate;
                    502:         formname.submit();
                    503:     }
                    504:     return;
                    505: }
                    506: 
1.2       raeburn   507: function check_can_request(crschoice,actionchoice) {
1.1       raeburn   508:     var official = '';
                    509:     var unofficial = '';
                    510:     var community = '';    
                    511: END
                    512: 
                    513:     foreach my $item (keys(%{$can_request})) {
                    514:             $js .= " 
                    515:         $item = 1;
                    516: ";
                    517:     }
                    518:     my %lt = &Apache::lonlocal::texthash(
                    519:         official => 'You are not permitted to request creation of an official course in this domain.',
                    520:         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
                    521:         community => 'You are not permitted to request creation of a community this domain.',
                    522:         all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
                    523:     ); 
                    524:     $js .= <<END;
                    525:     if (crschoice == 'official') {
                    526:         if (official != 1) {
                    527:             alert("$lt{'official'}");
                    528:             return false;
                    529:         }
                    530:     } else {
                    531:         if (crschoice == 'unofficial') {
                    532:             if (unofficial != 1) {
                    533:                 alert("$lt{'unofficial'}");
                    534:                 return false;
                    535:             }
                    536:         } else {
                    537:             if (crschoice == 'community') {
                    538:                 if (community != 1) {
                    539:                     alert("$lt{'community'}");
                    540:                     return false;
                    541:                 }
                    542:             } else {
                    543:                 if (actionchoice == 'new') {
                    544:                     alert("$lt{'all'}");
                    545:                     return false;
                    546:                 }               
                    547:             }
                    548:         }
                    549:     }
                    550:     return true;
                    551: }
                    552: 
                    553: END
                    554: 
1.3       raeburn   555:     $r->print(&header('Course Requests',$js.$jscript,$loaditems).$crumb.
                    556:               '<div>'.
1.1       raeburn   557:               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
                    558:               &Apache::lonhtmlcommon::start_pick_box().
1.8       raeburn   559:               &Apache::lonhtmlcommon::row_title('Course Domain').
1.1       raeburn   560:               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange));
                    561:     if (!$onchange) {
                    562:         $r->print('&nbsp;<input type="submit" name="godom" value="'.
                    563:                    &mt('Change').'" />');
                    564:     }
                    565:     $r->print(&Apache::lonhtmlcommon::row_closure(1).
                    566:               &Apache::lonhtmlcommon::end_pick_box().'</form></div>');
                    567: 
1.2       raeburn   568:     my $formname = 'requestcrs';
1.1       raeburn   569:     my $nexttext = &mt('Next');
1.14      raeburn   570:     $r->print(
                    571:               '<div><form name="'.$formname.'" method="post" action="/adm/requestcourse">'.
1.1       raeburn   572:               &Apache::lonhtmlcommon::start_pick_box().
                    573:               &Apache::lonhtmlcommon::row_title('Action').'
                    574: <input type="hidden" name="showdom" value="'.$dom.'" />
                    575: <select size="1" name="action" >
1.2       raeburn   576:  <option value="new">'.&mt('New request').'</option>
1.1       raeburn   577:  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
                    578:  <option value="log">'.&mt('View request history').'</option>
                    579: </select>'.
                    580:               &Apache::lonhtmlcommon::row_closure().
                    581:               &Apache::lonhtmlcommon::row_title('Course Type').'
                    582: <select size="1" name="crstype">
1.4       raeburn   583:  <option value="any">'.&mt('All types').'</option>');
                    584:     if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
                    585:         foreach my $type (@{$types}) {
                    586:             my $selected = '';
                    587:             if ($type eq 'official') {
                    588:                 $selected = ' selected="selected"';
                    589:             }
                    590:             $r->print('<option value="'.$type.'"'.$selected.'>'.$typename->{$type}.
                    591:                       '</option>'."\n");
                    592:         }
                    593:     }
                    594:     $r->print('</select>
1.1       raeburn   595: <input type="hidden" name="state" value="crstype" />'.
                    596:               &Apache::lonhtmlcommon::row_closure(1).
                    597:               &Apache::lonhtmlcommon::end_pick_box().'<br />
1.2       raeburn   598: <input type="button" name="next" value="'.$nexttext.'" onclick="javascript:nextPage(document.'.$formname.')" />
1.1       raeburn   599: </form></div>');
                    600:     $r->print(&Apache::loncommon::end_page());
                    601:     return;
                    602: }
                    603: 
                    604: sub request_administration {
1.3       raeburn   605:     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb) = @_;
1.2       raeburn   606:     my $js;
1.16      raeburn   607:     if (($action eq 'new') || (($action eq 'view') && ($state eq 'pick_request'))) {
1.2       raeburn   608:         $js =  <<END;
1.1       raeburn   609: 
                    610: function nextPage(formname,nextstate) {
                    611:     formname.state.value= nextstate;
                    612:     formname.submit();
                    613: }
1.16      raeburn   614: 
                    615: END
                    616:     }
                    617:     if (($action eq 'new') || ($action eq 'view')) {
                    618:         $js .= <<END;   
                    619: 
1.1       raeburn   620: function backPage(formname,prevstate) {
                    621:     formname.state.value = prevstate;
                    622:     formname.submit();
                    623: }
                    624: 
                    625: END
1.2       raeburn   626:     }
                    627:     if ($action eq 'new') {
                    628:         my $jsextra;
1.1       raeburn   629:         unless (($state eq 'review') || ($state eq 'process')) {
1.2       raeburn   630:             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
1.1       raeburn   631:         }
1.3       raeburn   632:         $r->print(&header('Request a course',$js.$jscript,$loaditems,$jsextra).$crumb);
1.8       raeburn   633:         &print_request_form($r,$action,$state,$page,$states,$dom);
1.2       raeburn   634:     } elsif ($action eq 'view') {
1.16      raeburn   635:         my $jsextra;
                    636:         my $formname = 'requestcrs';
                    637:         my $prev = $states->{$action}[$page-1];
                    638:         my $next = $states->{$action}[$page+1];
                    639:         if ($state eq 'pick_request') {
                    640:             $next = $states->{$action}[$page+1];
                    641:             $jsextra = &viewrequest_javascript($formname,$next);
                    642:         } elsif ($state eq 'details') {
                    643:             $jsextra = &viewdetails_javascript($formname);
                    644: 
                    645:         } elsif ($state eq 'cancel') {
                    646:             $jsextra = &viewcancel_javascript($formname);
                    647:         }
                    648:         $r->print(&header('Manage course requests',$js.$jscript.$jsextra,$loaditems).
                    649:                   $crumb);
                    650:         my $form = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />';
1.11      raeburn   651:         if ($state eq 'pick_request') {
1.16      raeburn   652:             $r->print('<h3>'.&mt('Pending course requests').'</h3><div>'."\n".$form."\n".
                    653:                       &print_request_status($dom).'</form>');
                    654:         } elsif ($state eq 'details') {
                    655:             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
                    656:             my $origcnum = $env{'form.cnum'};
                    657:             if ($origcnum eq '') {
                    658:                 $origcnum = $env{'form.origcnum'};   
                    659:             }
                    660:             if ($env{'form.crstype'} eq 'official') {
                    661:                 &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    662:                                                          \%cat_order,\@code_order);
                    663:             }
                    664:             $r->print('<h3>'.&mt('Course Request Details').'</h3><div>'."\n".$form."\n".
                    665:                       &print_review($formname,$dom,\@codetitles,\%cat_titles,\%cat_order,
                    666:                                     \@code_order)."\n".
                    667:                       '<input name="origcnum" value="'.$origcnum.'" type="hidden" />'."\n");
                    668:             my @excluded = &get_excluded_elements($dom,$states,'new','review');
                    669:             push(@excluded,'origcnum');
                    670:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
                    671:             my $other = 'modify';
                    672:             my %navtxt = &Apache::lonlocal::texthash (
                    673:                                                       prev => 'Back',
                    674:                                                       other => 'Modify Request',
                    675:                                                       next => 'Cancel Request',
                    676:                                                      );
                    677:             &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'},
                    678:                                 $state,$other,$navtxt{'other'});
                    679:             $r->print('</form>');
                    680:         } elsif ($state eq 'cancel') {
                    681:             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
                    682:             $r->print('<h3>'.&mt('Cancel course request').'</h3><div>'."\n".$form."\n".
                    683:                       $output);
                    684:             my @excluded = &get_excluded_elements($dom,$states,'view','cancel');
                    685:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
                    686:             my %navtxt = &Apache::lonlocal::texthash (
                    687:                                                       prev => 'Back',
                    688:                                                       next => 'Confirm Cancellation',
                    689:                                                      );
                    690:             if ($result eq 'ok') {
                    691:                 &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,
                    692:                                     $navtxt{'next'},$state);
                    693:             } else {
                    694:                 &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},undef,'',$state);
                    695:             }
                    696:             $r->print('</form>');
                    697:         } elsif ($state eq 'removal') {
                    698:             my $cnum = $env{'form.origcnum'};
                    699:             my $statuskey = 'status:'.$dom.':'.$cnum;
                    700:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
                    701:                                                    $env{'user.domain'},$env{'user.name'});
                    702:             my $currstatus = $userreqhash{$statuskey};
                    703:             my ($result,$error); 
                    704:             if (($currstatus eq 'approval') || ($currstatus eq 'pending')) { 
                    705:                 my %status = (
                    706:                                  $statuskey => 'cancelled',
                    707:                              );
                    708:                 my $statusresult = &Apache::lonnet::put('courserequests',\%status);
                    709:                 if ($statusresult eq 'ok') {
                    710:                     my $delresult = 
                    711:                         &Apache::lonnet::del_dom('courserequestqueue',
                    712:                                                  [$cnum.'_'.$currstatus],$dom);
                    713:                     if ($delresult eq 'ok') {
                    714:                         $result = 'ok';
                    715:                     } else {
                    716:                         $error = &mt('An error occurred when updating the pending requests queue: [_1]',$delresult);
                    717:                     }
                    718:                 } else {
                    719:                     $error = &mt("An error occurred when updating the status of this request in the requestor's records: [_1]",$statusresult);
                    720:                 }
                    721:             } else {
                    722:                 $error = &mt('The current status of this request could not be verified as pending approval/institutional action.');  
                    723:             }
                    724:             $r->print('<h3>'.&mt('Request Cancellation').'</h3><div>'."\n".$form."\n".
                    725:                       '<input type="hidden" name="state" value="'.$state.'" />'."\n".
                    726:                       '<input type="hidden" name="action" value="'.$action.'" />'."\n".
                    727:                       '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
                    728:                       '<input type="hidden" name="orignum" value="'.$cnum.'" />'."\n");
                    729:             if ($result eq 'ok') {
                    730:                 $r->print(&mt('Your course request has been cancelled.'));
                    731:             } else {
                    732:                 $r->print('<div class="LC_error">'.
                    733:                           &mt('The request cancellation process was not complete.').
                    734:                           '<br />'.$error.'</div>');
                    735:             }
                    736:             $r->print('</form>');
1.11      raeburn   737:         }
1.1       raeburn   738:     } elsif ($action eq 'log') {
1.11      raeburn   739:         $r->print(&coursereq_log('View request log',$jscript,$loaditems).$crumb);
1.1       raeburn   740:     }
1.2       raeburn   741:     $r->print(&Apache::loncommon::end_page());
1.1       raeburn   742:     return;
                    743: }
                    744: 
                    745: sub print_request_form {
1.2       raeburn   746:     my ($r,$action,$state,$page,$states,$dom) = @_;
1.1       raeburn   747:     my $formname = 'requestcrs';
1.2       raeburn   748:     my ($next,$prev,$message,$output,$codepicker,$crstype);
                    749:     $prev = $states->{$action}[$page-1];
                    750:     $next = $states->{$action}[$page+1];
1.4       raeburn   751:     my %navtxt = &Apache::lonlocal::texthash (
1.10      raeburn   752:                                                prev => 'Back',
1.4       raeburn   753:                                                next => 'Next',
                    754:                                              );
1.2       raeburn   755:     $crstype = $env{'form.crstype'};
1.1       raeburn   756:     $r->print('<form name="'.$formname.'" method="post" action="/adm/requestcourse">');
1.2       raeburn   757:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
1.1       raeburn   758:     if ($crstype eq 'official') {
1.2       raeburn   759:         if ($env{'form.instcode'} ne '') {
                    760:             $instcode = $env{'form.instcode'};
                    761:         }
                    762:     }
                    763:     if ($prev eq 'codepick') {
1.4       raeburn   764:         if ($crstype eq 'official') {
                    765:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    766:                                                      \%cat_order,\@code_order);
                    767:         }
1.2       raeburn   768:         if (@code_order > 0) {
                    769:             my $message;
                    770:             if ($instcode eq '') {
1.1       raeburn   771:                 foreach my $item (@code_order) {
1.2       raeburn   772:                     $instcode .= $env{'form.instcode_'.$item};
1.1       raeburn   773:                 }
1.2       raeburn   774:                 $r->print('<input type="hidden" name="instcode" value="'.$instcode.'" />'."\n");
1.1       raeburn   775:             }
                    776:             if ($instcode ne '') {
1.2       raeburn   777:                 $code_chk = &Apache::lonnet::auto_validate_instcode('',$dom,$instcode);
1.17      raeburn   778:                 if ($code_chk eq 'valid') {
1.1       raeburn   779:                     $message = '<div class="LC_info">'.
                    780:                                &mt('The chosen course category [_1] is valid.','<b>'.
1.2       raeburn   781:                                $instcode.'</b>').'</div>';
1.1       raeburn   782:                 } else {
                    783:                     $message = '<div class="LC_warning">'.
                    784:                                &mt('No course was found matching your choice of institutional course category.');
                    785:                     if ($code_chk ne '') {
                    786:                         $message .= '<br />'.$code_chk;
                    787:                     }
                    788:                     $message .= '</div>';
                    789:                 }
1.2       raeburn   790:             } else {
                    791:                 $message = '<div class="LC_warning">'.
                    792:                            &mt('No course was found matching your choice of institutional course category.');
1.1       raeburn   793:             }
1.2       raeburn   794:             unless ($code_chk eq 'ok') {
                    795:                 $prev = 'crstype';
                    796:             }
                    797:             $r->print($message);
1.1       raeburn   798:         }
1.2       raeburn   799:     }
                    800:     if ($prev eq 'crstype') {
1.4       raeburn   801:         if ($crstype eq 'official') {
                    802:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    803:                                                      \%cat_order,\@code_order);
                    804:         }
1.2       raeburn   805:         if (@code_order > 0) {
1.1       raeburn   806:             $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
                    807:                                            \%cat_titles,\%cat_order);
1.2       raeburn   808:             if ($codepicker) {
                    809:                 $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box().$codepicker.
                    810:                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
                    811:             } else {
1.15      raeburn   812:                 $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.2       raeburn   813:             }
                    814:         } else {
1.15      raeburn   815:             $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.1       raeburn   816:         }
1.2       raeburn   817:     } elsif ($prev eq 'codepick') {
1.15      raeburn   818:         $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
1.2       raeburn   819:     } elsif ($state eq 'enrollment') {
1.4       raeburn   820:         if ($crstype eq 'official') {
                    821:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    822:                                                      \%cat_order,\@code_order);
                    823:         }
1.2       raeburn   824:         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
                    825:                                          \%cat_titles,\%cat_order,\@code_order));
                    826:     } elsif ($state eq 'personnel') {
1.8       raeburn   827:         $r->print(&print_personnel_menu($dom,$formname,$crstype));
1.4       raeburn   828:     } elsif ($state eq 'review') {
1.16      raeburn   829:         my $cnum;
                    830:         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
                    831:             $cnum = $env{'form.origcnum'};
                    832:         } else {
                    833:             $cnum = &Apache::lonnet::generate_coursenum($dom);
                    834:         }
1.4       raeburn   835:         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    836:                                                  \%cat_order,\@code_order);
1.16      raeburn   837:         $r->print('<p>'.&mt('Review the details of the course request before submission.').'</p>'.
                    838:                   &print_review($formname,$dom,\@codetitles,\%cat_titles,\%cat_order,
                    839:                                 \@code_order).
                    840:                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
1.4       raeburn   841:         $navtxt{'next'} = &mt('Submit course request');
1.10      raeburn   842:     }  elsif ($state eq 'process') {
                    843:         if ($crstype eq 'official') {
                    844:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    845:                                                      \%cat_order,\@code_order);
                    846:         }
                    847:         my $result = &print_request_outcome($dom,\@codetitles,\@code_order);
1.13      raeburn   848:         $r->print($result);
1.1       raeburn   849:     }
1.16      raeburn   850:     my @excluded = &get_excluded_elements($dom,$states,$action,$state);
                    851:     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
                    852:     &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'},$state);
                    853:     return;
                    854: }
                    855: 
                    856: sub get_excluded_elements {
                    857:     my ($dom,$states,$action,$state) = @_;
1.2       raeburn   858:     my @excluded = ('counter');
                    859:     my %elements = &form_elements($dom);
                    860:     if (ref($states) eq 'HASH') {
                    861:         if (ref($states->{$action}) eq 'ARRAY') {
                    862:             my @items = @{$states->{$action}};
                    863:             my $numitems = scalar(@items);
                    864:             if ($numitems) {
                    865:                 for (my $i=$numitems-1; $i>=0; $i--) {
                    866:                     if (ref($elements{$action}) eq 'HASH') {
                    867:                         if (ref($elements{$action}{$items[$i]}) eq 'HASH') {
1.16      raeburn   868:                             foreach my $key (keys(%{$elements{$action}{$items[$i]}})) {
1.2       raeburn   869:                                 push(@excluded,$key);
                    870:                             }
                    871:                         }
                    872:                     }
                    873:                     last if ($items[$i] eq $state);
                    874:                 }
                    875:             }
                    876:         }
                    877:     }
                    878:     if (grep(/^instcode_/,@excluded)) {
                    879:         push(@excluded,'instcode');
1.1       raeburn   880:     }
1.16      raeburn   881:     return @excluded;
1.1       raeburn   882: }
                    883: 
1.2       raeburn   884: sub print_enrollment_menu {
                    885:     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order) =@_;
                    886:     my ($sections,$autoenroll,$access_dates);
                    887:     my $starttime = time;
                    888:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                    889: 
                    890:     my %accesstitles = (
                    891:                           'start' => 'Default start access',
1.16      raeburn   892:                            'end'   => 'Default end access',
1.2       raeburn   893:                        );
                    894:     my %enrolltitles = (
                    895:                            'start' => 'Start auto-enrollment',
                    896:                            'end'   => 'End auto-enrollment',
                    897:                        );
                    898:     if ($env{'form.crstype'} eq 'official') {
                    899:         if (&Apache::lonnet::auto_run('',$dom)) {
                    900:             my ($section_form,$crosslist_form,$autoenroll_form);
                    901:             $section_form = &inst_section_selector($dom,$instcode);
                    902:             my $crosslisttotal = $env{'form.crosslisttotal'};
                    903:             if (!defined($crosslisttotal)) {
                    904:                 $crosslisttotal = 1;
                    905:             }
                    906:             if ($env{'form.addcrosslist'}) {
                    907:                 $crosslisttotal ++;
                    908:             }
                    909:             for (my $i=0; $i<$crosslisttotal; $i++) {
                    910:                 $crosslist_form .= &coursecode_form($dom,'crosslist',$codetitles, 
                    911:                                                   $cat_titles,$cat_order,$i);
                    912:             }
                    913:             if ($crosslist_form) { 
                    914:                 $crosslist_form .= 
                    915:                     &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                    916:                     '<input name="crosslisttotal" type="hidden" value="'.$crosslisttotal.'" />'.
                    917:                     '<input name="addcrosslist" type="checkbox" value="'.$crosslisttotal.'"'.
                    918:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                    919:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1);
                    920:             }
                    921:             if ($section_form || $crosslist_form) {
                    922:                 $sections = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    923:                             $section_form.$crosslist_form.
                    924:                             &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    925:             }
                    926:             $autoenroll_form = 
                    927:                 &Apache::lonhtmlcommon::row_title(&mt('Add registered students automatically')).
                    928:                 '<span class="LC_nobreak"><label>'.
                    929:                 '<input type="radio" name="autoadds" value="1">'.
                    930:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                    931:                 '<input type="radio" name="autoadds" value="0" checked="checked">'.
                    932:                 &mt('No').'</label></span>'.
                    933:                 &Apache::lonhtmlcommon::row_closure().
                    934:                 &Apache::lonhtmlcommon::row_title(&mt('Drop unregistered students automatically')).
                    935:                 '<span class="LC_nobreak"><label>'.
                    936:                 '<input type="radio" name="autodrops" value="1">'.
                    937:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                    938:                 '<input type="radio" name="autodrops" value="0" checked="checked">'.
                    939:                 &mt('No').'</label></span>'. 
                    940:                 &Apache::lonhtmlcommon::row_closure().
                    941:                 &date_setting_table($starttime,$endtime,$formname,'enroll',%enrolltitles);
                    942:             if ($autoenroll_form) {
                    943:                 $autoenroll = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    944:                               $autoenroll_form.
                    945:                               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    946:             }
                    947:         }
                    948:     }
                    949:     my $access_dates_form = 
                    950:         &date_setting_table($starttime,$endtime,$formname,'access',%accesstitles);
                    951:     if ($access_dates_form) {
                    952:         $access_dates = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    953:                         $access_dates_form.
                    954:                         &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    955:     }
                    956:     return $sections.$autoenroll.$access_dates;
                    957: }
                    958: 
1.1       raeburn   959: sub inst_section_selector {
1.2       raeburn   960:     my ($dom,$instcode) = @_;
                    961:     my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
                    962:     my $sectotal = scalar(@sections);
1.1       raeburn   963:     my $output;
1.2       raeburn   964:     if ($sectotal) {
                    965:         $output .=  &Apache::lonhtmlcommon::row_title('Sections').
                    966:                     &Apache::loncommon::start_data_table().
                    967:                     &Apache::loncommon::start_data_table_row().
                    968:                     '<th>'.&mt('Include?').'<input type="hidden" name="sectotal" '.
                    969:                     'value="'.$sectotal.'"</th>'.
                    970:                     '<th>'.&mt('Institutional Section').'</th>'.
                    971:                     '<th>'.&mt('LON-CAPA section').'</th>'.
                    972:                     &Apache::loncommon::end_data_table_row();
                    973:         for (my $i=0; $i<@sections; $i++) {
1.1       raeburn   974:             my $colflag = $i%2;
                    975:             $output .= &Apache::loncommon::start_data_table_row().
1.2       raeburn   976:                        '<td><input type="checkbox" name="sec_'.$i.
                    977:                        '" checked="checked" /></td>'.
                    978:                        '<td>'.$sections[$i].
1.1       raeburn   979:                        '<input type="hidden" name="secnum_'.$i.'" value="'.
1.2       raeburn   980:                        $sections[$i].'" /></td>'.
1.1       raeburn   981:                        '<td><input type="text" size="10" name="loncapasec_'.$i.
1.2       raeburn   982:                        '" value="'.$sections[$i].'" /></td>'.
1.1       raeburn   983:                        &Apache::loncommon::end_data_table_row();
                    984:         }
1.2       raeburn   985:         $output .= &Apache::loncommon::end_data_table().
                    986:                    &Apache::lonhtmlcommon::row_closure();
1.1       raeburn   987:     }
                    988:     return $output;
                    989: }
                    990: 
1.2       raeburn   991: sub date_setting_table {
1.14      raeburn   992:     my ($starttime,$endtime,$formname,$prefix,%datetitles) = @_;
1.2       raeburn   993:     my ($perpetual,$table);
1.14      raeburn   994:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'start',
1.2       raeburn   995:                                                         $starttime,'','','',1,'','','',1);
1.14      raeburn   996:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'end',
1.2       raeburn   997:                                                       $endtime,'','','',1,'','','',1);
1.14      raeburn   998:     if ($prefix eq 'access') {
1.2       raeburn   999:         $perpetual = ' <span class="LC_nobreak"><label>'.
                   1000:                      '<input type="checkbox" name="no_end_date" />'.
                   1001:                      &mt('No end date').'</label></span>';
                   1002:     }
                   1003:     $table = &Apache::lonhtmlcommon::row_title($datetitles{'start'}).
                   1004:              $startform.
                   1005:              &Apache::lonhtmlcommon::row_closure().
                   1006:              &Apache::lonhtmlcommon::row_title($datetitles{'end'}).
                   1007:              $endform.$perpetual.
                   1008:              &Apache::lonhtmlcommon::row_closure(1);
                   1009:     return $table;
                   1010: }
                   1011: 
                   1012: sub print_personnel_menu {
1.8       raeburn  1013:     my ($dom,$formname,$crstype) = @_;
1.2       raeburn  1014:     my $output = '<div>'.&Apache::lonhtmlcommon::start_pick_box();
                   1015:     my $persontotal = $env{'form.persontotal'};
1.16      raeburn  1016:     if ((!defined($persontotal)) || (!$persontotal)) {
1.2       raeburn  1017:         $persontotal = 1;
                   1018:     }
                   1019:     if ($env{'form.addperson'}) {
                   1020:         $persontotal ++;
                   1021:     }
                   1022:     my $userlinktxt = &mt('Set User');
1.13      raeburn  1023:     my @items = ('uname','dom','lastname','firstname','emailaddr','hidedom');
1.2       raeburn  1024: 
                   1025:     my $roleoptions;
                   1026:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.8       raeburn  1027:     my $type = 'Course';
                   1028:     if ($crstype eq 'community') {
                   1029:         $type = 'Community';
                   1030:     }
1.2       raeburn  1031:     foreach my $role (@roles) {
1.8       raeburn  1032:         my $plrole=&Apache::lonnet::plaintext($role,$type);
1.2       raeburn  1033:         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1034:     }
                   1035:     my %customroles=&Apache::lonuserutils::my_custom_roles();
                   1036:     if (keys(%customroles) > 0) {
                   1037:         foreach my $cust (sort(keys(%customroles))) {
                   1038:             my $custrole='cr_cr_'.$env{'user.domain'}.
                   1039:                     '_'.$env{'user.name'}.'_'.$cust;
                   1040:             $roleoptions .= '  <option value="'.$custrole.'">'.$cust.'</option>'."\n";
                   1041:         }
                   1042:     }
                   1043: 
                   1044:     my @currsecs;
                   1045:     if ($env{'form.sectotal'}) {
                   1046:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   1047:             if (defined($env{'form.loncapasec_'.$i})) {
                   1048:                 my $lcsec = $env{'form.loncapasec_'.$i};
                   1049:                 unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
                   1050:                     push(@currsecs,$lcsec);
                   1051:                 }
                   1052:             }
                   1053:         }
                   1054:     }
                   1055: 
                   1056:     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
                   1057:     if (@currsecs) {
                   1058:         my $existsize = scalar(@currsecs);
                   1059:         if ($existsize > 3) {
                   1060:             $existsize = 3;
                   1061:         }
                   1062:         if ($existsize > 1) {
                   1063:             $existmult = ' multiple="multiple" size="'.$existsize.'" ';
                   1064:         }
                   1065:         @currsecs = sort { $a <=> $b } (@currsecs);
                   1066:         $existtitle = &mt('Official').':&nbsp;';
                   1067:         $existops = '<option value="">'.&mt('None').'</option>';
                   1068:         foreach my $sec (@currsecs) {
                   1069:             $existops .= '<option value="'.$sec.'">'.$sec.'</option>'."\n";
                   1070:         }
                   1071:         $seccolspan = ' colspan="2"';
                   1072:         $newtitle = &mt('Other').':&nbsp;';
                   1073:     }
                   1074: 
                   1075:     for (my $i=0; $i<$persontotal; $i++) { 
                   1076:         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
                   1077:         my $linkargstr = join("','",@linkargs);
1.7       raeburn  1078:         my $userlink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,$userlinktxt);
1.2       raeburn  1079:         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value=""'.
                   1080:                          ' onFocus="this.blur();'.
1.7       raeburn  1081:                          'openuserbrowser('."'$formname','$linkargstr','$dom'".');" />';
1.2       raeburn  1082:         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
                   1083:                        "'person_".$i."_hidedom'".');'.
1.7       raeburn  1084:                        'openuserbrowser('."'$formname','$linkargstr','$dom'".');';
1.2       raeburn  1085:         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
                   1086:                                                             1,$onchange).
                   1087:                         '<input type="hidden" name="person_'.$i.'_hidedom" value="'.$dom.'" />';
                   1088:         my %form_elems;
                   1089:         foreach my $item (@items) {
                   1090:             next if (($item eq 'dom') || ($item eq 'uname') || ($item eq 'hidedom'));
                   1091:             $form_elems{$item} = '<input type="text" name="person_'.$i.'_'.$item.'" '.
                   1092:                                  'value="" readonly="readonly" />';
                   1093:         }
                   1094:         my $roleselector = '<select name="person_'.$i.'_role">'."\n".
                   1095:                            $roleoptions.'</select>';
                   1096:         my $sectionselector;
                   1097:         if (@currsecs) {
                   1098:             $sectionselector = $existtitle.'<select name="person_'.$i.'_sec"'.
                   1099:                                $existmult.'>'."\n".$existops.'</select>'.('&nbsp;' x3);
                   1100:         }
                   1101:         $sectionselector .= $newtitle.
1.14      raeburn  1102:             '<input type="text" name="person_'.$i.'_newsec" size="15" value="" />'."\n";
1.2       raeburn  1103:         $output .= 
                   1104:             &Apache::lonhtmlcommon::row_title(&mt('Additional Personnel').'<br />'.
                   1105:                                               '<span class="LC_nobreak">'.$userlink.
                   1106:                                               '</span>').
                   1107:             '<table><tr><td align="center" valign="top">'.&mt('Username').'<br />'.$uname_form.'</td>'."\n".
                   1108:             '<td align="center" valign="top" colspan="2">'.&mt('Domain').'<br />'.$udom_form.'</td></tr><tr>'."\n".
1.13      raeburn  1109:             '<td align="center" valign="top">'.&mt('First Name').'<br />'.$form_elems{'firstname'}.'</td>'."\n".
                   1110:             '<td align="center" valign="top">'.&mt('Last Name').'<br />'.$form_elems{'lastname'}.'</td>'."\n".
                   1111:             '<td align="center" valign="top">'.&mt('E-mail').'<br />'.$form_elems{'emailaddr'}.'</td></tr>'."\n".
1.2       raeburn  1112:             '<tr><td align="center" valign="top">'.&mt('Role').'<br />'.$roleselector.'</td>'."\n".
                   1113:             '<td'.$seccolspan.' align="center" valign="top">'.&mt('Section(s)').'<br />'.$sectionselector.'</td>'."\n".
                   1114:             '</tr></table>'.&Apache::lonhtmlcommon::row_closure();
                   1115:     }
                   1116:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                   1117:                '<input name="persontotal" type="hidden" value="'.$persontotal.'" />'.
                   1118:                '<input name="addperson" type="checkbox" value="'.$persontotal.'"'.
                   1119:                ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                   1120:                "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1).
                   1121:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
                   1122:     return $output;
                   1123: }
                   1124: 
1.1       raeburn  1125: sub print_request_status {
1.11      raeburn  1126:     my ($dom) = @_;
1.14      raeburn  1127:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                   1128:                                            $env{'user.name'},'^status:'.$dom);
                   1129:     my ($output,$formname,%queue_by_date,%typenames);
                   1130:     if ($env{'form.crstype'} eq 'any') {
                   1131:         %typenames = &Apache::lonlocal::texthash (
                   1132:                                                    official   => 'Official course',
                   1133:                                                    unofficial => 'Unofficial course',
                   1134:                                                    community  => 'Community',
                   1135:                                                  );
                   1136:     }
                   1137:     foreach my $key (keys(%statusinfo)) {
                   1138:         if (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending')) { 
                   1139:             (undef,my($cdom,$cnum)) = split(':',$key);
                   1140:             next if ($cdom ne $dom);   
                   1141:             my $requestkey = $cdom.'_'.$cnum;
                   1142:             if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   1143:                 my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   1144:                                                        $env{'user.domain'},$env{'user.name'});
                   1145:                 my $entry;
                   1146:                 my $timestamp = $history{'reqtime'};
                   1147:                 my $crstype = $history{'crstype'};
                   1148:                 my $disposition = $history{'disposition'};
                   1149:                 next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
                   1150:                 next unless (($env{'form.crstype'} eq 'any') || 
                   1151:                             ($env{'form.crstype'} eq $crstype));
                   1152:                 next unless (($disposition eq 'approval') || 
                   1153:                              ($disposition eq 'pending'));
                   1154:                 if (ref($history{'details'}) eq 'HASH') {
                   1155:                     $entry = $requestkey.':'.$crstype.':'.
                   1156:                              &escape($history{'details'}{'cdescr'});
                   1157:                     if ($crstype eq 'official') {
                   1158:                         $entry .= ':'.&escape($history{'details'}{'instcode'}); 
                   1159:                     }
                   1160:                 }
                   1161:                 if ($entry ne '') {
                   1162:                     if (exists($queue_by_date{$timestamp})) {
                   1163:                         if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                   1164:                             push(@{$queue_by_date{$timestamp}},$entry);
                   1165:                         }
                   1166:                     } else {
                   1167:                         @{$queue_by_date{$timestamp}} = ($entry);
1.10      raeburn  1168:                     }
                   1169:                 }
                   1170:             }
                   1171:         }
                   1172:     }
1.11      raeburn  1173:     $formname = 'requestcrs';
1.10      raeburn  1174:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
1.16      raeburn  1175:     $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                   1176: 
1.10      raeburn  1177:               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
1.16      raeburn  1178:               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n".
                   1179:               '<input type="hidden" name="showdom" value="" />'."\n".
                   1180:               '<input type="hidden" name="cnum" value="" />'."\n";
1.10      raeburn  1181:     if (@sortedtimes > 0) {
                   1182:         $output .= &Apache::loncommon::start_data_table().
                   1183:                    &Apache::loncommon::start_data_table_header_row().
                   1184:                    '<th>'.&mt('Action').'</th>'.
1.14      raeburn  1185:                    '<th>'.&mt('Course Description').'</th>'.
                   1186:                    '<th>'.&mt('Domain').'</th>';
                   1187:         if ($env{'form.crstype'} eq 'any') {
1.10      raeburn  1188:             $output .= '<th>'.&mt('Type').'</th>';
                   1189:         }
1.14      raeburn  1190:         if (($env{'form.crstype'} eq 'any') || ($env{'form.crstype'} eq 'official')) {
1.10      raeburn  1191:             $output .= '<th>'.&mt('Institutional Code').'</th>';
                   1192:         }
                   1193:         $output .= '<th>'.&mt('Date requested').'</th>'.
                   1194:                    &Apache::loncommon::end_data_table_header_row();
                   1195:         my $count = 0;
                   1196:         foreach my $item (@sortedtimes) {
                   1197:             my $showtime = &Apache::lonlocal::locallocaltime($item);
                   1198:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
                   1199:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
                   1200:                     my ($key,$type,$desc,$instcode) = split(':',$request);
                   1201:                     my ($cdom,$cnum) = split('_',$key);
                   1202:                     $output .= &Apache::loncommon::start_data_table_row().
1.16      raeburn  1203:      '<td><input type="button" value="'.&mt('Select').'" onclick="javascript:chooseRequest('."'$cdom','$cnum'".')" /></td>'.
1.14      raeburn  1204:      '<td>'.&unescape($desc).'</td>'.
                   1205:      '<td>'.$cdom.'</td>';
                   1206:                     if ($env{'form.crstype'} eq 'any') {
                   1207:                         my $typename = $typenames{$type};
                   1208:                         if ($typename eq '') {
                   1209:                             $typename = &mt('Unknown type');
                   1210:                         }
                   1211:                         $output .= '<td>'.$typename.'</td>';
1.10      raeburn  1212:                     }
1.14      raeburn  1213:                     if (($env{'form.crstype'} eq 'any') || 
1.10      raeburn  1214:                         ($env{'form.crstype'} eq 'official')) {
1.14      raeburn  1215:                         my $showinstcode;
                   1216:                         if ($type eq 'official') {
                   1217:                             $showinstcode = &unescape($instcode);
                   1218:                         } else {
                   1219:                             $showinstcode = &mt('Not applicable');
                   1220:                         }
                   1221:                         $output .= '<td>'.$showinstcode.'</td>';
1.10      raeburn  1222:                     }
                   1223:                     $output .= '<td>'.$showtime.'</td>'.
                   1224:                                &Apache::loncommon::end_data_table_row();
                   1225:                 }
                   1226:             }
                   1227:         }
                   1228:         $output .= &Apache::loncommon::end_data_table();
                   1229:     } else {
1.11      raeburn  1230:         $output .= '<div>'.&mt('You have no matching course requests awaiting approval by a Domain Coordinator or held in a queue pending administrative action at your institution.').'</div>';
1.10      raeburn  1231:     }
                   1232:     $output .= '
1.14      raeburn  1233: <br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />
                   1234: </form></div>';
1.10      raeburn  1235:     return $output;
1.1       raeburn  1236: }
                   1237: 
1.16      raeburn  1238: sub print_cancel_request {
                   1239:     my ($dom,$cnum) = @_;
                   1240:     my $requestkey = $dom.'_'.$cnum;
                   1241:     my ($result,$output);
                   1242:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   1243:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   1244:                                                $env{'user.domain'},$env{'user.name'});
                   1245:         my $timestamp = $history{'reqtime'};
                   1246:         my $crstype = $history{'crstype'};
                   1247:         my $status = $history{'status'};
                   1248:         if (($status eq 'cancelled') || ($status eq 'created')) { 
                   1249:             if ($status eq 'cancelled') {
                   1250:                 $output = &mt('This request has already been cancelled.');
                   1251:             } elsif ($status eq 'created') {
                   1252:                 $output = &mt('This request has already been processed, and a course created.');
                   1253:             }
                   1254:             $output = &mt('No further action will be taken');
                   1255:         } elsif (ref($history{'details'}) eq 'HASH') {
                   1256:             my ($types,$typename) = &course_types();
                   1257:             my $showtype = $crstype;
                   1258:             if (defined($typename->{$crstype})) {
                   1259:                 $showtype = $typename->{$crstype}; 
                   1260:             }
                   1261:             $output = '<p>'.&Apache::loncommon::start_data_table().
                   1262:                       &Apache::loncommon::start_data_table_header_row().
                   1263:                       '<th>'.&mt('Description').'</th><th>'.&mt('Requested').'</th>'.
                   1264:                       '<th>'.&mt('Type').'</th>'.
                   1265:                       &Apache::loncommon::end_data_table_header_row().
                   1266:                       &Apache::loncommon::start_data_table_row().
                   1267:                       '<td>'.$history{details}{'cdescr'}.'</td><td>'.
                   1268:                       &Apache::lonlocal::locallocaltime($timestamp).'</td>'.
                   1269:                       '<td>'.$showtype.'</td>'.
                   1270:                       &Apache::loncommon::end_data_table_row().
                   1271:                       &Apache::loncommon::end_data_table().
                   1272:                       '<br /><div class="LC_warning">'.
                   1273:                       &mt('Cancelling the request will remove it from the queue of pending course requests').'</div>';
                   1274:             $result = 'ok';
                   1275:         } else {
                   1276:             $output = '<div class="LC_error">'.&mt('No record exists for the course ID').'</div>';
                   1277:         }
                   1278:     } else {
                   1279:         $output = '<div class="LC_error">'.&mt('Invalid course ID').'</div>';
                   1280:     }
                   1281:     return ($result,$output);
                   1282: }
                   1283: 
                   1284: sub viewrequest_javascript {
                   1285:     my ($formname,$next) = @_;
                   1286:     return <<"ENDJS";
                   1287: 
                   1288: function chooseRequest(cdom,cnum) {
                   1289:     document.$formname.showdom.value = cdom;
                   1290:     document.$formname.cnum.value = cnum;
                   1291:     nextPage(document.$formname,'$next');
                   1292: }
                   1293: 
                   1294: ENDJS
                   1295: }
                   1296: 
                   1297: sub viewdetails_javascript {
                   1298:     my ($formname) = @_;
                   1299:     return << "ENDJS";
                   1300: 
                   1301: function nextPage(formname,nextstate) {
                   1302:     if (nextstate == "modify") { 
                   1303:         formname.state.value = "personnel";
                   1304:         formname.action.value = "new";
                   1305:     } else {
                   1306:         formname.state.value = nextstate;
                   1307:     }
                   1308:     formname.submit();
                   1309: }
                   1310: 
                   1311: function backPage(formname,prevstate) {
                   1312:     formname.state.value = prevstate;
                   1313:     formname.submit();
                   1314: }
                   1315: 
                   1316: ENDJS
                   1317: }
                   1318: 
                   1319: sub viewcancel_javascript {
                   1320:     my $alert = &mt('Are you sure you want to cancel this request?\\n'.
                   1321:                     'Your request will be removed.');
                   1322:     return << "ENDJS";
                   1323: function nextPage(formname,nextstate) {
                   1324:     if (confirm('$alert')) {
                   1325:         formname.state.value = nextstate;
                   1326:         formname.submit();
                   1327:     }
                   1328:     return;
                   1329: }
                   1330: 
                   1331: ENDJS
                   1332: }
                   1333: 
1.1       raeburn  1334: sub print_request_logs {
1.10      raeburn  1335:     my ($jscript,$loaditems,$crumb) = @_;
1.1       raeburn  1336:     return;
                   1337: }
                   1338: 
                   1339: sub print_review {
1.4       raeburn  1340:     my ($formname,$dom,$codetitles,$cat_titles,$cat_order,$code_order) = @_;
                   1341:     my ($types,$typename) = &course_types();
                   1342:     my ($owner,$ownername,$owneremail);
                   1343:     $owner = $env{'user.name'}.':'.$env{'user.domain'};
                   1344:     $ownername = &Apache::loncommon::plainname($env{'user.name'},
                   1345:                                                $env{'user.domain'},'first');
                   1346:     my %emails = &Apache::loncommon::getemails();
                   1347:     foreach my $email ('permanentemail','critnotification','notification') {
                   1348:         $owneremail = $emails{$email};
                   1349:         last if ($owneremail ne '');
                   1350:     }
                   1351:     my ($inst_headers,$inst_values,$crstypename,$enroll_headers,$enroll_values,
                   1352:         $section_headers,$section_values,$personnel_headers,$personnel_values);
                   1353: 
                   1354:     $crstypename = $env{'form.crstype'};
                   1355:     if (ref($typename) eq 'HASH') {
                   1356:         unless ($typename->{$env{'form.crstype'}} eq '') {
                   1357:             $crstypename = $typename->{$env{'form.crstype'}};
                   1358:         }
                   1359:     }
1.16      raeburn  1360:     my $category = 'Course';
                   1361:     if ($env{'form.crstype'} eq 'community') {
                   1362:         $category = 'Community';
                   1363:     }
1.4       raeburn  1364: 
                   1365:     $inst_headers = '<th>'.&mt('Description').'</th><th>'.&mt('Type').'</th>';
                   1366:     $inst_values = '<td>'.$env{'form.cdescr'}.'</td><td>'.$crstypename.'</td>';
                   1367: 
1.16      raeburn  1368:     my $enrollrow_title = &mt('Default Access Dates').'<br />'.
                   1369:                           '('.&Apache::lonnet::plaintext('st',$category).')';
1.4       raeburn  1370:     if ($env{'form.crstype'} eq 'official') {
                   1371:         if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH')) {
                   1372:             foreach my $title (@{$codetitles}) {
                   1373:                 if ($env{'form.instcode_'.$title} ne '') {
                   1374:                     $inst_headers .= '<th>'.$title.'</th>';
                   1375:                     my $longitem = $env{'form.instcode_'.$title};
                   1376:                     if (ref($cat_titles->{$title}) eq 'HASH') {
                   1377:                         if ($cat_titles->{$title}{$env{'form.instcode_'.$title}} ne '') {
                   1378:                             $longitem = $cat_titles->{$title}{$env{'form.instcode_'.$title}};
                   1379:                         }
                   1380:                     }
                   1381:                     $inst_values .= '<td>'.$longitem.'</td>';
                   1382:                 }
                   1383:             }
                   1384:         }
                   1385:         if (&Apache::lonnet::auto_run('',$dom)) {
1.16      raeburn  1386:             $enrollrow_title = &mt('Enrollment');
1.4       raeburn  1387:             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
                   1388:                               '<th>'.&mt('Automatic Drops').'</th>'.
                   1389:                               '<th>'.&mt('Enrollment Starts').'</th>'.
                   1390:                               '<th>'.&mt('Enrollment Ends').'</th>';
                   1391:             $section_headers = '<th>'.&mt('Sections').'</th>'.
                   1392:                                '<th>'.&mt('Crosslistings').'</th>';
                   1393: 
1.13      raeburn  1394:             my ($enrollstart,$enrollend) = &dates_from_form('enrollstart','enrollend');
1.4       raeburn  1395:             my @autoroster = (&mt('No'),&mt('Yes'));
                   1396:             $enroll_values = '<td>'.$autoroster[$env{'form.autoadds'}].'</td>'.
                   1397:                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
1.13      raeburn  1398:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollstart).'</td>'.
                   1399:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollend).'</td>';
1.6       raeburn  1400:             $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
                   1401:                               &mt('Institutional section').'</th>'.
                   1402:                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  1403:             my $secinfo;
1.4       raeburn  1404:             if ($env{'form.sectotal'} > 0) {
                   1405:                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   1406:                     if ($env{'form.sec_'.$i}) {
1.5       raeburn  1407:                         $secinfo .= '<tr><td>'.$env{'form.secnum_'.$i}.'</td><td>';
1.4       raeburn  1408:                         if ($env{'form.loncapasec_'.$i} ne '') {
1.5       raeburn  1409:                             $secinfo .= $env{'form.loncapasec_'.$i};
                   1410:                         } else {
                   1411:                             $secinfo .= &mt('None');
1.4       raeburn  1412:                         }
1.5       raeburn  1413:                         $secinfo .= '</td></tr>';
1.4       raeburn  1414:                     }
                   1415:                 }
                   1416:             }
1.6       raeburn  1417:             if ($secinfo eq '') {
                   1418:                 $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  1419:             }
1.6       raeburn  1420:             $section_values .= $secinfo.'</table></td><td>'.
                   1421:                                '<table class="LC_innerpickbox"><tr><th>'.
                   1422:                                &mt('Institutional course/section').'</th>'.
                   1423:                                '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  1424:             my $xlistinfo;
1.4       raeburn  1425:             if ($env{'form.crosslisttotal'}) {
                   1426:                 for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                   1427:                     if ($env{'form.crosslist_'.$i}) {
1.5       raeburn  1428:                         $xlistinfo .= '<tr><td>';
                   1429:                         if (ref($code_order) eq 'ARRAY') {
                   1430:                             if (@{$code_order} > 0) {
                   1431:                                 foreach my $item (@{$code_order}) {
                   1432:                                     $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
1.4       raeburn  1433:                                 }
                   1434:                             }
                   1435:                         }
1.5       raeburn  1436:                         $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'}.'</td><td>';
1.4       raeburn  1437:                         if ($env{'form.crosslist_'.$i.'_lcsec'}) {
1.5       raeburn  1438:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_lcsec'};
                   1439:                         } else {
                   1440:                             $xlistinfo .= &mt('None');
1.4       raeburn  1441:                         }
1.5       raeburn  1442:                         $xlistinfo .= '</td></tr>';
1.4       raeburn  1443:                     }
                   1444:                 }
                   1445:             }
1.6       raeburn  1446:             if ($xlistinfo eq '') {
                   1447:                 $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  1448:             }
1.6       raeburn  1449:             $section_values .= $xlistinfo.'</table></td>';
1.4       raeburn  1450:         }
                   1451:     }
                   1452: 
                   1453:     my %ctxt = &clone_text();
                   1454:     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
1.13      raeburn  1455:     if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
                   1456:         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
1.15      raeburn  1457:         my $canclone = &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
                   1458:                            $env{'user.domain'},$env{'form.clonecrs'}, $env{'form.clonedom'});
                   1459:         if ($canclone) {
                   1460:             my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
                   1461:                               $env{'form.clonecrs'},('description','internal.coursecode'));
                   1462:             if (keys(%courseenv) > 0) {
                   1463:                 $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
                   1464:                 $inst_values .= '<td>'.$courseenv{'description'}.'&nbsp;';
                   1465:                 my $cloneinst = $courseenv{'internal.coursecode'};
                   1466:                 if ($cloneinst ne '') {
1.18    ! raeburn  1467:                     $inst_values .= $cloneinst.' '.&mt('in').' '.$env{'form.clonedom'};
1.15      raeburn  1468:                 } else {
1.18    ! raeburn  1469:                     $inst_values .= &mt('from').' '.$env{'form.clonedom'};
1.15      raeburn  1470:                 }
                   1471:                 $inst_values .= '</td><td>';
                   1472:                 if ($env{'form.datemode'} eq 'preserve') {
1.16      raeburn  1473:                     $inst_values .= $ctxt{'prd'}; 
1.15      raeburn  1474:                 } elsif ($env{'form.datemode'} eq 'shift') {
                   1475:                     $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
                   1476:                 } else {
                   1477:                     $inst_values .= $ctxt{'ncd'};
                   1478:                 }
                   1479:                 $inst_values .= '</td>';
                   1480:              } else {
                   1481:                  $inst_values .= '<td>'.&mt('Unknown').'</td>';
                   1482:              }
                   1483:          } else {
                   1484:              $inst_values .= '<td>'.&mt('Not permitted'),'</td>';
                   1485:          }
1.4       raeburn  1486:     } else {
                   1487:         $inst_values .= '<td>'.&mt('None').'</td>';
                   1488:     }
                   1489:     $enroll_headers .= '<th>'.&mt('Access Starts').'</th>'.
                   1490:                        '<th>'.&mt('Access Ends').'</th>';
1.13      raeburn  1491:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
                   1492:     $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessstart).'</td>';
                   1493:     if ($accessend == 0) {
1.4       raeburn  1494:         $enroll_values .= '<td>'.&mt('No end date').'</td>';
                   1495:     } else {
1.13      raeburn  1496:         $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessend).'</td>';
1.4       raeburn  1497:     }
                   1498: 
                   1499:     my $container = 'Course';
                   1500:     if ($env{'form.crstype'} eq 'community') {
                   1501:         $container = 'Community';
                   1502:     }
                   1503: 
                   1504:     $personnel_headers = '<th>'.&mt('Name').'</th><th>'.&mt('Username:Domain').
                   1505:                          '</th><th>'.&mt('Role').'</th><th>'.&mt('LON-CAPA Sections').
                   1506:                          '</th>';
                   1507:     $personnel_values .= '<tr><td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   1508:                          '<td>'.&Apache::lonnet::plaintext('cc',$container).'</td>'.
                   1509:                          '<td>'.&mt('None').'</td></tr>';
                   1510:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1511:         if ($env{'form.person_'.$i.'_uname'} ne '') {
1.14      raeburn  1512:             my @allsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
                   1513:             my $newsec = $env{'form.person_'.$i.'_newsec'};
                   1514:             $newsec =~ s/^\s+//;
                   1515:             $newsec =~s/\s+$//;
                   1516:             my @newsecs = split(/[\s,;]+/,$newsec);
                   1517:             foreach my $sec (@newsecs) {
                   1518:                 next if ($sec =~ /\W/);
                   1519:                 next if ($newsec eq 'none');
                   1520:                 if ($sec ne '') {
                   1521:                     unless (grep(/^\Q$sec\E$/,@allsecs)) {
                   1522:                         push(@allsecs,$sec); 
                   1523:                     }
                   1524:                 }
                   1525:             }
                   1526:             my $showsec = 'None';
                   1527:             if (@allsecs) {
                   1528:                 $showsec = join(', ',@allsecs);
                   1529:             }
1.4       raeburn  1530:             $personnel_values .= 
1.13      raeburn  1531:                 '<tr><td>'.$env{'form.person_'.$i.'_firstname'}.' '.
                   1532:                 $env{'form.person_'.$i.'_lastname'}.'</td>'.
1.4       raeburn  1533:                 '<td>'.$env{'form.person_'.$i.'_uname'}.':'.
                   1534:                 $env{'form.person_'.$i.'_dom'}.'</td>'.
                   1535:                 '<td>'.&Apache::lonnet::plaintext($env{'form.person_'.$i.'_role'},
                   1536:                                                   $container).'</td>'.
1.14      raeburn  1537:                 '<td>'.$showsec.'</td></tr>';
1.4       raeburn  1538:         }
                   1539:     }
1.16      raeburn  1540:     my $output =  
1.4       raeburn  1541:                   '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   1542:                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
1.6       raeburn  1543:                   '<table class="LC_innerpickbox"><tr>'.
1.4       raeburn  1544:                   '<th>'.&mt('Name').'</th>'.
                   1545:                   '<th>'.&mt('Username:Domain').'</th>'.
                   1546:                   '<th>'.&mt('E-mail address').'</th>'.
                   1547:                   '</tr><tr>'."\n".
                   1548:                   '<td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   1549:                   '<td>'.$owneremail.'</td>'.
                   1550:                   '</tr></table>'."\n".
                   1551:                   &Apache::lonhtmlcommon::row_closure().
1.5       raeburn  1552:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
1.4       raeburn  1553:                   '<table class="LC_innerpickbox"><tr>'.$inst_headers.'</tr>'."\n".
                   1554:                   '<tr>'.$inst_values.'</tr></table>'."\n".
                   1555:                   &Apache::lonhtmlcommon::row_closure().
1.16      raeburn  1556:                   &Apache::lonhtmlcommon::row_title($enrollrow_title).
1.4       raeburn  1557:                   '<table class="LC_innerpickbox"><tr>'.$enroll_headers.'</tr>'."\n".
                   1558:                   '<tr>'.$enroll_values.'</tr></table>'."\n".
                   1559:                   &Apache::lonhtmlcommon::row_closure();
                   1560:     if ($section_headers ne '') {
                   1561:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Sections')).
                   1562:                    '<table class="LC_innerpickbox"><tr>'.$section_headers.'</tr>'."\n".
                   1563:                    '<tr>'.$section_values.'</tr></table>'."\n".
                   1564:                    &Apache::lonhtmlcommon::row_closure();
                   1565:     }
                   1566:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Personnel')).
                   1567:                '<table class="LC_innerpickbox"><tr>'.$personnel_headers.'</tr>'."\n".
                   1568:                $personnel_values.'</table>'."\n".
                   1569:                &Apache::lonhtmlcommon::row_closure(1).
                   1570:                &Apache::lonhtmlcommon::end_pick_box();
                   1571:     return $output;
                   1572: }
                   1573: 
                   1574: sub dates_from_form {
                   1575:     my ($startname,$endname) = @_;
                   1576:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1577:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.13      raeburn  1578:     if ($endname eq 'accessend') {
1.4       raeburn  1579:         if (exists($env{'form.no_end_date'}) ) {
                   1580:             $enddate = 0;
                   1581:         }
                   1582:     }
                   1583:     return ($startdate,$enddate);
1.1       raeburn  1584: }
                   1585: 
                   1586: sub courseinfo_form {
1.15      raeburn  1587:     my ($dom,$formname,$crstype,$next) = @_;
                   1588:     my $nodescr = &mt('You must provide a (brief) course description.');
                   1589:     my $js_validate = <<"ENDJS";
                   1590: <script type="text/javascript">
                   1591: // <![CDATA['
                   1592: 
                   1593: function validateForm() {
                   1594:     if ((document.$formname.cdescr.value == "")  || (document.$formname.cdescr.value == "undefined")) {
                   1595:         alert('$nodescr');
                   1596:         return;
                   1597:     }
                   1598:     nextPage(document.$formname,'$next');
                   1599: }
                   1600: // ]]
                   1601: </script>
                   1602: 
                   1603: ENDJS
                   1604: 
                   1605:     my $output .= $js_validate."\n".'<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   1606:                   &Apache::lonhtmlcommon::row_title('Course Description').
1.13      raeburn  1607:                  '<input type="text" size="40" name="cdescr" />';
                   1608:     my ($home_server_pick,$numlib) =
                   1609:         &Apache::loncommon::home_server_form_item($dom,'chome',
                   1610:                                                   'default','hide');
                   1611:     if ($numlib > 1) {
                   1612:         $output .= &Apache::lonhtmlcommon::row_closure().
1.16      raeburn  1613:                    &Apache::lonhtmlcommon::row_title(&mt('Home Server for Course'));
1.13      raeburn  1614:     }
                   1615:     $output .= $home_server_pick.
                   1616:                &Apache::lonhtmlcommon::row_closure(1).
                   1617:                &Apache::lonhtmlcommon::end_pick_box().'</div>'.
                   1618:                '<div>'.&clone_form($dom,$formname,$crstype).'</div>'."\n";
1.1       raeburn  1619:     return $output;
                   1620: }
                   1621: 
                   1622: sub clone_form {
                   1623:     my ($dom,$formname,$crstype) = @_;
                   1624:     my $type = 'Course';
                   1625:     if ($crstype eq 'community') {
                   1626:         $type = 'Community';
                   1627:     }
1.13      raeburn  1628:     my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedom').
                   1629:                     &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type);
1.4       raeburn  1630:     my %lt = &clone_text();
1.2       raeburn  1631:     my $output .= 
                   1632:         &Apache::lonhtmlcommon::start_pick_box(). 
                   1633:         &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
1.13      raeburn  1634:         '<input type="text" size="25" name="clonecrs" value="" onfocus="this.blur();'.
                   1635:         'opencrsbrowser('."'$formname','clonecrs','clonedom','','','','$type'".');" />'.
1.2       raeburn  1636:         '</label>'.&Apache::lonhtmlcommon::row_closure(1).'<label>'.
                   1637:         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'</label>'.
                   1638:         $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure().
                   1639:         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
                   1640:         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
                   1641:         '</label><br /><label>'.
                   1642:         '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
                   1643:         '</label><br /><label>'.
                   1644:         '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
                   1645:         $lt{'shd'}.'</label>'.
                   1646:         '<input type="text" size="5" name="dateshift" value="365" />'.
                   1647:         &Apache::lonhtmlcommon::row_closure(1).
                   1648:         &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1649:     return $output;
                   1650: }
                   1651: 
1.16      raeburn  1652: sub clone_text {
1.4       raeburn  1653:     return &Apache::lonlocal::texthash(
                   1654:                'cid'  => 'Course ID',
                   1655:                'dmn'  => 'Domain',
                   1656:                'dsh'  => 'Date Shift',
                   1657:                'ncd'  => 'Do not clone date parameters',
                   1658:                'prd'  => 'Clone date parameters as-is',
                   1659:                'shd'  => 'Shift date parameters by number of days',
                   1660:         );
                   1661: }
                   1662: 
1.1       raeburn  1663: sub coursecode_form {
1.2       raeburn  1664:     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
1.1       raeburn  1665:     my $output;
1.2       raeburn  1666:     my %rowtitle = (
                   1667:                     instcode  => 'Course Category',
                   1668:                     crosslist => 'Cross Listed Course',
                   1669:                    );
1.1       raeburn  1670:     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   1671:         (ref($cat_order))) {
1.2       raeburn  1672:         my ($sel,$instsec,$lcsec);
                   1673:         $sel = $context;
                   1674:         if ($context eq 'crosslist') {
                   1675:             $sel .= '_'.$num;
                   1676:             $instsec = &mt('Institutional section').'<br />'.
                   1677:                        '<input type="text" size="10" name="'.$sel.'_instsec" />';
                   1678:             $lcsec = &mt('LON-CAPA section').'<br />'.
                   1679:                      '<input type="text" size="10" name="'.$sel.'_lcsec" />';
                   1680:         }
1.1       raeburn  1681:         if (@{$codetitles} > 0) {
                   1682:             my $lastitem = pop(@{$codetitles});
1.2       raeburn  1683:             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
1.1       raeburn  1684:             if (@{$codetitles} > 0) {
1.2       raeburn  1685:                 $output = &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   1686:                           '<table><tr>';
1.1       raeburn  1687:                 if ($context eq 'crosslist') {
1.2       raeburn  1688:                     $output .= '<td>'.&mt('Include?').'<br />'.
                   1689:                                '<input type="checkbox" name="'.$sel.'" value="1" /></td>';
1.1       raeburn  1690:                 }
                   1691:                 foreach my $title (@{$codetitles}) {
                   1692:                     if (ref($cat_order->{$title}) eq 'ARRAY') {
                   1693:                         if (@{$cat_order->{$title}} > 0) {
                   1694:                             $output .= '<td align="center">'.$title.'<br />'."\n".
                   1695:                                        '<select name="'.$sel.'_'.$title.'">'."\n".
                   1696:                                        ' <option value="" selected="selected">'.
                   1697:                                        &mt('Select').'</option>'."\n";
                   1698:                             foreach my $item (@{$cat_order->{$title}}) {
                   1699:                                 my $longitem = $item;
                   1700:                                 if (ref($cat_titles->{$title}) eq 'HASH') {
                   1701:                                     if ($cat_titles->{$title}{$item} ne '') {
                   1702:                                         $longitem = $cat_titles->{$title}{$item};
                   1703:                                     }
                   1704:                                 }
                   1705:                                 $output .= '<option value="'.$item.'">'.$longitem.
                   1706:                                            '</option>'."\n";
                   1707:                             }
                   1708:                         }
                   1709:                         $output .= '</select></td>'."\n";
                   1710:                     }
                   1711:                 }
                   1712:                 if ($context eq 'crosslist') {
                   1713:                     $output .= '<td align="center">'.$lastitem.'<br />'."\n".
1.2       raeburn  1714:                                $lastinput.'</td><td align="center">'.$instsec.'</td>'.
                   1715:                                '<td align="center">'.$lcsec.'</td></tr></table>';
1.1       raeburn  1716:                 } else {
                   1717:                     $output .= '</tr></table>'.
                   1718:                                &Apache::lonhtmlcommon::row_closure().
                   1719:                                &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   1720:                                $lastinput;
                   1721:                 }
                   1722:             } else {
                   1723:                 if ($context eq 'crosslist') {
                   1724:                     $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
1.2       raeburn  1725:                                '<table><tr>'.
                   1726:                                '<td align="center">'.$lastitem.'<br />'.$lastinput.'</td>'.
                   1727:                                '<td align="center">'.$instsec.'</td><td>'.$lcsec.'</td>'.
                   1728:                                '</tr></table>';
1.1       raeburn  1729:                 } else { 
                   1730:                     $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   1731:                                $lastinput;
                   1732:                 }
                   1733:             }
1.2       raeburn  1734:             $output .=  &Apache::lonhtmlcommon::row_closure(1);
                   1735:             push(@$codetitles,$lastitem);    
                   1736:         } elsif ($context eq 'crosslist') {
                   1737:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   1738:                        '<table><tr><td align="center">'.
                   1739:                        '<span class="LC_nobreak">'.&mt('Include?').
                   1740:                        '<input type="checkbox" name="'.$sel.'" value="1" /></span>'.
                   1741:                        '</td><td align="center">'.&mt('Institutional ID').'<br />'.
                   1742:                        '<input type="text" size="10" name="'.$sel.'_instsec" />'.
                   1743:                        '</td><td align="center">'.$lcsec.'</td></tr></table>'.
                   1744:                        &Apache::lonhtmlcommon::row_closure(1);
1.1       raeburn  1745:         }
                   1746:     }
                   1747:     return $output;
                   1748: }
                   1749: 
                   1750: sub get_course_dom {
                   1751:     my $codedom = &Apache::lonnet::default_login_domain();
                   1752:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
                   1753:         $codedom = $env{'user.domain'};
                   1754:         if ($env{'request.role.domain'} ne '') {
                   1755:             $codedom = $env{'request.role.domain'};
                   1756:         }
                   1757:     }
                   1758:     if ($env{'form.showdom'} ne '') {
                   1759:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
                   1760:             $codedom = $env{'form.showdom'};
                   1761:         }
                   1762:     }
                   1763:     return $codedom;
                   1764: }
                   1765: 
                   1766: sub display_navbuttons {
1.16      raeburn  1767:     my ($r,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
1.1       raeburn  1768:     $r->print('<div class="LC_navbuttons">');
                   1769:     if ($prev) {
1.16      raeburn  1770:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
                   1771:                   'onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>'.
                   1772:                   ('&nbsp;'x3));
1.1       raeburn  1773:     } elsif ($prevtext) {
1.16      raeburn  1774:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
                   1775:                   'onclick="javascript:history.back()"/>'.('&nbsp;'x3));
                   1776:     }
                   1777:     if ($state eq 'details') {
                   1778:         $r->print(' <input type="button" name="other" value="'.$othertext.'" '.
                   1779:                   'onclick="javascript:nextPage(document.'.$formname.','."'".$other."'".
                   1780:                   ')" />');
1.1       raeburn  1781:     }
1.15      raeburn  1782:     if ($state eq 'courseinfo') {
1.16      raeburn  1783:         $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
                   1784:                   'onclick="javascript:validateForm();" />');
1.15      raeburn  1785:     } elsif ($next) {
1.1       raeburn  1786:         $r->print('
1.16      raeburn  1787:       <input type="button" name="next" value="'.$nexttext.'" '.
                   1788:       'onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
1.1       raeburn  1789:     }
                   1790:     $r->print('</div>');
                   1791: }
                   1792: 
                   1793: sub print_request_outcome {
1.10      raeburn  1794:     my ($dom,$codetitles,$code_order) = @_;
1.13      raeburn  1795:     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
1.10      raeburn  1796:         %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,);
                   1797:     $cnum = $env{'form.cnum'};
1.8       raeburn  1798:     unless ($cnum =~ /^$match_courseid$/) {
                   1799:         $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
                   1800:         return $output;
                   1801:     }
1.11      raeburn  1802: 
1.10      raeburn  1803:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
1.9       raeburn  1804:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   1805:         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
                   1806:             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
                   1807:         }
                   1808:     }
1.10      raeburn  1809:     $now = time;
                   1810:     $crstype = $env{'form.crstype'};
1.17      raeburn  1811:     my @instsections;
1.8       raeburn  1812:     if ($crstype eq 'official') {
                   1813:         if (&Apache::lonnet::auto_run('',$dom)) {
1.13      raeburn  1814:             ($enrollstart,$enrollend)=&dates_from_form('enrollstart','enrollend');
1.8       raeburn  1815:         }
1.10      raeburn  1816:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   1817:             if ($env{'form.sec_'.$i}) {
                   1818:                 if ($env{'form.secnum_'.$i} ne '') {
1.17      raeburn  1819:                     my $sec = $env{'form.secnum_'.$i};
                   1820:                     $sections{$i}{'inst'} = $sec;
                   1821:                     if (($sec ne '') && (!grep(/^\Q$sec\E$/,@instsections))) {
                   1822:                         push(@instsections,$sec);
                   1823:                     }
1.13      raeburn  1824:                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
1.10      raeburn  1825:                 }
                   1826:             }
                   1827:         }
                   1828:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                   1829:             if ($env{'form.crosslist_'.$i}) {
                   1830:                 my $xlistinfo = '';
                   1831:                 if (ref($code_order) eq 'ARRAY') {
                   1832:                     if (@{$code_order} > 0) {
                   1833:                         foreach my $item (@{$code_order}) {
                   1834:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
                   1835:                         }
                   1836:                     }
                   1837:                 }
                   1838:                 $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'};
1.13      raeburn  1839:                 $crosslistings{$i}{'inst'} = $xlistinfo;
                   1840:                 $crosslistings{$i}{'loncapa'} = $env{'form.crosslist_'.$i.'_lcsec'};
1.10      raeburn  1841:             }
                   1842:         }
1.14      raeburn  1843:     } else {
                   1844:         $enrollstart = '';
                   1845:         $enrollend = '';
1.10      raeburn  1846:     }
                   1847:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1848:         my $uname = $env{'form.person_'.$i.'_uname'};
1.16      raeburn  1849:         my $udom = $env{'form.person_'.$i.'_dom'};
1.10      raeburn  1850:         if (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
                   1851:             if (&Apache::lonnet::domain($udom) ne '') {
1.13      raeburn  1852:                 unless (ref($personnel{$uname.':'.$udom}) eq 'HASH') {
                   1853:                     $personnel{$uname.':'.$udom} = {
                   1854:                              firstname    => $env{'form.person_'.$i.'_firstname'},
                   1855:                              lastname     => $env{'form.person_'.$i.'_lastname'},
                   1856:                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
                   1857:                                                    };
                   1858:                 }
                   1859:                 my $role = $env{'form.person_'.$i.'_role'};
                   1860:                 unless ($role eq '') {
1.16      raeburn  1861:                     if (ref($personnel{$uname.':'.$udom}{'roles'}) eq 'ARRAY') {
1.13      raeburn  1862:                         my @curr_roles = @{$personnel{$uname.':'.$udom}{'roles'}};
                   1863:                         unless (grep(/^\Q$role\E$/,@curr_roles)) {
                   1864:                             push(@{$personnel{$uname.':'.$udom}{'roles'}},$role);
                   1865:                         }
                   1866:                     } else {
                   1867:                         @{$personnel{$uname.':'.$udom}{'roles'}} = ($role);
                   1868:                     }
                   1869:                     if ($role eq 'cc') {
                   1870:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
                   1871:                     } else {
1.14      raeburn  1872:                         my @currsec = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
                   1873:                         my $newsec = $env{'form.person_'.$i.'_newsec'};
                   1874:                         $newsec =~ s/^\s+//;
                   1875:                         $newsec =~s/\s+$//;
                   1876:                         my @newsecs = split(/[\s,;]+/,$newsec);
                   1877:                         foreach my $sec (@newsecs) {
                   1878:                             next if ($sec =~ /\W/);
                   1879:                             next if ($newsec eq 'none');
                   1880:                             if ($sec ne '') {
                   1881:                                 unless (grep(/^\Q$sec\E$/,@currsec)) {
                   1882:                                     push(@currsec,$sec);
1.13      raeburn  1883:                                 }
                   1884:                             }
                   1885:                         }
                   1886:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @currsec;
                   1887:                     }
                   1888:                 }
1.10      raeburn  1889:             } else {
                   1890:                 push(@missingdom,$uname.':'.$udom);
                   1891:             }
                   1892:         } else {
                   1893:             push(@baduname,$uname.':'.$udom);
                   1894:         }
1.8       raeburn  1895:     }
1.13      raeburn  1896:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
1.14      raeburn  1897:     my $autodrops = 0;
                   1898:     if ($env{'form.autodrops'}) {
                   1899:         $autodrops = $env{'form.autodrops'}; 
                   1900:     }
                   1901:     my $autoadds = 0;
                   1902:     if ($env{'form.autoadds'}) {
                   1903:         $autodrops = $env{'form.autoadds'};
                   1904:     }
                   1905:     my $sectotal = 0;
                   1906:     if ($env{'form.sectotal'}) {
                   1907:         $sectotal = $env{'form.sectotal'};
                   1908:     }
                   1909:     if ($env{'form.autoadds'}) {
                   1910:         $autodrops = $env{'form.autoadds'};
                   1911:     }
                   1912:     my $crosslisttotal = 0;
                   1913:     if ($env{'form.crosslisttotal'}) {
                   1914:         $crosslisttotal = $env{'form.crosslisttotal'};
                   1915:     }
                   1916:     my $instcode = '';
                   1917:     if (exists($env{'form.instcode'})) {
                   1918:         $instcode = $env{'form.instcode'};
                   1919:     }
1.15      raeburn  1920:     my $clonecrs = '';
                   1921:     my $clonedom = '';
                   1922:     if (($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
                   1923:         ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
1.16      raeburn  1924:         my $clonehome = &Apache::lonnet::homeserver($env{'form.clonecrs'},
                   1925:                                                     $env{'form.clonedom'});
1.15      raeburn  1926:         if ($clonehome ne 'no_host') {  
1.16      raeburn  1927:             my $canclone =  
                   1928:                 &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
                   1929:                         $env{'user.domain'},$env{'form.clonecrs'}, $env{'form.clonedom'});
1.15      raeburn  1930:             if ($canclone) {
                   1931:                 $clonecrs = $env{'form.clonecrs'};
                   1932:                 $clonedom = $env{'form.clonedom'};
                   1933:             }
                   1934:         }
                   1935:     }
1.8       raeburn  1936:     my $details = {
1.10      raeburn  1937:                     owner          => $env{'user.name'},
                   1938:                     domain         => $env{'user.domain'}, 
                   1939:                     cdom           => $dom,
1.11      raeburn  1940:                     cnum           => $cnum,
1.13      raeburn  1941:                     coursehome     => $env{'form.chome'},
                   1942:                     cdescr         => $env{'form.cdescr'},
1.10      raeburn  1943:                     crstype        => $env{'form.crstype'},
1.14      raeburn  1944:                     instcode       => $instcode,
1.15      raeburn  1945:                     clonedom       => $clonedom,
                   1946:                     clonecrs       => $clonecrs,
1.10      raeburn  1947:                     datemode       => $env{'form.datemode'},
1.14      raeburn  1948:                     dateshift      => $env{'form.dateshift'},
                   1949:                     sectotal       => $sectotal,
1.10      raeburn  1950:                     sections       => \%sections,
1.14      raeburn  1951:                     crosslisttotal => $crosslisttotal,
1.13      raeburn  1952:                     crosslists     => \%crosslistings,
1.14      raeburn  1953:                     autoadds       => $autoadds,
                   1954:                     autodrops      => $autodrops,
1.13      raeburn  1955:                     enrollstart    => $enrollstart,
                   1956:                     enrollend      => $enrollend,
                   1957:                     accessstart    => $accessstart,
                   1958:                     accessend      => $accessend,
1.10      raeburn  1959:                     personnel      => \%personnel,
1.8       raeburn  1960:                   };
                   1961:     my @inststatuses;
1.9       raeburn  1962:     my $val = &get_processtype($dom,$crstype,\@inststatuses,\%domconfig);
1.8       raeburn  1963:     if ($val eq '') {
                   1964:         if ($crstype eq 'official') {
                   1965:             $output = &mt('You are not permitted to request creation of official courses');
                   1966:         } elsif ($crstype eq 'unofficial') {
                   1967:             $output = &mt('You are not permitted to request creation of unofficial courses');
                   1968:         } elsif ($crstype eq 'community') {
                   1969:             $output = &mt('You are not permitted to request creation of communities');
                   1970:         } else {
                   1971:             $output = &mt('Unrecognized course type: [_1]',$crstype);
                   1972:         }
                   1973:     } else {
1.14      raeburn  1974:         my ($disposition,$message,$reqstatus);
1.8       raeburn  1975:         my %reqhash = (
1.14      raeburn  1976:                         reqtime   => $now,
1.10      raeburn  1977:                         crstype   => $crstype,
                   1978:                         details   => $details,
1.8       raeburn  1979:                       );
                   1980:         my $requestkey = $dom.'_'.$cnum;
1.17      raeburn  1981:         my $validationerror;
1.10      raeburn  1982:         if ($val eq 'autolimit=') {
                   1983:             $disposition = 'process';
                   1984:         } elsif ($val =~ /^autolimit=(\d+)$/) {
                   1985:             my $limit = $1;
1.8       raeburn  1986:             $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
1.10      raeburn  1987:                                             $dom,$crstype,$limit,\$message);
1.8       raeburn  1988:         } elsif ($val eq 'validate') {
1.17      raeburn  1989:             my $inststatuslist;
                   1990:             if (@inststatuses > 0) {
                   1991:                 $inststatuslist = join(',',@inststatuses);
                   1992:             }
                   1993:             my $instseclist;
                   1994:             if (@instsections > 0) {
                   1995:                 $instseclist = join(',',@instsections);
                   1996:             }
                   1997:             my $validation = 
                   1998:                 &Apache::lonnet::auto_validate_instcode('',$dom,$instcode,
                   1999:                     $env{'user.name'}.':'.$env{'user.domain'},$inststatuslist,
                   2000:                     $instseclist);
                   2001:             if ($validation eq 'valid') {
                   2002:                 $disposition = 'process';
                   2003:             } elsif ($validation eq 'rejected') {
                   2004:                 $disposition = 'rejected';
                   2005:             } elsif ($validation eq 'pending') {
                   2006:                 $disposition = 'pending';
                   2007:             } elsif ($disposition eq 'approval') {
                   2008:                 $disposition = 'approval';
                   2009:             } elsif ($disposition =~ /^error(.*)$/) {
                   2010:                 $disposition = 'approval';
                   2011:                 $validationerror = $1;
                   2012:             } else {
                   2013:                 $disposition = 'approval';  
                   2014:             }
1.8       raeburn  2015:         } else {
                   2016:             $disposition = 'approval';
                   2017:         }
1.14      raeburn  2018:         $reqhash{'disposition'} = $disposition;
                   2019:         $reqstatus = $disposition;
1.16      raeburn  2020:         my ($modified,$queued);
1.8       raeburn  2021:         if ($disposition eq 'rejected') {
                   2022:             $output = &mt('Your course request was rejected.');
                   2023:             if ($message) {
                   2024:                 $output .= '<div class="LC_warning">'.$message.'</div>';
                   2025:             }
                   2026:         } elsif ($disposition eq 'process') {
1.14      raeburn  2027:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
                   2028:             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles);
                   2029:             my @roles = &Apache::lonuserutils::roles_by_context('course');
1.8       raeburn  2030:             my $type = 'Course';
                   2031:             if ($crstype eq 'community') {
                   2032:                 $type = 'Community';
                   2033:             }
                   2034:             foreach my $role (@roles) {
                   2035:                 $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
                   2036:             }
1.14      raeburn  2037:             my $result = &Apache::loncoursequeueadmin::course_creation($dom,$cnum,
                   2038:                                    'autocreate',$details,\$logmsg,\$newusermsg,\$addresult,
                   2039:                                    \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
                   2040:             if ($result eq 'created') {
1.8       raeburn  2041:                 $disposition = 'created';
1.14      raeburn  2042:                 $reqstatus = 'created';
                   2043:                 $output = &mt('Your course request has been processed and the course has been created.').
                   2044:                           '<br />'.
                   2045:                           &mt('You will need to logout and log-in again to be able to select a role in the course.');
1.8       raeburn  2046:             } else {
1.14      raeburn  2047:                 $output = '<span class="LC_error">'.
                   2048:                           &mt('An error occurred when processing your course request.').
                   2049:                           '<br />'.
                   2050:                           &mt('You may want to review the request details and submit the request again.').
                   2051:                           '</span>';
1.8       raeburn  2052:             }
                   2053:         } else {
                   2054:             my $requestid = $cnum.'_'.$disposition;
                   2055:             my $request = { 
                   2056:                             $requestid => {
                   2057:                                             timestamp   => $now,
                   2058:                                             crstype     => $crstype,
                   2059:                                             ownername   => $env{'user.name'},
                   2060:                                             ownerdom    => $env{'user.domain'},
1.13      raeburn  2061:                                             description => $env{'form.cdescr'}, 
1.8       raeburn  2062:                                           },
                   2063:                           };
1.16      raeburn  2064:             my $statuskey = 'status:'.$dom.':'.$cnum;
                   2065:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
                   2066:                                                    $env{'user.domain'},$env{'user.name'});
1.17      raeburn  2067:             if ($userreqhash{$statuskey} ne '') {
1.16      raeburn  2068:                 $modified = 1;
                   2069:                 my %queuehash = &Apache::lonnet::get_dom('courserequestqueue',
                   2070:                                                          [$cnum.'_approval',
                   2071:                                                           $cnum.'_pending'],$dom);
1.17      raeburn  2072:                 if (($queuehash{$cnum.'_approval'} ne '') || 
                   2073:                     ($queuehash{$cnum.'_pending'} ne '')) {
1.16      raeburn  2074:                     $queued = 1;
                   2075:                 }
                   2076:             }
                   2077:             unless ($queued) {
                   2078:                 my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
                   2079:                                                             $dom);
                   2080:                 if ($putresult eq 'ok') {
                   2081:                     $output = &mt('Your course request has been recorded.').'<br />'.
                   2082:                               &notification_information($disposition,$req_notifylist,
                   2083:                                                         $cnum,$now);
1.8       raeburn  2084:                 } else {
1.16      raeburn  2085:                     $reqstatus = 'domainerror';
                   2086:                     $reqhash{'disposition'} = $disposition;
                   2087:                     my $warning = &mt('An error occurred saving your request in the pending requests queue.');
                   2088:                     $output = '<span class"LC_warning">'.$warning.'</span><br />';
1.8       raeburn  2089:                 }
                   2090:             }
                   2091:         }
1.14      raeburn  2092:         my ($storeresult,$statusresult);
1.10      raeburn  2093:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2094:             $storeresult = &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                   2095:                                                            'courserequests');
1.14      raeburn  2096:             if ($storeresult eq 'ok') {
                   2097:                 my %status = (
                   2098:                                  'status:'.$dom.':'.$cnum => $reqstatus,
                   2099:                              );
                   2100:                 $statusresult = &Apache::lonnet::put('courserequests',\%status); 
                   2101:             }
1.10      raeburn  2102:         } else {
                   2103:             $storeresult = 'error: invalid requestkey format'; 
                   2104:         }
1.8       raeburn  2105:         if ($storeresult ne 'ok') {
1.13      raeburn  2106:             $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the details of your request: [_1].',$storeresult).'</span><br />';
                   2107:             &Apache::lonnet::logthis("Error saving course request - $requestkey for $env{'user.name'}:$env{'user.domain'} - $storeresult");
1.14      raeburn  2108:         } elsif ($statusresult ne 'ok') {
                   2109:              $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the status of your request: [_1].',$statusresult).'</span><br />';
                   2110:             &Apache::lonnet::logthis("Error saving course request status for  $requestkey (for $env{'user.name'}:$env{'user.domain'}) - $statusresult");
1.8       raeburn  2111:         }
1.16      raeburn  2112:         if ($modified && $queued && $storeresult eq 'ok') {
                   2113:             $output .= '<p>'.&mt('Your course request has been updated').'</p>'.
                   2114:                        &notification_information($disposition,$req_notifylist,$cnum,$now);
                   2115:         }
1.17      raeburn  2116:         if ($validationerror ne '') {
                   2117:             $output .= '<span class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: {_1].',$validationerror).'</p>';
                   2118:         }
1.16      raeburn  2119:     }
                   2120:     return $output;
                   2121: }
                   2122: 
                   2123: sub notification_information {
                   2124:     my ($disposition,$req_notifylist,$cnum,$now) = @_;
                   2125:     my %emails = &Apache::loncommon::getemails();
                   2126:     my $address;
                   2127:     if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                   2128:         $address = $emails{'permanentemail'};
                   2129:         if ($address eq '') {
                   2130:             $address = $emails{'notification'};
                   2131:         }
                   2132:     }
                   2133:     my $output;
                   2134:     if ($disposition eq 'approval') {
                   2135:         $output .= &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
                   2136:                    &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
                   2137:         if ($address ne '') {
                   2138:             $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                   2139:         }
                   2140:         if ($req_notifylist) {
                   2141:             my $fullname = &Apache::loncommon::plainname($env{'user.name'},
                   2142:                                                                      $env{'user.domain'});
                   2143:             my $sender = $env{'user.name'}.':'.$env{'user.domain'};
                   2144:             &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,"$fullname ($env{'user.name'}:$env{'user.domain'})",$cnum,$env{'form.cdescr'},$now,'coursereq',$sender);
                   2145:         }
1.17      raeburn  2146:     } elsif ($disposition eq 'pending') {
1.16      raeburn  2147:         $output .= '<div class="LC_info">'.
                   2148: &mt('Your request has been placed in a queue pending administrative action.').'<br />'.
                   2149: &mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
                   2150: &mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
                   2151:                    '</div>';
1.17      raeburn  2152:     } else {
                   2153:         $output .= '<div class="LC_warning">'.
                   2154:                    &mt('Your request status is: [_1].',$disposition). 
                   2155:                    '</div>'
1.8       raeburn  2156:     }
                   2157:     return $output;
                   2158: }
                   2159: 
                   2160: sub get_processtype {
1.9       raeburn  2161:     my ($dom,$crstype,$inststatuses,$domconfig) = @_;
                   2162:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
1.8       raeburn  2163:     my (%userenv,%settings,$val);
                   2164:     my @options = ('autolimit','validate','approve');
                   2165:     if ($dom eq $env{'user.domain'}) {
                   2166:         %userenv = 
                   2167:             &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
                   2168:                 'requestcourses.'.$crstype,'inststatus');
                   2169:         if ($userenv{'requestcourses.'.$crstype}) {
                   2170:             $val = $userenv{'requestcourses.'.$crstype};
                   2171:             @{$inststatuses} = ('_custom_');
                   2172:         } else {
                   2173:             my ($task,%alltasks);
1.9       raeburn  2174:             if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
                   2175:                 %settings = %{$domconfig->{'requestcourses'}};
1.8       raeburn  2176:                 if (ref($settings{$crstype}) eq 'HASH') {
                   2177:                     if (($env{'user.adv'}) && (exists($settings{$crstype}{'_LC_adv'}))) {
                   2178:                         $val = $settings{$crstype}{'_LC_adv'};
                   2179:                         @{$inststatuses} = ('_LC_adv_');
                   2180:                     } else {
                   2181:                         if ($userenv{'inststatus'} ne '') {
                   2182:                             @{$inststatuses} = split(',',$userenv{'inststatus'});
                   2183:                         } else {
1.13      raeburn  2184:                             @{$inststatuses} = ('default');
1.8       raeburn  2185:                         }
                   2186:                         foreach my $status (@{$inststatuses}) {
                   2187:                             if (exists($settings{$crstype}{$status})) {
                   2188:                                 my $value = $settings{$crstype}{$status};
                   2189:                                 next unless ($value); 
                   2190:                                 unless (exists($alltasks{$value})) {
                   2191:                                     if (ref($alltasks{$value}) eq 'ARRAY') {
                   2192:                                         unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                   2193:                                             push(@{$alltasks{$value}},$status); 
                   2194:                                         }
                   2195:                                     } else {
                   2196:                                         @{$alltasks{$value}} = ($status);
                   2197:                                     }
                   2198:                                 }
                   2199:                             }
                   2200:                         }
                   2201:                         my $maxlimit = 0;
1.13      raeburn  2202:                         
1.8       raeburn  2203:                         foreach my $key (sort(keys(%alltasks))) {
                   2204:                             if ($key =~ /^autolimit=(\d*)$/) {
                   2205:                                 if ($1 eq '') {
                   2206:                                     $val ='autolimit=';
                   2207:                                     last;
                   2208:                                 } elsif ($1 > $maxlimit) {
                   2209:                                     $maxlimit = $1; 
                   2210:                                 }
                   2211:                             }
                   2212:                         }
                   2213:                         if ($maxlimit) {
                   2214:                             $val = 'autolimit='.$maxlimit;
                   2215:                         } else {
                   2216:                             foreach my $option (@options) {
                   2217:                                 if ($alltasks{$option}) {
                   2218:                                     $val = $option;
                   2219:                                     last;  
                   2220:                                 }
                   2221:                             }
                   2222:                         }
                   2223:                     }
                   2224:                 }
                   2225:             }
                   2226:         }
                   2227:     } else {
                   2228:         %userenv = &Apache::lonnet::userenvironment($env{'user.domain'},
                   2229:                       $env{'user.name'},'reqcrsotherdom.'.$env{'form.crstype'});
                   2230:         if ($userenv{'reqcrsotherdom'}) {
                   2231:             my @doms = split(',',$userenv{'reqcrsotherdom'});
                   2232:             my $optregex = join('|',@options); 
                   2233:             if (grep(/^\Q$dom\E:($optregex=?\d*)/,@doms)) {
                   2234:                 $val = $1;
                   2235:             }
                   2236:             @{$inststatuses} = ('_external_');
                   2237:         }
                   2238:     }
                   2239:     return $val;
                   2240: }
                   2241: 
                   2242: sub check_autolimit {
1.10      raeburn  2243:     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
                   2244:     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
                   2245:                                         'userroles',['active','future'],['cc'],[$dom]);
                   2246:     my ($types,$typename) = &course_types();
                   2247:     my %requests = &Apache::lonnet::dumpstore('courserequests',$udom,$uname);
                   2248:     my %count;
                   2249:     if (ref($types) eq 'ARRAY') {
                   2250:         foreach my $type (@{$types}) {
                   2251:             $count{$type} = 0;
                   2252:         }
                   2253:     }
                   2254:     foreach my $key (keys(%requests)) {
                   2255:         my ($cdom,$cnum) = split('_',$key);
                   2256:         if (exists($crsroles{$cnum.':'.$cdom.':cc'})) {
                   2257:             if (ref($requests{$key}) eq 'HASH') {
                   2258:                 my $type = $requests{$key}{'crstype'};
                   2259:                 if ($type =~ /^official|unofficial|community$/) {
                   2260:                     $count{$type} ++;
                   2261:                 }
                   2262:             }
                   2263:         }
                   2264:     }
                   2265:     if ($count{$crstype} < $limit) {
                   2266:         return 'process';
                   2267:     } else {
                   2268:         if (ref($typename) eq 'HASH') {
                   2269:             $$message = &mt('Your request has not been processed because you have reached the limit for the number of courses of this type.').'<br />'.&mt("Your $typename->{$crstype} limit is [_1].",$limit);
                   2270:         }
                   2271:         return 'rejected';
                   2272:     }
1.1       raeburn  2273:     return;
                   2274: }
                   2275: 
1.2       raeburn  2276: sub retrieve_settings {
1.16      raeburn  2277:     my ($dom,$cnum) = @_;
                   2278:     my ($result,%reqinfo) = &get_request_settings($dom,$cnum);
                   2279:     if ($result eq 'ok') {
                   2280:         if (($env{'user.name'} eq $reqinfo{'owner'}) && 
                   2281:             ($env{'user.domain'} eq $reqinfo{'domain'})) {
                   2282:             $env{'form.chome'} = $reqinfo{'coursehome'};
                   2283:             $env{'form.cdescr'} = $reqinfo{'cdescr'};
                   2284:             $env{'form.crstype'} = $reqinfo{'crstype'}; 
                   2285:             &generate_date_items($reqinfo{'accessstart'},'accessstart');
                   2286:             &generate_date_items($reqinfo{'accessend'},'accessend');
                   2287:             if ($reqinfo{'accessend'} == 0) {
                   2288:                 $env{'form.no_end_date'} = 1;
                   2289:             }
                   2290:             if (($reqinfo{'crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
                   2291:                 &generate_date_items($reqinfo{'enrollstart'},'enrollstart');
                   2292:                 &generate_date_items($reqinfo{'enrollend'},'enrollend');
                   2293:             }
                   2294:             $env{'form.clonecrs'} = $reqinfo{'clonecrs'};
                   2295:             $env{'form.clonedom'} = $reqinfo{'clonedom'};
                   2296:             $env{'form.datemode'} = $reqinfo{'datemode'};
                   2297:             $env{'form.dateshift'} = $reqinfo{'dateshift'};
                   2298:             if (($reqinfo{'crstype'} eq 'official') && ($reqinfo{'instcode'} ne '')) { 
                   2299:                  $env{'form.sectotal'} = $reqinfo{'sectotal'};
                   2300:                  $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
                   2301:                  $env{'form.autoadds'} = $reqinfo{'autoadds'};
                   2302:                  $env{'form.autdrops'} = $reqinfo{'autodrops'};
                   2303:                  $env{'form.instcode'} = $reqinfo{'instcode'};
                   2304:                  my %crscode = (
                   2305:                                  instcode => $reqinfo{'instcode'},
                   2306:                                );
                   2307:                  &extract_instcode($dom,'instcode',\%crscode);
                   2308:             }
                   2309:             my @currsec;
                   2310:             if (ref($reqinfo{'sections'}) eq 'HASH') {
                   2311:                 foreach my $i (sort(keys(%{$reqinfo{'sections'}}))) {
                   2312:                     if (ref($reqinfo{'sections'}{$i}) eq 'HASH') {
                   2313:                         my $sec = $reqinfo{'sections'}{$i}{'inst'};;
                   2314:                         $env{'form.secnum_'.$i} = $sec;
                   2315:                         if (!grep(/^\Q$sec\E$/,@currsec)) {
                   2316:                             push(@currsec,$sec);
                   2317:                         }
                   2318:                         $env{'form.loncapasec_'.$i} = $reqinfo{'sections'}{$i}{'loncapa'};
                   2319:                     }
                   2320:                 }
                   2321:             }
                   2322:             if (ref($reqinfo{'crosslistings'}) eq 'HASH') {
                   2323:                 foreach my $i (sort(keys(%{$reqinfo{'crosslistings'}}))) {
                   2324:                     if (ref($reqinfo{'crosslistings'}{$i}) eq 'HASH') {
                   2325:                         $env{'form.crosslist_'.$i.'_lcsec'} = $reqinfo{'crosslistings'}{$i}{'loncapa'};
                   2326:                         my $xlistsec = $reqinfo{'crosslistings'}{$i}{'instsec'};
                   2327:                         my %crscode = (
                   2328:                                         $i => $reqinfo{'crosslistings'}{$i}{'instcode'},
                   2329:                                       );
                   2330:                         &extract_instcode($dom,'crosslist',\%crscode,$1,$xlistsec);
                   2331:                     }
                   2332:                 }
                   2333:             }
                   2334:             if (ref($reqinfo{'personnel'}) eq 'HASH') {
                   2335:                 my $i = 0;
                   2336:                 foreach my $user (sort(keys(%{$reqinfo{'personnel'}}))) {
                   2337:                     my ($uname,$udom) = split(':',$user);
                   2338:                     if (ref($reqinfo{'personnel'}{$user}) eq 'HASH') {
                   2339:                         if (ref($reqinfo{'personnel'}{$user}{'roles'}) eq 'ARRAY') {
                   2340:                             foreach my $role (sort(@{$reqinfo{'personnel'}{$user}{'roles'}})) {
                   2341:                                 $env{'form.person_'.$i.'_role'} = $role;
                   2342:                                 $env{'form.person_'.$i.'_firstname'} = $reqinfo{'personnel'}{$user}{'firstname'};
                   2343:                                 $env{'form.person_'.$i.'_lastname'} = $reqinfo{'personnel'}{$user}{'lastname'}; ;
                   2344:                                 $env{'form.person_'.$i.'_emailaddr'} = $reqinfo{'personnel'}{$user}{'emailaddr'};
                   2345:                                 $env{'form.person_'.$i.'_uname'} = $uname;
                   2346:                                 $env{'form.person_'.$i.'_dom'} = $udom;
                   2347:                                 if (ref($reqinfo{'personnel'}{$user}{$role}) eq 'HASH') {
                   2348:                                     if (ref($reqinfo{'personnel'}{$user}{$role}{'usec'}) eq 'ARRAY') {
                   2349:                                         my @usecs = @{$reqinfo{'personnel'}{$user}{$role}{'usec'}};
                   2350:                                         my @newsecs;
                   2351:                                         if (@usecs > 0) {
                   2352:                                             foreach my $sec (@usecs) {
                   2353:                                                 if (grep(/^\Q$sec\E/,@currsec)) {
                   2354:                                                     $env{'form.person_'.$i.'_sec'} = $sec;
                   2355:                                                 } else {
                   2356:                                                     push (@newsecs,$sec);
                   2357:                                                 }
                   2358:                                             }
                   2359:                                         }
                   2360:                                         if (@newsecs > 0) {
                   2361:                                             $env{'form.person_'.$i.'_newsec'} = join(',',@newsecs); 
                   2362:                                         }
                   2363:                                     }
                   2364:                                 }
                   2365:                                 $i ++;
                   2366:                             }
                   2367:                         }
                   2368:                     }
                   2369:                 }
                   2370:                 $env{'form.persontotal'} = $i;
                   2371:             }
                   2372:         }
                   2373:     }
                   2374:     return $result;
                   2375: }
                   2376: 
                   2377: sub get_request_settings {
                   2378:     my ($dom,$cnum) = @_;
                   2379:     my $requestkey = $dom.'_'.$cnum;
                   2380:     my ($result,%reqinfo);
                   2381:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2382:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   2383:                                                $env{'user.domain'},$env{'user.name'});
                   2384:         my $disposition = $history{'disposition'};
                   2385:         if (($disposition eq 'approval') || ($disposition eq 'pending')) { 
                   2386:             if (ref($history{'details'}) eq 'HASH') {
                   2387:                 %reqinfo = %{$history{'details'}};
                   2388:                 $result = 'ok';
                   2389:             } else {
                   2390:                 $result = 'nothash';
                   2391:             }
                   2392:         } else {
                   2393:             $result = 'notqueued';
                   2394:         }
                   2395:     } else {
                   2396:         $result = 'invalid';
                   2397:     }
                   2398:     return ($result,%reqinfo);
                   2399: }
1.2       raeburn  2400: 
1.16      raeburn  2401: sub extract_instcode {
                   2402:     my ($cdom,$element,$crscodehashref,$counter,$xlistsec) = @_;
                   2403:     my (%codes,@codetitles,%cat_titles,%cat_order);
                   2404:     if (ref($crscodehashref) eq 'HASH') {
                   2405:         if (&Apache::lonnet::auto_instcode_format('requests',$cdom,$crscodehashref,
                   2406:                        \%codes,\@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
                   2407:             if (@codetitles > 0) {
                   2408:                 my $sel = $element;
                   2409:                 if ($element eq 'crosslist') {
                   2410:                     $sel .= '_'.$counter;
                   2411:                 }
                   2412:                 my $lastitem = pop(@codetitles);
                   2413:                 foreach my $title (@codetitles) {
                   2414:                     if (ref($cat_order{$title}) eq 'ARRAY') {
                   2415:                         if (@{$cat_order{$title}} > 0) {
                   2416:                             $env{'form.'.$sel.'_'.$title} = $cat_order{$title}[0];
                   2417:                         }
                   2418:                     }
                   2419:                 }
                   2420:                 if ($element eq 'crosslist') { 
                   2421:                     $env{'form.'.$sel.'_'.$lastitem} = $xlistsec; 
                   2422:                 }
                   2423:             }
                   2424:         }
                   2425:     }
                   2426:     return;
1.2       raeburn  2427: }
                   2428: 
1.16      raeburn  2429: sub generate_date_items {
                   2430:     my ($currentval,$item) = @_;
                   2431:     if ($currentval =~ /\d+/) {
                   2432:         my ($tzname,$sec,$min,$hour,$mday,$month,$year) = 
                   2433:             &Apache::lonhtmlcommon::get_timedates($currentval);
                   2434:         $env{'form.'.$item.'_day'} = $mday;
                   2435:         $env{'form.'.$item.'_month'} = $month+1;
                   2436:         $env{'form.'.$item.'_year'} = $year;
                   2437:     }
                   2438:     return;
1.2       raeburn  2439: }
                   2440: 
1.1       raeburn  2441: 1;
                   2442: 

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