[LON-CAPA-cvs] cvs: modules /raeburn/register register.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Tue, 22 May 2007 18:55:09 -0000


This is a MIME encoded message

--raeburn1179860109
Content-Type: text/plain

raeburn		Tue May 22 14:55:09 2007 EDT

  Modified files:              
    /modules/raeburn/register	register.pm 
  Log:
  Added cvs version header
  Added brief title and author information
  Include support for fee waivers
  Include support for conditional text (web and email targets) dependent on
  selections made by user
  Display of workshop selections now sorted numerically
  
  
--raeburn1179860109
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070522145509.txt"

Index: modules/raeburn/register/register.pm
diff -u modules/raeburn/register/register.pm:1.12 modules/raeburn/register/register.pm:1.13
--- modules/raeburn/register/register.pm:1.12	Mon Apr  2 16:21:47 2007
+++ modules/raeburn/register/register.pm	Tue May 22 14:55:06 2007
@@ -1,3 +1,13 @@
+########################################################## 
+#
+# Session-based registration for conferences/workshops
+# Data read/written from MySQL database.
+#
+# $Id: register.pm,v 1.13 2007/05/22 18:55:06 raeburn Exp $
+#
+# Stuart P Raeburn
+# 
+##########################################################
 package Apache::LON::register;
 
 use strict;
@@ -76,8 +86,8 @@
     my %sponsorinfo = ();
     my $domain = $params{'domain'};
     my %feeinfo;
-
-    my ($page,$year,$event,$portal,$returnpage,$event_id,$webpath,$year_quoted,$event_quoted,$fees);
+    my @consortium;
+    my ($page,$year,$event,$portal,$returnpage,$event_id,$webpath,$year_quoted,$event_quoted,$fees,$condmail);
     if (exists $params{'return'}) {
         $returnpage = $params{'return'};
     }
@@ -108,7 +118,7 @@
             $sponsorinfo{$name}{orientation} = $orientation;
         }
         $sth->finish;
-        ($event_id,$webpath,$fees) = $dbh->selectrow_array("SELECT event_id,webpath,fees FROM event_config WHERE year=$year_quoted AND event=$event_quoted");
+        ($event_id,$webpath,$fees,$condmail) = $dbh->selectrow_array("SELECT event_id,webpath,fees,condmail FROM event_config WHERE year=$year_quoted AND event=$event_quoted");
         $portal = $webpath.$returnpage;
         if ($event_id eq '') {
             &invalid_event($r,$dbh,$page,$year,$event,$portal,$returnpage,$exitpage,\@sponsors,\%sponsorinfo,$domain);
@@ -116,14 +126,21 @@
         } else {
 # check for fees
             if ($fees) {
-                $sth = $dbh->prepare("SELECT iid,description,cost,required,longdesc,type FROM products WHERE event_id = '$event_id'");
+                $sth = $dbh->prepare("SELECT iid,description,cost,required,longdesc,type,waiver FROM products WHERE event_id = '$event_id'");
                 $sth->execute;
-                while ( my ($iid,$desc,$cost,$reqd,$longdesc,$type) = $sth->fetchrow_array) {
+                while ( my ($iid,$desc,$cost,$reqd,$longdesc,$type,$waiver) = $sth->fetchrow_array) {
                     $feeinfo{$iid}{desc} = $desc;
                     $feeinfo{$iid}{cost} = $cost;
                     $feeinfo{$iid}{reqd} = $reqd;
                     $feeinfo{$iid}{longdesc} = $longdesc;
                     $feeinfo{$iid}{type} = $type;
+                    $feeinfo{$iid}{waiver} = $waiver;
+                }
+                $sth->finish;
+                $sth = $dbh->prepare("SELECT user_id FROM consortium");
+                $sth->execute;
+                while (my $user_id = $sth->fetchrow_array) {
+                    push @consortium, $user_id;
                 }
                 $sth->finish;
             }
@@ -208,9 +225,9 @@
          my $status = $dbh->selectrow_array("SELECT status FROM event_registration WHERE user_id = $quoted_user AND event_id = '$event_id'");
          &display_three($r,$event,$year,$event_id,$status,$domain,$portal,$returnpage,$exitpage,$dbh,$current_page,$contact_email,$contact_name,\@sponsors,\%sponsorinfo);
     } elsif ($current_page == 2) {
-        &display_two($r,$event,$year,$event_id,\%params,$user,$domain,$portal,$returnpage,$exitpage,\%items,\@info,\@workshop,$dbh,$current_page,$contact_email,$contact_name,\%form_elements,\@rowitems,\@rowtitles,\@sponsors,\%sponsorinfo,$fees,\%feeinfo);
+        &display_two($r,$event,$year,$event_id,\%params,$user,$domain,$portal,$returnpage,$exitpage,\%items,\@info,\@workshop,$dbh,$current_page,$contact_email,$contact_name,\%form_elements,\@rowitems,\@rowtitles,\@sponsors,\%sponsorinfo,$fees,\%feeinfo,\@consortium,$condmail);
     } else {
-        &display_one($r,$event_id,\%params,$user,\$domain,$portal,$returnpage,$exitpage,\%items,\@info,\@workshop,$dbh,$current_page,$contact_email,$contact_name,\%form_elements,\@rowitems,\@rowtitles,\@sponsors,\%sponsorinfo,$fees,\%feeinfo);
+        &display_one($r,$event_id,\%params,$user,\$domain,$portal,$returnpage,$exitpage,\%items,\@info,\@workshop,$dbh,$current_page,$contact_email,$contact_name,\%form_elements,\@rowitems,\@rowtitles,\@sponsors,\%sponsorinfo,$fees,\%feeinfo,\@consortium);
     }
     return OK;
 }
