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

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.64    ! raeburn     4: # $Id: loncoursequeueadmin.pm,v 1.63 2022/11/23 02:55:37 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.57      raeburn   108:                       args => ["\n$contextdesc\n"],
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.57      raeburn   113:                       mt  => 'Course/Community creation requests in the following domain: [_1] have been reviewed.',
                    114:                       args => ["\n$contextdesc\n"],
1.30      raeburn   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.57      raeburn   122:                       mt  => 'Authoring requests in the following domain: [_1] have been reviewed.',
                    123:                       args => ["\n$contextdesc\n"],
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,{
1.57      raeburn   131:                       mt  => 'Account requests in the following domain: [_1] have been reviewed.',
                    132:                       args => ["\n$contextdesc\n"],
1.43      raeburn   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:         if (ref($textstr) eq 'ARRAY') {
                    149:             push(@rawmsg,@{$textstr});
                    150:         }
                    151:     } elsif ($context eq 'courserequestor') {
1.12      raeburn   152:         if ($crstype eq 'Community') {
                    153:             $rawsubj = 'Community request';
                    154:             $msgtxt = 'Your request for creation of the following community: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
                    155:         } else {
                    156:             $rawsubj = 'Course request';
                    157:             $msgtxt = 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
                    158:         }
1.1       raeburn   159:         push(@rawmsg,{
1.12      raeburn   160:                       mt  => $msgtxt,
1.2       raeburn   161:                       args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
1.1       raeburn   162: 
                    163:                      });
                    164:         if (ref($textstr) eq 'ARRAY') {
                    165:             push(@rawmsg,@{$textstr});
                    166:         }
1.26      raeburn   167:     } elsif ($context eq 'pendingrequestor') {
                    168:         if ($crstype eq 'Community') {
                    169:             $rawsubj = 'Community request';
                    170:         } else {
                    171:             $rawsubj = 'Processed course request';
                    172:         }
                    173:         if (ref($textstr) eq 'ARRAY') {
                    174:             push(@rawmsg,@{$textstr});
                    175:         }
1.1       raeburn   176:     } elsif ($context eq 'coursereq') {
1.12      raeburn   177:         if ($crstype eq 'community') {
                    178:             $rawsubj = 'Community request to review';
                    179:             $msgtxt = 'Creation of the following community: [_1]was requested by [_2] on [_3].';
                    180:         } else {
                    181:             $rawsubj = 'Course request to review';
                    182:             $msgtxt = 'Creation of the following course: [_1]was requested by [_2] on [_3].';
                    183:         }
1.1       raeburn   184:         push(@rawmsg,{
1.12      raeburn   185:                       mt  => $msgtxt,
1.2       raeburn   186:                       args => ["\n  $contextdesc\n",$textstr,$timestamp],
1.1       raeburn   187:                      },
                    188:                      {
1.12      raeburn   189:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Course and community creation -> Approve or reject requests[_3]to display a list of pending requests, which you can either approve or reject.',
1.31      raeburn   190:                       args => ["\n","\n\n","\n\n"],
1.1       raeburn   191:                      });
                    192:     } elsif ($context eq 'selfenrollreq') {
                    193:         $rawsubj = 'Self-enrollment request';
1.12      raeburn   194:         if ($crstype eq 'community') {
1.31      raeburn   195:             $msgtxt = 'Enrollment in the following community: [_1]was requested by [_2] on [_3].'
1.12      raeburn   196:         } else {
1.31      raeburn   197:             $msgtxt = 'Enrollment in the following course: [_1]was requested by [_2] on [_3].'
1.12      raeburn   198:         }
1.1       raeburn   199:         push(@rawmsg,{
1.12      raeburn   200:                       mt  => $msgtxt,
1.2       raeburn   201:                       args => ["\n  $contextdesc\n",$textstr,$timestamp."\n"],
                    202:                      });
1.12      raeburn   203:         my $directions;  
                    204:         if ($crstype eq 'community') {
                    205:             $directions = 'As Coordinator, use: [_1]Main Menu -> Manage Community Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
1.2       raeburn   206:         } else {
1.12      raeburn   207:             $directions = 'As Course Coordinator, use: [_1]Main Menu -> Manage Course Users -> Enrollment Requests[_2]to display a list of pending enrollment requests, which you can either approve or reject.';
                    208:         }
                    209:         push(@rawmsg,
1.1       raeburn   210:                      {
1.12      raeburn   211:                       mt   => $directions,
1.2       raeburn   212:                       args => ["  \n\n","\n"],
1.1       raeburn   213:                      });
1.30      raeburn   214:     } elsif ($context eq 'authorreq') {
1.42      bisitz    215:         $rawsubj = 'Authoring Space request to review';
1.31      raeburn   216:         $msgtxt = 'Assignment of an author role in the [_1] domain[_2]was requested by [_3] on [_4].';
1.30      raeburn   217:         push(@rawmsg,{
                    218:                       mt  => $msgtxt,
                    219:                       args => [$contextdesc,"\n",$textstr,$timestamp],
                    220:                      },
                    221:                      {
1.42      bisitz    222:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users -> Authoring Space requests[_3]to display a list of pending requests, which you can either approve or reject.',
1.30      raeburn   223:                       args => ["\n","\n\n  ","\n\n"],
                    224:                      });
                    225:     } elsif ($context eq 'requestauthor') {
1.42      bisitz    226:         $rawsubj = 'Authoring Space request';
                    227:         $msgtxt = 'Your request for an Authoring Space requested on [_1]has been reviewed by a Domain Coordinator.';
1.30      raeburn   228:         push(@rawmsg,{
                    229:                       mt  => $msgtxt,
                    230:                       args => [$timestamp."\n"],
                    231:                      });
                    232:         if (ref($textstr) eq 'ARRAY') {
                    233:             push(@rawmsg,@{$textstr});
                    234:         }
1.43      raeburn   235:     } elsif ($context eq 'usernamereq') {
                    236:         $rawsubj = 'LON-CAPA account request';
                    237:         $msgtxt = 'Creation of a LON-CAPA account in the [_1] domain[_2]was requested by [_3] on [_4].';
                    238:         push(@rawmsg,{
                    239:                       mt  => $msgtxt,
                    240:                       args => [$contextdesc,"\n",$textstr,$timestamp],
                    241:                      },
                    242:                      {
                    243:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users
                    244:  -> LON-CAPA account requests[_3]to display a list of pending requests, which you can either approve or reject.',
                    245:                       args => ["\n","\n\n  ","\n\n"],
                    246:                      });
                    247:     } elsif ($context eq 'requestusername') {
                    248:         $rawsubj = 'LON-CAPA account request';
                    249:         $msgtxt = 'Your request for a LON-CAPA account requested on [_1]has been reviewed by a Domain Coordinator.';
                    250:         push(@rawmsg,{
                    251:                       mt  => $msgtxt,
                    252:                       args => [$timestamp."\n"],
                    253:                      });
                    254:         if (ref($textstr) eq 'ARRAY') {
                    255:             push(@rawmsg,@{$textstr});
                    256:         }
1.39      raeburn   257:     } elsif ($context eq 'uniquecode') {
                    258:         $rawsubj = 'Course Identifier';
                    259:         if (ref($textstr) eq 'ARRAY') {
                    260:             push(@rawmsg,@{$textstr});
                    261:         }
1.41      raeburn   262:     } elsif ($context eq 'queuedcrsreq') {
                    263:         $rawsubj = 'Course Request Queued';
                    264:         if (ref($textstr) eq 'ARRAY') {
                    265:             push(@rawmsg,@{$textstr});
                    266:         }
                    267:     } elsif ($context eq 'createdcrsreq') {
                    268:         $rawsubj = 'Course Creation Information';
                    269:         if (ref($textstr) eq 'ARRAY') {
                    270:             push(@rawmsg,@{$textstr});
                    271:         }
1.63      raeburn   272:     } elsif ($context eq 'othdomroleuser') {
                    273:         my $linktext = 'Roles';
                    274:         if (&Apache::loncommon::show_course()) {
                    275:             $linktext = 'Courses';
                    276:         }
                    277:         $rawsubj = 'Role Assignment Approval';
                    278:         push(@rawmsg,{
                    279:                       mt  => 'A domain different to your own LON-CAPA domain ([_1]) wants to assign you a role in their domain.',
                    280:                       args => ["\n$contextdesc\n"],
                    281:                      },
                    282:                      {
                    283:                       mt =>"[_1]Click $linktext at top right, then click 'Manage Role Requests' in the gray Functions bar ".
                    284:                            "to display a list of pending role assignments in other domain(s), which you can either accept or reject.",
                    285:                       args => ["\n\n"],
                    286:                      });
                    287:     } elsif ($context eq 'othdomroledc') {
                    288:         $rawsubj = 'Role Assignment Authorization';
                    289:         push(@rawmsg,{
                    290:                       mt  => 'Another LON-CAPA domain wants to assign a role in their domain to a user from your domain.',
                    291:                       args => [],
                    292:                      },
                    293:                      {
                    294:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users
                    295:  -> Queued Role Assignments (this domain) [_3]to display a list of pending requests, which you can either approve or reject.',
                    296:                       args => ["\n","\n\n  ","\n\n"],
                    297:                      });
1.64    ! raeburn   298:     } elsif ($context eq 'othdombydc') {
        !           299:         $rawsubj = 'Status of Role Assignment Requests';
        !           300:         push(@rawmsg,{
        !           301:                       mt =>'A Domain Coordinator in a domain different to your own LON-CAPA domain '.
        !           302:                            'has taken action on queued role assignment(s) in this domain for user(s) from that other domain ([_1])',
        !           303:                       args => ["\n$contextdesc\n"],
        !           304:                       });
1.1       raeburn   305:     }
                    306:     my @to_notify = split(/,/,$notifylist);
                    307:     my $numsent = 0;
                    308:     my @recusers;
                    309:     my @recudoms;
                    310:     foreach my $cc (@to_notify) {
                    311:         my ($ccname,$ccdom) = split(/:/,$cc);
                    312:         if (!exists($msgcc->{$ccname.':'.$ccdom})) {
                    313:             push(@recusers,$ccname);
                    314:             push(@recudoms,$ccdom);
                    315:             $msgcc->{$ccname.':'.$ccdom}='';
                    316:             $numsent ++;
                    317:         }
                    318:     }
                    319:     my %reciphash = (
                    320:                      cc => $msgcc,
                    321:     );
1.43      raeburn   322:     my ($uname,$udom,$need_temp_env);
1.1       raeburn   323:     if ($sender =~ /:/) {
                    324:         ($uname,$udom) = split(/:/,$sender);
1.43      raeburn   325:         if ($context eq 'usernamereq') {
                    326:             unless ($env{'user.name'} && $env{'user.domain'}) {
                    327:                 $need_temp_env = 1;
                    328:             }
                    329:         }
1.2       raeburn   330:     } elsif ($context eq 'course') {
1.1       raeburn   331:         $uname = $sender;
                    332:         my %courseinfo = &Apache::lonnet::coursedescription($cid);
                    333:         $udom = $courseinfo{'num'};
                    334:     }
                    335:     my %sentmessage;
                    336:     my $stamp = time;
                    337:     my $msgcount = &Apache::lonmsg::get_uniq();
                    338:     my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
1.59      raeburn   339:     $subject = &mt_user($sender_lh,$rawsubj);
1.1       raeburn   340:     $message = '';
                    341:     foreach my $item (@rawmsg) {
                    342:         if (ref($item) eq 'HASH') {
1.59      raeburn   343:             $message .= &mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
1.1       raeburn   344:         }
                    345:     }
1.43      raeburn   346:     &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,
                    347:                                        \@recusers,\@recudoms,undef,undef,undef,undef,$senderuname,$senderudom);
1.1       raeburn   348:     my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
                    349:     my $status;
1.43      raeburn   350:     if ($need_temp_env) {
                    351:         $env{'user.name'} = $uname;
                    352:         $env{'user.domain'} = $udom;
                    353:     }
1.1       raeburn   354:     foreach my $recip (sort(keys(%{$msgcc}))) {
                    355:         my ($ccname,$ccdom) = split(/:/,$recip);
                    356:         my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
1.59      raeburn   357:         my $subject = &mt_user($sender_lh,$rawsubj);
1.1       raeburn   358:         my $message = '';
                    359:         foreach my $item (@rawmsg) {
                    360:             if (ref($item) eq 'HASH') {
1.59      raeburn   361:                 $message .= &mt_user($sender_lh,$item->{mt},
                    362:                                      @{$item->{args}})."\n";
1.1       raeburn   363:             }
                    364:         }
1.10      raeburn   365:         if ($context eq 'coursemanagers') {
1.1       raeburn   366:             if ($approvedlist) {
1.59      raeburn   367:                 $message .= "\n\n".&mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
1.1       raeburn   368:             }
                    369:             if ($rejectedlist) {
1.59      raeburn   370:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
1.1       raeburn   371:             }
                    372:         } elsif ($context eq 'domainmanagers') {
                    373:             if ($approvedlist) {
1.59      raeburn   374:                 $message .= "\n\n".&mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
1.1       raeburn   375:             }
                    376:             if ($rejectedlist) {
1.59      raeburn   377:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
1.1       raeburn   378:             }
1.30      raeburn   379:         } elsif ($context eq 'authormanagers') {
                    380:             if ($approvedlist) {
1.59      raeburn   381:                 $message .= "\n\n".&mt_user($sender_lh,'Approved author role requests:')."\n".$approvedlist;
1.30      raeburn   382:             }
                    383:             if ($rejectedlist) {
1.59      raeburn   384:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected author role requests:')."\n".$rejectedlist;
1.30      raeburn   385:             }
1.43      raeburn   386:         } elsif ($context eq 'usernamemanagers') {
                    387:             if ($approvedlist) {
1.59      raeburn   388:                 $message .= "\n\n".&mt_user($sender_lh,'Approved LON-CAPA account requests:')."\n".$approvedlist;
1.43      raeburn   389:             }
                    390:             if ($rejectedlist) {
1.59      raeburn   391:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected LON-CAPA account requests:')."\n".$rejectedlist;
1.43      raeburn   392:             }
1.64    ! raeburn   393:         } elsif ($context eq 'othdombydc') {
        !           394:             if ($approvedlist) {
        !           395:                 $message .= "\n\n".&mt_user($sender_lh,'Approved LON-CAPA role assignments:')."\n".$approvedlist;
        !           396:             }
        !           397:             if ($rejectedlist) {
        !           398:                 $message .= "\n\n".&mt_user($sender_lh,'Rejected LON-CAPA role assignments:')."\n".$rejectedlist;
        !           399:             }
1.1       raeburn   400:         }
1.43      raeburn   401:         $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,
                    402:                                                     \%sentmessage,undef,undef,undef,1,$recipid).',';
1.1       raeburn   403:     }
                    404:     $status =~ s/,$//;
1.43      raeburn   405:     if ($need_temp_env) {
                    406:         undef($env{'user.name'});
                    407:         undef($env{'user.domain'});
                    408:     }
