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

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

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