File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.96: download - view: text, annotated - select for diffs
Sat Apr 2 04:30:21 2016 UTC (8 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6808. New course container -- "Placement" for Placement Tests.
  Work in progress.

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

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