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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Request a course
                      3: #
1.13    ! raeburn     4: # $Id: lonrequestcourse.pm,v 1.12 2009/08/12 14:39:23 raeburn Exp $
1.1       raeburn     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: =head1 NAME
                     31: 
                     32: Apache::lonrequestcourse.pm
                     33: 
                     34: =head1 SYNOPSIS
                     35: 
                     36: Allows users to request creation of new courses.
                     37: 
                     38: This is part of the LearningOnline Network with CAPA project
                     39: described at http://www.lon-capa.org.
                     40: 
                     41: =head1 SUBROUTINES
                     42: 
                     43: =over
                     44: 
                     45: =item handler()
                     46: 
1.12      raeburn    47: =item header()
                     48: 
                     49: =item form_elements()
                     50: 
                     51: =item onload_action()
                     52: 
                     53: =item check_can_request() 
                     54: 
                     55: =item course_types()
                     56: 
                     57: =item print_main_menu()
                     58: 
                     59: =item request_administration()
                     60: 
                     61: =item print_request_form()
                     62: 
                     63: =item print_enrollment_menu()
                     64: 
                     65: =item inst_section_selector()
                     66: 
                     67: =item date_setting_table()
                     68: 
                     69: =item print_personnel_menu()
                     70: 
                     71: =item print_request_status()
                     72: 
                     73: =item print_request_logs()
                     74: 
                     75: =item print_review()
                     76: 
                     77: =item dates_from_form()
                     78: 
                     79: =item courseinfo_form()
                     80: 
                     81: =item clone_form()
                     82: 
                     83: =item clone_text()
                     84: 
                     85: =item coursecode_form()
                     86: 
                     87: =item get_course_dom()
                     88: 
                     89: =item display_navbuttons()
                     90: 
                     91: =item print_request_outcome()
                     92: 
                     93: =item get_processtype()
                     94: 
                     95: =item check_autolimit()
                     96: 
                     97: =item build_batchcreatehash()
                     98: 
                     99: =item retrieve_settings()
                    100: 
                    101: =item get_request_settings()
                    102: 
1.1       raeburn   103: =back
                    104: 
                    105: =cut
                    106: 
                    107: package Apache::lonrequestcourse;
                    108: 
                    109: use strict;
                    110: use Apache::Constants qw(:common :http);
                    111: use Apache::lonnet;
                    112: use Apache::loncommon;
                    113: use Apache::lonlocal;
1.8       raeburn   114: use Apache::loncoursequeueadmin;
1.4       raeburn   115: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn   116: 
                    117: sub handler {
                    118:     my ($r) = @_;
                    119:     if ($r->header_only) {
                    120:         &Apache::loncommon::content_type($r,'text/html');
                    121:         $r->send_http_header;
                    122:         return OK;
                    123:     }
                    124:     &Apache::loncommon::content_type($r,'text/html');
                    125:     $r->send_http_header;
                    126: 
1.2       raeburn   127:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    128:     my $dom = &get_course_dom();
1.1       raeburn   129:     my $action = $env{'form.action'};
                    130:     my $state = $env{'form.state'};
1.2       raeburn   131: 
                    132:     my %stored;
                    133:     my $jscript;
                    134:     if ((defined($state)) && (defined($action))) {
                    135:         my %elements =  &form_elements($dom);
                    136:         if (($action eq 'view') && ($state ne 'crstype')) {
                    137:             if (defined($env{'form.request_id'})) {
                    138:                 %stored = &retrieve_settings($dom,$env{'form.request_id'});
                    139:             }
                    140:         }
                    141:         my $elementsref = {};
                    142:         if (ref($elements{$action}) eq 'HASH') {
                    143:             if (ref($elements{$action}{$state}) eq 'HASH') {
                    144:                 $elementsref = $elements{$action}{$state};
                    145:             }
                    146:         }
                    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;
                    157:     $states{'view'} = ['pick_request','details','review','process'];
                    158:     $states{'log'} = ['filter','display'];
                    159:     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
                    160:     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
                    161:         unless ($env{'form.state'} eq 'crstype') {
                    162:             unshift (@{$states{'new'}},'codepick');
                    163:         }
                    164:     }
                    165: 
                    166:     foreach my $key (keys(%states)) {
                    167:         if (ref($states{$key}) eq 'ARRAY') {
                    168:             unshift (@{$states{$key}},'crstype');
                    169:         }
                    170:     }
                    171: 
1.3       raeburn   172:     my %trail = (
1.10      raeburn   173:                  crstype       => 'Course Request Action',
                    174:                  codepick      => 'Category',
                    175:                  courseinfo    => 'Description',
                    176:                  enrollment    => 'Enrollment',
                    177:                  personnel     => 'Personnel',
                    178:                  review        => 'Review',
                    179:                  process       => 'Result',
                    180:                  pick_request  => 'Display Summary',
1.3       raeburn   181:                 );
                    182: 
1.2       raeburn   183:     my $page = 0;
1.3       raeburn   184:     my $crumb;
1.2       raeburn   185:     if (defined($action)) {
                    186:         my $done = 0;
                    187:         my $i=0;
                    188:         if (ref($states{$action}) eq 'ARRAY') {
                    189:             while ($i<@{$states{$action}} && !$done) {
                    190:                 if ($states{$action}[$i] eq $state) {
                    191:                     $page = $i;
                    192:                     $done = 1;
                    193:                 }
                    194:                 $i++;
                    195:             }
                    196:         }
1.3       raeburn   197:         for (my $i=0; $i<@{$states{$action}}; $i++) {
                    198:             if ($state eq $states{$action}[$i]) {
                    199:                 &Apache::lonhtmlcommon::add_breadcrumb(
                    200:                    {text=>"$trail{$state}"});
                    201:                 $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'); 
                    202:                 last;
                    203:             } else {
                    204:                 if (($state eq 'process') && ($i > 0)) {
                    205:                     &Apache::lonhtmlcommon::add_breadcrumb(
                    206:     {href=>"javascript:backPage(document.requestcrs,'$states{$action}[0]')",
                    207:      text=>"$trail{$states{$action}[$i]}"});
                    208:                 } else {
                    209:                     &Apache::lonhtmlcommon::add_breadcrumb(
                    210:      {href=>"javascript:backPage(document.requestcrs,'$states{$action}[$i]')",
                    211:       text=>"$trail{$states{$action}[$i]}"});
                    212:                 }
                    213:             }
                    214:         }
                    215:     } else {
                    216:         &Apache::lonhtmlcommon::add_breadcrumb(
                    217:                 {text=>'Pick Action'});
                    218:         $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests');
1.2       raeburn   219:     }
                    220: 
1.1       raeburn   221:     my %can_request;
                    222:     my $canreq = &check_can_request($dom,\%can_request);
                    223:     if ($action eq 'new') {
                    224:         if ($canreq) {
                    225:             if ($state eq 'crstype') {
1.3       raeburn   226:                 &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,
                    227:                                  $crumb);
1.1       raeburn   228:             } else {
1.2       raeburn   229:                 &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
1.3       raeburn   230:                                         $loaditems,$crumb);
1.1       raeburn   231:             }
                    232:         } else {
1.3       raeburn   233:             $r->print(&header('Course Requests').$crumb.
1.1       raeburn   234:                       '<div class="LC_warning">'.
                    235:                       &mt('You do not have privileges to request creation of courses.').
1.2       raeburn   236:                       '</div>'.&Apache::loncommon::end_page());
1.1       raeburn   237:         }
                    238:     } elsif ($action eq 'view') {
1.10      raeburn   239:         if ($state eq 'crstype') {
1.11      raeburn   240:            &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
                    241:         } else {
                    242:            &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
                    243:                                    $loaditems,$crumb);
1.10      raeburn   244:         }
1.1       raeburn   245:     } elsif ($action eq 'log') {
1.3       raeburn   246:         &print_request_logs($jscript,$loaditems,$crumb);
1.1       raeburn   247:     } else {
1.3       raeburn   248:         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb);
1.1       raeburn   249:     }
                    250:     return OK;
                    251: }
                    252: 
1.2       raeburn   253: sub header {
                    254:     my ($bodytitle,$jscript,$loaditems,$jsextra) = @_;
                    255:     if ($jscript) {
1.6       raeburn   256:         $jscript = '<script type="text/javascript">'."\n".
                    257:                    '// <![CDATA['."\n".
                    258:                    $jscript."\n".'// ]]>'."\n".'</script>'."\n";
1.2       raeburn   259:     }
                    260:     if ($loaditems) {
1.3       raeburn   261:         $loaditems = {'add_entries' => $loaditems,};
                    262:         return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra,$loaditems);
                    263:     } else {
                    264:         return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra);
                    265:     }
