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

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

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