Annotation of loncom/interface/loncoursequeueadmin.pm, revision 1.44

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

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