1.2       raeburn   266: }
                    267: 
                    268: sub form_elements {
                    269:     my ($dom) = @_;
                    270:     my %elements =
                    271:     (
                    272:         new => {
                    273:             crstype => {
                    274:                 crstype => 'selectbox',
                    275:                 action  => 'selectbox',
                    276:             },
                    277:             courseinfo => {
                    278:                 cdescr           => 'text',
1.13    ! raeburn   279:                 clonecrs         => 'text',
        !           280:                 clonedom         => 'selectbox',
1.2       raeburn   281:                 datemode         => 'radio',
                    282:                 dateshift        => 'text',
                    283:             },
                    284:             enrollment  => {
1.13    ! raeburn   285:                 accessstart_month  => 'selectbox',
        !           286:                 accessstart_hour   => 'selectbox',
        !           287:                 accessend_month    => 'selectbox',
        !           288:                 accessend_hour     => 'selectbox',
        !           289:                 accessstart_day    => 'text',
        !           290:                 accessstart_year   => 'text',
        !           291:                 accessstart_minute => 'text',
        !           292:                 accessstart_second => 'text',
        !           293:                 accessend_day      => 'text',
        !           294:                 accessend_year     => 'text',
        !           295:                 accessend_minute   => 'text',
        !           296:                 accessend_second   => 'text',
1.2       raeburn   297:                 no_end_date      => 'checkbox',
                    298:             },
                    299:             personnel => {
                    300:                 persontotal => 'hidden',
                    301:                 addperson   => 'checkbox', 
                    302:             },
1.13    ! raeburn   303:             review => {
        !           304:                 cnum => 'hidden',
        !           305:             },
1.2       raeburn   306:          },
                    307:          view => {
                    308:             crstype => {
                    309:                 crstype => 'selectbox',
                    310:                 action  => 'selectbox',
                    311:             },
                    312:          },
                    313:     );
1.13    ! raeburn   314:     my %servers = &Apache::lonnet::get_servers($dom,'library');
        !           315:     my $numlib = keys(%servers);
        !           316:     if ($numlib > 1) {
        !           317:         $elements{'new'}{'courseinfo'}{'chome'} = 'selectbox';
        !           318:     } else {
        !           319:         $elements{'new'}{'courseinfo'}{'chome'} = 'hidden';
        !           320:     }
1.2       raeburn   321:     my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
                    322:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    323:                                              \%cat_order,\@code_order);
                    324:     my $numtitles = scalar(@codetitles);
                    325:     if ($numtitles) {
                    326:         my %extras;
                    327:         $lastitem = pop(@codetitles);
                    328:         $extras{'instcode_'.$lastitem} = 'text'; 
                    329:         foreach my $item (@codetitles) {
                    330:             $extras{'instcode_'.$item} = 'selectbox';   
                    331:         }
                    332:         $elements{'new'}{'codepick'} = \%extras;
                    333:     }
                    334:     if (&Apache::lonnet::auto_run('',$dom)) {
                    335:         my %extras = (
                    336:                        sectotal           => 'hidden',
1.13    ! raeburn   337:                        enrollstart_month  => 'selectbox',
        !           338:                        enrollstart_hour   => 'selectbox',
        !           339:                        enrollend_month    => 'selectbox',
        !           340:                        enrollend_hour     => 'selectbox',
        !           341:                        enrollstart_day    => 'text',
        !           342:                        enrollstart_year   => 'text',
        !           343:                        enrollstart_minute => 'text',
        !           344:                        enrollstart_second => 'text',
        !           345:                        enrollend_day      => 'text',
        !           346:                        enrollend_year     => 'text',
        !           347:                        enrollend_minute   => 'text',
        !           348:                        enrollend_second   => 'text',
1.2       raeburn   349:                        crosslisttotal     => 'hidden',
                    350:                        addcrosslist       => 'checkbox',
                    351:                        autoadds           => 'radio',
                    352:                        autodrops          => 'radio',
                    353:                      );
                    354:         if ($env{'form.sectotal'} > 0) {
                    355:             for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                    356:                 $extras{'sec_'.$i} = 'checkbox',
                    357:                 $extras{'secnum_'.$i} = 'text',
                    358:                 $extras{'loncapasec_'.$i} = 'checkbox',
                    359:             }
                    360:         }
                    361:         my $crosslisttotal = $env{'form.crosslisttotal'};
                    362:         if (!defined($crosslisttotal)) {
                    363:             $crosslisttotal = 1;
                    364:         }
                    365:         if ($crosslisttotal > 0) {
                    366:             for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                    367:                 if ($numtitles) {
                    368:                     $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
                    369:                 }
                    370:                 if (@codetitles > 0) {
                    371:                     foreach my $item (@codetitles) {
                    372:                         $extras{'crosslist_'.$i.'_'.$item} = 'selectbox';
                    373:                     }
                    374:                 }
                    375:                 $extras{'crosslist_'.$i} = 'checkbox';
                    376:                 $extras{'crosslist_'.$i.'_instsec'} = 'text',
                    377:                 $extras{'crosslist_'.$i.'_lcsec'} = 'text',
                    378:             }
                    379:         }
                    380:         my %mergedhash = (%{$elements{'new'}{'enrollment'}},%extras);
                    381:         %{$elements{'new'}{'enrollment'}} = %mergedhash;
                    382:     }
                    383:     my %people;
                    384:     my $persontotal = $env{'form.persontotal'};
                    385:     if (!defined($persontotal)) {
                    386:         $persontotal = 1;
                    387:     }
                    388:     for (my $i=0; $i<$persontotal; $i++) {
1.13    ! raeburn   389:         $people{'person_'.$i.'_uname'}     = 'text',
        !           390:         $people{'person_'.$i.'_dom'}       = 'selectbox',
        !           391:         $people{'person_'.$i.'_hidedom'}   = 'hidden',
        !           392:         $people{'person_'.$i.'_firstname'} = 'text',
        !           393:         $people{'person_'.$i.'_lastname'}  = 'text',
        !           394:         $people{'person_'.$i.'_emailaddr'} = 'text',
        !           395:         $people{'person_'.$i.'_role'}      = 'selectbox',
        !           396:         $people{'person_'.$i.'_sec'}       = 'selectbox',
        !           397:         $people{'person_'.$i.'_newsec'}    = 'text',
        !           398:         $people{'person_'.$i.'_sections'}  = 'hidden',
1.2       raeburn   399:     }
                    400:     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
                    401:     %{$elements{'new'}{'personnel'}} = %personnelhash;
                    402:     return %elements;
                    403: }
                    404: 
                    405: sub onload_action {
                    406:     my ($action,$state) = @_;
                    407:     my %loaditems;
                    408:     if (($action eq 'new') || ($action eq 'view')) {
                    409:         $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
                    410:     }
                    411:     return \%loaditems;
                    412: }
                    413: 
1.1       raeburn   414: sub check_can_request {
                    415:     my ($dom,$can_request) = @_;
                    416:     my $canreq = 0;
1.4       raeburn   417:     my ($types,$typename) = &course_types();
                    418:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                    419:         foreach my $type (@{$types}) {
1.1       raeburn   420:             if (&Apache::lonnet::usertools_access($env{'user.name'},
                    421:                                                   $env{'user.domain'},
                    422:                                                   $type,undef,'requestcourses')) {
                    423:                 $canreq ++;
                    424:                 if ($dom eq $env{'user.domain'}) {
                    425:                     $can_request->{$type} = 1;
                    426:                 }
                    427:             }
                    428:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                    429:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                    430:                 if (@curr > 0) {
                    431:                     $canreq ++;
                    432:                     unless ($dom eq $env{'user.domain'}) {
                    433:                         if (grep(/^\Q$dom\E$/,@curr)) {
                    434:                             $can_request->{$type} = 1;
                    435:                         }
                    436:                     }
                    437:                 }
                    438:             }
                    439:         }
                    440:     }
                    441:     return $canreq;
                    442: }
                    443: 
1.4       raeburn   444: sub course_types {
                    445:     my @types = ('official','unofficial','community');
                    446:     my %typename = (
                    447:                          official   => 'Official course',
                    448:                          unofficial => 'Unofficial course',
                    449:                          community  => 'Community',
                    450:                     );
                    451:     return (\@types,\%typename);
                    452: }
                    453: 
                    454: 
1.1       raeburn   455: sub print_main_menu {
1.3       raeburn   456:     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb) = @_;
1.4       raeburn   457:     my ($types,$typename) = &course_types();
1.1       raeburn   458:     my $onchange;
                    459:     unless ($env{'form.interface'} eq 'textual') {
                    460:         $onchange = 1;
                    461:     }
                    462: 
1.2       raeburn   463:     my $nextstate_setter = "\n";
                    464:     if (ref($states) eq 'HASH') {
                    465:         foreach my $key (keys(%{$states})) {
                    466:             if (ref($states->{$key}) eq 'ARRAY') {
                    467:                 $nextstate_setter .= 
                    468: "             if (actionchoice == '$key') {
                    469:                   nextstate = '".$states->{$key}[1]."';
                    470:              }
                    471: ";
                    472:             }
                    473:         }
                    474:     }
1.1       raeburn   475: 
1.2       raeburn   476:     my $js = <<"END";
1.1       raeburn   477: 
1.2       raeburn   478: function nextPage(formname) {
                    479:     var crschoice = document.requestcrs.crstype.value;
                    480:     var actionchoice = document.requestcrs.action.value;
                    481:     if (check_can_request(crschoice,actionchoice) == true) {
                    482:         if ((actionchoice == 'new') && (crschoice == 'official')) {
                    483:             nextstate = 'codepick';
                    484:         } else {
                    485: $nextstate_setter 
                    486:         } 
1.1       raeburn   487:         formname.state.value= nextstate;
                    488:         formname.submit();
                    489:     }
                    490:     return;
                    491: }
                    492: 
1.2       raeburn   493: function check_can_request(crschoice,actionchoice) {
1.1       raeburn   494:     var official = '';
                    495:     var unofficial = '';
                    496:     var community = '';    
                    497: END
                    498: 
                    499:     foreach my $item (keys(%{$can_request})) {
                    500:             $js .= " 
                    501:         $item = 1;
                    502: ";
                    503:     }
                    504:     my %lt = &Apache::lonlocal::texthash(
                    505:         official => 'You are not permitted to request creation of an official course in this domain.',
                    506:         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
                    507:         community => 'You are not permitted to request creation of a community this domain.',
                    508:         all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
                    509:     ); 
                    510:     $js .= <<END;
                    511:     if (crschoice == 'official') {
                    512:         if (official != 1) {
                    513:             alert("$lt{'official'}");
                    514:             return false;
                    515:         }
                    516:     } else {
                    517:         if (crschoice == 'unofficial') {
                    518:             if (unofficial != 1) {
                    519:                 alert("$lt{'unofficial'}");
                    520:                 return false;
                    521:             }
                    522:         } else {
                    523:             if (crschoice == 'community') {
                    524:                 if (community != 1) {
                    525:                     alert("$lt{'community'}");
                    526:                     return false;
                    527:                 }
                    528:             } else {
                    529:                 if (actionchoice == 'new') {
                    530:                     alert("$lt{'all'}");
                    531:                     return false;
                    532:                 }               
                    533:             }
                    534:         }
                    535:     }
                    536:     return true;
                    537: }
                    538: 
                    539: END
                    540: 
1.3       raeburn   541:     $r->print(&header('Course Requests',$js.$jscript,$loaditems).$crumb.
                    542:               '<div>'.
1.1       raeburn   543:               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
                    544:               &Apache::lonhtmlcommon::start_pick_box().
1.8       raeburn   545:               &Apache::lonhtmlcommon::row_title('Course Domain').
1.1       raeburn   546:               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange));
                    547:     if (!$onchange) {
                    548:         $r->print('&nbsp;<input type="submit" name="godom" value="'.
                    549:                    &mt('Change').'" />');
                    550:     }
                    551:     $r->print(&Apache::lonhtmlcommon::row_closure(1).
                    552:               &Apache::lonhtmlcommon::end_pick_box().'</form></div>');
                    553: 
