[LON-CAPA-cvs] cvs: loncom /cgi graph.png loncgi.pm

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 09 Oct 2003 22:04:37 -0000


This is a MIME encoded message

--matthew1065737077
Content-Type: text/plain

matthew		Thu Oct  9 18:04:37 2003 EDT

  Added files:                 
    /loncom/cgi	loncgi.pm 

  Modified files:              
    /loncom/cgi	graph.png 
  Log:
  Adding loncgi.pm, which enables the use of the users environment to pass data.
  Reworked graph.png to give error messages in htmlese and use loncgi.pm.  
  This change is not backwards compatable and I will be updating the modules which
  use graph.png to use the new convention.
  
  
--matthew1065737077
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20031009180437.txt"

Index: loncom/cgi/graph.png
diff -u loncom/cgi/graph.png:1.23 loncom/cgi/graph.png:1.24
--- loncom/cgi/graph.png:1.23	Thu Oct  9 11:56:41 2003
+++ loncom/cgi/graph.png	Thu Oct  9 18:04:37 2003
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 #
-# $Id: graph.png,v 1.23 2003/10/09 15:56:41 matthew Exp $
+# $Id: graph.png,v 1.24 2003/10/09 22:04:37 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -55,9 +55,11 @@
 =cut
 
 use strict;
+use lib '/home/httpd/lib/perl';
 use GD::Graph::bars;
 use GD::Graph::colour;
 use GD::Graph::Data;
+use LONCAPA::loncgi();
 
 sub unescape {
     my $str=shift;
@@ -65,37 +67,46 @@
     return $str;
 }
 
+if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
+    print <<END;
+Content-type: text/html
+
+<html>
+<head><title>Bad Cookie</title></head>
+<body>
+Your cookie information is incorrect.  What\'s up with that?
+</body>
+</html>
+END
+    return;
+}
+
 $|=1;   # Autoflush after each print/write
-my ($Titr,$xlab,$ylab,$Max,$NumBars,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'});
-$Titr = &unescape($Titr);
-$xlab = &unescape($xlab);
-$ylab = &unescape($ylab);
+my $identifier = $ENV{'QUERY_STRING'};
+my $Title   = &unescape($ENV{$identifier.'.title'});
+my $xlabel  = &unescape($ENV{$identifier.'.xlabel'});
+my $ylabel  = &unescape($ENV{$identifier.'.ylabel'});
+my $Max     = $ENV{$identifier.'.Max'};
+my $NumBars = $ENV{$identifier.'.NumBars'};
+my $data1   = $ENV{$identifier.'.data1'};
+my $data2   = $ENV{$identifier.'.data2'};
 
 my @data11=split(/\,/,$data1);
 my @data12=split(/\,/,$data2);
 my $skip_x = 1;
-#my $bar_space=1;
 my $bar_width=10;
 
+#
+# Labels are always digits
 my @xlabels;
-
 for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) {
     $xlabels[$nIdx]=$nIdx+1;
 }
 
-#if ($Titr =~ /^Percentage$/){
-#    for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) {
-#        $xlabels[$nIdx]=$nIdx;
-#    }
-#    @data11=();
-#    @data11=split(/\,/,$data2);
-#    @data12=();
-#    $Titr = '';
-#} else {
-#} 
-
 my @data =(\@xlabels,\@data11,\@data12);
 
+#
+# Customize graph based on the 
 my $width;
 my $height = 200;
 
@@ -123,40 +134,84 @@
 }
 
 my $MyGraph = GD::Graph::bars->new($width,$height);
+my $error = '';
+if (! $MyGraph->set( x_label         => $xlabel,
+                     y_label         => $ylabel,
+                     x_label_position => 0.5,
+                     long_ticks      => 1,
+                     tick_length     => 0,
+                     x_ticks         => 0,
+                     title           => $Title,
+                     y_max_value     => $Max,
+                     x_label_skip    => $skip_x,   
+                     x_tick_offset   => $x_tick_offset,
+                     #
+                     dclrs           => [ qw(lgreen dgreen lyellow 
+                                             lpurple cyan lorange)],
+                     bar_width       => $bar_width,
+                     cumulate        => 2,
+                     zero_axis       => 1,
+                     fgclr           => 'black',
+                     boxclr          => 'white',
+                     accentclr       => 'dblue',
+                     valuesclr       => '#ffff77',
+                     l_margin        => 10,
+                     b_margin        => 10,
+                     r_margin        => 10,
+                     t_margin        => 10,
+                     #
+                     transparent     => 0,
+                     )) {
+    $error = $MyGraph->error;
+    print <<"END";
+Content-type: text/html
+
+<html>
+<head><title>Bad Graph</title></head>
+<body>
+<p>
+There was an error producing the graph you requested.
+</p><p>
+$error
+</p>
+</body>
+</html>
+END
+    return;
+}
 