1.1       raeburn   409:     return ($recipstatus,$status);
                    410: }
                    411: 
                    412: sub display_queued_requests {
1.63      raeburn   413:     my ($context,$dom,$cnum,$secondary) = @_;
1.2       raeburn   414:     my ($namespace,$formaction,$nextelement,%requesthash);
1.1       raeburn   415:     if ($context eq 'course') {
                    416:         $formaction = '/adm/createuser';
                    417:         $namespace = 'selfenrollrequests';
                    418:         %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
1.2       raeburn   419:         $nextelement = '<input type="hidden" name="state" value="done" />';
1.30      raeburn   420:     } elsif ($context eq 'requestauthor') {
                    421:         $formaction = '/adm/createuser';
                    422:         $namespace = 'requestauthorqueue';
                    423:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
                    424:         $nextelement = '<input type="hidden" name="state" value="done" />';
1.43      raeburn   425:     } elsif ($context eq 'requestusername') {
                    426:         $formaction = '/adm/createuser';
                    427:         $namespace = 'usernamequeue';
                    428:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
                    429:         $nextelement = '<input type="hidden" name="state" value="done" />';
1.63      raeburn   430:     } elsif ($context eq 'othdomqueue') {
                    431:         $formaction = '/adm/createuser';
1.64    ! raeburn   432:         $namespace = 'nohist_othdomqueued';
1.63      raeburn   433:         if ($secondary eq 'domain') {
                    434:             %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
                    435:             foreach my $key (keys(%requesthash)) {
                    436:                 delete($requesthash{$key}) if ($key =~ /:(ca|aa)$/);
                    437:             }
                    438:         } elsif ($secondary eq 'author') {
                    439:             %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
                    440:             if ($cnum eq &Apache::lonnet::get_domainconfiguser($dom)) {
                    441:                 foreach my $key (keys(%requesthash)) {
                    442:                     delete($requesthash{$key}) if ($key !~ /:(ca|aa)$/);
                    443:                 }
                    444:             }
                    445:         } else {
                    446:             %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
                    447:         }
                    448:     } elsif ($context eq 'othdomaction') {
                    449:         $formaction = '/adm/createuser';
1.64    ! raeburn   450:         $namespace = 'nohist_queuedrolereqs';
1.63      raeburn   451:         if ($secondary eq 'domain') {
                    452:             my $confname = &Apache::lonnet::get_domainconfiguser($dom);
                    453:             %requesthash = &Apache::lonnet::dump($namespace,$dom,$confname);
                    454:         } else {
                    455:             %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
                    456:         }
                    457:         $nextelement = '<input type="hidden" name="state" value="done" />';
1.1       raeburn   458:     } else {
                    459:         $formaction = '/adm/createcourse';
                    460:         $namespace = 'courserequestqueue';
1.23      raeburn   461:         my $disposition = 'approval';
1.24      raeburn   462:         my $nextphase = 'requestchange';
1.23      raeburn   463:         if ($context eq 'pending') {
                    464:             $disposition = 'pending';
1.24      raeburn   465:             $nextphase = 'requestvalidation';
1.56      raeburn   466:         } elsif ($context eq 'displaypending') {
                    467:             $disposition = 'pending';
1.23      raeburn   468:         }
                    469:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_'.$disposition);
1.24      raeburn   470:         $nextelement = '<input type="hidden" name="phase" value="'.$nextphase.'" />';
1.1       raeburn   471:     }
1.23      raeburn   472:     my ($output,%queue_by_date);
1.1       raeburn   473:     if (keys(%requesthash) > 0) {
                    474:         foreach my $item (keys(%requesthash)) {
1.23      raeburn   475:             my ($timestamp,$entry,$pending);
1.1       raeburn   476:             if ($context eq 'course') {
                    477:                 ($timestamp, my $usec) = split(/:/,$requesthash{$item});
                    478:                 $entry = $item.':'.$usec;
1.30      raeburn   479:             } elsif ($context eq 'requestauthor') {
                    480:                 $timestamp = $requesthash{$item};
                    481:                 ($entry) = ($item =~ /^($match_username)_approval$/);
1.43      raeburn   482:             } elsif ($context eq 'requestusername') {
                    483:                 $timestamp = $requesthash{$item};
                    484:                 ($entry) = (&unescape($item) =~ /^($match_username)_approval$/);
1.63      raeburn   485:             } elsif ($context eq 'othdomqueue') {
                    486:                 if (ref($requesthash{$item}) eq 'HASH') {
1.64    ! raeburn   487:                     next unless ($requesthash{'status&'.$item} eq 'pending');
1.63      raeburn   488:                     my ($puname,$pudom,$prole,$psec) = split(/:/,$item);
                    489:                     $timestamp = $requesthash{$item}{'timestamp'};
                    490:                     my $adj = $requesthash{$item}{'adj'};
                    491:                     $entry = join(':',$puname,$pudom,$prole,$adj,
                    492:                                       &escape($requesthash{$item}{'requester'}),
                    493:                                       $psec);
                    494:                 }
                    495:             } elsif ($context eq 'othdomaction') {
1.64    ! raeburn   496:                next unless ($item =~ /^pending:/);
1.63      raeburn   497:                if (ref($requesthash{$item}) eq 'HASH') {
                    498:                    $timestamp = $requesthash{$item}{'timestamp'};
                    499:                    $entry = &escape($item).':'.&escape($requesthash{$item}{'requester'});
                    500:                }
1.1       raeburn   501:             } else {
                    502:                 if (ref($requesthash{$item}) eq 'HASH') {
1.63      raeburn   503:                     $timestamp = $requesthash{$item}{'timestamp'};
1.2       raeburn   504:                     my ($cnum,$disposition) = split('_',$item);
                    505:                     $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
1.23      raeburn   506:                              $requesthash{$item}{'ownerdom'}.':';
1.56      raeburn   507:                     if (($context eq 'pending') || ($context eq 'displaypending')) {
1.23      raeburn   508:                         $entry .= $requesthash{$item}{'instcode'};
                    509:                     } else {
                    510:                         $entry .= $requesthash{$item}{'crstype'};
                    511:                     }
                    512:                     $entry .= ':'.$requesthash{$item}{'description'};
1.1       raeburn   513:                 }
                    514:             }
                    515:             if ($entry ne '') {
1.23      raeburn   516:                 if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                    517:                     push(@{$queue_by_date{$timestamp}},$entry);
1.1       raeburn   518:                 } else {
1.23      raeburn   519:                     $queue_by_date{$timestamp} = [$entry];
1.1       raeburn   520:                 }
                    521:             }
                    522:         }
1.64    ! raeburn   523:     }
        !           524:     if (keys(%queue_by_date) > 0) {
        !           525:         $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
        !           526:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
        !           527:                   $nextelement."\n";
        !           528:         if ($context eq 'course') {
        !           529:             $output .=  '<h3>'.&mt('Self-enrollment requests queued pending approval by a Coordinator').'</h3>';
        !           530:         } elsif (($context eq 'pending') || ($context eq 'displaypending')) {
        !           531:             $output .= '<h3>'.&mt('Requests for official courses queued pending validation').'</h3>'.
        !           532:                        '<p>'.&mt('Requests are validated against institutional data to confirm that the requestor is an instructor of record.').'<br />'.
        !           533:                        &mt('Validation is attempted when the request is submitted.').' '.
        !           534:                        &mt('If unvalidated, the request will be held in a queue.').' '.
        !           535:                        &mt('Validation of pending requests is automatically repeated daily.').'</p>';
        !           536:         } elsif ($context eq 'requestauthor') {
        !           537:             $output .= '<h3>'.&mt('Requests for Authoring Space queued pending approval by a Domain Coordinator').'</h3>';
        !           538:         } elsif ($context eq 'requestusername') {
        !           539:             $output .= '<h3>'.&mt('Requests for LON-CAPA accounts queued pending approval by a Domain Coordinator').'</h3>';
        !           540:         } elsif ($context eq 'othdomqueue') {
        !           541:             if ($secondary eq 'domain') {
        !           542:                 $output .= '<h3>'.&mt('Domain role assignments for users from another domain, queued pending approval').'</h3>';
        !           543:             } elsif ($secondary eq 'author') {
        !           544:                 $output .= '<h3>'.&mt('Co-author role assignments for users from another domain, queued pending approval').'</h3>';
        !           545:             } elsif ($secondary eq 'course') {
        !           546:                 $output .= '<h3>'.&mt('Course role assignments for users from another domain, queued pending approval').'</h3>';
        !           547:             } elsif ($secondary eq 'community') {
        !           548:                 $output .= '<h3>'.&mt('Community role assignments for users from another domain, queued pending approval').'</h3>';
        !           549:             }
        !           550:         } elsif ($context eq 'othdomaction') {
        !           551:             if ($secondary eq 'user') {
        !           552:                 $output .= '<h3>'.&mt('Role assignments for you in other domains, queued pending your acceptance of the role.').'</h3>';
        !           553:             } elsif ($secondary eq 'domain') {
        !           554:                 $output .= '<h3>'.&mt('Role assignments in other domains, queued pending domain coordinator approval in this domain.').'</h3>';
        !           555:             }
1.23      raeburn   556:         } else {
1.64    ! raeburn   557:             $output .= '<h3>'.&mt('Course/Community requests queued pending approval by a Domain Coordinator').'</h3>';
        !           558:         } 
        !           559:         $output .= &build_queue_display($dom,$context,\%queue_by_date,$secondary).
        !           560:                    '<input type="hidden" name="queue" value="approval" />';
1.23      raeburn   561:         if ($context eq 'pending') {
1.24      raeburn   562:             $output .= '<br /><input type="submit" name="validationcheck" value="'.
                    563:                        &mt('Validate').'" /><br />'."\n".
1.56      raeburn   564:                        '<p>'.&mt('Any course/community requests which are successfully validated will be created immediately.').' '.
                    565:                              &mt('Unvalidated requests will be listed for manual approval/rejection.').'</p>';
1.63      raeburn   566:         } elsif (($context ne 'helpdesk') && ($context ne 'displaypending') && ($context ne 'othdomqueue')) {
1.23      raeburn   567:             $output .= '<br /><input type="submit" name="processqueue" value="'.&mt('Save').'" />';
                    568:         }
                    569:         $output .= '</form>';
                    570:     } else {
                    571:         $output .= '<div class="LC_info">';
                    572:         if ($context eq 'course') {
                    573:             $output .= &mt('There are currently no enrollment requests awaiting approval.');
1.56      raeburn   574:         } elsif (($context eq 'pending') || ($context eq 'displaypending')) {
1.23      raeburn   575:             $output .= &mt('There are currently no requests for official courses awaiting validation.');
1.43      raeburn   576:         } elsif ($context eq 'requestauthor') {
                    577:             $output .= &mt('There are currently no requests for Authoring Space awaiting approval.');
                    578:         } elsif ($context eq 'requestusername') {
                    579:             $output .= &mt('There are currently no requests for LON-CAPA accounts awaiting approval.');
1.63      raeburn   580:         } elsif ($context eq 'othdomqueue') {
                    581:             if ($secondary eq 'domain') {
                    582:                 $output .= &mt('There are currently no domain role assignment(s) for user(s) from another domain queued pending approval');
                    583:             } elsif ($secondary eq 'author') {
                    584:                 $output .= &mt('There are currently no co-author role assignment(s) for user(s) from another domain queued pending approval');
                    585:             } elsif ($secondary eq 'course') {
                    586:                 $output .= &mt('There are currently no course role assignment(s) for user(s) from another domain queued pending approval');
                    587:             } elsif ($secondary eq 'community') {
                    588:                 $output .= &mt('There are currently no community role assignment(s) for user(s) from another domain queued pending approval');
                    589:             }
                    590:         } elsif ($context eq 'othdomaction') {
                    591:             if ($secondary eq 'user') {
                    592:                 $output .= &mt('There are currently no pending role assignments for you in other domains, queued pending your acceptance of the role.');
                    593:             } elsif ($secondary eq 'domain') {
                    594:                 $output .= &mt('There are currently no pending role assignments in other domains, queued pending domain coordinator approval in this domain.');
                    595:             }
1.23      raeburn   596:         } else {
                    597:             $output .= &mt('There are currently no course or community requests awaiting approval.');
                    598:         }
                    599:         $output .= '</div>';
                    600:     }
                    601:     return $output;
                    602: }
1.1       raeburn   603: 
1.23      raeburn   604: sub build_queue_display {
1.63      raeburn   605:     my ($dom,$context,$queue,$secondary) = @_;
1.23      raeburn   606:     return unless (ref($queue) eq 'HASH');
1.64    ! raeburn   607:     my (%crstypes,%roles_by_context,$output);
        !           608:     if ($context eq 'othdomqueue') {
        !           609:         $output = &print_filter_menu($context);
        !           610:     }
        !           611:     $output .= &Apache::loncommon::start_data_table().
        !           612:                &Apache::loncommon::start_data_table_header_row();
1.63      raeburn   613:     unless (($context eq 'pending') || ($context eq 'displaypending') || 
                    614:             ($context eq 'helpdesk') || ($context eq 'othdomqueue')) {
1.23      raeburn   615:         $output .= '<th>'.&mt('Action').'</th>';
                    616:     }
1.63      raeburn   617:     unless (($context eq 'othdomqueue') || (($context eq 'othdomaction') && ($secondary eq 'user'))) {
                    618:         $output .= '<th>'.&mt('Requestor').'</th>';
                    619:     }
1.23      raeburn   620:     if ($context eq 'course') {
                    621:         $output .= '<th>'.&mt('Section').'</th>'.
                    622:                    '<th>'.&mt('Date requested').'</th>';
1.30      raeburn   623:     } elsif ($context eq 'requestauthor') {
                    624:         $output .= '<th>'.&mt('Date requested').'</th>';
1.43      raeburn   625:     } elsif ($context eq 'requestusername') {
                    626:         $output .= '<th>'.&mt('Date requested').'</th>'.
                    627:                    '<th>'.&mt('Details').'</th>';
1.63      raeburn   628:     } elsif ($context eq 'othdomqueue') {
                    629:         $output .= '<th>'.&mt('Date requested').'</th>'.
                    630:                    '<th>'.&mt('Role').'</th>';
                    631:         if ($secondary eq 'course') {
                    632:             $output .= '<th>'.&mt('Section').'</th>';
                    633:         }
                    634:         $output .= '<th>'.&mt('Requested for').'</th>'.
                    635:                    '<th>'.&mt('Approval needed from').'</th>';
                    636:     } elsif ($context eq 'othdomaction') {
                    637:         $output .= '<th>'.&mt('Date requested').'</th>'.
                    638:                    '<th>'.&mt('Role type').'</th>'.
                    639:                    '<th>'.&mt('Location').'</th>';
                    640:         if ($secondary eq 'domain') {
                    641:             $output .= '<th>'.&mt('Affected User').'</th>';
                    642:         }
                    643:         foreach my $type ('domain','course') {
                    644:             my @possroles = &Apache::lonuserutils::roles_by_context($type);
                    645:             $roles_by_context{$type} = \@possroles;
                    646:         }
1.56      raeburn   647:     } elsif ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
1.23      raeburn   648:         $output .= '<th>'.&mt('Institutional code').'</th>'.
                    649:                    '<th>'.&mt('Date requested').'</th>'.
                    650:                    '<th>'.&mt('Details').'</th>';
                    651:     } else {
                    652:         %crstypes = &Apache::lonlocal::texthash (
                    653:                         official   => 'Official course',
                    654:                         unofficial => 'Unofficial course',
                    655:                         community  => 'Community',
1.38      raeburn   656:                         textbook   => 'Textbook course',
1.53      raeburn   657:                         placement  => 'Placement test',
1.23      raeburn   658:                     );
                    659:         $output .= '<th>'.&mt('Type').'</th>'.
                    660:                    '<th>'.&mt('Date requested').'</th>'.
                    661:                    '<th>'.&mt('Details').'</th>';
                    662:     }
                    663:     $output .= &Apache::loncommon::end_data_table_header_row();
                    664:     my @sortedtimes = sort {$a <=> $b} (keys(%{$queue}));
                    665:     my $count = 0;
                    666:     foreach my $item (@sortedtimes) {
                    667:         if (ref($queue->{$item}) eq 'ARRAY') {
                    668:             foreach my $request (sort(@{$queue->{$item}})) {
                    669:                 my ($row,$approve,$reject,$showtime,$showsec,$namelink,
1.63      raeburn   670:                     $detailslink,$crstype,$instcode,$showrole,$adjudicator,
                    671:                     $location,$showrequester);
1.23      raeburn   672:                 $showtime = &Apache::lonlocal::locallocaltime($item);
                    673:                 if ($context eq 'course') {
                    674:                     my ($puname,$pudom,$pusec) = split(/:/,$request);
                    675:                     $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
                    676:                     $reject = $puname.':'.$pudom;
                    677:                     $showsec = $pusec;
                    678:                     if ($showsec eq '') {
                    679:                         $showsec = &mt('none');
                    680:                     }
                    681:                     $namelink = &Apache::loncommon::aboutmewrapper(
                    682:                                 &Apache::loncommon::plainname($puname,$pudom),
                    683:                                 $puname,$pudom);
1.30      raeburn   684:                 } elsif ($context eq 'requestauthor') {
                    685:                     if (&Apache::lonnet::homeserver($request,$dom) ne 'no_host') {
                    686:                         $approve = $count.':'.$request;
1.63      raeburn   687:                         $reject = $request;
1.30      raeburn   688:                         $namelink = &Apache::loncommon::aboutmewrapper(
                    689:                                     &Apache::loncommon::plainname($request,$dom),
                    690:                                     $request,$dom);
                    691:                     }
1.43      raeburn   692:                 } elsif ($context eq 'requestusername') {
                    693:                     if (&Apache::lonnet::homeserver($request,$dom) eq 'no_host') {
                    694:                         my $queued = 'approval';
                    695:                         $approve = $count.':'.$request;
                    696:                         $reject = $request;
                    697:                         $detailslink='<a href="javascript:openusernamereqdisplay('.
                    698:                                      "'$dom','$request','$queued'".');">'.$request.'</a>';
                    699:                         $namelink = $request;
                    700:                     }
1.63      raeburn   701:                 } elsif ($context eq 'othdomqueue') {
                    702:                     my ($uname,$udom,$role,$adj,$requester,$sec) = split(/:/,$request);
                    703:                     if ($adj eq 'user') {
                    704:                         $adjudicator = &mt('Role Assignee');
                    705:                     } elsif ($adj eq 'domain') {
                    706:                         $adjudicator = &mt("[_1] in user's domain",
                    707:                                            &Apache::lonnet::plaintext('dc'));
                    708:                     }
                    709:                     my $crstype;
                    710:                     $showrole = &Apache::lonnet::plaintext($role,$crstype);
                    711:                     unless (&Apache::lonnet::homeserver($uname,$udom) eq 'no_host') {
                    712:                         $namelink = &Apache::loncommon::plainname($uname,$udom)." ($uname:$udom)";
                    713:                     }
                    714:                     if ($secondary eq 'course') {
                    715:                         $showsec = $sec;
                    716:                         if ($showsec eq '') {
                    717:                             $showsec = &mt('none');
                    718:                         }
                    719:                     }
                    720:                 } elsif ($context eq 'othdomaction') {
1.64    ! raeburn   721:                     my ($status,$extent,$role,$crstype);
1.63      raeburn   722:                     my ($info,$requester) = map { &unescape($_); } split(/:/,$request);
                    723:                     if ($secondary eq 'user') {
1.64    ! raeburn   724:                         ($status,$extent,$role) = split(/:/,$info);
1.63      raeburn   725:                         $approve = $count.':'.$extent.':'.$role;
                    726:                         $reject = $extent.':'.$role;
                    727:                     } elsif ($secondary eq 'domain') {
1.64    ! raeburn   728:                         ($status,my $uname,$extent,$role) = split(/:/,$info);
1.63      raeburn   729:                         $approve = $count.':'.$extent.':'.$role.':'.$uname.':'.$dom;
                    730:                         $reject = $extent.':'.$role.':'.$uname.':'.$dom;
                    731:                         unless (&Apache::lonnet::homeserver($uname,$dom) eq 'no_host') {
                    732:                             $namelink = &Apache::loncommon::plainname($uname,$dom);
                    733:                             unless ($namelink eq $uname.':'.$dom) {
                    734:                                 $namelink .= ' ('.$uname.':'.$dom.')';
                    735:                             }
                    736:                         }
                    737:                     }
                    738:                     if (($role eq 'ca') || ($role eq 'aa')) {
                    739:                         my ($audom,$auname) = ($extent =~ m{^/($match_domain)/($match_username)$});
                    740:                         $location = &mt('Author').': '.&Apache::loncommon::plainname($auname,$audom);
                    741:                     } elsif ($role eq 'co') {
                    742:                         my ($cdom,$cnum)  = ($extent =~ m{^/($match_domain)/($match_courseid)});
                    743:                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
                    744:                             my %info = &Apache::lonnet::coursedescription("$cdom/$cnum",{'one_time' => 1});
                    745:                             $crstype = $info{'type'};
                    746:                             $location = &mt('Community').': '.$info{'description'};
                    747:                             $showrole = &Apache::lonnet::plaintext($role,'Community');
                    748:                         }
                    749:                     } elsif ($role =~ m{^cr/}) {
                    750:                         $showrole = &Apache::lonnet::plaintext($role,$crstype);
                    751:                         my ($cdom,$cnum,$csec)  = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/(\w+))$});
                    752:                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
                    753:                             my %info = &Apache::lonnet::coursedescription("$cdom/$cnum",{'one_time' => 1});
                    754:                             $crstype = $info{'type'};
                    755:                             $location = &mt($crstype).': '.$info{'description'};
                    756:                             if ($csec ne '') {
1.64    ! raeburn   757:                                 $location .= '<br />'.&mt('Section').': '.$csec;
1.63      raeburn   758:                             }
                    759:                         }
                    760:                     } else {
                    761:                         foreach my $type ('course','domain') {
                    762:                             if (grep(/^\Q$role\E$/,@{$roles_by_context{$type}})) {
                    763:                                 if ($type eq 'course') {
                    764:                                     my ($cdom,$cnum,$csec) = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/(\w+))$});
                    765:                                     if (&Apache::lonnet::is_course($cdom,$cnum)) {
                    766:                                         my %info = &Apache::lonnet::coursedescription("$cdom/$cnum",{'one_time' => 1});
                    767:                                         $crstype = $info{'type'};
                    768:                                         $location = &mt($crstype).': '.$info{'description'};
                    769:                                         if ($csec ne '') {
1.64    ! raeburn   770:                                             $location .= '<br />'.&mt('Section').': '.$csec;
1.63      raeburn   771:                                         }
                    772:                                     }
                    773:                                 } else {
                    774:                                     my ($domain)  = ($extent =~ m{^/($match_domain)/});
                    775:                                     $location = &mt('Domain').': '.&Apache::lonnet::domain($domain,'description');
                    776:                                 }
                    777:                                 last;
                    778:                             }
                    779:                         }
                    780:                     }
                    781:                     if ($role =~ m{^cr/($match_domain)/($match_username)/(\w+)$}) {
                    782:                         my ($crudom,$cruname,$rolename) = ($1,$2,$3);
                    783:                         my $creator = &Apache::loncommon::plainname($cruname,$crudom);
                    784:                         unless ($creator eq $cruname.':'.$crudom) {
                    785:                             $creator .= ' ('.$cruname.':'.$crudom.')';
                    786:                         }
                    787:                         $showrole = &mt('Custom role').': '.$rolename.'<br />'.
                    788:                                     &mt('Created by').' '.$creator;
                    789:                     } else {
                    790:                         $showrole = &Apache::lonnet::plaintext($role,$crstype);
                    791:                     }
                    792:                     my ($requname,$requdom) = split(/:/,$requester);
                    793:                     $showrequester = &Apache::loncommon::plainname($requname,$requdom);
                    794:                     unless ($showrequester eq $requname.':'.$requdom) {
                    795:                         $showrequester .= ' ('.$requname.':'.$requdom.')';
                    796:                     }
1.23      raeburn   797:                 } else {
                    798:                     my ($cnum,$ownername,$ownerdom,$type,$cdesc);
1.43      raeburn   799:                     my $queued = 'approval'; 
1.56      raeburn   800:                     if ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
1.23      raeburn   801:                         ($cnum,$ownername,$ownerdom,$instcode,$cdesc)=split(/:/,$request,5);
1.43      raeburn   802:                         $queued = 'pending';                        
1.1       raeburn   803:                     } else {
1.23      raeburn   804:                         ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
1.1       raeburn   805:                         $crstype = $type;
                    806:                         if (defined($crstypes{$type})) {
                    807:                             $crstype = $crstypes{$type};
                    808:                         }
                    809:                     }