1.2       raeburn   554:     my $formname = 'requestcrs';
1.1       raeburn   555:     my $nexttext = &mt('Next');
                    556:     $r->print('<div><form name="'.$formname.'" method="post" action="/adm/requestcourse">'.
                    557:               &Apache::lonhtmlcommon::start_pick_box().
                    558:               &Apache::lonhtmlcommon::row_title('Action').'
                    559: <input type="hidden" name="showdom" value="'.$dom.'" />
                    560: <select size="1" name="action" >
1.2       raeburn   561:  <option value="new">'.&mt('New request').'</option>
1.1       raeburn   562:  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
                    563:  <option value="log">'.&mt('View request history').'</option>
                    564: </select>'.
                    565:               &Apache::lonhtmlcommon::row_closure().
                    566:               &Apache::lonhtmlcommon::row_title('Course Type').'
                    567: <select size="1" name="crstype">
1.4       raeburn   568:  <option value="any">'.&mt('All types').'</option>');
                    569:     if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
                    570:         foreach my $type (@{$types}) {
                    571:             my $selected = '';
                    572:             if ($type eq 'official') {
                    573:                 $selected = ' selected="selected"';
                    574:             }
                    575:             $r->print('<option value="'.$type.'"'.$selected.'>'.$typename->{$type}.
                    576:                       '</option>'."\n");
                    577:         }
                    578:     }
                    579:     $r->print('</select>
1.1       raeburn   580: <input type="hidden" name="state" value="crstype" />'.
                    581:               &Apache::lonhtmlcommon::row_closure(1).
                    582:               &Apache::lonhtmlcommon::end_pick_box().'<br />
1.2       raeburn   583: <input type="button" name="next" value="'.$nexttext.'" onclick="javascript:nextPage(document.'.$formname.')" />
1.1       raeburn   584: </form></div>');
                    585:     $r->print(&Apache::loncommon::end_page());
                    586:     return;
                    587: }
                    588: 
                    589: sub request_administration {
1.3       raeburn   590:     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb) = @_;
1.2       raeburn   591:     my $js;
                    592:     if (($action eq 'new') || ($action eq 'view')) {
                    593:         $js =  <<END;
1.1       raeburn   594: 
                    595: function nextPage(formname,nextstate) {
                    596:     formname.state.value= nextstate;
                    597:     formname.submit();
                    598: }
                    599: function backPage(formname,prevstate) {
                    600:     formname.state.value = prevstate;
                    601:     formname.submit();
                    602: }
                    603: 
                    604: END
1.2       raeburn   605:     }
                    606:     if ($action eq 'new') {
                    607:         my $jsextra;
1.1       raeburn   608:         unless (($state eq 'review') || ($state eq 'process')) {
1.2       raeburn   609:             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
1.1       raeburn   610:         }
1.3       raeburn   611:         $r->print(&header('Request a course',$js.$jscript,$loaditems,$jsextra).$crumb);
1.8       raeburn   612:         &print_request_form($r,$action,$state,$page,$states,$dom);
1.2       raeburn   613:     } elsif ($action eq 'view') {
1.3       raeburn   614:         $r->print(&header('Manage course requests',$js.$jscript,$loaditems).$crumb);
1.11      raeburn   615:         if ($state eq 'pick_request') {
                    616:             $r->print(&print_request_status($dom));
                    617:         }
1.1       raeburn   618:     } elsif ($action eq 'log') {
1.11      raeburn   619:         $r->print(&coursereq_log('View request log',$jscript,$loaditems).$crumb);
1.1       raeburn   620:     }
1.2       raeburn   621:     $r->print(&Apache::loncommon::end_page());
1.1       raeburn   622:     return;
                    623: }
                    624: 
                    625: sub print_request_form {
1.2       raeburn   626:     my ($r,$action,$state,$page,$states,$dom) = @_;
1.1       raeburn   627:     my $formname = 'requestcrs';
1.2       raeburn   628:     my ($next,$prev,$message,$output,$codepicker,$crstype);
                    629:     $prev = $states->{$action}[$page-1];
                    630:     $next = $states->{$action}[$page+1];
1.4       raeburn   631:     my %navtxt = &Apache::lonlocal::texthash (
1.10      raeburn   632:                                                prev => 'Back',
1.4       raeburn   633:                                                next => 'Next',
                    634:                                              );
1.2       raeburn   635:     $crstype = $env{'form.crstype'};
1.1       raeburn   636:     $r->print('<form name="'.$formname.'" method="post" action="/adm/requestcourse">');
1.2       raeburn   637:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
1.1       raeburn   638:     if ($crstype eq 'official') {
1.2       raeburn   639:         if ($env{'form.instcode'} ne '') {
                    640:             $instcode = $env{'form.instcode'};
                    641:         }
                    642:     }
                    643:     if ($prev eq 'codepick') {
1.4       raeburn   644:         if ($crstype eq 'official') {
                    645:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    646:                                                      \%cat_order,\@code_order);
                    647:         }
1.2       raeburn   648:         if (@code_order > 0) {
                    649:             my $message;
                    650:             if ($instcode eq '') {
1.1       raeburn   651:                 foreach my $item (@code_order) {
1.2       raeburn   652:                     $instcode .= $env{'form.instcode_'.$item};
1.1       raeburn   653:                 }
1.2       raeburn   654:                 $r->print('<input type="hidden" name="instcode" value="'.$instcode.'" />'."\n");
1.1       raeburn   655:             }
                    656:             if ($instcode ne '') {
1.2       raeburn   657:                 $code_chk = &Apache::lonnet::auto_validate_instcode('',$dom,$instcode);
1.1       raeburn   658:                 if ($code_chk eq 'ok') {
                    659:                     $message = '<div class="LC_info">'.
                    660:                                &mt('The chosen course category [_1] is valid.','<b>'.
1.2       raeburn   661:                                $instcode.'</b>').'</div>';
1.1       raeburn   662:                 } else {
                    663:                     $message = '<div class="LC_warning">'.
                    664:                                &mt('No course was found matching your choice of institutional course category.');
                    665:                     if ($code_chk ne '') {
                    666:                         $message .= '<br />'.$code_chk;
                    667:                     }
                    668:                     $message .= '</div>';
                    669:                 }
1.2       raeburn   670:             } else {
                    671:                 $message = '<div class="LC_warning">'.
                    672:                            &mt('No course was found matching your choice of institutional course category.');
1.1       raeburn   673:             }
1.2       raeburn   674:             unless ($code_chk eq 'ok') {
                    675:                 $prev = 'crstype';
                    676:             }
                    677:             $r->print($message);
1.1       raeburn   678:         }
1.2       raeburn   679:     }
                    680:     if ($prev eq 'crstype') {
1.4       raeburn   681:         if ($crstype eq 'official') {
                    682:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    683:                                                      \%cat_order,\@code_order);
                    684:         }
1.2       raeburn   685:         if (@code_order > 0) {
1.1       raeburn   686:             $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
                    687:                                            \%cat_titles,\%cat_order);
1.2       raeburn   688:             if ($codepicker) {
                    689:                 $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box().$codepicker.
                    690:                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
                    691:             } else {
                    692:                 $r->print(&courseinfo_form($dom,$formname,$crstype));
                    693:             }
                    694:         } else {
                    695:             $r->print(&courseinfo_form($dom,$formname,$crstype));
1.1       raeburn   696:         }
1.2       raeburn   697:     } elsif ($prev eq 'codepick') {
                    698:         $r->print(&courseinfo_form($dom,$formname,$crstype));
                    699:     } elsif ($state eq 'enrollment') {
1.4       raeburn   700:         if ($crstype eq 'official') {
                    701:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    702:                                                      \%cat_order,\@code_order);
                    703:         }
1.2       raeburn   704:         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
                    705:                                          \%cat_titles,\%cat_order,\@code_order));
                    706:     } elsif ($state eq 'personnel') {
1.8       raeburn   707:         $r->print(&print_personnel_menu($dom,$formname,$crstype));
1.4       raeburn   708:     } elsif ($state eq 'review') {
                    709:         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    710:                                                  \%cat_order,\@code_order);
                    711:         $r->print(&print_review($formname,$dom,\@codetitles,\%cat_titles,\%cat_order,
                    712:                                 \@code_order));
                    713:         $navtxt{'next'} = &mt('Submit course request');
1.10      raeburn   714:     }  elsif ($state eq 'process') {
                    715:         if ($crstype eq 'official') {
                    716:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                    717:                                                      \%cat_order,\@code_order);
                    718:         }
                    719:         my $result = &print_request_outcome($dom,\@codetitles,\@code_order);
1.13    ! raeburn   720:         $r->print($result);
1.1       raeburn   721:     }
1.2       raeburn   722:     my @excluded = ('counter');
                    723:     my %elements = &form_elements($dom);
                    724:     if (ref($states) eq 'HASH') {
                    725:         if (ref($states->{$action}) eq 'ARRAY') {
                    726:             my @items = @{$states->{$action}};
                    727:             my $numitems = scalar(@items);
                    728:             if ($numitems) {
                    729:                 for (my $i=$numitems-1; $i>=0; $i--) {
                    730:                     if (ref($elements{$action}) eq 'HASH') {
                    731:                         if (ref($elements{$action}{$items[$i]}) eq 'HASH') {
                    732:                             foreach my $key (keys(%{$elements{$action}{$items[$i]}})) { 
                    733:                                 push(@excluded,$key);
                    734:                             }
                    735:                         }
                    736:                     }
                    737:                     last if ($items[$i] eq $state);
                    738:                 }
                    739:             }
                    740:         }
                    741:     }
                    742:     if (grep(/^instcode_/,@excluded)) {
                    743:         push(@excluded,'instcode');
1.1       raeburn   744:     }
1.2       raeburn   745:     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
1.4       raeburn   746:     &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'});
1.1       raeburn   747:     return;
                    748: }
                    749: 
1.2       raeburn   750: sub print_enrollment_menu {
                    751:     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order) =@_;
                    752:     my ($sections,$autoenroll,$access_dates);
                    753:     my $starttime = time;
                    754:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                    755: 
                    756:     my %accesstitles = (
                    757:                           'start' => 'Default start access',
                    758:                            'end'   => 'Default end accss',
                    759:                        );
                    760:     my %enrolltitles = (
                    761:                            'start' => 'Start auto-enrollment',
                    762:                            'end'   => 'End auto-enrollment',
                    763:                        );
                    764:     if ($env{'form.crstype'} eq 'official') {
                    765:         if (&Apache::lonnet::auto_run('',$dom)) {
                    766:             my ($section_form,$crosslist_form,$autoenroll_form);
                    767:             $section_form = &inst_section_selector($dom,$instcode);
                    768:             my $crosslisttotal = $env{'form.crosslisttotal'};
                    769:             if (!defined($crosslisttotal)) {
                    770:                 $crosslisttotal = 1;
                    771:             }
                    772:             if ($env{'form.addcrosslist'}) {
                    773:                 $crosslisttotal ++;
                    774:             }
                    775:             for (my $i=0; $i<$crosslisttotal; $i++) {
                    776:                 $crosslist_form .= &coursecode_form($dom,'crosslist',$codetitles, 
                    777:                                                   $cat_titles,$cat_order,$i);
                    778:             }
                    779:             if ($crosslist_form) { 
                    780:                 $crosslist_form .= 
                    781:                     &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                    782:                     '<input name="crosslisttotal" type="hidden" value="'.$crosslisttotal.'" />'.
                    783:                     '<input name="addcrosslist" type="checkbox" value="'.$crosslisttotal.'"'.
                    784:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                    785:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1);
                    786:             }
                    787:             if ($section_form || $crosslist_form) {
                    788:                 $sections = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    789:                             $section_form.$crosslist_form.
                    790:                             &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    791:             }
                    792:             $autoenroll_form = 
                    793:                 &Apache::lonhtmlcommon::row_title(&mt('Add registered students automatically')).
                    794:                 '<span class="LC_nobreak"><label>'.
                    795:                 '<input type="radio" name="autoadds" value="1">'.
                    796:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                    797:                 '<input type="radio" name="autoadds" value="0" checked="checked">'.
                    798:                 &mt('No').'</label></span>'.
                    799:                 &Apache::lonhtmlcommon::row_closure().
                    800:                 &Apache::lonhtmlcommon::row_title(&mt('Drop unregistered students automatically')).
                    801:                 '<span class="LC_nobreak"><label>'.
                    802:                 '<input type="radio" name="autodrops" value="1">'.
                    803:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
                    804:                 '<input type="radio" name="autodrops" value="0" checked="checked">'.
                    805:                 &mt('No').'</label></span>'. 
                    806:                 &Apache::lonhtmlcommon::row_closure().
                    807:                 &date_setting_table($starttime,$endtime,$formname,'enroll',%enrolltitles);
                    808:             if ($autoenroll_form) {
                    809:                 $autoenroll = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    810:                               $autoenroll_form.
                    811:                               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    812:             }
                    813:         }
                    814:     }
                    815:     my $access_dates_form = 
                    816:         &date_setting_table($starttime,$endtime,$formname,'access',%accesstitles);
                    817:     if ($access_dates_form) {
                    818:         $access_dates = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                    819:                         $access_dates_form.
                    820:                         &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
                    821:     }
                    822:     return $sections.$autoenroll.$access_dates;
                    823: }
                    824: 
