[LON-CAPA-cvs] cvs: loncom /interface/spreadsheet Spreadsheet.pm assesscalc.pm classcalc.pm lonspreadsheet.pm studentcalc.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 23 May 2003 19:36:04 -0000


This is a MIME encoded message

--matthew1053718564
Content-Type: text/plain

matthew		Fri May 23 15:36:04 2003 EDT

  Modified files:              
    /loncom/interface/spreadsheet	Spreadsheet.pm assesscalc.pm 
                                 	classcalc.pm lonspreadsheet.pm 
                                 	studentcalc.pm 
  Log:
  Added output selector and implemented excel output.
  
  Moved $spreadsheet->display() to Spreadsheet.pm instead of having it reside
      three times in the descendents of the spreadsheet object.
  Moved $spreadsheet->outsheet_excel() to Spreadsheet.pm for the same reason.
  Created Spreadsheet::output_selector, which allows the user to select the 
      output mode they desire.
  Created $spreadsheet->excel_output_row which takes care of writing a row
      to an excel worksheet.
  classcalc.pm, studentcalc.pm, and assesscalc.pm each had the same work done
  on them:
    Created &excel_rows(), which writes the body of the spreadsheets to 
       an excel file.
    Rewrote &get_title to return an array of title lines.
    Created &get_html_title to return an HTML version of the results of
       &get_title.
  
  classcalc.pm and Spreadsheet.pm each have the following change:
      Created &html_header to return appropriate HTML for the spreadsheet.
  
  
--matthew1053718564
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20030523153604.txt"

Index: loncom/interface/spreadsheet/Spreadsheet.pm
diff -u loncom/interface/spreadsheet/Spreadsheet.pm:1.4 loncom/interface/spreadsheet/Spreadsheet.pm:1.5
--- loncom/interface/spreadsheet/Spreadsheet.pm:1.4	Fri May 23 10:52:51 2003
+++ loncom/interface/spreadsheet/Spreadsheet.pm	Fri May 23 15:36:04 2003
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.4 2003/05/23 14:52:51 matthew Exp $
+# $Id: Spreadsheet.pm,v 1.5 2003/05/23 19:36:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1110,6 +1110,24 @@
 ## Output Helpers
 ##
 ###########################################################
+sub display {
+    my $self = shift;
+    my ($r) = @_;
+    $self->compute($r);
+    my $outputmode = 'html';
+    if ($ENV{'form.output_format'} =~ /^(html|excel|csv)$/) {
+        $outputmode = $ENV{'form.output_format'};
+    }
+    if ($outputmode eq 'html') {
+        $self->outsheet_html($r);
+    } elsif ($outputmode eq 'excel') {
+        $self->outsheet_excel($r);
+    } elsif ($outputmode eq 'csv') {
+        $self->outsheet_csv($r);
+    }
+    return;
+}
+
 ############################################
 ##         HTML output routines           ##
 ############################################
@@ -1212,6 +1230,55 @@
     return $row_html;
 }
 
