[LON-CAPA-cvs] cvs: doc /loncapafiles loncapafiles.lpml loncom/interface loncoursequeueadmin.pm loncreateuser.pm selfenroll.pm

raeburn raeburn@source.lon-capa.org
Tue, 11 Aug 2009 00:39:55 -0000


This is a MIME encoded message

--raeburn1249951195
Content-Type: text/plain

raeburn		Tue Aug 11 00:39:55 2009 EDT

  Added files:                 
    /loncom/interface	loncoursequeueadmin.pm 

  Modified files:              
    /loncom/interface	loncreateuser.pm selfenroll.pm 
    /doc/loncapafiles	loncapafiles.lpml 
  Log:
  Course requests.
  - Moving and modifying some routines used for displaying queues (oringinally created for approval/rejection of self-enroll requests to also support approval/rejection of course creation requests.  
     - &display_selfenroll_queue() moved from loncreateuser.pm to loncoursequeueadmin.pm
        (and renamed &display_queued_requests() ).
     - &update_selfenroll_queue() moved from loncreateuser.pm to loncoursequeueadmin.pm
        (and renamed &update_request_queue() ).
     - &get_student_counts() moved from loncreateuser.pm to loncoursequeueadmin.pm
  
     - &send_notification() moved from selfenroll.pm to loncoursequeueadmin.pm 
        (and renamed &send_selfserver_notification() ).
  
  
--raeburn1249951195
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090811003955.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.306 loncom/interface/loncreateuser.pm:1.307
--- loncom/interface/loncreateuser.pm:1.306	Sat Aug  8 00:36:00 2009
+++ loncom/interface/loncreateuser.pm	Tue Aug 11 00:39:45 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.306 2009/08/08 00:36:00 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.307 2009/08/11 00:39:45 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -68,7 +68,7 @@
 use Apache::lonlocal;
 use Apache::longroup;
 use Apache::lonuserutils;
-use Apache::selfenroll();
+use Apache::loncoursequeueadmin;
 use LONCAPA qw(:DEFAULT :match);
 
 my $loginscript; # piece of javascript used in two separate instances
@@ -3937,11 +3937,13 @@
         my $cid = $env{'request.course.id'};
         my $cdom = $env{'course.'.$cid.'.domain'};
         my $cnum = $env{'course.'.$cid.'.num'};
+        my $coursedesc = $env{'course.'.$cid.'.description'};
         if (!exists($env{'form.state'})) {
             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Enrollment requests',
                                                           'Course_SelfEnrollment_Approval'));
             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
-            &display_selfenroll_queue($r,$context,$permission,$cnum,$cdom);
+            $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
+                                                                       $cdom,$cnum));
         } elsif ($env{'form.state'} eq 'done') {
             &Apache::lonhtmlcommon::add_breadcrumb
             ({href=>'/adm/createuser?action=selfenrollqueue',
@@ -3949,7 +3951,8 @@
             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Enrollment result',
                                                           'Course_Self_Enrollment'));
             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
-            &update_selfenroll_queue($r,$context,$permission,$cid,$cnum,$cdom);
+            $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
+                          $cdom,$cnum,$coursedesc));
         }
         $r->print(&Apache::loncommon::end_page());
     } elsif ($env{'form.action'} eq 'changelogs') {
@@ -4630,279 +4633,6 @@
     return;
 }
 
