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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Utilities to administer domain course requests and course self-enroll requests 
                      3: #
1.8     ! raeburn     4: # $Id: loncoursequeueadmin.pm,v 1.7 2009/08/27 00:06:18 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: 
                     30: =head1 NAME
                     31: 
                     32: Apache::loncoursequeueadmin.pm
                     33: 
                     34: =head1 SYNOPSIS
                     35: 
                     36: Adminitsration utilities used by domain coordinators for queued course creation requests, and by course coordinators for queued self-enrollment requests.
                     37: 
                     38: This is part of the LearningOnline Network with CAPA project
                     39: described at http://www.lon-capa.org.
                     40: 
                     41: =head1 SUBROUTINES
                     42: 
                     43: =over
                     44: 
                     45: =item send_selfserve_notification()
                     46: 
                     47: =item display_queued_requests()
                     48: 
                     49: =item update_request_queue()
                     50: 
                     51: =item get_student_counts()
                     52: 
                     53: =back
                     54: 
                     55: =cut
                     56: 
                     57: package Apache::loncoursequeueadmin;
                     58: 
                     59: use strict;
                     60: use Apache::Constants qw(:common :http);
                     61: use Apache::lonnet;
                     62: use Apache::loncommon;
                     63: use Apache::lonmsg;
                     64: use Apache::lonlocal;
1.2       raeburn    65: use Apache::lonuserutils;
1.1       raeburn    66: use LONCAPA;
                     67: 
                     68: sub send_selfserve_notification {
1.2       raeburn    69:     my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
1.1       raeburn    70:         $approvedlist,$rejectedlist) = @_;
                     71: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
                     72: #       so this can be localized to the recipients date display format/time zone
                     73:     $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
                     74:     my $msgcc;
1.2       raeburn    75:     my ($rawsubj,@rawmsg,$subject,$message,$reviewer);
1.1       raeburn    76:     if ($context eq 'coursemanagers') {
                     77:         $rawsubj = 'Self-enrollment requests processed';
                     78:         push(@rawmsg,{
1.2       raeburn    79:                       mt => 'Enrollment requests in the following course: [_1]have been processed.',
1.3       raeburn    80:                       args => ["\n  $contextdesc"],
1.1       raeburn    81:                      });
                     82:     } elsif ($context eq 'domainmanagers') {
1.2       raeburn    83:         $rawsubj = 'Course requests reviewed';
1.1       raeburn    84:         push(@rawmsg,{
1.2       raeburn    85:                       mt  => 'Course creation requests in the following domain: [_1]have been reviewed.',
1.3       raeburn    86:                       args => ["\n  $contextdesc"],
1.1       raeburn    87:                      });
                     88:         if (ref($textstr) eq 'ARRAY') {
                     89:             push(@rawmsg,@{$textstr});
                     90:         }
                     91:     } elsif ($context eq 'enroller') {
                     92:         $rawsubj = 'Enrollment request';
                     93:         push(@rawmsg,{
1.2       raeburn    94:                       mt  => 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.',
                     95:                       args => ["\n  ".$contextdesc.",\n",$timestamp.",\n"],
1.1       raeburn    96: 
                     97:                      });
                     98:         if (ref($textstr) eq 'ARRAY') {
                     99:             push(@rawmsg,@{$textstr});
                    100:         }
                    101:     } elsif ($context eq 'courserequestor') {
                    102:         $rawsubj = 'Course request';
                    103:         push(@rawmsg,{
1.2       raeburn   104:                       mt  => 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.',
                    105:                       args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
1.1       raeburn   106: 
                    107:                      });
                    108:         if (ref($textstr) eq 'ARRAY') {
                    109:             push(@rawmsg,@{$textstr});
                    110:         }
                    111:     } elsif ($context eq 'coursereq') {
1.2       raeburn   112:         $rawsubj = 'Course request to review', 
1.1       raeburn   113:         push(@rawmsg,{
1.2       raeburn   114:                       mt  => 'Creation of the following course: [_1]was requested by [_2] on [_3].',
                    115:                       args => ["\n  $contextdesc\n",$textstr,$timestamp],
1.1       raeburn   116:                      },
                    117:                      {
1.2       raeburn   118:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create a new course -> Approve or reject course requests[_3]to display a list of pending requests, which you can either approve or reject.',
                    119:                       args => ["\n","\n\n  ","\n\n"],
1.1       raeburn   120:                      });
                    121:     } elsif ($context eq 'selfenrollreq') {
                    122:         $rawsubj = 'Self-enrollment request';
                    123:         push(@rawmsg,{
                    124:                       mt  => 'Enrollment in the following course: [_1] was requested by [_2] on [_3].',
1.2       raeburn   125:                       args => ["\n  $contextdesc\n",$textstr,$timestamp."\n"],
                    126:                      });
                    127:         if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Community') {
                    128:             push(@rawmsg,
                    129:                      {
                    130:                       mt =>'As 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.',
                    131:                       args => ["  \n\n","\n"],
                    132:                      });
                    133:         } else {
                    134:             push(@rawmsg,
1.1       raeburn   135:                      {
1.2       raeburn   136:                       mt =>'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.',
                    137:                       args => ["  \n\n","\n"],
1.1       raeburn   138:                      });
1.2       raeburn   139: 
                    140:         }