@@ -289,7 +306,7 @@
 }
 
 sub display_one() {
-    my ($r,$event_id,$params,$user,$domain,$portal,$returnpage,$exitpage,$items,$info,$workshop,$dbh,$page,$contact_email,$contact_name,$form_elements,$rowitems,$rowtitles,$sponsors,$sponsorinfo,$fees,$feeinfo) = @_;
+    my ($r,$event_id,$params,$user,$domain,$portal,$returnpage,$exitpage,$items,$info,$workshop,$dbh,$page,$contact_email,$contact_name,$form_elements,$rowitems,$rowtitles,$sponsors,$sponsorinfo,$fees,$feeinfo,$consortium) = @_;
     my $year = $$params{'year'};
     my $event = $$params{'event'};
     my $quoted_user = $dbh->quote( $user );
@@ -437,7 +454,11 @@
             }
         }
         foreach my $iid (keys(%balance)) {
-            $credit{$iid} = $balance{$iid} - ($total{$iid} * $$feeinfo{$iid}{'cost'});
+            if (($$feeinfo{$iid}{'waiver'} eq 'Y') && (grep/^\Q$user\E$/,@{$consortium})) {
+                $credit{$iid} = $balance{$iid};
+            } else { 
+                $credit{$iid} = $balance{$iid} - ($total{$iid} * $$feeinfo{$iid}{'cost'});
+            }
             $credit{$iid} = sprintf("%.2f",$credit{$iid});
         }
         if ($status eq 'enroll') {
@@ -490,7 +511,7 @@
         $regopen = &UnixDate($registeropen, "%s");
         $opendate = localtime($regopen);
         if ($now < $regopen) {
-            if ((!(&authorized($dbh,$quoted_user,$event_id))) && ($user ne 'itds@msu.edu')) {
+            if ((!(&authorized($dbh,$quoted_user,$event_id))) && ($user ne 'raeburn:msu')) {
                 $regaccess = 'future';
             }
         }
@@ -946,7 +967,7 @@
 
 
 sub display_two() {
-    my ($r,$event,$year,$event_id,$params,$user,$domain,$portal,$returnpage,$exitpage,$items,$info,$workshop,$dbh,$page,$contact_email,$contact_name,$form_elements,$rowitems,$rowtitles,$sponsors,$sponsorinfo,$fees,$feeinfo) = @_;
+    my ($r,$event,$year,$event_id,$params,$user,$domain,$portal,$returnpage,$exitpage,$items,$info,$workshop,$dbh,$page,$contact_email,$contact_name,$form_elements,$rowitems,$rowtitles,$sponsors,$sponsorinfo,$fees,$feeinfo,$consortium,$condmail) = @_;
     my $enc_event = &HTML::Entities::encode($event);
     $enc_event =~ tr/ /+/;
     my $action = $$params{'nextaction'};
@@ -999,14 +1020,15 @@
 # Get fee information
     my %feehash;
     if ($fees) {
-        $sth = $dbh->prepare("SELECT iid,description,cost,required,longdesc,type FROM products WHERE event_id = '$event_id'");
+        $sth = $dbh->prepare("SELECT iid,description,cost,required,longdesc,type,waiver FROM products WHERE event_id = '$event_id'");
         $sth->execute;
-        while (my ($iid,$desc,$cost,$required,$longdesc,$type)=$sth->fetchrow_array) {
+        while (my ($iid,$desc,$cost,$required,$longdesc,$type,$waiver)=$sth->fetchrow_array) {
             $feehash{$desc}{'iid'} = $iid;
             $feehash{$desc}{'cost'} = $cost;
             $feehash{$desc}{'reqd'} = $required;
             $feehash{$desc}{'longdesc'} = $longdesc;
             $feehash{$desc}{'type'} = $type;
+            $feehash{$desc}{'waiver'} = $waiver;
         }
         $sth->finish;
     }
@@ -1195,7 +1217,7 @@
         }
     } elsif ($action eq 'cancel') {
         if ($register == 1) {
-            $dbh->do("UPDATE event_registration SET status = 'cancel' WHERE user_id = $quoted_user AND event_id = '$event_id'");
+            $dbh->do("UPDATE event_registration SET status='cancel',modified=NOW() WHERE user_id = $quoted_user AND event_id = '$event_id'");
             my $newmsg .= "Your registration for the $year LON-CAPA $event has been cancelled.";
             $msg .= $newmsg."\n";
             $webmsg .= "<li>".$newmsg."</li>";
@@ -1291,7 +1313,17 @@
     }
     $sth->finish;
     if (@finalworkshops) {
-        $finaldispdat{'workshop'} = join(", ",@finalworkshops);
+        @finalworkshops = sort { $a <=> $b } @finalworkshops;
+        my (@allworkshops,%workshoptitle,@displayworkshops);
+        @allworkshops = split(/&/,$form_elements->{'workshop'}{'contents'});
+        foreach my $item (@allworkshops) {
+            my ($key,$value) = split(/=/,$item);
+            $workshoptitle{$key} = $value;
+        }
+        foreach my $item (@finalworkshops) {
+            push(@displayworkshops,$workshoptitle{$item});
+        }
+        $finaldispdat{'workshop'} = join(", ",@displayworkshops);
         if ($finaldispdat{'workshop'} eq '') {
             $finaldispdat{'workshop'} = 'None';
         }
@@ -1303,11 +1335,11 @@
                       mail => '',
                       web => ''
                     );
+    my %linefeed = (
+                      mail => "\n",
+                      web => "<br />\n"
+                   );
     unless ($msg eq '' || $action eq 'cancel') {
-        my %linefeed = (
-                       mail => "\n",
-                       web => "<br />\n"
-                       );
         foreach my $target (keys %statusmsg) {
             $statusmsg{$target} .= $linefeed{$target};
             if ($target eq 'web') {
@@ -1373,7 +1405,11 @@
                         } else { 
                             $quantities{$key} ++;
                         }
-                        $costs{$key} = $feehash{$key}{'cost'} * $quantities{$key};
+                        if (($feehash{$key}{'waiver'} eq 'Y') && (grep/^\Q$user\E$/,@{$consortium})) {
+                            $costs{$key} = 0;
+                        } else {
+                            $costs{$key} = $feehash{$key}{'cost'} * $quantities{$key};
+                        }
                     } else {
                         $costs{$key} = 0;
                         $quantities{$key} = 0;
@@ -1442,6 +1478,9 @@
         }
     }
 
+# Construct conditional parts of mail message.
+
+    my %mailextra;
     if ($mailflag) {
         my $requestmail = "To: $$params{'email'}\n";
         $requestmail .=  "From: $contact_name <$contact_email>\n".
@@ -1453,6 +1492,13 @@
              if ($fees) {
                  $requestmail .= &refund_info($endcredit,$now)."\n\n";
              }
+             if ($condmail eq 'Y') {
+                 %mailextra = &custommail($dbh,$condmail,$event_id,$params,
+                                          \%linefeed);
+                 if ($mailextra{'mail'}) {
+                     $requestmail .= $mailextra{'mail'}."\n";
+                 }
+             }
          }
          $requestmail .= "MSU LON-CAPA support team\n".
     "$contact_email\n";
@@ -1467,7 +1513,7 @@
     "From: loncapa\@loncapa.org\n".
     "Subject: LON-CAPA conference/workshop change\n".
     "The following transaction occurred for $user in the LON-CAPA events registration system: \n".
-    "$msg\n"."$statusmsg{mail}\n";
+    "$msg\n"."$statusmsg{mail}\n".$mailextra{'mail'};
         if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
             print MAIL $mailcopy;
             close(MAIL);
@@ -1527,15 +1573,28 @@
       <td class="LC_receipt">$webmsg<br />$statusmsg{web}</td>
      </tr>
 END_OF_B
+    if ($condmail) {
+        if ($mailextra{'web'}) {
+            $r->print(<<"END_OF_C");
+     <tr>
+      <td colspan="2">&nbsp;</td>
+     </tr>
+     <tr>
+      <td>&nbsp;</td>
+      <td class="LC_receipt">$mailextra{'web'}</td>
+     </tr>
+END_OF_C
+        }
+    }
     if ($fees) {
-        $r->print(<<"END_OF_C");
+        $r->print(<<"END_OF_D");
      <tr>
       <td colspan="2">&nbsp;</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
       <td class="LC_receipt">$payinfo
-END_OF_C
+END_OF_D
         $order = sprintf("%.2f",$order);
         if ($order > 0) {
             $r->print('Please click the payment button to pay the balance due of $'.$order); 
@@ -2150,6 +2209,106 @@
     }
 }
 
+sub custommail {
+    my ($dbh,$condmail,$event_id,$params,$linefeed) = @_;
+# Get mail text
+    my %mailextra;
+    foreach my $type (keys(%{$linefeed})) {
+        $mailextra{$type} = '';
+    }
+    my %mail_conditions;
+    my %mailsettings;
+    my $sth = $dbh->prepare("SELECT conditionid,type FROM requestmailcondition WHERE event_id = '$event_id'");
+    $sth->execute();
+    while (my ($condid,$mailtype) = $sth->fetchrow_array) {
+        $mail_conditions{$condid} = $mailtype;
+    }
+    $sth->finish;
+    if (keys(%mail_conditions) > 0) {
+        foreach my $condid (sort(keys(%mail_conditions))) {
+            my $condpassed = 1;
+            if ($mail_conditions{$condid} ne 'default') {
+                $sth = $dbh->prepare("SELECT element,value,checktype,equality FROM requestmailcheck WHERE conditionid = '$condid'");
+                $sth->execute();
+                while (my ($formelement,$reqdvalue,$checktype,$equality) = $sth->fetchrow_array) {
+                    if ($condpassed) {
+                        if (ref($params) eq 'HASH') {
+                            if ($checktype eq 'regexp') {
+                                if ($equality eq 'Y') {
+                                    if ($params->{$formelement} !~ /$reqdvalue/) {
+                                        $condpassed = 0;
+                                    } 
+                                } else {
+                                    if ($params->{$formelement} =~ /$reqdvalue/) {
+                                        $condpassed = 0;
+                                    }
+                                }
+                            } else {
+                                if ($equality eq 'Y') {
+                                    if ($params->{$formelement} ne $reqdvalue) {
+                                        $condpassed = 0;
+                                    }
+                                } else {
+                                    if ($params->{$formelement} eq $reqdvalue) {
+                                        $condpassed = 0;
+                                    }
+                                }
+                            }
+                        } else {
+                            $condpassed = 0;
+                        }
+                    }
+                }
+                $sth->finish;
+            }
+            if ($condpassed) {
+                $sth = $dbh->prepare("SELECT mailid,mailtext,position FROM requestmailtext WHERE conditionid = '$condid'");
+                $sth->execute();
+                while (my ($mid,$mailtext,$mailpos) = $sth->fetchrow_array) {
+                    %{$mailsettings{$mid}} =
+                                            (
+                                                text => $mailtext,
+                                                position => $mailpos,
+                                            );
+                }
+                $sth->finish;
+                if (keys(%mailsettings) > 0) {
+                    foreach my $mid (keys(%mailsettings)) {
+                        $sth = $dbh->prepare("SELECT placeholder,element FROM requestmailregexp WHERE mailid = '$mid'");
+                        $sth->execute();
+                        while (my ($placeholder,$paramname) = $sth->fetchrow_array) {
+                            if (ref($params) eq 'HASH') {
+                                my $match = "'".$params->{$paramname}."'";
+                                $mailsettings{$mid}{text} =~ s/\Q$placeholder\E/$match/;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    my @orderedmail;
+    my %midbypos;
+    if (keys(%mailsettings) > 0) {
+        foreach my $mid (keys(%mailsettings)) {
+            push(@orderedmail,$mailsettings{$mid}{position});
+            $midbypos{$mailsettings{$mid}{position}} = $mid;
+        }
+    }
+    foreach my $item (sort(@orderedmail)) {
+        foreach my $type (keys(%{$linefeed})) {
+            $mailextra{$type} .= $mailsettings{$midbypos{$item}}{text}.$linefeed->{$type}.$linefeed->{$type};
+            $mailextra{$type} =~ s/\\n/$linefeed->{$type}/g;
+            if ($type eq 'web') {
+                $mailextra{$type} =~ s-(http://[^\s]+?)(\.?\s)-<a href="$1">$1</a>$2-g;
+            }
+        }
+
+    }
+    return %mailextra;
+}
+
 sub checkmsu {
     my ($user,$userinfo) = @_;
     $$userinfo{'institution'} = 'Michigan State University';

--raeburn1179860109--