File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.12.2.12: download - view: text, annotated - select for diffs
Wed Dec 8 03:49:34 2010 UTC (13 years, 5 months ago) by raeburn
Branches: GCI_3
- Customization for GCI_3.
  - Backport 1.25.

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

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