1.1       raeburn   141:     }
                    142:     my @to_notify = split(/,/,$notifylist);
                    143:     my $numsent = 0;
                    144:     my @recusers;
                    145:     my @recudoms;
                    146:     foreach my $cc (@to_notify) {
                    147:         my ($ccname,$ccdom) = split(/:/,$cc);
                    148:         if (!exists($msgcc->{$ccname.':'.$ccdom})) {
                    149:             push(@recusers,$ccname);
                    150:             push(@recudoms,$ccdom);
                    151:             $msgcc->{$ccname.':'.$ccdom}='';
                    152:             $numsent ++;
                    153:         }
                    154:     }
                    155:     my %reciphash = (
                    156:                      cc => $msgcc,
                    157:     );
                    158:     my ($uname,$udom);
                    159:     if ($sender =~ /:/) {
                    160:         ($uname,$udom) = split(/:/,$sender);
1.2       raeburn   161:     } elsif ($context eq 'course') {
1.1       raeburn   162:         $uname = $sender;
                    163:         my %courseinfo = &Apache::lonnet::coursedescription($cid);
                    164:         $udom = $courseinfo{'num'};
                    165:     }
                    166:     my %sentmessage;
                    167:     my $stamp = time;
                    168:     my $msgcount = &Apache::lonmsg::get_uniq();
                    169:     my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
                    170:     $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
                    171:     $message = '';
                    172:     foreach my $item (@rawmsg) {
                    173:         if (ref($item) eq 'HASH') {
                    174:             $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
                    175:         }
                    176:     }
                    177:     &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms);
                    178:     my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
                    179:     my $status;
                    180:     foreach my $recip (sort(keys(%{$msgcc}))) {
                    181:         my ($ccname,$ccdom) = split(/:/,$recip);
                    182:         my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
                    183:         my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
                    184:         my $message = '';
                    185:         foreach my $item (@rawmsg) {
                    186:             if (ref($item) eq 'HASH') {
                    187:                 $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
                    188:                                                        @{$item->{args}})."\n";
                    189:             }
                    190:         }
                    191:         if ($context eq 'managers') {
                    192:             if ($approvedlist) {
                    193:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
                    194:             }
                    195:             if ($rejectedlist) {
                    196:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
                    197:             }
                    198:         } elsif ($context eq 'domainmanagers') {
                    199:             if ($approvedlist) {
                    200:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
                    201:             }
                    202:             if ($rejectedlist) {
                    203:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
                    204:             }
                    205:         }
                    206:         $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
                    207:     }
                    208:     $status =~ s/,$//;
                    209:     return ($recipstatus,$status);
                    210: }
                    211: 
                    212: sub display_queued_requests {
                    213:     my ($context,$dom,$cnum) = @_;
1.2       raeburn   214:     my ($namespace,$formaction,$nextelement,%requesthash);
1.1       raeburn   215:     if ($context eq 'course') {
                    216:         $formaction = '/adm/createuser';
                    217:         $namespace = 'selfenrollrequests';
                    218:         %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
1.2       raeburn   219:         $nextelement = '<input type="hidden" name="state" value="done" />';
1.1       raeburn   220:     } else {
                    221:         $formaction = '/adm/createcourse';
                    222:         $namespace = 'courserequestqueue';
1.6       raeburn   223:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_approval');
1.2       raeburn   224:         $nextelement = '<input type="hidden" name="phase" value="requestchange" />';
1.1       raeburn   225:     }
                    226:     my ($output,%queue_by_date,%crstypes);
                    227:     if (keys(%requesthash) > 0) {
1.2       raeburn   228:         $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
                    229:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                    230:                   $nextelement."\n".
1.1       raeburn   231:                   &Apache::loncommon::start_data_table().
                    232:                   &Apache::loncommon::start_data_table_header_row().
                    233:                   '<th>'.&mt('Action').'</th>'.
                    234:                   '<th>'.&mt('Requestor').'</th>';
                    235:         if ($context eq 'course') {
                    236:             $output .= '<th>'.&mt('Section').'</th>'.
                    237:                        '<th>'.&mt('Date requested').'</th>';
                    238:             %crstypes = &Apache::lonlocal::texthash (
                    239:                             official   => 'Official course',
                    240:                             unofficial => 'Unofficial course',
                    241:                             community  => 'Community',
                    242:                         );
                    243:         } else {
                    244:             $output .= '<th>'.&mt('Type').'</th>'.
                    245:                        '<th>'.&mt('Date requested').'</th>'.
                    246:                        '<th>'.&mt('Details').'</th>';
                    247:         }
                    248:         $output .= &Apache::loncommon::end_data_table_header_row();
                    249:         foreach my $item (keys(%requesthash)) {
                    250:             my ($timestamp,$entry);
                    251:             if ($context eq 'course') {
                    252:                 ($timestamp, my $usec) = split(/:/,$requesthash{$item});
                    253:                 $entry = $item.':'.$usec;
                    254:             } else {
                    255:                 $timestamp = $requesthash{$item}{'timestamp'};
                    256:                 if (ref($requesthash{$item}) eq 'HASH') {
1.2       raeburn   257:                     my ($cnum,$disposition) = split('_',$item);
                    258:                     $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
1.1       raeburn   259:                              $requesthash{$item}{'ownerdom'}.':'.
                    260:                              $requesthash{$item}{'crstype'}.':'.
                    261:                              $requesthash{$item}{'description'};
                    262:                 }
                    263:             }
                    264:             if ($entry ne '') {
                    265:                 if (exists($queue_by_date{$timestamp})) {
                    266:                     if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                    267:                         push(@{$queue_by_date{$timestamp}},$entry);
                    268:                     }
                    269:                 } else {
                    270:                     @{$queue_by_date{$timestamp}} = ($entry);
                    271:                 }
                    272:             }
                    273:         }
                    274:         my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
                    275:         my $count = 0;
                    276:         foreach my $item (@sortedtimes) {
                    277:             if (ref($queue_by_date{$item}) eq 'ARRAY') {
                    278:                 foreach my $request (sort(@{$queue_by_date{$item}})) {
                    279:                     my ($row,$approve,$reject,$showtime,$showsec,$namelink,
                    280:                         $detailslink,$crstype);
                    281:                     $showtime = &Apache::lonlocal::locallocaltime($item);
                    282:                     if ($context eq 'course') {
                    283:                         my ($puname,$pudom,$pusec) = split(/:/,$request);
                    284:                         $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
                    285:                         $reject = $puname.':'.$pudom;
                    286:                         $showsec = $pusec;
                    287:                         if ($showsec eq '') {
                    288:                             $showsec = &mt('none');
                    289:                         }
                    290:                         $namelink = &Apache::loncommon::aboutmewrapper(
                    291:                                     &Apache::loncommon::plainname($puname,$pudom),
                    292:                                     $puname,$pudom);
                    293: 
                    294:                     } else {
1.2       raeburn   295:                         my ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
1.7       raeburn   296:                         $detailslink='<a href="javascript:opencoursereqdisplay('.
                    297:                                       "'$dom','$cnum'".');">'.$cdesc.'</a>';
1.1       raeburn   298:                         $crstype = $type;
                    299:                         if (defined($crstypes{$type})) {
                    300:                             $crstype = $crstypes{$type};
                    301:                         }
                    302:                         $approve = $count.':'.$cnum;
                    303:                         $reject = $cnum;
                    304:                         $namelink = &Apache::loncommon::aboutmewrapper(
                    305:                                     &Apache::loncommon::plainname($ownername,$ownerdom),
                    306:                                     $ownername,$ownerdom);
                    307:                     }
                    308:                     $row = '<td><span class="LC_nobreak"><label>'.
                    309:                            '<input type="checkbox" value="'.$approve.'" name="approvereq" />'.&mt('Approve').'</label></span><br />'.
                    310:                            '<span class="LC_nobreak"><label>'.
                    311:                            '<input type="checkbox" value="'.$reject.'" name="rejectreq" />'.&mt('Reject').'</label></span><br /></td>'.
                    312:                            '<td>'.$namelink.'</td>'."\n";
                    313:                     if ($context eq 'course') {
                    314:                         $row .= '<td>'.$showsec.'</td>'."\n".
                    315:                                 '<td>'.$showtime.'</td>'."\n";
                    316:                     } else { 
                    317:                         $row .= '<td>'.$crstype.'</td>'."\n".
                    318:                                 '<td>'.$showtime.'</td>'."\n".
                    319:                                 '<td>'.$detailslink.'</td>'."\n";
                    320:                     }
                    321:                     $output .= &Apache::loncommon::start_data_table_row()."\n".
                    322:                                $row.
                    323:                                &Apache::loncommon::end_data_table_row()."\n";
                    324:                     $count ++;
                    325:                 }
                    326:             }
                    327:         }
                    328:         $output .= &Apache::loncommon::end_data_table().
                    329:                    '<input type="submit" name="processqueue" value="'.&mt('Save').
                    330:                    '" /></form>';
                    331:     } else {
                    332:         if ($context eq 'course') {
                    333:             $output .= &mt('There are currently no enrollment requests.');
                    334:         } else {
                    335:             $output .= &mt('There are currently no course requests awaiting approval.');
                    336:         }
                    337:     }
                    338:     return $output;
                    339: }
                    340: 
                    341: sub update_request_queue {
                    342:     my ($context,$cdom,$cnum,$coursedesc) = @_;
                    343:     my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
1.2       raeburn   344:         $stucounts,$idx,$classlist,%requesthash,$cid,$hostname,$protocol,
                    345:         $domdesc,$now,$sender,$approvedmsg,$rejectedmsg,$beneficiary,
                    346:         @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
                    347:         @processing_errors,@warn_approves,@warn_rejects,@approvals,
1.5       raeburn   348:         @rejections,@rejectionerrors,@nopermissions,%courseroles,
                    349:         %communityroles,%domdefs);