1.23      raeburn   810:                     $detailslink='<a href="javascript:opencoursereqdisplay('.
1.43      raeburn   811:                                   "'$dom','$cnum','$queued'".');">'.$cdesc.'</a>';
1.23      raeburn   812:                     $approve = $count.':'.$cnum;
                    813:                     $reject = $cnum;
                    814:                     $namelink = &Apache::loncommon::aboutmewrapper(
                    815:                                 &Apache::loncommon::plainname($ownername,$ownerdom),
                    816:                                 $ownername,$ownerdom);
                    817:                 }
1.63      raeburn   818:                 unless (($context eq 'pending') || ($context eq 'displaypending') || 
                    819:                         ($context eq 'helpdesk') || ($context eq 'othdomqueue')) {
1.1       raeburn   820:                     $row = '<td><span class="LC_nobreak"><label>'.
1.30      raeburn   821:                            '<input type="radio" value="'.$approve.'" name="'.$count.'radioreq" />'.&mt('Approve').'</label>'.
                    822:                            '<label>'.('&nbsp;'x2).
                    823:                            '<input type="radio" value="'.$reject.'" name="'.$count.'radioreq" />'.&mt('Reject').'</label>'.
                    824:                            '<label>'.('&nbsp;'x2).
1.29      golterma  825:                            '<input type="radio" value="'."later:".$reject.'" name="'.$count.'radioreq" checked />'.&mt('Decide Later').
                    826:                            '</label></span><br /></td>';
1.1       raeburn   827:                 }
1.63      raeburn   828:                 unless (($context eq 'othdomqueue') || ($context eq 'othdomaction')) {
                    829:                     $row .= '<td>'.$namelink.'</td>'."\n";
                    830:                 }
1.23      raeburn   831:                 if ($context eq 'course') {
                    832:                     $row .= '<td>'.$showsec.'</td>'."\n".
                    833:                             '<td>'.$showtime.'</td>'."\n";
1.30      raeburn   834:                 } elsif ($context eq 'requestauthor') {
                    835:                     $row .= '<td>'.$showtime.'</td>'."\n";
1.43      raeburn   836:                 } elsif ($context eq 'requestusername') {
                    837:                     $row .= '<td>'.$showtime.'</td>'."\n".
                    838:                             '<td>'.$detailslink.'</td>'."\n";
1.63      raeburn   839:                 } elsif ($context eq 'othdomqueue') {
                    840:                     $row .= '<td>'.$showtime.'</td>'."\n".
                    841:                             '<td>'.$showrole.'</td>'."\n";
                    842:                     if ($secondary eq 'course') {
                    843:                         $row .= '<td>'.$showsec.'</td>'."\n";
                    844:                     }
                    845:                     $row .= '<td>'.$namelink.'</td>'."\n".
                    846:                             '<td>'.$adjudicator.'</td>'."\n";
                    847:                 } elsif ($context eq 'othdomaction') {
                    848:                     if ($secondary eq 'domain') {
                    849:                         $row .= '<td>'.$showrequester.'</td>'."\n";
                    850:                     }
                    851:                     $row .= '<td>'.$showtime.'</td>'."\n".
                    852:                             '<td>'.$showrole.'</td>'."\n".
                    853:                             '<td>'.$location.'</td>'."\n";
                    854:                     if ($secondary eq 'domain') {
                    855:                         $row .= '<td>'.$namelink.'</td>'."\n";
                    856:                     }
                    857:                 } else {
1.56      raeburn   858:                     if ($context eq 'pending' || $context eq 'displaypending' || $context eq 'stillpending') {
1.23      raeburn   859:                         $row .= '<td>'.$instcode.'</td>'."\n";
                    860:                     } else {
                    861:                         $row .= '<td>'.$crstype.'</td>'."\n";
                    862:                     }
                    863:                     $row .= '<td>'.$showtime.'</td>'."\n".
                    864:                             '<td>'.$detailslink.'</td>'."\n";
                    865:                 }
                    866:                 $output .= &Apache::loncommon::start_data_table_row()."\n".
                    867:                            $row.
                    868:                            &Apache::loncommon::end_data_table_row()."\n";
                    869:                 $count ++;
1.1       raeburn   870:             }
                    871:         }
                    872:     }
1.23      raeburn   873:     $output .= &Apache::loncommon::end_data_table();
1.1       raeburn   874:     return $output;
                    875: }
                    876: 
1.64    ! raeburn   877: sub print_filter_menu {
        !           878:     my ($context) = @_;
        !           879:     return;
        !           880: }
        !           881: 
1.1       raeburn   882: sub update_request_queue {
                    883:     my ($context,$cdom,$cnum,$coursedesc) = @_;
                    884:     my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
1.26      raeburn   885:         $stucounts,$idx,$classlist,%requesthash,$cid,$domdesc,$now,
                    886:         $sender,$approvedmsg,$rejectedmsg,$beneficiary,
1.2       raeburn   887:         @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
1.19      raeburn   888:         @processing_errors,@warn_approves,@warn_rejects,@approvals,@warn_dels,
1.30      raeburn   889:         @rejections,@rejectionerrors,@nopermissions,%courseroles,@toremove,
                    890:         %communityroles,%domdefs,%approvalmsg,%rejectionmsg,$crstype,$queue,
1.63      raeburn   891:         $firsturl,$uniquecode,%codes,%roles_by_context,%requesteractive,
1.64    ! raeburn   892:         %gotroles,$confname,%requestedby,@rejectedreqs,$dbname);
1.29      golterma  893:     my $count=0;
1.43      raeburn   894:     while (my $item = $env{'form.'.$count.'radioreq'}) {
                    895:         if ($item =~ /^\d+:/) {
                    896:             push(@approvals,$item);
                    897:         } elsif ($item !~ /^later:/) {
                    898:             push(@rejections,$item);
1.29      golterma  899:         }
1.43      raeburn   900:         $count ++;
1.29      golterma  901:     }
1.1       raeburn   902:     $now = time;
                    903:     $sender = $env{'user.name'}.':'.$env{'user.domain'};
                    904:     if ($context eq 'course') {
                    905:         $namespace = 'selfenrollrequests';
                    906:         $beneficiary = 'enroller';
                    907:         $cid = $env{'request.course.id'};
1.12      raeburn   908:         $crstype = lc(&Apache::loncommon::course_type());
1.54      raeburn   909:         $firsturl = &Apache::lonnet::course_portal_url($cnum,$cdom);
1.1       raeburn   910:         %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
                    911:         $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
                    912:         $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
                    913:         $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                    914:         $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
                    915:         $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                    916:         ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
                    917:         $approvedmsg = [{
                    918:                             mt => 'Your request for enrollment has been approved.',
                    919:                         },
                    920:                         {
1.28      raeburn   921:                             mt   => 'Visit [_1] to log-in and access the course',
1.25      raeburn   922:                             args => [$firsturl],
1.1       raeburn   923:                         }];
                    924:         $rejectedmsg =  [{
                    925:                             mt => 'Your request for enrollment has not been approved.',
                    926:                         }];
1.30      raeburn   927:     } elsif ($context eq 'requestauthor') {
                    928:         $namespace = 'requestauthorqueue';
                    929:         $beneficiary = 'requestauthor';
                    930:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
                    931:         my %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
                    932:         if (ref($domdefs{'requestauthor'}) eq 'HASH') {
                    933:             if (ref($domdefs{'requestauthor'}{'notify'}) eq 'HASH') {
                    934:                 $notifylist = $domdefs{'requestauthor'}{'notify'}{'approval'};
                    935:             }
                    936:         }
                    937:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
1.54      raeburn   938:         $firsturl = &Apache::lonnet::course_portal_url($domconfiguser,$cdom);
1.30      raeburn   939:         $approvedmsg = [{
1.42      bisitz    940:                             mt => 'Your request for Authoring Space has been approved.',
1.30      raeburn   941:                         },
                    942:                         {
                    943:                             mt   => 'Visit [_1] to log-in and select your author role',
                    944:                             args => [$firsturl],
                    945:                         }];
                    946:         $rejectedmsg =  [{
1.42      bisitz    947:                             mt => 'Your request for Authoring Space has not been approved.',
1.30      raeburn   948:                         }];
                    949:         $domdesc = &Apache::lonnet::domain($cdom);
1.43      raeburn   950:     } elsif ($context eq 'requestusername') {
                    951:         $namespace = 'usernamequeue';
                    952:         $beneficiary = 'requestusername';
                    953:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
                    954:         my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
                    955:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    956:             if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                    957:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'notify'}) eq 'HASH') {
                    958:                     $notifylist = $domconfig{'usercreation'}{'cancreate'}{'notify'}{'approval'};
                    959:                 }
                    960:             }
                    961:         }
                    962:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
1.54      raeburn   963:         $firsturl = &Apache::lonnet::course_portal_url($domconfiguser,$cdom);
1.43      raeburn   964:         $approvedmsg = [{
                    965:                             mt => 'Your request for a LON-CAPA account has been approved.',
                    966:                         },
                    967:                         {
                    968:                             mt   => 'Visit [_1] to log-in.',
                    969:                             args => [$firsturl],
                    970:                         }];
                    971:         $rejectedmsg =  [{
                    972:                             mt => 'Your request for a LON-CAPA account has not been approved.',
                    973:                         }];
                    974:         $domdesc = &Apache::lonnet::domain($cdom);
1.64    ! raeburn   975:         $dbname = 'nohist_requestedusernames';
1.63      raeburn   976:     } elsif (($context eq 'othdombydc') || ($context eq 'othdombyuser')) {
1.64    ! raeburn   977:         $namespace = 'nohist_queuedrolereqs';
1.63      raeburn   978:         foreach my $type ('domain','course') {
                    979:             my @possroles = &Apache::lonuserutils::roles_by_context($type);
                    980:             $roles_by_context{$type} = \@possroles;
                    981:         }
                    982:         if ($context eq 'othdombydc') {
1.64    ! raeburn   983:             $confname = &Apache::lonnet::get_domainconfiguser($cdom);
1.63      raeburn   984:             %requesthash = &Apache::lonnet::dump($namespace,$cdom,$confname);
                    985:         } else {
                    986:             %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
                    987:         }
                    988:         $domdesc = &Apache::lonnet::domain($cdom);
1.64    ! raeburn   989:         $dbname = 'nohist_othdomqueued';
1.1       raeburn   990:     } else {
1.2       raeburn   991:         $domdesc = &Apache::lonnet::domain($cdom);
                    992:         $namespace = 'courserequestqueue';
                    993:         $beneficiary = 'courserequestor';
1.23      raeburn   994:         $queue = 'approval';
                    995:         if ($env{'form.queue'} eq 'pending') {
                    996:             $queue = 'pending';
                    997:         }
                    998:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_'.$queue);
1.2       raeburn   999:         my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
                   1000:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   1001:             if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') { 
                   1002:                 $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
                   1003:             }
                   1004:         }
1.12      raeburn  1005:         $approvalmsg{'course'} = 
                   1006:                         [{
1.2       raeburn  1007:                             mt => 'Your course request has been approved.',
                   1008:                         },
                   1009:                         {
1.28      raeburn  1010:                             mt   => 'Visit [_1] to log-in and access the course',
                   1011:                             args => [],
1.2       raeburn  1012:                         }];
1.12      raeburn  1013:         $rejectionmsg{'course'} =
                   1014:                         [{
1.2       raeburn  1015:                             mt => 'Your course request has not been approved.',
                   1016:                         }];
1.12      raeburn  1017: 
                   1018:         $approvalmsg{'community'} = 
                   1019:                         [{
                   1020:                             mt => 'Your community request has been approved.',
                   1021:                         },
                   1022:                         {
1.28      raeburn  1023:                             mt   => 'Visit [_1] to log-in and access the community',
                   1024:                             args => [],
1.12      raeburn  1025:                         }];
                   1026: 
                   1027:         $rejectionmsg{'community'} = 
                   1028:                         [{
                   1029:                             mt => 'Your community request has not been approved.',
                   1030:                         }];
                   1031: 
1.2       raeburn  1032:         %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
                   1033:         my @roles = &Apache::lonuserutils::roles_by_context('course');
                   1034:         foreach my $role (@roles) {
                   1035:             $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
                   1036:         }
                   1037:         foreach my $role (@roles) {
                   1038:             $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
                   1039:         }
1.1       raeburn  1040:     }
                   1041:     foreach my $item (sort {$a <=> $b} @approvals) {
                   1042:         if ($context eq 'course') {
                   1043:             my ($num,$uname,$udom,$usec) = split(/:/,$item);
                   1044:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                   1045:             if ($uhome ne 'no_host') {
                   1046:                 if (exists($requesthash{$uname.':'.$udom})) {
                   1047:                     if (exists($classlist->{$uname.':'.$udom})) {
                   1048:                         if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
                   1049:                             if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
                   1050:                                 ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
                   1051:                                 push(@existing,$uname.':'.$udom);
                   1052:                                 next;
                   1053:                             }
                   1054:                         }
                   1055:                     }
                   1056:                 } else {
                   1057:                     push(@missingreq,$uname.':'.$udom);
                   1058:                     next;
                   1059:                 }
                   1060:                 if (!grep(/^\Q$item\E$/,@rejections)) {
                   1061:                     if ($limit eq 'allstudents') {
                   1062:                         if ($stucounts->{$limit} >= $cap) {
                   1063:                             push(@limitexceeded,$uname.':'.$udom);
                   1064:                             last;
                   1065:                         }
                   1066:                     } elsif ($limit eq 'selfenrolled') {
                   1067:                         if ($stucounts->{$limit} >= $cap) {
                   1068:                             push(@limitexceeded,$uname.':'.$udom);
                   1069:                             last;
                   1070:                         }
                   1071:                     }
                   1072:                     my $result =
                   1073:                         &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
                   1074:                     if ($result eq 'ok') {
1.2       raeburn  1075:                         push(@completed,$uname.':'.$udom);
1.1       raeburn  1076:                         $stucounts->{'allstudents'} ++;
                   1077:                         $stucounts->{'selfenrolled'} ++;
                   1078:                         &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
1.26      raeburn  1079:                                        $cid,$coursedesc,$now,$beneficiary,$sender,
                   1080:                                        undef,undef,$crstype);
1.2       raeburn  1081:                         my %userrequest = (
                   1082:                             $cdom.'_'.$cnum => {
                   1083:                                 timestamp   => $now,
                   1084:                                 section     => $usec,
                   1085:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                   1086:                                 status      => 'approved',
                   1087:                             }
                   1088:                         );
1.1       raeburn  1089:                         my $userresult =
                   1090:                             &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
                   1091:                         if ($userresult ne 'ok') {
                   1092:                             push(@warn_approves,$uname.':'.$udom);
                   1093:                         }
                   1094:                     } else {
1.2       raeburn  1095:                         push(@processing_errors,$uname.':'.$udom);
1.1       raeburn  1096:                     }
                   1097:                 }
                   1098:             } else {
                   1099:                 push(@invalidusers,$uname.':'.$udom);
                   1100:             }
1.30      raeburn  1101:         } elsif ($context eq 'requestauthor') {
                   1102:             my ($num,$uname) = split(/:/,$item);
                   1103:             my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
                   1104:             if ($uhome ne 'no_host') {
                   1105:                 my ($user_is_adv,$user_is_author) = &Apache::lonnet::is_advanced_user($cdom,$uname);
                   1106:                 if ($user_is_author) {
                   1107:                     push(@existing,$uname);
                   1108:                 } elsif (&Apache::lonnet::usertools_access($uname,$cdom,'requestauthor',
                   1109:                                                            undef,'requestauthor')) {
                   1110:                     if (&Apache::lonnet::allowed('cau',$cdom)) {
                   1111:                         if (&Apache::lonnet::assignrole($cdom,$uname,'/'.$cdom.'/','au',undef,time,undef,undef,'requestauthor') eq 'ok') {
                   1112:                             push(@completed,$uname);
                   1113:                             &send_selfserve_notification($uname.':'.$cdom,
                   1114:                                                          $approvedmsg,undef,undef,$now,
                   1115:                                                          $beneficiary,$sender);
                   1116:                             my %userrequest = (
                   1117:                                 author => {
                   1118:                                           timestamp   => $now,
                   1119:                                           adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                   1120:                                           status      => 'approved',
                   1121:                                         },
                   1122:                                 author_status => 'approved',
                   1123:                             );
                   1124:                             my $userresult =
                   1125:                                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$uname);
                   1126:                             if ($userresult ne 'ok') {
1.43      raeburn  1127:                                 push(@warn_approves,$uname.':'.$cdom);
1.30      raeburn  1128:                             }
                   1129:                         } else {
                   1130:                             push(@processing_errors,$uname);
                   1131:                         }
                   1132:                     } else {
                   1133:                         push(@nopermissions,$uname);
                   1134:                     }
                   1135:                 } else {
                   1136:                     push(@nopermissions,$uname);
                   1137:                 }
                   1138:             } else {
                   1139:                 push(@invalidusers,$uname.':'.$cdom);
                   1140:             }
                   1141:             push(@toremove,(@invalidusers,@nopermissions));
