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

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 26 Feb 2004 22:42:22 -0000


This is a MIME encoded message

--matthew1077835342
Content-Type: text/plain

matthew		Thu Feb 26 17:42:22 2004 EDT

  Modified files:              
    /loncom/interface	londropadd.pm 
  Log:
  Interface tweaks for html/csv/excel generation of classlist view.  Needs
  to be tested more....
  
  
--matthew1077835342
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20040226174222.txt"

Index: loncom/interface/londropadd.pm
diff -u loncom/interface/londropadd.pm:1.102 loncom/interface/londropadd.pm:1.103
--- loncom/interface/londropadd.pm:1.102	Fri Feb 20 14:44:59 2004
+++ loncom/interface/londropadd.pm	Thu Feb 26 17:42:21 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to drop and add students in courses 
 #
-# $Id: londropadd.pm,v 1.102 2004/02/20 19:44:59 matthew Exp $
+# $Id: londropadd.pm,v 1.103 2004/02/26 22:42:21 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1100,7 +1100,7 @@
 
 # ============================================== view classlist
 sub print_html_classlist {
-    my $r=shift;
+    my ($r,$mode) = @_;
     if (! exists($ENV{'form.sortby'})) {
         $ENV{'form.sortby'} = 'username';
     }
@@ -1108,13 +1108,14 @@
         $ENV{'form.Status'} = 'Active';
     }
     my $status_select = &Apache::lonhtmlcommon::StatusOptions
-        ($ENV{'form.Status'},'studentform');
+        ($ENV{'form.Status'});
     my $cid=$ENV{'request.course.id'};
     my $cdom=$ENV{'course.'.$cid.'.domain'};
     my $cnum=$ENV{'course.'.$cid.'.num'};
-# -------------------------------------------------------- Get course personnel
+    #
+    # List course personnel
     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
-    $r->print('<table border="2">');
+    $r->print('<br /><table border="2">');
     foreach (sort keys %coursepersonnel) {
 	$r->print('<tr><td>'.$_.'</td><td>');
         foreach (split(/\,/,$coursepersonnel{$_})) {
@@ -1126,31 +1127,38 @@
         $r->print('</td></tr>');
     }
     $r->print('</table>');
-# --------------------------------------------------------------- Student roles
+    #
+    # Interface output
     my $CCL=&mt('Current Class List');
-    $r->print(<<END);
-<input type="hidden" name="action" value="$ENV{'form.action'}" />
-<input type="hidden" name="state"  value="" />
-<p>
-<font size="+1">$CCL</font>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-END
+    $r->print('<input type="hidden" name="action" value="'.
+              $ENV{'form.action'}.'" />');
+    $r->print("<p>\n");
     if ($ENV{'form.action'} ne 'modifystudent') {
-	my %lt=&Apache::lonlocal::texthash(
-		   'ef'   => "Excel format",
-                   'ss'   => "Student Status",
-					   );
-        $r->print(<<END);
-<font size="+1">
-<a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-<a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">$lt{'ef'}</a>
-</font>
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-$lt{'ss'}:
-END
+	my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
+                                           'excel' => "Excel",
+                                           'html'  => 'HTML');
+        $r->print('<font size="+1">');
+        my $output_selector = '<select size="1" name="state" >';
+        if ($ENV{'form.state'} !~ /^(csv|excel|html)$/ ) {
+            $ENV{'form.state'} = 'html';
+        }
+        foreach my $outputformat ('html','csv','excel') {
+            my $option = '<option value="'.$outputformat.'" ';
+            if ($outputformat eq $ENV{'form.state'}) {
+                $option .= 'checked ';
+            }
+            $option .='>'.$lt{$outputformat}.'</option>';
+            $output_selector .= "\n".$option;
+        }
+        $output_selector .= '</select>';
+        $r->print(&mt('Output Format: [_1]',$output_selector).('&nbsp;'x3));
     }
-    $r->print($status_select."</p>\n");
+    $r->print(&mt('Student Status: [_1]',$status_select)."\n");
+    $r->print('<input type="submit" value="'.&mt('Refresh Display').'" />'."\n</p>\n");
+
+    #
+    # Print the classlist
+    $r->print('<h2>'.&mt('Current Class List').'</h2>');
     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
     if (! defined($classlist)) {
         $r->print(&mt('There are no students currently enrolled.')."\n");
@@ -1159,27 +1167,16 @@
         if ($ENV{'form.action'} eq 'modifystudent') {
             &show_class_list($r,'view','modify','modifystudent',
                              $ENV{'form.Status'},$classlist,$keylist);
-        } else {
+        } elsif (! defined($mode) || $mode eq '') {
             &show_class_list($r,'view','aboutme','classlist',
                              $ENV{'form.Status'},$classlist,$keylist);
+        } elsif ($mode eq 'csv' || $mode eq 'excel') {
+            &show_class_list($r,$mode,'nolink','csv',
+                             $ENV{'form.Status'},$classlist,$keylist);
         }
     }
 }
 