1.1       raeburn   825: sub inst_section_selector {
1.2       raeburn   826:     my ($dom,$instcode) = @_;
                    827:     my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
                    828:     my $sectotal = scalar(@sections);
1.1       raeburn   829:     my $output;
1.2       raeburn   830:     if ($sectotal) {
                    831:         $output .=  &Apache::lonhtmlcommon::row_title('Sections').
                    832:                     &Apache::loncommon::start_data_table().
                    833:                     &Apache::loncommon::start_data_table_row().
                    834:                     '<th>'.&mt('Include?').'<input type="hidden" name="sectotal" '.
                    835:                     'value="'.$sectotal.'"</th>'.
                    836:                     '<th>'.&mt('Institutional Section').'</th>'.
                    837:                     '<th>'.&mt('LON-CAPA section').'</th>'.
                    838:                     &Apache::loncommon::end_data_table_row();
                    839:         for (my $i=0; $i<@sections; $i++) {
1.1       raeburn   840:             my $colflag = $i%2;
                    841:             $output .= &Apache::loncommon::start_data_table_row().
1.2       raeburn   842:                        '<td><input type="checkbox" name="sec_'.$i.
                    843:                        '" checked="checked" /></td>'.
                    844:                        '<td>'.$sections[$i].
1.1       raeburn   845:                        '<input type="hidden" name="secnum_'.$i.'" value="'.
1.2       raeburn   846:                        $sections[$i].'" /></td>'.
1.1       raeburn   847:                        '<td><input type="text" size="10" name="loncapasec_'.$i.
1.2       raeburn   848:                        '" value="'.$sections[$i].'" /></td>'.
1.1       raeburn   849:                        &Apache::loncommon::end_data_table_row();
                    850:         }
1.2       raeburn   851:         $output .= &Apache::loncommon::end_data_table().
                    852:                    &Apache::lonhtmlcommon::row_closure();
1.1       raeburn   853:     }
                    854:     return $output;
                    855: }
                    856: 
1.2       raeburn   857: sub date_setting_table {
                    858:     my ($starttime,$endtime,$formname,$suffix,%datetitles) = @_;
                    859:     my ($perpetual,$table);
                    860:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,'start'.$suffix,
                    861:                                                         $starttime,'','','',1,'','','',1);
                    862:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,'end'.$suffix,
                    863:                                                       $endtime,'','','',1,'','','',1);
                    864:     if ($suffix eq 'access') {
                    865:         $perpetual = ' <span class="LC_nobreak"><label>'.
                    866:                      '<input type="checkbox" name="no_end_date" />'.
                    867:                      &mt('No end date').'</label></span>';
                    868:     }
                    869:     $table = &Apache::lonhtmlcommon::row_title($datetitles{'start'}).
                    870:              $startform.
                    871:              &Apache::lonhtmlcommon::row_closure().
                    872:              &Apache::lonhtmlcommon::row_title($datetitles{'end'}).
                    873:              $endform.$perpetual.
                    874:              &Apache::lonhtmlcommon::row_closure(1);
                    875:     return $table;
                    876: }
                    877: 
                    878: sub print_personnel_menu {
1.8       raeburn   879:     my ($dom,$formname,$crstype) = @_;
1.2       raeburn   880:     my $output = '<div>'.&Apache::lonhtmlcommon::start_pick_box();
                    881:     my $persontotal = $env{'form.persontotal'};
                    882:     if (!defined($persontotal)) {
                    883:         $persontotal = 1;
                    884:     }
                    885:     if ($env{'form.addperson'}) {
                    886:         $persontotal ++;
                    887:     }
                    888:     my $userlinktxt = &mt('Set User');
1.13    ! raeburn   889:     my @items = ('uname','dom','lastname','firstname','emailaddr','hidedom');
1.2       raeburn   890: 
                    891:     my $roleoptions;
                    892:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.8       raeburn   893:     my $type = 'Course';
                    894:     if ($crstype eq 'community') {
                    895:         $type = 'Community';
                    896:     }
1.2       raeburn   897:     foreach my $role (@roles) {
1.8       raeburn   898:         my $plrole=&Apache::lonnet::plaintext($role,$type);
1.2       raeburn   899:         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                    900:     }
                    901:     my %customroles=&Apache::lonuserutils::my_custom_roles();
                    902:     if (keys(%customroles) > 0) {
                    903:         foreach my $cust (sort(keys(%customroles))) {
                    904:             my $custrole='cr_cr_'.$env{'user.domain'}.
                    905:                     '_'.$env{'user.name'}.'_'.$cust;
                    906:             $roleoptions .= '  <option value="'.$custrole.'">'.$cust.'</option>'."\n";
                    907:         }
                    908:     }
                    909: 
                    910:     my @currsecs;
                    911:     if ($env{'form.sectotal'}) {
                    912:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                    913:             if (defined($env{'form.loncapasec_'.$i})) {
                    914:                 my $lcsec = $env{'form.loncapasec_'.$i};
                    915:                 unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
                    916:                     push(@currsecs,$lcsec);
                    917:                 }
                    918:             }
                    919:         }
                    920:     }
                    921: 
                    922:     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
                    923:     if (@currsecs) {
                    924:         my $existsize = scalar(@currsecs);
                    925:         if ($existsize > 3) {
                    926:             $existsize = 3;
                    927:         }
                    928:         if ($existsize > 1) {
                    929:             $existmult = ' multiple="multiple" size="'.$existsize.'" ';
                    930:         }
                    931:         @currsecs = sort { $a <=> $b } (@currsecs);
                    932:         $existtitle = &mt('Official').':&nbsp;';
                    933:         $existops = '<option value="">'.&mt('None').'</option>';
                    934:         foreach my $sec (@currsecs) {
                    935:             $existops .= '<option value="'.$sec.'">'.$sec.'</option>'."\n";
                    936:         }
                    937:         $seccolspan = ' colspan="2"';
                    938:         $newtitle = &mt('Other').':&nbsp;';
                    939:     }
                    940: 
                    941:     for (my $i=0; $i<$persontotal; $i++) { 
                    942:         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
                    943:         my $linkargstr = join("','",@linkargs);
1.7       raeburn   944:         my $userlink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,$userlinktxt);
1.2       raeburn   945:         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value=""'.
                    946:                          ' onFocus="this.blur();'.
1.7       raeburn   947:                          'openuserbrowser('."'$formname','$linkargstr','$dom'".');" />';
1.2       raeburn   948:         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
                    949:                        "'person_".$i."_hidedom'".');'.
1.7       raeburn   950:                        'openuserbrowser('."'$formname','$linkargstr','$dom'".');';
1.2       raeburn   951:         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
                    952:                                                             1,$onchange).
                    953:                         '<input type="hidden" name="person_'.$i.'_hidedom" value="'.$dom.'" />';
                    954:         my %form_elems;
                    955:         foreach my $item (@items) {
                    956:             next if (($item eq 'dom') || ($item eq 'uname') || ($item eq 'hidedom'));
                    957:             $form_elems{$item} = '<input type="text" name="person_'.$i.'_'.$item.'" '.
                    958:                                  'value="" readonly="readonly" />';
                    959:         }
                    960:         my $roleselector = '<select name="person_'.$i.'_role">'."\n".
                    961:                            $roleoptions.'</select>';
                    962:         my $sectionselector;
                    963:         if (@currsecs) {
                    964:             $sectionselector = $existtitle.'<select name="person_'.$i.'_sec"'.
                    965:                                $existmult.'>'."\n".$existops.'</select>'.('&nbsp;' x3);
                    966:         }
                    967:         $sectionselector .= $newtitle.
                    968:             '<input type="text" name="person_'.$i.'_newsec" size="15" value="" />'.
                    969:             '<input type="hidden" name="person_'.$i.'_sections" value="" />'."\n";
                    970:          
                    971:         $output .= 
                    972:             &Apache::lonhtmlcommon::row_title(&mt('Additional Personnel').'<br />'.
                    973:                                               '<span class="LC_nobreak">'.$userlink.
                    974:                                               '</span>').
                    975:             '<table><tr><td align="center" valign="top">'.&mt('Username').'<br />'.$uname_form.'</td>'."\n".
                    976:             '<td align="center" valign="top" colspan="2">'.&mt('Domain').'<br />'.$udom_form.'</td></tr><tr>'."\n".
1.13    ! raeburn   977:             '<td align="center" valign="top">'.&mt('First Name').'<br />'.$form_elems{'firstname'}.'</td>'."\n".
        !           978:             '<td align="center" valign="top">'.&mt('Last Name').'<br />'.$form_elems{'lastname'}.'</td>'."\n".
        !           979:             '<td align="center" valign="top">'.&mt('E-mail').'<br />'.$form_elems{'emailaddr'}.'</td></tr>'."\n".
1.2       raeburn   980:             '<tr><td align="center" valign="top">'.&mt('Role').'<br />'.$roleselector.'</td>'."\n".
                    981:             '<td'.$seccolspan.' align="center" valign="top">'.&mt('Section(s)').'<br />'.$sectionselector.'</td>'."\n".
                    982:             '</tr></table>'.&Apache::lonhtmlcommon::row_closure();
                    983:     }
                    984:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Add another?')).
                    985:                '<input name="persontotal" type="hidden" value="'.$persontotal.'" />'.
                    986:                '<input name="addperson" type="checkbox" value="'.$persontotal.'"'.
                    987:                ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                    988:                "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1).
                    989:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
                    990:     return $output;
                    991: }
                    992: 
1.1       raeburn   993: sub print_request_status {
1.11      raeburn   994:     my ($dom) = @_;
1.10      raeburn   995:     my %requests = &Apache::lonnet::dumpstore('courserequests',$env{'user.domain'},
                    996:                                               $env{'user.name'});
1.11      raeburn   997:     my ($output,$formname,%queue_by_date);
1.10      raeburn   998:     foreach my $key (keys(%requests)) {
                    999:         if (ref($requests{$key}) eq 'HASH') {
                   1000:             my ($cdom,$cnum) = split('_',$key);
                   1001:             next if ($cdom ne $dom);
                   1002:             my $entry;
                   1003:             my $timestamp = $requests{$key}{'timestamp'};
                   1004:             my $crstype = $requests{$key}{'crstype'};
                   1005:             my $status = $requests{$key}{'status'};
                   1006:             next unless (($env{'form.crstype'} eq 'all') || 
                   1007:                          ($env{'form.crstype'} eq $crstype));
                   1008:             next unless (($status eq 'approval') || ($status eq 'pending'));
                   1009:             if (ref($requests{$key}{'details'}) eq 'HASH') {
1.13    ! raeburn  1010:                 $entry = $key.':'.$crstype.':'.$requests{$key}{'details'}{'cdescr'};
1.10      raeburn  1011:                 if ($crstype eq 'official') {
                   1012:                     $entry .= ':'.$requests{$key}{'details'}{'instcode'}; 
                   1013:                 }
                   1014:             }
                   1015:             if ($entry ne '') {
                   1016:                 if (exists($queue_by_date{$timestamp})) {
                   1017:                     if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                   1018:                         push(@{$queue_by_date{$timestamp}},$entry);
                   1019:                     }
                   1020:                 } else {
                   1021:                     @{$queue_by_date{$timestamp}} = ($entry);
                   1022:                 }
                   1023:             }
                   1024:         }
                   1025:     }
