File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.2: download - view: text, annotated - select for diffs
Sun Aug 16 19:16:16 2009 UTC (14 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Course Requests
- Move &build_batchcreatehash() from lonrequestcourse.pm to loncoursequeueadmin.pm
- Move call to LONCAPA::batchcreatecourse into a separate routine: &course_creation()
  within loncoursequeueadmin.pm
- Code added for resolution of course requests subject to approval by DC.
   - either approve and create course, or reject.
   - formatting of LON-CAPA messages sent to notification list and requestor improved.
- Change in storage of status information for a request.
  - Status now stored in a separate key => value in courserequests.db file belonging to requestor
    - key is status:$cdom:$cnum
- User's display of pending requests now shows the required information.
(Select button to view/modify/cancel individual request currently inactive - work in progress).

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

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