+sub html_header {
+    my $self = shift;
+    return '' if (! $ENV{'request.role.adv'});
+    return "<table>\n".
+        '<tr><th align="center">Output Format</th><tr>'."\n".
+        '<tr><td>'.&output_selector()."</td></tr>\n".
+        "</table>\n";
+}
+
+sub output_selector {
+    my $output_selector = '<select name="output_format" size="3">'."\n";
+    my $default = 'html';
+    if (exists($ENV{'form.output_format'})) {
+        $default = $ENV{'form.output_format'} 
+    } else {
+        $ENV{'form.output_format'} = $default;
+    }
+    foreach (['html','HTML'],
+             ['excel','Excel'],
+             ['csv','Comma Seperated Values']) {
+        my ($name,$description) = @{$_};
+        $output_selector.=qq{<option value="$name"};
+        if ($name eq $default) {
+            $output_selector .= ' selected';
+        }
+        $output_selector .= ">$description</option>\n";
+    }
+    $output_selector .= "</select>\n";
+    return $output_selector;
+}
+
+################################################
+##          Excel output routines             ##
+################################################
+sub excel_output_row {
+    my $self = shift;
+    my ($worksheet,$rownum,$rows_output,@prepend) = @_;
+    my $cols_output = 0;
+    #
+    my @rowdata = $self->get_row($rownum);
+    foreach my $cell (@prepend,@rowdata) {
+        my $value = $cell;
+        $value = $cell->{'value'} if (ref($value));
+        $value =~ s/\&nbsp;/ /gi;
+        $worksheet->write($rows_output,$cols_output++,$value);
+    }
+    return;
+}
+
 sub create_excel_spreadsheet {
     my $self = shift;
     my ($r) = @_;
@@ -1235,6 +1302,44 @@
     #
     # Determine the name to give the worksheet
     return ($workbook,$filename);
+}
+
+sub outsheet_excel {
+    my $self = shift;
+    my ($r) = @_;
+    $r->print("<h2>Preparing Excel Spreadsheet</h2>");
+    #
+    # Create excel worksheet
+    my ($workbook,$filename) = $self->create_excel_spreadsheet($r);
+    return if (! defined($workbook));
+    #
+    # Create main worksheet
+    my $worksheet = $workbook->addworksheet('main');
+    my $rows_output = 0;
+    my $cols_output = 0;
+    #
+    # Write excel header
+    foreach my $value ($self->get_title()) {
+        $cols_output = 0;
+        $worksheet->write($rows_output++,$cols_output,$value);
+    }
+    $rows_output++;    # skip a line
+    #
+    # Write summary/export row
+    $cols_output = 0;
+    $self->excel_output_row($worksheet,0,$rows_output++,'Summary');
+    $rows_output++;    # skip a line
+    #
+    $self->excel_rows($worksheet,$cols_output,$rows_output);
+    #
+    #
+    # Close the excel file
+    $workbook->close();
+    #
+    # Write a link to allow them to download it
+    $r->print('<br />'.
+              '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
+    return;
 }
 
 ############################################
Index: loncom/interface/spreadsheet/assesscalc.pm
diff -u loncom/interface/spreadsheet/assesscalc.pm:1.7 loncom/interface/spreadsheet/assesscalc.pm:1.8
--- loncom/interface/spreadsheet/assesscalc.pm:1.7	Fri May 23 10:52:51 2003
+++ loncom/interface/spreadsheet/assesscalc.pm	Fri May 23 15:36:04 2003
@@ -1,5 +1,5 @@
 #
-# $Id: assesscalc.pm,v 1.7 2003/05/23 14:52:51 matthew Exp $
+# $Id: assesscalc.pm,v 1.8 2003/05/23 19:36:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -313,18 +313,30 @@
     return '';
 }
 
+sub get_html_title {
+    my $self = shift;
+    my ($assess_title,$name,$time) = $self->get_title();
+    my $title = '<h1>'.$assess_title.'</h1>'.
+        '<h2>'.$name.', '.
+        &Apache::loncommon::aboutmewrapper
+                         ($self->{'name'}.'@'.$self->{'domain'},
+                          $self->{'name'},$self->{'domain'});
+    $title .= '<h3>'.$time.'</h3>';
+    return $title;
+}
+
 sub get_title {
     my $self = shift;
-    my $title;
+    my @title = ();
     if (($self->{'usymb'} eq '_feedback') ||
         ($self->{'usymb'} eq '_evaluation') ||
         ($self->{'usymb'} eq '_discussion') ||
         ($self->{'usymb'} eq '_tutoring')) {
-        $title = $self->{'usymb'};
-        $title =~ s/^_//;
-        $title = '<h1>'.ucfirst($title)."</h1>\n";
+        my $assess_title = ucfirst($self->{'usymb'});
+        $assess_title =~ s/^_//;
+        push(@title,$assess_title);
     } else {
-        $title = '<h1>'.&Apache::lonnet::gettitle($self->{'symb'})."</h1>\n";
+        push(@title,&Apache::lonnet::gettitle($self->{'symb'}));
     }
     # Look up the users identifying information
     # Get the users information
@@ -333,13 +345,9 @@
     my $name = 
         join(' ',@userenv{'firstname','middlename','lastname','generation'});
     $name =~ s/\s+$//;
-    $title .= '<h2>'.$name.', '.
-        &Apache::loncommon::aboutmewrapper($self->{'name'}.'@'.$self->{'domain'},
-                                           $self->{'name'},$self->{'domain'}).
-                                           "</h2>\n";
-    $title .= '<h3>'.localtime(time).'</h3>';
-    #
-    return $title;
+    push (@title,$name);
+    push (@title,scalar(localtime(time)));
+    return @title;
 }
 
 sub parent_link {
@@ -405,7 +413,7 @@
     }
     #
     my $num_output = 0;
