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

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

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