-sub display_selfenroll_queue {
-    my ($r,$context,$permission,$cnum,$cdom) = @_;
-    my $namespace = 'selfenrollrequests';
-    my ($output,%queue_by_date);
-    my %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
-    if (keys(%requesthash) > 0) {
-        $r->print('<form method="post" name="changequeue" action="/adm/createuser" />'.
-                  '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
-                  '<input type="hidden" name="state" value="done" />'.
-                  &Apache::loncommon::start_data_table().
-                  &Apache::loncommon::start_data_table_header_row().
-                  '<th>'.&mt('Action').'</th>'.
-                  '<th>'.&mt('Requestor').'</th>'.
-                  '<th>'.&mt('Section').'</th>'.
-                  '<th>'.&mt('Date requested').'</th>'.
-                  &Apache::loncommon::end_data_table_header_row());
-        foreach my $item (keys(%requesthash)) {
-            my ($timestamp,$usec) = split(/:/,$requesthash{$item});
-            if (exists($queue_by_date{$timestamp})) {
-                if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
-                    push(@{$queue_by_date{$timestamp}},$item.':'.$usec);
-                }
-            } else {
-                @{$queue_by_date{$timestamp}} = ($item.':'.$usec);
-            }
-        }
-        my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
-        my $count = 0;
-        foreach my $item (@sortedtimes) {
-            if (ref($queue_by_date{$item}) eq 'ARRAY') {
-                foreach my $request (sort(@{$queue_by_date{$item}})) {
-                    my ($puname,$pudom,$pusec) = split(/:/,$request);
-                    my $showsec = $pusec;
-                    if ($showsec eq '') {
-                        $showsec = &mt('none');
-                    }
-                    my $namelink = &Apache::loncommon::aboutmewrapper(
-                                     &Apache::loncommon::plainname($puname,$pudom),
-                                     $puname,$pudom);
-                    $r->print(&Apache::loncommon::start_data_table_row().
-                              '<td><span class="LC_nobreak"><label>'.
-                              '<input type="checkbox" value="'.$count.':'.$puname.':'.$pudom.':'.$pusec.'" name="approvereq" />'.&mt('Approve').'</label></span><br />'.
-                              '<span class="LC_nobreak"><label>'.
-                              '<input type="checkbox" value="'.$puname.':'.$pudom.'" name="rejectreq" />'.&mt('Reject').'</label></span><br /></td>'.
-                              '<td>'.$namelink.'</td>'.
-                              '<td>'.$showsec.'</td>'.
-                              '<td>'.&Apache::lonlocal::locallocaltime($item).'</td>'.
-                              &Apache::loncommon::end_data_table_row());
-                    $count ++;
-                }
-            }
-        }
-        $r->print(&Apache::loncommon::end_data_table().
-                  '<input type="submit" name="processqueue" value="'.&mt('Save').'" /></form>');
-    } else {
-        $r->print(&mt('There are currently no enrollment requests.'));  
-    }
-    return;
-}
-
-sub update_selfenroll_queue {
-    my ($r,$context,$permission,$cid,$cnum,$cdom) = @_;
-    my @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
-    my @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
-    my $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
-    my $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
-    my $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'}; 
-    my $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
-    my $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
-    my $namespace = 'selfenrollrequests';
-    my ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
-    my %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
-    my $coursedesc = $env{'course.'.$cid.'.description'};
-    my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
-    my $hostname = &Apache::lonnet::hostname($chome);
-    my $protocol = $Apache::lonnet::protocol{$chome};
-    $protocol = 'http' if ($protocol ne 'https');
-    my (@existing,@missingreq,@invalidusers,@limitexceeded,@enrolled,
-        @enrollerrors,@warn_approves,@warn_rejects);
-    my $now = time;
-    my $sender = $env{'user.name'}.':'.$env{'user.domain'};
-    my $approvedmsg = [{
-                        mt => 'Your request for enrollment has been approved.',
-                      },
-                      {
-                        mt => 'Visit [_1], to log-in and access the course',
-                        args => [$protocol.'://'.$hostname],
-                      }];
-                      
-    my $rejectedmsg =  [{
-                         mt => 'Your request for enrollment has not been approved.',
-                       }];
-    foreach my $item (sort {$a <=> $b} @approvals) {
-        my ($num,$uname,$udom,$usec) = split(/:/,$item);
-        my $uhome = &Apache::lonnet::homeserver($uname,$udom);
-        if ($uhome ne 'no_host') {
-            if (exists($requesthash{$uname.':'.$udom})) {
-               
-                if (exists($classlist->{$uname.':'.$udom})) {
-                    if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
-                        if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
-                            ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
-                            push(@existing,$uname.':'.$udom);
-                            next;
-                        }
-                    }
-                }
-            } else {
-                push(@missingreq,$uname.':'.$udom);
-                next;
-            }
-            if (!grep(/^\Q$item\E$/,@rejections)) {
-                if ($limit eq 'allstudents') {
-                    if ($stucounts->{$limit} >= $cap) {
-                        push(@limitexceeded,$uname.':'.$udom);
-                        last;
-                    }
-                } elsif ($limit eq 'selfenrolled') {
-                    if ($stucounts->{$limit} >= $cap) {
-                        push(@limitexceeded,$uname.':'.$udom);
-                        last;
-                    }
-                }
-                my $result = 
-                    &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
-                if ($result eq 'ok') {
-                    push(@enrolled,$uname.':'.$udom);
-                    $stucounts->{'allstudents'} ++;
-                    $stucounts->{'selfenrolled'} ++;
-                    &Apache::selfenroll::send_notification($uname.':'.$udom,$approvedmsg,$cid,
-                                               $coursedesc,$now,'enroller',$sender);
-                    my %userrequest = (
-                        $cdom.'_'.$cnum => {
-                           timestamp   => $now,
-                           section     => $usec,
-                           adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
-                           status      => 'approved',
-                       }
-                    );
-                    my $userresult = 
-                        &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
-                    if ($userresult ne 'ok') {
-                        push(@warn_approves,$uname.':'.$udom);
-                    }
-                } else {
-                    push(@enrollerrors,$uname.':'.$udom);
-                }
-            }
-        } else {
-            push(@invalidusers,$uname.':'.$udom);
-        }
-    }
-    my @changes = (@enrolled,@rejections);
-    if (@rejections) {
-        foreach my $user (@rejections) {
-            &Apache::selfenroll::send_notification($user,$rejectedmsg,$cid,
-                                                   $coursedesc,$now,'enroller',$sender);
-            my ($uname,$udom) = split(/:/,$user);
-            my %userrequest = (
-                $cdom.'_'.$cnum => {
-                    timestamp   => $now,
-                    adjudicator => $env{'user.name'}.':'.$env{'user.domain'}, 
-                    status      => 'rejected',
-                }
-            );
-            my $userresult = 
-                &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
-            if ($userresult ne 'ok') {
-                push(@warn_rejects,$user);
-            }
-        }
-    }
-    if (@changes) {
-        my $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
-        if ($delresult eq 'ok') {
-            my $namelink = 
-                &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
-            my $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
-            my ($approvedlist,$rejectedlist);
-            if (@enrolled) {
-                $approvedlist = join("\n",@enrolled);
-                $r->print('<p>'.&mt('The following were enrolled in the course:').'<ul>');
-                foreach my $user (@enrolled) {
-                    my ($uname,$udom) = split(/:/,$user); 
-                    my $userlink = 
-                        &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
-                    $r->print('<li>'.$userlink.'</li>');
-                }
-                $r->print('</ul></p>');
-            }
-            if (@rejections) {
-                $rejectedlist = join("\n",@rejections);
-                $r->print('<p>'.&mt('The following enrollment requests were rejected:').'<ul>');
-                foreach my $user (@rejections) {
-                    $r->print('<li>'.$user.'</li>');
-                }
-                $r->print('</ul></p>');
-            }
-            &Apache::selfenroll::send_notification($notifylist,$chgmsg,$cid,
-                                                   $coursedesc,$now,'managers',
-                                                   $sender,$approvedlist,$rejectedlist);
-        }
-    }
-    if (@existing) {
-        $r->print('<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>');
-        foreach my $user (@existing) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@missingreq) {
-        $r->print('<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>');
-        foreach my $user (@missingreq) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@invalidusers) {
-        $r->print('<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>');
-        foreach my $user (@invalidusers) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@limitexceeded) {
-        $r->print('<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>');
-        foreach my $user (@limitexceeded) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@enrollerrors) {
-        $r->print('<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>');
-        foreach my $user (@enrollerrors) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@warn_approves) {
-        $r->print('<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>');
-        foreach my $user (@warn_approves) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    if (@warn_rejects) {
-        $r->print('<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>');
-        foreach my $user (@warn_rejects) {
-            $r->print('<li>'.$user.'</li>');
-        }
-        $r->print('</ul></p>');
-    }
-    return;
-}
-
-sub get_student_counts {
-    my ($cdom,$cnum) = @_;
-    my (%idx,%stucounts);
-    my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
-    $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
-    $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
-    while (my ($student,$data) = each(%$classlist)) {
-        if (($data->[$idx{'status'}] eq 'Active') ||
-            ($data->[$idx{'status'}] eq 'Future')) {
-            if ($data->[$idx{'type'}] eq 'selfenroll') {
-                $stucounts{'selfenroll'} ++;
-            }
-            $stucounts{'allstudents'} ++;
-        }
-    }
-    return (\%stucounts,\%idx,$classlist);
-}
-
 sub visible_in_cat {
     my ($cdom,$cnum) = @_;
     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
Index: loncom/interface/selfenroll.pm
diff -u loncom/interface/selfenroll.pm:1.20 loncom/interface/selfenroll.pm:1.21
--- loncom/interface/selfenroll.pm:1.20	Sun May 24 00:05:04 2009
+++ loncom/interface/selfenroll.pm	Tue Aug 11 00:39:46 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Allow users to self-enroll in a course
 #
-# $Id: selfenroll.pm,v 1.20 2009/05/24 00:05:04 bisitz Exp $
+# $Id: selfenroll.pm,v 1.21 2009/08/11 00:39:46 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -34,6 +34,7 @@
 use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::createaccount;
+use Apache::loncoursequeueadmin;
 use LONCAPA qw(:DEFAULT :match);
 
 sub handler {
@@ -580,8 +581,9 @@
                 my $fullname = &Apache::loncommon::plainname($uname,$udom);
                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                 my $coursedesc = $courseinfo{'description'};
-                &send_notification($selfenroll_notifylist,$fullname,$cdom.
-                                   '_'.$cnum,$coursedesc,$now,'request',$owner);
+                &Apache::loncoursequeueadmin::send_selfserver_notification(
+                    $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
+                    $coursedesc,$now,'selfenrollreq',$owner);
             }
         } else {
             $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
@@ -591,104 +593,6 @@
     return $output;
 }
 
