Annotation of loncom/interface/loncommon.pm, revision 1.581

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>