File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.95.2.2: download - view: text, annotated - select for diffs
Sun Sep 18 20:04:02 2016 UTC (7 years, 7 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
- For 2.11
  - Backport 1.98

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

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