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

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

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