1.11      raeburn  1026:     $formname = 'requestcrs';
1.10      raeburn  1027:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
1.11      raeburn  1028:     $output = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />'."\n".
1.10      raeburn  1029:               '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                   1030:               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
                   1031:               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n";
                   1032:     if (@sortedtimes > 0) {
                   1033:         $output .= &Apache::loncommon::start_data_table().
                   1034:                    &Apache::loncommon::start_data_table_header_row().
                   1035:                    '<th>'.&mt('Action').'</th>'.
                   1036:                    '<th>'.&mt('Description').'</th>';
                   1037:         if ($env{'form.crstype'} eq 'all') {
                   1038:             $output .= '<th>'.&mt('Type').'</th>';
                   1039:         }
                   1040:         if (($env{'form.crstype'} eq 'all') || ($env{'form.crstype'} eq 'official')) {
                   1041:             $output .= '<th>'.&mt('Institutional Code').'</th>';
                   1042:         }
                   1043:         $output .= '<th>'.&mt('Date requested').'</th>'.
                   1044:                    &Apache::loncommon::end_data_table_header_row();
                   1045:         my $count = 0;
                   1046:         foreach my $item (@sortedtimes) {
                   1047:             my $showtime = &Apache::lonlocal::locallocaltime($item);
                   1048:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
                   1049:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
                   1050:                     my ($key,$type,$desc,$instcode) = split(':',$request);
                   1051:                     my ($cdom,$cnum) = split('_',$key);
                   1052:                     $output .= &Apache::loncommon::start_data_table_row().
                   1053:      '<td><input type="button" value="'.&mt('Select').'" onclick="javascript:viewrequest('."'$cdom','$cnum'".')" /></td>'.
                   1054:      '<td>'.$desc.'</td>';
                   1055:                     if ($env{'form.crstype'} eq 'all') {
                   1056:                         $output .= '<td>'.&course_types($type).'</td>';
                   1057:                     }
                   1058:                     if (($env{'form.crstype'} eq 'all') || 
                   1059:                         ($env{'form.crstype'} eq 'official')) {
                   1060:                         $output .= '<td>'.$instcode.'</td>';
                   1061:                     }
                   1062:                     $output .= '<td>'.$showtime.'</td>'.
                   1063:                                &Apache::loncommon::end_data_table_row();
                   1064:                 }
                   1065:             }
                   1066:         }
                   1067:         $output .= &Apache::loncommon::end_data_table();
                   1068:     } else {
1.11      raeburn  1069:         $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  1070:     }
                   1071:     $output .= '
                   1072: <input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />
1.11      raeburn  1073: </form>';
1.10      raeburn  1074:     return $output;
1.1       raeburn  1075: }
                   1076: 
                   1077: sub print_request_logs {
1.10      raeburn  1078:     my ($jscript,$loaditems,$crumb) = @_;
1.1       raeburn  1079:     return;
                   1080: }
                   1081: 
                   1082: sub print_review {
1.4       raeburn  1083:     my ($formname,$dom,$codetitles,$cat_titles,$cat_order,$code_order) = @_;
                   1084:     my ($types,$typename) = &course_types();
                   1085:     my ($owner,$ownername,$owneremail);
                   1086:     $owner = $env{'user.name'}.':'.$env{'user.domain'};
                   1087:     $ownername = &Apache::loncommon::plainname($env{'user.name'},
                   1088:                                                $env{'user.domain'},'first');
                   1089:     my %emails = &Apache::loncommon::getemails();
                   1090:     foreach my $email ('permanentemail','critnotification','notification') {
                   1091:         $owneremail = $emails{$email};
                   1092:         last if ($owneremail ne '');
                   1093:     }
                   1094:     my ($inst_headers,$inst_values,$crstypename,$enroll_headers,$enroll_values,
                   1095:         $section_headers,$section_values,$personnel_headers,$personnel_values);
                   1096: 
                   1097:     $crstypename = $env{'form.crstype'};
                   1098:     if (ref($typename) eq 'HASH') {
                   1099:         unless ($typename->{$env{'form.crstype'}} eq '') {
                   1100:             $crstypename = $typename->{$env{'form.crstype'}};
                   1101:         }
                   1102:     }
                   1103: 
                   1104:     $inst_headers = '<th>'.&mt('Description').'</th><th>'.&mt('Type').'</th>';
                   1105:     $inst_values = '<td>'.$env{'form.cdescr'}.'</td><td>'.$crstypename.'</td>';
                   1106: 
                   1107:     if ($env{'form.crstype'} eq 'official') {
                   1108:         if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH')) {
                   1109:             foreach my $title (@{$codetitles}) {
                   1110:                 if ($env{'form.instcode_'.$title} ne '') {
                   1111:                     $inst_headers .= '<th>'.$title.'</th>';
                   1112:                     my $longitem = $env{'form.instcode_'.$title};
                   1113:                     if (ref($cat_titles->{$title}) eq 'HASH') {
                   1114:                         if ($cat_titles->{$title}{$env{'form.instcode_'.$title}} ne '') {
                   1115:                             $longitem = $cat_titles->{$title}{$env{'form.instcode_'.$title}};
                   1116:                         }
                   1117:                     }
                   1118:                     $inst_values .= '<td>'.$longitem.'</td>';
                   1119:                 }
                   1120:             }
                   1121:         }
                   1122:         if (&Apache::lonnet::auto_run('',$dom)) {
                   1123:             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
                   1124:                               '<th>'.&mt('Automatic Drops').'</th>'.
                   1125:                               '<th>'.&mt('Enrollment Starts').'</th>'.
                   1126:                               '<th>'.&mt('Enrollment Ends').'</th>';
                   1127:             $section_headers = '<th>'.&mt('Sections').'</th>'.
                   1128:                                '<th>'.&mt('Crosslistings').'</th>';
                   1129: 
1.13    ! raeburn  1130:             my ($enrollstart,$enrollend) = &dates_from_form('enrollstart','enrollend');
1.4       raeburn  1131:             my @autoroster = (&mt('No'),&mt('Yes'));
                   1132:             $enroll_values = '<td>'.$autoroster[$env{'form.autoadds'}].'</td>'.
                   1133:                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
1.13    ! raeburn  1134:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollstart).'</td>'.
        !          1135:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollend).'</td>';
1.6       raeburn  1136:             $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
                   1137:                               &mt('Institutional section').'</th>'.
                   1138:                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  1139:             my $secinfo;
1.4       raeburn  1140:             if ($env{'form.sectotal'} > 0) {
                   1141:                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   1142:                     if ($env{'form.sec_'.$i}) {
1.5       raeburn  1143:                         $secinfo .= '<tr><td>'.$env{'form.secnum_'.$i}.'</td><td>';
1.4       raeburn  1144:                         if ($env{'form.loncapasec_'.$i} ne '') {
1.5       raeburn  1145:                             $secinfo .= $env{'form.loncapasec_'.$i};
                   1146:                         } else {
                   1147:                             $secinfo .= &mt('None');
1.4       raeburn  1148:                         }
1.5       raeburn  1149:                         $secinfo .= '</td></tr>';
1.4       raeburn  1150:                     }
                   1151:                 }
                   1152:             }
1.6       raeburn  1153:             if ($secinfo eq '') {
                   1154:                 $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  1155:             }
1.6       raeburn  1156:             $section_values .= $secinfo.'</table></td><td>'.
                   1157:                                '<table class="LC_innerpickbox"><tr><th>'.
                   1158:                                &mt('Institutional course/section').'</th>'.
                   1159:                                '<th>'.&mt('LON-CAPA section').'</th></tr>';
1.5       raeburn  1160:             my $xlistinfo;
1.4       raeburn  1161:             if ($env{'form.crosslisttotal'}) {
                   1162:                 for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                   1163:                     if ($env{'form.crosslist_'.$i}) {
1.5       raeburn  1164:                         $xlistinfo .= '<tr><td>';
                   1165:                         if (ref($code_order) eq 'ARRAY') {
                   1166:                             if (@{$code_order} > 0) {
                   1167:                                 foreach my $item (@{$code_order}) {
                   1168:                                     $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
1.4       raeburn  1169:                                 }
                   1170:                             }
                   1171:                         }
1.5       raeburn  1172:                         $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'}.'</td><td>';
1.4       raeburn  1173:                         if ($env{'form.crosslist_'.$i.'_lcsec'}) {
1.5       raeburn  1174:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_lcsec'};
                   1175:                         } else {
                   1176:                             $xlistinfo .= &mt('None');
1.4       raeburn  1177:                         }
1.5       raeburn  1178:                         $xlistinfo .= '</td></tr>';
1.4       raeburn  1179:                     }
                   1180:                 }
                   1181:             }
1.6       raeburn  1182:             if ($xlistinfo eq '') {
                   1183:                 $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
1.5       raeburn  1184:             }
1.6       raeburn  1185:             $section_values .= $xlistinfo.'</table></td>';
1.4       raeburn  1186:         }
                   1187:     }
                   1188: 
                   1189:     my %ctxt = &clone_text();
                   1190:     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
1.13    ! raeburn  1191:     if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
        !          1192:         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
1.4       raeburn  1193:         my %coursehash = 
1.13    ! raeburn  1194:             &Apache::lonnet::courseiddump($env{'form.clonedom'},'.',1,'.','.',
        !          1195:                                           $env{'form.clonecrs'},undef,undef,'.');
        !          1196:         my $cloneid = $env{'form.clonedom'}.'_'.$env{'form.clonecrs'};
1.4       raeburn  1197:         if (ref($coursehash{$cloneid}) eq 'HASH') {
                   1198:             $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
                   1199:             my $clonedesc = $coursehash{$cloneid}{'description'};
                   1200:             my $cloneinst = $coursehash{$cloneid}{'inst_code'};
                   1201: 
                   1202:             $inst_values .= '<td>'.$clonedesc.'&nbsp;';
                   1203:             if ($cloneinst ne '') {
1.13    ! raeburn  1204:                 $inst_values .= &mt('([_1] in [_2])',$cloneinst,$env{'form.clonedom'}); 
1.4       raeburn  1205:             } else {
1.13    ! raeburn  1206:                 $inst_values .= &mt('(from [_1])',$env{'form.clonedom'});
1.4       raeburn  1207:             }
                   1208:             $inst_values .= '</td><td>';
                   1209:             if ($env{'form.datemode'} eq 'preserve') {
                   1210:                 $inst_values .= $ctxt{'pcd'}; 
                   1211:             } elsif ($env{'form.datemode'} eq 'shift') {
                   1212:                 $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
                   1213:             } else {
                   1214:                 $inst_values .= $ctxt{'ncd'};
                   1215:             }
                   1216:             $inst_values .= '</td>';
                   1217:         } else {
                   1218:             $inst_values .= '<td>'.&mt('Unknown').'</td>';
                   1219:         }
                   1220:     } else {
                   1221:         $inst_values .= '<td>'.&mt('None').'</td>';
                   1222:     }
                   1223:     $enroll_headers .= '<th>'.&mt('Access Starts').'</th>'.
                   1224:                        '<th>'.&mt('Access Ends').'</th>';
1.13    ! raeburn  1225:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
        !          1226:     $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessstart).'</td>';
        !          1227:     if ($accessend == 0) {
1.4       raeburn  1228:         $enroll_values .= '<td>'.&mt('No end date').'</td>';
                   1229:     } else {
1.13    ! raeburn  1230:         $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessend).'</td>';
1.4       raeburn  1231:     }
                   1232: 
                   1233:     my $container = 'Course';
                   1234:     if ($env{'form.crstype'} eq 'community') {
                   1235:         $container = 'Community';
                   1236:     }
                   1237: 
                   1238:     $personnel_headers = '<th>'.&mt('Name').'</th><th>'.&mt('Username:Domain').
                   1239:                          '</th><th>'.&mt('Role').'</th><th>'.&mt('LON-CAPA Sections').
                   1240:                          '</th>';
                   1241:     $personnel_values .= '<tr><td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   1242:                          '<td>'.&Apache::lonnet::plaintext('cc',$container).'</td>'.
                   1243:                          '<td>'.&mt('None').'</td></tr>';
                   1244:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1245:         if ($env{'form.person_'.$i.'_uname'} ne '') {
                   1246:             $personnel_values .= 
1.13    ! raeburn  1247:                 '<tr><td>'.$env{'form.person_'.$i.'_firstname'}.' '.
        !          1248:                 $env{'form.person_'.$i.'_lastname'}.'</td>'.
1.4       raeburn  1249:                 '<td>'.$env{'form.person_'.$i.'_uname'}.':'.
                   1250:                 $env{'form.person_'.$i.'_dom'}.'</td>'.
                   1251:                 '<td>'.&Apache::lonnet::plaintext($env{'form.person_'.$i.'_role'},
                   1252:                                                   $container).'</td>'.
                   1253:                 '<td>'.$env{'form.person_'.$i.'_sections'}.'</td></tr>';
                   1254:         }
                   1255:     }
                   1256:     my $output =  '<p>'.&mt('Review the details of the course request before submission.').'</p>'.  
                   1257:                   '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   1258:                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
