Annotation of loncom/interface/loncommon.pm, revision 1.397
1.10 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # a pile of common routines
1.10 albertel 3: #
1.397 ! albertel 4: # $Id: loncommon.pm,v 1.396 2006/06/23 05:56:35 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.82 www 61: use Apache::lonmenu();
1.117 www 62: use Apache::lonlocal;
1.139 matthew 63: use HTML::Entities;
1.334 albertel 64: use Apache::lonhtmlcommon();
65: use Apache::loncoursedata();
1.344 albertel 66: use Apache::lontexconvert();
1.369 www 67: use LONCAPA;
1.117 www 68:
1.22 www 69: my $readit;
70:
1.157 matthew 71: ##
72: ## Global Variables
73: ##
1.46 matthew 74:
1.20 www 75: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12 harris41 76: my %language;
1.124 www 77: my %supported_language;
1.12 harris41 78: my %cprtag;
1.192 taceyjo1 79: my %scprtag;
1.351 www 80: my %fe; my %fd; my %fm;
1.41 ng 81: my %category_extensions;
1.12 harris41 82:
1.63 www 83: # ---------------------------------------------- Designs
84:
85: my %designhash;
86:
1.46 matthew 87: # ---------------------------------------------- Thesaurus variables
1.144 matthew 88: #
89: # %Keywords:
90: # A hash used by &keyword to determine if a word is considered a keyword.
91: # $thesaurus_db_file
92: # Scalar containing the full path to the thesaurus database.
1.46 matthew 93:
94: my %Keywords;
95: my $thesaurus_db_file;
96:
1.144 matthew 97: #
98: # Initialize values from language.tab, copyright.tab, filetypes.tab,
99: # thesaurus.tab, and filecategories.tab.
100: #
1.18 www 101: BEGIN {
1.46 matthew 102: # Variable initialization
103: $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
104: #
1.22 www 105: unless ($readit) {
1.12 harris41 106: # ------------------------------------------------------------------- languages
107: {
1.158 raeburn 108: my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
109: '/language.tab';
110: if ( open(my $fh,"<$langtabfile") ) {
1.356 albertel 111: while (my $line = <$fh>) {
112: next if ($line=~/^\#/);
113: chomp($line);
114: my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
1.158 raeburn 115: $language{$key}=$val.' - '.$enc;
116: if ($sup) {
117: $supported_language{$key}=$sup;
118: }
119: }
120: close($fh);
121: }
1.12 harris41 122: }
123: # ------------------------------------------------------------------ copyrights
124: {
1.158 raeburn 125: my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
126: '/copyright.tab';
127: if ( open (my $fh,"<$copyrightfile") ) {
1.356 albertel 128: while (my $line = <$fh>) {
129: next if ($line=~/^\#/);
130: chomp($line);
131: my ($key,$val)=(split(/\s+/,$line,2));
1.158 raeburn 132: $cprtag{$key}=$val;
133: }
134: close($fh);
135: }
1.12 harris41 136: }
1.351 www 137: # ----------------------------------------------------------- source copyrights
1.192 taceyjo1 138: {
139: my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
140: '/source_copyright.tab';
141: if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356 albertel 142: while (my $line = <$fh>) {
143: next if ($line =~ /^\#/);
144: chomp($line);
145: my ($key,$val)=(split(/\s+/,$line,2));
1.192 taceyjo1 146: $scprtag{$key}=$val;
147: }
148: close($fh);
149: }
150: }
1.63 www 151:
152: # -------------------------------------------------------------- domain designs
153:
154: my $filename;
155: my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
156: opendir(DIR,$designdir);
157: while ($filename=readdir(DIR)) {
1.271 albertel 158: if ($filename!~/\.tab$/) { next; }
1.63 www 159: my ($domain)=($filename=~/^(\w+)\./);
1.271 albertel 160: {
161: my $designfile = $designdir.'/'.$filename;
162: if ( open (my $fh,"<$designfile") ) {
1.356 albertel 163: while (my $line = <$fh>) {
164: next if ($line =~ /^\#/);
165: chomp($line);
166: my ($key,$val)=(split(/\=/,$line));
1.271 albertel 167: if ($val) { $designhash{$domain.'.'.$key}=$val; }
168: }
169: close($fh);
170: }
171: }
1.63 www 172:
173: }
174: closedir(DIR);
175:
176:
1.15 harris41 177: # ------------------------------------------------------------- file categories
178: {
1.158 raeburn 179: my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
180: '/filecategories.tab';
181: if ( open (my $fh,"<$categoryfile") ) {
1.356 albertel 182: while (my $line = <$fh>) {
183: next if ($line =~ /^\#/);
184: chomp($line);
185: my ($extension,$category)=(split(/\s+/,$line,2));
1.158 raeburn 186: push @{$category_extensions{lc($category)}},$extension;
187: }
188: close($fh);
189: }
190:
1.15 harris41 191: }
1.12 harris41 192: # ------------------------------------------------------------------ file types
193: {
1.158 raeburn 194: my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
195: '/filetypes.tab';
196: if ( open (my $fh,"<$typesfile") ) {
1.356 albertel 197: while (my $line = <$fh>) {
198: next if ($line =~ /^\#/);
199: chomp($line);
200: my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158 raeburn 201: if ($descr ne '') {
202: $fe{$ending}=lc($emb);
203: $fd{$ending}=$descr;
1.351 www 204: if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158 raeburn 205: }
206: }
207: close($fh);
208: }
1.12 harris41 209: }
1.22 www 210: &Apache::lonnet::logthis(
1.46 matthew 211: "<font color=yellow>INFO: Read file types</font>");
1.22 www 212: $readit=1;
1.46 matthew 213: } # end of unless($readit)
1.32 matthew 214:
215: }
1.112 bowersj2 216:
1.42 matthew 217: ###############################################################
218: ## HTML and Javascript Helper Functions ##
219: ###############################################################
220:
221: =pod
222:
1.112 bowersj2 223: =head1 HTML and Javascript Functions
1.42 matthew 224:
1.112 bowersj2 225: =over 4
226:
227: =item * browser_and_searcher_javascript ()
228:
229: X<browsing, javascript>X<searching, javascript>Returns a string
230: containing javascript with two functions, C<openbrowser> and
231: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
232: tags.
1.42 matthew 233:
1.112 bowersj2 234: =item * openbrowser(formname,elementname,only,omit) [javascript]
1.42 matthew 235:
236: inputs: formname, elementname, only, omit
237:
238: formname and elementname indicate the name of the html form and name of
239: the element that the results of the browsing selection are to be placed in.
240:
241: Specifying 'only' will restrict the browser to displaying only files
1.185 www 242: with the given extension. Can be a comma separated list.
1.42 matthew 243:
244: Specifying 'omit' will restrict the browser to NOT displaying files
1.185 www 245: with the given extension. Can be a comma separated list.
1.42 matthew 246:
1.112 bowersj2 247: =item * opensearcher(formname, elementname) [javascript]
1.42 matthew 248:
249: Inputs: formname, elementname
250:
251: formname and elementname specify the name of the html form and the name
252: of the element the selection from the search results will be placed in.
253:
254: =cut
255:
256: sub browser_and_searcher_javascript {
1.199 albertel 257: my ($mode)=@_;
258: if (!defined($mode)) { $mode='edit'; }
1.170 www 259: my $resurl=&lastresurl();
1.42 matthew 260: return <<END;
1.219 albertel 261: // <!-- BEGIN LON-CAPA Internal
1.50 matthew 262: var editbrowser = null;
1.135 albertel 263: function openbrowser(formname,elementname,only,omit,titleelement) {
1.170 www 264: var url = '$resurl/?';
1.42 matthew 265: if (editbrowser == null) {
266: url += 'launch=1&';
267: }
268: url += 'catalogmode=interactive&';
1.199 albertel 269: url += 'mode=$mode&';
1.42 matthew 270: url += 'form=' + formname + '&';
271: if (only != null) {
272: url += 'only=' + only + '&';
1.217 albertel 273: } else {
274: url += 'only=&';
275: }
1.42 matthew 276: if (omit != null) {
277: url += 'omit=' + omit + '&';
1.217 albertel 278: } else {
279: url += 'omit=&';
280: }
1.135 albertel 281: if (titleelement != null) {
282: url += 'titleelement=' + titleelement + '&';
1.217 albertel 283: } else {
284: url += 'titleelement=&';
285: }
1.42 matthew 286: url += 'element=' + elementname + '';
287: var title = 'Browser';
1.217 albertel 288: var options = 'scrollbars=1,resizable=1,menubar=1,location=1';
1.42 matthew 289: options += ',width=700,height=600';
290: editbrowser = open(url,title,options,'1');
291: editbrowser.focus();
292: }
293: var editsearcher;
1.135 albertel 294: function opensearcher(formname,elementname,titleelement) {
1.42 matthew 295: var url = '/adm/searchcat?';
296: if (editsearcher == null) {
297: url += 'launch=1&';
298: }
299: url += 'catalogmode=interactive&';
1.199 albertel 300: url += 'mode=$mode&';
1.42 matthew 301: url += 'form=' + formname + '&';
1.135 albertel 302: if (titleelement != null) {
303: url += 'titleelement=' + titleelement + '&';
1.217 albertel 304: } else {
305: url += 'titleelement=&';
306: }
1.42 matthew 307: url += 'element=' + elementname + '';
308: var title = 'Search';
309: var options = 'scrollbars=1,resizable=1,menubar=0';
310: options += ',width=700,height=600';
311: editsearcher = open(url,title,options,'1');
312: editsearcher.focus();
313: }
1.219 albertel 314: // END LON-CAPA Internal -->
1.42 matthew 315: END
1.170 www 316: }
317:
318: sub lastresurl {
1.258 albertel 319: if ($env{'environment.lastresurl'}) {
320: return $env{'environment.lastresurl'}
1.170 www 321: } else {
322: return '/res';
323: }
324: }
325:
326: sub storeresurl {
327: my $resurl=&Apache::lonnet::clutter(shift);
328: unless ($resurl=~/^\/res/) { return 0; }
329: $resurl=~s/\/$//;
330: &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
331: &Apache::lonnet::appenv('environment.lastresurl' => $resurl);
332: return 1;
1.42 matthew 333: }
334:
1.74 www 335: sub studentbrowser_javascript {
1.111 www 336: unless (
1.258 albertel 337: (($env{'request.course.id'}) &&
1.302 albertel 338: (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
339: || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
340: '/'.$env{'request.course.sec'})
341: ))
1.258 albertel 342: || ($env{'request.role'}=~/^(au|dc|su)/)
1.111 www 343: ) { return ''; }
1.74 www 344: return (<<'ENDSTDBRW');
345: <script type="text/javascript" language="Javascript" >
346: var stdeditbrowser;
1.111 www 347: function openstdbrowser(formname,uname,udom,roleflag) {
1.74 www 348: var url = '/adm/pickstudent?';
349: var filter;
350: eval('filter=document.'+formname+'.'+uname+'.value;');
351: if (filter != null) {
352: if (filter != '') {
353: url += 'filter='+filter+'&';
354: }
355: }
356: url += 'form=' + formname + '&unameelement='+uname+
357: '&udomelement='+udom;
1.111 www 358: if (roleflag) { url+="&roles=1"; }
1.102 www 359: var title = 'Student_Browser';
1.74 www 360: var options = 'scrollbars=1,resizable=1,menubar=0';
361: options += ',width=700,height=600';
362: stdeditbrowser = open(url,title,options,'1');
363: stdeditbrowser.focus();
364: }
365: </script>
366: ENDSTDBRW
367: }
1.42 matthew 368:
1.74 www 369: sub selectstudent_link {
1.111 www 370: my ($form,$unameele,$udomele)=@_;
1.258 albertel 371: if ($env{'request.course.id'}) {
1.302 albertel 372: if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
373: && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
374: '/'.$env{'request.course.sec'})) {
1.111 www 375: return '';
376: }
377: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 378: '","'.$udomele.'");'."'>".&mt('Select User')."</a>";
1.74 www 379: }
1.258 albertel 380: if ($env{'request.role'}=~/^(au|dc|su)/) {
1.111 www 381: return "<a href='".'javascript:openstdbrowser("'.$form.'","'.$unameele.
1.119 www 382: '","'.$udomele.'",1);'."'>".&mt('Select User')."</a>";
1.111 www 383: }
384: return '';
1.91 www 385: }
386:
387: sub coursebrowser_javascript {
1.234 raeburn 388: my ($domainfilter)=@_;
1.377 raeburn 389: my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Group - for which you wish to add/modify a user role');
1.128 albertel 390: return (<<ENDSTDBRW);
1.91 www 391: <script type="text/javascript" language="Javascript" >
392: var stdeditbrowser;
1.377 raeburn 393: function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,crstype) {
1.91 www 394: var url = '/adm/pickcourse?';
395: var filter;
396: if (filter != null) {
397: if (filter != '') {
398: url += 'filter='+filter+'&';
399: }
400: }
1.128 albertel 401: var domainfilter='$domainfilter';
402: if (domainfilter != null) {
403: if (domainfilter != '') {
404: url += 'domainfilter='+domainfilter+'&';
405: }
406: }
1.91 www 407: url += 'form=' + formname + '&cnumelement='+uname+
1.187 albertel 408: '&cdomelement='+udom+
409: '&cnameelement='+desc;
1.234 raeburn 410: if (extra_element !=null && extra_element != '' && formname == 'rolechoice') {
411: url += '&roleelement='+extra_element;
412: if (domainfilter == null || domainfilter == '') {
413: url += '&domainfilter='+extra_element;
414: }
1.230 raeburn 415: }
1.293 raeburn 416: if (multflag !=null && multflag != '') {
417: url += '&multiple='+multflag;
418: }
1.377 raeburn 419: if (crstype == 'Course/Group') {
420: if (formname == 'cu') {
421: crstype = document.cu.crstype.options[document.cu.crstype.selectedIndex].value;
422: if (crstype == "") {
423: alert("$crs_or_grp_alert");
424: return;
425: }
426: }
427: }
428: if (crstype !=null && crstype != '') {
429: url += '&type='+crstype;
430: }
1.102 www 431: var title = 'Course_Browser';
1.91 www 432: var options = 'scrollbars=1,resizable=1,menubar=0';
433: options += ',width=700,height=600';
434: stdeditbrowser = open(url,title,options,'1');
435: stdeditbrowser.focus();
436: }
437: </script>
438: ENDSTDBRW
439: }
440:
441: sub selectcourse_link {
1.377 raeburn 442: my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype)=@_;
1.91 www 443: return "<a href='".'javascript:opencrsbrowser("'.$form.'","'.$unameele.
1.377 raeburn 444: '","'.$udomele.'","'.$desc.'","'.$extra_element.'","'.$multflag.'","'.$selecttype.'");'."'>".&mt('Select [_1]',$selecttype)."</a>";
1.74 www 445: }
1.42 matthew 446:
1.273 raeburn 447: sub check_uncheck_jscript {
448: my $jscript = <<"ENDSCRT";
449: function checkAll(field) {
450: if (field.length > 0) {
451: for (i = 0; i < field.length; i++) {
452: field[i].checked = true ;
453: }
454: } else {
455: field.checked = true
456: }
457: }
458:
459: function uncheckAll(field) {
460: if (field.length > 0) {
461: for (i = 0; i < field.length; i++) {
462: field[i].checked = false ;
463: } } else {
464: field.checked = false ;
465: }
466: }
467: ENDSCRT
468: return $jscript;
469: }
470:
471:
1.42 matthew 472: =pod
1.36 matthew 473:
1.112 bowersj2 474: =item * linked_select_forms(...)
1.36 matthew 475:
476: linked_select_forms returns a string containing a <script></script> block
477: and html for two <select> menus. The select menus will be linked in that
478: changing the value of the first menu will result in new values being placed
479: in the second menu. The values in the select menu will appear in alphabetical
480: order.
481:
482: linked_select_forms takes the following ordered inputs:
483:
484: =over 4
485:
1.112 bowersj2 486: =item * $formname, the name of the <form> tag
1.36 matthew 487:
1.112 bowersj2 488: =item * $middletext, the text which appears between the <select> tags
1.36 matthew 489:
1.112 bowersj2 490: =item * $firstdefault, the default value for the first menu
1.36 matthew 491:
1.112 bowersj2 492: =item * $firstselectname, the name of the first <select> tag
1.36 matthew 493:
1.112 bowersj2 494: =item * $secondselectname, the name of the second <select> tag
1.36 matthew 495:
1.112 bowersj2 496: =item * $hashref, a reference to a hash containing the data for the menus.
1.36 matthew 497:
1.41 ng 498: =back
499:
1.36 matthew 500: Below is an example of such a hash. Only the 'text', 'default', and
501: 'select2' keys must appear as stated. keys(%menu) are the possible
502: values for the first select menu. The text that coincides with the
1.41 ng 503: first menu value is given in $menu{$choice1}->{'text'}. The values
1.36 matthew 504: and text for the second menu are given in the hash pointed to by
505: $menu{$choice1}->{'select2'}.
506:
1.112 bowersj2 507: my %menu = ( A1 => { text =>"Choice A1" ,
508: default => "B3",
509: select2 => {
510: B1 => "Choice B1",
511: B2 => "Choice B2",
512: B3 => "Choice B3",
513: B4 => "Choice B4"
514: }
515: },
516: A2 => { text =>"Choice A2" ,
517: default => "C2",
518: select2 => {
519: C1 => "Choice C1",
520: C2 => "Choice C2",
521: C3 => "Choice C3"
522: }
523: },
524: A3 => { text =>"Choice A3" ,
525: default => "D6",
526: select2 => {
527: D1 => "Choice D1",
528: D2 => "Choice D2",
529: D3 => "Choice D3",
530: D4 => "Choice D4",
531: D5 => "Choice D5",
532: D6 => "Choice D6",
533: D7 => "Choice D7"
534: }
535: }
536: );
1.36 matthew 537:
538: =cut
539:
540: sub linked_select_forms {
541: my ($formname,
542: $middletext,
543: $firstdefault,
544: $firstselectname,
545: $secondselectname,
546: $hashref
547: ) = @_;
548: my $second = "document.$formname.$secondselectname";
549: my $first = "document.$formname.$firstselectname";
550: # output the javascript to do the changing
551: my $result = '';
1.219 albertel 552: $result.="<script type=\"text/javascript\">\n";
1.36 matthew 553: $result.="var select2data = new Object();\n";
554: $" = '","';
555: my $debug = '';
556: foreach my $s1 (sort(keys(%$hashref))) {
557: $result.="select2data.d_$s1 = new Object();\n";
558: $result.="select2data.d_$s1.def = new String('".
559: $hashref->{$s1}->{'default'}."');\n";
560: $result.="select2data.d_$s1.values = new Array(";
561: my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
562: $result.="\"@s2values\");\n";
563: $result.="select2data.d_$s1.texts = new Array(";
564: my @s2texts;
565: foreach my $value (@s2values) {
566: push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
567: }
568: $result.="\"@s2texts\");\n";
569: }
570: $"=' ';
571: $result.= <<"END";
572:
573: function select1_changed() {
574: // Determine new choice
575: var newvalue = "d_" + $first.value;
576: // update select2
577: var values = select2data[newvalue].values;
578: var texts = select2data[newvalue].texts;
579: var select2def = select2data[newvalue].def;
580: var i;
581: // out with the old
582: for (i = 0; i < $second.options.length; i++) {
583: $second.options[i] = null;
584: }
585: // in with the nuclear
586: for (i=0;i<values.length; i++) {
587: $second.options[i] = new Option(values[i]);
1.143 matthew 588: $second.options[i].value = values[i];
1.36 matthew 589: $second.options[i].text = texts[i];
590: if (values[i] == select2def) {
591: $second.options[i].selected = true;
592: }
593: }
594: }
595: </script>
596: END
597: # output the initial values for the selection lists
598: $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
599: foreach my $value (sort(keys(%$hashref))) {
600: $result.=" <option value=\"$value\" ";
1.253 albertel 601: $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119 www 602: $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36 matthew 603: }
604: $result .= "</select>\n";
605: my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
606: $result .= $middletext;
607: $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
608: my $seconddefault = $hashref->{$firstdefault}->{'default'};
609: foreach my $value (sort(keys(%select2))) {
610: $result.=" <option value=\"$value\" ";
1.253 albertel 611: $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119 www 612: $result.=">".&mt($select2{$value})."</option>\n";
1.36 matthew 613: }
614: $result .= "</select>\n";
615: # return $debug;
616: return $result;
617: } # end of sub linked_select_forms {
618:
1.45 matthew 619: =pod
1.44 bowersj2 620:
1.112 bowersj2 621: =item * help_open_topic($topic, $text, $stayOnPage, $width, $height)
1.44 bowersj2 622:
1.112 bowersj2 623: Returns a string corresponding to an HTML link to the given help
624: $topic, where $topic corresponds to the name of a .tex file in
625: /home/httpd/html/adm/help/tex, with underscores replaced by
626: spaces.
627:
628: $text will optionally be linked to the same topic, allowing you to
629: link text in addition to the graphic. If you do not want to link
630: text, but wish to specify one of the later parameters, pass an
631: empty string.
632:
633: $stayOnPage is a value that will be interpreted as a boolean. If true,
634: the link will not open a new window. If false, the link will open
635: a new window using Javascript. (Default is false.)
636:
637: $width and $height are optional numerical parameters that will
638: override the width and height of the popped up window, which may
639: be useful for certain help topics with big pictures included.
1.44 bowersj2 640:
641: =cut
642:
643: sub help_open_topic {
1.48 bowersj2 644: my ($topic, $text, $stayOnPage, $width, $height) = @_;
645: $text = "" if (not defined $text);
1.44 bowersj2 646: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 647: if ($env{'browser.interface'} eq 'textual' ||
648: $env{'environment.remote'} eq 'off' ) {
1.79 www 649: $stayOnPage=1;
650: }
1.44 bowersj2 651: $width = 350 if (not defined $width);
652: $height = 400 if (not defined $height);
653: my $filename = $topic;
654: $filename =~ s/ /_/g;
655:
1.48 bowersj2 656: my $template = "";
657: my $link;
1.159 www 658:
659: $topic=~s/\W/\_/g;
1.44 bowersj2 660:
661: if (!$stayOnPage)
662: {
1.72 bowersj2 663: $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 664: }
665: else
666: {
1.48 bowersj2 667: $link = "/adm/help/${filename}.hlp";
668: }
669:
670: # Add the text
671: if ($text ne "")
672: {
1.77 www 673: $template .=
674: "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
1.78 www 675: "<td bgcolor='#5555FF'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48 bowersj2 676: }
677:
678: # Add the graphic
1.179 matthew 679: my $title = &mt('Online Help');
1.215 albertel 680: my $helpicon=&lonhttpdurl("/adm/help/gif/smallHelp.gif");
1.48 bowersj2 681: $template .= <<"ENDTEMPLATE";
1.218 albertel 682: <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help: $topic)" /></a>
1.44 bowersj2 683: ENDTEMPLATE
1.78 www 684: if ($text ne '') { $template.='</td></tr></table>' };
1.44 bowersj2 685: return $template;
686:
1.106 bowersj2 687: }
688:
689: # This is a quicky function for Latex cheatsheet editing, since it
690: # appears in at least four places
691: sub helpLatexCheatsheet {
692: my $other = shift;
693: my $addOther = '';
694: if ($other) {
695: $addOther = Apache::loncommon::help_open_topic($other, shift,
696: undef, undef, 600) .
697: '</td><td>';
698: }
699: return '<table><tr><td>'.
700: $addOther .
701: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
702: undef,undef,600)
703: .'</td><td>'.
704: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
705: undef,undef,600)
706: .'</td></tr></table>';
1.172 www 707: }
708:
1.193 raeburn 709: sub help_open_menu {
710: my ($color,$topic,$component_help,$function,$faq,$bug,$stayOnPage,$width,$height,$text) = @_;
711: $text = "" if (not defined $text);
712: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 713: if ($env{'browser.interface'} eq 'textual' ||
714: $env{'environment.remote'} eq 'off' ) {
1.193 raeburn 715: $stayOnPage=1;
716: }
717: $width = 620 if (not defined $width);
718: $height = 600 if (not defined $height);
719: my $link='';
1.201 raeburn 720: my $title = &mt('Get help');
1.193 raeburn 721: my $origurl = $ENV{'REQUEST_URI'};
1.227 albertel 722: $origurl=~s|^/~|/priv/|;
1.193 raeburn 723: my $timestamp = time;
1.356 albertel 724: foreach my $datum (\$color,\$function,\$topic,\$component_help,\$faq,
725: \$bug,\$origurl) {
1.369 www 726: $$datum = &escape($$datum);
1.193 raeburn 727: }
1.195 albertel 728: if (!$stayOnPage) {
1.193 raeburn 729: $link = "javascript:helpMenu('open')";
1.195 albertel 730: } else {
1.193 raeburn 731: $link = "javascript:helpMenu('display')";
732: }
1.379 albertel 733: 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";
734: 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 735: my $template;
736: if ($text ne "") {
737: $template .=
1.265 albertel 738: "<table bgcolor='#CC3300' cellspacing='1' cellpadding='1' border='0'><tr>".
739: "<td bgcolor='#CC6600'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.196 albertel 740: }
1.261 albertel 741: my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.215 albertel 742: my $helpicon=&lonhttpdurl("/adm/lonIcons/helpgateway.gif");
1.331 albertel 743: my $start_page =
744: &Apache::loncommon::start_page('Help Menu', undef,
745: {'frameset' => 1,
746: 'js_ready' => 1,
747: 'add_entries' => {
748: 'border' => '0',
749: 'rows' => "105,*",},});
750: my $end_page =
751: &Apache::loncommon::end_page({'frameset' => 1,
752: 'js_ready' => 1,});
753:
1.196 albertel 754: $template .= <<"ENDTEMPLATE";
1.219 albertel 755: <script type="text/javascript">
1.253 albertel 756: // <!-- BEGIN LON-CAPA Internal
757: // <![CDATA[
1.243 raeburn 758: function helpMenu(target) {
759: var caller = this;
760: if (target == 'open') {
761: var newWindow = null;
762: try {
1.262 albertel 763: newWindow = window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243 raeburn 764: }
765: catch(error) {
766: writeHelp(caller);
767: return;
768: }
769: if (newWindow) {
770: caller = newWindow;
771: }
1.193 raeburn 772: }
1.243 raeburn 773: writeHelp(caller);
774: return;
775: }
776: function writeHelp(caller) {
1.331 albertel 777: caller.document.writeln('$start_page<frame name="bannerframe" src="$banner_link" /><frame name="bodyframe" src="$details_link" /> $end_page')
1.243 raeburn 778: caller.document.close()
779: caller.focus()
1.193 raeburn 780: }
1.253 albertel 781: // ]]>
1.219 albertel 782: // END LON-CAPA Internal -->
1.193 raeburn 783: </script>
1.218 albertel 784: <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help Menu)" /></a>
1.193 raeburn 785: ENDTEMPLATE
1.203 albertel 786: if ($component_help) {
787: if (!$text) {
788: $template=&help_open_topic($component_help,undef,$stayOnPage,
789: $width,$height).' '.$template;
790: } else {
791: my $help_text;
1.369 www 792: $help_text=&unescape($topic);
1.203 albertel 793: $template='<table><tr><td>'.
794: &help_open_topic($component_help,$help_text,$stayOnPage,
795: $width,$height).'</td><td>'.$template.
796: '</td></tr></table>';
797: }
798: }
1.196 albertel 799: if ($text ne '') { $template.='</td></tr></table>' };
1.193 raeburn 800: return $template;
801: }
802:
1.172 www 803: sub help_open_bug {
804: my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258 albertel 805: unless ($env{'user.adv'}) { return ''; }
1.172 www 806: unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
807: $text = "" if (not defined $text);
808: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 809: if ($env{'browser.interface'} eq 'textual' ||
810: $env{'environment.remote'} eq 'off' ) {
1.172 www 811: $stayOnPage=1;
812: }
1.184 albertel 813: $width = 600 if (not defined $width);
814: $height = 600 if (not defined $height);
1.172 www 815:
816: $topic=~s/\W+/\+/g;
817: my $link='';
818: my $template='';
1.379 albertel 819: my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&bug_file_loc='.
820: &escape($ENV{'REQUEST_URI'}).'&component='.$topic;
1.172 www 821: if (!$stayOnPage)
822: {
823: $link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
824: }
825: else
826: {
827: $link = $url;
828: }
829: # Add the text
830: if ($text ne "")
831: {
832: $template .=
833: "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
834: "<td bgcolor='#FF5555'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
835: }
836:
837: # Add the graphic
1.179 matthew 838: my $title = &mt('Report a Bug');
1.215 albertel 839: my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172 www 840: $template .= <<"ENDTEMPLATE";
1.218 albertel 841: <a href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172 www 842: ENDTEMPLATE
843: if ($text ne '') { $template.='</td></tr></table>' };
844: return $template;
845:
846: }
847:
848: sub help_open_faq {
849: my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258 albertel 850: unless ($env{'user.adv'}) { return ''; }
1.172 www 851: unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
852: $text = "" if (not defined $text);
853: $stayOnPage = 0 if (not defined $stayOnPage);
1.258 albertel 854: if ($env{'browser.interface'} eq 'textual' ||
855: $env{'environment.remote'} eq 'off' ) {
1.172 www 856: $stayOnPage=1;
857: }
858: $width = 350 if (not defined $width);
859: $height = 400 if (not defined $height);
860:
861: $topic=~s/\W+/\+/g;
862: my $link='';
863: my $template='';
864: my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
865: if (!$stayOnPage)
866: {
867: $link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
868: }
869: else
870: {
871: $link = $url;
872: }
873:
874: # Add the text
875: if ($text ne "")
876: {
877: $template .=
1.173 www 878: "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
879: "<td bgcolor='#448844'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172 www 880: }
881:
882: # Add the graphic
1.179 matthew 883: my $title = &mt('View the FAQ');
1.215 albertel 884: my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172 www 885: $template .= <<"ENDTEMPLATE";
1.218 albertel 886: <a href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172 www 887: ENDTEMPLATE
888: if ($text ne '') { $template.='</td></tr></table>' };
889: return $template;
890:
1.44 bowersj2 891: }
1.37 matthew 892:
1.180 matthew 893: ###############################################################
894: ###############################################################
895:
1.45 matthew 896: =pod
897:
1.256 matthew 898: =item * change_content_javascript():
899:
900: This and the next function allow you to create small sections of an
901: otherwise static HTML page that you can update on the fly with
902: Javascript, even in Netscape 4.
903:
904: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
905: must be written to the HTML page once. It will prove the Javascript
906: function "change(name, content)". Calling the change function with the
907: name of the section
908: you want to update, matching the name passed to C<changable_area>, and
909: the new content you want to put in there, will put the content into
910: that area.
911:
912: B<Note>: Netscape 4 only reserves enough space for the changable area
913: to contain room for the original contents. You need to "make space"
914: for whatever changes you wish to make, and be B<sure> to check your
915: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
916: it's adequate for updating a one-line status display, but little more.
917: This script will set the space to 100% width, so you only need to
918: worry about height in Netscape 4.
919:
920: Modern browsers are much less limiting, and if you can commit to the
921: user not using Netscape 4, this feature may be used freely with
922: pretty much any HTML.
923:
924: =cut
925:
926: sub change_content_javascript {
927: # If we're on Netscape 4, we need to use Layer-based code
1.258 albertel 928: if ($env{'browser.type'} eq 'netscape' &&
929: $env{'browser.version'} =~ /^4\./) {
1.256 matthew 930: return (<<NETSCAPE4);
931: function change(name, content) {
932: doc = document.layers[name+"___escape"].layers[0].document;
933: doc.open();
934: doc.write(content);
935: doc.close();
936: }
937: NETSCAPE4
938: } else {
939: # Otherwise, we need to use semi-standards-compliant code
940: # (technically, "innerHTML" isn't standard but the equivalent
941: # is really scary, and every useful browser supports it
942: return (<<DOMBASED);
943: function change(name, content) {
944: element = document.getElementById(name);
945: element.innerHTML = content;
946: }
947: DOMBASED
948: }
949: }
950:
951: =pod
952:
953: =item * changable_area($name, $origContent):
954:
955: This provides a "changable area" that can be modified on the fly via
956: the Javascript code provided in C<change_content_javascript>. $name is
957: the name you will use to reference the area later; do not repeat the
958: same name on a given HTML page more then once. $origContent is what
959: the area will originally contain, which can be left blank.
960:
961: =cut
962:
963: sub changable_area {
964: my ($name, $origContent) = @_;
965:
1.258 albertel 966: if ($env{'browser.type'} eq 'netscape' &&
967: $env{'browser.version'} =~ /^4\./) {
1.256 matthew 968: # If this is netscape 4, we need to use the Layer tag
969: return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
970: } else {
971: return "<span id='$name'>$origContent</span>";
972: }
973: }
974:
975: =pod
976:
977: =back
978:
979: =head1 Excel and CSV file utility routines
980:
981: =over 4
982:
983: =cut
984:
985: ###############################################################
986: ###############################################################
987:
988: =pod
989:
1.112 bowersj2 990: =item * csv_translate($text)
1.37 matthew 991:
1.185 www 992: Translate $text to allow it to be output as a 'comma separated values'
1.37 matthew 993: format.
994:
995: =cut
996:
1.180 matthew 997: ###############################################################
998: ###############################################################
1.37 matthew 999: sub csv_translate {
1000: my $text = shift;
1001: $text =~ s/\"/\"\"/g;
1.209 albertel 1002: $text =~ s/\n/ /g;
1.37 matthew 1003: return $text;
1004: }
1.180 matthew 1005:
1006: ###############################################################
1007: ###############################################################
1008:
1009: =pod
1010:
1011: =item * define_excel_formats
1012:
1013: Define some commonly used Excel cell formats.
1014:
1015: Currently supported formats:
1016:
1017: =over 4
1018:
1019: =item header
1020:
1021: =item bold
1022:
1023: =item h1
1024:
1025: =item h2
1026:
1027: =item h3
1028:
1.256 matthew 1029: =item h4
1030:
1031: =item i
1032:
1.180 matthew 1033: =item date
1034:
1035: =back
1036:
1037: Inputs: $workbook
1038:
1039: Returns: $format, a hash reference.
1040:
1041: =cut
1042:
1043: ###############################################################
1044: ###############################################################
1045: sub define_excel_formats {
1046: my ($workbook) = @_;
1047: my $format;
1048: $format->{'header'} = $workbook->add_format(bold => 1,
1049: bottom => 1,
1050: align => 'center');
1051: $format->{'bold'} = $workbook->add_format(bold=>1);
1052: $format->{'h1'} = $workbook->add_format(bold=>1, size=>18);
1053: $format->{'h2'} = $workbook->add_format(bold=>1, size=>16);
1054: $format->{'h3'} = $workbook->add_format(bold=>1, size=>14);
1.255 matthew 1055: $format->{'h4'} = $workbook->add_format(bold=>1, size=>12);
1.246 matthew 1056: $format->{'i'} = $workbook->add_format(italic=>1);
1.180 matthew 1057: $format->{'date'} = $workbook->add_format(num_format=>
1.207 matthew 1058: 'mm/dd/yyyy hh:mm:ss');
1.180 matthew 1059: return $format;
1060: }
1061:
1062: ###############################################################
1063: ###############################################################
1.113 bowersj2 1064:
1065: =pod
1066:
1.256 matthew 1067: =item * create_workbook
1.255 matthew 1068:
1069: Create an Excel worksheet. If it fails, output message on the
1070: request object and return undefs.
1071:
1072: Inputs: Apache request object
1073:
1074: Returns (undef) on failure,
1075: Excel worksheet object, scalar with filename, and formats
1076: from &Apache::loncommon::define_excel_formats on success
1077:
1078: =cut
1079:
1080: ###############################################################
1081: ###############################################################
1082: sub create_workbook {
1083: my ($r) = @_;
1084: #
1085: # Create the excel spreadsheet
1086: my $filename = '/prtspool/'.
1.258 albertel 1087: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255 matthew 1088: time.'_'.rand(1000000000).'.xls';
1089: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
1090: if (! defined($workbook)) {
1091: $r->log_error("Error creating excel spreadsheet $filename: $!");
1092: $r->print('<p>'.&mt("Unable to create new Excel file. ".
1093: "This error has been logged. ".
1094: "Please alert your LON-CAPA administrator").
1095: '</p>');
1096: return (undef);
1097: }
1098: #
1099: $workbook->set_tempdir('/home/httpd/perl/tmp');
1100: #
1101: my $format = &Apache::loncommon::define_excel_formats($workbook);
1102: return ($workbook,$filename,$format);
1103: }
1104:
1105: ###############################################################
1106: ###############################################################
1107:
1108: =pod
1109:
1.256 matthew 1110: =item * create_text_file
1.113 bowersj2 1111:
1.256 matthew 1112: Create a file to write to and eventually make available to the usre.
1113: If file creation fails, outputs an error message on the request object and
1114: return undefs.
1.113 bowersj2 1115:
1.256 matthew 1116: Inputs: Apache request object, and file suffix
1.113 bowersj2 1117:
1.256 matthew 1118: Returns (undef) on failure,
1119: Filehandle and filename on success.
1.113 bowersj2 1120:
1121: =cut
1122:
1.256 matthew 1123: ###############################################################
1124: ###############################################################
1125: sub create_text_file {
1126: my ($r,$suffix) = @_;
1127: if (! defined($suffix)) { $suffix = 'txt'; };
1128: my $fh;
1129: my $filename = '/prtspool/'.
1.258 albertel 1130: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256 matthew 1131: time.'_'.rand(1000000000).'.'.$suffix;
1132: $fh = Apache::File->new('>/home/httpd'.$filename);
1133: if (! defined($fh)) {
1134: $r->log_error("Couldn't open $filename for output $!");
1135: $r->print("Problems occured in creating the output file. ".
1136: "This error has been logged. ".
1137: "Please alert your LON-CAPA administrator.");
1.113 bowersj2 1138: }
1.256 matthew 1139: return ($fh,$filename)
1.113 bowersj2 1140: }
1141:
1142:
1.256 matthew 1143: =pod
1.113 bowersj2 1144:
1145: =back
1146:
1147: =cut
1.37 matthew 1148:
1149: ###############################################################
1.33 matthew 1150: ## Home server <option> list generating code ##
1151: ###############################################################
1.35 matthew 1152:
1.45 matthew 1153: =pod
1154:
1.112 bowersj2 1155: =head1 Home Server option list generating code
1156:
1157: =over 4
1158:
1159: =item * get_domains()
1.35 matthew 1160:
1161: Returns an array containing each of the domains listed in the hosts.tab
1162: file.
1163:
1164: =cut
1165:
1166: #-------------------------------------------
1.34 matthew 1167: sub get_domains {
1168: # The code below was stolen from "The Perl Cookbook", p 102, 1st ed.
1169: my @domains;
1170: my %seen;
1.356 albertel 1171: foreach my $dom (sort(values(%Apache::lonnet::hostdom))) {
1172: push(@domains,$dom) unless $seen{$dom}++;
1.34 matthew 1173: }
1174: return @domains;
1175: }
1.88 www 1176:
1.169 www 1177: # ------------------------------------------
1178:
1179: sub domain_select {
1180: my ($name,$value,$multiple)=@_;
1181: my %domains=map {
1182: $_ => $_.' '.$Apache::lonnet::domaindescription{$_}
1183: } &get_domains;
1184: if ($multiple) {
1185: $domains{''}=&mt('Any domain');
1.287 albertel 1186: return &multiple_select_form($name,$value,4,\%domains);
1.169 www 1187: } else {
1188: return &select_form($name,$value,%domains);
1189: }
1190: }
1191:
1.282 albertel 1192: #-------------------------------------------
1193:
1194: =pod
1195:
1.287 albertel 1196: =item * multiple_select_form($name,$value,$size,$hash,$order)
1.282 albertel 1197:
1198: Returns a string containing a <select> element int multiple mode
1199:
1200:
1201: Args:
1202: $name - name of the <select> element
1203: $value - sclara or array ref of values that should already be selected
1204: $size - number of rows long the select element is
1.283 albertel 1205: $hash - the elements should be 'option' => 'shown text'
1.282 albertel 1206: (shown text should already have been &mt())
1.284 albertel 1207: $order - (optional) array ref of the order to show the elments in
1.283 albertel 1208:
1.282 albertel 1209: =cut
1210:
1211: #-------------------------------------------
1.169 www 1212: sub multiple_select_form {
1.284 albertel 1213: my ($name,$value,$size,$hash,$order)=@_;
1.169 www 1214: my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
1215: my $output='';
1.191 matthew 1216: if (! defined($size)) {
1217: $size = 4;
1.283 albertel 1218: if (scalar(keys(%$hash))<4) {
1219: $size = scalar(keys(%$hash));
1.191 matthew 1220: }
1221: }
1.169 www 1222: $output.="\n<select name='$name' size='$size' multiple='1'>";
1.286 banghart 1223: my @order = ref($order) ? @$order
1.284 albertel 1224: : sort(keys(%$hash));
1225: foreach my $key (@order) {
1.356 albertel 1226: $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284 albertel 1227: $output.='selected="selected" ' if ($selected{$key});
1228: $output.='>'.$hash->{$key}."</option>\n";
1.169 www 1229: }
1230: $output.="</select>\n";
1231: return $output;
1232: }
1233:
1.88 www 1234: #-------------------------------------------
1235:
1236: =pod
1237:
1.112 bowersj2 1238: =item * select_form($defdom,$name,%hash)
1.88 www 1239:
1240: Returns a string containing a <select name='$name' size='1'> form to
1241: allow a user to select options from a hash option_name => displayed text.
1242: See lonrights.pm for an example invocation and use.
1243:
1244: =cut
1245:
1246: #-------------------------------------------
1247: sub select_form {
1248: my ($def,$name,%hash) = @_;
1249: my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128 albertel 1250: my @keys;
1251: if (exists($hash{'select_form_order'})) {
1252: @keys=@{$hash{'select_form_order'}};
1253: } else {
1254: @keys=sort(keys(%hash));
1255: }
1.356 albertel 1256: foreach my $key (@keys) {
1257: $selectform.=
1258: '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
1259: ($key eq $def ? 'selected="selected" ' : '').
1260: ">".&mt($hash{$key})."</option>\n";
1.88 www 1261: }
1262: $selectform.="</select>";
1263: return $selectform;
1264: }
1265:
1.167 www 1266: sub gradeleveldescription {
1267: my $gradelevel=shift;
1268: my %gradelevels=(0 => 'Not specified',
1269: 1 => 'Grade 1',
1270: 2 => 'Grade 2',
1271: 3 => 'Grade 3',
1272: 4 => 'Grade 4',
1273: 5 => 'Grade 5',
1274: 6 => 'Grade 6',
1275: 7 => 'Grade 7',
1276: 8 => 'Grade 8',
1277: 9 => 'Grade 9',
1278: 10 => 'Grade 10',
1279: 11 => 'Grade 11',
1280: 12 => 'Grade 12',
1281: 13 => 'Grade 13',
1282: 14 => '100 Level',
1283: 15 => '200 Level',
1284: 16 => '300 Level',
1285: 17 => '400 Level',
1286: 18 => 'Graduate Level');
1287: return &mt($gradelevels{$gradelevel});
1288: }
1289:
1.163 www 1290: sub select_level_form {
1291: my ($deflevel,$name)=@_;
1292: unless ($deflevel) { $deflevel=0; }
1.167 www 1293: my $selectform = "<select name=\"$name\" size=\"1\">\n";
1294: for (my $i=0; $i<=18; $i++) {
1295: $selectform.="<option value=\"$i\" ".
1.253 albertel 1296: ($i==$deflevel ? 'selected="selected" ' : '').
1.167 www 1297: ">".&gradeleveldescription($i)."</option>\n";
1298: }
1299: $selectform.="</select>";
1300: return $selectform;
1.163 www 1301: }
1.167 www 1302:
1.35 matthew 1303: #-------------------------------------------
1304:
1.45 matthew 1305: =pod
1306:
1.112 bowersj2 1307: =item * select_dom_form($defdom,$name,$includeempty)
1.35 matthew 1308:
1309: Returns a string containing a <select name='$name' size='1'> form to
1310: allow a user to select the domain to preform an operation in.
1311: See loncreateuser.pm for an example invocation and use.
1312:
1.90 www 1313: If the $includeempty flag is set, it also includes an empty choice ("no domain
1314: selected");
1315:
1.35 matthew 1316: =cut
1317:
1318: #-------------------------------------------
1.34 matthew 1319: sub select_dom_form {
1.90 www 1320: my ($defdom,$name,$includeempty) = @_;
1.34 matthew 1321: my @domains = get_domains();
1.90 www 1322: if ($includeempty) { @domains=('',@domains); }
1.34 matthew 1323: my $selectdomain = "<select name=\"$name\" size=\"1\">\n";
1.356 albertel 1324: foreach my $dom (@domains) {
1325: $selectdomain.="<option value=\"$dom\" ".
1326: ($dom eq $defdom ? 'selected="selected" ' : '').
1327: ">$dom</option>\n";
1.34 matthew 1328: }
1329: $selectdomain.="</select>";
1330: return $selectdomain;
1331: }
1332:
1.35 matthew 1333: #-------------------------------------------
1334:
1.45 matthew 1335: =pod
1336:
1.112 bowersj2 1337: =item * get_library_servers($domain)
1.35 matthew 1338:
1339: Returns a hash which contains keys like '103l3' and values like
1340: 'kirk.lite.msu.edu'. All of the keys will be for machines in the
1341: given $domain.
1342:
1343: =cut
1344:
1345: #-------------------------------------------
1.52 matthew 1346: sub get_library_servers {
1.33 matthew 1347: my $domain = shift;
1.52 matthew 1348: my %library_servers;
1.356 albertel 1349: foreach my $hostid (keys(%Apache::lonnet::libserv)) {
1350: if ($Apache::lonnet::hostdom{$hostid} eq $domain) {
1351: $library_servers{$hostid} = $Apache::lonnet::hostname{$hostid};
1.33 matthew 1352: }
1353: }
1.52 matthew 1354: return %library_servers;
1.33 matthew 1355: }
1356:
1.35 matthew 1357: #-------------------------------------------
1358:
1.45 matthew 1359: =pod
1360:
1.112 bowersj2 1361: =item * home_server_option_list($domain)
1.35 matthew 1362:
1363: returns a string which contains an <option> list to be used in a
1364: <select> form input. See loncreateuser.pm for an example.
1365:
1366: =cut
1367:
1368: #-------------------------------------------
1.33 matthew 1369: sub home_server_option_list {
1370: my $domain = shift;
1.52 matthew 1371: my %servers = &get_library_servers($domain);
1.33 matthew 1372: my $result = '';
1.356 albertel 1373: foreach my $hostid (sort(keys(%servers))) {
1.33 matthew 1374: $result.=
1.356 albertel 1375: '<option value="'.$hostid.'">'.
1376: $hostid.' '.$servers{$hostid}."</option>\n";
1.33 matthew 1377: }
1378: return $result;
1379: }
1.112 bowersj2 1380:
1381: =pod
1382:
1383: =back
1384:
1385: =cut
1.87 matthew 1386:
1387: ###############################################################
1.112 bowersj2 1388: ## Decoding User Agent ##
1.87 matthew 1389: ###############################################################
1390:
1391: =pod
1392:
1.112 bowersj2 1393: =head1 Decoding the User Agent
1394:
1395: =over 4
1396:
1397: =item * &decode_user_agent()
1.87 matthew 1398:
1399: Inputs: $r
1400:
1401: Outputs:
1402:
1403: =over 4
1404:
1.112 bowersj2 1405: =item * $httpbrowser
1.87 matthew 1406:
1.112 bowersj2 1407: =item * $clientbrowser
1.87 matthew 1408:
1.112 bowersj2 1409: =item * $clientversion
1.87 matthew 1410:
1.112 bowersj2 1411: =item * $clientmathml
1.87 matthew 1412:
1.112 bowersj2 1413: =item * $clientunicode
1.87 matthew 1414:
1.112 bowersj2 1415: =item * $clientos
1.87 matthew 1416:
1417: =back
1418:
1.157 matthew 1419: =back
1420:
1.87 matthew 1421: =cut
1422:
1423: ###############################################################
1424: ###############################################################
1425: sub decode_user_agent {
1.247 albertel 1426: my ($r)=@_;
1.87 matthew 1427: my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
1428: my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
1429: my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247 albertel 1430: if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87 matthew 1431: my $clientbrowser='unknown';
1432: my $clientversion='0';
1433: my $clientmathml='';
1434: my $clientunicode='0';
1435: for (my $i=0;$i<=$#browsertype;$i++) {
1436: my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
1437: if (($httpbrowser=~/$match/i) && ($httpbrowser!~/$notmatch/i)) {
1438: $clientbrowser=$bname;
1439: $httpbrowser=~/$vreg/i;
1440: $clientversion=$1;
1441: $clientmathml=($clientversion>=$minv);
1442: $clientunicode=($clientversion>=$univ);
1443: }
1444: }
1445: my $clientos='unknown';
1446: if (($httpbrowser=~/linux/i) ||
1447: ($httpbrowser=~/unix/i) ||
1448: ($httpbrowser=~/ux/i) ||
1449: ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
1450: if (($httpbrowser=~/vax/i) ||
1451: ($httpbrowser=~/vms/i)) { $clientos='vms'; }
1452: if ($httpbrowser=~/next/i) { $clientos='next'; }
1453: if (($httpbrowser=~/mac/i) ||
1454: ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1455: if ($httpbrowser=~/win/i) { $clientos='win'; }
1456: if ($httpbrowser=~/embed/i) { $clientos='pda'; }
1457: return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
1458: $clientunicode,$clientos,);
1459: }
1460:
1.32 matthew 1461: ###############################################################
1462: ## Authentication changing form generation subroutines ##
1463: ###############################################################
1464: ##
1465: ## All of the authform_xxxxxxx subroutines take their inputs in a
1466: ## hash, and have reasonable default values.
1467: ##
1468: ## formname = the name given in the <form> tag.
1.35 matthew 1469: #-------------------------------------------
1470:
1.45 matthew 1471: =pod
1472:
1.112 bowersj2 1473: =head1 Authentication Routines
1474:
1475: =over 4
1476:
1477: =item * authform_xxxxxx
1.35 matthew 1478:
1479: The authform_xxxxxx subroutines provide javascript and html forms which
1480: handle some of the conveniences required for authentication forms.
1481: This is not an optimal method, but it works.
1482:
1483: See loncreateuser.pm for invocation and use examples.
1484:
1485: =over 4
1486:
1.112 bowersj2 1487: =item * authform_header
1.35 matthew 1488:
1.112 bowersj2 1489: =item * authform_authorwarning
1.35 matthew 1490:
1.112 bowersj2 1491: =item * authform_nochange
1.35 matthew 1492:
1.112 bowersj2 1493: =item * authform_kerberos
1.35 matthew 1494:
1.112 bowersj2 1495: =item * authform_internal
1.35 matthew 1496:
1.112 bowersj2 1497: =item * authform_filesystem
1.35 matthew 1498:
1499: =back
1500:
1.157 matthew 1501: =back
1502:
1.35 matthew 1503: =cut
1504:
1505: #-------------------------------------------
1.32 matthew 1506: sub authform_header{
1507: my %in = (
1508: formname => 'cu',
1.80 albertel 1509: kerb_def_dom => '',
1.32 matthew 1510: @_,
1511: );
1512: $in{'formname'} = 'document.' . $in{'formname'};
1513: my $result='';
1.80 albertel 1514:
1515: #---------------------------------------------- Code for upper case translation
1516: my $Javascript_toUpperCase;
1517: unless ($in{kerb_def_dom}) {
1518: $Javascript_toUpperCase =<<"END";
1519: switch (choice) {
1520: case 'krb': currentform.elements[choicearg].value =
1521: currentform.elements[choicearg].value.toUpperCase();
1522: break;
1523: default:
1524: }
1525: END
1526: } else {
1527: $Javascript_toUpperCase = "";
1528: }
1529:
1.165 raeburn 1530: my $radioval = "'nochange'";
1.174 matthew 1531: if (exists($in{'curr_authtype'}) &&
1532: defined($in{'curr_authtype'}) &&
1533: $in{'curr_authtype'} ne '') {
1534: $radioval = "'$in{'curr_authtype'}arg'";
1535: }
1.165 raeburn 1536: my $argfield = 'null';
1537: if ( grep/^mode$/,(keys %in) ) {
1538: if ($in{'mode'} eq 'modifycourse') {
1539: if ( grep/^curr_authtype$/,(keys %in) ) {
1540: $radioval = "'$in{'curr_authtype'}'";
1541: }
1542: if ( grep/^curr_autharg$/,(keys %in) ) {
1543: unless ($in{'curr_autharg'} eq '') {
1544: $argfield = "'$in{'curr_autharg'}'";
1545: }
1546: }
1547: }
1548: }
1549:
1.32 matthew 1550: $result.=<<"END";
1551: var current = new Object();
1.165 raeburn 1552: current.radiovalue = $radioval;
1553: current.argfield = $argfield;
1.32 matthew 1554:
1555: function changed_radio(choice,currentform) {
1556: var choicearg = choice + 'arg';
1557: // If a radio button in changed, we need to change the argfield
1558: if (current.radiovalue != choice) {
1559: current.radiovalue = choice;
1560: if (current.argfield != null) {
1561: currentform.elements[current.argfield].value = '';
1562: }
1563: if (choice == 'nochange') {
1564: current.argfield = null;
1565: } else {
1566: current.argfield = choicearg;
1567: switch(choice) {
1568: case 'krb':
1569: currentform.elements[current.argfield].value =
1570: "$in{'kerb_def_dom'}";
1571: break;
1572: default:
1573: break;
1574: }
1575: }
1576: }
1577: return;
1578: }
1.22 www 1579:
1.32 matthew 1580: function changed_text(choice,currentform) {
1581: var choicearg = choice + 'arg';
1582: if (currentform.elements[choicearg].value !='') {
1.80 albertel 1583: $Javascript_toUpperCase
1.32 matthew 1584: // clear old field
1585: if ((current.argfield != choicearg) && (current.argfield != null)) {
1586: currentform.elements[current.argfield].value = '';
1587: }
1588: current.argfield = choicearg;
1589: }
1590: set_auth_radio_buttons(choice,currentform);
1591: return;
1.20 www 1592: }
1.32 matthew 1593:
1594: function set_auth_radio_buttons(newvalue,currentform) {
1595: var i=0;
1596: while (i < currentform.login.length) {
1597: if (currentform.login[i].value == newvalue) { break; }
1598: i++;
1599: }
1600: if (i == currentform.login.length) {
1601: return;
1602: }
1603: current.radiovalue = newvalue;
1604: currentform.login[i].checked = true;
1605: return;
1606: }
1607: END
1608: return $result;
1609: }
1610:
1611: sub authform_authorwarning{
1612: my $result='';
1.144 matthew 1613: $result='<i>'.
1614: &mt('As a general rule, only authors or co-authors should be '.
1615: 'filesystem authenticated '.
1616: '(which allows access to the server filesystem).')."</i>\n";
1.32 matthew 1617: return $result;
1618: }
1619:
1620: sub authform_nochange{
1621: my %in = (
1622: formname => 'document.cu',
1623: kerb_def_dom => 'MSU.EDU',
1624: @_,
1625: );
1.281 albertel 1626: my $result = '<label>'.&mt('[_1] Do not change login data',
1.144 matthew 1627: '<input type="radio" name="login" value="nochange" '.
1628: 'checked="checked" onclick="'.
1.281 albertel 1629: "javascript:changed_radio('nochange',$in{'formname'});".'" />').
1630: '</label>';
1.32 matthew 1631: return $result;
1632: }
1633:
1634: sub authform_kerberos{
1635: my %in = (
1636: formname => 'document.cu',
1637: kerb_def_dom => 'MSU.EDU',
1.80 albertel 1638: kerb_def_auth => 'krb4',
1.32 matthew 1639: @_,
1640: );
1.165 raeburn 1641: my ($check4,$check5,$krbarg);
1.80 albertel 1642: if ($in{'kerb_def_auth'} eq 'krb5') {
1643: $check5 = " checked=\"on\"";
1644: } else {
1645: $check4 = " checked=\"on\"";
1646: }
1.165 raeburn 1647: $krbarg = $in{'kerb_def_dom'};
1648:
1649: my $krbcheck = "";
1650: if ( grep/^curr_authtype$/,(keys %in) ) {
1651: if ($in{'curr_authtype'} =~ m/^krb/) {
1652: $krbcheck = " checked=\"on\"";
1653: if ( grep/^curr_autharg$/,(keys %in) ) {
1654: $krbarg = $in{'curr_autharg'};
1655: }
1656: }
1657: }
1658:
1.144 matthew 1659: my $jscall = "javascript:changed_radio('krb',$in{'formname'});";
1660: my $result .= &mt
1661: ('[_1] Kerberos authenticated with domain [_2] '.
1.281 albertel 1662: '[_3] Version 4 [_4] Version 5 [_5]',
1663: '<label><input type="radio" name="login" value="krb" '.
1.165 raeburn 1664: 'onclick="'.$jscall.'" onchange="'.$jscall.'"'.$krbcheck.' />',
1.281 albertel 1665: '</label><input type="text" size="10" name="krbarg" '.
1.165 raeburn 1666: 'value="'.$krbarg.'" '.
1.144 matthew 1667: 'onchange="'.$jscall.'" />',
1.281 albertel 1668: '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
1669: '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
1670: '</label>');
1.32 matthew 1671: return $result;
1672: }
1673:
1674: sub authform_internal{
1675: my %args = (
1676: formname => 'document.cu',
1677: kerb_def_dom => 'MSU.EDU',
1678: @_,
1679: );
1.165 raeburn 1680:
1681: my $intcheck = "";
1682: my $intarg = 'value=""';
1683: if ( grep/^curr_authtype$/,(keys %args) ) {
1684: if ($args{'curr_authtype'} eq 'int') {
1685: $intcheck = " checked=\"on\"";
1686: if ( grep/^curr_autharg$/,(keys %args) ) {
1687: $intarg = "value=\"$args{'curr_autharg'}\"";
1688: }
1689: }
1690: }
1691:
1.144 matthew 1692: my $jscall = "javascript:changed_radio('int',$args{'formname'});";
1693: my $result.=&mt
1694: ('[_1] Internally authenticated (with initial password [_2])',
1.281 albertel 1695: '<label><input type="radio" name="login" value="int" '.$intcheck.
1.165 raeburn 1696: ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281 albertel 1697: '</label><input type="text" size="10" name="intarg" '.$intarg.
1.165 raeburn 1698: ' onchange="'.$jscall.'" />');
1.32 matthew 1699: return $result;
1700: }
1701:
1702: sub authform_local{
1703: my %in = (
1704: formname => 'document.cu',
1705: kerb_def_dom => 'MSU.EDU',
1706: @_,
1707: );
1.165 raeburn 1708:
1709: my $loccheck = "";
1710: my $locarg = 'value=""';
1711: if ( grep/^curr_authtype$/,(keys %in) ) {
1712: if ($in{'curr_authtype'} eq 'loc') {
1713: $loccheck = " checked=\"on\"";
1714: if ( grep/^curr_autharg$/,(keys %in) ) {
1715: $locarg = "value=\"$in{'curr_autharg'}\"";
1716: }
1717: }
1718: }
1719:
1.144 matthew 1720: my $jscall = "javascript:changed_radio('loc',$in{'formname'});";
1.160 matthew 1721: my $result.=&mt('[_1] Local Authentication with argument [_2]',
1.281 albertel 1722: '<label><input type="radio" name="login" value="loc" '.$loccheck.
1.165 raeburn 1723: ' onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281 albertel 1724: '</label><input type="text" size="10" name="locarg" '.$locarg.
1.165 raeburn 1725: ' onchange="'.$jscall.'" />');
1.32 matthew 1726: return $result;
1727: }
1728:
1729: sub authform_filesystem{
1730: my %in = (
1731: formname => 'document.cu',
1732: kerb_def_dom => 'MSU.EDU',
1733: @_,
1734: );
1.144 matthew 1735: my $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
1736: my $result.= &mt
1737: ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281 albertel 1738: '<label><input type="radio" name="login" value="fsys" '.
1.144 matthew 1739: 'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.281 albertel 1740: '</label><input type="text" size="10" name="fsysarg" value="" '.
1.144 matthew 1741: 'onchange="'.$jscall.'" />');
1.32 matthew 1742: return $result;
1743: }
1744:
1.80 albertel 1745: ###############################################################
1746: ## Get Authentication Defaults for Domain ##
1747: ###############################################################
1748:
1749: =pod
1750:
1.112 bowersj2 1751: =head1 Domains and Authentication
1752:
1753: Returns default authentication type and an associated argument as
1754: listed in file 'domain.tab'.
1755:
1756: =over 4
1757:
1758: =item * get_auth_defaults
1.80 albertel 1759:
1760: get_auth_defaults($target_domain) returns the default authentication
1761: type and an associated argument (initial password or a kerberos domain).
1762: These values are stored in lonTabs/domain.tab
1763:
1764: ($def_auth, $def_arg) = &get_auth_defaults($target_domain);
1765:
1766: If target_domain is not found in domain.tab, returns nothing ('').
1767:
1768: =cut
1769:
1770: #-------------------------------------------
1771: sub get_auth_defaults {
1772: my $domain=shift;
1773: return ($Apache::lonnet::domain_auth_def{$domain},$Apache::lonnet::domain_auth_arg_def{$domain});
1774: }
1775: ###############################################################
1776: ## End Get Authentication Defaults for Domain ##
1777: ###############################################################
1778:
1779: ###############################################################
1780: ## Get Kerberos Defaults for Domain ##
1781: ###############################################################
1782: ##
1783: ## Returns default kerberos version and an associated argument
1784: ## as listed in file domain.tab. If not listed, provides
1785: ## appropriate default domain and kerberos version.
1786: ##
1787: #-------------------------------------------
1788:
1789: =pod
1790:
1.112 bowersj2 1791: =item * get_kerberos_defaults
1.80 albertel 1792:
1793: get_kerberos_defaults($target_domain) returns the default kerberos
1794: version and domain. If not found in domain.tabs, it defaults to
1795: version 4 and the domain of the server.
1796:
1797: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
1798:
1799: =cut
1800:
1801: #-------------------------------------------
1802: sub get_kerberos_defaults {
1803: my $domain=shift;
1804: my ($krbdef,$krbdefdom) =
1805: &Apache::loncommon::get_auth_defaults($domain);
1806: unless ($krbdef =~/^krb/ && $krbdefdom) {
1807: $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
1808: my $krbdefdom=$1;
1809: $krbdefdom=~tr/a-z/A-Z/;
1810: $krbdef = "krb4";
1811: }
1812: return ($krbdef,$krbdefdom);
1813: }
1.112 bowersj2 1814:
1815: =pod
1816:
1817: =back
1818:
1819: =cut
1.32 matthew 1820:
1.46 matthew 1821: ###############################################################
1822: ## Thesaurus Functions ##
1823: ###############################################################
1.20 www 1824:
1.46 matthew 1825: =pod
1.20 www 1826:
1.112 bowersj2 1827: =head1 Thesaurus Functions
1828:
1829: =over 4
1830:
1831: =item * initialize_keywords
1.46 matthew 1832:
1833: Initializes the package variable %Keywords if it is empty. Uses the
1834: package variable $thesaurus_db_file.
1835:
1836: =cut
1837:
1838: ###################################################
1839:
1840: sub initialize_keywords {
1841: return 1 if (scalar keys(%Keywords));
1842: # If we are here, %Keywords is empty, so fill it up
1843: # Make sure the file we need exists...
1844: if (! -e $thesaurus_db_file) {
1845: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
1846: " failed because it does not exist");
1847: return 0;
1848: }
1849: # Set up the hash as a database
1850: my %thesaurus_db;
1851: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1852: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1853: &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
1854: $thesaurus_db_file);
1855: return 0;
1856: }
1857: # Get the average number of appearances of a word.
1858: my $avecount = $thesaurus_db{'average.count'};
1859: # Put keywords (those that appear > average) into %Keywords
1860: while (my ($word,$data)=each (%thesaurus_db)) {
1861: my ($count,undef) = split /:/,$data;
1862: $Keywords{$word}++ if ($count > $avecount);
1863: }
1864: untie %thesaurus_db;
1865: # Remove special values from %Keywords.
1.356 albertel 1866: foreach my $value ('total.count','average.count') {
1867: delete($Keywords{$value}) if (exists($Keywords{$value}));
1.46 matthew 1868: }
1869: return 1;
1870: }
1871:
1872: ###################################################
1873:
1874: =pod
1875:
1.112 bowersj2 1876: =item * keyword($word)
1.46 matthew 1877:
1878: Returns true if $word is a keyword. A keyword is a word that appears more
1879: than the average number of times in the thesaurus database. Calls
1880: &initialize_keywords
1881:
1882: =cut
1883:
1884: ###################################################
1.20 www 1885:
1886: sub keyword {
1.46 matthew 1887: return if (!&initialize_keywords());
1888: my $word=lc(shift());
1889: $word=~s/\W//g;
1890: return exists($Keywords{$word});
1.20 www 1891: }
1.46 matthew 1892:
1893: ###############################################################
1894:
1895: =pod
1.20 www 1896:
1.112 bowersj2 1897: =item * get_related_words
1.46 matthew 1898:
1.160 matthew 1899: Look up a word in the thesaurus. Takes a scalar argument and returns
1.46 matthew 1900: an array of words. If the keyword is not in the thesaurus, an empty array
1901: will be returned. The order of the words returned is determined by the
1902: database which holds them.
1903:
1904: Uses global $thesaurus_db_file.
1905:
1906: =cut
1907:
1908: ###############################################################
1909: sub get_related_words {
1910: my $keyword = shift;
1911: my %thesaurus_db;
1912: if (! -e $thesaurus_db_file) {
1913: &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
1914: "failed because the file does not exist");
1915: return ();
1916: }
1917: if (! tie(%thesaurus_db,'GDBM_File',
1.53 albertel 1918: $thesaurus_db_file,&GDBM_READER(),0640)){
1.46 matthew 1919: return ();
1920: }
1921: my @Words=();
1922: if (exists($thesaurus_db{$keyword})) {
1.356 albertel 1923: # The first element is the number of times
1924: # the word appears. We do not need it now.
1925: (undef,@Words) = (split(/:/,$thesaurus_db{$keyword}));
1.46 matthew 1926: for (my $i=0;$i<=$#Words;$i++) {
1.356 albertel 1927: ($Words[$i],undef)= split(/\,/,$Words[$i]);
1.20 www 1928: }
1929: }
1.46 matthew 1930: untie %thesaurus_db;
1931: return @Words;
1.14 harris41 1932: }
1.46 matthew 1933:
1.112 bowersj2 1934: =pod
1935:
1936: =back
1937:
1938: =cut
1.61 www 1939:
1940: # -------------------------------------------------------------- Plaintext name
1.81 albertel 1941: =pod
1942:
1.112 bowersj2 1943: =head1 User Name Functions
1944:
1945: =over 4
1946:
1.226 albertel 1947: =item * plainname($uname,$udom,$first)
1.81 albertel 1948:
1.112 bowersj2 1949: Takes a users logon name and returns it as a string in
1.226 albertel 1950: "first middle last generation" form
1951: if $first is set to 'lastname' then it returns it as
1952: 'lastname generation, firstname middlename' if their is a lastname
1.81 albertel 1953:
1954: =cut
1.61 www 1955:
1.295 www 1956:
1.81 albertel 1957: ###############################################################
1.61 www 1958: sub plainname {
1.226 albertel 1959: my ($uname,$udom,$first)=@_;
1.295 www 1960: my %names=&getnames($uname,$udom);
1.226 albertel 1961: my $name=&Apache::lonnet::format_name($names{'firstname'},
1962: $names{'middlename'},
1963: $names{'lastname'},
1964: $names{'generation'},$first);
1965: $name=~s/^\s+//;
1.62 www 1966: $name=~s/\s+$//;
1967: $name=~s/\s+/ /g;
1.353 albertel 1968: if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62 www 1969: return $name;
1.61 www 1970: }
1.66 www 1971:
1972: # -------------------------------------------------------------------- Nickname
1.81 albertel 1973: =pod
1974:
1.112 bowersj2 1975: =item * nickname($uname,$udom)
1.81 albertel 1976:
1977: Gets a users name and returns it as a string as
1978:
1979: ""nickname""
1.66 www 1980:
1.81 albertel 1981: if the user has a nickname or
1982:
1983: "first middle last generation"
1984:
1985: if the user does not
1986:
1987: =cut
1.66 www 1988:
1989: sub nickname {
1990: my ($uname,$udom)=@_;
1.295 www 1991: my %names=&getnames($uname,$udom);
1.68 albertel 1992: my $name=$names{'nickname'};
1.66 www 1993: if ($name) {
1994: $name='"'.$name.'"';
1995: } else {
1996: $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
1997: $names{'lastname'}.' '.$names{'generation'};
1998: $name=~s/\s+$//;
1999: $name=~s/\s+/ /g;
2000: }
2001: return $name;
2002: }
2003:
1.295 www 2004: sub getnames {
2005: my ($uname,$udom)=@_;
2006: my $id=$uname.':'.$udom;
2007: my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
2008: if ($cached) {
2009: return %{$names};
2010: } else {
2011: my %loadnames=&Apache::lonnet::get('environment',
2012: ['firstname','middlename','lastname','generation','nickname'],
2013: $udom,$uname);
2014: &Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
2015: return %loadnames;
2016: }
2017: }
1.61 www 2018:
2019: # ------------------------------------------------------------------ Screenname
1.81 albertel 2020:
2021: =pod
2022:
1.112 bowersj2 2023: =item * screenname($uname,$udom)
1.81 albertel 2024:
2025: Gets a users screenname and returns it as a string
2026:
2027: =cut
1.61 www 2028:
2029: sub screenname {
2030: my ($uname,$udom)=@_;
1.258 albertel 2031: if ($uname eq $env{'user.name'} &&
2032: $udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212 albertel 2033: my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68 albertel 2034: return $names{'screenname'};
1.62 www 2035: }
2036:
1.212 albertel 2037:
1.62 www 2038: # ------------------------------------------------------------- Message Wrapper
2039:
2040: sub messagewrapper {
1.369 www 2041: my ($link,$username,$domain,$subject,$text)=@_;
1.62 www 2042: return
1.200 matthew 2043: '<a href="/adm/email?compose=individual&'.
1.369 www 2044: 'recname='.$username.'&recdom='.$domain.
2045: '&subject='.&escape($subject).'&text='.&escape($text).'" '.
1.200 matthew 2046: 'title="'.&mt('Send message').'">'.$link.'</a>';
1.74 www 2047: }
2048: # --------------------------------------------------------------- Notes Wrapper
2049:
2050: sub noteswrapper {
2051: my ($link,$un,$do)=@_;
2052: return
2053: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62 www 2054: }
2055: # ------------------------------------------------------------- Aboutme Wrapper
2056:
2057: sub aboutmewrapper {
1.166 www 2058: my ($link,$username,$domain,$target)=@_;
1.205 www 2059: return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.200 matthew 2060: ($target?' target="$target"':'').' title="'.&mt('View this users personal page').'">'.$link.'</a>';
1.62 www 2061: }
2062:
2063: # ------------------------------------------------------------ Syllabus Wrapper
2064:
2065:
2066: sub syllabuswrapper {
1.109 matthew 2067: my ($linktext,$coursedir,$domain,$fontcolor)=@_;
2068: if ($fontcolor) {
2069: $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>';
2070: }
1.208 matthew 2071: return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61 www 2072: }
1.14 harris41 2073:
1.208 matthew 2074: sub track_student_link {
1.268 albertel 2075: my ($linktext,$sname,$sdom,$target,$start) = @_;
2076: my $link ="/adm/trackstudent?";
1.208 matthew 2077: my $title = 'View recent activity';
2078: if (defined($sname) && $sname !~ /^\s*$/ &&
2079: defined($sdom) && $sdom !~ /^\s*$/) {
1.268 albertel 2080: $link .= "selected_student=$sname:$sdom";
1.208 matthew 2081: $title .= ' of this student';
1.268 albertel 2082: }
1.208 matthew 2083: if (defined($target) && $target !~ /^\s*$/) {
2084: $target = qq{target="$target"};
2085: } else {
2086: $target = '';
2087: }
1.268 albertel 2088: if ($start) { $link.='&start='.$start; }
1.208 matthew 2089: return qq{<a href="$link" title="$title" $target>$linktext</a>};
2090: }
2091:
1.112 bowersj2 2092: =pod
2093:
2094: =back
2095:
2096: =head1 Access .tab File Data
2097:
2098: =over 4
2099:
2100: =item * languageids()
2101:
2102: returns list of all language ids
2103:
2104: =cut
2105:
1.14 harris41 2106: sub languageids {
1.16 harris41 2107: return sort(keys(%language));
1.14 harris41 2108: }
2109:
1.112 bowersj2 2110: =pod
2111:
2112: =item * languagedescription()
2113:
2114: returns description of a specified language id
2115:
2116: =cut
2117:
1.14 harris41 2118: sub languagedescription {
1.125 www 2119: my $code=shift;
2120: return ($supported_language{$code}?'* ':'').
2121: $language{$code}.
1.126 www 2122: ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145 www 2123: }
2124:
2125: sub plainlanguagedescription {
2126: my $code=shift;
2127: return $language{$code};
2128: }
2129:
2130: sub supportedlanguagecode {
2131: my $code=shift;
2132: return $supported_language{$code};
1.97 www 2133: }
2134:
1.112 bowersj2 2135: =pod
2136:
2137: =item * copyrightids()
2138:
2139: returns list of all copyrights
2140:
2141: =cut
2142:
2143: sub copyrightids {
2144: return sort(keys(%cprtag));
2145: }
2146:
2147: =pod
2148:
2149: =item * copyrightdescription()
2150:
2151: returns description of a specified copyright id
2152:
2153: =cut
2154:
2155: sub copyrightdescription {
1.166 www 2156: return &mt($cprtag{shift(@_)});
1.112 bowersj2 2157: }
1.197 matthew 2158:
2159: =pod
2160:
1.192 taceyjo1 2161: =item * source_copyrightids()
2162:
2163: returns list of all source copyrights
2164:
2165: =cut
2166:
2167: sub source_copyrightids {
2168: return sort(keys(%scprtag));
2169: }
2170:
2171: =pod
2172:
2173: =item * source_copyrightdescription()
2174:
2175: returns description of a specified source copyright id
2176:
2177: =cut
2178:
2179: sub source_copyrightdescription {
2180: return &mt($scprtag{shift(@_)});
2181: }
1.112 bowersj2 2182:
2183: =pod
2184:
2185: =item * filecategories()
2186:
2187: returns list of all file categories
2188:
2189: =cut
2190:
2191: sub filecategories {
2192: return sort(keys(%category_extensions));
2193: }
2194:
2195: =pod
2196:
2197: =item * filecategorytypes()
2198:
2199: returns list of file types belonging to a given file
2200: category
2201:
2202: =cut
2203:
2204: sub filecategorytypes {
1.356 albertel 2205: my ($cat) = @_;
2206: return @{$category_extensions{lc($cat)}};
1.112 bowersj2 2207: }
2208:
2209: =pod
2210:
2211: =item * fileembstyle()
2212:
2213: returns embedding style for a specified file type
2214:
2215: =cut
2216:
2217: sub fileembstyle {
2218: return $fe{lc(shift(@_))};
1.169 www 2219: }
2220:
1.351 www 2221: sub filemimetype {
2222: return $fm{lc(shift(@_))};
2223: }
2224:
1.169 www 2225:
2226: sub filecategoryselect {
2227: my ($name,$value)=@_;
1.189 matthew 2228: return &select_form($value,$name,
1.169 www 2229: '' => &mt('Any category'),
2230: map { $_,$_ } sort(keys(%category_extensions)));
1.112 bowersj2 2231: }
2232:
2233: =pod
2234:
2235: =item * filedescription()
2236:
2237: returns description for a specified file type
2238:
2239: =cut
2240:
2241: sub filedescription {
1.188 matthew 2242: my $file_description = $fd{lc(shift())};
2243: $file_description =~ s:([\[\]]):~$1:g;
2244: return &mt($file_description);
1.112 bowersj2 2245: }
2246:
2247: =pod
2248:
2249: =item * filedescriptionex()
2250:
2251: returns description for a specified file type with
2252: extra formatting
2253:
2254: =cut
2255:
2256: sub filedescriptionex {
2257: my $ex=shift;
1.188 matthew 2258: my $file_description = $fd{lc($ex)};
2259: $file_description =~ s:([\[\]]):~$1:g;
2260: return '.'.$ex.' '.&mt($file_description);
1.112 bowersj2 2261: }
2262:
2263: # End of .tab access
2264: =pod
2265:
2266: =back
2267:
2268: =cut
2269:
2270: # ------------------------------------------------------------------ File Types
2271: sub fileextensions {
2272: return sort(keys(%fe));
2273: }
2274:
1.97 www 2275: # ----------------------------------------------------------- Display Languages
2276: # returns a hash with all desired display languages
2277: #
2278:
2279: sub display_languages {
2280: my %languages=();
1.356 albertel 2281: foreach my $lang (&preferred_languages()) {
2282: $languages{$lang}=1;
1.97 www 2283: }
2284: &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258 albertel 2285: if ($env{'form.displaylanguage'}) {
1.356 albertel 2286: foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
2287: $languages{$lang}=1;
1.97 www 2288: }
2289: }
2290: return %languages;
1.14 harris41 2291: }
2292:
1.117 www 2293: sub preferred_languages {
2294: my @languages=();
1.258 albertel 2295: if ($env{'course.'.$env{'request.course.id'}.'.languages'}) {
1.117 www 2296: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
1.258 albertel 2297: $env{'course.'.$env{'request.course.id'}.'.languages'}));
1.177 www 2298: }
1.258 albertel 2299: if ($env{'environment.languages'}) {
2300: @languages=split(/\s*(\,|\;|\:)\s*/,$env{'environment.languages'});
1.118 www 2301: }
1.162 www 2302: my $browser=(split(/\;/,$ENV{'HTTP_ACCEPT_LANGUAGE'}))[0];
2303: if ($browser) {
2304: @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$browser));
2305: }
1.258 albertel 2306: if ($Apache::lonnet::domain_lang_def{$env{'user.domain'}}) {
1.118 www 2307: @languages=(@languages,
1.258 albertel 2308: $Apache::lonnet::domain_lang_def{$env{'user.domain'}});
1.118 www 2309: }
1.258 albertel 2310: if ($Apache::lonnet::domain_lang_def{$env{'request.role.domain'}}) {
1.118 www 2311: @languages=(@languages,
1.258 albertel 2312: $Apache::lonnet::domain_lang_def{$env{'request.role.domain'}});
1.118 www 2313: }
2314: if ($Apache::lonnet::domain_lang_def{
2315: $Apache::lonnet::perlvar{'lonDefDomain'}}) {
2316: @languages=(@languages,
2317: $Apache::lonnet::domain_lang_def{
2318: $Apache::lonnet::perlvar{'lonDefDomain'}});
2319: }
2320: # turn "en-ca" into "en-ca,en"
2321: my @genlanguages;
1.356 albertel 2322: foreach my $lang (@languages) {
2323: unless ($lang=~/\w/) { next; }
2324: push (@genlanguages,$lang);
2325: if ($lang=~/(\-|\_)/) {
2326: push(@genlanguages,(split(/(\-|\_)/,$lang))[0]);
1.118 www 2327: }
2328: }
2329: return @genlanguages;
1.117 www 2330: }
2331:
1.112 bowersj2 2332: ###############################################################
2333: ## Student Answer Attempts ##
2334: ###############################################################
2335:
2336: =pod
2337:
2338: =head1 Alternate Problem Views
2339:
2340: =over 4
2341:
2342: =item * get_previous_attempt($symb, $username, $domain, $course,
2343: $getattempt, $regexp, $gradesub)
2344:
2345: Return string with previous attempt on problem. Arguments:
2346:
2347: =over 4
2348:
2349: =item * $symb: Problem, including path
2350:
2351: =item * $username: username of the desired student
2352:
2353: =item * $domain: domain of the desired student
1.14 harris41 2354:
1.112 bowersj2 2355: =item * $course: Course ID
1.14 harris41 2356:
1.112 bowersj2 2357: =item * $getattempt: Leave blank for all attempts, otherwise put
2358: something
1.14 harris41 2359:
1.112 bowersj2 2360: =item * $regexp: if string matches this regexp, the string will be
2361: sent to $gradesub
1.14 harris41 2362:
1.112 bowersj2 2363: =item * $gradesub: routine that processes the string if it matches $regexp
1.14 harris41 2364:
1.112 bowersj2 2365: =back
1.14 harris41 2366:
1.112 bowersj2 2367: The output string is a table containing all desired attempts, if any.
1.16 harris41 2368:
1.112 bowersj2 2369: =cut
1.1 albertel 2370:
2371: sub get_previous_attempt {
1.43 ng 2372: my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1 albertel 2373: my $prevattempts='';
1.43 ng 2374: no strict 'refs';
1.1 albertel 2375: if ($symb) {
1.3 albertel 2376: my (%returnhash)=
2377: &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1 albertel 2378: if ($returnhash{'version'}) {
2379: my %lasthash=();
2380: my $version;
2381: for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356 albertel 2382: foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
2383: $lasthash{$key}=$returnhash{$version.':'.$key};
1.19 harris41 2384: }
1.1 albertel 2385: }
1.43 ng 2386: $prevattempts='<table border="0" width="100%"><tr><td bgcolor="#777777">';
1.40 ng 2387: $prevattempts.='<table border="0" width="100%"><tr bgcolor="#e6ffff"><td>History</td>';
1.356 albertel 2388: foreach my $key (sort(keys(%lasthash))) {
2389: my ($ign,@parts) = split(/\./,$key);
1.41 ng 2390: if ($#parts > 0) {
1.31 albertel 2391: my $data=$parts[-1];
2392: pop(@parts);
1.40 ng 2393: $prevattempts.='<td>Part '.join('.',@parts).'<br />'.$data.' </td>';
1.31 albertel 2394: } else {
1.41 ng 2395: if ($#parts == 0) {
2396: $prevattempts.='<th>'.$parts[0].'</th>';
2397: } else {
2398: $prevattempts.='<th>'.$ign.'</th>';
2399: }
1.31 albertel 2400: }
1.16 harris41 2401: }
1.40 ng 2402: if ($getattempt eq '') {
2403: for ($version=1;$version<=$returnhash{'version'};$version++) {
2404: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Transaction '.$version.'</td>';
1.356 albertel 2405: foreach my $key (sort(keys(%lasthash))) {
1.40 ng 2406: my $value;
1.356 albertel 2407: if ($key =~ /timestamp/) {
2408: $value=scalar(localtime($returnhash{$version.':'.$key}));
1.40 ng 2409: } else {
1.356 albertel 2410: $value=$returnhash{$version.':'.$key};
1.40 ng 2411: }
1.369 www 2412: $prevattempts.='<td>'.&unescape($value).' </td>';
1.40 ng 2413: }
2414: }
1.1 albertel 2415: }
1.40 ng 2416: $prevattempts.='</tr><tr bgcolor="#ffffe6"><td>Current</td>';
1.356 albertel 2417: foreach my $key (sort(keys(%lasthash))) {
1.5 albertel 2418: my $value;
1.356 albertel 2419: if ($key =~ /timestamp/) {
2420: $value=scalar(localtime($lasthash{$key}));
1.5 albertel 2421: } else {
1.356 albertel 2422: $value=$lasthash{$key};
1.5 albertel 2423: }
1.369 www 2424: $value=&unescape($value);
1.356 albertel 2425: if ($key =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40 ng 2426: $prevattempts.='<td>'.$value.' </td>';
1.16 harris41 2427: }
1.40 ng 2428: $prevattempts.='</tr></table></td></tr></table>';
1.1 albertel 2429: } else {
2430: $prevattempts='Nothing submitted - no attempts.';
2431: }
2432: } else {
2433: $prevattempts='No data.';
2434: }
1.10 albertel 2435: }
2436:
1.107 albertel 2437: sub relative_to_absolute {
2438: my ($url,$output)=@_;
2439: my $parser=HTML::TokeParser->new(\$output);
2440: my $token;
2441: my $thisdir=$url;
2442: my @rlinks=();
2443: while ($token=$parser->get_token) {
2444: if ($token->[0] eq 'S') {
2445: if ($token->[1] eq 'a') {
2446: if ($token->[2]->{'href'}) {
2447: $rlinks[$#rlinks+1]=$token->[2]->{'href'};
2448: }
2449: } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
2450: $rlinks[$#rlinks+1]=$token->[2]->{'src'};
2451: } elsif ($token->[1] eq 'base') {
2452: $thisdir=$token->[2]->{'href'};
2453: }
2454: }
2455: }
2456: $thisdir=~s-/[^/]*$--;
1.356 albertel 2457: foreach my $link (@rlinks) {
2458: unless (($link=~/^http:\/\//i) ||
2459: ($link=~/^\//) ||
2460: ($link=~/^javascript:/i) ||
2461: ($link=~/^mailto:/i) ||
2462: ($link=~/^\#/)) {
2463: my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
2464: $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107 albertel 2465: }
2466: }
2467: # -------------------------------------------------- Deal with Applet codebases
2468: $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
2469: return $output;
2470: }
2471:
1.112 bowersj2 2472: =pod
2473:
2474: =item * get_student_view
2475:
2476: show a snapshot of what student was looking at
2477:
2478: =cut
2479:
1.10 albertel 2480: sub get_student_view {
1.186 albertel 2481: my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114 www 2482: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186 albertel 2483: my (%form);
1.10 albertel 2484: my @elements=('symb','courseid','domain','username');
2485: foreach my $element (@elements) {
1.186 albertel 2486: $form{'grade_'.$element}=eval '$'.$element #'
1.10 albertel 2487: }
1.186 albertel 2488: if (defined($moreenv)) {
2489: %form=(%form,%{$moreenv});
2490: }
1.236 albertel 2491: if (defined($target)) { $form{'grade_target'} = $target; }
1.107 albertel 2492: $feedurl=&Apache::lonnet::clutter($feedurl);
1.186 albertel 2493: my $userview=&Apache::lonnet::ssi_body($feedurl,%form);
1.11 albertel 2494: $userview=~s/\<body[^\>]*\>//gi;
2495: $userview=~s/\<\/body\>//gi;
2496: $userview=~s/\<html\>//gi;
2497: $userview=~s/\<\/html\>//gi;
2498: $userview=~s/\<head\>//gi;
2499: $userview=~s/\<\/head\>//gi;
2500: $userview=~s/action\s*\=/would_be_action\=/gi;
1.107 albertel 2501: $userview=&relative_to_absolute($feedurl,$userview);
1.11 albertel 2502: return $userview;
2503: }
2504:
1.112 bowersj2 2505: =pod
2506:
2507: =item * get_student_answers()
2508:
2509: show a snapshot of how student was answering problem
2510:
2511: =cut
2512:
1.11 albertel 2513: sub get_student_answers {
1.100 sakharuk 2514: my ($symb,$username,$domain,$courseid,%form) = @_;
1.114 www 2515: my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186 albertel 2516: my (%moreenv);
1.11 albertel 2517: my @elements=('symb','courseid','domain','username');
2518: foreach my $element (@elements) {
1.186 albertel 2519: $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10 albertel 2520: }
1.186 albertel 2521: $moreenv{'grade_target'}='answer';
2522: %moreenv=(%form,%moreenv);
2523: my $userview=&Apache::lonnet::ssi('/res/'.$feedurl,%moreenv);
1.10 albertel 2524: return $userview;
1.1 albertel 2525: }
1.116 albertel 2526:
2527: =pod
2528:
2529: =item * &submlink()
2530:
1.242 albertel 2531: Inputs: $text $uname $udom $symb $target
1.116 albertel 2532:
2533: Returns: A link to grades.pm such as to see the SUBM view of a student
2534:
2535: =cut
2536:
2537: ###############################################
2538: sub submlink {
1.242 albertel 2539: my ($text,$uname,$udom,$symb,$target)=@_;
1.116 albertel 2540: if (!($uname && $udom)) {
2541: (my $cursymb, my $courseid,$udom,$uname)=
2542: &Apache::lonxml::whichuser($symb);
2543: if (!$symb) { $symb=$cursymb; }
2544: }
1.254 matthew 2545: if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369 www 2546: $symb=&escape($symb);
1.242 albertel 2547: if ($target) { $target="target=\"$target\""; }
2548: return '<a href="/adm/grades?&command=submission&'.
2549: 'symb='.$symb.'&student='.$uname.
2550: '&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
2551: }
2552: ##############################################
2553:
2554: =pod
2555:
2556: =item * &pgrdlink()
2557:
2558: Inputs: $text $uname $udom $symb $target
2559:
2560: Returns: A link to grades.pm such as to see the PGRD view of a student
2561:
2562: =cut
2563:
2564: ###############################################
2565: sub pgrdlink {
2566: my $link=&submlink(@_);
2567: $link=~s/(&command=submission)/$1&showgrading=yes/;
2568: return $link;
2569: }
2570: ##############################################
2571:
2572: =pod
2573:
2574: =item * &pprmlink()
2575:
2576: Inputs: $text $uname $udom $symb $target
2577:
2578: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283 albertel 2579: student and a specific resource
1.242 albertel 2580:
2581: =cut
2582:
2583: ###############################################
2584: sub pprmlink {
2585: my ($text,$uname,$udom,$symb,$target)=@_;
2586: if (!($uname && $udom)) {
2587: (my $cursymb, my $courseid,$udom,$uname)=
2588: &Apache::lonxml::whichuser($symb);
2589: if (!$symb) { $symb=$cursymb; }
2590: }
1.254 matthew 2591: if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369 www 2592: $symb=&escape($symb);
1.242 albertel 2593: if ($target) { $target="target=\"$target\""; }
2594: return '<a href="/adm/parmset?&command=set&'.
2595: 'symb='.$symb.'&uname='.$uname.
2596: '&udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116 albertel 2597: }
2598: ##############################################
1.37 matthew 2599:
1.112 bowersj2 2600: =pod
2601:
2602: =back
2603:
2604: =cut
2605:
1.37 matthew 2606: ###############################################
1.51 www 2607:
2608:
2609: sub timehash {
2610: my @ltime=localtime(shift);
2611: return ( 'seconds' => $ltime[0],
2612: 'minutes' => $ltime[1],
2613: 'hours' => $ltime[2],
2614: 'day' => $ltime[3],
2615: 'month' => $ltime[4]+1,
2616: 'year' => $ltime[5]+1900,
2617: 'weekday' => $ltime[6],
2618: 'dayyear' => $ltime[7]+1,
2619: 'dlsav' => $ltime[8] );
2620: }
2621:
1.370 www 2622: sub utc_string {
2623: my ($date)=@_;
1.371 www 2624: return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370 www 2625: }
2626:
1.51 www 2627: sub maketime {
2628: my %th=@_;
2629: return POSIX::mktime(
2630: ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210 www 2631: $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70 www 2632: }
2633:
2634: #########################################
1.51 www 2635:
2636: sub findallcourses {
1.355 albertel 2637: my ($roles) = @_;
2638: my %roles;
2639: if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348 albertel 2640: my %courses;
1.51 www 2641: my $now=time;
1.348 albertel 2642: foreach my $key (keys(%env)) {
2643: if ( $key=~m{^user\.role\.(\w+)\./(\w+)/(\w+)} ) {
2644: my ($role,$domain,$id) = ($1,$2,$3);
2645: next if ($role eq 'ca' || $role eq 'aa');
1.355 albertel 2646: next if (%roles && !exists($roles{$role}));
1.354 albertel 2647: my ($starttime,$endtime)=split(/\./,$env{$key});
1.51 www 2648: my $active=1;
2649: if ($starttime) {
2650: if ($now<$starttime) { $active=0; }
2651: }
2652: if ($endtime) {
2653: if ($now>$endtime) { $active=0; }
2654: }
1.348 albertel 2655: if ($active) { $courses{$domain.'_'.$id}=1; }
1.51 www 2656: }
2657: }
1.348 albertel 2658: return keys(%courses);
1.51 www 2659: }
1.37 matthew 2660:
1.54 www 2661: ###############################################
1.60 matthew 2662: ###############################################
2663:
2664: =pod
2665:
1.112 bowersj2 2666: =head1 Domain Template Functions
2667:
2668: =over 4
2669:
2670: =item * &determinedomain()
1.60 matthew 2671:
2672: Inputs: $domain (usually will be undef)
2673:
1.63 www 2674: Returns: Determines which domain should be used for designs
1.60 matthew 2675:
2676: =cut
1.54 www 2677:
1.60 matthew 2678: ###############################################
1.63 www 2679: sub determinedomain {
2680: my $domain=shift;
2681: if (! $domain) {
1.60 matthew 2682: # Determine domain if we have not been given one
2683: $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1.258 albertel 2684: if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
2685: if ($env{'request.role.domain'}) {
2686: $domain=$env{'request.role.domain'};
1.60 matthew 2687: }
2688: }
1.63 www 2689: return $domain;
2690: }
2691: ###############################################
2692: =pod
2693:
1.112 bowersj2 2694: =item * &domainlogo()
1.63 www 2695:
2696: Inputs: $domain (usually will be undef)
2697:
2698: Returns: A link to a domain logo, if the domain logo exists.
2699: If the domain logo does not exist, a description of the domain.
2700:
2701: =cut
1.112 bowersj2 2702:
1.63 www 2703: ###############################################
2704: sub domainlogo {
2705: my $domain = &determinedomain(shift);
2706: # See if there is a logo
1.59 www 2707: if (-e '/home/httpd/html/adm/lonDomLogos/'.$domain.'.gif') {
1.215 albertel 2708: my $logo=&lonhttpdurl("/adm/lonDomLogos/$domain.gif");
2709: return '<img src="'.$logo.'" alt="'.$domain.'" />';
1.60 matthew 2710: } elsif(exists($Apache::lonnet::domaindescription{$domain})) {
2711: return $Apache::lonnet::domaindescription{$domain};
1.59 www 2712: } else {
1.60 matthew 2713: return '';
1.59 www 2714: }
2715: }
1.63 www 2716: ##############################################
2717:
2718: =pod
2719:
1.112 bowersj2 2720: =item * &designparm()
1.63 www 2721:
2722: Inputs: $which parameter; $domain (usually will be undef)
2723:
2724: Returns: value of designparamter $which
2725:
2726: =cut
1.112 bowersj2 2727:
1.397 ! albertel 2728:
! 2729: sub designparm {
! 2730: my ($which,$domain)=@_;
! 2731: my $result = &designparm_real(@_);
! 2732: &Apache::lonnet::logthis(" $which $domain reulted in $result");
! 2733: return $result;
! 2734: }
! 2735:
! 2736:
1.63 www 2737: ##############################################
1.397 ! albertel 2738: sub designparm_real {
1.63 www 2739: my ($which,$domain)=@_;
1.258 albertel 2740: if ($env{'browser.blackwhite'} eq 'on') {
1.110 www 2741: if ($which=~/\.(font|alink|vlink|link)$/) {
2742: return '#000000';
2743: }
2744: if ($which=~/\.(pgbg|sidebg)$/) {
2745: return '#FFFFFF';
2746: }
2747: if ($which=~/\.tabbg$/) {
2748: return '#CCCCCC';
2749: }
2750: }
1.397 ! albertel 2751: if (exists($env{'environment.color.'.$which})) {
1.258 albertel 2752: return $env{'environment.color.'.$which};
1.96 www 2753: }
1.63 www 2754: $domain=&determinedomain($domain);
1.397 ! albertel 2755: if (exists($designhash{$domain.'.'.$which})) {
1.63 www 2756: return $designhash{$domain.'.'.$which};
2757: } else {
2758: return $designhash{'default.'.$which};
2759: }
2760: }
1.59 www 2761:
1.60 matthew 2762: ###############################################
2763: ###############################################
2764:
2765: =pod
2766:
1.112 bowersj2 2767: =back
2768:
2769: =head1 HTTP Helpers
2770:
2771: =over 4
2772:
2773: =item * &bodytag()
1.60 matthew 2774:
2775: Returns a uniform header for LON-CAPA web pages.
2776:
2777: Inputs:
2778:
1.112 bowersj2 2779: =over 4
2780:
2781: =item * $title, A title to be displayed on the page.
2782:
2783: =item * $function, the current role (can be undef).
2784:
2785: =item * $addentries, extra parameters for the <body> tag.
2786:
2787: =item * $bodyonly, if defined, only return the <body> tag.
2788:
2789: =item * $domain, if defined, force a given domain.
2790:
2791: =item * $forcereg, if page should register as content page (relevant for
1.86 www 2792: text interface only)
1.60 matthew 2793:
1.326 albertel 2794: =item * $customtitle, alternate text to use instead of $title
2795: in the title box that appears, this text
2796: is not auto translated like the $title is
1.309 albertel 2797:
2798: =item * $notopbar, if true, keep the 'what is this' info but remove the
2799: navigational links
1.317 albertel 2800:
1.338 albertel 2801: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
2802:
2803: =item * $notitle, if true keep the nav controls, but remove the title bar
2804:
1.361 albertel 2805: =item * $no_inline_link, if true and in remote mode, don't show the
2806: 'Switch To Inline Menu' link
2807:
1.317 albertel 2808:
1.112 bowersj2 2809: =back
2810:
1.60 matthew 2811: Returns: A uniform header for LON-CAPA web pages.
2812: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
2813: If $bodyonly is undef or zero, an html string containing a <body> tag and
2814: other decorations will be returned.
2815:
2816: =cut
2817:
1.54 www 2818: sub bodytag {
1.309 albertel 2819: my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
1.361 albertel 2820: $notopbar,$bgcolor,$notitle,$no_inline_link)=@_;
1.339 albertel 2821:
1.117 www 2822: $title=&mt($title);
1.339 albertel 2823:
1.183 matthew 2824: $function = &get_users_function() if (!$function);
1.339 albertel 2825: my $img = &designparm($function.'.img',$domain);
2826: my $font = &designparm($function.'.font',$domain);
2827: my $pgbg = $bgcolor || &designparm($function.'.pgbg',$domain);
2828:
2829: my %design = ( 'style' => 'margin-top: 0px',
2830: 'bgcolor' => $pgbg,
2831: 'text' => $font,
2832: 'alink' => &designparm($function.'.alink',$domain),
2833: 'vlink' => &designparm($function.'.vlink',$domain),
2834: 'link' => &designparm($function.'.link',$domain),);
2835: @$addentries{keys(%design)} = @design{keys(%design)};
2836:
1.63 www 2837: # role and realm
1.378 raeburn 2838: my ($role,$realm) = split(/\./,$env{'request.role'},2);
2839: if ($role eq 'ca') {
2840: my ($rdom,$rname) = ($realm =~ m-^/(\w+)/(\w+)$-);
2841: $realm = &plainname($rname,$rdom).':'.$rdom;
2842: }
1.55 www 2843: # realm
1.258 albertel 2844: if ($env{'request.course.id'}) {
1.378 raeburn 2845: if ($env{'request.role'} !~ /^cr/) {
2846: $role = &Apache::lonnet::plaintext($role,&course_type());
2847: }
1.359 albertel 2848: $realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378 raeburn 2849: } else {
2850: $role = &Apache::lonnet::plaintext($role);
1.54 www 2851: }
1.359 albertel 2852: if (!$realm) { $realm=' '; }
1.55 www 2853: # Set messages
1.60 matthew 2854: my $messages=&domainlogo($domain);
1.101 www 2855: # Port for miniserver
1.83 albertel 2856: my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'};
2857: if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; }
1.330 albertel 2858:
2859: my $extra_body_attr = &make_attr_string($forcereg,$addentries);
1.329 albertel 2860:
1.101 www 2861: # construct main body tag
1.359 albertel 2862: my $bodytag = "<body $extra_body_attr>".
2863: &Apache::lontexconvert::init_math_support();
1.252 albertel 2864:
1.332 albertel 2865: if ($bodyonly
2866: || ($env{'request.state'} eq 'construct'
2867: && $env{'environment.remote'} ne 'off' )) {
1.60 matthew 2868: return $bodytag;
1.258 albertel 2869: } elsif ($env{'browser.interface'} eq 'textual') {
1.95 www 2870: # Accessibility
1.224 raeburn 2871:
1.337 albertel 2872: $bodytag.=&Apache::lonmenu::menubuttons($forcereg,$forcereg);
1.338 albertel 2873: if (!$notitle) {
1.337 albertel 2874: $bodytag.='<h1>LON-CAPA: '.$title.'</h1>';
2875: }
2876: return $bodytag;
1.359 albertel 2877: }
2878:
2879:
2880:
2881: my $roleinfo=(<<ENDROLE);
2882: <td class="LC_title_bar_who">
2883: <div class="LC_title_bar_name">
1.258 albertel 2884: $env{'environment.firstname'}
2885: $env{'environment.middlename'}
2886: $env{'environment.lastname'}
2887: $env{'environment.generation'}
1.361 albertel 2888:
1.359 albertel 2889: </div>
2890: <div class="LC_title_bar_role">
1.361 albertel 2891: $role
1.359 albertel 2892: </div>
2893: <div class="LC_title_bar_realm">
1.361 albertel 2894: $realm
1.359 albertel 2895: </div>
1.206 albertel 2896: </td>
2897: ENDROLE
1.235 raeburn 2898:
1.359 albertel 2899: my $titleinfo = '<span class="LC_title_bar_title">'.$title.'</span>';
2900: if ($customtitle) {
2901: $titleinfo = $customtitle;
2902: }
2903: #
2904: # Extra info if you are the DC
2905: my $dc_info = '';
2906: if ($env{'user.adv'} && exists($env{'user.role.dc./'.
2907: $env{'course.'.$env{'request.course.id'}.
2908: '.domain'}.'/'})) {
2909: my $cid = $env{'request.course.id'};
2910: $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380 www 2911: $dc_info =~ s/\s+$//;
1.359 albertel 2912: $dc_info = '('.$dc_info.')';
2913: }
2914:
2915: if ($env{'environment.remote'} eq 'off') {
2916: # No Remote
1.258 albertel 2917: if ($env{'request.state'} eq 'construct') {
1.359 albertel 2918: $forcereg=1;
2919: }
2920:
2921: if (!$customtitle && $env{'request.state'} eq 'construct') {
2922: # this is for resources; directories have customtitle, and crumbs
2923: # and select recent are created in lonpubdir.pm
1.229 albertel 2924: my ($uname,$thisdisfn)=
1.258 albertel 2925: ($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229 albertel 2926: my $formaction='/priv/'.$uname.'/'.$thisdisfn;
2927: $formaction=~s/\/+/\//g;
2928:
1.359 albertel 2929: my $parentpath = '';
2930: my $lastitem = '';
2931: if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
2932: $parentpath = $1;
2933: $lastitem = $2;
2934: } else {
2935: $lastitem = $thisdisfn;
2936: }
2937: $titleinfo =
2938: &Apache::loncommon::help_open_menu('','','','',3,'Authoring').
2939: '<b>Construction Space</b>: '.
2940: '<form name="dirs" method="post" action="'.$formaction
2941: .'" target="_top"><tt><b>'
2942: .&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
2943: .&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
2944: .'</form>'
2945: .&Apache::lonmenu::constspaceform();
1.235 raeburn 2946: }
1.359 albertel 2947:
1.337 albertel 2948: my $titletable;
1.338 albertel 2949: if (!$notitle) {
1.337 albertel 2950: $titletable =
1.359 albertel 2951: '<table id="LC_title_bar">'.
2952: "<tr><td> $titleinfo $dc_info</td>".$roleinfo.
2953: '</tr></table>';
1.337 albertel 2954: }
1.359 albertel 2955: if ($notopbar) {
2956: $bodytag .= $titletable;
2957: } else {
2958: if ($env{'request.state'} eq 'construct') {
1.337 albertel 2959: $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg,
2960: $titletable);
1.272 raeburn 2961: } else {
1.336 albertel 2962: $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg).
1.359 albertel 2963: $titletable;
1.272 raeburn 2964: }
1.235 raeburn 2965: }
2966: return $bodytag;
1.94 www 2967: }
1.95 www 2968:
1.93 www 2969: #
1.95 www 2970: # Top frame rendering, Remote is up
1.93 www 2971: #
1.359 albertel 2972:
2973: my $upperleft='<img src="http://'.$ENV{'HTTP_HOST'}.':'.
2974: $lonhttpdPort.$img.'" alt="'.$function.'" />';
2975:
1.305 www 2976: # Explicit link to get inline menu
1.361 albertel 2977: my $menu= ($no_inline_link?''
2978: :'<br /><a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
1.245 matthew 2979: #
1.338 albertel 2980: if ($notitle) {
1.337 albertel 2981: return $bodytag;
2982: }
1.94 www 2983: return(<<ENDBODY);
1.60 matthew 2984: $bodytag
1.359 albertel 2985: <table id="LC_title_bar" class="LC_with_remote">
1.368 albertel 2986: <tr><td class="LC_title_bar_role_logo">$upperleft</td>
1.359 albertel 2987: <td class="LC_title_bar_domain_logo">$messages </td>
1.54 www 2988: </tr>
1.359 albertel 2989: <tr><td>$titleinfo $dc_info $menu</td>
2990: $roleinfo
1.368 albertel 2991: </tr>
1.356 albertel 2992: </table>
1.54 www 2993: ENDBODY
1.182 matthew 2994: }
2995:
1.330 albertel 2996: sub make_attr_string {
2997: my ($register,$attr_ref) = @_;
2998:
2999: if ($attr_ref && !ref($attr_ref)) {
3000: die("addentries Must be a hash ref ".
3001: join(':',caller(1))." ".
3002: join(':',caller(0))." ");
3003: }
3004:
3005: if ($register) {
1.339 albertel 3006: my ($on_load,$on_unload);
3007: foreach my $key (keys(%{$attr_ref})) {
3008: if (lc($key) eq 'onload') {
3009: $on_load.=$attr_ref->{$key}.';';
3010: delete($attr_ref->{$key});
3011:
3012: } elsif (lc($key) eq 'onunload') {
3013: $on_unload.=$attr_ref->{$key}.';';
3014: delete($attr_ref->{$key});
3015: }
3016: }
3017: $attr_ref->{'onload'} =
3018: &Apache::lonmenu::loadevents(). $on_load;
3019: $attr_ref->{'onunload'}=
3020: &Apache::lonmenu::unloadevents().$on_unload;
3021: }
3022:
3023: # Accessibility font enhance
3024: if ($env{'browser.fontenhance'} eq 'on') {
3025: my $style;
3026: foreach my $key (keys(%{$attr_ref})) {
3027: if (lc($key) eq 'style') {
3028: $style.=$attr_ref->{$key}.';';
3029: delete($attr_ref->{$key});
3030: }
3031: }
3032: $attr_ref->{'style'}=$style.'; font-size: x-large;';
1.330 albertel 3033: }
1.339 albertel 3034:
3035: if ($env{'browser.blackwhite'} eq 'on') {
3036: delete($attr_ref->{'font'});
3037: delete($attr_ref->{'link'});
3038: delete($attr_ref->{'alink'});
3039: delete($attr_ref->{'vlink'});
3040: delete($attr_ref->{'bgcolor'});
3041: delete($attr_ref->{'background'});
3042: }
3043:
1.330 albertel 3044: my $attr_string;
3045: foreach my $attr (keys(%$attr_ref)) {
3046: $attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
3047: }
3048: return $attr_string;
3049: }
3050:
3051:
1.182 matthew 3052: ###############################################
1.251 albertel 3053: ###############################################
3054:
3055: =pod
3056:
3057: =back
3058:
1.306 albertel 3059: =head1 HTML Helpers
1.251 albertel 3060:
3061: =over 4
3062:
3063: =item * &endbodytag()
3064:
3065: Returns a uniform footer for LON-CAPA web pages.
3066:
1.306 albertel 3067: Inputs: none
1.251 albertel 3068:
3069: =back
3070:
3071: =cut
3072:
3073: sub endbodytag {
3074: my $endbodytag='</body>';
1.269 albertel 3075: $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315 albertel 3076: if ( exists( $env{'internal.head.redirect'} ) ) {
3077: $endbodytag=
3078: "<br /><a href=\"$env{'internal.head.redirect'}\">".
3079: &mt('Continue').'</a>'.
3080: $endbodytag;
3081: }
1.251 albertel 3082: return $endbodytag;
3083: }
3084:
1.352 albertel 3085: =pod
3086:
3087: =over 4
3088:
3089: =item * &standard_css()
3090:
3091: Returns a style sheet
3092:
3093: Inputs: (all optional)
3094: domain -> force to color decorate a page for a specific
3095: domain
3096: function -> force usage of a specific rolish color scheme
3097: bgcolor -> override the default page bgcolor
3098:
3099: =back
3100:
3101: =cut
3102:
1.343 albertel 3103: sub standard_css {
1.345 albertel 3104: my ($function,$domain,$bgcolor) = @_;
1.352 albertel 3105: $function = &get_users_function() if (!$function);
3106: my $img = &designparm($function.'.img', $domain);
3107: my $tabbg = &designparm($function.'.tabbg', $domain);
3108: my $font = &designparm($function.'.font', $domain);
1.345 albertel 3109: my $sidebg = &designparm($function.'.sidebg',$domain);
1.382 albertel 3110: my $pgbg_or_bgcolor =
3111: $bgcolor ||
1.352 albertel 3112: &designparm($function.'.pgbg', $domain);
1.382 albertel 3113: my $pgbg = &designparm($function.'.pgbg', $domain);
1.352 albertel 3114: my $alink = &designparm($function.'.alink', $domain);
3115: my $vlink = &designparm($function.'.vlink', $domain);
3116: my $link = &designparm($function.'.link', $domain);
3117:
3118: my $sans = 'Arial,Helvetica,sans-serif';
1.395 albertel 3119: my $mono = 'monospace';
1.352 albertel 3120: my $data_table_head = $tabbg;
3121: my $data_table_light = '#EEEEEE';
3122: my $data_table_dark = '#DDD';
1.349 albertel 3123: my $data_table_highlight = '#FFFF00';
1.352 albertel 3124: my $mail_new = '#FFBB77';
3125: my $mail_new_hover = '#DD9955';
3126: my $mail_read = '#BBBB77';
3127: my $mail_read_hover = '#999944';
3128: my $mail_replied = '#AAAA88';
3129: my $mail_replied_hover = '#888855';
3130: my $mail_other = '#99BBBB';
3131: my $mail_other_hover = '#669999';
1.391 albertel 3132: my $table_header = '#DDDDDD';
1.392 albertel 3133:
3134: my $border = ($env{'browser.type'} eq 'explorer') ? '0px 2px 0px 2px'
3135: : '0px 3px 0px 4px';
1.343 albertel 3136: return <<END;
1.345 albertel 3137: h1, h2, h3, th { font-family: $sans }
1.343 albertel 3138: a:focus { color: red; background: yellow }
3139: table.thinborder { border-collapse: collapse; }
3140: table.thinborder tr th, table.thinborder tr td { border-style: solid; border-width: 1px}
3141: form, .inline { display: inline; }
3142: .center { text-align: center; }
1.395 albertel 3143: .LC_filename {font-family: $mono;}
1.350 albertel 3144: .LC_error {
3145: color: red;
3146: font-size: larger;
3147: }
1.394 albertel 3148: .LC_warning {
3149: color: red;
3150: }
1.350 albertel 3151: .LC_success {
3152: color: green;
3153: }
1.346 albertel 3154:
1.392 albertel 3155: table#LC_top_nav, table#LC_menubuttons {
1.345 albertel 3156: width: 100%;
3157: background: $pgbg;
1.392 albertel 3158: border: 2px;
3159: border-collapse: seperate;
1.345 albertel 3160: }
1.392 albertel 3161:
1.393 albertel 3162: table#LC_title_bar, table#LC_breadcrumbs, table#LC_nav_location,
3163: table#LC_title_bar.LC_with_remote {
1.359 albertel 3164: width: 100%;
1.392 albertel 3165: border-color: $pgbg;
3166: border-style: solid;
3167: border-width: $border;
3168:
1.379 albertel 3169: background: $pgbg;
3170: font-family: $sans;
1.392 albertel 3171: border-collapse: collapse;
1.359 albertel 3172: }
1.392 albertel 3173:
1.359 albertel 3174: table#LC_title_bar td {
3175: padding: 3px;
3176: background: $tabbg;
3177: }
3178: table#LC_title_bar td.LC_title_bar_who {
3179: background: $tabbg;
3180: color: $font;
3181: font: medium $sans;
3182: text-align: right;
3183: }
3184: span.LC_title_bar_title {
3185: font: bold xx-large $sans;
3186: }
3187: table#LC_title_bar td.LC_title_bar_domain_logo {
3188: background: $sidebg;
3189: text-align: right;
1.368 albertel 3190: padding: 0px;
3191: }
3192: table#LC_title_bar td.LC_title_bar_role_logo {
3193: background: $sidebg;
3194: padding: 0px;
1.359 albertel 3195: }
3196:
1.346 albertel 3197: table#LC_menubuttons_mainmenu {
3198: background: $pgbg;
3199: border: 0px;
3200: border-spacing: 1px;
1.372 albertel 3201: padding: 0px 1px;
1.346 albertel 3202: margin: 0px;
3203: border-collapse: separate;
3204: }
3205: table#LC_menubuttons img, table#LC_menubuttons_mainmenu img {
3206: border: 0px;
3207: }
1.345 albertel 3208: table#LC_top_nav td {
3209: background: $tabbg;
1.392 albertel 3210: border: 0px;
1.345 albertel 3211: }
3212: table#LC_top_nav td a, div#LC_top_nav a {
3213: color: $font;
3214: font-family: $sans;
3215: }
1.364 albertel 3216: table#LC_top_nav td.LC_top_nav_logo {
3217: background: $tabbg;
3218: text-align: right;
3219: }
1.357 albertel 3220: table#LC_breadcrumbs td {
3221: background: $tabbg;
3222: color: $font;
3223: font-family: $sans;
1.358 albertel 3224: font-size: smaller;
1.357 albertel 3225: }
3226: table#LC_breadcrumbs td.LC_breadcrumb_component {
3227: background: $tabbg;
3228: color: $font;
3229: font-family: $sans;
3230: font-size: larger;
3231: text-align: right;
3232: }
1.383 albertel 3233: td.LC_table_cell_checkbox {
3234: text-align: center;
3235: }
3236:
1.346 albertel 3237: .LC_menubuttons_inline_text {
3238: color: $font;
3239: font-family: $sans;
3240: font-size: smaller;
3241: }
3242:
3243: td.LC_menubuttons_text {
3244: color: $font;
3245: font-family: $sans;
3246: }
3247: td.LC_menubuttons_img {
3248: background: $tabbg;
3249: }
3250: .LC_current_location {
3251: font-family: $sans;
3252: background: $tabbg;
3253: }
3254: .LC_new_mail {
3255: font-family: $sans;
3256: font-weight: bold;
3257: }
1.347 albertel 3258:
1.349 albertel 3259: table.LC_data_table, table.LC_mail_list {
1.347 albertel 3260: border: 1px solid #000000;
3261: border-collapse: seperate;
3262: }
1.349 albertel 3263: table.LC_data_table tr th, table.LC_calendar tr th, table.LC_mail_list tr th {
3264: font-weight: bold;
3265: background-color: $data_table_head;
1.347 albertel 3266: }
3267: table.LC_data_table tr td {
1.349 albertel 3268: background-color: $data_table_light;
1.347 albertel 3269: }
3270: table.LC_data_table tr.LC_even_row td {
1.349 albertel 3271: background-color: $data_table_dark;
1.347 albertel 3272: }
3273: table.LC_data_table tr.LC_empty td {
3274: background-color: #FFFFFF;
3275: }
3276:
1.349 albertel 3277: table.LC_calendar {
3278: border: 1px solid #000000;
3279: border-collapse: collapse;
3280: }
3281: table.LC_calendar_pickdate {
3282: font-size: xx-small;
3283: }
3284: table.LC_calendar tr td {
3285: border: 1px solid #000000;
3286: vertical-align: top;
3287: }
3288: table.LC_calendar tr td.LC_calendar_day_empty {
3289: background-color: $data_table_dark;
3290: }
3291: table.LC_calendar tr td.LC_calendar_day_current {
3292: background-color: $data_table_highlight;
3293: }
3294:
3295: table.LC_mail_list tr.LC_mail_new {
3296: background-color: $mail_new;
3297: }
3298: table.LC_mail_list tr.LC_mail_new:hover {
3299: background-color: $mail_new_hover;
3300: }
3301: table.LC_mail_list tr.LC_mail_read {
3302: background-color: $mail_read;
3303: }
3304: table.LC_mail_list tr.LC_mail_read:hover {
3305: background-color: $mail_read_hover;
3306: }
3307: table.LC_mail_list tr.LC_mail_replied {
3308: background-color: $mail_replied;
3309: }
3310: table.LC_mail_list tr.LC_mail_replied:hover {
3311: background-color: $mail_replied_hover;
3312: }
3313: table.LC_mail_list tr.LC_mail_other {
3314: background-color: $mail_other;
3315: }
3316: table.LC_mail_list tr.LC_mail_other:hover {
3317: background-color: $mail_other_hover;
3318: }
1.385 albertel 3319:
1.386 albertel 3320: table#LC_portfolio_actions {
3321: width: auto;
3322: background: $pgbg;
3323: border: 0px;
3324: border-spacing: 2px 2px;
3325: padding: 0px;
3326: margin: 0px;
3327: border-collapse: separate;
3328: }
3329: table#LC_portfolio_actions td.LC_label {
3330: background: $tabbg;
3331: text-align: right;
3332: }
3333: table#LC_portfolio_actions td.LC_value {
3334: background: $tabbg;
3335: }
1.385 albertel 3336:
1.391 albertel 3337: table#LC_cstr_controls {
3338: width: 100%;
3339: border-collapse: collapse;
3340: }
3341: table#LC_cstr_controls tr td {
3342: border: 4px solid $pgbg;
3343: padding: 4px;
3344: text-align: center;
3345: background: $tabbg;
3346: }
3347: table#LC_cstr_controls tr th {
3348: border: 4px solid $pgbg;
3349: background: $table_header;
3350: text-align: center;
3351: font-family: $sans;
3352: font-size: smaller;
3353: }
3354:
1.389 albertel 3355: table#LC_browser {
3356:
3357: }
3358: table#LC_browser tr th {
1.391 albertel 3359: background: $table_header;
1.389 albertel 3360: }
1.390 albertel 3361: table#LC_browser tr td {
3362: padding: 2px;
3363: }
1.389 albertel 3364: table#LC_browser tr.LC_browser_file,
3365: table#LC_browser tr.LC_browser_file_published {
3366: background: #CCFF88;
3367: }
3368: table#LC_browser tr.LC_browser_file_locked,
3369: table#LC_browser tr.LC_browser_file_unpublished {
3370: background: #FFAA99;
1.387 albertel 3371: }
1.389 albertel 3372: table#LC_browser tr.LC_browser_file_obsolete {
3373: background: #AAAAAA;
1.387 albertel 3374: }
1.389 albertel 3375: table#LC_browser tr.LC_browser_file_modified {
3376: background: #FFFF77;
1.387 albertel 3377: }
1.389 albertel 3378: table#LC_browser tr.LC_browser_folder {
3379: background: #CCCCFF;
1.387 albertel 3380: }
1.388 albertel 3381: span.LC_current_location {
3382: font-size: x-large;
3383: background: $pgbg;
3384: }
1.387 albertel 3385:
1.395 albertel 3386: span.LC_parm_menu_item {
3387: font-size: larger;
3388: font-family: $sans;
3389: }
3390: span.LC_parm_scope_all {
3391: color: red;
3392: }
3393: span.LC_parm_scope_folder {
3394: color: green;
3395: }
3396: span.LC_parm_scope_resource {
3397: color: orange;
3398: }
3399: span.LC_parm_part {
3400: color: blue;
3401: }
3402: span.LC_parm_folder, span.LC_parm_symb {
3403: font-size: x-small;
3404: font-family: $mono;
3405: color: #AAAAAA;
3406: }
3407:
1.396 albertel 3408: td.LC_parm_overview_level_menu, td.LC_parm_overview_map_menu,
3409: td.LC_parm_overview_parm_selectors, td.LC_parm_overview_parm_restrictions {
3410: border: 1px solid black;
3411: border-collapse: collapse;
3412: }
3413: table.LC_parm_overview_restrictions td {
3414: border-width: 1px 4px 1px 4px;
3415: border-style: solid;
3416: border-color: $pgbg;
3417: text-align: center;
3418: }
3419: table.LC_parm_overview_restrictions th {
3420: background: $tabbg;
3421: border-width: 1px 4px 1px 4px;
3422: border-style: solid;
3423: border-color: $pgbg;
3424: }
1.397 ! albertel 3425: table#LC_helpmenu_links {
! 3426: width: 100%;
! 3427: border: 1px solid black;
! 3428: background: $pgbg;
! 3429: padding: 0px;
! 3430: border-spacing: 1px;
! 3431: }
! 3432: table#LC_helpmenu_links tr td {
! 3433: padding: 1px;
! 3434: background: $tabbg;
! 3435: }
1.396 albertel 3436:
1.397 ! albertel 3437: table#LC_helpmenu_links a:link, table#LC_helpmenu_links a:visited,
! 3438: table#LC_helpmenu_links a:active {
! 3439: text-decoration: none;
! 3440: color: $font;
! 3441: }
! 3442: table#LC_helpmenu_links a:hover {
! 3443: text-decoration: underline;
! 3444: color: $vlink;
! 3445: }
1.396 albertel 3446:
1.343 albertel 3447: END
3448: }
3449:
1.306 albertel 3450: =pod
3451:
3452: =over 4
3453:
3454: =item * &headtag()
3455:
3456: Returns a uniform footer for LON-CAPA web pages.
3457:
1.307 albertel 3458: Inputs: $title - optional title for the head
3459: $head_extra - optional extra HTML to put inside the <head>
1.315 albertel 3460: $args - optional arguments
1.319 albertel 3461: force_register - if is true call registerurl so the remote is
3462: informed
1.352 albertel 3463: redirect -> array ref of seconds before redirect occurs
1.315 albertel 3464: url to redirect to
3465: (side effect of setting
3466: $env{'internal.head.redirect'} to the url
3467: redirected too)
1.352 albertel 3468: domain -> force to color decorate a page for a specific
3469: domain
3470: function -> force usage of a specific rolish color scheme
3471: bgcolor -> override the default page bgcolor
3472:
1.306 albertel 3473: =back
3474:
3475: =cut
3476:
3477: sub headtag {
1.313 albertel 3478: my ($title,$head_extra,$args) = @_;
1.306 albertel 3479:
1.363 albertel 3480: my $function = $args->{'function'} || &get_users_function();
3481: my $domain = $args->{'domain'} || &determinedomain();
3482: my $bgcolor = $args->{'bgcolor'} || &designparm($function.'.pgbg',$domain);
1.391 albertel 3483: my $url = join(':',$env{'user.name'},$env{'user.domain'},time(),
3484: #$env{'environment.color.timestamp'},
1.363 albertel 3485: $function,$domain,$bgcolor);
3486:
1.369 www 3487: $url = '/adm/css/'.&escape($url).'.css';
1.363 albertel 3488:
1.308 albertel 3489: my $result =
3490: '<head>'.
1.363 albertel 3491: '<link rel="stylesheet" type="text/css" href="'.$url.'" />'.
1.340 albertel 3492: &font_settings().
1.308 albertel 3493: &Apache::lonhtmlcommon::htmlareaheaders();
1.319 albertel 3494:
3495: if ($args->{'force_register'}) {
3496: $result .= &Apache::lonmenu::registerurl(1);
3497: }
3498:
1.314 albertel 3499: if (ref($args->{'redirect'})) {
3500: my ($time,$url) = @{$args->{'redirect'}};
1.315 albertel 3501: $url = &Apache::lonenc::check_encrypt($url);
3502: $env{'internal.head.redirect'} = $url;
1.313 albertel 3503: $result.=<<ADDMETA
3504: <meta http-equiv="pragma" content="no-cache" />
1.344 albertel 3505: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313 albertel 3506: ADDMETA
3507: }
1.306 albertel 3508: if (!defined($title)) {
3509: $title = 'The LearningOnline Network with CAPA';
3510: }
3511:
1.315 albertel 3512: $result .= '<title> LON-CAPA '.&mt($title).'</title>'.$head_extra;
1.306 albertel 3513: return $result;
3514: }
3515:
3516: =pod
3517:
3518: =over 4
3519:
1.340 albertel 3520: =item * &font_settings()
3521:
3522: Returns neccessary <meta> to set the proper encoding
3523:
3524: Inputs: none
3525:
3526: =back
3527:
3528: =cut
3529:
3530: sub font_settings {
3531: my $headerstring='';
3532: if (($env{'browser.os'} eq 'mac') && (!$env{'browser.mathml'})) {
3533: $headerstring.=
3534: '<meta Content-Type="text/html; charset=x-mac-roman" />';
3535: } elsif (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
3536: $headerstring.=
3537: '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
3538: }
3539: return $headerstring;
3540: }
3541:
1.341 albertel 3542: =pod
3543:
3544: =over 4
3545:
3546: =item * &xml_begin()
3547:
3548: Returns the needed doctype and <html>
3549:
3550: Inputs: none
3551:
3552: =back
3553:
3554: =cut
3555:
3556: sub xml_begin {
3557: my $output='';
3558:
1.342 albertel 3559: &Apache::lonhtmlcommon::init_htmlareafields();
3560:
1.341 albertel 3561: if ($env{'browser.mathml'}) {
3562: $output='<?xml version="1.0"?>'
3563: #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
3564: # .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
3565:
3566: # .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">] >'
3567: .'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
3568: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
3569: .'xmlns="http://www.w3.org/1999/xhtml">';
3570: } else {
3571: $output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
3572: }
3573: return $output;
3574: }
1.340 albertel 3575:
3576: =pod
3577:
3578: =over 4
3579:
1.306 albertel 3580: =item * &endheadtag()
3581:
3582: Returns a uniform </head> for LON-CAPA web pages.
3583:
3584: Inputs: none
3585:
3586: =back
3587:
3588: =cut
3589:
3590: sub endheadtag {
3591: return '</head>';
3592: }
3593:
3594: =pod
3595:
3596: =over 4
3597:
3598: =item * &head()
3599:
3600: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
3601:
3602: Inputs: $title - optional title for the page
1.307 albertel 3603: $head_extra - optional extra HTML to put inside the <head>
1.306 albertel 3604: =back
3605:
3606: =cut
3607:
3608: sub head {
1.325 albertel 3609: my ($title,$head_extra,$args) = @_;
3610: return &headtag($title,$head_extra,$args).&endheadtag();
1.306 albertel 3611: }
3612:
3613: =pod
3614:
3615: =over 4
3616:
3617: =item * &start_page()
3618:
3619: Returns a complete <html> .. <body> section for LON-CAPA web pages.
3620:
3621: Inputs: $title - optional title for the page
3622: $head_extra - optional extra HTML to incude inside the <head>
1.315 albertel 3623: $args - additional optional args supported are:
1.317 albertel 3624: only_body -> is true will set &bodytag() onlybodytag
3625: arg on
3626: no_nav_bar -> is true will set &bodytag() notopbar arg on
3627: add_entries -> additional attributes to add to the <body>
3628: domain -> force to color decorate a page for a
3629: specific domain
3630: function -> force usage of a specific rolish color
3631: scheme
3632: redirect -> see &headtag()
3633: bgcolor -> override the default page bg color
3634: js_ready -> return a string ready for being used in
3635: a javascript writeln
1.320 albertel 3636: html_encode -> return a string ready for being used in
3637: a html attribute
1.317 albertel 3638: force_register -> if is true will turn on the &bodytag()
3639: $forcereg arg
1.326 albertel 3640: body_title -> alternate text to use instead of $title
3641: in the title box that appears, this text
3642: is not auto translated like the $title is
1.330 albertel 3643: frameset -> if true will start with a <frameset>
3644: rather than <body>
1.338 albertel 3645: no_title -> if true the title bar won't be shown
3646: skip_phases -> hash ref of
3647: head -> skip the <html><head> generation
3648: body -> skip all <body> generation
1.337 albertel 3649:
1.361 albertel 3650: no_inline_link -> if true and in remote mode, don't show the
3651: 'Switch To Inline Menu' link
3652:
1.306 albertel 3653: =back
3654:
3655: =cut
3656:
3657: sub start_page {
1.309 albertel 3658: my ($title,$head_extra,$args) = @_;
1.318 albertel 3659: #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.313 albertel 3660: my %head_args;
1.352 albertel 3661: foreach my $arg ('redirect','force_register','domain','function',
3662: 'bgcolor') {
1.319 albertel 3663: if (defined($args->{$arg})) {
1.324 raeburn 3664: $head_args{$arg} = $args->{$arg};
1.319 albertel 3665: }
1.313 albertel 3666: }
1.319 albertel 3667:
1.315 albertel 3668: $env{'internal.start_page'}++;
1.338 albertel 3669: my $result;
3670: if (! exists($args->{'skip_phases'}{'head'}) ) {
3671: $result.=
1.341 albertel 3672: &xml_begin().
1.338 albertel 3673: &headtag($title,$head_extra,\%head_args).&endheadtag();
3674: }
3675:
3676: if (! exists($args->{'skip_phases'}{'body'}) ) {
3677: if ($args->{'frameset'}) {
3678: my $attr_string = &make_attr_string($args->{'force_register'},
3679: $args->{'add_entries'});
3680: $result .= "\n<frameset $attr_string>\n";
3681: } else {
3682: $result .=
3683: &bodytag($title,
3684: $args->{'function'}, $args->{'add_entries'},
3685: $args->{'only_body'}, $args->{'domain'},
3686: $args->{'force_register'}, $args->{'body_title'},
3687: $args->{'no_nav_bar'}, $args->{'bgcolor'},
1.361 albertel 3688: $args->{'no_title'}, $args->{'no_inline_link'});
1.338 albertel 3689: }
1.330 albertel 3690: }
1.338 albertel 3691:
1.315 albertel 3692: if ($args->{'js_ready'}) {
1.317 albertel 3693: $result = &js_ready($result);
1.315 albertel 3694: }
1.320 albertel 3695: if ($args->{'html_encode'}) {
3696: $result = &html_encode($result);
3697: }
1.315 albertel 3698: return $result;
1.306 albertel 3699: }
3700:
1.330 albertel 3701:
1.306 albertel 3702: =pod
3703:
3704: =over 4
3705:
3706: =item * &head()
3707:
3708: Returns a complete </body></html> section for LON-CAPA web pages.
3709:
1.315 albertel 3710: Inputs: $args - additional optional args supported are:
3711: js_ready -> return a string ready for being used in
3712: a javascript writeln
1.320 albertel 3713: html_encode -> return a string ready for being used in
3714: a html attribute
1.330 albertel 3715: frameset -> if true will start with a <frameset>
3716: rather than <body>
1.306 albertel 3717: =back
3718:
3719: =cut
3720:
3721: sub end_page {
1.315 albertel 3722: my ($args) = @_;
3723: $env{'internal.end_page'}++;
1.330 albertel 3724: my $result;
1.335 albertel 3725: if ($args->{'discussion'}) {
3726: my ($target,$parser);
3727: if (ref($args->{'discussion'})) {
3728: ($target,$parser) =($args->{'discussion'}{'target'},
3729: $args->{'discussion'}{'parser'});
3730: }
3731: $result .= &Apache::lonxml::xmlend($target,$parser);
3732: }
3733:
1.330 albertel 3734: if ($args->{'frameset'}) {
3735: $result .= '</frameset>';
3736: } else {
3737: $result .= &endbodytag();
3738: }
3739: $result .= "\n</html>";
3740:
1.315 albertel 3741: if ($args->{'js_ready'}) {
1.317 albertel 3742: $result = &js_ready($result);
1.315 albertel 3743: }
1.335 albertel 3744:
1.320 albertel 3745: if ($args->{'html_encode'}) {
3746: $result = &html_encode($result);
3747: }
1.335 albertel 3748:
1.315 albertel 3749: return $result;
3750: }
3751:
1.320 albertel 3752: sub html_encode {
3753: my ($result) = @_;
3754:
1.322 albertel 3755: $result = &HTML::Entities::encode($result,'<>&"');
1.320 albertel 3756:
3757: return $result;
3758: }
1.317 albertel 3759: sub js_ready {
3760: my ($result) = @_;
3761:
1.323 albertel 3762: $result =~ s/[\n\r]/ /xmsg;
3763: $result =~ s/\\/\\\\/xmsg;
3764: $result =~ s/'/\\'/xmsg;
1.372 albertel 3765: $result =~ s{</}{<\\/}xmsg;
1.317 albertel 3766:
3767: return $result;
3768: }
3769:
1.315 albertel 3770: sub validate_page {
3771: if ( exists($env{'internal.start_page'})
1.316 albertel 3772: && $env{'internal.start_page'} > 1) {
3773: &Apache::lonnet::logthis('start_page called multiple times '.
1.318 albertel 3774: $env{'internal.start_page'}.' '.
1.316 albertel 3775: $ENV{'request.filename'});
1.315 albertel 3776: }
3777: if ( exists($env{'internal.end_page'})
1.316 albertel 3778: && $env{'internal.end_page'} > 1) {
3779: &Apache::lonnet::logthis('end_page called multiple times '.
1.318 albertel 3780: $env{'internal.end_page'}.' '.
1.316 albertel 3781: $env{'request.filename'});
1.315 albertel 3782: }
3783: if ( exists($env{'internal.start_page'})
3784: && ! exists($env{'internal.end_page'})) {
1.316 albertel 3785: &Apache::lonnet::logthis('start_page called without end_page '.
3786: $env{'request.filename'});
1.315 albertel 3787: }
3788: if ( ! exists($env{'internal.start_page'})
3789: && exists($env{'internal.end_page'})) {
1.316 albertel 3790: &Apache::lonnet::logthis('end_page called without start_page'.
3791: $env{'request.filename'});
1.315 albertel 3792: }
1.306 albertel 3793: }
1.315 albertel 3794:
1.318 albertel 3795: sub simple_error_page {
3796: my ($r,$title,$msg) = @_;
3797: my $page =
3798: &Apache::loncommon::start_page($title).
3799: &mt($msg).
3800: &Apache::loncommon::end_page();
3801: if (ref($r)) {
3802: $r->print($page);
1.327 albertel 3803: return;
1.318 albertel 3804: }
3805: return $page;
3806: }
1.347 albertel 3807:
3808: {
3809: my $row_count;
3810: sub start_data_table {
3811: undef($row_count);
1.389 albertel 3812: return '<table class="LC_data_table">'."\n";
1.347 albertel 3813: }
3814:
3815: sub end_data_table {
3816: undef($row_count);
1.389 albertel 3817: return '</table>'."\n";;
1.347 albertel 3818: }
3819:
3820: sub start_data_table_row {
3821: $row_count++;
1.389 albertel 3822: return '<tr '.(($row_count % 2)?'':'class="LC_even_row"').'>'."\n";;
1.347 albertel 3823: }
3824:
3825: sub end_data_table_row {
1.389 albertel 3826: return '</tr>'."\n";;
1.347 albertel 3827: }
1.367 www 3828:
3829: sub start_data_table_header_row {
1.389 albertel 3830: return '<tr class="LC_header_row">'."\n";;
1.367 www 3831: }
3832:
3833: sub end_data_table_header_row {
1.389 albertel 3834: return '</tr>'."\n";;
1.367 www 3835: }
1.347 albertel 3836: }
3837:
1.251 albertel 3838: ###############################################
1.182 matthew 3839:
3840: =pod
3841:
1.306 albertel 3842: =over 4
3843:
1.182 matthew 3844: =item get_users_function
3845:
3846: Used by &bodytag to determine the current users primary role.
3847: Returns either 'student','coordinator','admin', or 'author'.
3848:
3849: =cut
3850:
3851: ###############################################
3852: sub get_users_function {
3853: my $function = 'student';
1.258 albertel 3854: if ($env{'request.role'}=~/^(cc|in|ta|ep)/) {
1.182 matthew 3855: $function='coordinator';
3856: }
1.258 albertel 3857: if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182 matthew 3858: $function='admin';
3859: }
1.258 albertel 3860: if (($env{'request.role'}=~/^(au|ca)/) ||
1.182 matthew 3861: ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
3862: $function='author';
3863: }
3864: return $function;
1.54 www 3865: }
1.99 www 3866:
3867: ###############################################
3868:
1.233 raeburn 3869: =pod
3870:
1.274 raeburn 3871: =item check_user_status
3872:
3873: Determines current status of supplied role for a
3874: specific user. Roles can be active, previous or future.
3875:
3876: Inputs:
3877: user's domain, user's username, course's domain,
1.375 raeburn 3878: course's number, optional section ID.
1.274 raeburn 3879:
3880: Outputs:
3881: role status: active, previous or future.
3882:
3883: =cut
3884:
3885: sub check_user_status {
3886: my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
3887: my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
3888: my @uroles = keys %userinfo;
3889: my $srchstr;
3890: my $active_chk = 'none';
3891: if (@uroles > 0) {
3892: if (($role eq 'cc') || ($secgrp eq '') || (!defined($secgrp))) {
3893: $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
3894: } else {
3895: $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role; }
3896: if (grep/^$srchstr$/,@uroles) {
3897: my $role_end = 0;
3898: my $role_start = 0;
3899: $active_chk = 'active';
3900: if ($userinfo{$srchstr} =~ m/^($role)_(\d+)/) {
3901: $role_end = $2;
3902: if ($userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/) {
3903: $role_start = $3;
3904: }
3905: }
3906: if ($role_start > 0) {
3907: if (time < $role_start) {
3908: $active_chk = 'future';
3909: }
3910: }
3911: if ($role_end > 0) {
3912: if (time > $role_end) {
3913: $active_chk = 'previous';
3914: }
3915: }
3916: }
3917: }
3918: return $active_chk;
3919: }
3920:
3921: ###############################################
3922:
3923: =pod
3924:
1.233 raeburn 3925: =item get_sections
3926:
3927: Determines all the sections for a course including
3928: sections with students and sections containing other roles.
1.374 raeburn 3929: Incoming parameters: domain, course number,
3930: reference to array containing roles for which sections should
3931: be gathered (optional). If the third argument is undefined,
3932: sections are gathered for any role.
1.233 raeburn 3933:
1.374 raeburn 3934: Returns section hash (keys are section IDs, values are
3935: number of users in each section), subject to the
3936: optional roles filter.
1.233 raeburn 3937:
3938: =cut
3939:
3940: ###############################################
3941: sub get_sections {
1.366 albertel 3942: my ($cdom,$cnum,$possible_roles) = @_;
3943: if (!defined($cdom) || !defined($cnum)) {
3944: my $cid = $env{'request.course.id'};
3945:
3946: return if (!defined($cid));
3947:
3948: $cdom = $env{'course.'.$cid.'.domain'};
3949: $cnum = $env{'course.'.$cid.'.num'};
3950: }
3951:
3952: my %sectioncount;
1.240 albertel 3953:
1.366 albertel 3954: if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276 albertel 3955: my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240 albertel 3956: my $sec_index = &Apache::loncoursedata::CL_SECTION();
3957: my $status_index = &Apache::loncoursedata::CL_STATUS();
1.366 albertel 3958: while (my ($student,$data) = each(%$classlist)) {
1.240 albertel 3959: my ($section,$status) = ($data->[$sec_index],
3960: $data->[$status_index]);
3961: unless ($section eq '-1' || $section =~ /^\s*$/) {
1.366 albertel 3962: $sectioncount{$section}++;
1.240 albertel 3963: }
3964: }
3965: }
3966: my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
3967: foreach my $user (sort(keys(%courseroles))) {
3968: if ($user !~ /^(\w{2})/) { next; }
3969: my ($role) = ($user =~ /^(\w{2})/);
3970: if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
3971: my $section;
3972: if ($role eq 'cr' &&
3973: $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
3974: $section=$1;
3975: }
3976: if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
3977: if (!defined($section) || $section eq '-1') { next; }
1.366 albertel 3978: $sectioncount{$section}++;
1.233 raeburn 3979: }
1.366 albertel 3980: return %sectioncount;
1.233 raeburn 3981: }
3982:
1.274 raeburn 3983: ###############################################
1.294 raeburn 3984:
3985: =pod
1.275 raeburn 3986:
3987: =item get_course_users
3988:
3989: Retrieves usernames:domains for users in the specified course
3990: with specific role(s), and access status.
3991:
3992: Incoming parameters:
1.277 albertel 3993: 1. course domain
3994: 2. course number
3995: 3. access status: users must have - either active,
1.275 raeburn 3996: previous, future, or all.
1.277 albertel 3997: 4. reference to array of permissible roles
1.288 raeburn 3998: 5. reference to array of section restrictions (optional)
3999: 6. reference to results object (hash of hashes).
4000: 7. reference to optional userdata hash
1.275 raeburn 4001: Keys of top level hash are roles.
4002: Keys of inner hashes are username:domain, with
4003: values set to access type.
1.288 raeburn 4004: Optional userdata hash returns an array with arguments in the
4005: same order as loncoursedata::get_classlist() for student data.
4006:
4007: Entries for end, start, section and status are blank because
4008: of the possibility of multiple values for non-student roles.
4009:
1.275 raeburn 4010: =cut
4011:
4012: ###############################################
4013:
4014: sub get_course_users {
1.288 raeburn 4015: my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata) = @_;
4016: my %idx = ();
4017:
4018: $idx{udom} = &Apache::loncoursedata::CL_SDOM();
4019: $idx{uname} = &Apache::loncoursedata::CL_SNAME();
4020: $idx{end} = &Apache::loncoursedata::CL_END();
4021: $idx{start} = &Apache::loncoursedata::CL_START();
4022: $idx{id} = &Apache::loncoursedata::CL_ID();
4023: $idx{section} = &Apache::loncoursedata::CL_SECTION();
4024: $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
4025: $idx{status} = &Apache::loncoursedata::CL_STATUS();
4026:
1.290 albertel 4027: if (grep(/^st$/,@{$roles})) {
1.276 albertel 4028: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278 raeburn 4029: my $now = time;
1.277 albertel 4030: foreach my $student (keys(%{$classlist})) {
1.288 raeburn 4031: my $match = 0;
1.291 albertel 4032: if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.290 albertel 4033: unless(grep(/^\Q$$classlist{$student}[$idx{section}]\E$/,
4034: @{$sections})) {
4035: next;
4036: }
1.288 raeburn 4037: }
1.275 raeburn 4038: if (defined($$types{'active'})) {
1.288 raeburn 4039: if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275 raeburn 4040: push(@{$$users{st}{$student}},'active');
1.288 raeburn 4041: $match = 1;
1.275 raeburn 4042: }
4043: }
4044: if (defined($$types{'previous'})) {
1.288 raeburn 4045: if ($$classlist{$student}[$idx{end}] <= $now) {
1.275 raeburn 4046: push(@{$$users{st}{$student}},'previous');
1.288 raeburn 4047: $match = 1;
1.275 raeburn 4048: }
4049: }
4050: if (defined($$types{'future'})) {
1.288 raeburn 4051: if (($$classlist{$student}[$idx{start}] > $now) && ($$classlist{$student}[$idx{end}] > $now) || ($$classlist{$student}[$idx{end}] == 0) || ($$classlist{$student}[$idx{end}] eq '')) {
1.275 raeburn 4052: push(@{$$users{st}{$student}},'future');
1.288 raeburn 4053: $match = 1;
1.275 raeburn 4054: }
4055: }
1.288 raeburn 4056: if ($match && defined($userdata)) {
4057: $$userdata{$student} = $$classlist{$student};
4058: }
1.275 raeburn 4059: }
4060: }
4061: if ((@{$roles} > 0) && (@{$roles} ne "st")) {
4062: my @coursepersonnel = &Apache::lonnet::getkeys('nohist_userroles',$cdom,$cnum);
4063: foreach my $person (@coursepersonnel) {
1.288 raeburn 4064: my $match = 0;
1.275 raeburn 4065: my ($role,$user) = ($person =~ /^([^:]*):([^:]+:[^:]+)/);
4066: $user =~ s/:$//;
1.290 albertel 4067: if (($role) && (grep(/^\Q$role\E$/,@{$roles}))) {
1.288 raeburn 4068: my ($uname,$udom,$usec) = split(/:/,$user);
1.290 albertel 4069: if ($usec ne '' && (ref($sections) eq 'ARRAY') &&
4070: @{$sections} > 0) {
4071: unless(grep(/^\Q$usec\E$/,@{$sections})) {
4072: next;
4073: }
1.288 raeburn 4074: }
1.275 raeburn 4075: if ($uname ne '' && $udom ne '') {
4076: my $status = &check_user_status($udom,$uname,$cdom,$cnum,$role);
1.277 albertel 4077: foreach my $type (keys(%{$types})) {
1.275 raeburn 4078: if ($status eq $type) {
1.288 raeburn 4079: @{$$users{$role}{$user}} = $type;
4080: $match = 1;
4081: }
4082: }
1.290 albertel 4083: if ($match && defined($userdata) &&
4084: !exists($$userdata{$uname.':'.$udom})) {
4085: &get_user_info($udom,$uname,\%idx,$userdata);
1.275 raeburn 4086: }
4087: }
4088: }
4089: }
1.290 albertel 4090: if (grep(/^ow$/,@{$roles})) {
1.279 raeburn 4091: if ((defined($cdom)) && (defined($cnum))) {
4092: my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
4093: if ( defined($csettings{'internal.courseowner'}) ) {
4094: my $owner = $csettings{'internal.courseowner'};
1.288 raeburn 4095: @{$$users{'ow'}{$owner.':'.$cdom}} = 'any';
1.290 albertel 4096: if (defined($userdata) &&
4097: !exists($$userdata{$owner.':'.$cdom})) {
4098: &get_user_info($cdom,$owner,\%idx,$userdata);
4099: }
1.279 raeburn 4100: }
4101: }
4102: }
1.275 raeburn 4103: }
4104: return;
4105: }
4106:
1.288 raeburn 4107: sub get_user_info {
4108: my ($udom,$uname,$idx,$userdata) = @_;
1.289 albertel 4109: $$userdata{$uname.':'.$udom}[$$idx{fullname}] =
4110: &plainname($uname,$udom,'lastname');
1.291 albertel 4111: $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297 raeburn 4112: $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.288 raeburn 4113: return;
4114: }
1.275 raeburn 4115:
1.384 raeburn 4116: sub get_secgrprole_info {
4117: my ($cdom,$cnum,$needroles,$type) = @_;
4118: my %sections_count = &get_sections($cdom,$cnum);
4119: my @sections = (sort {$a <=> $b} keys(%sections_count));
4120: my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
4121: my @groups = sort(keys(%curr_groups));
4122: my $allroles = [];
4123: my $rolehash;
4124: my $accesshash = {
4125: active => 'Currently has access',
4126: future => 'Will have future access',
4127: previous => 'Previously had access',
4128: };
4129: if ($needroles) {
4130: $rolehash = {'all' => 'all'};
1.385 albertel 4131: my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
4132: if (&Apache::lonnet::error(%user_roles)) {
4133: undef(%user_roles);
4134: }
4135: foreach my $item (keys(%user_roles)) {
1.384 raeburn 4136: my ($role)=split(/\:/,$item,2);
4137: if ($role eq 'cr') { next; }
4138: if ($role =~ /^cr/) {
4139: $$rolehash{$role} = (split('/',$role))[3];
4140: } else {
4141: $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
4142: }
4143: }
4144: foreach my $key (sort(keys(%{$rolehash}))) {
4145: push(@{$allroles},$key);
4146: }
4147: push (@{$allroles},'st');
4148: $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
4149: }
4150: return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
4151: }
4152:
1.112 bowersj2 4153: =pod
4154:
4155: =item * get_unprocessed_cgi($query,$possible_names)
4156:
1.258 albertel 4157: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112 bowersj2 4158: $query. The parameters listed in $possible_names (an array reference),
1.258 albertel 4159: will be set in $env{'form.name'} if they do not already exist.
1.112 bowersj2 4160:
4161: Typically called with $ENV{'QUERY_STRING'} as the first parameter.
4162: $possible_names is an ref to an array of form element names. As an example:
4163: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258 albertel 4164: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112 bowersj2 4165:
4166: =cut
1.1 albertel 4167:
1.6 albertel 4168: sub get_unprocessed_cgi {
1.25 albertel 4169: my ($query,$possible_names)= @_;
1.26 matthew 4170: # $Apache::lonxml::debug=1;
1.356 albertel 4171: foreach my $pair (split(/&/,$query)) {
4172: my ($name, $value) = split(/=/,$pair);
1.369 www 4173: $name = &unescape($name);
1.25 albertel 4174: if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
4175: $value =~ tr/+/ /;
4176: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258 albertel 4177: unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25 albertel 4178: }
1.16 harris41 4179: }
1.6 albertel 4180: }
4181:
1.112 bowersj2 4182: =pod
4183:
4184: =item * cacheheader()
4185:
4186: returns cache-controlling header code
4187:
4188: =cut
4189:
1.7 albertel 4190: sub cacheheader {
1.258 albertel 4191: unless ($env{'request.method'} eq 'GET') { return ''; }
1.216 albertel 4192: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
4193: my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7 albertel 4194: <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
4195: <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216 albertel 4196: return $output;
1.7 albertel 4197: }
4198:
1.112 bowersj2 4199: =pod
4200:
4201: =item * no_cache($r)
4202:
4203: specifies header code to not have cache
4204:
4205: =cut
4206:
1.9 albertel 4207: sub no_cache {
1.216 albertel 4208: my ($r) = @_;
4209: if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258 albertel 4210: $env{'request.method'} ne 'GET') { return ''; }
1.216 albertel 4211: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
4212: $r->no_cache(1);
4213: $r->header_out("Expires" => $date);
4214: $r->header_out("Pragma" => "no-cache");
1.123 www 4215: }
4216:
4217: sub content_type {
1.181 albertel 4218: my ($r,$type,$charset) = @_;
1.299 foxr 4219: if ($r) {
4220: # Note that printout.pl calls this with undef for $r.
4221: &no_cache($r);
4222: }
1.258 albertel 4223: if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181 albertel 4224: unless ($charset) {
4225: $charset=&Apache::lonlocal::current_encoding;
4226: }
4227: if ($charset) { $type.='; charset='.$charset; }
4228: if ($r) {
4229: $r->content_type($type);
4230: } else {
4231: print("Content-type: $type\n\n");
4232: }
1.9 albertel 4233: }
1.25 albertel 4234:
1.112 bowersj2 4235: =pod
4236:
4237: =item * add_to_env($name,$value)
4238:
1.258 albertel 4239: adds $name to the %env hash with value
1.112 bowersj2 4240: $value, if $name already exists, the entry is converted to an array
4241: reference and $value is added to the array.
4242:
4243: =cut
4244:
1.25 albertel 4245: sub add_to_env {
4246: my ($name,$value)=@_;
1.258 albertel 4247: if (defined($env{$name})) {
4248: if (ref($env{$name})) {
1.25 albertel 4249: #already have multiple values
1.258 albertel 4250: push(@{ $env{$name} },$value);
1.25 albertel 4251: } else {
4252: #first time seeing multiple values, convert hash entry to an arrayref
1.258 albertel 4253: my $first=$env{$name};
4254: undef($env{$name});
4255: push(@{ $env{$name} },$first,$value);
1.25 albertel 4256: }
4257: } else {
1.258 albertel 4258: $env{$name}=$value;
1.25 albertel 4259: }
1.31 albertel 4260: }
1.149 albertel 4261:
4262: =pod
4263:
4264: =item * get_env_multiple($name)
4265:
1.258 albertel 4266: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149 albertel 4267: values may be defined and end up as an array ref.
4268:
4269: returns an array of values
4270:
4271: =cut
4272:
4273: sub get_env_multiple {
4274: my ($name) = @_;
4275: my @values;
1.258 albertel 4276: if (defined($env{$name})) {
1.149 albertel 4277: # exists is it an array
1.258 albertel 4278: if (ref($env{$name})) {
4279: @values=@{ $env{$name} };
1.149 albertel 4280: } else {
1.258 albertel 4281: $values[0]=$env{$name};
1.149 albertel 4282: }
4283: }
4284: return(@values);
4285: }
4286:
1.31 albertel 4287:
1.41 ng 4288: =pod
1.45 matthew 4289:
4290: =back
1.41 ng 4291:
1.112 bowersj2 4292: =head1 CSV Upload/Handling functions
1.38 albertel 4293:
1.41 ng 4294: =over 4
4295:
1.112 bowersj2 4296: =item * upfile_store($r)
1.41 ng 4297:
4298: Store uploaded file, $r should be the HTTP Request object,
1.258 albertel 4299: needs $env{'form.upfile'}
1.41 ng 4300: returns $datatoken to be put into hidden field
4301:
4302: =cut
1.31 albertel 4303:
4304: sub upfile_store {
4305: my $r=shift;
1.258 albertel 4306: $env{'form.upfile'}=~s/\r/\n/gs;
4307: $env{'form.upfile'}=~s/\f/\n/gs;
4308: $env{'form.upfile'}=~s/\n+/\n/gs;
4309: $env{'form.upfile'}=~s/\n+$//gs;
1.31 albertel 4310:
1.258 albertel 4311: my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
4312: '_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31 albertel 4313: {
1.158 raeburn 4314: my $datafile = $r->dir_config('lonDaemons').
4315: '/tmp/'.$datatoken.'.tmp';
4316: if ( open(my $fh,">$datafile") ) {
1.258 albertel 4317: print $fh $env{'form.upfile'};
1.158 raeburn 4318: close($fh);
4319: }
1.31 albertel 4320: }
4321: return $datatoken;
4322: }
4323:
1.56 matthew 4324: =pod
4325:
1.112 bowersj2 4326: =item * load_tmp_file($r)
1.41 ng 4327:
4328: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258 albertel 4329: needs $env{'form.datatoken'},
4330: sets $env{'form.upfile'} to the contents of the file
1.41 ng 4331:
4332: =cut
1.31 albertel 4333:
4334: sub load_tmp_file {
4335: my $r=shift;
4336: my @studentdata=();
4337: {
1.158 raeburn 4338: my $studentfile = $r->dir_config('lonDaemons').
1.258 albertel 4339: '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158 raeburn 4340: if ( open(my $fh,"<$studentfile") ) {
4341: @studentdata=<$fh>;
4342: close($fh);
4343: }
1.31 albertel 4344: }
1.258 albertel 4345: $env{'form.upfile'}=join('',@studentdata);
1.31 albertel 4346: }
4347:
1.56 matthew 4348: =pod
4349:
1.112 bowersj2 4350: =item * upfile_record_sep()
1.41 ng 4351:
4352: Separate uploaded file into records
4353: returns array of records,
1.258 albertel 4354: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41 ng 4355:
4356: =cut
1.31 albertel 4357:
4358: sub upfile_record_sep {
1.258 albertel 4359: if ($env{'form.upfiletype'} eq 'xml') {
1.31 albertel 4360: } else {
1.248 albertel 4361: my @records;
1.258 albertel 4362: foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248 albertel 4363: if ($line=~/^\s*$/) { next; }
4364: push(@records,$line);
4365: }
4366: return @records;
1.31 albertel 4367: }
4368: }
4369:
1.56 matthew 4370: =pod
4371:
1.112 bowersj2 4372: =item * record_sep($record)
1.41 ng 4373:
1.258 albertel 4374: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41 ng 4375:
4376: =cut
4377:
1.263 www 4378: sub takeleft {
4379: my $index=shift;
4380: return substr('0000'.$index,-4,4);
4381: }
4382:
1.31 albertel 4383: sub record_sep {
4384: my $record=shift;
4385: my %components=();
1.258 albertel 4386: if ($env{'form.upfiletype'} eq 'xml') {
4387: } elsif ($env{'form.upfiletype'} eq 'space') {
1.31 albertel 4388: my $i=0;
1.356 albertel 4389: foreach my $field (split(/\s+/,$record)) {
1.31 albertel 4390: $field=~s/^(\"|\')//;
4391: $field=~s/(\"|\')$//;
1.263 www 4392: $components{&takeleft($i)}=$field;
1.31 albertel 4393: $i++;
4394: }
1.258 albertel 4395: } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31 albertel 4396: my $i=0;
1.356 albertel 4397: foreach my $field (split(/\t/,$record)) {
1.31 albertel 4398: $field=~s/^(\"|\')//;
4399: $field=~s/(\"|\')$//;
1.263 www 4400: $components{&takeleft($i)}=$field;
1.31 albertel 4401: $i++;
4402: }
4403: } else {
4404: my @allfields=split(/\,/,$record);
4405: my $i=0;
4406: my $j;
4407: for ($j=0;$j<=$#allfields;$j++) {
4408: my $field=$allfields[$j];
4409: if ($field=~/^\s*(\"|\')/) {
4410: my $delimiter=$1;
4411: while (($field!~/$delimiter$/) && ($j<$#allfields)) {
4412: $j++;
4413: $field.=','.$allfields[$j];
4414: }
4415: $field=~s/^\s*$delimiter//;
4416: $field=~s/$delimiter\s*$//;
4417: }
1.263 www 4418: $components{&takeleft($i)}=$field;
1.31 albertel 4419: $i++;
4420: }
4421: }
4422: return %components;
4423: }
4424:
1.144 matthew 4425: ######################################################
4426: ######################################################
4427:
1.56 matthew 4428: =pod
4429:
1.112 bowersj2 4430: =item * upfile_select_html()
1.41 ng 4431:
1.144 matthew 4432: Return HTML code to select a file from the users machine and specify
4433: the file type.
1.41 ng 4434:
4435: =cut
4436:
1.144 matthew 4437: ######################################################
4438: ######################################################
1.31 albertel 4439: sub upfile_select_html {
1.144 matthew 4440: my %Types = (
4441: csv => &mt('CSV (comma separated values, spreadsheet)'),
4442: space => &mt('Space separated'),
4443: tab => &mt('Tabulator separated'),
4444: # xml => &mt('HTML/XML'),
4445: );
4446: my $Str = '<input type="file" name="upfile" size="50" />'.
4447: '<br />Type: <select name="upfiletype">';
4448: foreach my $type (sort(keys(%Types))) {
4449: $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
4450: }
4451: $Str .= "</select>\n";
4452: return $Str;
1.31 albertel 4453: }
4454:
1.301 albertel 4455: sub get_samples {
4456: my ($records,$toget) = @_;
4457: my @samples=({});
4458: my $got=0;
4459: foreach my $rec (@$records) {
4460: my %temp = &record_sep($rec);
4461: if (! grep(/\S/, values(%temp))) { next; }
4462: if (%temp) {
4463: $samples[$got]=\%temp;
4464: $got++;
4465: if ($got == $toget) { last; }
4466: }
4467: }
4468: return \@samples;
4469: }
4470:
1.144 matthew 4471: ######################################################
4472: ######################################################
4473:
1.56 matthew 4474: =pod
4475:
1.112 bowersj2 4476: =item * csv_print_samples($r,$records)
1.41 ng 4477:
4478: Prints a table of sample values from each column uploaded $r is an
4479: Apache Request ref, $records is an arrayref from
4480: &Apache::loncommon::upfile_record_sep
4481:
4482: =cut
4483:
1.144 matthew 4484: ######################################################
4485: ######################################################
1.31 albertel 4486: sub csv_print_samples {
4487: my ($r,$records) = @_;
1.301 albertel 4488: my $samples = &get_samples($records,3);
4489:
1.144 matthew 4490: $r->print(&mt('Samples').'<br /><table border="2"><tr>');
1.356 albertel 4491: foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
4492: $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>'); }
1.31 albertel 4493: $r->print('</tr>');
1.301 albertel 4494: foreach my $hash (@$samples) {
1.31 albertel 4495: $r->print('<tr>');
1.356 albertel 4496: foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31 albertel 4497: $r->print('<td>');
1.356 albertel 4498: if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31 albertel 4499: $r->print('</td>');
4500: }
4501: $r->print('</tr>');
4502: }
4503: $r->print('</tr></table><br />'."\n");
4504: }
4505:
1.144 matthew 4506: ######################################################
4507: ######################################################
4508:
1.56 matthew 4509: =pod
4510:
1.112 bowersj2 4511: =item * csv_print_select_table($r,$records,$d)
1.41 ng 4512:
4513: Prints a table to create associations between values and table columns.
1.144 matthew 4514:
1.41 ng 4515: $r is an Apache Request ref,
4516: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174 matthew 4517: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41 ng 4518:
4519: =cut
4520:
1.144 matthew 4521: ######################################################
4522: ######################################################
1.31 albertel 4523: sub csv_print_select_table {
4524: my ($r,$records,$d) = @_;
1.301 albertel 4525: my $i=0;
4526: my $samples = &get_samples($records,1);
1.144 matthew 4527: $r->print(&mt('Associate columns with student attributes.')."\n".
4528: '<table border="2"><tr>'.
4529: '<th>'.&mt('Attribute').'</th>'.
4530: '<th>'.&mt('Column').'</th></tr>'."\n");
1.356 albertel 4531: foreach my $array_ref (@$d) {
4532: my ($value,$display,$defaultcol)=@{ $array_ref };
1.31 albertel 4533: $r->print('<tr><td>'.$display.'</td>');
4534:
4535: $r->print('<td><select name=f'.$i.
1.32 matthew 4536: ' onchange="javascript:flip(this.form,'.$i.');">');
1.31 albertel 4537: $r->print('<option value="none"></option>');
1.356 albertel 4538: foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
4539: $r->print('<option value="'.$sample.'"'.
4540: ($sample eq $defaultcol ? ' selected="selected" ' : '').
4541: '>Column '.($sample+1).'</option>');
1.31 albertel 4542: }
4543: $r->print('</select></td></tr>'."\n");
4544: $i++;
4545: }
4546: $i--;
4547: return $i;
4548: }
1.56 matthew 4549:
1.144 matthew 4550: ######################################################
4551: ######################################################
4552:
1.56 matthew 4553: =pod
1.31 albertel 4554:
1.112 bowersj2 4555: =item * csv_samples_select_table($r,$records,$d)
1.41 ng 4556:
4557: Prints a table of sample values from the upload and can make associate samples to internal names.
4558:
4559: $r is an Apache Request ref,
4560: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
4561: $d is an array of 2 element arrays (internal name, displayed name)
4562:
4563: =cut
4564:
1.144 matthew 4565: ######################################################
4566: ######################################################
1.31 albertel 4567: sub csv_samples_select_table {
4568: my ($r,$records,$d) = @_;
4569: my $i=0;
1.144 matthew 4570: #
1.301 albertel 4571: my $samples = &get_samples($records,3);
1.144 matthew 4572: $r->print('<table border=2><tr><th>'.
4573: &mt('Field').'</th><th>'.&mt('Samples').'</th></tr>');
1.301 albertel 4574:
4575: foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.144 matthew 4576: $r->print('<tr><td><select name="f'.$i.'"'.
1.32 matthew 4577: ' onchange="javascript:flip(this.form,'.$i.');">');
1.301 albertel 4578: foreach my $option (@$d) {
4579: my ($value,$display,$defaultcol)=@{ $option };
1.174 matthew 4580: $r->print('<option value="'.$value.'"'.
1.253 albertel 4581: ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174 matthew 4582: $display.'</option>');
1.31 albertel 4583: }
4584: $r->print('</select></td><td>');
1.301 albertel 4585: foreach my $line (0..2) {
4586: if (defined($samples->[$line]{$key})) {
4587: $r->print($samples->[$line]{$key}."<br />\n");
4588: }
4589: }
1.31 albertel 4590: $r->print('</td></tr>');
4591: $i++;
4592: }
4593: $i--;
4594: return($i);
1.115 matthew 4595: }
4596:
1.144 matthew 4597: ######################################################
4598: ######################################################
4599:
1.115 matthew 4600: =pod
4601:
4602: =item clean_excel_name($name)
4603:
4604: Returns a replacement for $name which does not contain any illegal characters.
4605:
4606: =cut
4607:
1.144 matthew 4608: ######################################################
4609: ######################################################
1.115 matthew 4610: sub clean_excel_name {
4611: my ($name) = @_;
4612: $name =~ s/[:\*\?\/\\]//g;
4613: if (length($name) > 31) {
4614: $name = substr($name,0,31);
4615: }
4616: return $name;
1.25 albertel 4617: }
1.84 albertel 4618:
1.85 albertel 4619: =pod
4620:
1.112 bowersj2 4621: =item * check_if_partid_hidden($id,$symb,$udom,$uname)
1.85 albertel 4622:
4623: Returns either 1 or undef
4624:
4625: 1 if the part is to be hidden, undef if it is to be shown
4626:
4627: Arguments are:
4628:
4629: $id the id of the part to be checked
4630: $symb, optional the symb of the resource to check
4631: $udom, optional the domain of the user to check for
4632: $uname, optional the username of the user to check for
4633:
4634: =cut
1.84 albertel 4635:
4636: sub check_if_partid_hidden {
4637: my ($id,$symb,$udom,$uname) = @_;
1.133 albertel 4638: my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84 albertel 4639: $symb,$udom,$uname);
1.141 albertel 4640: my $truth=1;
4641: #if the string starts with !, then the list is the list to show not hide
4642: if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84 albertel 4643: my @hiddenlist=split(/,/,$hiddenparts);
4644: foreach my $checkid (@hiddenlist) {
1.141 albertel 4645: if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84 albertel 4646: }
1.141 albertel 4647: return !$truth;
1.84 albertel 4648: }
1.127 matthew 4649:
1.138 matthew 4650:
4651: ############################################################
4652: ############################################################
4653:
4654: =pod
4655:
1.157 matthew 4656: =back
4657:
1.138 matthew 4658: =head1 cgi-bin script and graphing routines
4659:
1.157 matthew 4660: =over 4
4661:
1.138 matthew 4662: =item get_cgi_id
4663:
4664: Inputs: none
4665:
4666: Returns an id which can be used to pass environment variables
4667: to various cgi-bin scripts. These environment variables will
4668: be removed from the users environment after a given time by
4669: the routine &Apache::lonnet::transfer_profile_to_env.
4670:
4671: =cut
4672:
4673: ############################################################
4674: ############################################################
1.152 albertel 4675: my $uniq=0;
1.136 matthew 4676: sub get_cgi_id {
1.154 albertel 4677: $uniq=($uniq+1)%100000;
1.280 albertel 4678: return (time.'_'.$$.'_'.$uniq);
1.136 matthew 4679: }
4680:
1.127 matthew 4681: ############################################################
4682: ############################################################
4683:
4684: =pod
4685:
1.134 matthew 4686: =item DrawBarGraph
1.127 matthew 4687:
1.138 matthew 4688: Facilitates the plotting of data in a (stacked) bar graph.
4689: Puts plot definition data into the users environment in order for
4690: graph.png to plot it. Returns an <img> tag for the plot.
4691: The bars on the plot are labeled '1','2',...,'n'.
4692:
4693: Inputs:
4694:
4695: =over 4
4696:
4697: =item $Title: string, the title of the plot
4698:
4699: =item $xlabel: string, text describing the X-axis of the plot
4700:
4701: =item $ylabel: string, text describing the Y-axis of the plot
4702:
4703: =item $Max: scalar, the maximum Y value to use in the plot
4704: If $Max is < any data point, the graph will not be rendered.
4705:
1.140 matthew 4706: =item $colors: array ref holding the colors to be used for the data sets when
1.138 matthew 4707: they are plotted. If undefined, default values will be used.
4708:
1.178 matthew 4709: =item $labels: array ref holding the labels to use on the x-axis for the bars.
4710:
1.138 matthew 4711: =item @Values: An array of array references. Each array reference holds data
4712: to be plotted in a stacked bar chart.
4713:
1.239 matthew 4714: =item If the final element of @Values is a hash reference the key/value
4715: pairs will be added to the graph definition.
4716:
1.138 matthew 4717: =back
4718:
4719: Returns:
4720:
4721: An <img> tag which references graph.png and the appropriate identifying
4722: information for the plot.
4723:
1.127 matthew 4724: =cut
4725:
4726: ############################################################
4727: ############################################################
1.134 matthew 4728: sub DrawBarGraph {
1.178 matthew 4729: my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134 matthew 4730: #
4731: if (! defined($colors)) {
4732: $colors = ['#33ff00',
4733: '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
4734: '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
4735: ];
4736: }
1.228 matthew 4737: my $extra_settings = {};
4738: if (ref($Values[-1]) eq 'HASH') {
4739: $extra_settings = pop(@Values);
4740: }
1.127 matthew 4741: #
1.136 matthew 4742: my $identifier = &get_cgi_id();
4743: my $id = 'cgi.'.$identifier;
1.129 matthew 4744: if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127 matthew 4745: return '';
4746: }
1.225 matthew 4747: #
4748: my @Labels;
4749: if (defined($labels)) {
4750: @Labels = @$labels;
4751: } else {
4752: for (my $i=0;$i<@{$Values[0]};$i++) {
4753: push (@Labels,$i+1);
4754: }
4755: }
4756: #
1.129 matthew 4757: my $NumBars = scalar(@{$Values[0]});
1.225 matthew 4758: if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129 matthew 4759: my %ValuesHash;
4760: my $NumSets=1;
4761: foreach my $array (@Values) {
4762: next if (! ref($array));
1.136 matthew 4763: $ValuesHash{$id.'.data.'.$NumSets++} =
1.132 matthew 4764: join(',',@$array);
1.129 matthew 4765: }
1.127 matthew 4766: #
1.136 matthew 4767: my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225 matthew 4768: if ($NumBars < 3) {
4769: $width = 120+$NumBars*32;
1.220 matthew 4770: $xskip = 1;
1.225 matthew 4771: $bar_width = 30;
4772: } elsif ($NumBars < 5) {
4773: $width = 120+$NumBars*20;
4774: $xskip = 1;
4775: $bar_width = 20;
1.220 matthew 4776: } elsif ($NumBars < 10) {
1.136 matthew 4777: $width = 120+$NumBars*15;
4778: $xskip = 1;
4779: $bar_width = 15;
4780: } elsif ($NumBars <= 25) {
4781: $width = 120+$NumBars*11;
4782: $xskip = 5;
4783: $bar_width = 8;
4784: } elsif ($NumBars <= 50) {
4785: $width = 120+$NumBars*8;
4786: $xskip = 5;
4787: $bar_width = 4;
4788: } else {
4789: $width = 120+$NumBars*8;
4790: $xskip = 5;
4791: $bar_width = 4;
4792: }
4793: #
1.137 matthew 4794: $Max = 1 if ($Max < 1);
4795: if ( int($Max) < $Max ) {
4796: $Max++;
4797: $Max = int($Max);
4798: }
1.127 matthew 4799: $Title = '' if (! defined($Title));
4800: $xlabel = '' if (! defined($xlabel));
4801: $ylabel = '' if (! defined($ylabel));
1.369 www 4802: $ValuesHash{$id.'.title'} = &escape($Title);
4803: $ValuesHash{$id.'.xlabel'} = &escape($xlabel);
4804: $ValuesHash{$id.'.ylabel'} = &escape($ylabel);
1.137 matthew 4805: $ValuesHash{$id.'.y_max_value'} = $Max;
1.136 matthew 4806: $ValuesHash{$id.'.NumBars'} = $NumBars;
4807: $ValuesHash{$id.'.NumSets'} = $NumSets;
4808: $ValuesHash{$id.'.PlotType'} = 'bar';
4809: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
4810: $ValuesHash{$id.'.height'} = $height;
4811: $ValuesHash{$id.'.width'} = $width;
4812: $ValuesHash{$id.'.xskip'} = $xskip;
4813: $ValuesHash{$id.'.bar_width'} = $bar_width;
4814: $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127 matthew 4815: #
1.228 matthew 4816: # Deal with other parameters
4817: while (my ($key,$value) = each(%$extra_settings)) {
4818: $ValuesHash{$id.'.'.$key} = $value;
4819: }
4820: #
1.137 matthew 4821: &Apache::lonnet::appenv(%ValuesHash);
4822: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
4823: }
4824:
4825: ############################################################
4826: ############################################################
4827:
4828: =pod
4829:
4830: =item DrawXYGraph
4831:
1.138 matthew 4832: Facilitates the plotting of data in an XY graph.
4833: Puts plot definition data into the users environment in order for
4834: graph.png to plot it. Returns an <img> tag for the plot.
4835:
4836: Inputs:
4837:
4838: =over 4
4839:
4840: =item $Title: string, the title of the plot
4841:
4842: =item $xlabel: string, text describing the X-axis of the plot
4843:
4844: =item $ylabel: string, text describing the Y-axis of the plot
4845:
4846: =item $Max: scalar, the maximum Y value to use in the plot
4847: If $Max is < any data point, the graph will not be rendered.
4848:
4849: =item $colors: Array ref containing the hex color codes for the data to be
4850: plotted in. If undefined, default values will be used.
4851:
4852: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
4853:
4854: =item $Ydata: Array ref containing Array refs.
1.185 www 4855: Each of the contained arrays will be plotted as a separate curve.
1.138 matthew 4856:
4857: =item %Values: hash indicating or overriding any default values which are
4858: passed to graph.png.
4859: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
4860:
4861: =back
4862:
4863: Returns:
4864:
4865: An <img> tag which references graph.png and the appropriate identifying
4866: information for the plot.
4867:
1.137 matthew 4868: =cut
4869:
4870: ############################################################
4871: ############################################################
4872: sub DrawXYGraph {
4873: my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
4874: #
4875: # Create the identifier for the graph
4876: my $identifier = &get_cgi_id();
4877: my $id = 'cgi.'.$identifier;
4878: #
4879: $Title = '' if (! defined($Title));
4880: $xlabel = '' if (! defined($xlabel));
4881: $ylabel = '' if (! defined($ylabel));
4882: my %ValuesHash =
4883: (
1.369 www 4884: $id.'.title' => &escape($Title),
4885: $id.'.xlabel' => &escape($xlabel),
4886: $id.'.ylabel' => &escape($ylabel),
1.137 matthew 4887: $id.'.y_max_value'=> $Max,
4888: $id.'.labels' => join(',',@$Xlabels),
4889: $id.'.PlotType' => 'XY',
4890: );
4891: #
4892: if (defined($colors) && ref($colors) eq 'ARRAY') {
4893: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
4894: }
4895: #
4896: if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
4897: return '';
4898: }
4899: my $NumSets=1;
1.138 matthew 4900: foreach my $array (@{$Ydata}){
1.137 matthew 4901: next if (! ref($array));
4902: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
4903: }
1.138 matthew 4904: $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137 matthew 4905: #
4906: # Deal with other parameters
4907: while (my ($key,$value) = each(%Values)) {
4908: $ValuesHash{$id.'.'.$key} = $value;
1.127 matthew 4909: }
4910: #
1.136 matthew 4911: &Apache::lonnet::appenv(%ValuesHash);
4912: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
4913: }
4914:
4915: ############################################################
4916: ############################################################
4917:
4918: =pod
4919:
1.138 matthew 4920: =item DrawXYYGraph
4921:
4922: Facilitates the plotting of data in an XY graph with two Y axes.
4923: Puts plot definition data into the users environment in order for
4924: graph.png to plot it. Returns an <img> tag for the plot.
4925:
4926: Inputs:
4927:
4928: =over 4
4929:
4930: =item $Title: string, the title of the plot
4931:
4932: =item $xlabel: string, text describing the X-axis of the plot
4933:
4934: =item $ylabel: string, text describing the Y-axis of the plot
4935:
4936: =item $colors: Array ref containing the hex color codes for the data to be
4937: plotted in. If undefined, default values will be used.
4938:
4939: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
4940:
4941: =item $Ydata1: The first data set
4942:
4943: =item $Min1: The minimum value of the left Y-axis
4944:
4945: =item $Max1: The maximum value of the left Y-axis
4946:
4947: =item $Ydata2: The second data set
4948:
4949: =item $Min2: The minimum value of the right Y-axis
4950:
4951: =item $Max2: The maximum value of the left Y-axis
4952:
4953: =item %Values: hash indicating or overriding any default values which are
4954: passed to graph.png.
4955: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
4956:
4957: =back
4958:
4959: Returns:
4960:
4961: An <img> tag which references graph.png and the appropriate identifying
4962: information for the plot.
1.136 matthew 4963:
4964: =cut
4965:
4966: ############################################################
4967: ############################################################
1.137 matthew 4968: sub DrawXYYGraph {
4969: my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
4970: $Ydata2,$Min2,$Max2,%Values)=@_;
1.136 matthew 4971: #
4972: # Create the identifier for the graph
4973: my $identifier = &get_cgi_id();
4974: my $id = 'cgi.'.$identifier;
4975: #
4976: $Title = '' if (! defined($Title));
4977: $xlabel = '' if (! defined($xlabel));
4978: $ylabel = '' if (! defined($ylabel));
4979: my %ValuesHash =
4980: (
1.369 www 4981: $id.'.title' => &escape($Title),
4982: $id.'.xlabel' => &escape($xlabel),
4983: $id.'.ylabel' => &escape($ylabel),
1.136 matthew 4984: $id.'.labels' => join(',',@$Xlabels),
4985: $id.'.PlotType' => 'XY',
4986: $id.'.NumSets' => 2,
1.137 matthew 4987: $id.'.two_axes' => 1,
4988: $id.'.y1_max_value' => $Max1,
4989: $id.'.y1_min_value' => $Min1,
4990: $id.'.y2_max_value' => $Max2,
4991: $id.'.y2_min_value' => $Min2,
1.136 matthew 4992: );
4993: #
1.137 matthew 4994: if (defined($colors) && ref($colors) eq 'ARRAY') {
4995: $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
4996: }
4997: #
4998: if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
4999: ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136 matthew 5000: return '';
5001: }
5002: my $NumSets=1;
1.137 matthew 5003: foreach my $array ($Ydata1,$Ydata2){
1.136 matthew 5004: next if (! ref($array));
5005: $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137 matthew 5006: }
5007: #
5008: # Deal with other parameters
5009: while (my ($key,$value) = each(%Values)) {
5010: $ValuesHash{$id.'.'.$key} = $value;
1.136 matthew 5011: }
5012: #
5013: &Apache::lonnet::appenv(%ValuesHash);
1.130 albertel 5014: return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139 matthew 5015: }
5016:
5017: ############################################################
5018: ############################################################
5019:
5020: =pod
5021:
1.157 matthew 5022: =back
5023:
1.139 matthew 5024: =head1 Statistics helper routines?
5025:
5026: Bad place for them but what the hell.
5027:
1.157 matthew 5028: =over 4
5029:
1.139 matthew 5030: =item &chartlink
5031:
5032: Returns a link to the chart for a specific student.
5033:
5034: Inputs:
5035:
5036: =over 4
5037:
5038: =item $linktext: The text of the link
5039:
5040: =item $sname: The students username
5041:
5042: =item $sdomain: The students domain
5043:
5044: =back
5045:
1.157 matthew 5046: =back
5047:
1.139 matthew 5048: =cut
5049:
5050: ############################################################
5051: ############################################################
5052: sub chartlink {
5053: my ($linktext, $sname, $sdomain) = @_;
5054: my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369 www 5055: '&SelectedStudent='.&escape($sname.':'.$sdomain).
1.219 albertel 5056: '&chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139 matthew 5057: '">'.$linktext.'</a>';
1.153 matthew 5058: }
5059:
5060: #######################################################
5061: #######################################################
5062:
5063: =pod
5064:
5065: =head1 Course Environment Routines
1.157 matthew 5066:
5067: =over 4
1.153 matthew 5068:
5069: =item &restore_course_settings
5070:
5071: =item &store_course_settings
5072:
5073: Restores/Store indicated form parameters from the course environment.
5074: Will not overwrite existing values of the form parameters.
5075:
5076: Inputs:
5077: a scalar describing the data (e.g. 'chart', 'problem_analysis')
5078:
5079: a hash ref describing the data to be stored. For example:
5080:
5081: %Save_Parameters = ('Status' => 'scalar',
5082: 'chartoutputmode' => 'scalar',
5083: 'chartoutputdata' => 'scalar',
5084: 'Section' => 'array',
1.373 raeburn 5085: 'Group' => 'array',
1.153 matthew 5086: 'StudentData' => 'array',
5087: 'Maps' => 'array');
5088:
5089: Returns: both routines return nothing
5090:
5091: =cut
5092:
5093: #######################################################
5094: #######################################################
5095: sub store_course_settings {
5096: # save to the environment
5097: # appenv the same items, just to be safe
1.258 albertel 5098: my $courseid = $env{'request.course.id'};
1.300 albertel 5099: my $udom = $env{'user.domain'};
5100: my $uname = $env{'user.name'};
1.153 matthew 5101: my ($prefix,$Settings) = @_;
5102: my %SaveHash;
5103: my %AppHash;
5104: while (my ($setting,$type) = each(%$Settings)) {
1.300 albertel 5105: my $basename = join('.','internal',$courseid,$prefix,$setting);
5106: my $envname = 'environment.'.$basename;
1.258 albertel 5107: if (exists($env{'form.'.$setting})) {
1.153 matthew 5108: # Save this value away
5109: if ($type eq 'scalar' &&
1.258 albertel 5110: (! exists($env{$envname}) ||
5111: $env{$envname} ne $env{'form.'.$setting})) {
5112: $SaveHash{$basename} = $env{'form.'.$setting};
5113: $AppHash{$envname} = $env{'form.'.$setting};
1.153 matthew 5114: } elsif ($type eq 'array') {
5115: my $stored_form;
1.258 albertel 5116: if (ref($env{'form.'.$setting})) {
1.153 matthew 5117: $stored_form = join(',',
5118: map {
1.369 www 5119: &escape($_);
1.258 albertel 5120: } sort(@{$env{'form.'.$setting}}));
1.153 matthew 5121: } else {
5122: $stored_form =
1.369 www 5123: &escape($env{'form.'.$setting});
1.153 matthew 5124: }
5125: # Determine if the array contents are the same.
1.258 albertel 5126: if ($stored_form ne $env{$envname}) {
1.153 matthew 5127: $SaveHash{$basename} = $stored_form;
5128: $AppHash{$envname} = $stored_form;
5129: }
5130: }
5131: }
5132: }
5133: my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300 albertel 5134: $udom,$uname);
1.153 matthew 5135: if ($put_result !~ /^(ok|delayed)/) {
5136: &Apache::lonnet::logthis('unable to save form parameters, '.
5137: 'got error:'.$put_result);
5138: }
5139: # Make sure these settings stick around in this session, too
5140: &Apache::lonnet::appenv(%AppHash);
5141: return;
5142: }
5143:
5144: sub restore_course_settings {
1.258 albertel 5145: my $courseid = $env{'request.course.id'};
1.153 matthew 5146: my ($prefix,$Settings) = @_;
5147: while (my ($setting,$type) = each(%$Settings)) {
1.258 albertel 5148: next if (exists($env{'form.'.$setting}));
1.300 albertel 5149: my $envname = 'environment.internal.'.$courseid.'.'.$prefix.
1.153 matthew 5150: '.'.$setting;
1.258 albertel 5151: if (exists($env{$envname})) {
1.153 matthew 5152: if ($type eq 'scalar') {
1.258 albertel 5153: $env{'form.'.$setting} = $env{$envname};
1.153 matthew 5154: } elsif ($type eq 'array') {
1.258 albertel 5155: $env{'form.'.$setting} = [
1.153 matthew 5156: map {
1.369 www 5157: &unescape($_);
1.258 albertel 5158: } split(',',$env{$envname})
1.153 matthew 5159: ];
5160: }
5161: }
5162: }
1.127 matthew 5163: }
5164:
5165: ############################################################
5166: ############################################################
1.154 albertel 5167:
1.378 raeburn 5168: sub course_type {
5169: my ($cid) = @_;
5170: if (!defined($cid)) {
5171: $cid = $env{'request.course.id'};
5172: }
5173: if (defined($env{'course.'.$cid.'type'})) {
5174: return $env{'course.'.$cid.'type'};
5175: } else {
5176: return 'Course';
1.377 raeburn 5177: }
5178: }
1.156 albertel 5179:
5180: sub icon {
5181: my ($file)=@_;
1.168 albertel 5182: my $curfext = (split(/\./,$file))[-1];
5183: my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156 albertel 5184: my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168 albertel 5185: if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
5186: if (-e $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
5187: $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
5188: $curfext.".gif") {
5189: $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
5190: $curfext.".gif";
5191: }
5192: }
1.249 albertel 5193: return &lonhttpdurl($iconname);
1.154 albertel 5194: }
1.84 albertel 5195:
1.215 albertel 5196: sub lonhttpdurl {
5197: my ($url)=@_;
5198: my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'};
5199: if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; }
5200: return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url;
5201: }
5202:
1.213 albertel 5203: sub connection_aborted {
5204: my ($r)=@_;
5205: $r->print(" ");$r->rflush();
5206: my $c = $r->connection;
5207: return $c->aborted();
5208: }
5209:
1.221 foxr 5210: # Escapes strings that may have embedded 's that will be put into
1.222 foxr 5211: # strings as 'strings'.
5212: sub escape_single {
1.221 foxr 5213: my ($input) = @_;
1.223 albertel 5214: $input =~ s/\\/\\\\/g; # Escape the \'s..(must be first)>
1.221 foxr 5215: $input =~ s/\'/\\\'/g; # Esacpe the 's....
5216: return $input;
5217: }
1.223 albertel 5218:
1.222 foxr 5219: # Same as escape_single, but escape's "'s This
5220: # can be used for "strings"
5221: sub escape_double {
5222: my ($input) = @_;
5223: $input =~ s/\\/\\\\/g; # Escape the /'s..(must be first)>
5224: $input =~ s/\"/\\\"/g; # Esacpe the "s....
5225: return $input;
5226: }
1.223 albertel 5227:
1.222 foxr 5228: # Escapes the last element of a full URL.
5229: sub escape_url {
5230: my ($url) = @_;
1.238 raeburn 5231: my @urlslices = split(/\//, $url,-1);
1.369 www 5232: my $lastitem = &escape(pop(@urlslices));
1.223 albertel 5233: return join('/',@urlslices).'/'.$lastitem;
1.222 foxr 5234: }
1.41 ng 5235: =pod
5236:
5237: =back
5238:
1.112 bowersj2 5239: =cut
1.41 ng 5240:
1.112 bowersj2 5241: 1;
5242: __END__;
1.41 ng 5243:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>