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