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

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

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