File:  [LON-CAPA] / loncom / interface / lonrequestcourse.pm
Revision 1.44: download - view: text, annotated - select for diffs
Sun Feb 21 23:38:51 2010 UTC (14 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Code to update user's courserequests.db moved to &update_coursereq_status() in loncoursequeueadmin.pm
  to be more widely available.

    1: # The LearningOnline Network
    2: # Request a course
    3: #
    4: # $Id: lonrequestcourse.pm,v 1.44 2010/02/21 23:38:51 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']);
  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);
  138: 
  139:     $states{'display'} = ['details'];
  140:     $states{'view'} = ['pick_request','details','cancel','removal'];
  141:     $states{'log'} = ['filter','display'];
  142:     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
  143: 
  144:     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
  145:         unless ($env{'form.state'} eq 'crstype') {
  146:             unshift(@{$states{'new'}},'codepick');
  147:         }
  148:     }
  149: 
  150:     foreach my $key (keys(%states)) {
  151:         if (ref($states{$key}) eq 'ARRAY') {
  152:             unshift (@{$states{$key}},'crstype');
  153:         }
  154:     }
  155: 
  156:     my @invalidcrosslist;
  157:     my %trail = (
  158:                  crstype       => 'Request Action',
  159:                  codepick      => 'Category',
  160:                  courseinfo    => 'Description',
  161:                  enrollment    => 'Access Dates',
  162:                  personnel     => 'Personnel',
  163:                  review        => 'Review',
  164:                  process       => 'Result',
  165:                  pick_request  => 'Display Summary',
  166:                  details       => 'Request Details',
  167:                  cancel        => 'Cancel Request',
  168:                  removal       => 'Outcome',
  169:                 );
  170: 
  171:     if (($env{'form.crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
  172:         $trail{'enrollment'} = 'Enrollment';
  173:     }
  174: 
  175:     my ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description) = 
  176:         &get_breadcrumbs($dom,$action,\$state,\%states,\%trail);
  177:     if ($action eq 'display') {
  178:         if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
  179:             if ($env{'form.cnum'} ne '') {
  180:                 my $cnum = $env{'form.cnum'};
  181:                 my $reqkey = $cnum.'_approval';
  182:                 my $namespace = 'courserequestqueue';
  183:                 my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
  184:                 my %queued =
  185:                     &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
  186:                 if (ref($queued{$reqkey}) eq 'HASH') {
  187:                     $uname = $queued{$reqkey}{'ownername'};
  188:                     $udom  = $queued{$reqkey}{'ownerdom'};
  189:                     if (($udom =~ /^$match_domain$/) && ($uname =~ /^$match_username$/)) {
  190:                         $result = &retrieve_settings($dom,$cnum,$udom,$uname);
  191:                     } else {
  192:                         if ($env{'form.crstype'} eq 'community') {
  193:                             $warning = &mt('Invalid username or domain for community requestor');
  194:                         } else {
  195:                             $warning = &mt('Invalid username or domain for course requestor');
  196:                         }
  197:                     }
  198:                 } else {
  199:                     if ($env{'form.crstype'} eq 'community') {
  200:                         $warning = &mt('No information was found for this community request.');
  201:                     } else {
  202:                         $warning = &mt('No information was found for this course request.');
  203:                     }
  204:                 }
  205:             } else {
  206:                 $warning = &mt('No course request ID provided.');
  207:             }
  208:         } else {
  209:             if ($env{'form.crstype'} eq 'any') {
  210:                $warning = &mt('You do not have rights to view course or community request information.');
  211:             } elsif ($env{'form.crstype'} eq 'community') {
  212:                 $warning = &mt('You do not have rights to view community request information.');
  213:             } else {
  214:                 $warning = &mt('You do not have rights to view course request information.');
  215:             }
  216:         }
  217:     } elsif ((defined($state)) && (defined($action))) {
  218:         if (($action eq 'view') && ($state eq 'details')) {
  219:             if ((defined($env{'form.showdom'})) && (defined($env{'form.cnum'}))) {
  220:                 my $result = &retrieve_settings($env{'form.showdom'},$env{'form.cnum'});
  221:             }
  222:         } elsif ($env{'form.crstype'} eq 'official') {
  223:             if (&Apache::lonnet::auto_run('',$dom)) {
  224:                 if (($action eq 'new') && (($state eq 'enrollment') || 
  225:                     ($state eq 'personnel'))) {
  226:                     my $checkcrosslist = 0;
  227:                     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  228:                         if ($env{'form.crosslist_'.$i}) {
  229:                             $checkcrosslist ++;
  230:                         }
  231:                     }
  232:                     if ($checkcrosslist) {
  233:                         my %codechk;
  234:                         my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
  235:                         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,
  236:                                                                  \%cat_titles,
  237:                                                                  \%cat_order,
  238:                                                                  \@code_order);
  239:                         my $numtitles = scalar(@codetitles);
  240:                         if ($numtitles) {
  241:                             for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  242:                                 if ($env{'form.crosslist_'.$i}) {
  243:                                     my $codecheck;
  244:                                     my $crosslistcode = '';
  245:                                     foreach my $item (@code_order) {
  246:                                         $crosslistcode .= $env{'form.crosslist_'.$i.'_'.$item}; 
  247:                                     }
  248:                                     if ($crosslistcode ne '') { 
  249:                                          ($codechk{$i}, my $rest) = 
  250:                                             &Apache::lonnet::auto_validate_instcode('',$dom,$crosslistcode);
  251:                                     }
  252:                                     unless ($codechk{$i} eq 'valid') {
  253:                                         $env{'form.crosslist_'.$i} = '';
  254:                                         push(@invalidcrosslist,$crosslistcode);
  255:                                     } 
  256:                                 }
  257:                             }
  258:                         }
  259:                     }
  260:                 }
  261:             }
  262:         }
  263:         my %elements =  &form_elements($dom);
  264:         my $elementsref = {};
  265:         if (ref($elements{$action}) eq 'HASH') {
  266:             if (ref($elements{$action}{$state}) eq 'HASH') {
  267:                 $elementsref = $elements{$action}{$state};
  268:             }
  269:         }
  270:         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
  271:             $env{'form.clonedom'} = $dom;
  272:         }
  273:         if ($state eq 'crstype') {
  274:             $jscript = &mainmenu_javascript();
  275:         } else {
  276:             $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
  277:         }
  278:     }
  279: 
  280:     if ($state eq 'personnel') {
  281:         $jscript .= "\n".&Apache::loncommon::userbrowser_javascript();
  282:     }
  283: 
  284:     my $loaditems = &onload_action($action,$state);
  285: 
  286:     my (%can_request,%request_domains);
  287:     my $canreq = 
  288:         &Apache::lonnet::check_can_request($dom,\%can_request,\%request_domains);
  289:     if ($action eq 'new') {
  290:         if ($canreq) {
  291:             if ($state eq 'crstype') {
  292:                 &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,
  293:                                  $crumb,\%request_domains);
  294:             } else {
  295:                 &request_administration($r,$action,$state,$page,\%states,$dom,
  296:                                         $jscript,$loaditems,$crumb,$newinstcode,
  297:                                         $codechk,$checkedcode,$description,
  298:                                         \@invalidcrosslist);
  299:             }
  300:         } else {
  301:             $r->print(&header('Course/Community Requests').$crumb.
  302:                       '<div class="LC_warning">'.
  303:                       &mt('You do not have privileges to request creation of courses or communities.').
  304:                       '</div>'.&Apache::loncommon::end_page());
  305:         }
  306:     } elsif ($action eq 'view') {
  307:         if ($state eq 'crstype') {
  308:             &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,$crumb,\%request_domains);
  309:         } else {
  310:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
  311:                                     $loaditems,$crumb);
  312:         }
  313:     } elsif ($action eq 'display') {
  314:         if ($warning ne '') {
  315:             my $args = { only_body => 1 };
  316:             $r->print(&header('Course/Community Requests','','',$args).$crumb.
  317:                       '<h3>'.&mt('Course/Community Request Details').'</h3>'.
  318:                       '<div class="LC_warning">'.$warning.'</div>'.
  319:                       &close_popup_form());
  320:         } else {
  321:             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
  322:                                     $loaditems,$crumb,'','','','','',$uname,$udom);
  323:         }
  324:     } elsif ($action eq 'log') {
  325:         &print_request_logs($jscript,$loaditems,$crumb);
  326:     } else {
  327:         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb,\%request_domains);
  328:     }
  329:     return OK;
  330: }
  331: 
  332: sub mainmenu_javascript {
  333:     return <<"END";
  334: function setType(courseForm) {
  335:     for (var i=0; i<courseForm.crstype.length; i++) {
  336:         if (courseForm.crstype.options[i].value == "$env{'form.crstype'}") {
  337:             courseForm.crstype.options[i].selected = true;
  338:         } else {
  339:             courseForm.crstype.options[i].selected = false;
  340:         }
  341:     }
  342: }
  343: 
  344: function setAction(courseForm) {
  345:     for (var i=0; i<courseForm.action.length; i++) {
  346:         if (courseForm.action.options[i].value == "$env{'form.action'}") {
  347:             courseForm.action.options[i].selected = true;
  348:         } else {
  349:             courseForm.action.options[i].selected = false;
  350:         }
  351:     }
  352: }
  353: END
  354: }
  355: 
  356: sub get_breadcrumbs {
  357:     my ($dom,$action,$state,$states,$trail) = @_;
  358:     my ($crumb,$newinstcode,$codechk,$checkedcode,$numtitles,$description);
  359:     my $page = 0;
  360:     if ((ref($states) eq 'HASH') && (ref($trail) eq 'HASH') && (ref($state))) {
  361:         if (defined($action)) {
  362:             my $done = 0;
  363:             my $i=0;
  364:             if (ref($states->{$action}) eq 'ARRAY') {
  365:                 while ($i<@{$states->{$action}} && !$done) {
  366:                     if ($states->{$action}[$i] eq $$state) {
  367:                         $page = $i;
  368:                         $done = 1;
  369:                     }
  370:                     $i++;
  371:                 }
  372:             }
  373:             if ($env{'form.crstype'} eq 'official') {
  374:                 if ($page > 1) {
  375:                     if ($states->{$action}[$page-1] eq 'codepick') {
  376:                         if ($env{'form.instcode'} eq '') {
  377:                             ($newinstcode,$numtitles) = &get_instcode($dom);
  378:                             if ($numtitles) {
  379:                                 if ($newinstcode eq '') {
  380:                                     $$state = 'codepick';
  381:                                     $page --;
  382:                                 } else {
  383:                                     ($codechk,$description) = 
  384:                                         &Apache::lonnet::auto_validate_instcode('',
  385:                                             $dom,$newinstcode);
  386:                                     if ($codechk ne 'valid') {
  387:                                         $$state = 'codepick';
  388:                                         $page --;
  389:                                     }
  390:                                     $checkedcode = 1;
  391:                                 }
  392:                             }
  393:                         }
  394:                     }
  395:                 }
  396:             }
  397:             for (my $i=0; $i<@{$states->{$action}}; $i++) {
  398:                 if ($$state eq $states->{$action}[$i]) {
  399:                     &Apache::lonhtmlcommon::add_breadcrumb(
  400:                        {text=>"$trail->{$$state}"});
  401:                     $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  402:                     last;
  403:                 } else {
  404:                     if (($$state eq 'process') || ($$state eq 'removal')) {
  405:                         &Apache::lonhtmlcommon::add_breadcrumb(
  406:                             { href => '/adm/requestcourse',
  407:                               text => "$trail->{$states->{$action}[$i]}",
  408:                             }
  409:                         );
  410:                     } else {
  411:                         &Apache::lonhtmlcommon::add_breadcrumb(
  412:      { href => "javascript:backPage(document.requestcrs,'$states->{$action}[$i]')",
  413:        text => "$trail->{$states->{$action}[$i]}", }
  414:                        );
  415:                    }
  416:                }
  417:            }
  418:         } else {
  419:             &Apache::lonhtmlcommon::add_breadcrumb(
  420:                     {text=>'Pick Action'});
  421:             $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  422:         }
  423:     } else {
  424:         &Apache::lonhtmlcommon::add_breadcrumb(
  425:                 {text=>'Pick Action'});
  426:         $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
  427:     }
  428:     return ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description);
  429: }
  430: 
  431: sub header {
  432:     my ($bodytitle,$jscript,$loaditems,$jsextra,$args) = @_;
  433:     if ($jscript) {
  434:         $jscript = '<script type="text/javascript">'."\n".
  435:                    '// <![CDATA['."\n".
  436:                    $jscript."\n".'// ]]>'."\n".'</script>'."\n";
  437:     }
  438:     if ($loaditems) {
  439:         if (ref($args) eq 'HASH') {
  440:             my %loadhash = (
  441:                              'add_entries' => $loaditems,
  442:                            );
  443:             my %arghash = (%loadhash,%{$args});
  444:             $args = \%arghash;                  
  445:         } else {
  446:             $args = {'add_entries' => $loaditems,};
  447:         }
  448:     }
  449:     return &Apache::loncommon::start_page($bodytitle,$jscript.$jsextra,$args);
  450: }
  451: 
  452: sub form_elements {
  453:     my ($dom) = @_;
  454:     my %elements =
  455:     (
  456:         new => {
  457:             crstype => {
  458:                 crstype => 'selectbox',
  459:                 action  => 'selectbox',
  460:                 origcnum => 'hidden', 
  461:             },
  462:             courseinfo => {
  463:                 cdescr           => 'text',
  464:                 clonecrs         => 'text',
  465:                 clonedom         => 'selectbox',
  466:                 datemode         => 'radio',
  467:                 dateshift        => 'text',
  468:             },
  469:             enrollment  => {
  470:                 accessstart_month  => 'selectbox',
  471:                 accessstart_hour   => 'selectbox',
  472:                 accessend_month    => 'selectbox',
  473:                 accessend_hour     => 'selectbox',
  474:                 accessstart_day    => 'text',
  475:                 accessstart_year   => 'text',
  476:                 accessstart_minute => 'text',
  477:                 accessstart_second => 'text',
  478:                 accessend_day      => 'text',
  479:                 accessend_year     => 'text',
  480:                 accessend_minute   => 'text',
  481:                 accessend_second   => 'text',
  482:                 no_end_date      => 'checkbox',
  483:             },
  484:             personnel => {
  485:                 addperson   => 'checkbox', 
  486:             },
  487:             review => {
  488:                 cnum => 'hidden',
  489:             },
  490:          },
  491:          view => {
  492:             crstype => {
  493:                 crstype => 'selectbox',
  494:                 action  => 'selectbox',
  495:             },
  496:          },
  497:     );
  498:     my %servers = &Apache::lonnet::get_servers($dom,'library');
  499:     my $numlib = keys(%servers);
  500:     if ($numlib > 1) {
  501:         $elements{'new'}{'courseinfo'}{'chome'} = 'selectbox';
  502:     } else {
  503:         $elements{'new'}{'courseinfo'}{'chome'} = 'hidden';
  504:     }
  505:     my (@codetitles,%cat_titles,%cat_order,@code_order,$lastitem);
  506:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
  507:                                              \%cat_order,\@code_order);
  508:     my $numtitles = scalar(@codetitles);
  509:     if ($numtitles) {
  510:         my %extras;
  511:         $lastitem = pop(@codetitles);
  512:         $extras{'instcode_'.$lastitem} = 'text'; 
  513:         foreach my $item (@codetitles) {
  514:             $extras{'instcode_'.$item} = 'selectbox';   
  515:         }
  516:         $elements{'new'}{'codepick'} = \%extras;
  517:     }
  518:     if (&Apache::lonnet::auto_run('',$dom)) {
  519:         my %extras = (
  520:                        sectotal           => 'hidden',
  521:                        enrollstart_month  => 'selectbox',
  522:                        enrollstart_hour   => 'selectbox',
  523:                        enrollend_month    => 'selectbox',
  524:                        enrollend_hour     => 'selectbox',
  525:                        enrollstart_day    => 'text',
  526:                        enrollstart_year   => 'text',
  527:                        enrollstart_minute => 'text',
  528:                        enrollstart_second => 'text',
  529:                        enrollend_day      => 'text',
  530:                        enrollend_year     => 'text',
  531:                        enrollend_minute   => 'text',
  532:                        enrollend_second   => 'text',
  533:                        addcrosslist       => 'checkbox',
  534:                        autoadds           => 'radio',
  535:                        autodrops          => 'radio',
  536:                      );
  537:         if ($env{'form.sectotal'} > 0) {
  538:             for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
  539:                 $extras{'sec_'.$i} = 'radio';
  540:                 $extras{'secnum_'.$i} = 'text';
  541:                 $extras{'loncapasec_'.$i} = 'text';
  542:             }
  543:         }
  544:         my $crosslisttotal = $env{'form.crosslisttotal'};
  545:         if ($env{'form.addcrosslist'}) {
  546:             $crosslisttotal ++;
  547:         }
  548:         if (!$crosslisttotal) {
  549:             $crosslisttotal = 1;
  550:         }
  551: 
  552:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
  553:             if ($numtitles) {
  554:                 $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
  555:             }
  556:             if (@codetitles > 0) {
  557:                 foreach my $item (@codetitles) {
  558:                     $extras{'crosslist_'.$i.'_'.$item} = 'selectbox';
  559:                 }
  560:             }
  561:             $extras{'crosslist_'.$i} = 'checkbox';
  562:             $extras{'crosslist_'.$i.'_instsec'} = 'text',
  563:             $extras{'crosslist_'.$i.'_lcsec'} = 'text',
  564:         }
  565:         my %mergedhash = (%{$elements{'new'}{'enrollment'}},%extras);
  566:         %{$elements{'new'}{'enrollment'}} = %mergedhash;
  567:     }
  568:     my %people;
  569:     my $persontotal = $env{'form.persontotal'};
  570:     if ($env{'form.addperson'}) {
  571:         $persontotal ++;
  572:     }
  573:     if ((!defined($persontotal)) || (!$persontotal)) {
  574:         $persontotal = 1;
  575:     }
  576:     for (my $i=0; $i<$persontotal; $i++) {
  577:         $people{'person_'.$i.'_uname'}     = 'text',
  578:         $people{'person_'.$i.'_dom'}       = 'selectbox',
  579:         $people{'person_'.$i.'_hidedom'}   = 'hidden',
  580:         $people{'person_'.$i.'_firstname'} = 'text',
  581:         $people{'person_'.$i.'_lastname'}  = 'text',
  582:         $people{'person_'.$i.'_emailaddr'} = 'text',
  583:         $people{'person_'.$i.'_role'}      = 'selectbox',
  584:         $people{'person_'.$i.'_sec'}       = 'selectbox',
  585:         $people{'person_'.$i.'_newsec'}    = 'text',
  586:     }
  587:     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
  588:     %{$elements{'new'}{'personnel'}} = %personnelhash;
  589:     return %elements;
  590: }
  591: 
  592: sub onload_action {
  593:     my ($action,$state) = @_;
  594:     my %loaditems;
  595:     if (($action eq 'new') || ($action eq 'view')) {
  596:         if ($state eq 'crstype') {
  597:             $loaditems{'onload'} = 'javascript:setAction(document.mainmenu_action);javascript:setType(document.mainmenu_coursetype)';
  598:         } else {
  599:             $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
  600:         }
  601:     }
  602:     return \%loaditems;
  603: }
  604: 
  605: sub print_main_menu {
  606:     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb,$request_domains) = @_;
  607:     my ($types,$typename) = &Apache::loncommon::course_types();
  608:     my $onchange;
  609:     unless ($env{'form.interface'} eq 'textual') {
  610:         $onchange = 'this.form.submit()';
  611:     }
  612: 
  613:     my $nextstate_setter = "\n";
  614:     if (ref($states) eq 'HASH') {
  615:         foreach my $key (keys(%{$states})) {
  616:             if (ref($states->{$key}) eq 'ARRAY') {
  617:                 $nextstate_setter .= 
  618: "             if (actionchoice == '$key') {
  619:                   nextstate = '".$states->{$key}[1]."';
  620:              }
  621: ";
  622:             }
  623:         }
  624:     }
  625: 
  626:     my $js = <<"END";
  627: 
  628: function nextPage(formname) {
  629:     var crschoice = document.mainmenu_coursetype.crstype.value;
  630:     var actionchoice = document.mainmenu_action.action.value;
  631:     if (check_can_request(crschoice,actionchoice) == true) {
  632:         if ((actionchoice == 'new') && (crschoice == 'official')) {
  633:             nextstate = 'codepick';
  634:         } else {
  635: $nextstate_setter 
  636:         }
  637:         formname.crstype.value = crschoice;
  638:         formname.action.value = actionchoice; 
  639:         formname.state.value= nextstate;
  640:         formname.submit();
  641:     }
  642:     return;
  643: }
  644: 
  645: function check_can_request(crschoice,actionchoice) {
  646:     var official = '';
  647:     var unofficial = '';
  648:     var community = '';    
  649: END
  650:     if (ref($can_request) eq 'HASH') {
  651:         foreach my $item (keys(%{$can_request})) {
  652:                 $js .= " 
  653:         $item = 1;
  654: ";
  655:         }
  656:     }
  657:     my %lt = &Apache::lonlocal::texthash(
  658:         official => 'You are not permitted to request creation of an official course in this domain.',
  659:         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
  660:         community => 'You are not permitted to request creation of a community this domain.',
  661:         all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
  662:     ); 
  663:     $js .= <<END;
  664:     if (crschoice == 'official') {
  665:         if (official != 1) {
  666:             alert("$lt{'official'}");
  667:             return false;
  668:         }
  669:     } else {
  670:         if (crschoice == 'unofficial') {
  671:             if (unofficial != 1) {
  672:                 alert("$lt{'unofficial'}");
  673:                 return false;
  674:             }
  675:         } else {
  676:             if (crschoice == 'community') {
  677:                 if (community != 1) {
  678:                     alert("$lt{'community'}");
  679:                     return false;
  680:                 }
  681:             } else {
  682:                 if (actionchoice == 'new') {
  683:                     alert("$lt{'all'}");
  684:                     return false;
  685:                 }               
  686:             }
  687:         }
  688:     }
  689:     return true;
  690: }
  691: END
  692:     my ($pagetitle,$pageinfo,$domaintitle);
  693:     if (ref($can_request) eq 'HASH') {
  694:         if (($can_request->{'official'}) || ($can_request->{'unofficial'})) {
  695:             if ($can_request->{'community'}) {
  696:                 $pagetitle = 'Course/Community Requests';
  697:                 $pageinfo = &mt('Request creation of a new course or community, or review your pending requests.');
  698:                 $domaintitle = &mt('Course/Community Domain');
  699:             } else {
  700:                 $pagetitle = 'Course Requests';
  701:                 $pageinfo = &mt('Request creation of a new course, or review your pending course requests.');
  702:                 $domaintitle = &mt('Course Domain');
  703:             }
  704:         } elsif ($can_request->{'community'}) {
  705:             $pagetitle = 'Community Requests';
  706:             $pageinfo = &mt('Request creation of a new course, or review your pending requests.');
  707:             $domaintitle = &mt('Community Domain');
  708:         } else {
  709:             $pagetitle = 'Course/Community Requests';
  710:             $pageinfo = &mt('You do not have rights to request creation of courses in this domain; please choose a different domain.');
  711:             $domaintitle = &mt('Course/Community Domain');
  712:         }
  713:     }
  714:     my @incdoms;
  715:     if (ref($request_domains) eq 'HASH') {
  716:         foreach my $item (keys(%{$request_domains})) {
  717:             if (ref($request_domains->{$item}) eq 'ARRAY') {
  718:                 foreach my $possdom (@{$request_domains->{$item}}) {
  719:                     unless(grep(/^\Q$possdom\E$/,@incdoms)) {
  720:                         push(@incdoms,$possdom);
  721:                     } 
  722:                 } 
  723:             }
  724:         }
  725:     }
  726:     $r->print(&header($pagetitle,$js.$jscript,$loaditems).$crumb.
  727:              '<p>'.$pageinfo.'</p>'.
  728:              '<div>'.
  729:               &Apache::lonhtmlcommon::start_pick_box().
  730:               &Apache::lonhtmlcommon::row_title($domaintitle).
  731:               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
  732:               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange,\@incdoms));
  733:     if (!$onchange) {
  734:         $r->print('&nbsp;<input type="submit" name="godom" value="'.
  735:                    &mt('Change').'" />');
  736:     }
  737:     unless ((ref($can_request) eq 'HASH') && (keys(%{$can_request}) > 0)) {
  738:         $r->print(&Apache::lonhtmlcommon::row_closure(1)."\n".
  739:                   &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n".
  740:                   &Apache::loncommon::end_page());
  741:         return;
  742:     }
  743:     $r->print('</form>'.&Apache::lonhtmlcommon::row_closure());
  744:     my $formname = 'requestcrs';
  745:     my $nexttext = &mt('Next');
  746:     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Action')).'
  747: <form name="mainmenu_action" method="post" action=""> 
  748: <select size="1" name="action" >
  749:  <option value="new">'.&mt('New request').'</option>
  750:  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
  751:  <option value="log">'.&mt('View request history').'</option>
  752: </select></form>'.
  753:               &Apache::lonhtmlcommon::row_closure(1).
  754:               &Apache::lonhtmlcommon::row_title(&mt('Type')).'
  755: <form name="mainmenu_coursetype" method="post" action="">
  756: <select size="1" name="crstype">');
  757:     if (ref($can_request) eq 'HASH') {
  758:         if (keys(%{$can_request}) > 1) {
  759:             $r->print(' <option value="any">'.&mt('All types').'</option>');
  760:         }
  761:         if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) {
  762:             foreach my $type (@{$types}) {
  763:                 next unless($can_request->{$type});
  764:                 my $selected = '';
  765:                 if ($env{'form.crstype'} eq '') {
  766:                     if ($type eq 'official') {
  767:                         $selected = ' selected="selected"';
  768:                     }
  769:                 } else {
  770:                     if ($type eq $env{'form.crstype'}) {
  771:                         $selected = ' selected="selected"';
  772:                     }
  773:                 }
  774:                 $r->print('<option value="'.$type.'"'.$selected.'>'.&mt($typename->{$type}).
  775:                           '</option>'."\n");
  776:             }
  777:         }
  778:     }
  779:     $r->print('</select></form>'."\n".
  780:               &Apache::lonhtmlcommon::row_closure(1)."\n".
  781:               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n".
  782:               '<div><form name="'.$formname.'" method="post" action="/adm/requestcourse">'."\n".
  783:               '<input type="hidden" name="state" value="crstype" />'."\n".
  784:               '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
  785:               '<input type="hidden" name="crstype" value="" />'."\n".
  786:               '<input type="hidden" name="action" value="" />'."\n".
  787:               '<input type="button" name="next" value="'.$nexttext.
  788:               '" onclick="javascript:nextPage(document.'.$formname.')" />'."\n".
  789:               '</form></div>');
  790:     $r->print(&Apache::loncommon::end_page());
  791:     return;
  792: }
  793: 
  794: sub request_administration {
  795:     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb,
  796:         $newinstcode,$codechk,$checkedcode,$description,$invalidcrosslist,
  797:         $uname,$udom) = @_;
  798:     my $js;
  799:     if (($action eq 'new') || (($action eq 'view') && ($state eq 'pick_request'))) {
  800:         $js =  <<END;
  801: 
  802: function nextPage(formname,nextstate) {
  803:     formname.state.value= nextstate;
  804:     formname.submit();
  805: }
  806: 
  807: END
  808:     }
  809:     if (($action eq 'new') || ($action eq 'view')) {
  810:         $js .= <<END;   
  811: 
  812: function backPage(formname,prevstate) {
  813:     formname.state.value = prevstate;
  814:     formname.submit();
  815: }
  816: 
  817: END
  818:     }
  819:     if ($action eq 'new') {
  820:         my $jsextra;
  821:         if ($state eq 'courseinfo') {
  822:             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
  823:         } elsif ($state eq 'enrollment') {
  824:             if (($env{'form.crstype'} eq 'official') && 
  825:                 (&Apache::lonnet::auto_run('',$dom))) {
  826:                 $js .= "\n".&section_check_javascript()."\n".&enrollment_lcsec_js();
  827:             }
  828:         } elsif ($state eq 'personnel') {
  829:             $js .= "\n".&section_check_javascript()."\n".&personnel_lcsec_js();
  830:         }
  831:         my $title;
  832:         if ($env{'form.crstype'} eq 'community') {
  833:             $title = 'Request a community';
  834:         } else {
  835:             $title = 'Request a course';
  836:         }
  837:         $r->print(&header($title,$js.$jscript,$loaditems,$jsextra).$crumb);
  838:         &print_request_form($r,$action,$state,$page,$states,$dom,$newinstcode,
  839:                             $codechk,$checkedcode,$description,$invalidcrosslist);
  840:     } elsif ($action eq 'view') {
  841:         my $jsextra;
  842:         my $formname = 'requestcrs';
  843:         my $prev = $states->{$action}[$page-1];
  844:         my $next = $states->{$action}[$page+1];
  845:         if ($state eq 'pick_request') {
  846:             $next = $states->{$action}[$page+1];
  847:             $jsextra = &viewrequest_javascript($formname,$next);
  848:         } elsif ($state eq 'details') {
  849:             $jsextra = &viewdetails_javascript($formname);
  850: 
  851:         } elsif ($state eq 'cancel') {
  852:             $jsextra = &viewcancel_javascript($formname);
  853:         }
  854:         my $title;
  855:         if ($env{'form.crstype'} eq 'community') {
  856:             $title = 'Manage community requests';
  857:         } else {
  858:             $title = 'Manage course requests';
  859:         }
  860:         $r->print(&header($title,$js.$jscript.$jsextra,$loaditems).$crumb);
  861:         my $form = '<form method="post" name="'.$formname.'" action="/adm/requestcourse" />';
  862:         if ($state eq 'pick_request') {
  863:             my $title;
  864:             if ($env{'form.crstype'} eq 'community') {
  865:                 $title = &mt('Pending community requests');
  866:             } elsif ($env{'form.crstype'} eq 'official') {
  867:                 $title = &mt('Pending requests for official courses');
  868:             } elsif ($env{'form.crstype'} eq 'unofficial') {
  869:                 $title = &mt('Pending requests for unofficial courses');
  870:             } else {
  871:                 $title = &mt('Pending course/community requests'); 
  872:             }
  873:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
  874:                       &print_request_status($dom).'</form></div>');
  875:         } elsif ($state eq 'details') {
  876:             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
  877:             my $origcnum = $env{'form.cnum'};
  878:             if ($origcnum eq '') {
  879:                 $origcnum = $env{'form.origcnum'};   
  880:             }
  881:             if ($env{'form.crstype'} eq 'official') {
  882:                 &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
  883:                                                          \%cat_order,\@code_order);
  884:             }
  885:             my $title;
  886:             if ($env{'form.crstype'} eq 'community') {
  887:                 $title = &mt('Community Request Details');
  888:             } else {
  889:                 $title = &mt('Course Request Details');
  890:             }
  891:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
  892:                       &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
  893:                                     \@code_order)."\n".
  894:                       '<input name="origcnum" value="'.$origcnum.'" type="hidden" />'."\n");
  895:             my @excluded = &get_excluded_elements($dom,$states,'new','review');
  896:             push(@excluded,'origcnum');
  897:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
  898:             my $other = 'modify';
  899:             my %navtxt = &Apache::lonlocal::texthash (
  900:                                                       prev => 'Back',
  901:                                                       other => 'Modify Request',
  902:                                                       next => 'Cancel Request',
  903:                                                      );
  904:             &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
  905:                                 $navtxt{'next'},$state,$other,$navtxt{'other'});
  906:             $r->print('</form>');
  907:         } elsif ($state eq 'cancel') {
  908:             my $title;
  909:             if ($env{'form.crstype'} eq 'community') {
  910:                 $title = &mt('Cancel community request');
  911:             } else {
  912:                 $title = &mt('Cancel course request');
  913:             }
  914:             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
  915:             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
  916:                       $output);
  917:             my @excluded = &get_excluded_elements($dom,$states,'view','cancel');
  918:             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
  919:             my %navtxt = &Apache::lonlocal::texthash (
  920:                                                       prev => 'Back',
  921:                                                       next => 'Confirm Cancellation',
  922:                                                      );
  923:             if ($result eq 'ok') {
  924:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
  925:                                     $navtxt{'next'},$state);
  926:             } else {
  927:                 &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},undef,
  928:                                     '',$state);
  929:             }
  930:             $r->print('</form>');
  931:         } elsif ($state eq 'removal') {
  932:             my $cnum = $env{'form.origcnum'};
  933:             my $statuskey = 'status:'.$dom.':'.$cnum;
  934:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
  935:                                                    $env{'user.domain'},$env{'user.name'});
  936:             my $currstatus = $userreqhash{$statuskey};
  937:             my ($result,$error); 
  938:             if (($currstatus eq 'approval') || ($currstatus eq 'pending')) { 
  939:                 my %status = (
  940:                                  $statuskey => 'cancelled',
  941:                              );
  942:                 my $statusresult = &Apache::lonnet::put('courserequests',\%status);
  943:                 if ($statusresult eq 'ok') {
  944:                     my $delresult = 
  945:                         &Apache::lonnet::del_dom('courserequestqueue',
  946:                                                  [$cnum.'_'.$currstatus],$dom);
  947:                     if ($delresult eq 'ok') {
  948:                         $result = 'ok';
  949:                     } else {
  950:                         $error = &mt('An error occurred when updating the pending requests queue: [_1]',$delresult);
  951:                     }
  952:                 } else {
  953:                     $error = &mt("An error occurred when updating the status of this request in the requestor's records: [_1]",$statusresult);
  954:                 }
  955:             } else {
  956:                 $error = &mt('The current status of this request could not be verified as pending approval/institutional action.');  
  957:             }
  958:             $r->print('<h3>'.&mt('Request Cancellation').'</h3><div>'."\n".$form."\n".
  959:                       '<input type="hidden" name="state" value="'.$state.'" />'."\n".
  960:                       '<input type="hidden" name="action" value="'.$action.'" />'."\n".
  961:                       '<input type="hidden" name="showdom" value="'.$dom.'" />'."\n".
  962:                       '<input type="hidden" name="orignum" value="'.$cnum.'" />'."\n");
  963:             if ($result eq 'ok') {
  964:                 if ($env{'form.crstype'} eq 'community') {
  965:                     $r->print(&mt('Your community request has been cancelled.'));
  966:                 } else {
  967:                     $r->print(&mt('Your course request has been cancelled.'));
  968:                 }
  969:             } else {
  970:                 $r->print('<div class="LC_error">'.
  971:                           &mt('The request cancellation process was not complete.').
  972:                           '<br />'.$error.'</div>');
  973:             }
  974:             $r->print('</form>');
  975:         }
  976:     } elsif ($action eq 'display') {
  977:         my $formname = 'requestcrs';
  978:         my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
  979:         if ($env{'form.crstype'} eq 'official') {
  980:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
  981:                                                      \%cat_order,\@code_order);
  982:         }
  983:         my ($title,$header);
  984:         if ($env{'form.crstype'} eq 'community') {
  985:             $title = 'Community Request';
  986:             $header = &mt('Community Request');
  987:         } else {
  988:             $title = 'Course Request';
  989:             $header = &mt('Course Request');
  990:         }
  991:         $r->print(&header($title,'','','',{ 'only_body' => 1}).
  992:                   $crumb."\n".'<h3>'.$header.'</h3>'.
  993:                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
  994:                                 \@code_order,$uname,$udom)."\n".'</div>'.
  995:                   &close_popup_form());
  996:     } elsif ($action eq 'log') {
  997:         $r->print(&coursereq_log('View request log',$jscript,$loaditems).$crumb);
  998:     }
  999:     $r->print(&Apache::loncommon::end_page());
 1000:     return;
 1001: }
 1002: 
 1003: sub enrollment_lcsec_js {
 1004:     my %alerts = &section_check_alerts();
 1005:     my $secname = $alerts{'badsec'};
 1006:     my $secnone = $alerts{'reserved'};
 1007:     my $output = '
 1008: function validateEnrollSections(formname,nextstate) {
 1009:     var badsectotal = 0;
 1010:     var reservedtotal = 0;
 1011:     var secTest = "";
 1012: ';
 1013:     for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 1014:         $output .= "
 1015:     var selSec = 0;
 1016:     for (var j=0; j<document.requestcrs.sec_".$i.".length; j++) {
 1017:         if (document.requestcrs.sec_".$i."[j].checked) {
 1018:             selSec = document.requestcrs.sec_".$i."[j].value;
 1019:         }
 1020:         if (selSec == 1) {
 1021:             secTest = validsection(document.requestcrs.loncapasec_".$i.");
 1022:             if (secTest == 'badsec') {
 1023:                 badsectotal++;
 1024:             }
 1025:             if (secTest == 'reserved') {
 1026:                 reservedtotal++;
 1027:             }
 1028:         }
 1029:     }
 1030: ";
 1031:     }
 1032:     for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
 1033:         $output .= "
 1034:     if (document.requestcrs.crosslist_".$i.".checked) {
 1035:         secTest = validsection(document.requestcrs.crosslist_".$i."_lcsec);
 1036:         if (secTest == 'badsec') {
 1037:             badsectotal++;
 1038:         }
 1039:         if (secTest == 'reserved') {
 1040:             reservedtotal++;
 1041:         }
 1042:     }
 1043: ";
 1044:     }
 1045:     $output .= "
 1046:     if (badsectotal>0) {
 1047:         alert('$secname');
 1048:         return false;
 1049:     }
 1050:     if (reservedtotal>0) {
 1051:         alert('$secnone');
 1052:         return false;
 1053:     }
 1054:     formname.state.value= nextstate;
 1055:     formname.submit();
 1056:     return;
 1057: }
 1058: ";
 1059:     return $output;
 1060: }
 1061: 
 1062: sub personnel_lcsec_js {
 1063:     my %alerts = &section_check_alerts();
 1064:     my $secname = $alerts{'badsec'}.'\\n'.$alerts{'separate'};
 1065:     my $secnone = $alerts{'reserved'};
 1066:     my $output = '
 1067: function validatePersonnelSections(formname,nextstate) {
 1068:     var badsectotal = 0;
 1069:     var reservedtotal = 0;
 1070:     var secTest = "";
 1071: ';
 1072:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1073:         $output .= "
 1074:     if (document.requestcrs.person_".$i."_uname.value != '') {
 1075:         secTest = validsection(document.requestcrs.person_".$i."_newsec,'1');
 1076:         if (secTest == 'badsec') {
 1077:             badsectotal++; 
 1078:         }
 1079:         if (secTest == 'reserved') {
 1080:             reservedtotal++;
 1081:         }
 1082:     }
 1083: ";
 1084:     }
 1085:     $output .= "
 1086:     if (badsectotal > 0) {
 1087:         alert('$secname');
 1088:         return false;
 1089:     } else {
 1090:         if (reservedtotal > 0) {
 1091:             alert('$secnone');
 1092:             return false;
 1093:         }
 1094:     }
 1095:     formname.state.value = nextstate;
 1096:     formname.submit();
 1097:     return;
 1098: }
 1099: ";
 1100:     return $output;
 1101: }
 1102: 
 1103: sub section_check_alerts {
 1104:     my %lt = 
 1105:         &Apache::lonlocal::texthash(
 1106:             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.",
 1107:             badsec => 'You need to change one or more LON-CAPA section names - names may only contain letters or numbers.',
 1108:             separate => 'Separate multiple sections with a comma.'
 1109:         );
 1110:     return %lt;
 1111: }
 1112: 
 1113: sub section_check_javascript {
 1114:     return <<"END";
 1115: function validsection(field,mult) {
 1116:     var str = field.value;
 1117:     var badsec=0;
 1118:     var reserved=0;
 1119:     if (window.RegExp) {
 1120:         var badsecnum=0;
 1121:         var reservednum=0;
 1122:         var pattern=/[^a-zA-Z0-9]/; 
 1123:         str = str.replace(/(^\\s*)|(\\s*\$)/gi,"");
 1124:         str = str.replace(/[ ]{2,}/gi," ");
 1125:         if (mult == '1') {
 1126:             var sections = new Array();
 1127:             sections = str.split(/\\s*[\\s,;:]\\s*/);
 1128:             var i;
 1129:             for (i=0; i<sections.length; i++) {
 1130:                 if ((sections[i] != '') && (sections[i] != undefined) && (sections[i] != null)) {
 1131:                     if (pattern.test(sections[i])) {
 1132:                         badsecnum++;
 1133:                     } else {
 1134:                         if (sections[i] == 'none') {
 1135:                             reservednum++;
 1136:                         }
 1137:                     }
 1138:                 }
 1139:             }
 1140:         } else {
 1141:             if ((str != '') && (str != undefined) && (str != null)) {
 1142:                 if (pattern.test(str)) {
 1143:                     badsecnum++;
 1144:                 } else {
 1145:                     if (str == 'none') {
 1146:                         reservednum++;
 1147:                     }
 1148:                 }
 1149:             }
 1150:         }
 1151:         if (badsecnum > 0) {
 1152:             return 'badsec';
 1153:         }
 1154:         if (reservednum > 0) {
 1155:             return 'reserved';
 1156:         }
 1157:     }
 1158:     return;
 1159: }
 1160: END
 1161: }
 1162: 
 1163: sub close_popup_form {
 1164:     my $close= &mt('Close Window');
 1165:     return << "END";
 1166: <p><form name="displayreq" action="" method="post">
 1167: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
 1168: </form></p>
 1169: END
 1170: }
 1171: 
 1172: sub get_instcode {
 1173:     my ($dom) = @_;
 1174:     my ($instcode,$numtitles);
 1175:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
 1176:     &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1177:                                              \%cat_order,\@code_order);
 1178:     $numtitles = scalar(@codetitles);
 1179:     if (@code_order > 0) {
 1180:         my $message;
 1181:         foreach my $item (@code_order) {
 1182:             $instcode .= $env{'form.instcode_'.$item};
 1183:         }
 1184:     }
 1185:     return ($instcode,$numtitles);
 1186: }
 1187: 
 1188: sub print_request_form {
 1189:     my ($r,$action,$state,$page,$states,$dom,$newinstcode,$codechk,$checkedcode,
 1190:         $description,$invalidcrosslist) = @_;
 1191:     my $formname = 'requestcrs';
 1192:     my ($next,$prev,$message,$output,$codepicker,$crstype);
 1193:     $prev = $states->{$action}[$page-1];
 1194:     $next = $states->{$action}[$page+1];
 1195:     my %navtxt = &Apache::lonlocal::texthash (
 1196:                                                prev => 'Back',
 1197:                                                next => 'Next',
 1198:                                              );
 1199:     $crstype = $env{'form.crstype'};
 1200:     $r->print('<br /><form name="'.$formname.'" method="post" action="/adm/requestcourse">');
 1201:     my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk,
 1202:         @disallowed);
 1203:     if ($crstype eq 'official') {
 1204:         if ($env{'form.instcode'} ne '') {
 1205:             $instcode = $env{'form.instcode'};
 1206:         } elsif ($newinstcode ne '') {
 1207:             $instcode = $newinstcode;
 1208:         }
 1209:         if ($checkedcode) {
 1210:             if ($codechk eq 'valid') {
 1211:                 $message = '<div class="LC_info">'.
 1212:                            &mt('The chosen course category [_1] is valid.','<b>'.
 1213:                            $instcode.'</b>').
 1214:                            '<input type="hidden" name="instcode" value="'.
 1215:                            $instcode.'" /></div>';
 1216:             } else {
 1217:                 $message = '<div class="LC_warning">'.
 1218:                             &mt('No course was found matching your choice of institutional course category.');
 1219:                 if ($codechk ne '') {
 1220:                     $message .= '<br />'.$codechk;
 1221:                 }
 1222:                 $message .= '</div>';
 1223:                 $prev = 'crstype';
 1224:             }
 1225:             $r->print($message);
 1226:         }
 1227:     }
 1228:     if ($prev eq 'crstype') {
 1229:         if ($crstype eq 'official') {
 1230:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1231:                                                      \%cat_order,\@code_order);
 1232:         }
 1233:         if (@code_order > 0) {
 1234:             $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
 1235:                                            \%cat_titles,\%cat_order);
 1236:             if ($codepicker) {
 1237:                 $r->print(&mt('Specify the course to be created.').
 1238:                           '<div>'.&Apache::lonhtmlcommon::start_pick_box().
 1239:                           $codepicker.
 1240:                           &Apache::lonhtmlcommon::end_pick_box().'</div>');
 1241:             } else {
 1242:                 $next = $states->{$action}[$page+2];
 1243:                 $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
 1244:             }
 1245:         } else {
 1246:             if ($crstype eq 'official') {
 1247:                 $next = $states->{$action}[$page+2];
 1248:             }
 1249:             $r->print(&courseinfo_form($dom,$formname,$crstype,$next));
 1250:         }
 1251:     } elsif ($prev eq 'codepick') {
 1252:         if ($instcode eq '') {
 1253:             $prev = $states->{$action}[$page-2];
 1254:         }
 1255:         $r->print(&courseinfo_form($dom,$formname,$crstype,$next,$description));
 1256:     } elsif ($state eq 'enrollment') {
 1257:         if ($crstype eq 'official') {
 1258:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1259:                                                      \%cat_order,\@code_order);
 1260:         }
 1261:         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
 1262:                                          \%cat_titles,\%cat_order,\@code_order,
 1263:                                          $invalidcrosslist));
 1264:     } elsif ($state eq 'personnel') {
 1265:         $r->print(&print_personnel_menu($dom,$formname,$crstype,$invalidcrosslist));
 1266:     } elsif ($state eq 'review') {
 1267:         my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg);
 1268:         my $now = time;
 1269:         for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1270:             my $personname = $env{'form.person_'.$i.'_uname'};
 1271:             my $persondom = $env{'form.person_'.$i.'_dom'};
 1272:             if (($personname =~ /^$match_username$/) && 
 1273:                 ($persondom =~ /^$match_domain$/)) {
 1274:                 if (&Apache::lonnet::domain($persondom)) {
 1275:                     my $personhome = 
 1276:                         &Apache::lonnet::homeserver($personname,$persondom);
 1277:                     if ($personhome eq 'no_host') {
 1278:                         if ($persondom ne $dom) {
 1279:                             my $skipuser = 1;
 1280:                             if ($env{'user.role.dc./'.$persondom.'/'}) {
 1281:                                 my ($start,$end) = split('.',$env{'user.role.dc./'.$persondom.'/'});
 1282:                                 if (((!$start) || ($start < $now)) && 
 1283:                                     ((!$end) || ($end > $now))) {
 1284:                                     $skipuser = 0;
 1285:                                 }
 1286:                             }
 1287:                             if ($skipuser) {
 1288:                                 push(@disallowed,$i);
 1289:                                 $disallowmsg{$i} = &mt('[_1] was excluded because new users need be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
 1290:                                 next;
 1291:                             }
 1292:                         }
 1293:                         if (&get_cancreate_status($persondom,$personname,$dom)) {
 1294:                             my ($allowed,$msg) = 
 1295:                                 &check_newuser_rules($persondom,$personname,
 1296:                                     \%alerts,\%rulematch,\%inst_results,
 1297:                                     \%curr_rules,\%got_rules);
 1298:                             if ($allowed) {
 1299:                                 if (ref($inst_results{$personname.':'.$persondom}) eq 'HASH') {
 1300:                                     if ($inst_results{$personname.':'.$persondom}{'lastname'} ne '') {
 1301:                                         $env{'form.person_'.$i.'_lastname'} = $inst_results{$personname.':'.$persondom}{'lastname'};
 1302:                                     }
 1303:                                     if ($inst_results{$personname.':'.$persondom}{'firstname'} ne '') {
 1304:                                         $env{'form.person_'.$i.'_firstname'} = $inst_results{$personname.':'.$persondom}{'firstname'};
 1305:                                     }
 1306:                                     if ($inst_results{$personname.':'.$persondom}{'permanentemail'} ne '') {
 1307:                                         $env{'form.person_'.$i.'_emailaddr'} = $inst_results{$personname.':'.$persondom}{'permanentemail'};
 1308:                                     }
 1309:                                 }
 1310:                             } else {
 1311:                                 push(@disallowed,$i);
 1312:                                 $disallowmsg{$i} = &mt('[_1] was excluded because the username violated format rules for the domain','<tt>'.$personname.':'.$persondom.'</tt>');  
 1313:                             }
 1314:                         } else {
 1315:                             push(@disallowed,$i);
 1316:                             $disallowmsg{$i} = &mt('[_1] was excluded because you may not request new users in the domain','<tt>'.$personname.':'.$persondom.'</tt>');
 1317:                         }
 1318:                     } else {
 1319:                         my %userenv = 
 1320:                             &Apache::lonnet::userenvironment($persondom,$personname,'lastname','firstname','permanentemail');
 1321:                         if ($env{'form.person_'.$i.'_lastname'} eq '') {
 1322:                             $env{'form.person_'.$i.'_lastname'} = $userenv{'lastname'};
 1323:                         }
 1324:                         if ($env{'form.person_'.$i.'_firstname'} eq '') {
 1325:                             $env{'form.person_'.$i.'_firstname'} = $userenv{'firstname'};
 1326:                         }
 1327:                         if ($env{'form.person_'.$i.'_emailaddr'} eq '') {
 1328:                             $env{'form.person_'.$i.'_emailaddr'} = $userenv{'permanentemail'};
 1329:                         }
 1330:                     }
 1331:                 } elsif ($personname ne '') {
 1332:                     push(@disallowed,$i);
 1333:                     $disallowmsg{$i} = &mt('[_1] was excluded because the domain is invalid','<tt>'.$personname.':'.$persondom.'</tt>');
 1334:                 }
 1335:             } elsif ($personname ne '') {
 1336:                 push(@disallowed,$i);
 1337:                 $disallowmsg{$i} = &mt('[_1] was excluded because the username or domain is invalid.','<tt>'.$personname.':'.$persondom.'</tt>');
 1338:             }
 1339:         }
 1340:         my $cnum;
 1341:         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
 1342:             $cnum = $env{'form.origcnum'};
 1343:         } else {
 1344:             $cnum = &Apache::lonnet::generate_coursenum($dom);
 1345:         }
 1346:         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1347:                                                  \%cat_order,\@code_order);
 1348:         if ($crstype eq 'community') {
 1349:             $r->print('<h3>'.&mt('Review community request details before submission').'</h3>');
 1350:         } else {
 1351:             $r->print('<h3>'.&mt('Review course request details before submission').'</h3>');
 1352:         }
 1353:         $r->print(&print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg).
 1354:                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
 1355:         if ($crstype eq 'community') {
 1356:             $navtxt{'next'} = &mt('Submit community request');
 1357:         } else {
 1358:             $navtxt{'next'} = &mt('Submit course request');
 1359:         }
 1360:     }  elsif ($state eq 'process') {
 1361:         if ($crstype eq 'official') {
 1362:             &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
 1363:                                                      \%cat_order,\@code_order);
 1364:         }
 1365:         my ($storeresult,$result) = &print_request_outcome($dom,\@codetitles,
 1366:                                                            \@code_order);
 1367:         $r->print($result);
 1368:         if (($storeresult eq 'ok') || ($storeresult eq 'created')) {
 1369:             $r->print('<p>');
 1370:             if ($storeresult eq 'ok') {
 1371:                 $r->print('<a href="/adm/requestcourse?action=view&state=details&showdom='.$dom.'&cnum='. $env{'form.cnum'}.'">'.
 1372:                           &mt('Modify this request').'</a>'.('&nbsp;'x4));
 1373:             }
 1374:             $r->print('<a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
 1375:             return;
 1376:         }
 1377:     }
 1378:     my @excluded = &get_excluded_elements($dom,$states,$action,$state);
 1379:     if ($state eq 'personnel') {
 1380:         push(@excluded,'persontotal');
 1381:     }
 1382:     if ($state eq 'review') {
 1383:         if (@disallowed > 0) {
 1384:             my @items = qw(uname dom lastname firstname emailaddr hidedom role newsec);
 1385:             my @currsecs = &current_lc_sections();
 1386:             if (@currsecs) {
 1387:                 push(@items,'sec');
 1388:             }
 1389:             my $count = 0;
 1390:             for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 1391:                 unless ($env{'form.person_'.$i.'_uname'} eq '') {
 1392:                     if (grep(/^$i$/,@disallowed)) {
 1393:                         foreach my $item (@items) {
 1394:                             $env{'form.person_'.$i.'_'.$item} = '';
 1395:                         }
 1396:                     } else { 
 1397:                         foreach my $item (@items) {
 1398:                             $env{'form.person_'.$count.'_'.$item} = $env{'form.person_'.$i.'_'.$item};
 1399:                         }
 1400:                     }
 1401:                 }
 1402:                 $count ++;
 1403:             }
 1404:             $env{'form.persontotal'} = $count;
 1405:              
 1406:         }
 1407:     }
 1408:     if ($state eq 'enrollment') {
 1409:         push(@excluded,'crosslisttotal');
 1410:     }
 1411:     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
 1412:     &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
 1413:                         $navtxt{'next'},$state);
 1414:     return;
 1415: }
 1416: 
 1417: sub get_cancreate_status {
 1418:     my ($persondom,$personname,$dom) = @_;
 1419:     my ($rules,$ruleorder) =
 1420:         &Apache::lonnet::inst_userrules($persondom,'username');
 1421:     my $usertype = &Apache::lonuserutils::check_usertype($persondom,$personname,
 1422:                                                          $rules);
 1423:     return &Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype);
 1424: }
 1425: 
 1426: sub check_newuser_rules {
 1427:     my ($persondom,$personname,$alerts,$rulematch,$inst_results,$curr_rules,
 1428:         $got_rules) = @_;
 1429:     my $allowed = 1;
 1430:     my $newuser = 1;
 1431:     my ($checkhash,$userchkmsg);
 1432:     my $checks = { 'username' => 1 };
 1433:     $checkhash->{$personname.':'.$persondom} = { 'newuser' => $newuser };
 1434:     &Apache::loncommon::user_rule_check($checkhash,$checks,$alerts,$rulematch,
 1435:                                         $inst_results,$curr_rules,$got_rules);
 1436:     if (ref($alerts->{'username'}) eq 'HASH') {
 1437:         if (ref($alerts->{'username'}{$persondom}) eq 'HASH') {
 1438:             my $domdesc =
 1439:                 &Apache::lonnet::domain($persondom,'description');
 1440:             if ($alerts->{'username'}{$persondom}{$personname}) {
 1441:                 if (ref($curr_rules->{$persondom}) eq 'HASH') {
 1442:                     $userchkmsg =
 1443:                         &Apache::loncommon::instrule_disallow_msg('username',
 1444:                                                                   $domdesc,1).
 1445:                         &Apache::loncommon::user_rule_formats($persondom,
 1446:                             $domdesc,$curr_rules->{$persondom}{'username'},
 1447:                             'username');
 1448:                 }
 1449:                 $allowed = 0;
 1450:             }
 1451:         }
 1452:     }
 1453:     return ($allowed,$userchkmsg);
 1454: }
 1455: 
 1456: sub get_excluded_elements {
 1457:     my ($dom,$states,$action,$state) = @_;
 1458:     my @excluded = ('counter');
 1459:     my %elements = &form_elements($dom);
 1460:     if (ref($states) eq 'HASH') {
 1461:         if (ref($states->{$action}) eq 'ARRAY') {
 1462:             my @items = @{$states->{$action}};
 1463:             my $numitems = scalar(@items);
 1464:             if ($numitems) {
 1465:                 for (my $i=$numitems-1; $i>=0; $i--) {
 1466:                     if (ref($elements{$action}) eq 'HASH') {
 1467:                         if (ref($elements{$action}{$items[$i]}) eq 'HASH') {
 1468:                             foreach my $key (keys(%{$elements{$action}{$items[$i]}})) {
 1469:                                 push(@excluded,$key);
 1470:                             }
 1471:                         }
 1472:                     }
 1473:                     last if ($items[$i] eq $state);
 1474:                 }
 1475:             }
 1476:         }
 1477:     }
 1478:     if (grep(/^instcode_/,@excluded)) {
 1479:         push(@excluded,'instcode');
 1480:     }
 1481:     return @excluded;
 1482: }
 1483: 
 1484: sub print_enrollment_menu {
 1485:     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order,
 1486:         $invalidcrosslist) =@_;
 1487:     my ($sections,$autoenroll,$access_dates,$output,$hasauto);
 1488:     my $starttime = time;
 1489:     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
 1490: 
 1491:     my %accesstitles = (
 1492:                           'start' => 'Default start access',
 1493:                            'end'   => 'Default end access',
 1494:                        );
 1495:     my %enrolltitles = (
 1496:                            'start' => 'Start auto-enrollment',
 1497:                            'end'   => 'End auto-enrollment',
 1498:                        );
 1499:     if ($env{'form.crstype'} eq 'official') {
 1500:         if (&Apache::lonnet::auto_run('',$dom)) {
 1501:             $output = &show_invalid_crosslists($invalidcrosslist);
 1502:             my ($section_form,$crosslist_form);
 1503:             $section_form = &inst_section_selector($dom,$instcode);
 1504:             if ($section_form) {
 1505:                 $sections = &Apache::lonhtmlcommon::row_headline().
 1506:                             '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Sections').
 1507:                             '&nbsp;'.&mt('Sections for auto-enrollment').'</h3>'.
 1508:                             &Apache::lonhtmlcommon::row_closure(1).
 1509:                             $section_form;
 1510:             }
 1511:             my $crosslisttotal = $env{'form.crosslisttotal'};
 1512:             if (!$crosslisttotal) {
 1513:                 $crosslisttotal = 1;
 1514:             }
 1515:             if ($env{'form.addcrosslist'}) {
 1516:                 $crosslisttotal ++;
 1517:             }
 1518:             for (my $i=0; $i<$crosslisttotal; $i++) {
 1519:                 $crosslist_form .= &coursecode_form($dom,'crosslist',$codetitles, 
 1520:                                                   $cat_titles,$cat_order,$i);
 1521:             }
 1522:             if ($crosslist_form) { 
 1523:                 $crosslist_form .= 
 1524:                     &Apache::lonhtmlcommon::row_title(&mt('Add another')).
 1525:                     '<input name="crosslisttotal" type="hidden" value="'.$crosslisttotal.'" />'.
 1526:                     '<input name="addcrosslist" type="checkbox" value="'.$crosslisttotal.'"'.
 1527:                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
 1528:                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
 1529:                 $sections .=  &Apache::lonhtmlcommon::row_headline.
 1530:                               '<h3>'.&mt('Crosslisted courses for auto-enrollment').'</h3>'.
 1531:                               &Apache::lonhtmlcommon::row_closure(1).
 1532:                               $crosslist_form;
 1533:             }
 1534:             $hasauto = 1;
 1535:             $autoenroll = 
 1536:                 &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic('Course_Request_Autoadd').'&nbsp;'.&mt('Add registered students automatically')).
 1537:                 '<span class="LC_nobreak"><label>'.
 1538:                 '<input type="radio" name="autoadds" value="1">'.
 1539:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
 1540:                 '<input type="radio" name="autoadds" value="0" checked="checked">'.
 1541:                 &mt('No').'</label></span>'.
 1542:                 &Apache::lonhtmlcommon::row_closure(1).
 1543:                 &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic('Course_Request_Autodrop').'&nbsp;'.&mt('Drop unregistered students automatically')).
 1544:                 '<span class="LC_nobreak"><label>'.
 1545:                 '<input type="radio" name="autodrops" value="1">'.
 1546:                 &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
 1547:                 '<input type="radio" name="autodrops" value="0" checked="checked">'.
 1548:                 &mt('No').'</label></span>'. 
 1549:                 &Apache::lonhtmlcommon::row_closure(1).
 1550:                 &date_setting_table($starttime,$endtime,$formname,'enroll',
 1551:                                     $hasauto,%enrolltitles);
 1552:         }
 1553:     }
 1554:     my $access_dates = 
 1555:         &date_setting_table($starttime,$endtime,$formname,'access',$hasauto,
 1556:                             %accesstitles);
 1557:     $output .= &Apache::lonhtmlcommon::start_pick_box();
 1558:     if ($sections) {
 1559:         $output .=  $sections;
 1560:     }
 1561:     if ($autoenroll) {
 1562:         $output .= &Apache::lonhtmlcommon::row_headline('Auto-enroll').
 1563:                    '<h3>'.&mt('Auto-enrollment settings').'</h3>'.
 1564:                    &Apache::lonhtmlcommon::row_closure(1).
 1565:                    $autoenroll;
 1566:     }
 1567:     if ($access_dates) {
 1568:         my $header = &mt('Access dates for students');
 1569:         if ($env{'form.crstype'} eq 'community') {
 1570:             $header = &mt('Access dates for community members');
 1571:         }
 1572:         $output .= &Apache::lonhtmlcommon::row_headline('Access').
 1573:                    '<h3>'.$header.'</h3>'.
 1574:                    &Apache::lonhtmlcommon::row_closure(1).
 1575:                    $access_dates
 1576:     }
 1577:     return '<div>'.&Apache::lonhtmlcommon::start_pick_box().$output.
 1578:            &Apache::lonhtmlcommon::end_pick_box().'</div>';
 1579: }
 1580: 
 1581: sub show_invalid_crosslists {
 1582:     my ($invalidcrosslist) = @_;
 1583:     my $output;
 1584:     if (ref($invalidcrosslist) eq 'ARRAY') {
 1585:         if (@{$invalidcrosslist} > 0) {
 1586:             $output = '<div class="LC_warning">'.
 1587:                       &mt('The following crosslisted courses were invalid:').'<ul>';
 1588:             foreach my $item (@{$invalidcrosslist}) {
 1589:                 $output .= '<li>'.$item.'</li>';
 1590:             }
 1591:             $output .= '</ul></div><br />';
 1592:         }
 1593:     }
 1594:     return $output;
 1595: }
 1596: 
 1597: 
 1598: sub inst_section_selector {
 1599:     my ($dom,$instcode) = @_;
 1600:     my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
 1601:     my $sectotal = scalar(@sections);
 1602:     my $output;
 1603:     if ($sectotal) {
 1604:         $output .=  &Apache::lonhtmlcommon::row_title(&mt('Sections of [_1]',$instcode)).
 1605:                     &Apache::loncommon::start_data_table().
 1606:                     &Apache::loncommon::start_data_table_row().
 1607:                     '<th>'.&mt('Include?').'<input type="hidden" name="sectotal" '.
 1608:                     'value="'.$sectotal.'"  /></th>'.
 1609:                     '<th>'.&mt('Institutional Section').'</th>'.
 1610:                     '<th>'.&Apache::loncommon::help_open_topic('Course_Request_LCSection').
 1611:                           '&nbsp;'.&mt('LON-CAPA section').'</th>'.
 1612:                     &Apache::loncommon::end_data_table_row();
 1613:         for (my $i=0; $i<@sections; $i++) {
 1614:             my $colflag = $i%2;
 1615:             my $secon = ' checked="checked"';
 1616:             my $secoff = '';
 1617:             if ($env{'form.origcnum'}) {
 1618:                 $secoff = $secon;
 1619:                 $secon='';
 1620:             }
 1621:             $output .= &Apache::loncommon::start_data_table_row().
 1622:                        '<td><label><input type="radio" name="sec_'.$i.
 1623:                        '"'.$secon.' value="1" />'.&mt('Yes').'</label>'.
 1624:                        ('&nbsp;'x2).'<label><input type="radio" name="sec_'.$i.
 1625:                         '"'.$secoff.' value="0" />'.&mt('No').'</label></td>'.
 1626:                        '<td align="center">'.$sections[$i].
 1627:                        '<input type="hidden" name="secnum_'.$i.'" value="'.
 1628:                        $sections[$i].'" /></td>'.
 1629:                        '<td><input type="text" size="10" name="loncapasec_'.$i.
 1630:                        '" value="'.$sections[$i].'" /></td>'.
 1631:                        &Apache::loncommon::end_data_table_row();
 1632:         }
 1633:         $output .= &Apache::loncommon::end_data_table().
 1634:                    &Apache::lonhtmlcommon::row_closure();
 1635:     }
 1636:     return $output;
 1637: }
 1638: 
 1639: sub date_setting_table {
 1640:     my ($starttime,$endtime,$formname,$prefix,$hasauto,%datetitles) = @_;
 1641:     my ($perpetual,$table);
 1642:     my $startform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'start',
 1643:                                                         $starttime,'','','',1,'','','',1);
 1644:     my $endform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'end',
 1645:                                                       $endtime,'','','',1,'','','',1);
 1646:     my $closure = '';
 1647:     if ($prefix eq 'access') {
 1648:         $perpetual = ' <span class="LC_nobreak"><label>'.
 1649:                      '<input type="checkbox" name="no_end_date" />'.
 1650:                      &mt('No end date').'</label></span>';
 1651:         $closure = '1';
 1652:     }
 1653: 
 1654:     my %help_item = (
 1655:                       access => {
 1656:                                   start => 'Course_Request_Access_Start', 
 1657:                                   end   => 'Course_Request_Access_End',
 1658:                                 },
 1659:                       enroll => {
 1660:                                   start => 'Course_Request_Enroll_Start',
 1661:                                   end   => 'Course_Request_Enroll_End',
 1662:                                 },
 1663:                      );
 1664:     if ($hasauto) {
 1665:         $help_item{'access'}{'start'} = 'Course_Request_RegAccess_Start';
 1666:         $help_item{'access'}{'end'}   = 'Course_Request_RegAccess_End';
 1667:     }
 1668: 
 1669:     $table = &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'start'}).
 1670:              '&nbsp;'.$datetitles{'start'}).$startform.
 1671:              &Apache::lonhtmlcommon::row_closure(1).
 1672:              &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'end'}).
 1673:              '&nbsp;'.$datetitles{'end'}).$endform.$perpetual.
 1674:              &Apache::lonhtmlcommon::row_closure($closure);
 1675:     return $table;
 1676: }
 1677: 
 1678: sub print_personnel_menu {
 1679:     my ($dom,$formname,$crstype,$invalidcrosslist) = @_;
 1680:     my $output;
 1681:     if ($crstype eq 'official') {
 1682:         if (&Apache::lonnet::auto_run('',$dom)) {
 1683:             $output .= &show_invalid_crosslists($invalidcrosslist);
 1684:         }  
 1685:     }
 1686:     $output .= '<div>'.&Apache::lonhtmlcommon::start_pick_box();
 1687:     my $persontotal = $env{'form.persontotal'};
 1688:     if ((!defined($persontotal)) || (!$persontotal)) {
 1689:         $persontotal = 1;
 1690:     }
 1691:     if ($env{'form.addperson'}) {
 1692:         $persontotal ++;
 1693:     }
 1694:     my @items = ('uname','dom','lastname','firstname','emailaddr','hidedom');
 1695: 
 1696:     my $type = 'Course';
 1697:     if ($crstype eq 'community') {
 1698:         $type = 'Community';
 1699:     }
 1700:     my $roleoptions;
 1701:     my @roles = &Apache::lonuserutils::roles_by_context('course','',$type);
 1702:     foreach my $role (@roles) {
 1703:         my $plrole = &Apache::lonnet::plaintext($role,$type);
 1704:         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
 1705:     }
 1706:     my %customroles=&Apache::lonuserutils::my_custom_roles();
 1707:     if (keys(%customroles) > 0) {
 1708:         foreach my $cust (sort(keys(%customroles))) {
 1709:             my $custrole="cr/$env{'user.domain'}/$env{'user.name'}/$cust";
 1710:             $roleoptions .= '  <option value="'.$custrole.'">'.$cust.'</option>'."\n";
 1711:         }
 1712:     }
 1713: 
 1714:     my @currsecs = &current_lc_sections();
 1715: 
 1716:     my ($existtitle,$existops,$existmult,$newtitle,$seccolspan);
 1717:     if (@currsecs) {
 1718:         my $existsize = scalar(@currsecs);
 1719:         if ($existsize > 3) {
 1720:             $existsize = 3;
 1721:         }
 1722:         if ($existsize > 1) {
 1723:             $existmult = ' multiple="multiple" size="'.$existsize.'" ';
 1724:         }
 1725:         @currsecs = sort { $a <=> $b } (@currsecs);
 1726:         $existtitle = &mt('Official').':&nbsp;';
 1727:         $existops = '<option value="">'.&mt('None').'</option>';
 1728:         foreach my $sec (@currsecs) {
 1729:             $existops .= '<option value="'.$sec.'">'.$sec.'</option>'."\n";
 1730:         }
 1731:         $seccolspan = ' colspan="2"';
 1732:         $newtitle = &mt('Other').':&nbsp;';
 1733:     }
 1734: 
 1735:     if ($persontotal) {
 1736:         my %lt = &Apache::lonlocal::texthash(
 1737:             community => 'Requestor is automatically assigned Coordinator role.',
 1738:             official => 'Requestor is automatically assigned Course Coordinator role.',
 1739:         );
 1740:         $lt{'unofficial'} = $lt{'official'};
 1741:         $output .= &Apache::lonhtmlcommon::row_headline().
 1742:                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Personnel').'&nbsp;'.$lt{$crstype}.' '.&mt('Include other personnel?').'</h3>';
 1743:     }
 1744:     for (my $i=0; $i<$persontotal; $i++) { 
 1745:         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
 1746:         my $linkargstr = join("','",@linkargs);
 1747:         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value="" size="20" />';
 1748:         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
 1749:                        "'person_".$i."_hidedom','person_".$i."_uname'".');';
 1750:         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
 1751:                                                             1,$onchange).
 1752:                         '<input type="hidden" name="person_'.$i.'_hidedom" value="" />';
 1753:         my %form_elems;
 1754:         foreach my $item (@items) {
 1755:             next if (($item eq 'dom') || ($item eq 'uname') || ($item eq 'hidedom'));
 1756:             $form_elems{$item} = '<input type="text" name="person_'.$i.'_'.$item.'" '.
 1757:                                  'value="" readonly="readonly" />';
 1758:         }
 1759:         my $roleselector = '<select name="person_'.$i.'_role">'."\n".
 1760:                            $roleoptions.'</select>';
 1761:         my $sectionselector;
 1762:         if (@currsecs) {
 1763:             $sectionselector = $existtitle.'<select name="person_'.$i.'_sec"'.
 1764:                                $existmult.'>'."\n".$existops.'</select>'.('&nbsp;' x3);
 1765:         }
 1766:         $sectionselector .= $newtitle.
 1767:             '<input type="text" name="person_'.$i.'_newsec" size="15" value="" />'."\n";
 1768:         my $usersrchlinktxt = &mt('Search for user');
 1769:         my $usersrchlink =  &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
 1770:                             $usersrchlinktxt);
 1771:         my $userchklinktxt = &mt('Check username');
 1772:         my $userchklink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,
 1773:                             $userchklinktxt,'checkusername');
 1774:         $output .= 
 1775:             &Apache::lonhtmlcommon::row_title(&mt('Additional Personnel')).
 1776:             '<table><tr><td align="center" valign="middle"><b>'.$usersrchlink.'</b></td>'."\n".
 1777:             '<td align="left" valign="top" colspan="2"><span class="LC_nobreak">'.
 1778:             &mt('Username').':&nbsp;'.$uname_form.'&nbsp;'.$userchklink.'</span><br />'."\n".
 1779:             '<span class="LC_nobreak">'.&mt('Domain').':&nbsp;'.$udom_form.'</span></td>'.
 1780:             '</tr>'."\n".'<tr>'.
 1781:             '<td align="center" valign="top">'.&mt('First Name').'<br />'.$form_elems{'firstname'}.'</td>'."\n".
 1782:             '<td align="center" valign="top">'.&mt('Last Name').'<br />'.$form_elems{'lastname'}.'</td>'."\n".
 1783:             '<td align="center" valign="top">'.&mt('E-mail').'<br />'.$form_elems{'emailaddr'}.'</td></tr>'."\n".
 1784:             '<tr><td align="center" valign="top">'.&Apache::loncommon::help_open_topic('Course_Roles').'&nbsp;'.&mt('Role').'<br />'.$roleselector.'</td>'."\n".
 1785:             '<td'.$seccolspan.' align="center" valign="top">'.
 1786:             &Apache::loncommon::help_open_topic('Course_Request_Rolesection').'&nbsp;'.&mt('LON-CAPA Section(s)').'<br />'.$sectionselector.'</td>'."\n".
 1787:             '</tr></table>'.&Apache::lonhtmlcommon::row_closure();
 1788:     }
 1789:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Add another')).
 1790:                '<input name="persontotal" type="hidden" value="'.$persontotal.'" />'.
 1791:                '<input name="addperson" type="checkbox" value="'.$persontotal.'"'.
 1792:                ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
 1793:                "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure(1).
 1794:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
 1795:     if ($crstype eq 'community') {
 1796:         $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>';
 1797:     } else {
 1798:         $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>';
 1799:     }
 1800:     return $output;
 1801: }
 1802: 
 1803: sub current_lc_sections {
 1804:     my @currsecs;
 1805:     if ($env{'form.sectotal'}) {
 1806:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 1807:             if ($env{'form.sec_'.$i}) { 
 1808:                 if (defined($env{'form.loncapasec_'.$i})) {
 1809:                     my $lcsec = $env{'form.loncapasec_'.$i};
 1810:                     unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
 1811:                         push(@currsecs,$lcsec);
 1812:                     }
 1813:                 }
 1814:             }
 1815:         }
 1816:     }
 1817:     return @currsecs;
 1818: }
 1819: 
 1820: sub print_request_status {
 1821:     my ($dom) = @_;
 1822:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
 1823:                                            $env{'user.name'},'^status:'.$dom);
 1824:     my ($output,$formname,%queue_by_date);
 1825:     my ($types,$typenames) = &Apache::loncommon::course_types();
 1826:     foreach my $key (keys(%statusinfo)) {
 1827:         if (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending')) { 
 1828:             (undef,my($cdom,$cnum)) = split(':',$key);
 1829:             next if ($cdom ne $dom);   
 1830:             my $requestkey = $cdom.'_'.$cnum;
 1831:             if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 1832:                 my %history = &Apache::lonnet::restore($requestkey,'courserequests',
 1833:                                                        $env{'user.domain'},$env{'user.name'});
 1834:                 my $entry;
 1835:                 my $timestamp = $history{'reqtime'};
 1836:                 my $crstype = $history{'crstype'};
 1837:                 my $disposition = $history{'disposition'};
 1838:                 next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
 1839:                 next unless (($env{'form.crstype'} eq 'any') || 
 1840:                             ($env{'form.crstype'} eq $crstype));
 1841:                 next unless (($disposition eq 'approval') || 
 1842:                              ($disposition eq 'pending'));
 1843:                 if (ref($history{'details'}) eq 'HASH') {
 1844:                     $entry = $requestkey.':'.$crstype.':'.
 1845:                              &escape($history{'details'}{'cdescr'});
 1846:                     if ($crstype eq 'official') {
 1847:                         $entry .= ':'.&escape($history{'details'}{'instcode'}); 
 1848:                     }
 1849:                 }
 1850:                 if ($entry ne '') {
 1851:                     if (exists($queue_by_date{$timestamp})) {
 1852:                         if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
 1853:                             push(@{$queue_by_date{$timestamp}},$entry);
 1854:                         }
 1855:                     } else {
 1856:                         @{$queue_by_date{$timestamp}} = ($entry);
 1857:                     }
 1858:                 }
 1859:             }
 1860:         }
 1861:     }
 1862:     $formname = 'requestcrs';
 1863:     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
 1864:     $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 1865: 
 1866:               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
 1867:               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n".
 1868:               '<input type="hidden" name="showdom" value="" />'."\n".
 1869:               '<input type="hidden" name="cnum" value="" />'."\n";
 1870:     if (@sortedtimes > 0) {
 1871:         my $desctitle;
 1872:         if ($env{'form.crstype'} eq 'any') {
 1873:             $desctitle = &mt('Course/Community Description')
 1874:         } elsif ($env{'form.crstype'} eq 'community') {
 1875:             $desctitle = &mt('Community Description')
 1876:         } else {
 1877:             $desctitle = &mt('Course Description');
 1878:         }
 1879:         $output .= &Apache::loncommon::start_data_table().
 1880:                    &Apache::loncommon::start_data_table_header_row().
 1881:                    '<th>'.&mt('Action').'</th>'.
 1882:                    '<th>'.$desctitle.'</th>'.
 1883:                    '<th>'.&mt('Domain').'</th>';
 1884:         if ($env{'form.crstype'} eq 'any') {
 1885:             $output .= '<th>'.&mt('Type').'</th>';
 1886:         }
 1887:         if (($env{'form.crstype'} eq 'any') || ($env{'form.crstype'} eq 'official')) {
 1888:             $output .= '<th>'.&mt('Institutional Code').'</th>';
 1889:         }
 1890:         $output .= '<th>'.&mt('Date requested').'</th>'.
 1891:                    &Apache::loncommon::end_data_table_header_row();
 1892:         my $count = 0;
 1893:         foreach my $item (@sortedtimes) {
 1894:             my $showtime = &Apache::lonlocal::locallocaltime($item);
 1895:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
 1896:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
 1897:                     my ($key,$type,$desc,$instcode) = split(':',$request);
 1898:                     my ($cdom,$cnum) = split('_',$key);
 1899:                     $output .= &Apache::loncommon::start_data_table_row().
 1900:      '<td><input type="button" value="'.&mt('Select').'" onclick="javascript:chooseRequest('."'$cdom','$cnum'".')" /></td>'.
 1901:      '<td>'.&unescape($desc).'</td>'.
 1902:      '<td>'.$cdom.'</td>';
 1903:                     if ($env{'form.crstype'} eq 'any') {
 1904:                         my $typename;
 1905:                         if (ref($typenames) eq 'HASH') {
 1906:                             $typename = &mt($typenames->{$type});
 1907:                         }
 1908:                         if ($typename eq '') {
 1909:                             $typename = &mt('Unknown type');
 1910:                         }
 1911:                         $output .= '<td>'.$typename.'</td>';
 1912:                     }
 1913:                     if (($env{'form.crstype'} eq 'any') || 
 1914:                         ($env{'form.crstype'} eq 'official')) {
 1915:                         my $showinstcode;
 1916:                         if ($type eq 'official') {
 1917:                             $showinstcode = &unescape($instcode);
 1918:                         } else {
 1919:                             $showinstcode = &mt('Not applicable');
 1920:                         }
 1921:                         $output .= '<td>'.$showinstcode.'</td>';
 1922:                     }
 1923:                     $output .= '<td>'.$showtime.'</td>'.
 1924:                                &Apache::loncommon::end_data_table_row();
 1925:                 }
 1926:             }
 1927:         }
 1928:         $output .= &Apache::loncommon::end_data_table();
 1929:     } else {
 1930:         if ($env{'form.crstype'} eq 'any') {
 1931: $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>';
 1932:         } elsif ($env{'form.crstype'} eq 'community') {
 1933:             $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>';
 1934:         } else {
 1935:             $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>';
 1936:         }
 1937:     }
 1938:     $output .= '
 1939: <br /><input type="button" name="prev" value="'.&mt('Back').'" onclick="javascript:backPage(document.'.$formname.",'crstype'".')" />';
 1940:     return $output;
 1941: }
 1942: 
 1943: sub print_cancel_request {
 1944:     my ($dom,$cnum) = @_;
 1945:     my $requestkey = $dom.'_'.$cnum;
 1946:     my ($result,$output);
 1947:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 1948:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',
 1949:                                                $env{'user.domain'},$env{'user.name'});
 1950:         my $timestamp = $history{'reqtime'};
 1951:         my $crstype = $history{'crstype'};
 1952:         my $status = $history{'status'};
 1953:         if (($status eq 'cancelled') || ($status eq 'created')) { 
 1954:             if ($status eq 'cancelled') {
 1955:                 $output = &mt('This request has already been cancelled.');
 1956:             } elsif ($status eq 'created') {
 1957:                 $output = &mt('This request has already been processed, and a course created.');
 1958:             }
 1959:             $output = &mt('No further action will be taken');
 1960:         } elsif (ref($history{'details'}) eq 'HASH') {
 1961:             my ($types,$typename) = &Apache::loncommon::course_types();
 1962:             my $showtype = $crstype;
 1963:             if (defined($typename->{$crstype})) {
 1964:                 $showtype = $typename->{$crstype}; 
 1965:             }
 1966:             $output = '<p>'.&Apache::loncommon::start_data_table().
 1967:                       &Apache::loncommon::start_data_table_header_row().
 1968:                       '<th>'.&mt('Description').'</th><th>'.&mt('Requested').'</th>'.
 1969:                       '<th>'.&mt('Type').'</th>'.
 1970:                       &Apache::loncommon::end_data_table_header_row().
 1971:                       &Apache::loncommon::start_data_table_row().
 1972:                       '<td>'.$history{details}{'cdescr'}.'</td><td>'.
 1973:                       &Apache::lonlocal::locallocaltime($timestamp).'</td>'.
 1974:                       '<td>'.$showtype.'</td>'.
 1975:                       &Apache::loncommon::end_data_table_row().
 1976:                       &Apache::loncommon::end_data_table().
 1977:                       '<br /><div class="LC_warning">';
 1978:             if ($crstype eq 'community') {
 1979:                 $output .= &mt('Cancelling the request will remove it from the queue of pending community requests').'</div>';
 1980:             } else {
 1981:                 $output .= &mt('Cancelling the request will remove it from the queue of pending course requests').'</div>';
 1982:             }
 1983:             $result = 'ok';
 1984:         } else {
 1985:             $output = '<div class="LC_error">'.&mt('No record exists for the course ID').'</div>';
 1986:         }
 1987:     } else {
 1988:         $output = '<div class="LC_error">'.&mt('Invalid course ID').'</div>';
 1989:     }
 1990:     return ($result,$output);
 1991: }
 1992: 
 1993: sub viewrequest_javascript {
 1994:     my ($formname,$next) = @_;
 1995:     return <<"ENDJS";
 1996: 
 1997: function chooseRequest(cdom,cnum) {
 1998:     document.$formname.showdom.value = cdom;
 1999:     document.$formname.cnum.value = cnum;
 2000:     nextPage(document.$formname,'$next');
 2001: }
 2002: 
 2003: ENDJS
 2004: }
 2005: 
 2006: sub viewdetails_javascript {
 2007:     my ($formname) = @_;
 2008:     return << "ENDJS";
 2009: 
 2010: function nextPage(formname,nextstate) {
 2011:     if (nextstate == "modify") { 
 2012:         formname.state.value = "personnel";
 2013:         formname.action.value = "new";
 2014:     } else {
 2015:         formname.state.value = nextstate;
 2016:     }
 2017:     formname.submit();
 2018: }
 2019: 
 2020: function backPage(formname,prevstate) {
 2021:     formname.state.value = prevstate;
 2022:     formname.submit();
 2023: }
 2024: 
 2025: ENDJS
 2026: }
 2027: 
 2028: sub viewcancel_javascript {
 2029:     my $alert = &mt('Are you sure you want to cancel this request?\\n'.
 2030:                     'Your request will be removed.');
 2031:     return << "ENDJS";
 2032: function nextPage(formname,nextstate) {
 2033:     if (confirm('$alert')) {
 2034:         formname.state.value = nextstate;
 2035:         formname.submit();
 2036:     }
 2037:     return;
 2038: }
 2039: 
 2040: ENDJS
 2041: }
 2042: 
 2043: sub print_request_logs {
 2044:     my ($jscript,$loaditems,$crumb) = @_;
 2045:     return;
 2046: }
 2047: 
 2048: sub print_review {
 2049:     my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
 2050:         $disallowed,$disallowmsg) = @_;
 2051:     my ($types,$typename) = &Apache::loncommon::course_types();
 2052:     my ($owner,$ownername,$owneremail);
 2053:     if ($uname eq '' || $udom eq '') {
 2054:         $uname = $env{'user.name'};
 2055:         $udom = $env{'user.domain'};
 2056:     }
 2057:     $owner = $uname.':'.$udom;
 2058:     $ownername = &Apache::loncommon::plainname($uname,$udom,'first');
 2059:     my %emails = &Apache::loncommon::getemails($uname,$udom);
 2060:     foreach my $email ('permanentemail','critnotification','notification') {
 2061:         $owneremail = $emails{$email};
 2062:         last if ($owneremail ne '');
 2063:     }
 2064:     my ($inst_headers,$inst_values,$crstypename,$enroll_headers,$enroll_values,
 2065:         $section_headers,$section_values,$personnel_headers,$personnel_values);
 2066: 
 2067:     $crstypename = $env{'form.crstype'};
 2068:     if (ref($typename) eq 'HASH') {
 2069:         unless ($typename->{$env{'form.crstype'}} eq '') {
 2070:             $crstypename = &mt($typename->{$env{'form.crstype'}});
 2071:         }
 2072:     }
 2073:     my $category = 'Course';
 2074:     if ($env{'form.crstype'} eq 'community') {
 2075:         $category = 'Community';
 2076:     }
 2077: 
 2078:     $inst_headers = '<th>'.&mt('Description').'</th><th>'.&mt('Type').'</th>';
 2079:     $inst_values = '<td>'.$env{'form.cdescr'}.'</td><td>'.$crstypename.'</td>';
 2080: 
 2081:     my $enrollrow_title = &mt('Default Access Dates').'<br />'.
 2082:                           '('.&Apache::lonnet::plaintext('st',$category).')';
 2083:     if ($env{'form.crstype'} eq 'official') {
 2084:         if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH')) {
 2085:             foreach my $title (@{$codetitles}) {
 2086:                 if ($env{'form.instcode_'.$title} ne '') {
 2087:                     $inst_headers .= '<th>'.$title.'</th>';
 2088:                     my $longitem = $env{'form.instcode_'.$title};
 2089:                     if (ref($cat_titles->{$title}) eq 'HASH') {
 2090:                         if ($cat_titles->{$title}{$env{'form.instcode_'.$title}} ne '') {
 2091:                             $longitem = $cat_titles->{$title}{$env{'form.instcode_'.$title}};
 2092:                         }
 2093:                     }
 2094:                     $inst_values .= '<td>'.$longitem.'</td>';
 2095:                 }
 2096:             }
 2097:         }
 2098:         if (&Apache::lonnet::auto_run('',$dom)) {
 2099:             $enrollrow_title = &mt('Enrollment');
 2100:             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
 2101:                               '<th>'.&mt('Automatic Drops').'</th>'.
 2102:                               '<th>'.&mt('Enrollment Starts').'</th>'.
 2103:                               '<th>'.&mt('Enrollment Ends').'</th>';
 2104:             $section_headers = '<th>'.&mt('Sections').'</th>'.
 2105:                                '<th>'.&mt('Crosslistings').'</th>';
 2106: 
 2107:             my ($enrollstart,$enrollend) = &dates_from_form('enrollstart','enrollend');
 2108:             my @autoroster = (&mt('No'),&mt('Yes'));
 2109:             $enroll_values = '<td>'.$autoroster[$env{'form.autoadds'}].'</td>'.
 2110:                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
 2111:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollstart).'</td>'.
 2112:                              '<td>'.&Apache::lonlocal::locallocaltime($enrollend).'</td>';
 2113:             $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
 2114:                               &mt('Institutional section').'</th>'.
 2115:                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
 2116:             my $secinfo;
 2117:             if ($env{'form.sectotal'} > 0) {
 2118:                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 2119:                     if ($env{'form.sec_'.$i}) {
 2120:                         $secinfo .= '<tr><td>'.$env{'form.secnum_'.$i}.'</td><td>';
 2121:                         if ($env{'form.loncapasec_'.$i} ne '') {
 2122:                             $secinfo .= $env{'form.loncapasec_'.$i};
 2123:                         } else {
 2124:                             $secinfo .= &mt('None');
 2125:                         }
 2126:                         $secinfo .= '</td></tr>';
 2127:                     }
 2128:                 }
 2129:             }
 2130:             if ($secinfo eq '') {
 2131:                 $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
 2132:             }
 2133:             $section_values .= $secinfo.'</table></td><td>'.
 2134:                                '<table class="LC_innerpickbox"><tr><th>'.
 2135:                                &mt('Institutional course/section').'</th>'.
 2136:                                '<th>'.&mt('LON-CAPA section').'</th></tr>';
 2137:             my $xlistinfo;
 2138:             my $crosslisttotal = $env{'form.crosslisttotal'};
 2139:             if (!$crosslisttotal) {
 2140:                 $crosslisttotal = 1;
 2141:             }
 2142:             for (my $i=0; $i<$crosslisttotal; $i++) {
 2143:                 if ($env{'form.crosslist_'.$i}) {
 2144:                     $xlistinfo .= '<tr><td>';
 2145:                     if (ref($code_order) eq 'ARRAY') {
 2146:                         if (@{$code_order} > 0) {
 2147:                             foreach my $item (@{$code_order}) {
 2148:                                 $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
 2149:                             }
 2150:                         }
 2151:                     }
 2152:                     $xlistinfo .= $env{'form.crosslist_'.$i.'_instsec'}.'</td><td>';
 2153:                     if ($env{'form.crosslist_'.$i.'_lcsec'}) {
 2154:                         $xlistinfo .= $env{'form.crosslist_'.$i.'_lcsec'};
 2155:                     } else {
 2156:                         $xlistinfo .= &mt('None');
 2157:                     }
 2158:                     $xlistinfo .= '</td></tr>';
 2159:                 }
 2160:             }
 2161:             if ($xlistinfo eq '') {
 2162:                 $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
 2163:             }
 2164:             $section_values .= $xlistinfo;
 2165:         }
 2166:         $section_values .= '</table></td>';
 2167:     }
 2168: 
 2169:     my %ctxt = &clone_text();
 2170:     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
 2171:     if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
 2172:         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
 2173:         my $canclone = &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
 2174:                            $env{'user.domain'},$env{'form.clonecrs'},$env{'form.clonedom'},
 2175:                            $env{'form.crstype'});
 2176:         if ($canclone) {
 2177:             my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
 2178:                               $env{'form.clonecrs'},('description','internal.coursecode'));
 2179:             if (keys(%courseenv) > 0) {
 2180:                 $inst_headers .= '<th>'.$ctxt{'dsh'}.'</th>';
 2181:                 $inst_values .= '<td>'.$courseenv{'description'}.'&nbsp;';
 2182:                 my $cloneinst = $courseenv{'internal.coursecode'};
 2183:                 if ($cloneinst ne '') {
 2184:                     $inst_values .= $cloneinst.' '.&mt('in').' '.$env{'form.clonedom'};
 2185:                 } else {
 2186:                     $inst_values .= &mt('from').' '.$env{'form.clonedom'};
 2187:                 }
 2188:                 $inst_values .= '</td><td>';
 2189:                 if ($env{'form.datemode'} eq 'preserve') {
 2190:                     $inst_values .= $ctxt{'prd'}; 
 2191:                 } elsif ($env{'form.datemode'} eq 'shift') {
 2192:                     $inst_values .= &mt('Shift dates by [_1] days',$env{'form.dateshift'});
 2193:                 } else {
 2194:                     $inst_values .= $ctxt{'ncd'};
 2195:                 }
 2196:                 $inst_values .= '</td>';
 2197:              } else {
 2198:                  $inst_values .= '<td>'.&mt('Unknown').'</td>';
 2199:              }
 2200:          } else {
 2201:              $inst_values .= '<td>'.&mt('Not permitted'),'</td>';
 2202:          }
 2203:     } else {
 2204:         $inst_values .= '<td>'.&mt('None').'</td>';
 2205:     }
 2206:     $enroll_headers .= '<th>'.&mt('Access Starts').'</th>'.
 2207:                        '<th>'.&mt('Access Ends').'</th>';
 2208:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
 2209:     $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessstart).'</td>';
 2210:     if ($accessend == 0) {
 2211:         $enroll_values .= '<td>'.&mt('No end date').'</td>';
 2212:     } else {
 2213:         $enroll_values .= '<td>'.&Apache::lonlocal::locallocaltime($accessend).'</td>';
 2214:     }
 2215: 
 2216:     my $container = 'Course';
 2217:     my $ccrole = 'cc';
 2218:     if ($env{'form.crstype'} eq 'community') {
 2219:         $container = 'Community';
 2220:         $ccrole = 'co';
 2221:     }
 2222: 
 2223:     $personnel_headers = '<th>'.&mt('Name').'</th><th>'.&mt('Username:Domain').
 2224:                          '</th><th>'.&mt('Role').'</th><th>'.&mt('LON-CAPA Sections').
 2225:                          '</th>';
 2226: 
 2227:     $personnel_values .= '<tr><td>'.$ownername.'</td><td>'.$owner.'</td>'.
 2228:                          '<td>'.&Apache::lonnet::plaintext($ccrole,$container).'</td>'.
 2229:                          '<td>'.&mt('None').'</td></tr>';
 2230:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 2231:         if ($env{'form.person_'.$i.'_uname'} ne '') {
 2232:             if (ref($disallowed) eq 'ARRAY') {
 2233:                 next if (grep(/^$i$/,@{$disallowed}));
 2234:             } 
 2235:             my @officialsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
 2236:             my @allsecs;
 2237:             foreach my $sec (@officialsecs) {
 2238:                 next unless ($sec =~ /\w/);
 2239:                 next if ($sec =~ /\W/);
 2240:                 next if ($sec eq 'none');
 2241:                 push(@allsecs,$sec);
 2242:             }
 2243:             my $newsec = $env{'form.person_'.$i.'_newsec'};
 2244:             $newsec =~ s/^\s+//;
 2245:             $newsec =~s/\s+$//;
 2246:             my @newsecs = split(/\s*[\s,;:]\s*/,$newsec);
 2247:             foreach my $sec (@newsecs) {
 2248:                 next unless ($sec =~ /\w/); 
 2249:                 next if ($sec =~ /\W/);
 2250:                 next if ($sec eq 'none');
 2251:                 if ($sec ne '') {
 2252:                     unless (grep(/^\Q$sec\E$/,@allsecs)) {
 2253:                         push(@allsecs,$sec); 
 2254:                     }
 2255:                 }
 2256:             }
 2257:             my $showsec;
 2258:             if (@allsecs) {
 2259:                 $showsec = join(', ',@allsecs);
 2260:             }
 2261:             if ($showsec eq '') {
 2262:                 $showsec = &mt('None');
 2263:             }
 2264:             if ($env{'form.person_'.$i.'_role'} eq $ccrole) {
 2265:                 $showsec = &mt('None');
 2266:             }
 2267:             my $role = $env{'form.person_'.$i.'_role'}; 
 2268:             $personnel_values .= 
 2269:                 '<tr><td>'.$env{'form.person_'.$i.'_firstname'}.' '.
 2270:                 $env{'form.person_'.$i.'_lastname'}.'</td>'.
 2271:                 '<td>'.$env{'form.person_'.$i.'_uname'}.':'.
 2272:                 $env{'form.person_'.$i.'_dom'}.'</td>'.
 2273:                 '<td>'.&Apache::lonnet::plaintext($role,$container).'</td>'.
 2274:                 '<td>'.$showsec.'</td></tr>';
 2275:         }
 2276:     }
 2277:     my $output;
 2278:     if (ref($disallowed) eq 'ARRAY') {
 2279:         if (@{$disallowed} > 0) {
 2280:             if (ref($disallowmsg) eq 'HASH') {
 2281:                 $output = '<p class="LC_warning">'.
 2282:                           &mt('Not all requested personnel could be included.').'<ul>'; 
 2283:                 foreach my $item (@{$disallowed}) {
 2284:                     $output .= '<li>'.$disallowmsg->{$item}.'</li>';
 2285:                 }
 2286:                 $output .= '</ul></p>';
 2287:             }
 2288:         }
 2289:     }
 2290:     $output .=    '<div>'.&Apache::lonhtmlcommon::start_pick_box().
 2291:                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
 2292:                   '<table class="LC_innerpickbox"><tr>'.
 2293:                   '<th>'.&mt('Name').'</th>'.
 2294:                   '<th>'.&mt('Username:Domain').'</th>'.
 2295:                   '<th>'.&mt('E-mail address').'</th>'.
 2296:                   '</tr><tr>'."\n".
 2297:                   '<td>'.$ownername.'</td><td>'.$owner.'</td>'.
 2298:                   '<td>'.$owneremail.'</td>'.
 2299:                   '</tr></table>'."\n".
 2300:                   &Apache::lonhtmlcommon::row_closure().
 2301:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
 2302:                   '<table class="LC_innerpickbox"><tr>'.$inst_headers.'</tr>'."\n".
 2303:                   '<tr>'.$inst_values.'</tr></table>'."\n".
 2304:                   &Apache::lonhtmlcommon::row_closure().
 2305:                   &Apache::lonhtmlcommon::row_title($enrollrow_title).
 2306:                   '<table class="LC_innerpickbox"><tr>'.$enroll_headers.'</tr>'."\n".
 2307:                   '<tr>'.$enroll_values.'</tr></table>'."\n".
 2308:                   &Apache::lonhtmlcommon::row_closure();
 2309:     if ($section_headers ne '') {
 2310:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Sections')).
 2311:                    '<table class="LC_innerpickbox"><tr>'.$section_headers.'</tr>'."\n".
 2312:                    '<tr>'.$section_values.'</tr></table>'."\n".
 2313:                    &Apache::lonhtmlcommon::row_closure();
 2314:     }
 2315:     $output .= &Apache::lonhtmlcommon::row_title(&mt('Personnel')).
 2316:                '<table class="LC_innerpickbox"><tr>'.$personnel_headers.'</tr>'."\n".
 2317:                $personnel_values.'</table>'."\n".
 2318:                &Apache::lonhtmlcommon::row_closure(1).
 2319:                &Apache::lonhtmlcommon::end_pick_box().'</div>';
 2320:     return $output;
 2321: }
 2322: 
 2323: sub dates_from_form {
 2324:     my ($startname,$endname) = @_;
 2325:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
 2326:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
 2327:     if ($endname eq 'accessend') {
 2328:         if (exists($env{'form.no_end_date'}) ) {
 2329:             $enddate = 0;
 2330:         }
 2331:     }
 2332:     return ($startdate,$enddate);
 2333: }
 2334: 
 2335: sub courseinfo_form {
 2336:     my ($dom,$formname,$crstype,$next,$description) = @_;
 2337:     my %lt = &Apache::lonlocal::texthash(
 2338:                 official => 'You must provide a (brief) course description.',
 2339:                 community => 'You must provide a (brief) community description.'
 2340:              );
 2341:     $lt{'unofficial'} = $lt{'official'};
 2342:     my $js_validate = <<"ENDJS";
 2343: <script type="text/javascript">
 2344: // <![CDATA['
 2345: 
 2346: function validateForm() {
 2347:     if ((document.$formname.cdescr.value == "")  || (document.$formname.cdescr.value == "undefined")) {
 2348:         alert('$lt{$crstype}');
 2349:         return;
 2350:     }
 2351:     nextPage(document.$formname,'$next');
 2352: }
 2353: // ]]
 2354: </script>
 2355: 
 2356: ENDJS
 2357:     my $title = &mt('Brief Course Description');
 2358:     my $clonetitle = &mt('Clone content and settings from an existing course?');
 2359:     if ($crstype eq 'community') {
 2360:         $title = &mt('Brief Community Description');
 2361:         $clonetitle = &mt('Clone content and settings from an existing community?');
 2362:     }
 2363:     my $output .= $js_validate."\n".'<div>'.&Apache::lonhtmlcommon::start_pick_box().
 2364:                   &Apache::lonhtmlcommon::row_headline().
 2365:                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Description').'&nbsp;'.$title.'</h3>'.
 2366:                   &Apache::lonhtmlcommon::row_closure(1).
 2367:                   &Apache::lonhtmlcommon::row_title(&mt('Description')).
 2368:                  '<input type="text" size="60" name="cdescr" value="'.$description.'" />';
 2369:     my ($home_server_pick,$numlib) =
 2370:         &Apache::loncommon::home_server_form_item($dom,'chome',
 2371:                                                   'default','hide');
 2372:     if ($numlib > 1) {
 2373:         $output .= &Apache::lonhtmlcommon::row_closure().
 2374:                    &Apache::lonhtmlcommon::row_title(&mt('Home Server for Course'));
 2375:     }
 2376:     $output .= $home_server_pick.
 2377:                &Apache::lonhtmlcommon::row_closure().
 2378:                &Apache::lonhtmlcommon::row_headline().
 2379:                '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Clone').'&nbsp;'.$clonetitle.'</h3>'. 
 2380:                &Apache::lonhtmlcommon::row_closure(1).
 2381:                &clone_form($dom,$formname,$crstype).
 2382:                &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
 2383:     return $output;
 2384: }
 2385: 
 2386: sub clone_form {
 2387:     my ($dom,$formname,$crstype) = @_;
 2388:     my $type = 'Course';
 2389:     if ($crstype eq 'community') {
 2390:         $type = 'Community';
 2391:     }
 2392:     my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedom').'&nbsp;'.
 2393:                     &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type);
 2394:     my %lt = &clone_text();
 2395:     my $output .= 
 2396:         &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
 2397:         '<input type="text" size="25" name="clonecrs" value=""  />'.
 2398:         '</label>'.&Apache::lonhtmlcommon::row_closure(1).
 2399:         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'<label>'.
 2400:         $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure(1).
 2401:         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
 2402:         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
 2403:         '</label><br /><label>'.
 2404:         '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
 2405:         '</label><br /><label>'.
 2406:         '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
 2407:         $lt{'shd'}.'</label>'.
 2408:         '<input type="text" size="5" name="dateshift" value="365" />'.
 2409:         &Apache::lonhtmlcommon::row_closure(1);
 2410:     return $output;
 2411: }
 2412: 
 2413: sub clone_text {
 2414:     return &Apache::lonlocal::texthash(
 2415:                'cid'  => 'Course ID',
 2416:                'dmn'  => 'Domain',
 2417:                'dsh'  => 'Date Shift',
 2418:                'ncd'  => 'Do not clone date parameters',
 2419:                'prd'  => 'Clone date parameters as-is',
 2420:                'shd'  => 'Shift date parameters by number of days',
 2421:         );
 2422: }
 2423: 
 2424: sub coursecode_form {
 2425:     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
 2426:     my $output;
 2427:     my %rowtitle = (
 2428:                     instcode  => 'Course Category',
 2429:                     crosslist => 'Cross Listed Course',
 2430:                    );
 2431:     my %helpitem = ( 
 2432:                      instcode => 'Course_Request_Category',
 2433:                      crosslist => 'Course_Request_Crosslist',
 2434:                    );
 2435:     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 2436:         (ref($cat_order))) {
 2437:         my ($sel,$instsec,$lcsec);
 2438:         $sel = $context;
 2439:         if ($context eq 'crosslist') {
 2440:             $sel .= '_'.$num;
 2441:             $instsec = &mt('Institutional section').'<br />'.
 2442:                        '<input type="text" size="10" name="'.$sel.'_instsec" />';
 2443:             $lcsec = &mt('LON-CAPA section').'<br />'.
 2444:                      '<input type="text" size="10" name="'.$sel.'_lcsec" />';
 2445:         }
 2446:         if (@{$codetitles} > 0) {
 2447:             my $lastitem = pop(@{$codetitles});
 2448:             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
 2449:             if (@{$codetitles} > 0) {
 2450:                 $output = &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($helpitem{$context}).'&nbsp;'.$rowtitle{$context}).
 2451:                           '<table><tr>';
 2452:                 if ($context eq 'crosslist') {
 2453:                     $output .= '<td>'.&mt('Include?').'<br />'.
 2454:                                '<input type="checkbox" name="'.$sel.'" value="1" /></td>';
 2455:                 }
 2456:                 foreach my $title (@{$codetitles}) {
 2457:                     if (ref($cat_order->{$title}) eq 'ARRAY') {
 2458:                         if (@{$cat_order->{$title}} > 0) {
 2459:                             $output .= '<td align="center">'.$title.'<br />'."\n".
 2460:                                        '<select name="'.$sel.'_'.$title.'">'."\n".
 2461:                                        ' <option value="" selected="selected">'.
 2462:                                        &mt('Select').'</option>'."\n";
 2463:                             foreach my $item (@{$cat_order->{$title}}) {
 2464:                                 my $longitem = $item;
 2465:                                 if (ref($cat_titles->{$title}) eq 'HASH') {
 2466:                                     if ($cat_titles->{$title}{$item} ne '') {
 2467:                                         $longitem = $cat_titles->{$title}{$item};
 2468:                                     }
 2469:                                 }
 2470:                                 $output .= '<option value="'.$item.'">'.$longitem.
 2471:                                            '</option>'."\n";
 2472:                             }
 2473:                         }
 2474:                         $output .= '</select></td>'."\n";
 2475:                     }
 2476:                 }
 2477:                 if ($context eq 'crosslist') {
 2478:                     $output .= '<td align="center">'.$lastitem.'<br />'."\n".
 2479:                                $lastinput.'</td><td align="center">'.$instsec.'</td>'.
 2480:                                '<td align="center">'.$lcsec.'</td></tr></table>';
 2481:                 } else {
 2482:                     $output .= '</tr></table>'.
 2483:                                &Apache::lonhtmlcommon::row_closure().
 2484:                                &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
 2485:                                $lastinput;
 2486:                 }
 2487:             } else {
 2488:                 if ($context eq 'crosslist') {
 2489:                     $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
 2490:                                '<table><tr>'.
 2491:                                '<td align="center">'.$lastitem.'<br />'.$lastinput.'</td>'.
 2492:                                '<td align="center">'.$instsec.'</td><td>'.$lcsec.'</td>'.
 2493:                                '</tr></table>';
 2494:                 } else { 
 2495:                     $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
 2496:                                $lastinput;
 2497:                 }
 2498:             }
 2499:             $output .=  &Apache::lonhtmlcommon::row_closure(1);
 2500:             push(@$codetitles,$lastitem);    
 2501:         } elsif ($context eq 'crosslist') {
 2502:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
 2503:                        '<table><tr><td align="center">'.
 2504:                        '<span class="LC_nobreak">'.&mt('Include?').
 2505:                        '<input type="checkbox" name="'.$sel.'" value="1" /></span>'.
 2506:                        '</td><td align="center">'.&mt('Institutional ID').'<br />'.
 2507:                        '<input type="text" size="10" name="'.$sel.'_instsec" />'.
 2508:                        '</td><td align="center">'.$lcsec.'</td></tr></table>'.
 2509:                        &Apache::lonhtmlcommon::row_closure(1);
 2510:         }
 2511:     }
 2512:     return $output;
 2513: }
 2514: 
 2515: sub get_course_dom {
 2516:     my $codedom = &Apache::lonnet::default_login_domain();
 2517:     if ($env{'form.showdom'} ne '') {
 2518:         if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
 2519:             return $env{'form.showdom'};
 2520:         }
 2521:     }
 2522:     if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
 2523:         my ($types,$typename) = &Apache::loncommon::course_types();
 2524:         if (ref($types) eq 'ARRAY') {
 2525:             foreach my $type (@{$types}) {
 2526:                 if (&Apache::lonnet::usertools_access($env{'user.name'},
 2527:                                                       $env{'user.domain'},$type,
 2528:                                                       undef,'requestcourses')) {
 2529:                     return $env{'user.domain'};
 2530:                 }
 2531:             }
 2532:             my @possible_doms;
 2533:             foreach my $type (@{$types}) {
 2534:                 my $dom_str = $env{'environment.reqcrsotherdom.'.$type};
 2535:                 if ($dom_str ne '') {
 2536:                     my @domains = split(',',$dom_str);
 2537:                     foreach my $entry (@domains) {
 2538:                         my ($extdom,$extopt) = split(':',$entry);
 2539:                         if ($extdom eq $env{'request.role.domain'}) {
 2540:                             return $extdom;
 2541:                         } 
 2542:                         unless(grep(/^\Q$extdom\E$/,@possible_doms)) {
 2543:                             push(@possible_doms,$extdom);
 2544:                         }
 2545:                     }
 2546:                 }
 2547:             }
 2548:             if (@possible_doms) {
 2549:                 @possible_doms = sort(@possible_doms);
 2550:                 return $possible_doms[0];
 2551:             }
 2552:         }
 2553:         $codedom = $env{'user.domain'};
 2554:         if ($env{'request.role.domain'} ne '') {
 2555:             $codedom = $env{'request.role.domain'};
 2556:         }
 2557:     }
 2558:     return $codedom;
 2559: }
 2560: 
 2561: sub display_navbuttons {
 2562:     my ($r,$dom,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
 2563:     $r->print('<div class="LC_navbuttons">');
 2564:     if ($prev) {
 2565:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
 2566:                   'onclick="javascript:backPage('."document.$formname,'$prev'".')"/>'.
 2567:                   ('&nbsp;'x3));
 2568:     } elsif ($prevtext) {
 2569:         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
 2570:                   'onclick="javascript:history.back()"/>'.('&nbsp;'x3));
 2571:     }
 2572:     if ($state eq 'details') {
 2573:         $r->print(' <input type="button" name="other" value="'.$othertext.'" '.
 2574:                   'onclick="javascript:nextPage('."document.$formname,'$other'".
 2575:                   ')" />');
 2576:     }
 2577:     my $gotnext;
 2578:     if ($state eq 'courseinfo') {
 2579:         $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 2580:                   'onclick="javascript:validateForm();" />');
 2581:         $gotnext = 1;
 2582:     } elsif ($state eq 'enrollment') {
 2583:         if (($env{'form.crstype'} eq 'official') && 
 2584:             (&Apache::lonnet::auto_run('',$dom))) {
 2585:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 2586:                       'onclick="javascript:validateEnrollSections('."document.$formname,'$next'".');" />');
 2587:                 $gotnext = 1;
 2588:         }
 2589:     } elsif ($state eq 'personnel') {
 2590:         if ($env{'form.persontotal'} > 0) { 
 2591:             $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
 2592:                       'onclick="javascript:validatePersonnelSections('."document.$formname,'$next'".');" />');
 2593:             $gotnext = 1;
 2594:         }
 2595:     }
 2596:     unless ($gotnext) {
 2597:         if ($next) {
 2598:             $r->print('
 2599:                       <input type="button" name="next" value="'.$nexttext.'" '.
 2600:       'onclick="javascript:nextPage('."document.$formname,'$next'".')" />');
 2601:         }
 2602:     }
 2603:     $r->print('</div>');
 2604: }
 2605: 
 2606: sub print_request_outcome {
 2607:     my ($dom,$codetitles,$code_order) = @_;
 2608:     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
 2609:         %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,);
 2610:     my $sectotal = $env{'form.sectotal'};
 2611:     my $crosslisttotal = 0;
 2612:     $cnum = $env{'form.cnum'};
 2613:     unless ($cnum =~ /^$match_courseid$/) {
 2614:         $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
 2615:         return $output;
 2616:     }
 2617: 
 2618:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
 2619:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2620:         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
 2621:             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
 2622:         }
 2623:     }
 2624:     $now = time;
 2625:     $crstype = $env{'form.crstype'};
 2626:     my $ccrole = 'cc';
 2627:     if ($crstype eq 'community') {
 2628:         $ccrole = 'co';
 2629:     }
 2630:     my @instsections;
 2631:     if ($crstype eq 'official') {
 2632:         if (&Apache::lonnet::auto_run('',$dom)) {
 2633:             ($enrollstart,$enrollend)=&dates_from_form('enrollstart','enrollend');
 2634:         }
 2635:         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
 2636:             if ($env{'form.sec_'.$i}) {
 2637:                 if ($env{'form.secnum_'.$i} ne '') {
 2638:                     my $sec = $env{'form.secnum_'.$i};
 2639:                     $sections{$i}{'inst'} = $sec;
 2640:                     if (($sec ne '') && (!grep(/^\Q$sec\E$/,@instsections))) {
 2641:                         push(@instsections,$sec);
 2642:                     }
 2643:                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
 2644:                 }
 2645:             }
 2646:         }
 2647:         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
 2648:             if ($env{'form.crosslist_'.$i}) {
 2649:                 my $xlistinfo = '';
 2650:                 if (ref($code_order) eq 'ARRAY') {
 2651:                     if (@{$code_order} > 0) {
 2652:                         foreach my $item (@{$code_order}) {
 2653:                             $xlistinfo .= $env{'form.crosslist_'.$i.'_'.$item};
 2654:                         }
 2655:                     }
 2656:                 }
 2657:                 $crosslistings{$i}{'instcode'} = $xlistinfo;
 2658:                 if ($xlistinfo ne '') {
 2659:                     $crosslisttotal ++;
 2660:                 }
 2661:                 $crosslistings{$i}{'instsec'} = $env{'form.crosslist_'.$i.'_instsec'}; 
 2662:                 $crosslistings{$i}{'loncapa'} = $env{'form.crosslist_'.$i.'_lcsec'};
 2663:             }
 2664:         }
 2665:     } else {
 2666:         $enrollstart = '';
 2667:         $enrollend = '';
 2668:     }
 2669:     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
 2670:         my $uname = $env{'form.person_'.$i.'_uname'};
 2671:         my $udom = $env{'form.person_'.$i.'_dom'};
 2672:         if (($uname =~ /^$match_username$/) && ($udom =~ /^$match_domain$/)) {
 2673:             if (&Apache::lonnet::domain($udom) ne '') {
 2674:                 unless (ref($personnel{$uname.':'.$udom}) eq 'HASH') {
 2675:                     $personnel{$uname.':'.$udom} = {
 2676:                              firstname    => $env{'form.person_'.$i.'_firstname'},
 2677:                              lastname     => $env{'form.person_'.$i.'_lastname'},
 2678:                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
 2679:                                                    };
 2680:                 }
 2681:                 my $role = $env{'form.person_'.$i.'_role'};
 2682:                 unless ($role eq '') {
 2683:                     if (ref($personnel{$uname.':'.$udom}{'roles'}) eq 'ARRAY') {
 2684:                         my @curr_roles = @{$personnel{$uname.':'.$udom}{'roles'}};
 2685:                         unless (grep(/^\Q$role\E$/,@curr_roles)) {
 2686:                             push(@{$personnel{$uname.':'.$udom}{'roles'}},$role);
 2687:                         }
 2688:                     } else {
 2689:                         @{$personnel{$uname.':'.$udom}{'roles'}} = ($role);
 2690:                     }
 2691:                     if ($role eq $ccrole) {
 2692:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
 2693:                     } else {
 2694:                         my @currsec = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
 2695:                         my @allsecs;
 2696:                         foreach my $sec (@currsec) {
 2697:                             next unless ($sec =~ /\w/);
 2698:                             next if ($sec =~ /\W/);
 2699:                             next if ($sec eq 'none');
 2700:                             push(@allsecs,$sec);
 2701:                         }
 2702:                         my $newsec = $env{'form.person_'.$i.'_newsec'};
 2703:                         $newsec =~ s/^\s+//;
 2704:                         $newsec =~s/\s+$//;
 2705:                         my @newsecs = split(/[\s,;]+/,$newsec);
 2706:                         foreach my $sec (@newsecs) {
 2707:                             next if ($sec =~ /\W/);
 2708:                             next if ($sec eq 'none');
 2709:                             if ($sec ne '') {
 2710:                                 unless (grep(/^\Q$sec\E$/,@allsecs)) {
 2711:                                     push(@allsecs,$sec);
 2712:                                 }
 2713:                             }
 2714:                         }
 2715:                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @allsecs;
 2716:                     }
 2717:                 }
 2718:             } else {
 2719:                 push(@missingdom,$uname.':'.$udom);
 2720:             }
 2721:         } else {
 2722:             push(@baduname,$uname.':'.$udom);
 2723:         }
 2724:     }
 2725:     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
 2726:     my $autodrops = 0;
 2727:     if ($env{'form.autodrops'}) {
 2728:         $autodrops = $env{'form.autodrops'}; 
 2729:     }
 2730:     my $autoadds = 0;
 2731:     if ($env{'form.autoadds'}) {
 2732:         $autodrops = $env{'form.autoadds'};
 2733:     }
 2734:     if ($env{'form.autoadds'}) {
 2735:         $autodrops = $env{'form.autoadds'};
 2736:     }
 2737:     my $instcode = '';
 2738:     if (exists($env{'form.instcode'})) {
 2739:         $instcode = $env{'form.instcode'};
 2740:     }
 2741:     my $clonecrs = '';
 2742:     my $clonedom = '';
 2743:     if (($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
 2744:         ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
 2745:         my $clonehome = &Apache::lonnet::homeserver($env{'form.clonecrs'},
 2746:                                                     $env{'form.clonedom'});
 2747:         if ($clonehome ne 'no_host') {  
 2748:             my $canclone =  
 2749:                 &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
 2750:                         $env{'user.domain'},$env{'form.clonecrs'},$env{'form.clonedom'},
 2751:                         $crstype);
 2752:             if ($canclone) {
 2753:                 $clonecrs = $env{'form.clonecrs'};
 2754:                 $clonedom = $env{'form.clonedom'};
 2755:             }
 2756:         }
 2757:     }
 2758:     my $details = {
 2759:                     owner          => $env{'user.name'},
 2760:                     domain         => $env{'user.domain'}, 
 2761:                     cdom           => $dom,
 2762:                     cnum           => $cnum,
 2763:                     coursehome     => $env{'form.chome'},
 2764:                     cdescr         => $env{'form.cdescr'},
 2765:                     crstype        => $env{'form.crstype'},
 2766:                     instcode       => $instcode,
 2767:                     clonedom       => $clonedom,
 2768:                     clonecrs       => $clonecrs,
 2769:                     datemode       => $env{'form.datemode'},
 2770:                     dateshift      => $env{'form.dateshift'},
 2771:                     sectotal       => $sectotal,
 2772:                     sections       => \%sections,
 2773:                     crosslisttotal => $crosslisttotal,
 2774:                     crosslists     => \%crosslistings,
 2775:                     autoadds       => $autoadds,
 2776:                     autodrops      => $autodrops,
 2777:                     enrollstart    => $enrollstart,
 2778:                     enrollend      => $enrollend,
 2779:                     accessstart    => $accessstart,
 2780:                     accessend      => $accessend,
 2781:                     personnel      => \%personnel,
 2782:                   };
 2783:     my (@inststatuses,$storeresult,$creationresult);
 2784:     my $val = 
 2785:         &Apache::loncoursequeueadmin::get_processtype($env{'user.name'},$env{'user.domain'},
 2786:             $env{'user.adv'},$dom,$crstype,\@inststatuses,\%domconfig);
 2787:     if ($val eq '') {
 2788:         if ($crstype eq 'official') {
 2789:             $output = &mt('You are not permitted to request creation of official courses.');
 2790:         } elsif ($crstype eq 'unofficial') {
 2791:             $output = &mt('You are not permitted to request creation of unofficial courses.');
 2792:         } elsif ($crstype eq 'community') {
 2793:             $output = &mt('You are not permitted to request creation of communities');
 2794:         } else {
 2795:             $output = &mt('Unrecognized course type: [_1]',$crstype);
 2796:         }
 2797:         $storeresult = 'notpermitted'; 
 2798:     } else {
 2799:         my ($disposition,$message,$reqstatus);
 2800:         my %reqhash = (
 2801:                         reqtime   => $now,
 2802:                         crstype   => $crstype,
 2803:                         details   => $details,
 2804:                       );
 2805:         my $requestkey = $dom.'_'.$cnum;
 2806:         my $validationerror;
 2807:         if ($val eq 'autolimit=') {
 2808:             $disposition = 'process';
 2809:         } elsif ($val =~ /^autolimit=(\d+)$/) {
 2810:             my $limit = $1;
 2811:             $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
 2812:                                             $dom,$crstype,$limit,\$message);
 2813:         } elsif ($val eq 'validate') {
 2814:             my ($inststatuslist,$validationchk,$validation);
 2815:             if (@inststatuses > 0) {
 2816:                 $inststatuslist = join(',',@inststatuses);
 2817:             }
 2818:             my $instseclist;
 2819:             if (@instsections > 0) {
 2820:                 $instseclist = join(',',@instsections);
 2821:             }
 2822:             $validationchk = 
 2823:                 &Apache::lonnet::auto_courserequest_validation($dom,
 2824:                     $env{'user.name'}.':'.$env{'user.domain'},$crstype,
 2825:                     $inststatuslist,$instcode,$instseclist);
 2826:             if ($validationchk =~ /:/) {
 2827:                 ($validation,$message) = split(':',$validationchk);
 2828:             } else {
 2829:                 $validation = $validationchk;
 2830:             }
 2831:             if ($validation =~ /^error(.*)$/) {
 2832:                 $disposition = 'approval';
 2833:                 $validationerror = $1;
 2834:             } else {
 2835:                 $disposition = $validation;
 2836:             }
 2837:         } else {
 2838:             $disposition = 'approval';
 2839:         }
 2840:         $reqhash{'disposition'} = $disposition;
 2841:         $reqstatus = $disposition;
 2842:         my ($modified,$queued);
 2843:         if ($disposition eq 'rejected') {
 2844:             if ($crstype eq 'community') {
 2845:                 $output = &mt('Your community request was rejected.');
 2846:             } else {
 2847:                 $output = &mt('Your course request was rejected.');
 2848:             }
 2849:             if ($message) {
 2850:                 $output .= '<div class="LC_warning">'.$message.'</div>';
 2851:             }
 2852:             $storeresult = 'rejected';
 2853:         } elsif ($disposition eq 'process') {
 2854:             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 2855:             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles);
 2856:             my $type = 'Course';
 2857:             if ($crstype eq 'community') {
 2858:                 $type = 'Community';
 2859:             }
 2860:             my @roles = &Apache::lonuserutils::roles_by_context('course','',$type);
 2861:             foreach my $role (@roles) {
 2862:                 $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
 2863:             }
 2864:             my $result = &Apache::loncoursequeueadmin::course_creation($dom,$cnum,
 2865:                                    'autocreate',$details,\$logmsg,\$newusermsg,\$addresult,
 2866:                                    \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
 2867:             if ($result eq 'created') {
 2868:                 $disposition = 'created';
 2869:                 $reqstatus = 'created';
 2870:                 my $role_result = &update_requestors_roles($dom,$cnum,$crstype,$details,
 2871:                                                            \%longroles);
 2872:                 if ($crstype eq 'community') {
 2873:                     $output = '<p>'.&mt('Your community request has been processed and the community has been created.');
 2874:                 } else {
 2875:                     $output = '<p>'.&mt('Your course request has been processed and the course has been created.');
 2876:                 }
 2877:                 $output .= '<br />'.$role_result.'</p>';
 2878:                 $creationresult = 'created';
 2879:             } else {
 2880:                 $output = '<span class="LC_error">';
 2881:                 if ($crstype eq 'community') {
 2882:                     $output .= &mt('An error occurred when processing your community request.');
 2883:                 } else {
 2884:                     $output .= &mt('An error occurred when processing your course request.');
 2885:                 }
 2886:                 $output .= '<br />'.
 2887:                            &mt('You may want to review the request details and submit the request again.').
 2888:                           '</span>';
 2889:                 $creationresult = 'error';
 2890:             }
 2891:         } else {
 2892:             my $requestid = $cnum.'_'.$disposition;
 2893:             my $request = { 
 2894:                             $requestid => {
 2895:                                             timestamp   => $now,
 2896:                                             crstype     => $crstype,
 2897:                                             ownername   => $env{'user.name'},
 2898:                                             ownerdom    => $env{'user.domain'},
 2899:                                             description => $env{'form.cdescr'}, 
 2900:                                           },
 2901:                           };
 2902:             my $statuskey = 'status:'.$dom.':'.$cnum;
 2903:             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
 2904:                                                    $env{'user.domain'},$env{'user.name'});
 2905:             if ($userreqhash{$statuskey} ne '') {
 2906:                 $modified = 1;
 2907:                 my $uname = &Apache::lonnet::get_domainconfiguser($dom);
 2908:                 my %queuehash = &Apache::lonnet::get('courserequestqueue',
 2909:                                                      [$cnum.'_approval',
 2910:                                                       $cnum.'_pending'],$dom,$uname);
 2911:                 if (($queuehash{$cnum.'_approval'} ne '') || 
 2912:                     ($queuehash{$cnum.'_pending'} ne '')) {
 2913:                     $queued = 1;
 2914:                 }
 2915:             }
 2916:             unless ($queued) {
 2917:                 my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
 2918:                                                             $dom);
 2919:                 if ($putresult eq 'ok') {
 2920:                     if ($crstype eq 'community') {
 2921:                         $output .= &mt('Your community request has been recorded.');
 2922:                     } else {
 2923:                         $output .= &mt('Your course request has been recorded.') 
 2924:                     }
 2925:                     $output .= '<br />'.
 2926:                               &notification_information($disposition,$req_notifylist,
 2927:                                                         $cnum,$now);
 2928:                 } else {
 2929:                     $reqstatus = 'domainerror';
 2930:                     $reqhash{'disposition'} = $disposition;
 2931:                     my $warning = &mt('An error occurred saving your request in the pending requests queue.');
 2932:                     $output = '<span class"LC_warning">'.$warning.'</span><br />';
 2933:                 }
 2934:             }
 2935:         }
 2936:         ($storeresult,my $updateresult) = 
 2937:             &Apache::loncoursequeueadmin::update_coursereq_status(\%reqhash,$dom,
 2938:                 $cnum,$reqstatus,'request');
 2939:         if ($modified && $queued && $storeresult eq 'ok') {
 2940:             if ($crstype eq 'community') {
 2941:                 $output .= '<p>'.&mt('Your community request has been updated').'</p>';
 2942:             } else {
 2943:                 $output .= '<p>'.&mt('Your course request has been updated').'</p>';
 2944:             }
 2945:             $output .= &notification_information($disposition,$req_notifylist,$cnum,$now);
 2946:         }
 2947:         if ($validationerror ne '') {
 2948:             $output .= '<p class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: [_1].',$validationerror).'</p>';
 2949:         }
 2950:         if ($updateresult) {
 2951:             $output .= $updateresult;
 2952:         }
 2953:     }
 2954:     if ($creationresult ne '') {
 2955:         return ($creationresult,$output);
 2956:     } else {
 2957:         return ($storeresult,$output);
 2958:     }
 2959: }
 2960: 
 2961: sub update_requestors_roles {
 2962:     my ($dom,$cnum,$crstype,$details,$longroles) = @_;
 2963:     my $now = time;
 2964:     my ($active,$future,$numactive,$numfuture,$output);
 2965:     my $owner = $env{'user.name'}.':'.$env{'user.domain'};
 2966:     if (ref($details) eq 'HASH') {
 2967:         if (ref($details->{'personnel'}) eq 'HASH') {
 2968:             my $ccrole = 'cc';
 2969:             if ($crstype eq 'community') {
 2970:                 $ccrole = 'co';
 2971:             }
 2972:             unless (ref($details->{'personnel'}{$owner}) eq 'HASH') {
 2973:                 $details->{'personnel'}{$owner} = {
 2974:                                                     'roles' => [$ccrole],
 2975:                                                     $ccrole => { 'usec' => [] },
 2976:                                                   };
 2977:             }
 2978:             my @roles;
 2979:             if (ref($details->{'personnel'}{$owner}{'roles'}) eq 'ARRAY') {
 2980:                 @roles = sort(@{$details->{'personnel'}{$owner}{'roles'}});
 2981:                 unless (grep(/^\Q$ccrole\E$/,@roles)) {
 2982:                     push(@roles,$ccrole);
 2983:                 }
 2984:             } else {
 2985:                 @roles = ($ccrole);
 2986:             }
 2987:             foreach my $role (@roles) {
 2988:                 my $refresh=$env{'user.refresh.time'};
 2989:                 if ($refresh eq '') {
 2990:                     $refresh = $env{'user.login.time'};
 2991:                 }
 2992:                 if ($refresh eq '') {
 2993:                     $refresh = $now;
 2994:                 }
 2995:                 my $start = $refresh-1;
 2996:                 my $end = '0';
 2997:                 if ($role eq 'st') {
 2998:                     if ($details->{'accessstart'} ne '') {
 2999:                         $start = $details->{'accessstart'};
 3000:                     }
 3001:                     if ($details->{'accessend'} ne '') {
 3002:                         $end = $details->{'accessend'};
 3003:                     }
 3004:                 }
 3005:                 my @usecs;
 3006:                 if ($role ne $ccrole) {
 3007:                     if (ref($details->{'personnel'}{$owner}{$role}{'usec'}) eq 'ARRAY') {
 3008:                         @usecs = @{$details->{'personnel'}{$owner}{$role}{'usec'}};
 3009:                     }
 3010:                 } 
 3011:                 if ($role eq 'st') {
 3012:                     if (@usecs > 1) {
 3013:                         my $firstsec = $usecs[0];
 3014:                         @usecs = ($firstsec);
 3015:                     }
 3016:                 }
 3017:                 if (@usecs == 0) {
 3018:                     push(@usecs,'');
 3019:                 }
 3020:                 foreach my $usec (@usecs) {
 3021:                     my (%userroles,%newrole,%newgroups,$spec,$area);
 3022:                     my $area = '/'.$dom.'/'.$cnum;
 3023:                     my $spec = $role.'.'.$area;
 3024:                     if ($usec ne '') {
 3025:                        $spec .= '/'.$usec;
 3026:                        $area .= '/'.$usec;
 3027:                     }
 3028:                     if ($role =~ /^cr\//) {
 3029:                         &Apache::lonnet::custom_roleprivs(\%newrole,$role,$dom,
 3030:                                                           $cnum,$spec,$area);
 3031:                     } else {
 3032:                         &Apache::lonnet::standard_roleprivs(\%newrole,$role,$dom,
 3033:                                                             $spec,$cnum,$area);
 3034:                     }
 3035:                     &Apache::lonnet::set_userprivs(\%userroles,\%newrole,
 3036:                                                    \%newgroups);
 3037:                     $userroles{'user.role.'.$spec} = $start.'.'.$end;
 3038:                     &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
 3039:                     if (($end == 0) || ($end > $now)) {
 3040:                         my $showrole = $role;
 3041:                         if ($role =~ /^cr\//) {
 3042:                             $showrole = &Apache::lonnet::plaintext($role,$crstype);
 3043:                         } elsif (ref($longroles) eq 'HASH') {
 3044:                             if ($longroles->{$role} ne '') {
 3045:                                 $showrole = $longroles->{$role};
 3046:                             }
 3047:                         }
 3048:                         if ($start <= $now) {
 3049:                             $active .= '<li><a href="/adm/roles?selectrole=1&'.
 3050:                                        $spec.'=1">'.$showrole;
 3051:                             if ($usec ne '') {
 3052:                                 $active .= ' - '.&mt('section:').' '.$usec; 
 3053:                             }
 3054:                             $active .= '</a></li>';
 3055:                             $numactive ++;
 3056:                         } else { 
 3057:                             $future .= '<li>'.$showrole;
 3058:                             if ($usec ne '') {
 3059:                                 $future .= ' - '.&mt('section:').' '.$usec;
 3060:                             }
 3061:                             $future .= '</li>';
 3062:                             $numfuture ++;
 3063:                         }
 3064:                     }
 3065:                 }
 3066:             }
 3067:         }
 3068:     }
 3069:     if ($active) {
 3070:         if ($numactive == 1) {
 3071:             if ($crstype eq 'Community') {
 3072:                 $output = &mt('Use the following link to enter the community:');
 3073:             } else {
 3074:                 $output = &mt('Use the following link to enter the course:'); 
 3075:             }
 3076:         } else {
 3077:             if ($crstype eq 'Community') {
 3078:                 $output = &mt('Use the following links to your new roles to enter the community:');
 3079:             } else {
 3080:                 $output = &mt('Use the following links to your new roles to enter the course:');
 3081:             }
 3082:         }
 3083:         $output .= ' <ul>'.$active.'</ul><br />';
 3084:     }
 3085:     if ($future) {
 3086:         if ($crstype eq 'Community') {
 3087:             $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'}))
 3088:         } else {
 3089:             $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'}));
 3090:         }
 3091:         $output .= ' <ul>'.$future.'</ul>';
 3092:     }
 3093:     return $output;
 3094: }
 3095: 
 3096: sub notification_information {
 3097:     my ($disposition,$req_notifylist,$cnum,$now) = @_;
 3098:     my %emails = &Apache::loncommon::getemails();
 3099:     my $address;
 3100:     if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
 3101:         $address = $emails{'permanentemail'};
 3102:         if ($address eq '') {
 3103:             $address = $emails{'notification'};
 3104:         }
 3105:     }
 3106:     my $output;
 3107:     if ($disposition eq 'approval') {
 3108:         $output .= &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
 3109:                    &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
 3110:         if ($address ne '') {
 3111:             $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
 3112:         }
 3113:         if ($req_notifylist) {
 3114:             my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 3115:                                                                      $env{'user.domain'});
 3116:             my $sender = $env{'user.name'}.':'.$env{'user.domain'};
 3117:             &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,"$fullname ($env{'user.name'}:$env{'user.domain'})",$cnum,$env{'form.cdescr'},$now,'coursereq',$sender);
 3118:         }
 3119:     } elsif ($disposition eq 'pending') {
 3120:         $output .= '<div class="LC_info">'.
 3121: &mt('Your request has been placed in a queue pending administrative action.').'<br />'.
 3122: &mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
 3123: &mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
 3124:                    '</div>';
 3125:     } else {
 3126:         $output .= '<div class="LC_warning">'.
 3127:                    &mt('Your request status is: [_1].',$disposition).
 3128:                    '</div>';
 3129:     }
 3130:     return $output;
 3131: }
 3132: 
 3133: sub check_autolimit {
 3134:     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
 3135:     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
 3136:                        'userroles',['active','future'],['cc','co'],[$dom]);
 3137:     my ($types,$typename) = &Apache::loncommon::course_types();
 3138:     my %requests = &Apache::lonnet::dumpstore('courserequests',$udom,$uname);
 3139:     my $count = 0;
 3140:     foreach my $key (keys(%requests)) {
 3141:         my ($cdom,$cnum) = split('_',$key);
 3142:         if (ref($requests{$key}) eq 'HASH') {
 3143:             next if ($requests{$key}{'crstype'} ne $crstype);
 3144:             if (($crstype eq 'community') && 
 3145:                 (exists($crsroles{$cnum.':'.$cdom.':co'}))) {
 3146:                 $count ++;
 3147:             } elsif ((($crstype eq 'official') || ($crstype eq 'unofficial')) &&
 3148:                      (exists($crsroles{$cnum.':'.$cdom.':cc'}))) {
 3149:                 $count ++;
 3150:             }
 3151:         }
 3152:     }
 3153:     if ($count < $limit) {
 3154:         return 'process';
 3155:     } else {
 3156:         if (ref($typename) eq 'HASH') {
 3157:             if ($crstype eq 'community') {
 3158:                 $$message = &mt('Your request has not been processed because you have reached the limit for the number of communities.').
 3159:                             '<br />'.&mt("Your limit is [_1].",$limit);
 3160:             } else {
 3161:                 $$message = &mt('Your request has not been processed because you have reached the limit for the number of courses of this type.').
 3162:                             '<br />'.&mt("Your $typename->{$crstype} limit is [_1].",$limit);
 3163:             }
 3164:         }
 3165:         return 'rejected';
 3166:     }
 3167:     return;
 3168: }
 3169: 
 3170: sub retrieve_settings {
 3171:     my ($dom,$cnum,$udom,$uname) = @_;
 3172:     if ($udom eq '' || $uname eq '') {
 3173:         $udom = $env{'user.domain'};
 3174:         $uname = $env{'user.name'};
 3175:     }
 3176:     my ($result,%reqinfo) = &get_request_settings($dom,$cnum,$udom,$uname);
 3177:     if ($result eq 'ok') {
 3178:         if (($udom eq $reqinfo{'domain'}) &&  ($uname eq $reqinfo{'owner'})) {
 3179:             $env{'form.chome'} = $reqinfo{'coursehome'};
 3180:             $env{'form.cdescr'} = $reqinfo{'cdescr'};
 3181:             $env{'form.crstype'} = $reqinfo{'crstype'}; 
 3182:             &generate_date_items($reqinfo{'accessstart'},'accessstart');
 3183:             &generate_date_items($reqinfo{'accessend'},'accessend');
 3184:             if ($reqinfo{'accessend'} == 0) {
 3185:                 $env{'form.no_end_date'} = 1;
 3186:             }
 3187:             if (($reqinfo{'crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
 3188:                 &generate_date_items($reqinfo{'enrollstart'},'enrollstart');
 3189:                 &generate_date_items($reqinfo{'enrollend'},'enrollend');
 3190:             }
 3191:             $env{'form.clonecrs'} = $reqinfo{'clonecrs'};
 3192:             $env{'form.clonedom'} = $reqinfo{'clonedom'};
 3193:             $env{'form.datemode'} = $reqinfo{'datemode'};
 3194:             $env{'form.dateshift'} = $reqinfo{'dateshift'};
 3195:             if (($reqinfo{'crstype'} eq 'official') && ($reqinfo{'instcode'} ne '')) { 
 3196:                  $env{'form.sectotal'} = $reqinfo{'sectotal'};
 3197:                  $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
 3198:                  $env{'form.autoadds'} = $reqinfo{'autoadds'};
 3199:                  $env{'form.autdrops'} = $reqinfo{'autodrops'};
 3200:                  $env{'form.instcode'} = $reqinfo{'instcode'};
 3201:                  my $crscode = { 
 3202:                                  $cnum => $reqinfo{'instcode'},
 3203:                                };
 3204:                  &extract_instcode($dom,'instcode',$crscode,$cnum);
 3205:             }
 3206:             my @currsec;
 3207:             if (ref($reqinfo{'sections'}) eq 'HASH') {
 3208:                 foreach my $i (sort(keys(%{$reqinfo{'sections'}}))) {
 3209:                     if (ref($reqinfo{'sections'}{$i}) eq 'HASH') {
 3210:                         my $sec = $reqinfo{'sections'}{$i}{'inst'};
 3211:                         $env{'form.secnum_'.$i} = $sec;
 3212:                         $env{'form.sec_'.$i} = '1';
 3213:                         if (!grep(/^\Q$sec\E$/,@currsec)) {
 3214:                             push(@currsec,$sec);
 3215:                         }
 3216:                         $env{'form.loncapasec_'.$i} = $reqinfo{'sections'}{$i}{'loncapa'};
 3217:                     }
 3218:                 }
 3219:             }
 3220:             if (ref($reqinfo{'crosslists'}) eq 'HASH') {
 3221:                 foreach my $i (sort(keys(%{$reqinfo{'crosslists'}}))) {
 3222:                     if (ref($reqinfo{'crosslists'}{$i}) eq 'HASH') {
 3223:                         $env{'form.crosslist_'.$i} = '1';
 3224:                         $env{'form.crosslist_'.$i.'_instsec'} = $reqinfo{'crosslists'}{$i}{'instsec'};
 3225:                         $env{'form.crosslist_'.$i.'_lcsec'} = $reqinfo{'crosslists'}{$i}{'loncapa'};
 3226:                         if ($reqinfo{'crosslists'}{$i}{'instcode'} ne '') {
 3227:                             my $key = $cnum.$i; 
 3228:                             my $crscode = {
 3229:                                               $key => $reqinfo{'crosslists'}{$i}{'instcode'},
 3230:                                           };
 3231:                             &extract_instcode($dom,'crosslist',$crscode,$key,$i);
 3232:                         }
 3233:                     }
 3234:                 }
 3235:             }
 3236:             if (ref($reqinfo{'personnel'}) eq 'HASH') {
 3237:                 my $i = 0;
 3238:                 foreach my $user (sort(keys(%{$reqinfo{'personnel'}}))) {
 3239:                     my ($uname,$udom) = split(':',$user);
 3240:                     if (ref($reqinfo{'personnel'}{$user}) eq 'HASH') {
 3241:                         if (ref($reqinfo{'personnel'}{$user}{'roles'}) eq 'ARRAY') {
 3242:                             foreach my $role (sort(@{$reqinfo{'personnel'}{$user}{'roles'}})) {
 3243:                                 $env{'form.person_'.$i.'_role'} = $role;
 3244:                                 $env{'form.person_'.$i.'_firstname'} = $reqinfo{'personnel'}{$user}{'firstname'};
 3245:                                 $env{'form.person_'.$i.'_lastname'} = $reqinfo{'personnel'}{$user}{'lastname'}; ;
 3246:                                 $env{'form.person_'.$i.'_emailaddr'} = $reqinfo{'personnel'}{$user}{'emailaddr'};
 3247:                                 $env{'form.person_'.$i.'_uname'} = $uname;
 3248:                                 $env{'form.person_'.$i.'_dom'} = $udom;
 3249:                                 if (ref($reqinfo{'personnel'}{$user}{$role}) eq 'HASH') {
 3250:                                     if (ref($reqinfo{'personnel'}{$user}{$role}{'usec'}) eq 'ARRAY') {
 3251:                                         my @usecs = @{$reqinfo{'personnel'}{$user}{$role}{'usec'}};
 3252:                                         my @newsecs;
 3253:                                         if (@usecs > 0) {
 3254:                                             foreach my $sec (@usecs) {
 3255:                                                 if (grep(/^\Q$sec\E/,@currsec)) {
 3256:                                                     $env{'form.person_'.$i.'_sec'} = $sec;
 3257:                                                 } else {
 3258:                                                     push(@newsecs,$sec);
 3259:                                                 }
 3260:                                             }
 3261:                                         }
 3262:                                         if (@newsecs > 0) {
 3263:                                             $env{'form.person_'.$i.'_newsec'} = join(',',@newsecs); 
 3264:                                         }
 3265:                                     }
 3266:                                 }
 3267:                                 $i ++;
 3268:                             }
 3269:                         }
 3270:                     }
 3271:                 }
 3272:                 $env{'form.persontotal'} = $i;
 3273:             }
 3274:         }
 3275:     }
 3276:     return $result;
 3277: }
 3278: 
 3279: sub get_request_settings {
 3280:     my ($dom,$cnum,$udom,$uname) = @_;
 3281:     my $requestkey = $dom.'_'.$cnum;
 3282:     my ($result,%reqinfo);
 3283:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 3284:         my %history = &Apache::lonnet::restore($requestkey,'courserequests',$udom,$uname);
 3285:         my $disposition = $history{'disposition'};
 3286:         if (($disposition eq 'approval') || ($disposition eq 'pending')) { 
 3287:             if (ref($history{'details'}) eq 'HASH') {
 3288:                 %reqinfo = %{$history{'details'}};
 3289:                 $result = 'ok';
 3290:             } else {
 3291:                 $result = 'nothash';
 3292:             }
 3293:         } else {
 3294:             $result = 'notqueued';
 3295:         }
 3296:     } else {
 3297:         $result = 'invalid';
 3298:     }
 3299:     return ($result,%reqinfo);
 3300: }
 3301: 
 3302: sub extract_instcode {
 3303:     my ($cdom,$element,$crscode,$crskey,$counter) = @_;
 3304:     my (%codes,@codetitles,%cat_titles,%cat_order);
 3305:     if (&Apache::lonnet::auto_instcode_format('requests',$cdom,$crscode,\%codes,
 3306:                                               \@codetitles,\%cat_titles,
 3307:                                               \%cat_order) eq 'ok') {
 3308:         if (ref($codes{$crskey}) eq 'HASH') {
 3309:             if (@codetitles > 0) {
 3310:                 my $sel = $element;
 3311:                 if ($element eq 'crosslist') {
 3312:                     $sel .= '_'.$counter;
 3313:                 }
 3314:                 foreach my $title (@codetitles) {
 3315:                     $env{'form.'.$sel.'_'.$title} = $codes{$crskey}{$title};
 3316:                 }
 3317:             }
 3318:         }
 3319:     }
 3320:     return;
 3321: }
 3322: 
 3323: sub generate_date_items {
 3324:     my ($currentval,$item) = @_;
 3325:     if ($currentval =~ /\d+/) {
 3326:         my ($tzname,$sec,$min,$hour,$mday,$month,$year) = 
 3327:             &Apache::lonhtmlcommon::get_timedates($currentval);
 3328:         $env{'form.'.$item.'_day'} = $mday;
 3329:         $env{'form.'.$item.'_month'} = $month+1;
 3330:         $env{'form.'.$item.'_year'} = $year;
 3331:     }
 3332:     return;
 3333: }
 3334: 
 3335: 1;
 3336: 

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