-    foreach my $rownum ($self->rows()) {
+    foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
 	if ($num_output++ % 50 == 0) {
 	    $r->print("</table>\n".$tableheader);
 	}
@@ -419,7 +427,6 @@
 sub assess_html_row {
     my $self = shift();
     my ($num_uneditable,$row) = @_;
-    my $requester_is_student = ($ENV{'request.role'} =~ /^st\./);
     my $parameter_name = $self->{'formulas'}->{'A'.$row};
     my @rowdata = $self->get_row($row);
     my $num_cols_output = 0;
@@ -432,14 +439,8 @@
         $row_html .= '<td>'.$parameter_name.'</td>';
     }
     foreach my $cell (@rowdata) {
-	if ($requester_is_student || 
-	    $num_cols_output++ < $num_uneditable) {
-	    $row_html .= '<td bgcolor="#FFDDDD">';
-	    $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,'#FFDDDD');
-	} else {
-	    $row_html .= '<td bgcolor="#EOFFDD">';
-	    $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,'#E0FFDD');
-	}
+        $row_html .= '<td bgcolor="#EOFFDD">';
+        $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,'#E0FFDD');
 	$row_html .= '</td>';
     }
     return $row_html;
@@ -447,19 +448,34 @@
 
 sub outsheet_csv {
     my $self = shift;
-    my ($r)=@_;
-}
-
-sub outsheet_excel {
-    my $self = shift;
-    my ($r)=@_;
+    my ($r) = @_;
+    $r->print('<h1>csv output is not supported yet</h1>');
 }
 