1.43      raeburn  1142:         } elsif ($context eq 'requestusername') {
                   1143:             my ($num,$uname) = split(/:/,$item);
                   1144:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   1145:             my %curr = &Apache::lonnet::get($dbname,[$uname],$cdom,$domconfiguser);
1.58      raeburn  1146: 
1.43      raeburn  1147:             if (ref($curr{$uname}) eq 'HASH') {
1.58      raeburn  1148:                 my ($logtoken,$serverid,$encpass,$courseid,$id,$firstname,
                   1149:                     $middlename,$lastname,$generation,$inststatus,$email);
1.43      raeburn  1150:                 $curr{$uname}{'timestamp'} = $now;
                   1151:                 $curr{$uname}{'adjudicator'} = $env{'user.name'}.':'.$env{'user.domain'};
                   1152:                 $courseid   = $curr{$uname}{'courseid'};
                   1153:                 $id         = $curr{$uname}{'id'};
                   1154:                 $firstname  = $curr{$uname}{'firstname'};
                   1155:                 $middlename = $curr{$uname}{'middlename'};
                   1156:                 $lastname   = $curr{$uname}{'lastname'};
                   1157:                 $generation = $curr{$uname}{'generation'};
1.46      raeburn  1158:                 $inststatus = $curr{$uname}{'inststatus'};
1.43      raeburn  1159: 
1.58      raeburn  1160:                 if ($curr{$uname}{'email'} ne '') {
                   1161:                     $email = $curr{$uname}{'email'};
                   1162:                 } elsif ($uname =~ /^[^\@]+\@[^\@]+$/) {
                   1163:                     $email = $uname;
                   1164:                 }
                   1165: 
                   1166:                 my $upass;
                   1167:                 if ($curr{$uname}{'tmpinfo'}) {
                   1168:                     my ($key,$caller)=split(/&/,$curr{$uname}{'tmpinfo'});
                   1169:                     if ($caller eq 'createaccount') {
                   1170:                         if ($curr{$uname}{'upass'} eq '') {
                   1171:                             $upass = $curr{$uname}{'upass'};
                   1172:                         } else {
                   1173:                             $upass = &Apache::loncommon::des_decrypt($key,$curr{$uname}{'upass'});
                   1174:                         }
                   1175:                     } else {
                   1176:                         push(@processing_errors,$uname);
                   1177:                     }
                   1178:                 } else {
                   1179:                     $upass = $curr{$uname}{'upass'};
                   1180:                 }
                   1181:                 if ($upass eq '') {
                   1182:                     push(@processing_errors,$uname);
                   1183:                 } else {
1.43      raeburn  1184:                     undef($curr{$uname}{'upass'});
                   1185:                     my $result =
                   1186:                         &Apache::lonnet::modifyuser($cdom,$uname,$id,'internal',$upass,
                   1187:                                                     $firstname,$middlename,$lastname,
1.58      raeburn  1188:                                                     $generation,undef,undef,$email);
1.43      raeburn  1189:                     if ($result eq 'ok') {
                   1190:                         $curr{$uname}{'status'} = 'created';
                   1191:                         push(@completed,$uname); 
                   1192:                         my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
                   1193:                         if ($uhome eq 'no_host') {
                   1194:                             push(@warn_approves,$uname);
                   1195:                         } else {
1.44      raeburn  1196:                             unless (($inststatus eq 'default') || ($inststatus eq '')) {
                   1197:                                 &Apache::lonnet::put('environment',{inststatus => $inststatus},$cdom,$uname);
                   1198:                             }
1.43      raeburn  1199:                             &send_selfserve_notification($uname.':'.$cdom,
                   1200:                                                          $approvedmsg,undef,undef,$now,
                   1201:                                                          $beneficiary,$sender);
                   1202:                             if (&Apache::lonnet::put($dbname,\%curr,$cdom,$domconfiguser) ne 'ok') {
                   1203:                                 push(@warn_approves,$uname);
                   1204:                             }
                   1205:                         }
                   1206:                     } else {
                   1207:                         push(@processing_errors,$uname);
                   1208:                     }
                   1209:                 }
                   1210:             } else {
                   1211:                 push(@invalidusers,$uname);
                   1212:             }
                   1213:             push(@toremove,@invalidusers);
1.63      raeburn  1214:         } elsif (($context eq 'othdombydc') || ($context eq 'othdombyuser')) {
                   1215:             if ($context eq 'othdombydc') {
                   1216:                 my ($num,$extent,$role,$uname,$udom) = split(/:/,$item);
                   1217:                 my ($logmsg,$result);
                   1218:                 if ($udom eq $cdom) {
1.64    ! raeburn  1219:                     my $key = 'pending:'.$uname.':'.$extent.':'.$role;
1.63      raeburn  1220:                     if (exists($requesthash{$key})) {
                   1221:                         if (ref($requesthash{$key}) eq 'HASH') {
                   1222:                             my $requester = $requesthash{$key}->{'requester'};
                   1223:                             my ($requname,$requdom) = split(/:/,$requester);
                   1224:                             my $start = $requesthash{$key}->{'start'};
                   1225:                             my $end = $requesthash{$key}->{'end'};
                   1226:                             my $credits = $requesthash{$key}->{'credits'};
                   1227:                             my $reqcontext = $requesthash{$key}->{'context'};
                   1228:                             if ((&Apache::lonnet::homeserver($uname,$udom) ne 'no_host') &&
                   1229:                                 (&Apache::lonnet::homeserver($requname,$requdom) ne 'no_host')) {
                   1230:                                 if (($role eq 'ca') || ($role eq 'aa')) {
                   1231:                                     my ($audom,$auname) = ($extent =~ m{^/($match_domain)/($match_username)$});
                   1232:                                     if (&Apache::lonnet::homeserver($auname,$audom) ne 'no_host') {
                   1233:                                         if ($requester eq $auname.':'.$audom) {
                   1234:                                             unless ($gotroles{$requester}) {
                   1235:                                                 &requester_roles($auname,$audom,\%requesteractive);
                   1236:                                                 $gotroles{$requester} = 1;
                   1237:                                             }
                   1238:                                             if (ref($requesteractive{$requester}) eq 'HASH') {
                   1239:                                                 if ($requesteractive{$requester}{':'.$audom.':au'}) {
                   1240:                                                     $result = &Apache::lonnet::assignrole($udom,$uname,$extent,$role,
                   1241:                                                                                           $end,$start,'','',$reqcontext);
                   1242:                                                 }
                   1243:                                             }
                   1244:                                         }
                   1245:                                     }
                   1246:                                 } elsif (($role eq 'co') || ($role eq 'cc')) {
                   1247:                                     my ($crsdom,$crsnum)  = ($extent =~ m{^/($match_domain)/($match_courseid)});
                   1248:                                     if (&Apache::lonnet::is_course($crsdom,$crsnum)) {
                   1249:                                         my %info = &Apache::lonnet::coursedescription("$crsdom/$crsnum",{'one_time' => 1});
                   1250:                                         if ((($role eq 'co') && ($info{'type'} eq 'Community')) ||
                   1251:                                            (($role eq 'cc') && ($info{'type'} ne 'Community'))) {
                   1252:                                             if ($info{'internal.courseowner'} eq $requester) {
                   1253:                                                 unless ($gotroles{$requester}) {
                   1254:                                                     &requester_roles($requname,$requdom,\%requesteractive);
                   1255:                                                     $gotroles{$requester} = 1;
                   1256:                                                 }
                   1257:                                                 if (ref($requesteractive{$requester}) eq 'HASH') {
                   1258:                                                     if ($requesteractive{$requester}{"$crsnum:$crsdom:$role"}) {
1.64    ! raeburn  1259:                                                         ($logmsg,$result) =
1.63      raeburn  1260:                                                             &Apache::loncommon::commit_standardrole($udom,$uname,$extent,$role,$start,
                   1261:                                                                                                     $end,$crsdom,$crsnum,'',
                   1262:                                                                                                     $reqcontext);
                   1263:                                                     }
                   1264:                                                 }
                   1265:                                             }
                   1266:                                         }
                   1267:                                     }
                   1268:                                 } elsif ($role =~ m{^(cr)/($match_domain)/($match_username)/(\w+)$}) {
                   1269:                                     my ($mrole,$crudom,$cruname,$rolename) = ($1,$2,$3,$4);
                   1270:                                     my ($crsdom,$crsnum,$csec) =
                   1271:                                         ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/(\w+))$});
                   1272:                                     if (&Apache::lonnet::is_course($crsdom,$crsnum)) {
                   1273:                                         my ($rdummy,$roledef) =
                   1274:                                             &Apache::lonnet::get('roles',["rolesdef_$rolename"],$crudom,$cruname);
                   1275:                                         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   1276:                                             unless ($gotroles{$requester}) {
                   1277:                                                 &requester_roles($requname,$requdom,\%requesteractive);
                   1278:                                                 $gotroles{$requester} = 1;
                   1279:                                             }
                   1280:                                             if (ref($requesteractive{$requester}) eq 'HASH') {
                   1281:                                                 if (&requester_has_perm($crsdom,$crsnum,$mrole,$requesteractive{$requester})) {
1.64    ! raeburn  1282:                                                     ($logmsg,$result) =
        !          1283:                                                         &Apache::loncommon::commit_customrole($udom,$uname,$extent,$crudom,$cruname,
        !          1284:                                                                                               $rolename,$start,$end,$reqcontext);
1.63      raeburn  1285:                                                 }
                   1286:                                             }
                   1287:                                         }
                   1288:                                     }
                   1289:                                 } else {
                   1290:                                     my $process;
                   1291:                                     foreach my $type ('course','domain') {
                   1292:                                         if (grep(/^\Q$role\E$/,@{$roles_by_context{$type}})) {
                   1293:                                             if ($type eq 'course') {
                   1294:                                                 my ($crsdom,$crsnum,$csec) = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/(\w+))$});
                   1295:                                                 if (&Apache::lonnet::is_course($crsdom,$crsnum)) {
                   1296:                                                     my $typeok;
                   1297:                                                     if ($role eq 'cc') {
                   1298:                                                         my %info = &Apache::lonnet::coursedescription("$crsdom/$crsnum",{'one_time' => 1});
                   1299:                                                         if ($info{'type'} eq 'Course') {
                   1300:                                                             $typeok = 1;
                   1301:                                                         }
                   1302:                                                     } else {
                   1303:                                                         $typeok = 1;
                   1304:                                                     }
                   1305:                                                     if ($typeok) {
                   1306:                                                         unless ($gotroles{$requester}) {
                   1307:                                                             &requester_roles($requname,$requdom,\%requesteractive);
                   1308:                                                             $gotroles{$requester} = 1;
                   1309:                                                         }
                   1310:                                                         if (ref($requesteractive{$requester}) eq 'HASH') {
                   1311:                                                             if (&requester_has_perm($crsdom,$crsnum,$role,$requesteractive{$requester})) {
1.64    ! raeburn  1312:                                                                 ($logmsg,$result) =
1.63      raeburn  1313:                                                                     &Apache::loncommon::commit_standardrole($udom,$uname,$extent,$role,$start,
                   1314:                                                                                                             $end,$crsdom,$crsnum,$csec,
                   1315:                                                                                                             $reqcontext,$credits);
                   1316:                                                             }
                   1317:                                                         }
                   1318:                                                     }
                   1319:                                                 }
                   1320:                                             } else {
1.64    ! raeburn  1321:                                                 my ($domain) = ($extent =~ m{^/($match_domain)/});
1.63      raeburn  1322:                                                 if (&Apache::lonnet::domain($domain) ne '') {
                   1323:                                                     unless ($gotroles{$requester}) {
                   1324:                                                         &requester_roles($requname,$requdom,\%requesteractive);
                   1325:                                                         $gotroles{$requester} = 1;
                   1326:                                                     }
                   1327:                                                     if (&requester_has_perm($domain,'',$role,$requesteractive{$requester})) {
                   1328:                                                         $result = &Apache::lonnet::assignrole($udom,$uname,$extent,$role,
                   1329:                                                                                               $end,$start,'','',$reqcontext);
                   1330:                                                     }
                   1331:                                                 }
                   1332:                                             }
                   1333:                                             last;
                   1334:                                         }
                   1335:                                     }
                   1336:                                 }
                   1337:                             }
1.64    ! raeburn  1338:                             if ($result eq 'ok') {
        !          1339:                                 $requestedby{$item} = $requester;
        !          1340:                                 my $statusres;
        !          1341:                                 my $id = $uname.':'.$udom.':'.$role;
        !          1342:                                 if (($role eq 'ca') || ($role eq 'aa')) {
        !          1343:                                     my ($audom,$auname) = ($extent =~ m{^/($match_domain)/($match_username)$});
        !          1344:                                     $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'approved'},$audom,$auname);
        !          1345:                                 } elsif ($extent =~ m{^/($match_domain)/$}) {
        !          1346:                                     my $domain = $1;
        !          1347:                                     my $configuser = &Apache::lonnet::get_domainconfiguser($domain);
        !          1348:                                     $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'approved'},$domain,$configuser);
        !          1349:                                 } else {
        !          1350:                                     my ($crsdom,$crsnum,$csec) = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/([^/]+))$});
        !          1351:                                     $id .= ':'.$csec;
        !          1352:                                     $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'approved'},$crsdom,$crsnum);
        !          1353:                                 }
        !          1354:                                 if ($statusres eq 'ok') {
        !          1355:                                     my $newkey = 'approved:'.$uname.':'.$extent.':'.$role;
        !          1356:                                     $requesthash{$newkey} = $requesthash{$key};
        !          1357:                                     delete($requesthash{$key});
        !          1358:                                     push(@toremove,$key);
        !          1359:                                     push(@completed,$item);
        !          1360:                                 }
        !          1361:                             }
1.63      raeburn  1362:                         }
                   1363:                     }
                   1364:                 }
                   1365:             } else {
                   1366:                 my ($num,$extent,$role) = split(/:/,$item);
                   1367:                 if (exists($requesthash{$extent.':'.$role})) {
                   1368:                     if (ref($requesthash{$extent.':'.$role}) eq 'HASH') {
                   1369: #FIXME
                   1370: #check if extent is valid
                   1371: #check if role is valid
                   1372: #check requester privs
                   1373:                     }
                   1374:                 }
                   1375:             }
1.1       raeburn  1376:         } else {
1.2       raeburn  1377:             my ($num,$cnum) = split(':',$item);
1.23      raeburn  1378:             if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
1.2       raeburn  1379:                 if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
1.23      raeburn  1380:                     my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
                   1381:                     my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
                   1382:                     $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
                   1383:                     my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
1.2       raeburn  1384:                     my $longroles = \%courseroles;
                   1385:                     if ($crstype eq 'community') {
                   1386:                         $longroles = \%communityroles;
                   1387:                     }
1.5       raeburn  1388:                     my $cancreate;
                   1389:                     if ($cdom eq $ownerdom) {
                   1390:                         if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
                   1391:                                                               undef,'requestcourses')) {
                   1392:                             $cancreate = 1;
                   1393:                         }
                   1394:                     } else {
                   1395:                         my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
                   1396:                         if ($userenv{'reqcrsotherdom.'.$crstype}) {
                   1397:                             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
                   1398:                             if (grep(/^\Q$cdom\E:/,@doms)) {
                   1399:                                 $cancreate = 1;
                   1400:                             }
                   1401:                         }
                   1402:                     }
                   1403:                     if ($cancreate) {
1.2       raeburn  1404:                         my $requestkey = $cdom.'_'.$cnum;
                   1405:                         my %history = 
                   1406:                             &Apache::lonnet::restore($requestkey,'courserequests',
                   1407:                                                      $ownerdom,$ownername);
                   1408:                         if ((ref($history{'details'}) eq 'HASH') && 
1.23      raeburn  1409:                             ($history{'disposition'} eq $queue)) {
1.59      raeburn  1410:                             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,
                   1411:                                 $keysmsg,$code,%customitems);
                   1412:                             my $clonemsg = [];
1.41      raeburn  1413:                             my $fullname = '';
                   1414:                             my $inprocess = &Apache::lonnet::auto_crsreq_update($cdom,$cnum,$crstype,'process',$ownername,
                   1415:                                                                                 $ownerdom,$fullname,$coursedesc);
                   1416:                             if (ref($inprocess) eq 'HASH') {
                   1417:                                 foreach my $key (keys(%{$inprocess})) {
                   1418:                                     if (exists($history{'details'}{$key})) { 
                   1419:                                         $customitems{$key} = $history{'details'}{$key};
                   1420:                                     }
                   1421:                                 }
                   1422:                             }
1.50      raeburn  1423:                             if ($history{'details'}{'clonecrs'}) {
                   1424:                                 $customitems{'_LC_clonefrom'} = $history{'details'}{'clonedom'}.'_'.$history{'details'}{'clonecrs'};
                   1425:                             }
1.59      raeburn  1426:                             my ($result,$postprocess) = &course_creation($cdom,$cnum,$context,$history{'details'},
                   1427:                                                         \$logmsg,$clonemsg,\$newusermsg,\$addresult,\$enrollcount,
1.41      raeburn  1428:                                                         \$response,\$keysmsg,\%domdefs,$longroles,\$code,\%customitems);
1.2       raeburn  1429:                             if ($result eq 'created') {
1.12      raeburn  1430:                                 if ($crstype eq 'community') {
                   1431:                                     $approvedmsg = $approvalmsg{'community'};
                   1432:                                 } else {
                   1433:                                     $approvedmsg = $approvalmsg{'course'};
                   1434:                                 }
1.54      raeburn  1435:                                 my $firsturl = &Apache::lonnet::course_portal_url($cnum,$cdom);
1.28      raeburn  1436:                                 if (ref($approvedmsg) eq 'ARRAY') {
                   1437:                                     if (ref($approvedmsg->[1]) eq 'HASH') {
                   1438:                                         $approvedmsg->[1]->{'args'} = [$firsturl];
                   1439:                                     }
1.59      raeburn  1440:                                     if ((ref($clonemsg) eq 'ARRAY') && (@{$clonemsg})) {
                   1441:                                         push(@{$approvedmsg},@{$clonemsg});
                   1442:                                     }
1.39      raeburn  1443:                                     if ($code) {
                   1444:                                         push(@{$approvedmsg},
                   1445:                                             {
                   1446:                                               mt   => 'Students can automatically select your course by entering this code: [_1]',
                   1447:                                               args => [$code],
                   1448:                                             });
                   1449:                                         $codes{$cnum} = $code;
                   1450:                                     }
1.41      raeburn  1451:                                     if (ref($postprocess) eq 'HASH') {
                   1452:                                         if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
                   1453:                                             foreach my $item (@{$postprocess->{'createdmsg'}}) {
                   1454:                                                 if (ref($item) eq 'HASH') {
                   1455:                                                     if ($item->{'mt'} ne '') {
                   1456:                                                         push(@{$approvedmsg},$item);
                   1457:                                                     }
                   1458:                                                 }
                   1459:                                             }
                   1460:                                         }
                   1461:                                     }
1.28      raeburn  1462:                                 }
1.2       raeburn  1463:                                 push(@completed,$cnum);
1.19      raeburn  1464:                                 
1.23      raeburn  1465:                                 unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
1.19      raeburn  1466:                                     push(@warn_dels,$cnum);
                   1467:                                 }
1.26      raeburn  1468:                                 &send_selfserve_notification($ownername.':'.$ownerdom,
                   1469:                                               $approvedmsg,$cid,$coursedesc,$now,
                   1470:                                               $beneficiary,$sender,undef,undef,$crstype);
1.2       raeburn  1471:                                 my %reqhash = (
                   1472:                                                 reqtime     => $history{'reqtime'},
                   1473:                                                 crstype     => $history{'crstype'},
                   1474:                                                 details     => $history{'details'},
                   1475:                                                 disposition => $history{'disposition'},
                   1476:                                                 status      => 'created',
                   1477:                                                 adjudicator => $env{'user.name'}.':'.
                   1478:                                                                $env{'user.domain'},
                   1479:                                               );
                   1480:                                 my $userresult =
                   1481:                                     &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                   1482:                                                    'courserequests',$ownerdom,$ownername);
                   1483:                                 if ($userresult eq 'ok') {
                   1484:                                     my %status = (
                   1485:                                                    'status:'.$cdom.':'.$cnum => 'created'
                   1486:                                                  );
                   1487:                                     my $statusresult = 
                   1488:                                         &Apache::lonnet::put('courserequests',\%status,
                   1489:                                                              $ownerdom,$ownername);
                   1490:                                     if ($statusresult ne 'ok') {
                   1491:                                         push(@warn_approves,$cnum);
                   1492:                                     }
                   1493:                                 }
                   1494:                                 if ($userresult ne 'ok') {
                   1495:                                     push(@warn_approves,$cnum);
                   1496:                                 }
                   1497:                             } else {
                   1498:                                 push(@processing_errors,$cnum);
                   1499:                             }
                   1500:                         } else {
                   1501:                             push(@processing_errors,$cnum);
                   1502:                         }
                   1503:                     } else {
1.5       raeburn  1504:                         push(@nopermissions,$cnum);
1.2       raeburn  1505:                     }
                   1506:                 } else {
                   1507:                     push(@existing,$cnum);
                   1508:                 }
                   1509:             } else {
                   1510:                 push(@missingreq,$cnum);
                   1511:             }
1.1       raeburn  1512:         }
                   1513:     }
