File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.52.2.2.2.2: download - view: text, annotated - select for diffs
Fri Jul 17 23:04:15 2020 UTC (3 years, 9 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.52.2.2: preferred, unified
- For 2.11.2 modified
  Include changes in 1.59

    1: # The LearningOnline Network
    2: # Utilities to administer domain course requests and course self-enroll requests
    3: #
    4: # $Id: loncoursequeueadmin.pm,v 1.52.2.2.2.2 2020/07/17 23:04:15 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: =item is_active_author()
   74: 
   75: =item author_prompt()
   76: 
   77: =item reqauthor_check()
   78: 
   79: =item process_reqauthor()
   80: 
   81: =back
   82: 
   83: =cut
   84: 
   85: package Apache::loncoursequeueadmin;
   86: 
   87: use strict;
   88: use Apache::lonnet;
   89: use Apache::loncommon;
   90: use Apache::lonmsg;
   91: use Apache::lonlocal;
   92: use Apache::lonuserutils;
   93: use LONCAPA::batchcreatecourse;
   94: use LONCAPA qw(:DEFAULT :match);
   95: 
   96: sub send_selfserve_notification {
   97:     my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
   98:         $approvedlist,$rejectedlist,$crstype) = @_;
   99: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
  100: #       so this can be localized to the recipients date display format/time zone
  101:     $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
  102:     my ($msgcc,$rawsubj,@rawmsg,$subject,$message,$reviewer,$msgtxt);
  103:     my ($senderuname,$senderudom) = split(':',$sender);
  104:     if ($context eq 'coursemanagers') {
  105:         $rawsubj = 'Self-enrollment requests processed';
  106:         push(@rawmsg,{
  107:                       mt => 'Enrollment requests in the following course: [_1] have been processed.',
  108:                       args => ["\n$contextdesc"],
  109:                      });
  110:     } elsif ($context eq 'domainmanagers') {
  111:         $rawsubj = 'Course/Community requests reviewed';
  112:         push(@rawmsg,{
  113:                       mt  => 'Course/Community creation 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 'authormanagers') {
  120:         $rawsubj = 'Authoring Space requests reviewed';
  121:         push(@rawmsg,{
  122:                       mt  => 'Authoring requests in the following domain: "[_1]" have been reviewed.',
  123:                       args => ["\n$contextdesc"],
  124:                      });
  125:         if (ref($textstr) eq 'ARRAY') {
  126:             push(@rawmsg,@{$textstr});
  127:         }
  128:     } elsif ($context eq 'usernamemanagers') {
  129:         $rawsubj = 'LON-CAPA account requests reviewed';
  130:         push(@rawmsg,{
  131:                       mt  => 'Account requests in the following domain: "[_1]" have been reviewed.',
  132:                       args => ["\n$contextdesc"],
  133:                      });
  134:         if (ref($textstr) eq 'ARRAY') {
  135:             push(@rawmsg,@{$textstr});
  136:         }
  137:     } elsif ($context eq 'enroller') {
  138:         $rawsubj = 'Enrollment request';
  139:         if ($crstype eq 'community') {
  140:             $msgtxt = 'Your request for enrollment in the following community: [_1]requested on [_2]has been reviewed by a Coordinator.'
  141:         } else {
  142:             $msgtxt = 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.';
  143:         } 
  144:         push(@rawmsg,{
  145:                       mt  => $msgtxt,
  146:                       args => ["\n  ".$contextdesc.",\n",$timestamp.",\n"],
  147: 
  148:                      });
  149:         if (ref($textstr) eq 'ARRAY') {
  150:             push(@rawmsg,@{$textstr});
  151:         }
  152:     } elsif ($context eq 'courserequestor') {
  153:         if ($crstype eq 'Community') {
  154:             $rawsubj = 'Community request';
  155:             $msgtxt = 'Your request for creation of the following community: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  156:         } else {
  157:             $rawsubj = 'Course request';
  158:             $msgtxt = 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  159:         }
  160:         push(@rawmsg,{
  161:                       mt  => $msgtxt,
  162:                       args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
  163: 
  164:                      });
  165:         if (ref($textstr) eq 'ARRAY') {
  166:             push(@rawmsg,@{$textstr});
  167:         }
  168:     } elsif ($context eq 'pendingrequestor') {
  169:         if ($crstype eq 'Community') {
  170:             $rawsubj = 'Community request';
  171:         } else {
  172:             $rawsubj = 'Processed course request';
  173:         }
  174:         if (ref($textstr) eq 'ARRAY') {
  175:             push(@rawmsg,@{$textstr});
  176:         }
  177:     } elsif ($context eq 'coursereq') {
  178:         if ($crstype eq 'community') {
  179:             $rawsubj = 'Community request to review';
  180:             $msgtxt = 'Creation of the following community: [_1]was requested by [_2] on [_3].';
  181:         } else {
  182:             $rawsubj = 'Course request to review';
  183:             $msgtxt = 'Creation of the following course: [_1]was requested by [_2] on [_3].';
  184:         }
  185:         push(@rawmsg,{
  186:                       mt  => $msgtxt,
  187:                       args => ["\n  $contextdesc\n",$textstr,$timestamp],
  188:                      },
  189:                      {
  190:                       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.',
  191:                       args => ["\n","\n\n","\n\n"],
  192:                      });
  193:     } elsif ($context eq 'selfenrollreq') {
  194:         $rawsubj = 'Self-enrollment request';
  195:         if ($crstype eq 'community') {
  196:             $msgtxt = 'Enrollment in the following community: [_1]was requested by [_2] on [_3].'
  197:         } else {
  198:             $msgtxt = 'Enrollment in the following course: [_1]was requested by [_2] on [_3].'
  199:         }
  200:         push(@rawmsg,{
  201:                       mt  => $msgtxt,
  202:                       args => ["\n  $contextdesc\n",$textstr,$timestamp."\n"],
  203:                      });
  204:         my $directions;  
  205:         if ($crstype eq 'community') {
  206:             $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.';
  207:         } else {
  208:             $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.';
  209:         }
  210:         push(@rawmsg,
  211:                      {
  212:                       mt   => $directions,
  213:                       args => ["  \n\n","\n"],
  214:                      });
  215:     } elsif ($context eq 'authorreq') {
  216:         $rawsubj = 'Authoring Space request to review';
  217:         $msgtxt = 'Assignment of an author role in the [_1] domain[_2]was requested by [_3] on [_4].';
  218:         push(@rawmsg,{
  219:                       mt  => $msgtxt,
  220:                       args => [$contextdesc,"\n",$textstr,$timestamp],
  221:                      },
  222:                      {
  223:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users -> Authoring Space requests[_3]to display a list of pending requests, which you can either approve or reject.',
  224:                       args => ["\n","\n\n  ","\n\n"],
  225:                      });
  226:     } elsif ($context eq 'requestauthor') {
  227:         $rawsubj = 'Authoring Space request';
  228:         $msgtxt = 'Your request for an Authoring Space requested on [_1]has been reviewed by a Domain Coordinator.';
  229:         push(@rawmsg,{
  230:                       mt  => $msgtxt,
  231:                       args => [$timestamp."\n"],
  232:                      });
  233:         if (ref($textstr) eq 'ARRAY') {
  234:             push(@rawmsg,@{$textstr});
  235:         }
  236:     } elsif ($context eq 'usernamereq') {
  237:         $rawsubj = 'LON-CAPA account request';
  238:         $msgtxt = 'Creation of a LON-CAPA account in the [_1] domain[_2]was requested by [_3] on [_4].';
  239:         push(@rawmsg,{
  240:                       mt  => $msgtxt,
  241:                       args => [$contextdesc,"\n",$textstr,$timestamp],
  242:                      },
  243:                      {
  244:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users
  245:  -> LON-CAPA account requests[_3]to display a list of pending requests, which you can either approve or reject.',
  246:                       args => ["\n","\n\n  ","\n\n"],
  247:                      });
  248:     } elsif ($context eq 'requestusername') {
  249:         $rawsubj = 'LON-CAPA account request';
  250:         $msgtxt = 'Your request for a LON-CAPA account requested on [_1]has been reviewed by a Domain Coordinator.';
  251:         push(@rawmsg,{
  252:                       mt  => $msgtxt,
  253:                       args => [$timestamp."\n"],
  254:                      });
  255:         if (ref($textstr) eq 'ARRAY') {
  256:             push(@rawmsg,@{$textstr});
  257:         }
  258:     } elsif ($context eq 'uniquecode') {
  259:         $rawsubj = 'Course Identifier';
  260:         if (ref($textstr) eq 'ARRAY') {
  261:             push(@rawmsg,@{$textstr});
  262:         }
  263:     } elsif ($context eq 'queuedcrsreq') {
  264:         $rawsubj = 'Course Request Queued';
  265:         if (ref($textstr) eq 'ARRAY') {
  266:             push(@rawmsg,@{$textstr});
  267:         }
  268:     } elsif ($context eq 'createdcrsreq') {
  269:         $rawsubj = 'Course Creation Information';
  270:         if (ref($textstr) eq 'ARRAY') {
  271:             push(@rawmsg,@{$textstr});
  272:         }
  273:     }
  274:     my @to_notify = split(/,/,$notifylist);
  275:     my $numsent = 0;
  276:     my @recusers;
  277:     my @recudoms;
  278:     foreach my $cc (@to_notify) {
  279:         my ($ccname,$ccdom) = split(/:/,$cc);
  280:         if (!exists($msgcc->{$ccname.':'.$ccdom})) {
  281:             push(@recusers,$ccname);
  282:             push(@recudoms,$ccdom);
  283:             $msgcc->{$ccname.':'.$ccdom}='';
  284:             $numsent ++;
  285:         }
  286:     }
  287:     my %reciphash = (
  288:                      cc => $msgcc,
  289:     );
  290:     my ($uname,$udom,$need_temp_env);
  291:     if ($sender =~ /:/) {
  292:         ($uname,$udom) = split(/:/,$sender);
  293:         if ($context eq 'usernamereq') {
  294:             unless ($env{'user.name'} && $env{'user.domain'}) {
  295:                 $need_temp_env = 1;
  296:             }
  297:         }
  298:     } elsif ($context eq 'course') {
  299:         $uname = $sender;
  300:         my %courseinfo = &Apache::lonnet::coursedescription($cid);
  301:         $udom = $courseinfo{'num'};
  302:     }
  303:     my %sentmessage;
  304:     my $stamp = time;
  305:     my $msgcount = &Apache::lonmsg::get_uniq();
  306:     my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
  307:     $subject = &mt_user($sender_lh,$rawsubj);
  308:     $message = '';
  309:     foreach my $item (@rawmsg) {
  310:         if (ref($item) eq 'HASH') {
  311:             $message .= &mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
  312:         }
  313:     }
  314:     &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,
  315:                                        \@recusers,\@recudoms,undef,undef,undef,undef,$senderuname,$senderudom);
  316:     my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
  317:     my $status;
  318:     if ($need_temp_env) {
  319:         $env{'user.name'} = $uname;
  320:         $env{'user.domain'} = $udom;
  321:     }
  322:     foreach my $recip (sort(keys(%{$msgcc}))) {
  323:         my ($ccname,$ccdom) = split(/:/,$recip);
  324:         my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
  325:         my $subject = &mt_user($sender_lh,$rawsubj);
  326:         my $message = '';
  327:         foreach my $item (@rawmsg) {
  328:             if (ref($item) eq 'HASH') {
  329:                 $message .= &mt_user($sender_lh,$item->{mt},
  330:                                      @{$item->{args}})."\n";
  331:             }
  332:         }
  333:         if ($context eq 'coursemanagers') {
  334:             if ($approvedlist) {
  335:                 $message .= "\n\n".&mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
  336:             }
  337:             if ($rejectedlist) {
  338:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
  339:             }
  340:         } elsif ($context eq 'domainmanagers') {
  341:             if ($approvedlist) {
  342:                 $message .= "\n\n".&mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
  343:             }
  344:             if ($rejectedlist) {
  345:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
  346:             }
  347:         } elsif ($context eq 'authormanagers') {
  348:             if ($approvedlist) {
  349:                 $message .= "\n\n".&mt_user($sender_lh,'Approved author role requests:')."\n".$approvedlist;
  350:             }
  351:             if ($rejectedlist) {
  352:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected author role requests:')."\n".$rejectedlist;
  353:             }
  354:         } elsif ($context eq 'usernamemanagers') {
  355:             if ($approvedlist) {
  356:                 $message .= "\n\n".&mt_user($sender_lh,'Approved LON-CAPA account requests:')."\n".$approvedlist;
  357:             }
  358:             if ($rejectedlist) {
  359:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected LON-CAPA account requests:')."\n".$rejectedlist;
  360:             }
  361:         }
  362:         $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,
  363:                                                     \%sentmessage,undef,undef,undef,1,$recipid).',';
  364:     }
  365:     $status =~ s/,$//;
  366:     if ($need_temp_env) {
  367:         undef($env{'user.name'});
  368:         undef($env{'user.domain'});
  369:     }
  370:     return ($recipstatus,$status);
  371: }
  372: 
  373: sub display_queued_requests {
  374:     my ($context,$dom,$cnum) = @_;
  375:     my ($namespace,$formaction,$nextelement,%requesthash);
  376:     if ($context eq 'course') {
  377:         $formaction = '/adm/createuser';
  378:         $namespace = 'selfenrollrequests';
  379:         %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
  380:         $nextelement = '<input type="hidden" name="state" value="done" />';
  381:     } elsif ($context eq 'requestauthor') {
  382:         $formaction = '/adm/createuser';
  383:         $namespace = 'requestauthorqueue';
  384:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
  385:         $nextelement = '<input type="hidden" name="state" value="done" />';
  386:     } elsif ($context eq 'requestusername') {
  387:         $formaction = '/adm/createuser';
  388:         $namespace = 'usernamequeue';
  389:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
  390:         $nextelement = '<input type="hidden" name="state" value="done" />';
  391:     } else {
  392:         $formaction = '/adm/createcourse';
  393:         $namespace = 'courserequestqueue';
  394:         my $disposition = 'approval';
  395:         my $nextphase = 'requestchange';
  396:         if ($context eq 'pending') {
  397:             $disposition = 'pending';
  398:             $nextphase = 'requestvalidation';
  399:         } elsif ($context eq 'displaypending') {
  400:             $disposition = 'pending';
  401:         }
  402:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_'.$disposition);
  403:         $nextelement = '<input type="hidden" name="phase" value="'.$nextphase.'" />';
  404:     }
  405:     my ($output,%queue_by_date);
  406:     if (keys(%requesthash) > 0) {
  407:         $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
  408:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
  409:                   $nextelement."\n";
  410:         foreach my $item (keys(%requesthash)) {
  411:             my ($timestamp,$entry,$pending);
  412:             if ($context eq 'course') {
  413:                 ($timestamp, my $usec) = split(/:/,$requesthash{$item});
  414:                 $entry = $item.':'.$usec;
  415:             } elsif ($context eq 'requestauthor') {
  416:                 $timestamp = $requesthash{$item};
  417:                 ($entry) = ($item =~ /^($match_username)_approval$/);
  418:             } elsif ($context eq 'requestusername') {
  419:                 $timestamp = $requesthash{$item};
  420:                 ($entry) = (&unescape($item) =~ /^($match_username)_approval$/);
  421:             } else {
  422:                 $timestamp = $requesthash{$item}{'timestamp'};
  423:                 if (ref($requesthash{$item}) eq 'HASH') {
  424:                     my ($cnum,$disposition) = split('_',$item);
  425:                     $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
  426:                              $requesthash{$item}{'ownerdom'}.':';
  427:                     if (($context eq 'pending') || ($context eq 'displaypending')) {
  428:                         $entry .= $requesthash{$item}{'instcode'};
  429:                     } else {
  430:                         $entry .= $requesthash{$item}{'crstype'};
  431:                     }
  432:                     $entry .= ':'.$requesthash{$item}{'description'};
  433:                 }
  434:             }
  435:             if ($entry ne '') {
  436:                 if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
  437:                     push(@{$queue_by_date{$timestamp}},$entry);
  438:                 } else {
  439:                     $queue_by_date{$timestamp} = [$entry];
  440:                 }
  441:             }
  442:         }
  443:         if (keys(%queue_by_date) > 0) {
  444:             if ($context eq 'course') {
  445:                 $output .=  '<h3>'.&mt('Self-enrollment requests queued pending approval by a Coordinator').'</h3>';
  446:             } elsif (($context eq 'pending') || ($context eq 'displaypending')) {
  447:                 $output .= '<h3>'.&mt('Requests for official courses queued pending validation').'</h3>'.
  448:                            '<p>'.&mt('Requests are validated against institutional data to confirm that the requestor is an instructor of record.').'<br />'.
  449:                            &mt('Validation is attempted when the request is submitted.').' '.
  450:                            &mt('If unvalidated, the request will be held in a queue.').' '.
  451:                            &mt('Validation of pending requests is automatically repeated daily.').'</p>';
  452:             } elsif ($context eq 'requestauthor') {
  453:                 $output .= '<h3>'.&mt('Requests for Authoring Space queued pending approval by a Domain Coordinator').'</h3>';
  454:             } elsif ($context eq 'requestusername') {
  455:                 $output .= '<h3>'.&mt('Requests for LON-CAPA accounts queued pending approval by a Domain Coordinator').'</h3>';
  456:             } else {
  457:                 $output .= '<h3>'.&mt('Course/Community requests queued pending approval by a Domain Coordinator').'</h3>';
  458:             } 
  459:             $output .= &build_queue_display($dom,$context,\%queue_by_date).
  460:                        '<input type="hidden" name="queue" value="approval" />';
  461:         } else {
  462:             $output .= '<div class="LC_info">';
  463:             if ($context eq 'course') {
  464:                 $output .= &mt('There are currently no enrollment requests awaiting approval.');
  465:             } elsif ($context eq 'pending') {
  466:                 $output .= &mt('There are currently no requests for official courses awaiting validation.');
  467:             } elsif ($context eq 'requestauthor') {
  468:                 $output .= &mt('There are currently no requests for Authoring Space awaiting approval.');
  469:             } elsif ($context eq 'requestusername') {
  470:                 $output .= &mt('There are currently no requests for LON-CAPA accounts awaiting approval.');
  471:             } elsif ($context eq 'domain') {
  472:                 $output .= &mt('There are currently no course or community requests awaiting approval.');
  473:             }
  474:             $output .= '</div>'; 
  475:         }
  476:         if ($context eq 'pending') {
  477:             $output .= '<br /><input type="submit" name="validationcheck" value="'.
  478:                        &mt('Validate').'" /><br />'."\n".
  479:                        '<p>'.&mt('Any course/community requests which are successfully validated will be created immediately.').' '.
  480:                              &mt('Unvalidated requests will be listed for manual approval/rejection.').'</p>';
  481:         } elsif (($context ne 'helpdesk') && ($context ne 'displaypending')) {
  482:             $output .= '<br /><input type="submit" name="processqueue" value="'.&mt('Save').'" />';
  483:         }
  484:         $output .= '</form>';
  485:     } else {
  486:         $output .= '<div class="LC_info">';
  487:         if ($context eq 'course') {
  488:             $output .= &mt('There are currently no enrollment requests awaiting approval.');
  489:         } elsif (($context eq 'pending') || ($context eq 'displaypending')) {
  490:             $output .= &mt('There are currently no requests for official courses awaiting validation.');
  491:         } elsif ($context eq 'requestauthor') {
  492:             $output .= &mt('There are currently no requests for Authoring Space awaiting approval.');
  493:         } elsif ($context eq 'requestusername') {
  494:             $output .= &mt('There are currently no requests for LON-CAPA accounts awaiting approval.');
  495:         } else {
  496:             $output .= &mt('There are currently no course or community requests awaiting approval.');
  497:         }
  498:         $output .= '</div>';
  499:     }
  500:     return $output;
  501: }
  502: 
  503: sub build_queue_display {
  504:     my ($dom,$context,$queue) = @_;
  505:     return unless (ref($queue) eq 'HASH');
  506:     my %crstypes;
  507:     my $output =  &Apache::loncommon::start_data_table().
  508:                   &Apache::loncommon::start_data_table_header_row();
  509:     unless (($context eq 'pending') || ($context eq 'displaypending') || ($context eq 'helpdesk')) {
  510:         $output .= '<th>'.&mt('Action').'</th>';
  511:     }
  512:     $output .= '<th>'.&mt('Requestor').'</th>';
  513:     if ($context eq 'course') {
  514:         $output .= '<th>'.&mt('Section').'</th>'.
  515:                    '<th>'.&mt('Date requested').'</th>';
  516:     } elsif ($context eq 'requestauthor') {
  517:         $output .= '<th>'.&mt('Date requested').'</th>';
  518:     } elsif ($context eq 'requestusername') {
  519:         $output .= '<th>'.&mt('Date requested').'</th>'.
  520:                    '<th>'.&mt('Details').'</th>';
  521:     } elsif ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
  522:         $output .= '<th>'.&mt('Institutional code').'</th>'.
  523:                    '<th>'.&mt('Date requested').'</th>'.
  524:                    '<th>'.&mt('Details').'</th>';
  525:     } else {
  526:         %crstypes = &Apache::lonlocal::texthash (
  527:                         official   => 'Official course',
  528:                         unofficial => 'Unofficial course',
  529:                         community  => 'Community',
  530:                         textbook   => 'Textbook course',
  531:                     );
  532:         $output .= '<th>'.&mt('Type').'</th>'.
  533:                    '<th>'.&mt('Date requested').'</th>'.
  534:                    '<th>'.&mt('Details').'</th>';
  535:     }
  536:     $output .= &Apache::loncommon::end_data_table_header_row();
  537:     my @sortedtimes = sort {$a <=> $b} (keys(%{$queue}));
  538:     my $count = 0;
  539:     foreach my $item (@sortedtimes) {
  540:         if (ref($queue->{$item}) eq 'ARRAY') {
  541:             foreach my $request (sort(@{$queue->{$item}})) {
  542:                 my ($row,$approve,$reject,$showtime,$showsec,$namelink,
  543:                     $detailslink,$crstype,$instcode);
  544:                 $showtime = &Apache::lonlocal::locallocaltime($item);
  545:                 if ($context eq 'course') {
  546:                     my ($puname,$pudom,$pusec) = split(/:/,$request);
  547:                     $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
  548:                     $reject = $puname.':'.$pudom;
  549:                     $showsec = $pusec;
  550:                     if ($showsec eq '') {
  551:                         $showsec = &mt('none');
  552:                     }
  553:                     $namelink = &Apache::loncommon::aboutmewrapper(
  554:                                 &Apache::loncommon::plainname($puname,$pudom),
  555:                                 $puname,$pudom);
  556:                 } elsif ($context eq 'requestauthor') {
  557:                     if (&Apache::lonnet::homeserver($request,$dom) ne 'no_host') {
  558:                         $approve = $count.':'.$request;
  559:                         $reject = $request; 
  560:                         $namelink = &Apache::loncommon::aboutmewrapper(
  561:                                     &Apache::loncommon::plainname($request,$dom),
  562:                                     $request,$dom);
  563:                     }
  564:                 } elsif ($context eq 'requestusername') {
  565:                     if (&Apache::lonnet::homeserver($request,$dom) eq 'no_host') {
  566:                         my $queued = 'approval';
  567:                         $approve = $count.':'.$request;
  568:                         $reject = $request;
  569:                         $detailslink='<a href="javascript:openusernamereqdisplay('.
  570:                                      "'$dom','$request','$queued'".');">'.$request.'</a>';
  571:                         $namelink = $request;
  572:                     }
  573:                 } else {
  574:                     my ($cnum,$ownername,$ownerdom,$type,$cdesc);
  575:                     my $queued = 'approval'; 
  576:                     if ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
  577:                         ($cnum,$ownername,$ownerdom,$instcode,$cdesc)=split(/:/,$request,5);
  578:                         $queued = 'pending';                        
  579:                     } else {
  580:                         ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
  581:                         $crstype = $type;
  582:                         if (defined($crstypes{$type})) {
  583:                             $crstype = $crstypes{$type};
  584:                         }
  585:                     }
  586:                     $detailslink='<a href="javascript:opencoursereqdisplay('.
  587:                                   "'$dom','$cnum','$queued'".');">'.$cdesc.'</a>';
  588:                     $approve = $count.':'.$cnum;
  589:                     $reject = $cnum;
  590:                     $namelink = &Apache::loncommon::aboutmewrapper(
  591:                                 &Apache::loncommon::plainname($ownername,$ownerdom),
  592:                                 $ownername,$ownerdom);
  593:                 }
  594:                 unless (($context eq 'pending') || ($context eq 'displaypending') || ($context eq 'helpdesk')) {
  595:                     $row = '<td><span class="LC_nobreak"><label>'.
  596:                            '<input type="radio" value="'.$approve.'" name="'.$count.'radioreq" />'.&mt('Approve').'</label>'.
  597:                            '<label>'.('&nbsp;'x2).
  598:                            '<input type="radio" value="'.$reject.'" name="'.$count.'radioreq" />'.&mt('Reject').'</label>'.
  599:                            '<label>'.('&nbsp;'x2).
  600:                            '<input type="radio" value="'."later:".$reject.'" name="'.$count.'radioreq" checked />'.&mt('Decide Later').
  601:                            '</label></span><br /></td>';
  602:                 }
  603:                 $row .= '<td>'.$namelink.'</td>'."\n";
  604:                 if ($context eq 'course') {
  605:                     $row .= '<td>'.$showsec.'</td>'."\n".
  606:                             '<td>'.$showtime.'</td>'."\n";
  607:                 } elsif ($context eq 'requestauthor') {
  608:                     $row .= '<td>'.$showtime.'</td>'."\n";
  609:                 } elsif ($context eq 'requestusername') {
  610:                     $row .= '<td>'.$showtime.'</td>'."\n".
  611:                             '<td>'.$detailslink.'</td>'."\n";
  612:                 } else {
  613:                     if ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
  614:                         $row .= '<td>'.$instcode.'</td>'."\n";
  615:                     } else {
  616:                         $row .= '<td>'.$crstype.'</td>'."\n";
  617:                     }
  618:                     $row .= '<td>'.$showtime.'</td>'."\n".
  619:                             '<td>'.$detailslink.'</td>'."\n";
  620:                 }
  621:                 $output .= &Apache::loncommon::start_data_table_row()."\n".
  622:                            $row.
  623:                            &Apache::loncommon::end_data_table_row()."\n";
  624:                 $count ++;
  625:             }
  626:         }
  627:     }
  628:     $output .= &Apache::loncommon::end_data_table();
  629:     return $output;
  630: }
  631: 
  632: sub update_request_queue {
  633:     my ($context,$cdom,$cnum,$coursedesc) = @_;
  634:     my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
  635:         $stucounts,$idx,$classlist,%requesthash,$cid,$domdesc,$now,
  636:         $sender,$approvedmsg,$rejectedmsg,$beneficiary,
  637:         @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
  638:         @processing_errors,@warn_approves,@warn_rejects,@approvals,@warn_dels,
  639:         @rejections,@rejectionerrors,@nopermissions,%courseroles,@toremove,
  640:         %communityroles,%domdefs,%approvalmsg,%rejectionmsg,$crstype,$queue,
  641:         $firsturl,$uniquecode,%codes);
  642:     my $count=0;
  643:     while (my $item = $env{'form.'.$count.'radioreq'}) {
  644:         if ($item =~ /^\d+:/) {
  645:             push(@approvals,$item);
  646:         } elsif ($item !~ /^later:/) {
  647:             push(@rejections,$item);
  648:         }
  649:         $count ++;
  650:     }
  651: 
  652:     $now = time;
  653:     $sender = $env{'user.name'}.':'.$env{'user.domain'};
  654:     if ($context eq 'course') {
  655:         $namespace = 'selfenrollrequests';
  656:         $beneficiary = 'enroller';
  657:         $cid = $env{'request.course.id'};
  658:         $crstype = lc(&Apache::loncommon::course_type());
  659:         $firsturl = &Apache::lonnet::course_portal_url($cnum,$cdom);
  660:         %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
  661:         $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
  662:         $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
  663:         $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
  664:         $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
  665:         $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
  666:         ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
  667:         $approvedmsg = [{
  668:                             mt => 'Your request for enrollment has been approved.',
  669:                         },
  670:                         {
  671:                             mt   => 'Visit [_1] to log-in and access the course',
  672:                             args => [$firsturl],
  673:                         }];
  674:         $rejectedmsg =  [{
  675:                             mt => 'Your request for enrollment has not been approved.',
  676:                         }];
  677:     } elsif ($context eq 'requestauthor') {
  678:         $namespace = 'requestauthorqueue';
  679:         $beneficiary = 'requestauthor';
  680:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
  681:         my %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  682:         if (ref($domdefs{'requestauthor'}) eq 'HASH') {
  683:             if (ref($domdefs{'requestauthor'}{'notify'}) eq 'HASH') {
  684:                 $notifylist = $domdefs{'requestauthor'}{'notify'}{'approval'};
  685:             }
  686:         }
  687:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
  688:         $firsturl = &Apache::lonnet::course_portal_url($domconfiguser,$cdom);
  689:         $approvedmsg = [{
  690:                             mt => 'Your request for Authoring Space has been approved.',
  691:                         },
  692:                         {
  693:                             mt   => 'Visit [_1] to log-in and select your author role',
  694:                             args => [$firsturl],
  695:                         }];
  696:         $rejectedmsg =  [{
  697:                             mt => 'Your request for Authoring Space has not been approved.',
  698:                         }];
  699:         $domdesc = &Apache::lonnet::domain($cdom);
  700:     } elsif ($context eq 'requestusername') {
  701:         $namespace = 'usernamequeue';
  702:         $beneficiary = 'requestusername';
  703:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
  704:         my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
  705:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
  706:             if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
  707:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'notify'}) eq 'HASH') {
  708:                     $notifylist = $domconfig{'usercreation'}{'cancreate'}{'notify'}{'approval'};
  709:                 }
  710:             }
  711:         }
  712:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
  713:         $firsturl = &Apache::lonnet::course_portal_url($domconfiguser,$cdom);
  714:         $approvedmsg = [{
  715:                             mt => 'Your request for a LON-CAPA account has been approved.',
  716:                         },
  717:                         {
  718:                             mt   => 'Visit [_1] to log-in.',
  719:                             args => [$firsturl],
  720:                         }];
  721:         $rejectedmsg =  [{
  722:                             mt => 'Your request for a LON-CAPA account has not been approved.',
  723:                         }];
  724:         $domdesc = &Apache::lonnet::domain($cdom);
  725:     } else {
  726:         $domdesc = &Apache::lonnet::domain($cdom);
  727:         $namespace = 'courserequestqueue';
  728:         $beneficiary = 'courserequestor';
  729:         $queue = 'approval';
  730:         if ($env{'form.queue'} eq 'pending') {
  731:             $queue = 'pending';
  732:         }
  733:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_'.$queue);
  734:         my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
  735:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
  736:             if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') { 
  737:                 $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
  738:             }
  739:         }
  740:         $approvalmsg{'course'} = 
  741:                         [{
  742:                             mt => 'Your course request has been approved.',
  743:                         },
  744:                         {
  745:                             mt   => 'Visit [_1] to log-in and access the course',
  746:                             args => [],
  747:                         }];
  748:         $rejectionmsg{'course'} =
  749:                         [{
  750:                             mt => 'Your course request has not been approved.',
  751:                         }];
  752: 
  753:         $approvalmsg{'community'} = 
  754:                         [{
  755:                             mt => 'Your community request has been approved.',
  756:                         },
  757:                         {
  758:                             mt   => 'Visit [_1] to log-in and access the community',
  759:                             args => [],
  760:                         }];
  761: 
  762:         $rejectionmsg{'community'} = 
  763:                         [{
  764:                             mt => 'Your community request has not been approved.',
  765:                         }];
  766: 
  767:         %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  768:         my @roles = &Apache::lonuserutils::roles_by_context('course');
  769:         foreach my $role (@roles) {
  770:             $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
  771:         }
  772:         foreach my $role (@roles) {
  773:             $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
  774:         }
  775:     }
  776:     foreach my $item (sort {$a <=> $b} @approvals) {
  777:         if ($context eq 'course') {
  778:             my ($num,$uname,$udom,$usec) = split(/:/,$item);
  779:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
  780:             if ($uhome ne 'no_host') {
  781:                 if (exists($requesthash{$uname.':'.$udom})) {
  782:                     if (exists($classlist->{$uname.':'.$udom})) {
  783:                         if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
  784:                             if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
  785:                                 ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
  786:                                 push(@existing,$uname.':'.$udom);
  787:                                 next;
  788:                             }
  789:                         }
  790:                     }
  791:                 } else {
  792:                     push(@missingreq,$uname.':'.$udom);
  793:                     next;
  794:                 }
  795:                 if (!grep(/^\Q$item\E$/,@rejections)) {
  796:                     if ($limit eq 'allstudents') {
  797:                         if ($stucounts->{$limit} >= $cap) {
  798:                             push(@limitexceeded,$uname.':'.$udom);
  799:                             last;
  800:                         }
  801:                     } elsif ($limit eq 'selfenrolled') {
  802:                         if ($stucounts->{$limit} >= $cap) {
  803:                             push(@limitexceeded,$uname.':'.$udom);
  804:                             last;
  805:                         }
  806:                     }
  807:                     my $result =
  808:                         &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
  809:                     if ($result eq 'ok') {
  810:                         push(@completed,$uname.':'.$udom);
  811:                         $stucounts->{'allstudents'} ++;
  812:                         $stucounts->{'selfenrolled'} ++;
  813:                         &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
  814:                                        $cid,$coursedesc,$now,$beneficiary,$sender,
  815:                                        undef,undef,$crstype);
  816:                         my %userrequest = (
  817:                             $cdom.'_'.$cnum => {
  818:                                 timestamp   => $now,
  819:                                 section     => $usec,
  820:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  821:                                 status      => 'approved',
  822:                             }
  823:                         );
  824:                         my $userresult =
  825:                             &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
  826:                         if ($userresult ne 'ok') {
  827:                             push(@warn_approves,$uname.':'.$udom);
  828:                         }
  829:                     } else {
  830:                         push(@processing_errors,$uname.':'.$udom);
  831:                     }
  832:                 }
  833:             } else {
  834:                 push(@invalidusers,$uname.':'.$udom);
  835:             }
  836:         } elsif ($context eq 'requestauthor') {
  837:             my ($num,$uname) = split(/:/,$item);
  838:             my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
  839:             if ($uhome ne 'no_host') {
  840:                 my ($user_is_adv,$user_is_author) = &Apache::lonnet::is_advanced_user($cdom,$uname);
  841:                 if ($user_is_author) {
  842:                     push(@existing,$uname);
  843:                 } elsif (&Apache::lonnet::usertools_access($uname,$cdom,'requestauthor',
  844:                                                            undef,'requestauthor')) {
  845:                     if (&Apache::lonnet::allowed('cau',$cdom)) {
  846:                         if (&Apache::lonnet::assignrole($cdom,$uname,'/'.$cdom.'/','au',undef,time,undef,undef,'requestauthor') eq 'ok') {
  847:                             push(@completed,$uname);
  848:                             &send_selfserve_notification($uname.':'.$cdom,
  849:                                                          $approvedmsg,undef,undef,$now,
  850:                                                          $beneficiary,$sender);
  851:                             my %userrequest = (
  852:                                 author => {
  853:                                           timestamp   => $now,
  854:                                           adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  855:                                           status      => 'approved',
  856:                                         },
  857:                                 author_status => 'approved',
  858:                             );
  859:                             my $userresult =
  860:                                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$uname);
  861:                             if ($userresult ne 'ok') {
  862:                                 push(@warn_approves,$uname.':'.$cdom);
  863:                             }
  864:                         } else {
  865:                             push(@processing_errors,$uname);
  866:                         }
  867:                     } else {
  868:                         push(@nopermissions,$uname);
  869:                     }
  870:                 } else {
  871:                     push(@nopermissions,$uname);
  872:                 }
  873:             } else {
  874:                 push(@invalidusers,$uname.':'.$cdom);
  875:             }
  876:             push(@toremove,(@invalidusers,@nopermissions));
  877:         } elsif ($context eq 'requestusername') {
  878:             my ($num,$uname) = split(/:/,$item);
  879:             my $dbname = 'nohist_requestedusernames';
  880:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
  881:             my %curr = &Apache::lonnet::get($dbname,[$uname],$cdom,$domconfiguser);
  882:             
  883:             if (ref($curr{$uname}) eq 'HASH') {
  884:                 my ($username,$logtoken,$serverid,$encpass,$courseid,$id,$firstname,
  885:                     $middlename,$lastname,$generation,$inststatus);
  886:                 $curr{$uname}{'timestamp'} = $now;
  887:                 $curr{$uname}{'adjudicator'} = $env{'user.name'}.':'.$env{'user.domain'};
  888:                 $courseid   = $curr{$uname}{'courseid'};
  889:                 $id         = $curr{$uname}{'id'};
  890:                 $firstname  = $curr{$uname}{'firstname'};
  891:                 $middlename = $curr{$uname}{'middlename'};
  892:                 $lastname   = $curr{$uname}{'lastname'};
  893:                 $generation = $curr{$uname}{'generation'};
  894:                 $inststatus = $curr{$uname}{'inststatus'};
  895: 
  896:                 my ($key,$caller)=split(/&/,$curr{$uname}{'tmpinfo'});
  897:                 if ($caller eq 'createaccount') {
  898:                     my $upass = &Apache::loncommon::des_decrypt($key,$curr{$uname}{'upass'});
  899:                     undef($curr{$uname}{'upass'});
  900:                     my $result =
  901:                         &Apache::lonnet::modifyuser($cdom,$uname,$id,'internal',$upass,
  902:                                                     $firstname,$middlename,$lastname,
  903:                                                     $generation,undef,undef,$uname);
  904:                     if ($result eq 'ok') {
  905:                         $curr{$uname}{'status'} = 'created';
  906:                         push(@completed,$uname); 
  907:                         my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
  908:                         if ($uhome eq 'no_host') {
  909:                             push(@warn_approves,$uname);
  910:                         } else {
  911:                             unless (($inststatus eq 'default') || ($inststatus eq '')) {
  912:                                 &Apache::lonnet::put('environment',{inststatus => $inststatus},$cdom,$uname);
  913:                             }
  914:                             &send_selfserve_notification($uname.':'.$cdom,
  915:                                                          $approvedmsg,undef,undef,$now,
  916:                                                          $beneficiary,$sender);
  917:                             if (&Apache::lonnet::put($dbname,\%curr,$cdom,$domconfiguser) ne 'ok') {
  918:                                 push(@warn_approves,$uname);
  919:                             }
  920:                         }
  921:                     } else {
  922:                         push(@processing_errors,$uname);
  923:                     }
  924:                 } else {
  925:                     push(@processing_errors,$uname);
  926:                 }
  927:             } else {
  928:                 push(@invalidusers,$uname);
  929:             }
  930:             push(@toremove,@invalidusers);
  931:         } else {
  932:             my ($num,$cnum) = split(':',$item);
  933:             if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
  934:                 if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  935:                     my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
  936:                     my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
  937:                     $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
  938:                     my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
  939:                     my $longroles = \%courseroles;
  940:                     if ($crstype eq 'community') {
  941:                         $longroles = \%communityroles;
  942:                     }
  943:                     my $cancreate;
  944:                     if ($cdom eq $ownerdom) {
  945:                         if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
  946:                                                               undef,'requestcourses')) {
  947:                             $cancreate = 1;
  948:                         }
  949:                     } else {
  950:                         my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
  951:                         if ($userenv{'reqcrsotherdom.'.$crstype}) {
  952:                             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
  953:                             if (grep(/^\Q$cdom\E:/,@doms)) {
  954:                                 $cancreate = 1;
  955:                             }
  956:                         }
  957:                     }
  958:                     if ($cancreate) {
  959:                         my $requestkey = $cdom.'_'.$cnum;
  960:                         my %history = 
  961:                             &Apache::lonnet::restore($requestkey,'courserequests',
  962:                                                      $ownerdom,$ownername);
  963:                         if ((ref($history{'details'}) eq 'HASH') && 
  964:                             ($history{'disposition'} eq $queue)) {
  965:                             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,
  966:                                 $keysmsg,$code,%customitems);
  967:                             my $clonemsg = [];
  968:                             my $fullname = '';
  969:                             my $inprocess = &Apache::lonnet::auto_crsreq_update($cdom,$cnum,$crstype,'process',$ownername,
  970:                                                                                 $ownerdom,$fullname,$coursedesc);
  971:                             if (ref($inprocess) eq 'HASH') {
  972:                                 foreach my $key (keys(%{$inprocess})) {
  973:                                     if (exists($history{'details'}{$key})) { 
  974:                                         $customitems{$key} = $history{'details'}{$key};
  975:                                     }
  976:                                 }
  977:                             }
  978:                             if ($history{'details'}{'clonecrs'}) {
  979:                                 $customitems{'_LC_clonefrom'} = $history{'details'}{'clonedom'}.'_'.$history{'details'}{'clonecrs'};
  980:                             }
  981:                             my ($result,$postprocess) = &course_creation($cdom,$cnum,$context,$history{'details'},
  982:                                                         \$logmsg,$clonemsg,\$newusermsg,\$addresult,\$enrollcount,
  983:                                                         \$response,\$keysmsg,\%domdefs,$longroles,\$code,\%customitems);
  984:                             if ($result eq 'created') {
  985:                                 if ($crstype eq 'community') {
  986:                                     $approvedmsg = $approvalmsg{'community'};
  987:                                 } else {
  988:                                     $approvedmsg = $approvalmsg{'course'};
  989:                                 }
  990:                                 my $firsturl = &Apache::lonnet::course_portal_url($cnum,$cdom);
  991:                                 if (ref($approvedmsg) eq 'ARRAY') {
  992:                                     if (ref($approvedmsg->[1]) eq 'HASH') {
  993:                                         $approvedmsg->[1]->{'args'} = [$firsturl];
  994:                                     }
  995:                                     if ((ref($clonemsg) eq 'ARRAY') && (@{$clonemsg})) {
  996:                                         push(@{$approvedmsg},@{$clonemsg});
  997:                                     }
  998:                                     if ($code) {
  999:                                         push(@{$approvedmsg},
 1000:                                             {
 1001:                                               mt   => 'Students can automatically select your course by entering this code: [_1]',
 1002:                                               args => [$code],
 1003:                                             });
 1004:                                         $codes{$cnum} = $code;
 1005:                                     }
 1006:                                     if (ref($postprocess) eq 'HASH') {
 1007:                                         if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
 1008:                                             foreach my $item (@{$postprocess->{'createdmsg'}}) {
 1009:                                                 if (ref($item) eq 'HASH') {
 1010:                                                     if ($item->{'mt'} ne '') {
 1011:                                                         push(@{$approvedmsg},$item);
 1012:                                                     }
 1013:                                                 }
 1014:                                             }
 1015:                                         }
 1016:                                     }
 1017:                                 }
 1018:                                 push(@completed,$cnum);
 1019:                                 
 1020:                                 unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
 1021:                                     push(@warn_dels,$cnum);
 1022:                                 }
 1023:                                 &send_selfserve_notification($ownername.':'.$ownerdom,
 1024:                                               $approvedmsg,$cid,$coursedesc,$now,
 1025:                                               $beneficiary,$sender,undef,undef,$crstype);
 1026:                                 my %reqhash = (
 1027:                                                 reqtime     => $history{'reqtime'},
 1028:                                                 crstype     => $history{'crstype'},
 1029:                                                 details     => $history{'details'},
 1030:                                                 disposition => $history{'disposition'},
 1031:                                                 status      => 'created',
 1032:                                                 adjudicator => $env{'user.name'}.':'.
 1033:                                                                $env{'user.domain'},
 1034:                                               );
 1035:                                 my $userresult =
 1036:                                     &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
 1037:                                                    'courserequests',$ownerdom,$ownername);
 1038:                                 if ($userresult eq 'ok') {
 1039:                                     my %status = (
 1040:                                                    'status:'.$cdom.':'.$cnum => 'created'
 1041:                                                  );
 1042:                                     my $statusresult = 
 1043:                                         &Apache::lonnet::put('courserequests',\%status,
 1044:                                                              $ownerdom,$ownername);
 1045:                                     if ($statusresult ne 'ok') {
 1046:                                         push(@warn_approves,$cnum);
 1047:                                     }
 1048:                                 }
 1049:                                 if ($userresult ne 'ok') {
 1050:                                     push(@warn_approves,$cnum);
 1051:                                 }
 1052:                             } else {
 1053:                                 push(@processing_errors,$cnum);
 1054:                             }
 1055:                         } else {
 1056:                             push(@processing_errors,$cnum);
 1057:                         }
 1058:                     } else {
 1059:                         push(@nopermissions,$cnum);
 1060:                     }
 1061:                 } else {
 1062:                     push(@existing,$cnum);
 1063:                 }
 1064:             } else {
 1065:                 push(@missingreq,$cnum);
 1066:             }
 1067:         }
 1068:     }
 1069:     my @changes = (@completed,@rejections);
 1070:     if ($context eq 'domain') {
 1071:         @changes = map {$_.'_'.$queue} (@changes);
 1072:     } elsif ($context eq 'requestauthor') {
 1073:         @changes = map {$_.'_approval'} (@changes);
 1074:     } elsif ($context eq 'requestusername') {
 1075:         @changes = map {&escape($_).'_approval'} (@changes);
 1076:     }
 1077:     if (@rejections) {
 1078:         foreach my $item (@rejections) {
 1079:             if (($context eq 'course') || ($context eq 'requestauthor')) {
 1080:                 my ($user,$uname,$udom,%userrequest,$key,$dbname);
 1081:                 if ($context eq 'requestauthor') {
 1082:                     $uname = $item;
 1083:                     $udom = $cdom;
 1084:                     $user = $uname.':'.$udom;
 1085:                     $key = 'author';
 1086:                     $dbname = 'requestauthor';
 1087:                 } else {
 1088:                     $user = $item;
 1089:                     ($uname,$udom) = split(/:/,$user);
 1090:                     $key = $cdom.'_'.$cnum;
 1091:                     $dbname = $namespace;
 1092:                 }
 1093:                 &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
 1094:                                              $now,$beneficiary,$sender,undef,undef,
 1095:                                              $crstype);
 1096:                 %userrequest = (
 1097:                     $key => {
 1098:                         timestamp   => $now,
 1099:                         adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
 1100:                         status      => 'rejection',
 1101:                     }
 1102:                 );
 1103:                 if ($context eq 'requestauthor') {
 1104:                     $userrequest{'author_status'} = 'rejection';  
 1105:                 }
 1106:                 my $userresult =
 1107:                     &Apache::lonnet::put($dbname,\%userrequest,$udom,$uname);
 1108:                 if ($userresult ne 'ok') {
 1109:                     push(@warn_rejects,$item);
 1110:                 }
 1111:             } elsif ($context eq 'requestusername') {
 1112:                 my ($uname,$udom,$dbname);
 1113:                 $uname = $item;
 1114:                 $udom = $cdom;
 1115:                 $dbname = 'nohist_requestedusernames';
 1116:                 my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 1117:                 my %curr = &Apache::lonnet::get($dbname,[$uname],$cdom,$domconfiguser);
 1118:                 if (ref($curr{$uname}) eq 'HASH') {
 1119:                      $curr{$uname}{'status'} = 'rejected';
 1120:                      $curr{$uname}{'timestamp'} = $now;
 1121:                      $curr{$uname}{'adjudicator'} =  $env{'user.name'}.':'.$env{'user.domain'};
 1122:                      undef($curr{$uname}{'tmpinfo'});
 1123:                      undef($curr{$uname}{'upass'}); 
 1124:                 }
 1125:                 my $userresult =
 1126:                     &Apache::lonnet::put($dbname,\%curr,$cdom,$domconfiguser);
 1127:                 if ($userresult ne 'ok') {
 1128:                     push(@warn_rejects,$uname);
 1129:                 }
 1130:             } else {
 1131:                 my $cnum = $item;
 1132:                 if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
 1133:                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 1134:                         my $requestkey = $cdom.'_'.$cnum;
 1135:                         my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
 1136:                         my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
 1137:                         my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
 1138:                         $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
 1139:                         if ($crstype eq 'community') {
 1140:                             $rejectedmsg = $rejectionmsg{'community'};
 1141:                         } else {
 1142:                             $rejectedmsg = $rejectionmsg{'course'};
 1143:                         }
 1144:                         &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
 1145:                                                      $cid,$coursedesc,$now,$beneficiary,
 1146:                                                      $sender,undef,undef,$crstype);
 1147:                         my %history =
 1148:                             &Apache::lonnet::restore($requestkey,'courserequests',
 1149:                                                      $ownerdom,$ownername);
 1150:                         if ((ref($history{'details'}) eq 'HASH') &&
 1151:                             ($history{'disposition'} eq $queue)) {
 1152:                             my %reqhash = (
 1153:                                             reqtime     => $history{'reqtime'},
 1154:                                             crstype     => $history{'crstype'},
 1155:                                             details     => $history{'details'},
 1156:                                             disposition => $history{'disposition'},
 1157:                                             status      => 'rejected',
 1158:                                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
 1159:                                               );
 1160:                             my $userresult =
 1161:                                 &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
 1162:                                                 'courserequests',$ownerdom,$ownername);
 1163:                             if ($userresult eq 'ok') {
 1164:                                 my %status = (
 1165:                                                'status:'.$cdom.':'.$cnum => 'rejected'
 1166:                                              );
 1167:                                 my $statusresult =
 1168:                                     &Apache::lonnet::put('courserequests',\%status,
 1169:                                                          $ownerdom,$ownername);
 1170:                                 if ($statusresult ne 'ok') {
 1171:                                     push(@warn_rejects,$cnum);
 1172:                                 }
 1173:                             } else {
 1174:                                 push(@warn_rejects,$cnum);
 1175:                             }
 1176:                             unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
 1177:                                 push(@warn_dels,$cnum);
 1178:                             }
 1179:                         } else {
 1180:                             push(@warn_rejects,$cnum);
 1181:                         }
 1182:                     } else {
 1183:                         push(@existing,$cnum);
 1184:                     }
 1185:                 } else {
 1186:                     push(@rejectionerrors,$cnum);
 1187:                 }
 1188:             }
 1189:         }
 1190:     }
 1191:     if (@toremove) {
 1192:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 1193:         foreach my $item (@toremove) {
 1194:             if ($context eq 'requestauthor') {
 1195:                 my %userrequest = (
 1196:                     author => {
 1197:                                 timestamp   => $now,
 1198:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
 1199:                                 status      => 'deleted',
 1200:                               },
 1201:                               author_status => 'deleted',
 1202:                 );
 1203:                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$item);
 1204:             } elsif ($context eq 'requestusername') {
 1205:                 my $dbname = 'nohist_requestedusernames';
 1206:                 my %curr = &Apache::lonnet::get($dbname,[$item],$cdom,$domconfiguser);
 1207:                 if (ref($curr{$item}) eq 'HASH') {
 1208:                     $curr{$item}{'status'} = 'deleted';
 1209:                     $curr{$item}{'timestamp'} = $now;
 1210:                     $curr{$item}{'adjudicator'} = $env{'user.name'}.':'.$env{'user.domain'};
 1211:                     undef($curr{$item}{'upass'});
 1212:                     undef($curr{$item}{'tmpinfo'}); 
 1213:                 }
 1214:             }
 1215:         }
 1216:         @toremove = map {$_.'_approval'} (@toremove);
 1217:         my $delresult = &Apache::lonnet::del_dom($namespace,\@toremove,$cdom);
 1218:     }
 1219:     if (@changes) {
 1220:         my $delresult;
 1221:         if ($context eq 'course') {
 1222:             $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
 1223:         } else {
 1224:             $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
 1225:         }
 1226:         if ($delresult eq 'ok') {
 1227:             my $namelink =
 1228:                 &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
 1229:             my ($chgmsg,$approvedlist,$rejectedlist);
 1230:             if ($context eq 'course') {
 1231:                 $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
 1232:                 if (@completed) {
 1233:                     $approvedlist = join("\n",@completed);
 1234:                     if ($crstype eq 'community') {
 1235:                         $output .= '<p>'.&mt('The following were enrolled in the community:').'<ul>';
 1236:                     } else {
 1237:                         $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
 1238:                     }
 1239:                     foreach my $user (@completed) {
 1240:                         my ($uname,$udom) = split(/:/,$user);
 1241:                         my $userlink =
 1242:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
 1243:                         $output .= '<li>'.$userlink.'</li>';
 1244:                     }
 1245:                     $output .= '</ul></p>';
 1246:                 }
 1247:                 if (@rejections) {
 1248:                     $rejectedlist = join("\n",@rejections);
 1249:                     $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
 1250:                     foreach my $user (@rejections) {
 1251:                         $output .= '<li>'.$user.'</li>';
 1252:                     }
 1253:                     $output .= '</ul></p>';
 1254:                 }
 1255:                 if ($notifylist ne '') {
 1256:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
 1257:                                                  $now,'coursemanagers',$sender,
 1258:                                                  $approvedlist,$rejectedlist,$crstype);
 1259:                 }
 1260:             } elsif ($context eq 'requestauthor') {
 1261:                 $chgmsg = "'Action was taken on the following Authoring Space requests by [_1].',$namelink";
 1262:                 if (@completed) {
 1263:                     $approvedlist = join("\n",@completed);
 1264:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
 1265:                     foreach my $uname (@completed) {
 1266:                         my $userlink =
 1267:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1268:                         $output .= '<li>'.$userlink.'</li>';
 1269:                         
 1270:                     }
 1271:                     $output .= '</ul></p>';
 1272:                 }
 1273:                 if (@rejections) {
 1274:                     $rejectedlist = join("\n",@rejections);
 1275:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1276:                     foreach my $uname (@rejections) {
 1277:                         my $userlink =
 1278:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1279:                         $output .= '<li>'.$userlink.'</li>';
 1280:                     }
 1281:                     $output .= '</ul></p>';
 1282:                 }
 1283:                 if ($notifylist ne '') {
 1284:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
 1285:                                                  $now,'authormanagers',$sender,
 1286:                                                  $approvedlist,$rejectedlist);
 1287:                 }
 1288:             } elsif ($context eq 'requestusername') {
 1289:                 $chgmsg = "'Action was taken on the following LON-CAPA account requests by [_1].',$namelink";
 1290:                 if (@completed) {
 1291:                     $approvedlist = join("\n",@completed);
 1292:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
 1293:                     foreach my $uname (@completed) {
 1294:                         $output .= '<li>'.$uname.'</li>';
 1295: 
 1296:                     }
 1297:                     $output .= '</ul></p>';
 1298:                 }
 1299:                 if (@rejections) {
 1300:                     $rejectedlist = join("\n",@rejections);
 1301:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1302:                     foreach my $uname (@rejections) {
 1303:                         $output .= '<li>'.$uname.'</li>';
 1304:                     }
 1305:                     $output .= '</ul></p>';
 1306:                 }
 1307:                 if ($notifylist ne '') {
 1308:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
 1309:                                                  $now,'usernamemanagers',$sender,
 1310:                                                  $approvedlist,$rejectedlist);
 1311:                 }
 1312:             } else {
 1313:                 $chgmsg = "'Action was taken on the following course and community requests by [_1].',$namelink";
 1314:                 if (@completed) {
 1315:                     $approvedlist = join("\n",@completed);
 1316:                     $output .= '<p>'.&mt('The following courses/communities were created:').'<ul>';
 1317:                     foreach my $cnum (@completed) {
 1318:                         my $showcourse;
 1319:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1320:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1321:                         } else {
 1322:                             $showcourse = $cnum;
 1323:                         }
 1324:                         my $syllabuslink =
 1325:                             &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
 1326:                         if ($codes{$cnum}) {
 1327:                             $syllabuslink .= ' '.&mt('Unique code: [_1]',$codes{$cnum});
 1328:                         }
 1329:                         $output .= '<li>'.$syllabuslink.'</li>';
 1330:                     }
 1331:                     $output .= '</ul></p>';
 1332:                 }
 1333:                 if (@rejections) {
 1334:                     $rejectedlist = join("\n",@rejections);
 1335:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1336:                     foreach my $cnum (@rejections) {
 1337:                         my $showcourse;
 1338:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1339:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1340:                         } else {
 1341:                             $showcourse = $cnum;
 1342:                         }
 1343:                         $output .= '<li>'.$showcourse.'</li>';
 1344:                     }
 1345:                     $output .= '</ul></p>';
 1346:                 }
 1347:                 if ($notifylist ne '') {
 1348:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
 1349:                                                  $now,'domainmanagers',$sender,
 1350:                                                  $approvedlist,$rejectedlist,$crstype);
 1351:                 }
 1352:             }
 1353:         } else {
 1354:             if (($context eq 'requestauthor') || ($context eq 'requestusername')) {
 1355:                 push(@warn_dels,@changes);
 1356:             }
 1357:         }
 1358:     }
 1359:     if (@existing) {
 1360:         if ($context eq 'course') {
 1361:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
 1362:             foreach my $user (@existing) {
 1363:                 $output .= '<li>'.$user.'</li>';
 1364:             }
 1365:             $output .= '</ul></p>';
 1366:         } elsif ($context eq 'requestauthor') {
 1367:             $output .= '<p>'.&mt('Authoring Space requests from the following users were deleted because one already exists:').'<ul>';
 1368:             foreach my $uname (@existing) {
 1369:                 my $userlink =
 1370:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1371:                 $output .= '<li>'.$userlink.'</li>';
 1372:             }
 1373:             $output .= '</ul></p>';
 1374:         } else {
 1375:             $output .= '<p>'.&mt('The following course/community creation requests were deleted because the course or community has already been created:').'<ul>';
 1376:             foreach my $cnum (@existing) {
 1377:                 my $showcourse;
 1378:                 my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 1379:                 if ($coursehash{'description'} ne '') {
 1380:                     $showcourse = $coursehash{'description'};
 1381:                 } else {
 1382:                     $showcourse = $cnum;
 1383:                 }
 1384:                 $output .= '<li>'.$showcourse.'</li>';
 1385:             }
 1386:             $output .= '</ul></p>';
 1387:         }
 1388:     }
 1389:     if (@missingreq) {
 1390:         if ($context eq 'course') {
 1391:             $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
 1392:             foreach my $user (@missingreq) {
 1393:                 $output .= '<li>'.$user.'</li>';
 1394:             }
 1395:             $output .= '</ul></p>';
 1396:         } elsif ($context eq 'requestauthor') {
 1397:             $output .= '<p>'.&mt('The following requests were ignored because the request is no longer in the queue:').'<ul>';
 1398:             foreach my $uname (@missingreq) {
 1399:                 my $userlink =
 1400:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1401:                 $output .= '<li>'.$userlink.'</li>';
 1402:             }
 1403:             $output .= '</ul></p>';
 1404:         } else {
 1405:             $output .= '<p>'.&mt('The following course/community creation requests were ignored because the request is no longer in the queue:').'<ul>';
 1406:             foreach my $cnum (@missingreq) {
 1407:                 $output .= '<li>'.$cnum.'</li>';
 1408:             }
 1409:             $output .= '</ul></p>';
 1410:         }
 1411:     }
 1412:     if (@invalidusers) {
 1413:         if ($context eq 'course') {
 1414:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1415:             foreach my $user (@invalidusers) {
 1416:                 $output .= '<li>'.$user.'</li>';
 1417:             }
 1418:             $output .= '</ul></p>';
 1419:         } elsif ($context eq 'requestauthor') {
 1420:             $output .= '<p>'.&mt('The following Authoring Space requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1421:             foreach my $uname (@invalidusers) {
 1422:                 my $userlink =
 1423:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1424:                 $output .= '<li>'.$userlink.'</li>';
 1425:             }
 1426:             $output .= '</ul></p>';
 1427:         }
 1428:     }
 1429:     if (@limitexceeded) {
 1430:         if ($context eq 'course') {
 1431:             $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
 1432:             foreach my $user (@limitexceeded) {
 1433:                 $output .= '<li>'.$user.'</li>';
 1434:             }
 1435:             $output .= '</ul></p>';
 1436:         }
 1437:     }
 1438:     if (@nopermissions) {
 1439:         if ($context eq 'course') {
 1440:             $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>';
 1441:             foreach my $cnum (@nopermissions) {
 1442:                 my $showcourse;
 1443:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1444:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1445:                 } else {
 1446:                     $showcourse = $cnum;
 1447:                 }
 1448:                 $output .= '<li>'.$showcourse.'</li>';
 1449:             }
 1450:             $output .= '</ul></p>';
 1451:         } elsif ($context eq 'requestauthor') {
 1452:             $output .= '<p>'.&mt('The following requests could not be processed because the requestor does not have rights to request an Authoring Space:').'<ul>';
 1453:             foreach my $uname (@nopermissions) {
 1454:                 my $userlink =
 1455:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1456:                 $output .= '<li>'.$userlink.'</li>';
 1457:             }
 1458:             $output .= '</ul></p>';
 1459:         }
 1460:     }
 1461:     if (@processing_errors) {
 1462:         if ($context eq 'course') {
 1463:             $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
 1464:             foreach my $user (@processing_errors) {
 1465:                 $output .= '<li>'.$user.'</li>';
 1466:             }
 1467:             $output .= '</ul></p>';
 1468:         } elsif ($context eq 'requestauthor') {
 1469:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
 1470:             foreach my $uname (@processing_errors) {
 1471:                 my $userlink =
 1472:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1473:                 $output .= '<li>'.$userlink.'</li>';
 1474:             }
 1475:             $output .= '</ul></p>';
 1476:         } elsif ($context eq 'requestusername') {
 1477:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
 1478:             foreach my $uname (@processing_errors) {
 1479:                 $output .= '<li>'.$uname.'</li>';
 1480:             }
 1481:             $output .= '</ul></p>';
 1482:         } else {
 1483:             $output .= '<p>'.&mt('The following course/community creation requests could not be processed because an error occurred:').'<ul>';
 1484:             foreach my $cnum (@processing_errors) {
 1485:                 my $showcourse;
 1486:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1487:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1488:                 } else {
 1489:                     $showcourse = $cnum;
 1490:                 }
 1491:                 $output .= '<li>'.$showcourse.'</li>';
 1492:             }
 1493:             $output .= '</ul></p>';
 1494:         }
 1495:     }
 1496:     if (@rejectionerrors) {
 1497:         $output .= '<p>'.&mt('The following course/community creation request rejections could not be fully processed because an error occurred:').'<ul>';
 1498:         foreach my $cnum (@rejectionerrors) {
 1499:             my $showcourse;
 1500:             if (ref($requesthash{$cnum.'_'.$queue})) {
 1501:                 $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1502:             } else {
 1503:                 $showcourse = $cnum;
 1504:             }
 1505:             $output .= '<li>'.$showcourse.'</li>';
 1506:         }
 1507:         $output .= '</ul></p>';
 1508:     }
 1509:     if (@warn_approves || @warn_rejects) {
 1510:         if ($context eq 'course') {
 1511:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
 1512:             foreach my $user (@warn_approves) {
 1513:                 $output .= '<li>'.$user.'</li>';
 1514:             }
 1515:             $output .= '</ul></p>';
 1516:         } elsif ($context eq 'requestauthor') {
 1517:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own author request record:").'<ul>';
 1518:             foreach my $uname (@warn_approves,@warn_rejects) {
 1519:                 my $userlink =
 1520:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1521:                 $output .= '<li>'.$userlink.'</li>';
 1522:             }
 1523:             $output .= '</ul></p>';
 1524:         } elsif ($context eq 'requestusername') {
 1525:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the account request record for the user:").'<ul>';
 1526:             foreach my $uname (@warn_approves,@warn_rejects) {
 1527:                 $output .= '<li>'.$uname.'</li>';
 1528:             }
 1529:             $output .= '</ul></p>';
 1530:         } else {
 1531:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when updating the requestor's own requests record:").'<ul>';
 1532:             foreach my $cnum (@warn_approves,@warn_rejects) {
 1533:                 my $showcourse;
 1534:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1535:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1536:                 } else {
 1537:                     $showcourse = $cnum;
 1538:                 }
 1539:                 $output .= '<li>'.$showcourse.'</li>';
 1540:             }
 1541:             $output .= '</ul></p>';
 1542:         }
 1543:     }
 1544:     if (@warn_dels) {
 1545:         if ($context eq 'requestauthor') {
 1546:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
 1547:             foreach my $uname (@warn_dels) {
 1548:                 my $userlink =
 1549:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1550:                 $output .= '<li>'.$userlink.'</li>';
 1551:             }
 1552:             $output .= '</ul></p>';
 1553:         } elsif ($context eq 'requestusername') {
 1554:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
 1555:             foreach my $item (@warn_dels) {
 1556:                 my ($escuname) = split(/_/,$item);
 1557:                 $output .= '<li>'.&unescape($escuname).'</li>';
 1558:             }
 1559:             $output .= '</ul></p>';            
 1560:         } else {
 1561:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when removing requests from the pending queue:").'<ul>';
 1562:             foreach my $cnum (@warn_dels) {
 1563:                 my $showcourse;
 1564:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1565:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1566:                 } else {
 1567:                     $showcourse = $cnum;
 1568:                 }
 1569:                 $output .= '<li>'.$showcourse.'</li>';
 1570:             }
 1571:             $output .= '</ul></p>';
 1572:         }
 1573:     }
 1574:     return $output;
 1575: }
 1576: 
 1577: sub get_student_counts {
 1578:     my ($cdom,$cnum) = @_;
 1579:     my (%idx,%stucounts);
 1580:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1581:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
 1582:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1583:     while (my ($student,$data) = each(%$classlist)) {
 1584:         if (($data->[$idx{'status'}] eq 'Active') ||
 1585:             ($data->[$idx{'status'}] eq 'Future')) {
 1586:             if ($data->[$idx{'type'}] eq 'selfenroll') {
 1587:                 $stucounts{'selfenroll'} ++;
 1588:             }
 1589:             $stucounts{'allstudents'} ++;
 1590:         }
 1591:     }
 1592:     return (\%stucounts,\%idx,$classlist);
 1593: }
 1594: 
 1595: sub course_creation {
 1596:     my ($dom,$cnum,$context,$details,$logmsg,$clonemsg,$newusermsg,$addresult,
 1597:         $enrollcount,$output,$keysmsg,$domdefs,$longroles,$coderef,$customhash,
 1598:         $callercontext,$user_lh) = @_;
 1599:     unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') && 
 1600:             (ref($longroles) eq 'HASH')) {
 1601:         return ('error: Invalid request');
 1602:     }
 1603:     my ($result,$ownername,$ownerdom);
 1604:     my $crstype = $details->{'crstype'};
 1605:     my $coursedesc = $details->{'cdescr'};
 1606:     my $accessstart = $details->{'accessstart'};
 1607:     my $accessend = $details->{'accessend'};
 1608:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
 1609:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 1610:         if (ref($domconfig{'requestcourses'}{'uniquecode'}) eq 'HASH') {
 1611:             if ($domconfig{'requestcourses'}{'uniquecode'}{$crstype}) {
 1612:                 $details->{'uniquecode'} = 1;
 1613:             }
 1614:         }
 1615:     }
 1616:     if ($context eq 'domain') {
 1617:         $ownername = $details->{'owner'};
 1618:         $ownerdom  = $details->{'domain'};
 1619:     } else {
 1620:         $ownername = $env{'user.name'};
 1621:         $ownerdom  = $env{'user.domain'};
 1622:     }
 1623:     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
 1624:     my $owneremail;
 1625:     my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
 1626:     foreach my $email ('permanentemail','critnotification','notification') {
 1627:         $owneremail = $emails{$email};
 1628:         last if ($owneremail ne '');
 1629:     }
 1630:     my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
 1631:     my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
 1632:                   \%reqdetails,$longroles,$logmsg,$clonemsg,$newusermsg,$addresult,
 1633:                   $enrollcount,$output,$keysmsg,$ownerdom,$ownername,$cnum,$crstype,
 1634:                   $coderef,$callercontext,$user_lh);
 1635:     my $postprocess;
 1636:     if ($cid eq "/$dom/$cnum") {
 1637:         $result = 'created';
 1638:         my $code;
 1639:         if (ref($coderef)) {
 1640:             $code = $$coderef;
 1641:         }
 1642:         $postprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,$result,$ownername,
 1643:                                                            $ownerdom,$fullname,$coursedesc,$code,
 1644:                                                            $accessstart,$accessend,$customhash);
 1645:     } else {
 1646:         $result = 'error: '.$cid;
 1647:     }
 1648:     return ($result,$postprocess);
 1649: }
 1650: 
 1651: sub build_batchcreatehash {
 1652:     my ($dom,$context,$details,$owneremail,$domdefs) = @_;
 1653:     my %batchhash;
 1654:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift tinyurls enrollstart enrollend accessstart accessend sections crosslists users uniquecode};
 1655:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
 1656:         my $emailenc = &escape($owneremail);
 1657:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
 1658:         foreach my $item (@items) {
 1659:             $batchhash{$item} = $details->{$item};
 1660:         }
 1661:         $batchhash{'title'} = $details->{'cdescr'};
 1662:         $batchhash{'coursecode'} = $details->{'instcode'};
 1663:         if ($domdefs->{'officialcredits'} || $domdefs->{'unofficialcredits'}) {
 1664:             $batchhash{'defaultcredits'} = $details->{'defaultcredits'};
 1665:         }
 1666:         $batchhash{'emailenc'} = $emailenc;
 1667:         $batchhash{'adds'} = $details->{'autoadds'};
 1668:         $batchhash{'drops'} = $details->{'autodrops'};
 1669:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
 1670:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
 1671:         if ($details->{'crstype'} eq 'community') {
 1672:             $batchhash{'crstype'} = 'Community';
 1673:         } else {
 1674:             if ($details->{'crstype'} eq 'textbook') {
 1675:                 if ($details->{'clonecrs'} && $details->{'clonedom'}) {
 1676:                     my %clonedfrom = &Apache::lonnet::coursedescription($details->{'clonedom'}.'_'.$details->{'clonecrs'});
 1677:                     $batchhash{'textbook'} = $clonedfrom{'description'};
 1678:                 }
 1679:             }
 1680:             $batchhash{'crstype'} = 'Course';
 1681:         }
 1682:         my ($owner_firstname,$owner_lastname);
 1683:         if ($context eq 'domain') {
 1684:             my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
 1685:                                                            $details->{'owner'},
 1686:                                                            'firstname','lastname');
 1687:             $owner_firstname = $userenv{'firstname'};
 1688:             $owner_lastname = $userenv{'lastname'};
 1689:         } else {
 1690:             $owner_firstname = $env{'environment.firstname'};
 1691:             $owner_lastname = $env{'environment.lastname'};
 1692:         }
 1693:         if (ref($details->{'personnel'}) eq 'HASH') {
 1694:             %{$batchhash{'users'}} = %{$details->{'personnel'}};
 1695:             if (ref($batchhash{'users'}) eq 'HASH') {  
 1696:                 foreach my $userkey (keys(%{$batchhash{'users'}})) {
 1697:                     if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
 1698:                         if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
 1699:                             foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
 1700:                                 my $start = '';
 1701:                                 my $end = '';
 1702:                                 if ($role eq 'st') {
 1703:                                     $start = $details->{'accessstart'};
 1704:                                     $end = $details->{'accessend'};
 1705:                                 }
 1706:                                 $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
 1707:                                 $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
 1708:                             } 
 1709:                         }
 1710:                     }
 1711:                 }
 1712:             }
 1713:         }
 1714:         $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
 1715:         $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
 1716:         $batchhash{'users'}{$owner}{emailenc} = $emailenc;
 1717:         $batchhash{'users'}{$owner}{owneremail} = $owneremail;
 1718:         $batchhash{'setcomment'} = 1;
 1719:     }
 1720:     return %batchhash;
 1721: }
 1722: 
 1723: sub can_clone_course {
 1724:     my ($uname,$udom,$clonecrs,$clonedom,$crstype,$dom,$instcode) = @_;
 1725:     my $canclone;
 1726:     my $ccrole = 'cc';
 1727:     if ($crstype eq 'community') {
 1728:         $ccrole = 'co';
 1729:     }
 1730:     my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
 1731:                                                   [$ccrole],[$clonedom]);
 1732:     if (exists($roleshash{$clonecrs.':'.$clonedom.':'.$ccrole})) {
 1733:         $canclone = 1;
 1734:     } else {
 1735:         my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,
 1736:                                                          ('cloners','internal.coursecode'));
 1737:         my $cloners = $courseenv{'cloners'};
 1738:         my $clonefromcode = $courseenv{'internal.coursecode'};
 1739:         if ($cloners ne '') {
 1740:             my @cloneable = split(',',$cloners);
 1741:             if (grep(/^\*$/,@cloneable)) {
 1742:                 $canclone = 1;
 1743:             } elsif (grep(/^\*:\Q$udom\E$/,@cloneable)) {
 1744:                 $canclone = 1;
 1745:             } elsif (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
 1746:                 $canclone = 1;
 1747:             }
 1748:             unless ($canclone) {
 1749:                 if (($clonefromcode) && ($instcode) && ($clonedom eq $dom)) {
 1750:                     my (%gotdomdefaults,%gotcodedefaults);
 1751:                     foreach my $cloner (@cloneable) {
 1752:                         if (($cloner ne '*') && ($cloner !~ /^\*\:$match_domain$/) &&
 1753:                             ($cloner !~ /^$match_username\:$match_domain$/) && ($cloner ne '')) {
 1754:                             if ($cloner =~ /\=/) {
 1755:                                 my (%codedefaults,@code_order);
 1756:                                 if (ref($gotcodedefaults{$clonedom}) eq 'HASH') {
 1757:                                     if (ref($gotcodedefaults{$clonedom}{'defaults'}) eq 'HASH') {
 1758:                                         %codedefaults = %{$gotcodedefaults{$clonedom}{'defaults'}};
 1759:                                     }
 1760:                                     if (ref($gotcodedefaults{$clonedom}{'order'}) eq 'ARRAY') {
 1761:                                         @code_order = @{$gotcodedefaults{$dom}{'order'}};
 1762:                                     }
 1763:                                 } else {
 1764:                                     &Apache::lonnet::auto_instcode_defaults($clonedom,
 1765:                                                                             \%codedefaults,
 1766:                                                                             \@code_order);
 1767:                                     $gotcodedefaults{$clonedom}{'defaults'} = \%codedefaults;
 1768:                                     $gotcodedefaults{$clonedom}{'order'} = \@code_order;
 1769:                                 }
 1770:                                 if (@code_order > 0) {
 1771:                                     if (&Apache::lonnet::check_instcode_cloning(\%codedefaults,\@code_order,
 1772:                                                                                 $cloner,$clonefromcode,$instcode)) {
 1773:                                         $canclone = 1;
 1774:                                         last; 
 1775:                                     }
 1776:                                 }
 1777:                             }
 1778:                         }
 1779:                     }
 1780:                 }
 1781:             }
 1782:         } else {
 1783:             my %domdefs = &Apache::lonnet::get_domain_defaults($clonedom);
 1784:             if ($domdefs{'canclone'}) {
 1785:                 unless ($domdefs{'canclone'} eq 'none') {
 1786:                     if ($domdefs{'canclone'} eq 'domain') {
 1787:                         if ($udom eq $clonedom) {
 1788:                             $canclone = 1;
 1789:                         }
 1790:                     } elsif (($clonefromcode) && ($instcode) &&
 1791:                              ($clonedom eq $dom)) {
 1792:                         if (&Apache::lonnet::default_instcode_cloning($clonedom,$domdefs{'canclone'},
 1793:                                                                       $clonefromcode,$instcode)) {
 1794:                             $canclone = 1;
 1795:                         }
 1796:                     }
 1797:                 }
 1798:             }
 1799:         }
 1800:         unless ($canclone) {
 1801:             if (&Apache::lonnet::is_course_owner($clonedom,$clonecrs,$uname,$udom)) {
 1802:                 $canclone = 1;
 1803:             }
 1804:         }
 1805:     }
 1806:     return $canclone;
 1807: }
 1808: 
 1809: sub get_processtype {
 1810:     my ($context,$uname,$udom,$isadv,$dom,$crstype,$inststatuses,$domconfig) = @_;
 1811:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
 1812:     if ($uname eq '' || $udom eq '') {
 1813:         $uname = $env{'user.name'};
 1814:         $udom = $env{'user.domain'};
 1815:         $isadv = $env{'user.adv'};
 1816:     }
 1817:     my (%userenv,%settings,$val,@options,$envkey);
 1818:     if ($context eq 'course') {
 1819:         @options = ('autolimit','validate','approval');
 1820:         $envkey = 'requestcourses.'.$crstype;
 1821:         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
 1822:             if (ref($domconfig->{'requestcourses'}->{$crstype}) eq 'HASH') {
 1823:                 %settings = %{$domconfig->{'requestcourses'}->{$crstype}};
 1824:             }
 1825:         }
 1826:     } else {
 1827:         @options = ('automatic','approval');
 1828:         $envkey = 'requestauthor';
 1829:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') { 
 1830:             %settings = %{$domconfig->{'requestauthor'}};
 1831:         }
 1832:     }
 1833:     if (($dom eq $udom) || ($context eq 'requestauthor')) {
 1834:         %userenv =
 1835:             &Apache::lonnet::userenvironment($udom,$uname,$envkey,'inststatus');
 1836:         if ($userenv{$envkey}) {
 1837:             $val = $userenv{$envkey};
 1838:             @{$inststatuses} = ('_custom_');
 1839:         } else {
 1840:             my %alltasks;
 1841:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
 1842:                 $val = $settings{'_LC_adv'};
 1843:                 @{$inststatuses} = ('_LC_adv_');
 1844:             } else {
 1845:                 if ($userenv{'inststatus'} ne '') {
 1846:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
 1847:                 } else {
 1848:                     @{$inststatuses} = ('default');
 1849:                 }
 1850:                 foreach my $status (@{$inststatuses}) {
 1851:                     if (exists($settings{$status})) {
 1852:                         my $value = $settings{$status};
 1853:                         next unless ($value);
 1854:                         unless (exists($alltasks{$value})) {
 1855:                             if (ref($alltasks{$value}) eq 'ARRAY') {
 1856:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
 1857:                                     push(@{$alltasks{$value}},$status);
 1858:                                 }
 1859:                             } else {
 1860:                                 @{$alltasks{$value}} = ($status);
 1861:                             }
 1862:                         }
 1863:                     }
 1864:                 }
 1865:                 my $maxlimit = 0;
 1866:                 if ($context eq 'course') {
 1867:                     foreach my $key (sort(keys(%alltasks))) {
 1868:                         if ($key =~ /^autolimit=(\d*)$/) {
 1869:                             if ($1 eq '') {
 1870:                                 $val ='autolimit=';
 1871:                                 last;
 1872:                             } elsif ($1 > $maxlimit) {
 1873:                                 $maxlimit = $1;
 1874:                             }
 1875:                         }
 1876:                     }
 1877:                 }
 1878:                 if (($context eq 'requestauthor') || (!$val)) {
 1879:                     if ($context eq 'course' && $maxlimit) {
 1880:                         $val = 'autolimit='.$maxlimit;
 1881:                     } else {
 1882:                         foreach my $option (@options) {
 1883:                             if ($alltasks{$option}) {
 1884:                                 $val = $option;
 1885:                                 last;
 1886:                             }
 1887:                         }
 1888:                     }
 1889:                 }
 1890:             }
 1891:         }
 1892:     } else {
 1893:         %userenv = &Apache::lonnet::userenvironment($udom,$uname,'reqcrsotherdom.'.$crstype);
 1894:         if ($userenv{'reqcrsotherdom.'.$crstype}) {
 1895:             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
 1896:             my $optregex = join('|',@options);
 1897:             foreach my $item (@doms) {
 1898:                 my ($extdom,$extopt) = split(':',$item);
 1899:                 if ($extdom eq $dom) {
 1900:                     if ($extopt =~ /^($optregex)(=?\d*)$/) {
 1901:                         $val = $1.$2;
 1902:                     }
 1903:                     last;
 1904:                 }
 1905:             }
 1906:             @{$inststatuses} = ('_external_');
 1907:         }
 1908:     }
 1909:     return $val;
 1910: }
 1911: 
 1912: sub queued_selfenrollment {
 1913:     my ($notitle) = @_;
 1914:     my $output;
 1915:     my %selfenrollrequests = &Apache::lonnet::dump('selfenrollrequests');
 1916:     my %reqs_by_date;
 1917:     foreach my $item (keys(%selfenrollrequests)) {
 1918:         if (ref($selfenrollrequests{$item}) eq 'HASH') {
 1919:             if ($selfenrollrequests{$item}{'status'} eq 'request') {
 1920:                 if ($selfenrollrequests{$item}{'timestamp'}) {
 1921:                     push(@{$reqs_by_date{$selfenrollrequests{$item}{'timestamp'}}},$item);
 1922:                 }
 1923:             }
 1924:         }
 1925:     }
 1926:     if (keys(%reqs_by_date)) {
 1927:         unless ($notitle) {
 1928:             $output .= '<br /><b>'.&mt('Enrollment requests pending Course Coordinator approval').'</b><br />';
 1929:         }
 1930:         $output .= &Apache::loncommon::start_data_table().
 1931:                    &Apache::loncommon::start_data_table_header_row().
 1932:                    '<th>'.&mt('Date requested').'</th><th>'.&mt('Course title').'</th>'.
 1933:                    '<th>'.&mt('User role').'</th><th>'.&mt('Section').'</th>'.
 1934:                    &Apache::loncommon::end_data_table_header_row();
 1935:         my @sorted = sort { $a <=> $b } (keys(%reqs_by_date));
 1936:         foreach my $item (@sorted) {
 1937:             if (ref($reqs_by_date{$item}) eq 'ARRAY') {
 1938:                 foreach my $crs (@{$reqs_by_date{$item}}) {
 1939:                     my %courseinfo = &Apache::lonnet::coursedescription($crs);
 1940:                     my $usec = $selfenrollrequests{$crs}{'section'};
 1941:                     my $rolename = &Apache::lonnet::plaintext('st',$courseinfo{'type'},$crs);
 1942:                     if ($usec eq '') {
 1943:                         $usec = &mt('No section');
 1944:                     }
 1945:                     $output .= &Apache::loncommon::start_data_table_row().
 1946:                                '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
 1947:                                '<td>'.$courseinfo{'description'}.'</td>'.
 1948:                                '<td>'.$rolename.'</td><td>'.$usec.'</td>'.
 1949:                                &Apache::loncommon::end_data_table_row();
 1950:                 }
 1951:             }
 1952:         }
 1953:         $output .= &Apache::loncommon::end_data_table();
 1954:     }
 1955:     return $output;
 1956: }
 1957: 
 1958: sub update_coursereq_status {
 1959:     my ($reqhash,$dom,$cnum,$reqstatus,$context,$udom,$uname) = @_;
 1960:     my ($storeresult,$statusresult,$output);
 1961:     my $requestkey = $dom.'_'.$cnum;
 1962:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 1963:         $storeresult = &Apache::lonnet::store_userdata($reqhash,$requestkey,
 1964:                                                        'courserequests',$udom,$uname);
 1965:         if ($storeresult eq 'ok') {
 1966:             my %status = (
 1967:                              'status:'.$dom.':'.$cnum => $reqstatus,
 1968:                          );
 1969:             $statusresult = &Apache::lonnet::put('courserequests',\%status,$udom,$uname);
 1970:         }
 1971:     } else {
 1972:         $storeresult = 'error: invalid requestkey format';
 1973:     }
 1974:     if ($storeresult ne 'ok') {
 1975:         $output = &mt('An error occurred saving a record of the details of your request: [_1].',$storeresult);
 1976:         if ($context eq 'domain') {
 1977:             $output .= "\n";  
 1978:         } else {
 1979:             $output =  '<span class="LC_warning">'.$output.'</span><br />';
 1980:         }
 1981:         &Apache::lonnet::logthis("Error saving course request - $requestkey for $uname:$udom - $storeresult");
 1982:     } elsif ($statusresult ne 'ok') {
 1983:         $output = &mt('An error occurred saving a record of the status of your request: [_1].',$statusresult);
 1984:         if ($context eq 'domain') {
 1985:             $output .= "\n";
 1986:         } else {
 1987:             $output = '<span class="LC_warning">'.$output.'</span><br />';
 1988:         }
 1989:         &Apache::lonnet::logthis("Error saving course request status for $requestkey (for $uname:$udom) - $statusresult");
 1990:     }
 1991:     return ($storeresult,$output);
 1992: }
 1993: 
 1994: sub process_official_reqs {
 1995:     my ($context,$dom,$dcname,$dcdom) = @_;
 1996:     my $reqsnamespace = 'courserequestqueue';
 1997:     my %requesthash =
 1998:         &Apache::lonnet::dump_dom($reqsnamespace,$dom,'_pending');
 1999:     my (%newcids,%longroles,%stillpending);
 2000:     my @courseroles = ('cc','in','ta','ep','ad','st');
 2001:     foreach my $role (@courseroles) {
 2002:         $longroles{$role}=&Apache::lonnet::plaintext($role);
 2003:     }
 2004:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 2005:     my ($output,$linefeed,$user_lh);
 2006:     if ($context eq 'auto') {
 2007:         $linefeed = "\n";
 2008:         $user_lh = &Apache::loncommon::user_lang($dcname,$dcdom);
 2009:     } else {
 2010:         $linefeed = '<br />'."\n";
 2011:     }
 2012:     foreach my $key (keys(%requesthash)) {
 2013:         my ($cnum,$status) = split('_',$key);
 2014:         next if (&Apache::lonnet::homeserver($cnum,$dom) ne 'no_host');
 2015:         if (ref($requesthash{$key}) eq 'HASH') {
 2016:             my $ownername = $requesthash{$key}{'ownername'};
 2017:             my $ownerdom = $requesthash{$key}{'ownerdom'};
 2018:             next if (&Apache::lonnet::homeserver($ownername,$ownerdom) eq 'no_host');
 2019:             my $inststatus;
 2020:             my %userenv =
 2021:                 &Apache::lonnet::get('environment',['inststatus'],
 2022:                                      $ownerdom,$ownername);
 2023:             my ($tmp) = keys(%userenv);
 2024:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 2025:                 $inststatus = $userenv{'inststatus'};
 2026:             } else {
 2027:                 undef(%userenv);
 2028:             }
 2029:             my $reqkey = $dom.'_'.$cnum;
 2030:             my %history = &Apache::lonnet::restore($reqkey,'courserequests',
 2031:                                                    $ownerdom,$ownername);
 2032:             if (ref($history{'details'}) eq 'HASH') {
 2033:                 my $instcode = $history{'details'}{'instcode'};
 2034:                 my $crstype = $history{'details'}{'crstype'};
 2035:                 my $reqtime = $history{'details'}{'reqtime'};
 2036:                 my $cdescr = $history{'details'}{'cdescr'};
 2037:                 my @currsec;
 2038:                 my $sections = $history{'details'}{'sections'};
 2039:                 if (ref($sections) eq 'HASH') {
 2040:                     foreach my $i (sort(keys(%{$sections}))) {
 2041:                         if (ref($sections->{$i}) eq 'HASH') {
 2042:                             my $sec = $sections->{$i}{'inst'};
 2043:                             if (!grep(/^\Q$sec\E$/,@currsec)) {
 2044:                                 push(@currsec,$sec);
 2045:                             }
 2046:                         }
 2047:                     }
 2048:                 }
 2049:                 my $instseclist = join(',',@currsec);
 2050:                 my ($validationchk,$disposition,$reqstatus,$message,
 2051:                     $validation,$validationerror);
 2052:                 $validationchk =
 2053:                     &Apache::lonnet::auto_courserequest_validation($dom,
 2054:                         $ownername.':'.$ownerdom,$crstype,$inststatus,
 2055:                         $instcode,$instseclist);
 2056:                 if ($validationchk =~ /:/) {
 2057:                     ($validation,$message) = split(':',$validationchk);
 2058:                 } else {
 2059:                     $validation = $validationchk;
 2060:                 }
 2061:                 if ($validation =~ /^error(.*)$/) {
 2062:                     $disposition = 'approval';
 2063:                     $validationerror = $1;
 2064:                 } else {
 2065:                     $disposition = $validation;
 2066:                 }
 2067:                 $reqstatus = $disposition;
 2068:                 if ($disposition eq 'process') {
 2069:                     my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,$code);
 2070:                     my $clonemsg = [];
 2071:                     my %customitems;
 2072:                     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
 2073:                     my $inprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'process',$ownername,
 2074:                                                                         $ownerdom,$fullname,$cdescr);
 2075:                     if (ref($inprocess) eq 'HASH') {
 2076:                         foreach my $key (keys(%{$inprocess})) {
 2077:                             if (exists($history{'details'}{$key})) {
 2078:                                 $customitems{$key} = $history{'details'}{$key};
 2079:                             }
 2080:                         }
 2081:                     }
 2082:                     if ($history{'details'}{'clonecrs'}) {
 2083:                         $customitems{'_LC_clonefrom'} = $history{'details'}{'clonedom'}.'_'.$history{'details'}{'clonecrs'};
 2084:                     }
 2085:                     my ($result,$postprocess) = 
 2086:                         &course_creation($dom,$cnum,'domain',$history{'details'},\$logmsg,$clonemsg,\$newusermsg,
 2087:                                          \$addresult,\$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles,
 2088:                                          \$code,\%customitems,$context,$user_lh);
 2089:                     if ($result eq 'created') {
 2090:                         $disposition = 'created';
 2091:                         $reqstatus = 'created';
 2092:                         my $cid = $dom.'_'.$cnum;
 2093:                         push(@{$newcids{$instcode}},$cid);
 2094:                         if ($dcname && $dcdom) {
 2095:                             my $firsturl = &Apache::lonnet::course_portal_url($cnum,$dom);
 2096:                             my $beneficiary = 'pendingrequestor';
 2097:                             my $now = time;
 2098:                             my $owner = $ownername.':'.$ownerdom;
 2099:                             my $approvedmsg =
 2100:                                 [{
 2101:                                     mt => 'Your requested course: [_1], (queued pending validation) has now been created.',
 2102:                                     args => [$cdescr],
 2103:                                  }];
 2104:                             if ((ref($clonemsg) eq 'ARRAY') && (@{$clonemsg})) {
 2105:                                 push(@{$approvedmsg},@{$clonemsg});
 2106:                             }
 2107:                             push(@{$approvedmsg},
 2108:                                  {
 2109:                                     mt   => 'Visit [_1] to log-in and access the course.',
 2110:                                     args => [$firsturl],
 2111:                                  },
 2112:                                  {
 2113:                                     mt => 'If currently logged-in to LON-CAPA, log-out and log-in again to select your new course role.',
 2114:                                     args => [],
 2115:                                  }
 2116:                             );
 2117:                             my $sender = $dcname.':'.$dcdom;
 2118:                             if (ref($postprocess) eq 'HASH') {
 2119:                                 if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
 2120:                                     foreach my $item (@{$postprocess->{'createdmsg'}}) {
 2121:                                         if (ref($item) eq 'HASH') {
 2122:                                              if ($item->{'mt'} ne '') {
 2123:                                                  push(@{$approvedmsg},$item);
 2124:                                              }
 2125:                                         }
 2126:                                     }
 2127:                                 }
 2128:                                 if (ref($postprocess->{'createdactions'}) eq 'HASH') {
 2129:                                     if (ref($postprocess->{'createdactions'}{'environment'}) eq 'HASH') {
 2130:                                         &postprocess_crsenv($dom,$cnum,$postprocess->{'createdactions'}{'environment'});
 2131:                                     }
 2132:                                 }
 2133:                             }
 2134:                             &send_selfserve_notification($owner,$approvedmsg,
 2135:                                                          $cid,$cdescr,$now,
 2136:                                                          $beneficiary,$sender,
 2137:                                                          undef,undef,$crstype);
 2138:                         }
 2139:                     }
 2140:                 } elsif ($disposition eq 'rejected') {
 2141:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] rejected when validating.',$instcode,$ownername.':'.$ownerdom,$inststatus).$linefeed;
 2142:                 } elsif ($disposition eq 'approval') {
 2143:                     $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;
 2144: 
 2145:                     my $requestid = $cnum.'_'.$disposition;
 2146:                     my $request = {
 2147:                             $requestid => {
 2148:                                             timestamp   => $reqtime,
 2149:                                             crstype     => $crstype,
 2150:                                             ownername   => $ownername,
 2151:                                             ownerdom    => $ownerdom,
 2152:                                             description => $cdescr,
 2153:                                           },
 2154:                           };
 2155:                     my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,$dom);
 2156:                     unless ($putresult eq 'ok') {
 2157:                         $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;
 2158:                     }
 2159:                 } elsif ($disposition eq 'pending') {
 2160:                     my $instcode = $requesthash{$key}{'instcode'};
 2161:                     my $description = $requesthash{$key}{'description'};
 2162:                     my $timestamp = $requesthash{$key}{'timestamp'};
 2163:                     my $entry = $cnum.':'.$ownername.':'.$ownerdom.':'.
 2164:                                 $instcode.':'.$description;
 2165:                     if (ref($stillpending{$timestamp}) eq 'ARRAY') {
 2166:                         push(@{$stillpending{$timestamp}},$entry);
 2167:                     } else {
 2168:                         $stillpending{$timestamp} = [$entry];
 2169:                     }
 2170:                 }
 2171:                 unless ($disposition eq 'pending') {
 2172:                     my ($statusresult,$output) =
 2173:                         &update_coursereq_status(\%requesthash,$dom,$cnum,
 2174:                                                  $reqstatus,'domain',$ownerdom,
 2175:                                                  $ownername);
 2176:                     unless (&Apache::lonnet::del_dom($reqsnamespace,[$cnum.'_pending'],$dom) eq 'ok') {
 2177:                         $output .= &mt('An error occurred when removing the request for [_1] submitted by [_2] from the pending queue.',$instcode,$ownername.':'.$ownerdom).$linefeed;
 2178:                     }
 2179:                 }
 2180:             }
 2181:         }
 2182:     }
 2183:     foreach my $key (sort(keys(%newcids))) {
 2184:         if (ref($newcids{$key}) eq 'ARRAY') {
 2185:             $output .= "created course from queued request: $key - ".join(', ',@{$newcids{$key}}).$linefeed;
 2186:             my $newcourse = &LONCAPA::escape($key.':'.$newcids{$key});
 2187:         }
 2188:     }
 2189:     unless ($context eq 'auto') {
 2190:         if (keys(%stillpending) > 0) {
 2191:             $output .= '<form method="post" name="changequeue" action="/adm/createcourse" />'."\n".
 2192:                        '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 2193:                        '<input type="hidden" name="phase" value="requestchange" />'.
 2194:                        '<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>'.
 2195:                        &build_queue_display($dom,'stillpending',\%stillpending).
 2196:                        '<br /><input type="hidden" name="queue" value="pending" />'."\n".
 2197:                        '<input type="submit" name="processqueue" value="'.&mt('Save').'" />'.
 2198:                        '</form>';
 2199:         }
 2200:     }
 2201:     return $output;
 2202: }
 2203: 
 2204: sub postprocess_crsenv {
 2205:     my ($dom,$cnum,$postprocessenv) = @_;
 2206:     if (ref($postprocessenv) eq 'HASH') {    
 2207:         my $cid = $dom.'_'.$cnum;
 2208:         my %settablecrsenv = (
 2209:                               'internal.selfenroll_types'        => 1,
 2210:                               'internal.selfenroll_registered'   => 1,
 2211:                               'internal.selfenroll_section'      => 1,
 2212:                               'internal.selfenroll_start_access' => 1,
 2213:                               'internal.selfenroll_end_access'   => 1,
 2214:                               'internal.selfenroll_limit'        => 1,
 2215:                               'internal.selfenroll_cap'          => 1,
 2216:                               'internal.selfenroll_approval'     => 1,
 2217:                               'internal.selfenroll_notifylist'   => 1,
 2218:                              );
 2219:         my %needcrsidput = (
 2220:                               'internal.selfenroll_types'      => 1,
 2221:                               'internal.selfenroll_start_date' => 1,
 2222:                               'internal. selfenroll_end_date'  => 1,
 2223:                            );
 2224:         my (@needupdate,%newcrsenv);
 2225:         foreach my $key (keys(%{$postprocessenv})) { 
 2226:             if ($settablecrsenv{$key}) {
 2227:                 $newcrsenv{$key} = $postprocessenv->{$key};
 2228:                 if ($needcrsidput{$key}) {
 2229:                     push(@needupdate,$key); 
 2230:                 }
 2231:             }
 2232:             if (keys(%newcrsenv)) {
 2233:                 my $putresult = &Apache::lonnet::put('environment',\%newcrsenv,$dom,$cnum);
 2234:                 if ($putresult eq 'ok') {
 2235:                     if (@needupdate) {
 2236:                         my %crsinfo =
 2237:                             &Apache::lonnet::courseiddump($dom,'.',1,'.','.',$cnum,undef,undef,'.');
 2238:                         if (ref($crsinfo{$cid}) eq 'HASH') {
 2239:                             foreach my $key (@needupdate) {
 2240:                                 $crsinfo{$cid}{$key} = $newcrsenv{$key};
 2241:                             }
 2242:                             my $chome = &Apache::lonnet::homeserver($cnum,$dom);
 2243:                             &Apache::lonnet::courseidput($dom,\%crsinfo,$chome,'notime');
 2244:                         }
 2245:                     }
 2246:                 }
 2247:             }
 2248:         }
 2249:     }
 2250:     return;
 2251: }
 2252: 
 2253: 
 2254: sub requestcourses_validation_types {
 2255:     my @items = ('url','fields','button','markup');
 2256:     my %names =  &Apache::lonlocal::texthash (
 2257:             url      => 'Web address of validation server/script',
 2258:             fields   => 'Form fields to send to validator',
 2259:             button   => 'Text for validation button',
 2260:             markup   => 'Validation description (HTML)',
 2261:     );
 2262:     my @fields = ('owner','course','coursetype','description');
 2263:     return (\@items,\%names,\@fields);
 2264: }
 2265: 
 2266: sub is_active_author {
 2267:     if ($env{'user.role.au./'.$env{'user.domain'}.'/'} =~ /^(\d*)\.(\d*)$/) {
 2268:         if ((!$1 || $1 < time) &&
 2269:             (!$2 || $2 > time)) {
 2270:             return 1;
 2271:         }
 2272:     }
 2273: }
 2274: 
 2275: sub author_prompt {
 2276:     my ($is_active_author,$offer_author);
 2277:     if ($env{'environment.canrequest.author'}) {
 2278:         unless (&is_active_author()) {
 2279:             unless (&reqauthor_check() =~ /^approval:\d+$/) {
 2280:                 $offer_author = 1;
 2281:             }
 2282:         }
 2283:     }
 2284:     return $offer_author;
 2285: }
 2286: 
 2287: sub reqauthor_check {
 2288:     my $queued = $env{'environment.requestauthorqueued'};
 2289:     my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
 2290:                                          $env{'user.domain'},$env{'user.name'});
 2291:     my $reqstatus = $reqauthor{'author_status'};
 2292:     if (($reqstatus eq '' && $queued ne '') ||
 2293:         ($env{'environment.requestauthorqueued'} !~ /^\Q$reqstatus\E/)) {
 2294:         if (ref($reqauthor{'author'}) eq 'HASH') {
 2295:             $queued = $reqstatus.':'.$reqauthor{'author'}{'timestamp'};
 2296:         } else {
 2297:             undef($queued);
 2298:         }
 2299:         &Apache::lonnet::appenv({'environment.requestauthorqueued' => $queued});
 2300:     }
 2301:     return $queued;
 2302: }
 2303: 
 2304: sub process_reqauthor {
 2305:     my ($dispositionref,$updateref) = @_;
 2306:     if (&is_active_author()) {
 2307:         return '<span class="LC_warning">'.
 2308:                 &mt('An Authoring Space has already been assigned to you.').'<br />'.
 2309:                 &mt('Please select the Author role from your [_1]roles page[_2].','<a href="/adm/roles">',
 2310:                 '</a>').'</span>';
 2311:     }
 2312:     unless ($env{'environment.canrequest.author'}) {
 2313:         return '<span class="LC_warning">'.
 2314:                 &mt('You do not currently have rights to request an Authoring Space.').'<br />'.
 2315:                 &mt('Please contact the [_1]helpdesk[_2] for assistance.','<a href="/adm/helpdesk">',
 2316:                 '</a>').'</span>';
 2317:     }
 2318:     my $queued = &reqauthor_check();
 2319:     if ($queued =~ /^approval:(\d+)$/) {
 2320:         my $timestamp = $1;
 2321:         return '<span class="LC_info">'.
 2322:                &mt('A request for Authoring Space submitted on [_1] is awaiting approval',
 2323:                &Apache::lonlocal::locallocaltime($timestamp)).
 2324:                '</span>';
 2325:     } elsif ($queued =~ /^approved:(\d+)$/) {
 2326:         my $timestamp = $1;   
 2327:         my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
 2328:                                                       ['active'],['au'],[$env{'user.domain'}]);
 2329:         if (keys(%roleshash) > 0) {
 2330:             return '<span class="LC_info">'.
 2331:                    &mt('A request for Authoring Space submitted on [_1] has been approved.',
 2332:                    &Apache::lonlocal::locallocaltime($timestamp)).
 2333:                    '</span>';
 2334:         }
 2335:     }
 2336:     my ($output,@inststatuses,%domconfig);
 2337:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestauthor'],
 2338:                                           $env{'user.domain'});
 2339:     my $val = &get_processtype('requestauthor',$env{'user.name'},$env{'user.domain'},
 2340:                                $env{'user.adv'},$env{'user.domain'},undef,
 2341:                                \@inststatuses,\%domconfig);
 2342:     my $now = time;
 2343:     if ($val eq 'automatic') {
 2344:         my $start = $now-1;
 2345:         if (&Apache::lonnet::assignrole($env{'user.domain'},$env{'user.name'},'/'.$env{'user.domain'}.'/',
 2346:                                         'au',undef,$start,undef,undef,'requestauthor') eq 'ok') {
 2347:             $output = '<span class="LC_info">'.
 2348:                       &mt('Access to Authoring Space has been activated').'</span><br />';
 2349:                       &Apache::lonroles::update_session_roles();
 2350:             &Apache::lonnet::appenv({'user.update.time'  => $now});
 2351:             if (ref($updateref)) {
 2352:                 $$updateref = $now;
 2353:             }
 2354:             if (ref($dispositionref)) {
 2355:                 $$dispositionref = 'created';
 2356:             }
 2357:         } else {
 2358:             $output = '<span class="LC_info">'.
 2359:                       &mt('An error occurred while activating your access to Authoring Space');
 2360:         }
 2361:     } elsif ($val eq 'approval') {
 2362:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($env{'user.domain'});
 2363:         if (&Apache::lonnet::put('requestauthorqueue',{ $env{'user.name'}.'_'.$val => $now },
 2364:                                  $env{'user.domain'},$domconfiguser) eq 'ok') {
 2365:             my %userrequest = (
 2366:                 author => {
 2367:                             timestamp   => $now,
 2368:                             status      => $val,
 2369:                           },
 2370:                 author_status => $val,
 2371:             );
 2372:             my $req_notifylist;
 2373:             if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2374:                 if (ref($domconfig{'requestauthor'}{'notify'}) eq 'HASH') {
 2375:                     my $req_notifylist = $domconfig{'requestauthor'}{'notify'}{'approval'};
 2376:                     if ($req_notifylist) {
 2377:                         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 2378:                                                                      $env{'user.domain'});
 2379:                         my $sender = $env{'user.name'}.':'.$env{'user.domain'};
 2380:                         my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
 2381:                         &send_selfserve_notification($req_notifylist,
 2382:                                                      "$fullname ($env{'user.name'}:$env{'user.domain'})",
 2383:                                                      undef,$domdesc,$now,'authorreq',$sender);
 2384:                     }
 2385:                 }
 2386:             }
 2387:             my $userresult =
 2388:                 &Apache::lonnet::put('requestauthor',\%userrequest,$env{'user.domain'},$env{'user.name'});
 2389:             $output = '<span class="LC_info">'.
 2390:                       &mt('Your request for Authoring Space has been submitted for approval.').
 2391:                       '</span>';
 2392:             &Apache::lonnet::appenv({'environment.requestauthorqueued' => $val.':'.$now});
 2393:         } else {
 2394:             $output = '<span class="LC_info">'.
 2395:                       &mt('An error occurred saving your request for Authoring Space.').
 2396:                       '</span>';
 2397:         }
 2398:     }
 2399:     return $output;
 2400: }
 2401: 
 2402: 1;

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