1.6       raeburn  1259:                   '<table class="LC_innerpickbox"><tr>'.
1.4       raeburn  1260:                   '<th>'.&mt('Name').'</th>'.
                   1261:                   '<th>'.&mt('Username:Domain').'</th>'.
                   1262:                   '<th>'.&mt('E-mail address').'</th>'.
                   1263:                   '</tr><tr>'."\n".
                   1264:                   '<td>'.$ownername.'</td><td>'.$owner.'</td>'.
                   1265:                   '<td>'.$owneremail.'</td>'.
                   1266:                   '</tr></table>'."\n".
                   1267:                   &Apache::lonhtmlcommon::row_closure().
1.5       raeburn  1268:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
1.4       raeburn  1269:                   '<table class="LC_innerpickbox"><tr>'.$inst_headers.'</tr>'."\n".
                   1270:                   '<tr>'.$inst_values.'</tr></table>'."\n".
                   1271:                   &Apache::lonhtmlcommon::row_closure().
                   1272:                   &Apache::lonhtmlcommon::row_title(&mt('Enrollment')).
                   1273:                   '<table class="LC_innerpickbox"><tr>'.$enroll_headers.'</tr>'."\n".
                   1274:                   '<tr>'.$enroll_values.'</tr></table>'."\n".
                   1275:                   &Apache::lonhtmlcommon::row_closure();
                   1276:     if ($section_headers ne '') {
                   1277:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Sections')).
                   1278:                    '<table class="LC_innerpickbox"><tr>'.$section_headers.'</tr>'."\n".
                   1279:                    '<tr>'.$section_values.'</tr></table>'."\n".
                   1280:                    &Apache::lonhtmlcommon::row_closure();
                   1281:     }
                   1282:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Personnel')).
                   1283:                '<table class="LC_innerpickbox"><tr>'.$personnel_headers.'</tr>'."\n".
                   1284:                $personnel_values.'</table>'."\n".
                   1285:                &Apache::lonhtmlcommon::row_closure(1).
                   1286:                &Apache::lonhtmlcommon::end_pick_box();
1.8       raeburn  1287:     my $cnum = &Apache::lonnet::generate_coursenum($dom);
                   1288:     $output .= '<input type="hidden" name="cnum" value="'.$cnum.'" />';
1.4       raeburn  1289:     return $output;
                   1290: }
                   1291: 
                   1292: sub dates_from_form {
                   1293:     my ($startname,$endname) = @_;
                   1294:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1295:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.13    ! raeburn  1296:     if ($endname eq 'accessend') {
1.4       raeburn  1297:         if (exists($env{'form.no_end_date'}) ) {
                   1298:             $enddate = 0;
                   1299:         }
                   1300:     }
                   1301:     return ($startdate,$enddate);
1.1       raeburn  1302: }
                   1303: 
                   1304: sub courseinfo_form {
1.2       raeburn  1305:     my ($dom,$formname,$crstype) = @_;
                   1306:     my $output = '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   1307:                  &Apache::lonhtmlcommon::row_title('Course Description').
1.13    ! raeburn  1308:                  '<input type="text" size="40" name="cdescr" />';
        !          1309:     my ($home_server_pick,$numlib) =
        !          1310:         &Apache::loncommon::home_server_form_item($dom,'chome',
        !          1311:                                                   'default','hide');
        !          1312:     if ($numlib > 1) {
        !          1313:         $output .= &Apache::lonhtmlcommon::row_closure().
        !          1314:                    &Apache::lonhtmlcommon::row_title(
        !          1315:                        &mt('Home Server for Course'));
        !          1316:     }
        !          1317:     $output .= $home_server_pick.
        !          1318:                &Apache::lonhtmlcommon::row_closure(1).
        !          1319:                &Apache::lonhtmlcommon::end_pick_box().'</div>'.
        !          1320:                '<div>'.&clone_form($dom,$formname,$crstype).'</div>'."\n";
1.1       raeburn  1321:     return $output;
                   1322: }
                   1323: 
                   1324: sub clone_form {
                   1325:     my ($dom,$formname,$crstype) = @_;
                   1326:     my $type = 'Course';
                   1327:     if ($crstype eq 'community') {
                   1328:         $type = 'Community';
                   1329:     }
1.13    ! raeburn  1330:     my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedom').
        !          1331:                     &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type);
1.4       raeburn  1332:     my %lt = &clone_text();
1.2       raeburn  1333:     my $output .= 
                   1334:         &Apache::lonhtmlcommon::start_pick_box(). 
                   1335:         &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
1.13    ! raeburn  1336:         '<input type="text" size="25" name="clonecrs" value="" onfocus="this.blur();'.
        !          1337:         'opencrsbrowser('."'$formname','clonecrs','clonedom','','','','$type'".');" />'.
1.2       raeburn  1338:         '</label>'.&Apache::lonhtmlcommon::row_closure(1).'<label>'.
                   1339:         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'</label>'.
                   1340:         $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure().
                   1341:         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
                   1342:         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
                   1343:         '</label><br /><label>'.
                   1344:         '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
                   1345:         '</label><br /><label>'.
                   1346:         '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
                   1347:         $lt{'shd'}.'</label>'.
                   1348:         '<input type="text" size="5" name="dateshift" value="365" />'.
                   1349:         &Apache::lonhtmlcommon::row_closure(1).
                   1350:         &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1351:     return $output;
                   1352: }
                   1353: 
1.4       raeburn  1354: sub clone_text { 
                   1355:     return &Apache::lonlocal::texthash(
                   1356:                'cid'  => 'Course ID',
                   1357:                'dmn'  => 'Domain',
                   1358:                'dsh'  => 'Date Shift',
                   1359:                'ncd'  => 'Do not clone date parameters',
                   1360:                'prd'  => 'Clone date parameters as-is',
                   1361:                'shd'  => 'Shift date parameters by number of days',
                   1362:         );
                   1363: }
                   1364: 