-# ============================================== view 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(&mt('There are no students currently enrolled.')."\n");
-    } else {
-        &show_class_list($r,$mode,'nolink','csv',
-                         $ENV{'form.Status'},$classlist,$keylist);
-    }
-}
-
 # =================================================== Show student list to drop
 sub show_class_list {
     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
@@ -1188,11 +1185,19 @@
     # Variables for excel output
     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
     #
+    # Variables for csv output
+    my ($CSVfile,$CSVfilename);
+    #
     my $sortby = $ENV{'form.sortby'};
     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
         $sortby = 'username';
     }
     # Print out header 
+    $r->print(<<END);
+<input type="hidden" name="sortby" value="$sortby" />
+<input type="hidden" name="sname"  value="" />
+<input type="hidden" name="sdom"   value="" />
+END
     if ($mode eq 'view') {
         if ($linkto eq 'aboutme') {
             $r->print(&mt('Select a user name to view the users personal page.'));
@@ -1226,16 +1231,33 @@
 </tr>
 END
     } elsif ($mode eq 'csv') {
+	#
+	# Open a file
+	$CSVfilename = '/prtspool/'.
+	    $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+            time.'_'.rand(1000000000).'.csv';
+	unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
+	    $r->log_error("Couldn't open $CSVfilename for output $!");
+	    $r->print("Problems occured in writing the csv file.  ".
+		      "This error has been logged.  ".
+		      "Please alert your LON-CAPA administrator.");
+	    $CSVfile = undef;
+	}
+	#
+	# Write headers and data to file
         if($statusmode eq 'Expired') {
-            $r->print(&mt('Students with expired roles'));
+            print $CSVfile '"'.&mt('Students with expired roles').'"'."\n";
         }
         if ($statusmode eq 'Any') {
-            $r->print('"'.join('","',(&mt("username"),&mt("domain"),"ID",
-                      &mt("student name"),&mt("section"),&mt("status"))).
-                      '"'."\n");
+            print $CSVfile '"'.join('","',map {
+		&Apache::loncommon::csv_translate(&mt($_))
+                } ("username","domain","ID","student name",
+                   "section","status")).'"'."\n";
         } else {
-            $r->print('"'.join('","',(&mt("username"),&mt("domain"),"ID",
-                      &mt("student name"),&mt("section"))).'"'."\n");
+            print $CSVfile '"'.join('","',map {
+		&Apache::loncommon::csv_translate(&mt($_))
+                } ("username","domain","ID","student name",
+                   "section")).'"'."\n";
         }
     } elsif ($mode eq 'excel') {
         # Create the excel spreadsheet
@@ -1305,6 +1327,7 @@
 </tr>
 END
         } elsif ($mode eq 'csv') {
+            next if (! defined($CSVfile));
             # no need to bother with $linkto
             my @line = ();
             foreach ($username,$domain,$id,$name,$section) {
@@ -1313,10 +1336,7 @@
             if ($statusmode eq 'Any') {
                 push @line,&Apache::loncommon::csv_translate($status);
             }
-            my $tmp = $";
-            $" = '","';
-            $r->print("\"@line\"\n");
-            $" = $tmp;
+            print $CSVfile '"'.join('","',@line).'"'."\n";
         } elsif ($mode eq 'excel') {
             $excel_sheet->write($row++,0,[$username,$domain,$id,
                                           $name,$section,$status]);
@@ -1328,6 +1348,12 @@
         $excel_workbook->close();
         $r->print('<p><a href="'.$excel_filename.'">'.
                   &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
+    } elsif ($mode eq 'csv') {
+        close($CSVfile);
+        $r->print('<a href="'.$CSVfilename.'">'.
+                  &mt('Your CSV file').'</a> is ready for download.'.
+                  "\n");
+        $r->rflush();
     }
 }
 
@@ -2121,14 +2147,10 @@
     #
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                                             ['state','action']);
-    if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
-        $r->content_type('text/csv');
-    } else {
-        # Start page
-        &Apache::loncommon::content_type($r,'text/html');
-        $r->send_http_header;
-        $r->print(&header());
-    }
+    # Start page
+    &Apache::loncommon::content_type($r,'text/html');
+    $r->send_http_header;
+    $r->print(&header());
     #
     # Main switch on form.action and form.state, as appropriate
     if (! exists($ENV{'form.action'})) {
@@ -2193,13 +2215,13 @@
         $r->print(&Apache::lonhtmlcommon::breadcrumbs
                   (undef,'View Classlist'));
         if (! exists($ENV{'form.state'})) {
-            &print_html_classlist($r);
+            &print_html_classlist($r,undef);
         } elsif ($ENV{'form.state'} eq 'csv') {
-            &print_formatted_classlist($r,'csv');
+            &print_html_classlist($r,'csv');
         } elsif ($ENV{'form.state'} eq 'excel') {
-            &print_formatted_classlist($r,'excel');
+            &print_html_classlist($r,'excel');
         } else {
-            &print_html_classlist($r);
+            &print_html_classlist($r,undef);
         }
     } elsif ($ENV{'form.action'} eq 'modifystudent') {
         &Apache::lonhtmlcommon::add_breadcrumb
@@ -2228,11 +2250,7 @@
     }
     #
     # Finish up
-    if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
-        $r->print("\n");
-    } else {
-        $r->print('</form></body></html>');
-    }
+    $r->print('</form></body></html>');
     return OK;
 }
 

--matthew1077835342--