-sub send_notification {
-    my ($notifylist,$textstr,$cid,$coursedesc,$timestamp,$context,$sender,
-        $approvedlist,$rejectedlist) = @_;
-# FIXME locallocaltime needs to be able to take $sender_lh as an argument
-#       so this can be localized to the recipients date display format/time zone 
-    $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
-    my $msgcc;
-    my ($rawsubj,@rawmsg,$subject,$message,$namelink);
-    $namelink = &Apache::loncommon::aboutmewrapper(
-                &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}));
-    if ($context eq 'managers') {
-        $rawsubj = 'Self-enrollment requests processed';
-        push(@rawmsg,{
-                      mt => 'Enrollment requests in the following course: [_1] have been processed.',
-                      args => ["\n$coursedesc\n"],
-                     });
-    } elsif ($context eq 'enroller') {
-        $rawsubj = 'Enrollment request';
-        push(@rawmsg,{
-                      mt  => 'Your request for enrollment in the following course: [_1], requested on [_2], has been reviewed by a Course Coordinator.',
-                      args => ["\n$coursedesc\n","$timestamp\n"],
-
-                     });
-        if (ref($textstr) eq 'ARRAY') {
-            push(@rawmsg,@{$textstr});
-        }
-    } else {
-        $rawsubj = 'Self-enrollment request';
-        push(@rawmsg,{
-                      mt  => 'Enrollment in the following course: [_1] was requested by [_2] on [_3].',
-                      args => ["\n$coursedesc\n",$textstr,$timestamp],
-                     },
-                     {
-                      mt =>'As Course Coordinator, use: [_1]Main Menu -> Manage Course Users -> Enrollment Requests[_1] to display a list of pending enrollment requests,[_1] which you can either approve or reject.',
-                      args => ["\n"],
-                     });
-    }
-    my @to_notify = split(/,/,$notifylist);
-    my $numsent = 0;
-    my @recusers;
-    my @recudoms;
-    foreach my $cc (@to_notify) {
-        my ($ccname,$ccdom) = split(/:/,$cc);
-        if (!exists($msgcc->{$ccname.':'.$ccdom})) {
-            push(@recusers,$ccname);
-            push(@recudoms,$ccdom);
-            $msgcc->{$ccname.':'.$ccdom}='';
-            $numsent ++;
-        }
-    }
-    my %reciphash = (
-                     cc => $msgcc,
-    );
-    my ($uname,$udom);
-    if ($sender =~ /:/) {
-        ($uname,$udom) = split(/:/,$sender);
-    } else {
-        $uname = $sender;
-        my %courseinfo = &Apache::lonnet::coursedescription($cid);
-        $udom = $courseinfo{'num'};
-    }
-    my %sentmessage;
-    my $stamp = time;
-    my $msgcount = &Apache::lonmsg::get_uniq();
-    my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
-    $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
-    $message = '';
-    foreach my $item (@rawmsg) {
-        if (ref($item) eq 'HASH') {
-            $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
-        }
-    }
-    &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms);
-    my ($recipid,$recipstatus) =
-        &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
-    foreach my $recip (sort(keys(%{$msgcc}))) {
-        my ($ccname,$ccdom) = split(/:/,$recip);
-        my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
-        my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
-        my $message = '';
-        foreach my $item (@rawmsg) {
-            if (ref($item) eq 'HASH') {
-                $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
-                                                       @{$item->{args}})."\n";
-            }
-        }
-        if ($context eq 'managers') {
-            if ($approvedlist) {
-                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
-            }
-            if ($rejectedlist) {
-                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
-            }
-        }
-        my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid);
-    }
-}
-
 sub jump_to_role {
     my ($role) = @_;
     my $output = <<"END";
Index: doc/loncapafiles/loncapafiles.lpml
diff -u doc/loncapafiles/loncapafiles.lpml:1.655 doc/loncapafiles/loncapafiles.lpml:1.656
--- doc/loncapafiles/loncapafiles.lpml:1.655	Wed Aug  5 00:15:01 2009
+++ doc/loncapafiles/loncapafiles.lpml	Tue Aug 11 00:39:55 2009
@@ -2,7 +2,7 @@
  "http://lpml.sourceforge.net/DTD/lpml.dtd">
 <!-- loncapafiles.lpml -->
 
-<!-- $Id: loncapafiles.lpml,v 1.655 2009/08/05 00:15:01 raeburn Exp $ -->
+<!-- $Id: loncapafiles.lpml,v 1.656 2009/08/11 00:39:55 raeburn Exp $ -->
 
 <!--
 
@@ -3506,6 +3506,15 @@
 <status>works/unverified</status>
 </file>
 <file>
+<source>loncom/interface/loncoursequeueadmin.pm</source>
+<target dist='default'>home/httpd/lib/perl/Apache/loncoursequeueadmin.pm</target>
+<categoryname>handler</categoryname>
+<description>
+Common routines for administration of queues containing user-submitted requests - either for self-enrollment in an existing course, or for creation of a new course.
+</description>
+<status>works/unverified</status>
+</file>
+<file>
 <source>loncom/interface/loncreateuser.pm</source>
 <target dist='default'>home/httpd/lib/perl/Apache/loncreateuser.pm</target>
 <categoryname>handler</categoryname>

Index: loncom/interface/loncoursequeueadmin.pm
+++ loncom/interface/loncoursequeueadmin.pm
# The LearningOnline Network
# Utilities to administer domain course requests and course self-enroll requests 
#
# $Id: loncoursequeueadmin.pm,v 1.1 2009/08/11 00:39:45 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
###

=head1 NAME

Apache::loncoursequeueadmin.pm

=head1 SYNOPSIS

Adminitsration utilities used by domain coordinators for queued course creation requests, and by course coordinators for queued self-enrollment requests.

This is part of the LearningOnline Network with CAPA project
described at http://www.lon-capa.org.

=head1 SUBROUTINES

=over

=item send_selfserve_notification()

=item display_queued_requests()

=item update_request_queue()

=item get_student_counts()

=back

=cut

package Apache::loncoursequeueadmin;

use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonmsg;
use Apache::lonlocal;
use LONCAPA;

sub send_selfserve_notification {
    my ($notifylist,$textstr,$cid,$coursedesc,$timestamp,$context,$sender,
        $approvedlist,$rejectedlist) = @_;
# FIXME locallocaltime needs to be able to take $sender_lh as an argument
#       so this can be localized to the recipients date display format/time zone
    $timestamp =&Apache::lonlocal::locallocaltime($timestamp);
    my $msgcc;
    my ($rawsubj,@rawmsg,$subject,$message,$namelink);
    $namelink = &Apache::loncommon::aboutmewrapper(
                &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}));
    if ($context eq 'coursemanagers') {
        $rawsubj = 'Self-enrollment requests processed';
        push(@rawmsg,{
                      mt => 'Enrollment requests in the following course: [_1] have been processed.',
                      args => ["\n$coursedesc\n"],
                     });
    } elsif ($context eq 'domainmanagers') {
        $rawsubj = 'Course request';
        push(@rawmsg,{
                      mt  => 'Your request for creation of the following course: [_1], requested on [_2], has been reviewed by a Domain Coordinator.',
                      args => ["\n$coursedesc\n","$timestamp\n"],

                     });
        if (ref($textstr) eq 'ARRAY') {
            push(@rawmsg,@{$textstr});
        }
    } elsif ($context eq 'enroller') {
        $rawsubj = 'Enrollment request';
        push(@rawmsg,{
                      mt  => 'Your request for enrollment in the following course: [_1], requested on [_2], has been reviewed by a Course Coordinator.',
                      args => ["\n$coursedesc\n","$timestamp\n"],

                     });
        if (ref($textstr) eq 'ARRAY') {
            push(@rawmsg,@{$textstr});
        }
    } elsif ($context eq 'courserequestor') {
        $rawsubj = 'Course request';
        push(@rawmsg,{
                      mt  => 'Your request for creation of the following course: [_1], requested on [_2], has been reviewed by a Domain Coordinator.',
                      args => ["\n$coursedesc\n","$timestamp\n"],

                     });
        if (ref($textstr) eq 'ARRAY') {
            push(@rawmsg,@{$textstr});
        }
    } elsif ($context eq 'coursereq') {
        push(@rawmsg,{
                      mt  => 'Creation of the following course: [_1] was requested by [_2] on [_3].',
                      args => ["\n$coursedesc\n",$textstr,$timestamp],
                     },
                     {
                      mt =>'As Domain Coordinator, use: [_1]Main Menu -> Create a new course -> Manage Course Requests[_1] to display a list of pending course requests,[_1] which you can either approve or reject.',
                      args => ["\n"],
                     });
    } elsif ($context eq 'selfenrollreq') {
        $rawsubj = 'Self-enrollment request';
        push(@rawmsg,{
                      mt  => 'Enrollment in the following course: [_1] was requested by [_2] on [_3].',
                      args => ["\n$coursedesc\n",$textstr,$timestamp],
                     },
                     {
                      mt =>'As Course Coordinator, use: [_1]Main Menu -> Manage Course Users -> Enrollment Requests[_1] to display a list of pending enrollment requests,[_1] which you can either approve or reject.',
                      args => ["\n"],
                     });
    }
    my @to_notify = split(/,/,$notifylist);
    my $numsent = 0;
    my @recusers;
    my @recudoms;
    foreach my $cc (@to_notify) {
        my ($ccname,$ccdom) = split(/:/,$cc);
        if (!exists($msgcc->{$ccname.':'.$ccdom})) {
            push(@recusers,$ccname);
            push(@recudoms,$ccdom);
            $msgcc->{$ccname.':'.$ccdom}='';
            $numsent ++;
        }
    }
    my %reciphash = (
                     cc => $msgcc,
    );
    my ($uname,$udom);
    if ($sender =~ /:/) {
        ($uname,$udom) = split(/:/,$sender);
    } else {
        $uname = $sender;
        my %courseinfo = &Apache::lonnet::coursedescription($cid);
        $udom = $courseinfo{'num'};
    }
    my %sentmessage;
    my $stamp = time;
    my $msgcount = &Apache::lonmsg::get_uniq();
    my $sender_lh = &Apache::loncommon::user_lang($uname,$udom,$cid);
    $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
    $message = '';
    foreach my $item (@rawmsg) {
        if (ref($item) eq 'HASH') {
            $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},@{$item->{args}})."\n";
        }
    }
    &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$uname,$udom,$msgcount,$cid,$$,$message,\@recusers,\@recudoms);
    my ($recipid,$recipstatus) = &Apache::lonmsg::store_recipients($subject,$uname,$udom,\%reciphash);
    my $status;
    foreach my $recip (sort(keys(%{$msgcc}))) {
        my ($ccname,$ccdom) = split(/:/,$recip);
        my $recip_lh = &Apache::loncommon::user_lang($ccname,$ccdom,$cid);
        my $subject = &Apache::lonlocal::mt_user($sender_lh,$rawsubj);
        my $message = '';
        foreach my $item (@rawmsg) {
            if (ref($item) eq 'HASH') {
                $message .= &Apache::lonlocal::mt_user($sender_lh,$item->{mt},
                                                       @{$item->{args}})."\n";
            }
        }
        if ($context eq 'managers') {
            if ($approvedlist) {
                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved enrollments:')."\n".$approvedlist;
            }
            if ($rejectedlist) {
                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected enrollments:')."\n".$rejectedlist;
            }
        } elsif ($context eq 'domainmanagers') {
            if ($approvedlist) {
                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Approved course requests:')."\n".$approvedlist;
            }
            if ($rejectedlist) {
                $message .= "\n\n".&Apache::lonlocal::mt_user($sender_lh,'Rejected course requests:')."\n".$rejectedlist;
            }
        }
        $status .= &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,1,\%sentmessage,undef,undef,undef,1,$recipid).',';
    }
    $status =~ s/,$//;
    return ($recipstatus,$status);
}