1.1       raeburn  1365: sub coursecode_form {
1.2       raeburn  1366:     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
1.1       raeburn  1367:     my $output;
1.2       raeburn  1368:     my %rowtitle = (
                   1369:                     instcode  => 'Course Category',
                   1370:                     crosslist => 'Cross Listed Course',
                   1371:                    );
1.1       raeburn  1372:     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   1373:         (ref($cat_order))) {
1.2       raeburn  1374:         my ($sel,$instsec,$lcsec);
                   1375:         $sel = $context;
                   1376:         if ($context eq 'crosslist') {
                   1377:             $sel .= '_'.$num;
                   1378:             $instsec = &mt('Institutional section').'<br />'.
                   1379:                        '<input type="text" size="10" name="'.$sel.'_instsec" />';
                   1380:             $lcsec = &mt('LON-CAPA section').'<br />'.
                   1381:                      '<input type="text" size="10" name="'.$sel.'_lcsec" />';
                   1382:         }
1.1       raeburn  1383:         if (@{$codetitles} > 0) {
                   1384:             my $lastitem = pop(@{$codetitles});
1.2       raeburn  1385:             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
1.1       raeburn  1386:             if (@{$codetitles} > 0) {
1.2       raeburn  1387:                 $output = &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   1388:                           '<table><tr>';
1.1       raeburn  1389:                 if ($context eq 'crosslist') {
1.2       raeburn  1390:                     $output .= '<td>'.&mt('Include?').'<br />'.
                   1391:                                '<input type="checkbox" name="'.$sel.'" value="1" /></td>';
1.1       raeburn  1392:                 }
                   1393:                 foreach my $title (@{$codetitles}) {
                   1394:                     if (ref($cat_order->{$title}) eq 'ARRAY') {
                   1395:                         if (@{$cat_order->{$title}} > 0) {
                   1396:                             $output .= '<td align="center">'.$title.'<br />'."\n".
                   1397:                                        '<select name="'.$sel.'_'.$title.'">'."\n".
                   1398:                                        ' <option value="" selected="selected">'.
                   1399:                                        &mt('Select').'</option>'."\n";
                   1400:                             foreach my $item (@{$cat_order->{$title}}) {
                   1401:                                 my $longitem = $item;
                   1402:                                 if (ref($cat_titles->{$title}) eq 'HASH') {
                   1403:                                     if ($cat_titles->{$title}{$item} ne '') {
                   1404:                                         $longitem = $cat_titles->{$title}{$item};
                   1405:                                     }
                   1406:                                 }
                   1407:                                 $output .= '<option value="'.$item.'">'.$longitem.
                   1408:                                            '</option>'."\n";
                   1409:                             }
                   1410:                         }
                   1411:                         $output .= '</select></td>'."\n";
                   1412:                     }
                   1413:                 }
                   1414:                 if ($context eq 'crosslist') {
                   1415:                     $output .= '<td align="center">'.$lastitem.'<br />'."\n".
1.2       raeburn  1416:                                $lastinput.'</td><td align="center">'.$instsec.'</td>'.
                   1417:                                '<td align="center">'.$lcsec.'</td></tr></table>';
1.1       raeburn  1418:                 } else {
                   1419:                     $output .= '</tr></table>'.
                   1420:                                &Apache::lonhtmlcommon::row_closure().
                   1421:                                &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   1422:                                $lastinput;
                   1423:                 }
                   1424:             } else {
                   1425:                 if ($context eq 'crosslist') {
                   1426:                     $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
1.2       raeburn  1427:                                '<table><tr>'.
                   1428:                                '<td align="center">'.$lastitem.'<br />'.$lastinput.'</td>'.
                   1429:                                '<td align="center">'.$instsec.'</td><td>'.$lcsec.'</td>'.
                   1430:                                '</tr></table>';
1.1       raeburn  1431:                 } else { 
                   1432:                     $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                   1433:                                $lastinput;
                   1434:                 }
                   1435:             }
1.2       raeburn  1436:             $output .=  &Apache::lonhtmlcommon::row_closure(1);
                   1437:             push(@$codetitles,$lastitem);    
                   1438:         } elsif ($context eq 'crosslist') {
                   1439:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                   1440:                        '<table><tr><td align="center">'.
                   1441:                        '<span class="LC_nobreak">'.&mt('Include?').
                   1442:                        '<input type="checkbox" name="'.$sel.'" value="1" /></span>'.
                   1443:                        '</td><td align="center">'.&mt('Institutional ID').'<br />'.
                   1444:                        '<input type="text" size="10" name="'.$sel.'_instsec" />'.
                   1445:                        '</td><td align="center">'.$lcsec.'</td></tr></table>'.
                   1446:                        &Apache::lonhtmlcommon::row_closure(1);
1.1       raeburn  1447:         }
                   1448:     }
                   1449:     return $output;
                   1450: }
                   1451: 
                   1452: sub get_course_dom {
                   1453:     my $codedom = &Apache::lonnet::default_login_domain();
                   1454:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
                   1455:         $codedom = $env{'user.domain'};
                   1456:         if ($env{'request.role.domain'} ne '') {
                   1457:             $codedom = $env{'request.role.domain'};
                   1458:         }
                   1459:     }
                   1460:     if ($env{'form.showdom'} ne '') {
                   1461:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
                   1462:             $codedom = $env{'form.showdom'};
                   1463:         }
                   1464:     }
                   1465:     return $codedom;
                   1466: }
                   1467: 
                   1468: sub display_navbuttons {
                   1469:     my ($r,$formname,$prev,$prevtext,$next,$nexttext) = @_;
                   1470:     $r->print('<div class="LC_navbuttons">');
                   1471:     if ($prev) {
                   1472:         $r->print('
                   1473:       <input type="button" name="previous" value = "'.$prevtext.'"
                   1474:     onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>
                   1475:    &nbsp;&nbsp;&nbsp;');
                   1476:     } elsif ($prevtext) {
                   1477:         $r->print('
                   1478:       <input type="button" name="previous" value = "'.$prevtext.'"
                   1479:     onclick="javascript:history.back()"/>
                   1480:    &nbsp;&nbsp;&nbsp;');
                   1481:     }
                   1482:     if ($next) {
                   1483:         $r->print('
                   1484:       <input type="button" name="next" value="'.$nexttext.'"
                   1485:  onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
                   1486:     }
                   1487:     $r->print('</div>');
                   1488: }
                   1489: 
                   1490: sub print_request_outcome {
1.10      raeburn  1491:     my ($dom,$codetitles,$code_order) = @_;
1.13    ! raeburn  1492:     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
1.10      raeburn  1493:         %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,);
                   1494:     $cnum = $env{'form.cnum'};
1.8       raeburn  1495:     unless ($cnum =~ /^$match_courseid$/) {
                   1496:         $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
                   1497:         return $output;
                   1498:     }
1.11      raeburn  1499: 
1.10      raeburn  1500:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
1.9       raeburn  1501:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   1502:         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
                   1503:             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
                   1504:         }
                   1505:     }
1.10      raeburn  1506:     $now = time;
                   1507:     $crstype = $env{'form.crstype'};
1.8       raeburn  1508:     if ($crstype eq 'official') {
                   1509:         if (&Apache::lonnet::auto_run('',$dom)) {
1.13    ! raeburn  1510:             ($enrollstart,$enrollend)=&dates_from_form('enrollstart','enrollend');
1.8       raeburn  1511:         }
1.10      raeburn  1512:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
                   1513:             if ($env{'form.sec_'.$i}) {
                   1514:                 if ($env{'form.secnum_'.$i} ne '') {
1.13    ! raeburn  1515:                     $sections{$i}{'inst'} = $env{'form.secnum_'.$i};
        !          1516:                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
1.10      raeburn  1517:                 }
                   1518:             }
                   1519:         }
                   1520:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
                   1521:             if ($env{'form.crosslist_'.$i}) {
                   1522:                 my $xlistinfo = '';
                   1523:                 if (ref($code_order) eq 'ARRAY') {
                   1524:                     if (@{$code_order} > 0) {
                   1525:                         foreach my $item (@{$code_order}) {
                   1526:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
                   1527:                         }
                   1528:                     }
                   1529:                 }
                   1530:                 $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'};
1.13    ! raeburn  1531:                 $crosslistings{$i}{'inst'} = $xlistinfo;
        !          1532:                 $crosslistings{$i}{'loncapa'} = $env{'form.crosslist_'.$i.'_lcsec'};
1.10      raeburn  1533:             }
                   1534:         }
                   1535:     }
                   1536: 
                   1537:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
                   1538:         my $uname = $env{'form.person_'.$i.'_uname'};
                   1539:         my $udom = $env{'form.person_'.$i.'_uname'};
                   1540:         if (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
                   1541:             if (&Apache::lonnet::domain($udom) ne '') {
1.13    ! raeburn  1542:                 unless (ref($personnel{$uname.':'.$udom}) eq 'HASH') {
        !          1543:                     $personnel{$uname.':'.$udom} = {
        !          1544:                              firstname    => $env{'form.person_'.$i.'_firstname'},
        !          1545:                              lastname     => $env{'form.person_'.$i.'_lastname'},
        !          1546:                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
        !          1547:                                                    };
        !          1548:                 }
        !          1549:                 my $role = $env{'form.person_'.$i.'_role'};
        !          1550:                 unless ($role eq '') {
        !          1551:                     if (ref(@{$personnel{$uname.':'.$udom}{'roles'}}) eq 'ARRAY') {
        !          1552:                         my @curr_roles = @{$personnel{$uname.':'.$udom}{'roles'}};
        !          1553:                         unless (grep(/^\Q$role\E$/,@curr_roles)) {
        !          1554:                             push(@{$personnel{$uname.':'.$udom}{'roles'}},$role);
        !          1555:                         }
        !          1556:                     } else {
        !          1557:                         @{$personnel{$uname.':'.$udom}{'roles'}} = ($role);
        !          1558:                     }
        !          1559:                     if ($role eq 'cc') {
        !          1560:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
        !          1561:                     } else {
        !          1562:                         my @currsec;
        !          1563:                         if ($env{'form.person_'.$i.'_sections'} ne '') {
        !          1564:                             my @sections = split(',',$env{'form.person_'.$i.'_sections'});
        !          1565:                             foreach my $sec (@sections) {
        !          1566:                                 next if ($sec eq '');
        !          1567:                                 if (@currsec > 0) {
        !          1568:                                     unless(grep(/^\Q$sec\E$/,@currsec)) {
        !          1569:                                         push(@currsec,$sec);
        !          1570:                                     }
        !          1571:                                 }
        !          1572:                             }
        !          1573:                         }
        !          1574:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @currsec;
        !          1575:                     }
        !          1576:                 }
1.10      raeburn  1577:             } else {
                   1578:                 push(@missingdom,$uname.':'.$udom);
                   1579:             }
                   1580:         } else {
                   1581:             push(@baduname,$uname.':'.$udom);
                   1582:         }
1.8       raeburn  1583:     }
1.13    ! raeburn  1584:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
1.8       raeburn  1585:     my $details = {
1.10      raeburn  1586:                     owner          => $env{'user.name'},
                   1587:                     domain         => $env{'user.domain'}, 
                   1588:                     cdom           => $dom,
1.11      raeburn  1589:                     cnum           => $cnum,
1.13    ! raeburn  1590:                     coursehome     => $env{'form.chome'},
        !          1591:                     cdescr         => $env{'form.cdescr'},
1.10      raeburn  1592:                     crstype        => $env{'form.crstype'},
                   1593:                     instcode       => $env{'form.instcode'},
1.13    ! raeburn  1594:                     clonedom       => $env{'form.clonedom'},
        !          1595:                     clonecrs       => $env{'form.clonecrs'},
1.10      raeburn  1596:                     datemode       => $env{'form.datemode'},
                   1597:                     dateshift      => $env{'form.datshift'},
                   1598:                     sectotal       => $env{'form.sectotal'},
                   1599:                     sections       => \%sections,
                   1600:                     crosslisttotal => $env{'form.crosslisttotal'},
1.13    ! raeburn  1601:                     crosslists     => \%crosslistings,
1.10      raeburn  1602:                     autoadds       => $env{'form.autoadds'},
                   1603:                     autodrops      => $env{'form.autodrops'},
1.13    ! raeburn  1604:                     enrollstart    => $enrollstart,
        !          1605:                     enrollend      => $enrollend,
        !          1606:                     accessstart    => $accessstart,
        !          1607:                     accessend      => $accessend,
1.10      raeburn  1608:                     personnel      => \%personnel,
1.8       raeburn  1609:                   };
                   1610:     my @inststatuses;
1.9       raeburn  1611:     my $val = &get_processtype($dom,$crstype,\@inststatuses,\%domconfig);
1.8       raeburn  1612:     if ($val eq '') {
                   1613:         if ($crstype eq 'official') {
                   1614:             $output = &mt('You are not permitted to request creation of official courses');
                   1615:         } elsif ($crstype eq 'unofficial') {
                   1616:             $output = &mt('You are not permitted to request creation of unofficial courses');
                   1617:         } elsif ($crstype eq 'community') {
                   1618:             $output = &mt('You are not permitted to request creation of communities');
                   1619:         } else {
                   1620:             $output = &mt('Unrecognized course type: [_1]',$crstype);
                   1621:         }
                   1622:     } else {
                   1623:         my ($disposition,$message);
                   1624:         my %reqhash = (
1.10      raeburn  1625:                         timestamp => $now,
                   1626:                         crstype   => $crstype,
                   1627:                         details   => $details,
1.8       raeburn  1628:                       );
                   1629:         my $requestkey = $dom.'_'.$cnum;
1.10      raeburn  1630:         if ($val eq 'autolimit=') {
                   1631:             $disposition = 'process';
                   1632:         } elsif ($val =~ /^autolimit=(\d+)$/) {
                   1633:             my $limit = $1;
1.8       raeburn  1634:             $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
1.10      raeburn  1635:                                             $dom,$crstype,$limit,\$message);
1.8       raeburn  1636:         } elsif ($val eq 'validate') {
                   1637:             $disposition =
                   1638:                 &Apache::lonnet::auto_courserequest_validation($dom,$details,
                   1639:                                                               \@inststatuses,\$message);
                   1640:         } else {
                   1641:             $disposition = 'approval';
                   1642:         }
                   1643:         $reqhash{'status'} = $disposition;
                   1644:         if ($disposition eq 'rejected') {
                   1645:             $output = &mt('Your course request was rejected.');
                   1646:             if ($message) {
                   1647:                 $output .= '<div class="LC_warning">'.$message.'</div>';
                   1648:             }
                   1649:         } elsif ($disposition eq 'process') {
                   1650:             my $type = 'Course';
                   1651:             if ($crstype eq 'community') {
                   1652:                 $type = 'Community';
                   1653:             }
1.13    ! raeburn  1654:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
        !          1655:             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles);
1.8       raeburn  1656:             my @roles = &Apache::lonuserutils::roles_by_context('course');
                   1657:             foreach my $role (@roles) {
                   1658:                 $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
                   1659:             }
1.13    ! raeburn  1660:             my $owneremail;
        !          1661:             my %emails = &Apache::loncommon::getemails();
        !          1662:             foreach my $email ('permanentemail','critnotification','notification') {
        !          1663:                 $owneremail = $emails{$email};
        !          1664:                 last if ($owneremail ne '');
        !          1665:             }
        !          1666:             my %reqdetails = &build_batchcreatehash($dom,$details,$owneremail,\%domdefs);
        !          1667:             my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',\%reqdetails,\%longroles,\$logmsg,\$newusermsg,\$addresult,\$enrollcount,\$output,\$keysmsg,$env{'user.domain'},$env{'user.name'},$cnum,$crstype);
