File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.15: download - view: text, annotated - select for diffs
Sat Jan 16 16:15:08 2010 UTC (14 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Apache::Constants not needed.

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

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