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

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.692.4.37! raeburn     4: # $Id: loncommon.pm,v 1.692.4.36 2010/07/21 22:23:08 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.685     tempelho   64: use Apache::lonnet();
1.139     matthew    65: use HTML::Entities;
1.334     albertel   66: use Apache::lonhtmlcommon();
                     67: use Apache::loncoursedata();
1.344     albertel   68: use Apache::lontexconvert();
1.444     albertel   69: use Apache::lonclonecourse();
1.479     albertel   70: use LONCAPA qw(:DEFAULT :match);
1.657     raeburn    71: use DateTime::TimeZone;
1.687     raeburn    72: use DateTime::Locale::Catalog;
1.117     www        73: 
1.517     raeburn    74: # ---------------------------------------------- Designs
                     75: use vars qw(%defaultdesign);
                     76: 
1.22      www        77: my $readit;
                     78: 
1.517     raeburn    79: 
1.157     matthew    80: ##
                     81: ## Global Variables
                     82: ##
1.46      matthew    83: 
1.643     foxr       84: 
                     85: # ----------------------------------------------- SSI with retries:
                     86: #
                     87: 
                     88: =pod
                     89: 
1.648     raeburn    90: =head1 Server Side include with retries:
1.643     foxr       91: 
                     92: =over 4
                     93: 
1.648     raeburn    94: =item * &ssi_with_retries(resource,retries form)
1.643     foxr       95: 
                     96: Performs an ssi with some number of retries.  Retries continue either
                     97: until the result is ok or until the retry count supplied by the
                     98: caller is exhausted.  
                     99: 
                    100: Inputs:
1.648     raeburn   101: 
                    102: =over 4
                    103: 
1.643     foxr      104: resource   - Identifies the resource to insert.
1.648     raeburn   105: 
1.643     foxr      106: retries    - Count of the number of retries allowed.
1.648     raeburn   107: 
1.643     foxr      108: form       - Hash that identifies the rendering options.
                    109: 
1.648     raeburn   110: =back
                    111: 
                    112: Returns:
                    113: 
                    114: =over 4
                    115: 
1.643     foxr      116: content    - The content of the response.  If retries were exhausted this is empty.
1.648     raeburn   117: 
1.643     foxr      118: response   - The response from the last attempt (which may or may not have been successful.
                    119: 
1.648     raeburn   120: =back
                    121: 
                    122: =back
                    123: 
1.643     foxr      124: =cut
                    125: 
                    126: sub ssi_with_retries {
                    127:     my ($resource, $retries, %form) = @_;
                    128: 
                    129: 
                    130:     my $ok = 0;			# True if we got a good response.
                    131:     my $content;
                    132:     my $response;
                    133: 
                    134:     # Try to get the ssi done. within the retries count:
                    135: 
                    136:     do {
                    137: 	($content, $response) = &Apache::lonnet::ssi($resource, %form);
                    138: 	$ok      = $response->is_success;
1.650     www       139:         if (!$ok) {
                    140:             &Apache::lonnet::logthis("Failed ssi_with_retries on $resource: ".$response->is_success.', '.$response->code.', '.$response->message);
                    141:         }
1.643     foxr      142: 	$retries--;
                    143:     } while (!$ok && ($retries > 0));
                    144: 
                    145:     if (!$ok) {
                    146: 	$content = '';		# On error return an empty content.
                    147:     }
                    148:     return ($content, $response);
                    149: 
                    150: }
                    151: 
                    152: 
                    153: 
1.20      www       154: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41  155: my %language;
1.124     www       156: my %supported_language;
1.12      harris41  157: my %cprtag;
1.192     taceyjo1  158: my %scprtag;
1.351     www       159: my %fe; my %fd; my %fm;
1.41      ng        160: my %category_extensions;
1.12      harris41  161: 
1.46      matthew   162: # ---------------------------------------------- Thesaurus variables
1.144     matthew   163: #
                    164: # %Keywords:
                    165: #      A hash used by &keyword to determine if a word is considered a keyword.
                    166: # $thesaurus_db_file 
                    167: #      Scalar containing the full path to the thesaurus database.
1.46      matthew   168: 
                    169: my %Keywords;
                    170: my $thesaurus_db_file;
                    171: 
1.144     matthew   172: #
                    173: # Initialize values from language.tab, copyright.tab, filetypes.tab,
                    174: # thesaurus.tab, and filecategories.tab.
                    175: #
1.18      www       176: BEGIN {
1.46      matthew   177:     # Variable initialization
                    178:     $thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
                    179:     #
1.22      www       180:     unless ($readit) {
1.12      harris41  181: # ------------------------------------------------------------------- languages
                    182:     {
1.158     raeburn   183:         my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    184:                                    '/language.tab';
                    185:         if ( open(my $fh,"<$langtabfile") ) {
1.356     albertel  186:             while (my $line = <$fh>) {
                    187:                 next if ($line=~/^\#/);
                    188:                 chomp($line);
                    189:                 my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
1.158     raeburn   190:                 $language{$key}=$val.' - '.$enc;
                    191:                 if ($sup) {
                    192:                     $supported_language{$key}=$sup;
                    193:                 }
                    194:             }
                    195:             close($fh);
                    196:         }
1.12      harris41  197:     }
                    198: # ------------------------------------------------------------------ copyrights
                    199:     {
1.158     raeburn   200:         my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    201:                                   '/copyright.tab';
                    202:         if ( open (my $fh,"<$copyrightfile") ) {
1.356     albertel  203:             while (my $line = <$fh>) {
                    204:                 next if ($line=~/^\#/);
                    205:                 chomp($line);
                    206:                 my ($key,$val)=(split(/\s+/,$line,2));
1.158     raeburn   207:                 $cprtag{$key}=$val;
                    208:             }
                    209:             close($fh);
                    210:         }
1.12      harris41  211:     }
1.351     www       212: # ----------------------------------------------------------- source copyrights
1.192     taceyjo1  213:     {
                    214:         my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
                    215:                                   '/source_copyright.tab';
                    216:         if ( open (my $fh,"<$sourcecopyrightfile") ) {
1.356     albertel  217:             while (my $line = <$fh>) {
                    218:                 next if ($line =~ /^\#/);
                    219:                 chomp($line);
                    220:                 my ($key,$val)=(split(/\s+/,$line,2));
1.192     taceyjo1  221:                 $scprtag{$key}=$val;
                    222:             }
                    223:             close($fh);
                    224:         }
                    225:     }
1.63      www       226: 
1.517     raeburn   227: # -------------------------------------------------------------- default domain designs
1.63      www       228:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
1.517     raeburn   229:     my $designfile = $designdir.'/default.tab';
                    230:     if ( open (my $fh,"<$designfile") ) {
                    231:         while (my $line = <$fh>) {
                    232:             next if ($line =~ /^\#/);
                    233:             chomp($line);
                    234:             my ($key,$val)=(split(/\=/,$line));
                    235:             if ($val) { $defaultdesign{$key}=$val; }
                    236:         }
                    237:         close($fh);
1.63      www       238:     }
                    239: 
1.15      harris41  240: # ------------------------------------------------------------- file categories
                    241:     {
1.158     raeburn   242:         my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    243:                                   '/filecategories.tab';
                    244:         if ( open (my $fh,"<$categoryfile") ) {
1.356     albertel  245: 	    while (my $line = <$fh>) {
                    246: 		next if ($line =~ /^\#/);
                    247: 		chomp($line);
                    248:                 my ($extension,$category)=(split(/\s+/,$line,2));
1.158     raeburn   249:                 push @{$category_extensions{lc($category)}},$extension;
                    250:             }
                    251:             close($fh);
                    252:         }
                    253: 
1.15      harris41  254:     }
1.12      harris41  255: # ------------------------------------------------------------------ file types
                    256:     {
1.158     raeburn   257:         my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
                    258:                '/filetypes.tab';
                    259:         if ( open (my $fh,"<$typesfile") ) {
1.356     albertel  260:             while (my $line = <$fh>) {
                    261: 		next if ($line =~ /^\#/);
                    262: 		chomp($line);
                    263:                 my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
1.158     raeburn   264:                 if ($descr ne '') {
                    265:                     $fe{$ending}=lc($emb);
                    266:                     $fd{$ending}=$descr;
1.351     www       267:                     if ($mime ne 'unk') { $fm{$ending}=$mime; }
1.158     raeburn   268:                 }
                    269:             }
                    270:             close($fh);
                    271:         }
1.12      harris41  272:     }
1.22      www       273:     &Apache::lonnet::logthis(
1.46      matthew   274:               "<font color=yellow>INFO: Read file types</font>");
1.22      www       275:     $readit=1;
1.46      matthew   276:     }  # end of unless($readit) 
1.32      matthew   277:     
                    278: }
1.112     bowersj2  279: 
1.42      matthew   280: ###############################################################
                    281: ##           HTML and Javascript Helper Functions            ##
                    282: ###############################################################
                    283: 
                    284: =pod 
                    285: 
1.112     bowersj2  286: =head1 HTML and Javascript Functions
1.42      matthew   287: 
1.112     bowersj2  288: =over 4
                    289: 
1.648     raeburn   290: =item * &browser_and_searcher_javascript()
1.112     bowersj2  291: 
                    292: X<browsing, javascript>X<searching, javascript>Returns a string
                    293: containing javascript with two functions, C<openbrowser> and
                    294: C<opensearcher>. Returned string does not contain E<lt>scriptE<gt>
                    295: tags.
1.42      matthew   296: 
1.648     raeburn   297: =item * &openbrowser(formname,elementname,only,omit) [javascript]
1.42      matthew   298: 
                    299: inputs: formname, elementname, only, omit
                    300: 
                    301: formname and elementname indicate the name of the html form and name of
                    302: the element that the results of the browsing selection are to be placed in. 
                    303: 
                    304: Specifying 'only' will restrict the browser to displaying only files
1.185     www       305: with the given extension.  Can be a comma separated list.
1.42      matthew   306: 
                    307: Specifying 'omit' will restrict the browser to NOT displaying files
1.185     www       308: with the given extension.  Can be a comma separated list.
1.42      matthew   309: 
1.648     raeburn   310: =item * &opensearcher(formname,elementname) [javascript]
1.42      matthew   311: 
                    312: Inputs: formname, elementname
                    313: 
                    314: formname and elementname specify the name of the html form and the name
                    315: of the element the selection from the search results will be placed in.
1.542     raeburn   316: 
1.42      matthew   317: =cut
                    318: 
                    319: sub browser_and_searcher_javascript {
1.199     albertel  320:     my ($mode)=@_;
                    321:     if (!defined($mode)) { $mode='edit'; }
1.453     albertel  322:     my $resurl=&escape_single(&lastresurl());
1.42      matthew   323:     return <<END;
1.219     albertel  324: // <!-- BEGIN LON-CAPA Internal
1.50      matthew   325:     var editbrowser = null;
1.135     albertel  326:     function openbrowser(formname,elementname,only,omit,titleelement) {
1.170     www       327:         var url = '$resurl/?';
1.42      matthew   328:         if (editbrowser == null) {
                    329:             url += 'launch=1&';
                    330:         }
                    331:         url += 'catalogmode=interactive&';
1.199     albertel  332:         url += 'mode=$mode&';
1.611     albertel  333:         url += 'inhibitmenu=yes&';
1.42      matthew   334:         url += 'form=' + formname + '&';
                    335:         if (only != null) {
                    336:             url += 'only=' + only + '&';
1.217     albertel  337:         } else {
                    338:             url += 'only=&';
                    339: 	}
1.42      matthew   340:         if (omit != null) {
                    341:             url += 'omit=' + omit + '&';
1.217     albertel  342:         } else {
                    343:             url += 'omit=&';
                    344: 	}
1.135     albertel  345:         if (titleelement != null) {
                    346:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  347:         } else {
                    348: 	    url += 'titleelement=&';
                    349: 	}
1.42      matthew   350:         url += 'element=' + elementname + '';
                    351:         var title = 'Browser';
1.435     albertel  352:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   353:         options += ',width=700,height=600';
                    354:         editbrowser = open(url,title,options,'1');
                    355:         editbrowser.focus();
                    356:     }
                    357:     var editsearcher;
1.135     albertel  358:     function opensearcher(formname,elementname,titleelement) {
1.42      matthew   359:         var url = '/adm/searchcat?';
                    360:         if (editsearcher == null) {
                    361:             url += 'launch=1&';
                    362:         }
                    363:         url += 'catalogmode=interactive&';
1.199     albertel  364:         url += 'mode=$mode&';
1.42      matthew   365:         url += 'form=' + formname + '&';
1.135     albertel  366:         if (titleelement != null) {
                    367:             url += 'titleelement=' + titleelement + '&';
1.217     albertel  368:         } else {
                    369: 	    url += 'titleelement=&';
                    370: 	}
1.42      matthew   371:         url += 'element=' + elementname + '';
                    372:         var title = 'Search';
1.435     albertel  373:         var options = 'scrollbars=1,resizable=1,menubar=0,toolbar=1,location=1';
1.42      matthew   374:         options += ',width=700,height=600';
                    375:         editsearcher = open(url,title,options,'1');
                    376:         editsearcher.focus();
                    377:     }
1.219     albertel  378: // END LON-CAPA Internal -->
1.42      matthew   379: END
1.170     www       380: }
                    381: 
                    382: sub lastresurl {
1.258     albertel  383:     if ($env{'environment.lastresurl'}) {
                    384: 	return $env{'environment.lastresurl'}
1.170     www       385:     } else {
                    386: 	return '/res';
                    387:     }
                    388: }
                    389: 
                    390: sub storeresurl {
                    391:     my $resurl=&Apache::lonnet::clutter(shift);
                    392:     unless ($resurl=~/^\/res/) { return 0; }
                    393:     $resurl=~s/\/$//;
                    394:     &Apache::lonnet::put('environment',{'lastresurl' => $resurl});
1.646     raeburn   395:     &Apache::lonnet::appenv({'environment.lastresurl' => $resurl});
1.170     www       396:     return 1;
1.42      matthew   397: }
                    398: 
1.74      www       399: sub studentbrowser_javascript {
1.111     www       400:    unless (
1.258     albertel  401:             (($env{'request.course.id'}) && 
1.302     albertel  402:              (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    403: 	      || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    404: 					  '/'.$env{'request.course.sec'})
                    405: 	      ))
1.258     albertel  406:          || ($env{'request.role'}=~/^(au|dc|su)/)
1.111     www       407:           ) { return ''; }  
1.74      www       408:    return (<<'ENDSTDBRW');
1.692.4.2  raeburn   409: <script type="text/javascript" language="Javascript">
1.692.4.4  raeburn   410: // <![CDATA[
1.74      www       411:     var stdeditbrowser;
1.692.4.2  raeburn   412:     function openstdbrowser(formname,uname,udom,roleflag,ignorefilter,courseadvonly) {
1.74      www       413:         var url = '/adm/pickstudent?';
                    414:         var filter;
1.558     albertel  415: 	if (!ignorefilter) {
                    416: 	    eval('filter=document.'+formname+'.'+uname+'.value;');
                    417: 	}
1.74      www       418:         if (filter != null) {
                    419:            if (filter != '') {
                    420:                url += 'filter='+filter+'&';
                    421: 	   }
                    422:         }
                    423:         url += 'form=' + formname + '&unameelement='+uname+
                    424:                                     '&udomelement='+udom;
1.111     www       425: 	if (roleflag) { url+="&roles=1"; }
1.692.4.2  raeburn   426:         if (courseadvonly) { url+="&courseadvonly=1"; }
1.102     www       427:         var title = 'Student_Browser';
1.74      www       428:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    429:         options += ',width=700,height=600';
                    430:         stdeditbrowser = open(url,title,options,'1');
                    431:         stdeditbrowser.focus();
                    432:     }
1.692.4.4  raeburn   433: // ]]>
1.74      www       434: </script>
                    435: ENDSTDBRW
                    436: }
1.42      matthew   437: 
1.74      www       438: sub selectstudent_link {
1.692.4.2  raeburn   439:    my ($form,$unameele,$udomele,$courseadvonly)=@_;
                    440:    my $callargs = "'".$form."','".$unameele."','".$udomele."'";
1.258     albertel  441:    if ($env{'request.course.id'}) {  
1.302     albertel  442:        if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    443: 	   && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    444: 					'/'.$env{'request.course.sec'})) {
1.111     www       445: 	   return '';
                    446:        }
1.692.4.2  raeburn   447:        if ($courseadvonly)  {
                    448:            $callargs .= ",'',1,1";
                    449:        }
                    450:        return '<span class="LC_nobreak">'.
                    451:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    452:               &mt('Select User').'</a></span>';
1.74      www       453:    }
1.258     albertel  454:    if ($env{'request.role'}=~/^(au|dc|su)/) {
1.692.4.2  raeburn   455:        $callargs .= ",1";
                    456:        return '<span class="LC_nobreak">'.
                    457:               '<a href="javascript:openstdbrowser('.$callargs.');">'.
                    458:               &mt('Select User').'</a></span>';
1.111     www       459:    }
                    460:    return '';
1.91      www       461: }
                    462: 
1.653     raeburn   463: sub authorbrowser_javascript {
                    464:     return <<"ENDAUTHORBRW";
                    465: <script type="text/javascript">
1.692.4.4  raeburn   466: // <![CDATA[
1.653     raeburn   467: var stdeditbrowser;
                    468: 
                    469: function openauthorbrowser(formname,udom) {
                    470:     var url = '/adm/pickauthor?';
                    471:     url += 'form='+formname+'&roledom='+udom;
                    472:     var title = 'Author_Browser';
                    473:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    474:     options += ',width=700,height=600';
                    475:     stdeditbrowser = open(url,title,options,'1');
                    476:     stdeditbrowser.focus();
                    477: }
1.692.4.4  raeburn   478: // ]]>
1.653     raeburn   479: </script>
                    480: ENDAUTHORBRW
                    481: }
                    482: 
1.91      www       483: sub coursebrowser_javascript {
1.692.4.22  raeburn   484:     my ($domainfilter,$sec_element,$formname,$role_element,$crstype) = @_;
                    485:     my $wintitle = 'Course_Browser';
                    486:     if ($crstype eq 'Community') {
                    487:         $wintitle = 'Community_Browser';
                    488:     }
1.692.4.9  raeburn   489:     my $id_functions = &javascript_index_functions();
                    490:     my $output = '
1.692.4.2  raeburn   491: <script type="text/javascript" language="JavaScript">
1.692.4.4  raeburn   492: // <![CDATA[
1.468     raeburn   493:     var stdeditbrowser;'."\n";
1.692.4.9  raeburn   494: 
                    495:     $output .= <<"ENDSTDBRW";
1.692.4.22  raeburn   496:     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,type,type_elem) {
1.91      www       497:         var url = '/adm/pickcourse?';
1.692.4.18  raeburn   498:         var formid = getFormIdByName(formname);
1.692.4.9  raeburn   499:         var domainfilter = getDomainFromSelectbox(formname,udom);
1.128     albertel  500:         if (domainfilter != null) {
                    501:            if (domainfilter != '') {
                    502:                url += 'domainfilter='+domainfilter+'&';
                    503: 	   }
                    504:         }
1.91      www       505:         url += 'form=' + formname + '&cnumelement='+uname+
1.187     albertel  506: 	                            '&cdomelement='+udom+
                    507:                                     '&cnameelement='+desc;
1.468     raeburn   508:         if (extra_element !=null && extra_element != '') {
1.594     raeburn   509:             if (formname == 'rolechoice' || formname == 'studentform') {
1.468     raeburn   510:                 url += '&roleelement='+extra_element;
                    511:                 if (domainfilter == null || domainfilter == '') {
                    512:                     url += '&domainfilter='+extra_element;
                    513:                 }
1.234     raeburn   514:             }
1.468     raeburn   515:             else {
                    516:                 if (formname == 'portform') {
                    517:                     url += '&setroles='+extra_element;
1.692.4.25  raeburn   518:                 } else {
                    519:                     if (formname == 'rules') {
                    520:                         url += '&fixeddom='+extra_element;
                    521:                     }
1.468     raeburn   522:                 }
                    523:             }     
1.230     raeburn   524:         }
1.692.4.22  raeburn   525:         if (type != null && type != '') {
                    526:             url += '&type='+type;
                    527:         }
                    528:         if (type_elem != null && type_elem != '') {
                    529:             url += '&typeelement='+type_elem;
                    530:         }
1.692.4.7  raeburn   531:         if (formname == 'ccrs') {
                    532:             var ownername = document.forms[formid].ccuname.value;
                    533:             var ownerdom =  document.forms[formid].ccdomain.options[document.forms[formid].ccdomain.selectedIndex].value;
                    534:             url += '&cloner='+ownername+':'+ownerdom;
                    535:         }
1.293     raeburn   536:         if (multflag !=null && multflag != '') {
                    537:             url += '&multiple='+multflag;
                    538:         }
1.692.4.22  raeburn   539:         var title = '$wintitle';
1.91      www       540:         var options = 'scrollbars=1,resizable=1,menubar=0';
                    541:         options += ',width=700,height=600';
                    542:         stdeditbrowser = open(url,title,options,'1');
                    543:         stdeditbrowser.focus();
                    544:     }
1.692.4.9  raeburn   545: $id_functions
1.91      www       546: ENDSTDBRW
1.692.4.21  raeburn   547:     if (($sec_element ne '') || ($role_element ne '')) {
                    548:         $output .= &setsec_javascript($sec_element,$formname,$role_element);
1.468     raeburn   549:     }
                    550:     $output .= '
1.692.4.4  raeburn   551: // ]]>
1.468     raeburn   552: </script>';
                    553:     return $output;
                    554: }
                    555: 
1.692.4.9  raeburn   556: sub javascript_index_functions {
                    557:     return <<"ENDJS";
                    558: 
                    559: function getFormIdByName(formname) {
                    560:     for (var i=0;i<document.forms.length;i++) {
                    561:         if (document.forms[i].name == formname) {
                    562:             return i;
                    563:         }
                    564:     }
                    565:     return -1;
                    566: }
                    567: 
                    568: function getIndexByName(formid,item) {
                    569:     for (var i=0;i<document.forms[formid].elements.length;i++) {
                    570:         if (document.forms[formid].elements[i].name == item) {
                    571:             return i;
                    572:         }
                    573:     }
                    574:     return -1;
                    575: }
                    576: 
                    577: function getDomainFromSelectbox(formname,udom) {
                    578:     var userdom;
                    579:     var formid = getFormIdByName(formname);
                    580:     if (formid > -1) {
                    581:         var domid = getIndexByName(formid,udom);
                    582:         if (domid > -1) {
                    583:             if (document.forms[formid].elements[domid].type == 'select-one') {
                    584:                 userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
                    585:             }
                    586:             if (document.forms[formid].elements[domid].type == 'hidden') {
                    587:                 userdom=document.forms[formid].elements[domid].value;
                    588:             }
                    589:         }
                    590:     }
                    591:     return userdom;
                    592: }
                    593: 
                    594: ENDJS
                    595: 
                    596: }
                    597: 
                    598: sub userbrowser_javascript {
                    599:     my $id_functions = &javascript_index_functions();
                    600:     return <<"ENDUSERBRW";
                    601: 
1.692.4.17  raeburn   602: function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom,crsdom,caller) {
1.692.4.9  raeburn   603:     var url = '/adm/pickuser?';
                    604:     var userdom = getDomainFromSelectbox(formname,udom);
                    605:     if (userdom != null) {
                    606:        if (userdom != '') {
                    607:            url += 'srchdom='+userdom+'&';
                    608:        }
                    609:     }
                    610:     url += 'form=' + formname + '&unameelement='+uname+
                    611:                                 '&udomelement='+udom+
                    612:                                 '&ulastelement='+ulast+
                    613:                                 '&ufirstelement='+ufirst+
                    614:                                 '&uemailelement='+uemail+
                    615:                                 '&hideudomelement='+hideudom+
                    616:                                 '&coursedom='+crsdom;
1.692.4.17  raeburn   617:     if ((caller != null) && (caller != undefined)) {
                    618:         url += '&caller='+caller;
                    619:     }
1.692.4.9  raeburn   620:     var title = 'User_Browser';
                    621:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    622:     options += ',width=700,height=600';
                    623:     var stdeditbrowser = open(url,title,options,'1');
                    624:     stdeditbrowser.focus();
                    625: }
                    626: 
1.692.4.17  raeburn   627: function fix_domain (formname,udom,origdom,uname) {
1.692.4.9  raeburn   628:     var formid = getFormIdByName(formname);
                    629:     if (formid > -1) {
1.692.4.17  raeburn   630:         var unameid = getIndexByName(formid,uname);
1.692.4.9  raeburn   631:         var domid = getIndexByName(formid,udom);
                    632:         var hidedomid = getIndexByName(formid,origdom);
                    633:         if (hidedomid > -1) {
                    634:             var fixeddom = document.forms[formid].elements[hidedomid].value;
1.692.4.17  raeburn   635:             var unameval = document.forms[formid].elements[unameid].value;
                    636:             if ((fixeddom != '') && (fixeddom != undefined) && (fixeddom != null) && (unameval != '') && (unameval != undefined) && (unameval != null)) {
                    637:                 if (domid > -1) {
                    638:                     var slct = document.forms[formid].elements[domid];
                    639:                     if (slct.type == 'select-one') {
                    640:                         var i;
                    641:                         for (i=0;i<slct.length;i++) {
                    642:                             if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
                    643:                         }
                    644:                     }
                    645:                     if (slct.type == 'hidden') {
                    646:                         slct.value = fixeddom;
1.692.4.9  raeburn   647:                     }
                    648:                 }
                    649:             }
                    650:         }
                    651:     }
                    652:     return;
                    653: }
                    654: 
                    655: $id_functions
                    656: ENDUSERBRW
                    657: }
                    658: 
                    659: 
1.468     raeburn   660: sub setsec_javascript {
1.692.4.21  raeburn   661:     my ($sec_element,$formname,$role_element) = @_;
                    662:     my (@courserolenames,@communityrolenames,$rolestr,$courserolestr,
                    663:         $communityrolestr);
                    664:     if ($role_element ne '') {
                    665:         my @allroles = ('st','ta','ep','in','ad');
                    666:         foreach my $crstype ('Course','Community') {
                    667:             if ($crstype eq 'Community') {
                    668:                 foreach my $role (@allroles) {
                    669:                     push(@communityrolenames,&Apache::lonnet::plaintext($role,$crstype));
                    670:                 }
                    671:                 push(@communityrolenames,&Apache::lonnet::plaintext('co'));
                    672:             } else {
                    673:                 foreach my $role (@allroles) {
                    674:                     push(@courserolenames,&Apache::lonnet::plaintext($role,$crstype));
                    675:                 }
                    676:                 push(@courserolenames,&Apache::lonnet::plaintext('cc'));
                    677:             }
                    678:         }
                    679:         $rolestr = '"'.join('","',@allroles).'"';
                    680:         $courserolestr = '"'.join('","',@courserolenames).'"';
                    681:         $communityrolestr = '"'.join('","',@communityrolenames).'"';
                    682:     }
1.468     raeburn   683:     my $setsections = qq|
                    684: function setSect(sectionlist) {
1.629     raeburn   685:     var sectionsArray = new Array();
                    686:     if ((sectionlist != '') && (typeof sectionlist != "undefined")) {
                    687:         sectionsArray = sectionlist.split(",");
                    688:     }
1.468     raeburn   689:     var numSections = sectionsArray.length;
                    690:     document.$formname.$sec_element.length = 0;
                    691:     if (numSections == 0) {
                    692:         document.$formname.$sec_element.multiple=false;
                    693:         document.$formname.$sec_element.size=1;
                    694:         document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
                    695:     } else {
                    696:         if (numSections == 1) {
                    697:             document.$formname.$sec_element.multiple=false;
                    698:             document.$formname.$sec_element.size=1;
                    699:             document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
                    700:             document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
                    701:             document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
                    702:         } else {
                    703:             for (var i=0; i<numSections; i++) {
                    704:                 document.$formname.$sec_element.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
                    705:             }
                    706:             document.$formname.$sec_element.multiple=true
                    707:             if (numSections < 3) {
                    708:                 document.$formname.$sec_element.size=numSections;
                    709:             } else {
                    710:                 document.$formname.$sec_element.size=3;
                    711:             }
                    712:             document.$formname.$sec_element.options[0].selected = false
                    713:         }
                    714:     }
1.91      www       715: }
1.692.4.21  raeburn   716: 
                    717: function setRole(crstype) {
                    718: |;
                    719:     if ($role_element eq '') {
                    720:         $setsections .= '    return;
                    721: }
                    722: ';
                    723:     } else {
                    724:         $setsections .= qq|
                    725:     var elementLength = document.$formname.$role_element.length;
                    726:     var allroles = Array($rolestr);
                    727:     var courserolenames = Array($courserolestr);
                    728:     var communityrolenames = Array($communityrolestr);
                    729:     if (elementLength != undefined) {
                    730:         if (document.$formname.$role_element.options[5].value == 'cc') {
                    731:             if (crstype == 'Course') {
                    732:                 return;
                    733:             } else {
                    734:                 allroles[5] = 'co';
                    735:                 for (var i=0; i<6; i++) {
                    736:                     document.$formname.$role_element.options[i].value = allroles[i];
                    737:                     document.$formname.$role_element.options[i].text = communityrolenames[i];
                    738:                 }
                    739:             }
                    740:         } else {
                    741:             if (crstype == 'Community') {
                    742:                 return;
                    743:             } else {
                    744:                 allroles[5] = 'cc';
                    745:                 for (var i=0; i<6; i++) {
                    746:                     document.$formname.$role_element.options[i].value = allroles[i];
                    747:                     document.$formname.$role_element.options[i].text = courserolenames[i];
                    748:                 }
                    749:             }
                    750:         }
                    751:     }
                    752:     return;
                    753: }
1.468     raeburn   754: |;
1.692.4.21  raeburn   755:     }
1.468     raeburn   756:     return $setsections;
                    757: }
                    758: 
1.91      www       759: sub selectcourse_link {
1.692.4.22  raeburn   760:    my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype,
                    761:        $typeelement) = @_;
                    762:    my $type = $selecttype;
1.692.4.6  raeburn   763:    my $linktext = &mt('Select Course');
                    764:    if ($selecttype eq 'Community') {
                    765:        $linktext = &mt('Select Community');
1.692.4.22  raeburn   766:    } elsif ($selecttype eq 'Course/Community') {
                    767:        $linktext = &mt('Select Course/Community');
                    768:        $type = '';
1.692.4.6  raeburn   769:    }
1.692.4.2  raeburn   770:    return '<span class="LC_nobreak">'
                    771:          ."<a href='"
                    772:          .'javascript:opencrsbrowser("'.$form.'","'.$unameele
                    773:          .'","'.$udomele.'","'.$desc.'","'.$extra_element
1.692.4.22  raeburn   774:          .'","'.$multflag.'","'.$type.'","'.$typeelement.'");'
1.692.4.6  raeburn   775:          ."'>".$linktext.'</a>'
1.692.4.2  raeburn   776:          .'</span>';
1.74      www       777: }
1.42      matthew   778: 
1.653     raeburn   779: sub selectauthor_link {
                    780:    my ($form,$udom)=@_;
                    781:    return '<a href="javascript:openauthorbrowser('."'$form','$udom'".');">'.
                    782:           &mt('Select Author').'</a>';
                    783: }
                    784: 
1.692.4.9  raeburn   785: sub selectuser_link {
                    786:     my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,
1.692.4.17  raeburn   787:         $coursedom,$linktext,$caller) = @_;
1.692.4.9  raeburn   788:     return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
1.692.4.17  raeburn   789:            "'$lastelem','$firstelem','$emailelem','$hdomelem','$coursedom','$caller'".
1.692.4.9  raeburn   790:            ');">'.$linktext.'</a>';
                    791: }
                    792: 
1.273     raeburn   793: sub check_uncheck_jscript {
                    794:     my $jscript = <<"ENDSCRT";
                    795: function checkAll(field) {
                    796:     if (field.length > 0) {
                    797:         for (i = 0; i < field.length; i++) {
                    798:             field[i].checked = true ;
                    799:         }
                    800:     } else {
                    801:         field.checked = true
                    802:     }
                    803: }
                    804:  
                    805: function uncheckAll(field) {
                    806:     if (field.length > 0) {
                    807:         for (i = 0; i < field.length; i++) {
                    808:             field[i].checked = false ;
1.543     albertel  809:         }
                    810:     } else {
1.273     raeburn   811:         field.checked = false ;
                    812:     }
                    813: }
                    814: ENDSCRT
                    815:     return $jscript;
                    816: }
                    817: 
1.656     www       818: sub select_timezone {
1.659     raeburn   819:    my ($name,$selected,$onchange,$includeempty)=@_;
                    820:    my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    821:    if ($includeempty) {
                    822:        $output .= '<option value=""';
                    823:        if (($selected eq '') || ($selected eq 'local')) {
                    824:            $output .= ' selected="selected" ';
                    825:        }
                    826:        $output .= '> </option>';
                    827:    }
1.657     raeburn   828:    my @timezones = DateTime::TimeZone->all_names;
                    829:    foreach my $tzone (@timezones) {
                    830:        $output.= '<option value="'.$tzone.'"';
                    831:        if ($tzone eq $selected) {
                    832:            $output.=' selected="selected"';
                    833:        }
                    834:        $output.=">$tzone</option>\n";
1.656     www       835:    }
                    836:    $output.="</select>";
                    837:    return $output;
                    838: }
1.273     raeburn   839: 
1.687     raeburn   840: sub select_datelocale {
                    841:     my ($name,$selected,$onchange,$includeempty)=@_;
                    842:     my $output='<select name="'.$name.'" '.$onchange.'>'."\n";
                    843:     if ($includeempty) {
                    844:         $output .= '<option value=""';
                    845:         if ($selected eq '') {
                    846:             $output .= ' selected="selected" ';
                    847:         }
                    848:         $output .= '> </option>';
                    849:     }
                    850:     my (@possibles,%locale_names);
                    851:     my @locales = DateTime::Locale::Catalog::Locales;
                    852:     foreach my $locale (@locales) {
                    853:         if (ref($locale) eq 'HASH') {
                    854:             my $id = $locale->{'id'};
                    855:             if ($id ne '') {
                    856:                 my $en_terr = $locale->{'en_territory'};
                    857:                 my $native_terr = $locale->{'native_territory'};
1.692.4.1  raeburn   858:                 my @languages = &Apache::lonlocal::preferred_languages();
1.687     raeburn   859:                 if (grep(/^en$/,@languages) || !@languages) {
                    860:                     if ($en_terr ne '') {
                    861:                         $locale_names{$id} = '('.$en_terr.')';
                    862:                     } elsif ($native_terr ne '') {
                    863:                         $locale_names{$id} = $native_terr;
                    864:                     }
                    865:                 } else {
                    866:                     if ($native_terr ne '') {
                    867:                         $locale_names{$id} = $native_terr.' ';
                    868:                     } elsif ($en_terr ne '') {
                    869:                         $locale_names{$id} = '('.$en_terr.')';
                    870:                     }
                    871:                 }
                    872:                 push (@possibles,$id);
                    873:             }
                    874:         }
                    875:     }
                    876:     foreach my $item (sort(@possibles)) {
                    877:         $output.= '<option value="'.$item.'"';
                    878:         if ($item eq $selected) {
                    879:             $output.=' selected="selected"';
                    880:         }
                    881:         $output.=">$item";
                    882:         if ($locale_names{$item} ne '') {
                    883:             $output.="  $locale_names{$item}</option>\n";
                    884:         }
                    885:         $output.="</option>\n";
                    886:     }
                    887:     $output.="</select>";
                    888:     return $output;
                    889: }
                    890: 
1.692.4.2  raeburn   891: sub select_language {
                    892:     my ($name,$selected,$includeempty) = @_;
                    893:     my %langchoices;
                    894:     if ($includeempty) {
                    895:         %langchoices = ('' => 'No language preference');
                    896:     }
                    897:     foreach my $id (&languageids()) {
                    898:         my $code = &supportedlanguagecode($id);
                    899:         if ($code) {
                    900:             $langchoices{$code} = &plainlanguagedescription($id);
                    901:         }
                    902:     }
                    903:     return &select_form($selected,$name,%langchoices);
                    904: }
                    905: 
1.42      matthew   906: =pod
1.36      matthew   907: 
1.648     raeburn   908: =item * &linked_select_forms(...)
1.36      matthew   909: 
                    910: linked_select_forms returns a string containing a <script></script> block
                    911: and html for two <select> menus.  The select menus will be linked in that
                    912: changing the value of the first menu will result in new values being placed
                    913: in the second menu.  The values in the select menu will appear in alphabetical
1.609     raeburn   914: order unless a defined order is provided.
1.36      matthew   915: 
                    916: linked_select_forms takes the following ordered inputs:
                    917: 
                    918: =over 4
                    919: 
1.112     bowersj2  920: =item * $formname, the name of the <form> tag
1.36      matthew   921: 
1.112     bowersj2  922: =item * $middletext, the text which appears between the <select> tags
1.36      matthew   923: 
1.112     bowersj2  924: =item * $firstdefault, the default value for the first menu
1.36      matthew   925: 
1.112     bowersj2  926: =item * $firstselectname, the name of the first <select> tag
1.36      matthew   927: 
1.112     bowersj2  928: =item * $secondselectname, the name of the second <select> tag
1.36      matthew   929: 
1.112     bowersj2  930: =item * $hashref, a reference to a hash containing the data for the menus.
1.36      matthew   931: 
1.609     raeburn   932: =item * $menuorder, the order of values in the first menu
                    933: 
1.41      ng        934: =back 
                    935: 
1.36      matthew   936: Below is an example of such a hash.  Only the 'text', 'default', and 
                    937: 'select2' keys must appear as stated.  keys(%menu) are the possible 
                    938: values for the first select menu.  The text that coincides with the 
1.41      ng        939: first menu value is given in $menu{$choice1}->{'text'}.  The values 
1.36      matthew   940: and text for the second menu are given in the hash pointed to by 
                    941: $menu{$choice1}->{'select2'}.  
                    942: 
1.112     bowersj2  943:  my %menu = ( A1 => { text =>"Choice A1" ,
                    944:                        default => "B3",
                    945:                        select2 => { 
                    946:                            B1 => "Choice B1",
                    947:                            B2 => "Choice B2",
                    948:                            B3 => "Choice B3",
                    949:                            B4 => "Choice B4"
1.609     raeburn   950:                            },
                    951:                        order => ['B4','B3','B1','B2'],
1.112     bowersj2  952:                    },
                    953:                A2 => { text =>"Choice A2" ,
                    954:                        default => "C2",
                    955:                        select2 => { 
                    956:                            C1 => "Choice C1",
                    957:                            C2 => "Choice C2",
                    958:                            C3 => "Choice C3"
1.609     raeburn   959:                            },
                    960:                        order => ['C2','C1','C3'],
1.112     bowersj2  961:                    },
                    962:                A3 => { text =>"Choice A3" ,
                    963:                        default => "D6",
                    964:                        select2 => { 
                    965:                            D1 => "Choice D1",
                    966:                            D2 => "Choice D2",
                    967:                            D3 => "Choice D3",
                    968:                            D4 => "Choice D4",
                    969:                            D5 => "Choice D5",
                    970:                            D6 => "Choice D6",
                    971:                            D7 => "Choice D7"
1.609     raeburn   972:                            },
                    973:                        order => ['D4','D3','D2','D1','D7','D6','D5'],
1.112     bowersj2  974:                    }
                    975:                );
1.36      matthew   976: 
                    977: =cut
                    978: 
                    979: sub linked_select_forms {
                    980:     my ($formname,
                    981:         $middletext,
                    982:         $firstdefault,
                    983:         $firstselectname,
                    984:         $secondselectname, 
1.609     raeburn   985:         $hashref,
                    986:         $menuorder,
1.36      matthew   987:         ) = @_;
                    988:     my $second = "document.$formname.$secondselectname";
                    989:     my $first = "document.$formname.$firstselectname";
                    990:     # output the javascript to do the changing
                    991:     my $result = '';
1.692.4.2  raeburn   992:     $result.='<script type="text/javascript" language="JavaScript">'."\n";
1.692.4.4  raeburn   993:     $result.="// <![CDATA[\n";
1.36      matthew   994:     $result.="var select2data = new Object();\n";
                    995:     $" = '","';
                    996:     my $debug = '';
                    997:     foreach my $s1 (sort(keys(%$hashref))) {
                    998:         $result.="select2data.d_$s1 = new Object();\n";        
                    999:         $result.="select2data.d_$s1.def = new String('".
                   1000:             $hashref->{$s1}->{'default'}."');\n";
1.609     raeburn  1001:         $result.="select2data.d_$s1.values = new Array(";
1.36      matthew  1002:         my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
1.609     raeburn  1003:         if (ref($hashref->{$s1}->{'order'}) eq 'ARRAY') {
                   1004:             @s2values = @{$hashref->{$s1}->{'order'}};
                   1005:         }
1.36      matthew  1006:         $result.="\"@s2values\");\n";
                   1007:         $result.="select2data.d_$s1.texts = new Array(";        
                   1008:         my @s2texts;
                   1009:         foreach my $value (@s2values) {
                   1010:             push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
                   1011:         }
                   1012:         $result.="\"@s2texts\");\n";
                   1013:     }
                   1014:     $"=' ';
                   1015:     $result.= <<"END";
                   1016: 
                   1017: function select1_changed() {
                   1018:     // Determine new choice
                   1019:     var newvalue = "d_" + $first.value;
                   1020:     // update select2
                   1021:     var values     = select2data[newvalue].values;
                   1022:     var texts      = select2data[newvalue].texts;
                   1023:     var select2def = select2data[newvalue].def;
                   1024:     var i;
                   1025:     // out with the old
                   1026:     for (i = 0; i < $second.options.length; i++) {
                   1027:         $second.options[i] = null;
                   1028:     }
                   1029:     // in with the nuclear
                   1030:     for (i=0;i<values.length; i++) {
                   1031:         $second.options[i] = new Option(values[i]);
1.143     matthew  1032:         $second.options[i].value = values[i];
1.36      matthew  1033:         $second.options[i].text = texts[i];
                   1034:         if (values[i] == select2def) {
                   1035:             $second.options[i].selected = true;
                   1036:         }
                   1037:     }
                   1038: }
1.692.4.4  raeburn  1039: // ]]>
1.36      matthew  1040: </script>
                   1041: END
                   1042:     # output the initial values for the selection lists
                   1043:     $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
1.609     raeburn  1044:     my @order = sort(keys(%{$hashref}));
                   1045:     if (ref($menuorder) eq 'ARRAY') {
                   1046:         @order = @{$menuorder};
                   1047:     }
                   1048:     foreach my $value (@order) {
1.36      matthew  1049:         $result.="    <option value=\"$value\" ";
1.253     albertel 1050:         $result.=" selected=\"selected\" " if ($value eq $firstdefault);
1.119     www      1051:         $result.=">".&mt($hashref->{$value}->{'text'})."</option>\n";
1.36      matthew  1052:     }
                   1053:     $result .= "</select>\n";
                   1054:     my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
                   1055:     $result .= $middletext;
                   1056:     $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
                   1057:     my $seconddefault = $hashref->{$firstdefault}->{'default'};
1.609     raeburn  1058:     
                   1059:     my @secondorder = sort(keys(%select2));
                   1060:     if (ref($hashref->{$firstdefault}->{'order'}) eq 'ARRAY') {
                   1061:         @secondorder = @{$hashref->{$firstdefault}->{'order'}};
                   1062:     }
                   1063:     foreach my $value (@secondorder) {
1.36      matthew  1064:         $result.="    <option value=\"$value\" ";        
1.253     albertel 1065:         $result.=" selected=\"selected\" " if ($value eq $seconddefault);
1.119     www      1066:         $result.=">".&mt($select2{$value})."</option>\n";
1.36      matthew  1067:     }
                   1068:     $result .= "</select>\n";
                   1069:     #    return $debug;
                   1070:     return $result;
                   1071: }   #  end of sub linked_select_forms {
                   1072: 
1.45      matthew  1073: =pod
1.44      bowersj2 1074: 
1.648     raeburn  1075: =item * &help_open_topic($topic,$text,$stayOnPage,$width,$height)
1.44      bowersj2 1076: 
1.112     bowersj2 1077: Returns a string corresponding to an HTML link to the given help
                   1078: $topic, where $topic corresponds to the name of a .tex file in
                   1079: /home/httpd/html/adm/help/tex, with underscores replaced by
                   1080: spaces. 
                   1081: 
                   1082: $text will optionally be linked to the same topic, allowing you to
                   1083: link text in addition to the graphic. If you do not want to link
                   1084: text, but wish to specify one of the later parameters, pass an
                   1085: empty string. 
                   1086: 
                   1087: $stayOnPage is a value that will be interpreted as a boolean. If true,
                   1088: the link will not open a new window. If false, the link will open
                   1089: a new window using Javascript. (Default is false.) 
                   1090: 
                   1091: $width and $height are optional numerical parameters that will
                   1092: override the width and height of the popped up window, which may
                   1093: be useful for certain help topics with big pictures included. 
1.44      bowersj2 1094: 
                   1095: =cut
                   1096: 
                   1097: sub help_open_topic {
1.48      bowersj2 1098:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
                   1099:     $text = "" if (not defined $text);
1.44      bowersj2 1100:     $stayOnPage = 0 if (not defined $stayOnPage);
1.552     banghart 1101:     if ($env{'browser.interface'} eq 'textual') {
1.79      www      1102: 	$stayOnPage=1;
                   1103:     }
1.44      bowersj2 1104:     $width = 350 if (not defined $width);
                   1105:     $height = 400 if (not defined $height);
                   1106:     my $filename = $topic;
                   1107:     $filename =~ s/ /_/g;
                   1108: 
1.48      bowersj2 1109:     my $template = "";
                   1110:     my $link;
1.572     banghart 1111:     
1.159     www      1112:     $topic=~s/\W/\_/g;
1.44      bowersj2 1113: 
1.572     banghart 1114:     if (!$stayOnPage) {
1.72      bowersj2 1115: 	$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 1116:     } else {
1.48      bowersj2 1117: 	$link = "/adm/help/${filename}.hlp";
                   1118:     }
                   1119: 
                   1120:     # Add the text
1.572     banghart 1121:     if ($text ne "") {
1.77      www      1122: 	$template .= 
1.572     banghart 1123:             "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
1.691     bisitz   1124:             "<td bgcolor='#5555FF'><span class=\"LC_nobreak\"><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.48      bowersj2 1125:     }
                   1126: 
                   1127:     # Add the graphic
1.179     matthew  1128:     my $title = &mt('Online Help');
1.667     raeburn  1129:     my $helpicon=&lonhttpdurl("/adm/help/help.png");
1.692.4.2  raeburn  1130:     $template .= '<a target="_top" href="'.$link.'" title="'.$title.'">'.
                   1131:                  '<img src="'.$helpicon.'" border="0" alt="'.&mt('Help: [_1]',$topic).
                   1132:                  '" title="'.$title.'" /></a>';
                   1133:     if ($text ne '') {
                   1134:         $template.='</span></td></tr></table>';
                   1135:     }
1.44      bowersj2 1136:     return $template;
                   1137: 
1.106     bowersj2 1138: }
                   1139: 
                   1140: # This is a quicky function for Latex cheatsheet editing, since it 
                   1141: # appears in at least four places
                   1142: sub helpLatexCheatsheet {
1.692.4.2  raeburn  1143:     my ($topic,$text,$not_author) = @_;
                   1144:     my $out;
1.106     bowersj2 1145:     my $addOther = '';
1.692.4.3  raeburn  1146:     if ($topic) {
1.692.4.2  raeburn  1147: 	$addOther = &Apache::loncommon::help_open_topic($topic,$text,
1.106     bowersj2 1148: 						       undef, undef, 600) .
                   1149: 							   '</td><td>';
                   1150:     }
1.692.4.2  raeburn  1151:     $out = '<table><tr><td>'.
                   1152:            $addOther .
                   1153:            &Apache::loncommon::help_open_topic("Greek_Symbols",&mt('Greek Symbols'),
                   1154:                                                undef,undef,600).
                   1155:            '</td><td>'.
                   1156:            &Apache::loncommon::help_open_topic("Other_Symbols",&mt('Other Symbols'),
                   1157:                                                undef,undef,600).
                   1158:            '</td>';
                   1159:     unless ($not_author) {
                   1160:         $out .= '<td>'.
                   1161:                 &Apache::loncommon::help_open_topic("Authoring_Output_Tags",&mt('Output Tags'),
                   1162:                                                     undef,undef,600).
                   1163:                 '</td>';
                   1164:     }
                   1165:     $out .= '</tr></table>';
                   1166:     return $out;
1.172     www      1167: }
                   1168: 
1.430     albertel 1169: sub general_help {
                   1170:     my $helptopic='Student_Intro';
                   1171:     if ($env{'request.role'}=~/^(ca|au)/) {
                   1172: 	$helptopic='Authoring_Intro';
1.692.4.22  raeburn  1173:     } elsif ($env{'request.role'}=~/^(cc|co)/) {
1.430     albertel 1174: 	$helptopic='Course_Coordination_Intro';
1.672     raeburn  1175:     } elsif ($env{'request.role'}=~/^dc/) {
                   1176:         $helptopic='Domain_Coordination_Intro';
1.430     albertel 1177:     }
                   1178:     return $helptopic;
                   1179: }
                   1180: 
                   1181: sub update_help_link {
                   1182:     my ($topic,$component_help,$faq,$bug,$stayOnPage) = @_;
                   1183:     my $origurl = $ENV{'REQUEST_URI'};
                   1184:     $origurl=~s|^/~|/priv/|;
                   1185:     my $timestamp = time;
                   1186:     foreach my $datum (\$topic,\$component_help,\$faq,\$bug,\$origurl) {
                   1187:         $$datum = &escape($$datum);
                   1188:     }
                   1189: 
                   1190:     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";
                   1191:     my $output .= <<"ENDOUTPUT";
                   1192: <script type="text/javascript">
1.692.4.4  raeburn  1193: // <![CDATA[
1.430     albertel 1194: banner_link = '$banner_link';
1.692.4.4  raeburn  1195: // ]]>
1.430     albertel 1196: </script>
                   1197: ENDOUTPUT
                   1198:     return $output;
                   1199: }
                   1200: 
                   1201: # now just updates the help link and generates a blue icon
1.193     raeburn  1202: sub help_open_menu {
1.430     albertel 1203:     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
1.552     banghart 1204: 	= @_;    
1.430     albertel 1205:     $stayOnPage = 0 if (not defined $stayOnPage);
1.572     banghart 1206:     # only use pop-up help (stayOnPage == 0)
1.552     banghart 1207:     # if environment.remote is on (using remote control UI)
1.572     banghart 1208:     if ($env{'browser.interface'} eq 'textual' ||
                   1209:     	$env{'environment.remote'} eq 'off' ) {
1.552     banghart 1210:         $stayOnPage=1;
1.430     albertel 1211:     }
                   1212:     my $output;
                   1213:     if ($component_help) {
                   1214: 	if (!$text) {
                   1215: 	    $output=&help_open_topic($component_help,undef,$stayOnPage,
                   1216: 				       $width,$height);
                   1217: 	} else {
                   1218: 	    my $help_text;
                   1219: 	    $help_text=&unescape($topic);
                   1220: 	    $output='<table><tr><td>'.
                   1221: 		&help_open_topic($component_help,$help_text,$stayOnPage,
                   1222: 				 $width,$height).'</td></tr></table>';
                   1223: 	}
                   1224:     }
                   1225:     my $banner_link = &update_help_link($topic,$component_help,$faq,$bug,$stayOnPage);
                   1226:     return $output.$banner_link;
                   1227: }
                   1228: 
                   1229: sub top_nav_help {
                   1230:     my ($text) = @_;
1.436     albertel 1231:     $text = &mt($text);
1.572     banghart 1232:     my $stay_on_page = 
1.436     albertel 1233: 	($env{'browser.interface'}  eq 'textual' ||
                   1234: 	 $env{'environment.remote'} eq 'off' );
1.572     banghart 1235:     my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
1.436     albertel 1236: 	                     : "javascript:helpMenu('open')";
1.572     banghart 1237:     my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
1.436     albertel 1238: 
1.201     raeburn  1239:     my $title = &mt('Get help');
1.436     albertel 1240: 
                   1241:     return <<"END";
                   1242: $banner_link
                   1243:  <a href="$link" title="$title">$text</a>
                   1244: END
                   1245: }
                   1246: 
                   1247: sub help_menu_js {
                   1248:     my ($text) = @_;
                   1249: 
                   1250:     my $stayOnPage = 
                   1251: 	($env{'browser.interface'}  eq 'textual' ||
                   1252: 	 $env{'environment.remote'} eq 'off' );
                   1253: 
                   1254:     my $width = 620;
                   1255:     my $height = 600;
1.430     albertel 1256:     my $helptopic=&general_help();
                   1257:     my $details_link = '/adm/help/'.$helptopic.'.hlp';
1.261     albertel 1258:     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
1.331     albertel 1259:     my $start_page =
                   1260:         &Apache::loncommon::start_page('Help Menu', undef,
                   1261: 				       {'frameset'    => 1,
                   1262: 					'js_ready'    => 1,
                   1263: 					'add_entries' => {
                   1264: 					    'border' => '0',
1.579     raeburn  1265: 					    'rows'   => "110,*",},});
1.331     albertel 1266:     my $end_page =
                   1267:         &Apache::loncommon::end_page({'frameset' => 1,
                   1268: 				      'js_ready' => 1,});
                   1269: 
1.436     albertel 1270:     my $template .= <<"ENDTEMPLATE";
                   1271: <script type="text/javascript">
1.253     albertel 1272: // <![CDATA[
1.692.4.10  raeburn  1273: // <!-- BEGIN LON-CAPA Internal
1.430     albertel 1274: var banner_link = '';
1.243     raeburn  1275: function helpMenu(target) {
                   1276:     var caller = this;
                   1277:     if (target == 'open') {
                   1278:         var newWindow = null;
                   1279:         try {
1.262     albertel 1280:             newWindow =  window.open($nothing,"helpmenu","HEIGHT=$height,WIDTH=$width,resizable=yes,scrollbars=yes" )
1.243     raeburn  1281:         }
                   1282:         catch(error) {
                   1283:             writeHelp(caller);
                   1284:             return;
                   1285:         }
                   1286:         if (newWindow) {
                   1287:             caller = newWindow;
                   1288:         }
1.193     raeburn  1289:     }
1.243     raeburn  1290:     writeHelp(caller);
                   1291:     return;
                   1292: }
                   1293: function writeHelp(caller) {
1.430     albertel 1294:     caller.document.writeln('$start_page<frame name="bannerframe"  src="'+banner_link+'" /><frame name="bodyframe" src="$details_link" /> $end_page')
1.243     raeburn  1295:     caller.document.close()
                   1296:     caller.focus()
1.193     raeburn  1297: }
1.219     albertel 1298: // END LON-CAPA Internal -->
1.692.4.10  raeburn  1299: // ]]>
1.436     albertel 1300: </script>
1.193     raeburn  1301: ENDTEMPLATE
                   1302:     return $template;
                   1303: }
                   1304: 
1.172     www      1305: sub help_open_bug {
                   1306:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1307:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1308:     unless ($Apache::lonnet::perlvar{'BugzillaHost'}) { return ''; }
                   1309:     $text = "" if (not defined $text);
                   1310:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel 1311:     if ($env{'browser.interface'} eq 'textual' ||
                   1312: 	$env{'environment.remote'} eq 'off' ) {
1.172     www      1313: 	$stayOnPage=1;
                   1314:     }
1.184     albertel 1315:     $width = 600 if (not defined $width);
                   1316:     $height = 600 if (not defined $height);
1.172     www      1317: 
                   1318:     $topic=~s/\W+/\+/g;
                   1319:     my $link='';
                   1320:     my $template='';
1.379     albertel 1321:     my $url=$Apache::lonnet::perlvar{'BugzillaHost'}.'enter_bug.cgi?product=LON-CAPA&amp;bug_file_loc='.
                   1322: 	&escape($ENV{'REQUEST_URI'}).'&amp;component='.$topic;
1.172     www      1323:     if (!$stayOnPage)
                   1324:     {
                   1325: 	$link = "javascript:void(open('$url', 'Bugzilla', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1326:     }
                   1327:     else
                   1328:     {
                   1329: 	$link = $url;
                   1330:     }
                   1331:     # Add the text
                   1332:     if ($text ne "")
                   1333:     {
                   1334: 	$template .= 
                   1335:   "<table bgcolor='#AA3333' cellspacing='1' cellpadding='1' border='0'><tr>".
1.436     albertel 1336:   "<td bgcolor='#FF5555'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172     www      1337:     }
                   1338: 
                   1339:     # Add the graphic
1.179     matthew  1340:     my $title = &mt('Report a Bug');
1.215     albertel 1341:     my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif");
1.172     www      1342:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1343:  <a target="_top" href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a>
1.172     www      1344: ENDTEMPLATE
                   1345:     if ($text ne '') { $template.='</td></tr></table>' };
                   1346:     return $template;
                   1347: 
                   1348: }
                   1349: 
                   1350: sub help_open_faq {
                   1351:     my ($topic, $text, $stayOnPage, $width, $height) = @_;
1.258     albertel 1352:     unless ($env{'user.adv'}) { return ''; }
1.172     www      1353:     unless ($Apache::lonnet::perlvar{'FAQHost'}) { return ''; }
                   1354:     $text = "" if (not defined $text);
                   1355:     $stayOnPage = 0 if (not defined $stayOnPage);
1.258     albertel 1356:     if ($env{'browser.interface'} eq 'textual' ||
                   1357: 	$env{'environment.remote'} eq 'off' ) {
1.172     www      1358: 	$stayOnPage=1;
                   1359:     }
                   1360:     $width = 350 if (not defined $width);
                   1361:     $height = 400 if (not defined $height);
                   1362: 
                   1363:     $topic=~s/\W+/\+/g;
                   1364:     my $link='';
                   1365:     my $template='';
                   1366:     my $url=$Apache::lonnet::perlvar{'FAQHost'}.'/fom/cache/'.$topic.'.html';
                   1367:     if (!$stayOnPage)
                   1368:     {
                   1369: 	$link = "javascript:void(open('$url', 'FAQ-O-Matic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
                   1370:     }
                   1371:     else
                   1372:     {
                   1373: 	$link = $url;
                   1374:     }
                   1375: 
                   1376:     # Add the text
                   1377:     if ($text ne "")
                   1378:     {
                   1379: 	$template .= 
1.173     www      1380:   "<table bgcolor='#337733' cellspacing='1' cellpadding='1' border='0'><tr>".
1.436     albertel 1381:   "<td bgcolor='#448844'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
1.172     www      1382:     }
                   1383: 
                   1384:     # Add the graphic
1.179     matthew  1385:     my $title = &mt('View the FAQ');
1.215     albertel 1386:     my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif");
1.172     www      1387:     $template .= <<"ENDTEMPLATE";
1.436     albertel 1388:  <a target="_top" href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a>
1.172     www      1389: ENDTEMPLATE
                   1390:     if ($text ne '') { $template.='</td></tr></table>' };
                   1391:     return $template;
                   1392: 
1.44      bowersj2 1393: }
1.37      matthew  1394: 
1.180     matthew  1395: ###############################################################
                   1396: ###############################################################
                   1397: 
1.45      matthew  1398: =pod
                   1399: 
1.648     raeburn  1400: =item * &change_content_javascript():
1.256     matthew  1401: 
                   1402: This and the next function allow you to create small sections of an
                   1403: otherwise static HTML page that you can update on the fly with
                   1404: Javascript, even in Netscape 4.
                   1405: 
                   1406: The Javascript fragment returned by this function (no E<lt>scriptE<gt> tag)
                   1407: must be written to the HTML page once. It will prove the Javascript
                   1408: function "change(name, content)". Calling the change function with the
                   1409: name of the section 
                   1410: you want to update, matching the name passed to C<changable_area>, and
                   1411: the new content you want to put in there, will put the content into
                   1412: that area.
                   1413: 
                   1414: B<Note>: Netscape 4 only reserves enough space for the changable area
                   1415: to contain room for the original contents. You need to "make space"
                   1416: for whatever changes you wish to make, and be B<sure> to check your
                   1417: code in Netscape 4. This feature in Netscape 4 is B<not> powerful;
                   1418: it's adequate for updating a one-line status display, but little more.
                   1419: This script will set the space to 100% width, so you only need to
                   1420: worry about height in Netscape 4.
                   1421: 
                   1422: Modern browsers are much less limiting, and if you can commit to the
                   1423: user not using Netscape 4, this feature may be used freely with
                   1424: pretty much any HTML.
                   1425: 
                   1426: =cut
                   1427: 
                   1428: sub change_content_javascript {
                   1429:     # If we're on Netscape 4, we need to use Layer-based code
1.258     albertel 1430:     if ($env{'browser.type'} eq 'netscape' &&
                   1431: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1432: 	return (<<NETSCAPE4);
                   1433: 	function change(name, content) {
                   1434: 	    doc = document.layers[name+"___escape"].layers[0].document;
                   1435: 	    doc.open();
                   1436: 	    doc.write(content);
                   1437: 	    doc.close();
                   1438: 	}
                   1439: NETSCAPE4
                   1440:     } else {
                   1441: 	# Otherwise, we need to use semi-standards-compliant code
                   1442: 	# (technically, "innerHTML" isn't standard but the equivalent
                   1443: 	# is really scary, and every useful browser supports it
                   1444: 	return (<<DOMBASED);
                   1445: 	function change(name, content) {
                   1446: 	    element = document.getElementById(name);
                   1447: 	    element.innerHTML = content;
                   1448: 	}
                   1449: DOMBASED
                   1450:     }
                   1451: }
                   1452: 
                   1453: =pod
                   1454: 
1.648     raeburn  1455: =item * &changable_area($name,$origContent):
1.256     matthew  1456: 
                   1457: This provides a "changable area" that can be modified on the fly via
                   1458: the Javascript code provided in C<change_content_javascript>. $name is
                   1459: the name you will use to reference the area later; do not repeat the
                   1460: same name on a given HTML page more then once. $origContent is what
                   1461: the area will originally contain, which can be left blank.
                   1462: 
                   1463: =cut
                   1464: 
                   1465: sub changable_area {
                   1466:     my ($name, $origContent) = @_;
                   1467: 
1.258     albertel 1468:     if ($env{'browser.type'} eq 'netscape' &&
                   1469: 	$env{'browser.version'} =~ /^4\./) {
1.256     matthew  1470: 	# If this is netscape 4, we need to use the Layer tag
                   1471: 	return "<ilayer width='100%' id='${name}___escape' overflow='none'><layer width='100%' id='$name' overflow='none'>$origContent</layer></ilayer>";
                   1472:     } else {
                   1473: 	return "<span id='$name'>$origContent</span>";
                   1474:     }
                   1475: }
                   1476: 
                   1477: =pod
                   1478: 
1.648     raeburn  1479: =item * &viewport_geometry_js 
1.590     raeburn  1480: 
                   1481: Provides javascript object (Geometry) which can provide information about the viewport geometry for the client browser.
                   1482: 
                   1483: =cut
                   1484: 
                   1485: 
                   1486: sub viewport_geometry_js { 
                   1487:     return <<"GEOMETRY";
                   1488: var Geometry = {};
                   1489: function init_geometry() {
                   1490:     if (Geometry.init) { return };
                   1491:     Geometry.init=1;
                   1492:     if (window.innerHeight) {
                   1493:         Geometry.getViewportHeight   = function() { return window.innerHeight; };
                   1494:         Geometry.getViewportWidth   = function() { return window.innerWidth; };
                   1495:         Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
                   1496:         Geometry.getVerticalScroll   = function() { return window.pageYOffset; };
                   1497:     }
                   1498:     else if (document.documentElement && document.documentElement.clientHeight) {
                   1499:         Geometry.getViewportHeight =
                   1500:             function() { return document.documentElement.clientHeight; };
                   1501:         Geometry.getViewportWidth =
                   1502:             function() { return document.documentElement.clientWidth; };
                   1503: 
                   1504:         Geometry.getHorizontalScroll =
                   1505:             function() { return document.documentElement.scrollLeft; };
                   1506:         Geometry.getVerticalScroll =
                   1507:             function() { return document.documentElement.scrollTop; };
                   1508:     }
                   1509:     else if (document.body.clientHeight) {
                   1510:         Geometry.getViewportHeight =
                   1511:             function() { return document.body.clientHeight; };
                   1512:         Geometry.getViewportWidth =
                   1513:             function() { return document.body.clientWidth; };
                   1514:         Geometry.getHorizontalScroll =
                   1515:             function() { return document.body.scrollLeft; };
                   1516:         Geometry.getVerticalScroll =
                   1517:             function() { return document.body.scrollTop; };
                   1518:     }
                   1519: }
                   1520: 
                   1521: GEOMETRY
                   1522: }
                   1523: 
                   1524: =pod
                   1525: 
1.648     raeburn  1526: =item * &viewport_size_js()
1.590     raeburn  1527: 
                   1528: Provides a javascript function to set values of two form elements - width and height (elements are passed in as arguments to the javascript function) to the dimensions of the user's browser window. 
                   1529: 
                   1530: =cut
                   1531: 
                   1532: sub viewport_size_js {
                   1533:     my $geometry = &viewport_geometry_js();
                   1534:     return <<"DIMS";
                   1535: 
                   1536: $geometry
                   1537: 
                   1538: function getViewportDims(width,height) {
                   1539:     init_geometry();
                   1540:     width.value = Geometry.getViewportWidth();
                   1541:     height.value = Geometry.getViewportHeight();
                   1542:     return;
                   1543: }
                   1544: 
                   1545: DIMS
                   1546: }
                   1547: 
                   1548: =pod
                   1549: 
1.648     raeburn  1550: =item * &resize_textarea_js()
1.565     albertel 1551: 
                   1552: emits the needed javascript to resize a textarea to be as big as possible
                   1553: 
                   1554: creates a function resize_textrea that takes two IDs first should be
                   1555: the id of the element to resize, second should be the id of a div that
                   1556: surrounds everything that comes after the textarea, this routine needs
                   1557: to be attached to the <body> for the onload and onresize events.
                   1558: 
1.648     raeburn  1559: =back
1.565     albertel 1560: 
                   1561: =cut
                   1562: 
                   1563: sub resize_textarea_js {
1.590     raeburn  1564:     my $geometry = &viewport_geometry_js();
1.565     albertel 1565:     return <<"RESIZE";
                   1566:     <script type="text/javascript">
1.692.4.4  raeburn  1567: // <![CDATA[
1.590     raeburn  1568: $geometry
1.565     albertel 1569: 
1.588     albertel 1570: function getX(element) {
                   1571:     var x = 0;
                   1572:     while (element) {
                   1573: 	x += element.offsetLeft;
                   1574: 	element = element.offsetParent;
                   1575:     }
                   1576:     return x;
                   1577: }
                   1578: function getY(element) {
                   1579:     var y = 0;
                   1580:     while (element) {
                   1581: 	y += element.offsetTop;
                   1582: 	element = element.offsetParent;
                   1583:     }
                   1584:     return y;
                   1585: }
                   1586: 
                   1587: 
1.565     albertel 1588: function resize_textarea(textarea_id,bottom_id) {
                   1589:     init_geometry();
                   1590:     var textarea        = document.getElementById(textarea_id);
                   1591:     //alert(textarea);
                   1592: 
1.588     albertel 1593:     var textarea_top    = getY(textarea);
1.565     albertel 1594:     var textarea_height = textarea.offsetHeight;
                   1595:     var bottom          = document.getElementById(bottom_id);
1.588     albertel 1596:     var bottom_top      = getY(bottom);
1.565     albertel 1597:     var bottom_height   = bottom.offsetHeight;
                   1598:     var window_height   = Geometry.getViewportHeight();
1.588     albertel 1599:     var fudge           = 23;
1.565     albertel 1600:     var new_height      = window_height-fudge-textarea_top-bottom_height;
                   1601:     if (new_height < 300) {
                   1602: 	new_height = 300;
                   1603:     }
                   1604:     textarea.style.height=new_height+'px';
                   1605: }
1.692.4.4  raeburn  1606: // ]]>
1.565     albertel 1607: </script>
                   1608: RESIZE
                   1609: 
                   1610: }
                   1611: 
                   1612: =pod
                   1613: 
1.256     matthew  1614: =head1 Excel and CSV file utility routines
                   1615: 
                   1616: =over 4
                   1617: 
                   1618: =cut
                   1619: 
                   1620: ###############################################################
                   1621: ###############################################################
                   1622: 
                   1623: =pod
                   1624: 
1.648     raeburn  1625: =item * &csv_translate($text) 
1.37      matthew  1626: 
1.185     www      1627: Translate $text to allow it to be output as a 'comma separated values' 
1.37      matthew  1628: format.
                   1629: 
                   1630: =cut
                   1631: 
1.180     matthew  1632: ###############################################################
                   1633: ###############################################################
1.37      matthew  1634: sub csv_translate {
                   1635:     my $text = shift;
                   1636:     $text =~ s/\"/\"\"/g;
1.209     albertel 1637:     $text =~ s/\n/ /g;
1.37      matthew  1638:     return $text;
                   1639: }
1.180     matthew  1640: 
                   1641: ###############################################################
                   1642: ###############################################################
                   1643: 
                   1644: =pod
                   1645: 
1.648     raeburn  1646: =item * &define_excel_formats()
1.180     matthew  1647: 
                   1648: Define some commonly used Excel cell formats.
                   1649: 
                   1650: Currently supported formats:
                   1651: 
                   1652: =over 4
                   1653: 
                   1654: =item header
                   1655: 
                   1656: =item bold
                   1657: 
                   1658: =item h1
                   1659: 
                   1660: =item h2
                   1661: 
                   1662: =item h3
                   1663: 
1.256     matthew  1664: =item h4
                   1665: 
                   1666: =item i
                   1667: 
1.180     matthew  1668: =item date
                   1669: 
                   1670: =back
                   1671: 
                   1672: Inputs: $workbook
                   1673: 
                   1674: Returns: $format, a hash reference.
                   1675: 
                   1676: =cut
                   1677: 
                   1678: ###############################################################
                   1679: ###############################################################
                   1680: sub define_excel_formats {
                   1681:     my ($workbook) = @_;
                   1682:     my $format;
                   1683:     $format->{'header'} = $workbook->add_format(bold      => 1, 
                   1684:                                                 bottom    => 1,
                   1685:                                                 align     => 'center');
                   1686:     $format->{'bold'} = $workbook->add_format(bold=>1);
                   1687:     $format->{'h1'}   = $workbook->add_format(bold=>1, size=>18);
                   1688:     $format->{'h2'}   = $workbook->add_format(bold=>1, size=>16);
                   1689:     $format->{'h3'}   = $workbook->add_format(bold=>1, size=>14);
1.255     matthew  1690:     $format->{'h4'}   = $workbook->add_format(bold=>1, size=>12);
1.246     matthew  1691:     $format->{'i'}    = $workbook->add_format(italic=>1);
1.180     matthew  1692:     $format->{'date'} = $workbook->add_format(num_format=>
1.207     matthew  1693:                                             'mm/dd/yyyy hh:mm:ss');
1.180     matthew  1694:     return $format;
                   1695: }
                   1696: 
                   1697: ###############################################################
                   1698: ###############################################################
1.113     bowersj2 1699: 
                   1700: =pod
                   1701: 
1.648     raeburn  1702: =item * &create_workbook()
1.255     matthew  1703: 
                   1704: Create an Excel worksheet.  If it fails, output message on the
                   1705: request object and return undefs.
                   1706: 
                   1707: Inputs: Apache request object
                   1708: 
                   1709: Returns (undef) on failure, 
                   1710:     Excel worksheet object, scalar with filename, and formats 
                   1711:     from &Apache::loncommon::define_excel_formats on success
                   1712: 
                   1713: =cut
                   1714: 
                   1715: ###############################################################
                   1716: ###############################################################
                   1717: sub create_workbook {
                   1718:     my ($r) = @_;
                   1719:         #
                   1720:     # Create the excel spreadsheet
                   1721:     my $filename = '/prtspool/'.
1.258     albertel 1722:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.255     matthew  1723:         time.'_'.rand(1000000000).'.xls';
                   1724:     my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                   1725:     if (! defined($workbook)) {
                   1726:         $r->log_error("Error creating excel spreadsheet $filename: $!");
                   1727:         $r->print('<p>'.&mt("Unable to create new Excel file.  ".
                   1728:                             "This error has been logged.  ".
                   1729:                             "Please alert your LON-CAPA administrator").
                   1730:                   '</p>');
                   1731:         return (undef);
                   1732:     }
                   1733:     #
                   1734:     $workbook->set_tempdir('/home/httpd/perl/tmp');
                   1735:     #
                   1736:     my $format = &Apache::loncommon::define_excel_formats($workbook);
                   1737:     return ($workbook,$filename,$format);
                   1738: }
                   1739: 
                   1740: ###############################################################
                   1741: ###############################################################
                   1742: 
                   1743: =pod
                   1744: 
1.648     raeburn  1745: =item * &create_text_file()
1.113     bowersj2 1746: 
1.542     raeburn  1747: Create a file to write to and eventually make available to the user.
1.256     matthew  1748: If file creation fails, outputs an error message on the request object and 
                   1749: return undefs.
1.113     bowersj2 1750: 
1.256     matthew  1751: Inputs: Apache request object, and file suffix
1.113     bowersj2 1752: 
1.256     matthew  1753: Returns (undef) on failure, 
                   1754:     Filehandle and filename on success.
1.113     bowersj2 1755: 
                   1756: =cut
                   1757: 
1.256     matthew  1758: ###############################################################
                   1759: ###############################################################
                   1760: sub create_text_file {
                   1761:     my ($r,$suffix) = @_;
                   1762:     if (! defined($suffix)) { $suffix = 'txt'; };
                   1763:     my $fh;
                   1764:     my $filename = '/prtspool/'.
1.258     albertel 1765:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
1.256     matthew  1766:         time.'_'.rand(1000000000).'.'.$suffix;
                   1767:     $fh = Apache::File->new('>/home/httpd'.$filename);
                   1768:     if (! defined($fh)) {
                   1769:         $r->log_error("Couldn't open $filename for output $!");
1.683     bisitz   1770:         $r->print(&mt('Problems occurred in creating the output file. '
                   1771:                      .'This error has been logged. '
                   1772:                      .'Please alert your LON-CAPA administrator.'));
1.113     bowersj2 1773:     }
1.256     matthew  1774:     return ($fh,$filename)
1.113     bowersj2 1775: }
                   1776: 
                   1777: 
1.256     matthew  1778: =pod 
1.113     bowersj2 1779: 
                   1780: =back
                   1781: 
                   1782: =cut
1.37      matthew  1783: 
                   1784: ###############################################################
1.33      matthew  1785: ##        Home server <option> list generating code          ##
                   1786: ###############################################################
1.35      matthew  1787: 
1.169     www      1788: # ------------------------------------------
                   1789: 
                   1790: sub domain_select {
                   1791:     my ($name,$value,$multiple)=@_;
                   1792:     my %domains=map { 
1.514     albertel 1793: 	$_ => $_.' '. &Apache::lonnet::domain($_,'description') 
1.512     albertel 1794:     } &Apache::lonnet::all_domains();
1.169     www      1795:     if ($multiple) {
                   1796: 	$domains{''}=&mt('Any domain');
1.550     albertel 1797: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.287     albertel 1798: 	return &multiple_select_form($name,$value,4,\%domains);
1.169     www      1799:     } else {
1.550     albertel 1800: 	$domains{'select_form_order'} = [sort {lc($a) cmp lc($b) } (keys(%domains))];
1.169     www      1801: 	return &select_form($name,$value,%domains);
                   1802:     }
                   1803: }
                   1804: 
1.282     albertel 1805: #-------------------------------------------
                   1806: 
                   1807: =pod
                   1808: 
1.519     raeburn  1809: =head1 Routines for form select boxes
                   1810: 
                   1811: =over 4
                   1812: 
1.648     raeburn  1813: =item * &multiple_select_form($name,$value,$size,$hash,$order)
1.282     albertel 1814: 
                   1815: Returns a string containing a <select> element int multiple mode
                   1816: 
                   1817: 
                   1818: Args:
                   1819:   $name - name of the <select> element
1.506     raeburn  1820:   $value - scalar or array ref of values that should already be selected
1.282     albertel 1821:   $size - number of rows long the select element is
1.283     albertel 1822:   $hash - the elements should be 'option' => 'shown text'
1.282     albertel 1823:           (shown text should already have been &mt())
1.506     raeburn  1824:   $order - (optional) array ref of the order to show the elements in
1.283     albertel 1825: 
1.282     albertel 1826: =cut
                   1827: 
                   1828: #-------------------------------------------
1.169     www      1829: sub multiple_select_form {
1.284     albertel 1830:     my ($name,$value,$size,$hash,$order)=@_;
1.169     www      1831:     my %selected = map { $_ => 1 } ref($value)?@{$value}:($value);
                   1832:     my $output='';
1.191     matthew  1833:     if (! defined($size)) {
                   1834:         $size = 4;
1.283     albertel 1835:         if (scalar(keys(%$hash))<4) {
                   1836:             $size = scalar(keys(%$hash));
1.191     matthew  1837:         }
                   1838:     }
1.692.4.2  raeburn  1839:     $output.="\n".'<select name="'.$name.'" size="'.$size.'" multiple="multiple">';
1.501     banghart 1840:     my @order;
1.506     raeburn  1841:     if (ref($order) eq 'ARRAY')  {
                   1842:         @order = @{$order};
                   1843:     } else {
                   1844:         @order = sort(keys(%$hash));
1.501     banghart 1845:     }
                   1846:     if (exists($$hash{'select_form_order'})) {
                   1847:         @order = @{$$hash{'select_form_order'}};
                   1848:     }
                   1849:         
1.284     albertel 1850:     foreach my $key (@order) {
1.356     albertel 1851:         $output.='<option value="'.&HTML::Entities::encode($key,'"<>&').'" ';
1.284     albertel 1852:         $output.='selected="selected" ' if ($selected{$key});
                   1853:         $output.='>'.$hash->{$key}."</option>\n";
1.169     www      1854:     }
                   1855:     $output.="</select>\n";
                   1856:     return $output;
                   1857: }
                   1858: 
1.88      www      1859: #-------------------------------------------
                   1860: 
                   1861: =pod
                   1862: 
1.648     raeburn  1863: =item * &select_form($defdom,$name,%hash)
1.88      www      1864: 
                   1865: Returns a string containing a <select name='$name' size='1'> form to 
                   1866: allow a user to select options from a hash option_name => displayed text.  
                   1867: See lonrights.pm for an example invocation and use.
                   1868: 
                   1869: =cut
                   1870: 
                   1871: #-------------------------------------------
                   1872: sub select_form {
                   1873:     my ($def,$name,%hash) = @_;
                   1874:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
1.128     albertel 1875:     my @keys;
                   1876:     if (exists($hash{'select_form_order'})) {
                   1877: 	@keys=@{$hash{'select_form_order'}};
                   1878:     } else {
                   1879: 	@keys=sort(keys(%hash));
                   1880:     }
1.356     albertel 1881:     foreach my $key (@keys) {
                   1882:         $selectform.=
                   1883: 	    '<option value="'.&HTML::Entities::encode($key,'"<>&').'" '.
                   1884:             ($key eq $def ? 'selected="selected" ' : '').
1.692.4.27  raeburn  1885:                 ">".$hash{$key}."</option>\n";
1.88      www      1886:     }
                   1887:     $selectform.="</select>";
                   1888:     return $selectform;
                   1889: }
                   1890: 
1.475     www      1891: # For display filters
                   1892: 
                   1893: sub display_filter {
                   1894:     if (!$env{'form.show'}) { $env{'form.show'}=10; }
1.477     www      1895:     if (!$env{'form.displayfilter'}) { $env{'form.displayfilter'}='currentfolder'; }
1.692.4.2  raeburn  1896:     return '<span class="LC_nobreak"><label>'.&mt('Records [_1]',
1.475     www      1897: 			       &Apache::lonmeta::selectbox('show',$env{'form.show'},undef,
                   1898: 							   (&mt('all'),10,20,50,100,1000,10000))).
1.692.4.2  raeburn  1899: 	   '</label></span> <span class="LC_nobreak">'.
1.475     www      1900:            &mt('Filter [_1]',
1.477     www      1901: 	   &select_form($env{'form.displayfilter'},
                   1902: 			'displayfilter',
                   1903: 			('currentfolder' => 'Current folder/page',
                   1904: 			 'containing' => 'Containing phrase',
                   1905: 			 'none' => 'None'))).
1.692.4.2  raeburn  1906: 			 '<input type="text" name="containingphrase" size="30" value="'.&HTML::Entities::encode($env{'form.containingphrase'}).'" /></span>';
1.475     www      1907: }
                   1908: 
1.167     www      1909: sub gradeleveldescription {
                   1910:     my $gradelevel=shift;
                   1911:     my %gradelevels=(0 => 'Not specified',
                   1912: 		     1 => 'Grade 1',
                   1913: 		     2 => 'Grade 2',
                   1914: 		     3 => 'Grade 3',
                   1915: 		     4 => 'Grade 4',
                   1916: 		     5 => 'Grade 5',
                   1917: 		     6 => 'Grade 6',
                   1918: 		     7 => 'Grade 7',
                   1919: 		     8 => 'Grade 8',
                   1920: 		     9 => 'Grade 9',
                   1921: 		     10 => 'Grade 10',
                   1922: 		     11 => 'Grade 11',
                   1923: 		     12 => 'Grade 12',
                   1924: 		     13 => 'Grade 13',
                   1925: 		     14 => '100 Level',
                   1926: 		     15 => '200 Level',
                   1927: 		     16 => '300 Level',
                   1928: 		     17 => '400 Level',
                   1929: 		     18 => 'Graduate Level');
                   1930:     return &mt($gradelevels{$gradelevel});
                   1931: }
                   1932: 
1.163     www      1933: sub select_level_form {
                   1934:     my ($deflevel,$name)=@_;
                   1935:     unless ($deflevel) { $deflevel=0; }
1.167     www      1936:     my $selectform = "<select name=\"$name\" size=\"1\">\n";
                   1937:     for (my $i=0; $i<=18; $i++) {
                   1938:         $selectform.="<option value=\"$i\" ".
1.253     albertel 1939:             ($i==$deflevel ? 'selected="selected" ' : '').
1.167     www      1940:                 ">".&gradeleveldescription($i)."</option>\n";
                   1941:     }
                   1942:     $selectform.="</select>";
                   1943:     return $selectform;
1.163     www      1944: }
1.167     www      1945: 
1.35      matthew  1946: #-------------------------------------------
                   1947: 
1.45      matthew  1948: =pod
                   1949: 
1.692.4.23  raeburn  1950: =item * &select_dom_form($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms)
1.35      matthew  1951: 
                   1952: Returns a string containing a <select name='$name' size='1'> form to 
                   1953: allow a user to select the domain to preform an operation in.  
                   1954: See loncreateuser.pm for an example invocation and use.
                   1955: 
1.90      www      1956: If the $includeempty flag is set, it also includes an empty choice ("no domain
                   1957: selected");
                   1958: 
1.692.4.2  raeburn  1959: If the $showdomdesc flag is set, the domain name is followed by the domain description.
                   1960: 
1.692.4.7  raeburn  1961: The optional $onchange argument specifies what should occur if the domain selector is changed, e.g., 'this.form.submit()' if the form is to be automatically submitted.
1.563     raeburn  1962: 
1.692.4.23  raeburn  1963: The optional $incdoms is a reference to an array of domains which will be the only available options.
                   1964: 
1.35      matthew  1965: =cut
                   1966: 
                   1967: #-------------------------------------------
1.34      matthew  1968: sub select_dom_form {
1.692.4.23  raeburn  1969:     my ($defdom,$name,$includeempty,$showdomdesc,$onchange,$incdoms) = @_;
1.692.4.7  raeburn  1970:     if ($onchange) {
                   1971:         $onchange = ' onchange="'.$onchange.'"';
1.692.4.2  raeburn  1972:     }
1.692.4.23  raeburn  1973:     my @domains;
                   1974:     if (ref($incdoms) eq 'ARRAY') {
                   1975:         @domains = sort {lc($a) cmp lc($b)} (@{$incdoms});
                   1976:     } else {
                   1977:         @domains = sort {lc($a) cmp lc($b)} (&Apache::lonnet::all_domains());
                   1978:     }
1.90      www      1979:     if ($includeempty) { @domains=('',@domains); }
1.692.4.2  raeburn  1980:     my $selectdomain = "<select name=\"$name\" size=\"1\"$onchange>\n";
1.356     albertel 1981:     foreach my $dom (@domains) {
                   1982:         $selectdomain.="<option value=\"$dom\" ".
1.563     raeburn  1983:             ($dom eq $defdom ? 'selected="selected" ' : '').'>'.$dom;
                   1984:         if ($showdomdesc) {
                   1985:             if ($dom ne '') {
                   1986:                 my $domdesc = &Apache::lonnet::domain($dom,'description');
                   1987:                 if ($domdesc ne '') {
                   1988:                     $selectdomain .= ' ('.$domdesc.')';
                   1989:                 }
                   1990:             } 
                   1991:         }
                   1992:         $selectdomain .= "</option>\n";
1.34      matthew  1993:     }
                   1994:     $selectdomain.="</select>";
                   1995:     return $selectdomain;
                   1996: }
                   1997: 
1.35      matthew  1998: #-------------------------------------------
                   1999: 
1.45      matthew  2000: =pod
                   2001: 
1.648     raeburn  2002: =item * &home_server_form_item($domain,$name,$defaultflag)
1.35      matthew  2003: 
1.586     raeburn  2004: input: 4 arguments (two required, two optional) - 
                   2005:     $domain - domain of new user
                   2006:     $name - name of form element
                   2007:     $default - Value of 'default' causes a default item to be first 
                   2008:                             option, and selected by default. 
                   2009:     $hide - Value of 'hide' causes hiding of the name of the server, 
                   2010:                             if 1 server found, or default, if 0 found.
1.594     raeburn  2011: output: returns 2 items: 
1.586     raeburn  2012: (a) form element which contains either:
                   2013:    (i) <select name="$name">
                   2014:         <option value="$hostid1">$hostid $servers{$hostid}</option>
                   2015:         <option value="$hostid2">$hostid $servers{$hostid}</option>       
                   2016:        </select>
                   2017:        form item if there are multiple library servers in $domain, or
                   2018:    (ii) an <input type="hidden" name="$name" value="$hostid" /> form item 
                   2019:        if there is only one library server in $domain.
                   2020: 
                   2021: (b) number of library servers found.
                   2022: 
                   2023: See loncreateuser.pm for example of use.
1.35      matthew  2024: 
                   2025: =cut
                   2026: 
                   2027: #-------------------------------------------
1.586     raeburn  2028: sub home_server_form_item {
                   2029:     my ($domain,$name,$default,$hide) = @_;
1.513     albertel 2030:     my %servers = &Apache::lonnet::get_servers($domain,'library');
1.586     raeburn  2031:     my $result;
                   2032:     my $numlib = keys(%servers);
                   2033:     if ($numlib > 1) {
                   2034:         $result .= '<select name="'.$name.'" />'."\n";
                   2035:         if ($default) {
1.692.4.2  raeburn  2036:             $result .= '<option value="default" selected="selected">'.&mt('default').
1.586     raeburn  2037:                        '</option>'."\n";
                   2038:         }
                   2039:         foreach my $hostid (sort(keys(%servers))) {
                   2040:             $result.= '<option value="'.$hostid.'">'.
                   2041: 	              $hostid.' '.$servers{$hostid}."</option>\n";
                   2042:         }
                   2043:         $result .= '</select>'."\n";
                   2044:     } elsif ($numlib == 1) {
                   2045:         my $hostid;
                   2046:         foreach my $item (keys(%servers)) {
                   2047:             $hostid = $item;
                   2048:         }
                   2049:         $result .= '<input type="hidden" name="'.$name.'" value="'.
                   2050:                    $hostid.'" />';
                   2051:                    if (!$hide) {
                   2052:                        $result .= $hostid.' '.$servers{$hostid};
                   2053:                    }
                   2054:                    $result .= "\n";
                   2055:     } elsif ($default) {
                   2056:         $result .= '<input type="hidden" name="'.$name.
                   2057:                    '" value="default" />';
                   2058:                    if (!$hide) {
                   2059:                        $result .= &mt('default');
                   2060:                    }
                   2061:                    $result .= "\n";
1.33      matthew  2062:     }
1.586     raeburn  2063:     return ($result,$numlib);
1.33      matthew  2064: }
1.112     bowersj2 2065: 
                   2066: =pod
                   2067: 
1.534     albertel 2068: =back 
                   2069: 
1.112     bowersj2 2070: =cut
1.87      matthew  2071: 
                   2072: ###############################################################
1.112     bowersj2 2073: ##                  Decoding User Agent                      ##
1.87      matthew  2074: ###############################################################
                   2075: 
                   2076: =pod
                   2077: 
1.112     bowersj2 2078: =head1 Decoding the User Agent
                   2079: 
                   2080: =over 4
                   2081: 
                   2082: =item * &decode_user_agent()
1.87      matthew  2083: 
                   2084: Inputs: $r
                   2085: 
                   2086: Outputs:
                   2087: 
                   2088: =over 4
                   2089: 
1.112     bowersj2 2090: =item * $httpbrowser
1.87      matthew  2091: 
1.112     bowersj2 2092: =item * $clientbrowser
1.87      matthew  2093: 
1.112     bowersj2 2094: =item * $clientversion
1.87      matthew  2095: 
1.112     bowersj2 2096: =item * $clientmathml
1.87      matthew  2097: 
1.112     bowersj2 2098: =item * $clientunicode
1.87      matthew  2099: 
1.112     bowersj2 2100: =item * $clientos
1.87      matthew  2101: 
                   2102: =back
                   2103: 
1.157     matthew  2104: =back 
                   2105: 
1.87      matthew  2106: =cut
                   2107: 
                   2108: ###############################################################
                   2109: ###############################################################
                   2110: sub decode_user_agent {
1.247     albertel 2111:     my ($r)=@_;
1.87      matthew  2112:     my @browsertype=split(/\&/,$Apache::lonnet::perlvar{"lonBrowsDet"});
                   2113:     my %mathcap=split(/\&/,$$Apache::lonnet::perlvar{"lonMathML"});
                   2114:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
1.247     albertel 2115:     if (!$httpbrowser && $r) { $httpbrowser=$r->header_in('User-Agent'); }
1.87      matthew  2116:     my $clientbrowser='unknown';
                   2117:     my $clientversion='0';
                   2118:     my $clientmathml='';
                   2119:     my $clientunicode='0';
                   2120:     for (my $i=0;$i<=$#browsertype;$i++) {
                   2121:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
                   2122: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                   2123: 	    $clientbrowser=$bname;
                   2124:             $httpbrowser=~/$vreg/i;
                   2125: 	    $clientversion=$1;
                   2126:             $clientmathml=($clientversion>=$minv);
                   2127:             $clientunicode=($clientversion>=$univ);
                   2128: 	}
                   2129:     }
                   2130:     my $clientos='unknown';
                   2131:     if (($httpbrowser=~/linux/i) ||
                   2132:         ($httpbrowser=~/unix/i) ||
                   2133:         ($httpbrowser=~/ux/i) ||
                   2134:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                   2135:     if (($httpbrowser=~/vax/i) ||
                   2136:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                   2137:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                   2138:     if (($httpbrowser=~/mac/i) ||
                   2139:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
                   2140:     if ($httpbrowser=~/win/i) { $clientos='win'; }
                   2141:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
                   2142:     return ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   2143:             $clientunicode,$clientos,);
                   2144: }
                   2145: 
1.32      matthew  2146: ###############################################################
                   2147: ##    Authentication changing form generation subroutines    ##
                   2148: ###############################################################
                   2149: ##
                   2150: ## All of the authform_xxxxxxx subroutines take their inputs in a
                   2151: ## hash, and have reasonable default values.
                   2152: ##
                   2153: ##    formname = the name given in the <form> tag.
1.35      matthew  2154: #-------------------------------------------
                   2155: 
1.45      matthew  2156: =pod
                   2157: 
1.112     bowersj2 2158: =head1 Authentication Routines
                   2159: 
                   2160: =over 4
                   2161: 
1.648     raeburn  2162: =item * &authform_xxxxxx()
1.35      matthew  2163: 
                   2164: The authform_xxxxxx subroutines provide javascript and html forms which 
                   2165: handle some of the conveniences required for authentication forms.  
                   2166: This is not an optimal method, but it works.  
                   2167: 
                   2168: =over 4
                   2169: 
1.112     bowersj2 2170: =item * authform_header
1.35      matthew  2171: 
1.112     bowersj2 2172: =item * authform_authorwarning
1.35      matthew  2173: 
1.112     bowersj2 2174: =item * authform_nochange
1.35      matthew  2175: 
1.112     bowersj2 2176: =item * authform_kerberos
1.35      matthew  2177: 
1.112     bowersj2 2178: =item * authform_internal
1.35      matthew  2179: 
1.112     bowersj2 2180: =item * authform_filesystem
1.35      matthew  2181: 
                   2182: =back
                   2183: 
1.648     raeburn  2184: See loncreateuser.pm for invocation and use examples.
1.157     matthew  2185: 
1.35      matthew  2186: =cut
                   2187: 
                   2188: #-------------------------------------------
1.32      matthew  2189: sub authform_header{  
                   2190:     my %in = (
                   2191:         formname => 'cu',
1.80      albertel 2192:         kerb_def_dom => '',
1.32      matthew  2193:         @_,
                   2194:     );
                   2195:     $in{'formname'} = 'document.' . $in{'formname'};
                   2196:     my $result='';
1.80      albertel 2197: 
                   2198: #---------------------------------------------- Code for upper case translation
                   2199:     my $Javascript_toUpperCase;
                   2200:     unless ($in{kerb_def_dom}) {
                   2201:         $Javascript_toUpperCase =<<"END";
                   2202:         switch (choice) {
                   2203:            case 'krb': currentform.elements[choicearg].value =
                   2204:                currentform.elements[choicearg].value.toUpperCase();
                   2205:                break;
                   2206:            default:
                   2207:         }
                   2208: END
                   2209:     } else {
                   2210:         $Javascript_toUpperCase = "";
                   2211:     }
                   2212: 
1.165     raeburn  2213:     my $radioval = "'nochange'";
1.591     raeburn  2214:     if (defined($in{'curr_authtype'})) {
                   2215:         if ($in{'curr_authtype'} ne '') {
                   2216:             $radioval = "'".$in{'curr_authtype'}."arg'";
                   2217:         }
1.174     matthew  2218:     }
1.165     raeburn  2219:     my $argfield = 'null';
1.591     raeburn  2220:     if (defined($in{'mode'})) {
1.165     raeburn  2221:         if ($in{'mode'} eq 'modifycourse')  {
1.591     raeburn  2222:             if (defined($in{'curr_autharg'})) {
                   2223:                 if ($in{'curr_autharg'} ne '') {
1.165     raeburn  2224:                     $argfield = "'$in{'curr_autharg'}'";
                   2225:                 }
                   2226:             }
                   2227:         }
                   2228:     }
                   2229: 
1.32      matthew  2230:     $result.=<<"END";
                   2231: var current = new Object();
1.165     raeburn  2232: current.radiovalue = $radioval;
                   2233: current.argfield = $argfield;
1.32      matthew  2234: 
                   2235: function changed_radio(choice,currentform) {
                   2236:     var choicearg = choice + 'arg';
                   2237:     // If a radio button in changed, we need to change the argfield
                   2238:     if (current.radiovalue != choice) {
                   2239:         current.radiovalue = choice;
                   2240:         if (current.argfield != null) {
                   2241:             currentform.elements[current.argfield].value = '';
                   2242:         }
                   2243:         if (choice == 'nochange') {
                   2244:             current.argfield = null;
                   2245:         } else {
                   2246:             current.argfield = choicearg;
                   2247:             switch(choice) {
                   2248:                 case 'krb': 
                   2249:                     currentform.elements[current.argfield].value = 
                   2250:                         "$in{'kerb_def_dom'}";
                   2251:                 break;
                   2252:               default:
                   2253:                 break;
                   2254:             }
                   2255:         }
                   2256:     }
                   2257:     return;
                   2258: }
1.22      www      2259: 
1.32      matthew  2260: function changed_text(choice,currentform) {
                   2261:     var choicearg = choice + 'arg';
                   2262:     if (currentform.elements[choicearg].value !='') {
1.80      albertel 2263:         $Javascript_toUpperCase
1.32      matthew  2264:         // clear old field
                   2265:         if ((current.argfield != choicearg) && (current.argfield != null)) {
                   2266:             currentform.elements[current.argfield].value = '';
                   2267:         }
                   2268:         current.argfield = choicearg;
                   2269:     }
                   2270:     set_auth_radio_buttons(choice,currentform);
                   2271:     return;
1.20      www      2272: }
1.32      matthew  2273: 
                   2274: function set_auth_radio_buttons(newvalue,currentform) {
                   2275:     var i=0;
                   2276:     while (i < currentform.login.length) {
                   2277:         if (currentform.login[i].value == newvalue) { break; }
                   2278:         i++;
                   2279:     }
                   2280:     if (i == currentform.login.length) {
                   2281:         return;
                   2282:     }
                   2283:     current.radiovalue = newvalue;
                   2284:     currentform.login[i].checked = true;
                   2285:     return;
                   2286: }
                   2287: END
                   2288:     return $result;
                   2289: }
                   2290: 
                   2291: sub authform_authorwarning{
                   2292:     my $result='';
1.144     matthew  2293:     $result='<i>'.
                   2294:         &mt('As a general rule, only authors or co-authors should be '.
                   2295:             'filesystem authenticated '.
                   2296:             '(which allows access to the server filesystem).')."</i>\n";
1.32      matthew  2297:     return $result;
                   2298: }
                   2299: 
                   2300: sub authform_nochange{  
                   2301:     my %in = (
                   2302:               formname => 'document.cu',
                   2303:               kerb_def_dom => 'MSU.EDU',
                   2304:               @_,
                   2305:           );
1.586     raeburn  2306:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'}); 
                   2307:     my $result;
                   2308:     if (keys(%can_assign) == 0) {
                   2309:         $result = &mt('Under you current role you are not permitted to change login settings for this user');  
                   2310:     } else {
                   2311:         $result = '<label>'.&mt('[_1] Do not change login data',
                   2312:                   '<input type="radio" name="login" value="nochange" '.
                   2313:                   'checked="checked" onclick="'.
1.281     albertel 2314:             "javascript:changed_radio('nochange',$in{'formname'});".'" />').
                   2315: 	    '</label>';
1.586     raeburn  2316:     }
1.32      matthew  2317:     return $result;
                   2318: }
                   2319: 
1.591     raeburn  2320: sub authform_kerberos {
1.32      matthew  2321:     my %in = (
                   2322:               formname => 'document.cu',
                   2323:               kerb_def_dom => 'MSU.EDU',
1.80      albertel 2324:               kerb_def_auth => 'krb4',
1.32      matthew  2325:               @_,
                   2326:               );
1.586     raeburn  2327:     my ($check4,$check5,$krbcheck,$krbarg,$krbver,$result,$authtype,
                   2328:         $autharg,$jscall);
                   2329:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.80      albertel 2330:     if ($in{'kerb_def_auth'} eq 'krb5') {
1.692.4.2  raeburn  2331:        $check5 = ' checked="checked"';
1.80      albertel 2332:     } else {
1.692.4.2  raeburn  2333:        $check4 = ' checked="checked"';
1.80      albertel 2334:     }
1.165     raeburn  2335:     $krbarg = $in{'kerb_def_dom'};
1.591     raeburn  2336:     if (defined($in{'curr_authtype'})) {
                   2337:         if ($in{'curr_authtype'} eq 'krb') {
1.692.4.2  raeburn  2338:             $krbcheck = ' checked="checked"';
1.623     raeburn  2339:             if (defined($in{'mode'})) {
                   2340:                 if ($in{'mode'} eq 'modifyuser') {
                   2341:                     $krbcheck = '';
                   2342:                 }
                   2343:             }
1.591     raeburn  2344:             if (defined($in{'curr_kerb_ver'})) {
                   2345:                 if ($in{'curr_krb_ver'} eq '5') {
1.692.4.2  raeburn  2346:                     $check5 = ' checked="checked"';
1.591     raeburn  2347:                     $check4 = '';
                   2348:                 } else {
1.692.4.2  raeburn  2349:                     $check4 = ' checked="checked"';
1.591     raeburn  2350:                     $check5 = '';
                   2351:                 }
1.586     raeburn  2352:             }
1.591     raeburn  2353:             if (defined($in{'curr_autharg'})) {
1.165     raeburn  2354:                 $krbarg = $in{'curr_autharg'};
                   2355:             }
1.586     raeburn  2356:             if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
1.591     raeburn  2357:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2358:                     $result = 
                   2359:     &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2360:         $in{'curr_autharg'},$krbver);
                   2361:                 } else {
                   2362:                     $result =
                   2363:     &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2364:                 }
                   2365:                 return $result; 
                   2366:             }
                   2367:         }
                   2368:     } else {
                   2369:         if ($authnum == 1) {
1.692.4.2  raeburn  2370:             $authtype = '<input type="hidden" name="login" value="krb" />';
1.165     raeburn  2371:         }
                   2372:     }
1.586     raeburn  2373:     if (!$can_assign{'krb4'} && !$can_assign{'krb5'}) {
                   2374:         return;
1.587     raeburn  2375:     } elsif ($authtype eq '') {
1.591     raeburn  2376:         if (defined($in{'mode'})) {
1.587     raeburn  2377:             if ($in{'mode'} eq 'modifycourse') {
                   2378:                 if ($authnum == 1) {
1.692.4.2  raeburn  2379:                     $authtype = '<input type="hidden" name="login" value="krb" />';
1.587     raeburn  2380:                 }
                   2381:             }
                   2382:         }
1.586     raeburn  2383:     }
                   2384:     $jscall = "javascript:changed_radio('krb',$in{'formname'});";
                   2385:     if ($authtype eq '') {
                   2386:         $authtype = '<input type="radio" name="login" value="krb" '.
                   2387:                     'onclick="'.$jscall.'" onchange="'.$jscall.'"'.
                   2388:                     $krbcheck.' />';
                   2389:     }
                   2390:     if (($can_assign{'krb4'} && $can_assign{'krb5'}) ||
                   2391:         ($can_assign{'krb4'} && !$can_assign{'krb5'} && 
                   2392:          $in{'curr_authtype'} eq 'krb5') ||
                   2393:         (!$can_assign{'krb4'} && $can_assign{'krb5'} && 
                   2394:          $in{'curr_authtype'} eq 'krb4')) {
                   2395:         $result .= &mt
1.144     matthew  2396:         ('[_1] Kerberos authenticated with domain [_2] '.
1.281     albertel 2397:          '[_3] Version 4 [_4] Version 5 [_5]',
1.586     raeburn  2398:          '<label>'.$authtype,
1.281     albertel 2399:          '</label><input type="text" size="10" name="krbarg" '.
1.165     raeburn  2400:              'value="'.$krbarg.'" '.
1.144     matthew  2401:              'onchange="'.$jscall.'" />',
1.281     albertel 2402:          '<label><input type="radio" name="krbver" value="4" '.$check4.' />',
                   2403:          '</label><label><input type="radio" name="krbver" value="5" '.$check5.' />',
                   2404: 	 '</label>');
1.586     raeburn  2405:     } elsif ($can_assign{'krb4'}) {
                   2406:         $result .= &mt
                   2407:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2408:          '[_3] Version 4 [_4]',
                   2409:          '<label>'.$authtype,
                   2410:          '</label><input type="text" size="10" name="krbarg" '.
                   2411:              'value="'.$krbarg.'" '.
                   2412:              'onchange="'.$jscall.'" />',
                   2413:          '<label><input type="hidden" name="krbver" value="4" />',
                   2414:          '</label>');
                   2415:     } elsif ($can_assign{'krb5'}) {
                   2416:         $result .= &mt
                   2417:         ('[_1] Kerberos authenticated with domain [_2] '.
                   2418:          '[_3] Version 5 [_4]',
                   2419:          '<label>'.$authtype,
                   2420:          '</label><input type="text" size="10" name="krbarg" '.
                   2421:              'value="'.$krbarg.'" '.
                   2422:              'onchange="'.$jscall.'" />',
                   2423:          '<label><input type="hidden" name="krbver" value="5" />',
                   2424:          '</label>');
                   2425:     }
1.32      matthew  2426:     return $result;
                   2427: }
                   2428: 
                   2429: sub authform_internal{  
1.586     raeburn  2430:     my %in = (
1.32      matthew  2431:                 formname => 'document.cu',
                   2432:                 kerb_def_dom => 'MSU.EDU',
                   2433:                 @_,
                   2434:                 );
1.586     raeburn  2435:     my ($intcheck,$intarg,$result,$authtype,$autharg,$jscall);
                   2436:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2437:     if (defined($in{'curr_authtype'})) {
                   2438:         if ($in{'curr_authtype'} eq 'int') {
1.586     raeburn  2439:             if ($can_assign{'int'}) {
1.692.4.2  raeburn  2440:                 $intcheck = 'checked="checked" ';
1.623     raeburn  2441:                 if (defined($in{'mode'})) {
                   2442:                     if ($in{'mode'} eq 'modifyuser') {
                   2443:                         $intcheck = '';
                   2444:                     }
                   2445:                 }
1.591     raeburn  2446:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2447:                     $intarg = $in{'curr_autharg'};
                   2448:                 }
                   2449:             } else {
                   2450:                 $result = &mt('Currently internally authenticated.');
                   2451:                 return $result;
1.165     raeburn  2452:             }
                   2453:         }
1.586     raeburn  2454:     } else {
                   2455:         if ($authnum == 1) {
1.692.4.2  raeburn  2456:             $authtype = '<input type="hidden" name="login" value="int" />';
1.586     raeburn  2457:         }
                   2458:     }
                   2459:     if (!$can_assign{'int'}) {
                   2460:         return;
1.587     raeburn  2461:     } elsif ($authtype eq '') {
1.591     raeburn  2462:         if (defined($in{'mode'})) {
1.587     raeburn  2463:             if ($in{'mode'} eq 'modifycourse') {
                   2464:                 if ($authnum == 1) {
1.692.4.2  raeburn  2465:                     $authtype = '<input type="hidden" name="login" value="int" />';
1.587     raeburn  2466:                 }
                   2467:             }
                   2468:         }
1.165     raeburn  2469:     }
1.586     raeburn  2470:     $jscall = "javascript:changed_radio('int',$in{'formname'});";
                   2471:     if ($authtype eq '') {
                   2472:         $authtype = '<input type="radio" name="login" value="int" '.$intcheck.
                   2473:                     ' onchange="'.$jscall.'" onclick="'.$jscall.'" />';
                   2474:     }
1.605     bisitz   2475:     $autharg = '<input type="password" size="10" name="intarg" value="'.
1.586     raeburn  2476:                $intarg.'" onchange="'.$jscall.'" />';
                   2477:     $result = &mt
1.144     matthew  2478:         ('[_1] Internally authenticated (with initial password [_2])',
1.586     raeburn  2479:          '<label>'.$authtype,'</label>'.$autharg);
1.692.4.4  raeburn  2480:     $result.="<label><input type=\"checkbox\" name=\"visible\" onclick='if (this.checked) { this.form.intarg.type=\"text\" } else { this.form.intarg.type=\"password\" }' />".&mt('Visible input').'</label>';
1.32      matthew  2481:     return $result;
                   2482: }
                   2483: 
                   2484: sub authform_local{  
                   2485:     my %in = (
                   2486:               formname => 'document.cu',
                   2487:               kerb_def_dom => 'MSU.EDU',
                   2488:               @_,
                   2489:               );
1.586     raeburn  2490:     my ($loccheck,$locarg,$result,$authtype,$autharg,$jscall);
                   2491:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2492:     if (defined($in{'curr_authtype'})) {
                   2493:         if ($in{'curr_authtype'} eq 'loc') {
1.586     raeburn  2494:             if ($can_assign{'loc'}) {
1.692.4.2  raeburn  2495:                 $loccheck = 'checked="checked" ';
1.623     raeburn  2496:                 if (defined($in{'mode'})) {
                   2497:                     if ($in{'mode'} eq 'modifyuser') {
                   2498:                         $loccheck = '';
                   2499:                     }
                   2500:                 }
1.591     raeburn  2501:                 if (defined($in{'curr_autharg'})) {
1.586     raeburn  2502:                     $locarg = $in{'curr_autharg'};
                   2503:                 }
                   2504:             } else {
                   2505:                 $result = &mt('Currently using local (institutional) authentication.');
                   2506:                 return $result;
1.165     raeburn  2507:             }
                   2508:         }
1.586     raeburn  2509:     } else {
                   2510:         if ($authnum == 1) {
1.692.4.2  raeburn  2511:             $authtype = '<input type="hidden" name="login" value="loc" />';
1.586     raeburn  2512:         }
                   2513:     }
                   2514:     if (!$can_assign{'loc'}) {
                   2515:         return;
1.587     raeburn  2516:     } elsif ($authtype eq '') {
1.591     raeburn  2517:         if (defined($in{'mode'})) {
1.587     raeburn  2518:             if ($in{'mode'} eq 'modifycourse') {
                   2519:                 if ($authnum == 1) {
1.692.4.2  raeburn  2520:                     $authtype = '<input type="hidden" name="login" value="loc" />';
1.587     raeburn  2521:                 }
                   2522:             }
                   2523:         }
1.165     raeburn  2524:     }
1.586     raeburn  2525:     $jscall = "javascript:changed_radio('loc',$in{'formname'});";
                   2526:     if ($authtype eq '') {
                   2527:         $authtype = '<input type="radio" name="login" value="loc" '.
                   2528:                     $loccheck.' onchange="'.$jscall.'" onclick="'.
                   2529:                     $jscall.'" />';
                   2530:     }
                   2531:     $autharg = '<input type="text" size="10" name="locarg" value="'.
                   2532:                $locarg.'" onchange="'.$jscall.'" />';
                   2533:     $result = &mt('[_1] Local Authentication with argument [_2]',
                   2534:                   '<label>'.$authtype,'</label>'.$autharg);
1.32      matthew  2535:     return $result;
                   2536: }
                   2537: 
                   2538: sub authform_filesystem{  
                   2539:     my %in = (
                   2540:               formname => 'document.cu',
                   2541:               kerb_def_dom => 'MSU.EDU',
                   2542:               @_,
                   2543:               );
1.586     raeburn  2544:     my ($fsyscheck,$result,$authtype,$autharg,$jscall);
                   2545:     my ($authnum,%can_assign) =  &get_assignable_auth($in{'domain'});
1.591     raeburn  2546:     if (defined($in{'curr_authtype'})) {
                   2547:         if ($in{'curr_authtype'} eq 'fsys') {
1.586     raeburn  2548:             if ($can_assign{'fsys'}) {
1.692.4.2  raeburn  2549:                 $fsyscheck = 'checked="checked" ';
1.623     raeburn  2550:                 if (defined($in{'mode'})) {
                   2551:                     if ($in{'mode'} eq 'modifyuser') {
                   2552:                         $fsyscheck = '';
                   2553:                     }
                   2554:                 }
1.586     raeburn  2555:             } else {
                   2556:                 $result = &mt('Currently Filesystem Authenticated.');
                   2557:                 return $result;
                   2558:             }           
                   2559:         }
                   2560:     } else {
                   2561:         if ($authnum == 1) {
1.692.4.2  raeburn  2562:             $authtype = '<input type="hidden" name="login" value="fsys" />';
1.586     raeburn  2563:         }
                   2564:     }
                   2565:     if (!$can_assign{'fsys'}) {
                   2566:         return;
1.587     raeburn  2567:     } elsif ($authtype eq '') {
1.591     raeburn  2568:         if (defined($in{'mode'})) {
1.587     raeburn  2569:             if ($in{'mode'} eq 'modifycourse') {
                   2570:                 if ($authnum == 1) {
1.692.4.2  raeburn  2571:                     $authtype = '<input type="hidden" name="login" value="fsys" />';
1.587     raeburn  2572:                 }
                   2573:             }
                   2574:         }
1.586     raeburn  2575:     }
                   2576:     $jscall = "javascript:changed_radio('fsys',$in{'formname'});";
                   2577:     if ($authtype eq '') {
                   2578:         $authtype = '<input type="radio" name="login" value="fsys" '.
                   2579:                     $fsyscheck.' onchange="'.$jscall.'" onclick="'.
                   2580:                     $jscall.'" />';
                   2581:     }
                   2582:     $autharg = '<input type="text" size="10" name="fsysarg" value=""'.
                   2583:                ' onchange="'.$jscall.'" />';
                   2584:     $result = &mt
1.144     matthew  2585:         ('[_1] Filesystem Authenticated (with initial password [_2])',
1.281     albertel 2586:          '<label><input type="radio" name="login" value="fsys" '.
1.586     raeburn  2587:          $fsyscheck.'onchange="'.$jscall.'" onclick="'.$jscall.'" />',
1.605     bisitz   2588:          '</label><input type="password" size="10" name="fsysarg" value="" '.
1.144     matthew  2589:                   'onchange="'.$jscall.'" />');
1.32      matthew  2590:     return $result;
                   2591: }
                   2592: 
1.586     raeburn  2593: sub get_assignable_auth {
                   2594:     my ($dom) = @_;
                   2595:     if ($dom eq '') {
                   2596:         $dom = $env{'request.role.domain'};
                   2597:     }
                   2598:     my %can_assign = (
                   2599:                           krb4 => 1,
                   2600:                           krb5 => 1,
                   2601:                           int  => 1,
                   2602:                           loc  => 1,
                   2603:                      );
                   2604:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2605:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   2606:         if (ref($domconfig{'usercreation'}{'authtypes'}) eq 'HASH') {
                   2607:             my $authhash = $domconfig{'usercreation'}{'authtypes'};
                   2608:             my $context;
                   2609:             if ($env{'request.role'} =~ /^au/) {
                   2610:                 $context = 'author';
                   2611:             } elsif ($env{'request.role'} =~ /^dc/) {
                   2612:                 $context = 'domain';
                   2613:             } elsif ($env{'request.course.id'}) {
                   2614:                 $context = 'course';
                   2615:             }
                   2616:             if ($context) {
                   2617:                 if (ref($authhash->{$context}) eq 'HASH') {
                   2618:                    %can_assign = %{$authhash->{$context}}; 
                   2619:                 }
                   2620:             }
                   2621:         }
                   2622:     }
                   2623:     my $authnum = 0;
                   2624:     foreach my $key (keys(%can_assign)) {
                   2625:         if ($can_assign{$key}) {
                   2626:             $authnum ++;
                   2627:         }
                   2628:     }
                   2629:     if ($can_assign{'krb4'} && $can_assign{'krb5'}) {
                   2630:         $authnum --;
                   2631:     }
                   2632:     return ($authnum,%can_assign);
                   2633: }
                   2634: 
1.80      albertel 2635: ###############################################################
                   2636: ##    Get Kerberos Defaults for Domain                 ##
                   2637: ###############################################################
                   2638: ##
                   2639: ## Returns default kerberos version and an associated argument
                   2640: ## as listed in file domain.tab. If not listed, provides
                   2641: ## appropriate default domain and kerberos version.
                   2642: ##
                   2643: #-------------------------------------------
                   2644: 
                   2645: =pod
                   2646: 
1.648     raeburn  2647: =item * &get_kerberos_defaults()
1.80      albertel 2648: 
                   2649: get_kerberos_defaults($target_domain) returns the default kerberos
1.641     raeburn  2650: version and domain. If not found, it defaults to version 4 and the 
                   2651: domain of the server.
1.80      albertel 2652: 
1.648     raeburn  2653: =over 4
                   2654: 
1.80      albertel 2655: ($def_version, $def_krb_domain) = &get_kerberos_defaults($target_domain);
                   2656: 
1.648     raeburn  2657: =back
                   2658: 
                   2659: =back
                   2660: 
1.80      albertel 2661: =cut
                   2662: 
                   2663: #-------------------------------------------
                   2664: sub get_kerberos_defaults {
                   2665:     my $domain=shift;
1.641     raeburn  2666:     my ($krbdef,$krbdefdom);
                   2667:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   2668:     if (($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) {
                   2669:         $krbdef = $domdefaults{'auth_def'};
                   2670:         $krbdefdom = $domdefaults{'auth_arg_def'};
                   2671:     } else {
1.80      albertel 2672:         $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                   2673:         my $krbdefdom=$1;
                   2674:         $krbdefdom=~tr/a-z/A-Z/;
                   2675:         $krbdef = "krb4";
                   2676:     }
                   2677:     return ($krbdef,$krbdefdom);
                   2678: }
1.112     bowersj2 2679: 
1.32      matthew  2680: 
1.46      matthew  2681: ###############################################################
                   2682: ##                Thesaurus Functions                        ##
                   2683: ###############################################################
1.20      www      2684: 
1.46      matthew  2685: =pod
1.20      www      2686: 
1.112     bowersj2 2687: =head1 Thesaurus Functions
                   2688: 
                   2689: =over 4
                   2690: 
1.648     raeburn  2691: =item * &initialize_keywords()
1.46      matthew  2692: 
                   2693: Initializes the package variable %Keywords if it is empty.  Uses the
                   2694: package variable $thesaurus_db_file.
                   2695: 
                   2696: =cut
                   2697: 
                   2698: ###################################################
                   2699: 
                   2700: sub initialize_keywords {
                   2701:     return 1 if (scalar keys(%Keywords));
                   2702:     # If we are here, %Keywords is empty, so fill it up
                   2703:     #   Make sure the file we need exists...
                   2704:     if (! -e $thesaurus_db_file) {
                   2705:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file".
                   2706:                                  " failed because it does not exist");
                   2707:         return 0;
                   2708:     }
                   2709:     #   Set up the hash as a database
                   2710:     my %thesaurus_db;
                   2711:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2712:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2713:         &Apache::lonnet::logthis("Could not tie \%thesaurus_db to ".
                   2714:                                  $thesaurus_db_file);
                   2715:         return 0;
                   2716:     } 
                   2717:     #  Get the average number of appearances of a word.
                   2718:     my $avecount = $thesaurus_db{'average.count'};
                   2719:     #  Put keywords (those that appear > average) into %Keywords
                   2720:     while (my ($word,$data)=each (%thesaurus_db)) {
                   2721:         my ($count,undef) = split /:/,$data;
                   2722:         $Keywords{$word}++ if ($count > $avecount);
                   2723:     }
                   2724:     untie %thesaurus_db;
                   2725:     # Remove special values from %Keywords.
1.356     albertel 2726:     foreach my $value ('total.count','average.count') {
                   2727:         delete($Keywords{$value}) if (exists($Keywords{$value}));
1.586     raeburn  2728:   }
1.46      matthew  2729:     return 1;
                   2730: }
                   2731: 
                   2732: ###################################################
                   2733: 
                   2734: =pod
                   2735: 
1.648     raeburn  2736: =item * &keyword($word)
1.46      matthew  2737: 
                   2738: Returns true if $word is a keyword.  A keyword is a word that appears more 
                   2739: than the average number of times in the thesaurus database.  Calls 
                   2740: &initialize_keywords
                   2741: 
                   2742: =cut
                   2743: 
                   2744: ###################################################
1.20      www      2745: 
                   2746: sub keyword {
1.46      matthew  2747:     return if (!&initialize_keywords());
                   2748:     my $word=lc(shift());
                   2749:     $word=~s/\W//g;
                   2750:     return exists($Keywords{$word});
1.20      www      2751: }
1.46      matthew  2752: 
                   2753: ###############################################################
                   2754: 
                   2755: =pod 
1.20      www      2756: 
1.648     raeburn  2757: =item * &get_related_words()
1.46      matthew  2758: 
1.160     matthew  2759: Look up a word in the thesaurus.  Takes a scalar argument and returns
1.46      matthew  2760: an array of words.  If the keyword is not in the thesaurus, an empty array
                   2761: will be returned.  The order of the words returned is determined by the
                   2762: database which holds them.
                   2763: 
                   2764: Uses global $thesaurus_db_file.
                   2765: 
                   2766: =cut
                   2767: 
                   2768: ###############################################################
                   2769: sub get_related_words {
                   2770:     my $keyword = shift;
                   2771:     my %thesaurus_db;
                   2772:     if (! -e $thesaurus_db_file) {
                   2773:         &Apache::lonnet::logthis("Attempt to access $thesaurus_db_file ".
                   2774:                                  "failed because the file does not exist");
                   2775:         return ();
                   2776:     }
                   2777:     if (! tie(%thesaurus_db,'GDBM_File',
1.53      albertel 2778:               $thesaurus_db_file,&GDBM_READER(),0640)){
1.46      matthew  2779:         return ();
                   2780:     } 
                   2781:     my @Words=();
1.429     www      2782:     my $count=0;
1.46      matthew  2783:     if (exists($thesaurus_db{$keyword})) {
1.356     albertel 2784: 	# The first element is the number of times
                   2785: 	# the word appears.  We do not need it now.
1.429     www      2786: 	my (undef,@RelatedWords) = (split(/:/,$thesaurus_db{$keyword}));
                   2787: 	my (undef,$mostfrequentcount)=split(/\,/,$RelatedWords[0]);
                   2788: 	my $threshold=$mostfrequentcount/10;
                   2789:         foreach my $possibleword (@RelatedWords) {
                   2790:             my ($word,$wordcount)=split(/\,/,$possibleword);
                   2791:             if ($wordcount>$threshold) {
                   2792: 		push(@Words,$word);
                   2793:                 $count++;
                   2794:                 if ($count>10) { last; }
                   2795: 	    }
1.20      www      2796:         }
                   2797:     }
1.46      matthew  2798:     untie %thesaurus_db;
                   2799:     return @Words;
1.14      harris41 2800: }
1.46      matthew  2801: 
1.112     bowersj2 2802: =pod
                   2803: 
                   2804: =back
                   2805: 
                   2806: =cut
1.61      www      2807: 
                   2808: # -------------------------------------------------------------- Plaintext name
1.81      albertel 2809: =pod
                   2810: 
1.112     bowersj2 2811: =head1 User Name Functions
                   2812: 
                   2813: =over 4
                   2814: 
1.648     raeburn  2815: =item * &plainname($uname,$udom,$first)
1.81      albertel 2816: 
1.112     bowersj2 2817: Takes a users logon name and returns it as a string in
1.226     albertel 2818: "first middle last generation" form 
                   2819: if $first is set to 'lastname' then it returns it as
                   2820: 'lastname generation, firstname middlename' if their is a lastname
1.81      albertel 2821: 
                   2822: =cut
1.61      www      2823: 
1.295     www      2824: 
1.81      albertel 2825: ###############################################################
1.61      www      2826: sub plainname {
1.226     albertel 2827:     my ($uname,$udom,$first)=@_;
1.537     albertel 2828:     return if (!defined($uname) || !defined($udom));
1.295     www      2829:     my %names=&getnames($uname,$udom);
1.226     albertel 2830:     my $name=&Apache::lonnet::format_name($names{'firstname'},
                   2831: 					  $names{'middlename'},
                   2832: 					  $names{'lastname'},
                   2833: 					  $names{'generation'},$first);
                   2834:     $name=~s/^\s+//;
1.62      www      2835:     $name=~s/\s+$//;
                   2836:     $name=~s/\s+/ /g;
1.353     albertel 2837:     if ($name !~ /\S/) { $name=$uname.':'.$udom; }
1.62      www      2838:     return $name;
1.61      www      2839: }
1.66      www      2840: 
                   2841: # -------------------------------------------------------------------- Nickname
1.81      albertel 2842: =pod
                   2843: 
1.648     raeburn  2844: =item * &nickname($uname,$udom)
1.81      albertel 2845: 
                   2846: Gets a users name and returns it as a string as
                   2847: 
                   2848: "&quot;nickname&quot;"
1.66      www      2849: 
1.81      albertel 2850: if the user has a nickname or
                   2851: 
                   2852: "first middle last generation"
                   2853: 
                   2854: if the user does not
                   2855: 
                   2856: =cut
1.66      www      2857: 
                   2858: sub nickname {
                   2859:     my ($uname,$udom)=@_;
1.537     albertel 2860:     return if (!defined($uname) || !defined($udom));
1.295     www      2861:     my %names=&getnames($uname,$udom);
1.68      albertel 2862:     my $name=$names{'nickname'};
1.66      www      2863:     if ($name) {
                   2864:        $name='&quot;'.$name.'&quot;'; 
                   2865:     } else {
                   2866:        $name=$names{'firstname'}.' '.$names{'middlename'}.' '.
                   2867: 	     $names{'lastname'}.' '.$names{'generation'};
                   2868:        $name=~s/\s+$//;
                   2869:        $name=~s/\s+/ /g;
                   2870:     }
                   2871:     return $name;
                   2872: }
                   2873: 
1.295     www      2874: sub getnames {
                   2875:     my ($uname,$udom)=@_;
1.537     albertel 2876:     return if (!defined($uname) || !defined($udom));
1.433     albertel 2877:     if ($udom eq 'public' && $uname eq 'public') {
                   2878: 	return ('lastname' => &mt('Public'));
                   2879:     }
1.295     www      2880:     my $id=$uname.':'.$udom;
                   2881:     my ($names,$cached)=&Apache::lonnet::is_cached_new('namescache',$id);
                   2882:     if ($cached) {
                   2883: 	return %{$names};
                   2884:     } else {
                   2885: 	my %loadnames=&Apache::lonnet::get('environment',
                   2886:                     ['firstname','middlename','lastname','generation','nickname'],
                   2887: 					 $udom,$uname);
                   2888: 	&Apache::lonnet::do_cache_new('namescache',$id,\%loadnames);
                   2889: 	return %loadnames;
                   2890:     }
                   2891: }
1.61      www      2892: 
1.542     raeburn  2893: # -------------------------------------------------------------------- getemails
1.648     raeburn  2894: 
1.542     raeburn  2895: =pod
                   2896: 
1.648     raeburn  2897: =item * &getemails($uname,$udom)
1.542     raeburn  2898: 
                   2899: Gets a user's email information and returns it as a hash with keys:
                   2900: notification, critnotification, permanentemail
                   2901: 
                   2902: For notification and critnotification, values are comma-separated lists 
1.648     raeburn  2903: of e-mail addresses; for permanentemail, value is a single e-mail address.
1.542     raeburn  2904:  
1.648     raeburn  2905: 
1.542     raeburn  2906: =cut
                   2907: 
1.648     raeburn  2908: 
1.466     albertel 2909: sub getemails {
                   2910:     my ($uname,$udom)=@_;
                   2911:     if ($udom eq 'public' && $uname eq 'public') {
                   2912: 	return;
                   2913:     }
1.467     www      2914:     if (!$udom) { $udom=$env{'user.domain'}; }
                   2915:     if (!$uname) { $uname=$env{'user.name'}; }
1.466     albertel 2916:     my $id=$uname.':'.$udom;
                   2917:     my ($names,$cached)=&Apache::lonnet::is_cached_new('emailscache',$id);
                   2918:     if ($cached) {
                   2919: 	return %{$names};
                   2920:     } else {
                   2921: 	my %loadnames=&Apache::lonnet::get('environment',
                   2922:                     			   ['notification','critnotification',
                   2923: 					    'permanentemail'],
                   2924: 					   $udom,$uname);
                   2925: 	&Apache::lonnet::do_cache_new('emailscache',$id,\%loadnames);
                   2926: 	return %loadnames;
                   2927:     }
                   2928: }
                   2929: 
1.551     albertel 2930: sub flush_email_cache {
                   2931:     my ($uname,$udom)=@_;
                   2932:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   2933:     if (!$uname) { $uname=$env{'user.name'};   }
                   2934:     return if ($udom eq 'public' && $uname eq 'public');
                   2935:     my $id=$uname.':'.$udom;
                   2936:     &Apache::lonnet::devalidate_cache_new('emailscache',$id);
                   2937: }
                   2938: 
1.692.4.2  raeburn  2939: # -------------------------------------------------------------------- getlangs
                   2940: 
                   2941: =pod
                   2942: 
                   2943: =item * &getlangs($uname,$udom)
                   2944: 
                   2945: Gets a user's language preference and returns it as a hash with key:
                   2946: language.
                   2947: 
                   2948: =cut
                   2949: 
                   2950: 
                   2951: sub getlangs {
                   2952:     my ($uname,$udom) = @_;
                   2953:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   2954:     if (!$uname) { $uname=$env{'user.name'};   }
                   2955:     my $id=$uname.':'.$udom;
                   2956:     my ($langs,$cached)=&Apache::lonnet::is_cached_new('userlangs',$id);
                   2957:     if ($cached) {
                   2958:         return %{$langs};
                   2959:     } else {
                   2960:         my %loadlangs=&Apache::lonnet::get('environment',['languages'],
                   2961:                                            $udom,$uname);
                   2962:         &Apache::lonnet::do_cache_new('userlangs',$id,\%loadlangs);
                   2963:         return %loadlangs;
                   2964:     }
                   2965: }
                   2966: 
                   2967: sub flush_langs_cache {
                   2968:     my ($uname,$udom)=@_;
                   2969:     if (!$udom)  { $udom =$env{'user.domain'}; }
                   2970:     if (!$uname) { $uname=$env{'user.name'};   }
                   2971:     return if ($udom eq 'public' && $uname eq 'public');
                   2972:     my $id=$uname.':'.$udom;
                   2973:     &Apache::lonnet::devalidate_cache_new('userlangs',$id);
                   2974: }
                   2975: 
1.61      www      2976: # ------------------------------------------------------------------ Screenname
1.81      albertel 2977: 
                   2978: =pod
                   2979: 
1.648     raeburn  2980: =item * &screenname($uname,$udom)
1.81      albertel 2981: 
                   2982: Gets a users screenname and returns it as a string
                   2983: 
                   2984: =cut
1.61      www      2985: 
                   2986: sub screenname {
                   2987:     my ($uname,$udom)=@_;
1.258     albertel 2988:     if ($uname eq $env{'user.name'} &&
                   2989: 	$udom eq $env{'user.domain'}) {return $env{'environment.screenname'};}
1.212     albertel 2990:     my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname);
1.68      albertel 2991:     return $names{'screenname'};
1.62      www      2992: }
                   2993: 
1.692.4.2  raeburn  2994: # ------------------------------------------------------------- Confirm Wrapper
                   2995: =pod
                   2996: 
                   2997: =item confirmwrapper
                   2998: 
                   2999: Wrap messages about completion of operation in box
                   3000: 
                   3001: =cut
                   3002: 
                   3003: sub confirmwrapper {
                   3004:     my ($message)=@_;
                   3005:     if ($message) {
                   3006:         return "\n".'<div class="LC_confirm_box">'."\n"
                   3007:                .$message."\n"
                   3008:                .'</div>'."\n";
                   3009:     } else {
                   3010:         return $message;
                   3011:     }
                   3012: }
1.212     albertel 3013: 
1.62      www      3014: # ------------------------------------------------------------- Message Wrapper
                   3015: 
                   3016: sub messagewrapper {
1.369     www      3017:     my ($link,$username,$domain,$subject,$text)=@_;
1.62      www      3018:     return 
1.441     albertel 3019:         '<a href="/adm/email?compose=individual&amp;'.
                   3020:         'recname='.$username.'&amp;recdom='.$domain.
                   3021: 	'&amp;subject='.&escape($subject).'&amp;text='.&escape($text).'" '.
1.200     matthew  3022:         'title="'.&mt('Send message').'">'.$link.'</a>';
1.74      www      3023: }
                   3024: # --------------------------------------------------------------- Notes Wrapper
                   3025: 
                   3026: sub noteswrapper {
                   3027:     my ($link,$un,$do)=@_;
                   3028:     return 
                   3029: "<a href='/adm/email?recordftf=retrieve&recname=$un&recdom=$do'>$link</a>";
1.62      www      3030: }
                   3031: # ------------------------------------------------------------- Aboutme Wrapper
                   3032: 
                   3033: sub aboutmewrapper {
1.166     www      3034:     my ($link,$username,$domain,$target)=@_;
1.447     raeburn  3035:     if (!defined($username)  && !defined($domain)) {
                   3036:         return;
                   3037:     }
1.205     www      3038:     return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'.
1.692.4.2  raeburn  3039: 	($target?' target="$target"':'').' title="'.&mt("View this user's personal information page").'">'.$link.'</a>';
1.62      www      3040: }
                   3041: 
                   3042: # ------------------------------------------------------------ Syllabus Wrapper
                   3043: 
                   3044: 
                   3045: sub syllabuswrapper {
1.109     matthew  3046:     my ($linktext,$coursedir,$domain,$fontcolor)=@_;
                   3047:     if ($fontcolor) { 
                   3048:         $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>'; 
                   3049:     }
1.208     matthew  3050:     return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>};
1.61      www      3051: }
1.14      harris41 3052: 
1.208     matthew  3053: sub track_student_link {
1.692.4.17  raeburn  3054:     my ($linktext,$sname,$sdom,$target,$start,$only_body) = @_;
1.268     albertel 3055:     my $link ="/adm/trackstudent?";
1.208     matthew  3056:     my $title = 'View recent activity';
                   3057:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3058:         defined($sdom)  && $sdom  !~ /^\s*$/) {
1.268     albertel 3059:         $link .= "selected_student=$sname:$sdom";
1.208     matthew  3060:         $title .= ' of this student';
1.268     albertel 3061:     } 
1.208     matthew  3062:     if (defined($target) && $target !~ /^\s*$/) {
                   3063:         $target = qq{target="$target"};
                   3064:     } else {
                   3065:         $target = '';
                   3066:     }
1.268     albertel 3067:     if ($start) { $link.='&amp;start='.$start; }
1.692.4.17  raeburn  3068:     if ($only_body) { $link .= '&amp;only_body=1'; }
1.554     albertel 3069:     $title = &mt($title);
                   3070:     $linktext = &mt($linktext);
1.448     albertel 3071:     return qq{<a href="$link" title="$title" $target>$linktext</a>}.
                   3072: 	&help_open_topic('View_recent_activity');
1.208     matthew  3073: }
                   3074: 
1.692.4.2  raeburn  3075: sub slot_reservations_link {
                   3076:     my ($linktext,$sname,$sdom,$target) = @_;
                   3077:     my $link ="/adm/slotrequest?command=showresv&amp;origin=aboutme";
                   3078:     my $title = 'View slot reservation history';
                   3079:     if (defined($sname) && $sname !~ /^\s*$/ &&
                   3080:         defined($sdom)  && $sdom  !~ /^\s*$/) {
                   3081:         $link .= "&amp;uname=$sname&amp;udom=$sdom";
                   3082:         $title .= ' of this student';
                   3083:     }
                   3084:     if (defined($target) && $target !~ /^\s*$/) {
                   3085:         $target = qq{target="$target"};
                   3086:     } else {
                   3087:         $target = '';
                   3088:     }
                   3089:     $title = &mt($title);
                   3090:     $linktext = &mt($linktext);
                   3091:     return qq{<a href="$link" title="$title" $target>$linktext</a>};
                   3092: # FIXME uncomment when help item created: &help_open_topic('Slot_Reservation_History');
                   3093: 
                   3094: }
                   3095: 
1.508     www      3096: # ===================================================== Display a student photo
                   3097: 
                   3098: 
1.509     albertel 3099: sub student_image_tag {
1.508     www      3100:     my ($domain,$user)=@_;
                   3101:     my $imgsrc=&Apache::lonnet::studentphoto($domain,$user,'jpg');
                   3102:     if (($imgsrc) && ($imgsrc ne '/adm/lonKaputt/lonlogo_broken.gif')) {
                   3103: 	return '<img src="'.$imgsrc.'" align="right" />';
                   3104:     } else {
                   3105: 	return '';
                   3106:     }
                   3107: }
                   3108: 
1.112     bowersj2 3109: =pod
                   3110: 
                   3111: =back
                   3112: 
                   3113: =head1 Access .tab File Data
                   3114: 
                   3115: =over 4
                   3116: 
1.648     raeburn  3117: =item * &languageids() 
1.112     bowersj2 3118: 
                   3119: returns list of all language ids
                   3120: 
                   3121: =cut
                   3122: 
1.14      harris41 3123: sub languageids {
1.16      harris41 3124:     return sort(keys(%language));
1.14      harris41 3125: }
                   3126: 
1.112     bowersj2 3127: =pod
                   3128: 
1.648     raeburn  3129: =item * &languagedescription() 
1.112     bowersj2 3130: 
                   3131: returns description of a specified language id
                   3132: 
                   3133: =cut
                   3134: 
1.14      harris41 3135: sub languagedescription {
1.125     www      3136:     my $code=shift;
                   3137:     return  ($supported_language{$code}?'* ':'').
                   3138:             $language{$code}.
1.126     www      3139: 	    ($supported_language{$code}?' ('.&mt('interface available').')':'');
1.145     www      3140: }
                   3141: 
                   3142: sub plainlanguagedescription {
                   3143:     my $code=shift;
                   3144:     return $language{$code};
                   3145: }
                   3146: 
                   3147: sub supportedlanguagecode {
                   3148:     my $code=shift;
                   3149:     return $supported_language{$code};
1.97      www      3150: }
                   3151: 
1.112     bowersj2 3152: =pod
                   3153: 
1.648     raeburn  3154: =item * &copyrightids() 
1.112     bowersj2 3155: 
                   3156: returns list of all copyrights
                   3157: 
                   3158: =cut
                   3159: 
                   3160: sub copyrightids {
                   3161:     return sort(keys(%cprtag));
                   3162: }
                   3163: 
                   3164: =pod
                   3165: 
1.648     raeburn  3166: =item * &copyrightdescription() 
1.112     bowersj2 3167: 
                   3168: returns description of a specified copyright id
                   3169: 
                   3170: =cut
                   3171: 
                   3172: sub copyrightdescription {
1.166     www      3173:     return &mt($cprtag{shift(@_)});
1.112     bowersj2 3174: }
1.197     matthew  3175: 
                   3176: =pod
                   3177: 
1.648     raeburn  3178: =item * &source_copyrightids() 
1.192     taceyjo1 3179: 
                   3180: returns list of all source copyrights
                   3181: 
                   3182: =cut
                   3183: 
                   3184: sub source_copyrightids {
                   3185:     return sort(keys(%scprtag));
                   3186: }
                   3187: 
                   3188: =pod
                   3189: 
1.648     raeburn  3190: =item * &source_copyrightdescription() 
1.192     taceyjo1 3191: 
                   3192: returns description of a specified source copyright id
                   3193: 
                   3194: =cut
                   3195: 
                   3196: sub source_copyrightdescription {
                   3197:     return &mt($scprtag{shift(@_)});
                   3198: }
1.112     bowersj2 3199: 
                   3200: =pod
                   3201: 
1.648     raeburn  3202: =item * &filecategories() 
1.112     bowersj2 3203: 
                   3204: returns list of all file categories
                   3205: 
                   3206: =cut
                   3207: 
                   3208: sub filecategories {
                   3209:     return sort(keys(%category_extensions));
                   3210: }
                   3211: 
                   3212: =pod
                   3213: 
1.648     raeburn  3214: =item * &filecategorytypes() 
1.112     bowersj2 3215: 
                   3216: returns list of file types belonging to a given file
                   3217: category
                   3218: 
                   3219: =cut
                   3220: 
                   3221: sub filecategorytypes {
1.356     albertel 3222:     my ($cat) = @_;
                   3223:     return @{$category_extensions{lc($cat)}};
1.112     bowersj2 3224: }
                   3225: 
                   3226: =pod
                   3227: 
1.648     raeburn  3228: =item * &fileembstyle() 
1.112     bowersj2 3229: 
                   3230: returns embedding style for a specified file type
                   3231: 
                   3232: =cut
                   3233: 
                   3234: sub fileembstyle {
                   3235:     return $fe{lc(shift(@_))};
1.169     www      3236: }
                   3237: 
1.351     www      3238: sub filemimetype {
                   3239:     return $fm{lc(shift(@_))};
                   3240: }
                   3241: 
1.169     www      3242: 
                   3243: sub filecategoryselect {
                   3244:     my ($name,$value)=@_;
1.189     matthew  3245:     return &select_form($value,$name,
1.169     www      3246: 			'' => &mt('Any category'),
                   3247: 			map { $_,$_ } sort(keys(%category_extensions)));
1.112     bowersj2 3248: }
                   3249: 
                   3250: =pod
                   3251: 
1.648     raeburn  3252: =item * &filedescription() 
1.112     bowersj2 3253: 
                   3254: returns description for a specified file type
                   3255: 
                   3256: =cut
                   3257: 
                   3258: sub filedescription {
1.188     matthew  3259:     my $file_description = $fd{lc(shift())};
                   3260:     $file_description =~ s:([\[\]]):~$1:g;
                   3261:     return &mt($file_description);
1.112     bowersj2 3262: }
                   3263: 
                   3264: =pod
                   3265: 
1.648     raeburn  3266: =item * &filedescriptionex() 
1.112     bowersj2 3267: 
                   3268: returns description for a specified file type with
                   3269: extra formatting
                   3270: 
                   3271: =cut
                   3272: 
                   3273: sub filedescriptionex {
                   3274:     my $ex=shift;
1.188     matthew  3275:     my $file_description = $fd{lc($ex)};
                   3276:     $file_description =~ s:([\[\]]):~$1:g;
                   3277:     return '.'.$ex.' '.&mt($file_description);
1.112     bowersj2 3278: }
                   3279: 
                   3280: # End of .tab access
                   3281: =pod
                   3282: 
                   3283: =back
                   3284: 
                   3285: =cut
                   3286: 
                   3287: # ------------------------------------------------------------------ File Types
                   3288: sub fileextensions {
                   3289:     return sort(keys(%fe));
                   3290: }
                   3291: 
1.97      www      3292: # ----------------------------------------------------------- Display Languages
                   3293: # returns a hash with all desired display languages
                   3294: #
                   3295: 
                   3296: sub display_languages {
                   3297:     my %languages=();
1.692.4.1  raeburn  3298:     foreach my $lang (&Apache::lonlocal::preferred_languages()) {
1.356     albertel 3299: 	$languages{$lang}=1;
1.97      www      3300:     }
                   3301:     &get_unprocessed_cgi($ENV{'QUERY_STRING'},['displaylanguage']);
1.258     albertel 3302:     if ($env{'form.displaylanguage'}) {
1.356     albertel 3303: 	foreach my $lang (split(/\s*(\,|\;|\:)\s*/,$env{'form.displaylanguage'})) {
                   3304: 	    $languages{$lang}=1;
1.97      www      3305:         }
                   3306:     }
                   3307:     return %languages;
1.14      harris41 3308: }
                   3309: 
1.582     albertel 3310: sub languages {
                   3311:     my ($possible_langs) = @_;
1.692.4.1  raeburn  3312:     my @preferred_langs = &Apache::lonlocal::preferred_languages();
1.582     albertel 3313:     if (!ref($possible_langs)) {
                   3314: 	if( wantarray ) {
                   3315: 	    return @preferred_langs;
                   3316: 	} else {
                   3317: 	    return $preferred_langs[0];
                   3318: 	}
                   3319:     }
                   3320:     my %possibilities = map { $_ => 1 } (@$possible_langs);
                   3321:     my @preferred_possibilities;
                   3322:     foreach my $preferred_lang (@preferred_langs) {
                   3323: 	if (exists($possibilities{$preferred_lang})) {
                   3324: 	    push(@preferred_possibilities, $preferred_lang);
                   3325: 	}
                   3326:     }
                   3327:     if( wantarray ) {
                   3328: 	return @preferred_possibilities;
                   3329:     }
                   3330:     return $preferred_possibilities[0];
                   3331: }
                   3332: 
1.692.4.2  raeburn  3333: sub user_lang {
                   3334:     my ($touname,$toudom,$fromcid) = @_;
                   3335:     my @userlangs;
                   3336:     if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
                   3337:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
                   3338:                     $env{'course.'.$fromcid.'.languages'}));
                   3339:     } else {
                   3340:         my %langhash = &getlangs($touname,$toudom);
                   3341:         if ($langhash{'languages'} ne '') {
                   3342:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
                   3343:         } else {
                   3344:             my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
                   3345:             if ($domdefs{'lang_def'} ne '') {
                   3346:                 @userlangs = ($domdefs{'lang_def'});
                   3347:             }
                   3348:         }
                   3349:     }
                   3350:     my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
                   3351:     my $user_lh = Apache::localize->get_handle(@languages);
                   3352:     return $user_lh;
                   3353: }
                   3354: 
1.112     bowersj2 3355: ###############################################################
                   3356: ##               Student Answer Attempts                     ##
                   3357: ###############################################################
                   3358: 
                   3359: =pod
                   3360: 
                   3361: =head1 Alternate Problem Views
                   3362: 
                   3363: =over 4
                   3364: 
1.648     raeburn  3365: =item * &get_previous_attempt($symb, $username, $domain, $course,
1.112     bowersj2 3366:     $getattempt, $regexp, $gradesub)
                   3367: 
                   3368: Return string with previous attempt on problem. Arguments:
                   3369: 
                   3370: =over 4
                   3371: 
                   3372: =item * $symb: Problem, including path
                   3373: 
                   3374: =item * $username: username of the desired student
                   3375: 
                   3376: =item * $domain: domain of the desired student
1.14      harris41 3377: 
1.112     bowersj2 3378: =item * $course: Course ID
1.14      harris41 3379: 
1.112     bowersj2 3380: =item * $getattempt: Leave blank for all attempts, otherwise put
                   3381:     something
1.14      harris41 3382: 
1.112     bowersj2 3383: =item * $regexp: if string matches this regexp, the string will be
                   3384:     sent to $gradesub
1.14      harris41 3385: 
1.112     bowersj2 3386: =item * $gradesub: routine that processes the string if it matches $regexp
1.14      harris41 3387: 
1.112     bowersj2 3388: =back
1.14      harris41 3389: 
1.112     bowersj2 3390: The output string is a table containing all desired attempts, if any.
1.16      harris41 3391: 
1.112     bowersj2 3392: =cut
1.1       albertel 3393: 
                   3394: sub get_previous_attempt {
1.43      ng       3395:   my ($symb,$username,$domain,$course,$getattempt,$regexp,$gradesub)=@_;
1.1       albertel 3396:   my $prevattempts='';
1.43      ng       3397:   no strict 'refs';
1.1       albertel 3398:   if ($symb) {
1.3       albertel 3399:     my (%returnhash)=
                   3400:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel 3401:     if ($returnhash{'version'}) {
                   3402:       my %lasthash=();
                   3403:       my $version;
                   3404:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.356     albertel 3405:         foreach my $key (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   3406: 	  $lasthash{$key}=$returnhash{$version.':'.$key};
1.19      harris41 3407:         }
1.1       albertel 3408:       }
1.596     albertel 3409:       $prevattempts=&start_data_table().&start_data_table_header_row();
                   3410:       $prevattempts.='<th>'.&mt('History').'</th>';
1.356     albertel 3411:       foreach my $key (sort(keys(%lasthash))) {
                   3412: 	my ($ign,@parts) = split(/\./,$key);
1.41      ng       3413: 	if ($#parts > 0) {
1.31      albertel 3414: 	  my $data=$parts[-1];
                   3415: 	  pop(@parts);
1.596     albertel 3416: 	  $prevattempts.='<th>'.&mt('Part ').join('.',@parts).'<br />'.$data.'&nbsp;</th>';
1.31      albertel 3417: 	} else {
1.41      ng       3418: 	  if ($#parts == 0) {
                   3419: 	    $prevattempts.='<th>'.$parts[0].'</th>';
                   3420: 	  } else {
                   3421: 	    $prevattempts.='<th>'.$ign.'</th>';
                   3422: 	  }
1.31      albertel 3423: 	}
1.16      harris41 3424:       }
1.596     albertel 3425:       $prevattempts.=&end_data_table_header_row();
1.40      ng       3426:       if ($getattempt eq '') {
                   3427: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
1.596     albertel 3428: 	  $prevattempts.=&start_data_table_row().
                   3429: 	      '<td>'.&mt('Transaction [_1]',$version).'</td>';
1.356     albertel 3430: 	    foreach my $key (sort(keys(%lasthash))) {
1.581     albertel 3431: 		my $value = &format_previous_attempt_value($key,
                   3432: 							   $returnhash{$version.':'.$key});
                   3433: 		$prevattempts.='<td>'.$value.'&nbsp;</td>';   
1.40      ng       3434: 	    }
1.596     albertel 3435: 	  $prevattempts.=&end_data_table_row();
1.40      ng       3436: 	 }
1.1       albertel 3437:       }
1.596     albertel 3438:       $prevattempts.=&start_data_table_row().'<td>'.&mt('Current').'</td>';
1.356     albertel 3439:       foreach my $key (sort(keys(%lasthash))) {
1.581     albertel 3440: 	my $value = &format_previous_attempt_value($key,$lasthash{$key});
1.356     albertel 3441: 	if ($key =~/$regexp$/ && (defined &$gradesub)) {$value = &$gradesub($value)}
1.40      ng       3442: 	$prevattempts.='<td>'.$value.'&nbsp;</td>';
1.16      harris41 3443:       }
1.596     albertel 3444:       $prevattempts.= &end_data_table_row().&end_data_table();
1.1       albertel 3445:     } else {
1.596     albertel 3446:       $prevattempts=
                   3447: 	  &start_data_table().&start_data_table_row().
                   3448: 	  '<td>'.&mt('Nothing submitted - no attempts.').'</td>'.
                   3449: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3450:     }
                   3451:   } else {
1.596     albertel 3452:     $prevattempts=
                   3453: 	  &start_data_table().&start_data_table_row().
                   3454: 	  '<td>'.&mt('No data.').'</td>'.
                   3455: 	  &end_data_table_row().&end_data_table();
1.1       albertel 3456:   }
1.10      albertel 3457: }
                   3458: 
1.581     albertel 3459: sub format_previous_attempt_value {
                   3460:     my ($key,$value) = @_;
                   3461:     if ($key =~ /timestamp/) {
                   3462: 	$value = &Apache::lonlocal::locallocaltime($value);
                   3463:     } elsif (ref($value) eq 'ARRAY') {
                   3464: 	$value = '('.join(', ', @{ $value }).')';
                   3465:     } else {
                   3466: 	$value = &unescape($value);
                   3467:     }
                   3468:     return $value;
                   3469: }
                   3470: 
                   3471: 
1.107     albertel 3472: sub relative_to_absolute {
                   3473:     my ($url,$output)=@_;
                   3474:     my $parser=HTML::TokeParser->new(\$output);
                   3475:     my $token;
                   3476:     my $thisdir=$url;
                   3477:     my @rlinks=();
                   3478:     while ($token=$parser->get_token) {
                   3479: 	if ($token->[0] eq 'S') {
                   3480: 	    if ($token->[1] eq 'a') {
                   3481: 		if ($token->[2]->{'href'}) {
                   3482: 		    $rlinks[$#rlinks+1]=$token->[2]->{'href'};
                   3483: 		}
                   3484: 	    } elsif ($token->[1] eq 'img' || $token->[1] eq 'embed' ) {
                   3485: 		$rlinks[$#rlinks+1]=$token->[2]->{'src'};
                   3486: 	    } elsif ($token->[1] eq 'base') {
                   3487: 		$thisdir=$token->[2]->{'href'};
                   3488: 	    }
                   3489: 	}
                   3490:     }
                   3491:     $thisdir=~s-/[^/]*$--;
1.356     albertel 3492:     foreach my $link (@rlinks) {
1.692.4.2  raeburn  3493: 	unless (($link=~/^https?\:\/\//i) ||
1.356     albertel 3494: 		($link=~/^\//) ||
                   3495: 		($link=~/^javascript:/i) ||
                   3496: 		($link=~/^mailto:/i) ||
                   3497: 		($link=~/^\#/)) {
                   3498: 	    my $newlocation=&Apache::lonnet::hreflocation($thisdir,$link);
                   3499: 	    $output=~s/(\"|\'|\=\s*)\Q$link\E(\"|\'|\s|\>)/$1$newlocation$2/;
1.107     albertel 3500: 	}
                   3501:     }
                   3502: # -------------------------------------------------- Deal with Applet codebases
                   3503:     $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
                   3504:     return $output;
                   3505: }
                   3506: 
1.112     bowersj2 3507: =pod
                   3508: 
1.648     raeburn  3509: =item * &get_student_view()
1.112     bowersj2 3510: 
                   3511: show a snapshot of what student was looking at
                   3512: 
                   3513: =cut
                   3514: 
1.10      albertel 3515: sub get_student_view {
1.186     albertel 3516:   my ($symb,$username,$domain,$courseid,$target,$moreenv) = @_;
1.114     www      3517:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3518:   my (%form);
1.10      albertel 3519:   my @elements=('symb','courseid','domain','username');
                   3520:   foreach my $element (@elements) {
1.186     albertel 3521:       $form{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3522:   }
1.186     albertel 3523:   if (defined($moreenv)) {
                   3524:       %form=(%form,%{$moreenv});
                   3525:   }
1.236     albertel 3526:   if (defined($target)) { $form{'grade_target'} = $target; }
1.107     albertel 3527:   $feedurl=&Apache::lonnet::clutter($feedurl);
1.650     www      3528:   my ($userview,$response)=&Apache::lonnet::ssi_body($feedurl,%form);
1.11      albertel 3529:   $userview=~s/\<body[^\>]*\>//gi;
                   3530:   $userview=~s/\<\/body\>//gi;
                   3531:   $userview=~s/\<html\>//gi;
                   3532:   $userview=~s/\<\/html\>//gi;
                   3533:   $userview=~s/\<head\>//gi;
                   3534:   $userview=~s/\<\/head\>//gi;
                   3535:   $userview=~s/action\s*\=/would_be_action\=/gi;
1.107     albertel 3536:   $userview=&relative_to_absolute($feedurl,$userview);
1.650     www      3537:   if (wantarray) {
                   3538:      return ($userview,$response);
                   3539:   } else {
                   3540:      return $userview;
                   3541:   }
                   3542: }
                   3543: 
                   3544: sub get_student_view_with_retries {
                   3545:   my ($symb,$retries,$username,$domain,$courseid,$target,$moreenv) = @_;
                   3546: 
                   3547:     my $ok = 0;                 # True if we got a good response.
                   3548:     my $content;
                   3549:     my $response;
                   3550: 
                   3551:     # Try to get the student_view done. within the retries count:
                   3552:     
                   3553:     do {
                   3554:          ($content, $response) = &get_student_view($symb,$username,$domain,$courseid,$target,$moreenv);
                   3555:          $ok      = $response->is_success;
                   3556:          if (!$ok) {
                   3557:             &Apache::lonnet::logthis("Failed get_student_view_with_retries on $symb: ".$response->is_success.', '.$response->code.', '.$response->message);
                   3558:          }
                   3559:          $retries--;
                   3560:     } while (!$ok && ($retries > 0));
                   3561:     
                   3562:     if (!$ok) {
                   3563:        $content = '';          # On error return an empty content.
                   3564:     }
1.651     www      3565:     if (wantarray) {
                   3566:        return ($content, $response);
                   3567:     } else {
                   3568:        return $content;
                   3569:     }
1.11      albertel 3570: }
                   3571: 
1.112     bowersj2 3572: =pod
                   3573: 
1.648     raeburn  3574: =item * &get_student_answers() 
1.112     bowersj2 3575: 
                   3576: show a snapshot of how student was answering problem
                   3577: 
                   3578: =cut
                   3579: 
1.11      albertel 3580: sub get_student_answers {
1.100     sakharuk 3581:   my ($symb,$username,$domain,$courseid,%form) = @_;
1.114     www      3582:   my ($map,$id,$feedurl) = &Apache::lonnet::decode_symb($symb);
1.186     albertel 3583:   my (%moreenv);
1.11      albertel 3584:   my @elements=('symb','courseid','domain','username');
                   3585:   foreach my $element (@elements) {
1.186     albertel 3586:     $moreenv{'grade_'.$element}=eval '$'.$element #'
1.10      albertel 3587:   }
1.186     albertel 3588:   $moreenv{'grade_target'}='answer';
                   3589:   %moreenv=(%form,%moreenv);
1.497     raeburn  3590:   $feedurl = &Apache::lonnet::clutter($feedurl);
                   3591:   my $userview=&Apache::lonnet::ssi($feedurl,%moreenv);
1.10      albertel 3592:   return $userview;
1.1       albertel 3593: }
1.116     albertel 3594: 
                   3595: =pod
                   3596: 
                   3597: =item * &submlink()
                   3598: 
1.242     albertel 3599: Inputs: $text $uname $udom $symb $target
1.116     albertel 3600: 
                   3601: Returns: A link to grades.pm such as to see the SUBM view of a student
                   3602: 
                   3603: =cut
                   3604: 
                   3605: ###############################################
                   3606: sub submlink {
1.242     albertel 3607:     my ($text,$uname,$udom,$symb,$target)=@_;
1.116     albertel 3608:     if (!($uname && $udom)) {
                   3609: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3610: 	    &Apache::lonnet::whichuser($symb);
1.116     albertel 3611: 	if (!$symb) { $symb=$cursymb; }
                   3612:     }
1.254     matthew  3613:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3614:     $symb=&escape($symb);
1.242     albertel 3615:     if ($target) { $target="target=\"$target\""; }
                   3616:     return '<a href="/adm/grades?&command=submission&'.
                   3617: 	'symb='.$symb.'&student='.$uname.
                   3618: 	'&userdom='.$udom.'" '.$target.'>'.$text.'</a>';
                   3619: }
                   3620: ##############################################
                   3621: 
                   3622: =pod
                   3623: 
                   3624: =item * &pgrdlink()
                   3625: 
                   3626: Inputs: $text $uname $udom $symb $target
                   3627: 
                   3628: Returns: A link to grades.pm such as to see the PGRD view of a student
                   3629: 
                   3630: =cut
                   3631: 
                   3632: ###############################################
                   3633: sub pgrdlink {
                   3634:     my $link=&submlink(@_);
                   3635:     $link=~s/(&command=submission)/$1&showgrading=yes/;
                   3636:     return $link;
                   3637: }
                   3638: ##############################################
                   3639: 
                   3640: =pod
                   3641: 
                   3642: =item * &pprmlink()
                   3643: 
                   3644: Inputs: $text $uname $udom $symb $target
                   3645: 
                   3646: Returns: A link to parmset.pm such as to see the PPRM view of a
1.283     albertel 3647: student and a specific resource
1.242     albertel 3648: 
                   3649: =cut
                   3650: 
                   3651: ###############################################
                   3652: sub pprmlink {
                   3653:     my ($text,$uname,$udom,$symb,$target)=@_;
                   3654:     if (!($uname && $udom)) {
                   3655: 	(my $cursymb, my $courseid,$udom,$uname)=
1.463     albertel 3656: 	    &Apache::lonnet::whichuser($symb);
1.242     albertel 3657: 	if (!$symb) { $symb=$cursymb; }
                   3658:     }
1.254     matthew  3659:     if (!$symb) { $symb=&Apache::lonnet::symbread(); }
1.369     www      3660:     $symb=&escape($symb);
1.242     albertel 3661:     if ($target) { $target="target=\"$target\""; }
1.595     albertel 3662:     return '<a href="/adm/parmset?command=set&amp;'.
                   3663: 	'symb='.$symb.'&amp;uname='.$uname.
                   3664: 	'&amp;udom='.$udom.'" '.$target.'>'.$text.'</a>';
1.116     albertel 3665: }
                   3666: ##############################################
1.37      matthew  3667: 
1.112     bowersj2 3668: =pod
                   3669: 
                   3670: =back
                   3671: 
                   3672: =cut
                   3673: 
1.37      matthew  3674: ###############################################
1.51      www      3675: 
                   3676: 
                   3677: sub timehash {
1.687     raeburn  3678:     my ($thistime) = @_;
                   3679:     my $timezone = &Apache::lonlocal::gettimezone();
                   3680:     my $dt = DateTime->from_epoch(epoch => $thistime)
                   3681:                      ->set_time_zone($timezone);
                   3682:     my $wday = $dt->day_of_week();
                   3683:     if ($wday == 7) { $wday = 0; }
                   3684:     return ( 'second' => $dt->second(),
                   3685:              'minute' => $dt->minute(),
                   3686:              'hour'   => $dt->hour(),
                   3687:              'day'     => $dt->day_of_month(),
                   3688:              'month'   => $dt->month(),
                   3689:              'year'    => $dt->year(),
                   3690:              'weekday' => $wday,
                   3691:              'dayyear' => $dt->day_of_year(),
                   3692:              'dlsav'   => $dt->is_dst() );
1.51      www      3693: }
                   3694: 
1.370     www      3695: sub utc_string {
                   3696:     my ($date)=@_;
1.371     www      3697:     return strftime("%Y%m%dT%H%M%SZ",gmtime($date));
1.370     www      3698: }
                   3699: 
1.51      www      3700: sub maketime {
                   3701:     my %th=@_;
1.687     raeburn  3702:     my ($epoch_time,$timezone,$dt);
                   3703:     $timezone = &Apache::lonlocal::gettimezone();
                   3704:     eval {
                   3705:         $dt = DateTime->new( year   => $th{'year'},
                   3706:                              month  => $th{'month'},
                   3707:                              day    => $th{'day'},
                   3708:                              hour   => $th{'hour'},
                   3709:                              minute => $th{'minute'},
                   3710:                              second => $th{'second'},
                   3711:                              time_zone => $timezone,
                   3712:                          );
                   3713:     };
                   3714:     if (!$@) {
                   3715:         $epoch_time = $dt->epoch;
                   3716:         if ($epoch_time) {
                   3717:             return $epoch_time;
                   3718:         }
                   3719:     }
1.51      www      3720:     return POSIX::mktime(
                   3721:         ($th{'seconds'},$th{'minutes'},$th{'hours'},
1.210     www      3722:          $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1));
1.70      www      3723: }
                   3724: 
                   3725: #########################################
1.51      www      3726: 
                   3727: sub findallcourses {
1.482     raeburn  3728:     my ($roles,$uname,$udom) = @_;
1.355     albertel 3729:     my %roles;
                   3730:     if (ref($roles)) { %roles = map { $_ => 1 } @{$roles}; }
1.348     albertel 3731:     my %courses;
1.51      www      3732:     my $now=time;
1.482     raeburn  3733:     if (!defined($uname)) {
                   3734:         $uname = $env{'user.name'};
                   3735:     }
                   3736:     if (!defined($udom)) {
                   3737:         $udom = $env{'user.domain'};
                   3738:     }
                   3739:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.692.4.37! raeburn  3740:         my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
        !          3741:         my %roleshash = &Apache::lonnet::dump('roles',$udom,$uname,'.',undef,
        !          3742:                                               $extra);
1.482     raeburn  3743:         if (!%roles) {
                   3744:             %roles = (
                   3745:                        cc => 1,
1.692.4.22  raeburn  3746:                        co => 1,
1.482     raeburn  3747:                        in => 1,
                   3748:                        ep => 1,
                   3749:                        ta => 1,
                   3750:                        cr => 1,
                   3751:                        st => 1,
                   3752:              );
                   3753:         }
                   3754:         foreach my $entry (keys(%roleshash)) {
                   3755:             my ($trole,$tend,$tstart) = split(/_/,$roleshash{$entry});
                   3756:             if ($trole =~ /^cr/) { 
                   3757:                 next if (!exists($roles{$trole}) && !exists($roles{'cr'}));
                   3758:             } else {
                   3759:                 next if (!exists($roles{$trole}));
                   3760:             }
                   3761:             if ($tend) {
                   3762:                 next if ($tend < $now);
                   3763:             }
                   3764:             if ($tstart) {
                   3765:                 next if ($tstart > $now);
                   3766:             }
                   3767:             my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role,$realsec);
                   3768:             (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry);
                   3769:             if ($secpart eq '') {
                   3770:                 ($cnum,$role) = split(/_/,$cnumpart); 
                   3771:                 $sec = 'none';
                   3772:                 $realsec = '';
                   3773:             } else {
                   3774:                 $cnum = $cnumpart;
                   3775:                 ($sec,$role) = split(/_/,$secpart);
                   3776:                 $realsec = $sec;
1.490     raeburn  3777:             }
1.482     raeburn  3778:             $courses{$cdom.'_'.$cnum}{$sec} = $trole.'/'.$cdom.'/'.$cnum.'/'.$realsec;
                   3779:         }
                   3780:     } else {
                   3781:         foreach my $key (keys(%env)) {
1.483     albertel 3782: 	    if ( $key=~m{^user\.role\.(\w+)\./($match_domain)/($match_courseid)/?(\w*)$} ||
                   3783:                  $key=~m{^user\.role\.(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_courseid)/?(\w*)$}) {
1.482     raeburn  3784: 	        my ($role,$cdom,$cnum,$sec) = ($1,$2,$3,$4);
                   3785: 	        next if ($role eq 'ca' || $role eq 'aa');
                   3786: 	        next if (%roles && !exists($roles{$role}));
                   3787: 	        my ($starttime,$endtime)=split(/\./,$env{$key});
                   3788:                 my $active=1;
                   3789:                 if ($starttime) {
                   3790: 		    if ($now<$starttime) { $active=0; }
                   3791:                 }
                   3792:                 if ($endtime) {
                   3793:                     if ($now>$endtime) { $active=0; }
                   3794:                 }
                   3795:                 if ($active) {
                   3796:                     if ($sec eq '') {
                   3797:                         $sec = 'none';
                   3798:                     }
                   3799:                     $courses{$cdom.'_'.$cnum}{$sec} = 
                   3800:                                      $role.'/'.$cdom.'/'.$cnum.'/'.$sec;
1.474     raeburn  3801:                 }
                   3802:             }
1.51      www      3803:         }
                   3804:     }
1.474     raeburn  3805:     return %courses;
1.51      www      3806: }
1.37      matthew  3807: 
1.54      www      3808: ###############################################
1.474     raeburn  3809: 
                   3810: sub blockcheck {
1.482     raeburn  3811:     my ($setters,$activity,$uname,$udom) = @_;
1.490     raeburn  3812: 
                   3813:     if (!defined($udom)) {
                   3814:         $udom = $env{'user.domain'};
                   3815:     }
                   3816:     if (!defined($uname)) {
                   3817:         $uname = $env{'user.name'};
                   3818:     }
                   3819: 
                   3820:     # If uname and udom are for a course, check for blocks in the course.
                   3821: 
                   3822:     if (&Apache::lonnet::is_course($udom,$uname)) {
                   3823:         my %records = &Apache::lonnet::dump('comm_block',$udom,$uname);
1.502     raeburn  3824:         my ($startblock,$endblock)=&get_blocks($setters,$activity,$udom,$uname);
1.490     raeburn  3825:         return ($startblock,$endblock);
                   3826:     }
1.474     raeburn  3827: 
1.502     raeburn  3828:     my $startblock = 0;
                   3829:     my $endblock = 0;
1.482     raeburn  3830:     my %live_courses = &findallcourses(undef,$uname,$udom);
1.474     raeburn  3831: 
1.490     raeburn  3832:     # If uname is for a user, and activity is course-specific, i.e.,
                   3833:     # boards, chat or groups, check for blocking in current course only.
1.474     raeburn  3834: 
1.490     raeburn  3835:     if (($activity eq 'boards' || $activity eq 'chat' ||
                   3836:          $activity eq 'groups') && ($env{'request.course.id'})) {
                   3837:         foreach my $key (keys(%live_courses)) {
                   3838:             if ($key ne $env{'request.course.id'}) {
                   3839:                 delete($live_courses{$key});
                   3840:             }
                   3841:         }
                   3842:     }
                   3843: 
                   3844:     my $otheruser = 0;
                   3845:     my %own_courses;
                   3846:     if ((($uname ne $env{'user.name'})) || ($udom ne $env{'user.domain'})) {
                   3847:         # Resource belongs to user other than current user.
                   3848:         $otheruser = 1;
                   3849:         # Gather courses for current user
                   3850:         %own_courses = 
                   3851:             &findallcourses(undef,$env{'user.name'},$env{'user.domain'});
                   3852:     }
                   3853: 
                   3854:     # Gather active course roles - course coordinator, instructor, 
                   3855:     # exam proctor, ta, student, or custom role.
1.474     raeburn  3856: 
                   3857:     foreach my $course (keys(%live_courses)) {
1.482     raeburn  3858:         my ($cdom,$cnum);
                   3859:         if ((defined($env{'course.'.$course.'.domain'})) && (defined($env{'course.'.$course.'.num'}))) {
                   3860:             $cdom = $env{'course.'.$course.'.domain'};
                   3861:             $cnum = $env{'course.'.$course.'.num'};
                   3862:         } else {
1.490     raeburn  3863:             ($cdom,$cnum) = split(/_/,$course); 
1.482     raeburn  3864:         }
                   3865:         my $no_ownblock = 0;
                   3866:         my $no_userblock = 0;
1.533     raeburn  3867:         if ($otheruser && $activity ne 'com') {
1.490     raeburn  3868:             # Check if current user has 'evb' priv for this
                   3869:             if (defined($own_courses{$course})) {
                   3870:                 foreach my $sec (keys(%{$own_courses{$course}})) {
                   3871:                     my $checkrole = 'cm./'.$cdom.'/'.$cnum;
                   3872:                     if ($sec ne 'none') {
                   3873:                         $checkrole .= '/'.$sec;
                   3874:                     }
                   3875:                     if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   3876:                         $no_ownblock = 1;
                   3877:                         last;
                   3878:                     }
                   3879:                 }
                   3880:             }
                   3881:             # if they have 'evb' priv and are currently not playing student
                   3882:             next if (($no_ownblock) &&
                   3883:                  ($env{'request.role'} !~ m{^st\./$cdom/$cnum}));
                   3884:         }
1.474     raeburn  3885:         foreach my $sec (keys(%{$live_courses{$course}})) {
1.482     raeburn  3886:             my $checkrole = 'cm./'.$cdom.'/'.$cnum;
1.474     raeburn  3887:             if ($sec ne 'none') {
1.482     raeburn  3888:                 $checkrole .= '/'.$sec;
1.474     raeburn  3889:             }
1.490     raeburn  3890:             if ($otheruser) {
                   3891:                 # Resource belongs to user other than current user.
                   3892:                 # Assemble privs for that user, and check for 'evb' priv.
1.482     raeburn  3893:                 my ($trole,$tdom,$tnum,$tsec);
                   3894:                 my $entry = $live_courses{$course}{$sec};
                   3895:                 if ($entry =~ /^cr/) {
                   3896:                     ($trole,$tdom,$tnum,$tsec) = 
                   3897:                       ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|);
                   3898:                 } else {
                   3899:                     ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry);
                   3900:                 }
                   3901:                 my ($spec,$area,$trest,%allroles,%userroles);
                   3902:                 $area = '/'.$tdom.'/'.$tnum;
                   3903:                 $trest = $tnum;
                   3904:                 if ($tsec ne '') {
                   3905:                     $area .= '/'.$tsec;
                   3906:                     $trest .= '/'.$tsec;
                   3907:                 }
                   3908:                 $spec = $trole.'.'.$area;
                   3909:                 if ($trole =~ /^cr/) {
                   3910:                     &Apache::lonnet::custom_roleprivs(\%allroles,$trole,
                   3911:                                                       $tdom,$spec,$trest,$area);
                   3912:                 } else {
                   3913:                     &Apache::lonnet::standard_roleprivs(\%allroles,$trole,
                   3914:                                                        $tdom,$spec,$trest,$area);
                   3915:                 }
                   3916:                 my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles);
1.486     raeburn  3917:                 if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) {
                   3918:                     if ($1) {
                   3919:                         $no_userblock = 1;
                   3920:                         last;
                   3921:                     }
                   3922:                 }
1.490     raeburn  3923:             } else {
                   3924:                 # Resource belongs to current user
                   3925:                 # Check for 'evb' priv via lonnet::allowed().
1.482     raeburn  3926:                 if (&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) {
                   3927:                     $no_ownblock = 1;
                   3928:                     last;
                   3929:                 }
1.474     raeburn  3930:             }
                   3931:         }
                   3932:         # if they have the evb priv and are currently not playing student
1.482     raeburn  3933:         next if (($no_ownblock) &&
1.491     albertel 3934:                  ($env{'request.role'} !~ m{^st\./\Q$cdom\E/\Q$cnum\E}));
1.482     raeburn  3935:         next if ($no_userblock);
1.474     raeburn  3936: 
1.490     raeburn  3937:         # Retrieve blocking times and identity of blocker for course
                   3938:         # of specified user, unless user has 'evb' privilege.
1.502     raeburn  3939:         
                   3940:         my ($start,$end)=&get_blocks($setters,$activity,$cdom,$cnum);
                   3941:         if (($start != 0) && 
                   3942:             (($startblock == 0) || ($startblock > $start))) {
                   3943:             $startblock = $start;
                   3944:         }
                   3945:         if (($end != 0)  &&
                   3946:             (($endblock == 0) || ($endblock < $end))) {
                   3947:             $endblock = $end;
                   3948:         }
1.490     raeburn  3949:     }
                   3950:     return ($startblock,$endblock);
                   3951: }
                   3952: 
                   3953: sub get_blocks {
                   3954:     my ($setters,$activity,$cdom,$cnum) = @_;
                   3955:     my $startblock = 0;
                   3956:     my $endblock = 0;
                   3957:     my $course = $cdom.'_'.$cnum;
                   3958:     $setters->{$course} = {};
                   3959:     $setters->{$course}{'staff'} = [];
                   3960:     $setters->{$course}{'times'} = [];
                   3961:     my %records = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   3962:     foreach my $record (keys(%records)) {
                   3963:         my ($start,$end) = ($record =~ m/^(\d+)____(\d+)$/);
                   3964:         if ($start <= time && $end >= time) {
                   3965:             my ($staff_name,$staff_dom,$title,$blocks) =
                   3966:                 &parse_block_record($records{$record});
                   3967:             if ($blocks->{$activity} eq 'on') {
                   3968:                 push(@{$$setters{$course}{'staff'}},[$staff_name,$staff_dom]);
                   3969:                 push(@{$$setters{$course}{'times'}}, [$start,$end]);
1.491     albertel 3970:                 if ( ($startblock == 0) || ($startblock > $start) ) {
                   3971:                     $startblock = $start;
1.490     raeburn  3972:                 }
1.491     albertel 3973:                 if ( ($endblock == 0) || ($endblock < $end) ) {
                   3974:                     $endblock = $end;
1.474     raeburn  3975:                 }
                   3976:             }
                   3977:         }
                   3978:     }
                   3979:     return ($startblock,$endblock);
                   3980: }
                   3981: 
                   3982: sub parse_block_record {
                   3983:     my ($record) = @_;
                   3984:     my ($setuname,$setudom,$title,$blocks);
                   3985:     if (ref($record) eq 'HASH') {
                   3986:         ($setuname,$setudom) = split(/:/,$record->{'setter'});
                   3987:         $title = &unescape($record->{'event'});
                   3988:         $blocks = $record->{'blocks'};
                   3989:     } else {
                   3990:         my @data = split(/:/,$record,3);
                   3991:         if (scalar(@data) eq 2) {
                   3992:             $title = $data[1];
                   3993:             ($setuname,$setudom) = split(/@/,$data[0]);
                   3994:         } else {
                   3995:             ($setuname,$setudom,$title) = @data;
                   3996:         }
                   3997:         $blocks = { 'com' => 'on' };
                   3998:     }
                   3999:     return ($setuname,$setudom,$title,$blocks);
                   4000: }
                   4001: 
                   4002: sub build_block_table {
                   4003:     my ($startblock,$endblock,$setters) = @_;
                   4004:     my %lt = &Apache::lonlocal::texthash(
                   4005:         'cacb' => 'Currently active communication blocks',
                   4006:         'cour' => 'Course',
                   4007:         'dura' => 'Duration',
                   4008:         'blse' => 'Block set by'
                   4009:     );
                   4010:     my $output;
1.476     raeburn  4011:     $output = '<br />'.$lt{'cacb'}.':<br />';
1.474     raeburn  4012:     $output .= &start_data_table();
                   4013:     $output .= '
                   4014: <tr>
                   4015:  <th>'.$lt{'cour'}.'</th>
                   4016:  <th>'.$lt{'dura'}.'</th>
                   4017:  <th>'.$lt{'blse'}.'</th>
                   4018: </tr>
                   4019: ';
                   4020:     foreach my $course (keys(%{$setters})) {
                   4021:         my %courseinfo=&Apache::lonnet::coursedescription($course);
                   4022:         for (my $i=0; $i<@{$$setters{$course}{staff}}; $i++) {
                   4023:             my ($uname,$udom) = @{$$setters{$course}{staff}[$i]};
1.490     raeburn  4024:             my $fullname = &plainname($uname,$udom);
                   4025:             if (defined($env{'user.name'}) && defined($env{'user.domain'})
                   4026:                 && $env{'user.name'} ne 'public' 
                   4027:                 && $env{'user.domain'} ne 'public') {
                   4028:                 $fullname = &aboutmewrapper($fullname,$uname,$udom);
                   4029:             }
1.474     raeburn  4030:             my ($openblock,$closeblock) = @{$$setters{$course}{times}[$i]};
                   4031:             $openblock = &Apache::lonlocal::locallocaltime($openblock);
                   4032:             $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
                   4033:             $output .= &Apache::loncommon::start_data_table_row().
                   4034:                        '<td>'.$courseinfo{'description'}.'</td>'.
                   4035:                        '<td>'.$openblock.' to '.$closeblock.'</td>'.
1.490     raeburn  4036:                        '<td>'.$fullname.'</td>'.
1.474     raeburn  4037:                         &Apache::loncommon::end_data_table_row();
                   4038:         }
                   4039:     }
                   4040:     $output .= &end_data_table();
                   4041: }
                   4042: 
1.490     raeburn  4043: sub blocking_status {
                   4044:     my ($activity,$uname,$udom) = @_;
                   4045:     my %setters;
                   4046:     my ($blocked,$output,$ownitem,$is_course);
                   4047:     my ($startblock,$endblock)=&blockcheck(\%setters,$activity,$uname,$udom);
                   4048:     if ($startblock && $endblock) {
                   4049:         $blocked = 1;
                   4050:         if (wantarray) {
                   4051:             my $category;
                   4052:             if ($activity eq 'boards') {
                   4053:                 $category = 'Discussion posts in this course';
                   4054:             } elsif ($activity eq 'blogs') {
                   4055:                 $category = 'Blogs';
                   4056:             } elsif ($activity eq 'port') {
                   4057:                 if (defined($uname) && defined($udom)) {
                   4058:                     if ($uname eq $env{'user.name'} &&
                   4059:                         $udom eq $env{'user.domain'}) {
                   4060:                         $ownitem = 1;
                   4061:                     }
                   4062:                 }
                   4063:                 $is_course = &Apache::lonnet::is_course($udom,$uname);
                   4064:                 if ($ownitem) { 
                   4065:                     $category = 'Your portfolio files';  
                   4066:                 } elsif ($is_course) {
                   4067:                     my $coursedesc;
                   4068:                     foreach my $course (keys(%setters)) {
                   4069:                         my %courseinfo =
                   4070:                              &Apache::lonnet::coursedescription($course);
                   4071:                         $coursedesc = $courseinfo{'description'};
                   4072:                     }
1.692.4.2  raeburn  4073:                     $category = "Group portfolio files in the course '$coursedesc'";
1.490     raeburn  4074:                 } else {
                   4075:                     $category = 'Portfolio files belonging to ';
                   4076:                     if ($env{'user.name'} eq 'public' && 
                   4077:                         $env{'user.domain'} eq 'public') {
                   4078:                         $category .= &plainname($uname,$udom);
                   4079:                     } else {
                   4080:                         $category .= &aboutmewrapper(&plainname($uname,$udom),$uname,$udom);  
                   4081:                     }
                   4082:                 }
                   4083:             } elsif ($activity eq 'groups') {
                   4084:                 $category = 'Groups in this course';
                   4085:             }
                   4086:             my $showstart = &Apache::lonlocal::locallocaltime($startblock);
                   4087:             my $showend = &Apache::lonlocal::locallocaltime($endblock);
                   4088:             $output = '<br />'.&mt('[_1] will be inaccessible between [_2] and [_3] because communication is being blocked.',$category,$showstart,$showend).'<br />';
                   4089:             if (!($activity eq 'port' && !($ownitem) && !($is_course))) { 
                   4090:                 $output .= &build_block_table($startblock,$endblock,\%setters);
                   4091:             }
                   4092:         }
                   4093:     }
                   4094:     if (wantarray) {
                   4095:         return ($blocked,$output);
                   4096:     } else {
                   4097:         return $blocked;
                   4098:     }
                   4099: }
                   4100: 
1.60      matthew  4101: ###############################################
                   4102: 
1.682     raeburn  4103: sub check_ip_acc {
                   4104:     my ($acc)=@_;
                   4105:     &Apache::lonxml::debug("acc is $acc");
                   4106:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
                   4107:         return 1;
                   4108:     }
                   4109:     my $allowed=0;
                   4110:     my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
                   4111: 
                   4112:     my $name;
                   4113:     foreach my $pattern (split(',',$acc)) {
                   4114:         $pattern =~ s/^\s*//;
                   4115:         $pattern =~ s/\s*$//;
                   4116:         if ($pattern =~ /\*$/) {
                   4117:             #35.8.*
                   4118:             $pattern=~s/\*//;
                   4119:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4120:         } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
                   4121:             #35.8.3.[34-56]
                   4122:             my $low=$2;
                   4123:             my $high=$3;
                   4124:             $pattern=$1;
                   4125:             if ($ip =~ /^\Q$pattern\E/) {
                   4126:                 my $last=(split(/\./,$ip))[3];
                   4127:                 if ($last <=$high && $last >=$low) { $allowed=1; }
                   4128:             }
                   4129:         } elsif ($pattern =~ /^\*/) {
                   4130:             #*.msu.edu
                   4131:             $pattern=~s/\*//;
                   4132:             if (!defined($name)) {
                   4133:                 use Socket;
                   4134:                 my $netaddr=inet_aton($ip);
                   4135:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4136:             }
                   4137:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4138:         } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                   4139:             #127.0.0.1
                   4140:             if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                   4141:         } else {
                   4142:             #some.name.com
                   4143:             if (!defined($name)) {
                   4144:                 use Socket;
                   4145:                 my $netaddr=inet_aton($ip);
                   4146:                 ($name)=gethostbyaddr($netaddr,AF_INET);
                   4147:             }
                   4148:             if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                   4149:         }
                   4150:         if ($allowed) { last; }
                   4151:     }
                   4152:     return $allowed;
                   4153: }
                   4154: 
                   4155: ###############################################
                   4156: 
1.60      matthew  4157: =pod
                   4158: 
1.112     bowersj2 4159: =head1 Domain Template Functions
                   4160: 
                   4161: =over 4
                   4162: 
                   4163: =item * &determinedomain()
1.60      matthew  4164: 
                   4165: Inputs: $domain (usually will be undef)
                   4166: 
1.63      www      4167: Returns: Determines which domain should be used for designs
1.60      matthew  4168: 
                   4169: =cut
1.54      www      4170: 
1.60      matthew  4171: ###############################################
1.63      www      4172: sub determinedomain {
                   4173:     my $domain=shift;
1.531     albertel 4174:     if (! $domain) {
1.60      matthew  4175:         # Determine domain if we have not been given one
1.692.4.18  raeburn  4176:         $domain = &Apache::lonnet::default_login_domain();
1.258     albertel 4177:         if ($env{'user.domain'}) { $domain=$env{'user.domain'}; }
                   4178:         if ($env{'request.role.domain'}) { 
                   4179:             $domain=$env{'request.role.domain'}; 
1.60      matthew  4180:         }
                   4181:     }
1.63      www      4182:     return $domain;
                   4183: }
                   4184: ###############################################
1.517     raeburn  4185: 
1.518     albertel 4186: sub devalidate_domconfig_cache {
                   4187:     my ($udom)=@_;
                   4188:     &Apache::lonnet::devalidate_cache_new('domainconfig',$udom);
                   4189: }
                   4190: 
                   4191: # ---------------------- Get domain configuration for a domain
                   4192: sub get_domainconf {
                   4193:     my ($udom) = @_;
                   4194:     my $cachetime=1800;
                   4195:     my ($result,$cached)=&Apache::lonnet::is_cached_new('domainconfig',$udom);
                   4196:     if (defined($cached)) { return %{$result}; }
                   4197: 
                   4198:     my %domconfig = &Apache::lonnet::get_dom('configuration',
1.692.4.31  raeburn  4199: 					     ['login','rolecolors','loginvia'],$udom);
1.632     raeburn  4200:     my (%designhash,%legacy);
1.518     albertel 4201:     if (keys(%domconfig) > 0) {
                   4202:         if (ref($domconfig{'login'}) eq 'HASH') {
1.632     raeburn  4203:             if (keys(%{$domconfig{'login'}})) {
                   4204:                 foreach my $key (keys(%{$domconfig{'login'}})) {
1.692.4.2  raeburn  4205:                     if (ref($domconfig{'login'}{$key}) eq 'HASH') {
1.692.4.31  raeburn  4206:                         if ($key eq 'loginvia') {
                   4207:                             if (ref($domconfig{'login'}{'loginvia'}) eq 'HASH') {
                   4208:                                 my @ids = &Apache::lonnet::current_machine_ids();
                   4209:                                 foreach my $hostname (@ids) {
                   4210:                                     if (ref($domconfig{'login'}{'loginvia'}{$hostname}) eq 'HASH') {
                   4211:                                         if ($domconfig{'login'}{'loginvia'}{$hostname}{'server'}) {
                   4212:                                             my $server = $domconfig{'login'}{'loginvia'}{$hostname}{'server'};
                   4213:                                             $designhash{$udom.'.login.loginvia'} = $server;
                   4214:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'} eq 'custom') {
                   4215: 
                   4216:                                                 $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'custompath'};
                   4217:                                             } else {
                   4218:                                                  $designhash{$udom.'.login.loginvia_'.$hostname} = $server.':'.$domconfig{'login'}{'loginvia'}{$hostname}{'serverpath'};
                   4219:                                             }
                   4220:                                             if ($domconfig{'login'}{'loginvia'}{$hostname}{'exempt'}) {
                   4221:                                                 $designhash{$udom.'.login.loginvia_exempt_'.$hostname} = $domconfig{'login'}{'loginvia'}{$hostname}{'exempt'};
                   4222:                                             }
                   4223:                                         }
                   4224:                                     }
                   4225:                                 }
                   4226:                             }
                   4227:                         } else {
                   4228:                             foreach my $img (keys(%{$domconfig{'login'}{$key}})) {
                   4229:                                 $designhash{$udom.'.login.'.$key.'_'.$img} =
                   4230:                                     $domconfig{'login'}{$key}{$img};
                   4231:                             }
1.692.4.2  raeburn  4232:                         }
                   4233:                     } else {
                   4234:                         $designhash{$udom.'.login.'.$key}=$domconfig{'login'}{$key};
                   4235:                     }
1.632     raeburn  4236:                 }
                   4237:             } else {
                   4238:                 $legacy{'login'} = 1;
1.518     albertel 4239:             }
1.632     raeburn  4240:         } else {
                   4241:             $legacy{'login'} = 1;
1.518     albertel 4242:         }
                   4243:         if (ref($domconfig{'rolecolors'}) eq 'HASH') {
1.632     raeburn  4244:             if (keys(%{$domconfig{'rolecolors'}})) {
                   4245:                 foreach my $role (keys(%{$domconfig{'rolecolors'}})) {
                   4246:                     if (ref($domconfig{'rolecolors'}{$role}) eq 'HASH') {
                   4247:                         foreach my $item (keys(%{$domconfig{'rolecolors'}{$role}})) {
                   4248:                             $designhash{$udom.'.'.$role.'.'.$item}=$domconfig{'rolecolors'}{$role}{$item};
                   4249:                         }
1.518     albertel 4250:                     }
                   4251:                 }
1.632     raeburn  4252:             } else {
                   4253:                 $legacy{'rolecolors'} = 1;
1.518     albertel 4254:             }
1.632     raeburn  4255:         } else {
                   4256:             $legacy{'rolecolors'} = 1;
1.518     albertel 4257:         }
1.692.4.32  raeburn  4258:         if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4259:             if ($domconfig{'autoenroll'}{'co-owners'}) {
                   4260:                 $designhash{$udom.'.autoassign.co-owners'}=$domconfig{'autoenroll'}{'co-owners'};
                   4261:             }
                   4262:         }
1.632     raeburn  4263:         if (keys(%legacy) > 0) {
                   4264:             my %legacyhash = &get_legacy_domconf($udom);
                   4265:             foreach my $item (keys(%legacyhash)) {
                   4266:                 if ($item =~ /^\Q$udom\E\.login/) {
                   4267:                     if ($legacy{'login'}) { 
                   4268:                         $designhash{$item} = $legacyhash{$item};
                   4269:                     }
                   4270:                 } else {
                   4271:                     if ($legacy{'rolecolors'}) {
                   4272:                         $designhash{$item} = $legacyhash{$item};
                   4273:                     }
1.518     albertel 4274:                 }
                   4275:             }
                   4276:         }
1.632     raeburn  4277:     } else {
                   4278:         %designhash = &get_legacy_domconf($udom); 
1.518     albertel 4279:     }
                   4280:     &Apache::lonnet::do_cache_new('domainconfig',$udom,\%designhash,
                   4281: 				  $cachetime);
                   4282:     return %designhash;
                   4283: }
                   4284: 
1.632     raeburn  4285: sub get_legacy_domconf {
                   4286:     my ($udom) = @_;
                   4287:     my %legacyhash;
                   4288:     my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
                   4289:     my $designfile =  $designdir.'/'.$udom.'.tab';
                   4290:     if (-e $designfile) {
                   4291:         if ( open (my $fh,"<$designfile") ) {
                   4292:             while (my $line = <$fh>) {
                   4293:                 next if ($line =~ /^\#/);
                   4294:                 chomp($line);
                   4295:                 my ($key,$val)=(split(/\=/,$line));
                   4296:                 if ($val) { $legacyhash{$udom.'.'.$key}=$val; }
                   4297:             }
                   4298:             close($fh);
                   4299:         }
                   4300:     }
                   4301:     if (-e '/home/httpd/html/adm/lonDomLogos/'.$udom.'.gif') {
                   4302:         $legacyhash{$udom.'.login.domlogo'} = "/adm/lonDomLogos/$udom.gif";
                   4303:     }
                   4304:     return %legacyhash;
                   4305: }
                   4306: 
1.63      www      4307: =pod
                   4308: 
1.112     bowersj2 4309: =item * &domainlogo()
1.63      www      4310: 
                   4311: Inputs: $domain (usually will be undef)
                   4312: 
                   4313: Returns: A link to a domain logo, if the domain logo exists.
                   4314: If the domain logo does not exist, a description of the domain.
                   4315: 
                   4316: =cut
1.112     bowersj2 4317: 
1.63      www      4318: ###############################################
                   4319: sub domainlogo {
1.517     raeburn  4320:     my $domain = &determinedomain(shift);
1.518     albertel 4321:     my %designhash = &get_domainconf($domain);    
1.517     raeburn  4322:     # See if there is a logo
                   4323:     if ($designhash{$domain.'.login.domlogo'} ne '') {
1.519     raeburn  4324:         my $imgsrc = $designhash{$domain.'.login.domlogo'};
1.538     albertel 4325:         if ($imgsrc =~ m{^/(adm|res)/}) {
                   4326: 	    if ($imgsrc =~ m{^/res/}) {
                   4327: 		my $local_name = &Apache::lonnet::filelocation('',$imgsrc);
                   4328: 		&Apache::lonnet::repcopy($local_name);
                   4329: 	    }
                   4330: 	   $imgsrc = &lonhttpdurl($imgsrc);
1.519     raeburn  4331:         } 
                   4332:         return '<img src="'.$imgsrc.'" alt="'.$domain.'" />';
1.514     albertel 4333:     } elsif (defined(&Apache::lonnet::domain($domain,'description'))) {
                   4334:         return &Apache::lonnet::domain($domain,'description');
1.59      www      4335:     } else {
1.60      matthew  4336:         return '';
1.59      www      4337:     }
                   4338: }
1.63      www      4339: ##############################################
                   4340: 
                   4341: =pod
                   4342: 
1.112     bowersj2 4343: =item * &designparm()
1.63      www      4344: 
                   4345: Inputs: $which parameter; $domain (usually will be undef)
                   4346: 
                   4347: Returns: value of designparamter $which
                   4348: 
                   4349: =cut
1.112     bowersj2 4350: 
1.397     albertel 4351: 
1.400     albertel 4352: ##############################################
1.397     albertel 4353: sub designparm {
                   4354:     my ($which,$domain)=@_;
1.258     albertel 4355:     if ($env{'browser.blackwhite'} eq 'on') {
1.635     raeburn  4356: 	if ($which=~/\.(font|alink|vlink|link|textcol)$/) {
1.110     www      4357: 	    return '#000000';
                   4358: 	}
1.635     raeburn  4359: 	if ($which=~/\.(pgbg|sidebg|bgcol)$/) {
1.110     www      4360: 	    return '#FFFFFF';
                   4361: 	}
                   4362: 	if ($which=~/\.tabbg$/) {
                   4363: 	    return '#CCCCCC';
                   4364: 	}
                   4365:     }
1.397     albertel 4366:     if (exists($env{'environment.color.'.$which})) {
1.258     albertel 4367: 	return $env{'environment.color.'.$which};
1.96      www      4368:     }
1.63      www      4369:     $domain=&determinedomain($domain);
1.518     albertel 4370:     my %domdesign = &get_domainconf($domain);
1.520     raeburn  4371:     my $output;
1.517     raeburn  4372:     if ($domdesign{$domain.'.'.$which} ne '') {
1.520     raeburn  4373: 	$output = $domdesign{$domain.'.'.$which};
1.63      www      4374:     } else {
1.520     raeburn  4375:         $output = $defaultdesign{$which};
                   4376:     }
                   4377:     if (($which =~ /^(student|coordinator|author|admin)\.img$/) ||
1.635     raeburn  4378:         ($which =~ /login\.(img|logo|domlogo|login)/)) {
1.538     albertel 4379:         if ($output =~ m{^/(adm|res)/}) {
                   4380: 	    if ($output =~ m{^/res/}) {
                   4381: 		my $local_name = &Apache::lonnet::filelocation('',$output);
                   4382: 		&Apache::lonnet::repcopy($local_name);
                   4383: 	    }
1.520     raeburn  4384:             $output = &lonhttpdurl($output);
                   4385:         }
1.63      www      4386:     }
1.520     raeburn  4387:     return $output;
1.63      www      4388: }
1.59      www      4389: 
1.60      matthew  4390: ###############################################
                   4391: ###############################################
                   4392: 
                   4393: =pod
                   4394: 
1.112     bowersj2 4395: =back
                   4396: 
1.549     albertel 4397: =head1 HTML Helpers
1.112     bowersj2 4398: 
                   4399: =over 4
                   4400: 
                   4401: =item * &bodytag()
1.60      matthew  4402: 
                   4403: Returns a uniform header for LON-CAPA web pages.
                   4404: 
                   4405: Inputs: 
                   4406: 
1.112     bowersj2 4407: =over 4
                   4408: 
                   4409: =item * $title, A title to be displayed on the page.
                   4410: 
                   4411: =item * $function, the current role (can be undef).
                   4412: 
                   4413: =item * $addentries, extra parameters for the <body> tag.
                   4414: 
                   4415: =item * $bodyonly, if defined, only return the <body> tag.
                   4416: 
                   4417: =item * $domain, if defined, force a given domain.
                   4418: 
                   4419: =item * $forcereg, if page should register as content page (relevant for 
1.86      www      4420:             text interface only)
1.60      matthew  4421: 
1.326     albertel 4422: =item * $customtitle, alternate text to use instead of $title
                   4423:                       in the title box that appears, this text
                   4424:                       is not auto translated like the $title is
1.309     albertel 4425: 
                   4426: =item * $notopbar, if true, keep the 'what is this' info but remove the
                   4427:                    navigational links
1.317     albertel 4428: 
1.338     albertel 4429: =item * $bgcolor, used to override the bgcolor on a webpage to a specific value
                   4430: 
                   4431: =item * $notitle, if true keep the nav controls, but remove the title bar
                   4432: 
1.361     albertel 4433: =item * $no_inline_link, if true and in remote mode, don't show the 
                   4434:          'Switch To Inline Menu' link
                   4435: 
1.460     albertel 4436: =item * $args, optional argument valid values are
                   4437:             no_auto_mt_title -> prevents &mt()ing the title arg
1.562     albertel 4438:             inherit_jsmath -> when creating popup window in a page,
                   4439:                               should it have jsmath forced on by the
                   4440:                               current page
1.460     albertel 4441: 
1.112     bowersj2 4442: =back
                   4443: 
1.60      matthew  4444: Returns: A uniform header for LON-CAPA web pages.  
                   4445: If $bodyonly is nonzero, a string containing a <body> tag will be returned.
                   4446: If $bodyonly is undef or zero, an html string containing a <body> tag and 
                   4447: other decorations will be returned.
                   4448: 
                   4449: =cut
                   4450: 
1.54      www      4451: sub bodytag {
1.309     albertel 4452:     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
1.460     albertel 4453: 	$notopbar,$bgcolor,$notitle,$no_inline_link,$args)=@_;
1.339     albertel 4454: 
1.460     albertel 4455:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
1.339     albertel 4456: 
1.183     matthew  4457:     $function = &get_users_function() if (!$function);
1.339     albertel 4458:     my $img =    &designparm($function.'.img',$domain);
                   4459:     my $font =   &designparm($function.'.font',$domain);
                   4460:     my $pgbg   = $bgcolor || &designparm($function.'.pgbg',$domain);
                   4461: 
1.692.4.2  raeburn  4462:     my %design = ( 'style'   => 'margin-top: 0',
1.535     albertel 4463: 		   'bgcolor' => $pgbg,
1.339     albertel 4464: 		   'text'    => $font,
                   4465:                    'alink'   => &designparm($function.'.alink',$domain),
                   4466: 		   'vlink'   => &designparm($function.'.vlink',$domain),
                   4467: 		   'link'    => &designparm($function.'.link',$domain),);
1.438     albertel 4468:     @design{keys(%$addentries)} = @$addentries{keys(%$addentries)}; 
1.339     albertel 4469: 
1.63      www      4470:  # role and realm
1.378     raeburn  4471:     my ($role,$realm) = split(/\./,$env{'request.role'},2);
                   4472:     if ($role  eq 'ca') {
1.479     albertel 4473:         my ($rdom,$rname) = ($realm =~ m{^/($match_domain)/($match_username)$});
1.500     albertel 4474:         $realm = &plainname($rname,$rdom);
1.378     raeburn  4475:     } 
1.55      www      4476: # realm
1.258     albertel 4477:     if ($env{'request.course.id'}) {
1.378     raeburn  4478:         if ($env{'request.role'} !~ /^cr/) {
                   4479:             $role = &Apache::lonnet::plaintext($role,&course_type());
                   4480:         }
1.359     albertel 4481: 	$realm = $env{'course.'.$env{'request.course.id'}.'.description'};
1.378     raeburn  4482:     } else {
                   4483:         $role = &Apache::lonnet::plaintext($role);
1.54      www      4484:     }
1.433     albertel 4485: 
1.359     albertel 4486:     if (!$realm) { $realm='&nbsp;'; }
1.55      www      4487: # Set messages
1.60      matthew  4488:     my $messages=&domainlogo($domain);
1.330     albertel 4489: 
1.438     albertel 4490:     my $extra_body_attr = &make_attr_string($forcereg,\%design);
1.329     albertel 4491: 
1.101     www      4492: # construct main body tag
1.359     albertel 4493:     my $bodytag = "<body $extra_body_attr>".
1.562     albertel 4494: 	&Apache::lontexconvert::init_math_support($args->{'inherit_jsmath'});
1.252     albertel 4495: 
1.530     albertel 4496:     if ($bodyonly) {
1.60      matthew  4497:         return $bodytag;
1.258     albertel 4498:     } elsif ($env{'browser.interface'} eq 'textual') {
1.95      www      4499: # Accessibility
1.224     raeburn  4500:           
1.337     albertel 4501: 	$bodytag.=&Apache::lonmenu::menubuttons($forcereg,$forcereg);
1.338     albertel 4502: 	if (!$notitle) {
1.337     albertel 4503: 	    $bodytag.='<h1>LON-CAPA: '.$title.'</h1>';
                   4504: 	}
                   4505: 	return $bodytag;
1.359     albertel 4506:     }
                   4507: 
1.410     albertel 4508:     my $name = &plainname($env{'user.name'},$env{'user.domain'});
1.433     albertel 4509:     if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4510: 	undef($role);
1.434     albertel 4511:     } else {
                   4512: 	$name = &aboutmewrapper($name,$env{'user.name'},$env{'user.domain'});
1.433     albertel 4513:     }
1.359     albertel 4514:     
                   4515:     my $roleinfo=(<<ENDROLE);
                   4516: <td class="LC_title_bar_who">
                   4517: <div class="LC_title_bar_name">
1.410     albertel 4518:     $name
1.361     albertel 4519:     &nbsp;
1.359     albertel 4520: </div>
                   4521: <div class="LC_title_bar_role">
1.361     albertel 4522: $role&nbsp;
1.359     albertel 4523: </div>
                   4524: <div class="LC_title_bar_realm">
1.361     albertel 4525: $realm&nbsp;
1.359     albertel 4526: </div>
1.206     albertel 4527: </td>
                   4528: ENDROLE
1.235     raeburn  4529: 
1.359     albertel 4530:     my $titleinfo = '<span class="LC_title_bar_title">'.$title.'</span>';
                   4531:     if ($customtitle) {
                   4532:         $titleinfo = $customtitle;
                   4533:     }
                   4534:     #
                   4535:     # Extra info if you are the DC
                   4536:     my $dc_info = '';
                   4537:     if ($env{'user.adv'} && exists($env{'user.role.dc./'.
                   4538:                         $env{'course.'.$env{'request.course.id'}.
                   4539:                                  '.domain'}.'/'})) {
                   4540:         my $cid = $env{'request.course.id'};
                   4541:         $dc_info.= $cid.' '.$env{'course.'.$cid.'.internal.coursecode'};
1.380     www      4542:         $dc_info =~ s/\s+$//;
1.359     albertel 4543:         $dc_info = '('.$dc_info.')';
                   4544:     }
                   4545: 
1.644     www      4546:     if (($env{'environment.remote'} eq 'off') || ($args->{'suppress_header_logos'})) {
1.359     albertel 4547:         # No Remote
1.258     albertel 4548: 	if ($env{'request.state'} eq 'construct') {
1.359     albertel 4549: 	    $forcereg=1;
                   4550: 	}
                   4551: 
                   4552: 	if (!$customtitle && $env{'request.state'} eq 'construct') {
                   4553: 	    # this is for resources; directories have customtitle, and crumbs
                   4554:             # and select recent are created in lonpubdir.pm  
1.229     albertel 4555: 	    my ($uname,$thisdisfn)=
1.258     albertel 4556: 		($env{'request.filename'} =~ m|^/home/([^/]+)/public_html/(.*)|);
1.229     albertel 4557: 	    my $formaction='/priv/'.$uname.'/'.$thisdisfn;
                   4558: 	    $formaction=~s/\/+/\//g;
                   4559: 
1.359     albertel 4560: 	    my $parentpath = '';
                   4561: 	    my $lastitem = '';
                   4562: 	    if ($thisdisfn =~ m-(.+/)([^/]*)$-) {
                   4563: 		$parentpath = $1;
                   4564: 		$lastitem = $2;
                   4565: 	    } else {
                   4566: 		$lastitem = $thisdisfn;
                   4567: 	    }
                   4568: 	    $titleinfo = 
1.640     bisitz   4569: 		&Apache::loncommon::help_open_menu('','',3,'Authoring')
                   4570: 		.'<b>'.&mt('Construction Space').'</b>:&nbsp;'
                   4571: 		.'<form name="dirs" method="post" action="'.$formaction
1.359     albertel 4572: 		.'" target="_top"><tt><b>'
                   4573: 		.&Apache::lonhtmlcommon::crumbs($uname.'/'.$parentpath,'_top','/priv','','+1',1)."<font size=\"+1\">$lastitem</font></b></tt><br />"
                   4574: 		.&Apache::lonhtmlcommon::select_recent('construct','recent','this.form.action=this.form.recent.value;this.form.submit()')
                   4575: 		.'</form>'
                   4576: 		.&Apache::lonmenu::constspaceform();
1.235     raeburn  4577:         }
1.359     albertel 4578: 
1.337     albertel 4579:         my $titletable;
1.338     albertel 4580: 	if (!$notitle) {
1.337     albertel 4581: 	    $titletable =
1.359     albertel 4582: 		'<table id="LC_title_bar">'.
                   4583:                          "<tr><td> $titleinfo $dc_info</td>".$roleinfo.
                   4584: 			 '</tr></table>';
1.337     albertel 4585: 	}
1.359     albertel 4586: 	if ($notopbar) {
                   4587: 	    $bodytag .= $titletable;
                   4588: 	} else {
                   4589: 	    if ($env{'request.state'} eq 'construct') {
1.337     albertel 4590:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg,
                   4591: 							  $titletable);
1.272     raeburn  4592:             } else {
1.336     albertel 4593:                 $bodytag .= &Apache::lonmenu::menubuttons($forcereg,$forcereg).
1.359     albertel 4594: 		    $titletable;
1.272     raeburn  4595:             }
1.235     raeburn  4596:         }
                   4597:         return $bodytag;
1.94      www      4598:     }
1.95      www      4599: 
1.93      www      4600: #
1.95      www      4601: # Top frame rendering, Remote is up
1.93      www      4602: #
1.359     albertel 4603: 
1.517     raeburn  4604:     my $imgsrc = $img;
                   4605:     if ($img =~ /^\/adm/) {
1.575     albertel 4606:         $imgsrc = &lonhttpdurl($img);
1.517     raeburn  4607:     }
                   4608:     my $upperleft='<img src="'.$imgsrc.'" alt="'.$function.'" />';
1.359     albertel 4609: 
1.305     www      4610:     # Explicit link to get inline menu
1.361     albertel 4611:     my $menu= ($no_inline_link?''
                   4612: 	       :'<br /><a href="/adm/remote?action=collapse">'.&mt('Switch to Inline Menu Mode').'</a>');
1.245     matthew  4613:     #
1.338     albertel 4614:     if ($notitle) {
1.337     albertel 4615: 	return $bodytag;
                   4616:     }
1.94      www      4617:     return(<<ENDBODY);
1.60      matthew  4618: $bodytag
1.359     albertel 4619: <table id="LC_title_bar" class="LC_with_remote">
1.368     albertel 4620: <tr><td class="LC_title_bar_role_logo">$upperleft</td>
1.359     albertel 4621:     <td class="LC_title_bar_domain_logo">$messages&nbsp;</td>
1.54      www      4622: </tr>
1.359     albertel 4623: <tr><td>$titleinfo $dc_info $menu</td>
                   4624: $roleinfo
1.368     albertel 4625: </tr>
1.356     albertel 4626: </table>
1.54      www      4627: ENDBODY
1.182     matthew  4628: }
                   4629: 
1.330     albertel 4630: sub make_attr_string {
                   4631:     my ($register,$attr_ref) = @_;
                   4632: 
                   4633:     if ($attr_ref && !ref($attr_ref)) {
                   4634: 	die("addentries Must be a hash ref ".
                   4635: 	    join(':',caller(1))." ".
                   4636: 	    join(':',caller(0))." ");
                   4637:     }
                   4638: 
                   4639:     if ($register) {
1.339     albertel 4640: 	my ($on_load,$on_unload);
                   4641: 	foreach my $key (keys(%{$attr_ref})) {
                   4642: 	    if      (lc($key) eq 'onload') {
                   4643: 		$on_load.=$attr_ref->{$key}.';';
                   4644: 		delete($attr_ref->{$key});
                   4645: 
                   4646: 	    } elsif (lc($key) eq 'onunload') {
                   4647: 		$on_unload.=$attr_ref->{$key}.';';
                   4648: 		delete($attr_ref->{$key});
                   4649: 	    }
                   4650: 	}
                   4651: 	$attr_ref->{'onload'}  =
                   4652: 	    &Apache::lonmenu::loadevents().  $on_load;
                   4653: 	$attr_ref->{'onunload'}=
                   4654: 	    &Apache::lonmenu::unloadevents().$on_unload;
                   4655:     }
                   4656: 
                   4657: # Accessibility font enhance
                   4658:     if ($env{'browser.fontenhance'} eq 'on') {
                   4659: 	my $style;
                   4660: 	foreach my $key (keys(%{$attr_ref})) {
                   4661: 	    if (lc($key) eq 'style') {
                   4662: 		$style.=$attr_ref->{$key}.';';
                   4663: 		delete($attr_ref->{$key});
                   4664: 	    }
                   4665: 	}
                   4666: 	$attr_ref->{'style'}=$style.'; font-size: x-large;';
1.330     albertel 4667:     }
1.339     albertel 4668: 
                   4669:     if ($env{'browser.blackwhite'} eq 'on') {
                   4670: 	delete($attr_ref->{'font'});
                   4671: 	delete($attr_ref->{'link'});
                   4672: 	delete($attr_ref->{'alink'});
                   4673: 	delete($attr_ref->{'vlink'});
                   4674: 	delete($attr_ref->{'bgcolor'});
                   4675: 	delete($attr_ref->{'background'});
                   4676:     }
                   4677: 
1.330     albertel 4678:     my $attr_string;
                   4679:     foreach my $attr (keys(%$attr_ref)) {
                   4680: 	$attr_string .= " $attr=\"".$attr_ref->{$attr}.'" ';
                   4681:     }
                   4682:     return $attr_string;
                   4683: }
                   4684: 
                   4685: 
1.182     matthew  4686: ###############################################
1.251     albertel 4687: ###############################################
                   4688: 
                   4689: =pod
                   4690: 
                   4691: =item * &endbodytag()
                   4692: 
                   4693: Returns a uniform footer for LON-CAPA web pages.
                   4694: 
1.635     raeburn  4695: Inputs: 1 - optional reference to an args hash
                   4696: If in the hash, key for noredirectlink has a value which evaluates to true,
                   4697: a 'Continue' link is not displayed if the page contains an
                   4698: internal redirect in the <head></head> section,
                   4699: i.e., $env{'internal.head.redirect'} exists   
1.251     albertel 4700: 
                   4701: =cut
                   4702: 
                   4703: sub endbodytag {
1.635     raeburn  4704:     my ($args) = @_;
1.251     albertel 4705:     my $endbodytag='</body>';
1.269     albertel 4706:     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
1.315     albertel 4707:     if ( exists( $env{'internal.head.redirect'} ) ) {
1.635     raeburn  4708:         if (!(ref($args) eq 'HASH' && $args->{'noredirectlink'})) {
                   4709: 	    $endbodytag=
                   4710: 	        "<br /><a href=\"$env{'internal.head.redirect'}\">".
                   4711: 	        &mt('Continue').'</a>'.
                   4712: 	        $endbodytag;
                   4713:         }
1.315     albertel 4714:     }
1.251     albertel 4715:     return $endbodytag;
                   4716: }
                   4717: 
1.352     albertel 4718: =pod
                   4719: 
                   4720: =item * &standard_css()
                   4721: 
                   4722: Returns a style sheet
                   4723: 
                   4724: Inputs: (all optional)
                   4725:             domain         -> force to color decorate a page for a specific
                   4726:                                domain
                   4727:             function       -> force usage of a specific rolish color scheme
                   4728:             bgcolor        -> override the default page bgcolor
                   4729: 
                   4730: =cut
                   4731: 
1.343     albertel 4732: sub standard_css {
1.345     albertel 4733:     my ($function,$domain,$bgcolor) = @_;
1.352     albertel 4734:     $function  = &get_users_function() if (!$function);
                   4735:     my $img    = &designparm($function.'.img',   $domain);
                   4736:     my $tabbg  = &designparm($function.'.tabbg', $domain);
                   4737:     my $font   = &designparm($function.'.font',  $domain);
1.345     albertel 4738:     my $sidebg = &designparm($function.'.sidebg',$domain);
1.382     albertel 4739:     my $pgbg_or_bgcolor =
                   4740: 	         $bgcolor ||
1.352     albertel 4741: 	         &designparm($function.'.pgbg',  $domain);
1.382     albertel 4742:     my $pgbg   = &designparm($function.'.pgbg',  $domain);
1.352     albertel 4743:     my $alink  = &designparm($function.'.alink', $domain);
                   4744:     my $vlink  = &designparm($function.'.vlink', $domain);
                   4745:     my $link   = &designparm($function.'.link',  $domain);
                   4746: 
1.602     albertel 4747:     my $sans                 = 'Verdana,Arial,Helvetica,sans-serif';
1.395     albertel 4748:     my $mono                 = 'monospace';
1.692.4.13  raeburn  4749:     my $data_table_head      = $tabbg;
1.692.4.6  raeburn  4750:     my $data_table_light     = '#FAFAFA';
                   4751:     my $data_table_dark      = '#F0F0F0';
1.470     banghart 4752:     my $data_table_darker    = '#CCCCCC';
1.349     albertel 4753:     my $data_table_highlight = '#FFFF00';
1.352     albertel 4754:     my $mail_new             = '#FFBB77';
                   4755:     my $mail_new_hover       = '#DD9955';
                   4756:     my $mail_read            = '#BBBB77';
                   4757:     my $mail_read_hover      = '#999944';
                   4758:     my $mail_replied         = '#AAAA88';
                   4759:     my $mail_replied_hover   = '#888855';
                   4760:     my $mail_other           = '#99BBBB';
                   4761:     my $mail_other_hover     = '#669999';
1.391     albertel 4762:     my $table_header         = '#DDDDDD';
1.489     raeburn  4763:     my $feedback_link_bg     = '#BBBBBB';
1.692.4.3  raeburn  4764:     my $lg_border_color      = '#C8C8C8';
1.392     albertel 4765: 
1.608     albertel 4766:     my $border = ($env{'browser.type'} eq 'explorer' ||
1.692.4.2  raeburn  4767: 		  $env{'browser.type'} eq 'safari'     ) ? '0 2px 0 2px'
                   4768: 	                                                 : '0 3px 0 4px';
1.448     albertel 4769: 
1.523     albertel 4770: 
1.343     albertel 4771:     return <<END;
1.345     albertel 4772: h1, h2, h3, th { font-family: $sans }
1.343     albertel 4773: a:focus { color: red; background: yellow } 
1.692.4.6  raeburn  4774: 
1.510     albertel 4775: table.thinborder,
1.523     albertel 4776: 
1.510     albertel 4777: table.thinborder tr th {
                   4778:   border-style: solid;
                   4779:   border-width: 1px;
                   4780:   background: $tabbg;
                   4781: }
1.523     albertel 4782: table.thinborder tr td {
1.510     albertel 4783:   border-style: solid;
                   4784:   border-width: 1px
                   4785: }
1.426     albertel 4786: 
1.343     albertel 4787: form, .inline { display: inline; }
                   4788: .center { text-align: center; }
1.593     albertel 4789: .LC_filename {font-family: $mono; white-space:pre;}
1.350     albertel 4790: .LC_error {
                   4791:   color: red;
                   4792:   font-size: larger;
                   4793: }
1.457     albertel 4794: .LC_warning,
                   4795: .LC_diff_removed {
1.394     albertel 4796:   color: red;
                   4797: }
1.532     albertel 4798: 
                   4799: .LC_info,
1.457     albertel 4800: .LC_success,
                   4801: .LC_diff_added {
1.350     albertel 4802:   color: green;
                   4803: }
1.692.4.2  raeburn  4804: 
                   4805: div.LC_confirm_box {
                   4806:   background-color: #FAFAFA;
                   4807:   border: 1px solid $lg_border_color;
                   4808:   margin-right: 0;
                   4809:   padding: 5px;
                   4810: }
                   4811: 
                   4812: div.LC_confirm_box .LC_error img,
                   4813: div.LC_confirm_box .LC_success img {
                   4814:   vertical-align: middle;
1.543     albertel 4815: }
                   4816: 
1.440     albertel 4817: .LC_icon {
1.692.4.2  raeburn  4818:   border: none;
1.440     albertel 4819: }
1.539     albertel 4820: .LC_indexer_icon {
1.692.4.2  raeburn  4821:   border: 0;
1.539     albertel 4822:   height: 22px;
                   4823: }
1.543     albertel 4824: .LC_docs_spacer {
                   4825:   width: 25px;
                   4826:   height: 1px;
1.692.4.2  raeburn  4827:   border: none;
1.543     albertel 4828: }
1.346     albertel 4829: 
1.532     albertel 4830: .LC_internal_info {
1.692.4.2  raeburn  4831:   color: #999999;
1.532     albertel 4832: }
                   4833: 
1.692.4.19  raeburn  4834: .LC_discussion {
                   4835:    background: $tabbg;
                   4836:    border: 1px solid black;
                   4837:    margin: 2px;
                   4838: }
                   4839: 
                   4840: .LC_disc_action_links_bar {
                   4841:    background: $tabbg;
                   4842:    border: none;
                   4843:    margin: 4px;
                   4844: }
                   4845: 
                   4846: .LC_disc_action_left {
                   4847:    text-align: left;
                   4848: }
                   4849: 
                   4850: .LC_disc_action_right {
                   4851:    text-align: right;
                   4852: }
                   4853: 
                   4854: .LC_disc_new_item {
                   4855:    background: white;
                   4856:    border: 2px solid red;
                   4857:    margin: 2px;
                   4858: }
                   4859: 
                   4860: .LC_disc_old_item {
                   4861:    background: white;
                   4862:    border: 1px solid black;
                   4863:    margin: 2px;
                   4864: }
                   4865: 
1.458     albertel 4866: table.LC_pastsubmission {
                   4867:   border: 1px solid black;
                   4868:   margin: 2px;
                   4869: }
                   4870: 
1.606     albertel 4871: table#LC_top_nav, table#LC_menubuttons,table#LC_nav_location {
1.345     albertel 4872:   width: 100%;
                   4873:   background: $pgbg;
1.392     albertel 4874:   border: 2px;
1.402     albertel 4875:   border-collapse: separate;
1.692.4.2  raeburn  4876:   padding: 0;
1.345     albertel 4877: }
1.392     albertel 4878: 
1.606     albertel 4879: table#LC_title_bar, table.LC_breadcrumbs, 
1.393     albertel 4880: table#LC_title_bar.LC_with_remote {
1.359     albertel 4881:   width: 100%;
1.392     albertel 4882:   border-color: $pgbg;
                   4883:   border-style: solid;
                   4884:   border-width: $border;
                   4885: 
1.379     albertel 4886:   background: $pgbg;
                   4887:   font-family: $sans;
1.392     albertel 4888:   border-collapse: collapse;
1.692.4.2  raeburn  4889:   padding: 0;
1.359     albertel 4890: }
1.392     albertel 4891: 
1.409     albertel 4892: table.LC_docs_path {
                   4893:   width: 100%;
                   4894:   border: 0;
                   4895:   background: $pgbg;
                   4896:   font-family: $sans;
                   4897:   border-collapse: collapse;
1.692.4.2  raeburn  4898:   padding: 0;
1.409     albertel 4899: }
                   4900: 
1.359     albertel 4901: table#LC_title_bar td {
                   4902:   background: $tabbg;
                   4903: }
                   4904: table#LC_title_bar td.LC_title_bar_who {
                   4905:   background: $tabbg;
                   4906:   color: $font;
1.427     albertel 4907:   font: small $sans;
1.359     albertel 4908:   text-align: right;
                   4909: }
1.469     banghart 4910: span.LC_metadata {
                   4911:     font-family: $sans;
                   4912: }
1.359     albertel 4913: span.LC_title_bar_title {
1.416     albertel 4914:   font: bold x-large $sans;
1.359     albertel 4915: }
                   4916: table#LC_title_bar td.LC_title_bar_domain_logo {
                   4917:   background: $sidebg;
                   4918:   text-align: right;
1.692.4.2  raeburn  4919:   padding: 0;
1.368     albertel 4920: }
                   4921: table#LC_title_bar td.LC_title_bar_role_logo {
                   4922:   background: $sidebg;
1.692.4.2  raeburn  4923:   padding: 0;
1.359     albertel 4924: }
                   4925: 
1.346     albertel 4926: table#LC_menubuttons_mainmenu {
1.526     www      4927:   width: 100%;
1.692.4.2  raeburn  4928:   border: 0;
1.346     albertel 4929:   border-spacing: 1px;
1.692.4.2  raeburn  4930:   padding: 0 1px;
                   4931:   margin: 0;
1.346     albertel 4932:   border-collapse: separate;
                   4933: }
                   4934: table#LC_menubuttons img, table#LC_menubuttons_mainmenu img {
1.692.4.2  raeburn  4935:   border: none;
1.346     albertel 4936: }
1.345     albertel 4937: table#LC_top_nav td {
                   4938:   background: $tabbg;
1.692.4.2  raeburn  4939:   border: none;
1.407     albertel 4940:   font-size: small;
1.345     albertel 4941: }
                   4942: table#LC_top_nav td a, div#LC_top_nav a {
                   4943:   color: $font;
                   4944:   font-family: $sans;
                   4945: }
1.364     albertel 4946: table#LC_top_nav td.LC_top_nav_logo {
                   4947:   background: $tabbg;
1.432     albertel 4948:   text-align: left;
1.408     albertel 4949:   white-space: nowrap;
1.432     albertel 4950:   width: 31px;
1.408     albertel 4951: }
                   4952: table#LC_top_nav td.LC_top_nav_logo img {
1.692.4.2  raeburn  4953:   border: none;
1.408     albertel 4954:   vertical-align: bottom;
1.364     albertel 4955: }
1.432     albertel 4956: table#LC_top_nav td.LC_top_nav_exit,
                   4957: table#LC_top_nav td.LC_top_nav_help {
                   4958:   width: 2.0em;
                   4959: }
1.442     albertel 4960: table#LC_top_nav td.LC_top_nav_login {
                   4961:   width: 4.0em;
                   4962:   text-align: center;
                   4963: }
1.409     albertel 4964: table.LC_breadcrumbs td, table.LC_docs_path td  {
1.357     albertel 4965:   background: $tabbg;
                   4966:   color: $font;
                   4967:   font-family: $sans;
1.358     albertel 4968:   font-size: smaller;
1.357     albertel 4969: }
1.411     albertel 4970: table.LC_breadcrumbs td.LC_breadcrumbs_component,
1.409     albertel 4971: table.LC_docs_path td.LC_docs_path_component {
1.357     albertel 4972:   background: $tabbg;
                   4973:   color: $font;
                   4974:   font-family: $sans;
                   4975:   font-size: larger;
                   4976:   text-align: right;
                   4977: }
1.383     albertel 4978: td.LC_table_cell_checkbox {
                   4979:   text-align: center;
                   4980: }
1.522     albertel 4981: table#LC_mainmenu td.LC_mainmenu_column {
                   4982:     vertical-align: top;
                   4983: }
                   4984: 
1.346     albertel 4985: .LC_menubuttons_inline_text {
                   4986:   color: $font;
                   4987:   font-family: $sans;
                   4988:   font-size: smaller;
                   4989: }
                   4990: 
1.526     www      4991: .LC_menubuttons_link {
                   4992:   text-decoration: none;
                   4993: }
1.692.4.2  raeburn  4994: /*2008--9-5: new menu style sheet.Changed category*/
1.522     albertel 4995: .LC_menubuttons_category {
1.521     www      4996:   color: $font;
1.526     www      4997:   background: $pgbg;
1.521     www      4998:   font-family: $sans;
                   4999:   font-size: larger;
                   5000:   font-weight: bold;
                   5001: }
                   5002: 
1.346     albertel 5003: td.LC_menubuttons_text {
1.526     www      5004:   width: 90%;
1.346     albertel 5005:   color: $font;
                   5006:   font-family: $sans;
                   5007: }
1.526     www      5008: 
1.346     albertel 5009: td.LC_menubuttons_img {
                   5010: }
1.526     www      5011: 
1.346     albertel 5012: .LC_current_location {
                   5013:   font-family: $sans;
                   5014:   background: $tabbg;
                   5015: }
                   5016: .LC_new_mail {
                   5017:   font-family: $sans;
1.634     www      5018:   background: $tabbg;
1.346     albertel 5019:   font-weight: bold;
                   5020: }
1.347     albertel 5021: 
1.527     www      5022: .LC_dropadd_labeltext {
                   5023:   font-family: $sans;
                   5024:   text-align: right;
                   5025: }
                   5026: 
                   5027: .LC_preferences_labeltext {
                   5028:   font-family: $sans;
                   5029:   text-align: right;
                   5030: }
                   5031: 
1.666     raeburn  5032: .LC_roleslog_note {
                   5033:   font-size: smaller;
                   5034: }
                   5035: 
1.692.4.2  raeburn  5036: .LC_mail_functions {
                   5037:     font-weight: bold;
                   5038: }
                   5039: 
1.440     albertel 5040: table.LC_aboutme_port {
1.692.4.2  raeburn  5041:   border: none;
1.440     albertel 5042:   border-collapse: collapse;
1.692.4.2  raeburn  5043:   border-spacing: 0;
1.440     albertel 5044: }
1.349     albertel 5045: table.LC_data_table, table.LC_mail_list {
1.347     albertel 5046:   border: 1px solid #000000;
1.402     albertel 5047:   border-collapse: separate;
1.426     albertel 5048:   border-spacing: 1px;
1.610     albertel 5049:   background: $pgbg;
1.347     albertel 5050: }
1.422     albertel 5051: .LC_data_table_dense {
                   5052:   font-size: small;
                   5053: }
1.507     raeburn  5054: table.LC_nested_outer {
                   5055:   border: 1px solid #000000;
1.589     raeburn  5056:   border-collapse: collapse;
1.692.4.2  raeburn  5057:   border-spacing: 0;
1.507     raeburn  5058:   width: 100%;
                   5059: }
1.692.4.11  raeburn  5060: table.LC_innerpickbox,
1.507     raeburn  5061: table.LC_nested {
1.692.4.2  raeburn  5062:   border: none;
1.589     raeburn  5063:   border-collapse: collapse;
1.692.4.2  raeburn  5064:   border-spacing: 0;
1.507     raeburn  5065:   width: 100%;
                   5066: }
1.523     albertel 5067: table.LC_data_table tr th, table.LC_calendar tr th, table.LC_mail_list tr th,
1.692.4.11  raeburn  5068: table.LC_prior_tries tr th,
                   5069: table.LC_innerpickbox tr th {
1.349     albertel 5070:   font-weight: bold;
                   5071:   background-color: $data_table_head;
1.421     albertel 5072:   font-size: smaller;
1.347     albertel 5073: }
1.692.4.11  raeburn  5074: table.LC_innerpickbox tr th,
                   5075: table.LC_innerpickbox tr td {
                   5076:   vertical-align: top;
                   5077: }
1.692.4.2  raeburn  5078: table.LC_data_table tr.LC_info_row > td {
                   5079:   background-color: #CCCCCC;
                   5080:   font-weight: bold;
                   5081:   text-align: left;
                   5082: }
1.610     albertel 5083: table.LC_data_table tr.LC_odd_row > td, 
1.692.4.2  raeburn  5084: table.LC_pick_box tr > td.LC_odd_row,
1.440     albertel 5085: table.LC_aboutme_port tr td {
1.349     albertel 5086:   background-color: $data_table_light;
1.425     albertel 5087:   padding: 2px;
1.347     albertel 5088: }
1.610     albertel 5089: table.LC_data_table tr.LC_even_row > td,
1.692.4.2  raeburn  5090: table.LC_pick_box tr > td.LC_even_row,
1.440     albertel 5091: table.LC_aboutme_port tr.LC_even_row td {
1.349     albertel 5092:   background-color: $data_table_dark;
1.692.4.2  raeburn  5093:   padding: 2px;
1.347     albertel 5094: }
1.425     albertel 5095: table.LC_data_table tr.LC_data_table_highlight td {
                   5096:   background-color: $data_table_darker;
                   5097: }
1.639     raeburn  5098: table.LC_data_table tr td.LC_leftcol_header {
                   5099:   background-color: $data_table_head;
                   5100:   font-weight: bold;
                   5101: }
1.451     albertel 5102: table.LC_data_table tr.LC_empty_row td,
1.507     raeburn  5103: table.LC_nested tr.LC_empty_row td {
1.421     albertel 5104:   font-weight: bold;
                   5105:   font-style: italic;
                   5106:   text-align: center;
                   5107:   padding: 8px;
1.347     albertel 5108: }
1.692.4.34  raeburn  5109: 
                   5110: table.LC_data_table tr.LC_empty_row td {
                   5111:   background-color: $tabbg;
                   5112: }
                   5113: 
                   5114: table.LC_nested tr.LC_empty_row td {
                   5115:   background-color: #FFFFFF;
                   5116: }
                   5117: 
1.507     raeburn  5118: table.LC_nested tr.LC_empty_row td {
1.465     albertel 5119:   padding: 4ex
                   5120: }
1.507     raeburn  5121: table.LC_nested_outer tr th {
                   5122:   font-weight: bold;
                   5123:   background-color: $data_table_head;
                   5124:   font-size: smaller;
                   5125:   border-bottom: 1px solid #000000;
                   5126: }
                   5127: table.LC_nested_outer tr td.LC_subheader {
                   5128:   background-color: $data_table_head;
                   5129:   font-weight: bold;
                   5130:   font-size: small;
                   5131:   border-bottom: 1px solid #000000;
                   5132:   text-align: right;
1.451     albertel 5133: }
1.507     raeburn  5134: table.LC_nested tr.LC_info_row td {
1.692.4.2  raeburn  5135:   background-color: #CCCCCC;
1.451     albertel 5136:   font-weight: bold;
                   5137:   font-size: small;
1.507     raeburn  5138:   text-align: center;
                   5139: }
1.589     raeburn  5140: table.LC_nested tr.LC_info_row td.LC_left_item,
                   5141: table.LC_nested_outer tr th.LC_left_item {
1.507     raeburn  5142:   text-align: left;
1.451     albertel 5143: }
1.507     raeburn  5144: table.LC_nested td {
1.692.4.2  raeburn  5145:   background-color: #FFFFFF;
1.451     albertel 5146:   font-size: small;
1.507     raeburn  5147: }
                   5148: table.LC_nested_outer tr th.LC_right_item,
                   5149: table.LC_nested tr.LC_info_row td.LC_right_item,
                   5150: table.LC_nested tr.LC_odd_row td.LC_right_item,
                   5151: table.LC_nested tr td.LC_right_item {
1.451     albertel 5152:   text-align: right;
                   5153: }
                   5154: 
1.507     raeburn  5155: table.LC_nested tr.LC_odd_row td {
1.692.4.2  raeburn  5156:   background-color: #EEEEEE;
1.451     albertel 5157: }
                   5158: 
1.473     raeburn  5159: table.LC_createuser {
                   5160: }
                   5161: 
                   5162: table.LC_createuser tr.LC_section_row td {
                   5163:   font-size: smaller;
                   5164: }
                   5165: 
                   5166: table.LC_createuser tr.LC_info_row td  {
1.692.4.2  raeburn  5167:   background-color: #CCCCCC;
1.473     raeburn  5168:   font-weight: bold;
                   5169:   text-align: center;
                   5170: }
                   5171: 
1.349     albertel 5172: table.LC_calendar {
                   5173:   border: 1px solid #000000;
                   5174:   border-collapse: collapse;
                   5175: }
                   5176: table.LC_calendar_pickdate {
                   5177:   font-size: xx-small;
                   5178: }
                   5179: table.LC_calendar tr td {
                   5180:   border: 1px solid #000000;
                   5181:   vertical-align: top;
                   5182: }
                   5183: table.LC_calendar tr td.LC_calendar_day_empty {
                   5184:   background-color: $data_table_dark;
                   5185: }
                   5186: table.LC_calendar tr td.LC_calendar_day_current {
                   5187:   background-color: $data_table_highlight;
                   5188: }
                   5189: 
                   5190: table.LC_mail_list tr.LC_mail_new {
                   5191:   background-color: $mail_new;
                   5192: }
                   5193: table.LC_mail_list tr.LC_mail_new:hover {
                   5194:   background-color: $mail_new_hover;
                   5195: }
                   5196: table.LC_mail_list tr.LC_mail_read {
                   5197:   background-color: $mail_read;
                   5198: }
                   5199: table.LC_mail_list tr.LC_mail_read:hover {
                   5200:   background-color: $mail_read_hover;
                   5201: }
                   5202: table.LC_mail_list tr.LC_mail_replied {
                   5203:   background-color: $mail_replied;
                   5204: }
                   5205: table.LC_mail_list tr.LC_mail_replied:hover {
                   5206:   background-color: $mail_replied_hover;
                   5207: }
                   5208: table.LC_mail_list tr.LC_mail_other {
                   5209:   background-color: $mail_other;
                   5210: }
                   5211: table.LC_mail_list tr.LC_mail_other:hover {
                   5212:   background-color: $mail_other_hover;
                   5213: }
1.494     raeburn  5214: table.LC_mail_list tr.LC_mail_even {
                   5215: }
                   5216: table.LC_mail_list tr.LC_mail_odd {
                   5217: }
                   5218: 
1.385     albertel 5219: 
1.386     albertel 5220: table#LC_portfolio_actions {
                   5221:   width: auto;
                   5222:   background: $pgbg;
1.692.4.2  raeburn  5223:   border: none;
1.386     albertel 5224:   border-spacing: 2px 2px;
1.692.4.2  raeburn  5225:   padding: 0;
                   5226:   margin: 0;
1.386     albertel 5227:   border-collapse: separate;
                   5228: }
                   5229: table#LC_portfolio_actions td.LC_label {
                   5230:   background: $tabbg;
                   5231:   text-align: right;
                   5232: }
                   5233: table#LC_portfolio_actions td.LC_value {
                   5234:   background: $tabbg;
                   5235: }
1.385     albertel 5236: 
1.391     albertel 5237: table#LC_cstr_controls {
                   5238:   width: 100%;
                   5239:   border-collapse: collapse;
                   5240: }
                   5241: table#LC_cstr_controls tr td {
                   5242:   border: 4px solid $pgbg;
                   5243:   padding: 4px;
                   5244:   text-align: center;
                   5245:   background: $tabbg;
                   5246: }
                   5247: table#LC_cstr_controls tr th {
                   5248:   border: 4px solid $pgbg;
                   5249:   background: $table_header;
                   5250:   text-align: center;
                   5251:   font-family: $sans;
                   5252:   font-size: smaller;
                   5253: }
                   5254: 
1.389     albertel 5255: table#LC_browser {
                   5256:  
                   5257: }
                   5258: table#LC_browser tr th {
1.391     albertel 5259:   background: $table_header;
1.389     albertel 5260: }
1.390     albertel 5261: table#LC_browser tr td {
                   5262:   padding: 2px;
                   5263: }
1.389     albertel 5264: table#LC_browser tr.LC_browser_file,
                   5265: table#LC_browser tr.LC_browser_file_published {
                   5266:   background: #CCFF88;
                   5267: }
                   5268: table#LC_browser tr.LC_browser_file_locked,
                   5269: table#LC_browser tr.LC_browser_file_unpublished {
                   5270:   background: #FFAA99;
1.387     albertel 5271: }
1.389     albertel 5272: table#LC_browser tr.LC_browser_file_obsolete {
                   5273:   background: #AAAAAA;
1.387     albertel 5274: }
1.455     albertel 5275: table#LC_browser tr.LC_browser_file_modified,
                   5276: table#LC_browser tr.LC_browser_file_metamodified {
1.389     albertel 5277:   background: #FFFF77;
1.387     albertel 5278: }
1.389     albertel 5279: table#LC_browser tr.LC_browser_folder {
                   5280:   background: #CCCCFF;
1.387     albertel 5281: }
1.692.4.2  raeburn  5282: 
1.692.4.28  raeburn  5283: table.LC_data_table tr > td.LC_browser_file,
                   5284: table.LC_data_table tr > td.LC_browser_file_published {
                   5285:   background: #AAEE77;
                   5286: }
                   5287: 
                   5288: table.LC_data_table tr > td.LC_browser_file_locked,
                   5289: table.LC_data_table tr > td.LC_browser_file_unpublished {
                   5290:   background: #FFAA99;
                   5291: }
                   5292: 
                   5293: table.LC_data_table tr > td.LC_browser_file_obsolete {
                   5294:   background: #888888;
                   5295: }
                   5296: 
                   5297: table.LC_data_table tr > td.LC_browser_file_modified,
                   5298: table.LC_data_table tr > td.LC_browser_file_metamodified {
                   5299:   background: #F8F866;
                   5300: }
                   5301: 
                   5302: table.LC_data_table tr.LC_browser_folder > td {
                   5303:   background: #E0E8FF;
                   5304: }
                   5305: 
1.692.4.2  raeburn  5306: table.LC_data_table tr > td.LC_roles_is {
                   5307: /*  background: #77FF77; */
                   5308: }
                   5309: table.LC_data_table tr > td.LC_roles_future {
                   5310:   background: #FFFF77;
                   5311: }
                   5312: table.LC_data_table tr > td.LC_roles_will {
                   5313:   background: #FFAA77;
                   5314: }
                   5315: table.LC_data_table tr > td.LC_roles_expired {
                   5316:   background: #FF7777;
                   5317: }
                   5318: table.LC_data_table tr > td.LC_roles_will_not {
                   5319:   background: #AAFF77;
                   5320: }
                   5321: table.LC_data_table tr > td.LC_roles_selected {
                   5322:   background: #11CC55;
                   5323: }
                   5324: 
1.388     albertel 5325: span.LC_current_location {
                   5326:   font-size: x-large;
                   5327:   background: $pgbg;
                   5328: }
1.387     albertel 5329: 
1.395     albertel 5330: span.LC_parm_menu_item {
                   5331:   font-size: larger;
                   5332:   font-family: $sans;
                   5333: }
                   5334: span.LC_parm_scope_all {
                   5335:   color: red;
                   5336: }
                   5337: span.LC_parm_scope_folder {
                   5338:   color: green;
                   5339: }
                   5340: span.LC_parm_scope_resource {
                   5341:   color: orange;
                   5342: }
                   5343: span.LC_parm_part {
                   5344:   color: blue;
                   5345: }
                   5346: span.LC_parm_folder, span.LC_parm_symb {
                   5347:   font-size: x-small;
                   5348:   font-family: $mono;
                   5349:   color: #AAAAAA;
                   5350: }
                   5351: 
1.396     albertel 5352: td.LC_parm_overview_level_menu, td.LC_parm_overview_map_menu,
                   5353: td.LC_parm_overview_parm_selectors, td.LC_parm_overview_parm_restrictions {
                   5354:   border: 1px solid black;
                   5355:   border-collapse: collapse;
                   5356: }
                   5357: table.LC_parm_overview_restrictions td {
                   5358:   border-width: 1px 4px 1px 4px;
                   5359:   border-style: solid;
                   5360:   border-color: $pgbg;
                   5361:   text-align: center;
                   5362: }
                   5363: table.LC_parm_overview_restrictions th {
                   5364:   background: $tabbg;
                   5365:   border-width: 1px 4px 1px 4px;
                   5366:   border-style: solid;
                   5367:   border-color: $pgbg;
                   5368: }
1.398     albertel 5369: table#LC_helpmenu {
1.692.4.2  raeburn  5370:   border: none;
1.398     albertel 5371:   height: 55px;
1.692.4.2  raeburn  5372:   border-spacing: 0;
1.398     albertel 5373: }
                   5374: 
                   5375: table#LC_helpmenu fieldset legend {
                   5376:   font-size: larger;
                   5377:   font-weight: bold;
                   5378: }
1.397     albertel 5379: table#LC_helpmenu_links {
                   5380:   width: 100%;
                   5381:   border: 1px solid black;
                   5382:   background: $pgbg;
1.692.4.2  raeburn  5383:   padding: 0;
1.397     albertel 5384:   border-spacing: 1px;
                   5385: }
                   5386: table#LC_helpmenu_links tr td {
                   5387:   padding: 1px;
                   5388:   background: $tabbg;
1.399     albertel 5389:   text-align: center;
                   5390:   font-weight: bold;
1.397     albertel 5391: }
1.396     albertel 5392: 
1.397     albertel 5393: table#LC_helpmenu_links a:link, table#LC_helpmenu_links a:visited,
                   5394: table#LC_helpmenu_links a:active {
                   5395:   text-decoration: none;
                   5396:   color: $font;
                   5397: }
                   5398: table#LC_helpmenu_links a:hover {
                   5399:   text-decoration: underline;
                   5400:   color: $vlink;
                   5401: }
1.396     albertel 5402: 
1.417     albertel 5403: .LC_chrt_popup_exists {
                   5404:   border: 1px solid #339933;
                   5405:   margin: -1px;
                   5406: }
                   5407: .LC_chrt_popup_up {
                   5408:   border: 1px solid yellow;
                   5409:   margin: -1px;
                   5410: }
                   5411: .LC_chrt_popup {
                   5412:   border: 1px solid #8888FF;
                   5413:   background: #CCCCFF;
                   5414: }
1.421     albertel 5415: table.LC_pick_box {
                   5416:   border-collapse: separate;
                   5417:   background: white;
                   5418:   border: 1px solid black;
                   5419:   border-spacing: 1px;
                   5420: }
                   5421: table.LC_pick_box td.LC_pick_box_title {
1.692.4.16  raeburn  5422:   background: $tabbg;
1.421     albertel 5423:   font-weight: bold;
                   5424:   text-align: right;
1.692.4.2  raeburn  5425:   vertical-align: top;
1.421     albertel 5426:   width: 184px;
                   5427:   padding: 8px;
                   5428: }
1.645     raeburn  5429: table.LC_pick_box td.LC_selfenroll_pick_box_title {
1.692.4.16  raeburn  5430:   background: $tabbg;
1.645     raeburn  5431:   font-weight: bold;
                   5432:   text-align: right;
                   5433:   width: 350px;
                   5434:   padding: 8px;
                   5435: }
                   5436: 
1.579     raeburn  5437: table.LC_pick_box td.LC_pick_box_value {
                   5438:   text-align: left;
                   5439:   padding: 8px;
                   5440: }
                   5441: table.LC_pick_box td.LC_pick_box_select {
                   5442:   text-align: left;
                   5443:   padding: 8px;
                   5444: }
1.424     albertel 5445: table.LC_pick_box td.LC_pick_box_separator {
1.692.4.2  raeburn  5446:   padding: 0;
1.421     albertel 5447:   height: 1px;
                   5448:   background: black;
                   5449: }
                   5450: table.LC_pick_box td.LC_pick_box_submit {
                   5451:   text-align: right;
                   5452: }
1.579     raeburn  5453: table.LC_pick_box td.LC_evenrow_value {
                   5454:   text-align: left;
                   5455:   padding: 8px;
                   5456:   background-color: $data_table_light;
                   5457: }
                   5458: table.LC_pick_box td.LC_oddrow_value {
                   5459:   text-align: left;
                   5460:   padding: 8px;
                   5461:   background-color: $data_table_light;
                   5462: }
                   5463: table.LC_helpform_receipt {
                   5464:   width: 620px;
                   5465:   border-collapse: separate;
                   5466:   background: white;
                   5467:   border: 1px solid black;
                   5468:   border-spacing: 1px;
                   5469: }
                   5470: table.LC_helpform_receipt td.LC_pick_box_title {
                   5471:   background: $tabbg;
                   5472:   font-weight: bold;
                   5473:   text-align: right;
                   5474:   width: 184px;
                   5475:   padding: 8px;
                   5476: }
                   5477: table.LC_helpform_receipt td.LC_evenrow_value {
                   5478:   text-align: left;
                   5479:   padding: 8px;
                   5480:   background-color: $data_table_light;
                   5481: }
                   5482: table.LC_helpform_receipt td.LC_oddrow_value {
                   5483:   text-align: left;
                   5484:   padding: 8px;
                   5485:   background-color: $data_table_light;
                   5486: }
                   5487: table.LC_helpform_receipt td.LC_pick_box_separator {
1.692.4.2  raeburn  5488:   padding: 0;
1.579     raeburn  5489:   height: 1px;
                   5490:   background: black;
                   5491: }
                   5492: span.LC_helpform_receipt_cat {
                   5493:   font-weight: bold;
                   5494: }
1.424     albertel 5495: table.LC_group_priv_box {
                   5496:   background: white;
                   5497:   border: 1px solid black;
                   5498:   border-spacing: 1px;
                   5499: }
                   5500: table.LC_group_priv_box td.LC_pick_box_title {
                   5501:   background: $tabbg;
                   5502:   font-weight: bold;
                   5503:   text-align: right;
                   5504:   width: 184px;
                   5505: }
                   5506: table.LC_group_priv_box td.LC_groups_fixed {
                   5507:   background: $data_table_light;
                   5508:   text-align: center;
                   5509: }
                   5510: table.LC_group_priv_box td.LC_groups_optional {
                   5511:   background: $data_table_dark;
                   5512:   text-align: center;
                   5513: }
                   5514: table.LC_group_priv_box td.LC_groups_functionality {
                   5515:   background: $data_table_darker;
                   5516:   text-align: center;
                   5517:   font-weight: bold;
                   5518: }
                   5519: table.LC_group_priv td {
                   5520:   text-align: left;
1.692.4.2  raeburn  5521:   padding: 0;
1.424     albertel 5522: }
                   5523: 
1.421     albertel 5524: table.LC_notify_front_page {
                   5525:   background: white;
                   5526:   border: 1px solid black;
                   5527:   padding: 8px;
                   5528: }
                   5529: table.LC_notify_front_page td {
                   5530:   padding: 8px;
                   5531: }
1.424     albertel 5532: .LC_navbuttons {
                   5533:   margin: 2ex 0ex 2ex 0ex;
                   5534: }
1.423     albertel 5535: .LC_topic_bar {
                   5536:   font-family: $sans;
                   5537:   font-weight: bold;
                   5538:   width: 100%;
                   5539:   background: $tabbg;
                   5540:   vertical-align: middle;
                   5541:   margin: 2ex 0ex 2ex 0ex;
1.692.4.2  raeburn  5542:   padding: 3px;
1.423     albertel 5543: }
                   5544: .LC_topic_bar span {
                   5545:   vertical-align: middle;
                   5546: }
                   5547: .LC_topic_bar img {
                   5548:   vertical-align: bottom;
                   5549: }
                   5550: table.LC_course_group_status {
                   5551:   margin: 20px;
                   5552: }
                   5553: table.LC_status_selector td {
                   5554:   vertical-align: top;
                   5555:   text-align: center;
1.424     albertel 5556:   padding: 4px;
                   5557: }
                   5558: table.LC_descriptive_input td.LC_description {
                   5559:   vertical-align: top;
                   5560:   text-align: right;
                   5561:   font-weight: bold;
1.423     albertel 5562: }
1.599     albertel 5563: div.LC_feedback_link {
1.616     albertel 5564:   clear: both;
1.599     albertel 5565:   background: white;
                   5566:   width: 100%;  
1.489     raeburn  5567: }
                   5568: span.LC_feedback_link {
1.599     albertel 5569:   background: $feedback_link_bg;
                   5570:   font-size: larger;
                   5571: }
                   5572: span.LC_message_link {
                   5573:   background: $feedback_link_bg;
                   5574:   font-size: larger;
                   5575:   position: absolute;
                   5576:   right: 1em;
1.489     raeburn  5577: }
1.421     albertel 5578: 
1.515     albertel 5579: table.LC_prior_tries {
1.524     albertel 5580:   border: 1px solid #000000;
                   5581:   border-collapse: separate;
                   5582:   border-spacing: 1px;
1.515     albertel 5583: }
1.523     albertel 5584: 
1.515     albertel 5585: table.LC_prior_tries td {
1.524     albertel 5586:   padding: 2px;
1.515     albertel 5587: }
1.523     albertel 5588: 
                   5589: .LC_answer_correct {
                   5590:   background: #AAFFAA;
                   5591:   color: black;
                   5592: }
                   5593: .LC_answer_charged_try {
                   5594:   background: #FFAAAA ! important;
                   5595:   color: black;
                   5596: }
                   5597: .LC_answer_not_charged_try, 
                   5598: .LC_answer_no_grade,
                   5599: .LC_answer_late {
                   5600:   background: #FFFFAA;
                   5601:   color: black;
                   5602: }
                   5603: .LC_answer_previous {
                   5604:   background: #AAAAFF;
                   5605:   color: black;
                   5606: }
                   5607: .LC_answer_no_message {
                   5608:   background: #FFFFFF;
                   5609:   color: black;
                   5610: }
                   5611: .LC_answer_unknown {
                   5612:   background: orange;
                   5613:   color: black;
                   5614: }
                   5615: 
                   5616: 
1.529     albertel 5617: span.LC_prior_numerical,
                   5618: span.LC_prior_string,
                   5619: span.LC_prior_custom,
                   5620: span.LC_prior_reaction,
                   5621: span.LC_prior_math {
1.523     albertel 5622:   font-family: monospace;
                   5623:   white-space: pre;
                   5624: }
                   5625: 
1.525     albertel 5626: span.LC_prior_string {
                   5627:   font-family: monospace;
                   5628:   white-space: pre;
                   5629: }
                   5630: 
1.523     albertel 5631: table.LC_prior_option {
                   5632:   width: 100%;
                   5633:   border-collapse: collapse;
                   5634: }
1.528     albertel 5635: table.LC_prior_rank, table.LC_prior_match {
                   5636:   border-collapse: collapse;
                   5637: }
                   5638: table.LC_prior_option tr td,
                   5639: table.LC_prior_rank tr td,
                   5640: table.LC_prior_match tr td {
1.524     albertel 5641:   border: 1px solid #000000;
1.515     albertel 5642: }
                   5643: 
1.692.4.28  raeburn  5644: .LC_nobreak {
1.544     albertel 5645:   white-space: nowrap;
1.519     raeburn  5646: }
                   5647: 
1.576     raeburn  5648: span.LC_cusr_emph {
                   5649:   font-style: italic;
                   5650: }
                   5651: 
1.633     raeburn  5652: span.LC_cusr_subheading {
                   5653:   font-weight: normal;
                   5654:   font-size: 85%;
                   5655: }
                   5656: 
1.545     albertel 5657: table.LC_docs_documents {
                   5658:   background: #BBBBBB;
1.692.4.2  raeburn  5659:   border-width: 0;
1.545     albertel 5660:   border-collapse: collapse;
                   5661: }
                   5662: 
                   5663: table.LC_docs_documents td.LC_docs_document {
                   5664:   border: 2px solid black;
                   5665:   padding: 4px;
                   5666: }
                   5667: 
                   5668: .LC_docs_course_commands div {
                   5669:   float: left;
                   5670:   border: 4px solid #AAAAAA;
                   5671:   padding: 4px;
                   5672:   background: #DDDDCC;
                   5673: }
                   5674: 
                   5675: .LC_docs_entry_move {
1.692.4.2  raeburn  5676:   border: none;
1.545     albertel 5677:   border-collapse: collapse;
1.544     albertel 5678: }
                   5679: 
1.545     albertel 5680: .LC_docs_entry_move td {
                   5681:   border: 2px solid #BBBBBB;
                   5682:   background: #DDDDDD;
                   5683: }
                   5684: 
                   5685: .LC_docs_editor td.LC_docs_entry_commands {
                   5686:   background: #DDDDDD;
                   5687:   font-size: x-small;
                   5688: }
1.544     albertel 5689: .LC_docs_copy {
1.545     albertel 5690:   color: #000099;
1.544     albertel 5691: }
                   5692: .LC_docs_cut {
1.545     albertel 5693:   color: #550044;
1.544     albertel 5694: }
                   5695: .LC_docs_rename {
1.545     albertel 5696:   color: #009900;
1.544     albertel 5697: }
                   5698: .LC_docs_remove {
1.545     albertel 5699:   color: #990000;
                   5700: }
                   5701: 
1.547     albertel 5702: .LC_docs_reinit_warn,
                   5703: .LC_docs_ext_edit {
                   5704:   font-size: x-small;
                   5705: }
                   5706: 
1.545     albertel 5707: .LC_docs_editor td.LC_docs_entry_title,
                   5708: .LC_docs_editor td.LC_docs_entry_icon {
                   5709:   background: #FFFFBB;
                   5710: }
                   5711: .LC_docs_editor td.LC_docs_entry_parameter {
                   5712:   background: #BBBBFF;
                   5713:   font-size: x-small;
                   5714:   white-space: nowrap;
                   5715: }
                   5716: 
                   5717: table.LC_docs_adddocs td,
                   5718: table.LC_docs_adddocs th {
                   5719:   border: 1px solid #BBBBBB;
                   5720:   padding: 4px;
                   5721:   background: #DDDDDD;
1.543     albertel 5722: }
                   5723: 
1.584     albertel 5724: table.LC_sty_begin {
                   5725:   background: #BBFFBB;
                   5726: }
                   5727: table.LC_sty_end {
                   5728:   background: #FFBBBB;
                   5729: }
                   5730: 
1.589     raeburn  5731: table.LC_double_column {
1.692.4.2  raeburn  5732:   border-width: 0;
1.589     raeburn  5733:   border-collapse: collapse;
                   5734:   width: 100%;
                   5735:   padding: 2px;
                   5736: }
                   5737: 
                   5738: table.LC_double_column tr td.LC_left_col {
1.590     raeburn  5739:   top: 2px;
1.589     raeburn  5740:   left: 2px;
                   5741:   width: 47%;
                   5742:   vertical-align: top;
                   5743: }
                   5744: 
                   5745: table.LC_double_column tr td.LC_right_col {
                   5746:   top: 2px;
                   5747:   right: 2px; 
                   5748:   width: 47%;
                   5749:   vertical-align: top;
                   5750: }
                   5751: 
1.594     raeburn  5752: span.LC_role_level {
                   5753:   font-weight: bold;
                   5754: }
                   5755: 
1.591     raeburn  5756: div.LC_left_float {
                   5757:   float: left;
                   5758:   padding-right: 5%;
1.597     albertel 5759:   padding-bottom: 4px;
1.591     raeburn  5760: }
                   5761: 
                   5762: div.LC_clear_float_header {
1.597     albertel 5763:   padding-bottom: 2px;
1.591     raeburn  5764: }
                   5765: 
                   5766: div.LC_clear_float_footer {
1.597     albertel 5767:   padding-top: 10px;
1.591     raeburn  5768:   clear: both;
                   5769: }
                   5770: 
1.597     albertel 5771: 
1.601     albertel 5772: div.LC_grade_select_mode {
1.604     albertel 5773:   font-family: $sans;
1.601     albertel 5774: }
                   5775: div.LC_grade_select_mode div div {
                   5776:   margin: 5px;
                   5777: }
                   5778: div.LC_grade_select_mode_selector {
                   5779:   margin: 5px;
                   5780:   float: left;
                   5781: }
                   5782: div.LC_grade_select_mode_selector_header {
                   5783:   font: bold medium $sans;
                   5784: }
                   5785: div.LC_grade_select_mode_type {
                   5786:   clear: left;
                   5787: }
                   5788: 
1.597     albertel 5789: div.LC_grade_show_user {
                   5790:   margin-top: 20px;
                   5791:   border: 1px solid black;
                   5792: }
                   5793: div.LC_grade_user_name {
                   5794:   background: #DDDDEE;
                   5795:   border-bottom: 1px solid black;
                   5796:   font: bold large $sans;
                   5797: }
                   5798: div.LC_grade_show_user_odd_row div.LC_grade_user_name {
                   5799:   background: #DDEEDD;
                   5800: }
                   5801: 
                   5802: div.LC_grade_show_problem,
                   5803: div.LC_grade_submissions,
                   5804: div.LC_grade_message_center,
                   5805: div.LC_grade_info_links,
                   5806: div.LC_grade_assign {
                   5807:   margin: 5px;
                   5808:   width: 99%;
                   5809:   background: #FFFFFF;
                   5810: }
                   5811: div.LC_grade_show_problem_header,
                   5812: div.LC_grade_submissions_header,
                   5813: div.LC_grade_message_center_header,
                   5814: div.LC_grade_assign_header {
                   5815:   font: bold large $sans;
                   5816: }
                   5817: div.LC_grade_show_problem_problem,
                   5818: div.LC_grade_submissions_body,
                   5819: div.LC_grade_message_center_body,
                   5820: div.LC_grade_assign_body {
                   5821:   border: 1px solid black;
                   5822:   width: 99%;
                   5823:   background: #FFFFFF;
                   5824: }
1.598     albertel 5825: span.LC_grade_check_note {
                   5826:   font: normal medium $sans;
                   5827:   display: inline;
                   5828:   position: absolute;
                   5829:   right: 1em;
                   5830: }
1.597     albertel 5831: 
1.613     albertel 5832: table.LC_scantron_action {
                   5833:   width: 100%;
                   5834: }
                   5835: table.LC_scantron_action tr th {
                   5836:   font: normal bold $sans;
                   5837: }
1.600     albertel 5838: 
1.614     albertel 5839: div.LC_edit_problem_header, 
                   5840: div.LC_edit_problem_footer {
1.600     albertel 5841:   font: normal medium $sans;
1.602     albertel 5842:   margin: 2px;
1.600     albertel 5843: }
                   5844: div.LC_edit_problem_header,
1.602     albertel 5845: div.LC_edit_problem_header div,
1.614     albertel 5846: div.LC_edit_problem_footer,
                   5847: div.LC_edit_problem_footer div,
1.602     albertel 5848: div.LC_edit_problem_editxml_header,
                   5849: div.LC_edit_problem_editxml_header div {
1.600     albertel 5850:   margin-top: 5px;
                   5851: }
1.602     albertel 5852: div.LC_edit_problem_header_edit_row {
                   5853:   background: $tabbg;
                   5854:   padding: 3px;
                   5855:   margin-bottom: 5px;
                   5856: }
1.600     albertel 5857: div.LC_edit_problem_header_title {
1.602     albertel 5858:   font: larger bold $sans;
                   5859:   background: $tabbg;
                   5860:   padding: 3px;
                   5861: }
                   5862: table.LC_edit_problem_header_title {
                   5863:   font: larger bold $sans;
                   5864:   width: 100%;
                   5865:   border-color: $pgbg;
                   5866:   border-style: solid;
                   5867:   border-width: $border;
                   5868: 
1.600     albertel 5869:   background: $tabbg;
1.602     albertel 5870:   border-collapse: collapse;
1.692.4.2  raeburn  5871:   padding: 0;
1.602     albertel 5872: }
                   5873: 
                   5874: div.LC_edit_problem_discards {
                   5875:   float: left;
                   5876:   padding-bottom: 5px;
                   5877: }
                   5878: div.LC_edit_problem_saves {
                   5879:   float: right;
                   5880:   padding-bottom: 5px;
1.600     albertel 5881: }
                   5882: hr.LC_edit_problem_divide {
1.602     albertel 5883:   clear: both;
1.600     albertel 5884:   color: $tabbg;
                   5885:   background-color: $tabbg;
                   5886:   height: 3px;
1.692.4.2  raeburn  5887:   border: none;
1.600     albertel 5888: }
1.679     riegler  5889: img.stift{
1.678     riegler  5890:   border-width:0;
1.679     riegler  5891:   vertical-align:middle;
1.677     riegler  5892: }
1.680     riegler  5893: 
1.681     riegler  5894: table#LC_mainmenu{
                   5895:  margin-top:10px;
                   5896:  width:80%;
                   5897: 
                   5898: }
                   5899: 
1.680     riegler  5900: table#LC_mainmenu td.LC_mainmenu_col_fieldset{
                   5901:   vertical-align: top;
                   5902:   width: 45%;
                   5903: }
                   5904: .LC_mainmenu_fieldset_category {
                   5905:   color: $font;
                   5906:   background: $pgbg;
                   5907:   font-family: $sans;
                   5908:   font-size: small;
                   5909:   font-weight: bold;
                   5910: }
                   5911: fieldset#LC_mainmenu_fieldset {
1.692.4.2  raeburn  5912:   margin:0 10px 10px 0;
                   5913: 
                   5914: }
1.680     riegler  5915: 
1.692.4.2  raeburn  5916: div.LC_createcourse {
                   5917:     margin: 10px 10px 10px 10px;
1.680     riegler  5918: }
1.692.4.2  raeburn  5919: 
1.343     albertel 5920: END
                   5921: }
                   5922: 
1.306     albertel 5923: =pod
                   5924: 
                   5925: =item * &headtag()
                   5926: 
                   5927: Returns a uniform footer for LON-CAPA web pages.
                   5928: 
1.307     albertel 5929: Inputs: $title - optional title for the head
                   5930:         $head_extra - optional extra HTML to put inside the <head>
1.315     albertel 5931:         $args - optional arguments
1.319     albertel 5932:             force_register - if is true call registerurl so the remote is 
                   5933:                              informed
1.415     albertel 5934:             redirect       -> array ref of
                   5935:                                    1- seconds before redirect occurs
                   5936:                                    2- url to redirect to
                   5937:                                    3- whether the side effect should occur
1.315     albertel 5938:                            (side effect of setting 
                   5939:                                $env{'internal.head.redirect'} to the url 
                   5940:                                redirected too)
1.352     albertel 5941:             domain         -> force to color decorate a page for a specific
                   5942:                                domain
                   5943:             function       -> force usage of a specific rolish color scheme
                   5944:             bgcolor        -> override the default page bgcolor
1.460     albertel 5945:             no_auto_mt_title
                   5946:                            -> prevent &mt()ing the title arg
1.464     albertel 5947: 
1.306     albertel 5948: =cut
                   5949: 
                   5950: sub headtag {
1.313     albertel 5951:     my ($title,$head_extra,$args) = @_;
1.306     albertel 5952:     
1.363     albertel 5953:     my $function = $args->{'function'} || &get_users_function();
                   5954:     my $domain   = $args->{'domain'}   || &determinedomain();
                   5955:     my $bgcolor  = $args->{'bgcolor'}  || &designparm($function.'.pgbg',$domain);
1.418     albertel 5956:     my $url = join(':',$env{'user.name'},$env{'user.domain'},
1.458     albertel 5957: 		   $Apache::lonnet::perlvar{'lonVersion'},
1.531     albertel 5958: 		   #time(),
1.418     albertel 5959: 		   $env{'environment.color.timestamp'},
1.363     albertel 5960: 		   $function,$domain,$bgcolor);
                   5961: 
1.369     www      5962:     $url = '/adm/css/'.&escape($url).'.css';
1.363     albertel 5963: 
1.308     albertel 5964:     my $result =
                   5965: 	'<head>'.
1.461     albertel 5966: 	&font_settings();
1.319     albertel 5967: 
1.461     albertel 5968:     if (!$args->{'frameset'}) {
                   5969: 	$result .= &Apache::lonhtmlcommon::htmlareaheaders();
                   5970:     }
1.319     albertel 5971:     if ($args->{'force_register'}) {
                   5972: 	$result .= &Apache::lonmenu::registerurl(1);
                   5973:     }
1.436     albertel 5974:     if (!$args->{'no_nav_bar'} 
                   5975: 	&& !$args->{'only_body'}
                   5976: 	&& !$args->{'frameset'}) {
                   5977: 	$result .= &help_menu_js();
                   5978:     }
1.319     albertel 5979: 
1.314     albertel 5980:     if (ref($args->{'redirect'})) {
1.414     albertel 5981: 	my ($time,$url,$inhibit_continue) = @{$args->{'redirect'}};
1.315     albertel 5982: 	$url = &Apache::lonenc::check_encrypt($url);
1.414     albertel 5983: 	if (!$inhibit_continue) {
                   5984: 	    $env{'internal.head.redirect'} = $url;
                   5985: 	}
1.313     albertel 5986: 	$result.=<<ADDMETA
                   5987: <meta http-equiv="pragma" content="no-cache" />
1.344     albertel 5988: <meta http-equiv="Refresh" content="$time; url=$url" />
1.313     albertel 5989: ADDMETA
                   5990:     }
1.306     albertel 5991:     if (!defined($title)) {
                   5992: 	$title = 'The LearningOnline Network with CAPA';
                   5993:     }
1.460     albertel 5994:     if (!$args->{'no_auto_mt_title'}) { $title = &mt($title); }
                   5995:     $result .= '<title> LON-CAPA '.$title.'</title>'
1.414     albertel 5996: 	.'<link rel="stylesheet" type="text/css" href="'.$url.'" />'
                   5997: 	.$head_extra;
1.306     albertel 5998:     return $result;
                   5999: }
                   6000: 
                   6001: =pod
                   6002: 
1.340     albertel 6003: =item * &font_settings()
                   6004: 
                   6005: Returns neccessary <meta> to set the proper encoding
                   6006: 
                   6007: Inputs: none
                   6008: 
                   6009: =cut
                   6010: 
                   6011: sub font_settings {
                   6012:     my $headerstring='';
1.647     www      6013:     if (!$env{'browser.mathml'} && $env{'browser.unicode'}) {
1.340     albertel 6014: 	$headerstring.=
                   6015: 	    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
                   6016:     }
                   6017:     return $headerstring;
                   6018: }
                   6019: 
1.341     albertel 6020: =pod
                   6021: 
                   6022: =item * &xml_begin()
                   6023: 
                   6024: Returns the needed doctype and <html>
                   6025: 
                   6026: Inputs: none
                   6027: 
                   6028: =cut
                   6029: 
                   6030: sub xml_begin {
                   6031:     my $output='';
                   6032: 
1.592     albertel 6033:     if ($env{'internal.start_page'}==1) {
                   6034: 	&Apache::lonhtmlcommon::init_htmlareafields();
                   6035:     }
1.342     albertel 6036: 
1.341     albertel 6037:     if ($env{'browser.mathml'}) {
                   6038: 	$output='<?xml version="1.0"?>'
                   6039:             #.'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'."\n"
                   6040: #            .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
                   6041:             
                   6042: #	    .'<!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">] >'
                   6043: 	    .'<!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">'
                   6044:             .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" ' 
                   6045: 	    .'xmlns="http://www.w3.org/1999/xhtml">';
                   6046:     } else {
1.692.4.6  raeburn  6047: 	$output='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.
                   6048:             '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
1.341     albertel 6049:     }
                   6050:     return $output;
                   6051: }
1.340     albertel 6052: 
                   6053: =pod
                   6054: 
1.306     albertel 6055: =item * &endheadtag()
                   6056: 
                   6057: Returns a uniform </head> for LON-CAPA web pages.
                   6058: 
                   6059: Inputs: none
                   6060: 
                   6061: =cut
                   6062: 
                   6063: sub endheadtag {
                   6064:     return '</head>';
                   6065: }
                   6066: 
                   6067: =pod
                   6068: 
                   6069: =item * &head()
                   6070: 
                   6071: Returns a uniform complete <head>..</head> section for LON-CAPA web pages.
                   6072: 
1.648     raeburn  6073: Inputs:
                   6074: 
                   6075: =over 4
                   6076: 
                   6077: $title - optional title for the page
                   6078: 
                   6079: $head_extra - optional extra HTML to put inside the <head>
                   6080: 
                   6081: =back
1.405     albertel 6082: 
1.306     albertel 6083: =cut
                   6084: 
                   6085: sub head {
1.325     albertel 6086:     my ($title,$head_extra,$args) = @_;
                   6087:     return &headtag($title,$head_extra,$args).&endheadtag();
1.306     albertel 6088: }
                   6089: 
                   6090: =pod
                   6091: 
                   6092: =item * &start_page()
                   6093: 
                   6094: Returns a complete <html> .. <body> section for LON-CAPA web pages.
                   6095: 
1.648     raeburn  6096: Inputs:
                   6097: 
                   6098: =over 4
                   6099: 
                   6100: $title - optional title for the page
                   6101: 
                   6102: $head_extra - optional extra HTML to incude inside the <head>
                   6103: 
                   6104: $args - additional optional args supported are:
                   6105: 
                   6106: =over 8
                   6107: 
                   6108:              only_body      -> is true will set &bodytag() onlybodytag
1.317     albertel 6109:                                     arg on
1.648     raeburn  6110:              no_nav_bar     -> is true will set &bodytag() notopbar arg on
                   6111:              add_entries    -> additional attributes to add to the  <body>
                   6112:              domain         -> force to color decorate a page for a 
1.317     albertel 6113:                                     specific domain
1.648     raeburn  6114:              function       -> force usage of a specific rolish color
1.317     albertel 6115:                                     scheme
1.648     raeburn  6116:              redirect       -> see &headtag()
                   6117:              bgcolor        -> override the default page bg color
                   6118:              js_ready       -> return a string ready for being used in 
1.317     albertel 6119:                                     a javascript writeln
1.648     raeburn  6120:              html_encode    -> return a string ready for being used in 
1.320     albertel 6121:                                     a html attribute
1.648     raeburn  6122:              force_register -> if is true will turn on the &bodytag()
1.317     albertel 6123:                                     $forcereg arg
1.648     raeburn  6124:              body_title     -> alternate text to use instead of $title
1.326     albertel 6125:                                     in the title box that appears, this text
                   6126:                                     is not auto translated like the $title is
1.648     raeburn  6127:              frameset       -> if true will start with a <frameset>
1.330     albertel 6128:                                     rather than <body>
1.648     raeburn  6129:              no_title       -> if true the title bar won't be shown
                   6130:              skip_phases    -> hash ref of 
1.338     albertel 6131:                                     head -> skip the <html><head> generation
                   6132:                                     body -> skip all <body> generation
1.648     raeburn  6133:              no_inline_link -> if true and in remote mode, don't show the 
1.361     albertel 6134:                                     'Switch To Inline Menu' link
1.648     raeburn  6135:              no_auto_mt_title -> prevent &mt()ing the title arg
                   6136:              inherit_jsmath -> when creating popup window in a page,
                   6137:                                     should it have jsmath forced on by the
                   6138:                                     current page
1.361     albertel 6139: 
1.648     raeburn  6140: =back
1.460     albertel 6141: 
1.648     raeburn  6142: =back
1.562     albertel 6143: 
1.306     albertel 6144: =cut
                   6145: 
                   6146: sub start_page {
1.309     albertel 6147:     my ($title,$head_extra,$args) = @_;
1.318     albertel 6148:     #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
1.313     albertel 6149:     my %head_args;
1.352     albertel 6150:     foreach my $arg ('redirect','force_register','domain','function',
1.460     albertel 6151: 		     'bgcolor','frameset','no_nav_bar','only_body',
                   6152: 		     'no_auto_mt_title') {
1.319     albertel 6153: 	if (defined($args->{$arg})) {
1.324     raeburn  6154: 	    $head_args{$arg} = $args->{$arg};
1.319     albertel 6155: 	}
1.313     albertel 6156:     }
1.319     albertel 6157: 
1.315     albertel 6158:     $env{'internal.start_page'}++;
1.338     albertel 6159:     my $result;
                   6160:     if (! exists($args->{'skip_phases'}{'head'}) ) {
                   6161: 	$result.=
1.341     albertel 6162: 	    &xml_begin().
1.338     albertel 6163: 	    &headtag($title,$head_extra,\%head_args).&endheadtag();
                   6164:     }
                   6165:     
                   6166:     if (! exists($args->{'skip_phases'}{'body'}) ) {
                   6167: 	if ($args->{'frameset'}) {
                   6168: 	    my $attr_string = &make_attr_string($args->{'force_register'},
                   6169: 						$args->{'add_entries'});
                   6170: 	    $result .= "\n<frameset $attr_string>\n";
                   6171: 	} else {
                   6172: 	    $result .=
                   6173: 		&bodytag($title, 
                   6174: 			 $args->{'function'},       $args->{'add_entries'},
                   6175: 			 $args->{'only_body'},      $args->{'domain'},
                   6176: 			 $args->{'force_register'}, $args->{'body_title'},
                   6177: 			 $args->{'no_nav_bar'},     $args->{'bgcolor'},
1.460     albertel 6178: 			 $args->{'no_title'},       $args->{'no_inline_link'},
                   6179: 			 $args);
1.338     albertel 6180: 	}
1.330     albertel 6181:     }
1.338     albertel 6182: 
1.315     albertel 6183:     if ($args->{'js_ready'}) {
1.317     albertel 6184: 	$result = &js_ready($result);
1.315     albertel 6185:     }
1.320     albertel 6186:     if ($args->{'html_encode'}) {
                   6187: 	$result = &html_encode($result);
                   6188:     }
1.692.4.2  raeburn  6189:     #Breadcrumbs
                   6190:     if (exists($args->{'bread_crumbs'}) or exists($args->{'bread_crumbs_component'})) {
                   6191:         &Apache::lonhtmlcommon::clear_breadcrumbs();
                   6192:         #if any br links exists, add them to the breadcrumbs
                   6193:         if (exists($args->{'bread_crumbs'}) and ref($args->{'bread_crumbs'}) eq 'ARRAY') {
                   6194:             foreach my $crumb (@{$args->{'bread_crumbs'}}){
                   6195:                 &Apache::lonhtmlcommon::add_breadcrumb($crumb);
                   6196:             }
                   6197:         }
1.306     albertel 6198: 
1.692.4.2  raeburn  6199:         #if bread_crumbs_component exists show it as headline else show only the breadcrumbs
                   6200:         if (exists($args->{'bread_crumbs_component'})){
                   6201:             $result .= &Apache::lonhtmlcommon::breadcrumbs($args->{'bread_crumbs_component'});
                   6202:         } else {
                   6203:             $result .= &Apache::lonhtmlcommon::breadcrumbs();
                   6204:         }
                   6205:     }
                   6206:     return $result;
1.692.4.3  raeburn  6207: }
1.330     albertel 6208: 
1.306     albertel 6209: =pod
                   6210: 
                   6211: =item * &head()
                   6212: 
                   6213: Returns a complete </body></html> section for LON-CAPA web pages.
                   6214: 
1.315     albertel 6215: Inputs:         $args - additional optional args supported are:
                   6216:                  js_ready     -> return a string ready for being used in 
                   6217:                                  a javascript writeln
1.320     albertel 6218:                  html_encode  -> return a string ready for being used in 
                   6219:                                  a html attribute
1.330     albertel 6220:                  frameset     -> if true will start with a <frameset>
                   6221:                                  rather than <body>
1.493     albertel 6222:                  dicsussion   -> if true will get discussion from
                   6223:                                   lonxml::xmlend
                   6224:                                  (you can pass the target and parser arguments
                   6225:                                   through optional 'target' and 'parser' args
                   6226:                                   to this routine)
1.306     albertel 6227: 
                   6228: =cut
                   6229: 
                   6230: sub end_page {
1.315     albertel 6231:     my ($args) = @_;
                   6232:     $env{'internal.end_page'}++;
1.330     albertel 6233:     my $result;
1.335     albertel 6234:     if ($args->{'discussion'}) {
                   6235: 	my ($target,$parser);
                   6236: 	if (ref($args->{'discussion'})) {
                   6237: 	    ($target,$parser) =($args->{'discussion'}{'target'},
                   6238: 				$args->{'discussion'}{'parser'});
                   6239: 	}
                   6240: 	$result .= &Apache::lonxml::xmlend($target,$parser);
                   6241:     }
                   6242: 
1.330     albertel 6243:     if ($args->{'frameset'}) {
                   6244: 	$result .= '</frameset>';
                   6245:     } else {
1.635     raeburn  6246: 	$result .= &endbodytag($args);
1.330     albertel 6247:     }
                   6248:     $result .= "\n</html>";
                   6249: 
1.315     albertel 6250:     if ($args->{'js_ready'}) {
1.317     albertel 6251: 	$result = &js_ready($result);
1.315     albertel 6252:     }
1.335     albertel 6253: 
1.320     albertel 6254:     if ($args->{'html_encode'}) {
                   6255: 	$result = &html_encode($result);
                   6256:     }
1.335     albertel 6257: 
1.315     albertel 6258:     return $result;
                   6259: }
                   6260: 
1.320     albertel 6261: sub html_encode {
                   6262:     my ($result) = @_;
                   6263: 
1.322     albertel 6264:     $result = &HTML::Entities::encode($result,'<>&"');
1.320     albertel 6265:     
                   6266:     return $result;
                   6267: }
1.317     albertel 6268: sub js_ready {
                   6269:     my ($result) = @_;
                   6270: 
1.323     albertel 6271:     $result =~ s/[\n\r]/ /xmsg;
                   6272:     $result =~ s/\\/\\\\/xmsg;
                   6273:     $result =~ s/'/\\'/xmsg;
1.372     albertel 6274:     $result =~ s{</}{<\\/}xmsg;
1.317     albertel 6275:     
                   6276:     return $result;
                   6277: }
                   6278: 
1.315     albertel 6279: sub validate_page {
                   6280:     if (  exists($env{'internal.start_page'})
1.316     albertel 6281: 	  &&     $env{'internal.start_page'} > 1) {
                   6282: 	&Apache::lonnet::logthis('start_page called multiple times '.
1.318     albertel 6283: 				 $env{'internal.start_page'}.' '.
1.316     albertel 6284: 				 $ENV{'request.filename'});
1.315     albertel 6285:     }
                   6286:     if (  exists($env{'internal.end_page'})
1.316     albertel 6287: 	  &&     $env{'internal.end_page'} > 1) {
                   6288: 	&Apache::lonnet::logthis('end_page called multiple times '.
1.318     albertel 6289: 				 $env{'internal.end_page'}.' '.
1.316     albertel 6290: 				 $env{'request.filename'});
1.315     albertel 6291:     }
                   6292:     if (     exists($env{'internal.start_page'})
                   6293: 	&& ! exists($env{'internal.end_page'})) {
1.316     albertel 6294: 	&Apache::lonnet::logthis('start_page called without end_page '.
                   6295: 				 $env{'request.filename'});
1.315     albertel 6296:     }
                   6297:     if (   ! exists($env{'internal.start_page'})
                   6298: 	&&   exists($env{'internal.end_page'})) {
1.316     albertel 6299: 	&Apache::lonnet::logthis('end_page called without start_page'.
                   6300: 				 $env{'request.filename'});
1.315     albertel 6301:     }
1.306     albertel 6302: }
1.315     albertel 6303: 
1.318     albertel 6304: sub simple_error_page {
                   6305:     my ($r,$title,$msg) = @_;
                   6306:     my $page =
                   6307: 	&Apache::loncommon::start_page($title).
                   6308: 	&mt($msg).
                   6309: 	&Apache::loncommon::end_page();
                   6310:     if (ref($r)) {
                   6311: 	$r->print($page);
1.327     albertel 6312: 	return;
1.318     albertel 6313:     }
                   6314:     return $page;
                   6315: }
1.347     albertel 6316: 
                   6317: {
1.610     albertel 6318:     my @row_count;
1.347     albertel 6319:     sub start_data_table {
1.422     albertel 6320: 	my ($add_class) = @_;
                   6321: 	my $css_class = (join(' ','LC_data_table',$add_class));
1.610     albertel 6322: 	unshift(@row_count,0);
1.422     albertel 6323: 	return '<table class="'.$css_class.'">'."\n";
1.347     albertel 6324:     }
                   6325: 
                   6326:     sub end_data_table {
1.610     albertel 6327: 	shift(@row_count);
1.389     albertel 6328: 	return '</table>'."\n";;
1.347     albertel 6329:     }
                   6330: 
                   6331:     sub start_data_table_row {
1.422     albertel 6332: 	my ($add_class) = @_;
1.610     albertel 6333: 	$row_count[0]++;
                   6334: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.428     albertel 6335: 	$css_class = (join(' ',$css_class,$add_class));
1.422     albertel 6336: 	return  '<tr class="'.$css_class.'">'."\n";;
1.347     albertel 6337:     }
1.471     banghart 6338:     
                   6339:     sub continue_data_table_row {
                   6340: 	my ($add_class) = @_;
1.610     albertel 6341: 	my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row';
1.471     banghart 6342: 	$css_class = (join(' ',$css_class,$add_class));
                   6343: 	return  '<tr class="'.$css_class.'">'."\n";;
                   6344:     }
1.347     albertel 6345: 
                   6346:     sub end_data_table_row {
1.389     albertel 6347: 	return '</tr>'."\n";;
1.347     albertel 6348:     }
1.367     www      6349: 
1.421     albertel 6350:     sub start_data_table_empty_row {
1.610     albertel 6351: 	$row_count[0]++;
1.421     albertel 6352: 	return  '<tr class="LC_empty_row" >'."\n";;
                   6353:     }
                   6354: 
                   6355:     sub end_data_table_empty_row {
                   6356: 	return '</tr>'."\n";;
                   6357:     }
                   6358: 
1.367     www      6359:     sub start_data_table_header_row {
1.389     albertel 6360: 	return  '<tr class="LC_header_row">'."\n";;
1.367     www      6361:     }
                   6362: 
                   6363:     sub end_data_table_header_row {
1.389     albertel 6364: 	return '</tr>'."\n";;
1.367     www      6365:     }
1.347     albertel 6366: }
                   6367: 
1.548     albertel 6368: =pod
                   6369: 
                   6370: =item * &inhibit_menu_check($arg)
                   6371: 
                   6372: Checks for a inhibitmenu state and generates output to preserve it
                   6373: 
                   6374: Inputs:         $arg - can be any of
                   6375:                      - undef - in which case the return value is a string 
                   6376:                                to add  into arguments list of a uri
                   6377:                      - 'input' - in which case the return value is a HTML
                   6378:                                  <form> <input> field of type hidden to
                   6379:                                  preserve the value
                   6380:                      - a url - in which case the return value is the url with
                   6381:                                the neccesary cgi args added to preserve the
                   6382:                                inhibitmenu state
                   6383:                      - a ref to a url - no return value, but the string is
                   6384:                                         updated to include the neccessary cgi
                   6385:                                         args to preserve the inhibitmenu state
                   6386: 
                   6387: =cut
                   6388: 
                   6389: sub inhibit_menu_check {
                   6390:     my ($arg) = @_;
                   6391:     &get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['inhibitmenu']);
                   6392:     if ($arg eq 'input') {
                   6393: 	if ($env{'form.inhibitmenu'}) {
                   6394: 	    return '<input type="hidden" name="inhibitmenu" value="'.$env{'form.inhibitmenu'}.'" />';
                   6395: 	} else {
                   6396: 	    return
                   6397: 	}
                   6398:     }
                   6399:     if ($env{'form.inhibitmenu'}) {
                   6400: 	if (ref($arg)) {
                   6401: 	    $$arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   6402: 	} elsif ($arg eq '') {
                   6403: 	    $arg .= 'inhibitmenu='.$env{'form.inhibitmenu'};
                   6404: 	} else {
                   6405: 	    $arg .= '?inhibitmenu='.$env{'form.inhibitmenu'};
                   6406: 	}
                   6407:     }
                   6408:     if (!ref($arg)) {
                   6409: 	return $arg;
                   6410:     }
                   6411: }
                   6412: 
1.251     albertel 6413: ###############################################
1.182     matthew  6414: 
                   6415: =pod
                   6416: 
1.549     albertel 6417: =back
                   6418: 
                   6419: =head1 User Information Routines
                   6420: 
                   6421: =over 4
                   6422: 
1.405     albertel 6423: =item * &get_users_function()
1.182     matthew  6424: 
                   6425: Used by &bodytag to determine the current users primary role.
                   6426: Returns either 'student','coordinator','admin', or 'author'.
                   6427: 
                   6428: =cut
                   6429: 
                   6430: ###############################################
                   6431: sub get_users_function {
                   6432:     my $function = 'student';
1.692.4.26  raeburn  6433:     if ($env{'request.role'}=~/^(cc|co|in|ta|ep)/) {
1.182     matthew  6434:         $function='coordinator';
                   6435:     }
1.258     albertel 6436:     if ($env{'request.role'}=~/^(su|dc|ad|li)/) {
1.182     matthew  6437:         $function='admin';
                   6438:     }
1.692.4.5  raeburn  6439:     if (($env{'request.role'}=~/^(au|ca|aa)/) ||
1.182     matthew  6440:         ($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                   6441:         $function='author';
                   6442:     }
                   6443:     return $function;
1.54      www      6444: }
1.99      www      6445: 
                   6446: ###############################################
                   6447: 
1.233     raeburn  6448: =pod
                   6449: 
1.692.4.2  raeburn  6450: =item * &show_course()
                   6451: 
                   6452: Used by lonmenu.pm and lonroles.pm to determine whether to use the word
                   6453: 'Courses' or 'Roles' in inline navigation and on screen displaying user's roles.
                   6454: Inputs:
                   6455: None
                   6456: 
                   6457: Outputs:
                   6458: Scalar: 1 if 'Course' to be used, 0 otherwise.
                   6459: 
                   6460: =cut
                   6461: 
                   6462: ###############################################
                   6463: sub show_course {
                   6464:     my $course = !$env{'user.adv'};
                   6465:     if (!$env{'user.adv'}) {
                   6466:         foreach my $env (keys(%env)) {
                   6467:             next if ($env !~ m/^user\.priv\./);
                   6468:             if ($env !~ m/^user\.priv\.(?:st|cm)/) {
                   6469:                 $course = 0;
                   6470:                 last;
                   6471:             }
                   6472:         }
                   6473:     }
                   6474:     return $course;
                   6475: }
                   6476: 
                   6477: ###############################################
                   6478: 
                   6479: =pod
                   6480: 
1.542     raeburn  6481: =item * &check_user_status()
1.274     raeburn  6482: 
                   6483: Determines current status of supplied role for a
                   6484: specific user. Roles can be active, previous or future.
                   6485: 
                   6486: Inputs: 
                   6487: user's domain, user's username, course's domain,
1.375     raeburn  6488: course's number, optional section ID.
1.274     raeburn  6489: 
                   6490: Outputs:
                   6491: role status: active, previous or future. 
                   6492: 
                   6493: =cut
                   6494: 
                   6495: sub check_user_status {
1.412     raeburn  6496:     my ($udom,$uname,$cdom,$crs,$role,$sec) = @_;
1.692.4.37! raeburn  6497:     my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
        !          6498:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname,'.',undef,$extra);
1.274     raeburn  6499:     my @uroles = keys %userinfo;
                   6500:     my $srchstr;
                   6501:     my $active_chk = 'none';
1.412     raeburn  6502:     my $now = time;
1.274     raeburn  6503:     if (@uroles > 0) {
1.692.4.22  raeburn  6504:         if (($role eq 'cc') || ($role eq 'co') || ($sec eq '') || (!defined($sec))) {
1.274     raeburn  6505:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                   6506:         } else {
1.412     raeburn  6507:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$sec.'_'.$role;
                   6508:         }
                   6509:         if (grep/^\Q$srchstr\E$/,@uroles) {
1.274     raeburn  6510:             my $role_end = 0;
                   6511:             my $role_start = 0;
                   6512:             $active_chk = 'active';
1.412     raeburn  6513:             if ($userinfo{$srchstr} =~ m/^\Q$role\E_(\d+)/) {
                   6514:                 $role_end = $1;
                   6515:                 if ($userinfo{$srchstr} =~ m/^\Q$role\E_\Q$role_end\E_(\d+)$/) {
                   6516:                     $role_start = $1;
1.274     raeburn  6517:                 }
                   6518:             }
                   6519:             if ($role_start > 0) {
1.412     raeburn  6520:                 if ($now < $role_start) {
1.274     raeburn  6521:                     $active_chk = 'future';
                   6522:                 }
                   6523:             }
                   6524:             if ($role_end > 0) {
1.412     raeburn  6525:                 if ($now > $role_end) {
1.274     raeburn  6526:                     $active_chk = 'previous';
                   6527:                 }
                   6528:             }
                   6529:         }
                   6530:     }
                   6531:     return $active_chk;
                   6532: }
                   6533: 
                   6534: ###############################################
                   6535: 
                   6536: =pod
                   6537: 
1.405     albertel 6538: =item * &get_sections()
1.233     raeburn  6539: 
                   6540: Determines all the sections for a course including
                   6541: sections with students and sections containing other roles.
1.419     raeburn  6542: Incoming parameters: 
                   6543: 
                   6544: 1. domain
                   6545: 2. course number 
                   6546: 3. reference to array containing roles for which sections should 
                   6547: be gathered (optional).
                   6548: 4. reference to array containing status types for which sections 
                   6549: should be gathered (optional).
                   6550: 
                   6551: If the third argument is undefined, sections are gathered for any role. 
                   6552: If the fourth argument is undefined, sections are gathered for any status.
                   6553: Permissible values are 'active' or 'future' or 'previous'.
1.233     raeburn  6554:  
1.374     raeburn  6555: Returns section hash (keys are section IDs, values are
                   6556: number of users in each section), subject to the
1.419     raeburn  6557: optional roles filter, optional status filter 
1.233     raeburn  6558: 
                   6559: =cut
                   6560: 
                   6561: ###############################################
                   6562: sub get_sections {
1.419     raeburn  6563:     my ($cdom,$cnum,$possible_roles,$possible_status) = @_;
1.366     albertel 6564:     if (!defined($cdom) || !defined($cnum)) {
                   6565:         my $cid =  $env{'request.course.id'};
                   6566: 
                   6567: 	return if (!defined($cid));
                   6568: 
                   6569:         $cdom = $env{'course.'.$cid.'.domain'};
                   6570:         $cnum = $env{'course.'.$cid.'.num'};
                   6571:     }
                   6572: 
                   6573:     my %sectioncount;
1.419     raeburn  6574:     my $now = time;
1.240     albertel 6575: 
1.366     albertel 6576:     if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
1.276     albertel 6577: 	my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
1.240     albertel 6578: 	my $sec_index = &Apache::loncoursedata::CL_SECTION();
                   6579: 	my $status_index = &Apache::loncoursedata::CL_STATUS();
1.419     raeburn  6580:         my $start_index = &Apache::loncoursedata::CL_START();
                   6581:         my $end_index = &Apache::loncoursedata::CL_END();
                   6582:         my $status;
1.366     albertel 6583: 	while (my ($student,$data) = each(%$classlist)) {
1.419     raeburn  6584: 	    my ($section,$stu_status,$start,$end) = ($data->[$sec_index],
                   6585: 				                     $data->[$status_index],
                   6586:                                                      $data->[$start_index],
                   6587:                                                      $data->[$end_index]);
                   6588:             if ($stu_status eq 'Active') {
                   6589:                 $status = 'active';
                   6590:             } elsif ($end < $now) {
                   6591:                 $status = 'previous';
                   6592:             } elsif ($start > $now) {
                   6593:                 $status = 'future';
                   6594:             } 
                   6595: 	    if ($section ne '-1' && $section !~ /^\s*$/) {
                   6596:                 if ((!defined($possible_status)) || (($status ne '') && 
                   6597:                     (grep/^\Q$status\E$/,@{$possible_status}))) { 
                   6598: 		    $sectioncount{$section}++;
                   6599:                 }
1.240     albertel 6600: 	    }
                   6601: 	}
                   6602:     }
                   6603:     my %courseroles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   6604:     foreach my $user (sort(keys(%courseroles))) {
                   6605: 	if ($user !~ /^(\w{2})/) { next; }
                   6606: 	my ($role) = ($user =~ /^(\w{2})/);
                   6607: 	if ($possible_roles && !(grep(/^$role$/,@$possible_roles))) { next; }
1.419     raeburn  6608: 	my ($section,$status);
1.240     albertel 6609: 	if ($role eq 'cr' &&
                   6610: 	    $user =~ m-^$role/[^/]*/[^/]*/[^/]*:[^:]*:[^:]*:(\w+)-) {
                   6611: 	    $section=$1;
                   6612: 	}
                   6613: 	if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
                   6614: 	if (!defined($section) || $section eq '-1') { next; }
1.419     raeburn  6615:         my ($end,$start) = ($courseroles{$user} =~ /^([^:]*):([^:]*)$/);
                   6616:         if ($end == -1 && $start == -1) {
                   6617:             next; #deleted role
                   6618:         }
                   6619:         if (!defined($possible_status)) { 
                   6620:             $sectioncount{$section}++;
                   6621:         } else {
                   6622:             if ((!$end || $end >= $now) && (!$start || $start <= $now)) {
                   6623:                 $status = 'active';
                   6624:             } elsif ($end < $now) {
                   6625:                 $status = 'future';
                   6626:             } elsif ($start > $now) {
                   6627:                 $status = 'previous';
                   6628:             }
                   6629:             if (($status ne '') && (grep/^\Q$status\E$/,@{$possible_status})) {
                   6630:                 $sectioncount{$section}++;
                   6631:             }
                   6632:         }
1.233     raeburn  6633:     }
1.366     albertel 6634:     return %sectioncount;
1.233     raeburn  6635: }
                   6636: 
1.274     raeburn  6637: ###############################################
1.294     raeburn  6638: 
                   6639: =pod
1.405     albertel 6640: 
                   6641: =item * &get_course_users()
                   6642: 
1.275     raeburn  6643: Retrieves usernames:domains for users in the specified course
                   6644: with specific role(s), and access status. 
                   6645: 
                   6646: Incoming parameters:
1.277     albertel 6647: 1. course domain
                   6648: 2. course number
                   6649: 3. access status: users must have - either active, 
1.275     raeburn  6650: previous, future, or all.
1.277     albertel 6651: 4. reference to array of permissible roles
1.288     raeburn  6652: 5. reference to array of section restrictions (optional)
                   6653: 6. reference to results object (hash of hashes).
                   6654: 7. reference to optional userdata hash
1.609     raeburn  6655: 8. reference to optional statushash
1.630     raeburn  6656: 9. flag if privileged users (except those set to unhide in
                   6657:    course settings) should be excluded    
1.609     raeburn  6658: Keys of top level results hash are roles.
1.275     raeburn  6659: Keys of inner hashes are username:domain, with 
                   6660: values set to access type.
1.288     raeburn  6661: Optional userdata hash returns an array with arguments in the 
                   6662: same order as loncoursedata::get_classlist() for student data.
                   6663: 
1.609     raeburn  6664: Optional statushash returns
                   6665: 
1.288     raeburn  6666: Entries for end, start, section and status are blank because
                   6667: of the possibility of multiple values for non-student roles.
                   6668: 
1.275     raeburn  6669: =cut
1.405     albertel 6670: 
1.275     raeburn  6671: ###############################################
1.405     albertel 6672: 
1.275     raeburn  6673: sub get_course_users {
1.630     raeburn  6674:     my ($cdom,$cnum,$types,$roles,$sections,$users,$userdata,$statushash,$hidepriv) = @_;
1.288     raeburn  6675:     my %idx = ();
1.419     raeburn  6676:     my %seclists;
1.288     raeburn  6677: 
                   6678:     $idx{udom} = &Apache::loncoursedata::CL_SDOM();
                   6679:     $idx{uname} =  &Apache::loncoursedata::CL_SNAME();
                   6680:     $idx{end} = &Apache::loncoursedata::CL_END();
                   6681:     $idx{start} = &Apache::loncoursedata::CL_START();
                   6682:     $idx{id} = &Apache::loncoursedata::CL_ID();
                   6683:     $idx{section} = &Apache::loncoursedata::CL_SECTION();
                   6684:     $idx{fullname} = &Apache::loncoursedata::CL_FULLNAME();
                   6685:     $idx{status} = &Apache::loncoursedata::CL_STATUS();
                   6686: 
1.290     albertel 6687:     if (grep(/^st$/,@{$roles})) {
1.276     albertel 6688:         my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist($cdom,$cnum);
1.278     raeburn  6689:         my $now = time;
1.277     albertel 6690:         foreach my $student (keys(%{$classlist})) {
1.288     raeburn  6691:             my $match = 0;
1.412     raeburn  6692:             my $secmatch = 0;
1.419     raeburn  6693:             my $section = $$classlist{$student}[$idx{section}];
1.609     raeburn  6694:             my $status = $$classlist{$student}[$idx{status}];
1.419     raeburn  6695:             if ($section eq '') {
                   6696:                 $section = 'none';
                   6697:             }
1.291     albertel 6698:             if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 6699:                 if (grep(/^all$/,@{$sections})) {
1.412     raeburn  6700:                     $secmatch = 1;
                   6701:                 } elsif ($$classlist{$student}[$idx{section}] eq '') {
1.420     albertel 6702:                     if (grep(/^none$/,@{$sections})) {
1.412     raeburn  6703:                         $secmatch = 1;
                   6704:                     }
                   6705:                 } else {  
1.419     raeburn  6706: 		    if (grep(/^\Q$section\E$/,@{$sections})) {
1.412     raeburn  6707: 		        $secmatch = 1;
                   6708:                     }
1.290     albertel 6709: 		}
1.412     raeburn  6710:                 if (!$secmatch) {
                   6711:                     next;
                   6712:                 }
1.419     raeburn  6713:             }
1.275     raeburn  6714:             if (defined($$types{'active'})) {
1.288     raeburn  6715:                 if ($$classlist{$student}[$idx{status}] eq 'Active') {
1.275     raeburn  6716:                     push(@{$$users{st}{$student}},'active');
1.288     raeburn  6717:                     $match = 1;
1.275     raeburn  6718:                 }
                   6719:             }
                   6720:             if (defined($$types{'previous'})) {
1.609     raeburn  6721:                 if ($$classlist{$student}[$idx{status}] eq 'Expired') {
1.275     raeburn  6722:                     push(@{$$users{st}{$student}},'previous');
1.288     raeburn  6723:                     $match = 1;
1.275     raeburn  6724:                 }
                   6725:             }
                   6726:             if (defined($$types{'future'})) {
1.609     raeburn  6727:                 if ($$classlist{$student}[$idx{status}] eq 'Future') {
1.275     raeburn  6728:                     push(@{$$users{st}{$student}},'future');
1.288     raeburn  6729:                     $match = 1;
1.275     raeburn  6730:                 }
                   6731:             }
1.609     raeburn  6732:             if ($match) {
                   6733:                 push(@{$seclists{$student}},$section);
                   6734:                 if (ref($userdata) eq 'HASH') {
                   6735:                     $$userdata{$student} = $$classlist{$student};
                   6736:                 }
                   6737:                 if (ref($statushash) eq 'HASH') {
                   6738:                     $statushash->{$student}{'st'}{$section} = $status;
                   6739:                 }
1.288     raeburn  6740:             }
1.275     raeburn  6741:         }
                   6742:     }
1.412     raeburn  6743:     if ((@{$roles} > 1) || ((@{$roles} == 1) && ($$roles[0] ne "st"))) {
1.439     raeburn  6744:         my %coursepersonnel = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   6745:         my $now = time;
1.609     raeburn  6746:         my %displaystatus = ( previous => 'Expired',
                   6747:                               active   => 'Active',
                   6748:                               future   => 'Future',
                   6749:                             );
1.630     raeburn  6750:         my %nothide;
                   6751:         if ($hidepriv) {
                   6752:             my %coursehash=&Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                   6753:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   6754:                 if ($user !~ /:/) {
                   6755:                     $nothide{join(':',split(/[\@]/,$user))}=1;
                   6756:                 } else {
                   6757:                     $nothide{$user} = 1;
                   6758:                 }
                   6759:             }
                   6760:         }
1.439     raeburn  6761:         foreach my $person (sort(keys(%coursepersonnel))) {
1.288     raeburn  6762:             my $match = 0;
1.412     raeburn  6763:             my $secmatch = 0;
1.439     raeburn  6764:             my $status;
1.412     raeburn  6765:             my ($role,$user,$usec) = ($person =~ /^([^:]*):([^:]+:[^:]+):([^:]*)/);
1.275     raeburn  6766:             $user =~ s/:$//;
1.439     raeburn  6767:             my ($end,$start) = split(/:/,$coursepersonnel{$person});
                   6768:             if ($end == -1 || $start == -1) {
                   6769:                 next;
                   6770:             }
                   6771:             if (($role) && ((grep(/^\Q$role\E$/,@{$roles})) ||
                   6772:                 (grep(/^cr$/,@{$roles}) && $role =~ /^cr\//))) {
1.412     raeburn  6773:                 my ($uname,$udom) = split(/:/,$user);
                   6774:                 if ((ref($sections) eq 'ARRAY') && (@{$sections} > 0)) {
1.420     albertel 6775:                     if (grep(/^all$/,@{$sections})) {
1.412     raeburn  6776:                         $secmatch = 1;
                   6777:                     } elsif ($usec eq '') {
1.420     albertel 6778:                         if (grep(/^none$/,@{$sections})) {
1.412     raeburn  6779:                             $secmatch = 1;
                   6780:                         }
                   6781:                     } else {
                   6782:                         if (grep(/^\Q$usec\E$/,@{$sections})) {
                   6783:                             $secmatch = 1;
                   6784:                         }
                   6785:                     }
                   6786:                     if (!$secmatch) {
                   6787:                         next;
                   6788:                     }
1.288     raeburn  6789:                 }
1.419     raeburn  6790:                 if ($usec eq '') {
                   6791:                     $usec = 'none';
                   6792:                 }
1.275     raeburn  6793:                 if ($uname ne '' && $udom ne '') {
1.630     raeburn  6794:                     if ($hidepriv) {
                   6795:                         if ((&Apache::lonnet::privileged($uname,$udom)) &&
                   6796:                             (!$nothide{$uname.':'.$udom})) {
                   6797:                             next;
                   6798:                         }
                   6799:                     }
1.503     raeburn  6800:                     if ($end > 0 && $end < $now) {
1.439     raeburn  6801:                         $status = 'previous';
                   6802:                     } elsif ($start > $now) {
                   6803:                         $status = 'future';
                   6804:                     } else {
                   6805:                         $status = 'active';
                   6806:                     }
1.277     albertel 6807:                     foreach my $type (keys(%{$types})) { 
1.275     raeburn  6808:                         if ($status eq $type) {
1.420     albertel 6809:                             if (!grep(/^\Q$type\E$/,@{$$users{$role}{$user}})) {
1.419     raeburn  6810:                                 push(@{$$users{$role}{$user}},$type);
                   6811:                             }
1.288     raeburn  6812:                             $match = 1;
                   6813:                         }
                   6814:                     }
1.419     raeburn  6815:                     if (($match) && (ref($userdata) eq 'HASH')) {
                   6816:                         if (!exists($$userdata{$uname.':'.$udom})) {
                   6817: 			    &get_user_info($udom,$uname,\%idx,$userdata);
                   6818:                         }
1.420     albertel 6819:                         if (!grep(/^\Q$usec\E$/,@{$seclists{$uname.':'.$udom}})) {
1.419     raeburn  6820:                             push(@{$seclists{$uname.':'.$udom}},$usec);
                   6821:                         }
1.609     raeburn  6822:                         if (ref($statushash) eq 'HASH') {
                   6823:                             $statushash->{$uname.':'.$udom}{$role}{$usec} = $displaystatus{$status};
                   6824:                         }
1.275     raeburn  6825:                     }
                   6826:                 }
                   6827:             }
                   6828:         }
1.290     albertel 6829:         if (grep(/^ow$/,@{$roles})) {
1.279     raeburn  6830:             if ((defined($cdom)) && (defined($cnum))) {
                   6831:                 my %csettings = &Apache::lonnet::get('environment',['internal.courseowner'],$cdom,$cnum);
                   6832:                 if ( defined($csettings{'internal.courseowner'}) ) {
                   6833:                     my $owner = $csettings{'internal.courseowner'};
1.609     raeburn  6834:                     next if ($owner eq '');
                   6835:                     my ($ownername,$ownerdom);
                   6836:                     if ($owner =~ /^([^:]+):([^:]+)$/) {
                   6837:                         $ownername = $1;
                   6838:                         $ownerdom = $2;
                   6839:                     } else {
                   6840:                         $ownername = $owner;
                   6841:                         $ownerdom = $cdom;
                   6842:                         $owner = $ownername.':'.$ownerdom;
1.439     raeburn  6843:                     }
                   6844:                     @{$$users{'ow'}{$owner}} = 'any';
1.290     albertel 6845:                     if (defined($userdata) && 
1.609     raeburn  6846: 			!exists($$userdata{$owner})) {
                   6847: 			&get_user_info($ownerdom,$ownername,\%idx,$userdata);
                   6848:                         if (!grep(/^none$/,@{$seclists{$owner}})) {
                   6849:                             push(@{$seclists{$owner}},'none');
                   6850:                         }
                   6851:                         if (ref($statushash) eq 'HASH') {
                   6852:                             $statushash->{$owner}{'ow'}{'none'} = 'Any';
1.419     raeburn  6853:                         }
1.290     albertel 6854: 		    }
1.279     raeburn  6855:                 }
                   6856:             }
                   6857:         }
1.419     raeburn  6858:         foreach my $user (keys(%seclists)) {
                   6859:             @{$seclists{$user}} = (sort {$a <=> $b} @{$seclists{$user}});
                   6860:             $$userdata{$user}[$idx{section}] = join(',',@{$seclists{$user}});
                   6861:         }
1.275     raeburn  6862:     }
                   6863:     return;
                   6864: }
                   6865: 
1.288     raeburn  6866: sub get_user_info {
                   6867:     my ($udom,$uname,$idx,$userdata) = @_;
1.289     albertel 6868:     $$userdata{$uname.':'.$udom}[$$idx{fullname}] = 
                   6869: 	&plainname($uname,$udom,'lastname');
1.291     albertel 6870:     $$userdata{$uname.':'.$udom}[$$idx{uname}] = $uname;
1.297     raeburn  6871:     $$userdata{$uname.':'.$udom}[$$idx{udom}] = $udom;
1.609     raeburn  6872:     my %idhash =  &Apache::lonnet::idrget($udom,($uname));
                   6873:     $$userdata{$uname.':'.$udom}[$$idx{id}] = $idhash{$uname}; 
1.288     raeburn  6874:     return;
                   6875: }
1.275     raeburn  6876: 
1.472     raeburn  6877: ###############################################
                   6878: 
                   6879: =pod
                   6880: 
                   6881: =item * &get_user_quota()
                   6882: 
                   6883: Retrieves quota assigned for storage of portfolio files for a user  
                   6884: 
                   6885: Incoming parameters:
                   6886: 1. user's username
                   6887: 2. user's domain
                   6888: 
                   6889: Returns:
1.536     raeburn  6890: 1. Disk quota (in Mb) assigned to student.
                   6891: 2. (Optional) Type of setting: custom or default
                   6892:    (individually assigned or default for user's 
                   6893:    institutional status).
                   6894: 3. (Optional) - User's institutional status (e.g., faculty, staff
                   6895:    or student - types as defined in localenroll::inst_usertypes 
                   6896:    for user's domain, which determines default quota for user.
                   6897: 4. (Optional) - Default quota which would apply to the user.
1.472     raeburn  6898: 
                   6899: If a value has been stored in the user's environment, 
1.536     raeburn  6900: it will return that, otherwise it returns the maximal default
                   6901: defined for the user's instituional status(es) in the domain.
1.472     raeburn  6902: 
                   6903: =cut
                   6904: 
                   6905: ###############################################
                   6906: 
                   6907: 
                   6908: sub get_user_quota {
                   6909:     my ($uname,$udom) = @_;
1.536     raeburn  6910:     my ($quota,$quotatype,$settingstatus,$defquota);
1.472     raeburn  6911:     if (!defined($udom)) {
                   6912:         $udom = $env{'user.domain'};
                   6913:     }
                   6914:     if (!defined($uname)) {
                   6915:         $uname = $env{'user.name'};
                   6916:     }
                   6917:     if (($udom eq '' || $uname eq '') ||
                   6918:         ($udom eq 'public') && ($uname eq 'public')) {
                   6919:         $quota = 0;
1.536     raeburn  6920:         $quotatype = 'default';
                   6921:         $defquota = 0; 
1.472     raeburn  6922:     } else {
1.536     raeburn  6923:         my $inststatus;
1.472     raeburn  6924:         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
                   6925:             $quota = $env{'environment.portfolioquota'};
1.536     raeburn  6926:             $inststatus = $env{'environment.inststatus'};
1.472     raeburn  6927:         } else {
1.536     raeburn  6928:             my %userenv = 
                   6929:                 &Apache::lonnet::get('environment',['portfolioquota',
                   6930:                                      'inststatus'],$udom,$uname);
1.472     raeburn  6931:             my ($tmp) = keys(%userenv);
                   6932:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   6933:                 $quota = $userenv{'portfolioquota'};
1.536     raeburn  6934:                 $inststatus = $userenv{'inststatus'};
1.472     raeburn  6935:             } else {
                   6936:                 undef(%userenv);
                   6937:             }
                   6938:         }
1.536     raeburn  6939:         ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
1.472     raeburn  6940:         if ($quota eq '') {
1.536     raeburn  6941:             $quota = $defquota;
                   6942:             $quotatype = 'default';
                   6943:         } else {
                   6944:             $quotatype = 'custom';
1.472     raeburn  6945:         }
                   6946:     }
1.536     raeburn  6947:     if (wantarray) {
                   6948:         return ($quota,$quotatype,$settingstatus,$defquota);
                   6949:     } else {
                   6950:         return $quota;
                   6951:     }
1.472     raeburn  6952: }
                   6953: 
                   6954: ###############################################
                   6955: 
                   6956: =pod
                   6957: 
                   6958: =item * &default_quota()
                   6959: 
1.536     raeburn  6960: Retrieves default quota assigned for storage of user portfolio files,
                   6961: given an (optional) user's institutional status.
1.472     raeburn  6962: 
                   6963: Incoming parameters:
                   6964: 1. domain
1.536     raeburn  6965: 2. (Optional) institutional status(es).  This is a : separated list of 
                   6966:    status types (e.g., faculty, staff, student etc.)
                   6967:    which apply to the user for whom the default is being retrieved.
                   6968:    If the institutional status string in undefined, the domain
                   6969:    default quota will be returned. 
1.472     raeburn  6970: 
                   6971: Returns:
                   6972: 1. Default disk quota (in Mb) for user portfolios in the domain.
1.536     raeburn  6973: 2. (Optional) institutional type which determined the value of the
                   6974:    default quota.
1.472     raeburn  6975: 
                   6976: If a value has been stored in the domain's configuration db,
                   6977: it will return that, otherwise it returns 20 (for backwards 
                   6978: compatibility with domains which have not set up a configuration
                   6979: db file; the original statically defined portfolio quota was 20 Mb). 
                   6980: 
1.536     raeburn  6981: If the user's status includes multiple types (e.g., staff and student),
                   6982: the largest default quota which applies to the user determines the
                   6983: default quota returned.
                   6984: 
1.692.4.15  raeburn  6985: =back
                   6986: 
1.472     raeburn  6987: =cut
                   6988: 
                   6989: ###############################################
                   6990: 
                   6991: 
                   6992: sub default_quota {
1.536     raeburn  6993:     my ($udom,$inststatus) = @_;
                   6994:     my ($defquota,$settingstatus);
                   6995:     my %quotahash = &Apache::lonnet::get_dom('configuration',
1.622     raeburn  6996:                                             ['quotas'],$udom);
                   6997:     if (ref($quotahash{'quotas'}) eq 'HASH') {
1.536     raeburn  6998:         if ($inststatus ne '') {
1.692.4.2  raeburn  6999:             my @statuses = map { &unescape($_); } split(/:/,$inststatus);
1.536     raeburn  7000:             foreach my $item (@statuses) {
1.692.4.2  raeburn  7001:                 if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   7002:                     if ($quotahash{'quotas'}{'defaultquota'}{$item} ne '') {
                   7003:                         if ($defquota eq '') {
                   7004:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   7005:                             $settingstatus = $item;
                   7006:                         } elsif ($quotahash{'quotas'}{'defaultquota'}{$item} > $defquota) {
                   7007:                             $defquota = $quotahash{'quotas'}{'defaultquota'}{$item};
                   7008:                             $settingstatus = $item;
                   7009:                         }
                   7010:                     }
                   7011:                 } else {
                   7012:                     if ($quotahash{'quotas'}{$item} ne '') {
                   7013:                         if ($defquota eq '') {
                   7014:                             $defquota = $quotahash{'quotas'}{$item};
                   7015:                             $settingstatus = $item;
                   7016:                         } elsif ($quotahash{'quotas'}{$item} > $defquota) {
                   7017:                             $defquota = $quotahash{'quotas'}{$item};
                   7018:                             $settingstatus = $item;
                   7019:                         }
1.536     raeburn  7020:                     }
                   7021:                 }
                   7022:             }
                   7023:         }
                   7024:         if ($defquota eq '') {
1.692.4.2  raeburn  7025:             if (ref($quotahash{'quotas'}{'defaultquota'}) eq 'HASH') {
                   7026:                 $defquota = $quotahash{'quotas'}{'defaultquota'}{'default'};
                   7027:             } else {
                   7028:                 $defquota = $quotahash{'quotas'}{'default'};
                   7029:             }
1.536     raeburn  7030:             $settingstatus = 'default';
                   7031:         }
                   7032:     } else {
                   7033:         $settingstatus = 'default';
                   7034:         $defquota = 20;
                   7035:     }
                   7036:     if (wantarray) {
                   7037:         return ($defquota,$settingstatus);
1.472     raeburn  7038:     } else {
1.536     raeburn  7039:         return $defquota;
1.472     raeburn  7040:     }
                   7041: }
                   7042: 
1.384     raeburn  7043: sub get_secgrprole_info {
                   7044:     my ($cdom,$cnum,$needroles,$type)  = @_;
                   7045:     my %sections_count = &get_sections($cdom,$cnum);
                   7046:     my @sections =  (sort {$a <=> $b} keys(%sections_count));
                   7047:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   7048:     my @groups = sort(keys(%curr_groups));
                   7049:     my $allroles = [];
                   7050:     my $rolehash;
                   7051:     my $accesshash = {
                   7052:                      active => 'Currently has access',
                   7053:                      future => 'Will have future access',
                   7054:                      previous => 'Previously had access',
                   7055:                   };
                   7056:     if ($needroles) {
                   7057:         $rolehash = {'all' => 'all'};
1.385     albertel 7058:         my %user_roles = &Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
                   7059: 	if (&Apache::lonnet::error(%user_roles)) {
                   7060: 	    undef(%user_roles);
                   7061: 	}
                   7062:         foreach my $item (keys(%user_roles)) {
1.384     raeburn  7063:             my ($role)=split(/\:/,$item,2);
                   7064:             if ($role eq 'cr') { next; }
                   7065:             if ($role =~ /^cr/) {
                   7066:                 $$rolehash{$role} = (split('/',$role))[3];
                   7067:             } else {
                   7068:                 $$rolehash{$role} = &Apache::lonnet::plaintext($role,$type);
                   7069:             }
                   7070:         }
                   7071:         foreach my $key (sort(keys(%{$rolehash}))) {
                   7072:             push(@{$allroles},$key);
                   7073:         }
                   7074:         push (@{$allroles},'st');
                   7075:         $$rolehash{'st'} = &Apache::lonnet::plaintext('st',$type);
                   7076:     }
                   7077:     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
                   7078: }
                   7079: 
1.555     raeburn  7080: sub user_picker {
1.627     raeburn  7081:     my ($dom,$srch,$forcenewuser,$caller,$cancreate,$usertype) = @_;
1.555     raeburn  7082:     my $currdom = $dom;
                   7083:     my %curr_selected = (
                   7084:                         srchin => 'dom',
1.580     raeburn  7085:                         srchby => 'lastname',
1.555     raeburn  7086:                       );
                   7087:     my $srchterm;
1.625     raeburn  7088:     if ((ref($srch) eq 'HASH') && ($env{'form.origform'} ne 'crtusername')) {
1.555     raeburn  7089:         if ($srch->{'srchby'} ne '') {
                   7090:             $curr_selected{'srchby'} = $srch->{'srchby'};
                   7091:         }
                   7092:         if ($srch->{'srchin'} ne '') {
                   7093:             $curr_selected{'srchin'} = $srch->{'srchin'};
                   7094:         }
                   7095:         if ($srch->{'srchtype'} ne '') {
                   7096:             $curr_selected{'srchtype'} = $srch->{'srchtype'};
                   7097:         }
                   7098:         if ($srch->{'srchdomain'} ne '') {
                   7099:             $currdom = $srch->{'srchdomain'};
                   7100:         }
                   7101:         $srchterm = $srch->{'srchterm'};
                   7102:     }
                   7103:     my %lt=&Apache::lonlocal::texthash(
1.573     raeburn  7104:                     'usr'       => 'Search criteria',
1.563     raeburn  7105:                     'doma'      => 'Domain/institution to search',
1.558     albertel 7106:                     'uname'     => 'username',
                   7107:                     'lastname'  => 'last name',
1.555     raeburn  7108:                     'lastfirst' => 'last name, first name',
1.558     albertel 7109:                     'crs'       => 'in this course',
1.576     raeburn  7110:                     'dom'       => 'in selected LON-CAPA domain', 
1.558     albertel 7111:                     'alc'       => 'all LON-CAPA',
1.573     raeburn  7112:                     'instd'     => 'in institutional directory for selected domain',
1.558     albertel 7113:                     'exact'     => 'is',
                   7114:                     'contains'  => 'contains',
1.569     raeburn  7115:                     'begins'    => 'begins with',
1.571     raeburn  7116:                     'youm'      => "You must include some text to search for.",
                   7117:                     'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
                   7118:                     'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
                   7119:                     'yomc'      => "You must choose a domain when using an institutional directory search.",
                   7120:                     'ymcd'      => "You must choose a domain when using a domain search.",
                   7121:                     'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
                   7122:                     'whse'      => "When searching by last,first you must include at least one character in the first name.",
                   7123:                      'thfo'     => "The following need to be corrected before the search can be run:",
1.555     raeburn  7124:                                        );
1.563     raeburn  7125:     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
                   7126:     my $srchinsel = ' <select name="srchin">';
1.555     raeburn  7127: 
                   7128:     my @srchins = ('crs','dom','alc','instd');
                   7129: 
                   7130:     foreach my $option (@srchins) {
                   7131:         # FIXME 'alc' option unavailable until 
                   7132:         #       loncreateuser::print_user_query_page()
                   7133:         #       has been completed.
                   7134:         next if ($option eq 'alc');
1.692.4.11  raeburn  7135:         next if (($option eq 'crs') && ($env{'form.form'} eq 'requestcrs'));
1.555     raeburn  7136:         next if ($option eq 'crs' && !$env{'request.course.id'});
1.563     raeburn  7137:         if ($curr_selected{'srchin'} eq $option) {
                   7138:             $srchinsel .= ' 
                   7139:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   7140:         } else {
                   7141:             $srchinsel .= '
                   7142:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   7143:         }
1.555     raeburn  7144:     }
1.563     raeburn  7145:     $srchinsel .= "\n  </select>\n";
1.555     raeburn  7146: 
                   7147:     my $srchbysel =  ' <select name="srchby">';
1.580     raeburn  7148:     foreach my $option ('lastname','lastfirst','uname') {
1.555     raeburn  7149:         if ($curr_selected{'srchby'} eq $option) {
                   7150:             $srchbysel .= '
                   7151:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   7152:         } else {
                   7153:             $srchbysel .= '
                   7154:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   7155:          }
                   7156:     }
                   7157:     $srchbysel .= "\n  </select>\n";
                   7158: 
                   7159:     my $srchtypesel = ' <select name="srchtype">';
1.580     raeburn  7160:     foreach my $option ('begins','contains','exact') {
1.555     raeburn  7161:         if ($curr_selected{'srchtype'} eq $option) {
                   7162:             $srchtypesel .= '
                   7163:    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
                   7164:         } else {
                   7165:             $srchtypesel .= '
                   7166:    <option value="'.$option.'">'.$lt{$option}.'</option>';
                   7167:         }
                   7168:     }
                   7169:     $srchtypesel .= "\n  </select>\n";
                   7170: 
1.558     albertel 7171:     my ($newuserscript,$new_user_create);
1.556     raeburn  7172: 
                   7173:     if ($forcenewuser) {
1.576     raeburn  7174:         if (ref($srch) eq 'HASH') {
                   7175:             if ($srch->{'srchby'} eq 'uname' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchin'} eq 'dom' && $srch->{'srchdomain'} eq $env{'request.role.domain'}) {
1.627     raeburn  7176:                 if ($cancreate) {
                   7177:                     $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>';
                   7178:                 } else {
1.692.4.2  raeburn  7179:                     my $helplink = 'javascript:helpMenu('."'display'".')';
1.627     raeburn  7180:                     my %usertypetext = (
                   7181:                         official   => 'institutional',
                   7182:                         unofficial => 'non-institutional',
                   7183:                     );
1.692.4.2  raeburn  7184:                     $new_user_create = '<p class="LC_warning">'.
                   7185:                                        &mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.").' '.
                   7186:                                        &mt('Please contact the [_1]helpdesk[_2] for assistance.','<a href="'.$helplink.'">','</a>').'</p><br />';
1.627     raeburn  7187:                 }
1.576     raeburn  7188:             }
                   7189:         }
                   7190: 
1.556     raeburn  7191:         $newuserscript = <<"ENDSCRIPT";
                   7192: 
1.570     raeburn  7193: function setSearch(createnew,callingForm) {
1.556     raeburn  7194:     if (createnew == 1) {
1.570     raeburn  7195:         for (var i=0; i<callingForm.srchby.length; i++) {
                   7196:             if (callingForm.srchby.options[i].value == 'uname') {
                   7197:                 callingForm.srchby.selectedIndex = i;
1.556     raeburn  7198:             }
                   7199:         }
1.570     raeburn  7200:         for (var i=0; i<callingForm.srchin.length; i++) {
                   7201:             if ( callingForm.srchin.options[i].value == 'dom') {
                   7202: 		callingForm.srchin.selectedIndex = i;
1.556     raeburn  7203:             }
                   7204:         }
1.570     raeburn  7205:         for (var i=0; i<callingForm.srchtype.length; i++) {
                   7206:             if (callingForm.srchtype.options[i].value == 'exact') {
                   7207:                 callingForm.srchtype.selectedIndex = i;
1.556     raeburn  7208:             }
                   7209:         }
1.570     raeburn  7210:         for (var i=0; i<callingForm.srchdomain.length; i++) {
                   7211:             if (callingForm.srchdomain.options[i].value == '$env{'request.role.domain'}') {
                   7212:                 callingForm.srchdomain.selectedIndex = i;
1.556     raeburn  7213:             }
                   7214:         }
                   7215:     }
                   7216: }
                   7217: ENDSCRIPT
1.558     albertel 7218: 
1.556     raeburn  7219:     }
                   7220: 
1.555     raeburn  7221:     my $output = <<"END_BLOCK";
1.556     raeburn  7222: <script type="text/javascript">
1.692.4.4  raeburn  7223: // <![CDATA[
1.570     raeburn  7224: function validateEntry(callingForm) {
1.558     albertel 7225: 
1.556     raeburn  7226:     var checkok = 1;
1.558     albertel 7227:     var srchin;
1.570     raeburn  7228:     for (var i=0; i<callingForm.srchin.length; i++) {
                   7229: 	if ( callingForm.srchin[i].checked ) {
                   7230: 	    srchin = callingForm.srchin[i].value;
1.558     albertel 7231: 	}
                   7232:     }
                   7233: 
1.570     raeburn  7234:     var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
                   7235:     var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
                   7236:     var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
                   7237:     var srchterm =  callingForm.srchterm.value;
                   7238:     var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
1.556     raeburn  7239:     var msg = "";
                   7240: 
                   7241:     if (srchterm == "") {
                   7242:         checkok = 0;
1.571     raeburn  7243:         msg += "$lt{'youm'}\\n";
1.556     raeburn  7244:     }
                   7245: 
1.569     raeburn  7246:     if (srchtype== 'begins') {
                   7247:         if (srchterm.length < 2) {
                   7248:             checkok = 0;
1.571     raeburn  7249:             msg += "$lt{'thte'}\\n";
1.569     raeburn  7250:         }
                   7251:     }
                   7252: 
1.556     raeburn  7253:     if (srchtype== 'contains') {
                   7254:         if (srchterm.length < 3) {
                   7255:             checkok = 0;
1.571     raeburn  7256:             msg += "$lt{'thet'}\\n";
1.556     raeburn  7257:         }
                   7258:     }
                   7259:     if (srchin == 'instd') {
                   7260:         if (srchdomain == '') {
                   7261:             checkok = 0;
1.571     raeburn  7262:             msg += "$lt{'yomc'}\\n";
1.556     raeburn  7263:         }
                   7264:     }
                   7265:     if (srchin == 'dom') {
                   7266:         if (srchdomain == '') {
                   7267:             checkok = 0;
1.571     raeburn  7268:             msg += "$lt{'ymcd'}\\n";
1.556     raeburn  7269:         }
                   7270:     }
                   7271:     if (srchby == 'lastfirst') {
                   7272:         if (srchterm.indexOf(",") == -1) {
                   7273:             checkok = 0;
1.571     raeburn  7274:             msg += "$lt{'whus'}\\n";
1.556     raeburn  7275:         }
                   7276:         if (srchterm.indexOf(",") == srchterm.length -1) {
                   7277:             checkok = 0;
1.571     raeburn  7278:             msg += "$lt{'whse'}\\n";
1.556     raeburn  7279:         }
                   7280:     }
                   7281:     if (checkok == 0) {
1.571     raeburn  7282:         alert("$lt{'thfo'}\\n"+msg);
1.556     raeburn  7283:         return;
                   7284:     }
                   7285:     if (checkok == 1) {
1.570     raeburn  7286:         callingForm.submit();
1.556     raeburn  7287:     }
                   7288: }
                   7289: 
                   7290: $newuserscript
                   7291: 
1.692.4.4  raeburn  7292: // ]]>
1.556     raeburn  7293: </script>
1.558     albertel 7294: 
                   7295: $new_user_create
                   7296: 
1.555     raeburn  7297: END_BLOCK
1.558     albertel 7298: 
1.692.4.9  raeburn  7299:     $output .= &Apache::lonhtmlcommon::start_pick_box().
                   7300:                &Apache::lonhtmlcommon::row_title($lt{'doma'}).
                   7301:                $domform.
                   7302:                &Apache::lonhtmlcommon::row_closure().
                   7303:                &Apache::lonhtmlcommon::row_title($lt{'usr'}).
                   7304:                $srchbysel.
                   7305:                $srchtypesel.
                   7306:                '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
                   7307:                $srchinsel.
                   7308:                &Apache::lonhtmlcommon::row_closure(1).
                   7309:                &Apache::lonhtmlcommon::end_pick_box().
                   7310:                '<br />';
1.555     raeburn  7311:     return $output;
                   7312: }
                   7313: 
1.612     raeburn  7314: sub user_rule_check {
1.615     raeburn  7315:     my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules,$got_rules) = @_;
1.612     raeburn  7316:     my $response;
                   7317:     if (ref($usershash) eq 'HASH') {
                   7318:         foreach my $user (keys(%{$usershash})) {
                   7319:             my ($uname,$udom) = split(/:/,$user);
                   7320:             next if ($udom eq '' || $uname eq '');
1.615     raeburn  7321:             my ($id,$newuser);
1.612     raeburn  7322:             if (ref($usershash->{$user}) eq 'HASH') {
1.615     raeburn  7323:                 $newuser = $usershash->{$user}->{'newuser'};
1.612     raeburn  7324:                 $id = $usershash->{$user}->{'id'};
                   7325:             }
                   7326:             my $inst_response;
                   7327:             if (ref($checks) eq 'HASH') {
                   7328:                 if (defined($checks->{'username'})) {
1.615     raeburn  7329:                     ($inst_response,%{$inst_results->{$user}}) = 
1.612     raeburn  7330:                         &Apache::lonnet::get_instuser($udom,$uname);
                   7331:                 } elsif (defined($checks->{'id'})) {
1.615     raeburn  7332:                     ($inst_response,%{$inst_results->{$user}}) =
1.612     raeburn  7333:                         &Apache::lonnet::get_instuser($udom,undef,$id);
                   7334:                 }
1.615     raeburn  7335:             } else {
                   7336:                 ($inst_response,%{$inst_results->{$user}}) =
                   7337:                     &Apache::lonnet::get_instuser($udom,$uname);
                   7338:                 return;
1.612     raeburn  7339:             }
1.615     raeburn  7340:             if (!$got_rules->{$udom}) {
1.612     raeburn  7341:                 my %domconfig = &Apache::lonnet::get_dom('configuration',
                   7342:                                                   ['usercreation'],$udom);
                   7343:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.615     raeburn  7344:                     foreach my $item ('username','id') {
1.612     raeburn  7345:                         if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   7346:                             $$curr_rules{$udom}{$item} = 
                   7347:                                 $domconfig{'usercreation'}{$item.'_rule'};
1.585     raeburn  7348:                         }
                   7349:                     }
                   7350:                 }
1.615     raeburn  7351:                 $got_rules->{$udom} = 1;  
1.585     raeburn  7352:             }
1.612     raeburn  7353:             foreach my $item (keys(%{$checks})) {
                   7354:                 if (ref($$curr_rules{$udom}) eq 'HASH') {
                   7355:                     if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
                   7356:                         if (@{$$curr_rules{$udom}{$item}} > 0) {
                   7357:                             my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
                   7358:                             foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
                   7359:                                 if ($rule_check{$rule}) {
                   7360:                                     $$rulematch{$user}{$item} = $rule;
                   7361:                                     if ($inst_response eq 'ok') {
1.615     raeburn  7362:                                         if (ref($inst_results) eq 'HASH') {
                   7363:                                             if (ref($inst_results->{$user}) eq 'HASH') {
                   7364:                                                 if (keys(%{$inst_results->{$user}}) == 0) {
                   7365:                                                     $$alerts{$item}{$udom}{$uname} = 1;
                   7366:                                                 }
1.612     raeburn  7367:                                             }
                   7368:                                         }
1.615     raeburn  7369:                                     }
                   7370:                                     last;
1.585     raeburn  7371:                                 }
                   7372:                             }
                   7373:                         }
                   7374:                     }
                   7375:                 }
                   7376:             }
                   7377:         }
                   7378:     }
1.612     raeburn  7379:     return;
                   7380: }
                   7381: 
                   7382: sub user_rule_formats {
                   7383:     my ($domain,$domdesc,$curr_rules,$check) = @_;
                   7384:     my %text = ( 
                   7385:                  'username' => 'Usernames',
                   7386:                  'id'       => 'IDs',
                   7387:                );
                   7388:     my $output;
                   7389:     my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
                   7390:     if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
                   7391:         if (@{$ruleorder} > 0) {
                   7392:             $output = '<br />'.&mt("$text{$check} with the following format(s) may <span class=\"LC_cusr_emph\">only</span> be used for verified users at [_1]:",$domdesc).' <ul>';
                   7393:             foreach my $rule (@{$ruleorder}) {
                   7394:                 if (ref($curr_rules) eq 'ARRAY') {
                   7395:                     if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
                   7396:                         if (ref($rules->{$rule}) eq 'HASH') {
                   7397:                             $output .= '<li>'.$rules->{$rule}{'name'}.': '.
                   7398:                                         $rules->{$rule}{'desc'}.'</li>';
                   7399:                         }
                   7400:                     }
                   7401:                 }
                   7402:             }
                   7403:             $output .= '</ul>';
                   7404:         }
                   7405:     }
                   7406:     return $output;
                   7407: }
                   7408: 
                   7409: sub instrule_disallow_msg {
1.615     raeburn  7410:     my ($checkitem,$domdesc,$count,$mode) = @_;
1.612     raeburn  7411:     my $response;
                   7412:     my %text = (
                   7413:                   item   => 'username',
                   7414:                   items  => 'usernames',
                   7415:                   match  => 'matches',
                   7416:                   do     => 'does',
                   7417:                   action => 'a username',
                   7418:                   one    => 'one',
                   7419:                );
                   7420:     if ($count > 1) {
                   7421:         $text{'item'} = 'usernames';
                   7422:         $text{'match'} ='match';
                   7423:         $text{'do'} = 'do';
                   7424:         $text{'action'} = 'usernames',
                   7425:         $text{'one'} = 'ones';
                   7426:     }
                   7427:     if ($checkitem eq 'id') {
                   7428:         $text{'items'} = 'IDs';
                   7429:         $text{'item'} = 'ID';
                   7430:         $text{'action'} = 'an ID';
1.615     raeburn  7431:         if ($count > 1) {
                   7432:             $text{'item'} = 'IDs';
                   7433:             $text{'action'} = 'IDs';
                   7434:         }
1.612     raeburn  7435:     }
1.674     bisitz   7436:     $response = &mt("The $text{'item'} you chose $text{'match'} the format of $text{'items'} defined for [_1], but the $text{'item'} $text{'do'} not exist in the institutional directory.",'<span class="LC_cusr_emph">'.$domdesc.'</span>').'<br />';
1.615     raeburn  7437:     if ($mode eq 'upload') {
                   7438:         if ($checkitem eq 'username') {
                   7439:             $response .= &mt("You will need to modify your upload file so it will include $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   7440:         } elsif ($checkitem eq 'id') {
1.674     bisitz   7441:             $response .= &mt("Either upload a file which includes $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or when associating fields with data columns, omit an association for the Student/Employee ID field.");
1.615     raeburn  7442:         }
1.669     raeburn  7443:     } elsif ($mode eq 'selfcreate') {
                   7444:         if ($checkitem eq 'id') {
                   7445:             $response .= &mt("You must either choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or leave the ID field blank.");
                   7446:         }
1.615     raeburn  7447:     } else {
                   7448:         if ($checkitem eq 'username') {
                   7449:             $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
                   7450:         } elsif ($checkitem eq 'id') {
                   7451:             $response .= &mt("You must either choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or leave the ID field blank.");
                   7452:         }
1.612     raeburn  7453:     }
                   7454:     return $response;
1.585     raeburn  7455: }
                   7456: 
1.624     raeburn  7457: sub personal_data_fieldtitles {
                   7458:     my %fieldtitles = &Apache::lonlocal::texthash (
                   7459:                         id => 'Student/Employee ID',
                   7460:                         permanentemail => 'E-mail address',
                   7461:                         lastname => 'Last Name',
                   7462:                         firstname => 'First Name',
                   7463:                         middlename => 'Middle Name',
                   7464:                         generation => 'Generation',
                   7465:                         gen => 'Generation',
1.692.4.2  raeburn  7466:                         inststatus => 'Affiliation',
1.624     raeburn  7467:                    );
                   7468:     return %fieldtitles;
                   7469: }
                   7470: 
1.642     raeburn  7471: sub sorted_inst_types {
                   7472:     my ($dom) = @_;
                   7473:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
                   7474:     my $othertitle = &mt('All users');
                   7475:     if ($env{'request.course.id'}) {
1.668     raeburn  7476:         $othertitle  = &mt('Any users');
1.642     raeburn  7477:     }
                   7478:     my @types;
                   7479:     if (ref($order) eq 'ARRAY') {
                   7480:         @types = @{$order};
                   7481:     }
                   7482:     if (@types == 0) {
                   7483:         if (ref($usertypes) eq 'HASH') {
                   7484:             @types = sort(keys(%{$usertypes}));
                   7485:         }
                   7486:     }
                   7487:     if (keys(%{$usertypes}) > 0) {
                   7488:         $othertitle = &mt('Other users');
                   7489:     }
                   7490:     return ($othertitle,$usertypes,\@types);
                   7491: }
                   7492: 
1.645     raeburn  7493: sub get_institutional_codes {
                   7494:     my ($settings,$allcourses,$LC_code) = @_;
                   7495: # Get complete list of course sections to update
                   7496:     my @currsections = ();
                   7497:     my @currxlists = ();
                   7498:     my $coursecode = $$settings{'internal.coursecode'};
                   7499: 
                   7500:     if ($$settings{'internal.sectionnums'} ne '') {
                   7501:         @currsections = split(/,/,$$settings{'internal.sectionnums'});
                   7502:     }
                   7503: 
                   7504:     if ($$settings{'internal.crosslistings'} ne '') {
                   7505:         @currxlists = split(/,/,$$settings{'internal.crosslistings'});
                   7506:     }
                   7507: 
                   7508:     if (@currxlists > 0) {
                   7509:         foreach (@currxlists) {
                   7510:             if (m/^([^:]+):(\w*)$/) {
                   7511:                 unless (grep/^$1$/,@{$allcourses}) {
                   7512:                     push @{$allcourses},$1;
                   7513:                     $$LC_code{$1} = $2;
                   7514:                 }
                   7515:             }
                   7516:         }
                   7517:     }
                   7518:  
                   7519:     if (@currsections > 0) {
                   7520:         foreach (@currsections) {
                   7521:             if (m/^(\w+):(\w*)$/) {
                   7522:                 my $sec = $coursecode.$1;
                   7523:                 my $lc_sec = $2;
                   7524:                 unless (grep/^$sec$/,@{$allcourses}) {
                   7525:                     push @{$allcourses},$sec;
                   7526:                     $$LC_code{$sec} = $lc_sec;
                   7527:                 }
                   7528:             }
                   7529:         }
                   7530:     }
                   7531:     return;
                   7532: }
                   7533: 
1.112     bowersj2 7534: =pod
                   7535: 
1.692.4.2  raeburn  7536: =head1 Slot Helpers
                   7537: 
                   7538: =over 4
                   7539: 
                   7540: =item * sorted_slots()
                   7541: 
                   7542: Sorts an array of slot names in order of slot start time (earliest first).
                   7543: 
                   7544: Inputs:
                   7545: 
                   7546: =over 4
                   7547: 
                   7548: slotsarr  - Reference to array of unsorted slot names.
                   7549: 
                   7550: slots     - Reference to hash of hash, where outer hash keys are slot names.
                   7551: 
                   7552: =back
                   7553: 
                   7554: Returns:
                   7555: 
                   7556: =over 4
                   7557: 
                   7558: sorted   - An array of slot names sorted by the start time of the slot.
                   7559: 
                   7560: =back
                   7561: 
                   7562: =back
                   7563: 
                   7564: =cut
                   7565: 
                   7566: 
                   7567: sub sorted_slots {
                   7568:     my ($slotsarr,$slots) = @_;
                   7569:     my @sorted;
                   7570:     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
                   7571:         @sorted =
                   7572:             sort {
                   7573:                      if (ref($slots->{$a}) && ref($slots->{$b})) {
                   7574:                          return $slots->{$a}{'starttime'} <=> $slots->{$b}{'starttime'}
                   7575:                      }
                   7576:                      if (ref($slots->{$a})) { return -1;}
                   7577:                      if (ref($slots->{$b})) { return 1;}
                   7578:                      return 0;
                   7579:                  } @{$slotsarr};
                   7580:     }
                   7581:     return @sorted;
                   7582: }
                   7583: 
                   7584: =pod
                   7585: 
1.549     albertel 7586: =head1 HTTP Helpers
                   7587: 
                   7588: =over 4
                   7589: 
1.648     raeburn  7590: =item * &get_unprocessed_cgi($query,$possible_names)
1.112     bowersj2 7591: 
1.258     albertel 7592: Modify the %env hash to contain unprocessed CGI form parameters held in
1.112     bowersj2 7593: $query.  The parameters listed in $possible_names (an array reference),
1.258     albertel 7594: will be set in $env{'form.name'} if they do not already exist.
1.112     bowersj2 7595: 
                   7596: Typically called with $ENV{'QUERY_STRING'} as the first parameter.  
                   7597: $possible_names is an ref to an array of form element names.  As an example:
                   7598: get_unprocessed_cgi($ENV{'QUERY_STRING'},['uname','udom']);
1.258     albertel 7599: will result in $env{'form.uname'} and $env{'form.udom'} being set.
1.112     bowersj2 7600: 
                   7601: =cut
1.1       albertel 7602: 
1.6       albertel 7603: sub get_unprocessed_cgi {
1.25      albertel 7604:   my ($query,$possible_names)= @_;
1.26      matthew  7605:   # $Apache::lonxml::debug=1;
1.356     albertel 7606:   foreach my $pair (split(/&/,$query)) {
                   7607:     my ($name, $value) = split(/=/,$pair);
1.369     www      7608:     $name = &unescape($name);
1.25      albertel 7609:     if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
                   7610:       $value =~ tr/+/ /;
                   7611:       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.258     albertel 7612:       unless (defined($env{'form.'.$name})) { &add_to_env('form.'.$name,$value) };
1.25      albertel 7613:     }
1.16      harris41 7614:   }
1.6       albertel 7615: }
                   7616: 
1.112     bowersj2 7617: =pod
                   7618: 
1.648     raeburn  7619: =item * &cacheheader() 
1.112     bowersj2 7620: 
                   7621: returns cache-controlling header code
                   7622: 
                   7623: =cut
                   7624: 
1.7       albertel 7625: sub cacheheader {
1.258     albertel 7626:     unless ($env{'request.method'} eq 'GET') { return ''; }
1.216     albertel 7627:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                   7628:     my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
1.7       albertel 7629:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                   7630:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
1.216     albertel 7631:     return $output;
1.7       albertel 7632: }
                   7633: 
1.112     bowersj2 7634: =pod
                   7635: 
1.648     raeburn  7636: =item * &no_cache($r) 
1.112     bowersj2 7637: 
                   7638: specifies header code to not have cache
                   7639: 
                   7640: =cut
                   7641: 
1.9       albertel 7642: sub no_cache {
1.216     albertel 7643:     my ($r) = @_;
                   7644:     if ($ENV{'REQUEST_METHOD'} ne 'GET' &&
1.258     albertel 7645: 	$env{'request.method'} ne 'GET') { return ''; }
1.216     albertel 7646:     my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time));
                   7647:     $r->no_cache(1);
                   7648:     $r->header_out("Expires" => $date);
                   7649:     $r->header_out("Pragma" => "no-cache");
1.123     www      7650: }
                   7651: 
                   7652: sub content_type {
1.181     albertel 7653:     my ($r,$type,$charset) = @_;
1.299     foxr     7654:     if ($r) {
                   7655: 	#  Note that printout.pl calls this with undef for $r.
                   7656: 	&no_cache($r);
                   7657:     }
1.258     albertel 7658:     if ($env{'browser.mathml'} && $type eq 'text/html') { $type='text/xml'; }
1.181     albertel 7659:     unless ($charset) {
                   7660: 	$charset=&Apache::lonlocal::current_encoding;
                   7661:     }
                   7662:     if ($charset) { $type.='; charset='.$charset; }
                   7663:     if ($r) {
                   7664: 	$r->content_type($type);
                   7665:     } else {
                   7666: 	print("Content-type: $type\n\n");
                   7667:     }
1.9       albertel 7668: }
1.25      albertel 7669: 
1.112     bowersj2 7670: =pod
                   7671: 
1.648     raeburn  7672: =item * &add_to_env($name,$value) 
1.112     bowersj2 7673: 
1.258     albertel 7674: adds $name to the %env hash with value
1.112     bowersj2 7675: $value, if $name already exists, the entry is converted to an array
                   7676: reference and $value is added to the array.
                   7677: 
                   7678: =cut
                   7679: 
1.25      albertel 7680: sub add_to_env {
                   7681:   my ($name,$value)=@_;
1.258     albertel 7682:   if (defined($env{$name})) {
                   7683:     if (ref($env{$name})) {
1.25      albertel 7684:       #already have multiple values
1.258     albertel 7685:       push(@{ $env{$name} },$value);
1.25      albertel 7686:     } else {
                   7687:       #first time seeing multiple values, convert hash entry to an arrayref
1.258     albertel 7688:       my $first=$env{$name};
                   7689:       undef($env{$name});
                   7690:       push(@{ $env{$name} },$first,$value);
1.25      albertel 7691:     }
                   7692:   } else {
1.258     albertel 7693:     $env{$name}=$value;
1.25      albertel 7694:   }
1.31      albertel 7695: }
1.149     albertel 7696: 
                   7697: =pod
                   7698: 
1.648     raeburn  7699: =item * &get_env_multiple($name) 
1.149     albertel 7700: 
1.258     albertel 7701: gets $name from the %env hash, it seemlessly handles the cases where multiple
1.149     albertel 7702: values may be defined and end up as an array ref.
                   7703: 
                   7704: returns an array of values
                   7705: 
                   7706: =cut
                   7707: 
                   7708: sub get_env_multiple {
                   7709:     my ($name) = @_;
                   7710:     my @values;
1.258     albertel 7711:     if (defined($env{$name})) {
1.149     albertel 7712:         # exists is it an array
1.258     albertel 7713:         if (ref($env{$name})) {
                   7714:             @values=@{ $env{$name} };
1.149     albertel 7715:         } else {
1.258     albertel 7716:             $values[0]=$env{$name};
1.149     albertel 7717:         }
                   7718:     }
                   7719:     return(@values);
                   7720: }
                   7721: 
1.660     raeburn  7722: sub ask_for_embedded_content {
                   7723:     my ($actionurl,$state,$allfiles,$codebase,$args)=@_;
                   7724:     my $upload_output = '
                   7725:    <form name="upload_embedded" action="'.$actionurl.'"
                   7726:                   method="post" enctype="multipart/form-data">';
                   7727:     $upload_output .= $state;
1.661     raeburn  7728:     $upload_output .= '<b>Upload embedded files</b>:<br />'.&start_data_table();
1.660     raeburn  7729: 
                   7730:     my $num = 0;
                   7731:     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%{$allfiles})) {
                   7732:         $upload_output .= &start_data_table_row().
                   7733:             '<td>'.$embed_file.'</td><td>';
                   7734:         if ($args->{'ignore_remote_references'}
                   7735:             && $embed_file =~ m{^\w+://}) {
                   7736:             $upload_output.='<span class="LC_warning">'.&mt("URL points to other server.").'</span>';
                   7737:         } elsif ($args->{'error_on_invalid_names'}
                   7738:             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
                   7739: 
                   7740:             $upload_output.='<span class="LC_warning">'.&mt("Invalid characters").'</span>';
                   7741: 
                   7742:         } else {
                   7743:             $upload_output .='
1.661     raeburn  7744:            <input name="embedded_item_'.$num.'" type="file" value="" />
1.660     raeburn  7745:            <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';
                   7746:             my $attrib = join(':',@{$$allfiles{$embed_file}});
                   7747:             $upload_output .=
                   7748:                 "\n\t\t".
                   7749:                 '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.
                   7750:                 $attrib.'" />';
                   7751:             if (exists($$codebase{$embed_file})) {
                   7752:                 $upload_output .=
                   7753:                     "\n\t\t".
                   7754:                     '<input name="codebase_'.$num.'" type="hidden" value="'.
                   7755:                     &escape($$codebase{$embed_file}).'" />';
                   7756:             }
                   7757:         }
                   7758:         $upload_output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   7759:         $num++;
                   7760:     }
                   7761:     $upload_output .= &Apache::loncommon::end_data_table().'<br />
                   7762:    <input type ="hidden" name="number_embedded_items" value="'.$num.'" />
                   7763:    <input type ="submit" value="'.&mt('Upload Listed Files').'" />
                   7764:    '.&mt('(only files for which a location has been provided will be uploaded)').'
                   7765:    </form>';
                   7766:     return $upload_output;
                   7767: }
                   7768: 
1.661     raeburn  7769: sub upload_embedded {
                   7770:     my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
                   7771:         $current_disk_usage) = @_;
                   7772:     my $output;
                   7773:     for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
                   7774:         next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
                   7775:         my $orig_uploaded_filename =
                   7776:             $env{'form.embedded_item_'.$i.'.filename'};
                   7777: 
                   7778:         $env{'form.embedded_orig_'.$i} =
                   7779:             &unescape($env{'form.embedded_orig_'.$i});
                   7780:         my ($path,$fname) =
                   7781:             ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
                   7782:         # no path, whole string is fname
                   7783:         if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
                   7784: 
                   7785:         $path = $env{'form.currentpath'}.$path;
                   7786:         $fname = &Apache::lonnet::clean_filename($fname);
                   7787:         # See if there is anything left
                   7788:         next if ($fname eq '');
                   7789: 
                   7790:         # Check if file already exists as a file or directory.
                   7791:         my ($state,$msg);
                   7792:         if ($context eq 'portfolio') {
                   7793:             my $port_path = $dirpath;
                   7794:             if ($group ne '') {
                   7795:                 $port_path = "groups/$group/$port_path";
                   7796:             }
                   7797:             ($state,$msg) = &check_for_upload($path,$fname,$group,'embedded_item_'.$i,
                   7798:                                               $dir_root,$port_path,$disk_quota,
                   7799:                                               $current_disk_usage,$uname,$udom);
                   7800:             if ($state eq 'will_exceed_quota'
                   7801:                 || $state eq 'file_locked'
                   7802:                 || $state eq 'file_exists' ) {
                   7803:                 $output .= $msg;
                   7804:                 next;
                   7805:             }
                   7806:         } elsif (($context eq 'author') || ($context eq 'testbank')) {
                   7807:             ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
                   7808:             if ($state eq 'exists') {
                   7809:                 $output .= $msg;
                   7810:                 next;
                   7811:             }
                   7812:         }
                   7813:         # Check if extension is valid
                   7814:         if (($fname =~ /\.(\w+)$/) &&
                   7815:             (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
                   7816:             $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1);
                   7817:             next;
                   7818:         } elsif (($fname =~ /\.(\w+)$/) &&
                   7819:                  (!defined(&Apache::loncommon::fileembstyle($1)))) {
                   7820:             $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
                   7821:             next;
                   7822:         } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
                   7823:             $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
                   7824:             next;
                   7825:         }
                   7826: 
                   7827:         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
                   7828:         if ($context eq 'portfolio') {
                   7829:             my $result=
                   7830:                 &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
                   7831:                                                 $dirpath.$path);
                   7832:             if ($result !~ m|^/uploaded/|) {
                   7833:                 $output .= '<span class="LC_error">'
                   7834:                       .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                   7835:                            ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                   7836:                       .'</span><br />';
                   7837:                 next;
                   7838:             } else {
                   7839:                 $output .= '<p>'.&mt('Uploaded [_1]','<span class="LC_filename">'.
                   7840:                            $path.$fname.'</span>').'</p>';     
                   7841:             }
                   7842:         } else {
                   7843: # Save the file
                   7844:             my $target = $env{'form.embedded_item_'.$i};
                   7845:             my $fullpath = $dir_root.$dirpath.'/'.$path;
                   7846:             my $dest = $fullpath.$fname;
                   7847:             my $url = $url_root.$dirpath.'/'.$path.$fname;
                   7848:             my @parts=split(/\//,$fullpath);
                   7849:             my $count;
                   7850:             my $filepath = $dir_root;
                   7851:             for ($count=4;$count<=$#parts;$count++) {
                   7852:                 $filepath .= "/$parts[$count]";
                   7853:                 if ((-e $filepath)!=1) {
                   7854:                     mkdir($filepath,0770);
                   7855:                 }
                   7856:             }
                   7857:             my $fh;
                   7858:             if (!open($fh,'>'.$dest)) {
                   7859:                 &Apache::lonnet::logthis('Failed to create '.$dest);
                   7860:                 $output .= '<span class="LC_error">'.
                   7861:                            &mt('An error occurred while trying to upload [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                   7862:                            '</span><br />';
                   7863:             } else {
                   7864:                 if (!print $fh $env{'form.embedded_item_'.$i}) {
                   7865:                     &Apache::lonnet::logthis('Failed to write to '.$dest);
                   7866:                     $output .= '<span class="LC_error">'.
                   7867:                               &mt('An error occurred while writing the file [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                   7868:                               '</span><br />';
                   7869:                 } else {
                   7870:                     if ($context eq 'testbank') {
                   7871:                         $output .= &mt('Embedded file uploaded successfully:').
                   7872:                                    '&nbsp;<a href="'.$url.'">'.
                   7873:                                    $orig_uploaded_filename.'</a><br />';
                   7874:                     } else {
                   7875:                         $output .= '<font size="+2">'.
                   7876:                                    &mt('View embedded file: [_1]','<a href="'.$url.'">'.
                   7877:                                    $orig_uploaded_filename.'</a>').'</font><br />';
                   7878:                     }
                   7879:                 }
                   7880:                 close($fh);
                   7881:             }
                   7882:         }
                   7883:     }
                   7884:     return $output;
                   7885: }
                   7886: 
                   7887: sub check_for_existing {
                   7888:     my ($path,$fname,$element) = @_;
                   7889:     my ($state,$msg);
                   7890:     if (-d $path.'/'.$fname) {
                   7891:         $state = 'exists';
                   7892:         $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   7893:     } elsif (-e $path.'/'.$fname) {
                   7894:         $state = 'exists';
                   7895:         $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
                   7896:     }
                   7897:     if ($state eq 'exists') {
                   7898:         $msg = '<span class="LC_error">'.$msg.'</span><br />';
                   7899:     }
                   7900:     return ($state,$msg);
                   7901: }
                   7902: 
                   7903: sub check_for_upload {
                   7904:     my ($path,$fname,$group,$element,$portfolio_root,$port_path,
                   7905:         $disk_quota,$current_disk_usage,$uname,$udom) = @_;
                   7906:     my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
                   7907:     my $getpropath = 1;
                   7908:     my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,
                   7909:                                             $getpropath);
                   7910:     my $found_file = 0;
                   7911:     my $locked_file = 0;
                   7912:     foreach my $line (@dir_list) {
                   7913:         my ($file_name)=split(/\&/,$line,2);
                   7914:         if ($file_name eq $fname){
                   7915:             $file_name = $path.$file_name;
                   7916:             if ($group ne '') {
                   7917:                 $file_name = $group.$file_name;
                   7918:             }
                   7919:             $found_file = 1;
                   7920:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
                   7921:                 $locked_file = 1;
                   7922:             }
                   7923:         }
                   7924:     }
                   7925:     if (($current_disk_usage + $filesize) > $disk_quota){
                   7926:         my $msg = '<span class="LC_error">'.
                   7927:                 &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                   7928:                   '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
                   7929:         return ('will_exceed_quota',$msg);
                   7930:     } elsif ($found_file) {
                   7931:         if ($locked_file) {
                   7932:             my $msg = '<span class="LC_error">';
                   7933:             $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
                   7934:             $msg .= '</span><br />';
                   7935:             $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
                   7936:             return ('file_locked',$msg);
                   7937:         } else {
                   7938:             my $msg = '<span class="LC_error">';
                   7939:             $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
                   7940:             $msg .= '</span>';
                   7941:             $msg .= '<br />';
                   7942:             $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
                   7943:             return ('file_exists',$msg);
                   7944:         }
                   7945:     }
                   7946: }
                   7947: 
1.31      albertel 7948: 
1.41      ng       7949: =pod
1.45      matthew  7950: 
1.464     albertel 7951: =back
1.41      ng       7952: 
1.112     bowersj2 7953: =head1 CSV Upload/Handling functions
1.38      albertel 7954: 
1.41      ng       7955: =over 4
                   7956: 
1.648     raeburn  7957: =item * &upfile_store($r)
1.41      ng       7958: 
                   7959: Store uploaded file, $r should be the HTTP Request object,
1.258     albertel 7960: needs $env{'form.upfile'}
1.41      ng       7961: returns $datatoken to be put into hidden field
                   7962: 
                   7963: =cut
1.31      albertel 7964: 
                   7965: sub upfile_store {
                   7966:     my $r=shift;
1.258     albertel 7967:     $env{'form.upfile'}=~s/\r/\n/gs;
                   7968:     $env{'form.upfile'}=~s/\f/\n/gs;
                   7969:     $env{'form.upfile'}=~s/\n+/\n/gs;
                   7970:     $env{'form.upfile'}=~s/\n+$//gs;
1.31      albertel 7971: 
1.258     albertel 7972:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
                   7973: 	'_enroll_'.$env{'request.course.id'}.'_'.time.'_'.$$;
1.31      albertel 7974:     {
1.158     raeburn  7975:         my $datafile = $r->dir_config('lonDaemons').
                   7976:                            '/tmp/'.$datatoken.'.tmp';
                   7977:         if ( open(my $fh,">$datafile") ) {
1.258     albertel 7978:             print $fh $env{'form.upfile'};
1.158     raeburn  7979:             close($fh);
                   7980:         }
1.31      albertel 7981:     }
                   7982:     return $datatoken;
                   7983: }
                   7984: 
1.56      matthew  7985: =pod
                   7986: 
1.648     raeburn  7987: =item * &load_tmp_file($r)
1.41      ng       7988: 
                   7989: Load uploaded file from tmp, $r should be the HTTP Request object,
1.258     albertel 7990: needs $env{'form.datatoken'},
                   7991: sets $env{'form.upfile'} to the contents of the file
1.41      ng       7992: 
                   7993: =cut
1.31      albertel 7994: 
                   7995: sub load_tmp_file {
                   7996:     my $r=shift;
                   7997:     my @studentdata=();
                   7998:     {
1.158     raeburn  7999:         my $studentfile = $r->dir_config('lonDaemons').
1.258     albertel 8000:                               '/tmp/'.$env{'form.datatoken'}.'.tmp';
1.158     raeburn  8001:         if ( open(my $fh,"<$studentfile") ) {
                   8002:             @studentdata=<$fh>;
                   8003:             close($fh);
                   8004:         }
1.31      albertel 8005:     }
1.258     albertel 8006:     $env{'form.upfile'}=join('',@studentdata);
1.31      albertel 8007: }
                   8008: 
1.56      matthew  8009: =pod
                   8010: 
1.648     raeburn  8011: =item * &upfile_record_sep()
1.41      ng       8012: 
                   8013: Separate uploaded file into records
                   8014: returns array of records,
1.258     albertel 8015: needs $env{'form.upfile'} and $env{'form.upfiletype'}
1.41      ng       8016: 
                   8017: =cut
1.31      albertel 8018: 
                   8019: sub upfile_record_sep {
1.258     albertel 8020:     if ($env{'form.upfiletype'} eq 'xml') {
1.31      albertel 8021:     } else {
1.248     albertel 8022: 	my @records;
1.258     albertel 8023: 	foreach my $line (split(/\n/,$env{'form.upfile'})) {
1.248     albertel 8024: 	    if ($line=~/^\s*$/) { next; }
                   8025: 	    push(@records,$line);
                   8026: 	}
                   8027: 	return @records;
1.31      albertel 8028:     }
                   8029: }
                   8030: 
1.56      matthew  8031: =pod
                   8032: 
1.648     raeburn  8033: =item * &record_sep($record)
1.41      ng       8034: 
1.258     albertel 8035: Separate a record into fields $record should be an item from the upfile_record_sep(), needs $env{'form.upfiletype'}
1.41      ng       8036: 
                   8037: =cut
                   8038: 
1.263     www      8039: sub takeleft {
                   8040:     my $index=shift;
                   8041:     return substr('0000'.$index,-4,4);
                   8042: }
                   8043: 
1.31      albertel 8044: sub record_sep {
                   8045:     my $record=shift;
                   8046:     my %components=();
1.258     albertel 8047:     if ($env{'form.upfiletype'} eq 'xml') {
                   8048:     } elsif ($env{'form.upfiletype'} eq 'space') {
1.31      albertel 8049:         my $i=0;
1.356     albertel 8050:         foreach my $field (split(/\s+/,$record)) {
1.31      albertel 8051:             $field=~s/^(\"|\')//;
                   8052:             $field=~s/(\"|\')$//;
1.263     www      8053:             $components{&takeleft($i)}=$field;
1.31      albertel 8054:             $i++;
                   8055:         }
1.258     albertel 8056:     } elsif ($env{'form.upfiletype'} eq 'tab') {
1.31      albertel 8057:         my $i=0;
1.356     albertel 8058:         foreach my $field (split(/\t/,$record)) {
1.31      albertel 8059:             $field=~s/^(\"|\')//;
                   8060:             $field=~s/(\"|\')$//;
1.263     www      8061:             $components{&takeleft($i)}=$field;
1.31      albertel 8062:             $i++;
                   8063:         }
                   8064:     } else {
1.561     www      8065:         my $separator=',';
1.480     banghart 8066:         if ($env{'form.upfiletype'} eq 'semisv') {
1.561     www      8067:             $separator=';';
1.480     banghart 8068:         }
1.31      albertel 8069:         my $i=0;
1.561     www      8070: # the character we are looking for to indicate the end of a quote or a record 
                   8071:         my $looking_for=$separator;
                   8072: # do not add the characters to the fields
                   8073:         my $ignore=0;
                   8074: # we just encountered a separator (or the beginning of the record)
                   8075:         my $just_found_separator=1;
                   8076: # store the field we are working on here
                   8077:         my $field='';
                   8078: # work our way through all characters in record
                   8079:         foreach my $character ($record=~/(.)/g) {
                   8080:             if ($character eq $looking_for) {
                   8081:                if ($character ne $separator) {
                   8082: # Found the end of a quote, again looking for separator
                   8083:                   $looking_for=$separator;
                   8084:                   $ignore=1;
                   8085:                } else {
                   8086: # Found a separator, store away what we got
                   8087:                   $components{&takeleft($i)}=$field;
                   8088: 	          $i++;
                   8089:                   $just_found_separator=1;
                   8090:                   $ignore=0;
                   8091:                   $field='';
                   8092:                }
                   8093:                next;
                   8094:             }
                   8095: # single or double quotation marks after a separator indicate beginning of a quote
                   8096: # we are now looking for the end of the quote and need to ignore separators
                   8097:             if ((($character eq '"') || ($character eq "'")) && ($just_found_separator))  {
                   8098:                $looking_for=$character;
                   8099:                next;
                   8100:             }
                   8101: # ignore would be true after we reached the end of a quote
                   8102:             if ($ignore) { next; }
                   8103:             if (($just_found_separator) && ($character=~/\s/)) { next; }
                   8104:             $field.=$character;
                   8105:             $just_found_separator=0; 
1.31      albertel 8106:         }
1.561     www      8107: # catch the very last entry, since we never encountered the separator
                   8108:         $components{&takeleft($i)}=$field;
1.31      albertel 8109:     }
                   8110:     return %components;
                   8111: }
                   8112: 
1.144     matthew  8113: ######################################################
                   8114: ######################################################
                   8115: 
1.56      matthew  8116: =pod
                   8117: 
1.648     raeburn  8118: =item * &upfile_select_html()
1.41      ng       8119: 
1.144     matthew  8120: Return HTML code to select a file from the users machine and specify 
                   8121: the file type.
1.41      ng       8122: 
                   8123: =cut
                   8124: 
1.144     matthew  8125: ######################################################
                   8126: ######################################################
1.31      albertel 8127: sub upfile_select_html {
1.144     matthew  8128:     my %Types = (
                   8129:                  csv   => &mt('CSV (comma separated values, spreadsheet)'),
1.480     banghart 8130:                  semisv => &mt('Semicolon separated values'),
1.144     matthew  8131:                  space => &mt('Space separated'),
                   8132:                  tab   => &mt('Tabulator separated'),
                   8133: #                 xml   => &mt('HTML/XML'),
                   8134:                  );
                   8135:     my $Str = '<input type="file" name="upfile" size="50" />'.
1.692.4.2  raeburn  8136:         '<br />'.&mt('Type').': <select name="upfiletype">';
1.144     matthew  8137:     foreach my $type (sort(keys(%Types))) {
                   8138:         $Str .= '<option value="'.$type.'" >'.$Types{$type}."</option>\n";
                   8139:     }
                   8140:     $Str .= "</select>\n";
                   8141:     return $Str;
1.31      albertel 8142: }
                   8143: 
1.301     albertel 8144: sub get_samples {
                   8145:     my ($records,$toget) = @_;
                   8146:     my @samples=({});
                   8147:     my $got=0;
                   8148:     foreach my $rec (@$records) {
                   8149: 	my %temp = &record_sep($rec);
                   8150: 	if (! grep(/\S/, values(%temp))) { next; }
                   8151: 	if (%temp) {
                   8152: 	    $samples[$got]=\%temp;
                   8153: 	    $got++;
                   8154: 	    if ($got == $toget) { last; }
                   8155: 	}
                   8156:     }
                   8157:     return \@samples;
                   8158: }
                   8159: 
1.144     matthew  8160: ######################################################
                   8161: ######################################################
                   8162: 
1.56      matthew  8163: =pod
                   8164: 
1.648     raeburn  8165: =item * &csv_print_samples($r,$records)
1.41      ng       8166: 
                   8167: Prints a table of sample values from each column uploaded $r is an
                   8168: Apache Request ref, $records is an arrayref from
                   8169: &Apache::loncommon::upfile_record_sep
                   8170: 
                   8171: =cut
                   8172: 
1.144     matthew  8173: ######################################################
                   8174: ######################################################
1.31      albertel 8175: sub csv_print_samples {
                   8176:     my ($r,$records) = @_;
1.662     bisitz   8177:     my $samples = &get_samples($records,5);
1.301     albertel 8178: 
1.594     raeburn  8179:     $r->print(&mt('Samples').'<br />'.&start_data_table().
                   8180:               &start_data_table_header_row());
1.356     albertel 8181:     foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) { 
1.692.4.6  raeburn  8182:         $r->print('<th>'.&mt('Column [_1]',($sample+1)).'</th>');
                   8183:     }
1.594     raeburn  8184:     $r->print(&end_data_table_header_row());
1.301     albertel 8185:     foreach my $hash (@$samples) {
1.594     raeburn  8186: 	$r->print(&start_data_table_row());
1.356     albertel 8187: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
1.31      albertel 8188: 	    $r->print('<td>');
1.356     albertel 8189: 	    if (defined($$hash{$sample})) { $r->print($$hash{$sample}); }
1.31      albertel 8190: 	    $r->print('</td>');
                   8191: 	}
1.594     raeburn  8192: 	$r->print(&end_data_table_row());
1.31      albertel 8193:     }
1.594     raeburn  8194:     $r->print(&end_data_table().'<br />'."\n");
1.31      albertel 8195: }
                   8196: 
1.144     matthew  8197: ######################################################
                   8198: ######################################################
                   8199: 
1.56      matthew  8200: =pod
                   8201: 
1.648     raeburn  8202: =item * &csv_print_select_table($r,$records,$d)
1.41      ng       8203: 
                   8204: Prints a table to create associations between values and table columns.
1.144     matthew  8205: 
1.41      ng       8206: $r is an Apache Request ref,
                   8207: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
1.174     matthew  8208: $d is an array of 2 element arrays (internal name, displayed name,defaultcol)
1.41      ng       8209: 
                   8210: =cut
                   8211: 
1.144     matthew  8212: ######################################################
                   8213: ######################################################
1.31      albertel 8214: sub csv_print_select_table {
                   8215:     my ($r,$records,$d) = @_;
1.301     albertel 8216:     my $i=0;
                   8217:     my $samples = &get_samples($records,1);
1.144     matthew  8218:     $r->print(&mt('Associate columns with student attributes.')."\n".
1.594     raeburn  8219: 	      &start_data_table().&start_data_table_header_row().
1.144     matthew  8220:               '<th>'.&mt('Attribute').'</th>'.
1.594     raeburn  8221:               '<th>'.&mt('Column').'</th>'.
                   8222:               &end_data_table_header_row()."\n");
1.356     albertel 8223:     foreach my $array_ref (@$d) {
                   8224: 	my ($value,$display,$defaultcol)=@{ $array_ref };
1.689     bisitz   8225: 	$r->print(&start_data_table_row().'<td>'.$display.'</td>');
1.31      albertel 8226: 
1.692.4.36  raeburn  8227: 	$r->print('<td><select name="f'.$i.'"'.
1.32      matthew  8228: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.31      albertel 8229: 	$r->print('<option value="none"></option>');
1.356     albertel 8230: 	foreach my $sample (sort({$a <=> $b} keys(%{ $samples->[0] }))) {
                   8231: 	    $r->print('<option value="'.$sample.'"'.
                   8232:                       ($sample eq $defaultcol ? ' selected="selected" ' : '').
1.662     bisitz   8233:                       '>'.&mt('Column [_1]',($sample+1)).'</option>');
1.31      albertel 8234: 	}
1.594     raeburn  8235: 	$r->print('</select></td>'.&end_data_table_row()."\n");
1.31      albertel 8236: 	$i++;
                   8237:     }
1.594     raeburn  8238:     $r->print(&end_data_table());
1.31      albertel 8239:     $i--;
                   8240:     return $i;
                   8241: }
1.56      matthew  8242: 
1.144     matthew  8243: ######################################################
                   8244: ######################################################
                   8245: 
1.56      matthew  8246: =pod
1.31      albertel 8247: 
1.648     raeburn  8248: =item * &csv_samples_select_table($r,$records,$d)
1.41      ng       8249: 
                   8250: Prints a table of sample values from the upload and can make associate samples to internal names.
                   8251: 
                   8252: $r is an Apache Request ref,
                   8253: $records is an arrayref from &Apache::loncommon::upfile_record_sep,
                   8254: $d is an array of 2 element arrays (internal name, displayed name)
                   8255: 
                   8256: =cut
                   8257: 
1.144     matthew  8258: ######################################################
                   8259: ######################################################
1.31      albertel 8260: sub csv_samples_select_table {
                   8261:     my ($r,$records,$d) = @_;
                   8262:     my $i=0;
1.144     matthew  8263:     #
1.662     bisitz   8264:     my $max_samples = 5;
                   8265:     my $samples = &get_samples($records,$max_samples);
1.594     raeburn  8266:     $r->print(&start_data_table().
                   8267:               &start_data_table_header_row().'<th>'.
                   8268:               &mt('Field').'</th><th>'.&mt('Samples').'</th>'.
                   8269:               &end_data_table_header_row());
1.301     albertel 8270: 
                   8271:     foreach my $key (sort(keys(%{ $samples->[0] }))) {
1.594     raeburn  8272: 	$r->print(&start_data_table_row().'<td><select name="f'.$i.'"'.
1.32      matthew  8273: 		  ' onchange="javascript:flip(this.form,'.$i.');">');
1.301     albertel 8274: 	foreach my $option (@$d) {
                   8275: 	    my ($value,$display,$defaultcol)=@{ $option };
1.174     matthew  8276: 	    $r->print('<option value="'.$value.'"'.
1.253     albertel 8277:                       ($i eq $defaultcol ? ' selected="selected" ':'').'>'.
1.174     matthew  8278:                       $display.'</option>');
1.31      albertel 8279: 	}
                   8280: 	$r->print('</select></td><td>');
1.662     bisitz   8281: 	foreach my $line (0..($max_samples-1)) {
1.301     albertel 8282: 	    if (defined($samples->[$line]{$key})) { 
                   8283: 		$r->print($samples->[$line]{$key}."<br />\n"); 
                   8284: 	    }
                   8285: 	}
1.594     raeburn  8286: 	$r->print('</td>'.&end_data_table_row());
1.31      albertel 8287: 	$i++;
                   8288:     }
1.594     raeburn  8289:     $r->print(&end_data_table());
1.31      albertel 8290:     $i--;
                   8291:     return($i);
1.115     matthew  8292: }
                   8293: 
1.144     matthew  8294: ######################################################
                   8295: ######################################################
                   8296: 
1.115     matthew  8297: =pod
                   8298: 
1.648     raeburn  8299: =item * &clean_excel_name($name)
1.115     matthew  8300: 
                   8301: Returns a replacement for $name which does not contain any illegal characters.
                   8302: 
                   8303: =cut
                   8304: 
1.144     matthew  8305: ######################################################
                   8306: ######################################################
1.115     matthew  8307: sub clean_excel_name {
                   8308:     my ($name) = @_;
                   8309:     $name =~ s/[:\*\?\/\\]//g;
                   8310:     if (length($name) > 31) {
                   8311:         $name = substr($name,0,31);
                   8312:     }
                   8313:     return $name;
1.25      albertel 8314: }
1.84      albertel 8315: 
1.85      albertel 8316: =pod
                   8317: 
1.648     raeburn  8318: =item * &check_if_partid_hidden($id,$symb,$udom,$uname)
1.85      albertel 8319: 
                   8320: Returns either 1 or undef
                   8321: 
                   8322: 1 if the part is to be hidden, undef if it is to be shown
                   8323: 
                   8324: Arguments are:
                   8325: 
                   8326: $id the id of the part to be checked
                   8327: $symb, optional the symb of the resource to check
                   8328: $udom, optional the domain of the user to check for
                   8329: $uname, optional the username of the user to check for
                   8330: 
                   8331: =cut
1.84      albertel 8332: 
                   8333: sub check_if_partid_hidden {
                   8334:     my ($id,$symb,$udom,$uname) = @_;
1.133     albertel 8335:     my $hiddenparts=&Apache::lonnet::EXT('resource.0.hiddenparts',
1.84      albertel 8336: 					 $symb,$udom,$uname);
1.141     albertel 8337:     my $truth=1;
                   8338:     #if the string starts with !, then the list is the list to show not hide
                   8339:     if ($hiddenparts=~s/^\s*!//) { $truth=undef; }
1.84      albertel 8340:     my @hiddenlist=split(/,/,$hiddenparts);
                   8341:     foreach my $checkid (@hiddenlist) {
1.141     albertel 8342: 	if ($checkid =~ /^\s*\Q$id\E\s*$/) { return $truth; }
1.84      albertel 8343:     }
1.141     albertel 8344:     return !$truth;
1.84      albertel 8345: }
1.127     matthew  8346: 
1.138     matthew  8347: 
                   8348: ############################################################
                   8349: ############################################################
                   8350: 
                   8351: =pod
                   8352: 
1.157     matthew  8353: =back 
                   8354: 
1.138     matthew  8355: =head1 cgi-bin script and graphing routines
                   8356: 
1.157     matthew  8357: =over 4
                   8358: 
1.648     raeburn  8359: =item * &get_cgi_id()
1.138     matthew  8360: 
                   8361: Inputs: none
                   8362: 
                   8363: Returns an id which can be used to pass environment variables
                   8364: to various cgi-bin scripts.  These environment variables will
                   8365: be removed from the users environment after a given time by
                   8366: the routine &Apache::lonnet::transfer_profile_to_env.
                   8367: 
                   8368: =cut
                   8369: 
                   8370: ############################################################
                   8371: ############################################################
1.152     albertel 8372: my $uniq=0;
1.136     matthew  8373: sub get_cgi_id {
1.154     albertel 8374:     $uniq=($uniq+1)%100000;
1.280     albertel 8375:     return (time.'_'.$$.'_'.$uniq);
1.136     matthew  8376: }
                   8377: 
1.127     matthew  8378: ############################################################
                   8379: ############################################################
                   8380: 
                   8381: =pod
                   8382: 
1.648     raeburn  8383: =item * &DrawBarGraph()
1.127     matthew  8384: 
1.138     matthew  8385: Facilitates the plotting of data in a (stacked) bar graph.
                   8386: Puts plot definition data into the users environment in order for 
                   8387: graph.png to plot it.  Returns an <img> tag for the plot.
                   8388: The bars on the plot are labeled '1','2',...,'n'.
                   8389: 
                   8390: Inputs:
                   8391: 
                   8392: =over 4
                   8393: 
                   8394: =item $Title: string, the title of the plot
                   8395: 
                   8396: =item $xlabel: string, text describing the X-axis of the plot
                   8397: 
                   8398: =item $ylabel: string, text describing the Y-axis of the plot
                   8399: 
                   8400: =item $Max: scalar, the maximum Y value to use in the plot
                   8401: If $Max is < any data point, the graph will not be rendered.
                   8402: 
1.140     matthew  8403: =item $colors: array ref holding the colors to be used for the data sets when
1.138     matthew  8404: they are plotted.  If undefined, default values will be used.
                   8405: 
1.178     matthew  8406: =item $labels: array ref holding the labels to use on the x-axis for the bars.
                   8407: 
1.138     matthew  8408: =item @Values: An array of array references.  Each array reference holds data
                   8409: to be plotted in a stacked bar chart.
                   8410: 
1.239     matthew  8411: =item If the final element of @Values is a hash reference the key/value
                   8412: pairs will be added to the graph definition.
                   8413: 
1.138     matthew  8414: =back
                   8415: 
                   8416: Returns:
                   8417: 
                   8418: An <img> tag which references graph.png and the appropriate identifying
                   8419: information for the plot.
                   8420: 
1.127     matthew  8421: =cut
                   8422: 
                   8423: ############################################################
                   8424: ############################################################
1.134     matthew  8425: sub DrawBarGraph {
1.178     matthew  8426:     my ($Title,$xlabel,$ylabel,$Max,$colors,$labels,@Values)=@_;
1.134     matthew  8427:     #
                   8428:     if (! defined($colors)) {
                   8429:         $colors = ['#33ff00', 
                   8430:                   '#0033cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                   8431:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                   8432:                   ]; 
                   8433:     }
1.228     matthew  8434:     my $extra_settings = {};
                   8435:     if (ref($Values[-1]) eq 'HASH') {
                   8436:         $extra_settings = pop(@Values);
                   8437:     }
1.127     matthew  8438:     #
1.136     matthew  8439:     my $identifier = &get_cgi_id();
                   8440:     my $id = 'cgi.'.$identifier;        
1.129     matthew  8441:     if (! @Values || ref($Values[0]) ne 'ARRAY') {
1.127     matthew  8442:         return '';
                   8443:     }
1.225     matthew  8444:     #
                   8445:     my @Labels;
                   8446:     if (defined($labels)) {
                   8447:         @Labels = @$labels;
                   8448:     } else {
                   8449:         for (my $i=0;$i<@{$Values[0]};$i++) {
                   8450:             push (@Labels,$i+1);
                   8451:         }
                   8452:     }
                   8453:     #
1.129     matthew  8454:     my $NumBars = scalar(@{$Values[0]});
1.225     matthew  8455:     if ($NumBars < scalar(@Labels)) { $NumBars = scalar(@Labels); }
1.129     matthew  8456:     my %ValuesHash;
                   8457:     my $NumSets=1;
                   8458:     foreach my $array (@Values) {
                   8459:         next if (! ref($array));
1.136     matthew  8460:         $ValuesHash{$id.'.data.'.$NumSets++} = 
1.132     matthew  8461:             join(',',@$array);
1.129     matthew  8462:     }
1.127     matthew  8463:     #
1.136     matthew  8464:     my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
1.225     matthew  8465:     if ($NumBars < 3) {
                   8466:         $width = 120+$NumBars*32;
1.220     matthew  8467:         $xskip = 1;
1.225     matthew  8468:         $bar_width = 30;
                   8469:     } elsif ($NumBars < 5) {
                   8470:         $width = 120+$NumBars*20;
                   8471:         $xskip = 1;
                   8472:         $bar_width = 20;
1.220     matthew  8473:     } elsif ($NumBars < 10) {
1.136     matthew  8474:         $width = 120+$NumBars*15;
                   8475:         $xskip = 1;
                   8476:         $bar_width = 15;
                   8477:     } elsif ($NumBars <= 25) {
                   8478:         $width = 120+$NumBars*11;
                   8479:         $xskip = 5;
                   8480:         $bar_width = 8;
                   8481:     } elsif ($NumBars <= 50) {
                   8482:         $width = 120+$NumBars*8;
                   8483:         $xskip = 5;
                   8484:         $bar_width = 4;
                   8485:     } else {
                   8486:         $width = 120+$NumBars*8;
                   8487:         $xskip = 5;
                   8488:         $bar_width = 4;
                   8489:     }
                   8490:     #
1.137     matthew  8491:     $Max = 1 if ($Max < 1);
                   8492:     if ( int($Max) < $Max ) {
                   8493:         $Max++;
                   8494:         $Max = int($Max);
                   8495:     }
1.127     matthew  8496:     $Title  = '' if (! defined($Title));
                   8497:     $xlabel = '' if (! defined($xlabel));
                   8498:     $ylabel = '' if (! defined($ylabel));
1.369     www      8499:     $ValuesHash{$id.'.title'}    = &escape($Title);
                   8500:     $ValuesHash{$id.'.xlabel'}   = &escape($xlabel);
                   8501:     $ValuesHash{$id.'.ylabel'}   = &escape($ylabel);
1.137     matthew  8502:     $ValuesHash{$id.'.y_max_value'} = $Max;
1.136     matthew  8503:     $ValuesHash{$id.'.NumBars'}  = $NumBars;
                   8504:     $ValuesHash{$id.'.NumSets'}  = $NumSets;
                   8505:     $ValuesHash{$id.'.PlotType'} = 'bar';
                   8506:     $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   8507:     $ValuesHash{$id.'.height'}   = $height;
                   8508:     $ValuesHash{$id.'.width'}    = $width;
                   8509:     $ValuesHash{$id.'.xskip'}    = $xskip;
                   8510:     $ValuesHash{$id.'.bar_width'} = $bar_width;
                   8511:     $ValuesHash{$id.'.labels'} = join(',',@Labels);
1.127     matthew  8512:     #
1.228     matthew  8513:     # Deal with other parameters
                   8514:     while (my ($key,$value) = each(%$extra_settings)) {
                   8515:         $ValuesHash{$id.'.'.$key} = $value;
                   8516:     }
                   8517:     #
1.646     raeburn  8518:     &Apache::lonnet::appenv(\%ValuesHash);
1.137     matthew  8519:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   8520: }
                   8521: 
                   8522: ############################################################
                   8523: ############################################################
                   8524: 
                   8525: =pod
                   8526: 
1.648     raeburn  8527: =item * &DrawXYGraph()
1.137     matthew  8528: 
1.138     matthew  8529: Facilitates the plotting of data in an XY graph.
                   8530: Puts plot definition data into the users environment in order for 
                   8531: graph.png to plot it.  Returns an <img> tag for the plot.
                   8532: 
                   8533: Inputs:
                   8534: 
                   8535: =over 4
                   8536: 
                   8537: =item $Title: string, the title of the plot
                   8538: 
                   8539: =item $xlabel: string, text describing the X-axis of the plot
                   8540: 
                   8541: =item $ylabel: string, text describing the Y-axis of the plot
                   8542: 
                   8543: =item $Max: scalar, the maximum Y value to use in the plot
                   8544: If $Max is < any data point, the graph will not be rendered.
                   8545: 
                   8546: =item $colors: Array ref containing the hex color codes for the data to be 
                   8547: plotted in.  If undefined, default values will be used.
                   8548: 
                   8549: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   8550: 
                   8551: =item $Ydata: Array ref containing Array refs.  
1.185     www      8552: Each of the contained arrays will be plotted as a separate curve.
1.138     matthew  8553: 
                   8554: =item %Values: hash indicating or overriding any default values which are 
                   8555: passed to graph.png.  
                   8556: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   8557: 
                   8558: =back
                   8559: 
                   8560: Returns:
                   8561: 
                   8562: An <img> tag which references graph.png and the appropriate identifying
                   8563: information for the plot.
                   8564: 
1.137     matthew  8565: =cut
                   8566: 
                   8567: ############################################################
                   8568: ############################################################
                   8569: sub DrawXYGraph {
                   8570:     my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
                   8571:     #
                   8572:     # Create the identifier for the graph
                   8573:     my $identifier = &get_cgi_id();
                   8574:     my $id = 'cgi.'.$identifier;
                   8575:     #
                   8576:     $Title  = '' if (! defined($Title));
                   8577:     $xlabel = '' if (! defined($xlabel));
                   8578:     $ylabel = '' if (! defined($ylabel));
                   8579:     my %ValuesHash = 
                   8580:         (
1.369     www      8581:          $id.'.title'  => &escape($Title),
                   8582:          $id.'.xlabel' => &escape($xlabel),
                   8583:          $id.'.ylabel' => &escape($ylabel),
1.137     matthew  8584:          $id.'.y_max_value'=> $Max,
                   8585:          $id.'.labels'     => join(',',@$Xlabels),
                   8586:          $id.'.PlotType'   => 'XY',
                   8587:          );
                   8588:     #
                   8589:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   8590:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   8591:     }
                   8592:     #
                   8593:     if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
                   8594:         return '';
                   8595:     }
                   8596:     my $NumSets=1;
1.138     matthew  8597:     foreach my $array (@{$Ydata}){
1.137     matthew  8598:         next if (! ref($array));
                   8599:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
                   8600:     }
1.138     matthew  8601:     $ValuesHash{$id.'.NumSets'} = $NumSets-1;
1.137     matthew  8602:     #
                   8603:     # Deal with other parameters
                   8604:     while (my ($key,$value) = each(%Values)) {
                   8605:         $ValuesHash{$id.'.'.$key} = $value;
1.127     matthew  8606:     }
                   8607:     #
1.646     raeburn  8608:     &Apache::lonnet::appenv(\%ValuesHash);
1.136     matthew  8609:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
                   8610: }
                   8611: 
                   8612: ############################################################
                   8613: ############################################################
                   8614: 
                   8615: =pod
                   8616: 
1.648     raeburn  8617: =item * &DrawXYYGraph()
1.138     matthew  8618: 
                   8619: Facilitates the plotting of data in an XY graph with two Y axes.
                   8620: Puts plot definition data into the users environment in order for 
                   8621: graph.png to plot it.  Returns an <img> tag for the plot.
                   8622: 
                   8623: Inputs:
                   8624: 
                   8625: =over 4
                   8626: 
                   8627: =item $Title: string, the title of the plot
                   8628: 
                   8629: =item $xlabel: string, text describing the X-axis of the plot
                   8630: 
                   8631: =item $ylabel: string, text describing the Y-axis of the plot
                   8632: 
                   8633: =item $colors: Array ref containing the hex color codes for the data to be 
                   8634: plotted in.  If undefined, default values will be used.
                   8635: 
                   8636: =item $Xlabels: Array ref containing the labels to be used for the X-axis.
                   8637: 
                   8638: =item $Ydata1: The first data set
                   8639: 
                   8640: =item $Min1: The minimum value of the left Y-axis
                   8641: 
                   8642: =item $Max1: The maximum value of the left Y-axis
                   8643: 
                   8644: =item $Ydata2: The second data set
                   8645: 
                   8646: =item $Min2: The minimum value of the right Y-axis
                   8647: 
                   8648: =item $Max2: The maximum value of the left Y-axis
                   8649: 
                   8650: =item %Values: hash indicating or overriding any default values which are 
                   8651: passed to graph.png.  
                   8652: Possible values are: width, xskip, x_ticks, x_tick_offset, among others.
                   8653: 
                   8654: =back
                   8655: 
                   8656: Returns:
                   8657: 
                   8658: An <img> tag which references graph.png and the appropriate identifying
                   8659: information for the plot.
1.136     matthew  8660: 
                   8661: =cut
                   8662: 
                   8663: ############################################################
                   8664: ############################################################
1.137     matthew  8665: sub DrawXYYGraph {
                   8666:     my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
                   8667:                                         $Ydata2,$Min2,$Max2,%Values)=@_;
1.136     matthew  8668:     #
                   8669:     # Create the identifier for the graph
                   8670:     my $identifier = &get_cgi_id();
                   8671:     my $id = 'cgi.'.$identifier;
                   8672:     #
                   8673:     $Title  = '' if (! defined($Title));
                   8674:     $xlabel = '' if (! defined($xlabel));
                   8675:     $ylabel = '' if (! defined($ylabel));
                   8676:     my %ValuesHash = 
                   8677:         (
1.369     www      8678:          $id.'.title'  => &escape($Title),
                   8679:          $id.'.xlabel' => &escape($xlabel),
                   8680:          $id.'.ylabel' => &escape($ylabel),
1.136     matthew  8681:          $id.'.labels' => join(',',@$Xlabels),
                   8682:          $id.'.PlotType' => 'XY',
                   8683:          $id.'.NumSets' => 2,
1.137     matthew  8684:          $id.'.two_axes' => 1,
                   8685:          $id.'.y1_max_value' => $Max1,
                   8686:          $id.'.y1_min_value' => $Min1,
                   8687:          $id.'.y2_max_value' => $Max2,
                   8688:          $id.'.y2_min_value' => $Min2,
1.136     matthew  8689:          );
                   8690:     #
1.137     matthew  8691:     if (defined($colors) && ref($colors) eq 'ARRAY') {
                   8692:         $ValuesHash{$id.'.Colors'}   = join(',',@{$colors});
                   8693:     }
                   8694:     #
                   8695:     if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
                   8696:         ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
1.136     matthew  8697:         return '';
                   8698:     }
                   8699:     my $NumSets=1;
1.137     matthew  8700:     foreach my $array ($Ydata1,$Ydata2){
1.136     matthew  8701:         next if (! ref($array));
                   8702:         $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
1.137     matthew  8703:     }
                   8704:     #
                   8705:     # Deal with other parameters
                   8706:     while (my ($key,$value) = each(%Values)) {
                   8707:         $ValuesHash{$id.'.'.$key} = $value;
1.136     matthew  8708:     }
                   8709:     #
1.646     raeburn  8710:     &Apache::lonnet::appenv(\%ValuesHash);
1.130     albertel 8711:     return '<img src="/cgi-bin/graph.png?'.$identifier.'" border="1" />';
1.139     matthew  8712: }
                   8713: 
                   8714: ############################################################
                   8715: ############################################################
                   8716: 
                   8717: =pod
                   8718: 
1.157     matthew  8719: =back 
                   8720: 
1.139     matthew  8721: =head1 Statistics helper routines?  
                   8722: 
                   8723: Bad place for them but what the hell.
                   8724: 
1.157     matthew  8725: =over 4
                   8726: 
1.648     raeburn  8727: =item * &chartlink()
1.139     matthew  8728: 
                   8729: Returns a link to the chart for a specific student.  
                   8730: 
                   8731: Inputs:
                   8732: 
                   8733: =over 4
                   8734: 
                   8735: =item $linktext: The text of the link
                   8736: 
                   8737: =item $sname: The students username
                   8738: 
                   8739: =item $sdomain: The students domain
                   8740: 
                   8741: =back
                   8742: 
1.157     matthew  8743: =back
                   8744: 
1.139     matthew  8745: =cut
                   8746: 
                   8747: ############################################################
                   8748: ############################################################
                   8749: sub chartlink {
                   8750:     my ($linktext, $sname, $sdomain) = @_;
                   8751:     my $link = '<a href="/adm/statistics?reportSelected=student_assessment'.
1.369     www      8752:         '&amp;SelectedStudent='.&escape($sname.':'.$sdomain).
1.219     albertel 8753:         '&amp;chartoutputmode='.HTML::Entities::encode('html, with all links').
1.139     matthew  8754:        '">'.$linktext.'</a>';
1.153     matthew  8755: }
                   8756: 
                   8757: #######################################################
                   8758: #######################################################
                   8759: 
                   8760: =pod
                   8761: 
                   8762: =head1 Course Environment Routines
1.157     matthew  8763: 
                   8764: =over 4
1.153     matthew  8765: 
1.648     raeburn  8766: =item * &restore_course_settings()
1.153     matthew  8767: 
1.648     raeburn  8768: =item * &store_course_settings()
1.153     matthew  8769: 
                   8770: Restores/Store indicated form parameters from the course environment.
                   8771: Will not overwrite existing values of the form parameters.
                   8772: 
                   8773: Inputs: 
                   8774: a scalar describing the data (e.g. 'chart', 'problem_analysis')
                   8775: 
                   8776: a hash ref describing the data to be stored.  For example:
                   8777:    
                   8778: %Save_Parameters = ('Status' => 'scalar',
                   8779:     'chartoutputmode' => 'scalar',
                   8780:     'chartoutputdata' => 'scalar',
                   8781:     'Section' => 'array',
1.373     raeburn  8782:     'Group' => 'array',
1.153     matthew  8783:     'StudentData' => 'array',
                   8784:     'Maps' => 'array');
                   8785: 
                   8786: Returns: both routines return nothing
                   8787: 
1.631     raeburn  8788: =back
                   8789: 
1.153     matthew  8790: =cut
                   8791: 
                   8792: #######################################################
                   8793: #######################################################
                   8794: sub store_course_settings {
1.496     albertel 8795:     return &store_settings($env{'request.course.id'},@_);
                   8796: }
                   8797: 
                   8798: sub store_settings {
1.153     matthew  8799:     # save to the environment
                   8800:     # appenv the same items, just to be safe
1.300     albertel 8801:     my $udom  = $env{'user.domain'};
                   8802:     my $uname = $env{'user.name'};
1.496     albertel 8803:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  8804:     my %SaveHash;
                   8805:     my %AppHash;
                   8806:     while (my ($setting,$type) = each(%$Settings)) {
1.496     albertel 8807:         my $basename = join('.','internal',$context,$prefix,$setting);
1.300     albertel 8808:         my $envname = 'environment.'.$basename;
1.258     albertel 8809:         if (exists($env{'form.'.$setting})) {
1.153     matthew  8810:             # Save this value away
                   8811:             if ($type eq 'scalar' &&
1.258     albertel 8812:                 (! exists($env{$envname}) || 
                   8813:                  $env{$envname} ne $env{'form.'.$setting})) {
                   8814:                 $SaveHash{$basename} = $env{'form.'.$setting};
                   8815:                 $AppHash{$envname}   = $env{'form.'.$setting};
1.153     matthew  8816:             } elsif ($type eq 'array') {
                   8817:                 my $stored_form;
1.258     albertel 8818:                 if (ref($env{'form.'.$setting})) {
1.153     matthew  8819:                     $stored_form = join(',',
                   8820:                                         map {
1.369     www      8821:                                             &escape($_);
1.258     albertel 8822:                                         } sort(@{$env{'form.'.$setting}}));
1.153     matthew  8823:                 } else {
                   8824:                     $stored_form = 
1.369     www      8825:                         &escape($env{'form.'.$setting});
1.153     matthew  8826:                 }
                   8827:                 # Determine if the array contents are the same.
1.258     albertel 8828:                 if ($stored_form ne $env{$envname}) {
1.153     matthew  8829:                     $SaveHash{$basename} = $stored_form;
                   8830:                     $AppHash{$envname}   = $stored_form;
                   8831:                 }
                   8832:             }
                   8833:         }
                   8834:     }
                   8835:     my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
1.300     albertel 8836:                                           $udom,$uname);
1.153     matthew  8837:     if ($put_result !~ /^(ok|delayed)/) {
                   8838:         &Apache::lonnet::logthis('unable to save form parameters, '.
                   8839:                                  'got error:'.$put_result);
                   8840:     }
                   8841:     # Make sure these settings stick around in this session, too
1.646     raeburn  8842:     &Apache::lonnet::appenv(\%AppHash);
1.153     matthew  8843:     return;
                   8844: }
                   8845: 
                   8846: sub restore_course_settings {
1.499     albertel 8847:     return &restore_settings($env{'request.course.id'},@_);
1.496     albertel 8848: }
                   8849: 
                   8850: sub restore_settings {
                   8851:     my ($context,$prefix,$Settings) = @_;
1.153     matthew  8852:     while (my ($setting,$type) = each(%$Settings)) {
1.258     albertel 8853:         next if (exists($env{'form.'.$setting}));
1.496     albertel 8854:         my $envname = 'environment.internal.'.$context.'.'.$prefix.
1.153     matthew  8855:             '.'.$setting;
1.258     albertel 8856:         if (exists($env{$envname})) {
1.153     matthew  8857:             if ($type eq 'scalar') {
1.258     albertel 8858:                 $env{'form.'.$setting} = $env{$envname};
1.153     matthew  8859:             } elsif ($type eq 'array') {
1.258     albertel 8860:                 $env{'form.'.$setting} = [ 
1.153     matthew  8861:                                            map { 
1.369     www      8862:                                                &unescape($_); 
1.258     albertel 8863:                                            } split(',',$env{$envname})
1.153     matthew  8864:                                            ];
                   8865:             }
                   8866:         }
                   8867:     }
1.127     matthew  8868: }
                   8869: 
1.618     raeburn  8870: #######################################################
                   8871: #######################################################
                   8872: 
                   8873: =pod
                   8874: 
                   8875: =head1 Domain E-mail Routines  
                   8876: 
                   8877: =over 4
                   8878: 
1.648     raeburn  8879: =item * &build_recipient_list()
1.618     raeburn  8880: 
1.692.4.14  raeburn  8881: Build recipient lists for five types of e-mail:
1.692.4.2  raeburn  8882: (a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors
1.692.4.14  raeburn  8883: (d) Help requests, (e) Course requests needing approval,  generated by
                   8884: lonerrorhandler.pm, CHECKRPMS, loncron, lonsupportreq.pm and
                   8885: loncoursequeueadmin.pm respectively.
1.618     raeburn  8886: 
                   8887: Inputs:
1.619     raeburn  8888: defmail (scalar - email address of default recipient), 
1.618     raeburn  8889: mailing type (scalar - errormail, packagesmail, or helpdeskmail), 
1.619     raeburn  8890: defdom (domain for which to retrieve configuration settings),
                   8891: origmail (scalar - email address of recipient from loncapa.conf, 
                   8892: i.e., predates configuration by DC via domainprefs.pm 
1.618     raeburn  8893: 
1.655     raeburn  8894: Returns: comma separated list of addresses to which to send e-mail.
                   8895: 
                   8896: =back
1.618     raeburn  8897: 
                   8898: =cut
                   8899: 
                   8900: ############################################################
                   8901: ############################################################
                   8902: sub build_recipient_list {
1.619     raeburn  8903:     my ($defmail,$mailing,$defdom,$origmail) = @_;
1.618     raeburn  8904:     my @recipients;
                   8905:     my $otheremails;
                   8906:     my %domconfig =
                   8907:          &Apache::lonnet::get_dom('configuration',['contacts'],$defdom);
                   8908:     if (ref($domconfig{'contacts'}) eq 'HASH') {
1.692.4.2  raeburn  8909:         if (exists($domconfig{'contacts'}{$mailing})) {
                   8910:             if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') {
                   8911:                 my @contacts = ('adminemail','supportemail');
                   8912:                 foreach my $item (@contacts) {
                   8913:                     if ($domconfig{'contacts'}{$mailing}{$item}) {
                   8914:                         my $addr = $domconfig{'contacts'}{$item};
                   8915:                         if (!grep(/^\Q$addr\E$/,@recipients)) {
                   8916:                             push(@recipients,$addr);
                   8917:                         }
1.619     raeburn  8918:                     }
1.692.4.2  raeburn  8919:                     $otheremails = $domconfig{'contacts'}{$mailing}{'others'};
1.618     raeburn  8920:                 }
                   8921:             }
1.692.4.2  raeburn  8922:         } elsif ($origmail ne '') {
                   8923:             push(@recipients,$origmail);
1.618     raeburn  8924:         }
1.619     raeburn  8925:     } elsif ($origmail ne '') {
                   8926:         push(@recipients,$origmail);
1.618     raeburn  8927:     }
1.688     raeburn  8928:     if (defined($defmail)) {
                   8929:         if ($defmail ne '') {
                   8930:             push(@recipients,$defmail);
                   8931:         }
1.618     raeburn  8932:     }
                   8933:     if ($otheremails) {
1.619     raeburn  8934:         my @others;
                   8935:         if ($otheremails =~ /,/) {
                   8936:             @others = split(/,/,$otheremails);
1.618     raeburn  8937:         } else {
1.619     raeburn  8938:             push(@others,$otheremails);
                   8939:         }
                   8940:         foreach my $addr (@others) {
                   8941:             if (!grep(/^\Q$addr\E$/,@recipients)) {
                   8942:                 push(@recipients,$addr);
                   8943:             }
1.618     raeburn  8944:         }
                   8945:     }
1.619     raeburn  8946:     my $recipientlist = join(',',@recipients); 
1.618     raeburn  8947:     return $recipientlist;
                   8948: }
                   8949: 
1.127     matthew  8950: ############################################################
                   8951: ############################################################
1.154     albertel 8952: 
1.655     raeburn  8953: =pod
                   8954: 
                   8955: =head1 Course Catalog Routines
                   8956: 
                   8957: =over 4
                   8958: 
                   8959: =item * &gather_categories()
                   8960: 
                   8961: Converts category definitions - keys of categories hash stored in  
                   8962: coursecategories in configuration.db on the primary library server in a 
                   8963: domain - to an array.  Also generates javascript and idx hash used to 
                   8964: generate Domain Coordinator interface for editing Course Categories.
                   8965: 
                   8966: Inputs:
1.663     raeburn  8967: 
1.655     raeburn  8968: categories (reference to hash of category definitions).
1.663     raeburn  8969: 
1.655     raeburn  8970: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   8971:       categories and subcategories).
1.663     raeburn  8972: 
1.655     raeburn  8973: idx (reference to hash of counters used in Domain Coordinator interface for 
                   8974:       editing Course Categories).
1.663     raeburn  8975: 
1.655     raeburn  8976: jsarray (reference to array of categories used to create Javascript arrays for
                   8977:          Domain Coordinator interface for editing Course Categories).
                   8978: 
                   8979: Returns: nothing
                   8980: 
                   8981: Side effects: populates cats, idx and jsarray. 
                   8982: 
                   8983: =cut
                   8984: 
                   8985: sub gather_categories {
                   8986:     my ($categories,$cats,$idx,$jsarray) = @_;
                   8987:     my %counters;
                   8988:     my $num = 0;
                   8989:     foreach my $item (keys(%{$categories})) {
                   8990:         my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   8991:         if ($container eq '' && $depth == 0) {
                   8992:             $cats->[$depth][$categories->{$item}] = $cat;
                   8993:         } else {
                   8994:             $cats->[$depth]{$container}[$categories->{$item}] = $cat;
                   8995:         }
                   8996:         my ($escitem,$tail) = split(/:/,$item,2);
                   8997:         if ($counters{$tail} eq '') {
                   8998:             $counters{$tail} = $num;
                   8999:             $num ++;
                   9000:         }
                   9001:         if (ref($idx) eq 'HASH') {
                   9002:             $idx->{$item} = $counters{$tail};
                   9003:         }
                   9004:         if (ref($jsarray) eq 'ARRAY') {
                   9005:             push(@{$jsarray->[$counters{$tail}]},$item);
                   9006:         }
                   9007:     }
                   9008:     return;
                   9009: }
                   9010: 
                   9011: =pod
                   9012: 
                   9013: =item * &extract_categories()
                   9014: 
                   9015: Used to generate breadcrumb trails for course categories.
                   9016: 
                   9017: Inputs:
1.663     raeburn  9018: 
1.655     raeburn  9019: categories (reference to hash of category definitions).
1.663     raeburn  9020: 
1.655     raeburn  9021: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   9022:       categories and subcategories).
1.663     raeburn  9023: 
1.655     raeburn  9024: trails (reference to array of breacrumb trails for each category).
1.663     raeburn  9025: 
1.655     raeburn  9026: allitems (reference to hash - key is category key 
                   9027:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  9028: 
1.655     raeburn  9029: idx (reference to hash of counters used in Domain Coordinator interface for
                   9030:       editing Course Categories).
1.663     raeburn  9031: 
1.655     raeburn  9032: jsarray (reference to array of categories used to create Javascript arrays for
                   9033:          Domain Coordinator interface for editing Course Categories).
                   9034: 
1.665     raeburn  9035: subcats (reference to hash of arrays containing all subcategories within each 
                   9036:          category, -recursive)
                   9037: 
1.655     raeburn  9038: Returns: nothing
                   9039: 
                   9040: Side effects: populates trails and allitems hash references.
                   9041: 
                   9042: =cut
                   9043: 
                   9044: sub extract_categories {
1.665     raeburn  9045:     my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
1.655     raeburn  9046:     if (ref($categories) eq 'HASH') {
                   9047:         &gather_categories($categories,$cats,$idx,$jsarray);
                   9048:         if (ref($cats->[0]) eq 'ARRAY') {
                   9049:             for (my $i=0; $i<@{$cats->[0]}; $i++) {
                   9050:                 my $name = $cats->[0][$i];
                   9051:                 my $item = &escape($name).'::0';
                   9052:                 my $trailstr;
                   9053:                 if ($name eq 'instcode') {
                   9054:                     $trailstr = &mt('Official courses (with institutional codes)');
1.692.4.24  raeburn  9055:                 } elsif ($name eq 'communities') {
                   9056:                     $trailstr = &mt('Communities');
1.655     raeburn  9057:                 } else {
                   9058:                     $trailstr = $name;
                   9059:                 }
                   9060:                 if ($allitems->{$item} eq '') {
                   9061:                     push(@{$trails},$trailstr);
                   9062:                     $allitems->{$item} = scalar(@{$trails})-1;
                   9063:                 }
                   9064:                 my @parents = ($name);
                   9065:                 if (ref($cats->[1]{$name}) eq 'ARRAY') {
                   9066:                     for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) {
                   9067:                         my $category = $cats->[1]{$name}[$j];
1.665     raeburn  9068:                         if (ref($subcats) eq 'HASH') {
                   9069:                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                   9070:                         }
                   9071:                         &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
                   9072:                     }
                   9073:                 } else {
                   9074:                     if (ref($subcats) eq 'HASH') {
                   9075:                         $subcats->{$item} = [];
1.655     raeburn  9076:                     }
                   9077:                 }
                   9078:             }
                   9079:         }
                   9080:     }
                   9081:     return;
                   9082: }
                   9083: 
                   9084: =pod
                   9085: 
                   9086: =item *&recurse_categories()
                   9087: 
                   9088: Recursively used to generate breadcrumb trails for course categories.
                   9089: 
                   9090: Inputs:
1.663     raeburn  9091: 
1.655     raeburn  9092: cats (reference to array of arrays/hashes which encapsulates hierarchy of
                   9093:       categories and subcategories).
1.663     raeburn  9094: 
1.655     raeburn  9095: depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
1.663     raeburn  9096: 
                   9097: category (current course category, for which breadcrumb trail is being generated).
                   9098: 
                   9099: trails (reference to array of breadcrumb trails for each category).
                   9100: 
1.655     raeburn  9101: allitems (reference to hash - key is category key
                   9102:          (format: escaped(name):escaped(parent category):depth in hierarchy).
1.663     raeburn  9103: 
1.655     raeburn  9104: parents (array containing containers directories for current category, 
                   9105:          back to top level). 
                   9106: 
                   9107: Returns: nothing
                   9108: 
                   9109: Side effects: populates trails and allitems hash references
                   9110: 
                   9111: =cut
                   9112: 
                   9113: sub recurse_categories {
1.665     raeburn  9114:     my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
1.655     raeburn  9115:     my $shallower = $depth - 1;
                   9116:     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
                   9117:         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
                   9118:             my $name = $cats->[$depth]{$category}[$k];
                   9119:             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   9120:             my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   9121:             if ($allitems->{$item} eq '') {
                   9122:                 push(@{$trails},$trailstr);
                   9123:                 $allitems->{$item} = scalar(@{$trails})-1;
                   9124:             }
                   9125:             my $deeper = $depth+1;
                   9126:             push(@{$parents},$category);
1.665     raeburn  9127:             if (ref($subcats) eq 'HASH') {
                   9128:                 my $subcat = &escape($name).':'.$category.':'.$depth;
                   9129:                 for (my $j=@{$parents}; $j>=0; $j--) {
                   9130:                     my $higher;
                   9131:                     if ($j > 0) {
                   9132:                         $higher = &escape($parents->[$j]).':'.
                   9133:                                   &escape($parents->[$j-1]).':'.$j;
                   9134:                     } else {
                   9135:                         $higher = &escape($parents->[$j]).'::'.$j;
                   9136:                     }
                   9137:                     push(@{$subcats->{$higher}},$subcat);
                   9138:                 }
                   9139:             }
                   9140:             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
                   9141:                                 $subcats);
1.655     raeburn  9142:             pop(@{$parents});
                   9143:         }
                   9144:     } else {
                   9145:         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
                   9146:         my $trailstr = join(' -&gt; ',(@{$parents},$category));
                   9147:         if ($allitems->{$item} eq '') {
                   9148:             push(@{$trails},$trailstr);
                   9149:             $allitems->{$item} = scalar(@{$trails})-1;
                   9150:         }
                   9151:     }
                   9152:     return;
                   9153: }
                   9154: 
1.663     raeburn  9155: =pod
                   9156: 
                   9157: =item *&assign_categories_table()
                   9158: 
                   9159: Create a datatable for display of hierarchical categories in a domain,
                   9160: with checkboxes to allow a course to be categorized. 
                   9161: 
                   9162: Inputs:
                   9163: 
                   9164: cathash - reference to hash of categories defined for the domain (from
                   9165:           configuration.db)
                   9166: 
1.692.4.24  raeburn  9167: currcat - scalar with an & separated list of categories assigned to a course.
                   9168: 
                   9169: type    - scalar contains course type (Course or Community).
1.663     raeburn  9170: 
                   9171: Returns: $output (markup to be displayed) 
                   9172: 
                   9173: =cut
                   9174: 
                   9175: sub assign_categories_table {
1.692.4.24  raeburn  9176:     my ($cathash,$currcat,$type) = @_;
1.663     raeburn  9177:     my $output;
                   9178:     if (ref($cathash) eq 'HASH') {
                   9179:         my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
                   9180:         &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
                   9181:         $maxdepth = scalar(@cats);
                   9182:         if (@cats > 0) {
                   9183:             my $itemcount = 0;
                   9184:             if (ref($cats[0]) eq 'ARRAY') {
                   9185:                 my @currcategories;
                   9186:                 if ($currcat ne '') {
                   9187:                     @currcategories = split('&',$currcat);
                   9188:                 }
1.692.4.24  raeburn  9189:                 my $table;
1.663     raeburn  9190:                 for (my $i=0; $i<@{$cats[0]}; $i++) {
                   9191:                     my $parent = $cats[0][$i];
                   9192:                     next if ($parent eq 'instcode');
1.692.4.24  raeburn  9193:                     if ($type eq 'Community') {
                   9194:                         next unless ($parent eq 'communities');
                   9195:                     } else {
                   9196:                         next if ($parent eq 'communities');
                   9197:                     }
                   9198:                     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.663     raeburn  9199:                     my $item = &escape($parent).'::0';
                   9200:                     my $checked = '';
                   9201:                     if (@currcategories > 0) {
                   9202:                         if (grep(/^\Q$item\E$/,@currcategories)) {
                   9203:                             $checked = ' checked="checked" ';
                   9204:                         }
                   9205:                     }
1.692.4.24  raeburn  9206:                     my $parent_title = $parent;
                   9207:                     if ($parent eq 'communities') {
                   9208:                         $parent_title = &mt('Communities');
                   9209:                     }
                   9210:                     $table .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.
                   9211:                               '<input type="checkbox" name="usecategory" value="'.
                   9212:                               $item.'"'.$checked.' />'.$parent_title.'</span>'.
                   9213:                               '<input type="hidden" name="catname" value="'.$parent.'" /></td>';
1.663     raeburn  9214:                     my $depth = 1;
                   9215:                     push(@path,$parent);
1.692.4.24  raeburn  9216:                     $table .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
1.663     raeburn  9217:                     pop(@path);
1.692.4.24  raeburn  9218:                     $table .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
1.663     raeburn  9219:                     $itemcount ++;
                   9220:                 }
1.692.4.24  raeburn  9221:                 if ($itemcount) {
                   9222:                     $output = &Apache::loncommon::start_data_table().
                   9223:                               $table.
                   9224:                               &Apache::loncommon::end_data_table();
                   9225:                 }
1.663     raeburn  9226:             }
                   9227:         }
                   9228:     }
                   9229:     return $output;
                   9230: }
                   9231: 
                   9232: =pod
                   9233: 
                   9234: =item *&assign_category_rows()
                   9235: 
                   9236: Create a datatable row for display of nested categories in a domain,
                   9237: with checkboxes to allow a course to be categorized,called recursively.
                   9238: 
                   9239: Inputs:
                   9240: 
                   9241: itemcount - track row number for alternating colors
                   9242: 
                   9243: cats - reference to array of arrays/hashes which encapsulates hierarchy of
                   9244:       categories and subcategories.
                   9245: 
                   9246: depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
                   9247: 
                   9248: parent - parent of current category item
                   9249: 
                   9250: path - Array containing all categories back up through the hierarchy from the
                   9251:        current category to the top level.
                   9252: 
                   9253: currcategories - reference to array of current categories assigned to the course
                   9254: 
                   9255: Returns: $output (markup to be displayed).
                   9256: 
                   9257: =cut
                   9258: 
                   9259: sub assign_category_rows {
                   9260:     my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
                   9261:     my ($text,$name,$item,$chgstr);
                   9262:     if (ref($cats) eq 'ARRAY') {
                   9263:         my $maxdepth = scalar(@{$cats});
                   9264:         if (ref($cats->[$depth]) eq 'HASH') {
                   9265:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   9266:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   9267:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   9268:                 $text .= '<td><table class="LC_datatable">';
                   9269:                 for (my $j=0; $j<$numchildren; $j++) {
                   9270:                     $name = $cats->[$depth]{$parent}[$j];
                   9271:                     $item = &escape($name).':'.&escape($parent).':'.$depth;
                   9272:                     my $deeper = $depth+1;
                   9273:                     my $checked = '';
                   9274:                     if (ref($currcategories) eq 'ARRAY') {
                   9275:                         if (@{$currcategories} > 0) {
                   9276:                             if (grep(/^\Q$item\E$/,@{$currcategories})) {
                   9277:                                 $checked = ' checked="checked" ';
                   9278:                             }
                   9279:                         }
                   9280:                     }
1.664     raeburn  9281:                     $text .= '<tr><td><span class="LC_nobreak"><label>'.
                   9282:                              '<input type="checkbox" name="usecategory" value="'.
1.675     raeburn  9283:                              $item.'"'.$checked.' />'.$name.'</label></span>'.
                   9284:                              '<input type="hidden" name="catname" value="'.$name.'" />'.
                   9285:                              '</td><td>';
1.663     raeburn  9286:                     if (ref($path) eq 'ARRAY') {
                   9287:                         push(@{$path},$name);
                   9288:                         $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
                   9289:                         pop(@{$path});
                   9290:                     }
                   9291:                     $text .= '</td></tr>';
                   9292:                 }
                   9293:                 $text .= '</table></td>';
                   9294:             }
                   9295:         }
                   9296:     }
                   9297:     return $text;
                   9298: }
                   9299: 
1.655     raeburn  9300: ############################################################
                   9301: ############################################################
                   9302: 
                   9303: 
1.443     albertel 9304: sub commit_customrole {
1.664     raeburn  9305:     my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_;
1.630     raeburn  9306:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url.
1.443     albertel 9307:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   9308:                          ($end?', ending '.localtime($end):'').': <b>'.
                   9309:               &Apache::lonnet::assigncustomrole(
1.664     raeburn  9310:                  $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context).
1.443     albertel 9311:                  '</b><br />';
                   9312:     return $output;
                   9313: }
                   9314: 
                   9315: sub commit_standardrole {
1.541     raeburn  9316:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
                   9317:     my ($output,$logmsg,$linefeed);
                   9318:     if ($context eq 'auto') {
                   9319:         $linefeed = "\n";
                   9320:     } else {
                   9321:         $linefeed = "<br />\n";
                   9322:     }  
1.443     albertel 9323:     if ($three eq 'st') {
1.541     raeburn  9324:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,
                   9325:                                          $one,$two,$sec,$context);
                   9326:         if (($result =~ /^error/) || ($result eq 'not_in_class') || 
1.626     raeburn  9327:             ($result eq 'unknown_course') || ($result eq 'refused')) {
                   9328:             $output = $logmsg.' '.&mt('Error: ').$result."\n"; 
1.443     albertel 9329:         } else {
1.541     raeburn  9330:             $output = $logmsg.$linefeed.&mt('Assigning').' '.$three.' in '.$url.
1.443     albertel 9331:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  9332:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
                   9333:             if ($context eq 'auto') {
                   9334:                 $output .= $result.$linefeed.&mt('Add to classlist').': ok';
                   9335:             } else {
                   9336:                $output .= '<b>'.$result.'</b>'.$linefeed.
                   9337:                &mt('Add to classlist').': <b>ok</b>';
                   9338:             }
                   9339:             $output .= $linefeed;
1.443     albertel 9340:         }
                   9341:     } else {
                   9342:         $output = &mt('Assigning').' '.$three.' in '.$url.
                   9343:                ($start?', '.&mt('starting').' '.localtime($start):'').
1.541     raeburn  9344:                ($end?', '.&mt('ending').' '.localtime($end):'').': ';
1.652     raeburn  9345:         my $result = &Apache::lonnet::assignrole($udom,$uname,$url,$three,$end,$start,'','',$context);
1.541     raeburn  9346:         if ($context eq 'auto') {
                   9347:             $output .= $result.$linefeed;
                   9348:         } else {
                   9349:             $output .= '<b>'.$result.'</b>'.$linefeed;
                   9350:         }
1.443     albertel 9351:     }
                   9352:     return $output;
                   9353: }
                   9354: 
                   9355: sub commit_studentrole {
1.541     raeburn  9356:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec,$context) = @_;
1.626     raeburn  9357:     my ($result,$linefeed,$oldsecurl,$newsecurl);
1.541     raeburn  9358:     if ($context eq 'auto') {
                   9359:         $linefeed = "\n";
                   9360:     } else {
                   9361:         $linefeed = '<br />'."\n";
                   9362:     }
1.443     albertel 9363:     if (defined($one) && defined($two)) {
                   9364:         my $cid=$one.'_'.$two;
                   9365:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
                   9366:         my $secchange = 0;
                   9367:         my $expire_role_result;
                   9368:         my $modify_section_result;
1.628     raeburn  9369:         if ($oldsec ne '-1') { 
                   9370:             if ($oldsec ne $sec) {
1.443     albertel 9371:                 $secchange = 1;
1.628     raeburn  9372:                 my $now = time;
1.443     albertel 9373:                 my $uurl='/'.$cid;
                   9374:                 $uurl=~s/\_/\//g;
                   9375:                 if ($oldsec) {
                   9376:                     $uurl.='/'.$oldsec;
                   9377:                 }
1.626     raeburn  9378:                 $oldsecurl = $uurl;
1.628     raeburn  9379:                 $expire_role_result = 
1.652     raeburn  9380:                     &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',$now,'','',$context);
1.628     raeburn  9381:                 if ($env{'request.course.sec'} ne '') { 
                   9382:                     if ($expire_role_result eq 'refused') {
                   9383:                         my @roles = ('st');
                   9384:                         my @statuses = ('previous');
                   9385:                         my @roledoms = ($one);
                   9386:                         my $withsec = 1;
                   9387:                         my %roleshash = 
                   9388:                             &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   9389:                                               \@statuses,\@roles,\@roledoms,$withsec);
                   9390:                         if (defined ($roleshash{$two.':'.$one.':st:'.$oldsec})) {
                   9391:                             my ($oldstart,$oldend) = 
                   9392:                                 split(':',$roleshash{$two.':'.$one.':st:'.$oldsec});
                   9393:                             if ($oldend > 0 && $oldend <= $now) {
                   9394:                                 $expire_role_result = 'ok';
                   9395:                             }
                   9396:                         }
                   9397:                     }
                   9398:                 }
1.443     albertel 9399:                 $result = $expire_role_result;
                   9400:             }
                   9401:         }
                   9402:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
1.652     raeburn  9403:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid,'',$context);
1.443     albertel 9404:             if ($modify_section_result =~ /^ok/) {
                   9405:                 if ($secchange == 1) {
1.628     raeburn  9406:                     if ($sec eq '') {
                   9407:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to student role without a section.',$uname,$oldsec).$linefeed;
                   9408:                     } else {
                   9409:                         $$logmsg .= &mt('Section for [_1] switched from (possibly expired) old section: [_2] to new section: [_3].',$uname,$oldsec,$sec).$linefeed;
                   9410:                     }
1.443     albertel 9411:                 } elsif ($oldsec eq '-1') {
1.628     raeburn  9412:                     if ($sec eq '') {
                   9413:                         $$logmsg .= &mt('New student role without a section for [_1] in course [_2].',$uname,$cid).$linefeed;
                   9414:                     } else {
                   9415:                         $$logmsg .= &mt('New student role for [_1] in section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   9416:                     }
1.443     albertel 9417:                 } else {
1.628     raeburn  9418:                     if ($sec eq '') {
                   9419:                         $$logmsg .= &mt('Student [_1] assigned to course [_2] without a section.',$uname,$cid).$linefeed;
                   9420:                     } else {
                   9421:                         $$logmsg .= &mt('Student [_1] assigned to section [_2] in course [_3].',$uname,$sec,$cid).$linefeed;
                   9422:                     }
1.443     albertel 9423:                 }
                   9424:             } else {
1.628     raeburn  9425:                 if ($secchange) {       
                   9426:                     $$logmsg .= &mt('Error when attempting section change for [_1] from old section "[_2]" to new section: "[_3]" in course [_4] -error:',$uname,$oldsec,$sec,$cid).' '.$modify_section_result.$linefeed;
                   9427:                 } else {
                   9428:                     $$logmsg .= &mt('Error when attempting to modify role for [_1] for section: "[_2]" in course [_3] -error:',$uname,$sec,$cid).' '.$modify_section_result.$linefeed;
                   9429:                 }
1.443     albertel 9430:             }
                   9431:             $result = $modify_section_result;
                   9432:         } elsif ($secchange == 1) {
1.628     raeburn  9433:             if ($oldsec eq '') {
                   9434:                 $$logmsg .= &mt('Error when attempting to expire existing role without a section for [_1] in course [_3] -error: ',$uname,$cid).' '.$expire_role_result.$linefeed;
                   9435:             } else {
                   9436:                 $$logmsg .= &mt('Error when attempting to expire existing role for [_1] in section [_2] in course [_3] -error: ',$uname,$oldsec,$cid).' '.$expire_role_result.$linefeed;
                   9437:             }
1.626     raeburn  9438:             if ($expire_role_result eq 'refused') {
                   9439:                 my $newsecurl = '/'.$cid;
                   9440:                 $newsecurl =~ s/\_/\//g;
                   9441:                 if ($sec ne '') {
                   9442:                     $newsecurl.='/'.$sec;
                   9443:                 }
                   9444:                 if (&Apache::lonnet::allowed('cst',$newsecurl) && !(&Apache::lonnet::allowed('cst',$oldsecurl))) {
                   9445:                     if ($sec eq '') {
                   9446:                         $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments unaffiliated with any section.',$sec).$linefeed;
                   9447:                     } else {
                   9448:                         $$logmsg .= &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$sec).$linefeed;
                   9449:                     }
                   9450:                 }
                   9451:             }
1.443     albertel 9452:         }
                   9453:     } else {
1.626     raeburn  9454:         $$logmsg .= &mt('Incomplete course id defined.').$linefeed.&mt('Addition of user [_1] from domain [_2] to course [_3], section [_4] not completed.',$uname,$udom,$one.'_'.$two,$sec).$linefeed;
1.443     albertel 9455:         $result = "error: incomplete course id\n";
                   9456:     }
                   9457:     return $result;
                   9458: }
                   9459: 
                   9460: ############################################################
                   9461: ############################################################
                   9462: 
1.566     albertel 9463: sub check_clone {
1.578     raeburn  9464:     my ($args,$linefeed) = @_;
1.566     albertel 9465:     my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
                   9466:     my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
                   9467:     my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                   9468:     my $clonemsg;
                   9469:     my $can_clone = 0;
1.692.4.30  raeburn  9470:     my $lctype = lc($args->{'crstype'});
1.692.4.22  raeburn  9471:     if ($lctype ne 'community') {
                   9472:         $lctype = 'course';
                   9473:     }
1.566     albertel 9474:     if ($clonehome eq 'no_host') {
1.692.4.30  raeburn  9475:         if ($args->{'crstype'} eq 'Community') {
1.692.4.22  raeburn  9476:             $clonemsg = &mt('No new community created.').$linefeed.&mt('A new community could not be cloned from the specified original - [_1] - because it is a non-existent community.',$args->{'clonecourse'}.':'.$args->{'clonedomain'});
                   9477:         } else {
                   9478:             $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'});
                   9479:         }
1.566     albertel 9480:     } else {
                   9481: 	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
1.692.4.30  raeburn  9482:         if ($args->{'crstype'} eq 'Community') {
1.692.4.22  raeburn  9483:             if ($clonedesc{'type'} ne 'Community') {
                   9484:                  $clonemsg = &mt('No new community created.').$linefeed.&mt('A new community could not be cloned from the specified original - [_1] - because it is a course not a community.',$args->{'clonecourse'}.':'.$args->{'clonedomain'});
                   9485:                 return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   9486:             }
                   9487:         }
                   9488:         if (($env{'request.role.domain'} eq $args->{'clonedomain'}) &&
1.692.4.12  raeburn  9489:             (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'}))) {
1.692.4.22  raeburn  9490:             $can_clone = 1;
                   9491:         } else {
                   9492:             my %clonehash = &Apache::lonnet::get('environment',['cloners'],
                   9493:                                                  $args->{'clonedomain'},$args->{'clonecourse'});
                   9494:             my @cloners = split(/,/,$clonehash{'cloners'});
1.578     raeburn  9495:             if (grep(/^\*$/,@cloners)) {
                   9496:                 $can_clone = 1;
                   9497:             } elsif (grep(/^\*\:\Q$args->{'ccdomain'}\E$/,@cloners)) {
                   9498:                 $can_clone = 1;
                   9499:             } else {
1.692.4.22  raeburn  9500:                 my $ccrole = 'cc';
1.692.4.30  raeburn  9501:                 if ($args->{'crstype'} eq 'Community') {
1.692.4.22  raeburn  9502:                     $ccrole = 'co';
                   9503:                 }
                   9504:                 my %roleshash =
                   9505:                     &Apache::lonnet::get_my_roles($args->{'ccuname'},
                   9506:                                          $args->{'ccdomain'},
                   9507:                                          'userroles',['active'],[$ccrole],
                   9508:                                          [$args->{'clonedomain'}]);
                   9509:                 if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':'.$ccrole}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
                   9510:                     $can_clone = 1;
1.692.4.29  raeburn  9511:                 } elsif (&Apache::lonnet::is_course_owner($args->{'clonedomain'},$args->{'clonecourse'},$args->{'ccuname'},$args->{'ccdomain'})) {
                   9512:                     $can_clone = 1;
1.692.4.22  raeburn  9513:                 } else {
1.692.4.30  raeburn  9514:                     if ($args->{'crstype'} eq 'Community') {
1.692.4.22  raeburn  9515:                         $clonemsg = &mt('No new community created.').$linefeed.&mt('The new community could not be cloned from the existing community because the new community owner ([_1]) does not have cloning rights in the existing community ([_2]).',$args->{'ccuname'}.':'.$args->{'ccdomain'},$clonedesc{'description'});
                   9516:                     } else {
                   9517:                         $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'});
                   9518:                     }
                   9519:                 }
                   9520:             }
1.578     raeburn  9521:         }
1.566     albertel 9522:     }
                   9523:     return ($can_clone, $clonemsg, $cloneid, $clonehome);
                   9524: }
                   9525: 
1.444     albertel 9526: sub construct_course {
1.692.4.14  raeburn  9527:     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context,$cnum,$category) = @_;
1.444     albertel 9528:     my $outcome;
1.541     raeburn  9529:     my $linefeed =  '<br />'."\n";
                   9530:     if ($context eq 'auto') {
                   9531:         $linefeed = "\n";
                   9532:     }
1.566     albertel 9533: 
                   9534: #
                   9535: # Are we cloning?
                   9536: #
                   9537:     my ($can_clone, $clonemsg, $cloneid, $clonehome);
                   9538:     if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
1.578     raeburn  9539: 	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args,$linefeed);
1.566     albertel 9540: 	if ($context ne 'auto') {
1.578     raeburn  9541:             if ($clonemsg ne '') {
                   9542: 	        $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
                   9543:             }
1.566     albertel 9544: 	}
                   9545: 	$outcome .= $clonemsg.$linefeed;
                   9546: 
                   9547:         if (!$can_clone) {
                   9548: 	    return (0,$outcome);
                   9549: 	}
                   9550:     }
                   9551: 
1.444     albertel 9552: #
                   9553: # Open course
                   9554: #
                   9555:     my $crstype = lc($args->{'crstype'});
                   9556:     my %cenv=();
                   9557:     $$courseid=&Apache::lonnet::createcourse($args->{'course_domain'},
                   9558:                                              $args->{'cdescr'},
                   9559:                                              $args->{'curl'},
                   9560:                                              $args->{'course_home'},
                   9561:                                              $args->{'nonstandard'},
                   9562:                                              $args->{'crscode'},
                   9563:                                              $args->{'ccuname'}.':'.
                   9564:                                              $args->{'ccdomain'},
1.692.4.12  raeburn  9565:                                              $args->{'crstype'},
1.692.4.14  raeburn  9566:                                              $cnum,$context,$category);
1.692.4.12  raeburn  9567: 
1.444     albertel 9568: 
                   9569:     # Note: The testing routines depend on this being output; see 
                   9570:     # Utils::Course. This needs to at least be output as a comment
                   9571:     # if anyone ever decides to not show this, and Utils::Course::new
                   9572:     # will need to be suitably modified.
1.541     raeburn  9573:     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
1.444     albertel 9574: #
                   9575: # Check if created correctly
                   9576: #
1.479     albertel 9577:     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
1.444     albertel 9578:     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
1.541     raeburn  9579:     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
1.566     albertel 9580: 
1.444     albertel 9581: #
1.566     albertel 9582: # Do the cloning
                   9583: #   
                   9584:     if ($can_clone && $cloneid) {
                   9585: 	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
                   9586: 	if ($context ne 'auto') {
                   9587: 	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
                   9588: 	}
                   9589: 	$outcome .= $clonemsg.$linefeed;
                   9590: 	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
1.444     albertel 9591: # Copy all files
1.637     www      9592: 	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid,$args->{'datemode'},$args->{'dateshift'});
1.444     albertel 9593: # Restore URL
1.566     albertel 9594: 	$cenv{'url'}=$oldcenv{'url'};
1.444     albertel 9595: # Restore title
1.566     albertel 9596: 	$cenv{'description'}=$oldcenv{'description'};
1.692.4.33  raeburn  9597: # Restore creation date, creator and creation context.
                   9598:         $cenv{'internal.created'}=$oldcenv{'internal.created'};
                   9599:         $cenv{'internal.creator'}=$oldcenv{'internal.creator'};
                   9600:         $cenv{'internal.creationcontext'}=$oldcenv{'internal.creationcontext'};
1.444     albertel 9601: # Mark as cloned
1.566     albertel 9602: 	$cenv{'clonedfrom'}=$cloneid;
1.638     www      9603: # Need to clone grading mode
                   9604:         my %newenv=&Apache::lonnet::get('environment',['grading'],$$crsudom,$$crsunum);
                   9605:         $cenv{'grading'}=$newenv{'grading'};
                   9606: # Do not clone these environment entries
                   9607:         &Apache::lonnet::del('environment',
                   9608:                   ['default_enrollment_start_date',
                   9609:                    'default_enrollment_end_date',
                   9610:                    'question.email',
                   9611:                    'policy.email',
                   9612:                    'comment.email',
                   9613:                    'pch.users.denied',
1.692.4.2  raeburn  9614:                    'plc.users.denied',
                   9615:                    'hidefromcat',
                   9616:                    'categories'],
1.638     www      9617:                    $$crsudom,$$crsunum);
1.444     albertel 9618:     }
1.566     albertel 9619: 
1.444     albertel 9620: #
                   9621: # Set environment (will override cloned, if existing)
                   9622: #
                   9623:     my @sections = ();
                   9624:     my @xlists = ();
                   9625:     if ($args->{'crstype'}) {
                   9626:         $cenv{'type'}=$args->{'crstype'};
                   9627:     }
                   9628:     if ($args->{'crsid'}) {
                   9629:         $cenv{'courseid'}=$args->{'crsid'};
                   9630:     }
                   9631:     if ($args->{'crscode'}) {
                   9632:         $cenv{'internal.coursecode'}=$args->{'crscode'};
                   9633:     }
                   9634:     if ($args->{'crsquota'} ne '') {
                   9635:         $cenv{'internal.coursequota'}=$args->{'crsquota'};
                   9636:     } else {
                   9637:         $cenv{'internal.coursequota'}=$args->{'crsquota'} = 20;
                   9638:     }
                   9639:     if ($args->{'ccuname'}) {
                   9640:         $cenv{'internal.courseowner'} = $args->{'ccuname'}.
                   9641:                                         ':'.$args->{'ccdomain'};
                   9642:     } else {
                   9643:         $cenv{'internal.courseowner'} = $args->{'curruser'};
                   9644:     }
                   9645:     my @badclasses = (); # Used to accumulate sections/crosslistings that did not pass classlist access check for course owner.
                   9646:     if ($args->{'crssections'}) {
                   9647:         $cenv{'internal.sectionnums'} = '';
                   9648:         if ($args->{'crssections'} =~ m/,/) {
                   9649:             @sections = split/,/,$args->{'crssections'};
                   9650:         } else {
                   9651:             $sections[0] = $args->{'crssections'};
                   9652:         }
                   9653:         if (@sections > 0) {
                   9654:             foreach my $item (@sections) {
                   9655:                 my ($sec,$gp) = split/:/,$item;
                   9656:                 my $class = $args->{'crscode'}.$sec;
                   9657:                 my $addcheck = &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$class,$cenv{'internal.courseowner'});
                   9658:                 $cenv{'internal.sectionnums'} .= $item.',';
                   9659:                 unless ($addcheck eq 'ok') {
                   9660:                     push @badclasses, $class;
                   9661:                 }
                   9662:             }
                   9663:             $cenv{'internal.sectionnums'} =~ s/,$//;
                   9664:         }
                   9665:     }
                   9666: # do not hide course coordinator from staff listing, 
                   9667: # even if privileged
                   9668:     $cenv{'nothideprivileged'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   9669: # add crosslistings
                   9670:     if ($args->{'crsxlist'}) {
                   9671:         $cenv{'internal.crosslistings'}='';
                   9672:         if ($args->{'crsxlist'} =~ m/,/) {
                   9673:             @xlists = split/,/,$args->{'crsxlist'};
                   9674:         } else {
                   9675:             $xlists[0] = $args->{'crsxlist'};
                   9676:         }
                   9677:         if (@xlists > 0) {
                   9678:             foreach my $item (@xlists) {
                   9679:                 my ($xl,$gp) = split/:/,$item;
                   9680:                 my $addcheck =  &Apache::lonnet::auto_new_course($$crsunum,$$crsudom,$xl,$cenv{'internal.courseowner'});
                   9681:                 $cenv{'internal.crosslistings'} .= $item.',';
                   9682:                 unless ($addcheck eq 'ok') {
                   9683:                     push @badclasses, $xl;
                   9684:                 }
                   9685:             }
                   9686:             $cenv{'internal.crosslistings'} =~ s/,$//;
                   9687:         }
                   9688:     }
                   9689:     if ($args->{'autoadds'}) {
                   9690:         $cenv{'internal.autoadds'}=$args->{'autoadds'};
                   9691:     }
                   9692:     if ($args->{'autodrops'}) {
                   9693:         $cenv{'internal.autodrops'}=$args->{'autodrops'};
                   9694:     }
                   9695: # check for notification of enrollment changes
                   9696:     my @notified = ();
                   9697:     if ($args->{'notify_owner'}) {
                   9698:         if ($args->{'ccuname'} ne '') {
                   9699:             push(@notified,$args->{'ccuname'}.':'.$args->{'ccdomain'});
                   9700:         }
                   9701:     }
                   9702:     if ($args->{'notify_dc'}) {
                   9703:         if ($uname ne '') { 
1.630     raeburn  9704:             push(@notified,$uname.':'.$udom);
1.444     albertel 9705:         }
                   9706:     }
                   9707:     if (@notified > 0) {
                   9708:         my $notifylist;
                   9709:         if (@notified > 1) {
                   9710:             $notifylist = join(',',@notified);
                   9711:         } else {
                   9712:             $notifylist = $notified[0];
                   9713:         }
                   9714:         $cenv{'internal.notifylist'} = $notifylist;
                   9715:     }
                   9716:     if (@badclasses > 0) {
                   9717:         my %lt=&Apache::lonlocal::texthash(
                   9718:                 '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',
                   9719:                 'dnhr' => 'does not have rights to access enrollment in these classes',
                   9720:                 'adby' => 'as determined by the policies of your institution on access to official classlists'
                   9721:         );
1.541     raeburn  9722:         my $badclass_msg = $cenv{'internal.courseowner'}.') - '.$lt{'dnhr'}.
                   9723:                            ' ('.$lt{'adby'}.')';
                   9724:         if ($context eq 'auto') {
                   9725:             $outcome .= $badclass_msg.$linefeed;
1.566     albertel 9726:             $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
1.541     raeburn  9727:             foreach my $item (@badclasses) {
                   9728:                 if ($context eq 'auto') {
                   9729:                     $outcome .= " - $item\n";
                   9730:                 } else {
                   9731:                     $outcome .= "<li>$item</li>\n";
                   9732:                 }
                   9733:             }
                   9734:             if ($context eq 'auto') {
                   9735:                 $outcome .= $linefeed;
                   9736:             } else {
1.566     albertel 9737:                 $outcome .= "</ul><br /><br /></div>\n";
1.541     raeburn  9738:             }
                   9739:         } 
1.444     albertel 9740:     }
                   9741:     if ($args->{'no_end_date'}) {
                   9742:         $args->{'endaccess'} = 0;
                   9743:     }
                   9744:     $cenv{'internal.autostart'}=$args->{'enrollstart'};
                   9745:     $cenv{'internal.autoend'}=$args->{'enrollend'};
                   9746:     $cenv{'default_enrollment_start_date'}=$args->{'startaccess'};
                   9747:     $cenv{'default_enrollment_end_date'}=$args->{'endaccess'};
                   9748:     if ($args->{'showphotos'}) {
                   9749:       $cenv{'internal.showphotos'}=$args->{'showphotos'};
                   9750:     }
                   9751:     $cenv{'internal.authtype'} = $args->{'authtype'};
                   9752:     $cenv{'internal.autharg'} = $args->{'autharg'}; 
                   9753:     if ( ($cenv{'internal.authtype'} =~ /^krb/) && ($cenv{'internal.autoadds'} == 1)) {
                   9754:         if (! defined($cenv{'internal.autharg'}) || $cenv{'internal.autharg'}  eq '') {
1.541     raeburn  9755:             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'); 
                   9756:             if ($context eq 'auto') {
                   9757:                 $outcome .= $krb_msg;
                   9758:             } else {
1.566     albertel 9759:                 $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
1.541     raeburn  9760:             }
                   9761:             $outcome .= $linefeed;
1.444     albertel 9762:         }
                   9763:     }
                   9764:     if (($args->{'ccdomain'}) && ($args->{'ccuname'})) {
                   9765:        if ($args->{'setpolicy'}) {
                   9766:            $cenv{'policy.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   9767:        }
                   9768:        if ($args->{'setcontent'}) {
                   9769:            $cenv{'question.email'}=$args->{'ccuname'}.':'.$args->{'ccdomain'};
                   9770:        }
                   9771:     }
                   9772:     if ($args->{'reshome'}) {
                   9773: 	$cenv{'reshome'}=$args->{'reshome'}.'/';
                   9774: 	$cenv{'reshome'}=~s/\/+$/\//;
                   9775:     }
                   9776: #
                   9777: # course has keyed access
                   9778: #
                   9779:     if ($args->{'setkeys'}) {
                   9780:        $cenv{'keyaccess'}='yes';
                   9781:     }
                   9782: # if specified, key authority is not course, but user
                   9783: # only active if keyaccess is yes
                   9784:     if ($args->{'keyauth'}) {
1.487     albertel 9785: 	my ($user,$domain) = split(':',$args->{'keyauth'});
                   9786: 	$user = &LONCAPA::clean_username($user);
                   9787: 	$domain = &LONCAPA::clean_username($domain);
1.488     foxr     9788: 	if ($user ne '' && $domain ne '') {
1.487     albertel 9789: 	    $cenv{'keyauth'}=$user.':'.$domain;
1.444     albertel 9790: 	}
                   9791:     }
                   9792: 
                   9793:     if ($args->{'disresdis'}) {
                   9794:         $cenv{'pch.roles.denied'}='st';
                   9795:     }
                   9796:     if ($args->{'disablechat'}) {
                   9797:         $cenv{'plc.roles.denied'}='st';
                   9798:     }
                   9799: 
                   9800:     # Record we've not yet viewed the Course Initialization Helper for this 
                   9801:     # course
                   9802:     $cenv{'course.helper.not.run'} = 1;
                   9803:     #
                   9804:     # Use new Randomseed
                   9805:     #
                   9806:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
                   9807:     $cenv{'receiptalg'}=&Apache::lonnet::latest_receipt_algorithm_id();;
                   9808:     #
                   9809:     # The encryption code and receipt prefix for this course
                   9810:     #
                   9811:     $cenv{'internal.encseed'}=$Apache::lonnet::perlvar{'lonReceipt'}.$$.time.int(rand(9999));
                   9812:     $cenv{'internal.encpref'}=100+int(9*rand(99));
                   9813:     #
                   9814:     # By default, use standard grading
                   9815:     if (!defined($cenv{'grading'})) { $cenv{'grading'} = 'standard'; }
                   9816: 
1.541     raeburn  9817:     $outcome .= $linefeed.&mt('Setting environment').': '.                 
                   9818:           &Apache::lonnet::put('environment',\%cenv,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 9819: #
                   9820: # Open all assignments
                   9821: #
                   9822:     if ($args->{'openall'}) {
                   9823:        my $storeunder=$$crsudom.'_'.$$crsunum.'.0.opendate';
                   9824:        my %storecontent = ($storeunder         => time,
                   9825:                            $storeunder.'.type' => 'date_start');
                   9826:        
                   9827:        $outcome .= &mt('Opening all assignments').': '.&Apache::lonnet::cput
1.541     raeburn  9828:                  ('resourcedata',\%storecontent,$$crsudom,$$crsunum).$linefeed;
1.444     albertel 9829:    }
                   9830: #
                   9831: # Set first page
                   9832: #
                   9833:     unless (($args->{'nonstandard'}) || ($args->{'firstres'} eq 'blank')
                   9834: 	    || ($cloneid)) {
1.445     albertel 9835: 	use LONCAPA::map;
1.444     albertel 9836: 	$outcome .= &mt('Setting first resource').': ';
1.445     albertel 9837: 
                   9838: 	my $map = '/uploaded/'.$$crsudom.'/'.$$crsunum.'/default.sequence';
                   9839:         my ($errtext,$fatal)=&LONCAPA::map::mapread($map);
                   9840: 
1.444     albertel 9841:         $outcome .= ($fatal?$errtext:'read ok').' - ';
                   9842:         my $title; my $url;
                   9843:         if ($args->{'firstres'} eq 'syl') {
1.690     bisitz   9844: 	    $title=&mt('Syllabus');
1.444     albertel 9845:             $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus';
                   9846:         } else {
1.690     bisitz   9847:             $title=&mt('Navigate Contents');
1.444     albertel 9848:             $url='/adm/navmaps';
                   9849:         }
1.445     albertel 9850: 
                   9851:         $LONCAPA::map::resources[1]=$title.':'.$url.':false:start:res';
                   9852: 	(my $outtext,$errtext) = &LONCAPA::map::storemap($map,1);
                   9853: 
                   9854: 	if ($errtext) { $fatal=2; }
1.541     raeburn  9855:         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
1.444     albertel 9856:     }
1.566     albertel 9857: 
                   9858:     return (1,$outcome);
1.444     albertel 9859: }
                   9860: 
                   9861: ############################################################
                   9862: ############################################################
                   9863: 
1.378     raeburn  9864: sub course_type {
                   9865:     my ($cid) = @_;
                   9866:     if (!defined($cid)) {
                   9867:         $cid = $env{'request.course.id'};
                   9868:     }
1.404     albertel 9869:     if (defined($env{'course.'.$cid.'.type'})) {
                   9870:         return $env{'course.'.$cid.'.type'};
1.378     raeburn  9871:     } else {
                   9872:         return 'Course';
1.377     raeburn  9873:     }
                   9874: }
1.156     albertel 9875: 
1.406     raeburn  9876: sub group_term {
                   9877:     my $crstype = &course_type();
                   9878:     my %names = (
1.692.4.6  raeburn  9879:                   'Course'    => 'group',
                   9880:                   'Community' => 'group',
1.406     raeburn  9881:                 );
                   9882:     return $names{$crstype};
                   9883: }
                   9884: 
1.692.4.20  raeburn  9885: sub course_types {
                   9886:     my @types = ('official','unofficial','community');
                   9887:     my %typename = (
                   9888:                          official   => 'Official course',
                   9889:                          unofficial => 'Unofficial course',
                   9890:                          community  => 'Community',
                   9891:                    );
                   9892:     return (\@types,\%typename);
                   9893: }
                   9894: 
1.156     albertel 9895: sub icon {
                   9896:     my ($file)=@_;
1.505     albertel 9897:     my $curfext = lc((split(/\./,$file))[-1]);
1.168     albertel 9898:     my $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/unknown.gif';
1.156     albertel 9899:     my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.168     albertel 9900:     if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) {
                   9901: 	if (-e  $Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                   9902: 	          $Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   9903: 	            $curfext.".gif") {
                   9904: 	    $iconname=$Apache::lonnet::perlvar{'lonIconsURL'}.'/'.
                   9905: 		$curfext.".gif";
                   9906: 	}
                   9907:     }
1.249     albertel 9908:     return &lonhttpdurl($iconname);
1.154     albertel 9909: } 
1.84      albertel 9910: 
1.575     albertel 9911: sub lonhttpdurl {
1.692     www      9912: #
                   9913: # Had been used for "small fry" static images on separate port 8080.
                   9914: # Modify here if lightweight http functionality desired again.
                   9915: # Currently eliminated due to increasing firewall issues.
                   9916: #
1.575     albertel 9917:     my ($url)=@_;
1.692     www      9918:     return $url;
1.215     albertel 9919: }
                   9920: 
1.213     albertel 9921: sub connection_aborted {
                   9922:     my ($r)=@_;
                   9923:     $r->print(" ");$r->rflush();
                   9924:     my $c = $r->connection;
                   9925:     return $c->aborted();
                   9926: }
                   9927: 
1.221     foxr     9928: #    Escapes strings that may have embedded 's that will be put into
1.222     foxr     9929: #    strings as 'strings'.
                   9930: sub escape_single {
1.221     foxr     9931:     my ($input) = @_;
1.223     albertel 9932:     $input =~ s/\\/\\\\/g;	# Escape the \'s..(must be first)>
1.221     foxr     9933:     $input =~ s/\'/\\\'/g;	# Esacpe the 's....
                   9934:     return $input;
                   9935: }
1.223     albertel 9936: 
1.222     foxr     9937: #  Same as escape_single, but escape's "'s  This 
                   9938: #  can be used for  "strings"
                   9939: sub escape_double {
                   9940:     my ($input) = @_;
                   9941:     $input =~ s/\\/\\\\/g;	# Escape the /'s..(must be first)>
                   9942:     $input =~ s/\"/\\\"/g;	# Esacpe the "s....
                   9943:     return $input;
                   9944: }
1.223     albertel 9945:  
1.222     foxr     9946: #   Escapes the last element of a full URL.
                   9947: sub escape_url {
                   9948:     my ($url)   = @_;
1.238     raeburn  9949:     my @urlslices = split(/\//, $url,-1);
1.369     www      9950:     my $lastitem = &escape(pop(@urlslices));
1.223     albertel 9951:     return join('/',@urlslices).'/'.$lastitem;
1.222     foxr     9952: }
1.462     albertel 9953: 
1.692.4.2  raeburn  9954: sub compare_arrays {
                   9955:     my ($arrayref1,$arrayref2) = @_;
                   9956:     my (@difference,%count);
                   9957:     @difference = ();
                   9958:     %count = ();
                   9959:     if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) {
                   9960:         foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; }
                   9961:         foreach my $element (keys(%count)) {
                   9962:             if ($count{$element} == 1) {
                   9963:                 push(@difference,$element);
                   9964:             }
                   9965:         }
                   9966:     }
                   9967:     return @difference;
                   9968: }
                   9969: 
1.462     albertel 9970: # -------------------------------------------------------- Initliaze user login
                   9971: sub init_user_environment {
1.463     albertel 9972:     my ($r, $username, $domain, $authhost, $form, $args) = @_;
1.462     albertel 9973:     my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'};
                   9974: 
                   9975:     my $public=($username eq 'public' && $domain eq 'public');
                   9976: 
                   9977: # See if old ID present, if so, remove
                   9978: 
                   9979:     my ($filename,$cookie,$userroles);
                   9980:     my $now=time;
                   9981: 
                   9982:     if ($public) {
                   9983: 	my $max_public=100;
                   9984: 	my $oldest;
                   9985: 	my $oldest_time=0;
                   9986: 	for(my $next=1;$next<=$max_public;$next++) {
                   9987: 	    if (-e $lonids."/publicuser_$next.id") {
                   9988: 		my $mtime=(stat($lonids."/publicuser_$next.id"))[9];
                   9989: 		if ($mtime<$oldest_time || !$oldest_time) {
                   9990: 		    $oldest_time=$mtime;
                   9991: 		    $oldest=$next;
                   9992: 		}
                   9993: 	    } else {
                   9994: 		$cookie="publicuser_$next";
                   9995: 		last;
                   9996: 	    }
                   9997: 	}
                   9998: 	if (!$cookie) { $cookie="publicuser_$oldest"; }
                   9999:     } else {
1.463     albertel 10000: 	# if this isn't a robot, kill any existing non-robot sessions
                   10001: 	if (!$args->{'robot'}) {
                   10002: 	    opendir(DIR,$lonids);
                   10003: 	    while ($filename=readdir(DIR)) {
                   10004: 		if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                   10005: 		    unlink($lonids.'/'.$filename);
                   10006: 		}
1.462     albertel 10007: 	    }
1.463     albertel 10008: 	    closedir(DIR);
1.462     albertel 10009: 	}
                   10010: # Give them a new cookie
1.463     albertel 10011: 	my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'}
1.684     www      10012: 		                   : $now.$$.int(rand(10000)));
1.463     albertel 10013: 	$cookie="$username\_$id\_$domain\_$authhost";
1.462     albertel 10014:     
                   10015: # Initialize roles
                   10016: 
                   10017: 	$userroles=&Apache::lonnet::rolesinit($domain,$username,$authhost);
                   10018:     }
                   10019: # ------------------------------------ Check browser type and MathML capability
                   10020: 
                   10021:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                   10022:         $clientunicode,$clientos) = &decode_user_agent($r);
                   10023: 
                   10024: # -------------------------------------- Any accessibility options to remember?
                   10025:     if (($form->{'interface'}) && ($form->{'remember'} eq 'true')) {
                   10026: 	foreach my $option ('imagesuppress','appletsuppress',
                   10027: 			    'embedsuppress','fontenhance','blackwhite') {
                   10028: 	    if ($form->{$option} eq 'true') {
                   10029: 		&Apache::lonnet::put('environment',{$option => 'on'},
                   10030: 				     $domain,$username);
                   10031: 	    } else {
                   10032: 		&Apache::lonnet::del('environment',[$option],
                   10033: 				     $domain,$username);
                   10034: 	    }
                   10035: 	}
                   10036:     }
                   10037: # ------------------------------------------------------------- Get environment
                   10038: 
                   10039:     my %userenv = &Apache::lonnet::dump('environment',$domain,$username);
                   10040:     my ($tmp) = keys(%userenv);
                   10041:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   10042: 	# default remote control to off
                   10043: 	if ($userenv{'remote'} ne 'on') { $userenv{'remote'} = 'off'; }
                   10044:     } else {
                   10045: 	undef(%userenv);
                   10046:     }
                   10047:     if (($userenv{'interface'}) && (!$form->{'interface'})) {
                   10048: 	$form->{'interface'}=$userenv{'interface'};
                   10049:     }
                   10050:     $env{'environment.remote'}=$userenv{'remote'};
                   10051:     if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; }
                   10052: 
                   10053: # --------------- Do not trust query string to be put directly into environment
                   10054:     foreach my $option ('imagesuppress','appletsuppress',
                   10055: 			'embedsuppress','fontenhance','blackwhite',
                   10056: 			'interface','localpath','localres') {
                   10057: 	$form->{$option}=~s/[\n\r\=]//gs;
                   10058:     }
                   10059: # --------------------------------------------------------- Write first profile
                   10060: 
                   10061:     {
                   10062: 	my %initial_env = 
                   10063: 	    ("user.name"          => $username,
                   10064: 	     "user.domain"        => $domain,
                   10065: 	     "user.home"          => $authhost,
                   10066: 	     "browser.type"       => $clientbrowser,
                   10067: 	     "browser.version"    => $clientversion,
                   10068: 	     "browser.mathml"     => $clientmathml,
                   10069: 	     "browser.unicode"    => $clientunicode,
                   10070: 	     "browser.os"         => $clientos,
                   10071: 	     "server.domain"      => $Apache::lonnet::perlvar{'lonDefDomain'},
                   10072: 	     "request.course.fn"  => '',
                   10073: 	     "request.course.uri" => '',
                   10074: 	     "request.course.sec" => '',
                   10075: 	     "request.role"       => 'cm',
                   10076: 	     "request.role.adv"   => $env{'user.adv'},
                   10077: 	     "request.host"       => $ENV{'REMOTE_ADDR'},);
                   10078: 
                   10079:         if ($form->{'localpath'}) {
                   10080: 	    $initial_env{"browser.localpath"}  = $form->{'localpath'};
                   10081: 	    $initial_env{"browser.localres"}   = $form->{'localres'};
                   10082:         }
                   10083: 	
                   10084: 	if ($public) {
                   10085: 	    $initial_env{"environment.remote"} = "off";
                   10086: 	}
                   10087: 	if ($form->{'interface'}) {
                   10088: 	    $form->{'interface'}=~s/\W//gs;
                   10089: 	    $initial_env{"browser.interface"} = $form->{'interface'};
                   10090: 	    $env{'browser.interface'}=$form->{'interface'};
                   10091: 	    foreach my $option ('imagesuppress','appletsuppress',
                   10092: 				'embedsuppress','fontenhance','blackwhite') {
                   10093: 		if (($form->{$option} eq 'true') ||
                   10094: 		    ($userenv{$option} eq 'on')) {
                   10095: 		    $initial_env{"browser.$option"} = "on";
                   10096: 		}
                   10097: 	    }
                   10098: 	}
                   10099: 
1.692.4.2  raeburn  10100:         foreach my $tool ('aboutme','blog','portfolio') {
                   10101:             $userenv{'availabletools.'.$tool} =
                   10102:                 &Apache::lonnet::usertools_access($username,$domain,$tool,'reload');
                   10103:         }
                   10104: 
1.692.4.6  raeburn  10105:         foreach my $crstype ('official','unofficial','community') {
1.692.4.2  raeburn  10106:             $userenv{'canrequest.'.$crstype} =
                   10107:                 &Apache::lonnet::usertools_access($username,$domain,$crstype,
                   10108:                                                   'reload','requestcourses');
                   10109:         }
                   10110: 
1.462     albertel 10111: 	$env{'user.environment'} = "$lonids/$cookie.id";
                   10112: 	
                   10113: 	if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id",
                   10114: 		 &GDBM_WRCREAT(),0640)) {
                   10115: 	    &_add_to_env(\%disk_env,\%initial_env);
                   10116: 	    &_add_to_env(\%disk_env,\%userenv,'environment.');
                   10117: 	    &_add_to_env(\%disk_env,$userroles);
1.463     albertel 10118: 	    if (ref($args->{'extra_env'})) {
                   10119: 		&_add_to_env(\%disk_env,$args->{'extra_env'});
                   10120: 	    }
1.462     albertel 10121: 	    untie(%disk_env);
                   10122: 	} else {
                   10123: 	    &Apache::lonnet::logthis("<font color=\"blue\">WARNING: ".
                   10124: 			   'Could not create environment storage in lonauth: '.$!.'</font>');
                   10125: 	    return 'error: '.$!;
                   10126: 	}
                   10127:     }
                   10128:     $env{'request.role'}='cm';
                   10129:     $env{'request.role.adv'}=$env{'user.adv'};
                   10130:     $env{'browser.type'}=$clientbrowser;
                   10131: 
                   10132:     return $cookie;
                   10133: 
                   10134: }
                   10135: 
                   10136: sub _add_to_env {
                   10137:     my ($idf,$env_data,$prefix) = @_;
1.676     raeburn  10138:     if (ref($env_data) eq 'HASH') {
                   10139:         while (my ($key,$value) = each(%$env_data)) {
                   10140: 	    $idf->{$prefix.$key} = $value;
                   10141: 	    $env{$prefix.$key}   = $value;
                   10142:         }
1.462     albertel 10143:     }
                   10144: }
                   10145: 
1.685     tempelho 10146: # --- Get the symbolic name of a problem and the url
                   10147: sub get_symb {
                   10148:     my ($request,$silent) = @_;
1.692.4.2  raeburn  10149:     (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.685     tempelho 10150:     my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url)));
                   10151:     if ($symb eq '') {
                   10152:         if (!$silent) {
                   10153:             $request->print("Unable to handle ambiguous references:$url:.");
                   10154:             return ();
                   10155:         }
                   10156:     }
                   10157:     &Apache::lonenc::check_decrypt(\$symb);
                   10158:     return ($symb);
                   10159: }
                   10160: 
                   10161: # --------------------------------------------------------------Get annotation
                   10162: 
                   10163: sub get_annotation {
                   10164:     my ($symb,$enc) = @_;
                   10165: 
                   10166:     my $key = $symb;
                   10167:     if (!$enc) {
                   10168:         $key =
                   10169:             &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
                   10170:     }
                   10171:     my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]);
                   10172:     return $annotation{$key};
                   10173: }
                   10174: 
                   10175: sub clean_symb {
1.692.4.2  raeburn  10176:     my ($symb,$delete_enc) = @_;
1.685     tempelho 10177: 
                   10178:     &Apache::lonenc::check_decrypt(\$symb);
                   10179:     my $enc = $env{'request.enc'};
1.692.4.2  raeburn  10180:     if ($delete_enc) {
                   10181:         delete($env{'request.enc'});
                   10182:     }
1.685     tempelho 10183: 
                   10184:     return ($symb,$enc);
                   10185: }
1.462     albertel 10186: 
1.41      ng       10187: =pod
                   10188: 
                   10189: =back
                   10190: 
1.112     bowersj2 10191: =cut
1.41      ng       10192: 
1.112     bowersj2 10193: 1;
                   10194: __END__;
1.41      ng       10195: 

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