1.1       raeburn   350:     @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
                    351:     @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
                    352:     $now = time;
                    353:     $sender = $env{'user.name'}.':'.$env{'user.domain'};
                    354:     if ($context eq 'course') {
                    355:         $namespace = 'selfenrollrequests';
                    356:         $beneficiary = 'enroller';
                    357:         $cid = $env{'request.course.id'};
                    358:         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                    359:         $hostname = &Apache::lonnet::hostname($chome);
                    360:         $protocol = $Apache::lonnet::protocol{$chome};
                    361:         $protocol = 'http' if ($protocol ne 'https');
                    362:         %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
                    363:         $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
                    364:         $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
                    365:         $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                    366:         $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
                    367:         $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                    368:         ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
                    369:         $approvedmsg = [{
                    370:                             mt => 'Your request for enrollment has been approved.',
                    371:                         },
                    372:                         {
                    373:                             mt   => 'Visit [_1], to log-in and access the course',
                    374:                             args => [$protocol.'://'.$hostname],
                    375:                         }];
                    376:         $rejectedmsg =  [{
                    377:                             mt => 'Your request for enrollment has not been approved.',
                    378:                         }];
                    379:     } else {
1.2       raeburn   380:         $domdesc = &Apache::lonnet::domain($cdom);
                    381:         $namespace = 'courserequestqueue';
                    382:         $beneficiary = 'courserequestor';
1.6       raeburn   383:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_approval');
1.2       raeburn   384:         my $chome = &Apache::lonnet::domain($cdom,'primary');
                    385:         $hostname = &Apache::lonnet::hostname($chome);
                    386:         $protocol = $Apache::lonnet::protocol{$chome};
                    387:         $protocol = 'http' if ($protocol ne 'https');
                    388:         my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
                    389:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                    390:             if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') { 
                    391:                 $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
                    392:             }
                    393:         }
                    394:         $approvedmsg = [{
                    395:                             mt => 'Your course request has been approved.',
                    396:                         },
                    397:                         {
                    398:                             mt   => 'Visit [_1], to log-in and access the course',
                    399:                             args => [$protocol.'://'.$hostname],
                    400:                         }];
                    401:         $rejectedmsg =  [{
                    402:                             mt => 'Your course request has not been approved.',
                    403:                         }];
                    404:         %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
                    405:         my @roles = &Apache::lonuserutils::roles_by_context('course');
                    406:         foreach my $role (@roles) {
                    407:             $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
                    408:         }
                    409:         foreach my $role (@roles) {
                    410:             $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
                    411:         }
                    412: 