-$MyGraph->set( 
-    x_label         => $xlab,
-    y_label         => $ylab,
-    x_label_position => 0.5,
-    long_ticks      => 1,
-    tick_length     => 0,
-    x_ticks         => 0,
-    title           => $Titr,
-    y_max_value     => $Max,
-#    y_tick_number   => $ytic,
-#    y_label_skip    => 5,
-    x_label_skip    => $skip_x,   
-    x_tick_offset   => $x_tick_offset,
-
-    dclrs           => [ qw( lgreen dgreen lyellow lpurple cyan lorange)],
-    bar_width       => $bar_width,
-#    bar_spacing     => $bar_space,
-    cumulate        => 2,
-    zero_axis        => 1,
-
-#    legend_placement    => 'RT',
-
-    fgclr               => 'black',
-    boxclr              => 'white',
-    accentclr           => 'dblue',
-    valuesclr           => '#ffff77',
-    l_margin            => 10,
-    b_margin            => 10,
-    r_margin            => 10,
-    t_margin            => 10,
+my $plot = $MyGraph->plot(\@data);
+if (! defined($plot)) {
+    print <<"END";
+Content-type: text/html
+
+<html>
+<head><title>Bad Graph</title></head>
+<body>
+The system was unable to create the graph you requested.
+</body>
+</html>
+END
+    return;
+}
 
-    transparent     => 0,
-) or warn $MyGraph->error; 
+my $BinaryData=$plot->png;
+undef($MyGraph);
+undef($plot);
+
+if (! defined($BinaryData)) {
+    print <<"END";
+Content-type: text/html
+
+<html>
+<head><title>Bad Graph</title></head>
+<body>
+The system was unable to produce a png image of the graph you requested.
+</body>
+</html>
+END
+    return;
+}
 
 
 # Tell the server we are sending a png graphic
@@ -165,8 +220,6 @@
 
 END
 
-my $BinaryData=$MyGraph->plot(\@data)->png;
-undef $MyGraph;
 binmode(STDOUT);
 #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
 #print IMG $BinaryData; # output image

Index: loncom/cgi/loncgi.pm
+++ loncom/cgi/loncgi.pm
#
# LON-CAPA helpers for cgi-bin scripts
#
# $Id: loncgi.pm,v 1.1 2003/10/09 22:04:37 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#############################################
#############################################

=pod

=head1 NAME

loncgi

=head1 SYNOPSIS

Provides subroutines for checking a LON-CAPA cookie and loading the users
environment.

=head1 Subroutines

=over 4 

=cut

#############################################
#############################################
package LONCAPA::loncgi;

use strict;
use warnings FATAL=>'all';
no warnings 'uninitialized';
use CGI();
use CGI::Cookie();
use Fcntl qw(:flock);
use LONCAPA::Configuration();

my $lonidsdir;

BEGIN {
    my $perlvar=LONCAPA::Configuration::read_conf('loncapa.conf');
    delete $perlvar->{'lonReceipt'};
    $lonidsdir = $perlvar->{'lonIDsDir'};
}

#############################################
#############################################

=pod

=item check_cookie_and_load_env

Inputs: none

Returns: 1 if the user has a LON-CAPA cookie 0 if not.
Loads the users environment into the %ENV hash if the cookie is correct.

=cut

#############################################
#############################################
sub check_cookie_and_load_env {
    my %cookies=fetch CGI::Cookie;
    if (exists($cookies{'lonID'}) && 
        -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
        # cookie found
        &transfer_profile_to_env($cookies{'lonID'}->value);
        return 1;
    } else {
        # No cookie found
        return 0;
    }
}

#############################################
#############################################

=pod

=item check_cookie

Inputs: none

Returns: 1 if the user has a LON-CAPA cookie and 0 if not.

=cut

#############################################
#############################################
sub check_cookie {
    my %cookies=fetch CGI::Cookie;
    if (exists($cookies{'lonID'}) && 
        -e "$lonidsdir/".$cookies{'lonID'}->value.".id") {
        # cookie found
        return 1;
    } else {
        # No cookie found
        return 0;
    }
}

#############################################
#############################################

=pod

=item transfer_profile_to_env

Load the users environment into the %ENV hash.

Inputs: $handle, the name of the users LON-CAPA cookie.

Returns: undef

=cut

#############################################
#############################################
sub transfer_profile_to_env {
    my ($handle)=@_;
    my @profile;
    {
        open(IDFILE, "<$lonidsdir/$handle.id");
        flock(IDFILE,LOCK_SH);
        @profile=<IDFILE>;
        close(IDFILE);
    }
    foreach my $envrow (@profile) {
        chomp($envrow);
        my ($envname,$envvalue)=split(/=/,$envrow);
        $ENV{$envname} = $envvalue;
    }
    $ENV{'user.environment'} = "$lonidsdir/$handle.id";
    return undef;
}

#############################################
#############################################

=pod

=back

=cut

1;

__END__

--matthew1065737077--