Annotation of loncom/interface/loncommon.pm, revision 1.266
1.10 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # a pile of common routines
1.10 albertel 3: #
1.266 ! www 4: # $Id: loncommon.pm,v 1.265 2005/06/04 19:09:17 albertel 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.1 albertel 28:
29: # Makes a table out of the previous attempts
1.2 albertel 30: # Inputs result_from_symbread, user, domain, course_id
1.16 harris41 31: # Reads in non-network-related .tab files
1.1 albertel 32:
1.35 matthew 33: # POD header:
34:
1.45 matthew 35: =pod
36:
1.35 matthew 37: =head1 NAME
38:
39: Apache::loncommon - pile of common routines
40:
41: =head1 SYNOPSIS
42:
1.112 bowersj2 43: Common routines for manipulating connections, student answers,
44: domains, common Javascript fragments, etc.
1.35 matthew 45:
1.112 bowersj2 46: =head1 OVERVIEW
1.35 matthew 47:
1.112 bowersj2 48: A collection of commonly used subroutines that don't have a natural
49: home anywhere else. This collection helps remove
1.35 matthew 50: redundancy from other modules and increase efficiency of memory usage.
51:
52: =cut
53:
54: # End of POD header
1.1 albertel 55: package Apache::loncommon;
56:
57: use strict;
1.258 albertel 58: use Apache::lonnet;
1.46 matthew 59: use GDBM_File;
1.51 www 60: use POSIX qw(strftime mktime);
1.99 www 61: use Apache::Constants qw(:common :http :methods);
1.82 www 62: use Apache::lonmenu();
1.117 www 63: use Apache::lonlocal;
1.139 matthew 64: use HTML::Entities;
1.117 www 65:
1.22 www 66: my $readit;
67:
1.157 matthew 68: ##
69: ## Global Variables
70: ##
1.46 matthew 71:
1.20 www 72: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12 harris41 73: my %language;
1.124 www 74: my %supported_language;
1.12 harris41 75: my %cprtag;
1.192 taceyjo1 76: my %scprtag;
1.12 harris41 77: my %fe; my %fd;
1.41 ng 78: my %category_extensions;
1.12 harris41 79:
1.63 www 80: # ---------------------------------------------- Designs
81:
82: my %designhash;
83:
1.46 matthew 84: # ---------------------------------------------- Thesaurus variables
1.144 matthew 85: #
86: # %Keywords:
87: # A hash used by &keyword to determine if a word is considered a keyword.
88: # $thesaurus_db_file
89: # Scalar containing the full path to the thesaurus database.
1.46 matthew 90:
91: my %Keywords;
92: my $thesaurus_db_file;
93:
1.144 matthew 94: #
95: # Initialize values from language.tab, copyright.tab, filetypes.tab,
96: # thesaurus.tab, and filecategories.tab.
97: #
1.18 www 98: BEGIN {
1.46 matthew 99: # Variable initialization
100: $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
101: #
1.22 www 102: unless ($readit) {
1.12 harris41 103: # ------------------------------------------------------------------- languages
104: {
1.158 raeburn 105: my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
106: '/language.tab';
107: if ( open(my $fh,"<$langtabfile") ) {
108: while (<$fh>) {
109: next if /^\#/;
110: chomp;
111: my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$_));
112: $language{$key}=$val.' - '.$enc;
113: if ($sup) {
114: $supported_language{$key}=$sup;
115: }
116: }
117: close($fh);
118: }
1.12 harris41 119: }
120: # ------------------------------------------------------------------ copyrights
121: {
1.158 raeburn 122: my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
123: '/copyright.tab';
124: if ( open (my $fh,"<$copyrightfile") ) {
125: while (<$fh>) {
126: next if /^\#/;
127: chomp;
128: my ($key,$val)=(split(/\s+/,$_,2));
129: $cprtag{$key}=$val;
130: }
131: close($fh);
132: }
1.12 harris41 133: }
1.192 taceyjo1 134: # ------------------------------------------------------------------ source copyrights
135: {
136: my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
137: '/source_copyright.tab';
138: if ( open (my $fh,"<$sourcecopyrightfile") ) {
139: while (<$fh>) {
140: next if /^\#/;
141: chomp;
142: my ($key,$val)=(split(/\s+/,$_,2));
143: $scprtag{$key}=$val;
144: }
145: close($fh);
146: }
147: }
1.63 www 148:
149: # -------------------------------------------------------------- domain designs
150:
151: my $filename;
152: my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
153: opendir(DIR,$designdir);
154: while ($filename=readdir(DIR)) {
155: my ($domain)=($filename=~/^(\w+)\./);
156: {
1.158 raeburn 157: my $designfile = $designdir.'/'.$filename;
158: if ( open (my $fh,"<$designfile") ) {
159: while (<$fh>) {
160: next if /^\#/;
161: chomp;
162: my ($key,$val)=(split(/\=/,$_));
163: if ($val) { $designhash{$domain.'.'.$key}=$val; }
164: }
165: close($fh);
166: }
1.63 www 167: }
168:
169: }
170: closedir(DIR);
171:
172:
1.15 harris41 173: # ------------------------------------------------------------- file categories
174: {
1.158 raeburn 175: my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
176: '/filecategories.tab';
177: if ( open (my $fh,"<$categoryfile") ) {
178: while (<$fh>) {
179: next if /^\#/;
180: chomp;
181: my ($extension,$category)=(split(/\s+/,$_,2));
182: push @{$category_extensions{lc($category)}},$extension;
183: }
184: close($fh);
185: }
186:
1.15 harris41 187: }
1.12 harris41 188: # ------------------------------------------------------------------ file types
189: {
1.158 raeburn 190: my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
191: '/filetypes.tab';
192: if ( open (my $fh,"<$typesfile") ) {
1.16 harris41 193: while (<$fh>) {
1.158 raeburn 194: next if (/^\#/);
195: chomp;
196: my ($ending,$emb,$descr)=split(/\s+/,$_,3);
197: if ($descr ne '') {
198: $fe{$ending}=lc($emb);
199: $fd{$ending}=$descr;
200: }
201: }
202: close($fh);
203: }
1.12 harris41 204: }
1.22 www 205: &Apache::lonnet::logthis(
1.46 matthew 206: "<font color=yellow>INFO: Read file types</font>");
1.22 www 207: $readit=1;
1.46 matthew 208: } # end of unless($readit)
1.32 matthew 209:
210: }
1.112 bowersj2 211:
1.42 matthew 212: ###############################################################
213: ## HTML and Javascript Helper Functions ##
214: ###############################################################
215:
216: =pod
217:
1.112 bowersj2 218: =head1 HTML and Javascript Functions
1.42 matthew 219:
1.112 bowersj2 220: =over 4
221:
222: =item * browser_and_searcher_javascript ()
223:
224: X<browsing, javascript>X<searching, javascript>Returns a string
225: containing javascript with two functions, C<openbrowser> and
226: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
227: tags.
1.42 matthew 228:
1.112 bowersj2 229: =item * openbrowser(formname,elementname,only,omit) [javascript]
1.42 matthew 230:
231: inputs: formname, elementname, only, omit
232:
233: formname and elementname indicate the name of the html form and name of
234: the element that the results of the browsing selection are to be placed in.
235:
236: Specifying 'only' will restrict the browser to displaying only files
1.185 www 237: with the given extension. Can be a comma separated list.
1.42 matthew 238:
239: Specifying 'omit' will restrict the browser to NOT displaying files
1.185 www 240: with the given extension. Can be a comma separated list.
1.42 matthew 241:
1.112 bowersj2 242: =item * opensearcher(formname, elementname) [javascript]
1.42 matthew 243:
244: Inputs: formname, elementname
245:
246: formname and elementname specify the name of the html form and the name
247: of the element the selection from the search results will be placed in.
248:
249: =cut
250:
251: sub browser_and_searcher_javascript {
1.199 albertel 252: my ($mode)=@_;
253: if (!defined($mode)) { $mode='edit'; }
1.170 www 254: my $resurl=&lastresurl();
1.42 matthew 255: return <<END;
1.219 albertel 256: // <!-- BEGIN LON-CAPA Internal
1.50 matthew 257: var editbrowser = null;
1.135 albertel 258: function openbrowser(formname,elementname,only,omit,titleelement) {
1.170 www 259: var url = '$resurl/?';
1.42 matthew 260: if (editbrowser == null) {
261: url += 'launch=1&';
262: }
263: url += 'catalogmode=interactive&';
1.199 albertel 264: url += 'mode=$mode&';
1.42 matthew 265: url += 'form=' + formname + '&';
266: if (only != null) {
267: url += 'only=' + only + '&';
1.217 albertel 268: } else {
269: url += 'only=&';
270: }
1.42 matthew 271: if (omit != null) {
272: url += 'omit=' + omit + '&';
1.217 albertel 273: } else {
274: url += 'omit=&';
275: }
1.135 albertel 276: if (titleelement != null) {
277: url += 'titleelement=' + titleelement + '&';
1.217 albertel 278: } else {
279: url += 'titleelement=&';
280: }
1.42 matthew 281: url += 'element=' + elementname + '';
282: var title = 'Browser';
1.217 albertel 283: var options = 'scrollbars=1,resizable=1,menubar=1,location=1';
1.42 matthew 284: options += ',width=700,height=600';
285: editbrowser = open(url,title,options,'1');
286: editbrowser.focus();
287: }
288: var editsearcher;
1.135 albertel 289: function opensearcher(formname,elementname,titleelement) {
1.42 matthew 290: var url = '/adm/searchcat?';
291: if (editsearcher == null) {
292: url += 'launch=1&';
293: }
294: url += 'catalogmode=interactive&';
1.199 albertel 295: url += 'mode=$mode&';
1.42 matthew 296: url += 'form=' + formname + '&';
1.135 albertel 297: if (titleelement != null) {
298: url += 'titleelement=' + titleelement + '&';
1.217 albertel 299: } else {
300: url += 'titleelement=&';
301: }
1.42 matthew 302: url += 'element=' + elementname + '';
303: var title = 'Search';
304: var options = 'scrollbars=1,resizable=1,menubar=0';
305: options += ',width=700,height=600';
306: editsearcher = open(url,title,options,'1');
307: editsearcher.focus();
308: }
1.219 albertel 309: // END LON-CAPA Internal -->
1.42 matthew 310: END
1.170 www 311: }
312:
313: sub lastresurl {
1.258 albertel 314: if ($env{'environment.lastresurl'}) {
315: return $env{'environment.lastresurl'}
1.170 www 316: } else {
317: return '/res';
318: }
319: }
320:
321: sub storeresurl {
322: my $resurl=&Apache::lonnet::clutter(shift);
323: unless ($resurl=~/^\/res/) { return 0; }
324: $resurl=~s/\/$//;
325: &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
326: &Apache::lonnet::appenv('environment.lastresurl' => $resurl);
327: return 1;
1.42 matthew 328: }
329:
1.74 www 330: sub studentbrowser_javascript {
1.111 www 331: unless (
1.258 albertel 332: (($env{'request.course.id'}) &&
333: (&Apache::lonnet::allowed('srm',$env{'request.course.id'})))
334: || ($env{'request.role'}=~/^(au|dc|su)/)
1.111 www 335: ) { return ''; }
1.74 www 336: return (<<'ENDSTDBRW');
337: <script type="text/javascript" language="Javascript" >
338: var stdeditbrowser;
1.111 www 339: function openstdbrowser(formname,uname,udom,roleflag) {
1.74 www 340: var url = '/adm/pickstudent?';
341: var filter;
342: eval('filter=document.'+formname+'.'+uname+'.value;');
343: if (filter != null) {
344: if (filter != '') {
345: url += 'filter='+filter+'&';
346: }
347: }
348: url += 'form=' + formname + '&unameelement='+uname+
349: '&udomelement='+udom;
1.111 www 350: if (roleflag) { url+="&roles=1"; }
1.102 www 351: var title = 'Student_Browser';
1.74 www 352: var options = 'scrollbars=1,resizable=1,menubar=0';
353: options += ',width=700,height=600';
354: stdeditbrowser = open(url,title,options,'1');
355: stdeditbrowser.focus();
356: }
357: </script>
358: ENDSTDBRW
359: }
1.42 matthew 360:
1.74 www 361: sub selectstudent_link {
1.111 www 362: my ($form,$unameele,$udomele)=@_;
1.258 albertel 363: if ($env{'request.course.id'}) {
364: unless (&Apache::lonnet::allowed('srm',$env{'request.course.id'})) {
1.111 www 365: return '';
366: }
367: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 368: '","'.$udomele.'");'."'>".&mt('Select User')."</a>";
1.74 www 369: }
1.258 albertel 370: if ($env{'request.role'}=~/^(au|dc|su)/) {
1.111 www 371: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 372: '","'.$udomele.'",1);'."'>".&mt('Select User')."</a>";
1.111 www 373: }
374: return '';
1.91 www 375: }
376:
377: sub coursebrowser_javascript {
1.234 raeburn 378: my ($domainfilter)=@_;
1.128 albertel 379: return (<<ENDSTDBRW);
1.91 www 380: <script type="text/javascript" language="Javascript" >
381: var stdeditbrowser;
1.234 raeburn 382: function opencrsbrowser(formname,uname,udom,desc,extra_element) {
1.91 www 383: var url = '/adm/pickcourse?';
384: var filter;
385: if (filter != null) {
386: if (filter != '') {
387: url += 'filter='+filter+'&';
388: }
389: }
1.128 albertel 390: var domainfilter='$domainfilter';
391: if (domainfilter != null) {
392: if (domainfilter != '') {
393: url += 'domainfilter='+domainfilter+'&';
394: }
395: }
1.91 www 396: url += 'form=' + formname + '&cnumelement='+uname+
1.187 albertel 397: '&cdomelement='+udom+
398: '&cnameelement='+desc;
1.234 raeburn 399: if (extra_element !=null && extra_element != '' && formname == 'rolechoice') {
400: url += '&roleelement='+extra_element;
401: if (domainfilter == null || domainfilter == '') {
402: url += '&domainfilter='+extra_element;
403: }
1.230 raeburn 404: }
1.102 www 405: var title = 'Course_Browser';
1.91 www 406: var options = 'scrollbars=1,resizable=1,menubar=0';
407: options += ',width=700,height=600';
408: stdeditbrowser = open(url,title,options,'1');
409: stdeditbrowser.focus();
410: }
411: </script>
412: ENDSTDBRW
413: }
414:
415: sub selectcourse_link {
1.234 raeburn 416: my ($form,$unameele,$udomele,$desc,$extra_element)=@_;
1.91 www 417: return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
1.234 raeburn 418: '","'.$udomele.'","'.$desc.'","'.$extra_element.'");'."'>".&mt('Select Course')."</a>";
1.74 www 419: }
1.42 matthew 420:
421: =pod
1.36 matthew 422:
1.112 bowersj2 423: =item * linked_select_forms(...)
1.36 matthew 424:
425: linked_select_forms returns a string containing a <script></script> block
426: and html for two <select> menus. The select menus will be linked in that
427: changing the value of the first menu will result in new values being placed
428: in the second menu. The values in the select menu will appear in alphabetical
429: order.
430:
431: linked_select_forms takes the following ordered inputs:
432:
433: =over 4
434:
1.112 bowersj2 435: =item * $formname, the name of the <form> tag
1.36 matthew 436:
1.112 bowersj2 437: =item * $middletext, the text which appears between the <select> tags
1.36 matthew 438:
1.112 bowersj2 439: =item * $firstdefault, the default value for the first menu
1.36 matthew 440:
1.112 bowersj2 441: =item * $firstselectname, the name of the first <select> tag
1.36 matthew 442:
1.112 bowersj2 443: =item * $secondselectname, the name of the second <select> tag
1.36 matthew 444:
1.112 bowersj2 445: =item * $hashref, a reference to a hash containing the data for the menus.
1.36 matthew 446:
1.41 ng 447: =back
448:
1.36 matthew 449: Below is an example of such a hash. Only the 'text', 'default', and
450: 'select2' keys must appear as stated. keys(%menu) are the possible
451: values for the first select menu. The text that coincides with the
1.41 ng 452: first menu value is given in $menu{$choice1}->{'text'}. The values
1.36 matthew 453: and text for the second menu are given in the hash pointed to by
454: $menu{$choice1}->{'select2'}.
455:
1.112 bowersj2 456: my %menu = ( A1 => { text =>"Choice A1" ,
457: default => "B3",
458: select2 => {
459: B1 => "Choice B1",
460: B2 => "Choice B2",
461: B3 => "Choice B3",
462: B4 => "Choice B4"
463: }
464: },
465: A2 => { text =>"Choice A2" ,
466: default => "C2",
467: select2 => {
468: C1 => "Choice C1",
469: C2 => "Choice C2",
470: C3 => "Choice C3"
471: }
472: },
473: A3 => { text =>"Choice A3" ,
474: default => "D6",
475: select2 => {
476: D1 => "Choice D1",
477: D2 => "Choice D2",
478: D3 => "Choice D3",
479: D4 => "Choice D4",
480: D5 => "Choice D5",
481: D6 => "Choice D6",
482: D7 => "Choice D7"
483: }
484: }
485: );
1.36 matthew 486:
487: =cut
488:
489: sub linked_select_forms {
490: my ($formname,
491: $middletext,
492: $firstdefault,
493: $firstselectname,
494: $secondselectname,
495: $hashref
496: ) = @_;
497: my $second = "document.$formname.$secondselectname";
498: my $first = "document.$formname.$firstselectname";
499: # output the javascript to do the changing
500: my $result = '';
1.219 albertel 501: $result.="<script type=\"text/javascript\">\n";
1.36 matthew 502: $result.="var select2data = new Object();\n";
503: $" = '","';
504: my $debug = '';
505: foreach my $s1 (sort(keys(%$hashref))) {
506: $result.="select2data.d_$s1 = new Object();\n";
507: $result.="select2data.d_$s1.def = new String('".
508: $hashref->{$s1}->{'default'}."');\n";
509: $result.="select2data.d_$s1.values = new Array(";
510: my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
511: $result.="\"@s2values\");\n";
512: $result.="select2data.d_$s1.texts = new Array(";
513: my @s2texts;
514: foreach my $value (@s2values) {
515: push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
516: }
517: $result.="\"@s2texts\");\n";
518: }
519: $"=' ';
520: $result.= <<"END";
521:
522: function select1_changed() {
523: // Determine new choice
524: var newvalue = "d_" + $first.value;
525: // update select2
526: var values = select2data[newvalue].values;
527: var texts = select2data[newvalue].texts;
528: var select2def = select2data[newvalue].def;
529: var i;
530: // out with the old
531: for (i = 0; i < $second.options.length; i++) {
532: $second.options[i] = null;
533: }
534: // in with the nuclear
535: for (i=0;i<values.length; i++) {
536: $second.options[i] = new Option(values[i]);
1.143 matthew 537: $second.options[i].value = values[i];
1.36 matthew 538: $second.options[i].text = texts[i];
539: if (values[i] == select2def) {
540: $second.options[i].selected = true;
541: }
542: }
543: }
544: </script>
545: END
546: # output the initial values for the selection lists
547: $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
548: foreach my $value (sort(keys(%$hashref))) {
549: $result.=" <option value=\"$value\" ";
1.253 albertel 550: $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119 www 551: $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36 matthew 552: }
553: $result .= "</select>\n";
554: my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
555: $result .= $middletext;
556: $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
557: my $seconddefault = $hashref->{$firstdefault}->{'default'};
558: foreach my $value (sort(keys(%select2))) {
559: $result.=" <option value=\"$value\" ";
1.253 albertel 560: $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119 www 561: $result.=">".&mt($select2{$value})."</option>\n";
1.36 matthew 562: }
563: $result .= "</select>\n";
564: # return $debug;
565: return $result;
566: } # end of sub linked_select_forms {
567:
1.45 matthew 568: =pod
1.44 bowersj2 569:
1.112 bowersj2 570: =item * help_open_topic($topic, $text, $stayOnPage, $width, $height)
1.44 bowersj2 571:
1.112 bowersj2 572: Returns a string corresponding to an HTML link to the given help
573: $topic, where $topic corresponds to the name of a .tex file in
574: /home/httpd/html/adm/help/tex, with underscores replaced by
575: spaces.
576:
577: $text will optionally be linked to the same topic, allowing you to
578: link text in addition to the graphic. If you do not want to link
579: text, but wish to specify one of the later parameters, pass an
580: empty string.
581:
582: $stayOnPage is a value that will be interpreted as a boolean. If true,
583: the link will not open a new window. If false, the link will open
584: a new window using Javascript. (Default is false.)
585:
586: $width and $height are optional numerical parameters that will
587: override the width and height of the popped up window, which may
588: be useful for certain help topics with big pictures included.
1.44 bowersj2 589:
590: =cut
591:
592: sub help_open_topic {
1.48 bowersj2 593: my ($topic, $text, $stayOnPage, $width, $height) = @_;
594: $text = "" if (not defined $text);
1.44 bowersj2 595: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 596: if ($env{'browser.interface'} eq 'textual' ||
597: $env{'environment.remote'} eq 'off' ) {
1.79 www 598: $stayOnPage=1;
599: }
1.44 bowersj2 600: $width = 350 if (not defined $width);
601: $height = 400 if (not defined $height);
602: my $filename = $topic;
603: $filename =~ s/ /_/g;
604:
1.48 bowersj2 605: my $template = "";
606: my $link;
1.159 www 607:
608: $topic=~s/\W/\_/g;
1.44 bowersj2 609:
610: if (!$stayOnPage)
611: {
1.72 bowersj2 612: $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 613: }
614: else
615: {
1.48 bowersj2 616: $link = "/adm/help/${filename}.hlp";
617: }
618:
619: # Add the text
620: if ($text ne "")
621: {
1.77 www 622: $template .=
623: "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
1.78 www 624: "<td bgcolor='#5555FF'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48 bowersj2 625: }
626:
627: # Add the graphic
1.179 matthew 628: my $title = &mt('Online Help');
1.215 albertel 629: my $helpicon=&lonhttpdurl("/adm/help/gif/smallHelp.gif");
1.48 bowersj2 630: $template .= <<"ENDTEMPLATE";
1.218 albertel 631: <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help: $topic)" /></a>
1.44 bowersj2 632: ENDTEMPLATE
1.78 www 633: if ($text ne '') { $template.='</td></tr></table>' };
1.44 bowersj2 634: return $template;
635:
1.106 bowersj2 636: }
637:
638: # This is a quicky function for Latex cheatsheet editing, since it
639: # appears in at least four places
640: sub helpLatexCheatsheet {
641: my $other = shift;
642: my $addOther = '';
643: if ($other) {
644: $addOther = Apache::loncommon::help_open_topic($other, shift,
645: undef, undef, 600) .
646: '</td><td>';
647: }
648: return '<table><tr><td>'.
649: $addOther .
650: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
651: undef,undef,600)
652: .'</td><td>'.
653: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
654: undef,undef,600)
655: .'</td></tr></table>';
1.172 www 656: }
657:
1.193 raeburn 658: sub help_open_menu {
659: my ($color,$topic,$component_help,$function,$faq,$bug,$stayOnPage,$width,$height,$text) = @_;
660: $text = "" if (not defined $text);
661: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 662: if ($env{'browser.interface'} eq 'textual' ||
663: $env{'environment.remote'} eq 'off' ) {
1.193 raeburn 664: $stayOnPage=1;
665: }
666: $width = 620 if (not defined $width);
667: $height = 600 if (not defined $height);
668: my $link='';
1.201 raeburn 669: my $title = &mt('Get help');
1.193 raeburn 670: my $origurl = $ENV{'REQUEST_URI'};
1.227 albertel 671: $origurl=~s|^/~|/priv/|;
1.193 raeburn 672: my $timestamp = time;
673: foreach (\$color,\$function,\$topic,\$component_help,\$faq,\$bug,\$origurl) {
674: $$_ = &Apache::lonnet::escape($$_);
675: }
1.195 albertel 676: if (!$stayOnPage) {
1.193 raeburn 677: $link = "javascript:helpMenu('open')";
1.195 albertel 678: } else {
1.193 raeburn 679: $link = "javascript:helpMenu('display')";
680: }
1.198 raeburn 681: my $banner_link = "/adm/helpmenu?page=banner&color=$color&function=$function&topic=$topic&component_help=$component_help&faq=$faq&bug=$bug&origurl=$origurl&stamp=$timestamp&stayonpage=$stayOnPage";
1.193 raeburn 682: my $details_link = "/adm/helpmenu?page=body&color=$color&function=$function&topic=$topic&component_help=$component_help&faq=$faq&bug=$bug&origurl=$origurl&stamp=$timestamp";
1.196 albertel 683: my $template;
684: if ($text ne "") {
685: $template .=
1.265 albertel 686: "<table bgcolor='#CC3300' cellspacing='1' cellpadding='1' border='0'><tr>".
687: "<td bgcolor='#CC6600'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.196 albertel 688: }
1.261 albertel 689: my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.250 albertel 690: my $html=&Apache::lonxml::xmlbegin();
1.215 albertel 691: my $helpicon=&lonhttpdurl("/adm/lonIcons/helpgateway.gif");
1.196 albertel 692: $template .= <<"ENDTEMPLATE";
1.219 albertel 693: <script type="text/javascript">
1.253 albertel 694: // <!-- BEGIN LON-CAPA Internal
695: // <![CDATA[
1.243 raeburn 696: function helpMenu(target) {
697: var caller = this;
698: if (target == 'open') {
699: var newWindow = null;
700: try {
1.262 albertel 701: newWindow = window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243 raeburn 702: }
703: catch(error) {
704: writeHelp(caller);
705: return;
706: }
707: if (newWindow) {
708: caller = newWindow;
709: }
1.193 raeburn 710: }
1.243 raeburn 711: writeHelp(caller);
712: return;
713: }
714: function writeHelp(caller) {
1.264 www 715: caller.document.writeln('$html<head><title>LON-CAPA Help Menu</title><meta http-equiv="pragma" content="no-cache"></head>')
716: caller.document.writeln("<frameset rows='105,*' border='0'><frame name='bannerframe' src='$banner_link'><frame name='bodyframe' src='$details_link'></frameset>")
717: caller.document.writeln("</html>")
1.243 raeburn 718: caller.document.close()
719: caller.focus()
1.193 raeburn 720: }
1.253 albertel 721: // ]]>
1.219 albertel 722: // END LON-CAPA Internal -->
1.193 raeburn 723: </script>
1.218 albertel 724: <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help Menu)" /></a>
1.193 raeburn 725: ENDTEMPLATE
1.203 albertel 726: if ($component_help) {
727: if (!$text) {
728: $template=&help_open_topic($component_help,undef,$stayOnPage,
729: $width,$height).' '.$template;
730: } else {
731: my $help_text;
732: $help_text=&Apache::lonnet::unescape($topic);
733: $template='<table><tr><td>'.
734: &help_open_topic($component_help,$help_text,$stayOnPage,
735: $width,$height).'</td><td>'.$template.
736: '</td></tr></table>';
737: }
738: }
1.196 albertel 739: if ($text ne '') { $template.='</td></tr></table>' };
1.193 raeburn 740: return $template;
741: }
742:
1.172 www 743: sub help_open_bug {
744: my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258 albertel 745: unless ($env{'user.adv'}) { return ''; }
1.172 www 746: unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
747: $text = "" if (not defined $text);
748: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 749: if ($env{'browser.interface'} eq 'textual' ||
750: $env{'environment.remote'} eq 'off' ) {
1.172 www 751: $stayOnPage=1;
752: }
1.184 albertel 753: $width = 600 if (not defined $width);
754: $height = 600 if (not defined $height);
1.172 www 755:
756: $topic=~s/\W+/\+/g;
757: my $link='';
758: my $template='';
759: my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&bug_file_loc='.
760: &Apache::lonnet::escape($ENV{'REQUEST_URI'}).'&component='.$topic;
761: if (!$stayOnPage)
762: {
763: $link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
764: }
765: else
766: {
767: $link = $url;
768: }
769: # Add the text
770: if ($text ne "")
771: {
772: $template .=
773: "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
774: "<td bgcolor='#FF5555'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
775: }
776:
777: # Add the graphic
1.179 matthew 778: my $title = &mt('Report a Bug');
1.215 albertel 779: my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172 www 780: $template .= <<"ENDTEMPLATE";
1.218 albertel 781: <a href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172 www 782: ENDTEMPLATE
783: if ($text ne '') { $template.='</td></tr></table>' };
784: return $template;
785:
786: }
787:
788: sub help_open_faq {
789: my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258 albertel 790: unless ($env{'user.adv'}) { return ''; }
1.172 www 791: unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
792: $text = "" if (not defined $text);
793: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 794: if ($env{'browser.interface'} eq 'textual' ||
795: $env{'environment.remote'} eq 'off' ) {
1.172 www 796: $stayOnPage=1;
797: }
798: $width = 350 if (not defined $width);
799: $height = 400 if (not defined $height);
800:
801: $topic=~s/\W+/\+/g;
802: my $link='';
803: my $template='';
804: my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
805: if (!$stayOnPage)
806: {
807: $link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
808: }
809: else
810: {
811: $link = $url;
812: }
813:
814: # Add the text
815: if ($text ne "")
816: {
817: $template .=
1.173 www 818: "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
819: "<td bgcolor='#448844'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172 www 820: }
821:
822: # Add the graphic
1.179 matthew 823: my $title = &mt('View the FAQ');
1.215 albertel 824: my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172 www 825: $template .= <<"ENDTEMPLATE";
1.218 albertel 826: <a href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172 www 827: ENDTEMPLATE
828: if ($text ne '') { $template.='</td></tr></table>' };
829: return $template;
830:
1.44 bowersj2 831: }
1.37 matthew 832:
1.180 matthew 833: ###############################################################
834: ###############################################################
835:
1.45 matthew 836: =pod
837:
1.256 matthew 838: =item * change_content_javascript():
839:
840: This and the next function allow you to create small sections of an
841: otherwise static HTML page that you can update on the fly with
842: Javascript, even in Netscape 4.
843:
844: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
845: must be written to the HTML page once. It will prove the Javascript
846: function "change(name, content)". Calling the change function with the
847: name of the section
848: you want to update, matching the name passed to C<changable_area>, and
849: the new content you want to put in there, will put the content into
850: that area.
851:
852: B<Note>: Netscape 4 only reserves enough space for the changable area
853: to contain room for the original contents. You need to "make space"
854: for whatever changes you wish to make, and be B<sure> to check your
855: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
856: it's adequate for updating a one-line status display, but little more.
857: This script will set the space to 100% width, so you only need to
858: worry about height in Netscape 4.
859:
860: Modern browsers are much less limiting, and if you can commit to the
861: user not using Netscape 4, this feature may be used freely with
862: pretty much any HTML.
863:
864: =cut
865:
866: sub change_content_javascript {
867: # If we're on Netscape 4, we need to use Layer-based code
1.258 albertel 868: if ($env{'browser.type'} eq 'netscape' &&
869: $env{'browser.version'} =~ /^4\./) {
1.256 matthew 870: return (<<NETSCAPE4);
871: function change(name, content) {
872: doc = document.layers[name+"___escape"].layers[0].document;
873: doc.open();
874: doc.write(content);
875: doc.close();
876: }
877: NETSCAPE4
878: } else {
879: # Otherwise, we need to use semi-standards-compliant code
880: # (technically, "innerHTML" isn't standard but the equivalent
881: # is really scary, and every useful browser supports it
882: return (<<DOMBASED);
883: function change(name, content) {
884: element = document.getElementById(name);
885: element.innerHTML = content;
886: }
887: DOMBASED
888: }
889: }
890:
891: =pod
892:
893: =item * changable_area($name, $origContent):
894:
895: This provides a "changable area" that can be modified on the fly via
896: the Javascript code provided in C<change_content_javascript>. $name is
897: the name you will use to reference the area later; do not repeat the
898: same name on a given HTML page more then once. $origContent is what
899: the area will originally contain, which can be left blank.
900:
901: =cut
902:
903: sub changable_area {
904: my ($name, $origContent) = @_;
905:
1.258 albertel 906: if ($env{'browser.type'} eq 'netscape' &&
907: $env{'browser.version'} =~ /^4\./) {
1.256 matthew 908: # If this is netscape 4, we need to use the Layer tag
909: return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
910: } else {
911: return "<span id='$name'>$origContent</span>";
912: }
913: }
914:
915: =pod
916:
917: =back
918:
919: =head1 Excel and CSV file utility routines
920:
921: =over 4
922:
923: =cut
924:
925: ###############################################################
926: ###############################################################
927:
928: =pod
929:
1.112 bowersj2 930: =item * csv_translate($text)
1.37 matthew 931:
1.185 www 932: Translate $text to allow it to be output as a 'comma separated values'
1.37 matthew 933: format.
934:
935: =cut
936:
1.180 matthew 937: ###############################################################
938: ###############################################################
1.37 matthew 939: sub csv_translate {
940: my $text = shift;
941: $text =~ s/\"/\"\"/g;
1.209 albertel 942: $text =~ s/\n/ /g;
1.37 matthew 943: return $text;
944: }
1.180 matthew 945:
946: ###############################################################
947: ###############################################################
948:
949: =pod
950:
951: =item * define_excel_formats
952:
953: Define some commonly used Excel cell formats.
954:
955: Currently supported formats:
956:
957: =over 4
958:
959: =item header
960:
961: =item bold
962:
963: =item h1
964:
965: =item h2
966:
967: =item h3
968:
1.256 matthew 969: =item h4
970:
971: =item i
972:
1.180 matthew 973: =item date
974:
975: =back
976:
977: Inputs: $workbook
978:
979: Returns: $format, a hash reference.
980:
981: =cut
982:
983: ###############################################################
984: ###############################################################
985: sub define_excel_formats {
986: my ($workbook) = @_;
987: my $format;
988: $format->{'header'} = $workbook->add_format(bold => 1,
989: bottom => 1,
990: align => 'center');
991: $format->{'bold'} = $workbook->add_format(bold=>1);
992: $format->{'h1'} = $workbook->add_format(bold=>1, size=>18);
993: $format->{'h2'} = $workbook->add_format(bold=>1, size=>16);
994: $format->{'h3'} = $workbook->add_format(bold=>1, size=>14);
1.255 matthew 995: $format->{'h4'} = $workbook->add_format(bold=>1, size=>12);
1.246 matthew 996: $format->{'i'} = $workbook->add_format(italic=>1);
1.180 matthew 997: $format->{'date'} = $workbook->add_format(num_format=>
1.207 matthew 998: 'mm/dd/yyyy hh:mm:ss');
1.180 matthew 999: return $format;
1000: }
1001:
1002: ###############################################################
1003: ###############################################################
1.113 bowersj2 1004:
1005: =pod
1006:
1.256 matthew 1007: =item * create_workbook
1.255 matthew 1008:
1009: Create an Excel worksheet. If it fails, output message on the
1010: request object and return undefs.
1011:
1012: Inputs: Apache request object
1013:
1014: Returns (undef) on failure,
1015: Excel worksheet object, scalar with filename, and formats
1016: from &Apache::loncommon::define_excel_formats on success
1017:
1018: =cut
1019:
1020: ###############################################################
1021: ###############################################################
1022: sub create_workbook {
1023: my ($r) = @_;
1024: #
1025: # Create the excel spreadsheet
1026: my $filename = '/prtspool/'.
1.258 albertel 1027: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255 matthew 1028: time.'_'.rand(1000000000).'.xls';
1029: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
1030: if (! defined($workbook)) {
1031: $r->log_error("Error creating excel spreadsheet $filename: $!");
1032: $r->print('<p>'.&mt("Unable to create new Excel file. ".
1033: "This error has been logged. ".
1034: "Please alert your LON-CAPA administrator").
1035: '</p>');
1036: return (undef);
1037: }
1038: #
1039: $workbook->set_tempdir('/home/httpd/perl/tmp');
1040: #
1041: my $format = &Apache::loncommon::define_excel_formats($workbook);
1042: return ($workbook,$filename,$format);
1043: }
1044:
1045: ###############################################################
1046: ###############################################################
1047:
1048: =pod
1049:
1.256 matthew 1050: =item * create_text_file
1.113 bowersj2 1051:
1.256 matthew 1052: Create a file to write to and eventually make available to the usre.
1053: If file creation fails, outputs an error message on the request object and
1054: return undefs.
1.113 bowersj2 1055:
1.256 matthew 1056: Inputs: Apache request object, and file suffix
1.113 bowersj2 1057:
1.256 matthew 1058: Returns (undef) on failure,
1059: Filehandle and filename on success.
1.113 bowersj2 1060:
1061: =cut
1062:
1.256 matthew 1063: ###############################################################
1064: ###############################################################
1065: sub create_text_file {
1066: my ($r,$suffix) = @_;
1067: if (! defined($suffix)) { $suffix = 'txt'; };
1068: my $fh;
1069: my $filename = '/prtspool/'.
1.258 albertel 1070: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256 matthew 1071: time.'_'.rand(1000000000).'.'.$suffix;
1072: $fh = Apache::File->new('>/home/httpd'.$filename);
1073: if (! defined($fh)) {
1074: $r->log_error("Couldn't open $filename for output $!");
1075: $r->print("Problems occured in creating the output file. ".
1076: "This error has been logged. ".
1077: "Please alert your LON-CAPA administrator.");
1.113 bowersj2 1078: }
1.256 matthew 1079: return ($fh,$filename)
1.113 bowersj2 1080: }
1081:
1082:
1.256 matthew 1083: =pod
1.113 bowersj2 1084:
1085: =back
1086:
1087: =cut
1.37 matthew 1088:
1089: ###############################################################
1.33 matthew 1090: ## Home server <option> list generating code ##
1091: ###############################################################
1.35 matthew 1092:
1.45 matthew 1093: =pod
1094:
1.112 bowersj2 1095: =head1 Home Server option list generating code
1096:
1097: =over 4
1098:
1099: =item * get_domains()
1.35 matthew 1100:
1101: Returns an array containing each of the domains listed in the hosts.tab
1102: file.
1103:
1104: =cut
1105:
1106: #-------------------------------------------
1.34 matthew 1107: sub get_domains {
1108: # The code below was stolen from "The Perl Cookbook", p 102, 1st ed.
1109: my @domains;
1110: my %seen;
1111: foreach (sort values(%Apache::lonnet::hostdom)) {
1.169 www 1112: push (@domains,$_) unless $seen{$_}++;
1.34 matthew 1113: }
1114: return @domains;
1115: }
1.88 www 1116:
1.169 www 1117: # ------------------------------------------
1118:
1119: sub domain_select {
1120: my ($name,$value,$multiple)=@_;
1121: my %domains=map {
1122: $_ => $_.' '.$Apache::lonnet::domaindescription{$_}
1123: } &get_domains;
1124: if ($multiple) {
1125: $domains{''}=&mt('Any domain');
1.191 matthew 1126: return &multiple_select_form($name,$value,4,%domains);
1.169 www 1127: } else {
1128: return &select_form($name,$value,%domains);
1129: }
1130: }
1131:
1132: sub multiple_select_form {
1.191 matthew 1133: my ($name,$value,$size,%hash)=@_;
1.169 www 1134: my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
1135: my $output='';
1.191 matthew 1136: if (! defined($size)) {
1137: $size = 4;
1138: if (scalar(keys(%hash))<4) {
1139: $size = scalar(keys(%hash));
1140: }
1141: }
1.169 www 1142: $output.="\n<select name='$name' size='$size' multiple='1'>";
1.191 matthew 1143: foreach (sort(keys(%hash))) {
1144: $output.='<option value="'.$_.'" ';
1.253 albertel 1145: $output.='selected="selected" ' if ($selected{$_});
1.191 matthew 1146: $output.='>'.$hash{$_}."</option>\n";
1.169 www 1147: }
1148: $output.="</select>\n";
1149: return $output;
1150: }
1151:
1.88 www 1152: #-------------------------------------------
1153:
1154: =pod
1155:
1.112 bowersj2 1156: =item * select_form($defdom,$name,%hash)
1.88 www 1157:
1158: Returns a string containing a <select name='$name' size='1'> form to
1159: allow a user to select options from a hash option_name => displayed text.
1160: See lonrights.pm for an example invocation and use.
1161:
1162: =cut
1163:
1164: #-------------------------------------------
1165: sub select_form {
1166: my ($def,$name,%hash) = @_;
1167: my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128 albertel 1168: my @keys;
1169: if (exists($hash{'select_form_order'})) {
1170: @keys=@{$hash{'select_form_order'}};
1171: } else {
1172: @keys=sort(keys(%hash));
1173: }
1174: foreach (@keys) {
1.88 www 1175: $selectform.="<option value=\"$_\" ".
1.253 albertel 1176: ($_ eq $def ? 'selected="selected" ' : '').
1.119 www 1177: ">".&mt($hash{$_})."</option>\n";
1.88 www 1178: }
1179: $selectform.="</select>";
1180: return $selectform;
1181: }
1182:
1.167 www 1183: sub gradeleveldescription {
1184: my $gradelevel=shift;
1185: my %gradelevels=(0 => 'Not specified',
1186: 1 => 'Grade 1',
1187: 2 => 'Grade 2',
1188: 3 => 'Grade 3',
1189: 4 => 'Grade 4',
1190: 5 => 'Grade 5',
1191: 6 => 'Grade 6',
1192: 7 => 'Grade 7',
1193: 8 => 'Grade 8',
1194: 9 => 'Grade 9',
1195: 10 => 'Grade 10',
1196: 11 => 'Grade 11',
1197: 12 => 'Grade 12',
1198: 13 => 'Grade 13',
1199: 14 => '100 Level',
1200: 15 => '200 Level',
1201: 16 => '300 Level',
1202: 17 => '400 Level',
1203: 18 => 'Graduate Level');
1204: return &mt($gradelevels{$gradelevel});
1205: }
1206:
1.163 www 1207: sub select_level_form {
1208: my ($deflevel,$name)=@_;
1209: unless ($deflevel) { $deflevel=0; }
1.167 www 1210: my $selectform = "<select name=\"$name\" size=\"1\">\n";
1211: for (my $i=0; $i<=18; $i++) {
1212: $selectform.="<option value=\"$i\" ".
1.253 albertel 1213: ($i==$deflevel ? 'selected="selected" ' : '').
1.167 www 1214: ">".&gradeleveldescription($i)."</option>\n";
1215: }
1216: $selectform.="</select>";
1217: return $selectform;
1.163 www 1218: }
1.167 www 1219:
1.35 matthew 1220: #-------------------------------------------
1221:
1.45 matthew 1222: =pod
1223:
1.112 bowersj2 1224: =item * select_dom_form($defdom,$name,$includeempty)
1.35 matthew 1225:
1226: Returns a string containing a <select name='$name' size='1'> form to
1227: allow a user to select the domain to preform an operation in.
1228: See loncreateuser.pm for an example invocation and use.
1229:
1.90 www 1230: If the $includeempty flag is set, it also includes an empty choice ("no domain
1231: selected");
1232:
1.35 matthew 1233: =cut
1234:
1235: #-------------------------------------------
1.34 matthew 1236: sub select_dom_form {
1.90 www 1237: my ($defdom,$name,$includeempty) = @_;
1.34 matthew 1238: my @domains = get_domains();
1.90 www 1239: if ($includeempty) { @domains=('',@domains); }
1.34 matthew 1240: my $selectdomain = "<select name=\"$name\" size=\"1\">\n";
1241: foreach (@domains) {
1242: $selectdomain.="<option value=\"$_\" ".
1.253 albertel 1243: ($_ eq $defdom ? 'selected="selected" ' : '').
1.34 matthew 1244: ">$_</option>\n";
1245: }
1246: $selectdomain.="</select>";
1247: return $selectdomain;
1248: }
1249:
1.35 matthew 1250: #-------------------------------------------
1251:
1.45 matthew 1252: =pod
1253:
1.112 bowersj2 1254: =item * get_library_servers($domain)
1.35 matthew 1255:
1256: Returns a hash which contains keys like '103l3' and values like
1257: 'kirk.lite.msu.edu'. All of the keys will be for machines in the
1258: given $domain.
1259:
1260: =cut
1261:
1262: #-------------------------------------------
1.52 matthew 1263: sub get_library_servers {
1.33 matthew 1264: my $domain = shift;
1.52 matthew 1265: my %library_servers;
1.33 matthew 1266: foreach (keys(%Apache::lonnet::libserv)) {
1267: if ($Apache::lonnet::hostdom{$_} eq $domain) {
1.52 matthew 1268: $library_servers{$_} = $Apache::lonnet::hostname{$_};
1.33 matthew 1269: }
1270: }
1.52 matthew 1271: return %library_servers;
1.33 matthew 1272: }
1273:
1.35 matthew 1274: #-------------------------------------------
1275:
1.45 matthew 1276: =pod
1277:
1.112 bowersj2 1278: =item * home_server_option_list($domain)
1.35 matthew 1279:
1280: returns a string which contains an <option> list to be used in a
1281: <select> form input. See loncreateuser.pm for an example.
1282:
1283: =cut
1284:
1285: #-------------------------------------------
1.33 matthew 1286: sub home_server_option_list {
1287: my $domain = shift;
1.52 matthew 1288: my %servers = &get_library_servers($domain);
1.33 matthew 1289: my $result = '';
1290: foreach (sort keys(%servers)) {
1291: $result.=
1292: '<option value="'.$_.'">'.$_.' '.$servers{$_}."</option>\n";
1293: }
1294: return $result;
1295: }
1.112 bowersj2 1296:
1297: =pod
1298:
1299: =back
1300:
1301: =cut
1.87 matthew 1302:
1303: ###############################################################
1.112 bowersj2 1304: ## Decoding User Agent ##
1.87 matthew 1305: ###############################################################
1306:
1307: =pod
1308:
1.112 bowersj2 1309: =head1 Decoding the User Agent
1310:
1311: =over 4
1312:
1313: =item * &decode_user_agent()
1.87 matthew 1314:
1315: Inputs: $r
1316:
1317: Outputs:
1318:
1319: =over 4
1320:
1.112 bowersj2 1321: =item * $httpbrowser
1.87 matthew 1322:
1.112 bowersj2 1323: =item * $clientbrowser
1.87 matthew 1324:
1.112 bowersj2 1325: =item * $clientversion
1.87 matthew 1326:
1.112 bowersj2 1327: =item * $clientmathml
1.87 matthew 1328:
1.112 bowersj2 1329: =item * $clientunicode
1.87 matthew 1330:
1.112 bowersj2 1331: =item * $clientos
1.87 matthew 1332:
1333: =back
1334:
1.157 matthew 1335: =back
1336:
1.87 matthew 1337: =cut
1338:
1339: ###############################################################
1340: ###############################################################
1341: sub decode_user_agent {
1.247 albertel 1342: my ($r)=@_;
1.87 matthew 1343: my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
1344: my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
1345: my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247 albertel 1346: if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87 matthew 1347: my $clientbrowser='unknown';
1348: my $clientversion='0';
1349: my $clientmathml='';
1350: my $clientunicode='0';
1351: for (my $i=0;$i<=$#browsertype;$i++) {
1352: my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
1353: if (($httpbrowser=~/$match/i) && ($httpbrowser!~/$notmatch/i)) {
1354: $clientbrowser=$bname;
1355: $httpbrowser=~/$vreg/i;
1356: $clientversion=$1;
1357: $clientmathml=($clientversion>=$minv);
1358: $clientunicode=($clientversion>=$univ);
1359: }
1360: }
1361: my $clientos='unknown';
1362: if (($httpbrowser=~/linux/i) ||
1363: ($httpbrowser=~/unix/i) ||
1364: ($httpbrowser=~/ux/i) ||
1365: ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
1366: if (($httpbrowser=~/vax/i) ||
1367: ($httpbrowser=~/vms/i)) { $clientos='vms'; }
1368: if ($httpbrowser=~/next/i) { $clientos='next'; }
1369: if (($httpbrowser=~/mac/i) ||
1370: ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1371: if ($httpbrowser=~/win/i) { $clientos='win'; }
1372: if ($httpbrowser=~/embed/i) { $clientos='pda'; }
1373: return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
1374: $clientunicode,$clientos,);
1375: }
1376:
1.32 matthew 1377: ###############################################################
1378: ## Authentication changing form generation subroutines ##
1379: ###############################################################
1380: ##
1381: ## All of the authform_xxxxxxx subroutines take their inputs in a
1382: ## hash, and have reasonable default values.
1383: ##
1384: ## formname = the name given in the <form> tag.
1.35 matthew 1385: #-------------------------------------------
1386:
1.45 matthew 1387: =pod
1388:
1.112 bowersj2 1389: =head1 Authentication Routines
1390:
1391: =over 4
1392:
1393: =item * authform_xxxxxx
1.35 matthew 1394:
1395: The authform_xxxxxx subroutines provide javascript and html forms which
1396: handle some of the conveniences required for authentication forms.
1397: This is not an optimal method, but it works.
1398:
1399: See loncreateuser.pm for invocation and use examples.
1400:
1401: =over 4
1402:
1.112 bowersj2 1403: =item * authform_header
1.35 matthew 1404:
1.112 bowersj2 1405: =item * authform_authorwarning
1.35 matthew 1406:
1.112 bowersj2 1407: =item * authform_nochange
1.35 matthew 1408:
1.112 bowersj2 1409: =item * authform_kerberos
1.35 matthew 1410:
1.112 bowersj2 1411: =item * authform_internal
1.35 matthew 1412:
1.112 bowersj2 1413: =item * authform_filesystem
1.35 matthew 1414:
1415: =back
1416:
1.157 matthew 1417: =back
1418:
1.35 matthew 1419: =cut
1420:
1421: #-------------------------------------------
1.32 matthew 1422: sub authform_header{
1423: my %in = (
1424: formname => 'cu',
1.80 albertel 1425: kerb_def_dom => '',
1.32 matthew 1426: @_,
1427: );
1428: $in{'formname'} = 'document.' . $in{'formname'};
1429: my $result='';
1.80 albertel 1430:
1431: #---------------------------------------------- Code for upper case translation
1432: my $Javascript_toUpperCase;
1433: unless ($in{kerb_def_dom}) {
1434: $Javascript_toUpperCase =<<"END";
1435: switch (choice) {
1436: case 'krb': currentform.elements[choicearg].value =
1437: currentform.elements[choicearg].value.toUpperCase();
1438: break;
1439: default:
1440: }
1441: END
1442: } else {
1443: $Javascript_toUpperCase = "";
1444: }
1445:
1.165 raeburn 1446: my $radioval = "'nochange'";
1.174 matthew 1447: if (exists($in{'curr_authtype'}) &&
1448: defined($in{'curr_authtype'}) &&
1449: $in{'curr_authtype'} ne '') {
1450: $radioval = "'$in{'curr_authtype'}arg'";
1451: }
1.165 raeburn 1452: my $argfield = 'null';
1453: if ( grep/^mode$/,(keys %in) ) {
1454: if ($in{'mode'} eq 'modifycourse') {
1455: if ( grep/^curr_authtype$/,(keys %in) ) {
1456: $radioval = "'$in{'curr_authtype'}'";
1457: }
1458: if ( grep/^curr_autharg$/,(keys %in) ) {
1459: unless ($in{'curr_autharg'} eq '') {
1460: $argfield = "'$in{'curr_autharg'}'";
1461: }
1462: }
1463: }
1464: }
1465:
1.32 matthew 1466: $result.=<<"END";
1467: var current = new Object();
1.165 raeburn 1468: current.radiovalue = $radioval;
1469: current.argfield = $argfield;
1.32 matthew 1470:
1471: function changed_radio(choice,currentform) {
1472: var choicearg = choice + 'arg';
1473: // If a radio button in changed, we need to change the argfield
1474: if (current.radiovalue != choice) {
1475: current.radiovalue = choice;
1476: if (current.argfield != null) {
1477: currentform.elements[current.argfield].value = '';
1478: }
1479: if (choice == 'nochange') {
1480: current.argfield = null;
1481: } else {
1482: current.argfield = choicearg;
1483: switch(choice) {
1484: case 'krb':
1485: currentform.elements[current.argfield].value =
1486: "$in{'kerb_def_dom'}";
1487: break;
1488: default:
1489: break;
1490: }
1491: }
1492: }
1493: return;
1494: }
1.22 www 1495:
1.32 matthew 1496: function changed_text(choice,currentform) {
1497: var choicearg = choice + 'arg';
1498: if (currentform.elements[choicearg].value !='') {
1.80 albertel 1499: $Javascript_toUpperCase
1.32 matthew 1500: // clear old field
1501: if ((current.argfield != choicearg) && (current.argfield != null)) {
1502: currentform.elements[current.argfield].value = '';
1503: }
1504: current.argfield = choicearg;
1505: }
1506: set_auth_radio_buttons(choice,currentform);
1507: return;
1.20 www 1508: }
1.32 matthew 1509:
1510: function set_auth_radio_buttons(newvalue,currentform) {
1511: var i=0;
1512: while (i < currentform.login.length) {
1513: if (currentform.login[i].value == newvalue) { break; }
1514: i++;
1515: }
1516: if (i == currentform.login.length) {
1517: return;
1518: }
1519: current.radiovalue = newvalue;
1520: currentform.login[i].checked = true;
1521: return;
1522: }
1523: END
1524: return $result;
1525: }
1526:
1527: sub authform_authorwarning{
1528: my $result='';
1.144 matthew 1529: $result='<i>'.
1530: &mt('As a general rule, only authors or co-authors should be '.
1531: 'filesystem authenticated '.
1532: '(which allows access to the server filesystem).')."</i>\n";
1.32 matthew 1533: return $result;
1534: }
1535:
1536: sub authform_nochange{
1537: my %in = (
1538: formname => 'document.cu',
1539: kerb_def_dom => 'MSU.EDU',
1540: @_,
1541: );
1.144 matthew 1542: my $result = &mt('[_1] Do not change login data',
1543: '<input type="radio" name="login" value="nochange" '.
1544: 'checked="checked" onclick="'.
1545: "javascript:changed_radio('nochange',$in{'formname'});".'" />');
1.32 matthew 1546: return $result;
1547: }
1548:
1549: sub authform_kerberos{
1550: my %in = (
1551: formname => 'document.cu',
1552: kerb_def_dom => 'MSU.EDU',
1.80 albertel 1553: kerb_def_auth => 'krb4',
1.32 matthew 1554: @_,
1555: );
1.165 raeburn 1556: my ($check4,$check5,$krbarg);
1.80 albertel 1557: if ($in{'kerb_def_auth'} eq 'krb5') {
1558: $check5 = " checked=\"on\"";
1559: } else {
1560: $check4 = " checked=\"on\"";
1561: }
1.165 raeburn 1562: $krbarg = $in{'kerb_def_dom'};
1563:
1564: my $krbcheck = "";
1565: if ( grep/^curr_authtype$/,(keys %in) ) {
1566: if ($in{'curr_authtype'} =~ m/^krb/) {
1567: $krbcheck = " checked=\"on\"";
1568: if ( grep/^curr_autharg$/,(keys %in) ) {
1569: $krbarg = $in{'curr_autharg'};
1570: }
1571: }
1572: }
1573:
1.144 matthew 1574: my $jscall = "javascript:changed_radio('krb',$in{'formname'});";
1575: my $result .= &mt
1576: ('[_1] Kerberos authenticated with domain [_2] '.
1577: '[_3] Version 4 [_4] Version 5',
1578: '<input type="radio" name="login" value="krb" '.
1.165 raeburn 1579: 'onclick="'.$jscall.'" onchange="'.$jscall.'"'.$krbcheck.' />',
1.144 matthew 1580: '<input type="text" size="10" name="krbarg" '.
1.165 raeburn 1581: 'value="'.$krbarg.'" '.
1.144 matthew 1582: 'onchange="'.$jscall.'" />',
1583: '<input type="radio" name="krbver" value="4" '.$check4.' />',
1584: '<input type="radio" name="krbver" value="5" '.$check5.' />');
1.32 matthew 1585: return $result;
1586: }
1587:
1588: sub authform_internal{
1589: my %args = (
1590: formname => 'document.cu',
1591: kerb_def_dom => 'MSU.EDU',
1592: @_,
1593: );
1.165 raeburn 1594:
1595: my $intcheck = "";
1596: my $intarg = 'value=""';
1597: if ( grep/^curr_authtype$/,(keys %args) ) {
1598: if ($args{'curr_authtype'} eq 'int') {
1599: $intcheck = " checked=\"on\"";
1600: if ( grep/^curr_autharg$/,(keys %args) ) {
1601: $intarg = "value=\"$args{'curr_autharg'}\"";
1602: }
1603: }
1604: }
1605:
1.144 matthew 1606: my $jscall = "javascript:changed_radio('int',$args{'formname'});";
1607: my $result.=&mt
1608: ('[_1] Internally authenticated (with initial password [_2])',
1.165 raeburn 1609: '<input type="radio" name="login" value="int" '.$intcheck.
1610: ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1611: '<input type="text" size="10" name="intarg" '.$intarg.
1612: ' onchange="'.$jscall.'" />');
1.32 matthew 1613: return $result;
1614: }
1615:
1616: sub authform_local{
1617: my %in = (
1618: formname => 'document.cu',
1619: kerb_def_dom => 'MSU.EDU',
1620: @_,
1621: );
1.165 raeburn 1622:
1623: my $loccheck = "";
1624: my $locarg = 'value=""';
1625: if ( grep/^curr_authtype$/,(keys %in) ) {
1626: if ($in{'curr_authtype'} eq 'loc') {
1627: $loccheck = " checked=\"on\"";
1628: if ( grep/^curr_autharg$/,(keys %in) ) {
1629: $locarg = "value=\"$in{'curr_autharg'}\"";
1630: }
1631: }
1632: }
1633:
1.144 matthew 1634: my $jscall = "javascript:changed_radio('loc',$in{'formname'});";
1.160 matthew 1635: my $result.=&mt('[_1] Local Authentication with argument [_2]',
1.165 raeburn 1636: '<input type="radio" name="login" value="loc" '.$loccheck.
1637: ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1638: '<input type="text" size="10" name="locarg" '.$locarg.
1639: ' onchange="'.$jscall.'" />');
1.32 matthew 1640: return $result;
1641: }
1642:
1643: sub authform_filesystem{
1644: my %in = (
1645: formname => 'document.cu',
1646: kerb_def_dom => 'MSU.EDU',
1647: @_,
1648: );
1.144 matthew 1649: my $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
1650: my $result.= &mt
1651: ('[_1] Filesystem Authenticated (with initial password [_2])',
1652: '<input type="radio" name="login" value="fsys" '.
1653: 'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1654: '<input type="text" size="10" name="fsysarg" value="" '.
1655: 'onchange="'.$jscall.'" />');
1.32 matthew 1656: return $result;
1657: }
1658:
1.80 albertel 1659: ###############################################################
1660: ## Get Authentication Defaults for Domain ##
1661: ###############################################################
1662:
1663: =pod
1664:
1.112 bowersj2 1665: =head1 Domains and Authentication
1666:
1667: Returns default authentication type and an associated argument as
1668: listed in file 'domain.tab'.
1669:
1670: =over 4
1671:
1672: =item * get_auth_defaults
1.80 albertel 1673:
1674: get_auth_defaults($target_domain) returns the default authentication
1675: type and an associated argument (initial password or a kerberos domain).
1676: These values are stored in lonTabs/domain.tab
1677:
1678: ($def_auth, $def_arg) = &get_auth_defaults($target_domain);
1679:
1680: If target_domain is not found in domain.tab, returns nothing ('').
1681:
1682: =cut
1683:
1684: #-------------------------------------------
1685: sub get_auth_defaults {
1686: my $domain=shift;
1687: return ($Apache::lonnet::domain_auth_def{$domain},$Apache::lonnet::domain_auth_arg_def{$domain});
1688: }
1689: ###############################################################
1690: ## End Get Authentication Defaults for Domain ##
1691: ###############################################################
1692:
1693: ###############################################################
1694: ## Get Kerberos Defaults for Domain ##
1695: ###############################################################
1696: ##
1697: ## Returns default kerberos version and an associated argument
1698: ## as listed in file domain.tab. If not listed, provides
1699: ## appropriate default domain and kerberos version.
1700: ##
1701: #-------------------------------------------
1702:
1703: =pod
1704:
1.112 bowersj2 1705: =item * get_kerberos_defaults
1.80 albertel 1706:
1707: get_kerberos_defaults($target_domain) returns the default kerberos
1708: version and domain. If not found in domain.tabs, it defaults to
1709: version 4 and the domain of the server.
1710:
1711: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
1712:
1713: =cut
1714:
1715: #-------------------------------------------
1716: sub get_kerberos_defaults {
1717: my $domain=shift;
1718: my ($krbdef,$krbdefdom) =
1719: &Apache::loncommon::get_auth_defaults($domain);
1720: unless ($krbdef =~/^krb/ && $krbdefdom) {
1721: $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
1722: my $krbdefdom=$1;
1723: $krbdefdom=~tr/a-z/A-Z/;
1724: $krbdef = "krb4";
1725: }
1726: return ($krbdef,$krbdefdom);
1727: }
1.112 bowersj2 1728:
1729: =pod
1730:
1731: =back
1732:
1733: =cut
1.32 matthew 1734:
1.46 matthew 1735: ###############################################################
1736: ## Thesaurus Functions ##
1737: ###############################################################
1.20 www 1738:
1.46 matthew 1739: =pod
1.20 www 1740:
1.112 bowersj2 1741: =head1 Thesaurus Functions
1742:
1743: =over 4
1744:
1745: =item * initialize_keywords
1.46 matthew 1746:
1747: Initializes the package variable %Keywords if it is empty. Uses the
1748: package variable $thesaurus_db_file.
1749:
1750: =cut
1751:
1752: ###################################################
1753:
1754: sub initialize_keywords {
1755: return 1 if (scalar keys(%Keywords));
1756: # If we are here, %Keywords is empty, so fill it up
1757: # Make sure the file we need exists...
1758: if (! -e $thesaurus_db_file) {
1759: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
1760: " failed because it does not exist");
1761: return 0;
1762: }
1763: # Set up the hash as a database
1764: my %thesaurus_db;
1765: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1766: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1767: &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
1768: $thesaurus_db_file);
1769: return 0;
1770: }
1771: # Get the average number of appearances of a word.
1772: my $avecount = $thesaurus_db{'average.count'};
1773: # Put keywords (those that appear > average) into %Keywords
1774: while (my ($word,$data)=each (%thesaurus_db)) {
1775: my ($count,undef) = split /:/,$data;
1776: $Keywords{$word}++ if ($count > $avecount);
1777: }
1778: untie %thesaurus_db;
1779: # Remove special values from %Keywords.
1780: foreach ('total.count','average.count') {
1781: delete($Keywords{$_}) if (exists($Keywords{$_}));
1782: }
1783: return 1;
1784: }
1785:
1786: ###################################################
1787:
1788: =pod
1789:
1.112 bowersj2 1790: =item * keyword($word)
1.46 matthew 1791:
1792: Returns true if $word is a keyword. A keyword is a word that appears more
1793: than the average number of times in the thesaurus database. Calls
1794: &initialize_keywords
1795:
1796: =cut
1797:
1798: ###################################################
1.20 www 1799:
1800: sub keyword {
1.46 matthew 1801: return if (!&initialize_keywords());
1802: my $word=lc(shift());
1803: $word=~s/\W//g;
1804: return exists($Keywords{$word});
1.20 www 1805: }
1.46 matthew 1806:
1807: ###############################################################
1808:
1809: =pod
1.20 www 1810:
1.112 bowersj2 1811: =item * get_related_words
1.46 matthew 1812:
1.160 matthew 1813: Look up a word in the thesaurus. Takes a scalar argument and returns
1.46 matthew 1814: an array of words. If the keyword is not in the thesaurus, an empty array
1815: will be returned. The order of the words returned is determined by the
1816: database which holds them.
1817:
1818: Uses global $thesaurus_db_file.
1819:
1820: =cut
1821:
1822: ###############################################################
1823: sub get_related_words {
1824: my $keyword = shift;
1825: my %thesaurus_db;
1826: if (! -e $thesaurus_db_file) {
1827: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
1828: "failed because the file does not exist");
1829: return ();
1830: }
1831: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1832: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1833: return ();
1834: }
1835: my @Words=();
1836: if (exists($thesaurus_db{$keyword})) {
1837: $_ = $thesaurus_db{$keyword};
1838: (undef,@Words) = split/:/; # The first element is the number of times
1839: # the word appears. We do not need it now.
1840: for (my $i=0;$i<=$#Words;$i++) {
1841: ($Words[$i],undef)= split/\,/,$Words[$i];
1.20 www 1842: }
1843: }
1.46 matthew 1844: untie %thesaurus_db;
1845: return @Words;
1.14 harris41 1846: }
1.46 matthew 1847:
1.112 bowersj2 1848: =pod
1849:
1850: =back
1851:
1852: =cut
1.61 www 1853:
1854: # -------------------------------------------------------------- Plaintext name
1.81 albertel 1855: =pod
1856:
1.112 bowersj2 1857: =head1 User Name Functions
1858:
1859: =over 4
1860:
1.226 albertel 1861: =item * plainname($uname,$udom,$first)
1.81 albertel 1862:
1.112 bowersj2 1863: Takes a users logon name and returns it as a string in
1.226 albertel 1864: "first middle last generation" form
1865: if $first is set to 'lastname' then it returns it as
1866: 'lastname generation, firstname middlename' if their is a lastname
1.81 albertel 1867:
1868: =cut
1.61 www 1869:
1.81 albertel 1870: ###############################################################
1.61 www 1871: sub plainname {
1.226 albertel 1872: my ($uname,$udom,$first)=@_;
1.61 www 1873: my %names=&Apache::lonnet::get('environment',
1874: ['firstname','middlename','lastname','generation'],
1875: $udom,$uname);
1.226 albertel 1876: my $name=&Apache::lonnet::format_name($names{'firstname'},
1877: $names{'middlename'},
1878: $names{'lastname'},
1879: $names{'generation'},$first);
1880: $name=~s/^\s+//;
1.62 www 1881: $name=~s/\s+$//;
1882: $name=~s/\s+/ /g;
1.190 albertel 1883: if ($name !~ /\S/) { $name=$uname.'@'.$udom; }
1.62 www 1884: return $name;
1.61 www 1885: }
1.66 www 1886:
1887: # -------------------------------------------------------------------- Nickname
1.81 albertel 1888: =pod
1889:
1.112 bowersj2 1890: =item * nickname($uname,$udom)
1.81 albertel 1891:
1892: Gets a users name and returns it as a string as
1893:
1894: ""nickname""
1.66 www 1895:
1.81 albertel 1896: if the user has a nickname or
1897:
1898: "first middle last generation"
1899:
1900: if the user does not
1901:
1902: =cut
1.66 www 1903:
1904: sub nickname {
1905: my ($uname,$udom)=@_;
1.212 albertel 1906: my %names;
1.258 albertel 1907: if ($uname eq $env{'user.name'} &&
1908: $udom eq $env{'user.domain'}) {
1909: %names=('nickname' => $env{'environment.nickname'} ,
1910: 'firstname' => $env{'environment.firstname'} ,
1911: 'middlename' => $env{'environment.middlename'},
1912: 'lastname' => $env{'environment.lastname'} ,
1913: 'generation' => $env{'environment.generation'});
1.212 albertel 1914: } else {
1915: %names=&Apache::lonnet::get('environment',
1916: ['nickname','firstname','middlename',
1917: 'lastname','generation'],$udom,$uname);
1918: }
1.68 albertel 1919: my $name=$names{'nickname'};
1.66 www 1920: if ($name) {
1921: $name='"'.$name.'"';
1922: } else {
1923: $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
1924: $names{'lastname'}.' '.$names{'generation'};
1925: $name=~s/\s+$//;
1926: $name=~s/\s+/ /g;
1927: }
1928: return $name;
1929: }
1930:
1.61 www 1931:
1932: # ------------------------------------------------------------------ Screenname
1.81 albertel 1933:
1934: =pod
1935:
1.112 bowersj2 1936: =item * screenname($uname,$udom)
1.81 albertel 1937:
1938: Gets a users screenname and returns it as a string
1939:
1940: =cut
1.61 www 1941:
1942: sub screenname {
1943: my ($uname,$udom)=@_;
1.258 albertel 1944: if ($uname eq $env{'user.name'} &&
1945: $udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212 albertel 1946: my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68 albertel 1947: return $names{'screenname'};
1.62 www 1948: }
1949:
1.212 albertel 1950:
1.62 www 1951: # ------------------------------------------------------------- Message Wrapper
1952:
1953: sub messagewrapper {
1.200 matthew 1954: my ($link,$username,$domain)=@_;
1.62 www 1955: return
1.200 matthew 1956: '<a href="/adm/email?compose=individual&'.
1957: 'recname='.$username.'&recdom='.$domain.'" '.
1958: 'title="'.&mt('Send message').'">'.$link.'</a>';
1.74 www 1959: }
1960: # --------------------------------------------------------------- Notes Wrapper
1961:
1962: sub noteswrapper {
1963: my ($link,$un,$do)=@_;
1964: return
1965: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62 www 1966: }
1967: # ------------------------------------------------------------- Aboutme Wrapper
1968:
1969: sub aboutmewrapper {
1.166 www 1970: my ($link,$username,$domain,$target)=@_;
1.205 www 1971: return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.200 matthew 1972: ($target?' target="$target"':'').' title="'.&mt('View this users personal page').'">'.$link.'</a>';
1.62 www 1973: }
1974:
1975: # ------------------------------------------------------------ Syllabus Wrapper
1976:
1977:
1978: sub syllabuswrapper {
1.109 matthew 1979: my ($linktext,$coursedir,$domain,$fontcolor)=@_;
1980: if ($fontcolor) {
1981: $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>';
1982: }
1.208 matthew 1983: return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61 www 1984: }
1.14 harris41 1985:
1.208 matthew 1986: sub track_student_link {
1987: my ($linktext,$sname,$sdom,$target) = @_;
1988: my $link ="/adm/trackstudent";
1989: my $title = 'View recent activity';
1990: if (defined($sname) && $sname !~ /^\s*$/ &&
1991: defined($sdom) && $sdom !~ /^\s*$/) {
1992: $link .= "?selected_student=$sname:$sdom";
1993: $title .= ' of this student';
1994: }
1995: if (defined($target) && $target !~ /^\s*$/) {
1996: $target = qq{target="$target"};
1997: } else {
1998: $target = '';
1999: }
2000: return qq{<a href="$link" title="$title" $target>$linktext</a>};
2001: }
2002:
1.266 ! www 2003: # --------------------------------------------------------- Links into grading, etc
1.208 matthew 2004:
1.266 ! www 2005: sub parm_direct_link {
! 2006: my ($linktext,$sname,$sdom,$symb,$target)=@_;
! 2007: unless ($sname) { return ''; }
! 2008: unless ($sdom) { return ''; }
! 2009: unless ($symb) { return ''; }
! 2010:
! 2011: my $link ="/adm/parmset";
! 2012: my $title = 'Change parameters';
! 2013: if (defined($sname) && $sname !~ /^\s*$/ &&
! 2014: defined($sdom) && $sdom !~ /^\s*$/) {
! 2015: $link .= "?uname=$sname&udom=$sdom&action=settable&symb=".
! 2016: &Apache::lonnet::escape($symb);
! 2017: }
! 2018: if (defined($target) && $target !~ /^\s*$/) {
! 2019: $target = qq{target="$target"};
! 2020: } else {
! 2021: $target = '';
! 2022: }
! 2023: return qq{<a href="$link" title="$title" $target>$linktext</a>};
! 2024: }
! 2025:
! 2026: sub grds_direct_link {
! 2027: my ($linktext,$sname,$sdom,$symb,$target)=@_;
! 2028: unless ($sname) { return ''; }
! 2029: unless ($sdom) { return ''; }
! 2030: unless ($symb) { return ''; }
! 2031: my $url=(&Apache::lonnet::decode_symb($symb))[2];
! 2032: my $link ="/adm/grades";
! 2033: my $title = 'Change grades';
! 2034: if (defined($sname) && $sname !~ /^\s*$/ &&
! 2035: defined($sdom) && $sdom !~ /^\s*$/) {
! 2036: $link .= "?stuinfo=$sname:$sdom&symb=".
! 2037: &Apache::lonnet::escape($symb)."&url=".
! 2038: &Apache::lonnet::escape($url).
! 2039: "&showgrading=yes&lastsub=all&vProp=yes&vAns=yes&command=processGroup";
! 2040: }
! 2041: if (defined($target) && $target !~ /^\s*$/) {
! 2042: $target = qq{target="$target"};
! 2043: } else {
! 2044: $target = '';
! 2045: }
! 2046: return qq{<a href="$link" title="$title" $target>$linktext</a>};
! 2047: }
1.208 matthew 2048:
1.112 bowersj2 2049: =pod
2050:
2051: =back
2052:
2053: =head1 Access .tab File Data
2054:
2055: =over 4
2056:
2057: =item * languageids()
2058:
2059: returns list of all language ids
2060:
2061: =cut
2062:
1.14 harris41 2063: sub languageids {
1.16 harris41 2064: return sort(keys(%language));
1.14 harris41 2065: }
2066:
1.112 bowersj2 2067: =pod
2068:
2069: =item * languagedescription()
2070:
2071: returns description of a specified language id
2072:
2073: =cut
2074:
1.14 harris41 2075: sub languagedescription {
1.125 www 2076: my $code=shift;
2077: return ($supported_language{$code}?'* ':'').
2078: $language{$code}.
1.126 www 2079: ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145 www 2080: }
2081:
2082: sub plainlanguagedescription {
2083: my $code=shift;
2084: return $language{$code};
2085: }
2086:
2087: sub supportedlanguagecode {
2088: my $code=shift;
2089: return $supported_language{$code};
1.97 www 2090: }
2091:
1.112 bowersj2 2092: =pod
2093:
2094: =item * copyrightids()
2095:
2096: returns list of all copyrights
2097:
2098: =cut
2099:
2100: sub copyrightids {
2101: return sort(keys(%cprtag));
2102: }
2103:
2104: =pod
2105:
2106: =item * copyrightdescription()
2107:
2108: returns description of a specified copyright id
2109:
2110: =cut
2111:
2112: sub copyrightdescription {
1.166 www 2113: return &mt($cprtag{shift(@_)});
1.112 bowersj2 2114: }
1.197 matthew 2115:
2116: =pod
2117:
1.192 taceyjo1 2118: =item * source_copyrightids()
2119:
2120: returns list of all source copyrights
2121:
2122: =cut
2123:
2124: sub source_copyrightids {
2125: return sort(keys(%scprtag));
2126: }
2127:
2128: =pod
2129:
2130: =item * source_copyrightdescription()
2131:
2132: returns description of a specified source copyright id
2133:
2134: =cut
2135:
2136: sub source_copyrightdescription {
2137: return &mt($scprtag{shift(@_)});
2138: }
1.112 bowersj2 2139:
2140: =pod
2141:
2142: =item * filecategories()
2143:
2144: returns list of all file categories
2145:
2146: =cut
2147:
2148: sub filecategories {
2149: return sort(keys(%category_extensions));
2150: }
2151:
2152: =pod
2153:
2154: =item * filecategorytypes()
2155:
2156: returns list of file types belonging to a given file
2157: category
2158:
2159: =cut
2160:
2161: sub filecategorytypes {
2162: return @{$category_extensions{lc($_[0])}};
2163: }
2164:
2165: =pod
2166:
2167: =item * fileembstyle()
2168:
2169: returns embedding style for a specified file type
2170:
2171: =cut
2172:
2173: sub fileembstyle {
2174: return $fe{lc(shift(@_))};
1.169 www 2175: }
2176:
2177:
2178: sub filecategoryselect {
2179: my ($name,$value)=@_;
1.189 matthew 2180: return &select_form($value,$name,
1.169 www 2181: '' => &mt('Any category'),
2182: map { $_,$_ } sort(keys(%category_extensions)));
1.112 bowersj2 2183: }
2184:
2185: =pod
2186:
2187: =item * filedescription()
2188:
2189: returns description for a specified file type
2190:
2191: =cut
2192:
2193: sub filedescription {
1.188 matthew 2194: my $file_description = $fd{lc(shift())};
2195: $file_description =~ s:([\[\]]):~$1:g;
2196: return &mt($file_description);
1.112 bowersj2 2197: }
2198:
2199: =pod
2200:
2201: =item * filedescriptionex()
2202:
2203: returns description for a specified file type with
2204: extra formatting
2205:
2206: =cut
2207:
2208: sub filedescriptionex {
2209: my $ex=shift;
1.188 matthew 2210: my $file_description = $fd{lc($ex)};
2211: $file_description =~ s:([\[\]]):~$1:g;
2212: return '.'.$ex.' '.&mt($file_description);
1.112 bowersj2 2213: }
2214:
2215: # End of .tab access
2216: =pod
2217:
2218: =back
2219:
2220: =cut
2221:
2222: # ------------------------------------------------------------------ File Types
2223: sub fileextensions {
2224: return sort(keys(%fe));
2225: }
2226:
1.97 www 2227: # ----------------------------------------------------------- Display Languages
2228: # returns a hash with all desired display languages
2229: #
2230:
2231: sub display_languages {
2232: my %languages=();
1.118 www 2233: foreach (&preferred_languages()) {
2234: $languages{$_}=1;
1.97 www 2235: }
2236: &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258 albertel 2237: if ($env{'form.displaylanguage'}) {
2238: foreach (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
1.97 www 2239: $languages{$_}=1;
2240: }
2241: }
2242: return %languages;
1.14 harris41 2243: }
2244:
1.117 www 2245: sub preferred_languages {
2246: my @languages=();
1.258 albertel 2247: if ($env{'course.'.$env{'request.course.id'}.'.languages'}) {
1.117 www 2248: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
1.258 albertel 2249: $env{'course.'.$env{'request.course.id'}.'.languages'}));
1.177 www 2250: }
1.258 albertel 2251: if ($env{'environment.languages'}) {
2252: @languages=split(/\s*(\,|\;|\:)\s*/,$env{'environment.languages'});
1.118 www 2253: }
1.162 www 2254: my $browser=(split(/\;/,$ENV{'HTTP_ACCEPT_LANGUAGE'}))[0];
2255: if ($browser) {
2256: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$browser));
2257: }
1.258 albertel 2258: if ($Apache::lonnet::domain_lang_def{$env{'user.domain'}}) {
1.118 www 2259: @languages=(@languages,
1.258 albertel 2260: $Apache::lonnet::domain_lang_def{$env{'user.domain'}});
1.118 www 2261: }
1.258 albertel 2262: if ($Apache::lonnet::domain_lang_def{$env{'request.role.domain'}}) {
1.118 www 2263: @languages=(@languages,
1.258 albertel 2264: $Apache::lonnet::domain_lang_def{$env{'request.role.domain'}});
1.118 www 2265: }
2266: if ($Apache::lonnet::domain_lang_def{
2267: $Apache::lonnet::perlvar{'lonDefDomain'}}) {
2268: @languages=(@languages,
2269: $Apache::lonnet::domain_lang_def{
2270: $Apache::lonnet::perlvar{'lonDefDomain'}});
2271: }
2272: # turn "en-ca" into "en-ca,en"
2273: my @genlanguages;
2274: foreach (@languages) {
2275: unless ($_=~/\w/) { next; }
2276: push (@genlanguages,$_);
2277: if ($_=~/(\-|\_)/) {
2278: push (@genlanguages,(split(/(\-|\_)/,$_))[0]);
2279: }
2280: }
2281: return @genlanguages;
1.117 www 2282: }
2283:
1.112 bowersj2 2284: ###############################################################
2285: ## Student Answer Attempts ##
2286: ###############################################################
2287:
2288: =pod
2289:
2290: =head1 Alternate Problem Views
2291:
2292: =over 4
2293:
2294: =item * get_previous_attempt($symb, $username, $domain, $course,
2295: $getattempt, $regexp, $gradesub)
2296:
2297: Return string with previous attempt on problem. Arguments:
2298:
2299: =over 4
2300:
2301: =item * $symb: Problem, including path
2302:
2303: =item * $username: username of the desired student
2304:
2305: =item * $domain: domain of the desired student
1.14 harris41 2306:
1.112 bowersj2 2307: =item * $course: Course ID
1.14 harris41 2308:
1.112 bowersj2 2309: =item * $getattempt: Leave blank for all attempts, otherwise put
2310: something
1.14 harris41 2311:
1.112 bowersj2 2312: =item * $regexp: if string matches this regexp, the string will be
2313: sent to $gradesub
1.14 harris41 2314:
1.112 bowersj2 2315: =item * $gradesub: routine that processes the string if it matches $regexp
1.14 harris41 2316:
1.112 bowersj2 2317: =back
1.14 harris41 2318:
1.112 bowersj2 2319: The output string is a table containing all desired attempts, if any.
1.16 harris41 2320:
1.112 bowersj2 2321: =cut
1.1 albertel 2322:
2323: sub get_previous_attempt {
1.43 ng 2324: my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1 albertel 2325: my $prevattempts='';
1.43 ng 2326: no strict 'refs';
1.1 albertel 2327: if ($symb) {
1.3 albertel 2328: my (%returnhash)=
2329: &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1 albertel 2330: if ($returnhash{'version'}) {
2331: my %lasthash=();
2332: my $version;
2333: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.19 harris41 2334: foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
1.1 albertel 2335: $lasthash{$_}=$returnhash{$version.':'.$_};
1.19 harris41 2336: }
1.1 albertel 2337: }
1.43 ng 2338: $prevattempts='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.40 ng 2339: $prevattempts.='<table border="0" width="100%"><tr bgcolor="#e6ffff"><td>History</td>';
1.16 harris41 2340: foreach (sort(keys %lasthash)) {
1.31 albertel 2341: my ($ign,@parts) = split(/\./,$_);
1.41 ng 2342: if ($#parts > 0) {
1.31 albertel 2343: my $data=$parts[-1];
2344: pop(@parts);
1.40 ng 2345: $prevattempts.='<td>Part '.join('.',@parts).'<br />'.$data.' </td>';
1.31 albertel 2346: } else {
1.41 ng 2347: if ($#parts == 0) {
2348: $prevattempts.='<th>'.$parts[0].'</th>';
2349: } else {
2350: $prevattempts.='<th>'.$ign.'</th>';
2351: }
1.31 albertel 2352: }
1.16 harris41 2353: }
1.40 ng 2354: if ($getattempt eq '') {
2355: for ($version=1;$version<=$returnhash{'version'};$version++) {
2356: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Transaction '.$version.'</td>';
2357: foreach (sort(keys %lasthash)) {
2358: my $value;
2359: if ($_ =~ /timestamp/) {
2360: $value=scalar(localtime($returnhash{$version.':'.$_}));
2361: } else {
2362: $value=$returnhash{$version.':'.$_};
2363: }
1.142 albertel 2364: $prevattempts.='<td>'.&Apache::lonnet::unescape($value).' </td>';
1.40 ng 2365: }
2366: }
1.1 albertel 2367: }
1.40 ng 2368: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Current</td>';
1.16 harris41 2369: foreach (sort(keys %lasthash)) {
1.5 albertel 2370: my $value;
2371: if ($_ =~ /timestamp/) {
2372: $value=scalar(localtime($lasthash{$_}));
2373: } else {
2374: $value=$lasthash{$_};
2375: }
1.142 albertel 2376: $value=&Apache::lonnet::unescape($value);
1.49 ng 2377: if ($_ =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40 ng 2378: $prevattempts.='<td>'.$value.' </td>';
1.16 harris41 2379: }
1.40 ng 2380: $prevattempts.='</tr></table></td></tr></table>';
1.1 albertel 2381: } else {
2382: $prevattempts='Nothing submitted - no attempts.';
2383: }
2384: } else {
2385: $prevattempts='No data.';
2386: }
1.10 albertel 2387: }
2388:
1.107 albertel 2389: sub relative_to_absolute {
2390: my ($url,$output)=@_;
2391: my $parser=HTML::TokeParser->new(\$output);
2392: my $token;
2393: my $thisdir=$url;
2394: my @rlinks=();
2395: while ($token=$parser->get_token) {
2396: if ($token->[0] eq 'S') {
2397: if ($token->[1] eq 'a') {
2398: if ($token->[2]->{'href'}) {
2399: $rlinks[$#rlinks+1]=$token->[2]->{'href'};
2400: }
2401: } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
2402: $rlinks[$#rlinks+1]=$token->[2]->{'src'};
2403: } elsif ($token->[1] eq 'base') {
2404: $thisdir=$token->[2]->{'href'};
2405: }
2406: }
2407: }
2408: $thisdir=~s-/[^/]*$--;
2409: foreach (@rlinks) {
2410: unless (($_=~/^http:\/\//i) ||
2411: ($_=~/^\//) ||
2412: ($_=~/^javascript:/i) ||
2413: ($_=~/^mailto:/i) ||
2414: ($_=~/^\#/)) {
2415: my $newlocation=&Apache::lonnet::hreflocation($thisdir,$_);
2416: $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
2417: }
2418: }
2419: # -------------------------------------------------- Deal with Applet codebases
2420: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
2421: return $output;
2422: }
2423:
1.112 bowersj2 2424: =pod
2425:
2426: =item * get_student_view
2427:
2428: show a snapshot of what student was looking at
2429:
2430: =cut
2431:
1.10 albertel 2432: sub get_student_view {
1.186 albertel 2433: my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114 www 2434: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186 albertel 2435: my (%form);
1.10 albertel 2436: my @elements=('symb','courseid','domain','username');
2437: foreach my $element (@elements) {
1.186 albertel 2438: $form{'grade_'.$element}=eval '$'.$element #'
1.10 albertel 2439: }
1.186 albertel 2440: if (defined($moreenv)) {
2441: %form=(%form,%{$moreenv});
2442: }
1.236 albertel 2443: if (defined($target)) { $form{'grade_target'} = $target; }
1.107 albertel 2444: $feedurl=&Apache::lonnet::clutter($feedurl);
1.186 albertel 2445: my $userview=&Apache::lonnet::ssi_body($feedurl,%form);
1.11 albertel 2446: $userview=~s/\<body[^\>]*\>//gi;
2447: $userview=~s/\<\/body\>//gi;
2448: $userview=~s/\<html\>//gi;
2449: $userview=~s/\<\/html\>//gi;
2450: $userview=~s/\<head\>//gi;
2451: $userview=~s/\<\/head\>//gi;
2452: $userview=~s/action\s*\=/would_be_action\=/gi;
1.107 albertel 2453: $userview=&relative_to_absolute($feedurl,$userview);
1.11 albertel 2454: return $userview;
2455: }
2456:
1.112 bowersj2 2457: =pod
2458:
2459: =item * get_student_answers()
2460:
2461: show a snapshot of how student was answering problem
2462:
2463: =cut
2464:
1.11 albertel 2465: sub get_student_answers {
1.100 sakharuk 2466: my ($symb,$username,$domain,$courseid,%form) = @_;
1.114 www 2467: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186 albertel 2468: my (%moreenv);
1.11 albertel 2469: my @elements=('symb','courseid','domain','username');
2470: foreach my $element (@elements) {
1.186 albertel 2471: $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10 albertel 2472: }
1.186 albertel 2473: $moreenv{'grade_target'}='answer';
2474: %moreenv=(%form,%moreenv);
2475: my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%moreenv);
1.10 albertel 2476: return $userview;
1.1 albertel 2477: }
1.116 albertel 2478:
2479: =pod
2480:
2481: =item * &submlink()
2482:
1.242 albertel 2483: Inputs: $text $uname $udom $symb $target
1.116 albertel 2484:
2485: Returns: A link to grades.pm such as to see the SUBM view of a student
2486:
2487: =cut
2488:
2489: ###############################################
2490: sub submlink {
1.242 albertel 2491: my ($text,$uname,$udom,$symb,$target)=@_;
1.116 albertel 2492: if (!($uname && $udom)) {
2493: (my $cursymb, my $courseid,$udom,$uname)=
2494: &Apache::lonxml::whichuser($symb);
2495: if (!$symb) { $symb=$cursymb; }
2496: }
1.254 matthew 2497: if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.242 albertel 2498: $symb=&Apache::lonnet::escape($symb);
2499: if ($target) { $target="target=\"$target\""; }
2500: return '<a href="/adm/grades?&command=submission&'.
2501: 'symb='.$symb.'&student='.$uname.
2502: '&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
2503: }
2504: ##############################################
2505:
2506: =pod
2507:
2508: =item * &pgrdlink()
2509:
2510: Inputs: $text $uname $udom $symb $target
2511:
2512: Returns: A link to grades.pm such as to see the PGRD view of a student
2513:
2514: =cut
2515:
2516: ###############################################
2517: sub pgrdlink {
2518: my $link=&submlink(@_);
2519: $link=~s/(&command=submission)/$1&showgrading=yes/;
2520: return $link;
2521: }
2522: ##############################################
2523:
2524: =pod
2525:
2526: =item * &pprmlink()
2527:
2528: Inputs: $text $uname $udom $symb $target
2529:
2530: Returns: A link to parmset.pm such as to see the PPRM view of a
2531: student andn resource
2532:
2533: =cut
2534:
2535: ###############################################
2536: sub pprmlink {
2537: my ($text,$uname,$udom,$symb,$target)=@_;
2538: if (!($uname && $udom)) {
2539: (my $cursymb, my $courseid,$udom,$uname)=
2540: &Apache::lonxml::whichuser($symb);
2541: if (!$symb) { $symb=$cursymb; }
2542: }
1.254 matthew 2543: if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.242 albertel 2544: $symb=&Apache::lonnet::escape($symb);
2545: if ($target) { $target="target=\"$target\""; }
2546: return '<a href="/adm/parmset?&command=set&'.
2547: 'symb='.$symb.'&uname='.$uname.
2548: '&udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116 albertel 2549: }
2550: ##############################################
1.37 matthew 2551:
1.112 bowersj2 2552: =pod
2553:
2554: =back
2555:
2556: =cut
2557:
1.37 matthew 2558: ###############################################
1.51 www 2559:
2560:
2561: sub timehash {
2562: my @ltime=localtime(shift);
2563: return ( 'seconds' => $ltime[0],
2564: 'minutes' => $ltime[1],
2565: 'hours' => $ltime[2],
2566: 'day' => $ltime[3],
2567: 'month' => $ltime[4]+1,
2568: 'year' => $ltime[5]+1900,
2569: 'weekday' => $ltime[6],
2570: 'dayyear' => $ltime[7]+1,
2571: 'dlsav' => $ltime[8] );
2572: }
2573:
2574: sub maketime {
2575: my %th=@_;
2576: return POSIX::mktime(
2577: ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210 www 2578: $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70 www 2579: }
2580:
2581: #########################################
1.51 www 2582:
2583: sub findallcourses {
2584: my %courses=();
2585: my $now=time;
1.258 albertel 2586: foreach (keys %env) {
1.51 www 2587: if ($_=~/^user\.role\.\w+\.\/(\w+)\/(\w+)/) {
1.258 albertel 2588: my ($starttime,$endtime)=$env{$_};
1.51 www 2589: my $active=1;
2590: if ($starttime) {
2591: if ($now<$starttime) { $active=0; }
2592: }
2593: if ($endtime) {
2594: if ($now>$endtime) { $active=0; }
2595: }
2596: if ($active) { $courses{$1.'_'.$2}=1; }
2597: }
2598: }
2599: return keys %courses;
2600: }
1.37 matthew 2601:
1.54 www 2602: ###############################################
1.60 matthew 2603: ###############################################
2604:
2605: =pod
2606:
1.112 bowersj2 2607: =head1 Domain Template Functions
2608:
2609: =over 4
2610:
2611: =item * &determinedomain()
1.60 matthew 2612:
2613: Inputs: $domain (usually will be undef)
2614:
1.63 www 2615: Returns: Determines which domain should be used for designs
1.60 matthew 2616:
2617: =cut
1.54 www 2618:
1.60 matthew 2619: ###############################################
1.63 www 2620: sub determinedomain {
2621: my $domain=shift;
2622: if (! $domain) {
1.60 matthew 2623: # Determine domain if we have not been given one
2624: $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1.258 albertel 2625: if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
2626: if ($env{'request.role.domain'}) {
2627: $domain=$env{'request.role.domain'};
1.60 matthew 2628: }
2629: }
1.63 www 2630: return $domain;
2631: }
2632: ###############################################
2633: =pod
2634:
1.112 bowersj2 2635: =item * &domainlogo()
1.63 www 2636:
2637: Inputs: $domain (usually will be undef)
2638:
2639: Returns: A link to a domain logo, if the domain logo exists.
2640: If the domain logo does not exist, a description of the domain.
2641:
2642: =cut
1.112 bowersj2 2643:
1.63 www 2644: ###############################################
2645: sub domainlogo {
2646: my $domain = &determinedomain(shift);
2647: # See if there is a logo
1.59 www 2648: if (-e '/home/httpd/html/adm/lonDomLogos/'.$domain.'.gif') {
1.215 albertel 2649: my $logo=&lonhttpdurl("/adm/lonDomLogos/$domain.gif");
2650: return '<img src="'.$logo.'" alt="'.$domain.'" />';
1.60 matthew 2651: } elsif(exists($Apache::lonnet::domaindescription{$domain})) {
2652: return $Apache::lonnet::domaindescription{$domain};
1.59 www 2653: } else {
1.60 matthew 2654: return '';
1.59 www 2655: }
2656: }
1.63 www 2657: ##############################################
2658:
2659: =pod
2660:
1.112 bowersj2 2661: =item * &designparm()
1.63 www 2662:
2663: Inputs: $which parameter; $domain (usually will be undef)
2664:
2665: Returns: value of designparamter $which
2666:
2667: =cut
1.112 bowersj2 2668:
1.63 www 2669: ##############################################
2670: sub designparm {
2671: my ($which,$domain)=@_;
1.258 albertel 2672: if ($env{'browser.blackwhite'} eq 'on') {
1.110 www 2673: if ($which=~/\.(font|alink|vlink|link)$/) {
2674: return '#000000';
2675: }
2676: if ($which=~/\.(pgbg|sidebg)$/) {
2677: return '#FFFFFF';
2678: }
2679: if ($which=~/\.tabbg$/) {
2680: return '#CCCCCC';
2681: }
2682: }
1.258 albertel 2683: if ($env{'environment.color.'.$which}) {
2684: return $env{'environment.color.'.$which};
1.96 www 2685: }
1.63 www 2686: $domain=&determinedomain($domain);
2687: if ($designhash{$domain.'.'.$which}) {
2688: return $designhash{$domain.'.'.$which};
2689: } else {
2690: return $designhash{'default.'.$which};
2691: }
2692: }
1.59 www 2693:
1.60 matthew 2694: ###############################################
2695: ###############################################
2696:
2697: =pod
2698:
1.112 bowersj2 2699: =back
2700:
2701: =head1 HTTP Helpers
2702:
2703: =over 4
2704:
2705: =item * &bodytag()
1.60 matthew 2706:
2707: Returns a uniform header for LON-CAPA web pages.
2708:
2709: Inputs:
2710:
1.112 bowersj2 2711: =over 4
2712:
2713: =item * $title, A title to be displayed on the page.
2714:
2715: =item * $function, the current role (can be undef).
2716:
2717: =item * $addentries, extra parameters for the <body> tag.
2718:
2719: =item * $bodyonly, if defined, only return the <body> tag.
2720:
2721: =item * $domain, if defined, force a given domain.
2722:
2723: =item * $forcereg, if page should register as content page (relevant for
1.86 www 2724: text interface only)
1.60 matthew 2725:
1.112 bowersj2 2726: =back
2727:
1.60 matthew 2728: Returns: A uniform header for LON-CAPA web pages.
2729: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
2730: If $bodyonly is undef or zero, an html string containing a <body> tag and
2731: other decorations will be returned.
2732:
2733: =cut
2734:
1.54 www 2735: sub bodytag {
1.224 raeburn 2736: my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle)=@_;
1.117 www 2737: $title=&mt($title);
1.183 matthew 2738: $function = &get_users_function() if (!$function);
1.63 www 2739: my $img=&designparm($function.'.img',$domain);
2740: my $pgbg=&designparm($function.'.pgbg',$domain);
2741: my $tabbg=&designparm($function.'.tabbg',$domain);
2742: my $font=&designparm($function.'.font',$domain);
2743: my $link=&designparm($function.'.link',$domain);
2744: my $alink=&designparm($function.'.alink',$domain);
2745: my $vlink=&designparm($function.'.vlink',$domain);
2746: my $sidebg=&designparm($function.'.sidebg',$domain);
1.110 www 2747: # Accessibility font enhance
2748: unless ($addentries) { $addentries=''; }
1.146 www 2749: my $addstyle='';
1.258 albertel 2750: if ($env{'browser.fontenhance'} eq 'on') {
1.146 www 2751: $addstyle=' font-size: x-large;';
1.110 www 2752: }
1.63 www 2753: # role and realm
1.55 www 2754: my ($role,$realm)
1.258 albertel 2755: =&Apache::lonnet::plaintext((split(/\./,$env{'request.role'}))[0]);
1.55 www 2756: # realm
1.258 albertel 2757: if ($env{'request.course.id'}) {
1.55 www 2758: $realm=
1.258 albertel 2759: $env{'course.'.$env{'request.course.id'}.'.description'};
1.54 www 2760: }
1.55 www 2761: unless ($realm) { $realm=' '; }
2762: # Set messages
1.60 matthew 2763: my $messages=&domainlogo($domain);
1.101 www 2764: # Port for miniserver
1.83 albertel 2765: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
2766: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1.101 www 2767: # construct main body tag
1.60 matthew 2768: my $bodytag = <<END;
1.231 albertel 2769: <style type="text/css">
1.147 www 2770: h1, h2, h3, th { font-family: Arial, Helvetica, sans-serif }
1.146 www 2771: a:focus { color: red; background: yellow }
2772: </style>
1.54 www 2773: <body bgcolor="$pgbg" text="$font" alink="$alink" vlink="$vlink" link="$link"
1.151 www 2774: style="margin-top: 0px;$addstyle" $addentries>
1.60 matthew 2775: END
1.258 albertel 2776: if ($env{'environment.texengine'} eq 'jsMath') {
1.252 albertel 2777: $bodytag.='<script type="text/javascript">
2778: function NoFontMessage () {}
2779: </script>'."\n".
2780: '<script src="/adm/jsMath/jsMath.js"></script>'."\n";
2781: }
2782:
1.94 www 2783: my $upperleft='<img src="http://'.$ENV{'HTTP_HOST'}.':'.
1.150 matthew 2784: $lonhttpdPort.$img.'" alt="'.$function.'" />';
1.60 matthew 2785: if ($bodyonly) {
2786: return $bodytag;
1.258 albertel 2787: } elsif ($env{'browser.interface'} eq 'textual') {
1.95 www 2788: # Accessibility
1.224 raeburn 2789:
1.93 www 2790: return $bodytag.&Apache::lonmenu::menubuttons($forcereg,'web',
2791: $forcereg).
2792: '<h1>LON-CAPA: '.$title.'</h1>';
1.258 albertel 2793: } elsif ($env{'environment.remote'} eq 'off') {
1.95 www 2794: # No Remote
1.206 albertel 2795: my $roleinfo=(<<ENDROLE);
2796: <td bgcolor="$tabbg" align="right">
2797: <font size="2" face="Arial, Helvetica, sans-serif">
1.258 albertel 2798: $env{'environment.firstname'}
2799: $env{'environment.middlename'}
2800: $env{'environment.lastname'}
2801: $env{'environment.generation'}
1.206 albertel 2802: </font>
2803: <br />
2804: <font size="2" face="Arial, Helvetica, sans-serif">$role</font>
2805: <br />
2806: <font size="2" face="Arial, Helvetica, sans-serif">$realm</font>
2807: </td>
2808: ENDROLE
1.224 raeburn 2809: my $titleinfo = '<font face="Arial, Helvetica, sans-serif" size="+3" color="'.
1.229 albertel 2810: $font.'"><b>'.$title.'</b></font>';
1.224 raeburn 2811: if ($customtitle) {
2812: $titleinfo = $customtitle;
1.235 raeburn 2813: }
2814:
1.258 albertel 2815: if ($env{'request.state'} eq 'construct') {
1.229 albertel 2816: my ($uname,$thisdisfn)=
1.258 albertel 2817: ($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229 albertel 2818: my $formaction='/priv/'.$uname.'/'.$thisdisfn;
2819: $formaction=~s/\/+/\//g;
1.237 raeburn 2820: unless ($customtitle) { #this is for resources; directories have customtitle, and crumbs and select recent are created in lonpubdir.pm
2821: my $parentpath = '';
1.241 raeburn 2822: my $lastitem = '';
2823: if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
1.235 raeburn 2824: $parentpath = $1;
1.241 raeburn 2825: $lastitem = $2;
2826: } else {
2827: $lastitem = $thisdisfn;
1.235 raeburn 2828: }
2829: $titleinfo = &Apache::loncommon::help_open_menu('','','','',3,'Authoring').
2830: '<font face="Arial, Helvetica, sans-serif"><b>Construction Space</b>:</font> '.
2831: '<form name="dirs" method="post" action="'.$formaction
2832: .'" target="_top"><tt><b>'
1.241 raeburn 2833: .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
1.235 raeburn 2834: .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
2835: .'</form>'
2836: .&Apache::lonmenu::constspaceform();
1.229 albertel 2837:
1.235 raeburn 2838: }
2839: $forcereg=1;
2840: }
2841: my $titletable = '<table bgcolor="'.$pgbg.'" width="100%" border="0" '.
2842: 'cellspacing="3" cellpadding="3">'.
2843: '<tr><td rowspan="3" bgcolor="'.$tabbg.'">'.
2844: $titleinfo.'</td>'.$roleinfo.'</tr></table>';
1.258 albertel 2845: if ($env{'request.state'} eq 'construct') {
1.235 raeburn 2846: $bodytag .= &Apache::lonmenu::menubuttons($forcereg,'web',$forcereg,$titletable);
2847: } else {
2848: $bodytag .= &Apache::lonmenu::menubuttons($forcereg,'web',$forcereg).
2849: $titletable;
2850: }
2851: return $bodytag;
1.94 www 2852: }
1.95 www 2853:
1.93 www 2854: #
1.95 www 2855: # Top frame rendering, Remote is up
1.93 www 2856: #
1.224 raeburn 2857: my $titleinfo = ' <font size="5" face="Arial, Helvetica, sans-serif"><b>'.$title.'</b></font>';
2858: if ($customtitle) {
2859: $titleinfo = $customtitle;
2860: }
1.245 matthew 2861: #
2862: # Extra info if you are the DC
2863: my $dc_info = '';
1.258 albertel 2864: if ($env{'user.adv'} && exists($env{'user.role.dc./'.
2865: $env{'course.'.$env{'request.course.id'}.
1.245 matthew 2866: '.domain'}.'/'})) {
1.258 albertel 2867: my $cid = $env{'request.course.id'};
2868: $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.245 matthew 2869: $dc_info = '('.$dc_info.')';
2870: }
2871: #
1.94 www 2872: return(<<ENDBODY);
1.60 matthew 2873: $bodytag
1.55 www 2874: <table width="100%" cellspacing="0" border="0" cellpadding="0">
1.95 www 2875: <tr><td bgcolor="$sidebg">
1.94 www 2876: $upperleft</td>
1.95 www 2877: <td bgcolor="$sidebg" align="right">$messages </td>
1.55 www 2878: </tr>
1.54 www 2879: <tr>
1.55 www 2880: <td rowspan="3" bgcolor="$tabbg">
1.245 matthew 2881: $titleinfo $dc_info
1.257 matthew 2882: </td><td bgcolor="$tabbg" align="right">
1.146 www 2883: <font size="2" face="Arial, Helvetica, sans-serif">
1.258 albertel 2884: $env{'environment.firstname'}
2885: $env{'environment.middlename'}
2886: $env{'environment.lastname'}
2887: $env{'environment.generation'}
1.55 www 2888: </font>
1.54 www 2889: </td>
2890: </tr>
2891: <tr><td bgcolor="$tabbg" align="right">
1.146 www 2892: <font size="2" face="Arial, Helvetica, sans-serif">$role</font>
1.54 www 2893: </td></tr>
1.55 www 2894: <tr>
1.148 www 2895: <td bgcolor="$tabbg" align="right"><font size="2" face="Arial, Helvetica, sans-serif">$realm</font> </td></tr>
1.202 albertel 2896: </table><br />
1.54 www 2897: ENDBODY
1.182 matthew 2898: }
2899:
2900: ###############################################
1.251 albertel 2901: ###############################################
2902:
2903: =pod
2904:
2905: =back
2906:
2907: =head1 HTTP Helpers
2908:
2909: =over 4
2910:
2911: =item * &endbodytag()
2912:
2913: Returns a uniform footer for LON-CAPA web pages.
2914:
2915: Inputs:
2916:
2917: =over 4
2918:
2919: =back
2920:
2921: Returns: A uniform footer for LON-CAPA web pages.
2922:
2923: =cut
2924:
2925: sub endbodytag {
2926: my $endbodytag='</body>';
1.258 albertel 2927: if ($env{'environment.texengine'} eq 'jsMath') {
1.252 albertel 2928: $endbodytag='<script type="text/javascript">jsMath.Process()</script>'.
2929: "\n".$endbodytag;
2930: }
1.251 albertel 2931: return $endbodytag;
2932: }
2933:
2934: ###############################################
1.182 matthew 2935:
2936: =pod
2937:
2938: =item get_users_function
2939:
2940: Used by &bodytag to determine the current users primary role.
2941: Returns either 'student','coordinator','admin', or 'author'.
2942:
2943: =cut
2944:
2945: ###############################################
2946: sub get_users_function {
2947: my $function = 'student';
1.258 albertel 2948: if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
1.182 matthew 2949: $function='coordinator';
2950: }
1.258 albertel 2951: if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182 matthew 2952: $function='admin';
2953: }
1.258 albertel 2954: if (($env{'request.role'}=~/^(au|ca)/) ||
1.182 matthew 2955: ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
2956: $function='author';
2957: }
2958: return $function;
1.54 www 2959: }
1.99 www 2960:
2961: ###############################################
2962:
1.233 raeburn 2963: =pod
2964:
2965: =item get_sections
2966:
2967: Determines all the sections for a course including
2968: sections with students and sections containing other roles.
2969: Incoming parameters: domain, course number, reference to
2970: section hash (keys to be section/group IDs), reference to
2971: array containing roles for which sections should be gathered
2972: (optional). If the fourth argument is undefined, sections
2973: are gathered for any role.
2974:
2975: Returns number of sections.
2976:
2977: =cut
2978:
2979: ###############################################
2980: sub get_sections {
2981: my ($cdom,$cnum,$sectioncount,$possible_roles) = @_;
1.240 albertel 2982: if (!($cdom && $cnum)) { return 0; }
1.233 raeburn 2983: my $cid = $cdom.'_'.$cnum;
2984: my $numsections = 0;
1.240 albertel 2985:
2986: if (!defined($possible_roles) || (grep/^st$/,@$possible_roles)) {
2987: my ($classlist) = &Apache::loncoursedata::get_classlist($cid,$cdom,$cnum);
2988: my $sec_index = &Apache::loncoursedata::CL_SECTION();
2989: my $status_index = &Apache::loncoursedata::CL_STATUS();
2990: while (my ($student,$data) = each %$classlist) {
2991: my ($section,$status) = ($data->[$sec_index],
2992: $data->[$status_index]);
2993: unless ($section eq '-1' || $section =~ /^\s*$/) {
2994: if (!defined($$sectioncount{$section})) { $numsections++; }
2995: $$sectioncount{$section}++;
2996: }
2997: }
2998: }
2999: my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
3000: foreach my $user (sort(keys(%courseroles))) {
3001: if ($user !~ /^(\w{2})/) { next; }
3002: my ($role) = ($user =~ /^(\w{2})/);
3003: if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
3004: my $section;
3005: if ($role eq 'cr' &&
3006: $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
3007: $section=$1;
3008: }
3009: if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
3010: if (!defined($section) || $section eq '-1') { next; }
3011: if (!defined($$sectioncount{$section})) { $numsections++; }
3012: $$sectioncount{$section}++;
1.233 raeburn 3013: }
3014: return $numsections;
3015: }
3016:
3017:
1.99 www 3018: sub get_posted_cgi {
3019: my $r=shift;
3020:
3021: my $buffer;
1.260 albertel 3022: if ($r->header_in('Content-length')) {
3023: $r->read($buffer,$r->header_in('Content-length'),0);
3024: }
1.99 www 3025: unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
3026: my @pairs=split(/&/,$buffer);
3027: my $pair;
3028: foreach $pair (@pairs) {
3029: my ($name,$value) = split(/=/,$pair);
3030: $value =~ tr/+/ /;
3031: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
3032: $name =~ tr/+/ /;
3033: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
3034: &add_to_env("form.$name",$value);
3035: }
3036: } else {
3037: my $contentsep=$1;
3038: my @lines = split (/\n/,$buffer);
3039: my $name='';
3040: my $value='';
3041: my $fname='';
3042: my $fmime='';
3043: my $i;
3044: for ($i=0;$i<=$#lines;$i++) {
3045: if ($lines[$i]=~/^$contentsep/) {
3046: if ($name) {
3047: chomp($value);
3048: if ($fname) {
1.258 albertel 3049: $env{"form.$name.filename"}=$fname;
3050: $env{"form.$name.mimetype"}=$fmime;
1.99 www 3051: } else {
3052: $value=~s/\s+$//s;
3053: }
3054: &add_to_env("form.$name",$value);
3055: }
3056: if ($i<$#lines) {
3057: $i++;
3058: $lines[$i]=~
3059: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
3060: $name=$1;
3061: $value='';
3062: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
3063: $fname=$1;
3064: if
3065: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
3066: $fmime=$1;
3067: $i++;
3068: } else {
3069: $fmime='';
3070: }
3071: } else {
3072: $fname='';
3073: $fmime='';
3074: }
3075: $i++;
3076: }
3077: } else {
3078: $value.=$lines[$i]."\n";
3079: }
3080: }
3081: }
1.258 albertel 3082: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
1.99 www 3083: $r->method_number(M_GET);
3084: $r->method('GET');
3085: $r->headers_in->unset('Content-length');
3086: }
3087:
1.112 bowersj2 3088: =pod
3089:
3090: =item * get_unprocessed_cgi($query,$possible_names)
3091:
1.258 albertel 3092: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112 bowersj2 3093: $query. The parameters listed in $possible_names (an array reference),
1.258 albertel 3094: will be set in $env{'form.name'} if they do not already exist.
1.112 bowersj2 3095:
3096: Typically called with $ENV{'QUERY_STRING'} as the first parameter.
3097: $possible_names is an ref to an array of form element names. As an example:
3098: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258 albertel 3099: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112 bowersj2 3100:
3101: =cut
1.1 albertel 3102:
1.6 albertel 3103: sub get_unprocessed_cgi {
1.25 albertel 3104: my ($query,$possible_names)= @_;
1.26 matthew 3105: # $Apache::lonxml::debug=1;
1.16 harris41 3106: foreach (split(/&/,$query)) {
1.6 albertel 3107: my ($name, $value) = split(/=/,$_);
1.25 albertel 3108: $name = &Apache::lonnet::unescape($name);
3109: if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
3110: $value =~ tr/+/ /;
3111: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
3112: &Apache::lonxml::debug("Seting :$name: to :$value:");
1.258 albertel 3113: unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25 albertel 3114: }
1.16 harris41 3115: }
1.6 albertel 3116: }
3117:
1.112 bowersj2 3118: =pod
3119:
3120: =item * cacheheader()
3121:
3122: returns cache-controlling header code
3123:
3124: =cut
3125:
1.7 albertel 3126: sub cacheheader {
1.258 albertel 3127: unless ($env{'request.method'} eq 'GET') { return ''; }
1.216 albertel 3128: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
3129: my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7 albertel 3130: <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
3131: <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216 albertel 3132: return $output;
1.7 albertel 3133: }
3134:
1.112 bowersj2 3135: =pod
3136:
3137: =item * no_cache($r)
3138:
3139: specifies header code to not have cache
3140:
3141: =cut
3142:
1.9 albertel 3143: sub no_cache {
1.216 albertel 3144: my ($r) = @_;
3145: if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258 albertel 3146: $env{'request.method'} ne 'GET') { return ''; }
1.216 albertel 3147: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
3148: $r->no_cache(1);
3149: $r->header_out("Expires" => $date);
3150: $r->header_out("Pragma" => "no-cache");
1.123 www 3151: }
3152:
3153: sub content_type {
1.181 albertel 3154: my ($r,$type,$charset) = @_;
1.258 albertel 3155: if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181 albertel 3156: unless ($charset) {
3157: $charset=&Apache::lonlocal::current_encoding;
3158: }
3159: if ($charset) { $type.='; charset='.$charset; }
3160: if ($r) {
3161: $r->content_type($type);
3162: } else {
3163: print("Content-type: $type\n\n");
3164: }
1.9 albertel 3165: }
1.25 albertel 3166:
1.112 bowersj2 3167: =pod
3168:
3169: =item * add_to_env($name,$value)
3170:
1.258 albertel 3171: adds $name to the %env hash with value
1.112 bowersj2 3172: $value, if $name already exists, the entry is converted to an array
3173: reference and $value is added to the array.
3174:
3175: =cut
3176:
1.25 albertel 3177: sub add_to_env {
3178: my ($name,$value)=@_;
1.258 albertel 3179: if (defined($env{$name})) {
3180: if (ref($env{$name})) {
1.25 albertel 3181: #already have multiple values
1.258 albertel 3182: push(@{ $env{$name} },$value);
1.25 albertel 3183: } else {
3184: #first time seeing multiple values, convert hash entry to an arrayref
1.258 albertel 3185: my $first=$env{$name};
3186: undef($env{$name});
3187: push(@{ $env{$name} },$first,$value);
1.25 albertel 3188: }
3189: } else {
1.258 albertel 3190: $env{$name}=$value;
1.25 albertel 3191: }
1.31 albertel 3192: }
1.149 albertel 3193:
3194: =pod
3195:
3196: =item * get_env_multiple($name)
3197:
1.258 albertel 3198: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149 albertel 3199: values may be defined and end up as an array ref.
3200:
3201: returns an array of values
3202:
3203: =cut
3204:
3205: sub get_env_multiple {
3206: my ($name) = @_;
3207: my @values;
1.258 albertel 3208: if (defined($env{$name})) {
1.149 albertel 3209: # exists is it an array
1.258 albertel 3210: if (ref($env{$name})) {
3211: @values=@{ $env{$name} };
1.149 albertel 3212: } else {
1.258 albertel 3213: $values[0]=$env{$name};
1.149 albertel 3214: }
3215: }
3216: return(@values);
3217: }
3218:
1.31 albertel 3219:
1.41 ng 3220: =pod
1.45 matthew 3221:
3222: =back
1.41 ng 3223:
1.112 bowersj2 3224: =head1 CSV Upload/Handling functions
1.38 albertel 3225:
1.41 ng 3226: =over 4
3227:
1.112 bowersj2 3228: =item * upfile_store($r)
1.41 ng 3229:
3230: Store uploaded file, $r should be the HTTP Request object,
1.258 albertel 3231: needs $env{'form.upfile'}
1.41 ng 3232: returns $datatoken to be put into hidden field
3233:
3234: =cut
1.31 albertel 3235:
3236: sub upfile_store {
3237: my $r=shift;
1.258 albertel 3238: $env{'form.upfile'}=~s/\r/\n/gs;
3239: $env{'form.upfile'}=~s/\f/\n/gs;
3240: $env{'form.upfile'}=~s/\n+/\n/gs;
3241: $env{'form.upfile'}=~s/\n+$//gs;
1.31 albertel 3242:
1.258 albertel 3243: my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
3244: '_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31 albertel 3245: {
1.158 raeburn 3246: my $datafile = $r->dir_config('lonDaemons').
3247: '/tmp/'.$datatoken.'.tmp';
3248: if ( open(my $fh,">$datafile") ) {
1.258 albertel 3249: print $fh $env{'form.upfile'};
1.158 raeburn 3250: close($fh);
3251: }
1.31 albertel 3252: }
3253: return $datatoken;
3254: }
3255:
1.56 matthew 3256: =pod
3257:
1.112 bowersj2 3258: =item * load_tmp_file($r)
1.41 ng 3259:
3260: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258 albertel 3261: needs $env{'form.datatoken'},
3262: sets $env{'form.upfile'} to the contents of the file
1.41 ng 3263:
3264: =cut
1.31 albertel 3265:
3266: sub load_tmp_file {
3267: my $r=shift;
3268: my @studentdata=();
3269: {
1.158 raeburn 3270: my $studentfile = $r->dir_config('lonDaemons').
1.258 albertel 3271: '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158 raeburn 3272: if ( open(my $fh,"<$studentfile") ) {
3273: @studentdata=<$fh>;
3274: close($fh);
3275: }
1.31 albertel 3276: }
1.258 albertel 3277: $env{'form.upfile'}=join('',@studentdata);
1.31 albertel 3278: }
3279:
1.56 matthew 3280: =pod
3281:
1.112 bowersj2 3282: =item * upfile_record_sep()
1.41 ng 3283:
3284: Separate uploaded file into records
3285: returns array of records,
1.258 albertel 3286: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41 ng 3287:
3288: =cut
1.31 albertel 3289:
3290: sub upfile_record_sep {
1.258 albertel 3291: if ($env{'form.upfiletype'} eq 'xml') {
1.31 albertel 3292: } else {
1.248 albertel 3293: my @records;
1.258 albertel 3294: foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248 albertel 3295: if ($line=~/^\s*$/) { next; }
3296: push(@records,$line);
3297: }
3298: return @records;
1.31 albertel 3299: }
3300: }
3301:
1.56 matthew 3302: =pod
3303:
1.112 bowersj2 3304: =item * record_sep($record)
1.41 ng 3305:
1.258 albertel 3306: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41 ng 3307:
3308: =cut
3309:
1.263 www 3310: sub takeleft {
3311: my $index=shift;
3312: return substr('0000'.$index,-4,4);
3313: }
3314:
1.31 albertel 3315: sub record_sep {
3316: my $record=shift;
3317: my %components=();
1.258 albertel 3318: if ($env{'form.upfiletype'} eq 'xml') {
3319: } elsif ($env{'form.upfiletype'} eq 'space') {
1.31 albertel 3320: my $i=0;
3321: foreach (split(/\s+/,$record)) {
3322: my $field=$_;
3323: $field=~s/^(\"|\')//;
3324: $field=~s/(\"|\')$//;
1.263 www 3325: $components{&takeleft($i)}=$field;
1.31 albertel 3326: $i++;
3327: }
1.258 albertel 3328: } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31 albertel 3329: my $i=0;
1.171 matthew 3330: foreach (split(/\t/,$record)) {
1.31 albertel 3331: my $field=$_;
3332: $field=~s/^(\"|\')//;
3333: $field=~s/(\"|\')$//;
1.263 www 3334: $components{&takeleft($i)}=$field;
1.31 albertel 3335: $i++;
3336: }
3337: } else {
3338: my @allfields=split(/\,/,$record);
3339: my $i=0;
3340: my $j;
3341: for ($j=0;$j<=$#allfields;$j++) {
3342: my $field=$allfields[$j];
3343: if ($field=~/^\s*(\"|\')/) {
3344: my $delimiter=$1;
3345: while (($field!~/$delimiter$/) && ($j<$#allfields)) {
3346: $j++;
3347: $field.=','.$allfields[$j];
3348: }
3349: $field=~s/^\s*$delimiter//;
3350: $field=~s/$delimiter\s*$//;
3351: }
1.263 www 3352: $components{&takeleft($i)}=$field;
1.31 albertel 3353: $i++;
3354: }
3355: }
3356: return %components;
3357: }
3358:
1.144 matthew 3359: ######################################################
3360: ######################################################
3361:
1.56 matthew 3362: =pod
3363:
1.112 bowersj2 3364: =item * upfile_select_html()
1.41 ng 3365:
1.144 matthew 3366: Return HTML code to select a file from the users machine and specify
3367: the file type.
1.41 ng 3368:
3369: =cut
3370:
1.144 matthew 3371: ######################################################
3372: ######################################################
1.31 albertel 3373: sub upfile_select_html {
1.144 matthew 3374: my %Types = (
3375: csv => &mt('CSV (comma separated values, spreadsheet)'),
3376: space => &mt('Space separated'),
3377: tab => &mt('Tabulator separated'),
3378: # xml => &mt('HTML/XML'),
3379: );
3380: my $Str = '<input type="file" name="upfile" size="50" />'.
3381: '<br />Type: <select name="upfiletype">';
3382: foreach my $type (sort(keys(%Types))) {
3383: $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
3384: }
3385: $Str .= "</select>\n";
3386: return $Str;
1.31 albertel 3387: }
3388:
1.144 matthew 3389: ######################################################
3390: ######################################################
3391:
1.56 matthew 3392: =pod
3393:
1.112 bowersj2 3394: =item * csv_print_samples($r,$records)
1.41 ng 3395:
3396: Prints a table of sample values from each column uploaded $r is an
3397: Apache Request ref, $records is an arrayref from
3398: &Apache::loncommon::upfile_record_sep
3399:
3400: =cut
3401:
1.144 matthew 3402: ######################################################
3403: ######################################################
1.31 albertel 3404: sub csv_print_samples {
3405: my ($r,$records) = @_;
3406: my (%sone,%stwo,%sthree);
3407: %sone=&record_sep($$records[0]);
3408: if (defined($$records[1])) {%stwo=&record_sep($$records[1]);}
3409: if (defined($$records[2])) {%sthree=&record_sep($$records[2]);}
1.144 matthew 3410: #
3411: $r->print(&mt('Samples').'<br /><table border="2"><tr>');
3412: foreach (sort({$a <=> $b} keys(%sone))) {
3413: $r->print('<th>'.&mt('Column [_1]',($_+1)).'</th>'); }
1.31 albertel 3414: $r->print('</tr>');
3415: foreach my $hash (\%sone,\%stwo,\%sthree) {
3416: $r->print('<tr>');
3417: foreach (sort({$a <=> $b} keys(%sone))) {
3418: $r->print('<td>');
3419: if (defined($$hash{$_})) { $r->print($$hash{$_}); }
3420: $r->print('</td>');
3421: }
3422: $r->print('</tr>');
3423: }
3424: $r->print('</tr></table><br />'."\n");
3425: }
3426:
1.144 matthew 3427: ######################################################
3428: ######################################################
3429:
1.56 matthew 3430: =pod
3431:
1.112 bowersj2 3432: =item * csv_print_select_table($r,$records,$d)
1.41 ng 3433:
3434: Prints a table to create associations between values and table columns.
1.144 matthew 3435:
1.41 ng 3436: $r is an Apache Request ref,
3437: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174 matthew 3438: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41 ng 3439:
3440: =cut
3441:
1.144 matthew 3442: ######################################################
3443: ######################################################
1.31 albertel 3444: sub csv_print_select_table {
3445: my ($r,$records,$d) = @_;
3446: my $i=0;my %sone;
3447: %sone=&record_sep($$records[0]);
1.144 matthew 3448: $r->print(&mt('Associate columns with student attributes.')."\n".
3449: '<table border="2"><tr>'.
3450: '<th>'.&mt('Attribute').'</th>'.
3451: '<th>'.&mt('Column').'</th></tr>'."\n");
1.31 albertel 3452: foreach (@$d) {
1.174 matthew 3453: my ($value,$display,$defaultcol)=@{ $_ };
1.31 albertel 3454: $r->print('<tr><td>'.$display.'</td>');
3455:
3456: $r->print('<td><select name=f'.$i.
1.32 matthew 3457: ' onchange="javascript:flip(this.form,'.$i.');">');
1.31 albertel 3458: $r->print('<option value="none"></option>');
3459: foreach (sort({$a <=> $b} keys(%sone))) {
1.174 matthew 3460: $r->print('<option value="'.$_.'"'.
1.253 albertel 3461: ($_ eq $defaultcol ? ' selected="selected" ' : '').
1.174 matthew 3462: '>Column '.($_+1).'</option>');
1.31 albertel 3463: }
3464: $r->print('</select></td></tr>'."\n");
3465: $i++;
3466: }
3467: $i--;
3468: return $i;
3469: }
1.56 matthew 3470:
1.144 matthew 3471: ######################################################
3472: ######################################################
3473:
1.56 matthew 3474: =pod
1.31 albertel 3475:
1.112 bowersj2 3476: =item * csv_samples_select_table($r,$records,$d)
1.41 ng 3477:
3478: Prints a table of sample values from the upload and can make associate samples to internal names.
3479:
3480: $r is an Apache Request ref,
3481: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
3482: $d is an array of 2 element arrays (internal name, displayed name)
3483:
3484: =cut
3485:
1.144 matthew 3486: ######################################################
3487: ######################################################
1.31 albertel 3488: sub csv_samples_select_table {
3489: my ($r,$records,$d) = @_;
3490: my %sone; my %stwo; my %sthree;
3491: my $i=0;
1.144 matthew 3492: #
3493: $r->print('<table border=2><tr><th>'.
3494: &mt('Field').'</th><th>'.&mt('Samples').'</th></tr>');
1.31 albertel 3495: %sone=&record_sep($$records[0]);
3496: if (defined($$records[1])) {%stwo=&record_sep($$records[1]);}
3497: if (defined($$records[2])) {%sthree=&record_sep($$records[2]);}
1.144 matthew 3498: #
1.31 albertel 3499: foreach (sort keys %sone) {
1.144 matthew 3500: $r->print('<tr><td><select name="f'.$i.'"'.
1.32 matthew 3501: ' onchange="javascript:flip(this.form,'.$i.');">');
1.31 albertel 3502: foreach (@$d) {
1.174 matthew 3503: my ($value,$display,$defaultcol)=@{ $_ };
3504: $r->print('<option value="'.$value.'"'.
1.253 albertel 3505: ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174 matthew 3506: $display.'</option>');
1.31 albertel 3507: }
3508: $r->print('</select></td><td>');
1.263 www 3509: if (defined($sone{$_})) { $r->print($sone{$_}."<br />\n"); }
3510: if (defined($stwo{$_})) { $r->print($stwo{$_}."<br />\n"); }
3511: if (defined($sthree{$_})) { $r->print($sthree{$_}."<br />\n"); }
1.31 albertel 3512: $r->print('</td></tr>');
3513: $i++;
3514: }
3515: $i--;
3516: return($i);
1.115 matthew 3517: }
3518:
1.144 matthew 3519: ######################################################
3520: ######################################################
3521:
1.115 matthew 3522: =pod
3523:
3524: =item clean_excel_name($name)
3525:
3526: Returns a replacement for $name which does not contain any illegal characters.
3527:
3528: =cut
3529:
1.144 matthew 3530: ######################################################
3531: ######################################################
1.115 matthew 3532: sub clean_excel_name {
3533: my ($name) = @_;
3534: $name =~ s/[:\*\?\/\\]//g;
3535: if (length($name) > 31) {
3536: $name = substr($name,0,31);
3537: }
3538: return $name;
1.25 albertel 3539: }
1.84 albertel 3540:
1.85 albertel 3541: =pod
3542:
1.112 bowersj2 3543: =item * check_if_partid_hidden($id,$symb,$udom,$uname)
1.85 albertel 3544:
3545: Returns either 1 or undef
3546:
3547: 1 if the part is to be hidden, undef if it is to be shown
3548:
3549: Arguments are:
3550:
3551: $id the id of the part to be checked
3552: $symb, optional the symb of the resource to check
3553: $udom, optional the domain of the user to check for
3554: $uname, optional the username of the user to check for
3555:
3556: =cut
1.84 albertel 3557:
3558: sub check_if_partid_hidden {
3559: my ($id,$symb,$udom,$uname) = @_;
1.133 albertel 3560: my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84 albertel 3561: $symb,$udom,$uname);
1.141 albertel 3562: my $truth=1;
3563: #if the string starts with !, then the list is the list to show not hide
3564: if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84 albertel 3565: my @hiddenlist=split(/,/,$hiddenparts);
3566: foreach my $checkid (@hiddenlist) {
1.141 albertel 3567: if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84 albertel 3568: }
1.141 albertel 3569: return !$truth;
1.84 albertel 3570: }
1.127 matthew 3571:
1.138 matthew 3572:
3573: ############################################################
3574: ############################################################
3575:
3576: =pod
3577:
1.157 matthew 3578: =back
3579:
1.138 matthew 3580: =head1 cgi-bin script and graphing routines
3581:
1.157 matthew 3582: =over 4
3583:
1.138 matthew 3584: =item get_cgi_id
3585:
3586: Inputs: none
3587:
3588: Returns an id which can be used to pass environment variables
3589: to various cgi-bin scripts. These environment variables will
3590: be removed from the users environment after a given time by
3591: the routine &Apache::lonnet::transfer_profile_to_env.
3592:
3593: =cut
3594:
3595: ############################################################
3596: ############################################################
1.152 albertel 3597: my $uniq=0;
1.136 matthew 3598: sub get_cgi_id {
1.154 albertel 3599: $uniq=($uniq+1)%100000;
1.152 albertel 3600: return (time.'_'.$uniq);
1.136 matthew 3601: }
3602:
1.127 matthew 3603: ############################################################
3604: ############################################################
3605:
3606: =pod
3607:
1.134 matthew 3608: =item DrawBarGraph
1.127 matthew 3609:
1.138 matthew 3610: Facilitates the plotting of data in a (stacked) bar graph.
3611: Puts plot definition data into the users environment in order for
3612: graph.png to plot it. Returns an <img> tag for the plot.
3613: The bars on the plot are labeled '1','2',...,'n'.
3614:
3615: Inputs:
3616:
3617: =over 4
3618:
3619: =item $Title: string, the title of the plot
3620:
3621: =item $xlabel: string, text describing the X-axis of the plot
3622:
3623: =item $ylabel: string, text describing the Y-axis of the plot
3624:
3625: =item $Max: scalar, the maximum Y value to use in the plot
3626: If $Max is < any data point, the graph will not be rendered.
3627:
1.140 matthew 3628: =item $colors: array ref holding the colors to be used for the data sets when
1.138 matthew 3629: they are plotted. If undefined, default values will be used.
3630:
1.178 matthew 3631: =item $labels: array ref holding the labels to use on the x-axis for the bars.
3632:
1.138 matthew 3633: =item @Values: An array of array references. Each array reference holds data
3634: to be plotted in a stacked bar chart.
3635:
1.239 matthew 3636: =item If the final element of @Values is a hash reference the key/value
3637: pairs will be added to the graph definition.
3638:
1.138 matthew 3639: =back
3640:
3641: Returns:
3642:
3643: An <img> tag which references graph.png and the appropriate identifying
3644: information for the plot.
3645:
1.127 matthew 3646: =cut
3647:
3648: ############################################################
3649: ############################################################
1.134 matthew 3650: sub DrawBarGraph {
1.178 matthew 3651: my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134 matthew 3652: #
3653: if (! defined($colors)) {
3654: $colors = ['#33ff00',
3655: '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
3656: '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
3657: ];
3658: }
1.228 matthew 3659: my $extra_settings = {};
3660: if (ref($Values[-1]) eq 'HASH') {
3661: $extra_settings = pop(@Values);
3662: }
1.127 matthew 3663: #
1.136 matthew 3664: my $identifier = &get_cgi_id();
3665: my $id = 'cgi.'.$identifier;
1.129 matthew 3666: if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127 matthew 3667: return '';
3668: }
1.225 matthew 3669: #
3670: my @Labels;
3671: if (defined($labels)) {
3672: @Labels = @$labels;
3673: } else {
3674: for (my $i=0;$i<@{$Values[0]};$i++) {
3675: push (@Labels,$i+1);
3676: }
3677: }
3678: #
1.129 matthew 3679: my $NumBars = scalar(@{$Values[0]});
1.225 matthew 3680: if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129 matthew 3681: my %ValuesHash;
3682: my $NumSets=1;
3683: foreach my $array (@Values) {
3684: next if (! ref($array));
1.136 matthew 3685: $ValuesHash{$id.'.data.'.$NumSets++} =
1.132 matthew 3686: join(',',@$array);
1.129 matthew 3687: }
1.127 matthew 3688: #
1.136 matthew 3689: my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225 matthew 3690: if ($NumBars < 3) {
3691: $width = 120+$NumBars*32;
1.220 matthew 3692: $xskip = 1;
1.225 matthew 3693: $bar_width = 30;
3694: } elsif ($NumBars < 5) {
3695: $width = 120+$NumBars*20;
3696: $xskip = 1;
3697: $bar_width = 20;
1.220 matthew 3698: } elsif ($NumBars < 10) {
1.136 matthew 3699: $width = 120+$NumBars*15;
3700: $xskip = 1;
3701: $bar_width = 15;
3702: } elsif ($NumBars <= 25) {
3703: $width = 120+$NumBars*11;
3704: $xskip = 5;
3705: $bar_width = 8;
3706: } elsif ($NumBars <= 50) {
3707: $width = 120+$NumBars*8;
3708: $xskip = 5;
3709: $bar_width = 4;
3710: } else {
3711: $width = 120+$NumBars*8;
3712: $xskip = 5;
3713: $bar_width = 4;
3714: }
3715: #
1.137 matthew 3716: $Max = 1 if ($Max < 1);
3717: if ( int($Max) < $Max ) {
3718: $Max++;
3719: $Max = int($Max);
3720: }
1.127 matthew 3721: $Title = '' if (! defined($Title));
3722: $xlabel = '' if (! defined($xlabel));
3723: $ylabel = '' if (! defined($ylabel));
1.136 matthew 3724: $ValuesHash{$id.'.title'} = &Apache::lonnet::escape($Title);
3725: $ValuesHash{$id.'.xlabel'} = &Apache::lonnet::escape($xlabel);
3726: $ValuesHash{$id.'.ylabel'} = &Apache::lonnet::escape($ylabel);
1.137 matthew 3727: $ValuesHash{$id.'.y_max_value'} = $Max;
1.136 matthew 3728: $ValuesHash{$id.'.NumBars'} = $NumBars;
3729: $ValuesHash{$id.'.NumSets'} = $NumSets;
3730: $ValuesHash{$id.'.PlotType'} = 'bar';
3731: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
3732: $ValuesHash{$id.'.height'} = $height;
3733: $ValuesHash{$id.'.width'} = $width;
3734: $ValuesHash{$id.'.xskip'} = $xskip;
3735: $ValuesHash{$id.'.bar_width'} = $bar_width;
3736: $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127 matthew 3737: #
1.228 matthew 3738: # Deal with other parameters
3739: while (my ($key,$value) = each(%$extra_settings)) {
3740: $ValuesHash{$id.'.'.$key} = $value;
3741: }
3742: #
1.137 matthew 3743: &Apache::lonnet::appenv(%ValuesHash);
3744: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
3745: }
3746:
3747: ############################################################
3748: ############################################################
3749:
3750: =pod
3751:
3752: =item DrawXYGraph
3753:
1.138 matthew 3754: Facilitates the plotting of data in an XY graph.
3755: Puts plot definition data into the users environment in order for
3756: graph.png to plot it. Returns an <img> tag for the plot.
3757:
3758: Inputs:
3759:
3760: =over 4
3761:
3762: =item $Title: string, the title of the plot
3763:
3764: =item $xlabel: string, text describing the X-axis of the plot
3765:
3766: =item $ylabel: string, text describing the Y-axis of the plot
3767:
3768: =item $Max: scalar, the maximum Y value to use in the plot
3769: If $Max is < any data point, the graph will not be rendered.
3770:
3771: =item $colors: Array ref containing the hex color codes for the data to be
3772: plotted in. If undefined, default values will be used.
3773:
3774: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
3775:
3776: =item $Ydata: Array ref containing Array refs.
1.185 www 3777: Each of the contained arrays will be plotted as a separate curve.
1.138 matthew 3778:
3779: =item %Values: hash indicating or overriding any default values which are
3780: passed to graph.png.
3781: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
3782:
3783: =back
3784:
3785: Returns:
3786:
3787: An <img> tag which references graph.png and the appropriate identifying
3788: information for the plot.
3789:
1.137 matthew 3790: =cut
3791:
3792: ############################################################
3793: ############################################################
3794: sub DrawXYGraph {
3795: my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
3796: #
3797: # Create the identifier for the graph
3798: my $identifier = &get_cgi_id();
3799: my $id = 'cgi.'.$identifier;
3800: #
3801: $Title = '' if (! defined($Title));
3802: $xlabel = '' if (! defined($xlabel));
3803: $ylabel = '' if (! defined($ylabel));
3804: my %ValuesHash =
3805: (
3806: $id.'.title' => &Apache::lonnet::escape($Title),
3807: $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
3808: $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
3809: $id.'.y_max_value'=> $Max,
3810: $id.'.labels' => join(',',@$Xlabels),
3811: $id.'.PlotType' => 'XY',
3812: );
3813: #
3814: if (defined($colors) && ref($colors) eq 'ARRAY') {
3815: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
3816: }
3817: #
3818: if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
3819: return '';
3820: }
3821: my $NumSets=1;
1.138 matthew 3822: foreach my $array (@{$Ydata}){
1.137 matthew 3823: next if (! ref($array));
3824: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
3825: }
1.138 matthew 3826: $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137 matthew 3827: #
3828: # Deal with other parameters
3829: while (my ($key,$value) = each(%Values)) {
3830: $ValuesHash{$id.'.'.$key} = $value;
1.127 matthew 3831: }
3832: #
1.136 matthew 3833: &Apache::lonnet::appenv(%ValuesHash);
3834: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
3835: }
3836:
3837: ############################################################
3838: ############################################################
3839:
3840: =pod
3841:
1.138 matthew 3842: =item DrawXYYGraph
3843:
3844: Facilitates the plotting of data in an XY graph with two Y axes.
3845: Puts plot definition data into the users environment in order for
3846: graph.png to plot it. Returns an <img> tag for the plot.
3847:
3848: Inputs:
3849:
3850: =over 4
3851:
3852: =item $Title: string, the title of the plot
3853:
3854: =item $xlabel: string, text describing the X-axis of the plot
3855:
3856: =item $ylabel: string, text describing the Y-axis of the plot
3857:
3858: =item $colors: Array ref containing the hex color codes for the data to be
3859: plotted in. If undefined, default values will be used.
3860:
3861: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
3862:
3863: =item $Ydata1: The first data set
3864:
3865: =item $Min1: The minimum value of the left Y-axis
3866:
3867: =item $Max1: The maximum value of the left Y-axis
3868:
3869: =item $Ydata2: The second data set
3870:
3871: =item $Min2: The minimum value of the right Y-axis
3872:
3873: =item $Max2: The maximum value of the left Y-axis
3874:
3875: =item %Values: hash indicating or overriding any default values which are
3876: passed to graph.png.
3877: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
3878:
3879: =back
3880:
3881: Returns:
3882:
3883: An <img> tag which references graph.png and the appropriate identifying
3884: information for the plot.
1.136 matthew 3885:
3886: =cut
3887:
3888: ############################################################
3889: ############################################################
1.137 matthew 3890: sub DrawXYYGraph {
3891: my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
3892: $Ydata2,$Min2,$Max2,%Values)=@_;
1.136 matthew 3893: #
3894: # Create the identifier for the graph
3895: my $identifier = &get_cgi_id();
3896: my $id = 'cgi.'.$identifier;
3897: #
3898: $Title = '' if (! defined($Title));
3899: $xlabel = '' if (! defined($xlabel));
3900: $ylabel = '' if (! defined($ylabel));
3901: my %ValuesHash =
3902: (
3903: $id.'.title' => &Apache::lonnet::escape($Title),
3904: $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
3905: $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
3906: $id.'.labels' => join(',',@$Xlabels),
3907: $id.'.PlotType' => 'XY',
3908: $id.'.NumSets' => 2,
1.137 matthew 3909: $id.'.two_axes' => 1,
3910: $id.'.y1_max_value' => $Max1,
3911: $id.'.y1_min_value' => $Min1,
3912: $id.'.y2_max_value' => $Max2,
3913: $id.'.y2_min_value' => $Min2,
1.136 matthew 3914: );
3915: #
1.137 matthew 3916: if (defined($colors) && ref($colors) eq 'ARRAY') {
3917: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
3918: }
3919: #
3920: if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
3921: ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136 matthew 3922: return '';
3923: }
3924: my $NumSets=1;
1.137 matthew 3925: foreach my $array ($Ydata1,$Ydata2){
1.136 matthew 3926: next if (! ref($array));
3927: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137 matthew 3928: }
3929: #
3930: # Deal with other parameters
3931: while (my ($key,$value) = each(%Values)) {
3932: $ValuesHash{$id.'.'.$key} = $value;
1.136 matthew 3933: }
3934: #
3935: &Apache::lonnet::appenv(%ValuesHash);
1.130 albertel 3936: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139 matthew 3937: }
3938:
3939: ############################################################
3940: ############################################################
3941:
3942: =pod
3943:
1.157 matthew 3944: =back
3945:
1.139 matthew 3946: =head1 Statistics helper routines?
3947:
3948: Bad place for them but what the hell.
3949:
1.157 matthew 3950: =over 4
3951:
1.139 matthew 3952: =item &chartlink
3953:
3954: Returns a link to the chart for a specific student.
3955:
3956: Inputs:
3957:
3958: =over 4
3959:
3960: =item $linktext: The text of the link
3961:
3962: =item $sname: The students username
3963:
3964: =item $sdomain: The students domain
3965:
3966: =back
3967:
1.157 matthew 3968: =back
3969:
1.139 matthew 3970: =cut
3971:
3972: ############################################################
3973: ############################################################
3974: sub chartlink {
3975: my ($linktext, $sname, $sdomain) = @_;
3976: my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.219 albertel 3977: '&SelectedStudent='.&Apache::lonnet::escape($sname.':'.$sdomain).
3978: '&chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139 matthew 3979: '">'.$linktext.'</a>';
1.153 matthew 3980: }
3981:
3982: #######################################################
3983: #######################################################
3984:
3985: =pod
3986:
3987: =head1 Course Environment Routines
1.157 matthew 3988:
3989: =over 4
1.153 matthew 3990:
3991: =item &restore_course_settings
3992:
3993: =item &store_course_settings
3994:
3995: Restores/Store indicated form parameters from the course environment.
3996: Will not overwrite existing values of the form parameters.
3997:
3998: Inputs:
3999: a scalar describing the data (e.g. 'chart', 'problem_analysis')
4000:
4001: a hash ref describing the data to be stored. For example:
4002:
4003: %Save_Parameters = ('Status' => 'scalar',
4004: 'chartoutputmode' => 'scalar',
4005: 'chartoutputdata' => 'scalar',
4006: 'Section' => 'array',
4007: 'StudentData' => 'array',
4008: 'Maps' => 'array');
4009:
4010: Returns: both routines return nothing
4011:
4012: =cut
4013:
4014: #######################################################
4015: #######################################################
4016: sub store_course_settings {
4017: # save to the environment
4018: # appenv the same items, just to be safe
1.258 albertel 4019: my $courseid = $env{'request.course.id'};
4020: my $coursedom = $env{'course.'.$courseid.'.domain'};
1.153 matthew 4021: my ($prefix,$Settings) = @_;
4022: my %SaveHash;
4023: my %AppHash;
4024: while (my ($setting,$type) = each(%$Settings)) {
1.176 albertel 4025: my $basename = 'internal.'.$prefix.'.'.$setting;
1.153 matthew 4026: my $envname = 'course.'.$courseid.'.'.$basename;
1.258 albertel 4027: if (exists($env{'form.'.$setting})) {
1.153 matthew 4028: # Save this value away
4029: if ($type eq 'scalar' &&
1.258 albertel 4030: (! exists($env{$envname}) ||
4031: $env{$envname} ne $env{'form.'.$setting})) {
4032: $SaveHash{$basename} = $env{'form.'.$setting};
4033: $AppHash{$envname} = $env{'form.'.$setting};
1.153 matthew 4034: } elsif ($type eq 'array') {
4035: my $stored_form;
1.258 albertel 4036: if (ref($env{'form.'.$setting})) {
1.153 matthew 4037: $stored_form = join(',',
4038: map {
4039: &Apache::lonnet::escape($_);
1.258 albertel 4040: } sort(@{$env{'form.'.$setting}}));
1.153 matthew 4041: } else {
4042: $stored_form =
1.258 albertel 4043: &Apache::lonnet::escape($env{'form.'.$setting});
1.153 matthew 4044: }
4045: # Determine if the array contents are the same.
1.258 albertel 4046: if ($stored_form ne $env{$envname}) {
1.153 matthew 4047: $SaveHash{$basename} = $stored_form;
4048: $AppHash{$envname} = $stored_form;
4049: }
4050: }
4051: }
4052: }
4053: my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
4054: $coursedom,
1.258 albertel 4055: $env{'course.'.$courseid.'.num'});
1.153 matthew 4056: if ($put_result !~ /^(ok|delayed)/) {
4057: &Apache::lonnet::logthis('unable to save form parameters, '.
4058: 'got error:'.$put_result);
4059: }
4060: # Make sure these settings stick around in this session, too
4061: &Apache::lonnet::appenv(%AppHash);
4062: return;
4063: }
4064:
4065: sub restore_course_settings {
1.258 albertel 4066: my $courseid = $env{'request.course.id'};
1.153 matthew 4067: my ($prefix,$Settings) = @_;
4068: while (my ($setting,$type) = each(%$Settings)) {
1.258 albertel 4069: next if (exists($env{'form.'.$setting}));
1.176 albertel 4070: my $envname = 'course.'.$courseid.'.internal.'.$prefix.
1.153 matthew 4071: '.'.$setting;
1.258 albertel 4072: if (exists($env{$envname})) {
1.153 matthew 4073: if ($type eq 'scalar') {
1.258 albertel 4074: $env{'form.'.$setting} = $env{$envname};
1.153 matthew 4075: } elsif ($type eq 'array') {
1.258 albertel 4076: $env{'form.'.$setting} = [
1.153 matthew 4077: map {
4078: &Apache::lonnet::unescape($_);
1.258 albertel 4079: } split(',',$env{$envname})
1.153 matthew 4080: ];
4081: }
4082: }
4083: }
1.127 matthew 4084: }
4085:
4086: ############################################################
4087: ############################################################
1.154 albertel 4088:
4089: sub propath {
4090: my ($udom,$uname)=@_;
4091: $udom=~s/\W//g;
4092: $uname=~s/\W//g;
4093: my $subdir=$uname.'__';
4094: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
4095: my $proname="$Apache::lonnet::perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
4096: return $proname;
1.156 albertel 4097: }
4098:
4099: sub icon {
4100: my ($file)=@_;
1.168 albertel 4101: my $curfext = (split(/\./,$file))[-1];
4102: my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156 albertel 4103: my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168 albertel 4104: if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
4105: if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
4106: $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
4107: $curfext.".gif") {
4108: $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
4109: $curfext.".gif";
4110: }
4111: }
1.249 albertel 4112: return &lonhttpdurl($iconname);
1.154 albertel 4113: }
1.84 albertel 4114:
1.215 albertel 4115: sub lonhttpdurl {
4116: my ($url)=@_;
4117: my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'};
4118: if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; }
4119: return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url;
4120: }
4121:
1.213 albertel 4122: sub connection_aborted {
4123: my ($r)=@_;
4124: $r->print(" ");$r->rflush();
4125: my $c = $r->connection;
4126: return $c->aborted();
4127: }
4128:
1.221 foxr 4129: # Escapes strings that may have embedded 's that will be put into
1.222 foxr 4130: # strings as 'strings'.
4131: sub escape_single {
1.221 foxr 4132: my ($input) = @_;
1.223 albertel 4133: $input =~ s/\\/\\\\/g; # Escape the \'s..(must be first)>
1.221 foxr 4134: $input =~ s/\'/\\\'/g; # Esacpe the 's....
4135: return $input;
4136: }
1.223 albertel 4137:
1.222 foxr 4138: # Same as escape_single, but escape's "'s This
4139: # can be used for "strings"
4140: sub escape_double {
4141: my ($input) = @_;
4142: $input =~ s/\\/\\\\/g; # Escape the /'s..(must be first)>
4143: $input =~ s/\"/\\\"/g; # Esacpe the "s....
4144: return $input;
4145: }
1.223 albertel 4146:
1.222 foxr 4147: # Escapes the last element of a full URL.
4148: sub escape_url {
4149: my ($url) = @_;
1.238 raeburn 4150: my @urlslices = split(/\//, $url,-1);
1.223 albertel 4151: my $lastitem = &Apache::lonnet::escape(pop(@urlslices));
4152: return join('/',@urlslices).'/'.$lastitem;
1.222 foxr 4153: }
1.41 ng 4154: =pod
4155:
4156: =back
4157:
1.112 bowersj2 4158: =cut
1.41 ng 4159:
1.112 bowersj2 4160: 1;
4161: __END__;
1.41 ng 4162:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>