1.1       raeburn   413:     }
                    414:     foreach my $item (sort {$a <=> $b} @approvals) {
                    415:         if ($context eq 'course') {
                    416:             my ($num,$uname,$udom,$usec) = split(/:/,$item);
                    417:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    418:             if ($uhome ne 'no_host') {
                    419:                 if (exists($requesthash{$uname.':'.$udom})) {
                    420: 
                    421:                     if (exists($classlist->{$uname.':'.$udom})) {
                    422:                         if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
                    423:                             if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
                    424:                                 ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
                    425:                                 push(@existing,$uname.':'.$udom);
                    426:                                 next;
                    427:                             }
                    428:                         }
                    429:                     }
                    430:                 } else {
                    431:                     push(@missingreq,$uname.':'.$udom);
                    432:                     next;
                    433:                 }
                    434:                 if (!grep(/^\Q$item\E$/,@rejections)) {
                    435:                     if ($limit eq 'allstudents') {
                    436:                         if ($stucounts->{$limit} >= $cap) {
                    437:                             push(@limitexceeded,$uname.':'.$udom);
                    438:                             last;
                    439:                         }
                    440:                     } elsif ($limit eq 'selfenrolled') {
                    441:                         if ($stucounts->{$limit} >= $cap) {
                    442:                             push(@limitexceeded,$uname.':'.$udom);
                    443:                             last;
                    444:                         }
                    445:                     }
                    446:                     my $result =
                    447:                         &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
                    448:                     if ($result eq 'ok') {
1.2       raeburn   449:                         push(@completed,$uname.':'.$udom);
1.1       raeburn   450:                         $stucounts->{'allstudents'} ++;
                    451:                         $stucounts->{'selfenrolled'} ++;
                    452:                         &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
                    453:                                        $cid,$coursedesc,$now,$beneficiary,$sender);
1.2       raeburn   454:                         my %userrequest = (
                    455:                             $cdom.'_'.$cnum => {
                    456:                                 timestamp   => $now,
                    457:                                 section     => $usec,
                    458:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                    459:                                 status      => 'approved',
                    460:                             }
                    461:                         );
1.1       raeburn   462:                         my $userresult =
                    463:                             &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
                    464:                         if ($userresult ne 'ok') {
                    465:                             push(@warn_approves,$uname.':'.$udom);
                    466:                         }
                    467:                     } else {
1.2       raeburn   468:                         push(@processing_errors,$uname.':'.$udom);
1.1       raeburn   469:                     }
                    470:                 }
                    471:             } else {
                    472:                 push(@invalidusers,$uname.':'.$udom);
                    473:             }
                    474:         } else {
1.2       raeburn   475:             my ($num,$cnum) = split(':',$item);
                    476:             if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
                    477:                 if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
                    478:                     my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
                    479:                     my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
                    480:                     my $crstype = $requesthash{$cnum.'_approval'}{'crstype'};
                    481:                     my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
                    482:                     my $longroles = \%courseroles;
                    483:                     if ($crstype eq 'community') {
                    484:                         $longroles = \%communityroles;
                    485:                     }
1.5       raeburn   486:                     my $cancreate;
                    487:                     if ($cdom eq $ownerdom) {
                    488:                         if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
                    489:                                                               undef,'requestcourses')) {
                    490:                             $cancreate = 1;
                    491:                         }
                    492:                     } else {
                    493:                         my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
                    494:                         if ($userenv{'reqcrsotherdom.'.$crstype}) {
                    495:                             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
                    496:                             if (grep(/^\Q$cdom\E:/,@doms)) {
                    497:                                 $cancreate = 1;
                    498:                             }
                    499:                         }
                    500:                     }
                    501:                     if ($cancreate) {
1.2       raeburn   502:                         my $requestkey = $cdom.'_'.$cnum;
                    503:                         my %history = 
                    504:                             &Apache::lonnet::restore($requestkey,'courserequests',
                    505:                                                      $ownerdom,$ownername);
                    506:                         if ((ref($history{'details'}) eq 'HASH') && 
                    507:                             ($history{'disposition'} eq 'approval')) {
                    508:                             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg);
                    509:                             my $result = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
                    510:                                                       \$newusermsg,\$addresult,\$enrollcount,
                    511:                                                       \$response,\$keysmsg,\%domdefs,$longroles);
                    512:                             if ($result eq 'created') {
                    513:                                 push(@completed,$cnum);
                    514:                                 &send_selfserve_notification($ownername.':'.$ownerdom,$approvedmsg,
                    515:                                               $cid,$coursedesc,$now,$beneficiary,$sender);
                    516:                                 my %reqhash = (
                    517:                                                 reqtime     => $history{'reqtime'},
                    518:                                                 crstype     => $history{'crstype'},
                    519:                                                 details     => $history{'details'},
                    520:                                                 disposition => $history{'disposition'},
                    521:                                                 status      => 'created',
                    522:                                                 adjudicator => $env{'user.name'}.':'.
                    523:                                                                $env{'user.domain'},
                    524:                                               );
                    525:                                 my $userresult =
                    526:                                     &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                    527:                                                    'courserequests',$ownerdom,$ownername);
                    528:                                 if ($userresult eq 'ok') {
                    529:                                     my %status = (
                    530:                                                    'status:'.$cdom.':'.$cnum => 'created'
                    531:                                                  );
                    532:                                     my $statusresult = 
                    533:                                         &Apache::lonnet::put('courserequests',\%status,
                    534:                                                              $ownerdom,$ownername);
                    535:                                     if ($statusresult ne 'ok') {
                    536:                                         push(@warn_approves,$cnum);
                    537:                                     }
                    538:                                 }
                    539:                                 if ($userresult ne 'ok') {
                    540:                                     push(@warn_approves,$cnum);
                    541:                                 }
                    542:                             } else {
                    543:                                 push(@processing_errors,$cnum);
                    544:                             }
                    545:                         } else {
                    546:                             push(@processing_errors,$cnum);
                    547:                         }
                    548:                     } else {
1.5       raeburn   549:                         push(@nopermissions,$cnum);
1.2       raeburn   550:                     }
                    551:                 } else {
                    552:                     push(@existing,$cnum);
                    553:                 }
                    554:             } else {
                    555:                 push(@missingreq,$cnum);
                    556:             }
