--- loncom/interface/loncommon.pm	2004/01/28 16:18:29	1.176
+++ loncom/interface/loncommon.pm	2004/03/19 03:47:09	1.187
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.176 2004/01/28 16:18:29 albertel Exp $
+# $Id: loncommon.pm,v 1.187 2004/03/19 03:47:09 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -220,10 +220,10 @@ formname and elementname indicate the na
 the element that the results of the browsing selection are to be placed in. 
 
 Specifying 'only' will restrict the browser to displaying only files
-with the given extension.  Can be a comma seperated list.
+with the given extension.  Can be a comma separated list.
 
 Specifying 'omit' will restrict the browser to NOT displaying files
-with the given extension.  Can be a comma seperated list.
+with the given extension.  Can be a comma separated list.
 
 =item * opensearcher(formname, elementname) [javascript]
 
@@ -353,7 +353,7 @@ sub coursebrowser_javascript {
    return (<<ENDSTDBRW);
 <script type="text/javascript" language="Javascript" >
     var stdeditbrowser;
-    function opencrsbrowser(formname,uname,udom) {
+    function opencrsbrowser(formname,uname,udom,desc) {
         var url = '/adm/pickcourse?';
         var filter;
         if (filter != null) {
@@ -368,7 +368,8 @@ sub coursebrowser_javascript {
 	   }
         }
         url += 'form=' + formname + '&cnumelement='+uname+
-                                    '&cdomelement='+udom;
+	                            '&cdomelement='+udom+
+                                    '&cnameelement='+desc;
         var title = 'Course_Browser';
         var options = 'scrollbars=1,resizable=1,menubar=0';
         options += ',width=700,height=600';
@@ -380,9 +381,9 @@ ENDSTDBRW
 }
 
 sub selectcourse_link {
-   my ($form,$unameele,$udomele)=@_;
+   my ($form,$unameele,$udomele,$desc)=@_;
     return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
-        '","'.$udomele.'");'."'>".&mt('Select Course')."</a>";
+        '","'.$udomele.'","'.$desc.'");'."'>".&mt('Select Course')."</a>";
 }
 
 =pod
@@ -592,8 +593,9 @@ sub help_open_topic {
     }
 
     # Add the graphic
+    my $title = &mt('Online Help');
     $template .= <<"ENDTEMPLATE";
- <a href="$link" title="Online Help"><image src="/adm/help/gif/smallHelp.gif" border="0" alt="(Help: $topic)" /></a>
+ <a href="$link" title="$title"><image src="/adm/help/gif/smallHelp.gif" border="0" alt="(Help: $topic)" /></a>
 ENDTEMPLATE
     if ($text ne '') { $template.='</td></tr></table>' };
     return $template;
@@ -630,8 +632,8 @@ sub help_open_bug {
 	$ENV{'environment.remote'} eq 'off' ) {
 	$stayOnPage=1;
     }
-    $width = 350 if (not defined $width);
-    $height = 400 if (not defined $height);
+    $width = 600 if (not defined $width);
+    $height = 600 if (not defined $height);
 
     $topic=~s/\W+/\+/g;
     my $link='';
@@ -655,8 +657,9 @@ sub help_open_bug {
     }
 
     # Add the graphic
+    my $title = &mt('Report a Bug');
     $template .= <<"ENDTEMPLATE";
- <a href="$link" title="Report a Bug"><image src="/adm/lonMisc/smallBug.gif" border="0" alt="(Bug: $topic)" /></a>
+ <a href="$link" title="$title"><image src="/adm/lonMisc/smallBug.gif" border="0" alt="(Bug: $topic)" /></a>
 ENDTEMPLATE
     if ($text ne '') { $template.='</td></tr></table>' };
     return $template;
@@ -698,23 +701,29 @@ sub help_open_faq {
     }
 
     # Add the graphic
+    my $title = &mt('View the FAQ');
     $template .= <<"ENDTEMPLATE";
- <a href="$link" title="View the FAQ"><image src="/adm/lonMisc/smallFAQ.gif" border="0" alt="(FAQ: $topic)" /></a>
+ <a href="$link" title="$title"><image src="/adm/lonMisc/smallFAQ.gif" border="0" alt="(FAQ: $topic)" /></a>
 ENDTEMPLATE
     if ($text ne '') { $template.='</td></tr></table>' };
     return $template;
 
 }
 
+###############################################################
+###############################################################
+
 =pod
 
 =item * csv_translate($text) 
 
-Translate $text to allow it to be output as a 'comma seperated values' 
+Translate $text to allow it to be output as a 'comma separated values' 
 format.
 
 =cut
 
+###############################################################
+###############################################################
 sub csv_translate {
     my $text = shift;
     $text =~ s/\"/\"\"/g;
@@ -722,6 +731,60 @@ sub csv_translate {
     return $text;
 }
 
+
+###############################################################
+###############################################################
+
+=pod
+
+=item * define_excel_formats
+
+Define some commonly used Excel cell formats.
+
+Currently supported formats:
+
+=over 4
+
+=item header
+
+=item bold
+
+=item h1
+
+=item h2
+
+=item h3
+
+=item date
+
+=back
+
+Inputs: $workbook
+
+Returns: $format, a hash reference.
+
+=cut
+
+###############################################################
+###############################################################
+sub define_excel_formats {
+    my ($workbook) = @_;
+    my $format;
+    $format->{'header'} = $workbook->add_format(bold      => 1, 
+                                                bottom    => 1,
+                                                align     => 'center');
+    $format->{'bold'} = $workbook->add_format(bold=>1);
+    $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
+    $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
+    $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
+    $format->{'date'} = $workbook->add_format(num_format=>
+                                            'mmm d yyyy hh:mm AM/PM');
+    return $format;
+}
+
+###############################################################
+###############################################################
+
 =pod
 
 =item * change_content_javascript():
@@ -1845,13 +1908,13 @@ sub display_languages {
 
 sub preferred_languages {
     my @languages=();
-    if ($ENV{'environment.languages'}) {
-	@languages=split(/\s*(\,|\;|\:)\s*/,$ENV{'environment.languages'});
-    }
     if ($ENV{'course.'.$ENV{'request.course.id'}.'.languages'}) {
 	@languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
 	         $ENV{'course.'.$ENV{'request.course.id'}.'.languages'}));
     }
+    if ($ENV{'environment.languages'}) {
+	@languages=split(/\s*(\,|\;|\:)\s*/,$ENV{'environment.languages'});
+    }
     my $browser=(split(/\;/,$ENV{'HTTP_ACCEPT_LANGUAGE'}))[0];
     if ($browser) {
 	@languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$browser));
@@ -2031,22 +2094,19 @@ show a snapshot of what student was look
 =cut
 
 sub get_student_view {
-  my ($symb,$username,$domain,$courseid,$target) = @_;
+  my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
-  my (%old,%moreenv);
+  my (%form);
   my @elements=('symb','courseid','domain','username');
   foreach my $element (@elements) {
-    $old{$element}=$ENV{'form.grade_'.$element};
-    $moreenv{'form.grade_'.$element}=eval '$'.$element #'
+      $form{'grade_'.$element}=eval '$'.$element #'
   }
-  if ($target eq 'tex') {$moreenv{'form.grade_target'} = 'tex';}
-  &Apache::lonnet::appenv(%moreenv);
-  $feedurl=&Apache::lonnet::clutter($feedurl);
-  my $userview=&Apache::lonnet::ssi_body($feedurl);
-  &Apache::lonnet::delenv('form.grade_');
-  foreach my $element (@elements) {
-    $ENV{'form.grade_'.$element}=$old{$element};
+  if (defined($moreenv)) {
+      %form=(%form,%{$moreenv});
   }
+  if ($target eq 'tex') {$form{'grade_target'} = 'tex';}
+  $feedurl=&Apache::lonnet::clutter($feedurl);
+  my $userview=&Apache::lonnet::ssi_body($feedurl,%form);
   $userview=~s/\<body[^\>]*\>//gi;
   $userview=~s/\<\/body\>//gi;
   $userview=~s/\<html\>//gi;
@@ -2069,19 +2129,14 @@ show a snapshot of how student was answe
 sub get_student_answers {
   my ($symb,$username,$domain,$courseid,%form) = @_;
   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
-  my (%old,%moreenv);
+  my (%moreenv);
   my @elements=('symb','courseid','domain','username');
   foreach my $element (@elements) {
-    $old{$element}=$ENV{'form.grade_'.$element};
-    $moreenv{'form.grade_'.$element}=eval '$'.$element #'
-  }
-  $moreenv{'form.grade_target'}='answer';
-  &Apache::lonnet::appenv(%moreenv);
-  my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%form);
-  &Apache::lonnet::delenv('form.grade_');
-  foreach my $element (@elements) {
-    $ENV{'form.grade_'.$element}=$old{$element};
+    $moreenv{'grade_'.$element}=eval '$'.$element #'
   }
+  $moreenv{'grade_target'}='answer';
+  %moreenv=(%form,%moreenv);
+  my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%moreenv);
   return $userview;
 }
 
@@ -2297,19 +2352,7 @@ other decorations will be returned.
 sub bodytag {
     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg)=@_;
     $title=&mt($title);
-    unless ($function) {
-	$function='student';
-        if ($ENV{'request.role'}=~/^(cc|in|ta|ep)/) {
-	    $function='coordinator';
-        }
-	if ($ENV{'request.role'}=~/^(su|dc|ad|li)/) {
-            $function='admin';
-        }
-        if (($ENV{'request.role'}=~/^(au|ca)/) ||
-            ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
-            $function='author';
-        }
-    }
+    $function = &get_users_function() if (!$function);
     my $img=&designparm($function.'.img',$domain);
     my $pgbg=&designparm($function.'.pgbg',$domain);
     my $tabbg=&designparm($function.'.tabbg',$domain);
@@ -2397,6 +2440,33 @@ ENDBODY
 
 ###############################################
 
+=pod
+
+=item get_users_function
+
+Used by &bodytag to determine the current users primary role.
+Returns either 'student','coordinator','admin', or 'author'.
+
+=cut
+
+###############################################
+sub get_users_function {
+    my $function = 'student';
+    if ($ENV{'request.role'}=~/^(cc|in|ta|ep)/) {
+        $function='coordinator';
+    }
+    if ($ENV{'request.role'}=~/^(su|dc|ad|li)/) {
+        $function='admin';
+    }
+    if (($ENV{'request.role'}=~/^(au|ca)/) ||
+        ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
+        $function='author';
+    }
+    return $function;
+}
+
+###############################################
+
 sub get_posted_cgi {
     my $r=shift;
 
@@ -2531,11 +2601,16 @@ sub no_cache {
 }
 
 sub content_type {
-  my ($r,$type,$charset) = @_;
-  unless ($charset) {
-      $charset=&Apache::lonlocal::current_encoding;
-  }
-  $r->content_type($type.($charset?'; charset='.$charset:''));
+    my ($r,$type,$charset) = @_;
+    unless ($charset) {
+	$charset=&Apache::lonlocal::current_encoding;
+    }
+    if ($charset) { $type.='; charset='.$charset; }
+    if ($r) {
+	$r->content_type($type);
+    } else {
+	print("Content-type: $type\n\n");
+    }
 }
 
 =pod
@@ -2992,6 +3067,8 @@ If $Max is < any data point, the graph w
 =item $colors: array ref holding the colors to be used for the data sets when
 they are plotted.  If undefined, default values will be used.
 
+=item $labels: array ref holding the labels to use on the x-axis for the bars.
+
 =item @Values: An array of array references.  Each array reference holds data
 to be plotted in a stacked bar chart.
 
@@ -3007,7 +3084,7 @@ information for the plot.
 ############################################################
 ############################################################
 sub DrawBarGraph {
-    my ($Title,$xlabel,$ylabel,$Max,$colors,@Values)=@_;
+    my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
     #
     if (! defined($colors)) {
         $colors = ['#33ff00', 
@@ -3050,8 +3127,12 @@ sub DrawBarGraph {
     }
     #
     my @Labels;
-    for (my $i=0;$i<@{$Values[0]};$i++) {
-        push (@Labels,$i+1);
+    if (defined($labels)) {
+        @Labels = @$labels;
+    } else {
+        for (my $i=0;$i<@{$Values[0]};$i++) {
+            push (@Labels,$i+1);
+        }
     }
     #
     $Max = 1 if ($Max < 1);
@@ -3110,7 +3191,7 @@ plotted in.  If undefined, default value
 =item $Xlabels: Array ref containing the labels to be used for the X-axis.
 
 =item $Ydata: Array ref containing Array refs.  
-Each of the contained arrays will be plotted as a seperate curve.
+Each of the contained arrays will be plotted as a separate curve.
 
 =item %Values: hash indicating or overriding any default values which are 
 passed to graph.png.