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

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

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