1.1       raeburn   557:         }
                    558:     }
1.2       raeburn   559:     my @changes = (@completed,@rejections);
                    560:     if ($context eq 'domain') {
                    561:         @changes = map {$_.'_approval'} (@changes);
                    562:     }
1.1       raeburn   563:     if (@rejections) {
1.3       raeburn   564:         foreach my $item (@rejections) {
1.1       raeburn   565:             if ($context eq 'course') {
1.3       raeburn   566:                 my $user = $item;
1.2       raeburn   567:                 &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
                    568:                                              $now,$beneficiary,$sender);
1.1       raeburn   569:                 my ($uname,$udom) = split(/:/,$user);
                    570:                 my %userrequest = (
                    571:                     $cdom.'_'.$cnum => {
                    572:                         timestamp   => $now,
                    573:                         adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                    574:                         status      => 'rejected',
                    575:                     }
                    576:                 );
                    577:                 my $userresult =
                    578:                     &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
                    579:                 if ($userresult ne 'ok') {
                    580:                     push(@warn_rejects,$user);
                    581:                 }
                    582:             } else {
1.3       raeburn   583:                 my $cnum = $item;
                    584:                 if (ref($requesthash{$cnum.'_approval'}) eq 'HASH') {
                    585:                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
                    586:                         my $requestkey = $cdom.'_'.$cnum;
1.2       raeburn   587:                         my $ownername = $requesthash{$cnum.'_approval'}{'ownername'};
                    588:                         my $ownerdom = $requesthash{$cnum.'_approval'}{'ownerdom'};
                    589:                         my $coursedesc = $requesthash{$cnum.'_approval'}{'description'};
                    590:                         &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
                    591:                                                      $cid,$coursedesc,$now,$beneficiary,
                    592:                                                      $sender);
                    593:                         my %history =
1.3       raeburn   594:                             &Apache::lonnet::restore($requestkey,'courserequests',
1.2       raeburn   595:                                                      $ownerdom,$ownername);
                    596:                         if ((ref($history{'details'}) eq 'HASH') &&
                    597:                             ($history{'disposition'} eq 'approval')) {
                    598:                             my %reqhash = (
                    599:                                             reqtime     => $history{'reqtime'},
                    600:                                             crstype     => $history{'crstype'},
                    601:                                             details     => $history{'details'},
                    602:                                             disposition => $history{'disposition'},
                    603:                                             status      => 'rejected',
                    604:                                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                    605:                                               );
                    606:                             my $userresult =
1.3       raeburn   607:                                 &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
                    608:                                                 'courserequests',$ownerdom,$ownername);
                    609:                             if ($userresult eq 'ok') {
                    610:                                 my %status = (
                    611:                                                'status:'.$cdom.':'.$cnum => 'rejected'
                    612:                                              );
                    613:                                 my $statusresult =
                    614:                                     &Apache::lonnet::put('courserequests',\%status,
                    615:                                                          $ownerdom,$ownername);
                    616:                                 if ($statusresult ne 'ok') {
                    617:                                     push(@warn_rejects,$cnum);
                    618:                                 }
                    619:                             } else {
                    620:                                 push(@warn_rejects,$cnum);
1.2       raeburn   621:                             }
1.3       raeburn   622:                         } else {
                    623:                             push(@warn_rejects,$cnum);
1.2       raeburn   624:                         }
1.3       raeburn   625:                     } else {
                    626:                         push(@existing,$cnum);
1.2       raeburn   627:                     }
1.3       raeburn   628:                 } else {
                    629:                     push(@rejectionerrors,$cnum);
1.2       raeburn   630:                 }
1.1       raeburn   631:             }
                    632:         }
                    633:     }
                    634:     if (@changes) {
                    635:         my $delresult;
                    636:         if ($context eq 'course') {
                    637:             $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
                    638:         } else {
                    639:             $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
                    640:         }
                    641:         if ($delresult eq 'ok') {
                    642:             my $namelink =
                    643:                 &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
                    644:             my ($chgmsg,$approvedlist,$rejectedlist);
                    645:             if ($context eq 'course') {
                    646:                 $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
1.2       raeburn   647:                 if (@completed) {
                    648:                     $approvedlist = join("\n",@completed);
1.1       raeburn   649:                     $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
1.2       raeburn   650:                     foreach my $user (@completed) {
1.1       raeburn   651:                         my ($uname,$udom) = split(/:/,$user);
                    652:                         my $userlink =
                    653:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
                    654:                         $output .= '<li>'.$userlink.'</li>';
                    655:                     }
                    656:                     $output .= '</ul></p>';
                    657:                 }
                    658:                 if (@rejections) {
                    659:                     $rejectedlist = join("\n",@rejections);
                    660:                     $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
                    661:                     foreach my $user (@rejections) {
                    662:                         $output .= '<li>'.$user.'</li>';
                    663:                     }
                    664:                     $output .= '</ul></p>';
                    665:                 }
1.2       raeburn   666:                 if ($notifylist ne '') {
                    667:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
                    668:                                                  $now,'coursemanagers',$sender,
                    669:                                                  $approvedlist,$rejectedlist);
                    670:                 }
1.1       raeburn   671:             } else {
1.2       raeburn   672:                 $chgmsg = "'Action was taken on the following course requests by [_1].',$namelink";
                    673:                 if (@completed) {
                    674:                     $approvedlist = join("\n",@completed);
                    675:                     $output .= '<p>'.&mt('The following courses were created:').'<ul>';
                    676:                     foreach my $cnum (@completed) {
                    677:                         my $showcourse;
                    678:                         if (ref($requesthash{$cnum.'_approval'})) {
                    679:                             $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    680:                         } else {
                    681:                             $showcourse = $cnum;
                    682:                         }
                    683:                         my $syllabuslink =
                    684:                             &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
                    685:                         $output .= '<li>'.$syllabuslink.'</li>';
                    686:                     }
                    687:                     $output .= '</ul></p>';
                    688:                 }
                    689:                 if (@rejections) {
                    690:                     $rejectedlist = join("\n",@rejections);
                    691:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
                    692:                     foreach my $cnum (@rejections) {
                    693:                         my $showcourse;
                    694:                         if (ref($requesthash{$cnum.'_approval'})) {
                    695:                             $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    696:                         } else {
                    697:                             $showcourse = $cnum;
                    698:                         }
                    699:                         $output .= '<li>'.$showcourse.'</li>';
                    700:                     }
                    701:                     $output .= '</ul></p>';
                    702:                 }
                    703:                 if ($notifylist ne '') {
                    704:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
                    705:                                                  $now,'domainmanagers',$sender,
                    706:                                                  $approvedlist,$rejectedlist);
                    707:                 }
1.1       raeburn   708:             }
                    709:         }
                    710:     }
                    711:     if (@existing) {
                    712:         if ($context eq 'course') {
                    713:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
                    714:             foreach my $user (@existing) {
                    715:                 $output .= '<li>'.$user.'</li>';
                    716:             }
                    717:             $output .= '</ul></p>';
                    718:         } else {
1.2       raeburn   719:             $output .= '<p>'.&mt('The following course creation requests were deleted because the course has already been created:').'<ul>';
                    720:             foreach my $cnum (@existing) {
                    721:                 my $showcourse;
                    722:                 my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                    723:                 if ($coursehash{'description'} ne '') {
                    724:                     $showcourse = $coursehash{'description'};
                    725:                 } else {
                    726:                     $showcourse = $cnum;
                    727:                 }
                    728:                 $output .= '<li>'.$showcourse.'</li>';
                    729:             }
                    730:             $output .= '</ul></p>';
1.1       raeburn   731:         }
                    732:     }
                    733:     if (@missingreq) {
                    734:         if ($context eq 'course') {
                    735:             $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
                    736:             foreach my $user (@missingreq) {
                    737:                 $output .= '<li>'.$user.'</li>';
                    738:             }
                    739:             $output .= '</ul></p>';
1.2       raeburn   740:         } else {
                    741:             $output .= '<p>'.&mt('The following course creation requests were ignored because the request is no longer in the course request queue:').'<ul>';
                    742:             foreach my $cnum (@missingreq) {
                    743:                 $output .= '<li>'.$cnum.'</li>';
                    744:             }
                    745:             $output .= '</ul></p>';
                    746: 
1.1       raeburn   747:         }
                    748:     }
                    749:     if (@invalidusers) {
                    750:         if ($context eq 'course') {
                    751:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
                    752:             foreach my $user (@invalidusers) {
                    753:                 $output .= '<li>'.$user.'</li>';
                    754:             }
                    755:             $output .= '</ul></p>';
                    756:         }
                    757:     }
                    758:     if (@limitexceeded) {
                    759:         if ($context eq 'course') {
                    760:             $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
                    761:             foreach my $user (@limitexceeded) {
                    762:                 $output .= '<li>'.$user.'</li>';
                    763:             }
                    764:             $output .= '</ul></p>';
                    765:         }
                    766:     }
