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

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

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