1.2       raeburn  1514:     my @changes = (@completed,@rejections);
                   1515:     if ($context eq 'domain') {
1.23      raeburn  1516:         @changes = map {$_.'_'.$queue} (@changes);
1.30      raeburn  1517:     } elsif ($context eq 'requestauthor') {
                   1518:         @changes = map {$_.'_approval'} (@changes);
1.43      raeburn  1519:     } elsif ($context eq 'requestusername') {
                   1520:         @changes = map {&escape($_).'_approval'} (@changes);
1.2       raeburn  1521:     }
1.1       raeburn  1522:     if (@rejections) {
1.3       raeburn  1523:         foreach my $item (@rejections) {
1.30      raeburn  1524:             if (($context eq 'course') || ($context eq 'requestauthor')) {
1.43      raeburn  1525:                 my ($user,$uname,$udom,%userrequest,$key,$dbname);
1.30      raeburn  1526:                 if ($context eq 'requestauthor') {
                   1527:                     $uname = $item;
                   1528:                     $udom = $cdom;
                   1529:                     $user = $uname.':'.$udom;
                   1530:                     $key = 'author';
1.43      raeburn  1531:                     $dbname = 'requestauthor';
1.30      raeburn  1532:                 } else {
                   1533:                     $user = $item;
                   1534:                     ($uname,$udom) = split(/:/,$user);
                   1535:                     $key = $cdom.'_'.$cnum;
1.43      raeburn  1536:                     $dbname = $namespace;
1.30      raeburn  1537:                 }
1.2       raeburn  1538:                 &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
1.26      raeburn  1539:                                              $now,$beneficiary,$sender,undef,undef,
                   1540:                                              $crstype);
1.30      raeburn  1541:                 %userrequest = (
                   1542:                     $key => {
1.1       raeburn  1543:                         timestamp   => $now,
                   1544:                         adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
1.30      raeburn  1545:                         status      => 'rejection',
1.1       raeburn  1546:                     }
                   1547:                 );
1.30      raeburn  1548:                 if ($context eq 'requestauthor') {
                   1549:                     $userrequest{'author_status'} = 'rejection';  
                   1550:                 }
1.1       raeburn  1551:                 my $userresult =
1.43      raeburn  1552:                     &Apache::lonnet::put($dbname,\%userrequest,$udom,$uname);
1.1       raeburn  1553:                 if ($userresult ne 'ok') {
1.30      raeburn  1554:                     push(@warn_rejects,$item);
1.1       raeburn  1555:                 }
1.43      raeburn  1556:             } elsif ($context eq 'requestusername') {
                   1557:                 my ($uname,$udom,$dbname);
                   1558:                 $uname = $item;
                   1559:                 $udom = $cdom;
                   1560:                 $dbname = 'nohist_requestedusernames';
                   1561:                 my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   1562:                 my %curr = &Apache::lonnet::get($dbname,[$uname],$cdom,$domconfiguser);
                   1563:                 if (ref($curr{$uname}) eq 'HASH') {
                   1564:                      $curr{$uname}{'status'} = 'rejected';
                   1565:                      $curr{$uname}{'timestamp'} = $now;
                   1566:                      $curr{$uname}{'adjudicator'} =  $env{'user.name'}.':'.$env{'user.domain'};
                   1567:                      undef($curr{$uname}{'tmpinfo'});
                   1568:                      undef($curr{$uname}{'upass'}); 
                   1569:                 }
                   1570:                 my $userresult =
                   1571:                     &Apache::lonnet::put($dbname,\%curr,$cdom,$domconfiguser);
                   1572:                 if ($userresult ne 'ok') {
                   1573:                     push(@warn_rejects,$uname);
                   1574:                 }
1.63      raeburn  1575:             } elsif (($context eq 'othdombydc') || ($context eq 'othdombyuser')) {
1.64    ! raeburn  1576:                 if ($context eq 'othdombydc') {
        !          1577:                     my ($extent,$role,$uname,$udom) = split(/:/,$item);
        !          1578:                     my $oldkey = 'pending:'.$uname.':'.$extent.':'.$role;
        !          1579:                     my $newkey = 'rejected:'.$uname.':'.$extent.':'.$role;
        !          1580:                     my $dbname = 'nohist_othdomqueued';
        !          1581:                     if (exists($requesthash{$oldkey})) {
        !          1582:                         if (ref($requesthash{$oldkey}) eq 'HASH') {
        !          1583:                             $requesthash{$newkey} = $requesthash{$oldkey};
        !          1584:                             delete($requesthash{$oldkey});
        !          1585:                             push(@toremove,$oldkey);
        !          1586:                             $requesthash{$newkey}->{'timestamp'} = $now;
        !          1587:                             $requesthash{$newkey}->{'adjudicator'} = $env{'user.name'}.':'.$env{'user.domain'};
        !          1588:                             my $statusres;
        !          1589:                             my $id = $uname.':'.$udom.':'.$role;
        !          1590:                             if (($role eq 'ca') || ($role eq 'aa')) {
        !          1591:                                 my ($audom,$auname) = ($extent =~ m{^/($match_domain)/($match_username)$});
        !          1592:                                 $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'rejected'},$audom,$auname);
        !          1593:                             } elsif ($extent =~ m{^/($match_domain)/$}) {
        !          1594:                                 my $domain = $1;
        !          1595:                                 my $configuser = &Apache::lonnet::get_domainconfiguser($domain);
        !          1596:                                 $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'rejected'},$domain,$configuser);
        !          1597:                             } else {
        !          1598:                                 my ($crsdom,$crsnum,$csec) = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/([^/]+))$});
        !          1599:                                 $id .= ':'.$csec;
        !          1600:                                 $statusres = &Apache::lonnet::put($dbname,{'status&'.$id => 'rejected'},$crsdom,$crsnum);
        !          1601:                             }
        !          1602:                             if ($statusres eq 'ok') {
        !          1603:                                 $requestedby{$item} = $requesthash{$newkey}->{'requester'};
        !          1604:                                 push(@rejectedreqs,$item);
        !          1605:                             }
        !          1606:                         }
        !          1607:                     }
        !          1608:                 }
1.1       raeburn  1609:             } else {
1.3       raeburn  1610:                 my $cnum = $item;
1.23      raeburn  1611:                 if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
1.3       raeburn  1612:                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
                   1613:                         my $requestkey = $cdom.'_'.$cnum;
1.23      raeburn  1614:                         my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
                   1615:                         my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
                   1616:                         my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
                   1617:                         $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
1.12      raeburn  1618:                         if ($crstype eq 'community') {
                   1619:                             $rejectedmsg = $rejectionmsg{'community'};
                   1620:                         } else {
                   1621:                             $rejectedmsg = $rejectionmsg{'course'};
                   1622:                         }
1.2       raeburn  1623:                         &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
                   1624:                                                      $cid,$coursedesc,$now,$beneficiary,
1.12      raeburn  1625:                                                      $sender,undef,undef,$crstype);
1.2       raeburn  1626:                         my %history =
1.3       raeburn  1627:                             &Apache::lonnet::restore($requestkey,'courserequests',
1.2       raeburn  1628:                                                      $ownerdom,$ownername);
                   1629:                         if ((ref($history{'details'}) eq 'HASH') &&
1.23      raeburn  1630:                             ($history{'disposition'} eq $queue)) {
1.2       raeburn  1631:                             my %reqhash = (
                   1632:                                             reqtime     => $history{'reqtime'},
                   1633:                                             crstype     => $history{'crstype'},
                   1634:                                             details     => $history{'details'},
                   1635:                                             disposition => $history{'disposition'},
                   1636:                                             status      => 'rejected',
                   1637:                                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                   1638:                                               );
                   1639:                             my $userresult =
1.3       raeburn  1640:                                 &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                   1641:                                                 'courserequests',$ownerdom,$ownername);
                   1642:                             if ($userresult eq 'ok') {
                   1643:                                 my %status = (
                   1644:                                                'status:'.$cdom.':'.$cnum => 'rejected'
                   1645:                                              );
                   1646:                                 my $statusresult =
                   1647:                                     &Apache::lonnet::put('courserequests',\%status,
                   1648:                                                          $ownerdom,$ownername);
                   1649:                                 if ($statusresult ne 'ok') {
                   1650:                                     push(@warn_rejects,$cnum);
                   1651:                                 }
                   1652:                             } else {
                   1653:                                 push(@warn_rejects,$cnum);
1.2       raeburn  1654:                             }
1.23      raeburn  1655:                             unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
1.19      raeburn  1656:                                 push(@warn_dels,$cnum);
                   1657:                             }
1.3       raeburn  1658:                         } else {
                   1659:                             push(@warn_rejects,$cnum);
1.2       raeburn  1660:                         }
1.3       raeburn  1661:                     } else {
                   1662:                         push(@existing,$cnum);
1.2       raeburn  1663:                     }
1.3       raeburn  1664:                 } else {
                   1665:                     push(@rejectionerrors,$cnum);
1.2       raeburn  1666:                 }
1.1       raeburn  1667:             }
                   1668:         }
                   1669:     }
1.30      raeburn  1670:     if (@toremove) {
1.43      raeburn  1671:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
1.30      raeburn  1672:         foreach my $item (@toremove) {
1.43      raeburn  1673:             if ($context eq 'requestauthor') {
                   1674:                 my %userrequest = (
                   1675:                     author => {
                   1676:                                 timestamp   => $now,
                   1677:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                   1678:                                 status      => 'deleted',
                   1679:                               },
                   1680:                               author_status => 'deleted',
                   1681:                 );
                   1682:                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$item);
                   1683:             } elsif ($context eq 'requestusername') {
                   1684:                 my $dbname = 'nohist_requestedusernames';
                   1685:                 my %curr = &Apache::lonnet::get($dbname,[$item],$cdom,$domconfiguser);
                   1686:                 if (ref($curr{$item}) eq 'HASH') {
                   1687:                     $curr{$item}{'status'} = 'deleted';
                   1688:                     $curr{$item}{'timestamp'} = $now;
                   1689:                     $curr{$item}{'adjudicator'} = $env{'user.name'}.':'.$env{'user.domain'};
                   1690:                     undef($curr{$item}{'upass'});
                   1691:                     undef($curr{$item}{'tmpinfo'}); 
                   1692:                 }
                   1693:             }
1.30      raeburn  1694:         }
1.64    ! raeburn  1695:         unless ($context eq 'othdombydc') {
        !          1696:             @toremove = map {$_.'_approval'} (@toremove);
        !          1697:         }
1.30      raeburn  1698:         my $delresult = &Apache::lonnet::del_dom($namespace,\@toremove,$cdom);
1.64    ! raeburn  1699:         if (($delresult ne 'ok') && ($context eq 'othdombydc')) {
        !          1700:             push(@warn_dels,@toremove);
        !          1701:         }
1.30      raeburn  1702:     }
1.1       raeburn  1703:     if (@changes) {
                   1704:         my $delresult;
                   1705:         if ($context eq 'course') {
                   1706:             $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
1.64    ! raeburn  1707:         } elsif (($context eq 'othdombydc') || ($context eq 'othdombyuser')) {
        !          1708:             if ($context eq 'othdombydc') {
        !          1709:                 $delresult = &Apache::lonnet::put($namespace,\%requesthash,$cdom,$confname);
        !          1710:             }
1.1       raeburn  1711:         } else {
                   1712:             $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
                   1713:         }
                   1714:         if ($delresult eq 'ok') {
                   1715:             my $namelink =
                   1716:                 &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
                   1717:             my ($chgmsg,$approvedlist,$rejectedlist);
                   1718:             if ($context eq 'course') {
                   1719:                 $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
1.2       raeburn  1720:                 if (@completed) {
                   1721:                     $approvedlist = join("\n",@completed);
1.12      raeburn  1722:                     if ($crstype eq 'community') {
                   1723:                         $output .= '<p>'.&mt('The following were enrolled in the community:').'<ul>';
                   1724:                     } else {
                   1725:                         $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
                   1726:                     }
1.2       raeburn  1727:                     foreach my $user (@completed) {
1.1       raeburn  1728:                         my ($uname,$udom) = split(/:/,$user);
                   1729:                         my $userlink =
                   1730:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
                   1731:                         $output .= '<li>'.$userlink.'</li>';
                   1732:                     }
                   1733:                     $output .= '</ul></p>';
                   1734:                 }
                   1735:                 if (@rejections) {
                   1736:                     $rejectedlist = join("\n",@rejections);
                   1737:                     $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
                   1738:                     foreach my $user (@rejections) {
                   1739:                         $output .= '<li>'.$user.'</li>';
                   1740:                     }
                   1741:                     $output .= '</ul></p>';
                   1742:                 }
1.2       raeburn  1743:                 if ($notifylist ne '') {
                   1744:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
                   1745:                                                  $now,'coursemanagers',$sender,
1.12      raeburn  1746:                                                  $approvedlist,$rejectedlist,$crstype);
1.2       raeburn  1747:                 }
1.30      raeburn  1748:             } elsif ($context eq 'requestauthor') {
1.42      bisitz   1749:                 $chgmsg = "'Action was taken on the following Authoring Space requests by [_1].',$namelink";
1.30      raeburn  1750:                 if (@completed) {
                   1751:                     $approvedlist = join("\n",@completed);
                   1752:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
                   1753:                     foreach my $uname (@completed) {
                   1754:                         my $userlink =
                   1755:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1756:                         $output .= '<li>'.$userlink.'</li>';
                   1757:                     }
                   1758:                     $output .= '</ul></p>';
                   1759:                 }
                   1760:                 if (@rejections) {
                   1761:                     $rejectedlist = join("\n",@rejections);
                   1762:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
                   1763:                     foreach my $uname (@rejections) {
                   1764:                         my $userlink =
                   1765:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1766:                         $output .= '<li>'.$userlink.'</li>';
                   1767:                     }
                   1768:                     $output .= '</ul></p>';
                   1769:                 }
                   1770:                 if ($notifylist ne '') {
                   1771:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
                   1772:                                                  $now,'authormanagers',$sender,
                   1773:                                                  $approvedlist,$rejectedlist);
                   1774:                 }
1.43      raeburn  1775:             } elsif ($context eq 'requestusername') {
                   1776:                 $chgmsg = "'Action was taken on the following LON-CAPA account requests by [_1].',$namelink";
                   1777:                 if (@completed) {
                   1778:                     $approvedlist = join("\n",@completed);
                   1779:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
                   1780:                     foreach my $uname (@completed) {
                   1781:                         $output .= '<li>'.$uname.'</li>';
                   1782: 
                   1783:                     }
                   1784:                     $output .= '</ul></p>';
                   1785:                 }
                   1786:                 if (@rejections) {
                   1787:                     $rejectedlist = join("\n",@rejections);
                   1788:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
                   1789:                     foreach my $uname (@rejections) {
                   1790:                         $output .= '<li>'.$uname.'</li>';
                   1791:                     }
                   1792:                     $output .= '</ul></p>';
                   1793:                 }
                   1794:                 if ($notifylist ne '') {
                   1795:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
                   1796:                                                  $now,'usernamemanagers',$sender,
                   1797:                                                  $approvedlist,$rejectedlist);
                   1798:                 }
