Annotation of loncom/interface/loncommon.pm, revision 1.144
1.10 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # a pile of common routines
1.10 albertel 3: #
1.144 ! matthew 4: # $Id: loncommon.pm,v 1.143 2003/11/04 21:21:35 matthew Exp $
1.10 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.12 harris41 28: # YEAR=2001
29: # 2/13-12/7 Guy Albertelli
1.18 www 30: # 12/21 Gerd Kortemeyer
1.22 www 31: # 12/25,12/28 Gerd Kortemeyer
1.23 www 32: # YEAR=2002
33: # 1/4 Gerd Kortemeyer
1.43 ng 34: # 6/24,7/2 H. K. Ng
1.1 albertel 35:
36: # Makes a table out of the previous attempts
1.2 albertel 37: # Inputs result_from_symbread, user, domain, course_id
1.16 harris41 38: # Reads in non-network-related .tab files
1.1 albertel 39:
1.35 matthew 40: # POD header:
41:
1.45 matthew 42: =pod
43:
1.35 matthew 44: =head1 NAME
45:
46: Apache::loncommon - pile of common routines
47:
48: =head1 SYNOPSIS
49:
1.112 bowersj2 50: Common routines for manipulating connections, student answers,
51: domains, common Javascript fragments, etc.
1.35 matthew 52:
1.112 bowersj2 53: =head1 OVERVIEW
1.35 matthew 54:
1.112 bowersj2 55: A collection of commonly used subroutines that don't have a natural
56: home anywhere else. This collection helps remove
1.35 matthew 57: redundancy from other modules and increase efficiency of memory usage.
58:
59: =cut
60:
61: # End of POD header
1.1 albertel 62: package Apache::loncommon;
63:
64: use strict;
1.22 www 65: use Apache::lonnet();
1.46 matthew 66: use GDBM_File;
1.51 www 67: use POSIX qw(strftime mktime);
1.99 www 68: use Apache::Constants qw(:common :http :methods);
1.1 albertel 69: use Apache::lonmsg();
1.82 www 70: use Apache::lonmenu();
1.117 www 71: use Apache::lonlocal;
1.139 matthew 72: use HTML::Entities;
1.117 www 73:
1.22 www 74: my $readit;
75:
1.46 matthew 76: =pod
77:
1.112 bowersj2 78: =head1 Global Variables
1.46 matthew 79:
1.112 bowersj2 80: =cut
1.46 matthew 81:
1.20 www 82: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12 harris41 83: my %language;
1.124 www 84: my %supported_language;
1.12 harris41 85: my %cprtag;
86: my %fe; my %fd;
1.41 ng 87: my %category_extensions;
1.12 harris41 88:
1.63 www 89: # ---------------------------------------------- Designs
90:
91: my %designhash;
92:
1.46 matthew 93: # ---------------------------------------------- Thesaurus variables
1.144 ! matthew 94: #
! 95: # %Keywords:
! 96: # A hash used by &keyword to determine if a word is considered a keyword.
! 97: # $thesaurus_db_file
! 98: # Scalar containing the full path to the thesaurus database.
1.46 matthew 99:
100: my %Keywords;
101: my $thesaurus_db_file;
102:
1.144 ! matthew 103: #
! 104: # Initialize values from language.tab, copyright.tab, filetypes.tab,
! 105: # thesaurus.tab, and filecategories.tab.
! 106: #
1.18 www 107: BEGIN {
1.46 matthew 108: # Variable initialization
109: $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
110: #
1.22 www 111: unless ($readit) {
1.12 harris41 112: # ------------------------------------------------------------------- languages
113: {
114: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
115: '/language.tab');
1.16 harris41 116: if ($fh) {
117: while (<$fh>) {
118: next if /^\#/;
119: chomp;
1.124 www 120: my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$_));
1.98 www 121: $language{$key}=$val.' - '.$enc;
1.124 www 122: if ($sup) {
123: $supported_language{$key}=$sup;
124: }
1.16 harris41 125: }
1.12 harris41 126: }
127: }
128: # ------------------------------------------------------------------ copyrights
129: {
1.16 harris41 130: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonIncludes'}.
131: '/copyright.tab');
132: if ($fh) {
133: while (<$fh>) {
134: next if /^\#/;
135: chomp;
136: my ($key,$val)=(split(/\s+/,$_,2));
137: $cprtag{$key}=$val;
138: }
1.12 harris41 139: }
140: }
1.63 www 141:
142: # -------------------------------------------------------------- domain designs
143:
144: my $filename;
145: my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
146: opendir(DIR,$designdir);
147: while ($filename=readdir(DIR)) {
148: my ($domain)=($filename=~/^(\w+)\./);
149: {
150: my $fh=Apache::File->new($designdir.'/'.$filename);
151: if ($fh) {
152: while (<$fh>) {
153: next if /^\#/;
154: chomp;
155: my ($key,$val)=(split(/\=/,$_));
156: if ($val) { $designhash{$domain.'.'.$key}=$val; }
157: }
158: }
159: }
160:
161: }
162: closedir(DIR);
163:
164:
1.15 harris41 165: # ------------------------------------------------------------- file categories
166: {
167: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
1.16 harris41 168: '/filecategories.tab');
169: if ($fh) {
170: while (<$fh>) {
171: next if /^\#/;
172: chomp;
1.41 ng 173: my ($extension,$category)=(split(/\s+/,$_,2));
174: push @{$category_extensions{lc($category)}},$extension;
1.16 harris41 175: }
1.15 harris41 176: }
177: }
1.12 harris41 178: # ------------------------------------------------------------------ file types
179: {
1.16 harris41 180: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
181: '/filetypes.tab');
182: if ($fh) {
183: while (<$fh>) {
184: next if (/^\#/);
185: chomp;
186: my ($ending,$emb,$descr)=split(/\s+/,$_,3);
187: if ($descr ne '') {
188: $fe{$ending}=lc($emb);
189: $fd{$ending}=$descr;
190: }
1.12 harris41 191: }
192: }
193: }
1.22 www 194: &Apache::lonnet::logthis(
1.46 matthew 195: "<font color=yellow>INFO: Read file types</font>");
1.22 www 196: $readit=1;
1.46 matthew 197: } # end of unless($readit)
1.32 matthew 198:
199: }
1.112 bowersj2 200:
1.42 matthew 201: ###############################################################
202: ## HTML and Javascript Helper Functions ##
203: ###############################################################
204:
205: =pod
206:
1.144 ! matthew 207: =head1 General Subroutines
! 208:
! 209: =over 4
! 210:
1.112 bowersj2 211: =head1 HTML and Javascript Functions
1.42 matthew 212:
1.112 bowersj2 213: =over 4
214:
215: =item * browser_and_searcher_javascript ()
216:
217: X<browsing, javascript>X<searching, javascript>Returns a string
218: containing javascript with two functions, C<openbrowser> and
219: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
220: tags.
1.42 matthew 221:
222: =over 4
223:
1.112 bowersj2 224: =item * openbrowser(formname,elementname,only,omit) [javascript]
1.42 matthew 225:
226: inputs: formname, elementname, only, omit
227:
228: formname and elementname indicate the name of the html form and name of
229: the element that the results of the browsing selection are to be placed in.
230:
231: Specifying 'only' will restrict the browser to displaying only files
232: with the given extension. Can be a comma seperated list.
233:
234: Specifying 'omit' will restrict the browser to NOT displaying files
235: with the given extension. Can be a comma seperated list.
236:
1.112 bowersj2 237: =item * opensearcher(formname, elementname) [javascript]
1.42 matthew 238:
239: Inputs: formname, elementname
240:
241: formname and elementname specify the name of the html form and the name
242: of the element the selection from the search results will be placed in.
243:
244: =back
245:
246: =cut
247:
248: sub browser_and_searcher_javascript {
249: return <<END;
1.50 matthew 250: var editbrowser = null;
1.135 albertel 251: function openbrowser(formname,elementname,only,omit,titleelement) {
1.42 matthew 252: var url = '/res/?';
253: if (editbrowser == null) {
254: url += 'launch=1&';
255: }
256: url += 'catalogmode=interactive&';
257: url += 'mode=edit&';
258: url += 'form=' + formname + '&';
259: if (only != null) {
260: url += 'only=' + only + '&';
261: }
262: if (omit != null) {
263: url += 'omit=' + omit + '&';
264: }
1.135 albertel 265: if (titleelement != null) {
266: url += 'titleelement=' + titleelement + '&';
267: }
1.42 matthew 268: url += 'element=' + elementname + '';
269: var title = 'Browser';
270: var options = 'scrollbars=1,resizable=1,menubar=0';
271: options += ',width=700,height=600';
272: editbrowser = open(url,title,options,'1');
273: editbrowser.focus();
274: }
275: var editsearcher;
1.135 albertel 276: function opensearcher(formname,elementname,titleelement) {
1.42 matthew 277: var url = '/adm/searchcat?';
278: if (editsearcher == null) {
279: url += 'launch=1&';
280: }
281: url += 'catalogmode=interactive&';
282: url += 'mode=edit&';
283: url += 'form=' + formname + '&';
1.135 albertel 284: if (titleelement != null) {
285: url += 'titleelement=' + titleelement + '&';
286: }
1.42 matthew 287: url += 'element=' + elementname + '';
288: var title = 'Search';
289: var options = 'scrollbars=1,resizable=1,menubar=0';
290: options += ',width=700,height=600';
291: editsearcher = open(url,title,options,'1');
292: editsearcher.focus();
293: }
294: END
295: }
296:
1.74 www 297: sub studentbrowser_javascript {
1.111 www 298: unless (
299: (($ENV{'request.course.id'}) &&
300: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})))
301: || ($ENV{'request.role'}=~/^(au|dc|su)/)
302: ) { return ''; }
1.74 www 303: return (<<'ENDSTDBRW');
304: <script type="text/javascript" language="Javascript" >
305: var stdeditbrowser;
1.111 www 306: function openstdbrowser(formname,uname,udom,roleflag) {
1.74 www 307: var url = '/adm/pickstudent?';
308: var filter;
309: eval('filter=document.'+formname+'.'+uname+'.value;');
310: if (filter != null) {
311: if (filter != '') {
312: url += 'filter='+filter+'&';
313: }
314: }
315: url += 'form=' + formname + '&unameelement='+uname+
316: '&udomelement='+udom;
1.111 www 317: if (roleflag) { url+="&roles=1"; }
1.102 www 318: var title = 'Student_Browser';
1.74 www 319: var options = 'scrollbars=1,resizable=1,menubar=0';
320: options += ',width=700,height=600';
321: stdeditbrowser = open(url,title,options,'1');
322: stdeditbrowser.focus();
323: }
324: </script>
325: ENDSTDBRW
326: }
1.42 matthew 327:
1.74 www 328: sub selectstudent_link {
1.111 www 329: my ($form,$unameele,$udomele)=@_;
330: if ($ENV{'request.course.id'}) {
331: unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
332: return '';
333: }
334: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 335: '","'.$udomele.'");'."'>".&mt('Select User')."</a>";
1.74 www 336: }
1.111 www 337: if ($ENV{'request.role'}=~/^(au|dc|su)/) {
338: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 339: '","'.$udomele.'",1);'."'>".&mt('Select User')."</a>";
1.111 www 340: }
341: return '';
1.91 www 342: }
343:
344: sub coursebrowser_javascript {
1.128 albertel 345: my ($domainfilter)=@_;
346: return (<<ENDSTDBRW);
1.91 www 347: <script type="text/javascript" language="Javascript" >
348: var stdeditbrowser;
349: function opencrsbrowser(formname,uname,udom) {
350: var url = '/adm/pickcourse?';
351: var filter;
352: if (filter != null) {
353: if (filter != '') {
354: url += 'filter='+filter+'&';
355: }
356: }
1.128 albertel 357: var domainfilter='$domainfilter';
358: if (domainfilter != null) {
359: if (domainfilter != '') {
360: url += 'domainfilter='+domainfilter+'&';
361: }
362: }
1.91 www 363: url += 'form=' + formname + '&cnumelement='+uname+
364: '&cdomelement='+udom;
1.102 www 365: var title = 'Course_Browser';
1.91 www 366: var options = 'scrollbars=1,resizable=1,menubar=0';
367: options += ',width=700,height=600';
368: stdeditbrowser = open(url,title,options,'1');
369: stdeditbrowser.focus();
370: }
371: </script>
372: ENDSTDBRW
373: }
374:
375: sub selectcourse_link {
376: my ($form,$unameele,$udomele)=@_;
377: return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
1.119 www 378: '","'.$udomele.'");'."'>".&mt('Select Course')."</a>";
1.74 www 379: }
1.42 matthew 380:
381: =pod
1.36 matthew 382:
1.112 bowersj2 383: =item * linked_select_forms(...)
1.36 matthew 384:
385: linked_select_forms returns a string containing a <script></script> block
386: and html for two <select> menus. The select menus will be linked in that
387: changing the value of the first menu will result in new values being placed
388: in the second menu. The values in the select menu will appear in alphabetical
389: order.
390:
391: linked_select_forms takes the following ordered inputs:
392:
393: =over 4
394:
1.112 bowersj2 395: =item * $formname, the name of the <form> tag
1.36 matthew 396:
1.112 bowersj2 397: =item * $middletext, the text which appears between the <select> tags
1.36 matthew 398:
1.112 bowersj2 399: =item * $firstdefault, the default value for the first menu
1.36 matthew 400:
1.112 bowersj2 401: =item * $firstselectname, the name of the first <select> tag
1.36 matthew 402:
1.112 bowersj2 403: =item * $secondselectname, the name of the second <select> tag
1.36 matthew 404:
1.112 bowersj2 405: =item * $hashref, a reference to a hash containing the data for the menus.
1.36 matthew 406:
1.41 ng 407: =back
408:
1.36 matthew 409: Below is an example of such a hash. Only the 'text', 'default', and
410: 'select2' keys must appear as stated. keys(%menu) are the possible
411: values for the first select menu. The text that coincides with the
1.41 ng 412: first menu value is given in $menu{$choice1}->{'text'}. The values
1.36 matthew 413: and text for the second menu are given in the hash pointed to by
414: $menu{$choice1}->{'select2'}.
415:
1.112 bowersj2 416: my %menu = ( A1 => { text =>"Choice A1" ,
417: default => "B3",
418: select2 => {
419: B1 => "Choice B1",
420: B2 => "Choice B2",
421: B3 => "Choice B3",
422: B4 => "Choice B4"
423: }
424: },
425: A2 => { text =>"Choice A2" ,
426: default => "C2",
427: select2 => {
428: C1 => "Choice C1",
429: C2 => "Choice C2",
430: C3 => "Choice C3"
431: }
432: },
433: A3 => { text =>"Choice A3" ,
434: default => "D6",
435: select2 => {
436: D1 => "Choice D1",
437: D2 => "Choice D2",
438: D3 => "Choice D3",
439: D4 => "Choice D4",
440: D5 => "Choice D5",
441: D6 => "Choice D6",
442: D7 => "Choice D7"
443: }
444: }
445: );
1.36 matthew 446:
447: =cut
448:
449: sub linked_select_forms {
450: my ($formname,
451: $middletext,
452: $firstdefault,
453: $firstselectname,
454: $secondselectname,
455: $hashref
456: ) = @_;
457: my $second = "document.$formname.$secondselectname";
458: my $first = "document.$formname.$firstselectname";
459: # output the javascript to do the changing
460: my $result = '';
461: $result.="<script>\n";
462: $result.="var select2data = new Object();\n";
463: $" = '","';
464: my $debug = '';
465: foreach my $s1 (sort(keys(%$hashref))) {
466: $result.="select2data.d_$s1 = new Object();\n";
467: $result.="select2data.d_$s1.def = new String('".
468: $hashref->{$s1}->{'default'}."');\n";
469: $result.="select2data.d_$s1.values = new Array(";
470: my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
471: $result.="\"@s2values\");\n";
472: $result.="select2data.d_$s1.texts = new Array(";
473: my @s2texts;
474: foreach my $value (@s2values) {
475: push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
476: }
477: $result.="\"@s2texts\");\n";
478: }
479: $"=' ';
480: $result.= <<"END";
481:
482: function select1_changed() {
483: // Determine new choice
484: var newvalue = "d_" + $first.value;
485: // update select2
486: var values = select2data[newvalue].values;
487: var texts = select2data[newvalue].texts;
488: var select2def = select2data[newvalue].def;
489: var i;
490: // out with the old
491: for (i = 0; i < $second.options.length; i++) {
492: $second.options[i] = null;
493: }
494: // in with the nuclear
495: for (i=0;i<values.length; i++) {
496: $second.options[i] = new Option(values[i]);
1.143 matthew 497: $second.options[i].value = values[i];
1.36 matthew 498: $second.options[i].text = texts[i];
499: if (values[i] == select2def) {
500: $second.options[i].selected = true;
501: }
502: }
503: }
504: </script>
505: END
506: # output the initial values for the selection lists
507: $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
508: foreach my $value (sort(keys(%$hashref))) {
509: $result.=" <option value=\"$value\" ";
510: $result.=" selected=\"true\" " if ($value eq $firstdefault);
1.119 www 511: $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36 matthew 512: }
513: $result .= "</select>\n";
514: my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
515: $result .= $middletext;
516: $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
517: my $seconddefault = $hashref->{$firstdefault}->{'default'};
518: foreach my $value (sort(keys(%select2))) {
519: $result.=" <option value=\"$value\" ";
520: $result.=" selected=\"true\" " if ($value eq $seconddefault);
1.119 www 521: $result.=">".&mt($select2{$value})."</option>\n";
1.36 matthew 522: }
523: $result .= "</select>\n";
524: # return $debug;
525: return $result;
526: } # end of sub linked_select_forms {
527:
1.45 matthew 528: =pod
1.44 bowersj2 529:
1.112 bowersj2 530: =item * help_open_topic($topic, $text, $stayOnPage, $width, $height)
1.44 bowersj2 531:
1.112 bowersj2 532: Returns a string corresponding to an HTML link to the given help
533: $topic, where $topic corresponds to the name of a .tex file in
534: /home/httpd/html/adm/help/tex, with underscores replaced by
535: spaces.
536:
537: $text will optionally be linked to the same topic, allowing you to
538: link text in addition to the graphic. If you do not want to link
539: text, but wish to specify one of the later parameters, pass an
540: empty string.
541:
542: $stayOnPage is a value that will be interpreted as a boolean. If true,
543: the link will not open a new window. If false, the link will open
544: a new window using Javascript. (Default is false.)
545:
546: $width and $height are optional numerical parameters that will
547: override the width and height of the popped up window, which may
548: be useful for certain help topics with big pictures included.
1.44 bowersj2 549:
550: =cut
551:
552: sub help_open_topic {
1.48 bowersj2 553: my ($topic, $text, $stayOnPage, $width, $height) = @_;
554: $text = "" if (not defined $text);
1.44 bowersj2 555: $stayOnPage = 0 if (not defined $stayOnPage);
1.108 bowersj2 556: if ($ENV{'browser.interface'} eq 'textual' ||
557: $ENV{'environment.remote'} eq 'off' ) {
1.79 www 558: $stayOnPage=1;
559: }
1.44 bowersj2 560: $width = 350 if (not defined $width);
561: $height = 400 if (not defined $height);
562: my $filename = $topic;
563: $filename =~ s/ /_/g;
564:
1.48 bowersj2 565: my $template = "";
566: my $link;
1.44 bowersj2 567:
568: if (!$stayOnPage)
569: {
1.72 bowersj2 570: $link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
1.44 bowersj2 571: }
572: else
573: {
1.48 bowersj2 574: $link = "/adm/help/${filename}.hlp";
575: }
576:
577: # Add the text
578: if ($text ne "")
579: {
1.77 www 580: $template .=
581: "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
1.78 www 582: "<td bgcolor='#5555FF'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48 bowersj2 583: }
584:
585: # Add the graphic
586: $template .= <<"ENDTEMPLATE";
1.77 www 587: <a href="$link"><image src="/adm/help/gif/smallHelp.gif" border="0" alt="(Help: $topic)" /></a>
1.44 bowersj2 588: ENDTEMPLATE
1.78 www 589: if ($text ne '') { $template.='</td></tr></table>' };
1.44 bowersj2 590: return $template;
591:
1.106 bowersj2 592: }
593:
594: # This is a quicky function for Latex cheatsheet editing, since it
595: # appears in at least four places
596: sub helpLatexCheatsheet {
597: my $other = shift;
598: my $addOther = '';
599: if ($other) {
600: $addOther = Apache::loncommon::help_open_topic($other, shift,
601: undef, undef, 600) .
602: '</td><td>';
603: }
604: return '<table><tr><td>'.
605: $addOther .
606: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
607: undef,undef,600)
608: .'</td><td>'.
609: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
610: undef,undef,600)
611: .'</td></tr></table>';
1.44 bowersj2 612: }
1.37 matthew 613:
1.45 matthew 614: =pod
615:
1.112 bowersj2 616: =item * csv_translate($text)
1.37 matthew 617:
618: Translate $text to allow it to be output as a 'comma seperated values'
619: format.
620:
621: =cut
622:
623: sub csv_translate {
624: my $text = shift;
625: $text =~ s/\"/\"\"/g;
626: $text =~ s/\n//g;
627: return $text;
628: }
1.113 bowersj2 629:
630: =pod
631:
632: =item * change_content_javascript():
633:
634: This and the next function allow you to create small sections of an
635: otherwise static HTML page that you can update on the fly with
636: Javascript, even in Netscape 4.
637:
638: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
639: must be written to the HTML page once. It will prove the Javascript
640: function "change(name, content)". Calling the change function with the
641: name of the section
642: you want to update, matching the name passed to C<changable_area>, and
643: the new content you want to put in there, will put the content into
644: that area.
645:
646: B<Note>: Netscape 4 only reserves enough space for the changable area
647: to contain room for the original contents. You need to "make space"
648: for whatever changes you wish to make, and be B<sure> to check your
649: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
650: it's adequate for updating a one-line status display, but little more.
651: This script will set the space to 100% width, so you only need to
652: worry about height in Netscape 4.
653:
654: Modern browsers are much less limiting, and if you can commit to the
655: user not using Netscape 4, this feature may be used freely with
656: pretty much any HTML.
657:
658: =cut
659:
660: sub change_content_javascript {
661: # If we're on Netscape 4, we need to use Layer-based code
662: if ($ENV{'browser.type'} eq 'netscape' &&
663: $ENV{'browser.version'} =~ /^4\./) {
664: return (<<NETSCAPE4);
665: function change(name, content) {
666: doc = document.layers[name+"___escape"].layers[0].document;
667: doc.open();
668: doc.write(content);
669: doc.close();
670: }
671: NETSCAPE4
672: } else {
673: # Otherwise, we need to use semi-standards-compliant code
674: # (technically, "innerHTML" isn't standard but the equivalent
675: # is really scary, and every useful browser supports it
676: return (<<DOMBASED);
677: function change(name, content) {
678: element = document.getElementById(name);
679: element.innerHTML = content;
680: }
681: DOMBASED
682: }
683: }
684:
685: =pod
686:
687: =item * changable_area($name, $origContent):
688:
689: This provides a "changable area" that can be modified on the fly via
690: the Javascript code provided in C<change_content_javascript>. $name is
691: the name you will use to reference the area later; do not repeat the
692: same name on a given HTML page more then once. $origContent is what
693: the area will originally contain, which can be left blank.
694:
695: =cut
696:
697: sub changable_area {
698: my ($name, $origContent) = @_;
699:
700: if ($ENV{'browser.type'} eq 'netscape' &&
701: $ENV{'browser.version'} =~ /^4\./) {
702: # If this is netscape 4, we need to use the Layer tag
703: return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
704: } else {
705: return "<span id='$name'>$origContent</span>";
706: }
707: }
708:
709: =pod
710:
711: =back
712:
713: =cut
1.37 matthew 714:
715: ###############################################################
1.33 matthew 716: ## Home server <option> list generating code ##
717: ###############################################################
1.35 matthew 718:
1.45 matthew 719: =pod
720:
1.112 bowersj2 721: =head1 Home Server option list generating code
722:
723: =over 4
724:
725: =item * get_domains()
1.35 matthew 726:
727: Returns an array containing each of the domains listed in the hosts.tab
728: file.
729:
730: =cut
731:
732: #-------------------------------------------
1.34 matthew 733: sub get_domains {
734: # The code below was stolen from "The Perl Cookbook", p 102, 1st ed.
735: my @domains;
736: my %seen;
737: foreach (sort values(%Apache::lonnet::hostdom)) {
738: push (@domains,$_) unless $seen{$_}++;
739: }
740: return @domains;
741: }
1.88 www 742:
743: #-------------------------------------------
744:
745: =pod
746:
1.112 bowersj2 747: =item * select_form($defdom,$name,%hash)
1.88 www 748:
749: Returns a string containing a <select name='$name' size='1'> form to
750: allow a user to select options from a hash option_name => displayed text.
751: See lonrights.pm for an example invocation and use.
752:
753: =cut
754:
755: #-------------------------------------------
756: sub select_form {
757: my ($def,$name,%hash) = @_;
758: my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128 albertel 759: my @keys;
760: if (exists($hash{'select_form_order'})) {
761: @keys=@{$hash{'select_form_order'}};
762: } else {
763: @keys=sort(keys(%hash));
764: }
765: foreach (@keys) {
1.88 www 766: $selectform.="<option value=\"$_\" ".
767: ($_ eq $def ? 'selected' : '').
1.119 www 768: ">".&mt($hash{$_})."</option>\n";
1.88 www 769: }
770: $selectform.="</select>";
771: return $selectform;
772: }
773:
1.34 matthew 774:
1.35 matthew 775: #-------------------------------------------
776:
1.45 matthew 777: =pod
778:
1.112 bowersj2 779: =item * select_dom_form($defdom,$name,$includeempty)
1.35 matthew 780:
781: Returns a string containing a <select name='$name' size='1'> form to
782: allow a user to select the domain to preform an operation in.
783: See loncreateuser.pm for an example invocation and use.
784:
1.90 www 785: If the $includeempty flag is set, it also includes an empty choice ("no domain
786: selected");
787:
1.35 matthew 788: =cut
789:
790: #-------------------------------------------
1.34 matthew 791: sub select_dom_form {
1.90 www 792: my ($defdom,$name,$includeempty) = @_;
1.34 matthew 793: my @domains = get_domains();
1.90 www 794: if ($includeempty) { @domains=('',@domains); }
1.34 matthew 795: my $selectdomain = "<select name=\"$name\" size=\"1\">\n";
796: foreach (@domains) {
797: $selectdomain.="<option value=\"$_\" ".
798: ($_ eq $defdom ? 'selected' : '').
799: ">$_</option>\n";
800: }
801: $selectdomain.="</select>";
802: return $selectdomain;
803: }
804:
1.35 matthew 805: #-------------------------------------------
806:
1.45 matthew 807: =pod
808:
1.112 bowersj2 809: =item * get_library_servers($domain)
1.35 matthew 810:
811: Returns a hash which contains keys like '103l3' and values like
812: 'kirk.lite.msu.edu'. All of the keys will be for machines in the
813: given $domain.
814:
815: =cut
816:
817: #-------------------------------------------
1.52 matthew 818: sub get_library_servers {
1.33 matthew 819: my $domain = shift;
1.52 matthew 820: my %library_servers;
1.33 matthew 821: foreach (keys(%Apache::lonnet::libserv)) {
822: if ($Apache::lonnet::hostdom{$_} eq $domain) {
1.52 matthew 823: $library_servers{$_} = $Apache::lonnet::hostname{$_};
1.33 matthew 824: }
825: }
1.52 matthew 826: return %library_servers;
1.33 matthew 827: }
828:
1.35 matthew 829: #-------------------------------------------
830:
1.45 matthew 831: =pod
832:
1.112 bowersj2 833: =item * home_server_option_list($domain)
1.35 matthew 834:
835: returns a string which contains an <option> list to be used in a
836: <select> form input. See loncreateuser.pm for an example.
837:
838: =cut
839:
840: #-------------------------------------------
1.33 matthew 841: sub home_server_option_list {
842: my $domain = shift;
1.52 matthew 843: my %servers = &get_library_servers($domain);
1.33 matthew 844: my $result = '';
845: foreach (sort keys(%servers)) {
846: $result.=
847: '<option value="'.$_.'">'.$_.' '.$servers{$_}."</option>\n";
848: }
849: return $result;
850: }
1.112 bowersj2 851:
852: =pod
853:
854: =back
855:
856: =cut
1.87 matthew 857:
858: ###############################################################
1.112 bowersj2 859: ## Decoding User Agent ##
1.87 matthew 860: ###############################################################
861:
862: =pod
863:
1.112 bowersj2 864: =head1 Decoding the User Agent
865:
866: =over 4
867:
868: =item * &decode_user_agent()
1.87 matthew 869:
870: Inputs: $r
871:
872: Outputs:
873:
874: =over 4
875:
1.112 bowersj2 876: =item * $httpbrowser
1.87 matthew 877:
1.112 bowersj2 878: =item * $clientbrowser
1.87 matthew 879:
1.112 bowersj2 880: =item * $clientversion
1.87 matthew 881:
1.112 bowersj2 882: =item * $clientmathml
1.87 matthew 883:
1.112 bowersj2 884: =item * $clientunicode
1.87 matthew 885:
1.112 bowersj2 886: =item * $clientos
1.87 matthew 887:
888: =back
889:
890: =cut
891:
892: ###############################################################
893: ###############################################################
894: sub decode_user_agent {
895: my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
896: my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
897: my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
898: my $clientbrowser='unknown';
899: my $clientversion='0';
900: my $clientmathml='';
901: my $clientunicode='0';
902: for (my $i=0;$i<=$#browsertype;$i++) {
903: my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
904: if (($httpbrowser=~/$match/i) && ($httpbrowser!~/$notmatch/i)) {
905: $clientbrowser=$bname;
906: $httpbrowser=~/$vreg/i;
907: $clientversion=$1;
908: $clientmathml=($clientversion>=$minv);
909: $clientunicode=($clientversion>=$univ);
910: }
911: }
912: my $clientos='unknown';
913: if (($httpbrowser=~/linux/i) ||
914: ($httpbrowser=~/unix/i) ||
915: ($httpbrowser=~/ux/i) ||
916: ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
917: if (($httpbrowser=~/vax/i) ||
918: ($httpbrowser=~/vms/i)) { $clientos='vms'; }
919: if ($httpbrowser=~/next/i) { $clientos='next'; }
920: if (($httpbrowser=~/mac/i) ||
921: ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
922: if ($httpbrowser=~/win/i) { $clientos='win'; }
923: if ($httpbrowser=~/embed/i) { $clientos='pda'; }
924: return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
925: $clientunicode,$clientos,);
926: }
927:
1.112 bowersj2 928: =pod
929:
930: =back
1.87 matthew 931:
1.112 bowersj2 932: =cut
1.32 matthew 933:
934: ###############################################################
935: ## Authentication changing form generation subroutines ##
936: ###############################################################
937: ##
938: ## All of the authform_xxxxxxx subroutines take their inputs in a
939: ## hash, and have reasonable default values.
940: ##
941: ## formname = the name given in the <form> tag.
1.35 matthew 942: #-------------------------------------------
943:
1.45 matthew 944: =pod
945:
1.112 bowersj2 946: =head1 Authentication Routines
947:
948: =over 4
949:
950: =item * authform_xxxxxx
1.35 matthew 951:
952: The authform_xxxxxx subroutines provide javascript and html forms which
953: handle some of the conveniences required for authentication forms.
954: This is not an optimal method, but it works.
955:
956: See loncreateuser.pm for invocation and use examples.
957:
958: =over 4
959:
1.112 bowersj2 960: =item * authform_header
1.35 matthew 961:
1.112 bowersj2 962: =item * authform_authorwarning
1.35 matthew 963:
1.112 bowersj2 964: =item * authform_nochange
1.35 matthew 965:
1.112 bowersj2 966: =item * authform_kerberos
1.35 matthew 967:
1.112 bowersj2 968: =item * authform_internal
1.35 matthew 969:
1.112 bowersj2 970: =item * authform_filesystem
1.35 matthew 971:
972: =back
973:
974: =cut
975:
976: #-------------------------------------------
1.32 matthew 977: sub authform_header{
978: my %in = (
979: formname => 'cu',
1.80 albertel 980: kerb_def_dom => '',
1.32 matthew 981: @_,
982: );
983: $in{'formname'} = 'document.' . $in{'formname'};
984: my $result='';
1.80 albertel 985:
986: #---------------------------------------------- Code for upper case translation
987: my $Javascript_toUpperCase;
988: unless ($in{kerb_def_dom}) {
989: $Javascript_toUpperCase =<<"END";
990: switch (choice) {
991: case 'krb': currentform.elements[choicearg].value =
992: currentform.elements[choicearg].value.toUpperCase();
993: break;
994: default:
995: }
996: END
997: } else {
998: $Javascript_toUpperCase = "";
999: }
1000:
1.32 matthew 1001: $result.=<<"END";
1002: var current = new Object();
1003: current.radiovalue = 'nochange';
1004: current.argfield = null;
1005:
1006: function changed_radio(choice,currentform) {
1007: var choicearg = choice + 'arg';
1008: // If a radio button in changed, we need to change the argfield
1009: if (current.radiovalue != choice) {
1010: current.radiovalue = choice;
1011: if (current.argfield != null) {
1012: currentform.elements[current.argfield].value = '';
1013: }
1014: if (choice == 'nochange') {
1015: current.argfield = null;
1016: } else {
1017: current.argfield = choicearg;
1018: switch(choice) {
1019: case 'krb':
1020: currentform.elements[current.argfield].value =
1021: "$in{'kerb_def_dom'}";
1022: break;
1023: default:
1024: break;
1025: }
1026: }
1027: }
1028: return;
1029: }
1.22 www 1030:
1.32 matthew 1031: function changed_text(choice,currentform) {
1032: var choicearg = choice + 'arg';
1033: if (currentform.elements[choicearg].value !='') {
1.80 albertel 1034: $Javascript_toUpperCase
1.32 matthew 1035: // clear old field
1036: if ((current.argfield != choicearg) && (current.argfield != null)) {
1037: currentform.elements[current.argfield].value = '';
1038: }
1039: current.argfield = choicearg;
1040: }
1041: set_auth_radio_buttons(choice,currentform);
1042: return;
1.20 www 1043: }
1.32 matthew 1044:
1045: function set_auth_radio_buttons(newvalue,currentform) {
1046: var i=0;
1047: while (i < currentform.login.length) {
1048: if (currentform.login[i].value == newvalue) { break; }
1049: i++;
1050: }
1051: if (i == currentform.login.length) {
1052: return;
1053: }
1054: current.radiovalue = newvalue;
1055: currentform.login[i].checked = true;
1056: return;
1057: }
1058: END
1059: return $result;
1060: }
1061:
1062: sub authform_authorwarning{
1063: my $result='';
1.144 ! matthew 1064: $result='<i>'.
! 1065: &mt('As a general rule, only authors or co-authors should be '.
! 1066: 'filesystem authenticated '.
! 1067: '(which allows access to the server filesystem).')."</i>\n";
1.32 matthew 1068: return $result;
1069: }
1070:
1071: sub authform_nochange{
1072: my %in = (
1073: formname => 'document.cu',
1074: kerb_def_dom => 'MSU.EDU',
1075: @_,
1076: );
1.144 ! matthew 1077: my $result = &mt('[_1] Do not change login data',
! 1078: '<input type="radio" name="login" value="nochange" '.
! 1079: 'checked="checked" onclick="'.
! 1080: "javascript:changed_radio('nochange',$in{'formname'});".'" />');
1.32 matthew 1081: return $result;
1082: }
1083:
1084: sub authform_kerberos{
1085: my %in = (
1086: formname => 'document.cu',
1087: kerb_def_dom => 'MSU.EDU',
1.80 albertel 1088: kerb_def_auth => 'krb4',
1.32 matthew 1089: @_,
1090: );
1.144 ! matthew 1091: my ($check4,$check5);
1.80 albertel 1092: if ($in{'kerb_def_auth'} eq 'krb5') {
1093: $check5 = " checked=\"on\"";
1094: } else {
1095: $check4 = " checked=\"on\"";
1096: }
1.144 ! matthew 1097: my $jscall = "javascript:changed_radio('krb',$in{'formname'});";
! 1098: my $result .= &mt
! 1099: ('[_1] Kerberos authenticated with domain [_2] '.
! 1100: '[_3] Version 4 [_4] Version 5',
! 1101: '<input type="radio" name="login" value="krb" '.
! 1102: 'onclick="'.$jscall.'" onchange="'.$jscall.'" />',
! 1103: '<input type="text" size="10" name="krbarg" '.
! 1104: 'value="'.$in{'kerb_def_dom'}.'" '.
! 1105: 'onchange="'.$jscall.'" />',
! 1106: '<input type="radio" name="krbver" value="4" '.$check4.' />',
! 1107: '<input type="radio" name="krbver" value="5" '.$check5.' />');
1.32 matthew 1108: return $result;
1109: }
1110:
1111: sub authform_internal{
1112: my %args = (
1113: formname => 'document.cu',
1114: kerb_def_dom => 'MSU.EDU',
1115: @_,
1116: );
1.144 ! matthew 1117: my $jscall = "javascript:changed_radio('int',$args{'formname'});";
! 1118: my $result.=&mt
! 1119: ('[_1] Internally authenticated (with initial password [_2])',
! 1120: '<input type="radio" name="login" value="int" '.
! 1121: 'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
! 1122: '<input type="text" size="10" name="intarg" value="" '.
! 1123: 'onchange="'.$jscall.'" />');
1.32 matthew 1124: return $result;
1125: }
1126:
1127: sub authform_local{
1128: my %in = (
1129: formname => 'document.cu',
1130: kerb_def_dom => 'MSU.EDU',
1131: @_,
1132: );
1.144 ! matthew 1133: my $jscall = "javascript:changed_radio('loc',$in{'formname'});";
! 1134: my $result.=&mt('[_1] Local Authentication with arguement [_2]',
! 1135: '<input type="radio" name="login" value="loc" '.
! 1136: 'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
! 1137: '<input type="text" size="10" name="locarg" value="" '.
! 1138: 'onchange="'.$jscall.'" />');
1.32 matthew 1139: return $result;
1140: }
1141:
1142: sub authform_filesystem{
1143: my %in = (
1144: formname => 'document.cu',
1145: kerb_def_dom => 'MSU.EDU',
1146: @_,
1147: );
1.144 ! matthew 1148: my $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
! 1149: my $result.= &mt
! 1150: ('[_1] Filesystem Authenticated (with initial password [_2])',
! 1151: '<input type="radio" name="login" value="fsys" '.
! 1152: 'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
! 1153: '<input type="text" size="10" name="fsysarg" value="" '.
! 1154: 'onchange="'.$jscall.'" />');
1.32 matthew 1155: return $result;
1156: }
1157:
1.112 bowersj2 1158: =pod
1159:
1160: =back
1161:
1162: =cut
1.80 albertel 1163:
1164: ###############################################################
1165: ## Get Authentication Defaults for Domain ##
1166: ###############################################################
1167:
1168: =pod
1169:
1.112 bowersj2 1170: =head1 Domains and Authentication
1171:
1172: Returns default authentication type and an associated argument as
1173: listed in file 'domain.tab'.
1174:
1175: =over 4
1176:
1177: =item * get_auth_defaults
1.80 albertel 1178:
1179: get_auth_defaults($target_domain) returns the default authentication
1180: type and an associated argument (initial password or a kerberos domain).
1181: These values are stored in lonTabs/domain.tab
1182:
1183: ($def_auth, $def_arg) = &get_auth_defaults($target_domain);
1184:
1185: If target_domain is not found in domain.tab, returns nothing ('').
1186:
1187: =cut
1188:
1189: #-------------------------------------------
1190: sub get_auth_defaults {
1191: my $domain=shift;
1192: return ($Apache::lonnet::domain_auth_def{$domain},$Apache::lonnet::domain_auth_arg_def{$domain});
1193: }
1194: ###############################################################
1195: ## End Get Authentication Defaults for Domain ##
1196: ###############################################################
1197:
1198: ###############################################################
1199: ## Get Kerberos Defaults for Domain ##
1200: ###############################################################
1201: ##
1202: ## Returns default kerberos version and an associated argument
1203: ## as listed in file domain.tab. If not listed, provides
1204: ## appropriate default domain and kerberos version.
1205: ##
1206: #-------------------------------------------
1207:
1208: =pod
1209:
1.112 bowersj2 1210: =item * get_kerberos_defaults
1.80 albertel 1211:
1212: get_kerberos_defaults($target_domain) returns the default kerberos
1213: version and domain. If not found in domain.tabs, it defaults to
1214: version 4 and the domain of the server.
1215:
1216: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
1217:
1218: =cut
1219:
1220: #-------------------------------------------
1221: sub get_kerberos_defaults {
1222: my $domain=shift;
1223: my ($krbdef,$krbdefdom) =
1224: &Apache::loncommon::get_auth_defaults($domain);
1225: unless ($krbdef =~/^krb/ && $krbdefdom) {
1226: $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
1227: my $krbdefdom=$1;
1228: $krbdefdom=~tr/a-z/A-Z/;
1229: $krbdef = "krb4";
1230: }
1231: return ($krbdef,$krbdefdom);
1232: }
1.112 bowersj2 1233:
1234: =pod
1235:
1236: =back
1237:
1238: =cut
1.32 matthew 1239:
1.46 matthew 1240: ###############################################################
1241: ## Thesaurus Functions ##
1242: ###############################################################
1.20 www 1243:
1.46 matthew 1244: =pod
1.20 www 1245:
1.112 bowersj2 1246: =head1 Thesaurus Functions
1247:
1248: =over 4
1249:
1250: =item * initialize_keywords
1.46 matthew 1251:
1252: Initializes the package variable %Keywords if it is empty. Uses the
1253: package variable $thesaurus_db_file.
1254:
1255: =cut
1256:
1257: ###################################################
1258:
1259: sub initialize_keywords {
1260: return 1 if (scalar keys(%Keywords));
1261: # If we are here, %Keywords is empty, so fill it up
1262: # Make sure the file we need exists...
1263: if (! -e $thesaurus_db_file) {
1264: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
1265: " failed because it does not exist");
1266: return 0;
1267: }
1268: # Set up the hash as a database
1269: my %thesaurus_db;
1270: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1271: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1272: &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
1273: $thesaurus_db_file);
1274: return 0;
1275: }
1276: # Get the average number of appearances of a word.
1277: my $avecount = $thesaurus_db{'average.count'};
1278: # Put keywords (those that appear > average) into %Keywords
1279: while (my ($word,$data)=each (%thesaurus_db)) {
1280: my ($count,undef) = split /:/,$data;
1281: $Keywords{$word}++ if ($count > $avecount);
1282: }
1283: untie %thesaurus_db;
1284: # Remove special values from %Keywords.
1285: foreach ('total.count','average.count') {
1286: delete($Keywords{$_}) if (exists($Keywords{$_}));
1287: }
1288: return 1;
1289: }
1290:
1291: ###################################################
1292:
1293: =pod
1294:
1.112 bowersj2 1295: =item * keyword($word)
1.46 matthew 1296:
1297: Returns true if $word is a keyword. A keyword is a word that appears more
1298: than the average number of times in the thesaurus database. Calls
1299: &initialize_keywords
1300:
1301: =cut
1302:
1303: ###################################################
1.20 www 1304:
1305: sub keyword {
1.46 matthew 1306: return if (!&initialize_keywords());
1307: my $word=lc(shift());
1308: $word=~s/\W//g;
1309: return exists($Keywords{$word});
1.20 www 1310: }
1.46 matthew 1311:
1312: ###############################################################
1313:
1314: =pod
1.20 www 1315:
1.112 bowersj2 1316: =item * get_related_words
1.46 matthew 1317:
1318: Look up a word in the thesaurus. Takes a scalar arguement and returns
1319: an array of words. If the keyword is not in the thesaurus, an empty array
1320: will be returned. The order of the words returned is determined by the
1321: database which holds them.
1322:
1323: Uses global $thesaurus_db_file.
1324:
1325: =cut
1326:
1327: ###############################################################
1328: sub get_related_words {
1329: my $keyword = shift;
1330: my %thesaurus_db;
1331: if (! -e $thesaurus_db_file) {
1332: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
1333: "failed because the file does not exist");
1334: return ();
1335: }
1336: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1337: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1338: return ();
1339: }
1340: my @Words=();
1341: if (exists($thesaurus_db{$keyword})) {
1342: $_ = $thesaurus_db{$keyword};
1343: (undef,@Words) = split/:/; # The first element is the number of times
1344: # the word appears. We do not need it now.
1345: for (my $i=0;$i<=$#Words;$i++) {
1346: ($Words[$i],undef)= split/\,/,$Words[$i];
1.20 www 1347: }
1348: }
1.46 matthew 1349: untie %thesaurus_db;
1350: return @Words;
1.14 harris41 1351: }
1.46 matthew 1352:
1.112 bowersj2 1353: =pod
1354:
1355: =back
1356:
1357: =cut
1.61 www 1358:
1359: # -------------------------------------------------------------- Plaintext name
1.81 albertel 1360: =pod
1361:
1.112 bowersj2 1362: =head1 User Name Functions
1363:
1364: =over 4
1365:
1366: =item * plainname($uname,$udom)
1.81 albertel 1367:
1.112 bowersj2 1368: Takes a users logon name and returns it as a string in
1369: "first middle last generation" form
1.81 albertel 1370:
1371: =cut
1.61 www 1372:
1.81 albertel 1373: ###############################################################
1.61 www 1374: sub plainname {
1375: my ($uname,$udom)=@_;
1376: my %names=&Apache::lonnet::get('environment',
1377: ['firstname','middlename','lastname','generation'],
1378: $udom,$uname);
1.62 www 1379: my $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
1.61 www 1380: $names{'lastname'}.' '.$names{'generation'};
1.62 www 1381: $name=~s/\s+$//;
1382: $name=~s/\s+/ /g;
1383: return $name;
1.61 www 1384: }
1.66 www 1385:
1386: # -------------------------------------------------------------------- Nickname
1.81 albertel 1387: =pod
1388:
1.112 bowersj2 1389: =item * nickname($uname,$udom)
1.81 albertel 1390:
1391: Gets a users name and returns it as a string as
1392:
1393: ""nickname""
1.66 www 1394:
1.81 albertel 1395: if the user has a nickname or
1396:
1397: "first middle last generation"
1398:
1399: if the user does not
1400:
1401: =cut
1.66 www 1402:
1403: sub nickname {
1404: my ($uname,$udom)=@_;
1405: my %names=&Apache::lonnet::get('environment',
1406: ['nickname','firstname','middlename','lastname','generation'],$udom,$uname);
1.68 albertel 1407: my $name=$names{'nickname'};
1.66 www 1408: if ($name) {
1409: $name='"'.$name.'"';
1410: } else {
1411: $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
1412: $names{'lastname'}.' '.$names{'generation'};
1413: $name=~s/\s+$//;
1414: $name=~s/\s+/ /g;
1415: }
1416: return $name;
1417: }
1418:
1.61 www 1419:
1420: # ------------------------------------------------------------------ Screenname
1.81 albertel 1421:
1422: =pod
1423:
1.112 bowersj2 1424: =item * screenname($uname,$udom)
1.81 albertel 1425:
1426: Gets a users screenname and returns it as a string
1427:
1428: =cut
1.61 www 1429:
1430: sub screenname {
1431: my ($uname,$udom)=@_;
1432: my %names=
1433: &Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68 albertel 1434: return $names{'screenname'};
1.62 www 1435: }
1436:
1437: # ------------------------------------------------------------- Message Wrapper
1438:
1439: sub messagewrapper {
1440: my ($link,$un,$do)=@_;
1441: return
1442: "<a href='/adm/email?compose=individual&recname=$un&recdom=$do'>$link</a>";
1.74 www 1443: }
1444: # --------------------------------------------------------------- Notes Wrapper
1445:
1446: sub noteswrapper {
1447: my ($link,$un,$do)=@_;
1448: return
1449: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62 www 1450: }
1451: # ------------------------------------------------------------- Aboutme Wrapper
1452:
1453: sub aboutmewrapper {
1.69 matthew 1454: my ($link,$username,$domain)=@_;
1455: return "<a href='/adm/$domain/$username/aboutme'>$link</a>";
1.62 www 1456: }
1457:
1458: # ------------------------------------------------------------ Syllabus Wrapper
1459:
1460:
1461: sub syllabuswrapper {
1.109 matthew 1462: my ($linktext,$coursedir,$domain,$fontcolor)=@_;
1463: if ($fontcolor) {
1464: $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>';
1465: }
1466: return "<a href='/public/$domain/$coursedir/syllabus'>$linktext</a>";
1.61 www 1467: }
1.14 harris41 1468:
1.112 bowersj2 1469: =pod
1470:
1471: =back
1472:
1473: =head1 Access .tab File Data
1474:
1475: =over 4
1476:
1477: =item * languageids()
1478:
1479: returns list of all language ids
1480:
1481: =cut
1482:
1.14 harris41 1483: sub languageids {
1.16 harris41 1484: return sort(keys(%language));
1.14 harris41 1485: }
1486:
1.112 bowersj2 1487: =pod
1488:
1489: =item * languagedescription()
1490:
1491: returns description of a specified language id
1492:
1493: =cut
1494:
1.14 harris41 1495: sub languagedescription {
1.125 www 1496: my $code=shift;
1497: return ($supported_language{$code}?'* ':'').
1498: $language{$code}.
1.126 www 1499: ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.97 www 1500: }
1501:
1.112 bowersj2 1502: =pod
1503:
1504: =item * copyrightids()
1505:
1506: returns list of all copyrights
1507:
1508: =cut
1509:
1510: sub copyrightids {
1511: return sort(keys(%cprtag));
1512: }
1513:
1514: =pod
1515:
1516: =item * copyrightdescription()
1517:
1518: returns description of a specified copyright id
1519:
1520: =cut
1521:
1522: sub copyrightdescription {
1523: return $cprtag{shift(@_)};
1524: }
1525:
1526: =pod
1527:
1528: =item * filecategories()
1529:
1530: returns list of all file categories
1531:
1532: =cut
1533:
1534: sub filecategories {
1535: return sort(keys(%category_extensions));
1536: }
1537:
1538: =pod
1539:
1540: =item * filecategorytypes()
1541:
1542: returns list of file types belonging to a given file
1543: category
1544:
1545: =cut
1546:
1547: sub filecategorytypes {
1548: return @{$category_extensions{lc($_[0])}};
1549: }
1550:
1551: =pod
1552:
1553: =item * fileembstyle()
1554:
1555: returns embedding style for a specified file type
1556:
1557: =cut
1558:
1559: sub fileembstyle {
1560: return $fe{lc(shift(@_))};
1561: }
1562:
1563: =pod
1564:
1565: =item * filedescription()
1566:
1567: returns description for a specified file type
1568:
1569: =cut
1570:
1571: sub filedescription {
1572: return $fd{lc(shift(@_))};
1573: }
1574:
1575: =pod
1576:
1577: =item * filedescriptionex()
1578:
1579: returns description for a specified file type with
1580: extra formatting
1581:
1582: =cut
1583:
1584: sub filedescriptionex {
1585: my $ex=shift;
1586: return '.'.$ex.' '.$fd{lc($ex)};
1587: }
1588:
1589: # End of .tab access
1590: =pod
1591:
1592: =back
1593:
1594: =cut
1595:
1596: # ------------------------------------------------------------------ File Types
1597: sub fileextensions {
1598: return sort(keys(%fe));
1599: }
1600:
1.97 www 1601: # ----------------------------------------------------------- Display Languages
1602: # returns a hash with all desired display languages
1603: #
1604:
1605: sub display_languages {
1606: my %languages=();
1.118 www 1607: foreach (&preferred_languages()) {
1608: $languages{$_}=1;
1.97 www 1609: }
1610: &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1611: if ($ENV{'form.displaylanguage'}) {
1612: foreach (split(/\s*(\,|\;|\:)\s*/,$ENV{'form.displaylanguage'})) {
1613: $languages{$_}=1;
1614: }
1615: }
1616: return %languages;
1.14 harris41 1617: }
1618:
1.117 www 1619: sub preferred_languages {
1620: my @languages=();
1621: if ($ENV{'environment.languages'}) {
1622: @languages=split(/\s*(\,|\;|\:)\s*/,$ENV{'environment.languages'});
1623: }
1624: if ($ENV{'course.'.$ENV{'request.course.id'}.'.languages'}) {
1625: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
1626: $ENV{'course.'.$ENV{'request.course.id'}.'.languages'}));
1627: }
1.118 www 1628: my $browser=(split(/\;/,$ENV{'HTTP_ACCEPT_LANGUAGE'}))[0];
1629: if ($browser) {
1630: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$browser));
1631: }
1632: if ($Apache::lonnet::domain_lang_def{$ENV{'user.domain'}}) {
1633: @languages=(@languages,
1634: $Apache::lonnet::domain_lang_def{$ENV{'user.domain'}});
1635: }
1636: if ($Apache::lonnet::domain_lang_def{$ENV{'request.role.domain'}}) {
1637: @languages=(@languages,
1638: $Apache::lonnet::domain_lang_def{$ENV{'request.role.domain'}});
1639: }
1640: if ($Apache::lonnet::domain_lang_def{
1641: $Apache::lonnet::perlvar{'lonDefDomain'}}) {
1642: @languages=(@languages,
1643: $Apache::lonnet::domain_lang_def{
1644: $Apache::lonnet::perlvar{'lonDefDomain'}});
1645: }
1646: # turn "en-ca" into "en-ca,en"
1647: my @genlanguages;
1648: foreach (@languages) {
1649: unless ($_=~/\w/) { next; }
1650: push (@genlanguages,$_);
1651: if ($_=~/(\-|\_)/) {
1652: push (@genlanguages,(split(/(\-|\_)/,$_))[0]);
1653: }
1654: }
1655: return @genlanguages;
1.117 www 1656: }
1657:
1.112 bowersj2 1658: ###############################################################
1659: ## Student Answer Attempts ##
1660: ###############################################################
1661:
1662: =pod
1663:
1664: =head1 Alternate Problem Views
1665:
1666: =over 4
1667:
1668: =item * get_previous_attempt($symb, $username, $domain, $course,
1669: $getattempt, $regexp, $gradesub)
1670:
1671: Return string with previous attempt on problem. Arguments:
1672:
1673: =over 4
1674:
1675: =item * $symb: Problem, including path
1676:
1677: =item * $username: username of the desired student
1678:
1679: =item * $domain: domain of the desired student
1.14 harris41 1680:
1.112 bowersj2 1681: =item * $course: Course ID
1.14 harris41 1682:
1.112 bowersj2 1683: =item * $getattempt: Leave blank for all attempts, otherwise put
1684: something
1.14 harris41 1685:
1.112 bowersj2 1686: =item * $regexp: if string matches this regexp, the string will be
1687: sent to $gradesub
1.14 harris41 1688:
1.112 bowersj2 1689: =item * $gradesub: routine that processes the string if it matches $regexp
1.14 harris41 1690:
1.112 bowersj2 1691: =back
1.14 harris41 1692:
1.112 bowersj2 1693: The output string is a table containing all desired attempts, if any.
1.16 harris41 1694:
1.112 bowersj2 1695: =cut
1.1 albertel 1696:
1697: sub get_previous_attempt {
1.43 ng 1698: my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1 albertel 1699: my $prevattempts='';
1.43 ng 1700: no strict 'refs';
1.1 albertel 1701: if ($symb) {
1.3 albertel 1702: my (%returnhash)=
1703: &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1 albertel 1704: if ($returnhash{'version'}) {
1705: my %lasthash=();
1706: my $version;
1707: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.19 harris41 1708: foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
1.1 albertel 1709: $lasthash{$_}=$returnhash{$version.':'.$_};
1.19 harris41 1710: }
1.1 albertel 1711: }
1.43 ng 1712: $prevattempts='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.40 ng 1713: $prevattempts.='<table border="0" width="100%"><tr bgcolor="#e6ffff"><td>History</td>';
1.16 harris41 1714: foreach (sort(keys %lasthash)) {
1.31 albertel 1715: my ($ign,@parts) = split(/\./,$_);
1.41 ng 1716: if ($#parts > 0) {
1.31 albertel 1717: my $data=$parts[-1];
1718: pop(@parts);
1.40 ng 1719: $prevattempts.='<td>Part '.join('.',@parts).'<br />'.$data.' </td>';
1.31 albertel 1720: } else {
1.41 ng 1721: if ($#parts == 0) {
1722: $prevattempts.='<th>'.$parts[0].'</th>';
1723: } else {
1724: $prevattempts.='<th>'.$ign.'</th>';
1725: }
1.31 albertel 1726: }
1.16 harris41 1727: }
1.40 ng 1728: if ($getattempt eq '') {
1729: for ($version=1;$version<=$returnhash{'version'};$version++) {
1730: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Transaction '.$version.'</td>';
1731: foreach (sort(keys %lasthash)) {
1732: my $value;
1733: if ($_ =~ /timestamp/) {
1734: $value=scalar(localtime($returnhash{$version.':'.$_}));
1735: } else {
1736: $value=$returnhash{$version.':'.$_};
1737: }
1.142 albertel 1738: $prevattempts.='<td>'.&Apache::lonnet::unescape($value).' </td>';
1.40 ng 1739: }
1740: }
1.1 albertel 1741: }
1.40 ng 1742: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Current</td>';
1.16 harris41 1743: foreach (sort(keys %lasthash)) {
1.5 albertel 1744: my $value;
1745: if ($_ =~ /timestamp/) {
1746: $value=scalar(localtime($lasthash{$_}));
1747: } else {
1748: $value=$lasthash{$_};
1749: }
1.142 albertel 1750: $value=&Apache::lonnet::unescape($value);
1.49 ng 1751: if ($_ =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40 ng 1752: $prevattempts.='<td>'.$value.' </td>';
1.16 harris41 1753: }
1.40 ng 1754: $prevattempts.='</tr></table></td></tr></table>';
1.1 albertel 1755: } else {
1756: $prevattempts='Nothing submitted - no attempts.';
1757: }
1758: } else {
1759: $prevattempts='No data.';
1760: }
1.10 albertel 1761: }
1762:
1.107 albertel 1763: sub relative_to_absolute {
1764: my ($url,$output)=@_;
1765: my $parser=HTML::TokeParser->new(\$output);
1766: my $token;
1767: my $thisdir=$url;
1768: my @rlinks=();
1769: while ($token=$parser->get_token) {
1770: if ($token->[0] eq 'S') {
1771: if ($token->[1] eq 'a') {
1772: if ($token->[2]->{'href'}) {
1773: $rlinks[$#rlinks+1]=$token->[2]->{'href'};
1774: }
1775: } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
1776: $rlinks[$#rlinks+1]=$token->[2]->{'src'};
1777: } elsif ($token->[1] eq 'base') {
1778: $thisdir=$token->[2]->{'href'};
1779: }
1780: }
1781: }
1782: $thisdir=~s-/[^/]*$--;
1783: foreach (@rlinks) {
1784: unless (($_=~/^http:\/\//i) ||
1785: ($_=~/^\//) ||
1786: ($_=~/^javascript:/i) ||
1787: ($_=~/^mailto:/i) ||
1788: ($_=~/^\#/)) {
1789: my $newlocation=&Apache::lonnet::hreflocation($thisdir,$_);
1790: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
1791: }
1792: }
1793: # -------------------------------------------------- Deal with Applet codebases
1794: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
1795: return $output;
1796: }
1797:
1.112 bowersj2 1798: =pod
1799:
1800: =item * get_student_view
1801:
1802: show a snapshot of what student was looking at
1803:
1804: =cut
1805:
1.10 albertel 1806: sub get_student_view {
1.64 sakharuk 1807: my ($symb,$username,$domain,$courseid,$target) = @_;
1.114 www 1808: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.10 albertel 1809: my (%old,%moreenv);
1810: my @elements=('symb','courseid','domain','username');
1811: foreach my $element (@elements) {
1812: $old{$element}=$ENV{'form.grade_'.$element};
1813: $moreenv{'form.grade_'.$element}=eval '$'.$element #'
1814: }
1.64 sakharuk 1815: if ($target eq 'tex') {$moreenv{'form.grade_target'} = 'tex';}
1.11 albertel 1816: &Apache::lonnet::appenv(%moreenv);
1.107 albertel 1817: $feedurl=&Apache::lonnet::clutter($feedurl);
1818: my $userview=&Apache::lonnet::ssi_body($feedurl);
1.11 albertel 1819: &Apache::lonnet::delenv('form.grade_');
1820: foreach my $element (@elements) {
1821: $ENV{'form.grade_'.$element}=$old{$element};
1822: }
1823: $userview=~s/\<body[^\>]*\>//gi;
1824: $userview=~s/\<\/body\>//gi;
1825: $userview=~s/\<html\>//gi;
1826: $userview=~s/\<\/html\>//gi;
1827: $userview=~s/\<head\>//gi;
1828: $userview=~s/\<\/head\>//gi;
1829: $userview=~s/action\s*\=/would_be_action\=/gi;
1.107 albertel 1830: $userview=&relative_to_absolute($feedurl,$userview);
1.11 albertel 1831: return $userview;
1832: }
1833:
1.112 bowersj2 1834: =pod
1835:
1836: =item * get_student_answers()
1837:
1838: show a snapshot of how student was answering problem
1839:
1840: =cut
1841:
1.11 albertel 1842: sub get_student_answers {
1.100 sakharuk 1843: my ($symb,$username,$domain,$courseid,%form) = @_;
1.114 www 1844: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.11 albertel 1845: my (%old,%moreenv);
1846: my @elements=('symb','courseid','domain','username');
1847: foreach my $element (@elements) {
1848: $old{$element}=$ENV{'form.grade_'.$element};
1849: $moreenv{'form.grade_'.$element}=eval '$'.$element #'
1850: }
1851: $moreenv{'form.grade_target'}='answer';
1.10 albertel 1852: &Apache::lonnet::appenv(%moreenv);
1.100 sakharuk 1853: my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%form);
1.10 albertel 1854: &Apache::lonnet::delenv('form.grade_');
1855: foreach my $element (@elements) {
1856: $ENV{'form.grade_'.$element}=$old{$element};
1857: }
1858: return $userview;
1.1 albertel 1859: }
1.116 albertel 1860:
1861: =pod
1862:
1863: =item * &submlink()
1864:
1865: Inputs: $text $uname $udom $symb
1866:
1867: Returns: A link to grades.pm such as to see the SUBM view of a student
1868:
1869: =cut
1870:
1871: ###############################################
1872: sub submlink {
1873: my ($text,$uname,$udom,$symb)=@_;
1874: if (!($uname && $udom)) {
1875: (my $cursymb, my $courseid,$udom,$uname)=
1876: &Apache::lonxml::whichuser($symb);
1877: if (!$symb) { $symb=$cursymb; }
1878: }
1879: if (!$symb) { $symb=&symbread(); }
1880: return '<a href="/adm/grades?symb='.$symb.'&student='.$uname.
1881: '&userdom='.$udom.'&command=submission">'.$text.'</a>';
1882: }
1883: ##############################################
1.37 matthew 1884:
1.112 bowersj2 1885: =pod
1886:
1887: =back
1888:
1889: =cut
1890:
1.37 matthew 1891: ###############################################
1.51 www 1892:
1893:
1894: sub timehash {
1895: my @ltime=localtime(shift);
1896: return ( 'seconds' => $ltime[0],
1897: 'minutes' => $ltime[1],
1898: 'hours' => $ltime[2],
1899: 'day' => $ltime[3],
1900: 'month' => $ltime[4]+1,
1901: 'year' => $ltime[5]+1900,
1902: 'weekday' => $ltime[6],
1903: 'dayyear' => $ltime[7]+1,
1904: 'dlsav' => $ltime[8] );
1905: }
1906:
1907: sub maketime {
1908: my %th=@_;
1909: return POSIX::mktime(
1910: ($th{'seconds'},$th{'minutes'},$th{'hours'},
1911: $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,$th{'dlsav'}));
1912: }
1913:
1.70 www 1914:
1915: #########################################
1916: #
1917: # Retro-fixing of un-backward-compatible time format
1918:
1919: sub unsqltime {
1920: my $timestamp=shift;
1921: if ($timestamp=~/^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/) {
1922: $timestamp=&maketime(
1923: 'year'=>$1,'month'=>$2,'day'=>$3,
1924: 'hours'=>$4,'minutes'=>$5,'seconds'=>$6);
1925: }
1926: return $timestamp;
1927: }
1928:
1929: #########################################
1.51 www 1930:
1931: sub findallcourses {
1932: my %courses=();
1933: my $now=time;
1934: foreach (keys %ENV) {
1935: if ($_=~/^user\.role\.\w+\.\/(\w+)\/(\w+)/) {
1936: my ($starttime,$endtime)=$ENV{$_};
1937: my $active=1;
1938: if ($starttime) {
1939: if ($now<$starttime) { $active=0; }
1940: }
1941: if ($endtime) {
1942: if ($now>$endtime) { $active=0; }
1943: }
1944: if ($active) { $courses{$1.'_'.$2}=1; }
1945: }
1946: }
1947: return keys %courses;
1948: }
1.37 matthew 1949:
1.54 www 1950: ###############################################
1.60 matthew 1951: ###############################################
1952:
1953: =pod
1954:
1.112 bowersj2 1955: =head1 Domain Template Functions
1956:
1957: =over 4
1958:
1959: =item * &determinedomain()
1.60 matthew 1960:
1961: Inputs: $domain (usually will be undef)
1962:
1.63 www 1963: Returns: Determines which domain should be used for designs
1.60 matthew 1964:
1965: =cut
1.54 www 1966:
1.60 matthew 1967: ###############################################
1.63 www 1968: sub determinedomain {
1969: my $domain=shift;
1970: if (! $domain) {
1.60 matthew 1971: # Determine domain if we have not been given one
1972: $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1973: if ($ENV{'user.domain'}) { $domain=$ENV{'user.domain'}; }
1974: if ($ENV{'request.role.domain'}) {
1975: $domain=$ENV{'request.role.domain'};
1976: }
1977: }
1.63 www 1978: return $domain;
1979: }
1980: ###############################################
1981: =pod
1982:
1.112 bowersj2 1983: =item * &domainlogo()
1.63 www 1984:
1985: Inputs: $domain (usually will be undef)
1986:
1987: Returns: A link to a domain logo, if the domain logo exists.
1988: If the domain logo does not exist, a description of the domain.
1989:
1990: =cut
1.112 bowersj2 1991:
1.63 www 1992: ###############################################
1993: sub domainlogo {
1994: my $domain = &determinedomain(shift);
1995: # See if there is a logo
1.59 www 1996: if (-e '/home/httpd/html/adm/lonDomLogos/'.$domain.'.gif') {
1.83 albertel 1997: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
1998: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1999: return '<img src="http://'.$ENV{'HTTP_HOST'}.':'.$lonhttpdPort.
2000: '/adm/lonDomLogos/'.$domain.'.gif" />';
1.60 matthew 2001: } elsif(exists($Apache::lonnet::domaindescription{$domain})) {
2002: return $Apache::lonnet::domaindescription{$domain};
1.59 www 2003: } else {
1.60 matthew 2004: return '';
1.59 www 2005: }
2006: }
1.63 www 2007: ##############################################
2008:
2009: =pod
2010:
1.112 bowersj2 2011: =item * &designparm()
1.63 www 2012:
2013: Inputs: $which parameter; $domain (usually will be undef)
2014:
2015: Returns: value of designparamter $which
2016:
2017: =cut
1.112 bowersj2 2018:
1.63 www 2019: ##############################################
2020: sub designparm {
2021: my ($which,$domain)=@_;
1.110 www 2022: if ($ENV{'browser.blackwhite'} eq 'on') {
2023: if ($which=~/\.(font|alink|vlink|link)$/) {
2024: return '#000000';
2025: }
2026: if ($which=~/\.(pgbg|sidebg)$/) {
2027: return '#FFFFFF';
2028: }
2029: if ($which=~/\.tabbg$/) {
2030: return '#CCCCCC';
2031: }
2032: }
1.96 www 2033: if ($ENV{'environment.color.'.$which}) {
2034: return $ENV{'environment.color.'.$which};
2035: }
1.63 www 2036: $domain=&determinedomain($domain);
2037: if ($designhash{$domain.'.'.$which}) {
2038: return $designhash{$domain.'.'.$which};
2039: } else {
2040: return $designhash{'default.'.$which};
2041: }
2042: }
1.59 www 2043:
1.60 matthew 2044: ###############################################
2045: ###############################################
2046:
2047: =pod
2048:
1.112 bowersj2 2049: =back
2050:
2051: =head1 HTTP Helpers
2052:
2053: =over 4
2054:
2055: =item * &bodytag()
1.60 matthew 2056:
2057: Returns a uniform header for LON-CAPA web pages.
2058:
2059: Inputs:
2060:
1.112 bowersj2 2061: =over 4
2062:
2063: =item * $title, A title to be displayed on the page.
2064:
2065: =item * $function, the current role (can be undef).
2066:
2067: =item * $addentries, extra parameters for the <body> tag.
2068:
2069: =item * $bodyonly, if defined, only return the <body> tag.
2070:
2071: =item * $domain, if defined, force a given domain.
2072:
2073: =item * $forcereg, if page should register as content page (relevant for
1.86 www 2074: text interface only)
1.60 matthew 2075:
1.112 bowersj2 2076: =back
2077:
1.60 matthew 2078: Returns: A uniform header for LON-CAPA web pages.
2079: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
2080: If $bodyonly is undef or zero, an html string containing a <body> tag and
2081: other decorations will be returned.
2082:
2083: =cut
2084:
1.54 www 2085: sub bodytag {
1.86 www 2086: my ($title,$function,$addentries,$bodyonly,$domain,$forcereg)=@_;
1.117 www 2087: $title=&mt($title);
1.55 www 2088: unless ($function) {
2089: $function='student';
2090: if ($ENV{'request.role'}=~/^(cc|in|ta|ep)/) {
2091: $function='coordinator';
2092: }
2093: if ($ENV{'request.role'}=~/^(su|dc|ad|li)/) {
2094: $function='admin';
2095: }
2096: if (($ENV{'request.role'}=~/^(au|ca)/) ||
2097: ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
2098: $function='author';
2099: }
2100: }
1.63 www 2101: my $img=&designparm($function.'.img',$domain);
2102: my $pgbg=&designparm($function.'.pgbg',$domain);
2103: my $tabbg=&designparm($function.'.tabbg',$domain);
2104: my $font=&designparm($function.'.font',$domain);
2105: my $link=&designparm($function.'.link',$domain);
2106: my $alink=&designparm($function.'.alink',$domain);
2107: my $vlink=&designparm($function.'.vlink',$domain);
2108: my $sidebg=&designparm($function.'.sidebg',$domain);
1.110 www 2109: # Accessibility font enhance
2110: unless ($addentries) { $addentries=''; }
2111: if ($ENV{'browser.fontenhance'} eq 'on') {
2112: $addentries.=' style="font-size: x-large"';
2113: }
1.63 www 2114: # role and realm
1.55 www 2115: my ($role,$realm)
2116: =&Apache::lonnet::plaintext((split(/\./,$ENV{'request.role'}))[0]);
2117: # realm
1.54 www 2118: if ($ENV{'request.course.id'}) {
1.55 www 2119: $realm=
2120: $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.54 www 2121: }
1.55 www 2122: unless ($realm) { $realm=' '; }
2123: # Set messages
1.60 matthew 2124: my $messages=&domainlogo($domain);
1.101 www 2125: # Port for miniserver
1.83 albertel 2126: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
2127: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1.101 www 2128: # construct main body tag
1.60 matthew 2129: my $bodytag = <<END;
1.54 www 2130: <body bgcolor="$pgbg" text="$font" alink="$alink" vlink="$vlink" link="$link"
2131: $addentries>
1.60 matthew 2132: END
1.94 www 2133: my $upperleft='<img src="http://'.$ENV{'HTTP_HOST'}.':'.
2134: $lonhttpdPort.$img.'" />';
1.60 matthew 2135: if ($bodyonly) {
2136: return $bodytag;
1.79 www 2137: } elsif ($ENV{'browser.interface'} eq 'textual') {
1.95 www 2138: # Accessibility
1.93 www 2139: return $bodytag.&Apache::lonmenu::menubuttons($forcereg,'web',
2140: $forcereg).
2141: '<h1>LON-CAPA: '.$title.'</h1>';
2142: } elsif ($ENV{'environment.remote'} eq 'off') {
1.95 www 2143: # No Remote
2144: return $bodytag.&Apache::lonmenu::menubuttons($forcereg,'web',
2145: $forcereg).
2146: '<table bgcolor="'.$pgbg.'" width="100%" border="0" cellspacing="3" cellpadding="3"><tr><td bgcolor="'.$tabbg.'"><font size="+3" color="'.$font.'"><b>'.$title.
2147: '</b></font></td></tr></table>';
1.94 www 2148: }
1.95 www 2149:
1.93 www 2150: #
1.95 www 2151: # Top frame rendering, Remote is up
1.93 www 2152: #
1.94 www 2153: return(<<ENDBODY);
1.60 matthew 2154: $bodytag
1.55 www 2155: <table width="100%" cellspacing="0" border="0" cellpadding="0">
1.95 www 2156: <tr><td bgcolor="$sidebg">
1.94 www 2157: $upperleft</td>
1.95 www 2158: <td bgcolor="$sidebg" align="right">$messages </td>
1.55 www 2159: </tr>
1.54 www 2160: <tr>
1.55 www 2161: <td rowspan="3" bgcolor="$tabbg">
2162: <font size="5"><b>$title</b></font>
1.54 www 2163: <td bgcolor="$tabbg" align="right">
2164: <font size="2">
2165: $ENV{'environment.firstname'}
2166: $ENV{'environment.middlename'}
2167: $ENV{'environment.lastname'}
2168: $ENV{'environment.generation'}
1.55 www 2169: </font>
1.54 www 2170: </td>
2171: </tr>
2172: <tr><td bgcolor="$tabbg" align="right">
1.55 www 2173: <font size="2">$role</font>
1.54 www 2174: </td></tr>
1.55 www 2175: <tr>
2176: <td bgcolor="$tabbg" align="right"><font size="2">$realm</font> </td></tr>
1.54 www 2177: </table><br>
2178: ENDBODY
2179: }
1.99 www 2180:
2181: ###############################################
2182:
2183: sub get_posted_cgi {
2184: my $r=shift;
2185:
2186: my $buffer;
2187:
2188: $r->read($buffer,$r->header_in('Content-length'),0);
2189: unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
2190: my @pairs=split(/&/,$buffer);
2191: my $pair;
2192: foreach $pair (@pairs) {
2193: my ($name,$value) = split(/=/,$pair);
2194: $value =~ tr/+/ /;
2195: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2196: $name =~ tr/+/ /;
2197: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2198: &add_to_env("form.$name",$value);
2199: }
2200: } else {
2201: my $contentsep=$1;
2202: my @lines = split (/\n/,$buffer);
2203: my $name='';
2204: my $value='';
2205: my $fname='';
2206: my $fmime='';
2207: my $i;
2208: for ($i=0;$i<=$#lines;$i++) {
2209: if ($lines[$i]=~/^$contentsep/) {
2210: if ($name) {
2211: chomp($value);
2212: if ($fname) {
2213: $ENV{"form.$name.filename"}=$fname;
2214: $ENV{"form.$name.mimetype"}=$fmime;
2215: } else {
2216: $value=~s/\s+$//s;
2217: }
2218: &add_to_env("form.$name",$value);
2219: }
2220: if ($i<$#lines) {
2221: $i++;
2222: $lines[$i]=~
2223: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
2224: $name=$1;
2225: $value='';
2226: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
2227: $fname=$1;
2228: if
2229: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
2230: $fmime=$1;
2231: $i++;
2232: } else {
2233: $fmime='';
2234: }
2235: } else {
2236: $fname='';
2237: $fmime='';
2238: }
2239: $i++;
2240: }
2241: } else {
2242: $value.=$lines[$i]."\n";
2243: }
2244: }
2245: }
2246: $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
2247: $r->method_number(M_GET);
2248: $r->method('GET');
2249: $r->headers_in->unset('Content-length');
2250: }
2251:
1.112 bowersj2 2252: =pod
2253:
2254: =item * get_unprocessed_cgi($query,$possible_names)
2255:
2256: Modify the %ENV hash to contain unprocessed CGI form parameters held in
2257: $query. The parameters listed in $possible_names (an array reference),
2258: will be set in $ENV{'form.name'} if they do not already exist.
2259:
2260: Typically called with $ENV{'QUERY_STRING'} as the first parameter.
2261: $possible_names is an ref to an array of form element names. As an example:
2262: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
2263: will result in $ENV{'form.uname'} and $ENV{'form.udom'} being set.
2264:
2265: =cut
1.1 albertel 2266:
1.6 albertel 2267: sub get_unprocessed_cgi {
1.25 albertel 2268: my ($query,$possible_names)= @_;
1.26 matthew 2269: # $Apache::lonxml::debug=1;
1.16 harris41 2270: foreach (split(/&/,$query)) {
1.6 albertel 2271: my ($name, $value) = split(/=/,$_);
1.25 albertel 2272: $name = &Apache::lonnet::unescape($name);
2273: if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
2274: $value =~ tr/+/ /;
2275: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2276: &Apache::lonxml::debug("Seting :$name: to :$value:");
1.30 albertel 2277: unless (defined($ENV{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25 albertel 2278: }
1.16 harris41 2279: }
1.6 albertel 2280: }
2281:
1.112 bowersj2 2282: =pod
2283:
2284: =item * cacheheader()
2285:
2286: returns cache-controlling header code
2287:
2288: =cut
2289:
1.7 albertel 2290: sub cacheheader {
1.23 www 2291: unless ($ENV{'request.method'} eq 'GET') { return ''; }
1.8 albertel 2292: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
1.7 albertel 2293: my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
2294: <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
2295: <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
2296: return $output;
2297: }
2298:
1.112 bowersj2 2299: =pod
2300:
2301: =item * no_cache($r)
2302:
2303: specifies header code to not have cache
2304:
2305: =cut
2306:
1.9 albertel 2307: sub no_cache {
2308: my ($r) = @_;
1.23 www 2309: unless ($ENV{'request.method'} eq 'GET') { return ''; }
1.24 albertel 2310: #my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
1.9 albertel 2311: $r->no_cache(1);
2312: $r->header_out("Pragma" => "no-cache");
1.24 albertel 2313: #$r->header_out("Expires" => $date);
1.123 www 2314: }
2315:
2316: sub content_type {
2317: my ($r,$type,$charset) = @_;
2318: unless ($charset) {
2319: $charset=&Apache::lonlocal::current_encoding;
2320: }
2321: $r->content_type($type.($charset?'; charset='.$charset:''));
1.9 albertel 2322: }
1.25 albertel 2323:
1.112 bowersj2 2324: =pod
2325:
2326: =item * add_to_env($name,$value)
2327:
2328: adds $name to the %ENV hash with value
2329: $value, if $name already exists, the entry is converted to an array
2330: reference and $value is added to the array.
2331:
2332: =cut
2333:
1.25 albertel 2334: sub add_to_env {
2335: my ($name,$value)=@_;
1.28 albertel 2336: if (defined($ENV{$name})) {
1.27 albertel 2337: if (ref($ENV{$name})) {
1.25 albertel 2338: #already have multiple values
2339: push(@{ $ENV{$name} },$value);
2340: } else {
2341: #first time seeing multiple values, convert hash entry to an arrayref
2342: my $first=$ENV{$name};
2343: undef($ENV{$name});
2344: push(@{ $ENV{$name} },$first,$value);
2345: }
2346: } else {
2347: $ENV{$name}=$value;
2348: }
1.31 albertel 2349: }
2350:
1.41 ng 2351: =pod
1.45 matthew 2352:
2353: =back
1.41 ng 2354:
1.112 bowersj2 2355: =head1 CSV Upload/Handling functions
1.38 albertel 2356:
1.41 ng 2357: =over 4
2358:
1.112 bowersj2 2359: =item * upfile_store($r)
1.41 ng 2360:
2361: Store uploaded file, $r should be the HTTP Request object,
2362: needs $ENV{'form.upfile'}
2363: returns $datatoken to be put into hidden field
2364:
2365: =cut
1.31 albertel 2366:
2367: sub upfile_store {
2368: my $r=shift;
2369: $ENV{'form.upfile'}=~s/\r/\n/gs;
2370: $ENV{'form.upfile'}=~s/\f/\n/gs;
2371: $ENV{'form.upfile'}=~s/\n+/\n/gs;
2372: $ENV{'form.upfile'}=~s/\n+$//gs;
2373:
2374: my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
2375: '_enroll_'.$ENV{'request.course.id'}.'_'.time.'_'.$$;
2376: {
2377: my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
2378: '/tmp/'.$datatoken.'.tmp');
2379: print $fh $ENV{'form.upfile'};
2380: }
2381: return $datatoken;
2382: }
2383:
1.56 matthew 2384: =pod
2385:
1.112 bowersj2 2386: =item * load_tmp_file($r)
1.41 ng 2387:
2388: Load uploaded file from tmp, $r should be the HTTP Request object,
2389: needs $ENV{'form.datatoken'},
2390: sets $ENV{'form.upfile'} to the contents of the file
2391:
2392: =cut
1.31 albertel 2393:
2394: sub load_tmp_file {
2395: my $r=shift;
2396: my @studentdata=();
2397: {
2398: my $fh;
2399: if ($fh=Apache::File->new($r->dir_config('lonDaemons').
2400: '/tmp/'.$ENV{'form.datatoken'}.'.tmp')) {
2401: @studentdata=<$fh>;
2402: }
2403: }
2404: $ENV{'form.upfile'}=join('',@studentdata);
2405: }
2406:
1.56 matthew 2407: =pod
2408:
1.112 bowersj2 2409: =item * upfile_record_sep()
1.41 ng 2410:
2411: Separate uploaded file into records
2412: returns array of records,
2413: needs $ENV{'form.upfile'} and $ENV{'form.upfiletype'}
2414:
2415: =cut
1.31 albertel 2416:
2417: sub upfile_record_sep {
2418: if ($ENV{'form.upfiletype'} eq 'xml') {
2419: } else {
2420: return split(/\n/,$ENV{'form.upfile'});
2421: }
2422: }
2423:
1.56 matthew 2424: =pod
2425:
1.112 bowersj2 2426: =item * record_sep($record)
1.41 ng 2427:
2428: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $ENV{'form.upfiletype'}
2429:
2430: =cut
2431:
1.31 albertel 2432: sub record_sep {
2433: my $record=shift;
2434: my %components=();
2435: if ($ENV{'form.upfiletype'} eq 'xml') {
2436: } elsif ($ENV{'form.upfiletype'} eq 'space') {
2437: my $i=0;
2438: foreach (split(/\s+/,$record)) {
2439: my $field=$_;
2440: $field=~s/^(\"|\')//;
2441: $field=~s/(\"|\')$//;
2442: $components{$i}=$field;
2443: $i++;
2444: }
2445: } elsif ($ENV{'form.upfiletype'} eq 'tab') {
2446: my $i=0;
2447: foreach (split(/\t+/,$record)) {
2448: my $field=$_;
2449: $field=~s/^(\"|\')//;
2450: $field=~s/(\"|\')$//;
2451: $components{$i}=$field;
2452: $i++;
2453: }
2454: } else {
2455: my @allfields=split(/\,/,$record);
2456: my $i=0;
2457: my $j;
2458: for ($j=0;$j<=$#allfields;$j++) {
2459: my $field=$allfields[$j];
2460: if ($field=~/^\s*(\"|\')/) {
2461: my $delimiter=$1;
2462: while (($field!~/$delimiter$/) && ($j<$#allfields)) {
2463: $j++;
2464: $field.=','.$allfields[$j];
2465: }
2466: $field=~s/^\s*$delimiter//;
2467: $field=~s/$delimiter\s*$//;
2468: }
2469: $components{$i}=$field;
2470: $i++;
2471: }
2472: }
2473: return %components;
2474: }
2475:
1.144 ! matthew 2476: ######################################################
! 2477: ######################################################
! 2478:
1.56 matthew 2479: =pod
2480:
1.112 bowersj2 2481: =item * upfile_select_html()
1.41 ng 2482:
1.144 ! matthew 2483: Return HTML code to select a file from the users machine and specify
! 2484: the file type.
1.41 ng 2485:
2486: =cut
2487:
1.144 ! matthew 2488: ######################################################
! 2489: ######################################################
1.31 albertel 2490: sub upfile_select_html {
1.144 ! matthew 2491: my %Types = (
! 2492: csv => &mt('CSV (comma separated values, spreadsheet)'),
! 2493: space => &mt('Space separated'),
! 2494: tab => &mt('Tabulator separated'),
! 2495: # xml => &mt('HTML/XML'),
! 2496: );
! 2497: my $Str = '<input type="file" name="upfile" size="50" />'.
! 2498: '<br />Type: <select name="upfiletype">';
! 2499: foreach my $type (sort(keys(%Types))) {
! 2500: $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
! 2501: }
! 2502: $Str .= "</select>\n";
! 2503: return $Str;
1.31 albertel 2504: }
2505:
1.144 ! matthew 2506: ######################################################
! 2507: ######################################################
! 2508:
1.56 matthew 2509: =pod
2510:
1.112 bowersj2 2511: =item * csv_print_samples($r,$records)
1.41 ng 2512:
2513: Prints a table of sample values from each column uploaded $r is an
2514: Apache Request ref, $records is an arrayref from
2515: &Apache::loncommon::upfile_record_sep
2516:
2517: =cut
2518:
1.144 ! matthew 2519: ######################################################
! 2520: ######################################################
1.31 albertel 2521: sub csv_print_samples {
2522: my ($r,$records) = @_;
2523: my (%sone,%stwo,%sthree);
2524: %sone=&record_sep($$records[0]);
2525: if (defined($$records[1])) {%stwo=&record_sep($$records[1]);}
2526: if (defined($$records[2])) {%sthree=&record_sep($$records[2]);}
1.144 ! matthew 2527: #
! 2528: $r->print(&mt('Samples').'<br /><table border="2"><tr>');
! 2529: foreach (sort({$a <=> $b} keys(%sone))) {
! 2530: $r->print('<th>'.&mt('Column [_1]',($_+1)).'</th>'); }
1.31 albertel 2531: $r->print('</tr>');
2532: foreach my $hash (\%sone,\%stwo,\%sthree) {
2533: $r->print('<tr>');
2534: foreach (sort({$a <=> $b} keys(%sone))) {
2535: $r->print('<td>');
2536: if (defined($$hash{$_})) { $r->print($$hash{$_}); }
2537: $r->print('</td>');
2538: }
2539: $r->print('</tr>');
2540: }
2541: $r->print('</tr></table><br />'."\n");
2542: }
2543:
1.144 ! matthew 2544: ######################################################
! 2545: ######################################################
! 2546:
1.56 matthew 2547: =pod
2548:
1.112 bowersj2 2549: =item * csv_print_select_table($r,$records,$d)
1.41 ng 2550:
2551: Prints a table to create associations between values and table columns.
1.144 ! matthew 2552:
1.41 ng 2553: $r is an Apache Request ref,
2554: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
2555: $d is an array of 2 element arrays (internal name, displayed name)
2556:
2557: =cut
2558:
1.144 ! matthew 2559: ######################################################
! 2560: ######################################################
1.31 albertel 2561: sub csv_print_select_table {
2562: my ($r,$records,$d) = @_;
2563: my $i=0;my %sone;
2564: %sone=&record_sep($$records[0]);
1.144 ! matthew 2565: $r->print(&mt('Associate columns with student attributes.')."\n".
! 2566: '<table border="2"><tr>'.
! 2567: '<th>'.&mt('Attribute').'</th>'.
! 2568: '<th>'.&mt('Column').'</th></tr>'."\n");
1.31 albertel 2569: foreach (@$d) {
2570: my ($value,$display)=@{ $_ };
2571: $r->print('<tr><td>'.$display.'</td>');
2572:
2573: $r->print('<td><select name=f'.$i.
1.32 matthew 2574: ' onchange="javascript:flip(this.form,'.$i.');">');
1.31 albertel 2575: $r->print('<option value="none"></option>');
2576: foreach (sort({$a <=> $b} keys(%sone))) {
2577: $r->print('<option value="'.$_.'">Column '.($_+1).'</option>');
2578: }
2579: $r->print('</select></td></tr>'."\n");
2580: $i++;
2581: }
2582: $i--;
2583: return $i;
2584: }
1.56 matthew 2585:
1.144 ! matthew 2586: ######################################################
! 2587: ######################################################
! 2588:
1.56 matthew 2589: =pod
1.31 albertel 2590:
1.112 bowersj2 2591: =item * csv_samples_select_table($r,$records,$d)
1.41 ng 2592:
2593: Prints a table of sample values from the upload and can make associate samples to internal names.
2594:
2595: $r is an Apache Request ref,
2596: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
2597: $d is an array of 2 element arrays (internal name, displayed name)
2598:
2599: =cut
2600:
1.144 ! matthew 2601: ######################################################
! 2602: ######################################################
1.31 albertel 2603: sub csv_samples_select_table {
2604: my ($r,$records,$d) = @_;
2605: my %sone; my %stwo; my %sthree;
2606: my $i=0;
1.144 ! matthew 2607: #
! 2608: $r->print('<table border=2><tr><th>'.
! 2609: &mt('Field').'</th><th>'.&mt('Samples').'</th></tr>');
1.31 albertel 2610: %sone=&record_sep($$records[0]);
2611: if (defined($$records[1])) {%stwo=&record_sep($$records[1]);}
2612: if (defined($$records[2])) {%sthree=&record_sep($$records[2]);}
1.144 ! matthew 2613: #
1.31 albertel 2614: foreach (sort keys %sone) {
1.144 ! matthew 2615: $r->print('<tr><td><select name="f'.$i.'"'.
1.32 matthew 2616: ' onchange="javascript:flip(this.form,'.$i.');">');
1.31 albertel 2617: foreach (@$d) {
2618: my ($value,$display)=@{ $_ };
1.144 ! matthew 2619: $r->print('<option value="'.$value.'">'.$display.'</option>');
1.31 albertel 2620: }
2621: $r->print('</select></td><td>');
2622: if (defined($sone{$_})) { $r->print($sone{$_}."</br>\n"); }
2623: if (defined($stwo{$_})) { $r->print($stwo{$_}."</br>\n"); }
2624: if (defined($sthree{$_})) { $r->print($sthree{$_}."</br>\n"); }
2625: $r->print('</td></tr>');
2626: $i++;
2627: }
2628: $i--;
2629: return($i);
1.115 matthew 2630: }
2631:
1.144 ! matthew 2632: ######################################################
! 2633: ######################################################
! 2634:
1.115 matthew 2635: =pod
2636:
2637: =item clean_excel_name($name)
2638:
2639: Returns a replacement for $name which does not contain any illegal characters.
2640:
2641: =cut
2642:
1.144 ! matthew 2643: ######################################################
! 2644: ######################################################
1.115 matthew 2645: sub clean_excel_name {
2646: my ($name) = @_;
2647: $name =~ s/[:\*\?\/\\]//g;
2648: if (length($name) > 31) {
2649: $name = substr($name,0,31);
2650: }
2651: return $name;
1.25 albertel 2652: }
1.84 albertel 2653:
1.85 albertel 2654: =pod
2655:
1.112 bowersj2 2656: =item * check_if_partid_hidden($id,$symb,$udom,$uname)
1.85 albertel 2657:
2658: Returns either 1 or undef
2659:
2660: 1 if the part is to be hidden, undef if it is to be shown
2661:
2662: Arguments are:
2663:
2664: $id the id of the part to be checked
2665: $symb, optional the symb of the resource to check
2666: $udom, optional the domain of the user to check for
2667: $uname, optional the username of the user to check for
2668:
2669: =cut
1.84 albertel 2670:
2671: sub check_if_partid_hidden {
2672: my ($id,$symb,$udom,$uname) = @_;
1.133 albertel 2673: my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84 albertel 2674: $symb,$udom,$uname);
1.141 albertel 2675: my $truth=1;
2676: #if the string starts with !, then the list is the list to show not hide
2677: if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84 albertel 2678: my @hiddenlist=split(/,/,$hiddenparts);
2679: foreach my $checkid (@hiddenlist) {
1.141 albertel 2680: if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84 albertel 2681: }
1.141 albertel 2682: return !$truth;
1.84 albertel 2683: }
1.127 matthew 2684:
1.138 matthew 2685:
2686: ############################################################
2687: ############################################################
2688:
2689: =pod
2690:
2691: =head1 cgi-bin script and graphing routines
2692:
2693: =item get_cgi_id
2694:
2695: Inputs: none
2696:
2697: Returns an id which can be used to pass environment variables
2698: to various cgi-bin scripts. These environment variables will
2699: be removed from the users environment after a given time by
2700: the routine &Apache::lonnet::transfer_profile_to_env.
2701:
2702: =cut
2703:
2704: ############################################################
2705: ############################################################
2706:
1.136 matthew 2707: sub get_cgi_id {
2708: return (time.'_'.int(rand(1000)));
2709: }
2710:
1.127 matthew 2711: ############################################################
2712: ############################################################
2713:
2714: =pod
2715:
1.134 matthew 2716: =item DrawBarGraph
1.127 matthew 2717:
1.138 matthew 2718: Facilitates the plotting of data in a (stacked) bar graph.
2719: Puts plot definition data into the users environment in order for
2720: graph.png to plot it. Returns an <img> tag for the plot.
2721: The bars on the plot are labeled '1','2',...,'n'.
2722:
2723: Inputs:
2724:
2725: =over 4
2726:
2727: =item $Title: string, the title of the plot
2728:
2729: =item $xlabel: string, text describing the X-axis of the plot
2730:
2731: =item $ylabel: string, text describing the Y-axis of the plot
2732:
2733: =item $Max: scalar, the maximum Y value to use in the plot
2734: If $Max is < any data point, the graph will not be rendered.
2735:
1.140 matthew 2736: =item $colors: array ref holding the colors to be used for the data sets when
1.138 matthew 2737: they are plotted. If undefined, default values will be used.
2738:
2739: =item @Values: An array of array references. Each array reference holds data
2740: to be plotted in a stacked bar chart.
2741:
2742: =back
2743:
2744: Returns:
2745:
2746: An <img> tag which references graph.png and the appropriate identifying
2747: information for the plot.
2748:
1.127 matthew 2749: =cut
2750:
2751: ############################################################
2752: ############################################################
1.134 matthew 2753: sub DrawBarGraph {
1.129 matthew 2754: my ($Title,$xlabel,$ylabel,$Max,$colors,@Values)=@_;
1.134 matthew 2755: #
2756: if (! defined($colors)) {
2757: $colors = ['#33ff00',
2758: '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
2759: '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
2760: ];
2761: }
1.127 matthew 2762: #
1.136 matthew 2763: my $identifier = &get_cgi_id();
2764: my $id = 'cgi.'.$identifier;
1.129 matthew 2765: if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127 matthew 2766: return '';
2767: }
1.129 matthew 2768: my $NumBars = scalar(@{$Values[0]});
2769: my %ValuesHash;
2770: my $NumSets=1;
2771: foreach my $array (@Values) {
2772: next if (! ref($array));
1.136 matthew 2773: $ValuesHash{$id.'.data.'.$NumSets++} =
1.132 matthew 2774: join(',',@$array);
1.129 matthew 2775: }
1.127 matthew 2776: #
1.136 matthew 2777: my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
2778: if ($NumBars < 10) {
2779: $width = 120+$NumBars*15;
2780: $xskip = 1;
2781: $bar_width = 15;
2782: } elsif ($NumBars <= 25) {
2783: $width = 120+$NumBars*11;
2784: $xskip = 5;
2785: $bar_width = 8;
2786: } elsif ($NumBars <= 50) {
2787: $width = 120+$NumBars*8;
2788: $xskip = 5;
2789: $bar_width = 4;
2790: } else {
2791: $width = 120+$NumBars*8;
2792: $xskip = 5;
2793: $bar_width = 4;
2794: }
2795: #
2796: my @Labels;
2797: for (my $i=0;$i<@{$Values[0]};$i++) {
2798: push (@Labels,$i+1);
2799: }
2800: #
1.137 matthew 2801: $Max = 1 if ($Max < 1);
2802: if ( int($Max) < $Max ) {
2803: $Max++;
2804: $Max = int($Max);
2805: }
1.127 matthew 2806: $Title = '' if (! defined($Title));
2807: $xlabel = '' if (! defined($xlabel));
2808: $ylabel = '' if (! defined($ylabel));
1.136 matthew 2809: $ValuesHash{$id.'.title'} = &Apache::lonnet::escape($Title);
2810: $ValuesHash{$id.'.xlabel'} = &Apache::lonnet::escape($xlabel);
2811: $ValuesHash{$id.'.ylabel'} = &Apache::lonnet::escape($ylabel);
1.137 matthew 2812: $ValuesHash{$id.'.y_max_value'} = $Max;
1.136 matthew 2813: $ValuesHash{$id.'.NumBars'} = $NumBars;
2814: $ValuesHash{$id.'.NumSets'} = $NumSets;
2815: $ValuesHash{$id.'.PlotType'} = 'bar';
2816: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
2817: $ValuesHash{$id.'.height'} = $height;
2818: $ValuesHash{$id.'.width'} = $width;
2819: $ValuesHash{$id.'.xskip'} = $xskip;
2820: $ValuesHash{$id.'.bar_width'} = $bar_width;
2821: $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127 matthew 2822: #
1.137 matthew 2823: &Apache::lonnet::appenv(%ValuesHash);
2824: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
2825: }
2826:
2827: ############################################################
2828: ############################################################
2829:
2830: =pod
2831:
2832: =item DrawXYGraph
2833:
1.138 matthew 2834: Facilitates the plotting of data in an XY graph.
2835: Puts plot definition data into the users environment in order for
2836: graph.png to plot it. Returns an <img> tag for the plot.
2837:
2838: Inputs:
2839:
2840: =over 4
2841:
2842: =item $Title: string, the title of the plot
2843:
2844: =item $xlabel: string, text describing the X-axis of the plot
2845:
2846: =item $ylabel: string, text describing the Y-axis of the plot
2847:
2848: =item $Max: scalar, the maximum Y value to use in the plot
2849: If $Max is < any data point, the graph will not be rendered.
2850:
2851: =item $colors: Array ref containing the hex color codes for the data to be
2852: plotted in. If undefined, default values will be used.
2853:
2854: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
2855:
2856: =item $Ydata: Array ref containing Array refs.
2857: Each of the contained arrays will be plotted as a seperate curve.
2858:
2859: =item %Values: hash indicating or overriding any default values which are
2860: passed to graph.png.
2861: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
2862:
2863: =back
2864:
2865: Returns:
2866:
2867: An <img> tag which references graph.png and the appropriate identifying
2868: information for the plot.
2869:
1.137 matthew 2870: =cut
2871:
2872: ############################################################
2873: ############################################################
2874: sub DrawXYGraph {
2875: my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
2876: #
2877: # Create the identifier for the graph
2878: my $identifier = &get_cgi_id();
2879: my $id = 'cgi.'.$identifier;
2880: #
2881: $Title = '' if (! defined($Title));
2882: $xlabel = '' if (! defined($xlabel));
2883: $ylabel = '' if (! defined($ylabel));
2884: my %ValuesHash =
2885: (
2886: $id.'.title' => &Apache::lonnet::escape($Title),
2887: $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
2888: $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
2889: $id.'.y_max_value'=> $Max,
2890: $id.'.labels' => join(',',@$Xlabels),
2891: $id.'.PlotType' => 'XY',
2892: );
2893: #
2894: if (defined($colors) && ref($colors) eq 'ARRAY') {
2895: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
2896: }
2897: #
2898: if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
2899: return '';
2900: }
2901: my $NumSets=1;
1.138 matthew 2902: foreach my $array (@{$Ydata}){
1.137 matthew 2903: next if (! ref($array));
2904: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
2905: }
1.138 matthew 2906: $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137 matthew 2907: #
2908: # Deal with other parameters
2909: while (my ($key,$value) = each(%Values)) {
2910: $ValuesHash{$id.'.'.$key} = $value;
1.127 matthew 2911: }
2912: #
1.136 matthew 2913: &Apache::lonnet::appenv(%ValuesHash);
2914: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
2915: }
2916:
2917: ############################################################
2918: ############################################################
2919:
2920: =pod
2921:
1.138 matthew 2922: =item DrawXYYGraph
2923:
2924: Facilitates the plotting of data in an XY graph with two Y axes.
2925: Puts plot definition data into the users environment in order for
2926: graph.png to plot it. Returns an <img> tag for the plot.
2927:
2928: Inputs:
2929:
2930: =over 4
2931:
2932: =item $Title: string, the title of the plot
2933:
2934: =item $xlabel: string, text describing the X-axis of the plot
2935:
2936: =item $ylabel: string, text describing the Y-axis of the plot
2937:
2938: =item $colors: Array ref containing the hex color codes for the data to be
2939: plotted in. If undefined, default values will be used.
2940:
2941: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
2942:
2943: =item $Ydata1: The first data set
2944:
2945: =item $Min1: The minimum value of the left Y-axis
2946:
2947: =item $Max1: The maximum value of the left Y-axis
2948:
2949: =item $Ydata2: The second data set
2950:
2951: =item $Min2: The minimum value of the right Y-axis
2952:
2953: =item $Max2: The maximum value of the left Y-axis
2954:
2955: =item %Values: hash indicating or overriding any default values which are
2956: passed to graph.png.
2957: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
2958:
2959: =back
2960:
2961: Returns:
2962:
2963: An <img> tag which references graph.png and the appropriate identifying
2964: information for the plot.
1.136 matthew 2965:
2966: =cut
2967:
2968: ############################################################
2969: ############################################################
1.137 matthew 2970: sub DrawXYYGraph {
2971: my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
2972: $Ydata2,$Min2,$Max2,%Values)=@_;
1.136 matthew 2973: #
2974: # Create the identifier for the graph
2975: my $identifier = &get_cgi_id();
2976: my $id = 'cgi.'.$identifier;
2977: #
2978: $Title = '' if (! defined($Title));
2979: $xlabel = '' if (! defined($xlabel));
2980: $ylabel = '' if (! defined($ylabel));
2981: my %ValuesHash =
2982: (
2983: $id.'.title' => &Apache::lonnet::escape($Title),
2984: $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
2985: $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
2986: $id.'.labels' => join(',',@$Xlabels),
2987: $id.'.PlotType' => 'XY',
2988: $id.'.NumSets' => 2,
1.137 matthew 2989: $id.'.two_axes' => 1,
2990: $id.'.y1_max_value' => $Max1,
2991: $id.'.y1_min_value' => $Min1,
2992: $id.'.y2_max_value' => $Max2,
2993: $id.'.y2_min_value' => $Min2,
1.136 matthew 2994: );
2995: #
1.137 matthew 2996: if (defined($colors) && ref($colors) eq 'ARRAY') {
2997: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
2998: }
2999: #
3000: if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
3001: ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136 matthew 3002: return '';
3003: }
3004: my $NumSets=1;
1.137 matthew 3005: foreach my $array ($Ydata1,$Ydata2){
1.136 matthew 3006: next if (! ref($array));
3007: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137 matthew 3008: }
3009: #
3010: # Deal with other parameters
3011: while (my ($key,$value) = each(%Values)) {
3012: $ValuesHash{$id.'.'.$key} = $value;
1.136 matthew 3013: }
3014: #
3015: &Apache::lonnet::appenv(%ValuesHash);
1.130 albertel 3016: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139 matthew 3017: }
3018:
3019: ############################################################
3020: ############################################################
3021:
3022: =pod
3023:
3024: =head1 Statistics helper routines?
3025:
3026: Bad place for them but what the hell.
3027:
3028: =item &chartlink
3029:
3030: Returns a link to the chart for a specific student.
3031:
3032: Inputs:
3033:
3034: =over 4
3035:
3036: =item $linktext: The text of the link
3037:
3038: =item $sname: The students username
3039:
3040: =item $sdomain: The students domain
3041:
3042: =back
3043:
3044: =cut
3045:
3046: ############################################################
3047: ############################################################
3048: sub chartlink {
3049: my ($linktext, $sname, $sdomain) = @_;
3050: my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
3051: '&SelectedStudent='.&Apache::lonnet::escape($sname.':'.$sdomain).
3052: '&chartoutputmode='.HTML::Entities::encode('html, with all links').
3053: '">'.$linktext.'</a>';
1.127 matthew 3054: }
3055:
3056: ############################################################
3057: ############################################################
1.84 albertel 3058:
1.41 ng 3059: =pod
3060:
3061: =back
3062:
1.112 bowersj2 3063: =cut
1.41 ng 3064:
1.112 bowersj2 3065: 1;
3066: __END__;
1.41 ng 3067:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>