File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.30: download - view: text, annotated - select for diffs
Tue Aug 14 15:45:06 2012 UTC (11 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add ability for users to request creation of an author role in their
  domain, if they do not already have an active author role.
  - Domain defaults can be assigned based on institutional status.
  - Defaults can be overridden for individual users by the DC.
  - Requests can either be processed automatically or queued for DC
    approval.
  - Notification message can be sent to DCs when queued for approval
  - Notification message sent to requester if approved/rejected.

    1: # The LearningOnline Network
    2: # Utilities to administer domain course requests and course self-enroll requests
    3: #
    4: # $Id: loncoursequeueadmin.pm,v 1.30 2012/08/14 15:45:06 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: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::loncoursequeueadmin.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: Utilities used by domain coordinators to administer queued course creation requests,
   39: and by course coordinators for queued self-enrollment requests, and by general
   40: users to display their queued self-enrollment requests.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: =head1 SUBROUTINES
   46: 
   47: =over
   48: 
   49: =item send_selfserve_notification()
   50: 
   51: =item display_queued_requests()
   52: 
   53: =item build_queue_display()
   54: 
   55: =item update_request_queue()
   56: 
   57: =item get_student_counts()
   58: 
   59: =item course_creation()
   60: 
   61: =item build_batchcreatehash()
   62:  
   63: =item can_clone_course()
   64: 
   65: =item get_processtype()
   66: 
   67: =item queued_selfenrollment()
   68: 
   69: =item update_coursereq_status()
   70: 
   71: =item process_official_reqs()
   72: 
   73: =back
   74: 
   75: =cut
   76: 
   77: package Apache::loncoursequeueadmin;
   78: 
   79: use strict;
   80: use Apache::lonnet;
   81: use Apache::loncommon;
   82: use Apache::lonmsg;
   83: use Apache::lonlocal;
   84: use Apache::lonuserutils;
   85: use LONCAPA qw(:DEFAULT :match);
   86: 
   87: sub send_selfserve_notification {
   88:     my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
   89:         $approvedlist,$rejectedlist,$crstype) = @_;
   90: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
   91: #       so this can be localized to the recipients date display format/time zone
   92:     $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
   93:     my ($msgcc,$rawsubj,@rawmsg,$subject,$message,$reviewer,$msgtxt);
   94:     my ($senderuname,$senderudom) = split(':',$sender);
   95:     if ($context eq 'coursemanagers') {
   96:         $rawsubj = 'Self-enrollment requests processed';
   97:         push(@rawmsg,{
   98:                       mt => 'Enrollment requests in the following course: [_1] have been processed.',
   99:                       args => ["\n$contextdesc"],
  100:                      });
  101:     } elsif ($context eq 'domainmanagers') {
  102:         $rawsubj = 'Course/Community requests reviewed';
  103:         push(@rawmsg,{
  104:                       mt  => 'Course/Community creation requests in the following domain: [_1] have been reviewed.',
  105:                       args => ["\n$contextdesc"],
  106:                      });
  107:         if (ref($textstr) eq 'ARRAY') {
  108:             push(@rawmsg,@{$textstr});
  109:         }
  110:     } elsif ($context eq 'authormanagers') {
  111:         $rawsubj = 'Authoring space requests reviewed';
  112:         push(@rawmsg,{
  113:                       mt  => 'Authoring requests in the following domain: [_1] have been reviewed.',
  114:                       args => ["\n$contextdesc"],
  115:                      });
  116:         if (ref($textstr) eq 'ARRAY') {
  117:             push(@rawmsg,@{$textstr});
  118:         }
  119:     } elsif ($context eq 'enroller') {
  120:         $rawsubj = 'Enrollment request';
  121:         if ($crstype eq 'community') {
  122:             $msgtxt = 'Your request for enrollment in the following community: [_1]requested on [_2]has been reviewed by a Coordinator.'
  123:         } else {
  124:             $msgtxt = 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.';
  125:         } 
  126:         push(@rawmsg,{
  127:                       mt  => $msgtxt,
  128:                       args => ["\n  ".$contextdesc.",\n",$timestamp.",\n"],
  129: 
  130:                      });
  131:         if (ref($textstr) eq 'ARRAY') {
  132:             push(@rawmsg,@{$textstr});
  133:         }
  134:     } elsif ($context eq 'courserequestor') {
  135:         if ($crstype eq 'Community') {
  136:             $rawsubj = 'Community request';
  137:             $msgtxt = 'Your request for creation of the following community: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  138:         } else {
  139:             $rawsubj = 'Course request';
  140:             $msgtxt = 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  141:         }
  142:         push(@rawmsg,{
  143:                       mt  => $msgtxt,
  144:                       args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
  145: 
  146:                      });
  147:         if (ref($textstr) eq 'ARRAY') {
  148:             push(@rawmsg,@{$textstr});
  149:         }
  150:     } elsif ($context eq 'pendingrequestor') {
  151:         if ($crstype eq 'Community') {
  152:             $rawsubj = 'Community request';
  153:         } else {
  154:             $rawsubj = 'Processed course request';
  155:         }
  156:         if (ref($textstr) eq 'ARRAY') {
  157:             push(@rawmsg,@{$textstr});
  158:         }
  159:     } elsif ($context eq 'coursereq') {
  160:         if ($crstype eq 'community') {
  161:             $rawsubj = 'Community request to review';
  162:             $msgtxt = 'Creation of the following community: [_1]was requested by [_2] on [_3].';
  163:         } else {
  164:             $rawsubj = 'Course request to review';
  165:             $msgtxt = 'Creation of the following course: [_1]was requested by [_2] on [_3].';
  166:         }
  167:         push(@rawmsg,{
  168:                       mt  => $msgtxt,
  169:                       args => ["\n  $contextdesc\n",$textstr,$timestamp],
  170:                      },
  171:                      {
  172:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Course and community creation -> Approve or reject requests[_3]to display a list of pending requests, which you can either approve or reject.',
  173:                       args => ["\n","\n\n  ","\n\n"],
  174:                      });
  175:     } elsif ($context eq 'selfenrollreq') {
  176:         $rawsubj = 'Self-enrollment request';
  177:         if ($crstype eq 'community') {
  178:             $msgtxt = 'Enrollment in the following community: [_1] was requested by [_2] on [_3].'
  179:         } else {
  180:             $msgtxt = 'Enrollment in the following course: [_1] was requested by [_2] on [_3].'
  181:         }
  182:         push(@rawmsg,{
  183:                       mt  => $msgtxt,
  184:                       args => ["\n  $contextdesc\n",$textstr,$timestamp."\n"],
  185:                      });
  186:         my $directions;  
  187:         if ($crstype eq 'community') {
  188:             $directions = 'As Coordinator, use: [_1]Main Menu -> Manage Community Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
  189:         } else {
  190:             $directions = 'As Course Coordinator, use: [_1]Main Menu -> Manage Course Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
  191:         }
  192:         push(@rawmsg,
  193:                      {
  194:                       mt   => $directions,
  195:                       args => ["  \n\n","\n"],
  196:                      });
  197:     } elsif ($context eq 'authorreq') {
  198:         $rawsubj = 'Authoring space request to review';
  199:         $msgtxt = 'Assignment of an author role in the [_1] domain[_2] was requested by [_3] on [_4].';
  200:         push(@rawmsg,{
  201:                       mt  => $msgtxt,
  202:                       args => [$contextdesc,"\n",$textstr,$timestamp],
  203:                      },
  204:                      {
  205:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users -> Authoring space reqests[_3]to display a list of pending requests, which you can either approve or reject.',
  206:                       args => ["\n","\n\n  ","\n\n"],
  207:                      });
  208: 
  209:     } elsif ($context eq 'requestauthor') {
  210:         $rawsubj = 'Authoring space request';
  211:         $msgtxt = 'Your request for an authoring space requested on [_1]has been reviewed by a Domain Coordinator.';
  212:         push(@rawmsg,{
  213:                       mt  => $msgtxt,
  214:                       args => [$timestamp."\n"],
  215:                      });
  216:         if (ref($textstr) eq 'ARRAY') {
  217:             push(@rawmsg,@{$textstr});
  218:         }
  219:     }
  220:     my @to_notify = split(/,/,$notifylist);
  221:     my $numsent = 0;
  222:     my @recusers;
  223:     my @recudoms;
  224:     foreach my $cc (@to_notify) {
  225:         my ($ccname,$ccdom) = split(/:/,$cc);
  226:         if (!exists($msgcc->{$ccname.':'.$ccdom})) {
  227:             push(@recusers,$ccname);
  228:             push(@recudoms,$ccdom);
  229:             $msgcc->{$ccname.':'.$ccdom}='';
  230:             $numsent ++;
  231:         }
  232:     }
  233:     my %reciphash = (
  234:                      cc => $msgcc,
  235:     );
  236:     my ($uname,$udom);
  237:     if ($sender =~ /:/) {
  238:         ($uname,$udom) = split(/:/,$sender);
  239:     } elsif ($context eq 'course') {
  240:         $uname = $sender;
  241:         my %courseinfo = &Apache::lonnet::coursedescription($cid);
  242:         $udom = $courseinfo{'num'};
  243:     }
  244:     my %sentmessage;
  245:     my $stamp = time;
  246:     my $msgcount = &Apache::lonmsg::get_uniq();
  247:     my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
  248:     $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
  249:     $message = '';
  250:     foreach my $item (@rawmsg) {
  251:         if (ref($item) eq 'HASH') {
  252:             $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
  253:         }
  254:     }
  255:     &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms,undef,undef,undef,undef,$senderuname,$senderudom);
  256:     my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
  257:     my $status;
  258:     foreach my $recip (sort(keys(%{$msgcc}))) {
  259:         my ($ccname,$ccdom) = split(/:/,$recip);
  260:         my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
  261:         my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
  262:         my $message = '';
  263:         foreach my $item (@rawmsg) {
  264:             if (ref($item) eq 'HASH') {
  265:                 $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
  266:                                                        @{$item->{args}})."\n";
  267:             }
  268:         }
  269:         if ($context eq 'coursemanagers') {
  270:             if ($approvedlist) {
  271:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
  272:             }
  273:             if ($rejectedlist) {
  274:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
  275:             }
  276:         } elsif ($context eq 'domainmanagers') {
  277:             if ($approvedlist) {
  278:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
  279:             }
  280:             if ($rejectedlist) {
  281:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
  282:             }
  283:         } elsif ($context eq 'authormanagers') {
  284:             if ($approvedlist) {
  285:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved author role requests:')."\n".$approvedlist;
  286:             }
  287:             if ($rejectedlist) {
  288:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected author role requests:')."\n".$rejectedlist;
  289:             }
  290:         }
  291:         $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
  292:     }
  293:     $status =~ s/,$//;
  294:     return ($recipstatus,$status);
  295: }
  296: 
  297: sub display_queued_requests {
  298:     my ($context,$dom,$cnum) = @_;
  299:     my ($namespace,$formaction,$nextelement,%requesthash);
  300:     if ($context eq 'course') {
  301:         $formaction = '/adm/createuser';
  302:         $namespace = 'selfenrollrequests';
  303:         %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
  304:         $nextelement = '<input type="hidden" name="state" value="done" />';
  305:     } elsif ($context eq 'requestauthor') {
  306:         $formaction = '/adm/createuser';
  307:         $namespace = 'requestauthorqueue';
  308:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
  309:         $nextelement = '<input type="hidden" name="state" value="done" />';
  310:     } else {
  311:         $formaction = '/adm/createcourse';
  312:         $namespace = 'courserequestqueue';
  313:         my $disposition = 'approval';
  314:         my $nextphase = 'requestchange';
  315:         if ($context eq 'pending') {
  316:             $disposition = 'pending';
  317:             $nextphase = 'requestvalidation';
  318:         }
  319:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_'.$disposition);
  320:         $nextelement = '<input type="hidden" name="phase" value="'.$nextphase.'" />';
  321:     }
  322:     my ($output,%queue_by_date);
  323:     if (keys(%requesthash) > 0) {
  324:         $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
  325:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
  326:                   $nextelement."\n";
  327:         foreach my $item (keys(%requesthash)) {
  328:             my ($timestamp,$entry,$pending);
  329:             if ($context eq 'course') {
  330:                 ($timestamp, my $usec) = split(/:/,$requesthash{$item});
  331:                 $entry = $item.':'.$usec;
  332:             } elsif ($context eq 'requestauthor') {
  333:                 $timestamp = $requesthash{$item};
  334:                 ($entry) = ($item =~ /^($match_username)_approval$/);
  335:             } else {
  336:                 $timestamp = $requesthash{$item}{'timestamp'};
  337:                 if (ref($requesthash{$item}) eq 'HASH') {
  338:                     my ($cnum,$disposition) = split('_',$item);
  339:                     $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
  340:                              $requesthash{$item}{'ownerdom'}.':';
  341:                     if ($context eq 'pending') {
  342:                         $entry .= $requesthash{$item}{'instcode'};
  343:                     } else {
  344:                         $entry .= $requesthash{$item}{'crstype'};
  345:                     }
  346:                     $entry .= ':'.$requesthash{$item}{'description'};
  347:                 }
  348:             }
  349:             if ($entry ne '') {
  350:                 if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
  351:                     push(@{$queue_by_date{$timestamp}},$entry);
  352:                 } else {
  353:                     $queue_by_date{$timestamp} = [$entry];
  354:                 }
  355:             }
  356:         }
  357:         if (keys(%queue_by_date) > 0) {
  358:             if ($context eq 'course') {
  359:                 $output .=  '<h3>'.&mt('Self-enrollment requests queued pending approval by a Coordinator').'</h3>';
  360:             } elsif ($context eq 'pending') {
  361:                 $output .= '<h3>'.&mt('Requests for official courses queued pending validation').'</h3>'.
  362:                            '<p>'.&mt('Requests are validated against institutional data to confirm that the requestor is an instructor of record.').'<br />'.
  363:                            &mt('Validation is attempted when the request is submitted.').' '.&mt('If unvalidated, the request will be held in a queue.').' '.&mt('Validation of pending requests is automatically repeated daily.').'</p>';
  364:             } elsif ($context eq 'requestauthor') {
  365:                 $output .= '<h3>'.&mt('Requests for authoring space queued pending approval by a Domain Coordinator').'</h3>';
  366:             } else {
  367:                 $output .= '<h3>'.&mt('Course/Community requests queued pending approval by a Domain Coordinator').'</h3>';
  368:             } 
  369:             $output .= &build_queue_display($dom,$context,\%queue_by_date).
  370:                        '<input type="hidden" name="queue" value="approval" />';
  371:         } else {
  372:             $output .= '<div class="LC_info">';
  373:             if ($context eq 'course') {
  374:                 $output .= &mt('There are currently no enrollment requests awaiting approval.');
  375:             } elsif ($context eq 'pending') {
  376:                 $output .= &mt('There are currently no requests for official courses awaiting validation.');
  377:             } elsif ($context eq 'requestauthor') {
  378:                 $output .= &mt('There are currently no requests for authoring space awaiting approval.');
  379:             } elsif ($context eq 'domain') {
  380:                 $output .= &mt('There are currently no course or community requests awaiting approval.');
  381:             }
  382:             $output .= '</div>'; 
  383:         }
  384:         if ($context eq 'pending') {
  385:             $output .= '<br /><input type="submit" name="validationcheck" value="'.
  386:                        &mt('Validate').'" /><br />'."\n".
  387:                        '<p>'.&mt('Any course/community requests which are successfully validated will be created immediately.').' '.&mt('Unvalidated requests will be listed for manual approval/rejection.').'</p>';
  388:         } else {
  389:             $output .= '<br /><input type="submit" name="processqueue" value="'.&mt('Save').'" />';
  390:         }
  391:         $output .= '</form>';
  392:     } else {
  393:         $output .= '<div class="LC_info">';
  394:         if ($context eq 'course') {
  395:             $output .= &mt('There are currently no enrollment requests awaiting approval.');
  396:         } elsif ($context eq 'pending') {
  397:             $output .= &mt('There are currently no requests for official courses awaiting validation.');
  398:         } else {
  399:             $output .= &mt('There are currently no course or community requests awaiting approval.');
  400:         }
  401:         $output .= '</div>';
  402:     }
  403:     return $output;
  404: }
  405: 
  406: sub build_queue_display {
  407:     my ($dom,$context,$queue) = @_;
  408:     return unless (ref($queue) eq 'HASH');
  409:     my %crstypes;
  410:     my $output =  &Apache::loncommon::start_data_table().
  411:                   &Apache::loncommon::start_data_table_header_row();
  412:     unless ($context eq 'pending') { 
  413:         $output .= '<th>'.&mt('Action').'</th>';
  414:     }
  415:     $output .= '<th>'.&mt('Requestor').'</th>';
  416:     if ($context eq 'course') {
  417:         $output .= '<th>'.&mt('Section').'</th>'.
  418:                    '<th>'.&mt('Date requested').'</th>';
  419:     } elsif ($context eq 'requestauthor') {
  420:         $output .= '<th>'.&mt('Date requested').'</th>';
  421:     } elsif ($context eq 'pending' || $context eq 'stillpending') {
  422:         $output .= '<th>'.&mt('Institutional code').'</th>'.
  423:                    '<th>'.&mt('Date requested').'</th>'.
  424:                    '<th>'.&mt('Details').'</th>';
  425:     } else {
  426:         %crstypes = &Apache::lonlocal::texthash (
  427:                         official   => 'Official course',
  428:                         unofficial => 'Unofficial course',
  429:                         community  => 'Community',
  430:                     );
  431:         $output .= '<th>'.&mt('Type').'</th>'.
  432:                    '<th>'.&mt('Date requested').'</th>'.
  433:                    '<th>'.&mt('Details').'</th>';
  434:     }
  435:     $output .= &Apache::loncommon::end_data_table_header_row();
  436:     my @sortedtimes = sort {$a <=> $b} (keys(%{$queue}));
  437:     my $count = 0;
  438:     foreach my $item (@sortedtimes) {
  439:         if (ref($queue->{$item}) eq 'ARRAY') {
  440:             foreach my $request (sort(@{$queue->{$item}})) {
  441:                 my ($row,$approve,$reject,$showtime,$showsec,$namelink,
  442:                     $detailslink,$crstype,$instcode);
  443:                 $showtime = &Apache::lonlocal::locallocaltime($item);
  444:                 if ($context eq 'course') {
  445:                     my ($puname,$pudom,$pusec) = split(/:/,$request);
  446:                     $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
  447:                     $reject = $puname.':'.$pudom;
  448:                     $showsec = $pusec;
  449:                     if ($showsec eq '') {
  450:                         $showsec = &mt('none');
  451:                     }
  452:                     $namelink = &Apache::loncommon::aboutmewrapper(
  453:                                 &Apache::loncommon::plainname($puname,$pudom),
  454:                                 $puname,$pudom);
  455:                 } elsif ($context eq 'requestauthor') {
  456:                     if (&Apache::lonnet::homeserver($request,$dom) ne 'no_host') {
  457:                         $approve = $count.':'.$request;
  458:                         $reject = $request; 
  459:                         $namelink = &Apache::loncommon::aboutmewrapper(
  460:                                     &Apache::loncommon::plainname($request,$dom),
  461:                                     $request,$dom);
  462:                     }
  463:                 } else {
  464:                     my ($cnum,$ownername,$ownerdom,$type,$cdesc);
  465:                     my $queue = 'approval'; 
  466:                     if ($context eq 'pending' || $context eq 'stillpending') {
  467:                         ($cnum,$ownername,$ownerdom,$instcode,$cdesc)=split(/:/,$request,5);
  468:                         $queue = 'pending';                        
  469:                     } else {
  470:                         ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
  471:                         $crstype = $type;
  472:                         if (defined($crstypes{$type})) {
  473:                             $crstype = $crstypes{$type};
  474:                         }
  475:                     }
  476:                     $detailslink='<a href="javascript:opencoursereqdisplay('.
  477:                                   "'$dom','$cnum','$queue'".');">'.$cdesc.'</a>';
  478:                     $approve = $count.':'.$cnum;
  479:                     $reject = $cnum;
  480:                     $namelink = &Apache::loncommon::aboutmewrapper(
  481:                                 &Apache::loncommon::plainname($ownername,$ownerdom),
  482:                                 $ownername,$ownerdom);
  483:                 }
  484:                 unless ($context eq 'pending') {
  485:                     $row = '<td><span class="LC_nobreak"><label>'.
  486:                            '<input type="radio" value="'.$approve.'" name="'.$count.'radioreq" />'.&mt('Approve').'</label>'.
  487:                            '<label>'.('&nbsp;'x2).
  488:                            '<input type="radio" value="'.$reject.'" name="'.$count.'radioreq" />'.&mt('Reject').'</label>'.
  489:                            '<label>'.('&nbsp;'x2).
  490:                            '<input type="radio" value="'."later:".$reject.'" name="'.$count.'radioreq" checked />'.&mt('Decide Later').
  491:                            '</label></span><br /></td>';
  492:                 }
  493:                 $row .= '<td>'.$namelink.'</td>'."\n";
  494:                 if ($context eq 'course') {
  495:                     $row .= '<td>'.$showsec.'</td>'."\n".
  496:                             '<td>'.$showtime.'</td>'."\n";
  497:                 } elsif ($context eq 'requestauthor') {
  498:                     $row .= '<td>'.$showtime.'</td>'."\n";
  499:                 } else { 
  500:                     if ($context eq 'pending' || $context eq 'stillpending') {
  501:                         $row .= '<td>'.$instcode.'</td>'."\n";
  502:                     } else {
  503:                         $row .= '<td>'.$crstype.'</td>'."\n";
  504:                     }
  505:                     $row .= '<td>'.$showtime.'</td>'."\n".
  506:                             '<td>'.$detailslink.'</td>'."\n";
  507:                 }
  508:                 $output .= &Apache::loncommon::start_data_table_row()."\n".
  509:                            $row.
  510:                            &Apache::loncommon::end_data_table_row()."\n";
  511:                 $count ++;
  512:             }
  513:         }
  514:     }
  515:     $output .= &Apache::loncommon::end_data_table();
  516:     return $output;
  517: }
  518: 
  519: sub update_request_queue {
  520:     my ($context,$cdom,$cnum,$coursedesc) = @_;
  521:     my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
  522:         $stucounts,$idx,$classlist,%requesthash,$cid,$domdesc,$now,
  523:         $sender,$approvedmsg,$rejectedmsg,$beneficiary,
  524:         @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
  525:         @processing_errors,@warn_approves,@warn_rejects,@approvals,@warn_dels,
  526:         @rejections,@rejectionerrors,@nopermissions,%courseroles,@toremove,
  527:         %communityroles,%domdefs,%approvalmsg,%rejectionmsg,$crstype,$queue,
  528:         $firsturl);
  529:     my $count=0;
  530:     while (my @course = &Apache::loncommon::get_env_multiple('form.'.$count.'radioreq')) {
  531:         if (@course[0] =~ /^\d+:.*/) {
  532:             push(@approvals,@course[0]);
  533:         } elsif (@course[0] =~ /^later:.*/) {
  534:             #decide later
  535:         } else {
  536:             push(@rejections,@course[0]);
  537:         }
  538:         $count+=1;
  539:     }
  540: 
  541:     $now = time;
  542:     $sender = $env{'user.name'}.':'.$env{'user.domain'};
  543:     if ($context eq 'course') {
  544:         $namespace = 'selfenrollrequests';
  545:         $beneficiary = 'enroller';
  546:         $cid = $env{'request.course.id'};
  547:         $crstype = lc(&Apache::loncommon::course_type());
  548:         $firsturl = &course_portal_url($cnum,$cdom);
  549:         %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
  550:         $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
  551:         $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
  552:         $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
  553:         $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
  554:         $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
  555:         ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
  556:         $approvedmsg = [{
  557:                             mt => 'Your request for enrollment has been approved.',
  558:                         },
  559:                         {
  560:                             mt   => 'Visit [_1] to log-in and access the course',
  561:                             args => [$firsturl],
  562:                         }];
  563:         $rejectedmsg =  [{
  564:                             mt => 'Your request for enrollment has not been approved.',
  565:                         }];
  566:     } elsif ($context eq 'requestauthor') {
  567:         $namespace = 'requestauthorqueue';
  568:         $beneficiary = 'requestauthor';
  569:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
  570:         my %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  571:         if (ref($domdefs{'requestauthor'}) eq 'HASH') {
  572:             if (ref($domdefs{'requestauthor'}{'notify'}) eq 'HASH') {
  573:                 $notifylist = $domdefs{'requestauthor'}{'notify'}{'approval'};
  574:             }
  575:         }
  576:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
  577:         $firsturl = &course_portal_url($domconfiguser,$cdom);
  578:         $approvedmsg = [{
  579:                             mt => 'Your request for authoring space has been approved.',
  580:                         },
  581:                         {
  582:                             mt   => 'Visit [_1] to log-in and select your author role',
  583:                             args => [$firsturl],
  584:                         }];
  585:         $rejectedmsg =  [{
  586:                             mt => 'Your request for authoring space has not been approved.',
  587:                         }];
  588:         $domdesc = &Apache::lonnet::domain($cdom);
  589:     } else {
  590:         $domdesc = &Apache::lonnet::domain($cdom);
  591:         $namespace = 'courserequestqueue';
  592:         $beneficiary = 'courserequestor';
  593:         $queue = 'approval';
  594:         if ($env{'form.queue'} eq 'pending') {
  595:             $queue = 'pending';
  596:         }
  597:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_'.$queue);
  598:         my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
  599:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
  600:             if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') { 
  601:                 $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
  602:             }
  603:         }
  604:         $approvalmsg{'course'} = 
  605:                         [{
  606:                             mt => 'Your course request has been approved.',
  607:                         },
  608:                         {
  609:                             mt   => 'Visit [_1] to log-in and access the course',
  610:                             args => [],
  611:                         }];
  612:         $rejectionmsg{'course'} =
  613:                         [{
  614:                             mt => 'Your course request has not been approved.',
  615:                         }];
  616: 
  617:         $approvalmsg{'community'} = 
  618:                         [{
  619:                             mt => 'Your community request has been approved.',
  620:                         },
  621:                         {
  622:                             mt   => 'Visit [_1] to log-in and access the community',
  623:                             args => [],
  624:                         }];
  625: 
  626:         $rejectionmsg{'community'} = 
  627:                         [{
  628:                             mt => 'Your community request has not been approved.',
  629:                         }];
  630: 
  631:         %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  632:         my @roles = &Apache::lonuserutils::roles_by_context('course');
  633:         foreach my $role (@roles) {
  634:             $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
  635:         }
  636:         foreach my $role (@roles) {
  637:             $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
  638:         }
  639:     }
  640:     foreach my $item (sort {$a <=> $b} @approvals) {
  641:         if ($context eq 'course') {
  642:             my ($num,$uname,$udom,$usec) = split(/:/,$item);
  643:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
  644:             if ($uhome ne 'no_host') {
  645:                 if (exists($requesthash{$uname.':'.$udom})) {
  646:                     if (exists($classlist->{$uname.':'.$udom})) {
  647:                         if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
  648:                             if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
  649:                                 ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
  650:                                 push(@existing,$uname.':'.$udom);
  651:                                 next;
  652:                             }
  653:                         }
  654:                     }
  655:                 } else {
  656:                     push(@missingreq,$uname.':'.$udom);
  657:                     next;
  658:                 }
  659:                 if (!grep(/^\Q$item\E$/,@rejections)) {
  660:                     if ($limit eq 'allstudents') {
  661:                         if ($stucounts->{$limit} >= $cap) {
  662:                             push(@limitexceeded,$uname.':'.$udom);
  663:                             last;
  664:                         }
  665:                     } elsif ($limit eq 'selfenrolled') {
  666:                         if ($stucounts->{$limit} >= $cap) {
  667:                             push(@limitexceeded,$uname.':'.$udom);
  668:                             last;
  669:                         }
  670:                     }
  671:                     my $result =
  672:                         &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
  673:                     if ($result eq 'ok') {
  674:                         push(@completed,$uname.':'.$udom);
  675:                         $stucounts->{'allstudents'} ++;
  676:                         $stucounts->{'selfenrolled'} ++;
  677:                         &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
  678:                                        $cid,$coursedesc,$now,$beneficiary,$sender,
  679:                                        undef,undef,$crstype);
  680:                         my %userrequest = (
  681:                             $cdom.'_'.$cnum => {
  682:                                 timestamp   => $now,
  683:                                 section     => $usec,
  684:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  685:                                 status      => 'approved',
  686:                             }
  687:                         );
  688:                         my $userresult =
  689:                             &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
  690:                         if ($userresult ne 'ok') {
  691:                             push(@warn_approves,$uname.':'.$udom);
  692:                         }
  693:                     } else {
  694:                         push(@processing_errors,$uname.':'.$udom);
  695:                     }
  696:                 }
  697:             } else {
  698:                 push(@invalidusers,$uname.':'.$udom);
  699:             }
  700:         } elsif ($context eq 'requestauthor') {
  701:             my ($num,$uname) = split(/:/,$item);
  702:             my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
  703:             if ($uhome ne 'no_host') {
  704:                 my ($user_is_adv,$user_is_author) = &Apache::lonnet::is_advanced_user($cdom,$uname);
  705:                 if ($user_is_author) {
  706:                     push(@existing,$uname);
  707:                 } elsif (&Apache::lonnet::usertools_access($uname,$cdom,'requestauthor',
  708:                                                            undef,'requestauthor')) {
  709:                     if (&Apache::lonnet::allowed('cau',$cdom)) {
  710:                         if (&Apache::lonnet::assignrole($cdom,$uname,'/'.$cdom.'/','au',undef,time,undef,undef,'requestauthor') eq 'ok') {
  711:                             push(@completed,$uname);
  712:                             unless (&Apache::lonnet::del_dom($namespace,[$uname.'_approval'],$cdom) eq 'ok') {
  713:                                 push(@warn_dels,$uname);
  714:                             }
  715:                             &send_selfserve_notification($uname.':'.$cdom,
  716:                                                          $approvedmsg,undef,undef,$now,
  717:                                                          $beneficiary,$sender);
  718:                             my %userrequest = (
  719:                                 author => {
  720:                                           timestamp   => $now,
  721:                                           adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  722:                                           status      => 'approved',
  723:                                         },
  724:                                 author_status => 'approved',
  725:                             );
  726:                             my $userresult =
  727:                                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$uname);
  728:                             if ($userresult ne 'ok') {
  729:                                 push(@warn_approves,$item);
  730:                             }
  731:                         } else {
  732:                             push(@processing_errors,$uname);
  733:                         }
  734:                     } else {
  735:                         push(@nopermissions,$uname);
  736:                     }
  737:                 } else {
  738:                     push(@nopermissions,$uname);
  739:                 }
  740:             } else {
  741:                 push(@invalidusers,$uname.':'.$cdom);
  742:             }
  743:             push(@toremove,(@invalidusers,@nopermissions));
  744:         } else {
  745:             my ($num,$cnum) = split(':',$item);
  746:             if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
  747:                 if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  748:                     my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
  749:                     my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
  750:                     $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
  751:                     my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
  752:                     my $longroles = \%courseroles;
  753:                     if ($crstype eq 'community') {
  754:                         $longroles = \%communityroles;
  755:                     }
  756:                     my $cancreate;
  757:                     if ($cdom eq $ownerdom) {
  758:                         if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
  759:                                                               undef,'requestcourses')) {
  760:                             $cancreate = 1;
  761:                         }
  762:                     } else {
  763:                         my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
  764:                         if ($userenv{'reqcrsotherdom.'.$crstype}) {
  765:                             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
  766:                             if (grep(/^\Q$cdom\E:/,@doms)) {
  767:                                 $cancreate = 1;
  768:                             }
  769:                         }
  770:                     }
  771:                     if ($cancreate) {
  772:                         my $requestkey = $cdom.'_'.$cnum;
  773:                         my %history = 
  774:                             &Apache::lonnet::restore($requestkey,'courserequests',
  775:                                                      $ownerdom,$ownername);
  776:                         if ((ref($history{'details'}) eq 'HASH') && 
  777:                             ($history{'disposition'} eq $queue)) {
  778:                             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
  779:                             my $result = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
  780:                                                       \$newusermsg,\$addresult,\$enrollcount,
  781:                                                       \$response,\$keysmsg,\%domdefs,$longroles);
  782:                             if ($result eq 'created') {
  783:                                 if ($crstype eq 'community') {
  784:                                     $approvedmsg = $approvalmsg{'community'};
  785:                                 } else {
  786:                                     $approvedmsg = $approvalmsg{'course'};
  787:                                 }
  788:                                 my $firsturl = &course_portal_url($cnum,$cdom);
  789:                                 if (ref($approvedmsg) eq 'ARRAY') {
  790:                                     if (ref($approvedmsg->[1]) eq 'HASH') {
  791:                                         $approvedmsg->[1]->{'args'} = [$firsturl];
  792:                                     }
  793:                                 }
  794:                                 push(@completed,$cnum);
  795:                                 
  796:                                 unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
  797:                                     push(@warn_dels,$cnum);
  798:                                 }
  799:                                 &send_selfserve_notification($ownername.':'.$ownerdom,
  800:                                               $approvedmsg,$cid,$coursedesc,$now,
  801:                                               $beneficiary,$sender,undef,undef,$crstype);
  802:                                 my %reqhash = (
  803:                                                 reqtime     => $history{'reqtime'},
  804:                                                 crstype     => $history{'crstype'},
  805:                                                 details     => $history{'details'},
  806:                                                 disposition => $history{'disposition'},
  807:                                                 status      => 'created',
  808:                                                 adjudicator => $env{'user.name'}.':'.
  809:                                                                $env{'user.domain'},
  810:                                               );
  811:                                 my $userresult =
  812:                                     &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
  813:                                                    'courserequests',$ownerdom,$ownername);
  814:                                 if ($userresult eq 'ok') {
  815:                                     my %status = (
  816:                                                    'status:'.$cdom.':'.$cnum => 'created'
  817:                                                  );
  818:                                     my $statusresult = 
  819:                                         &Apache::lonnet::put('courserequests',\%status,
  820:                                                              $ownerdom,$ownername);
  821:                                     if ($statusresult ne 'ok') {
  822:                                         push(@warn_approves,$cnum);
  823:                                     }
  824:                                 }
  825:                                 if ($userresult ne 'ok') {
  826:                                     push(@warn_approves,$cnum);
  827:                                 }
  828:                             } else {
  829:                                 push(@processing_errors,$cnum);
  830:                             }
  831:                         } else {
  832:                             push(@processing_errors,$cnum);
  833:                         }
  834:                     } else {
  835:                         push(@nopermissions,$cnum);
  836:                     }
  837:                 } else {
  838:                     push(@existing,$cnum);
  839:                 }
  840:             } else {
  841:                 push(@missingreq,$cnum);
  842:             }
  843:         }
  844:     }
  845:     my @changes = (@completed,@rejections);
  846:     if ($context eq 'domain') {
  847:         @changes = map {$_.'_'.$queue} (@changes);
  848:     } elsif ($context eq 'requestauthor') {
  849:         @changes = map {$_.'_approval'} (@changes);
  850:     }
  851:     if (@rejections) {
  852:         foreach my $item (@rejections) {
  853:             if (($context eq 'course') || ($context eq 'requestauthor')) {
  854:                 my ($user,$uname,$udom,%userrequest,$key);
  855:                 if ($context eq 'requestauthor') {
  856:                     $uname = $item;
  857:                     $udom = $cdom;
  858:                     $user = $uname.':'.$udom;
  859:                     $key = 'author';
  860:                 } else {
  861:                     $user = $item;
  862:                     ($uname,$udom) = split(/:/,$user);
  863:                     $key = $cdom.'_'.$cnum;
  864:                 }
  865:                 &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
  866:                                              $now,$beneficiary,$sender,undef,undef,
  867:                                              $crstype);
  868:                 %userrequest = (
  869:                     $key => {
  870:                         timestamp   => $now,
  871:                         adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  872:                         status      => 'rejection',
  873:                     }
  874:                 );
  875:                 if ($context eq 'requestauthor') {
  876:                     $userrequest{'author_status'} = 'rejection';  
  877:                 }
  878:                 my $userresult =
  879:                     &Apache::lonnet::put('requestauthor',\%userrequest,$udom,$uname);
  880:                 if ($userresult ne 'ok') {
  881:                     push(@warn_rejects,$item);
  882:                 }
  883:             } else {
  884:                 my $cnum = $item;
  885:                 if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
  886:                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  887:                         my $requestkey = $cdom.'_'.$cnum;
  888:                         my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
  889:                         my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
  890:                         my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
  891:                         $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
  892:                         if ($crstype eq 'community') {
  893:                             $rejectedmsg = $rejectionmsg{'community'};
  894:                         } else {
  895:                             $rejectedmsg = $rejectionmsg{'course'};
  896:                         }
  897:                         &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
  898:                                                      $cid,$coursedesc,$now,$beneficiary,
  899:                                                      $sender,undef,undef,$crstype);
  900:                         my %history =
  901:                             &Apache::lonnet::restore($requestkey,'courserequests',
  902:                                                      $ownerdom,$ownername);
  903:                         if ((ref($history{'details'}) eq 'HASH') &&
  904:                             ($history{'disposition'} eq $queue)) {
  905:                             my %reqhash = (
  906:                                             reqtime     => $history{'reqtime'},
  907:                                             crstype     => $history{'crstype'},
  908:                                             details     => $history{'details'},
  909:                                             disposition => $history{'disposition'},
  910:                                             status      => 'rejected',
  911:                                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  912:                                               );
  913:                             my $userresult =
  914:                                 &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
  915:                                                 'courserequests',$ownerdom,$ownername);
  916:                             if ($userresult eq 'ok') {
  917:                                 my %status = (
  918:                                                'status:'.$cdom.':'.$cnum => 'rejected'
  919:                                              );
  920:                                 my $statusresult =
  921:                                     &Apache::lonnet::put('courserequests',\%status,
  922:                                                          $ownerdom,$ownername);
  923:                                 if ($statusresult ne 'ok') {
  924:                                     push(@warn_rejects,$cnum);
  925:                                 }
  926:                             } else {
  927:                                 push(@warn_rejects,$cnum);
  928:                             }
  929:                             unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
  930:                                 push(@warn_dels,$cnum);
  931:                             }
  932:                         } else {
  933:                             push(@warn_rejects,$cnum);
  934:                         }
  935:                     } else {
  936:                         push(@existing,$cnum);
  937:                     }
  938:                 } else {
  939:                     push(@rejectionerrors,$cnum);
  940:                 }
  941:             }
  942:         }
  943:     }
  944:     if (@toremove) {
  945:         foreach my $item (@toremove) {
  946:             my %userrequest = (
  947:                 author => {
  948:                             timestamp   => $now,
  949:                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  950:                             status      => 'deleted',
  951:                           },
  952:                           author_status => 'deleted',
  953:             );
  954:             &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$item);
  955:         }
  956:         @toremove = map {$_.'_approval'} (@toremove);
  957:         my $delresult = &Apache::lonnet::del_dom($namespace,\@toremove,$cdom);
  958:     }
  959:     if (@changes) {
  960:         my $delresult;
  961:         if ($context eq 'course') {
  962:             $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
  963:         } else {
  964:             $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
  965:         }
  966:         if ($delresult eq 'ok') {
  967:             my $namelink =
  968:                 &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
  969:             my ($chgmsg,$approvedlist,$rejectedlist);
  970:             if ($context eq 'course') {
  971:                 $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
  972:                 if (@completed) {
  973:                     $approvedlist = join("\n",@completed);
  974:                     if ($crstype eq 'community') {
  975:                         $output .= '<p>'.&mt('The following were enrolled in the community:').'<ul>';
  976:                     } else {
  977:                         $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
  978:                     }
  979:                     foreach my $user (@completed) {
  980:                         my ($uname,$udom) = split(/:/,$user);
  981:                         my $userlink =
  982:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
  983:                         $output .= '<li>'.$userlink.'</li>';
  984:                     }
  985:                     $output .= '</ul></p>';
  986:                 }
  987:                 if (@rejections) {
  988:                     $rejectedlist = join("\n",@rejections);
  989:                     $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
  990:                     foreach my $user (@rejections) {
  991:                         $output .= '<li>'.$user.'</li>';
  992:                     }
  993:                     $output .= '</ul></p>';
  994:                 }
  995:                 if ($notifylist ne '') {
  996:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
  997:                                                  $now,'coursemanagers',$sender,
  998:                                                  $approvedlist,$rejectedlist,$crstype);
  999:                 }
 1000:             } elsif ($context eq 'requestauthor') {
 1001:                 $chgmsg = "'Action was taken on the following authoring space requests by [_1].',$namelink";
 1002:                 if (@completed) {
 1003:                     $approvedlist = join("\n",@completed);
 1004:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
 1005:                     foreach my $uname (@completed) {
 1006:                         my $userlink =
 1007:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1008:                         $output .= '<li>'.$userlink.'</li>';
 1009:                         
 1010:                     }
 1011:                     $output .= '</ul></p>';
 1012:                 }
 1013:                 if (@rejections) {
 1014:                     $rejectedlist = join("\n",@rejections);
 1015:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1016:                     foreach my $uname (@rejections) {
 1017:                         my $userlink =
 1018:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1019:                         $output .= '<li>'.$userlink.'</li>';
 1020:                     }
 1021:                     $output .= '</ul></p>';
 1022:                 }
 1023:                 if ($notifylist ne '') {
 1024:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
 1025:                                                  $now,'authormanagers',$sender,
 1026:                                                  $approvedlist,$rejectedlist);
 1027:                 }
 1028:             } else {
 1029:                 $chgmsg = "'Action was taken on the following course and community requests by [_1].',$namelink";
 1030:                 if (@completed) {
 1031:                     $approvedlist = join("\n",@completed);
 1032:                     $output .= '<p>'.&mt('The following courses/communities were created:').'<ul>';
 1033:                     foreach my $cnum (@completed) {
 1034:                         my $showcourse;
 1035:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1036:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1037:                         } else {
 1038:                             $showcourse = $cnum;
 1039:                         }
 1040:                         my $syllabuslink =
 1041:                             &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
 1042:                         $output .= '<li>'.$syllabuslink.'</li>';
 1043:                     }
 1044:                     $output .= '</ul></p>';
 1045:                 }
 1046:                 if (@rejections) {
 1047:                     $rejectedlist = join("\n",@rejections);
 1048:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1049:                     foreach my $cnum (@rejections) {
 1050:                         my $showcourse;
 1051:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1052:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1053:                         } else {
 1054:                             $showcourse = $cnum;
 1055:                         }
 1056:                         $output .= '<li>'.$showcourse.'</li>';
 1057:                     }
 1058:                     $output .= '</ul></p>';
 1059:                 }
 1060:                 if ($notifylist ne '') {
 1061:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
 1062:                                                  $now,'domainmanagers',$sender,
 1063:                                                  $approvedlist,$rejectedlist,$crstype);
 1064:                 }
 1065:             }
 1066:         }
 1067:     }
 1068:     if (@existing) {
 1069:         if ($context eq 'course') {
 1070:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
 1071:             foreach my $user (@existing) {
 1072:                 $output .= '<li>'.$user.'</li>';
 1073:             }
 1074:             $output .= '</ul></p>';
 1075:         } elsif ($context eq 'requestauthor') {
 1076:             $output .= '<p>'.&mt('Authoring space requests from the following users were deleted because one already exists:').'<ul>';
 1077:             foreach my $uname (@existing) {
 1078:                 my $userlink =
 1079:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1080:                 $output .= '<li>'.$userlink.'</li>';
 1081:             }
 1082:             $output .= '</ul></p>';
 1083:         } else {
 1084:             $output .= '<p>'.&mt('The following course/community creation requests were deleted because the course or community has already been created:').'<ul>';
 1085:             foreach my $cnum (@existing) {
 1086:                 my $showcourse;
 1087:                 my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 1088:                 if ($coursehash{'description'} ne '') {
 1089:                     $showcourse = $coursehash{'description'};
 1090:                 } else {
 1091:                     $showcourse = $cnum;
 1092:                 }
 1093:                 $output .= '<li>'.$showcourse.'</li>';
 1094:             }
 1095:             $output .= '</ul></p>';
 1096:         }
 1097:     }
 1098:     if (@missingreq) {
 1099:         if ($context eq 'course') {
 1100:             $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
 1101:             foreach my $user (@missingreq) {
 1102:                 $output .= '<li>'.$user.'</li>';
 1103:             }
 1104:             $output .= '</ul></p>';
 1105:         } elsif ($context eq 'requestauthor') {
 1106:             $output .= '<p>'.&mt('The following requests were ignored because the request is no longer in the queue:').'<ul>';
 1107:             foreach my $uname (@missingreq) {
 1108:                 my $userlink =
 1109:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1110:                 $output .= '<li>'.$userlink.'</li>';
 1111:             }
 1112:             $output .= '</ul></p>';
 1113:         } else {
 1114:             $output .= '<p>'.&mt('The following course/community creation requests were ignored because the request is no longer in the queue:').'<ul>';
 1115:             foreach my $cnum (@missingreq) {
 1116:                 $output .= '<li>'.$cnum.'</li>';
 1117:             }
 1118:             $output .= '</ul></p>';
 1119:         }
 1120:     }
 1121:     if (@invalidusers) {
 1122:         if ($context eq 'course') {
 1123:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1124:             foreach my $user (@invalidusers) {
 1125:                 $output .= '<li>'.$user.'</li>';
 1126:             }
 1127:             $output .= '</ul></p>';
 1128:         } elsif ($context eq 'requestauthor') {
 1129:             $output .= '<p>'.&mt('The following authoring space requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1130:             foreach my $uname (@invalidusers) {
 1131:                 my $userlink =
 1132:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1133:                 $output .= '<li>'.$userlink.'</li>';
 1134:             }
 1135:             $output .= '</ul></p>';
 1136:         }
 1137:     }
 1138:     if (@limitexceeded) {
 1139:         if ($context eq 'course') {
 1140:             $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
 1141:             foreach my $user (@limitexceeded) {
 1142:                 $output .= '<li>'.$user.'</li>';
 1143:             }
 1144:             $output .= '</ul></p>';
 1145:         }
 1146:     }
 1147:     if (@nopermissions) {
 1148:         if ($context eq 'course') {
 1149:             $output .= '<p>'.&mt('The following course/community creation requests could not be processed because the owner does not have rights to create this type of course:').'<ul>';
 1150:             foreach my $cnum (@nopermissions) {
 1151:                 my $showcourse;
 1152:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1153:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1154:                 } else {
 1155:                     $showcourse = $cnum;
 1156:                 }
 1157:                 $output .= '<li>'.$showcourse.'</li>';
 1158:             }
 1159:             $output .= '</ul></p>';
 1160:         } elsif ($context eq 'requestauthor') {
 1161:             $output .= '<p>'.&mt('The following requests could not be processed because the requestor does not have rights to request an authoring space:').'<ul>';
 1162:             foreach my $uname (@nopermissions) {
 1163:                 my $userlink =
 1164:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1165:                 $output .= '<li>'.$userlink.'</li>';
 1166:             }
 1167:             $output .= '</ul></p>';
 1168:         }
 1169:     }
 1170:     if (@processing_errors) {
 1171:         if ($context eq 'course') {
 1172:             $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
 1173:             foreach my $user (@processing_errors) {
 1174:                 $output .= '<li>'.$user.'</li>';
 1175:             }
 1176:             $output .= '</ul></p>';
 1177:         } elsif ($context eq 'requestauthor') {
 1178:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
 1179:             foreach my $uname (@processing_errors) {
 1180:                 my $userlink =
 1181:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1182:                 $output .= '<li>'.$userlink.'</li>';
 1183:             }
 1184:             $output .= '</ul></p>';
 1185:         } else {
 1186:             $output .= '<p>'.&mt('The following course/community creation requests could not be processed because an error occurred:').'<ul>';
 1187:             foreach my $cnum (@processing_errors) {
 1188:                 my $showcourse;
 1189:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1190:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1191:                 } else {
 1192:                     $showcourse = $cnum;
 1193:                 }
 1194:                 $output .= '<li>'.$showcourse.'</li>';
 1195:             }
 1196:             $output .= '</ul></p>';
 1197:         }
 1198:     }
 1199:     if (@rejectionerrors) {
 1200:         $output .= '<p>'.&mt('The following course/community creation request rejections could not be fully processed because an error occurred:').'<ul>';
 1201:         foreach my $cnum (@rejectionerrors) {
 1202:             my $showcourse;
 1203:             if (ref($requesthash{$cnum.'_'.$queue})) {
 1204:                 $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1205:             } else {
 1206:                 $showcourse = $cnum;
 1207:             }
 1208:             $output .= '<li>'.$showcourse.'</li>';
 1209:         }
 1210:         $output .= '</ul></p>';
 1211:     }
 1212:     if (@warn_approves || @warn_rejects) {
 1213:         if ($context eq 'course') {
 1214:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
 1215:             foreach my $user (@warn_approves) {
 1216:                 $output .= '<li>'.$user.'</li>';
 1217:             }
 1218:             $output .= '</ul></p>';
 1219:         } elsif ($context eq 'requestauthor') {
 1220:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own author request record:").'<ul>';
 1221:             foreach my $uname (@warn_approves,@warn_rejects) {
 1222:                 my $userlink =
 1223:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1224:                 $output .= '<li>'.$userlink.'</li>';
 1225:             }
 1226:             $output .= '</ul></p>';
 1227:         } else {
 1228:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when updating the requestor's own requests record:").'<ul>';
 1229:             foreach my $cnum (@warn_approves,@warn_rejects) {
 1230:                 my $showcourse;
 1231:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1232:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1233:                 } else {
 1234:                     $showcourse = $cnum;
 1235:                 }
 1236:                 $output .= '<li>'.$showcourse.'</li>';
 1237:             }
 1238:             $output .= '</ul></p>';
 1239:         }
 1240:     }
 1241:     if (@warn_dels) {
 1242:         if ($context eq 'requestauthor') {
 1243:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
 1244:             foreach my $uname (@warn_dels) {
 1245:                 my $userlink =
 1246:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1247:                 $output .= '<li>'.$userlink.'</li>';
 1248:             }
 1249:             $output .= '</ul></p>';
 1250:         } else {
 1251:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when removing requests for the following from the pending queue:").'<ul>';
 1252:             foreach my $cnum (@warn_dels) {
 1253:                 my $showcourse;
 1254:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1255:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1256:                 } else {
 1257:                     $showcourse = $cnum;
 1258:                 }
 1259:                 $output .= '<li>'.$showcourse.'</li>';
 1260:             }
 1261:             $output .= '</ul></p>';
 1262:         }
 1263:     }
 1264:     return $output;
 1265: }
 1266: 
 1267: sub course_portal_url {
 1268:     my ($cnum,$cdom) = @_;
 1269:     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
 1270:     my $hostname = &Apache::lonnet::hostname($chome);
 1271:     my $protocol = $Apache::lonnet::protocol{$chome};
 1272:     $protocol = 'http' if ($protocol ne 'https');
 1273:     my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
 1274:     my $firsturl;
 1275:     if ($domdefaults{'portal_def'}) {
 1276:         $firsturl = $domdefaults{'portal_def'};
 1277:     } else {
 1278:         $firsturl = $protocol.'://'.$hostname;
 1279:     }
 1280:     return $firsturl;
 1281: }
 1282: 
 1283: sub get_student_counts {
 1284:     my ($cdom,$cnum) = @_;
 1285:     my (%idx,%stucounts);
 1286:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1287:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
 1288:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1289:     while (my ($student,$data) = each(%$classlist)) {
 1290:         if (($data->[$idx{'status'}] eq 'Active') ||
 1291:             ($data->[$idx{'status'}] eq 'Future')) {
 1292:             if ($data->[$idx{'type'}] eq 'selfenroll') {
 1293:                 $stucounts{'selfenroll'} ++;
 1294:             }
 1295:             $stucounts{'allstudents'} ++;
 1296:         }
 1297:     }
 1298:     return (\%stucounts,\%idx,$classlist);
 1299: }
 1300: 
 1301: sub course_creation {
 1302:     my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
 1303:         $keysmsg,$domdefs,$longroles) =  @_;
 1304:     unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') && 
 1305:             (ref($longroles) eq 'HASH')) {
 1306:         return 'error: Invalid request';
 1307:     }
 1308:     my ($result,$ownername,$ownerdom);
 1309:     my $crstype = $details->{'crstype'};
 1310:     if ($context eq 'domain') {
 1311:         $ownername = $details->{'owner'};
 1312:         $ownerdom  = $details->{'domain'};
 1313:     } else {
 1314:         $ownername = $env{'user.name'};
 1315:         $ownerdom  = $env{'user.domain'};
 1316:     }
 1317:     my $owneremail;
 1318:     my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
 1319:     foreach my $email ('permanentemail','critnotification','notification') {
 1320:         $owneremail = $emails{$email};
 1321:         last if ($owneremail ne '');
 1322:     }
 1323:     my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
 1324:     my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
 1325:                   \%reqdetails,$longroles,$logmsg,$newusermsg,$addresult,
 1326:                   $enrollcount,$output,$keysmsg,$ownerdom,$ownername,$cnum,$crstype);
 1327:     if ($cid eq "/$dom/$cnum") {
 1328:         $result = 'created';
 1329:     } else {
 1330:         $result = 'error: '.$cid;
 1331:     }
 1332:     return $result;
 1333: }
 1334: 
 1335: sub build_batchcreatehash {
 1336:     my ($dom,$context,$details,$owneremail,$domdefs) = @_;
 1337:     my %batchhash;
 1338:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
 1339:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
 1340:         my $emailenc = &escape($owneremail);
 1341:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
 1342:         foreach my $item (@items) {
 1343:             $batchhash{$item} = $details->{$item};
 1344:         }
 1345:         $batchhash{'title'} = $details->{'cdescr'};
 1346:         $batchhash{'coursecode'} = $details->{'instcode'};
 1347:         $batchhash{'emailenc'} = $emailenc;
 1348:         $batchhash{'adds'} = $details->{'autoadds'};
 1349:         $batchhash{'drops'} = $details->{'autodrops'};
 1350:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
 1351:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
 1352:         if ($details->{'crstype'} eq 'community') {
 1353:             $batchhash{'crstype'} = 'Community';
 1354:         } else {
 1355:             $batchhash{'crstype'} = 'Course';
 1356:         }
 1357:         my ($owner_firstname,$owner_lastname);
 1358:         if ($context eq 'domain') {
 1359:             my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
 1360:                                                            $details->{'owner'},
 1361:                                                            'firstname','lastname');
 1362:             $owner_firstname = $userenv{'firstname'};
 1363:             $owner_lastname = $userenv{'lastname'};
 1364:         } else {
 1365:             $owner_firstname = $env{'environment.firstname'};
 1366:             $owner_lastname = $env{'environment.lastname'};
 1367:         }
 1368:         if (ref($details->{'personnel'}) eq 'HASH') {
 1369:             %{$batchhash{'users'}} = %{$details->{'personnel'}};
 1370:             if (ref($batchhash{'users'}) eq 'HASH') {  
 1371:                 foreach my $userkey (keys(%{$batchhash{'users'}})) {
 1372:                     if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
 1373:                         if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
 1374:                             foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
 1375:                                 my $start = '';
 1376:                                 my $end = '';
 1377:                                 if ($role eq 'st') {
 1378:                                     $start = $details->{'accessstart'};
 1379:                                     $end = $details->{'accessend'};
 1380:                                 }
 1381:                                 $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
 1382:                                 $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
 1383:                             } 
 1384:                         }
 1385:                     }
 1386:                 }
 1387:             }
 1388:         }
 1389:         $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
 1390:         $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
 1391:         $batchhash{'users'}{$owner}{emailenc} = $emailenc;
 1392:         $batchhash{'users'}{$owner}{owneremail} = $owneremail;
 1393:     }
 1394:     return %batchhash;
 1395: }
 1396: 
 1397: sub can_clone_course {
 1398:     my ($uname,$udom,$clonecrs,$clonedom,$crstype) = @_;
 1399:     my $canclone;
 1400:     my $ccrole = 'cc';
 1401:     if ($crstype eq 'community') {
 1402:         $ccrole = 'co';
 1403:     }
 1404:     my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
 1405:                                                   [$ccrole],[$clonedom]);
 1406:     if (exists($roleshash{$clonecrs.':'.$clonedom.':'.$ccrole})) {
 1407:         $canclone = 1;
 1408:     } else {
 1409:         my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
 1410:         my $cloners = $courseenv{'cloners'};
 1411:         if ($cloners ne '') {
 1412:             my @cloneable = split(',',$cloners);
 1413:             if (grep(/^\*$/,@cloneable)) {
 1414:                 $canclone = 1;
 1415:             }
 1416:             if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
 1417:                 $canclone = 1;
 1418:             }
 1419:             if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
 1420:                 $canclone = 1;
 1421:             }
 1422:         }
 1423:         unless ($canclone) {
 1424:             if (&Apache::lonnet::is_course_owner($clonedom,$clonecrs,$uname,$udom)) {
 1425:                 $canclone = 1;
 1426:             }
 1427:         }
 1428:     }
 1429:     return $canclone;
 1430: }
 1431: 
 1432: sub get_processtype {
 1433:     my ($context,$uname,$udom,$isadv,$dom,$crstype,$inststatuses,$domconfig) = @_;
 1434:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
 1435:     if ($uname eq '' || $udom eq '') {
 1436:         $uname = $env{'user.name'};
 1437:         $udom = $env{'user.domain'};
 1438:         $isadv = $env{'user.adv'};
 1439:     }
 1440:     my (%userenv,%settings,$val,@options,$envkey);
 1441:     if ($context eq 'course') {
 1442:         @options = ('autolimit','validate','approval');
 1443:         $envkey = 'requestcourses.'.$crstype;
 1444:         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
 1445:             if (ref($domconfig->{'requestcourses'}->{$crstype}) eq 'HASH') {
 1446:                 %settings = %{$domconfig->{'requestcourses'}->{$crstype}};
 1447:             }
 1448:         }
 1449:     } else {
 1450:         @options = ('automatic','approval');
 1451:         $envkey = 'requestauthor';
 1452:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') { 
 1453:             %settings = %{$domconfig->{'requestauthor'}};
 1454:         }
 1455:     }
 1456:     if (($dom eq $udom) || ($context eq 'requestauthor')) {
 1457:         %userenv =
 1458:             &Apache::lonnet::userenvironment($udom,$uname,$envkey,'inststatus');
 1459:         if ($userenv{$envkey}) {
 1460:             $val = $userenv{$envkey};
 1461:             @{$inststatuses} = ('_custom_');
 1462:         } else {
 1463:             my %alltasks;
 1464:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
 1465:                 $val = $settings{$crstype}{'_LC_adv'};
 1466:                 @{$inststatuses} = ('_LC_adv_');
 1467:             } else {
 1468:                 if ($userenv{'inststatus'} ne '') {
 1469:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
 1470:                 } else {
 1471:                     @{$inststatuses} = ('default');
 1472:                 }
 1473:                 foreach my $status (@{$inststatuses}) {
 1474:                     if (exists($settings{$status})) {
 1475:                         my $value = $settings{$status};
 1476:                         next unless ($value);
 1477:                         unless (exists($alltasks{$value})) {
 1478:                             if (ref($alltasks{$value}) eq 'ARRAY') {
 1479:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
 1480:                                     push(@{$alltasks{$value}},$status);
 1481:                                 }
 1482:                             } else {
 1483:                                 @{$alltasks{$value}} = ($status);
 1484:                             }
 1485:                         }
 1486:                     }
 1487:                 }
 1488:                 my $maxlimit = 0;
 1489:                 if ($context eq 'course') {
 1490:                     foreach my $key (sort(keys(%alltasks))) {
 1491:                         if ($key =~ /^autolimit=(\d*)$/) {
 1492:                             if ($1 eq '') {
 1493:                                 $val ='autolimit=';
 1494:                                 last;
 1495:                             } elsif ($1 > $maxlimit) {
 1496:                                 $maxlimit = $1;
 1497:                             }
 1498:                         }
 1499:                     }
 1500:                 }
 1501:                 if (($context eq 'requestauthor') || (!$val)) {
 1502:                     if ($context eq 'course' && $maxlimit) {
 1503:                         $val = 'autolimit='.$maxlimit;
 1504:                     } else {
 1505:                         foreach my $option (@options) {
 1506:                             if ($alltasks{$option}) {
 1507:                                 $val = $option;
 1508:                                 last;
 1509:                             }
 1510:                         }
 1511:                     }
 1512:                 }
 1513:             }
 1514:         }
 1515:     } else {
 1516:         %userenv = &Apache::lonnet::userenvironment($udom,$uname,'reqcrsotherdom.'.$crstype);
 1517:         if ($userenv{'reqcrsotherdom.'.$crstype}) {
 1518:             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
 1519:             my $optregex = join('|',@options);
 1520:             foreach my $item (@doms) {
 1521:                 my ($extdom,$extopt) = split(':',$item);
 1522:                 if ($extdom eq $dom) {
 1523:                     if ($extopt =~ /^($optregex)(=?\d*)$/) {
 1524:                         $val = $1.$2;
 1525:                     }
 1526:                     last;
 1527:                 }
 1528:             }
 1529:             @{$inststatuses} = ('_external_');
 1530:         }
 1531:     }
 1532:     return $val;
 1533: }
 1534: 
 1535: sub queued_selfenrollment {
 1536:     my ($notitle) = @_;
 1537:     my $output;
 1538:     my %selfenrollrequests = &Apache::lonnet::dump('selfenrollrequests');
 1539:     my %reqs_by_date;
 1540:     foreach my $item (keys(%selfenrollrequests)) {
 1541:         if (ref($selfenrollrequests{$item}) eq 'HASH') {
 1542:             if ($selfenrollrequests{$item}{'status'} eq 'request') {
 1543:                 if ($selfenrollrequests{$item}{'timestamp'}) {
 1544:                     push(@{$reqs_by_date{$selfenrollrequests{$item}{'timestamp'}}},$item);
 1545:                 }
 1546:             }
 1547:         }
 1548:     }
 1549:     if (keys(%reqs_by_date)) {
 1550:         unless ($notitle) {
 1551:             $output .= '<b>'.&mt('Enrollment requests pending Course Coordinator approval').'</b><br />';
 1552:         }
 1553:         $output .= &Apache::loncommon::start_data_table().
 1554:                    &Apache::loncommon::start_data_table_header_row().
 1555:                    '<th>'.&mt('Date requested').'</th><th>'.&mt('Course title').'</th>'.
 1556:                    '<th>'.&mt('User role').'</th><th>'.&mt('Section').'</th>'.
 1557:                    &Apache::loncommon::end_data_table_header_row();
 1558:         my @sorted = sort { $a <=> $b } (keys(%reqs_by_date));
 1559:         foreach my $item (@sorted) {
 1560:             if (ref($reqs_by_date{$item}) eq 'ARRAY') {
 1561:                 foreach my $crs (@{$reqs_by_date{$item}}) {
 1562:                     my %courseinfo = &Apache::lonnet::coursedescription($crs);
 1563:                     my $usec = $selfenrollrequests{$crs}{'section'};
 1564:                     my $rolename = &Apache::lonnet::plaintext('st',$courseinfo{'type'},$crs);
 1565:                     if ($usec eq '') {
 1566:                         $usec = &mt('No section');
 1567:                     }
 1568:                     $output .= &Apache::loncommon::start_data_table_row().
 1569:                                '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
 1570:                                '<td>'.$courseinfo{'description'}.'</td>'.
 1571:                                '<td>'.$rolename.'</td><td>'.$usec.'</td>'.
 1572:                                &Apache::loncommon::end_data_table_row();
 1573:                 }
 1574:             }
 1575:         }
 1576:         $output .= &Apache::loncommon::end_data_table();
 1577:     }
 1578:     return $output;
 1579: }
 1580: 
 1581: sub update_coursereq_status {
 1582:     my ($reqhash,$dom,$cnum,$reqstatus,$context,$udom,$uname) = @_;
 1583:     my ($storeresult,$statusresult,$output);
 1584:     my $requestkey = $dom.'_'.$cnum;
 1585:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 1586:         $storeresult = &Apache::lonnet::store_userdata($reqhash,$requestkey,
 1587:                                                        'courserequests',$udom,$uname);
 1588:         if ($storeresult eq 'ok') {
 1589:             my %status = (
 1590:                              'status:'.$dom.':'.$cnum => $reqstatus,
 1591:                          );
 1592:             $statusresult = &Apache::lonnet::put('courserequests',\%status,$udom,$uname);
 1593:         }
 1594:     } else {
 1595:         $storeresult = 'error: invalid requestkey format';
 1596:     }
 1597:     if ($storeresult ne 'ok') {
 1598:         $output = &mt('An error occurred saving a record of the details of your request: [_1].',$storeresult);
 1599:         if ($context eq 'domain') {
 1600:             $output .= "\n";  
 1601:         } else {
 1602:             $output =  '<span class="LC_warning">'.$output.'</span><br />';
 1603:         }
 1604:         &Apache::lonnet::logthis("Error saving course request - $requestkey for $uname:$udom - $storeresult");
 1605:     } elsif ($statusresult ne 'ok') {
 1606:         $output = &mt('An error occurred saving a record of the status of your request: [_1].',$statusresult);
 1607:         if ($context eq 'domain') {
 1608:             $output .= "\n";
 1609:         } else {
 1610:             $output = '<span class="LC_warning">'.$output.'</span><br />';
 1611:         }
 1612:         &Apache::lonnet::logthis("Error saving course request status for $requestkey (for $uname:$udom) - $statusresult");
 1613:     }
 1614:     return ($storeresult,$output);
 1615: }
 1616: 
 1617: sub process_official_reqs {
 1618:     my ($context,$dom,$dcname,$dcdom) = @_;
 1619:     my $reqsnamespace = 'courserequestqueue';
 1620:     my %requesthash =
 1621:         &Apache::lonnet::dump_dom($reqsnamespace,$dom,'_pending');
 1622:     my (%newcids,%longroles,%stillpending);
 1623:     my @courseroles = ('cc','in','ta','ep','ad','st');
 1624:     foreach my $role (@courseroles) {
 1625:         $longroles{$role}=&Apache::lonnet::plaintext($role);
 1626:     }
 1627:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 1628:     my ($output,$linefeed);
 1629:     if ($context eq 'auto') {
 1630:         $linefeed = "\n";
 1631:     } else {
 1632:         $linefeed = '<br />'."\n";
 1633:     }
 1634:     foreach my $key (keys(%requesthash)) {
 1635:         my ($cnum,$status) = split('_',$key);
 1636:         next if (&Apache::lonnet::homeserver($cnum,$dom) ne 'no_host');
 1637:         if (ref($requesthash{$key}) eq 'HASH') {
 1638:             my $ownername = $requesthash{$key}{'ownername'};
 1639:             my $ownerdom = $requesthash{$key}{'ownerdom'};
 1640:             next if (&Apache::lonnet::homeserver($ownername,$ownerdom) eq 'no_host');
 1641:             my $inststatus;
 1642:             my %userenv =
 1643:                 &Apache::lonnet::get('environment',['inststatus'],
 1644:                                      $ownerdom,$ownername);
 1645:             my ($tmp) = keys(%userenv);
 1646:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 1647:                 $inststatus = $userenv{'inststatus'};
 1648:             } else {
 1649:                 undef(%userenv);
 1650:             }
 1651:             my $reqkey = $dom.'_'.$cnum;
 1652:             my %history = &Apache::lonnet::restore($reqkey,'courserequests',
 1653:                                                    $ownerdom,$ownername);
 1654:             if (ref($history{'details'}) eq 'HASH') {
 1655:                 my $instcode = $history{'details'}{'instcode'};
 1656:                 my $crstype = $history{'details'}{'crstype'};
 1657:                 my $reqtime = $history{'details'}{'reqtime'};
 1658:                 my $cdescr = $history{'details'}{'cdescr'};
 1659:                 my @currsec;
 1660:                 my $sections = $history{'details'}{'sections'};
 1661:                 if (ref($sections) eq 'HASH') {
 1662:                     foreach my $i (sort(keys(%{$sections}))) {
 1663:                         if (ref($sections->{$i}) eq 'HASH') {
 1664:                             my $sec = $sections->{$i}{'inst'};
 1665:                             if (!grep(/^\Q$sec\E$/,@currsec)) {
 1666:                                 push(@currsec,$sec);
 1667:                             }
 1668:                         }
 1669:                     }
 1670:                 }
 1671:                 my $instseclist = join(',',@currsec);
 1672:                 my ($validationchk,$disposition,$reqstatus,$message,
 1673:                     $validation,$validationerror);
 1674:                 $validationchk =
 1675:                     &Apache::lonnet::auto_courserequest_validation($dom,
 1676:                         $ownername.':'.$ownerdom,$crstype,$inststatus,
 1677:                         $instcode,$instseclist);
 1678:                 if ($validationchk =~ /:/) {
 1679:                     ($validation,$message) = split(':',$validationchk);
 1680:                 } else {
 1681:                     $validation = $validationchk;
 1682:                 }
 1683:                 if ($validation =~ /^error(.*)$/) {
 1684:                     $disposition = 'approval';
 1685:                     $validationerror = $1;
 1686:                 } else {
 1687:                     $disposition = $validation;
 1688:                 }
 1689:                 $reqstatus = $disposition;
 1690:                 if ($disposition eq 'process') {
 1691:                     my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
 1692:                     my $result = &course_creation($dom,$cnum,'domain',$history{'details'},\$logmsg,\$newusermsg,\$addresult,\$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
 1693:                     if ($result eq 'created') {
 1694:                         $disposition = 'created';
 1695:                         $reqstatus = 'created';
 1696:                         my $cid = $dom.'_'.$cnum;
 1697:                         push(@{$newcids{$instcode}},$cid);
 1698:                         if ($dcname && $dcdom) {
 1699:                             my $firsturl = &course_portal_url($cnum,$dom);
 1700:                             my $beneficiary = 'pendingrequestor';
 1701:                             my $now = time;
 1702:                             my $owner = $ownername.':'.$ownerdom;
 1703:                             my $approvedmsg =
 1704:                                 [{
 1705:                                     mt => 'Your requested course: [_1], (queued pending validation) has now been created.',
 1706:                                     args => [$cdescr],
 1707:                                  },
 1708:                                  {
 1709:                                     mt   => 'Visit [_1] to log-in and access the course.',
 1710:                                     args => [$firsturl],
 1711:                                  },
 1712:                                  {
 1713:                                     mt => 'If currently logged-in to LON-CAPA, log-out and log-in again to select your new course role.'
 1714:                                  }];
 1715:                             my $sender = $dcname.':'.$dcdom;
 1716:                             &send_selfserve_notification($owner,$approvedmsg,
 1717:                                                          $cid,$cdescr,$now,
 1718:                                                          $beneficiary,$sender,
 1719:                                                          undef,undef,$crstype);
 1720:                         }
 1721:                     }
 1722:                 } elsif ($disposition eq 'rejected') {
 1723:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] rejected when validating.',$instcode,$ownername.':'.$ownerdom,$inststatus).$linefeed;
 1724:                 } elsif ($disposition eq 'approval') {
 1725:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] switched to "approval by DC" because of validation error: [_4].',$instcode,$ownername.':'.$ownerdom,$inststatus,$validationerror).$linefeed;
 1726: 
 1727:                     my $requestid = $cnum.'_'.$disposition;
 1728:                     my $request = {
 1729:                             $requestid => {
 1730:                                             timestamp   => $reqtime,
 1731:                                             crstype     => $crstype,
 1732:                                             ownername   => $ownername,
 1733:                                             ownerdom    => $ownerdom,
 1734:                                             description => $cdescr,
 1735:                                           },
 1736:                           };
 1737:                     my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,$dom);
 1738:                     unless ($putresult eq 'ok') {
 1739:                         $output .= &mt("An error occurred saving the modified course request for [_1] submitted by [_2] in the domain's courserequestqueue.db.",$instcode,$ownername.':'.$ownerdom).$linefeed;
 1740:                     }
 1741:                 } elsif ($disposition eq 'pending') {
 1742:                     my $instcode = $requesthash{$key}{'instcode'};
 1743:                     my $description = $requesthash{$key}{'description'};
 1744:                     my $timestamp = $requesthash{$key}{'timestamp'};
 1745:                     my $entry = $cnum.':'.$ownername.':'.$ownerdom.':'.
 1746:                                 $instcode.':'.$description;
 1747:                     if (ref($stillpending{$timestamp}) eq 'ARRAY') {
 1748:                         push(@{$stillpending{$timestamp}},$entry);
 1749:                     } else {
 1750:                         $stillpending{$timestamp} = [$entry];
 1751:                     }
 1752:                 }
 1753:                 unless ($disposition eq 'pending') {
 1754:                     my ($statusresult,$output) =
 1755:                         &update_coursereq_status(\%requesthash,$dom,$cnum,
 1756:                                                  $reqstatus,'domain',$ownerdom,
 1757:                                                  $ownername);
 1758:                     unless (&Apache::lonnet::del_dom($reqsnamespace,[$cnum.'_pending'],$dom) eq 'ok') {
 1759:                         $output .= &mt('An error occurred when removing the request for [_1] submitted by [_2] from the pending queue.',$instcode,$ownername.':'.$ownerdom).$linefeed;
 1760:                     }
 1761:                 }
 1762:             }
 1763:         }
 1764:     }
 1765:     foreach my $key (sort(keys(%newcids))) {
 1766:         if (ref($newcids{$key}) eq 'ARRAY') {
 1767:             $output .= "created course from queued request: $key - ".join(', ',@{$newcids{$key}}).$linefeed;
 1768:             my $newcourse = &LONCAPA::escape($key.':'.$newcids{$key});
 1769:         }
 1770:     }
 1771:     unless ($context eq 'auto') {
 1772:         if (keys(%stillpending) > 0) {
 1773:             $output .= '<form method="post" name="changequeue" action="/adm/createcourse" />'."\n".
 1774:                        '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 1775:                        '<input type="hidden" name="phase" value="requestchange" />'.
 1776:                        '<p>'.&mt('For the following requests, the requestor could [_1]not[_2] be validated as official course personnel, so the request remains in the pending queue.','<b>','</b>').'<br />'.&mt('Requests may be left in the queue, or you can manually approve or reject them.').'</p>'.
 1777:                        &build_queue_display($dom,'stillpending',\%stillpending).
 1778:                        '<br /><input type="hidden" name="queue" value="pending" />'."\n".
 1779:                        '<input type="submit" name="processqueue" value="'.&mt('Save').'" />'.
 1780:                        '</form>';
 1781:         }
 1782:     }
 1783:     return $output;
 1784: }
 1785: 
 1786: 1;

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