1.63      raeburn  1799:             } elsif (($context eq 'othdombydc') || ($context eq 'othdombyuser')) {
1.64    ! raeburn  1800:                 $chgmsg = "'Action was taken on the following role requests by [_1].',$namelink";
        !          1801:                 my (%approvals_by_requester,%rejections_by_requester,%for_requester);
        !          1802:                 my $sender = $env{'user.name'}.':'.$env{'user.domain'};
        !          1803:                 if (@completed) {
        !          1804:                     $output .= '<p>'.&mt('The following roles in other domain(s) were assigned for user(s) in this domain:').'<ul>'.
        !          1805:                                &get_othdombydc_results('approved',\@completed,\%approvals_by_requester,\%requestedby,\%for_requester).
        !          1806:                                '</ul></p>';
        !          1807:                 }
        !          1808:                 if (@rejectedreqs) {
        !          1809:                     $output .= '<p>'.&mt('The following role assignments in other domain(s) for user(s) in this domain were rejected:').'<ul>'.
        !          1810:                                &get_othdombydc_results('rejected',\@rejectedreqs,\%rejections_by_requester,\%requestedby,\%for_requester).
        !          1811:                                '</ul></p>';
        !          1812:                 }
        !          1813:                 foreach my $key (sort(keys(%for_requester))) {
        !          1814:                     if (ref($approvals_by_requester{$key}) eq 'ARRAY') {
        !          1815:                         if (@{$approvals_by_requester{$key}} > 0) {
        !          1816:                             $approvedlist = join("\n\n",@{$approvals_by_requester{$key}});
        !          1817:                         }
        !          1818:                     }
        !          1819:                     if (ref($rejections_by_requester{$key}) eq 'ARRAY') {
        !          1820:                         if (@{$rejections_by_requester{$key}} > 0) {
        !          1821:                             $rejectedlist = join("\n\n",@{$rejections_by_requester{$key}});
        !          1822:                         }
        !          1823:                     }
        !          1824:                     if (($approvedlist ne '') || ($rejectedlist ne '')) {
        !          1825:                         &send_selfserve_notification($key,$chgmsg,'',$domdesc,$now,
        !          1826:                                                      $context,$sender,$approvedlist,
        !          1827:                                                      $rejectedlist);
        !          1828:                     }
        !          1829:                 }
1.1       raeburn  1830:             } else {
1.12      raeburn  1831:                 $chgmsg = "'Action was taken on the following course and community requests by [_1].',$namelink";
1.2       raeburn  1832:                 if (@completed) {
                   1833:                     $approvedlist = join("\n",@completed);
1.12      raeburn  1834:                     $output .= '<p>'.&mt('The following courses/communities were created:').'<ul>';
1.2       raeburn  1835:                     foreach my $cnum (@completed) {
                   1836:                         my $showcourse;
1.23      raeburn  1837:                         if (ref($requesthash{$cnum.'_'.$queue})) {
                   1838:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2       raeburn  1839:                         } else {
                   1840:                             $showcourse = $cnum;
                   1841:                         }
                   1842:                         my $syllabuslink =
                   1843:                             &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
1.40      raeburn  1844:                         if ($codes{$cnum}) {
1.49      raeburn  1845:                             $syllabuslink .= ' '.&mt('Unique code: [_1]',$codes{$cnum});
1.39      raeburn  1846:                         }
1.2       raeburn  1847:                         $output .= '<li>'.$syllabuslink.'</li>';
                   1848:                     }
                   1849:                     $output .= '</ul></p>';
                   1850:                 }
                   1851:                 if (@rejections) {
                   1852:                     $rejectedlist = join("\n",@rejections);
                   1853:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
                   1854:                     foreach my $cnum (@rejections) {
                   1855:                         my $showcourse;
1.23      raeburn  1856:                         if (ref($requesthash{$cnum.'_'.$queue})) {
                   1857:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2       raeburn  1858:                         } else {
                   1859:                             $showcourse = $cnum;
                   1860:                         }
                   1861:                         $output .= '<li>'.$showcourse.'</li>';
                   1862:                     }
                   1863:                     $output .= '</ul></p>';
                   1864:                 }
                   1865:                 if ($notifylist ne '') {
                   1866:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
                   1867:                                                  $now,'domainmanagers',$sender,
1.12      raeburn  1868:                                                  $approvedlist,$rejectedlist,$crstype);
1.2       raeburn  1869:                 }
1.1       raeburn  1870:             }
1.43      raeburn  1871:         } else {
1.64    ! raeburn  1872:             if (($context eq 'requestauthor') || ($context eq 'requestusername') || ($context eq 'othdombydc')) {
1.43      raeburn  1873:                 push(@warn_dels,@changes);
                   1874:             }
1.1       raeburn  1875:         }
                   1876:     }
                   1877:     if (@existing) {
                   1878:         if ($context eq 'course') {
                   1879:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
                   1880:             foreach my $user (@existing) {
                   1881:                 $output .= '<li>'.$user.'</li>';
                   1882:             }
                   1883:             $output .= '</ul></p>';
1.30      raeburn  1884:         } elsif ($context eq 'requestauthor') {
1.42      bisitz   1885:             $output .= '<p>'.&mt('Authoring Space requests from the following users were deleted because one already exists:').'<ul>';
1.30      raeburn  1886:             foreach my $uname (@existing) {
                   1887:                 my $userlink =
                   1888:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1889:                 $output .= '<li>'.$userlink.'</li>';
                   1890:             }
                   1891:             $output .= '</ul></p>';
1.1       raeburn  1892:         } else {
1.12      raeburn  1893:             $output .= '<p>'.&mt('The following course/community creation requests were deleted because the course or community has already been created:').'<ul>';
1.2       raeburn  1894:             foreach my $cnum (@existing) {
                   1895:                 my $showcourse;
                   1896:                 my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   1897:                 if ($coursehash{'description'} ne '') {
                   1898:                     $showcourse = $coursehash{'description'};
                   1899:                 } else {
                   1900:                     $showcourse = $cnum;
                   1901:                 }
                   1902:                 $output .= '<li>'.$showcourse.'</li>';
                   1903:             }
                   1904:             $output .= '</ul></p>';
1.1       raeburn  1905:         }
                   1906:     }
                   1907:     if (@missingreq) {
                   1908:         if ($context eq 'course') {
                   1909:             $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
                   1910:             foreach my $user (@missingreq) {
                   1911:                 $output .= '<li>'.$user.'</li>';
                   1912:             }
                   1913:             $output .= '</ul></p>';
1.30      raeburn  1914:         } elsif ($context eq 'requestauthor') {
                   1915:             $output .= '<p>'.&mt('The following requests were ignored because the request is no longer in the queue:').'<ul>';
                   1916:             foreach my $uname (@missingreq) {
                   1917:                 my $userlink =
                   1918:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1919:                 $output .= '<li>'.$userlink.'</li>';
                   1920:             }
                   1921:             $output .= '</ul></p>';
1.2       raeburn  1922:         } else {
1.12      raeburn  1923:             $output .= '<p>'.&mt('The following course/community creation requests were ignored because the request is no longer in the queue:').'<ul>';
1.2       raeburn  1924:             foreach my $cnum (@missingreq) {
                   1925:                 $output .= '<li>'.$cnum.'</li>';
                   1926:             }
                   1927:             $output .= '</ul></p>';
1.1       raeburn  1928:         }
                   1929:     }
                   1930:     if (@invalidusers) {
                   1931:         if ($context eq 'course') {
                   1932:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
                   1933:             foreach my $user (@invalidusers) {
                   1934:                 $output .= '<li>'.$user.'</li>';
                   1935:             }
                   1936:             $output .= '</ul></p>';
1.30      raeburn  1937:         } elsif ($context eq 'requestauthor') {
1.42      bisitz   1938:             $output .= '<p>'.&mt('The following Authoring Space requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
1.30      raeburn  1939:             foreach my $uname (@invalidusers) {
                   1940:                 my $userlink =
                   1941:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1942:                 $output .= '<li>'.$userlink.'</li>';
                   1943:             }
                   1944:             $output .= '</ul></p>';
1.1       raeburn  1945:         }
                   1946:     }
                   1947:     if (@limitexceeded) {
                   1948:         if ($context eq 'course') {
                   1949:             $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
                   1950:             foreach my $user (@limitexceeded) {
                   1951:                 $output .= '<li>'.$user.'</li>';
                   1952:             }
                   1953:             $output .= '</ul></p>';
                   1954:         }
                   1955:     }
1.5       raeburn  1956:     if (@nopermissions) {
1.30      raeburn  1957:         if ($context eq 'course') {
                   1958:             $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>';
                   1959:             foreach my $cnum (@nopermissions) {
                   1960:                 my $showcourse;
                   1961:                 if (ref($requesthash{$cnum.'_'.$queue})) {
                   1962:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
                   1963:                 } else {
                   1964:                     $showcourse = $cnum;
                   1965:                 }
                   1966:                 $output .= '<li>'.$showcourse.'</li>';
                   1967:             }
                   1968:             $output .= '</ul></p>';
                   1969:         } elsif ($context eq 'requestauthor') {
1.42      bisitz   1970:             $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  1971:             foreach my $uname (@nopermissions) {
                   1972:                 my $userlink =
                   1973:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1974:                 $output .= '<li>'.$userlink.'</li>';
1.5       raeburn  1975:             }
1.30      raeburn  1976:             $output .= '</ul></p>';
1.5       raeburn  1977:         }
                   1978:     }
1.2       raeburn  1979:     if (@processing_errors) {
1.1       raeburn  1980:         if ($context eq 'course') {
                   1981:             $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
1.2       raeburn  1982:             foreach my $user (@processing_errors) {
1.1       raeburn  1983:                 $output .= '<li>'.$user.'</li>';
                   1984:             }
                   1985:             $output .= '</ul></p>';
1.30      raeburn  1986:         } elsif ($context eq 'requestauthor') {
                   1987:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
                   1988:             foreach my $uname (@processing_errors) {
                   1989:                 my $userlink =
                   1990:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   1991:                 $output .= '<li>'.$userlink.'</li>';
                   1992:             }
                   1993:             $output .= '</ul></p>';
1.43      raeburn  1994:         } elsif ($context eq 'requestusername') {
                   1995:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
                   1996:             foreach my $uname (@processing_errors) {
                   1997:                 $output .= '<li>'.$uname.'</li>';
                   1998:             }
                   1999:             $output .= '</ul></p>';
1.1       raeburn  2000:         } else {
1.12      raeburn  2001:             $output .= '<p>'.&mt('The following course/community creation requests could not be processed because an error occurred:').'<ul>';
1.2       raeburn  2002:             foreach my $cnum (@processing_errors) {
                   2003:                 my $showcourse;
1.23      raeburn  2004:                 if (ref($requesthash{$cnum.'_'.$queue})) {
                   2005:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2       raeburn  2006:                 } else {
                   2007:                     $showcourse = $cnum;
                   2008:                 }
                   2009:                 $output .= '<li>'.$showcourse.'</li>';
                   2010:             }
                   2011:             $output .= '</ul></p>';
1.1       raeburn  2012:         }
                   2013:     }
1.3       raeburn  2014:     if (@rejectionerrors) {
1.12      raeburn  2015:         $output .= '<p>'.&mt('The following course/community creation request rejections could not be fully processed because an error occurred:').'<ul>';
1.3       raeburn  2016:         foreach my $cnum (@rejectionerrors) {
                   2017:             my $showcourse;
1.23      raeburn  2018:             if (ref($requesthash{$cnum.'_'.$queue})) {
                   2019:                 $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.3       raeburn  2020:             } else {
                   2021:                 $showcourse = $cnum;
                   2022:             }
                   2023:             $output .= '<li>'.$showcourse.'</li>';
                   2024:         }
                   2025:         $output .= '</ul></p>';
                   2026:     }
1.2       raeburn  2027:     if (@warn_approves || @warn_rejects) {
1.1       raeburn  2028:         if ($context eq 'course') {
                   2029:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
                   2030:             foreach my $user (@warn_approves) {
                   2031:                 $output .= '<li>'.$user.'</li>';
                   2032:             }
                   2033:             $output .= '</ul></p>';
1.30      raeburn  2034:         } elsif ($context eq 'requestauthor') {
                   2035:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own author request record:").'<ul>';
                   2036:             foreach my $uname (@warn_approves,@warn_rejects) {
                   2037:                 my $userlink =
                   2038:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   2039:                 $output .= '<li>'.$userlink.'</li>';
                   2040:             }
                   2041:             $output .= '</ul></p>';
1.43      raeburn  2042:         } elsif ($context eq 'requestusername') {
                   2043:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the account request record for the user:").'<ul>';
                   2044:             foreach my $uname (@warn_approves,@warn_rejects) {
                   2045:                 $output .= '<li>'.$uname.'</li>';
                   2046:             }
                   2047:             $output .= '</ul></p>';
1.64    ! raeburn  2048:         } elsif ($context eq 'othdombydc') {
        !          2049: 
1.1       raeburn  2050:         } else {
1.12      raeburn  2051:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when updating the requestor's own requests record:").'<ul>';
1.2       raeburn  2052:             foreach my $cnum (@warn_approves,@warn_rejects) {
                   2053:                 my $showcourse;
1.23      raeburn  2054:                 if (ref($requesthash{$cnum.'_'.$queue})) {
                   2055:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
1.2       raeburn  2056:                 } else {
                   2057:                     $showcourse = $cnum;
                   2058:                 }
                   2059:                 $output .= '<li>'.$showcourse.'</li>';
1.1       raeburn  2060:             }
                   2061:             $output .= '</ul></p>';
                   2062:         }
                   2063:     }
1.19      raeburn  2064:     if (@warn_dels) {
1.30      raeburn  2065:         if ($context eq 'requestauthor') {
                   2066:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
                   2067:             foreach my $uname (@warn_dels) {
                   2068:                 my $userlink =
                   2069:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
                   2070:                 $output .= '<li>'.$userlink.'</li>';
                   2071:             }
                   2072:             $output .= '</ul></p>';
1.43      raeburn  2073:         } elsif ($context eq 'requestusername') {
                   2074:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
                   2075:             foreach my $item (@warn_dels) {
                   2076:                 my ($escuname) = split(/_/,$item);
                   2077:                 $output .= '<li>'.&unescape($escuname).'</li>';
                   2078:             }
1.64    ! raeburn  2079:             $output .= '</ul></p>';
        !          2080:         } elsif ($context eq 'othdombydc') {
        !          2081:             $output .= '<p>'.
        !          2082:                        &mt("For the following queued role assignments an error occurred when removing the item from the queue:").
        !          2083:                        '<ul>'.&get_othdombydc_results('dequeue_error',\@warn_dels).'</ul>'.
        !          2084:                        '</p>';
1.30      raeburn  2085:         } else {
1.36      bisitz   2086:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when removing requests from the pending queue:").'<ul>';
1.30      raeburn  2087:             foreach my $cnum (@warn_dels) {
                   2088:                 my $showcourse;
                   2089:                 if (ref($requesthash{$cnum.'_'.$queue})) {
                   2090:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
                   2091:                 } else {
                   2092:                     $showcourse = $cnum;
                   2093:                 }
                   2094:                 $output .= '<li>'.$showcourse.'</li>';
1.19      raeburn  2095:             }
1.30      raeburn  2096:             $output .= '</ul></p>';
1.19      raeburn  2097:         }
                   2098:     }
1.1       raeburn  2099:     return $output;
                   2100: }
                   2101: 
1.64    ! raeburn  2102: sub get_othdombydc_results {
        !          2103:     my ($action,$items,$results,$requestedby,$for_requester) = @_;
        !          2104:     return unless (ref($items) eq 'ARRAY');
        !          2105:     unless ($action eq 'dequeue_error') {
        !          2106:         return unless ((ref($results) eq 'HASH') && (ref($requestedby) eq 'HASH') &&
        !          2107:                        (ref($for_requester) eq 'HASH'));
        !          2108:     }
        !          2109:     my $output;
        !          2110:     foreach my $item (@{$items}) {
        !          2111:         my ($extent,$role,$uname,$udom);
        !          2112:         if ($action eq 'approved') {
        !          2113:             (my $num,$extent,$role,$uname,$udom) = split(/:/,$item);
        !          2114:         } elsif ($action eq 'rejected') {
        !          2115:             ($extent,$role,$uname,$udom) = split(/:/,$item);
        !          2116:         } elsif ($action eq 'dequeue_error') {
        !          2117:             (my $oldstatus,$uname,$extent,$role) = split(/:/,$item);
        !          2118:             $udom = $env{'request.role.domain'};
        !          2119:         }
        !          2120:         my @text = (&mt('User: [_1]',$uname));
        !          2121:         if (($role eq 'ca') || ($role eq 'aa')) {
        !          2122:             my $plainrole = &Apache::lonnet::plaintext($role);
        !          2123:             my ($audom,$auname) = ($extent =~ m{^/($match_domain)/($match_username)$});
        !          2124:             my $title = &Apache::loncommon::plainname($auname,$audom);
        !          2125:             push(@text,(&mt('Role: [_1]',$plainrole),
        !          2126:                         &mt('Domain: [_1]',&Apache::lonnet::domain($audom)),
        !          2127:                         &mt('Authoring Space belonging to: [_1]',$title)));
        !          2128:         } elsif ($extent =~ m{^/($match_domain)/$}) {
        !          2129:             my $domain = $1;
        !          2130:             my $plainrole = &Apache::lonnet::plaintext($role);
        !          2131:             if (&Apache::lonnet::domain($domain) ne '') {
        !          2132:                 push(@text,(&mt('Role: [_1]',$plainrole),
        !          2133:                             &mt('Domain: [_1]',&Apache::lonnet::domain($domain))));
        !          2134:             }
        !          2135:         } else {
        !          2136:             my ($crsdom,$crsnum,$csec) = ($extent =~ m{^/($match_domain)/($match_courseid)(?:|/([^/]+)$)});
        !          2137:             if (($crsdom ne '') && ($crsnum ne '')) {
        !          2138:                 my %info = &Apache::lonnet::coursedescription("$crsdom/$crsnum");
        !          2139:                 my $plainrole = &Apache::lonnet::plaintext($role,$info{'type'});
        !          2140:                 push(@text,(&mt('Role: [_1]',$plainrole),
        !          2141:                             &mt('Domain: [_1]',&Apache::lonnet::domain($crsdom)),
        !          2142:                             &mt("$info{'type'}: [_1]",$info{'description'})));
        !          2143:                 if ($csec ne '') {
        !          2144:                     push(@text,&mt('Section: [_1]',$csec));
        !          2145:                 }
        !          2146:             }
        !          2147:         }
        !          2148:         $output .= '<li><ul><li>'.join('</li><li>',@text).'</li></ul></li>';
        !          2149:         unless ($action eq 'dequeue_error') {
        !          2150:             push(@{$results->{$requestedby->{$item}}},join("\n",@text));
        !          2151:             $for_requester->{$requestedby->{$item}} = 1;
        !          2152:         }
        !          2153:     }
        !          2154:     return $output;
        !          2155: }
        !          2156: 
1.63      raeburn  2157: sub requester_roles {
                   2158:     my ($requname,$requdom,$activeroles) = @_;
                   2159:     if (ref($activeroles) eq 'HASH') {
                   2160:         my %roleshash = &Apache::lonnet::get_my_roles($requname,$requdom,'userroles');
                   2161:         $activeroles->{$requname.':'.$requdom} = \%roleshash;
                   2162:     }
                   2163:     return;
                   2164: }
                   2165: 
                   2166: sub requester_has_perm {
                   2167:     my ($crsdom,$crsnum,$mrole,$requesterroles) = @_;
                   2168:     return unless (ref($requesterroles) eq 'HASH');
                   2169:     my $has_perm;
                   2170:     foreach my $key (keys(%{$requesterroles})) {
                   2171:         if ($crsnum eq '') {
                   2172:             next unless ($key =~ /^\Q:$crsdom:\E/);
                   2173:         } else {
                   2174:             next unless (($key =~ /^\Q$crsnum:$crsdom:\E/) || ($key =~ /^\Q:$crsdom:\E/));
                   2175:         }
                   2176:         my ($keycrs,$keydom,$keyrole) = split(/:/,$key);
                   2177:         if (($keycrs ne '') && ($crsnum ne '')) {
                   2178:             if ($keycrs eq $crsnum) {
                   2179:                 if ($Apache::lonnet::pr{$keyrole.':c'} =~ /(^|:)c\Q$mrole\E(&|:)/) {
                   2180:                     $has_perm = 1;
                   2181:                     last;
                   2182:                 }
                   2183:             }
                   2184:         } else {
                   2185:             if ($Apache::lonnet::pr{$keyrole.':d'} =~ /(^|:)c\Q$mrole\E(&|:)/) {
                   2186:                 $has_perm = 1;
                   2187:                 last;
                   2188:             }
                   2189:         }
                   2190:     }
                   2191:     return $has_perm;
                   2192: }
                   2193: 
1.1       raeburn  2194: sub get_student_counts {
                   2195:     my ($cdom,$cnum) = @_;
                   2196:     my (%idx,%stucounts);
                   2197:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
                   2198:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2199:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2200:     while (my ($student,$data) = each(%$classlist)) {
                   2201:         if (($data->[$idx{'status'}] eq 'Active') ||
                   2202:             ($data->[$idx{'status'}] eq 'Future')) {
                   2203:             if ($data->[$idx{'type'}] eq 'selfenroll') {
                   2204:                 $stucounts{'selfenroll'} ++;
                   2205:             }
                   2206:             $stucounts{'allstudents'} ++;
                   2207:         }
                   2208:     }
                   2209:     return (\%stucounts,\%idx,$classlist);
                   2210: }
                   2211: 
1.2       raeburn  2212: sub course_creation {
1.59      raeburn  2213:     my ($dom,$cnum,$context,$details,$logmsg,$clonemsg,$newusermsg,$addresult,
                   2214:         $enrollcount,$output,$keysmsg,$domdefs,$longroles,$coderef,$customhash,
                   2215:         $callercontext,$user_lh) = @_;
1.2       raeburn  2216:     unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') && 
                   2217:             (ref($longroles) eq 'HASH')) {
1.53      raeburn  2218:         return ('error: Invalid request');
1.2       raeburn  2219:     }
                   2220:     my ($result,$ownername,$ownerdom);
                   2221:     my $crstype = $details->{'crstype'};
1.41      raeburn  2222:     my $coursedesc = $details->{'cdescr'};
1.47      raeburn  2223:     my $accessstart = $details->{'accessstart'};
                   2224:     my $accessend = $details->{'accessend'};
                   2225:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
1.40      raeburn  2226:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   2227:         if (ref($domconfig{'requestcourses'}{'uniquecode'}) eq 'HASH') {
                   2228:             if ($domconfig{'requestcourses'}{'uniquecode'}{$crstype}) {
                   2229:                 $details->{'uniquecode'} = 1;
                   2230:             }
                   2231:         }
                   2232:     }
1.2       raeburn  2233:     if ($context eq 'domain') {
                   2234:         $ownername = $details->{'owner'};
                   2235:         $ownerdom  = $details->{'domain'};
                   2236:     } else {
                   2237:         $ownername = $env{'user.name'};
                   2238:         $ownerdom  = $env{'user.domain'};
                   2239:     }
1.41      raeburn  2240:     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
1.2       raeburn  2241:     my $owneremail;
                   2242:     my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
                   2243:     foreach my $email ('permanentemail','critnotification','notification') {
                   2244:         $owneremail = $emails{$email};
                   2245:         last if ($owneremail ne '');
                   2246:     }
1.61      raeburn  2247:     my %reqdetails = &build_batchcreatehash($dom,$cnum,$context,$details,$owneremail,$domdefs);
1.2       raeburn  2248:     my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
1.59      raeburn  2249:                   \%reqdetails,$longroles,$logmsg,$clonemsg,$newusermsg,$addresult,
                   2250:                   $enrollcount,$output,$keysmsg,$ownerdom,$ownername,$cnum,$crstype,
                   2251:                   $coderef,$callercontext,$user_lh);
1.41      raeburn  2252:     my $postprocess;
1.2       raeburn  2253:     if ($cid eq "/$dom/$cnum") {
                   2254:         $result = 'created';
1.41      raeburn  2255:         my $code;
                   2256:         if (ref($coderef)) {
                   2257:             $code = $$coderef;
                   2258:         }
                   2259:         $postprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,$result,$ownername,
1.47      raeburn  2260:                                                            $ownerdom,$fullname,$coursedesc,$code,
                   2261:                                                            $accessstart,$accessend,$customhash);
1.2       raeburn  2262:     } else {
                   2263:         $result = 'error: '.$cid;
                   2264:     }
1.41      raeburn  2265:     return ($result,$postprocess);
1.2       raeburn  2266: }
                   2267: 
                   2268: sub build_batchcreatehash {
1.61      raeburn  2269:     my ($dom,$cnum,$context,$details,$owneremail,$domdefs) = @_;
1.2       raeburn  2270:     my %batchhash;
1.60      raeburn  2271:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift tinyurls enrollstart enrollend accessstart accessend sections users uniquecode};
1.2       raeburn  2272:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
1.17      raeburn  2273:         my $emailenc = &escape($owneremail);
1.2       raeburn  2274:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
                   2275:         foreach my $item (@items) {
                   2276:             $batchhash{$item} = $details->{$item};
                   2277:         }
1.60      raeburn  2278:         if (ref($details->{'crosslists'}) eq 'HASH') {
                   2279:             foreach my $key (keys(%{$details->{'crosslists'}})) {
                   2280:                 if (ref($details->{'crosslists'}->{$key}) eq 'HASH') {
1.61      raeburn  2281:                     my $instsec = $details->{crosslists}->{$key}->{instsec};
                   2282:                     $batchhash{'crosslists'}{$key}{'inst'} = $details->{crosslists}->{$key}->{instcode};
                   2283:                     my $crskey = $cnum.':'.$batchhash{'crosslists'}{$key}{'inst'};
                   2284:                     my %formatted = &Apache::lonnet::auto_instsec_reformat($dom,'clutter',
                   2285:                                                                            {$crskey => [$instsec]});
                   2286:                     if (ref($formatted{$crskey}) eq 'ARRAY') {
                   2287:                         $batchhash{'crosslists'}{$key}{'inst'} .= $formatted{$crskey}->[0];
                   2288:                     }
1.60      raeburn  2289:                     $batchhash{'crosslists'}{$key}{'loncapa'} = $details->{crosslists}->{$key}->{loncapa};
                   2290:                 }
                   2291:             }
                   2292:         }
1.2       raeburn  2293:         $batchhash{'title'} = $details->{'cdescr'};
                   2294:         $batchhash{'coursecode'} = $details->{'instcode'};
