File:  [LON-CAPA] / loncom / interface / loncoursequeueadmin.pm
Revision 1.41: download - view: text, annotated - select for diffs
Fri Jan 3 18:39:51 2014 UTC (10 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC2, HEAD
- New routine in localenroll.pm -- &crsreq_updates() -- to support
  domain-specific operations during course request process.
- Tabbed interface used for textbook-type course requests, one tab
  for course creation, one for request logs.
- Column for unique codes included in user's request log display, if
  domain has enabled use of unique codes.
- Correction to LON-CAPA messaging of unique code -- recipient should
  be requestor.

    1: # The LearningOnline Network
    2: # Utilities to administer domain course requests and course self-enroll requests
    3: #
    4: # $Id: loncoursequeueadmin.pm,v 1.41 2014/01/03 18:39:51 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: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::loncoursequeueadmin.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: Utilities used by domain coordinators to administer queued course creation requests,
   39: and by course coordinators for queued self-enrollment requests, and by general
   40: users to display their queued self-enrollment requests.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: =head1 SUBROUTINES
   46: 
   47: =over
   48: 
   49: =item send_selfserve_notification()
   50: 
   51: =item display_queued_requests()
   52: 
   53: =item build_queue_display()
   54: 
   55: =item update_request_queue()
   56: 
   57: =item get_student_counts()
   58: 
   59: =item course_creation()
   60: 
   61: =item build_batchcreatehash()
   62:  
   63: =item can_clone_course()
   64: 
   65: =item get_processtype()
   66: 
   67: =item queued_selfenrollment()
   68: 
   69: =item update_coursereq_status()
   70: 
   71: =item process_official_reqs()
   72: 
   73: =item is_active_author()
   74: 
   75: =item author_prompt()
   76: 
   77: =item reqauthor_check()
   78: 
   79: =item process_reqauthor()
   80: 
   81: =back
   82: 
   83: =cut
   84: 
   85: package Apache::loncoursequeueadmin;
   86: 
   87: use strict;
   88: use Apache::lonnet;
   89: use Apache::loncommon;
   90: use Apache::lonmsg;
   91: use Apache::lonlocal;
   92: use Apache::lonuserutils;
   93: use LONCAPA qw(:DEFAULT :match);
   94: 
   95: sub send_selfserve_notification {
   96:     my ($notifylist,$textstr,$cid,$contextdesc,$timestamp,$context,$sender,
   97:         $approvedlist,$rejectedlist,$crstype) = @_;
   98: # FIXME locallocaltime needs to be able to take $sender_lh as an argument
   99: #       so this can be localized to the recipients date display format/time zone
  100:     $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
  101:     my ($msgcc,$rawsubj,@rawmsg,$subject,$message,$reviewer,$msgtxt);
  102:     my ($senderuname,$senderudom) = split(':',$sender);
  103:     if ($context eq 'coursemanagers') {
  104:         $rawsubj = 'Self-enrollment requests processed';
  105:         push(@rawmsg,{
  106:                       mt => 'Enrollment requests in the following course: [_1] have been processed.',
  107:                       args => ["\n$contextdesc"],
  108:                      });
  109:     } elsif ($context eq 'domainmanagers') {
  110:         $rawsubj = 'Course/Community requests reviewed';
  111:         push(@rawmsg,{
  112:                       mt  => 'Course/Community creation requests in the following domain: "[_1]" have been reviewed.',
  113:                       args => ["\n$contextdesc"],
  114:                      });
  115:         if (ref($textstr) eq 'ARRAY') {
  116:             push(@rawmsg,@{$textstr});
  117:         }
  118:     } elsif ($context eq 'authormanagers') {
  119:         $rawsubj = 'Authoring space requests reviewed';
  120:         push(@rawmsg,{
  121:                       mt  => 'Authoring requests in the following domain: "[_1]" have been reviewed.',
  122:                       args => ["\n$contextdesc"],
  123:                      });
  124:         if (ref($textstr) eq 'ARRAY') {
  125:             push(@rawmsg,@{$textstr});
  126:         }
  127:     } elsif ($context eq 'enroller') {
  128:         $rawsubj = 'Enrollment request';
  129:         if ($crstype eq 'community') {
  130:             $msgtxt = 'Your request for enrollment in the following community: [_1]requested on [_2]has been reviewed by a Coordinator.'
  131:         } else {
  132:             $msgtxt = 'Your request for enrollment in the following course: [_1]requested on [_2]has been reviewed by a Course Coordinator.';
  133:         } 
  134:         push(@rawmsg,{
  135:                       mt  => $msgtxt,
  136:                       args => ["\n  ".$contextdesc.",\n",$timestamp.",\n"],
  137: 
  138:                      });
  139:         if (ref($textstr) eq 'ARRAY') {
  140:             push(@rawmsg,@{$textstr});
  141:         }
  142:     } elsif ($context eq 'courserequestor') {
  143:         if ($crstype eq 'Community') {
  144:             $rawsubj = 'Community request';
  145:             $msgtxt = 'Your request for creation of the following community: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  146:         } else {
  147:             $rawsubj = 'Course request';
  148:             $msgtxt = 'Your request for creation of the following course: [_1]requested on [_2]has been reviewed by a Domain Coordinator.';
  149:         }
  150:         push(@rawmsg,{
  151:                       mt  => $msgtxt,
  152:                       args => ["\n".$contextdesc.",\n",$timestamp.",\n"],
  153: 
  154:                      });
  155:         if (ref($textstr) eq 'ARRAY') {
  156:             push(@rawmsg,@{$textstr});
  157:         }
  158:     } elsif ($context eq 'pendingrequestor') {
  159:         if ($crstype eq 'Community') {
  160:             $rawsubj = 'Community request';
  161:         } else {
  162:             $rawsubj = 'Processed course request';
  163:         }
  164:         if (ref($textstr) eq 'ARRAY') {
  165:             push(@rawmsg,@{$textstr});
  166:         }
  167:     } elsif ($context eq 'coursereq') {
  168:         if ($crstype eq 'community') {
  169:             $rawsubj = 'Community request to review';
  170:             $msgtxt = 'Creation of the following community: [_1]was requested by [_2] on [_3].';
  171:         } else {
  172:             $rawsubj = 'Course request to review';
  173:             $msgtxt = 'Creation of the following course: [_1]was requested by [_2] on [_3].';
  174:         }
  175:         push(@rawmsg,{
  176:                       mt  => $msgtxt,
  177:                       args => ["\n  $contextdesc\n",$textstr,$timestamp],
  178:                      },
  179:                      {
  180:                       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.',
  181:                       args => ["\n","\n\n","\n\n"],
  182:                      });
  183:     } elsif ($context eq 'selfenrollreq') {
  184:         $rawsubj = 'Self-enrollment request';
  185:         if ($crstype eq 'community') {
  186:             $msgtxt = 'Enrollment in the following community: [_1]was requested by [_2] on [_3].'
  187:         } else {
  188:             $msgtxt = 'Enrollment in the following course: [_1]was requested by [_2] on [_3].'
  189:         }
  190:         push(@rawmsg,{
  191:                       mt  => $msgtxt,
  192:                       args => ["\n  $contextdesc\n",$textstr,$timestamp."\n"],
  193:                      });
  194:         my $directions;  
  195:         if ($crstype eq 'community') {
  196:             $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.';
  197:         } else {
  198:             $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.';
  199:         }
  200:         push(@rawmsg,
  201:                      {
  202:                       mt   => $directions,
  203:                       args => ["  \n\n","\n"],
  204:                      });
  205:     } elsif ($context eq 'authorreq') {
  206:         $rawsubj = 'Authoring space request to review';
  207:         $msgtxt = 'Assignment of an author role in the [_1] domain[_2]was requested by [_3] on [_4].';
  208:         push(@rawmsg,{
  209:                       mt  => $msgtxt,
  210:                       args => [$contextdesc,"\n",$textstr,$timestamp],
  211:                      },
  212:                      {
  213:                       mt =>'[_1]As Domain Coordinator, use: [_2]Main Menu -> Create users or modify the roles and privileges of users -> Authoring space reqests[_3]to display a list of pending requests, which you can either approve or reject.',
  214:                       args => ["\n","\n\n  ","\n\n"],
  215:                      });
  216:     } elsif ($context eq 'requestauthor') {
  217:         $rawsubj = 'Authoring space request';
  218:         $msgtxt = 'Your request for an authoring space requested on [_1]has been reviewed by a Domain Coordinator.';
  219:         push(@rawmsg,{
  220:                       mt  => $msgtxt,
  221:                       args => [$timestamp."\n"],
  222:                      });
  223:         if (ref($textstr) eq 'ARRAY') {
  224:             push(@rawmsg,@{$textstr});
  225:         }
  226:     } elsif ($context eq 'uniquecode') {
  227:         $rawsubj = 'Course Identifier';
  228:         if (ref($textstr) eq 'ARRAY') {
  229:             push(@rawmsg,@{$textstr});
  230:         }
  231:     } elsif ($context eq 'queuedcrsreq') {
  232:         $rawsubj = 'Course Request Queued';
  233:         if (ref($textstr) eq 'ARRAY') {
  234:             push(@rawmsg,@{$textstr});
  235:         }
  236:     } elsif ($context eq 'createdcrsreq') {
  237:         $rawsubj = 'Course Creation Information';
  238:         if (ref($textstr) eq 'ARRAY') {
  239:             push(@rawmsg,@{$textstr});
  240:         }
  241:     }
  242:     my @to_notify = split(/,/,$notifylist);
  243:     my $numsent = 0;
  244:     my @recusers;
  245:     my @recudoms;
  246:     foreach my $cc (@to_notify) {
  247:         my ($ccname,$ccdom) = split(/:/,$cc);
  248:         if (!exists($msgcc->{$ccname.':'.$ccdom})) {
  249:             push(@recusers,$ccname);
  250:             push(@recudoms,$ccdom);
  251:             $msgcc->{$ccname.':'.$ccdom}='';
  252:             $numsent ++;
  253:         }
  254:     }
  255:     my %reciphash = (
  256:                      cc => $msgcc,
  257:     );
  258:     my ($uname,$udom);
  259:     if ($sender =~ /:/) {
  260:         ($uname,$udom) = split(/:/,$sender);
  261:     } elsif ($context eq 'course') {
  262:         $uname = $sender;
  263:         my %courseinfo = &Apache::lonnet::coursedescription($cid);
  264:         $udom = $courseinfo{'num'};
  265:     }
  266:     my %sentmessage;
  267:     my $stamp = time;
  268:     my $msgcount = &Apache::lonmsg::get_uniq();
  269:     my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
  270:     $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
  271:     $message = '';
  272:     foreach my $item (@rawmsg) {
  273:         if (ref($item) eq 'HASH') {
  274:             $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
  275:         }
  276:     }
  277:     &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms,undef,undef,undef,undef,$senderuname,$senderudom);
  278:     my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
  279:     my $status;
  280:     foreach my $recip (sort(keys(%{$msgcc}))) {
  281:         my ($ccname,$ccdom) = split(/:/,$recip);
  282:         my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
  283:         my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
  284:         my $message = '';
  285:         foreach my $item (@rawmsg) {
  286:             if (ref($item) eq 'HASH') {
  287:                 $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
  288:                                                        @{$item->{args}})."\n";
  289:             }
  290:         }
  291:         if ($context eq 'coursemanagers') {
  292:             if ($approvedlist) {
  293:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
  294:             }
  295:             if ($rejectedlist) {
  296:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
  297:             }
  298:         } elsif ($context eq 'domainmanagers') {
  299:             if ($approvedlist) {
  300:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
  301:             }
  302:             if ($rejectedlist) {
  303:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
  304:             }
  305:         } elsif ($context eq 'authormanagers') {
  306:             if ($approvedlist) {
  307:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved author role requests:')."\n".$approvedlist;
  308:             }
  309:             if ($rejectedlist) {
  310:                 $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected author role requests:')."\n".$rejectedlist;
  311:             }
  312:         }
  313:         $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
  314:     }
  315:     $status =~ s/,$//;
  316:     return ($recipstatus,$status);
  317: }
  318: 
  319: sub display_queued_requests {
  320:     my ($context,$dom,$cnum) = @_;
  321:     my ($namespace,$formaction,$nextelement,%requesthash);
  322:     if ($context eq 'course') {
  323:         $formaction = '/adm/createuser';
  324:         $namespace = 'selfenrollrequests';
  325:         %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
  326:         $nextelement = '<input type="hidden" name="state" value="done" />';
  327:     } elsif ($context eq 'requestauthor') {
  328:         $formaction = '/adm/createuser';
  329:         $namespace = 'requestauthorqueue';
  330:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom);
  331:         $nextelement = '<input type="hidden" name="state" value="done" />';
  332:     } else {
  333:         $formaction = '/adm/createcourse';
  334:         $namespace = 'courserequestqueue';
  335:         my $disposition = 'approval';
  336:         my $nextphase = 'requestchange';
  337:         if ($context eq 'pending') {
  338:             $disposition = 'pending';
  339:             $nextphase = 'requestvalidation';
  340:         }
  341:         %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,'_'.$disposition);
  342:         $nextelement = '<input type="hidden" name="phase" value="'.$nextphase.'" />';
  343:     }
  344:     my ($output,%queue_by_date);
  345:     if (keys(%requesthash) > 0) {
  346:         $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'."\n".
  347:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
  348:                   $nextelement."\n";
  349:         foreach my $item (keys(%requesthash)) {
  350:             my ($timestamp,$entry,$pending);
  351:             if ($context eq 'course') {
  352:                 ($timestamp, my $usec) = split(/:/,$requesthash{$item});
  353:                 $entry = $item.':'.$usec;
  354:             } elsif ($context eq 'requestauthor') {
  355:                 $timestamp = $requesthash{$item};
  356:                 ($entry) = ($item =~ /^($match_username)_approval$/);
  357:             } else {
  358:                 $timestamp = $requesthash{$item}{'timestamp'};
  359:                 if (ref($requesthash{$item}) eq 'HASH') {
  360:                     my ($cnum,$disposition) = split('_',$item);
  361:                     $entry = $cnum.':'.$requesthash{$item}{'ownername'}.':'.
  362:                              $requesthash{$item}{'ownerdom'}.':';
  363:                     if ($context eq 'pending') {
  364:                         $entry .= $requesthash{$item}{'instcode'};
  365:                     } else {
  366:                         $entry .= $requesthash{$item}{'crstype'};
  367:                     }
  368:                     $entry .= ':'.$requesthash{$item}{'description'};
  369:                 }
  370:             }
  371:             if ($entry ne '') {
  372:                 if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
  373:                     push(@{$queue_by_date{$timestamp}},$entry);
  374:                 } else {
  375:                     $queue_by_date{$timestamp} = [$entry];
  376:                 }
  377:             }
  378:         }
  379:         if (keys(%queue_by_date) > 0) {
  380:             if ($context eq 'course') {
  381:                 $output .=  '<h3>'.&mt('Self-enrollment requests queued pending approval by a Coordinator').'</h3>';
  382:             } elsif ($context eq 'pending') {
  383:                 $output .= '<h3>'.&mt('Requests for official courses queued pending validation').'</h3>'.
  384:                            '<p>'.&mt('Requests are validated against institutional data to confirm that the requestor is an instructor of record.').'<br />'.
  385:                            &mt('Validation is attempted when the request is submitted.').' '.&mt('If unvalidated, the request will be held in a queue.').' '.&mt('Validation of pending requests is automatically repeated daily.').'</p>';
  386:             } elsif ($context eq 'requestauthor') {
  387:                 $output .= '<h3>'.&mt('Requests for authoring space queued pending approval by a Domain Coordinator').'</h3>';
  388:             } else {
  389:                 $output .= '<h3>'.&mt('Course/Community requests queued pending approval by a Domain Coordinator').'</h3>';
  390:             } 
  391:             $output .= &build_queue_display($dom,$context,\%queue_by_date).
  392:                        '<input type="hidden" name="queue" value="approval" />';
  393:         } else {
  394:             $output .= '<div class="LC_info">';
  395:             if ($context eq 'course') {
  396:                 $output .= &mt('There are currently no enrollment requests awaiting approval.');
  397:             } elsif ($context eq 'pending') {
  398:                 $output .= &mt('There are currently no requests for official courses awaiting validation.');
  399:             } elsif ($context eq 'requestauthor') {
  400:                 $output .= &mt('There are currently no requests for authoring space awaiting approval.');
  401:             } elsif ($context eq 'domain') {
  402:                 $output .= &mt('There are currently no course or community requests awaiting approval.');
  403:             }
  404:             $output .= '</div>'; 
  405:         }
  406:         if ($context eq 'pending') {
  407:             $output .= '<br /><input type="submit" name="validationcheck" value="'.
  408:                        &mt('Validate').'" /><br />'."\n".
  409:                        '<p>'.&mt('Any course/community requests which are successfully validated will be created immediately.').' '.&mt('Unvalidated requests will be listed for manual approval/rejection.').'</p>';
  410:         } else {
  411:             $output .= '<br /><input type="submit" name="processqueue" value="'.&mt('Save').'" />';
  412:         }
  413:         $output .= '</form>';
  414:     } else {
  415:         $output .= '<div class="LC_info">';
  416:         if ($context eq 'course') {
  417:             $output .= &mt('There are currently no enrollment requests awaiting approval.');
  418:         } elsif ($context eq 'pending') {
  419:             $output .= &mt('There are currently no requests for official courses awaiting validation.');
  420:         } else {
  421:             $output .= &mt('There are currently no course or community requests awaiting approval.');
  422:         }
  423:         $output .= '</div>';
  424:     }
  425:     return $output;
  426: }
  427: 
  428: sub build_queue_display {
  429:     my ($dom,$context,$queue) = @_;
  430:     return unless (ref($queue) eq 'HASH');
  431:     my %crstypes;
  432:     my $output =  &Apache::loncommon::start_data_table().
  433:                   &Apache::loncommon::start_data_table_header_row();
  434:     unless ($context eq 'pending') { 
  435:         $output .= '<th>'.&mt('Action').'</th>';
  436:     }
  437:     $output .= '<th>'.&mt('Requestor').'</th>';
  438:     if ($context eq 'course') {
  439:         $output .= '<th>'.&mt('Section').'</th>'.
  440:                    '<th>'.&mt('Date requested').'</th>';
  441:     } elsif ($context eq 'requestauthor') {
  442:         $output .= '<th>'.&mt('Date requested').'</th>';
  443:     } elsif ($context eq 'pending' || $context eq 'stillpending') {
  444:         $output .= '<th>'.&mt('Institutional code').'</th>'.
  445:                    '<th>'.&mt('Date requested').'</th>'.
  446:                    '<th>'.&mt('Details').'</th>';
  447:     } else {
  448:         %crstypes = &Apache::lonlocal::texthash (
  449:                         official   => 'Official course',
  450:                         unofficial => 'Unofficial course',
  451:                         community  => 'Community',
  452:                         textbook   => 'Textbook course',
  453:                     );
  454:         $output .= '<th>'.&mt('Type').'</th>'.
  455:                    '<th>'.&mt('Date requested').'</th>'.
  456:                    '<th>'.&mt('Details').'</th>';
  457:     }
  458:     $output .= &Apache::loncommon::end_data_table_header_row();
  459:     my @sortedtimes = sort {$a <=> $b} (keys(%{$queue}));
  460:     my $count = 0;
  461:     foreach my $item (@sortedtimes) {
  462:         if (ref($queue->{$item}) eq 'ARRAY') {
  463:             foreach my $request (sort(@{$queue->{$item}})) {
  464:                 my ($row,$approve,$reject,$showtime,$showsec,$namelink,
  465:                     $detailslink,$crstype,$instcode);
  466:                 $showtime = &Apache::lonlocal::locallocaltime($item);
  467:                 if ($context eq 'course') {
  468:                     my ($puname,$pudom,$pusec) = split(/:/,$request);
  469:                     $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
  470:                     $reject = $puname.':'.$pudom;
  471:                     $showsec = $pusec;
  472:                     if ($showsec eq '') {
  473:                         $showsec = &mt('none');
  474:                     }
  475:                     $namelink = &Apache::loncommon::aboutmewrapper(
  476:                                 &Apache::loncommon::plainname($puname,$pudom),
  477:                                 $puname,$pudom);
  478:                 } elsif ($context eq 'requestauthor') {
  479:                     if (&Apache::lonnet::homeserver($request,$dom) ne 'no_host') {
  480:                         $approve = $count.':'.$request;
  481:                         $reject = $request; 
  482:                         $namelink = &Apache::loncommon::aboutmewrapper(
  483:                                     &Apache::loncommon::plainname($request,$dom),
  484:                                     $request,$dom);
  485:                     }
  486:                 } else {
  487:                     my ($cnum,$ownername,$ownerdom,$type,$cdesc);
  488:                     my $queue = 'approval'; 
  489:                     if ($context eq 'pending' || $context eq 'stillpending') {
  490:                         ($cnum,$ownername,$ownerdom,$instcode,$cdesc)=split(/:/,$request,5);
  491:                         $queue = 'pending';                        
  492:                     } else {
  493:                         ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request,5);
  494:                         $crstype = $type;
  495:                         if (defined($crstypes{$type})) {
  496:                             $crstype = $crstypes{$type};
  497:                         }
  498:                     }
  499:                     $detailslink='<a href="javascript:opencoursereqdisplay('.
  500:                                   "'$dom','$cnum','$queue'".');">'.$cdesc.'</a>';
  501:                     $approve = $count.':'.$cnum;
  502:                     $reject = $cnum;
  503:                     $namelink = &Apache::loncommon::aboutmewrapper(
  504:                                 &Apache::loncommon::plainname($ownername,$ownerdom),
  505:                                 $ownername,$ownerdom);
  506:                 }
  507:                 unless ($context eq 'pending') {
  508:                     $row = '<td><span class="LC_nobreak"><label>'.
  509:                            '<input type="radio" value="'.$approve.'" name="'.$count.'radioreq" />'.&mt('Approve').'</label>'.
  510:                            '<label>'.('&nbsp;'x2).
  511:                            '<input type="radio" value="'.$reject.'" name="'.$count.'radioreq" />'.&mt('Reject').'</label>'.
  512:                            '<label>'.('&nbsp;'x2).
  513:                            '<input type="radio" value="'."later:".$reject.'" name="'.$count.'radioreq" checked />'.&mt('Decide Later').
  514:                            '</label></span><br /></td>';
  515:                 }
  516:                 $row .= '<td>'.$namelink.'</td>'."\n";
  517:                 if ($context eq 'course') {
  518:                     $row .= '<td>'.$showsec.'</td>'."\n".
  519:                             '<td>'.$showtime.'</td>'."\n";
  520:                 } elsif ($context eq 'requestauthor') {
  521:                     $row .= '<td>'.$showtime.'</td>'."\n";
  522:                 } else { 
  523:                     if ($context eq 'pending' || $context eq 'stillpending') {
  524:                         $row .= '<td>'.$instcode.'</td>'."\n";
  525:                     } else {
  526:                         $row .= '<td>'.$crstype.'</td>'."\n";
  527:                     }
  528:                     $row .= '<td>'.$showtime.'</td>'."\n".
  529:                             '<td>'.$detailslink.'</td>'."\n";
  530:                 }
  531:                 $output .= &Apache::loncommon::start_data_table_row()."\n".
  532:                            $row.
  533:                            &Apache::loncommon::end_data_table_row()."\n";
  534:                 $count ++;
  535:             }
  536:         }
  537:     }
  538:     $output .= &Apache::loncommon::end_data_table();
  539:     return $output;
  540: }
  541: 
  542: sub update_request_queue {
  543:     my ($context,$cdom,$cnum,$coursedesc) = @_;
  544:     my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
  545:         $stucounts,$idx,$classlist,%requesthash,$cid,$domdesc,$now,
  546:         $sender,$approvedmsg,$rejectedmsg,$beneficiary,
  547:         @existing,@missingreq,@invalidusers,@limitexceeded,@completed,
  548:         @processing_errors,@warn_approves,@warn_rejects,@approvals,@warn_dels,
  549:         @rejections,@rejectionerrors,@nopermissions,%courseroles,@toremove,
  550:         %communityroles,%domdefs,%approvalmsg,%rejectionmsg,$crstype,$queue,
  551:         $firsturl,$uniquecode,%codes);
  552:     my $count=0;
  553:     while (my @course = &Apache::loncommon::get_env_multiple('form.'.$count.'radioreq')) {
  554:         if ($course[0] =~ /^\d+:.*/) {
  555:             push(@approvals,$course[0]);
  556:         } elsif ($course[0] =~ /^later:.*/) {
  557:             #decide later
  558:         } else {
  559:             push(@rejections,$course[0]);
  560:         }
  561:         $count+=1;
  562:     }
  563: 
  564:     $now = time;
  565:     $sender = $env{'user.name'}.':'.$env{'user.domain'};
  566:     if ($context eq 'course') {
  567:         $namespace = 'selfenrollrequests';
  568:         $beneficiary = 'enroller';
  569:         $cid = $env{'request.course.id'};
  570:         $crstype = lc(&Apache::loncommon::course_type());
  571:         $firsturl = &course_portal_url($cnum,$cdom);
  572:         %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
  573:         $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
  574:         $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
  575:         $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
  576:         $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
  577:         $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
  578:         ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
  579:         $approvedmsg = [{
  580:                             mt => 'Your request for enrollment has been approved.',
  581:                         },
  582:                         {
  583:                             mt   => 'Visit [_1] to log-in and access the course',
  584:                             args => [$firsturl],
  585:                         }];
  586:         $rejectedmsg =  [{
  587:                             mt => 'Your request for enrollment has not been approved.',
  588:                         }];
  589:     } elsif ($context eq 'requestauthor') {
  590:         $namespace = 'requestauthorqueue';
  591:         $beneficiary = 'requestauthor';
  592:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom);
  593:         my %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  594:         if (ref($domdefs{'requestauthor'}) eq 'HASH') {
  595:             if (ref($domdefs{'requestauthor'}{'notify'}) eq 'HASH') {
  596:                 $notifylist = $domdefs{'requestauthor'}{'notify'}{'approval'};
  597:             }
  598:         }
  599:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
  600:         $firsturl = &course_portal_url($domconfiguser,$cdom);
  601:         $approvedmsg = [{
  602:                             mt => 'Your request for authoring space has been approved.',
  603:                         },
  604:                         {
  605:                             mt   => 'Visit [_1] to log-in and select your author role',
  606:                             args => [$firsturl],
  607:                         }];
  608:         $rejectedmsg =  [{
  609:                             mt => 'Your request for authoring space has not been approved.',
  610:                         }];
  611:         $domdesc = &Apache::lonnet::domain($cdom);
  612:     } else {
  613:         $domdesc = &Apache::lonnet::domain($cdom);
  614:         $namespace = 'courserequestqueue';
  615:         $beneficiary = 'courserequestor';
  616:         $queue = 'approval';
  617:         if ($env{'form.queue'} eq 'pending') {
  618:             $queue = 'pending';
  619:         }
  620:         %requesthash = &Apache::lonnet::dump_dom($namespace,$cdom,'_'.$queue);
  621:         my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
  622:         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
  623:             if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') { 
  624:                 $notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
  625:             }
  626:         }
  627:         $approvalmsg{'course'} = 
  628:                         [{
  629:                             mt => 'Your course request has been approved.',
  630:                         },
  631:                         {
  632:                             mt   => 'Visit [_1] to log-in and access the course',
  633:                             args => [],
  634:                         }];
  635:         $rejectionmsg{'course'} =
  636:                         [{
  637:                             mt => 'Your course request has not been approved.',
  638:                         }];
  639: 
  640:         $approvalmsg{'community'} = 
  641:                         [{
  642:                             mt => 'Your community request has been approved.',
  643:                         },
  644:                         {
  645:                             mt   => 'Visit [_1] to log-in and access the community',
  646:                             args => [],
  647:                         }];
  648: 
  649:         $rejectionmsg{'community'} = 
  650:                         [{
  651:                             mt => 'Your community request has not been approved.',
  652:                         }];
  653: 
  654:         %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
  655:         my @roles = &Apache::lonuserutils::roles_by_context('course');
  656:         foreach my $role (@roles) {
  657:             $courseroles{$role}=&Apache::lonnet::plaintext($role,'Course');
  658:         }
  659:         foreach my $role (@roles) {
  660:             $communityroles{$role}=&Apache::lonnet::plaintext($role,'Community');
  661:         }
  662:     }
  663:     foreach my $item (sort {$a <=> $b} @approvals) {
  664:         if ($context eq 'course') {
  665:             my ($num,$uname,$udom,$usec) = split(/:/,$item);
  666:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
  667:             if ($uhome ne 'no_host') {
  668:                 if (exists($requesthash{$uname.':'.$udom})) {
  669:                     if (exists($classlist->{$uname.':'.$udom})) {
  670:                         if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
  671:                             if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
  672:                                 ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
  673:                                 push(@existing,$uname.':'.$udom);
  674:                                 next;
  675:                             }
  676:                         }
  677:                     }
  678:                 } else {
  679:                     push(@missingreq,$uname.':'.$udom);
  680:                     next;
  681:                 }
  682:                 if (!grep(/^\Q$item\E$/,@rejections)) {
  683:                     if ($limit eq 'allstudents') {
  684:                         if ($stucounts->{$limit} >= $cap) {
  685:                             push(@limitexceeded,$uname.':'.$udom);
  686:                             last;
  687:                         }
  688:                     } elsif ($limit eq 'selfenrolled') {
  689:                         if ($stucounts->{$limit} >= $cap) {
  690:                             push(@limitexceeded,$uname.':'.$udom);
  691:                             last;
  692:                         }
  693:                     }
  694:                     my $result =
  695:                         &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
  696:                     if ($result eq 'ok') {
  697:                         push(@completed,$uname.':'.$udom);
  698:                         $stucounts->{'allstudents'} ++;
  699:                         $stucounts->{'selfenrolled'} ++;
  700:                         &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
  701:                                        $cid,$coursedesc,$now,$beneficiary,$sender,
  702:                                        undef,undef,$crstype);
  703:                         my %userrequest = (
  704:                             $cdom.'_'.$cnum => {
  705:                                 timestamp   => $now,
  706:                                 section     => $usec,
  707:                                 adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  708:                                 status      => 'approved',
  709:                             }
  710:                         );
  711:                         my $userresult =
  712:                             &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
  713:                         if ($userresult ne 'ok') {
  714:                             push(@warn_approves,$uname.':'.$udom);
  715:                         }
  716:                     } else {
  717:                         push(@processing_errors,$uname.':'.$udom);
  718:                     }
  719:                 }
  720:             } else {
  721:                 push(@invalidusers,$uname.':'.$udom);
  722:             }
  723:         } elsif ($context eq 'requestauthor') {
  724:             my ($num,$uname) = split(/:/,$item);
  725:             my $uhome = &Apache::lonnet::homeserver($uname,$cdom);
  726:             if ($uhome ne 'no_host') {
  727:                 my ($user_is_adv,$user_is_author) = &Apache::lonnet::is_advanced_user($cdom,$uname);
  728:                 if ($user_is_author) {
  729:                     push(@existing,$uname);
  730:                 } elsif (&Apache::lonnet::usertools_access($uname,$cdom,'requestauthor',
  731:                                                            undef,'requestauthor')) {
  732:                     if (&Apache::lonnet::allowed('cau',$cdom)) {
  733:                         if (&Apache::lonnet::assignrole($cdom,$uname,'/'.$cdom.'/','au',undef,time,undef,undef,'requestauthor') eq 'ok') {
  734:                             push(@completed,$uname);
  735:                             unless (&Apache::lonnet::del_dom($namespace,[$uname.'_approval'],$cdom) eq 'ok') {
  736:                                 push(@warn_dels,$uname);
  737:                             }
  738:                             &send_selfserve_notification($uname.':'.$cdom,
  739:                                                          $approvedmsg,undef,undef,$now,
  740:                                                          $beneficiary,$sender);
  741:                             my %userrequest = (
  742:                                 author => {
  743:                                           timestamp   => $now,
  744:                                           adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  745:                                           status      => 'approved',
  746:                                         },
  747:                                 author_status => 'approved',
  748:                             );
  749:                             my $userresult =
  750:                                 &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$uname);
  751:                             if ($userresult ne 'ok') {
  752:                                 push(@warn_approves,$item);
  753:                             }
  754:                         } else {
  755:                             push(@processing_errors,$uname);
  756:                         }
  757:                     } else {
  758:                         push(@nopermissions,$uname);
  759:                     }
  760:                 } else {
  761:                     push(@nopermissions,$uname);
  762:                 }
  763:             } else {
  764:                 push(@invalidusers,$uname.':'.$cdom);
  765:             }
  766:             push(@toremove,(@invalidusers,@nopermissions));
  767:         } else {
  768:             my ($num,$cnum) = split(':',$item);
  769:             if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
  770:                 if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  771:                     my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
  772:                     my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
  773:                     $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
  774:                     my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
  775:                     my $longroles = \%courseroles;
  776:                     if ($crstype eq 'community') {
  777:                         $longroles = \%communityroles;
  778:                     }
  779:                     my $cancreate;
  780:                     if ($cdom eq $ownerdom) {
  781:                         if (&Apache::lonnet::usertools_access($ownername,$ownerdom,$crstype,
  782:                                                               undef,'requestcourses')) {
  783:                             $cancreate = 1;
  784:                         }
  785:                     } else {
  786:                         my %userenv = &Apache::lonnet::userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.$crstype);
  787:                         if ($userenv{'reqcrsotherdom.'.$crstype}) {
  788:                             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
  789:                             if (grep(/^\Q$cdom\E:/,@doms)) {
  790:                                 $cancreate = 1;
  791:                             }
  792:                         }
  793:                     }
  794:                     if ($cancreate) {
  795:                         my $requestkey = $cdom.'_'.$cnum;
  796:                         my %history = 
  797:                             &Apache::lonnet::restore($requestkey,'courserequests',
  798:                                                      $ownerdom,$ownername);
  799:                         if ((ref($history{'details'}) eq 'HASH') && 
  800:                             ($history{'disposition'} eq $queue)) {
  801:                             my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,$code,%customitems);
  802:                             my $fullname = '';
  803:                             my $inprocess = &Apache::lonnet::auto_crsreq_update($cdom,$cnum,$crstype,'process',$ownername,
  804:                                                                                 $ownerdom,$fullname,$coursedesc);
  805:                             if (ref($inprocess) eq 'HASH') {
  806:                                 foreach my $key (keys(%{$inprocess})) {
  807:                                     if (exists($history{'details'}{$key})) { 
  808:                                         $customitems{$key} = $history{'details'}{$key};
  809:                                     }
  810:                                 }
  811:                             }
  812:                             my ($result,$postprocess) = &course_creation($cdom,$cnum,$context,$history{'details'},\$logmsg,
  813:                                                         \$newusermsg,\$addresult,\$enrollcount,
  814:                                                         \$response,\$keysmsg,\%domdefs,$longroles,\$code,\%customitems);
  815:                             if ($result eq 'created') {
  816:                                 if ($crstype eq 'community') {
  817:                                     $approvedmsg = $approvalmsg{'community'};
  818:                                 } else {
  819:                                     $approvedmsg = $approvalmsg{'course'};
  820:                                 }
  821:                                 my $firsturl = &course_portal_url($cnum,$cdom);
  822:                                 if (ref($approvedmsg) eq 'ARRAY') {
  823:                                     if (ref($approvedmsg->[1]) eq 'HASH') {
  824:                                         $approvedmsg->[1]->{'args'} = [$firsturl];
  825:                                     }
  826:                                     if ($code) {
  827:                                         push(@{$approvedmsg},
  828:                                             {
  829:                                               mt   => 'Students can automatically select your course by entering this code: [_1]',
  830:                                               args => [$code],
  831:                                             });
  832:                                         $codes{$cnum} = $code;
  833:                                     }
  834:                                     if (ref($postprocess) eq 'HASH') {
  835:                                         if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
  836:                                             foreach my $item (@{$postprocess->{'createdmsg'}}) {
  837:                                                 if (ref($item) eq 'HASH') {
  838:                                                     if ($item->{'mt'} ne '') {
  839:                                                         push(@{$approvedmsg},$item);
  840:                                                     }
  841:                                                 }
  842:                                             }
  843:                                         }
  844:                                     }
  845:                                 }
  846:                                 push(@completed,$cnum);
  847:                                 
  848:                                 unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
  849:                                     push(@warn_dels,$cnum);
  850:                                 }
  851:                                 &send_selfserve_notification($ownername.':'.$ownerdom,
  852:                                               $approvedmsg,$cid,$coursedesc,$now,
  853:                                               $beneficiary,$sender,undef,undef,$crstype);
  854:                                 my %reqhash = (
  855:                                                 reqtime     => $history{'reqtime'},
  856:                                                 crstype     => $history{'crstype'},
  857:                                                 details     => $history{'details'},
  858:                                                 disposition => $history{'disposition'},
  859:                                                 status      => 'created',
  860:                                                 adjudicator => $env{'user.name'}.':'.
  861:                                                                $env{'user.domain'},
  862:                                               );
  863:                                 my $userresult =
  864:                                     &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
  865:                                                    'courserequests',$ownerdom,$ownername);
  866:                                 if ($userresult eq 'ok') {
  867:                                     my %status = (
  868:                                                    'status:'.$cdom.':'.$cnum => 'created'
  869:                                                  );
  870:                                     my $statusresult = 
  871:                                         &Apache::lonnet::put('courserequests',\%status,
  872:                                                              $ownerdom,$ownername);
  873:                                     if ($statusresult ne 'ok') {
  874:                                         push(@warn_approves,$cnum);
  875:                                     }
  876:                                 }
  877:                                 if ($userresult ne 'ok') {
  878:                                     push(@warn_approves,$cnum);
  879:                                 }
  880:                             } else {
  881:                                 push(@processing_errors,$cnum);
  882:                             }
  883:                         } else {
  884:                             push(@processing_errors,$cnum);
  885:                         }
  886:                     } else {
  887:                         push(@nopermissions,$cnum);
  888:                     }
  889:                 } else {
  890:                     push(@existing,$cnum);
  891:                 }
  892:             } else {
  893:                 push(@missingreq,$cnum);
  894:             }
  895:         }
  896:     }
  897:     my @changes = (@completed,@rejections);
  898:     if ($context eq 'domain') {
  899:         @changes = map {$_.'_'.$queue} (@changes);
  900:     } elsif ($context eq 'requestauthor') {
  901:         @changes = map {$_.'_approval'} (@changes);
  902:     }
  903:     if (@rejections) {
  904:         foreach my $item (@rejections) {
  905:             if (($context eq 'course') || ($context eq 'requestauthor')) {
  906:                 my ($user,$uname,$udom,%userrequest,$key);
  907:                 if ($context eq 'requestauthor') {
  908:                     $uname = $item;
  909:                     $udom = $cdom;
  910:                     $user = $uname.':'.$udom;
  911:                     $key = 'author';
  912:                 } else {
  913:                     $user = $item;
  914:                     ($uname,$udom) = split(/:/,$user);
  915:                     $key = $cdom.'_'.$cnum;
  916:                 }
  917:                 &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
  918:                                              $now,$beneficiary,$sender,undef,undef,
  919:                                              $crstype);
  920:                 %userrequest = (
  921:                     $key => {
  922:                         timestamp   => $now,
  923:                         adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  924:                         status      => 'rejection',
  925:                     }
  926:                 );
  927:                 if ($context eq 'requestauthor') {
  928:                     $userrequest{'author_status'} = 'rejection';  
  929:                 }
  930:                 my $userresult =
  931:                     &Apache::lonnet::put('requestauthor',\%userrequest,$udom,$uname);
  932:                 if ($userresult ne 'ok') {
  933:                     push(@warn_rejects,$item);
  934:                 }
  935:             } else {
  936:                 my $cnum = $item;
  937:                 if (ref($requesthash{$cnum.'_'.$queue}) eq 'HASH') {
  938:                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  939:                         my $requestkey = $cdom.'_'.$cnum;
  940:                         my $ownername = $requesthash{$cnum.'_'.$queue}{'ownername'};
  941:                         my $ownerdom = $requesthash{$cnum.'_'.$queue}{'ownerdom'};
  942:                         my $coursedesc = $requesthash{$cnum.'_'.$queue}{'description'};
  943:                         $crstype = $requesthash{$cnum.'_'.$queue}{'crstype'};
  944:                         if ($crstype eq 'community') {
  945:                             $rejectedmsg = $rejectionmsg{'community'};
  946:                         } else {
  947:                             $rejectedmsg = $rejectionmsg{'course'};
  948:                         }
  949:                         &send_selfserve_notification($ownername.':'.$ownerdom,$rejectedmsg,
  950:                                                      $cid,$coursedesc,$now,$beneficiary,
  951:                                                      $sender,undef,undef,$crstype);
  952:                         my %history =
  953:                             &Apache::lonnet::restore($requestkey,'courserequests',
  954:                                                      $ownerdom,$ownername);
  955:                         if ((ref($history{'details'}) eq 'HASH') &&
  956:                             ($history{'disposition'} eq $queue)) {
  957:                             my %reqhash = (
  958:                                             reqtime     => $history{'reqtime'},
  959:                                             crstype     => $history{'crstype'},
  960:                                             details     => $history{'details'},
  961:                                             disposition => $history{'disposition'},
  962:                                             status      => 'rejected',
  963:                                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
  964:                                               );
  965:                             my $userresult =
  966:                                 &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
  967:                                                 'courserequests',$ownerdom,$ownername);
  968:                             if ($userresult eq 'ok') {
  969:                                 my %status = (
  970:                                                'status:'.$cdom.':'.$cnum => 'rejected'
  971:                                              );
  972:                                 my $statusresult =
  973:                                     &Apache::lonnet::put('courserequests',\%status,
  974:                                                          $ownerdom,$ownername);
  975:                                 if ($statusresult ne 'ok') {
  976:                                     push(@warn_rejects,$cnum);
  977:                                 }
  978:                             } else {
  979:                                 push(@warn_rejects,$cnum);
  980:                             }
  981:                             unless (&Apache::lonnet::del_dom($namespace,[$cnum.'_'.$queue],$cdom) eq 'ok') {
  982:                                 push(@warn_dels,$cnum);
  983:                             }
  984:                         } else {
  985:                             push(@warn_rejects,$cnum);
  986:                         }
  987:                     } else {
  988:                         push(@existing,$cnum);
  989:                     }
  990:                 } else {
  991:                     push(@rejectionerrors,$cnum);
  992:                 }
  993:             }
  994:         }
  995:     }
  996:     if (@toremove) {
  997:         foreach my $item (@toremove) {
  998:             my %userrequest = (
  999:                 author => {
 1000:                             timestamp   => $now,
 1001:                             adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
 1002:                             status      => 'deleted',
 1003:                           },
 1004:                           author_status => 'deleted',
 1005:             );
 1006:             &Apache::lonnet::put('requestauthor',\%userrequest,$cdom,$item);
 1007:         }
 1008:         @toremove = map {$_.'_approval'} (@toremove);
 1009:         my $delresult = &Apache::lonnet::del_dom($namespace,\@toremove,$cdom);
 1010:     }
 1011:     if (@changes) {
 1012:         my $delresult;
 1013:         if ($context eq 'course') {
 1014:             $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
 1015:         } else {
 1016:             $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
 1017:         }
 1018:         if ($delresult eq 'ok') {
 1019:             my $namelink =
 1020:                 &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
 1021:             my ($chgmsg,$approvedlist,$rejectedlist);
 1022:             if ($context eq 'course') {
 1023:                 $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
 1024:                 if (@completed) {
 1025:                     $approvedlist = join("\n",@completed);
 1026:                     if ($crstype eq 'community') {
 1027:                         $output .= '<p>'.&mt('The following were enrolled in the community:').'<ul>';
 1028:                     } else {
 1029:                         $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
 1030:                     }
 1031:                     foreach my $user (@completed) {
 1032:                         my ($uname,$udom) = split(/:/,$user);
 1033:                         my $userlink =
 1034:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
 1035:                         $output .= '<li>'.$userlink.'</li>';
 1036:                     }
 1037:                     $output .= '</ul></p>';
 1038:                 }
 1039:                 if (@rejections) {
 1040:                     $rejectedlist = join("\n",@rejections);
 1041:                     $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
 1042:                     foreach my $user (@rejections) {
 1043:                         $output .= '<li>'.$user.'</li>';
 1044:                     }
 1045:                     $output .= '</ul></p>';
 1046:                 }
 1047:                 if ($notifylist ne '') {
 1048:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
 1049:                                                  $now,'coursemanagers',$sender,
 1050:                                                  $approvedlist,$rejectedlist,$crstype);
 1051:                 }
 1052:             } elsif ($context eq 'requestauthor') {
 1053:                 $chgmsg = "'Action was taken on the following authoring space requests by [_1].',$namelink";
 1054:                 if (@completed) {
 1055:                     $approvedlist = join("\n",@completed);
 1056:                     $output .= '<p>'.&mt('The following requests were approved:').'<ul>';
 1057:                     foreach my $uname (@completed) {
 1058:                         my $userlink =
 1059:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1060:                         $output .= '<li>'.$userlink.'</li>';
 1061:                         
 1062:                     }
 1063:                     $output .= '</ul></p>';
 1064:                 }
 1065:                 if (@rejections) {
 1066:                     $rejectedlist = join("\n",@rejections);
 1067:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1068:                     foreach my $uname (@rejections) {
 1069:                         my $userlink =
 1070:                             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1071:                         $output .= '<li>'.$userlink.'</li>';
 1072:                     }
 1073:                     $output .= '</ul></p>';
 1074:                 }
 1075:                 if ($notifylist ne '') {
 1076:                     &send_selfserve_notification($notifylist,$chgmsg,undef,$domdesc,
 1077:                                                  $now,'authormanagers',$sender,
 1078:                                                  $approvedlist,$rejectedlist);
 1079:                 }
 1080:             } else {
 1081:                 $chgmsg = "'Action was taken on the following course and community requests by [_1].',$namelink";
 1082:                 if (@completed) {
 1083:                     $approvedlist = join("\n",@completed);
 1084:                     $output .= '<p>'.&mt('The following courses/communities were created:').'<ul>';
 1085:                     foreach my $cnum (@completed) {
 1086:                         my $showcourse;
 1087:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1088:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1089:                         } else {
 1090:                             $showcourse = $cnum;
 1091:                         }
 1092:                         my $syllabuslink =
 1093:                             &Apache::loncommon::syllabuswrapper($showcourse,$cnum,$cdom);
 1094:                         if ($codes{$cnum}) {
 1095:                             $syllabuslink .= &mt('Unique code: [_1]',$codes{$cnum});
 1096:                         }
 1097:                         $output .= '<li>'.$syllabuslink.'</li>';
 1098:                     }
 1099:                     $output .= '</ul></p>';
 1100:                 }
 1101:                 if (@rejections) {
 1102:                     $rejectedlist = join("\n",@rejections);
 1103:                     $output .= '<p>'.&mt('The following requests were rejected:').'<ul>';
 1104:                     foreach my $cnum (@rejections) {
 1105:                         my $showcourse;
 1106:                         if (ref($requesthash{$cnum.'_'.$queue})) {
 1107:                             $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1108:                         } else {
 1109:                             $showcourse = $cnum;
 1110:                         }
 1111:                         $output .= '<li>'.$showcourse.'</li>';
 1112:                     }
 1113:                     $output .= '</ul></p>';
 1114:                 }
 1115:                 if ($notifylist ne '') {
 1116:                     &send_selfserve_notification($notifylist,$chgmsg,$cid,$domdesc,
 1117:                                                  $now,'domainmanagers',$sender,
 1118:                                                  $approvedlist,$rejectedlist,$crstype);
 1119:                 }
 1120:             }
 1121:         }
 1122:     }
 1123:     if (@existing) {
 1124:         if ($context eq 'course') {
 1125:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
 1126:             foreach my $user (@existing) {
 1127:                 $output .= '<li>'.$user.'</li>';
 1128:             }
 1129:             $output .= '</ul></p>';
 1130:         } elsif ($context eq 'requestauthor') {
 1131:             $output .= '<p>'.&mt('Authoring space requests from the following users were deleted because one already exists:').'<ul>';
 1132:             foreach my $uname (@existing) {
 1133:                 my $userlink =
 1134:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1135:                 $output .= '<li>'.$userlink.'</li>';
 1136:             }
 1137:             $output .= '</ul></p>';
 1138:         } else {
 1139:             $output .= '<p>'.&mt('The following course/community creation requests were deleted because the course or community has already been created:').'<ul>';
 1140:             foreach my $cnum (@existing) {
 1141:                 my $showcourse;
 1142:                 my %coursehash = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 1143:                 if ($coursehash{'description'} ne '') {
 1144:                     $showcourse = $coursehash{'description'};
 1145:                 } else {
 1146:                     $showcourse = $cnum;
 1147:                 }
 1148:                 $output .= '<li>'.$showcourse.'</li>';
 1149:             }
 1150:             $output .= '</ul></p>';
 1151:         }
 1152:     }
 1153:     if (@missingreq) {
 1154:         if ($context eq 'course') {
 1155:             $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
 1156:             foreach my $user (@missingreq) {
 1157:                 $output .= '<li>'.$user.'</li>';
 1158:             }
 1159:             $output .= '</ul></p>';
 1160:         } elsif ($context eq 'requestauthor') {
 1161:             $output .= '<p>'.&mt('The following requests were ignored because the request is no longer in the queue:').'<ul>';
 1162:             foreach my $uname (@missingreq) {
 1163:                 my $userlink =
 1164:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1165:                 $output .= '<li>'.$userlink.'</li>';
 1166:             }
 1167:             $output .= '</ul></p>';
 1168:         } else {
 1169:             $output .= '<p>'.&mt('The following course/community creation requests were ignored because the request is no longer in the queue:').'<ul>';
 1170:             foreach my $cnum (@missingreq) {
 1171:                 $output .= '<li>'.$cnum.'</li>';
 1172:             }
 1173:             $output .= '</ul></p>';
 1174:         }
 1175:     }
 1176:     if (@invalidusers) {
 1177:         if ($context eq 'course') {
 1178:             $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1179:             foreach my $user (@invalidusers) {
 1180:                 $output .= '<li>'.$user.'</li>';
 1181:             }
 1182:             $output .= '</ul></p>';
 1183:         } elsif ($context eq 'requestauthor') {
 1184:             $output .= '<p>'.&mt('The following authoring space requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
 1185:             foreach my $uname (@invalidusers) {
 1186:                 my $userlink =
 1187:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1188:                 $output .= '<li>'.$userlink.'</li>';
 1189:             }
 1190:             $output .= '</ul></p>';
 1191:         }
 1192:     }
 1193:     if (@limitexceeded) {
 1194:         if ($context eq 'course') {
 1195:             $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
 1196:             foreach my $user (@limitexceeded) {
 1197:                 $output .= '<li>'.$user.'</li>';
 1198:             }
 1199:             $output .= '</ul></p>';
 1200:         }
 1201:     }
 1202:     if (@nopermissions) {
 1203:         if ($context eq 'course') {
 1204:             $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>';
 1205:             foreach my $cnum (@nopermissions) {
 1206:                 my $showcourse;
 1207:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1208:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1209:                 } else {
 1210:                     $showcourse = $cnum;
 1211:                 }
 1212:                 $output .= '<li>'.$showcourse.'</li>';
 1213:             }
 1214:             $output .= '</ul></p>';
 1215:         } elsif ($context eq 'requestauthor') {
 1216:             $output .= '<p>'.&mt('The following requests could not be processed because the requestor does not have rights to request an authoring space:').'<ul>';
 1217:             foreach my $uname (@nopermissions) {
 1218:                 my $userlink =
 1219:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1220:                 $output .= '<li>'.$userlink.'</li>';
 1221:             }
 1222:             $output .= '</ul></p>';
 1223:         }
 1224:     }
 1225:     if (@processing_errors) {
 1226:         if ($context eq 'course') {
 1227:             $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
 1228:             foreach my $user (@processing_errors) {
 1229:                 $output .= '<li>'.$user.'</li>';
 1230:             }
 1231:             $output .= '</ul></p>';
 1232:         } elsif ($context eq 'requestauthor') {
 1233:             $output .= '<p>'.&mt('The following requests could not be processed because an error occurred:').'<ul>';
 1234:             foreach my $uname (@processing_errors) {
 1235:                 my $userlink =
 1236:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1237:                 $output .= '<li>'.$userlink.'</li>';
 1238:             }
 1239:             $output .= '</ul></p>';
 1240:         } else {
 1241:             $output .= '<p>'.&mt('The following course/community creation requests could not be processed because an error occurred:').'<ul>';
 1242:             foreach my $cnum (@processing_errors) {
 1243:                 my $showcourse;
 1244:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1245:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1246:                 } else {
 1247:                     $showcourse = $cnum;
 1248:                 }
 1249:                 $output .= '<li>'.$showcourse.'</li>';
 1250:             }
 1251:             $output .= '</ul></p>';
 1252:         }
 1253:     }
 1254:     if (@rejectionerrors) {
 1255:         $output .= '<p>'.&mt('The following course/community creation request rejections could not be fully processed because an error occurred:').'<ul>';
 1256:         foreach my $cnum (@rejectionerrors) {
 1257:             my $showcourse;
 1258:             if (ref($requesthash{$cnum.'_'.$queue})) {
 1259:                 $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1260:             } else {
 1261:                 $showcourse = $cnum;
 1262:             }
 1263:             $output .= '<li>'.$showcourse.'</li>';
 1264:         }
 1265:         $output .= '</ul></p>';
 1266:     }
 1267:     if (@warn_approves || @warn_rejects) {
 1268:         if ($context eq 'course') {
 1269:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
 1270:             foreach my $user (@warn_approves) {
 1271:                 $output .= '<li>'.$user.'</li>';
 1272:             }
 1273:             $output .= '</ul></p>';
 1274:         } elsif ($context eq 'requestauthor') {
 1275:             $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own author request record:").'<ul>';
 1276:             foreach my $uname (@warn_approves,@warn_rejects) {
 1277:                 my $userlink =
 1278:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1279:                 $output .= '<li>'.$userlink.'</li>';
 1280:             }
 1281:             $output .= '</ul></p>';
 1282:         } else {
 1283:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when updating the requestor's own requests record:").'<ul>';
 1284:             foreach my $cnum (@warn_approves,@warn_rejects) {
 1285:                 my $showcourse;
 1286:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1287:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1288:                 } else {
 1289:                     $showcourse = $cnum;
 1290:                 }
 1291:                 $output .= '<li>'.$showcourse.'</li>';
 1292:             }
 1293:             $output .= '</ul></p>';
 1294:         }
 1295:     }
 1296:     if (@warn_dels) {
 1297:         if ($context eq 'requestauthor') {
 1298:             $output .= '<p>'.&mt("For the following requests an error occurred when removing the request from the queue:").'<ul>';
 1299:             foreach my $uname (@warn_dels) {
 1300:                 my $userlink =
 1301:                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$cdom),$uname,$cdom);
 1302:                 $output .= '<li>'.$userlink.'</li>';
 1303:             }
 1304:             $output .= '</ul></p>';
 1305:         } else {
 1306:             $output .= '<p>'.&mt("For the following course/community requests an error occurred when removing requests from the pending queue:").'<ul>';
 1307:             foreach my $cnum (@warn_dels) {
 1308:                 my $showcourse;
 1309:                 if (ref($requesthash{$cnum.'_'.$queue})) {
 1310:                     $showcourse = $requesthash{$cnum.'_'.$queue}{'description'};
 1311:                 } else {
 1312:                     $showcourse = $cnum;
 1313:                 }
 1314:                 $output .= '<li>'.$showcourse.'</li>';
 1315:             }
 1316:             $output .= '</ul></p>';
 1317:         }
 1318:     }
 1319:     return $output;
 1320: }
 1321: 
 1322: sub course_portal_url {
 1323:     my ($cnum,$cdom) = @_;
 1324:     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
 1325:     my $hostname = &Apache::lonnet::hostname($chome);
 1326:     my $protocol = $Apache::lonnet::protocol{$chome};
 1327:     $protocol = 'http' if ($protocol ne 'https');
 1328:     my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
 1329:     my $firsturl;
 1330:     if ($domdefaults{'portal_def'}) {
 1331:         $firsturl = $domdefaults{'portal_def'};
 1332:     } else {
 1333:         $firsturl = $protocol.'://'.$hostname;
 1334:     }
 1335:     return $firsturl;
 1336: }
 1337: 
 1338: sub get_student_counts {
 1339:     my ($cdom,$cnum) = @_;
 1340:     my (%idx,%stucounts);
 1341:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
 1342:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
 1343:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
 1344:     while (my ($student,$data) = each(%$classlist)) {
 1345:         if (($data->[$idx{'status'}] eq 'Active') ||
 1346:             ($data->[$idx{'status'}] eq 'Future')) {
 1347:             if ($data->[$idx{'type'}] eq 'selfenroll') {
 1348:                 $stucounts{'selfenroll'} ++;
 1349:             }
 1350:             $stucounts{'allstudents'} ++;
 1351:         }
 1352:     }
 1353:     return (\%stucounts,\%idx,$classlist);
 1354: }
 1355: 
 1356: sub course_creation {
 1357:     my ($dom,$cnum,$context,$details,$logmsg,$newusermsg,$addresult,$enrollcount,$output,
 1358:         $keysmsg,$domdefs,$longroles,$coderef,$customhash) =  @_;
 1359:     unless ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH') && 
 1360:             (ref($longroles) eq 'HASH')) {
 1361:         return 'error: Invalid request';
 1362:     }
 1363:     my ($result,$ownername,$ownerdom);
 1364:     my $crstype = $details->{'crstype'};
 1365:     my $coursedesc = $details->{'cdescr'};
 1366:     my %domconfig = &Apache::lonnet::get_dom('configuration',['requestauthor'],$dom);
 1367:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 1368:         if (ref($domconfig{'requestcourses'}{'uniquecode'}) eq 'HASH') {
 1369:             if ($domconfig{'requestcourses'}{'uniquecode'}{$crstype}) {
 1370:                 $details->{'uniquecode'} = 1;
 1371:             }
 1372:         }
 1373:     }
 1374:     if ($context eq 'domain') {
 1375:         $ownername = $details->{'owner'};
 1376:         $ownerdom  = $details->{'domain'};
 1377:     } else {
 1378:         $ownername = $env{'user.name'};
 1379:         $ownerdom  = $env{'user.domain'};
 1380:     }
 1381:     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
 1382:     my $owneremail;
 1383:     my %emails = &Apache::loncommon::getemails($ownername,$ownerdom);
 1384:     foreach my $email ('permanentemail','critnotification','notification') {
 1385:         $owneremail = $emails{$email};
 1386:         last if ($owneremail ne '');
 1387:     }
 1388:     my %reqdetails = &build_batchcreatehash($dom,$context,$details,$owneremail,$domdefs);
 1389:     my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'requestcourses',
 1390:                   \%reqdetails,$longroles,$logmsg,$newusermsg,$addresult,
 1391:                   $enrollcount,$output,$keysmsg,$ownerdom,$ownername,$cnum,$crstype,$coderef);
 1392:     my $postprocess;
 1393:     if ($cid eq "/$dom/$cnum") {
 1394:         $result = 'created';
 1395:         my $code;
 1396:         if (ref($coderef)) {
 1397:             $code = $$coderef;
 1398:         }
 1399:         $postprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,$result,$ownername,
 1400:                                                            $ownerdom,$fullname,$coursedesc,$code,$customhash);
 1401:     } else {
 1402:         $result = 'error: '.$cid;
 1403:     }
 1404:     return ($result,$postprocess);
 1405: }
 1406: 
 1407: sub build_batchcreatehash {
 1408:     my ($dom,$context,$details,$owneremail,$domdefs) = @_;
 1409:     my %batchhash;
 1410:     my @items = qw{owner domain coursehome clonecrs clonedom datemode dateshift enrollstart enrollend accessstart accessend sections crosslists users uniquecode};
 1411:     if ((ref($details) eq 'HASH') && (ref($domdefs) eq 'HASH')) {
 1412:         my $emailenc = &escape($owneremail);
 1413:         my $owner = $details->{'owner'}.':'.$details->{'domain'};
 1414:         foreach my $item (@items) {
 1415:             $batchhash{$item} = $details->{$item};
 1416:         }
 1417:         $batchhash{'title'} = $details->{'cdescr'};
 1418:         $batchhash{'coursecode'} = $details->{'instcode'};
 1419:         if ($domdefs->{'officialcredits'} || $domdefs->{'unofficialcredits'}) {
 1420:             $batchhash{'defaultcredits'} = $details->{'defaultcredits'};
 1421:         }
 1422:         $batchhash{'emailenc'} = $emailenc;
 1423:         $batchhash{'adds'} = $details->{'autoadds'};
 1424:         $batchhash{'drops'} = $details->{'autodrops'};
 1425:         $batchhash{'authtype'} = $domdefs->{'auth_def'};
 1426:         $batchhash{'authparam'} = $domdefs->{'auth_arg_def'};
 1427:         if ($details->{'crstype'} eq 'community') {
 1428:             $batchhash{'crstype'} = 'Community';
 1429:         } else {
 1430:             if ($details->{'crstype'} eq 'textbook') {
 1431:                 if ($details->{'clonecrs'} && $details->{'clonedom'}) {
 1432:                     my %clonedfrom = &Apache::lonnet::coursedescription($details->{'clonedom'}.'_'.$details->{'clonecrs'});
 1433:                     $batchhash{'textbook'} = $clonedfrom{'description'};
 1434:                 }
 1435:             }
 1436:             $batchhash{'crstype'} = 'Course';
 1437:         }
 1438:         my ($owner_firstname,$owner_lastname);
 1439:         if ($context eq 'domain') {
 1440:             my %userenv = &Apache::lonnet::userenvironment($details->{'domain'},
 1441:                                                            $details->{'owner'},
 1442:                                                            'firstname','lastname');
 1443:             $owner_firstname = $userenv{'firstname'};
 1444:             $owner_lastname = $userenv{'lastname'};
 1445:         } else {
 1446:             $owner_firstname = $env{'environment.firstname'};
 1447:             $owner_lastname = $env{'environment.lastname'};
 1448:         }
 1449:         if (ref($details->{'personnel'}) eq 'HASH') {
 1450:             %{$batchhash{'users'}} = %{$details->{'personnel'}};
 1451:             if (ref($batchhash{'users'}) eq 'HASH') {  
 1452:                 foreach my $userkey (keys(%{$batchhash{'users'}})) {
 1453:                     if (ref($batchhash{'users'}{$userkey}) eq 'HASH') {
 1454:                         if (ref($batchhash{'users'}{$userkey}{'roles'}) eq 'ARRAY') {
 1455:                             foreach my $role (@{$batchhash{'users'}{$userkey}{'roles'}}) {
 1456:                                 my $start = '';
 1457:                                 my $end = '';
 1458:                                 if ($role eq 'st') {
 1459:                                     $start = $details->{'accessstart'};
 1460:                                     $end = $details->{'accessend'};
 1461:                                 }
 1462:                                 $batchhash{'users'}{$userkey}{$role}{'start'} = $start;
 1463:                                 $batchhash{'users'}{$userkey}{$role}{'end'} = $end;
 1464:                             } 
 1465:                         }
 1466:                     }
 1467:                 }
 1468:             }
 1469:         }
 1470:         $batchhash{'users'}{$owner}{firstname} = $owner_firstname;
 1471:         $batchhash{'users'}{$owner}{lastname} = $owner_lastname;
 1472:         $batchhash{'users'}{$owner}{emailenc} = $emailenc;
 1473:         $batchhash{'users'}{$owner}{owneremail} = $owneremail;
 1474:     }
 1475:     return %batchhash;
 1476: }
 1477: 
 1478: sub can_clone_course {
 1479:     my ($uname,$udom,$clonecrs,$clonedom,$crstype) = @_;
 1480:     my $canclone;
 1481:     my $ccrole = 'cc';
 1482:     if ($crstype eq 'community') {
 1483:         $ccrole = 'co';
 1484:     }
 1485:     my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',['active'],
 1486:                                                   [$ccrole],[$clonedom]);
 1487:     if (exists($roleshash{$clonecrs.':'.$clonedom.':'.$ccrole})) {
 1488:         $canclone = 1;
 1489:     } else {
 1490:         my %courseenv = &Apache::lonnet::userenvironment($clonedom,$clonecrs,('cloners'));
 1491:         my $cloners = $courseenv{'cloners'};
 1492:         if ($cloners ne '') {
 1493:             my @cloneable = split(',',$cloners);
 1494:             if (grep(/^\*$/,@cloneable)) {
 1495:                 $canclone = 1;
 1496:             }
 1497:             if (grep(/^\*:\Q$udom\E$/,@cloneable)) {
 1498:                 $canclone = 1;
 1499:             }
 1500:             if (grep(/^\Q$uname\E:\Q$udom\E$/,@cloneable)) {
 1501:                 $canclone = 1;
 1502:             }
 1503:         }
 1504:         unless ($canclone) {
 1505:             if (&Apache::lonnet::is_course_owner($clonedom,$clonecrs,$uname,$udom)) {
 1506:                 $canclone = 1;
 1507:             }
 1508:         }
 1509:     }
 1510:     return $canclone;
 1511: }
 1512: 
 1513: sub get_processtype {
 1514:     my ($context,$uname,$udom,$isadv,$dom,$crstype,$inststatuses,$domconfig) = @_;
 1515:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
 1516:     if ($uname eq '' || $udom eq '') {
 1517:         $uname = $env{'user.name'};
 1518:         $udom = $env{'user.domain'};
 1519:         $isadv = $env{'user.adv'};
 1520:     }
 1521:     my (%userenv,%settings,$val,@options,$envkey);
 1522:     if ($context eq 'course') {
 1523:         @options = ('autolimit','validate','approval');
 1524:         $envkey = 'requestcourses.'.$crstype;
 1525:         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
 1526:             if (ref($domconfig->{'requestcourses'}->{$crstype}) eq 'HASH') {
 1527:                 %settings = %{$domconfig->{'requestcourses'}->{$crstype}};
 1528:             }
 1529:         }
 1530:     } else {
 1531:         @options = ('automatic','approval');
 1532:         $envkey = 'requestauthor';
 1533:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') { 
 1534:             %settings = %{$domconfig->{'requestauthor'}};
 1535:         }
 1536:     }
 1537:     if (($dom eq $udom) || ($context eq 'requestauthor')) {
 1538:         %userenv =
 1539:             &Apache::lonnet::userenvironment($udom,$uname,$envkey,'inststatus');
 1540:         if ($userenv{$envkey}) {
 1541:             $val = $userenv{$envkey};
 1542:             @{$inststatuses} = ('_custom_');
 1543:         } else {
 1544:             my %alltasks;
 1545:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
 1546:                 $val = $settings{'_LC_adv'};
 1547:                 @{$inststatuses} = ('_LC_adv_');
 1548:             } else {
 1549:                 if ($userenv{'inststatus'} ne '') {
 1550:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
 1551:                 } else {
 1552:                     @{$inststatuses} = ('default');
 1553:                 }
 1554:                 foreach my $status (@{$inststatuses}) {
 1555:                     if (exists($settings{$status})) {
 1556:                         my $value = $settings{$status};
 1557:                         next unless ($value);
 1558:                         unless (exists($alltasks{$value})) {
 1559:                             if (ref($alltasks{$value}) eq 'ARRAY') {
 1560:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
 1561:                                     push(@{$alltasks{$value}},$status);
 1562:                                 }
 1563:                             } else {
 1564:                                 @{$alltasks{$value}} = ($status);
 1565:                             }
 1566:                         }
 1567:                     }
 1568:                 }
 1569:                 my $maxlimit = 0;
 1570:                 if ($context eq 'course') {
 1571:                     foreach my $key (sort(keys(%alltasks))) {
 1572:                         if ($key =~ /^autolimit=(\d*)$/) {
 1573:                             if ($1 eq '') {
 1574:                                 $val ='autolimit=';
 1575:                                 last;
 1576:                             } elsif ($1 > $maxlimit) {
 1577:                                 $maxlimit = $1;
 1578:                             }
 1579:                         }
 1580:                     }
 1581:                 }
 1582:                 if (($context eq 'requestauthor') || (!$val)) {
 1583:                     if ($context eq 'course' && $maxlimit) {
 1584:                         $val = 'autolimit='.$maxlimit;
 1585:                     } else {
 1586:                         foreach my $option (@options) {
 1587:                             if ($alltasks{$option}) {
 1588:                                 $val = $option;
 1589:                                 last;
 1590:                             }
 1591:                         }
 1592:                     }
 1593:                 }
 1594:             }
 1595:         }
 1596:     } else {
 1597:         %userenv = &Apache::lonnet::userenvironment($udom,$uname,'reqcrsotherdom.'.$crstype);
 1598:         if ($userenv{'reqcrsotherdom.'.$crstype}) {
 1599:             my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
 1600:             my $optregex = join('|',@options);
 1601:             foreach my $item (@doms) {
 1602:                 my ($extdom,$extopt) = split(':',$item);
 1603:                 if ($extdom eq $dom) {
 1604:                     if ($extopt =~ /^($optregex)(=?\d*)$/) {
 1605:                         $val = $1.$2;
 1606:                     }
 1607:                     last;
 1608:                 }
 1609:             }
 1610:             @{$inststatuses} = ('_external_');
 1611:         }
 1612:     }
 1613:     return $val;
 1614: }
 1615: 
 1616: sub queued_selfenrollment {
 1617:     my ($notitle) = @_;
 1618:     my $output;
 1619:     my %selfenrollrequests = &Apache::lonnet::dump('selfenrollrequests');
 1620:     my %reqs_by_date;
 1621:     foreach my $item (keys(%selfenrollrequests)) {
 1622:         if (ref($selfenrollrequests{$item}) eq 'HASH') {
 1623:             if ($selfenrollrequests{$item}{'status'} eq 'request') {
 1624:                 if ($selfenrollrequests{$item}{'timestamp'}) {
 1625:                     push(@{$reqs_by_date{$selfenrollrequests{$item}{'timestamp'}}},$item);
 1626:                 }
 1627:             }
 1628:         }
 1629:     }
 1630:     if (keys(%reqs_by_date)) {
 1631:         unless ($notitle) {
 1632:             $output .= '<br /><b>'.&mt('Enrollment requests pending Course Coordinator approval').'</b><br />';
 1633:         }
 1634:         $output .= &Apache::loncommon::start_data_table().
 1635:                    &Apache::loncommon::start_data_table_header_row().
 1636:                    '<th>'.&mt('Date requested').'</th><th>'.&mt('Course title').'</th>'.
 1637:                    '<th>'.&mt('User role').'</th><th>'.&mt('Section').'</th>'.
 1638:                    &Apache::loncommon::end_data_table_header_row();
 1639:         my @sorted = sort { $a <=> $b } (keys(%reqs_by_date));
 1640:         foreach my $item (@sorted) {
 1641:             if (ref($reqs_by_date{$item}) eq 'ARRAY') {
 1642:                 foreach my $crs (@{$reqs_by_date{$item}}) {
 1643:                     my %courseinfo = &Apache::lonnet::coursedescription($crs);
 1644:                     my $usec = $selfenrollrequests{$crs}{'section'};
 1645:                     my $rolename = &Apache::lonnet::plaintext('st',$courseinfo{'type'},$crs);
 1646:                     if ($usec eq '') {
 1647:                         $usec = &mt('No section');
 1648:                     }
 1649:                     $output .= &Apache::loncommon::start_data_table_row().
 1650:                                '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
 1651:                                '<td>'.$courseinfo{'description'}.'</td>'.
 1652:                                '<td>'.$rolename.'</td><td>'.$usec.'</td>'.
 1653:                                &Apache::loncommon::end_data_table_row();
 1654:                 }
 1655:             }
 1656:         }
 1657:         $output .= &Apache::loncommon::end_data_table();
 1658:     }
 1659:     return $output;
 1660: }
 1661: 
 1662: sub update_coursereq_status {
 1663:     my ($reqhash,$dom,$cnum,$reqstatus,$context,$udom,$uname) = @_;
 1664:     my ($storeresult,$statusresult,$output);
 1665:     my $requestkey = $dom.'_'.$cnum;
 1666:     if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
 1667:         $storeresult = &Apache::lonnet::store_userdata($reqhash,$requestkey,
 1668:                                                        'courserequests',$udom,$uname);
 1669:         if ($storeresult eq 'ok') {
 1670:             my %status = (
 1671:                              'status:'.$dom.':'.$cnum => $reqstatus,
 1672:                          );
 1673:             $statusresult = &Apache::lonnet::put('courserequests',\%status,$udom,$uname);
 1674:         }
 1675:     } else {
 1676:         $storeresult = 'error: invalid requestkey format';
 1677:     }
 1678:     if ($storeresult ne 'ok') {
 1679:         $output = &mt('An error occurred saving a record of the details of your request: [_1].',$storeresult);
 1680:         if ($context eq 'domain') {
 1681:             $output .= "\n";  
 1682:         } else {
 1683:             $output =  '<span class="LC_warning">'.$output.'</span><br />';
 1684:         }
 1685:         &Apache::lonnet::logthis("Error saving course request - $requestkey for $uname:$udom - $storeresult");
 1686:     } elsif ($statusresult ne 'ok') {
 1687:         $output = &mt('An error occurred saving a record of the status of your request: [_1].',$statusresult);
 1688:         if ($context eq 'domain') {
 1689:             $output .= "\n";
 1690:         } else {
 1691:             $output = '<span class="LC_warning">'.$output.'</span><br />';
 1692:         }
 1693:         &Apache::lonnet::logthis("Error saving course request status for $requestkey (for $uname:$udom) - $statusresult");
 1694:     }
 1695:     return ($storeresult,$output);
 1696: }
 1697: 
 1698: sub process_official_reqs {
 1699:     my ($context,$dom,$dcname,$dcdom) = @_;
 1700:     my $reqsnamespace = 'courserequestqueue';
 1701:     my %requesthash =
 1702:         &Apache::lonnet::dump_dom($reqsnamespace,$dom,'_pending');
 1703:     my (%newcids,%longroles,%stillpending);
 1704:     my @courseroles = ('cc','in','ta','ep','ad','st');
 1705:     foreach my $role (@courseroles) {
 1706:         $longroles{$role}=&Apache::lonnet::plaintext($role);
 1707:     }
 1708:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
 1709:     my ($output,$linefeed);
 1710:     if ($context eq 'auto') {
 1711:         $linefeed = "\n";
 1712:     } else {
 1713:         $linefeed = '<br />'."\n";
 1714:     }
 1715:     foreach my $key (keys(%requesthash)) {
 1716:         my ($cnum,$status) = split('_',$key);
 1717:         next if (&Apache::lonnet::homeserver($cnum,$dom) ne 'no_host');
 1718:         if (ref($requesthash{$key}) eq 'HASH') {
 1719:             my $ownername = $requesthash{$key}{'ownername'};
 1720:             my $ownerdom = $requesthash{$key}{'ownerdom'};
 1721:             next if (&Apache::lonnet::homeserver($ownername,$ownerdom) eq 'no_host');
 1722:             my $inststatus;
 1723:             my %userenv =
 1724:                 &Apache::lonnet::get('environment',['inststatus'],
 1725:                                      $ownerdom,$ownername);
 1726:             my ($tmp) = keys(%userenv);
 1727:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 1728:                 $inststatus = $userenv{'inststatus'};
 1729:             } else {
 1730:                 undef(%userenv);
 1731:             }
 1732:             my $reqkey = $dom.'_'.$cnum;
 1733:             my %history = &Apache::lonnet::restore($reqkey,'courserequests',
 1734:                                                    $ownerdom,$ownername);
 1735:             if (ref($history{'details'}) eq 'HASH') {
 1736:                 my $instcode = $history{'details'}{'instcode'};
 1737:                 my $crstype = $history{'details'}{'crstype'};
 1738:                 my $reqtime = $history{'details'}{'reqtime'};
 1739:                 my $cdescr = $history{'details'}{'cdescr'};
 1740:                 my @currsec;
 1741:                 my $sections = $history{'details'}{'sections'};
 1742:                 if (ref($sections) eq 'HASH') {
 1743:                     foreach my $i (sort(keys(%{$sections}))) {
 1744:                         if (ref($sections->{$i}) eq 'HASH') {
 1745:                             my $sec = $sections->{$i}{'inst'};
 1746:                             if (!grep(/^\Q$sec\E$/,@currsec)) {
 1747:                                 push(@currsec,$sec);
 1748:                             }
 1749:                         }
 1750:                     }
 1751:                 }
 1752:                 my $instseclist = join(',',@currsec);
 1753:                 my ($validationchk,$disposition,$reqstatus,$message,
 1754:                     $validation,$validationerror);
 1755:                 $validationchk =
 1756:                     &Apache::lonnet::auto_courserequest_validation($dom,
 1757:                         $ownername.':'.$ownerdom,$crstype,$inststatus,
 1758:                         $instcode,$instseclist);
 1759:                 if ($validationchk =~ /:/) {
 1760:                     ($validation,$message) = split(':',$validationchk);
 1761:                 } else {
 1762:                     $validation = $validationchk;
 1763:                 }
 1764:                 if ($validation =~ /^error(.*)$/) {
 1765:                     $disposition = 'approval';
 1766:                     $validationerror = $1;
 1767:                 } else {
 1768:                     $disposition = $validation;
 1769:                 }
 1770:                 $reqstatus = $disposition;
 1771:                 if ($disposition eq 'process') {
 1772:                     my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,$code);
 1773:                     my %customitems;
 1774:                     my $fullname = &Apache::loncommon::plainname($ownername,$ownerdom);
 1775:                     my $inprocess = &Apache::lonnet::auto_crsreq_update($dom,$cnum,$crstype,'process',$ownername,
 1776:                                                                         $ownerdom,$fullname,$cdescr);
 1777:                     if (ref($inprocess) eq 'HASH') {
 1778:                         foreach my $key (keys(%{$inprocess})) {
 1779:                             if (exists($history{'details'}{$key})) {
 1780:                                 $customitems{$key} = $history{'details'}{$key};
 1781:                             }
 1782:                         }
 1783:                     }
 1784:                     my ($result,$postprocess) = 
 1785:                         &course_creation($dom,$cnum,'domain',$history{'details'},\$logmsg,\$newusermsg,\$addresult,
 1786:                                          \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles,\$code,\%customitems);
 1787:                     if ($result eq 'created') {
 1788:                         $disposition = 'created';
 1789:                         $reqstatus = 'created';
 1790:                         my $cid = $dom.'_'.$cnum;
 1791:                         push(@{$newcids{$instcode}},$cid);
 1792:                         if ($dcname && $dcdom) {
 1793:                             my $firsturl = &course_portal_url($cnum,$dom);
 1794:                             my $beneficiary = 'pendingrequestor';
 1795:                             my $now = time;
 1796:                             my $owner = $ownername.':'.$ownerdom;
 1797:                             my $approvedmsg =
 1798:                                 [{
 1799:                                     mt => 'Your requested course: [_1], (queued pending validation) has now been created.',
 1800:                                     args => [$cdescr],
 1801:                                  },
 1802:                                  {
 1803:                                     mt   => 'Visit [_1] to log-in and access the course.',
 1804:                                     args => [$firsturl],
 1805:                                  },
 1806:                                  {
 1807:                                     mt => 'If currently logged-in to LON-CAPA, log-out and log-in again to select your new course role.'
 1808:                                  }];
 1809:                             my $sender = $dcname.':'.$dcdom;
 1810:                             if (ref($postprocess) eq 'HASH') {
 1811:                                 if (ref($postprocess->{'createdmsg'}) eq 'ARRAY') {
 1812:                                     foreach my $item (@{$postprocess->{'createdmsg'}}) {
 1813:                                         if (ref($item) eq 'HASH') {
 1814:                                              if ($item->{'mt'} ne '') {
 1815:                                                  push(@{$approvedmsg},$item);
 1816:                                              }
 1817:                                         }
 1818:                                     }
 1819:                                 }
 1820:                             }
 1821:                             &send_selfserve_notification($owner,$approvedmsg,
 1822:                                                          $cid,$cdescr,$now,
 1823:                                                          $beneficiary,$sender,
 1824:                                                          undef,undef,$crstype);
 1825:                         }
 1826:                     }
 1827:                 } elsif ($disposition eq 'rejected') {
 1828:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] rejected when validating.',$instcode,$ownername.':'.$ownerdom,$inststatus).$linefeed;
 1829:                 } elsif ($disposition eq 'approval') {
 1830:                     $output .= &mt('Queued course request for [_1] submitted by [_2] with status [_3] switched to "approval by DC" because of validation error: [_4].',$instcode,$ownername.':'.$ownerdom,$inststatus,$validationerror).$linefeed;
 1831: 
 1832:                     my $requestid = $cnum.'_'.$disposition;
 1833:                     my $request = {
 1834:                             $requestid => {
 1835:                                             timestamp   => $reqtime,
 1836:                                             crstype     => $crstype,
 1837:                                             ownername   => $ownername,
 1838:                                             ownerdom    => $ownerdom,
 1839:                                             description => $cdescr,
 1840:                                           },
 1841:                           };
 1842:                     my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,$dom);
 1843:                     unless ($putresult eq 'ok') {
 1844:                         $output .= &mt("An error occurred saving the modified course request for [_1] submitted by [_2] in the domain's courserequestqueue.db.",$instcode,$ownername.':'.$ownerdom).$linefeed;
 1845:                     }
 1846:                 } elsif ($disposition eq 'pending') {
 1847:                     my $instcode = $requesthash{$key}{'instcode'};
 1848:                     my $description = $requesthash{$key}{'description'};
 1849:                     my $timestamp = $requesthash{$key}{'timestamp'};
 1850:                     my $entry = $cnum.':'.$ownername.':'.$ownerdom.':'.
 1851:                                 $instcode.':'.$description;
 1852:                     if (ref($stillpending{$timestamp}) eq 'ARRAY') {
 1853:                         push(@{$stillpending{$timestamp}},$entry);
 1854:                     } else {
 1855:                         $stillpending{$timestamp} = [$entry];
 1856:                     }
 1857:                 }
 1858:                 unless ($disposition eq 'pending') {
 1859:                     my ($statusresult,$output) =
 1860:                         &update_coursereq_status(\%requesthash,$dom,$cnum,
 1861:                                                  $reqstatus,'domain',$ownerdom,
 1862:                                                  $ownername);
 1863:                     unless (&Apache::lonnet::del_dom($reqsnamespace,[$cnum.'_pending'],$dom) eq 'ok') {
 1864:                         $output .= &mt('An error occurred when removing the request for [_1] submitted by [_2] from the pending queue.',$instcode,$ownername.':'.$ownerdom).$linefeed;
 1865:                     }
 1866:                 }
 1867:             }
 1868:         }
 1869:     }
 1870:     foreach my $key (sort(keys(%newcids))) {
 1871:         if (ref($newcids{$key}) eq 'ARRAY') {
 1872:             $output .= "created course from queued request: $key - ".join(', ',@{$newcids{$key}}).$linefeed;
 1873:             my $newcourse = &LONCAPA::escape($key.':'.$newcids{$key});
 1874:         }
 1875:     }
 1876:     unless ($context eq 'auto') {
 1877:         if (keys(%stillpending) > 0) {
 1878:             $output .= '<form method="post" name="changequeue" action="/adm/createcourse" />'."\n".
 1879:                        '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 1880:                        '<input type="hidden" name="phase" value="requestchange" />'.
 1881:                        '<p>'.&mt('For the following requests, the requestor could [_1]not[_2] be validated as official course personnel, so the request remains in the pending queue.','<b>','</b>').'<br />'.&mt('Requests may be left in the queue, or you can manually approve or reject them.').'</p>'.
 1882:                        &build_queue_display($dom,'stillpending',\%stillpending).
 1883:                        '<br /><input type="hidden" name="queue" value="pending" />'."\n".
 1884:                        '<input type="submit" name="processqueue" value="'.&mt('Save').'" />'.
 1885:                        '</form>';
 1886:         }
 1887:     }
 1888:     return $output;
 1889: }
 1890: 
 1891: sub is_active_author {
 1892:     if ($env{'user.role.au./'.$env{'user.domain'}.'/'} =~ /^(\d*)\.(\d*)$/) {
 1893:         if ((!$1 || $1 < time) &&
 1894:             (!$2 || $2 > time)) {
 1895:             return 1;
 1896:         }
 1897:     }
 1898: }
 1899: 
 1900: sub author_prompt {
 1901:     my ($is_active_author,$offer_author);
 1902:     if ($env{'environment.canrequest.author'}) {
 1903:         unless (&is_active_author()) {
 1904:             unless (&reqauthor_check() =~ /^approval:\d+$/) {
 1905:                 $offer_author = 1;
 1906:             }
 1907:         }
 1908:     }
 1909:     return $offer_author;
 1910: }
 1911: 
 1912: sub reqauthor_check {
 1913:     my $queued = $env{'environment.requestauthorqueued'};
 1914:     my %reqauthor = &Apache::lonnet::get('requestauthor',['author_status','author'],
 1915:                                          $env{'user.domain'},$env{'user.name'});
 1916:     my $reqstatus = $reqauthor{'author_status'};
 1917:     if (($reqstatus eq '' && $queued ne '') ||
 1918:         ($env{'environment.requestauthorqueued'} !~ /^\Q$reqstatus\E/)) {
 1919:         if (ref($reqauthor{'author'}) eq 'HASH') {
 1920:             $queued = $reqstatus.':'.$reqauthor{'author'}{'timestamp'};
 1921:         } else {
 1922:             undef($queued);
 1923:         }
 1924:         &Apache::lonnet::appenv({'environment.requestauthorqueued' => $queued});
 1925:     }
 1926:     return $queued;
 1927: }
 1928: 
 1929: sub process_reqauthor {
 1930:     my ($dispositionref,$updateref) = @_;
 1931:     if (&is_active_author()) {
 1932:         return '<span class="LC_warning">'.
 1933:                 &mt('An authoring space has already been assigned to you.').'<br />'.
 1934:                 &mt('Please select the Author role from your [_1]roles page[_2].','<a href="/adm/roles">',
 1935:                 '</a>').'</span>';
 1936:     }
 1937:     unless ($env{'environment.canrequest.author'}) {
 1938:         return '<span class="LC_warning">'.
 1939:                 &mt('You do not currently have rights to request an authoring space.').'<br />'.
 1940:                 &mt('Please contact the [_1]helpdesk[_2] for assistance.','<a href="/adm/helpdesk">',
 1941:                 '</a>').'</span>';
 1942:     }
 1943:     my $queued = &reqauthor_check();
 1944:     if ($queued =~ /^approval:(\d+)$/) {
 1945:         my $timestamp = $1;
 1946:         return '<span class="LC_info">'.
 1947:                &mt('A request for authoring space submitted on [_1] is awaiting approval',
 1948:                &Apache::lonlocal::locallocaltime($timestamp)).
 1949:                '</span>';
 1950:     } elsif ($queued =~ /^approved:(\d+)$/) {
 1951:         my $timestamp = $1;   
 1952:         my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
 1953:                                                       ['active'],['au'],[$env{'user.domain'}]);
 1954:         if (keys(%roleshash) > 0) {
 1955:             return '<span class="LC_info">'.
 1956:                    &mt('A request for authoring space submitted on [_1] has been approved.',
 1957:                    &Apache::lonlocal::locallocaltime($timestamp)).
 1958:                    '</span>';
 1959:         }
 1960:     }
 1961:     my ($output,@inststatuses,%domconfig);
 1962:     %domconfig = &Apache::lonnet::get_dom('configuration',['requestauthor'],
 1963:                                           $env{'user.domain'});
 1964:     my $val = &get_processtype('requestauthor',$env{'user.name'},$env{'user.domain'},
 1965:                                $env{'user.adv'},$env{'user.domain'},undef,
 1966:                                \@inststatuses,\%domconfig);
 1967:     my $now = time;
 1968:     if ($val eq 'automatic') {
 1969:         my $start = $now-1;
 1970:         if (&Apache::lonnet::assignrole($env{'user.domain'},$env{'user.name'},'/'.$env{'user.domain'}.'/',
 1971:                                         'au',undef,$start,undef,undef,'requestauthor') eq 'ok') {
 1972:             $output = '<span class="LC_info">'.
 1973:                       &mt('Access to authoring space has been activated').'</span><br />';
 1974:                       &Apache::lonroles::update_session_roles();
 1975:             &Apache::lonnet::appenv({'user.update.time'  => $now});
 1976:             if (ref($updateref)) {
 1977:                 $$updateref = $now;
 1978:             }
 1979:             if (ref($dispositionref)) {
 1980:                 $$dispositionref = 'created';
 1981:             }
 1982:         } else {
 1983:             $output = '<span class="LC_info">'.
 1984:                       &mt('An error occurred while activating your access to authoring space');
 1985:         }
 1986:     } elsif ($val eq 'approval') {
 1987:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($env{'user.domain'});
 1988:         if (&Apache::lonnet::put('requestauthorqueue',{ $env{'user.name'}.'_'.$val => $now },
 1989:                                  $env{'user.domain'},$domconfiguser) eq 'ok') {
 1990:             my %userrequest = (
 1991:                 author => {
 1992:                             timestamp   => $now,
 1993:                             status      => $val,
 1994:                           },
 1995:                 author_status => $val,
 1996:             );
 1997:             my $req_notifylist;
 1998:             if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 1999:                 if (ref($domconfig{'requestauthor'}{'notify'}) eq 'HASH') {
 2000:                     my $req_notifylist = $domconfig{'requestauthor'}{'notify'}{'approval'};
 2001:                     if ($req_notifylist) {
 2002:                         my $fullname = &Apache::loncommon::plainname($env{'user.name'},
 2003:                                                                      $env{'user.domain'});
 2004:                         my $sender = $env{'user.name'}.':'.$env{'user.domain'};
 2005:                         my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
 2006:                         &send_selfserve_notification($req_notifylist,
 2007:                                                      "$fullname ($env{'user.name'}:$env{'user.domain'})",
 2008:                                                      undef,$domdesc,$now,'authorreq',$sender);
 2009:                     }
 2010:                 }
 2011:             }
 2012:             my $userresult =
 2013:                 &Apache::lonnet::put('requestauthor',\%userrequest,$env{'user.domain'},$env{'user.name'});
 2014:             $output = '<span class="LC_info">'.
 2015:                       &mt('Your request for authoring space has been submitted for approval.').
 2016:                       '</span>';
 2017:             &Apache::lonnet::appenv({'environment.requestauthorqueued' => $val.':'.$now});
 2018:         } else {
 2019:             $output = '<span class="LC_info">'.
 2020:                       &mt('An error occurred saving your request for authoring space.').
 2021:                       '</span>';
 2022:         }
 2023:     }
 2024:     return $output;
 2025: }
 2026: 
 2027: 1;

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