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

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

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