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

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

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