sub display_queued_requests {
    my ($context,$dom,$cnum) = @_;
    my ($namespace,$formaction,%requesthash);
    if ($context eq 'course') {
        $formaction = '/adm/createuser';
        $namespace = 'selfenrollrequests';
        %requesthash = &Apache::lonnet::dump($namespace,$dom,$cnum);
    } else {
        $formaction = '/adm/createcourse';
        $namespace = 'courserequestqueue';
        %requesthash = &Apache::lonnet::dump_dom($namespace,$dom,undef,'_approval');
    }
    my ($output,%queue_by_date,%crstypes);
    if (keys(%requesthash) > 0) {
        $output = '<form method="post" name="changequeue" action="'.$formaction.'" />'.
                  '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                  '<input type="hidden" name="state" value="done" />'.
                  &Apache::loncommon::start_data_table().
                  &Apache::loncommon::start_data_table_header_row().
                  '<th>'.&mt('Action').'</th>'.
                  '<th>'.&mt('Requestor').'</th>';
        if ($context eq 'course') {
            $output .= '<th>'.&mt('Section').'</th>'.
                       '<th>'.&mt('Date requested').'</th>';
            %crstypes = &Apache::lonlocal::texthash (
                            official   => 'Official course',
                            unofficial => 'Unofficial course',
                            community  => 'Community',
                        );
        } else {
            $output .= '<th>'.&mt('Type').'</th>'.
                       '<th>'.&mt('Date requested').'</th>'.
                       '<th>'.&mt('Details').'</th>';
        }
        $output .= &Apache::loncommon::end_data_table_header_row();
        foreach my $item (keys(%requesthash)) {
            my ($timestamp,$entry);
            if ($context eq 'course') {
                ($timestamp, my $usec) = split(/:/,$requesthash{$item});
                $entry = $item.':'.$usec;
            } else {
                $timestamp = $requesthash{$item}{'timestamp'};
                if (ref($requesthash{$item}) eq 'HASH') {
                    $entry = $item.':'.$requesthash{$item}{'ownername'}.':'.
                             $requesthash{$item}{'ownerdom'}.':'.
                             $requesthash{$item}{'crstype'}.':'.
                             $requesthash{$item}{'description'};
                }
            }
            if ($entry ne '') {
                if (exists($queue_by_date{$timestamp})) {
                    if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
                        push(@{$queue_by_date{$timestamp}},$entry);
                    }
                } else {
                    @{$queue_by_date{$timestamp}} = ($entry);
                }
            }
        }
        my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
        my $count = 0;
        foreach my $item (@sortedtimes) {
            if (ref($queue_by_date{$item}) eq 'ARRAY') {
                foreach my $request (sort(@{$queue_by_date{$item}})) {
                    my ($row,$approve,$reject,$showtime,$showsec,$namelink,
                        $detailslink,$crstype);
                    $showtime = &Apache::lonlocal::locallocaltime($item);
                    if ($context eq 'course') {
                        my ($puname,$pudom,$pusec) = split(/:/,$request);
                        $approve = $count.':'.$puname.':'.$pudom.':'.$pusec;
                        $reject = $puname.':'.$pudom;
                        $showsec = $pusec;
                        if ($showsec eq '') {
                            $showsec = &mt('none');
                        }
                        $namelink = &Apache::loncommon::aboutmewrapper(
                                    &Apache::loncommon::plainname($puname,$pudom),
                                    $puname,$pudom);

                    } else {
                        my ($cnum,$ownername,$ownerdom,$type,$cdesc)=split(/:/,$request);
                        $detailslink='<a href="javascript::opencoursereqdisplay('.
                                      "'$dom".'_'."$cnum'".');">'.$cdesc.'</a>';
                        $crstype = $type;
                        if (defined($crstypes{$type})) {
                            $crstype = $crstypes{$type};
                        }
                        $approve = $count.':'.$cnum;
                        $reject = $cnum;
                        $namelink = &Apache::loncommon::aboutmewrapper(
                                    &Apache::loncommon::plainname($ownername,$ownerdom),
                                    $ownername,$ownerdom);
                    }
                    $row = '<td><span class="LC_nobreak"><label>'.
                           '<input type="checkbox" value="'.$approve.'" name="approvereq" />'.&mt('Approve').'</label></span><br />'.
                           '<span class="LC_nobreak"><label>'.
                           '<input type="checkbox" value="'.$reject.'" name="rejectreq" />'.&mt('Reject').'</label></span><br /></td>'.
                           '<td>'.$namelink.'</td>'."\n";
                    if ($context eq 'course') {
                        $row .= '<td>'.$showsec.'</td>'."\n".
                                '<td>'.$showtime.'</td>'."\n";
                    } else { 
                        $row .= '<td>'.$crstype.'</td>'."\n".
                                '<td>'.$showtime.'</td>'."\n".
                                '<td>'.$detailslink.'</td>'."\n";
                    }
                    $output .= &Apache::loncommon::start_data_table_row()."\n".
                               $row.
                               &Apache::loncommon::end_data_table_row()."\n";
                    $count ++;
                }
            }
        }
        $output .= &Apache::loncommon::end_data_table().
                   '<input type="submit" name="processqueue" value="'.&mt('Save').
                   '" /></form>';
    } else {
        if ($context eq 'course') {
            $output .= &mt('There are currently no enrollment requests.');
        } else {
            $output .= &mt('There are currently no course requests awaiting approval.');
        }
    }
    return $output;
}