1.35      raeburn  2295:         if ($domdefs->{'officialcredits'} || $domdefs->{'unofficialcredits'}) {
                   2296:             $batchhash{'defaultcredits'} = $details->{'defaultcredits'};
                   2297:         }
1.2       raeburn  2298:         $batchhash{'emailenc'} = $emailenc;
                   2299:         $batchhash{'adds'} = $details->{'autoadds'};
                   2300:         $batchhash{'drops'} = $details->{'autodrops'};
                   2301:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
                   2302:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
                   2303:         if ($details->{'crstype'} eq 'community') {
                   2304:             $batchhash{'crstype'} = 'Community';
1.53      raeburn  2305:         } elsif ($details->{'crstype'} eq 'placement') {
                   2306:             $batchhash{'crstype'} = 'Placement';
1.2       raeburn  2307:         } else {
1.41      raeburn  2308:             if ($details->{'crstype'} eq 'textbook') {
                   2309:                 if ($details->{'clonecrs'} && $details->{'clonedom'}) {
                   2310:                     my %clonedfrom = &Apache::lonnet::coursedescription($details->{'clonedom'}.'_'.$details->{'clonecrs'});
                   2311:                     $batchhash{'textbook'} = $clonedfrom{'description'};
                   2312:                 }
1.62      raeburn  2313:             } elsif ($details->{'crstype'} eq 'lti') {
                   2314:                 $batchhash{'lti'} = 1;
1.41      raeburn  2315:             }
1.2       raeburn  2316:             $batchhash{'crstype'} = 'Course';
                   2317:         }
1.8       raeburn  2318:         my ($owner_firstname,$owner_lastname);
                   2319:         if ($context eq 'domain') {
                   2320:             my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
                   2321:                                                            $details->{'owner'},
                   2322:                                                            'firstname','lastname');
                   2323:             $owner_firstname = $userenv{'firstname'};
                   2324:             $owner_lastname = $userenv{'lastname'};
                   2325:         } else {
                   2326:             $owner_firstname = $env{'environment.firstname'};
                   2327:             $owner_lastname = $env{'environment.lastname'};
                   2328:         }
                   2329:         if (ref($details->{'personnel'}) eq 'HASH') {
                   2330:             %{$batchhash{'users'}} = %{$details->{'personnel'}};
                   2331:             if (ref($batchhash{'users'}) eq 'HASH') {  
                   2332:                 foreach my $userkey (keys(%{$batchhash{'users'}})) {
                   2333:                     if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
                   2334:                         if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
                   2335:                             foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
                   2336:                                 my $start = '';
                   2337:                                 my $end = '';
                   2338:                                 if ($role eq 'st') {
                   2339:                                     $start = $details->{'accessstart'};
                   2340:                                     $end = $details->{'accessend'};
                   2341:                                 }
                   2342:                                 $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
                   2343:                                 $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
                   2344:                             } 
                   2345:                         }
                   2346:                     }
                   2347:                 }
                   2348:             }
                   2349:         }
                   2350:         $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
                   2351:         $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
                   2352:         $batchhash{'users'}{$owner}{emailenc} = $emailenc;
                   2353:         $batchhash{'users'}{$owner}{owneremail} = $owneremail;
1.55      raeburn  2354:         $batchhash{'setcomment'} = 1;
1.2       raeburn  2355:     }
                   2356:     return %batchhash;
                   2357: }
                   2358: 
1.4       raeburn  2359: sub can_clone_course {
1.51      raeburn  2360:     my ($uname,$udom,$clonecrs,$clonedom,$crstype,$dom,$instcode) = @_;
1.4       raeburn  2361:     my $canclone;
1.11      raeburn  2362:     my $ccrole = 'cc';
1.12      raeburn  2363:     if ($crstype eq 'community') {
1.11      raeburn  2364:         $ccrole = 'co';
                   2365:     }
1.4       raeburn  2366:     my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
1.11      raeburn  2367:                                                   [$ccrole],[$clonedom]);
                   2368:     if (exists($roleshash{$clonecrs.':'.$clonedom.':'.$ccrole})) {
1.4       raeburn  2369:         $canclone = 1;
                   2370:     } else {
1.51      raeburn  2371:         my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,
                   2372:                                                          ('cloners','internal.coursecode'));
1.4       raeburn  2373:         my $cloners = $courseenv{'cloners'};
1.51      raeburn  2374:         my $clonefromcode = $courseenv{'internal.coursecode'};
1.4       raeburn  2375:         if ($cloners ne '') {
                   2376:             my @cloneable = split(',',$cloners);
                   2377:             if (grep(/^\*$/,@cloneable)) {
                   2378:                 $canclone = 1;
1.51      raeburn  2379:             } elsif (grep(/^\*:\Q$udom\E$/,@cloneable)) {
                   2380:                 $canclone = 1;
                   2381:             } elsif (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
                   2382:                 $canclone = 1;
1.4       raeburn  2383:             }
1.51      raeburn  2384:             unless ($canclone) {
                   2385:                 if (($clonefromcode) && ($instcode) && ($clonedom eq $dom)) {
                   2386:                     my (%gotdomdefaults,%gotcodedefaults);
                   2387:                     foreach my $cloner (@cloneable) {
                   2388:                         if (($cloner ne '*') && ($cloner !~ /^\*\:$match_domain$/) &&
                   2389:                             ($cloner !~ /^$match_username\:$match_domain$/) && ($cloner ne '')) {
                   2390:                             if ($cloner =~ /\=/) {
                   2391:                                 my (%codedefaults,@code_order);
                   2392:                                 if (ref($gotcodedefaults{$clonedom}) eq 'HASH') {
                   2393:                                     if (ref($gotcodedefaults{$clonedom}{'defaults'}) eq 'HASH') {
                   2394:                                         %codedefaults = %{$gotcodedefaults{$clonedom}{'defaults'}};
                   2395:                                     }
                   2396:                                     if (ref($gotcodedefaults{$clonedom}{'order'}) eq 'ARRAY') {
                   2397:                                         @code_order = @{$gotcodedefaults{$dom}{'order'}};
                   2398:                                     }
                   2399:                                 } else {
                   2400:                                     &Apache::lonnet::auto_instcode_defaults($clonedom,
                   2401:                                                                             \%codedefaults,
                   2402:                                                                             \@code_order);
                   2403:                                     $gotcodedefaults{$clonedom}{'defaults'} = \%codedefaults;
                   2404:                                     $gotcodedefaults{$clonedom}{'order'} = \@code_order;
                   2405:                                 }
                   2406:                                 if (@code_order > 0) {
                   2407:                                     if (&Apache::lonnet::check_instcode_cloning(\%codedefaults,\@code_order,
                   2408:                                                                                 $cloner,$clonefromcode,$instcode)) {
                   2409:                                         $canclone = 1;
                   2410:                                         last; 
                   2411:                                     }
                   2412:                                 }
                   2413:                             }
                   2414:                         }
                   2415:                     }
                   2416:                 }
1.4       raeburn  2417:             }
1.51      raeburn  2418:         } else {
                   2419:             my %domdefs = &Apache::lonnet::get_domain_defaults($clonedom);
                   2420:             if ($domdefs{'canclone'}) {
                   2421:                 unless ($domdefs{'canclone'} eq 'none') {
                   2422:                     if ($domdefs{'canclone'} eq 'domain') {
                   2423:                         if ($udom eq $clonedom) {
                   2424:                             $canclone = 1;
                   2425:                         }
                   2426:                     } elsif (($clonefromcode) && ($instcode) &&
                   2427:                              ($clonedom eq $dom)) {
                   2428:                         if (&Apache::lonnet::default_instcode_cloning($clonedom,$domdefs{'canclone'},
                   2429:                                                                       $clonefromcode,$instcode)) {
                   2430:                             $canclone = 1;
                   2431:                         }
                   2432:                     }
                   2433:                 }
1.4       raeburn  2434:             }
                   2435:         }
1.18      raeburn  2436:         unless ($canclone) {
                   2437:             if (&Apache::lonnet::is_course_owner($clonedom,$clonecrs,$uname,$udom)) {
1.20      bisitz   2438:                 $canclone = 1;
1.18      raeburn  2439:             }
                   2440:         }
1.4       raeburn  2441:     }
                   2442:     return $canclone;
                   2443: }
                   2444: 
1.13      raeburn  2445: sub get_processtype {
1.30      raeburn  2446:     my ($context,$uname,$udom,$isadv,$dom,$crstype,$inststatuses,$domconfig) = @_;
1.13      raeburn  2447:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                   2448:     if ($uname eq '' || $udom eq '') {
                   2449:         $uname = $env{'user.name'};
                   2450:         $udom = $env{'user.domain'};
                   2451:         $isadv = $env{'user.adv'};
                   2452:     }
1.30      raeburn  2453:     my (%userenv,%settings,$val,@options,$envkey);
                   2454:     if ($context eq 'course') {
                   2455:         @options = ('autolimit','validate','approval');
                   2456:         $envkey = 'requestcourses.'.$crstype;
                   2457:         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
                   2458:             if (ref($domconfig->{'requestcourses'}->{$crstype}) eq 'HASH') {
                   2459:                 %settings = %{$domconfig->{'requestcourses'}->{$crstype}};
                   2460:             }
                   2461:         }
                   2462:     } else {
                   2463:         @options = ('automatic','approval');
                   2464:         $envkey = 'requestauthor';
                   2465:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') { 
                   2466:             %settings = %{$domconfig->{'requestauthor'}};
                   2467:         }
                   2468:     }
                   2469:     if (($dom eq $udom) || ($context eq 'requestauthor')) {
1.13      raeburn  2470:         %userenv =
1.30      raeburn  2471:             &Apache::lonnet::userenvironment($udom,$uname,$envkey,'inststatus');
                   2472:         if ($userenv{$envkey}) {
                   2473:             $val = $userenv{$envkey};
1.13      raeburn  2474:             @{$inststatuses} = ('_custom_');
                   2475:         } else {
1.30      raeburn  2476:             my %alltasks;
                   2477:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
1.33      raeburn  2478:                 $val = $settings{'_LC_adv'};
1.30      raeburn  2479:                 @{$inststatuses} = ('_LC_adv_');
                   2480:             } else {
                   2481:                 if ($userenv{'inststatus'} ne '') {
                   2482:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                   2483:                 } else {
                   2484:                     @{$inststatuses} = ('default');
                   2485:                 }
                   2486:                 foreach my $status (@{$inststatuses}) {
                   2487:                     if (exists($settings{$status})) {
                   2488:                         my $value = $settings{$status};
                   2489:                         next unless ($value);
                   2490:                         unless (exists($alltasks{$value})) {
                   2491:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                   2492:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                   2493:                                     push(@{$alltasks{$value}},$status);
1.13      raeburn  2494:                                 }
1.30      raeburn  2495:                             } else {
                   2496:                                 @{$alltasks{$value}} = ($status);
1.13      raeburn  2497:                             }
                   2498:                         }
1.30      raeburn  2499:                     }
                   2500:                 }
                   2501:                 my $maxlimit = 0;
                   2502:                 if ($context eq 'course') {
                   2503:                     foreach my $key (sort(keys(%alltasks))) {
                   2504:                         if ($key =~ /^autolimit=(\d*)$/) {
                   2505:                             if ($1 eq '') {
                   2506:                                 $val ='autolimit=';
                   2507:                                 last;
                   2508:                             } elsif ($1 > $maxlimit) {
                   2509:                                 $maxlimit = $1;
1.13      raeburn  2510:                             }
                   2511:                         }
1.30      raeburn  2512:                     }
                   2513:                 }
                   2514:                 if (($context eq 'requestauthor') || (!$val)) {
                   2515:                     if ($context eq 'course' && $maxlimit) {
                   2516:                         $val = 'autolimit='.$maxlimit;
                   2517:                     } else {
                   2518:                         foreach my $option (@options) {
                   2519:                             if ($alltasks{$option}) {
                   2520:                                 $val = $option;
                   2521:                                 last;
1.13      raeburn  2522:                             }
                   2523:                         }
                   2524:                     }
                   2525:                 }
                   2526:             }
                   2527:         }
                   2528:     } else {
                   2529:         %userenv = &Apache::lonnet::userenvironment($udom,$uname,'reqcrsotherdom.'.$crstype);
                   2530:         if ($userenv{'reqcrsotherdom.'.$crstype}) {
                   2531:             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
                   2532:             my $optregex = join('|',@options);
                   2533:             foreach my $item (@doms) {
                   2534:                 my ($extdom,$extopt) = split(':',$item);
                   2535:                 if ($extdom eq $dom) {
                   2536:                     if ($extopt =~ /^($optregex)(=?\d*)$/) {
                   2537:                         $val = $1.$2;
                   2538:                     }
                   2539:                     last;
                   2540:                 }
                   2541:             }
                   2542:             @{$inststatuses} = ('_external_');
                   2543:         }
                   2544:     }
                   2545:     return $val;
                   2546: }
                   2547: 
1.14      raeburn  2548: sub queued_selfenrollment {
                   2549:     my ($notitle) = @_;
                   2550:     my $output;
                   2551:     my %selfenrollrequests = &Apache::lonnet::dump('selfenrollrequests');
                   2552:     my %reqs_by_date;
                   2553:     foreach my $item (keys(%selfenrollrequests)) {
                   2554:         if (ref($selfenrollrequests{$item}) eq 'HASH') {
                   2555:             if ($selfenrollrequests{$item}{'status'} eq 'request') {
                   2556:                 if ($selfenrollrequests{$item}{'timestamp'}) {
                   2557:                     push(@{$reqs_by_date{$selfenrollrequests{$item}{'timestamp'}}},$item);
                   2558:                 }
                   2559:             }
                   2560:         }
                   2561:     }
                   2562:     if (keys(%reqs_by_date)) {
                   2563:         unless ($notitle) {
1.32      raeburn  2564:             $output .= '<br /><b>'.&mt('Enrollment requests pending Course Coordinator approval').'</b><br />';
1.14      raeburn  2565:         }
                   2566:         $output .= &Apache::loncommon::start_data_table().
                   2567:                    &Apache::loncommon::start_data_table_header_row().
                   2568:                    '<th>'.&mt('Date requested').'</th><th>'.&mt('Course title').'</th>'.
                   2569:                    '<th>'.&mt('User role').'</th><th>'.&mt('Section').'</th>'.
                   2570:                    &Apache::loncommon::end_data_table_header_row();
                   2571:         my @sorted = sort { $a <=> $b } (keys(%reqs_by_date));
                   2572:         foreach my $item (@sorted) {
                   2573:             if (ref($reqs_by_date{$item}) eq 'ARRAY') {
                   2574:                 foreach my $crs (@{$reqs_by_date{$item}}) {
                   2575:                     my %courseinfo = &Apache::lonnet::coursedescription($crs);
                   2576:                     my $usec = $selfenrollrequests{$crs}{'section'};
1.16      raeburn  2577:                     my $rolename = &Apache::lonnet::plaintext('st',$courseinfo{'type'},$crs);
1.14      raeburn  2578:                     if ($usec eq '') {
                   2579:                         $usec = &mt('No section');
                   2580:                     }
                   2581:                     $output .= &Apache::loncommon::start_data_table_row().
                   2582:                                '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
                   2583:                                '<td>'.$courseinfo{'description'}.'</td>'.
                   2584:                                '<td>'.$rolename.'</td><td>'.$usec.'</td>'.
                   2585:                                &Apache::loncommon::end_data_table_row();
                   2586:                 }
                   2587:             }
                   2588:         }
                   2589:         $output .= &Apache::loncommon::end_data_table();
                   2590:     }
                   2591:     return $output;
                   2592: }
                   2593: 
1.19      raeburn  2594: sub update_coursereq_status {
1.26      raeburn  2595:     my ($reqhash,$dom,$cnum,$reqstatus,$context,$udom,$uname) = @_;
1.19      raeburn  2596:     my ($storeresult,$statusresult,$output);
                   2597:     my $requestkey = $dom.'_'.$cnum;
                   2598:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2599:         $storeresult = &Apache::lonnet::store_userdata($reqhash,$requestkey,
1.26      raeburn  2600:                                                        'courserequests',$udom,$uname);
1.19      raeburn  2601:         if ($storeresult eq 'ok') {
                   2602:             my %status = (
                   2603:                              'status:'.$dom.':'.$cnum => $reqstatus,
                   2604:                          );
1.26      raeburn  2605:             $statusresult = &Apache::lonnet::put('courserequests',\%status,$udom,$uname);
1.19      raeburn  2606:         }
                   2607:     } else {
                   2608:         $storeresult = 'error: invalid requestkey format';
                   2609:     }
                   2610:     if ($storeresult ne 'ok') {
                   2611:         $output = &mt('An error occurred saving a record of the details of your request: [_1].',$storeresult);
                   2612:         if ($context eq 'domain') {
                   2613:             $output .= "\n";  
                   2614:         } else {
                   2615:             $output =  '<span class="LC_warning">'.$output.'</span><br />';
                   2616:         }
1.26      raeburn  2617:         &Apache::lonnet::logthis("Error saving course request - $requestkey for $uname:$udom - $storeresult");
1.19      raeburn  2618:     } elsif ($statusresult ne 'ok') {
                   2619:         $output = &mt('An error occurred saving a record of the status of your request: [_1].',$statusresult);
                   2620:         if ($context eq 'domain') {
                   2621:             $output .= "\n";
                   2622:         } else {
                   2623:             $output = '<span class="LC_warning">'.$output.'</span><br />';
                   2624:         }
1.26      raeburn  2625:         &Apache::lonnet::logthis("Error saving course request status for $requestkey (for $uname:$udom) - $statusresult");
1.19      raeburn  2626:     }
                   2627:     return ($storeresult,$output);
                   2628: }
                   2629: 
