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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 14 Apr 2008 15:02:36 -0000


raeburn		Mon Apr 14 11:02:36 2008 EDT

  Modified files:              
    /modules/raeburn/register	registrations.pm 
  Log:
  - Access to attendance days data.
  - Provide path to excel file on local server if webpath to site for event is to a web page on a different server.
  - Create excel directory if not present.
  
  
Index: modules/raeburn/register/registrations.pm
diff -u modules/raeburn/register/registrations.pm:1.3 modules/raeburn/register/registrations.pm:1.4
--- modules/raeburn/register/registrations.pm:1.3	Tue May 22 15:19:51 2007
+++ modules/raeburn/register/registrations.pm	Mon Apr 14 11:02:34 2008
@@ -4,7 +4,7 @@
 # Data retrieval for users with administrative access for a
 # particular event.
 #
-# $Id: registrations.pm,v 1.3 2007/05/22 19:19:51 raeburn Exp $
+# $Id: registrations.pm,v 1.4 2008/04/14 15:02:34 raeburn Exp $
 #
 # Stuart P Raeburn
 #
@@ -156,15 +156,17 @@
             }
             $sth->finish;
 # get formfield values
-            $sth = $dbh->prepare("SELECT name,v_position,h_position,required,title,rowtitle,tablename FROM event_formfields WHERE event_id = '$event_id'");
+            $sth = $dbh->prepare("SELECT name,type,v_position,h_position,required,title,rowtitle,tablename,contents FROM event_formfields WHERE event_id = '$event_id'");
             $sth->execute();
-            while (my ($name,$v_position,$h_position,$required,$title,$rowtitle,$tablename) = $sth->fetchrow_array) {
+            while (my ($name,$type,$v_position,$h_position,$required,$title,$rowtitle,$tablename,$contents) = $sth->fetchrow_array) {
+                $form_elements{$name}{'type'} = $type;
                 $form_elements{$name}{'v_position'} = $v_position;
                 $form_elements{$name}{'h_position'} = $h_position;
                 $form_elements{$name}{'required'} = $required;
                 $form_elements{$name}{'title'} = $title;
                 $form_elements{$name}{'rowtitle'} = $rowtitle;
                 $form_elements{$name}{'tablename'} = $tablename;
+                $form_elements{$name}{'contents'} = $contents;
                 my $vpos = $v_position-1;
                 my $hpos = $h_position-1;
                 unless(exists($rowitems[$vpos])) {
@@ -933,6 +935,30 @@
         }
         $sth->finish;
     }
+    my %daytitle;
+    my @days = split(/&/,$form_elements->{'attendancedays'}{'contents'});
+    my @alldays;
+    foreach my $item (@days) {
+        my ($key,$value) = split(/=/,$item);
+        $daytitle{$key} = $value;
+        push (@alldays,$key); 
+    }
+    my %attendancedays;
+    foreach my $user_id (@users) {
+        my @days;  
+        my $user_id_quoted = $dbh->quote($user_id);
+        my $statement = "SELECT name from attendancedays WHERE user_id = $user_id_quoted AND event_id = '$event_id'";
+        my $sth = $dbh->prepare("$statement");
+        $sth->execute();
+        while (my @row = $sth->fetchrow_array) {
+            if (@row > 0) {
+                foreach my $day (@row) {
+                    $attendancedays{$user_id}{$day} = 'Y';
+                }
+            }
+        }
+        $sth->finish;
+    }
     my $jscript = '';
     my $headerweb = '';
     my @webcontent = ();
@@ -946,6 +972,11 @@
                     push(@header,'workshop - '.$workshop); 
                     $headerweb .= '<td align="center"><font face="Arial,Helvetica,sans-serif"><b>workshop - '.$workshop.'</b></font></td>';
                 }
+            } elsif ($field eq 'attendancedays') {
+                foreach my $day (@alldays) {
+                    push(@header,'attendance - '.$daytitle{$day});
+                    $headerweb .= '<td align="center"><font face="Arial,Helvetica,sans-serif"><b>attendance - '.$daytitle{$day}.'</b></font></td>';
+                }
             } else {
                 $headerweb .= '<td align="center"><font face="Arial,Helvetica,sans-serif"><b>'.$$form_elements{$field}{title}.'</b></font></td>';
                 push(@header,$$form_elements{$field}{title});
@@ -966,6 +997,16 @@
                             push(@{$data[$rowcount]},'N');
                         }
                     }
+                } elsif ($field eq 'attendancedays') {
+                    foreach my $day (@alldays) {
+                        if (defined($attendancedays{$user_id}{$day})) {
+                            $webcontent[$rowcount] .= '<td align="right">Y</td>';
+                            push(@{$data[$rowcount]},'Y');
+                        } else {
+                            $webcontent[$rowcount] .= '<td align="right">N</td>';
+                            push(@{$data[$rowcount]},'N');
+                        }
+                    }
                 } else {
                     $webcontent[$rowcount] .= '<td align="right">'.$currvalue{$user_id}{$field}.'</td>';
                     push(@{$data[$rowcount]},$currvalue{$user_id}{$field});
@@ -1014,7 +1055,7 @@
     if (@users == 0) {
         $r->print('<td><font face="Arial,Helvetica,sans-serif">Sorry, no users matched your search criteria</font></td>');
     } else{
-        my $url = &build_excel($webpath,$event_id,$user,\@header,\@data);
+        my $url = &build_excel($webpath,$event_id,$event,$year,$user,\@header,\@data);
         $r->print('<td><font face="Arial,Helvetica,sans-serif"><a href="'.$url.'">Download an Excel file</a> with the following contents:<br /></font></td>
   </tr>
   <tr>
@@ -1044,16 +1085,32 @@
 }
 
 sub build_excel {
-    my ($webpath,$event_id,$user,$headings,$data) = @_;
+    my ($webpath,$event_id,$event,$year,$user,$headings,$data) = @_;
     my $ss = Spreadsheet::WriteExcel::Simple->new;
     $ss->write_bold_row($headings);
     for (my $i=0; $i<@{$data}; $i++) {  
         $ss->write_row($$data[$i]);
     }
     my $filename = $event_id.'-'.time.'-'.$user.'.xls';
-    my $path = '/home/helpdesk/web/'.$webpath.'excel/'.$filename;
+    my ($path,$url);
+    if ($webpath =~ /^http/) {
+        my $dirname = $year;
+        if ($event eq 'users conference') {
+            $dirname = 'conf'.$dirname;
+        } elsif ($event eq 'summer workshop') {
+            $dirname = 'workshop.$dirname; 
+        }
+        $path = '/home/helpdesk/web/events/'.$dirname.'/excel';
+        $url =  '/events/'.$dirname.'/excel/'.$filename;
+    } else {
+        $path = '/home/helpdesk/web/'.$webpath.'excel;
+        $url = $webpath.'excel/'.$filename;
+    }
+    if (!-e $path) {
+        mkdir($path,0700);
+    }
+    $path .= '/'.$filename;
     $ss->save($path);
-    my $url = $webpath.'excel/'.$filename;
     return $url;
 }