1.8       raeburn  1668:             $disposition = 'created';
1.13    ! raeburn  1669:             if ($cid eq "/$dom/$cnum") {
1.8       raeburn  1670:                 $disposition = 'created';
                   1671:                 $output = &mt('Your course request has been processed and the course has been created.').'<br />'.&mt('You will need to logout and log-in again to be able to select a role in the course.');
                   1672:             } else {
                   1673:                 $output = &mt('An error occurred when processing your course request.').'<br />'.&mt('You may want to review the request details and submit the request again.');
                   1674:             }
                   1675:         } else {
                   1676:             my $requestid = $cnum.'_'.$disposition;
                   1677:             my $request = { 
                   1678:                             $requestid => {
                   1679:                                             timestamp   => $now,
                   1680:                                             crstype     => $crstype,
                   1681:                                             ownername   => $env{'user.name'},
                   1682:                                             ownerdom    => $env{'user.domain'},
1.13    ! raeburn  1683:                                             description => $env{'form.cdescr'}, 
1.8       raeburn  1684:                                           },
                   1685:                           };
                   1686:             my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
                   1687:                                                         $dom);
                   1688:             if ($putresult eq 'ok') {
                   1689:                 my %emails = &Apache::loncommon::getemails();
                   1690:                 my $address;
                   1691:                 if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                   1692:                     $address = $emails{'permanentemail'};
                   1693:                     if ($address eq '') {
                   1694:                         $address = $emails{'notification'};
                   1695:                     }
                   1696:                 }
                   1697:                 $output = &mt('Your course request has been recorded.').'<br />';
                   1698:                 if ($disposition eq 'approval') {
                   1699:                     $output .= &mt('Your course request has been recorded.').'<br />'.
                   1700:                               &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
                   1701:                               &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
                   1702:                     if ($address ne '') {     
                   1703:                         $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                   1704:                     }
                   1705:                     if ($req_notifylist) {
                   1706:                         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
                   1707:                                                                      $env{'user.domain'});
                   1708:                         &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,$fullname,$now,$dom,$details);
                   1709:                     }
                   1710:                 } else {
                   1711:                     $output .= '<div class="LC_info">'.
                   1712: &mt('Your request has been placed in a queue pending administrative action.').'<br />'.
                   1713: &mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
                   1714: &mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
                   1715:                                '</div>';
                   1716:                 }
                   1717:             } else {
                   1718:                 $reqhash{'status'} = 'domainerror';
                   1719:                 $reqhash{'disposition'} = $disposition;
                   1720:                 my $warning = &mt('An error occurred saving your request in the pending requests queue.');
                   1721:                 $output = '<span class"LC_warning">'.$warning.'</span><br />';
                   1722:                 
                   1723:             }
                   1724:         }
1.10      raeburn  1725:         my $storeresult;
                   1726:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   1727:             $storeresult = &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                   1728:                                                            'courserequests');
                   1729:         } else {
                   1730:             $storeresult = 'error: invalid requestkey format'; 
                   1731:         }
1.8       raeburn  1732:         if ($storeresult ne 'ok') {
1.13    ! raeburn  1733:             $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the details of your request: [_1].',$storeresult).'</span><br />';
        !          1734:             &Apache::lonnet::logthis("Error saving course request - $requestkey for $env{'user.name'}:$env{'user.domain'} - $storeresult");
1.8       raeburn  1735:         }
                   1736:     }
                   1737:     return $output;
                   1738: }
                   1739: 
                   1740: sub get_processtype {
1.9       raeburn  1741:     my ($dom,$crstype,$inststatuses,$domconfig) = @_;
                   1742:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
1.8       raeburn  1743:     my (%userenv,%settings,$val);
                   1744:     my @options = ('autolimit','validate','approve');
                   1745:     if ($dom eq $env{'user.domain'}) {
                   1746:         %userenv = 
                   1747:             &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
                   1748:                 'requestcourses.'.$crstype,'inststatus');
                   1749:         if ($userenv{'requestcourses.'.$crstype}) {
                   1750:             $val = $userenv{'requestcourses.'.$crstype};
                   1751:             @{$inststatuses} = ('_custom_');
                   1752:         } else {
                   1753:             my ($task,%alltasks);
1.9       raeburn  1754:             if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
                   1755:                 %settings = %{$domconfig->{'requestcourses'}};
1.8       raeburn  1756:                 if (ref($settings{$crstype}) eq 'HASH') {
                   1757:                     if (($env{'user.adv'}) && (exists($settings{$crstype}{'_LC_adv'}))) {
                   1758:                         $val = $settings{$crstype}{'_LC_adv'};
                   1759:                         @{$inststatuses} = ('_LC_adv_');
                   1760:                     } else {
                   1761:                         if ($userenv{'inststatus'} ne '') {
                   1762:                             @{$inststatuses} = split(',',$userenv{'inststatus'});
                   1763:                         } else {
1.13    ! raeburn  1764:                             @{$inststatuses} = ('default');
1.8       raeburn  1765:                         }
                   1766:                         foreach my $status (@{$inststatuses}) {
                   1767:                             if (exists($settings{$crstype}{$status})) {
                   1768:                                 my $value = $settings{$crstype}{$status};
                   1769:                                 next unless ($value); 
                   1770:                                 unless (exists($alltasks{$value})) {
                   1771:                                     if (ref($alltasks{$value}) eq 'ARRAY') {
                   1772:                                         unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                   1773:                                             push(@{$alltasks{$value}},$status); 
                   1774:                                         }
                   1775:                                     } else {
                   1776:                                         @{$alltasks{$value}} = ($status);
                   1777:                                     }
                   1778:                                 }
                   1779:                             }
                   1780:                         }
                   1781:                         my $maxlimit = 0;
1.13    ! raeburn  1782:                         
1.8       raeburn  1783:                         foreach my $key (sort(keys(%alltasks))) {
                   1784:                             if ($key =~ /^autolimit=(\d*)$/) {
                   1785:                                 if ($1 eq '') {
                   1786:                                     $val ='autolimit=';
                   1787:                                     last;
                   1788:                                 } elsif ($1 > $maxlimit) {
                   1789:                                     $maxlimit = $1; 
                   1790:                                 }
                   1791:                             }
                   1792:                         }
                   1793:                         if ($maxlimit) {
                   1794:                             $val = 'autolimit='.$maxlimit;
                   1795:                         } else {
                   1796:                             foreach my $option (@options) {
                   1797:                                 if ($alltasks{$option}) {
                   1798:                                     $val = $option;
                   1799:                                     last;  
                   1800:                                 }
                   1801:                             }
                   1802:                         }
                   1803:                     }
                   1804:                 }
                   1805:             }
                   1806:         }
                   1807:     } else {
                   1808:         %userenv = &Apache::lonnet::userenvironment($env{'user.domain'},
                   1809:                       $env{'user.name'},'reqcrsotherdom.'.$env{'form.crstype'});
                   1810:         if ($userenv{'reqcrsotherdom'}) {
                   1811:             my @doms = split(',',$userenv{'reqcrsotherdom'});
                   1812:             my $optregex = join('|',@options); 
                   1813:             if (grep(/^\Q$dom\E:($optregex=?\d*)/,@doms)) {
                   1814:                 $val = $1;
                   1815:             }
                   1816:             @{$inststatuses} = ('_external_');
                   1817:         }
                   1818:     }
                   1819:     return $val;
                   1820: }
                   1821: 
                   1822: sub check_autolimit {
1.10      raeburn  1823:     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
                   1824:     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
                   1825:                                         'userroles',['active','future'],['cc'],[$dom]);
                   1826:     my ($types,$typename) = &course_types();
                   1827:     my %requests = &Apache::lonnet::dumpstore('courserequests',$udom,$uname);
                   1828:     my %count;
                   1829:     if (ref($types) eq 'ARRAY') {
                   1830:         foreach my $type (@{$types}) {
                   1831:             $count{$type} = 0;
                   1832:         }
                   1833:     }
                   1834:     foreach my $key (keys(%requests)) {
                   1835:         my ($cdom,$cnum) = split('_',$key);
                   1836:         if (exists($crsroles{$cnum.':'.$cdom.':cc'})) {
                   1837:             if (ref($requests{$key}) eq 'HASH') {
                   1838:                 my $type = $requests{$key}{'crstype'};
                   1839:                 if ($type =~ /^official|unofficial|community$/) {
                   1840:                     $count{$type} ++;
                   1841:                 }
                   1842:             }
                   1843:         }
                   1844:     }
                   1845:     if ($count{$crstype} < $limit) {
                   1846:         return 'process';
                   1847:     } else {
                   1848:         if (ref($typename) eq 'HASH') {
                   1849:             $$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);
                   1850:         }
                   1851:         return 'rejected';
                   1852:     }
1.1       raeburn  1853:     return;
                   1854: }
                   1855: 
1.8       raeburn  1856: sub build_batchcreatehash {
1.13    ! raeburn  1857:     my ($dom,$details,$owneremail,$domdefs) = @_;
1.8       raeburn  1858:     my %batchhash;
1.13    ! raeburn  1859:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
        !          1860:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
        !          1861:         my $emailenc = &Apache::lonnet::escape($owneremail);
        !          1862:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
        !          1863:         foreach my $item (@items) {
        !          1864:             $batchhash{$item} = $details->{$item};
        !          1865:         }
        !          1866:         $batchhash{'title'} = $details->{'cdescr'};
        !          1867:         $batchhash{'coursecode'} = $details->{'instcode'};
        !          1868:         $batchhash{'emailenc'} = $emailenc;
        !          1869:         $batchhash{'adds'} = $details->{'autoadds'};
        !          1870:         $batchhash{'drops'} = $details->{'autodrops'};
        !          1871:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
        !          1872:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
        !          1873:         if ($details->{'crstype'} eq 'community') {
        !          1874:             $batchhash{'crstype'} = 'Community';
        !          1875:         } else {
        !          1876:             $batchhash{'crstype'} = 'Course'; 
        !          1877:         }
        !          1878:         $batchhash{'users'}{$details->{$owner}} = {
        !          1879:                                  firstname => $env{'environment.first'},
        !          1880:                                  lastname  => $env{'environment.last'},
        !          1881:                                  emailenc  => $emailenc,
        !          1882:                                  email     => $owneremail,
        !          1883:                              };
1.8       raeburn  1884:     }
                   1885:     return %batchhash;
                   1886: }
                   1887: 
1.2       raeburn  1888: sub retrieve_settings {
                   1889:     my ($dom,$request_id) = @_;
                   1890:     my %reqinfo = &get_request_settings($request_id,$dom);
                   1891:     my %stored;
                   1892:     $stored{'cdescr'} = &unescape($reqinfo{'description'});
1.13    ! raeburn  1893:     $stored{'accessstart'} = $reqinfo{'accessstart'};
        !          1894:     $stored{'accessend'} = $reqinfo{'accessend'};
        !          1895:     if ($stored{'accessend'} == 0) {
1.2       raeburn  1896:         $stored{'no_end_date'} = 1;
                   1897:     }
1.13    ! raeburn  1898:     $stored{'enrollstart'} = $reqinfo{'enrollstart'};
        !          1899:     $stored{'enrollend'} = $reqinfo{'enrollend'};
1.2       raeburn  1900:     $stored{'crosslist'} = $reqinfo{'crosslist'};
1.13    ! raeburn  1901:     $stored{'clonecrs'} = $reqinfo{'clonecrs'};
        !          1902:     $stored{'clonedom'} = $reqinfo{'clonedom'};
1.2       raeburn  1903:     $stored{'sections'} = $reqinfo{'sections'};
                   1904:     $stored{'personnel'} = $reqinfo{'personnel'};
                   1905: 
                   1906:     return %stored;
                   1907: }
                   1908: 
                   1909: sub get_request_settings {
                   1910:     my ($request_id,$dom);
                   1911: }
                   1912: 
1.1       raeburn  1913: 1;
                   1914: 

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