1.23      raeburn  2630: sub process_official_reqs {
1.26      raeburn  2631:     my ($context,$dom,$dcname,$dcdom) = @_;
1.23      raeburn  2632:     my $reqsnamespace = 'courserequestqueue';
                   2633:     my %requesthash =
1.24      raeburn  2634:         &Apache::lonnet::dump_dom($reqsnamespace,$dom,'_pending');
1.23      raeburn  2635:     my (%newcids,%longroles,%stillpending);
                   2636:     my @courseroles = ('cc','in','ta','ep','ad','st');
                   2637:     foreach my $role (@courseroles) {
                   2638:         $longroles{$role}=&Apache::lonnet::plaintext($role);
                   2639:     }
                   2640:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
1.59      raeburn  2641:     my ($output,$linefeed,$user_lh);
1.23      raeburn  2642:     if ($context eq 'auto') {
                   2643:         $linefeed = "\n";
1.59      raeburn  2644:         $user_lh = &Apache::loncommon::user_lang($dcname,$dcdom);
1.23      raeburn  2645:     } else {
                   2646:         $linefeed = '<br />'."\n";
                   2647:     }
                   2648:     foreach my $key (keys(%requesthash)) {
                   2649:         my ($cnum,$status) = split('_',$key);
                   2650:         next if (&Apache::lonnet::homeserver($cnum,$dom) ne 'no_host');
                   2651:         if (ref($requesthash{$key}) eq 'HASH') {
                   2652:             my $ownername = $requesthash{$key}{'ownername'};
                   2653:             my $ownerdom = $requesthash{$key}{'ownerdom'};
                   2654:             next if (&Apache::lonnet::homeserver($ownername,$ownerdom) eq 'no_host');
                   2655:             my $inststatus;
                   2656:             my %userenv =
                   2657:                 &Apache::lonnet::get('environment',['inststatus'],
                   2658:                                      $ownerdom,$ownername);
                   2659:             my ($tmp) = keys(%userenv);
                   2660:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   2661:                 $inststatus = $userenv{'inststatus'};
                   2662:             } else {
                   2663:                 undef(%userenv);
                   2664:             }
                   2665:             my $reqkey = $dom.'_'.$cnum;
                   2666:             my %history = &Apache::lonnet::restore($reqkey,'courserequests',
                   2667:                                                    $ownerdom,$ownername);
                   2668:             if (ref($history{'details'}) eq 'HASH') {
                   2669:                 my $instcode = $history{'details'}{'instcode'};
                   2670:                 my $crstype = $history{'details'}{'crstype'};
                   2671:                 my $reqtime = $history{'details'}{'reqtime'};
                   2672:                 my $cdescr = $history{'details'}{'cdescr'};
                   2673:                 my @currsec;
                   2674:                 my $sections = $history{'details'}{'sections'};
                   2675:                 if (ref($sections) eq 'HASH') {
                   2676:                     foreach my $i (sort(keys(%{$sections}))) {
                   2677:                         if (ref($sections->{$i}) eq 'HASH') {
                   2678:                             my $sec = $sections->{$i}{'inst'};
                   2679:                             if (!grep(/^\Q$sec\E$/,@currsec)) {
                   2680:                                 push(@currsec,$sec);
                   2681:                             }
                   2682:                         }
                   2683:                     }
                   2684:                 }
                   2685:                 my $instseclist = join(',',@currsec);
                   2686:                 my ($validationchk,$disposition,$reqstatus,$message,
                   2687:                     $validation,$validationerror);
                   2688:                 $validationchk =
                   2689:                     &Apache::lonnet::auto_courserequest_validation($dom,
                   2690:                         $ownername.':'.$ownerdom,$crstype,$inststatus,
                   2691:                         $instcode,$instseclist);
                   2692:                 if ($validationchk =~ /:/) {
                   2693:                     ($validation,$message) = split(':',$validationchk);
                   2694:                 } else {
                   2695:                     $validation = $validationchk;
                   2696:                 }
                   2697:                 if ($validation =~ /^error(.*)$/) {
                   2698:                     $disposition = 'approval';
                   2699:                     $validationerror = $1;
                   2700:                 } else {
                   2701:                     $disposition = $validation;
                   2702:                 }
                   2703:                 $reqstatus = $disposition;
                   2704:                 if ($disposition eq 'process') {
1.39      raeburn  2705:                     my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,$code);
1.59      raeburn  2706:                     my $clonemsg = [];
1.41      raeburn  2707:                     my %customitems;
                   2708:                     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
                   2709:                     my $inprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'process',$ownername,
                   2710:                                                                         $ownerdom,$fullname,$cdescr);
                   2711:                     if (ref($inprocess) eq 'HASH') {
                   2712:                         foreach my $key (keys(%{$inprocess})) {
                   2713:                             if (exists($history{'details'}{$key})) {
                   2714:                                 $customitems{$key} = $history{'details'}{$key};
                   2715:                             }
                   2716:                         }
                   2717:                     }
1.50      raeburn  2718:                     if ($history{'details'}{'clonecrs'}) {
                   2719:                         $customitems{'_LC_clonefrom'} = $history{'details'}{'clonedom'}.'_'.$history{'details'}{'clonecrs'};
                   2720:                     }
1.41      raeburn  2721:                     my ($result,$postprocess) = 
1.59      raeburn  2722:                         &course_creation($dom,$cnum,'domain',$history{'details'},\$logmsg,$clonemsg,\$newusermsg,
                   2723:                                          \$addresult,\$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles,
                   2724:                                          \$code,\%customitems,$context,$user_lh);
1.23      raeburn  2725:                     if ($result eq 'created') {
                   2726:                         $disposition = 'created';
                   2727:                         $reqstatus = 'created';
1.26      raeburn  2728:                         my $cid = $dom.'_'.$cnum;
                   2729:                         push(@{$newcids{$instcode}},$cid);
                   2730:                         if ($dcname && $dcdom) {
1.54      raeburn  2731:                             my $firsturl = &Apache::lonnet::course_portal_url($cnum,$dom);
1.26      raeburn  2732:                             my $beneficiary = 'pendingrequestor';
                   2733:                             my $now = time;
                   2734:                             my $owner = $ownername.':'.$ownerdom;
                   2735:                             my $approvedmsg =
                   2736:                                 [{
1.27      raeburn  2737:                                     mt => 'Your requested course: [_1], (queued pending validation) has now been created.',
                   2738:                                     args => [$cdescr],
1.59      raeburn  2739:                                  }];
                   2740:                             if ((ref($clonemsg) eq 'ARRAY') && (@{$clonemsg})) {
                   2741:                                 push(@{$approvedmsg},@{$clonemsg});
                   2742:                             }
                   2743:                             push(@{$approvedmsg},
1.26      raeburn  2744:                                  {
1.28      raeburn  2745:                                     mt   => 'Visit [_1] to log-in and access the course.',
1.26      raeburn  2746:                                     args => [$firsturl],
1.27      raeburn  2747:                                  },
                   2748:                                  {
1.59      raeburn  2749:                                     mt => 'If currently logged-in to LON-CAPA, log-out and log-in again to select your new course role.',
                   2750:                                     args => [],
                   2751:                                  }
                   2752:                             );
1.26      raeburn  2753:                             my $sender = $dcname.':'.$dcdom;
1.41      raeburn  2754:                             if (ref($postprocess) eq 'HASH') {
                   2755:                                 if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
                   2756:                                     foreach my $item (@{$postprocess->{'createdmsg'}}) {
                   2757:                                         if (ref($item) eq 'HASH') {
                   2758:                                              if ($item->{'mt'} ne '') {
                   2759:                                                  push(@{$approvedmsg},$item);
                   2760:                                              }
                   2761:                                         }
                   2762:                                     }
                   2763:                                 }
1.48      raeburn  2764:                                 if (ref($postprocess->{'createdactions'}) eq 'HASH') {
                   2765:                                     if (ref($postprocess->{'createdactions'}{'environment'}) eq 'HASH') {
                   2766:                                         &postprocess_crsenv($dom,$cnum,$postprocess->{'createdactions'}{'environment'});
                   2767:                                     }
                   2768:                                 }
1.41      raeburn  2769:                             }
1.26      raeburn  2770:                             &send_selfserve_notification($owner,$approvedmsg,
                   2771:                                                          $cid,$cdescr,$now,
                   2772:                                                          $beneficiary,$sender,
                   2773:                                                          undef,undef,$crstype);
                   2774:                         }
1.23      raeburn  2775:                     }
                   2776:                 } elsif ($disposition eq 'rejected') {
                   2777:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] rejected when validating.',$instcode,$ownername.':'.$ownerdom,$inststatus).$linefeed;
                   2778:                 } elsif ($disposition eq 'approval') {
                   2779:                     $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;
                   2780: 
                   2781:                     my $requestid = $cnum.'_'.$disposition;
                   2782:                     my $request = {
                   2783:                             $requestid => {
                   2784:                                             timestamp   => $reqtime,
                   2785:                                             crstype     => $crstype,
                   2786:                                             ownername   => $ownername,
                   2787:                                             ownerdom    => $ownerdom,
                   2788:                                             description => $cdescr,
                   2789:                                           },
                   2790:                           };
                   2791:                     my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,$dom);
                   2792:                     unless ($putresult eq 'ok') {
                   2793:                         $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;
                   2794:                     }
                   2795:                 } elsif ($disposition eq 'pending') {
                   2796:                     my $instcode = $requesthash{$key}{'instcode'};
                   2797:                     my $description = $requesthash{$key}{'description'};
                   2798:                     my $timestamp = $requesthash{$key}{'timestamp'};
                   2799:                     my $entry = $cnum.':'.$ownername.':'.$ownerdom.':'.
                   2800:                                 $instcode.':'.$description;
                   2801:                     if (ref($stillpending{$timestamp}) eq 'ARRAY') {
                   2802:                         push(@{$stillpending{$timestamp}},$entry);
                   2803:                     } else {
                   2804:                         $stillpending{$timestamp} = [$entry];
                   2805:                     }
                   2806:                 }
                   2807:                 unless ($disposition eq 'pending') {
                   2808:                     my ($statusresult,$output) =
                   2809:                         &update_coursereq_status(\%requesthash,$dom,$cnum,
1.26      raeburn  2810:                                                  $reqstatus,'domain',$ownerdom,
                   2811:                                                  $ownername);
1.23      raeburn  2812:                     unless (&Apache::lonnet::del_dom($reqsnamespace,[$cnum.'_pending'],$dom) eq 'ok') {
                   2813:                         $output .= &mt('An error occurred when removing the request for [_1] submitted by [_2] from the pending queue.',$instcode,$ownername.':'.$ownerdom).$linefeed;
                   2814:                     }
                   2815:                 }
                   2816:             }
                   2817:         }
                   2818:     }
                   2819:     foreach my $key (sort(keys(%newcids))) {
                   2820:         if (ref($newcids{$key}) eq 'ARRAY') {
                   2821:             $output .= "created course from queued request: $key - ".join(', ',@{$newcids{$key}}).$linefeed;
                   2822:             my $newcourse = &LONCAPA::escape($key.':'.$newcids{$key});
                   2823:         }
                   2824:     }
                   2825:     unless ($context eq 'auto') {
                   2826:         if (keys(%stillpending) > 0) {
                   2827:             $output .= '<form method="post" name="changequeue" action="/adm/createcourse" />'."\n".
                   2828:                        '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                   2829:                        '<input type="hidden" name="phase" value="requestchange" />'.
                   2830:                        '<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>'.
                   2831:                        &build_queue_display($dom,'stillpending',\%stillpending).
                   2832:                        '<br /><input type="hidden" name="queue" value="pending" />'."\n".
                   2833:                        '<input type="submit" name="processqueue" value="'.&mt('Save').'" />'.
                   2834:                        '</form>';
                   2835:         }
                   2836:     }
                   2837:     return $output;
                   2838: }
                   2839: 
1.48      raeburn  2840: sub postprocess_crsenv {
                   2841:     my ($dom,$cnum,$postprocessenv) = @_;
                   2842:     if (ref($postprocessenv) eq 'HASH') {    
                   2843:         my $cid = $dom.'_'.$cnum;
                   2844:         my %settablecrsenv = (
                   2845:                               'internal.selfenroll_types'        => 1,
                   2846:                               'internal.selfenroll_registered'   => 1,
                   2847:                               'internal.selfenroll_section'      => 1,
                   2848:                               'internal.selfenroll_start_access' => 1,
                   2849:                               'internal.selfenroll_end_access'   => 1,
                   2850:                               'internal.selfenroll_limit'        => 1,
                   2851:                               'internal.selfenroll_cap'          => 1,
                   2852:                               'internal.selfenroll_approval'     => 1,
                   2853:                               'internal.selfenroll_notifylist'   => 1,
                   2854:                              );
                   2855:         my %needcrsidput = (
                   2856:                               'internal.selfenroll_types'      => 1,
                   2857:                               'internal.selfenroll_start_date' => 1,
                   2858:                               'internal. selfenroll_end_date'  => 1,
                   2859:                            );
                   2860:         my (@needupdate,%newcrsenv);
                   2861:         foreach my $key (keys(%{$postprocessenv})) { 
                   2862:             if ($settablecrsenv{$key}) {
                   2863:                 $newcrsenv{$key} = $postprocessenv->{$key};
                   2864:                 if ($needcrsidput{$key}) {
                   2865:                     push(@needupdate,$key); 
                   2866:                 }
                   2867:             }
                   2868:             if (keys(%newcrsenv)) {
                   2869:                 my $putresult = &Apache::lonnet::put('environment',\%newcrsenv,$dom,$cnum);
                   2870:                 if ($putresult eq 'ok') {
                   2871:                     if (@needupdate) {
                   2872:                         my %crsinfo =
                   2873:                             &Apache::lonnet::courseiddump($dom,'.',1,'.','.',$cnum,undef,undef,'.');
                   2874:                         if (ref($crsinfo{$cid}) eq 'HASH') {
                   2875:                             foreach my $key (@needupdate) {
                   2876:                                 $crsinfo{$cid}{$key} = $newcrsenv{$key};
                   2877:                             }
                   2878:                             my $chome = &Apache::lonnet::homeserver($cnum,$dom);
                   2879:                             &Apache::lonnet::courseidput($dom,\%crsinfo,$chome,'notime');
                   2880:                         }
                   2881:                     }
                   2882:                 }
                   2883:             }
                   2884:         }
                   2885:     }
                   2886:     return;
                   2887: }
                   2888: 
                   2889: 
1.45      raeburn  2890: sub requestcourses_validation_types {
                   2891:     my @items = ('url','fields','button','markup');
                   2892:     my %names =  &Apache::lonlocal::texthash (
                   2893:             url      => 'Web address of validation server/script',
                   2894:             fields   => 'Form fields to send to validator',
                   2895:             button   => 'Text for validation button',
                   2896:             markup   => 'Validation description (HTML)',
                   2897:     );
                   2898:     my @fields = ('owner','course','coursetype','description');
                   2899:     return (\@items,\%names,\@fields);
                   2900: }
                   2901: 
1.31      raeburn  2902: sub is_active_author {
                   2903:     if ($env{'user.role.au./'.$env{'user.domain'}.'/'} =~ /^(\d*)\.(\d*)$/) {
                   2904:         if ((!$1 || $1 < time) &&
                   2905:             (!$2 || $2 > time)) {
                   2906:             return 1;
                   2907:         }
                   2908:     }
                   2909: }
                   2910: 
                   2911: sub author_prompt {
                   2912:     my ($is_active_author,$offer_author);
                   2913:     if ($env{'environment.canrequest.author'}) {
                   2914:         unless (&is_active_author()) {
1.32      raeburn  2915:             unless (&reqauthor_check() =~ /^approval:\d+$/) {
                   2916:                 $offer_author = 1;
1.31      raeburn  2917:             }
                   2918:         }
                   2919:     }
                   2920:     return $offer_author;
                   2921: }
                   2922: 
                   2923: sub reqauthor_check {
                   2924:     my $queued = $env{'environment.requestauthorqueued'};
                   2925:     my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
                   2926:                                          $env{'user.domain'},$env{'user.name'});
                   2927:     my $reqstatus = $reqauthor{'author_status'};
                   2928:     if (($reqstatus eq '' && $queued ne '') ||
                   2929:         ($env{'environment.requestauthorqueued'} !~ /^\Q$reqstatus\E/)) {
                   2930:         if (ref($reqauthor{'author'}) eq 'HASH') {
                   2931:             $queued = $reqstatus.':'.$reqauthor{'author'}{'timestamp'};
                   2932:         } else {
                   2933:             undef($queued);
                   2934:         }
                   2935:         &Apache::lonnet::appenv({'environment.requestauthorqueued' => $queued});
                   2936:     }
                   2937:     return $queued;
                   2938: }
                   2939: 
                   2940: sub process_reqauthor {
                   2941:     my ($dispositionref,$updateref) = @_;
                   2942:     if (&is_active_author()) {
                   2943:         return '<span class="LC_warning">'.
1.42      bisitz   2944:                 &mt('An Authoring Space has already been assigned to you.').'<br />'.
1.31      raeburn  2945:                 &mt('Please select the Author role from your [_1]roles page[_2].','<a href="/adm/roles">',
                   2946:                 '</a>').'</span>';
                   2947:     }
                   2948:     unless ($env{'environment.canrequest.author'}) {
                   2949:         return '<span class="LC_warning">'.
1.42      bisitz   2950:                 &mt('You do not currently have rights to request an Authoring Space.').'<br />'.
1.37      raeburn  2951:                 &mt('Please contact the [_1]helpdesk[_2] for assistance.','<a href="/adm/helpdesk">',
1.31      raeburn  2952:                 '</a>').'</span>';
                   2953:     }
                   2954:     my $queued = &reqauthor_check();
                   2955:     if ($queued =~ /^approval:(\d+)$/) {
                   2956:         my $timestamp = $1;
                   2957:         return '<span class="LC_info">'.
1.42      bisitz   2958:                &mt('A request for Authoring Space submitted on [_1] is awaiting approval',
1.31      raeburn  2959:                &Apache::lonlocal::locallocaltime($timestamp)).
                   2960:                '</span>';
                   2961:     } elsif ($queued =~ /^approved:(\d+)$/) {
                   2962:         my $timestamp = $1;   
                   2963:         my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
                   2964:                                                       ['active'],['au'],[$env{'user.domain'}]);
                   2965:         if (keys(%roleshash) > 0) {
                   2966:             return '<span class="LC_info">'.
1.42      bisitz   2967:                    &mt('A request for Authoring Space submitted on [_1] has been approved.',
1.31      raeburn  2968:                    &Apache::lonlocal::locallocaltime($timestamp)).
                   2969:                    '</span>';
                   2970:         }
                   2971:     }
                   2972:     my ($output,@inststatuses,%domconfig);
                   2973:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestauthor'],
                   2974:                                           $env{'user.domain'});
                   2975:     my $val = &get_processtype('requestauthor',$env{'user.name'},$env{'user.domain'},
                   2976:                                $env{'user.adv'},$env{'user.domain'},undef,
                   2977:                                \@inststatuses,\%domconfig);
                   2978:     my $now = time;
                   2979:     if ($val eq 'automatic') {
                   2980:         my $start = $now-1;
                   2981:         if (&Apache::lonnet::assignrole($env{'user.domain'},$env{'user.name'},'/'.$env{'user.domain'}.'/',
                   2982:                                         'au',undef,$start,undef,undef,'requestauthor') eq 'ok') {
                   2983:             $output = '<span class="LC_info">'.
1.42      bisitz   2984:                       &mt('Access to Authoring Space has been activated').'</span><br />';
1.31      raeburn  2985:                       &Apache::lonroles::update_session_roles();
                   2986:             &Apache::lonnet::appenv({'user.update.time'  => $now});
                   2987:             if (ref($updateref)) {
                   2988:                 $$updateref = $now;
                   2989:             }
                   2990:             if (ref($dispositionref)) {
                   2991:                 $$dispositionref = 'created';
                   2992:             }
                   2993:         } else {
                   2994:             $output = '<span class="LC_info">'.
1.42      bisitz   2995:                       &mt('An error occurred while activating your access to Authoring Space');
1.31      raeburn  2996:         }
                   2997:     } elsif ($val eq 'approval') {
                   2998:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($env{'user.domain'});
                   2999:         if (&Apache::lonnet::put('requestauthorqueue',{ $env{'user.name'}.'_'.$val => $now },
                   3000:                                  $env{'user.domain'},$domconfiguser) eq 'ok') {
                   3001:             my %userrequest = (
                   3002:                 author => {
                   3003:                             timestamp   => $now,
                   3004:                             status      => $val,
                   3005:                           },
                   3006:                 author_status => $val,
                   3007:             );
                   3008:             my $req_notifylist;
                   3009:             if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   3010:                 if (ref($domconfig{'requestauthor'}{'notify'}) eq 'HASH') {
                   3011:                     my $req_notifylist = $domconfig{'requestauthor'}{'notify'}{'approval'};
                   3012:                     if ($req_notifylist) {
                   3013:                         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
                   3014:                                                                      $env{'user.domain'});
                   3015:                         my $sender = $env{'user.name'}.':'.$env{'user.domain'};
                   3016:                         my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                   3017:                         &send_selfserve_notification($req_notifylist,
                   3018:                                                      "$fullname ($env{'user.name'}:$env{'user.domain'})",
                   3019:                                                      undef,$domdesc,$now,'authorreq',$sender);
                   3020:                     }
                   3021:                 }
                   3022:             }
                   3023:             my $userresult =
                   3024:                 &Apache::lonnet::put('requestauthor',\%userrequest,$env{'user.domain'},$env{'user.name'});
                   3025:             $output = '<span class="LC_info">'.
1.42      bisitz   3026:                       &mt('Your request for Authoring Space has been submitted for approval.').
1.31      raeburn  3027:                       '</span>';
                   3028:             &Apache::lonnet::appenv({'environment.requestauthorqueued' => $val.':'.$now});
                   3029:         } else {
                   3030:             $output = '<span class="LC_info">'.
1.42      bisitz   3031:                       &mt('An error occurred saving your request for Authoring Space.').
1.31      raeburn  3032:                       '</span>';
                   3033:         }
                   3034:     }
                   3035:     return $output;
                   3036: }
                   3037: 
1.1       raeburn  3038: 1;

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