1.5       raeburn   767:     if (@nopermissions) {
                    768:         $output .= '<p>'.&mt('The following course creation requests could not be processed because the course owner does hot have rights to create this type of course:').'<ul>';
                    769:         foreach my $cnum (@nopermissions) {
                    770:             my $showcourse;
                    771:             if (ref($requesthash{$cnum.'_approval'})) {
                    772:                 $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    773:             } else {
                    774:                 $showcourse = $cnum;
                    775:             }
                    776:             $output .= '<li>'.$showcourse.'</li>';
                    777:         }
                    778:         $output .= '</ul></p>';
                    779:     }
1.2       raeburn   780:     if (@processing_errors) {
1.1       raeburn   781:         if ($context eq 'course') {
                    782:             $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
1.2       raeburn   783:             foreach my $user (@processing_errors) {
1.1       raeburn   784:                 $output .= '<li>'.$user.'</li>';
                    785:             }
                    786:             $output .= '</ul></p>';
                    787:         } else {
1.2       raeburn   788:             $output .= '<p>'.&mt('The following course creation requests could not be processed because an error occurred:').'<ul>';
                    789:             foreach my $cnum (@processing_errors) {
                    790:                 my $showcourse;
                    791:                 if (ref($requesthash{$cnum.'_approval'})) {
                    792:                     $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    793:                 } else {
                    794:                     $showcourse = $cnum;
                    795:                 }
                    796:                 $output .= '<li>'.$showcourse.'</li>';
                    797:             }
                    798:             $output .= '</ul></p>';
1.1       raeburn   799:         }
                    800:     }
