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

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

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