--- loncom/interface/loncommon.pm	2003/10/16 20:03:31	1.131
+++ loncom/interface/loncommon.pm	2003/10/27 21:21:08	1.136
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.131 2003/10/16 20:03:31 albertel Exp $
+# $Id: loncommon.pm,v 1.136 2003/10/27 21:21:08 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -274,7 +274,7 @@ of the element the selection from the se
 sub browser_and_searcher_javascript {
     return <<END;
     var editbrowser = null;
-    function openbrowser(formname,elementname,only,omit) {
+    function openbrowser(formname,elementname,only,omit,titleelement) {
         var url = '/res/?';
         if (editbrowser == null) {
             url += 'launch=1&';
@@ -288,6 +288,9 @@ sub browser_and_searcher_javascript {
         if (omit != null) {
             url += 'omit=' + omit + '&';
         }
+        if (titleelement != null) {
+            url += 'titleelement=' + titleelement + '&';
+        }
         url += 'element=' + elementname + '';
         var title = 'Browser';
         var options = 'scrollbars=1,resizable=1,menubar=0';
@@ -296,7 +299,7 @@ sub browser_and_searcher_javascript {
         editbrowser.focus();
     }
     var editsearcher;
-    function opensearcher(formname,elementname) {
+    function opensearcher(formname,elementname,titleelement) {
         var url = '/adm/searchcat?';
         if (editsearcher == null) {
             url += 'launch=1&';
@@ -304,6 +307,9 @@ sub browser_and_searcher_javascript {
         url += 'catalogmode=interactive&';
         url += 'mode=edit&';
         url += 'form=' + formname + '&';
+        if (titleelement != null) {
+            url += 'titleelement=' + titleelement + '&';
+        }
         url += 'element=' + elementname + '';
         var title = 'Search';
         var options = 'scrollbars=1,resizable=1,menubar=0';
@@ -2663,7 +2669,7 @@ $uname, optional the username of the use
 
 sub check_if_partid_hidden {
     my ($id,$symb,$udom,$uname) = @_;
-    my $hiddenparts=&Apache::lonnet::EXT('resource.0.parameter_hiddenparts',
+    my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
 					 $symb,$udom,$uname);
     my @hiddenlist=split(/,/,$hiddenparts);
     foreach my $checkid (@hiddenlist) {
@@ -2672,23 +2678,33 @@ sub check_if_partid_hidden {
     return undef;
 }
 
+sub get_cgi_id {
+    return (time.'_'.int(rand(1000)));
+}
+
 ############################################################
 ############################################################
 
 =pod
 
-=item DrawGraph
-
-Returns a link to cgi-bin/graph
+=item DrawBarGraph
 
 =cut
 
 ############################################################
 ############################################################
-sub DrawGraph {
+sub DrawBarGraph {
     my ($Title,$xlabel,$ylabel,$Max,$colors,@Values)=@_;
     #
-    my $identifier = time.'_'.int(rand(1000));
+    if (! defined($colors)) {
+        $colors = ['#33ff00', 
+                  '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
+                  '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
+                  ]; 
+    }
+    #
+    my $identifier = &get_cgi_id();
+    my $id = 'cgi.'.$identifier;        
     if (! @Values || ref($Values[0]) ne 'ARRAY') {
         return '';
     }
@@ -2697,15 +2713,50 @@ sub DrawGraph {
     my $NumSets=1;
     foreach my $array (@Values) {
         next if (! ref($array));
-        $ValuesHash{$identifier.'.data.'.$NumSets++} = join(',',@$array);
+        $ValuesHash{$id.'.data.'.$NumSets++} = 
+            join(',',@$array);
+    }
+    #
+    my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
+    if ($NumBars < 10) {
+        $width = 120+$NumBars*15;
+        $xskip = 1;
+        $bar_width = 15;
+    } elsif ($NumBars <= 25) {
+        $width = 120+$NumBars*11;
+        $xskip = 5;
+        $bar_width = 8;
+    } elsif ($NumBars <= 50) {
+        $width = 120+$NumBars*8;
+        $xskip = 5;
+        $bar_width = 4;
+    } else {
+        $width = 120+$NumBars*8;
+        $xskip = 5;
+        $bar_width = 4;
+    }
+    #
+    my @Labels;
+    for (my $i=0;$i<@{$Values[0]};$i++) {
+        push (@Labels,$i+1);
     }
     #
     $Title  = '' if (! defined($Title));
     $xlabel = '' if (! defined($xlabel));
     $ylabel = '' if (! defined($ylabel));
-    $Title  = &Apache::lonnet::escape($Title);
-    $xlabel = &Apache::lonnet::escape($xlabel);
-    $ylabel = &Apache::lonnet::escape($ylabel);
+    $ValuesHash{$id.'.title'}    = &Apache::lonnet::escape($Title);
+    $ValuesHash{$id.'.xlabel'}   = &Apache::lonnet::escape($xlabel);
+    $ValuesHash{$id.'.ylabel'}   = &Apache::lonnet::escape($ylabel);
+    $ValuesHash{$id.'.Max'}      = $Max;
+    $ValuesHash{$id.'.NumBars'}  = $NumBars;
+    $ValuesHash{$id.'.NumSets'}  = $NumSets;
+    $ValuesHash{$id.'.PlotType'} = 'bar';
+    $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
+    $ValuesHash{$id.'.height'}   = $height;
+    $ValuesHash{$id.'.width'}    = $width;
+    $ValuesHash{$id.'.xskip'}    = $xskip;
+    $ValuesHash{$id.'.bar_width'} = $bar_width;
+    $ValuesHash{$id.'.labels'} = join(',',@Labels);
     #
     $Max = 1 if ($Max < 1);
     if ( int($Max) < $Max ) {
@@ -2713,14 +2764,52 @@ sub DrawGraph {
         $Max = int($Max);
     }
     #
-    &Apache::lonnet::appenv('cgi.'.$identifier.'.title'   => $Title,
-                            'cgi.'.$identifier.'.xlabel'  => $xlabel,
-                            'cgi.'.$identifier.'.ylabel'  => $ylabel,
-                            'cgi.'.$identifier.'.Max'     => $Max,
-                            'cgi.'.$identifier.'.NumBars' => $NumBars,
-                            'cgi.'.$identifier.'.NumSets' => $NumSets,
-                            'cgi.'.$identifier.'.Colors'  => join(',',@{$colors}),
-                            %ValuesHash);
+    &Apache::lonnet::appenv(%ValuesHash);
+    return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
+}
+
+############################################################
+############################################################
+
+=pod
+
+=item DrawXYGraph
+
+=cut
+
+############################################################
+############################################################
+sub DrawXYGraph {
+    my ($Title,$xlabel,$ylabel,$Max,$Xlabels,$Ydata,%Values)=@_;
+    #
+    # Create the identifier for the graph
+    my $identifier = &get_cgi_id();
+    my $id = 'cgi.'.$identifier;
+    #
+    $Title  = '' if (! defined($Title));
+    $xlabel = '' if (! defined($xlabel));
+    $ylabel = '' if (! defined($ylabel));
+    my %ValuesHash = 
+        (
+         $id.'.title'  => &Apache::lonnet::escape($Title),
+         $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
+         $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
+         $id.'.Max'    => $Max,
+         $id.'.labels' => join(',',@$Xlabels),
+         $id.'.PlotType' => 'XY',
+         $id.'.NumSets' => 2,
+         );
+    #
+    if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
+        return '';
+    }
+    my $NumSets=1;
+    foreach my $array ($Ydata){
+        next if (! ref($array));
+        $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
+    }
+    #
+    &Apache::lonnet::appenv(%ValuesHash);
     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
 }