1.3       raeburn   801:     if (@rejectionerrors) {
                    802:         $output .= '<p>'.&mt('The following course creation request rejections could not be fully processed because an error occurred:').'<ul>';
                    803:         foreach my $cnum (@rejectionerrors) {
                    804:             my $showcourse;
                    805:             if (ref($requesthash{$cnum.'_approval'})) {
                    806:                 $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    807:             } else {
                    808:                 $showcourse = $cnum;
                    809:             }
                    810:             $output .= '<li>'.$showcourse.'</li>';
                    811:         }
                    812:         $output .= '</ul></p>';
                    813:     }
1.2       raeburn   814:     if (@warn_approves || @warn_rejects) {
1.1       raeburn   815:         if ($context eq 'course') {
                    816:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
                    817:             foreach my $user (@warn_approves) {
                    818:                 $output .= '<li>'.$user.'</li>';
                    819:             }
                    820:             $output .= '</ul></p>';
                    821:         } else {
1.2       raeburn   822:             $output .= '<p>'.&mt("For the following course requests an error occurred when updating the requestor's own course requests record:").'<ul>';
                    823:             foreach my $cnum (@warn_approves,@warn_rejects) {
                    824:                 my $showcourse;
                    825:                 if (ref($requesthash{$cnum.'_approval'})) {
                    826:                     $showcourse = $requesthash{$cnum.'_approval'}{'description'};
                    827:                 } else {
                    828:                     $showcourse = $cnum;
                    829:                 }
                    830:                 $output .= '<li>'.$showcourse.'</li>';
1.1       raeburn   831:             }
                    832:             $output .= '</ul></p>';
                    833:         }
                    834:     }
                    835:     return $output;
                    836: }
                    837: 
                    838: sub get_student_counts {
                    839:     my ($cdom,$cnum) = @_;
                    840:     my (%idx,%stucounts);
                    841:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
                    842:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
                    843:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
                    844:     while (my ($student,$data) = each(%$classlist)) {
                    845:         if (($data->[$idx{'status'}] eq 'Active') ||
                    846:             ($data->[$idx{'status'}] eq 'Future')) {
                    847:             if ($data->[$idx{'type'}] eq 'selfenroll') {
                    848:                 $stucounts{'selfenroll'} ++;
                    849:             }
                    850:             $stucounts{'allstudents'} ++;
                    851:         }
                    852:     }
                    853:     return (\%stucounts,\%idx,$classlist);
                    854: }
                    855: 
