[LON-CAPA-cvs] cvs: loncom /interface londropadd.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 08 Nov 2002 20:51:48 -0000


matthew		Fri Nov  8 15:51:48 2002 EDT

  Modified files:              
    /loncom/interface	londropadd.pm 
  Log:
  Added excel spreadsheet as format for viewing classlist.
  
  
Index: loncom/interface/londropadd.pm
diff -u loncom/interface/londropadd.pm:1.59 loncom/interface/londropadd.pm:1.60
--- loncom/interface/londropadd.pm:1.59	Wed Oct 16 14:45:10 2002
+++ loncom/interface/londropadd.pm	Fri Nov  8 15:51:48 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to drop and add students in courses 
 #
-# $Id: londropadd.pm,v 1.59 2002/10/16 18:45:10 matthew Exp $
+# $Id: londropadd.pm,v 1.60 2002/11/08 20:51:48 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,6 +41,7 @@
 use Apache::loncommon();
 use Apache::lonhtmlcommon();
 use Apache::Constants qw(:common :http REDIRECT);
+use Spreadsheet::WriteExcel;
 
 ###############################################################
 ###############################################################
@@ -771,6 +772,7 @@
         $r->print(<<END);
 <font size="+1">
 <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
+<a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
 </font>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 END
@@ -793,14 +795,15 @@
 }
 
 # ============================================== view classlist
-sub print_csv_classlist {
+sub print_formatted_classlist {
     my $r=shift;
+    my $mode = shift;
     my $cid=$ENV{'request.course.id'};
     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
     if (! defined($classlist)) {
         $r->print("There are no students currently enrolled.\n");
     } else {
-        &show_class_list($r,'csv','nolink','csv',
+        &show_class_list($r,$mode,'nolink','csv',
                          $ENV{'form.Status'},$classlist,$keylist);
     }
 }
@@ -809,8 +812,10 @@
 sub show_class_list {
     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
     my $cid=$ENV{'request.course.id'};
-#    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-#                                            ['sortby']);
+    #
+    # Variables for excel output
+    my ($excel_workbook, $excel_sheet, $excel_filename,$row);
+    #
     my $sortby = $ENV{'form.sortby'};
     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
         $sortby = 'username';
@@ -853,6 +858,22 @@
             $r->print('"'.join('","',("username","domain","ID","student name",
                                       "section")).'"'."\n");
         }
+    } elsif ($mode eq 'excel') {
+        # Create the excel spreadsheet
+        $excel_filename = '/prtspool/'.
+            $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+                time.'_'.rand(1000000000).'.xls';
+        $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
+                                                       $excel_filename);
+        $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
+        $excel_sheet = $excel_workbook->addworksheet('classlist');
+        #
+        my $description = 'Classlist for '.
+            $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
+        $excel_sheet->write($row++,0,$description);
+        #
+        $excel_sheet->write($row++,0,["username","domain","ID",
+                                      "student name","section","status"]);
     }
     #
     # Sort the students
@@ -917,9 +938,18 @@
             $" = '","';
             $r->print("\"@line\"\n");
             $" = $tmp;
+        } elsif ($mode eq 'excel') {
+            $excel_sheet->write($row++,0,[$username,$domain,$id,
+                                          $name,$section,$status]);
         }
     }
-    $r->print('</table><br>') if ($mode eq 'view');
+    if ($mode eq 'view') {
+        $r->print('</table><br>');
+    } elsif ($mode eq 'excel') {
+        $excel_workbook->close();
+        $r->print('<p><a href="'.$excel_filename.'">'.
+                  'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
+    }
 }
 
 
@@ -1638,7 +1668,9 @@
         if (! exists($ENV{'form.state'})) {
             &print_html_classlist($r);
         } elsif ($ENV{'form.state'} eq 'csv') {
-            &print_csv_classlist($r);
+            &print_formatted_classlist($r,'csv');
+        } elsif ($ENV{'form.state'} eq 'excel') {
+            &print_formatted_classlist($r,'excel');
         } else {
             &print_html_classlist($r);
         }