-sub display {
+sub excel_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
-    $self->compute();
-    $self->outsheet_html($r);
+    my ($worksheet,$cols_output,$rows_output) = @_;
+    #
+    # Write a header row
+    $cols_output = 0;
+    foreach my $value ('Parameter','Description','Value') {
+        $worksheet->write($rows_output,$cols_output++,$value);
+    }
+    $rows_output++;    
+    #
+    # Write each row
+    foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
+        my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
+        my $description = '';
+        if (exists($nice_parameter_name{$parameter_name})) {
+            $description = $nice_parameter_name{$parameter_name};
+        }
+        $self->excel_output_row($worksheet,$rownum,$rows_output++,
+                                $parameter_name,$description);
+    }
+    return;
 }
 
 sub compute {
Index: loncom/interface/spreadsheet/classcalc.pm
diff -u loncom/interface/spreadsheet/classcalc.pm:1.2 loncom/interface/spreadsheet/classcalc.pm:1.3
--- loncom/interface/spreadsheet/classcalc.pm:1.2	Mon May 19 10:30:31 2003
+++ loncom/interface/spreadsheet/classcalc.pm	Fri May 23 15:36:04 2003
@@ -1,5 +1,5 @@
 #
-# $Id: classcalc.pm,v 1.2 2003/05/19 14:30:31 matthew Exp $
+# $Id: classcalc.pm,v 1.3 2003/05/23 19:36:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -58,6 +58,22 @@
 
 @Apache::classcalc::ISA = ('Apache::Spreadsheet');
 
+sub html_header {
+    my $self = shift;
+    my ($toprow,$bottomrow);
+    foreach (['Sections','Section selector goes here'],
+             ['Enrollment Status',&Apache::lonhtmlcommon::StatusOptions(undef,undef,3)],
+             ['Output Format',&Apache::Spreadsheet::output_selector()]) {
+        my ($name,$selector) = @{$_};
+        $toprow .= '<th align="center"><b>'.$name.'</b></th>';
+        $bottomrow .= '<td>'.$selector.'</td>';
+    }
+    return "<p>\n<table>\n".
+        "<tr>".$toprow."</tr>\n".
+        "<tr>".$bottomrow."</tr>\n".
+        "</table>\n</p>";
+}
+
 sub get_classlist {
     my $self = shift;
     # Retrieve the classlist
@@ -80,8 +96,15 @@
 
 sub get_title {
     my $self = shift;
-    my $title = '<h1>'.$self->{'coursedesc'}."</h1>\n";
     # Section info should be included
+    my @title = ($self->{'coursedesc'}, scalar(localtime(time)) );
+    return @title;
+}
+
+sub get_html_title {
+    my $self = shift;
+    my ($classcalc_title,$time) = $self->get_title();
+    my $title = '<h1>'.$classcalc_title."</h1>\n".'<h3>'.$time."</h3>\n";
     return $title;
 }
 
@@ -172,26 +195,45 @@
 sub outsheet_csv {
     my $self = shift;
     my ($r) = @_;
+    $r->print('<h1>csv output is not supported yet</h1>');
 }
 
-sub outsheet_excel {
+
+sub excel_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
+    my ($worksheet,$cols_output,$rows_output) = @_;
+    #
+    # Write a header row
+    $cols_output = 0;
+    foreach my $value ('fullname','username','domain','section','status') {
+        $worksheet->write($rows_output,$cols_output++,$value);
+    }
+    $rows_output++;    
+    #
+    # Write each students row
+    foreach my $student ($self->get_classlist()) {
+        $cols_output = 0;
+	my $rownum = $self->get_row_number_from_key
+	    ($student->{'username'}.':'.$student->{'domain'});
+        $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
+        my @studentdata = ($student->{'fullname'},
+                           $student->{'username'},
+                           $student->{'domain'},
+                           $student->{'section'},
+                           $student->{'status'});
+        $self->excel_output_row($worksheet,$rownum,$rows_output++,
+                                @studentdata);
+    }
+    return;
 }
 
+
 sub outsheet_recursive_excel {
     my $self = shift;
     my ($r) = @_;
 } 
-
-sub display {
-    my $self = shift;
-    my ($r) = @_;
-    $self->compute($r);
-    # display as html/csv/excel/etc....
-    $self->outsheet_html($r);
-    return;
-}
 
 sub compute {
     my $self = shift;
Index: loncom/interface/spreadsheet/lonspreadsheet.pm
diff -u loncom/interface/spreadsheet/lonspreadsheet.pm:1.3 loncom/interface/spreadsheet/lonspreadsheet.pm:1.4
--- loncom/interface/spreadsheet/lonspreadsheet.pm:1.3	Mon May 19 11:53:07 2003
+++ loncom/interface/spreadsheet/lonspreadsheet.pm	Fri May 23 15:36:04 2003
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.3 2003/05/19 15:53:07 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.4 2003/05/23 19:36:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -99,7 +99,7 @@
     # Check the course homeserver
     $loaderror= &Apache::lonnet::overloaderror($r,
                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
-    if ($loaderror) { return $loaderror; } 
+#    if ($loaderror) { return $loaderror; } 
     #
     # HTML Header
     #
@@ -320,10 +320,13 @@
         $r->rflush();
     }
     #
+    # Output selector
+    $r->print($spreadsheet->html_header());
+    #
     # Keep track of the filename
     $r->print(&hiddenfield('filename',$filename));
     #
-    $r->print($spreadsheet->get_title());
+    $r->print($spreadsheet->get_html_title());
     if ($allowed_to_view || $allowed_to_edit) {
         $r->print($spreadsheet->parent_link());
     }
Index: loncom/interface/spreadsheet/studentcalc.pm
diff -u loncom/interface/spreadsheet/studentcalc.pm:1.5 loncom/interface/spreadsheet/studentcalc.pm:1.6
--- loncom/interface/spreadsheet/studentcalc.pm:1.5	Fri May 23 10:52:51 2003
+++ loncom/interface/spreadsheet/studentcalc.pm	Fri May 23 15:36:04 2003
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.5 2003/05/23 14:52:51 matthew Exp $
+# $Id: studentcalc.pm,v 1.6 2003/05/23 19:36:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -91,14 +91,25 @@
 
 sub get_title {
     my $self = shift;
-    my $title = '';
+    my @title = ();
+    #
+    # Determine the students name
     my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
                                                       $self->{'domain'});
-    &Apache::lonnet::logthis('userenv = '.join(' ',%userenv));
-    my $name = 
-        join(' ',@userenv{'firstname','middlename','lastname','generation'});
+    my $name = join(' ',
+                 @userenv{'firstname','middlename','lastname','generation'});
     $name =~ s/\s+$//;
-    $title .= '<h1>'.$name;
+
+    push (@title,$name);
+    push (@title,$self->{'coursedesc'});
+    push (@title,scalar(localtime(time)));
+    return @title;
+}
+
+sub get_html_title {
+    my $self = shift;
+    my ($name,$desc,$time) = $self->get_title();
+    my $title = '<h1>'.$name;
     if ($ENV{'user.name'} ne $self->{'name'} && 
         $ENV{'user.domain'} ne $self->{'domain'}) {
         $title .= &Apache::loncommon::aboutmewrapper
@@ -106,8 +117,8 @@
                                      $self->{'name'},$self->{'domain'});
     }
     $title .= "</h1>\n";
-    $title .= '<h2>'.$self->{'coursedesc'}."</h2>\n";
-    $title .= '<h3>'.localtime(time).'</h3>';
+    $title .= '<h2>'.$desc."</h2>\n";
+    $title .= '<h3>'.$time.'</h3>';
     return $title;
 }
 
@@ -304,23 +315,43 @@
 sub outsheet_csv {
     my $self = shift;
     my ($r) = @_;
+    $r->print('<h1>csv output is not supported yet</h1>');
 }
-sub outsheet_excel {
+
+sub excel_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
+    my ($worksheet,$cols_output,$rows_output) = @_;
+    #
+    # Write a header row
+    $cols_output = 0;
+    foreach my $value ('Container','Assessment title') {
+        $worksheet->write($rows_output,$cols_output++,$value);
+    }
+    $rows_output++;    
+    #
+    # Write each assessments row
+    if (scalar(@Sequences)< 1) {
+        &initialize_sequence_cache();
+    }
+    foreach my $Sequence (@Sequences) {
+	next if ($Sequence->{'num_assess'} < 1);
+	foreach my $resource (@{$Sequence->{'contents'}}) {
+	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
+            my @assessdata = ($Sequence->{'title'},
+                              $resource->{'title'});
+            $self->excel_output_row($worksheet,$rownum,$rows_output++,
+                                    @assessdata);
+        }
+    }
+    return;
 }
+
 sub outsheet_recursive_excel {
     my $self = shift;
     my ($r) = @_;
 } 
-
-sub display {
-    my $self = shift;
-    my ($r) = @_;
-    $self->compute();
-    $self->outsheet_html($r);
-    return;
-}
 
 sub set_row_sources {
     my $self = shift;

--matthew1053718564--