1.2       raeburn   856: sub course_creation {
                    857:     my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
                    858:         $keysmsg,$domdefs,$longroles) =  @_;
                    859:     unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') && 
                    860:             (ref($longroles) eq 'HASH')) {
                    861:         return 'error: Invalid request';
                    862:     }
                    863:     my ($result,$ownername,$ownerdom);
                    864:     my $crstype = $details->{'crstype'};
                    865:     if ($context eq 'domain') {
                    866:         $ownername = $details->{'owner'};
                    867:         $ownerdom  = $details->{'domain'};
                    868:     } else {
                    869:         $ownername = $env{'user.name'};
                    870:         $ownerdom  = $env{'user.domain'};
                    871:     }
                    872:     my $type = 'Course';
                    873:     if ($crstype eq 'community') {
                    874:         $type = 'Community';
                    875:     }
                    876:     my $owneremail;
                    877:     my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
                    878:     foreach my $email ('permanentemail','critnotification','notification') {
                    879:         $owneremail = $emails{$email};
                    880:         last if ($owneremail ne '');
                    881:     }
1.8     ! raeburn   882:     my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
1.2       raeburn   883:     my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
                    884:                   \%reqdetails,$longroles,\$logmsg,\$newusermsg,\$addresult,
                    885:                   \$enrollcount,\$output,\$keysmsg,$ownerdom,$ownername,$cnum,$crstype);
                    886:     if ($cid eq "/$dom/$cnum") {
                    887:         $result = 'created';
                    888:     } else {
                    889:         $result = 'error: '.$cid;
                    890:     }
                    891:     return $result;
                    892: }
                    893: 
                    894: sub build_batchcreatehash {
1.8     ! raeburn   895:     my ($dom,$context,$details,$owneremail,$domdefs) = @_;
1.2       raeburn   896:     my %batchhash;
                    897:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users};
                    898:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
                    899:         my $emailenc = &Apache::lonnet::escape($owneremail);
                    900:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
                    901:         foreach my $item (@items) {
                    902:             $batchhash{$item} = $details->{$item};
                    903:         }
                    904:         $batchhash{'title'} = $details->{'cdescr'};
                    905:         $batchhash{'coursecode'} = $details->{'instcode'};
                    906:         $batchhash{'emailenc'} = $emailenc;
                    907:         $batchhash{'adds'} = $details->{'autoadds'};
                    908:         $batchhash{'drops'} = $details->{'autodrops'};
                    909:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
                    910:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
                    911:         if ($details->{'crstype'} eq 'community') {
                    912:             $batchhash{'crstype'} = 'Community';
                    913:         } else {
                    914:             $batchhash{'crstype'} = 'Course';
                    915:         }
1.8     ! raeburn   916:         my ($owner_firstname,$owner_lastname);
        !           917:         if ($context eq 'domain') {
        !           918:             my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
        !           919:                                                            $details->{'owner'},
        !           920:                                                            'firstname','lastname');
        !           921:             $owner_firstname = $userenv{'firstname'};
        !           922:             $owner_lastname = $userenv{'lastname'};
        !           923:         } else {
        !           924:             $owner_firstname = $env{'environment.firstname'};
        !           925:             $owner_lastname = $env{'environment.lastname'};
        !           926:         }
        !           927:         if (ref($details->{'personnel'}) eq 'HASH') {
        !           928:             %{$batchhash{'users'}} = %{$details->{'personnel'}};
        !           929:             if (ref($batchhash{'users'}) eq 'HASH') {  
        !           930:                 foreach my $userkey (keys(%{$batchhash{'users'}})) {
        !           931:                     if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
        !           932:                         if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
        !           933:                             foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
        !           934:                                 my $start = '';
        !           935:                                 my $end = '';
        !           936:                                 if ($role eq 'st') {
        !           937:                                     $start = $details->{'accessstart'};
        !           938:                                     $end = $details->{'accessend'};
        !           939:                                 }
        !           940:                                 $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
        !           941:                                 $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
        !           942:                             } 
        !           943:                         }
        !           944:                     }
        !           945:                 }
        !           946:             }
        !           947:         }
        !           948:         $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
        !           949:         $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
        !           950:         $batchhash{'users'}{$owner}{emailenc} = $emailenc;
        !           951:         $batchhash{'users'}{$owner}{owneremail} = $owneremail;
1.2       raeburn   952:     }
                    953:     return %batchhash;
                    954: }
                    955: 
1.4       raeburn   956: sub can_clone_course {
                    957:     my ($uname,$udom,$clonecrs,$clonedom) = @_;
                    958:     my $canclone;
                    959:     my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
                    960:                                                   ['cc'],[$clonedom]);
                    961:     if (exists($roleshash{$clonedom.':'.$clonecrs.':cc'})) {
                    962:         $canclone = 1;
                    963:     } else {
                    964:         my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
                    965:         my $cloners = $courseenv{'cloners'};
                    966:         if ($cloners ne '') {
                    967:             my @cloneable = split(',',$cloners);
                    968:             if (grep(/^\*$/,@cloneable)) {
                    969:                 $canclone = 1;
                    970:             }
                    971:             if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
                    972:                 $canclone = 1;
                    973:             }
                    974:             if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
                    975:                 $canclone = 1;
                    976:             }
                    977:         }
                    978:     }
                    979:     return $canclone;
                    980: }
                    981: 
1.1       raeburn   982: 1;

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