sub update_request_queue {
    my ($context,$cdom,$cnum,$coursedesc) = @_;
    my ($output,$access_start,$access_end,$limit,$cap,$notifylist,$namespace,
        $stucounts,$idx,$classlist,%requesthash,$coursedesc,$cid,$hostname,
        $protocol,$now,$sender,$approvedmsg,$rejectedmsg,$beneficiary,
        @existing,@missingreq,@invalidusers,@limitexceeded,@enrolled,@created,
        @enrollerrors,@create_errors,@warn_approves,@warn_rejects,@approvals,
        @rejections);
    @approvals = &Apache::loncommon::get_env_multiple('form.approvereq');
    @rejections = &Apache::loncommon::get_env_multiple('form.rejectreq');
    $now = time;
    $sender = $env{'user.name'}.':'.$env{'user.domain'};
    if ($context eq 'course') {
        $namespace = 'selfenrollrequests';
        $beneficiary = 'enroller';
        $cid = $env{'request.course.id'};
        my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
        $hostname = &Apache::lonnet::hostname($chome);
        $protocol = $Apache::lonnet::protocol{$chome};
        $protocol = 'http' if ($protocol ne 'https');
        %requesthash = &Apache::lonnet::dump($namespace,$cdom,$cnum);
        $access_start =  $env{'course.'.$cid.'.internal.selfenroll_start_access'};
        $access_end =  $env{'course.'.$cid.'.internal.selfenroll_end_access'};
        $limit = $env{'course.'.$cid.'.internal.selfenroll_limit'};
        $cap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
        $notifylist = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
        $namespace = 'selfenrollrequests';
        ($stucounts,$idx,$classlist) = &get_student_counts($cdom,$cnum);
        $approvedmsg = [{
                            mt => 'Your request for enrollment has been approved.',
                        },
                        {
                            mt   => 'Visit [_1], to log-in and access the course',
                            args => [$protocol.'://'.$hostname],
                        }];
        $rejectedmsg =  [{
                            mt => 'Your request for enrollment has not been approved.',
                        }];
    } else {
        $beneficiary = 'requestor';
    }
    foreach my $item (sort {$a <=> $b} @approvals) {
        if ($context eq 'course') {
            my ($num,$uname,$udom,$usec) = split(/:/,$item);
            my $uhome = &Apache::lonnet::homeserver($uname,$udom);
            if ($uhome ne 'no_host') {
                if (exists($requesthash{$uname.':'.$udom})) {

                    if (exists($classlist->{$uname.':'.$udom})) {
                        if (ref($classlist->{$uname.':'.$udom}) eq 'ARRAY') {
                            if (($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Active') ||
                                ($classlist->{$uname.':'.$udom}->[$idx->{'status'}] eq 'Future')) {
                                push(@existing,$uname.':'.$udom);
                                next;
                            }
                        }
                    }
                } else {
                    push(@missingreq,$uname.':'.$udom);
                    next;
                }
                if (!grep(/^\Q$item\E$/,@rejections)) {
                    if ($limit eq 'allstudents') {
                        if ($stucounts->{$limit} >= $cap) {
                            push(@limitexceeded,$uname.':'.$udom);
                            last;
                        }
                    } elsif ($limit eq 'selfenrolled') {
                        if ($stucounts->{$limit} >= $cap) {
                            push(@limitexceeded,$uname.':'.$udom);
                            last;
                        }
                    }
                    my $result =
                        &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$usec,$access_end,$access_start,'selfenroll',undef,$cdom.'_'.$cnum,1);
                    if ($result eq 'ok') {
                        push(@enrolled,$uname.':'.$udom);
                        $stucounts->{'allstudents'} ++;
                        $stucounts->{'selfenrolled'} ++;
                        &send_selfserve_notification($uname.':'.$udom,$approvedmsg,
                                       $cid,$coursedesc,$now,$beneficiary,$sender);
                        my %userrequest;
                        if ($context eq 'course') {
                            %userrequest = (
                                $cdom.'_'.$cnum => {
                                    timestamp   => $now,
                                    section     => $usec,
                                    adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                                    status      => 'approved',
                                }
                            );
                        } else {
                            %userrequest = ();
                        }
                        my $userresult =
                            &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
                        if ($userresult ne 'ok') {
                            push(@warn_approves,$uname.':'.$udom);
                        }
                    } else {
                        push(@enrollerrors,$uname.':'.$udom);
                    }
                }
            } else {
                push(@invalidusers,$uname.':'.$udom);
            }
        } else {

        }
    }
    my @changes = (@enrolled,@rejections);
    if (@rejections) {
        foreach my $user (@rejections) {
            &send_selfserve_notification($user,$rejectedmsg,$cid,$coursedesc,
                                         $now,$beneficiary,$sender);
            if ($context eq 'course') {
                my ($uname,$udom) = split(/:/,$user);
                my %userrequest = (
                    $cdom.'_'.$cnum => {
                        timestamp   => $now,
                        adjudicator => $env{'user.name'}.':'.$env{'user.domain'},
                        status      => 'rejected',
                    }
                );
                my $userresult =
                    &Apache::lonnet::put($namespace,\%userrequest,$udom,$uname);
                if ($userresult ne 'ok') {
                    push(@warn_rejects,$user);
                }
            } else {

            }
        }
    }
    if (@changes) {
        my $delresult;
        if ($context eq 'course') {
            $delresult = &Apache::lonnet::del($namespace,\@changes,$cdom,$cnum);
        } else {
            $delresult = &Apache::lonnet::del_dom($namespace,\@changes,$cdom);
        }
        if ($delresult eq 'ok') {
            my $namelink =
                &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}).' ('.$env{'user.name'}.':'.$env{'user.domain'}.')';
            my ($chgmsg,$approvedlist,$rejectedlist);
            if ($context eq 'course') {
                $chgmsg = "'Action was taken on the following enrollment requests by [_1].',$namelink";
                if (@enrolled) {
                    $approvedlist = join("\n",@enrolled);
                    $output .= '<p>'.&mt('The following were enrolled in the course:').'<ul>';
                    foreach my $user (@enrolled) {
                        my ($uname,$udom) = split(/:/,$user);
                        my $userlink =
                            &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom);
                        $output .= '<li>'.$userlink.'</li>';
                    }
                    $output .= '</ul></p>';
                }
                if (@rejections) {
                    $rejectedlist = join("\n",@rejections);
                    $output .= '<p>'.&mt('The following enrollment requests were rejected:').'<ul>';
                    foreach my $user (@rejections) {
                        $output .= '<li>'.$user.'</li>';
                    }
                    $output .= '</ul></p>';
                }
            } else {


            }
            &send_selfserve_notification($notifylist,$chgmsg,$cid,$coursedesc,
                                         $now,'coursemanagers',$sender,
                                         $approvedlist,$rejectedlist);
        }
    }
    if (@existing) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt('The following enrollment requests were deleted because the user is already enrolled in the course:').'<ul>';
            foreach my $user (@existing) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    if (@missingreq) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt('The following enrollment requests were ignored because the request is no longer in the enrollment queue:').'<ul>';
            foreach my $user (@missingreq) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        }
    }
    if (@invalidusers) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt('The following enrollment requests were deleted because the requestor does not have a LON-CAPA account:').'<ul>';
            foreach my $user (@invalidusers) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    if (@limitexceeded) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt('The following enrollment requests were skipped because the enrollment limit has been reached for the course:').'<ul>';
            foreach my $user (@limitexceeded) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    if (@enrollerrors) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt('The following enrollment requests could not be processed because an error occurred:').'<ul>';
            foreach my $user (@enrollerrors) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    if (@warn_approves) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
            foreach my $user (@warn_approves) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    if (@warn_rejects) {
        if ($context eq 'course') {
            $output .= '<p>'.&mt("For the following users, an error occurred when updating the user's own self-enroll requests record:").'<ul>';
            foreach my $user (@warn_rejects) {
                $output .= '<li>'.$user.'</li>';
            }
            $output .= '</ul></p>';
        } else {

        }
    }
    return $output;
}

sub get_student_counts {
    my ($cdom,$cnum) = @_;
    my (%idx,%stucounts);
    my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
    $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
    $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
    while (my ($student,$data) = each(%$classlist)) {
        if (($data->[$idx{'status'}] eq 'Active') ||
            ($data->[$idx{'status'}] eq 'Future')) {
            if ($data->[$idx{'type'}] eq 'selfenroll') {
                $stucounts{'selfenroll'} ++;
            }
            $stucounts{'allstudents'} ++;
        }
    }
    return (\%stucounts,\%idx,$classlist);
}

1;

--raeburn1249951195--