--- loncom/interface/loncommon.pm 2008/05/29 19:35:53 1.657 +++ loncom/interface/loncommon.pm 2009/07/01 13:24:29 1.848 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common routines # -# $Id: loncommon.pm,v 1.657 2008/05/29 19:35:53 raeburn Exp $ +# $Id: loncommon.pm,v 1.848 2009/07/01 13:24:29 tempelho Exp $ # # Copyright Michigan State University Board of Trustees # @@ -61,6 +61,7 @@ use POSIX qw(strftime mktime); use Apache::lonmenu(); use Apache::lonenc(); use Apache::lonlocal; +use Apache::lonnet(); use HTML::Entities; use Apache::lonhtmlcommon(); use Apache::loncoursedata(); @@ -68,6 +69,7 @@ use Apache::lontexconvert(); use Apache::lonclonecourse(); use LONCAPA qw(:DEFAULT :match); use DateTime::TimeZone; +use DateTime::Locale::Catalog; # ---------------------------------------------- Designs use vars qw(%defaultdesign); @@ -151,7 +153,6 @@ sub ssi_with_retries { # ----------------------------------------------- Filetypes/Languages/Copyright my %language; -my %timezone; my %supported_language; my %cprtag; my %scprtag; @@ -194,22 +195,6 @@ BEGIN { close($fh); } } -# ------------------------------------------------------------------- timezones - { - my $timetabfile = $Apache::lonnet::perlvar{'lonTabDir'}. - '/timezone.tab'; - if ( open(my $fh,"<$timetabfile") ) { - while (my $line = <$fh>) { - next if ($line=~/^\#/); - chomp($line); - my $value=$line; - $value=~s/\_/ /g; - $timezone{$line}=$value; - } - close($fh); - } - } - # ------------------------------------------------------------------ copyrights { my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}. @@ -286,7 +271,7 @@ BEGIN { } } &Apache::lonnet::logthis( - "INFO: Read file types"); + "INFO: Read file types"); $readit=1; } # end of unless($readit) @@ -421,9 +406,10 @@ sub studentbrowser_javascript { || ($env{'request.role'}=~/^(au|dc|su)/) ) { return ''; } return (<<'ENDSTDBRW'); - ENDSTDBRW } sub selectstudent_link { - my ($form,$unameele,$udomele)=@_; + my ($form,$unameele,$udomele,$courseadvonly)=@_; + my $callargs = "'".$form."','".$unameele."','".$udomele."'"; if ($env{'request.course.id'}) { if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'}) && !&Apache::lonnet::allowed('srm',$env{'request.course.id'}. '/'.$env{'request.course.sec'})) { return ''; } - return "".&mt('Select User').""; + if ($courseadvonly) { + $callargs .= ",'',1,1"; + } + return ''. + ''. + &mt('Select User').''; } if ($env{'request.role'}=~/^(au|dc|su)/) { - return "".&mt('Select User').""; + $callargs .= ",1"; + return ''. + ''. + &mt('Select User').''; } return ''; } sub authorbrowser_javascript { return <<"ENDAUTHORBRW"; - ENDAUTHORBRW } @@ -488,7 +485,8 @@ sub coursebrowser_javascript { my ($domainfilter,$sec_element,$formname)=@_; my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Group - for which you wish to add/modify a user role'); my $output = ' -'; return $output; } @@ -617,8 +620,13 @@ function setSect(sectionlist) { sub selectcourse_link { my ($form,$unameele,$udomele,$desc,$extra_element,$multflag,$selecttype)=@_; - return "".&mt('Select Course').""; + return '' + ."".&mt('Select Course').'' + .''; } sub selectauthor_link { @@ -653,8 +661,15 @@ ENDSCRT } sub select_timezone { - my ($name,$selected,$onchange)=@_; - my $output="'."\n"; + if ($includeempty) { + $output .= ''); } $r->print(''); - foreach my $line (0..2) { + foreach my $line (0..($max_samples-1)) { if (defined($samples->[$line]{$key})) { $r->print($samples->[$line]{$key}."
\n"); } @@ -7976,9 +9233,10 @@ sub restore_settings { =item * &build_recipient_list() -Build recipient lists for three types of e-mail: -(a) Error Reports, (b) Package Updates, (c) Help requests, generated by -lonerrorhandler.pm, CHECKRPMS and lonsupportreq.pm respectively. +Build recipient lists for four types of e-mail: +(a) Error Reports, (b) Package Updates, (c) lonstatus warnings/errors +(d) Help requests, generated by +lonerrorhandler.pm, CHECKRPMS, loncron, and lonsupportreq.pm respectively. Inputs: defmail (scalar - email address of default recipient), @@ -8002,23 +9260,29 @@ sub build_recipient_list { my %domconfig = &Apache::lonnet::get_dom('configuration',['contacts'],$defdom); if (ref($domconfig{'contacts'}) eq 'HASH') { - if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') { - my @contacts = ('adminemail','supportemail'); - foreach my $item (@contacts) { - if ($domconfig{'contacts'}{$mailing}{$item}) { - my $addr = $domconfig{'contacts'}{$item}; - if (!grep(/^\Q$addr\E$/,@recipients)) { - push(@recipients,$addr); + if (exists($domconfig{'contacts'}{$mailing})) { + if (ref($domconfig{'contacts'}{$mailing}) eq 'HASH') { + my @contacts = ('adminemail','supportemail'); + foreach my $item (@contacts) { + if ($domconfig{'contacts'}{$mailing}{$item}) { + my $addr = $domconfig{'contacts'}{$item}; + if (!grep(/^\Q$addr\E$/,@recipients)) { + push(@recipients,$addr); + } } + $otheremails = $domconfig{'contacts'}{$mailing}{'others'}; } - $otheremails = $domconfig{'contacts'}{$mailing}{'others'}; } + } elsif ($origmail ne '') { + push(@recipients,$origmail); } } elsif ($origmail ne '') { push(@recipients,$origmail); } - if ($defmail ne '') { - push(@recipients,$defmail); + if (defined($defmail)) { + if ($defmail ne '') { + push(@recipients,$defmail); + } } if ($otheremails) { my @others; @@ -8054,11 +9318,15 @@ domain - to an array. Also generates ja generate Domain Coordinator interface for editing Course Categories. Inputs: + categories (reference to hash of category definitions). + cats (reference to array of arrays/hashes which encapsulates hierarchy of categories and subcategories). + idx (reference to hash of counters used in Domain Coordinator interface for editing Course Categories). + jsarray (reference to array of categories used to create Javascript arrays for Domain Coordinator interface for editing Course Categories). @@ -8101,17 +9369,26 @@ sub gather_categories { Used to generate breadcrumb trails for course categories. Inputs: + categories (reference to hash of category definitions). + cats (reference to array of arrays/hashes which encapsulates hierarchy of categories and subcategories). + trails (reference to array of breacrumb trails for each category). + allitems (reference to hash - key is category key (format: escaped(name):escaped(parent category):depth in hierarchy). + idx (reference to hash of counters used in Domain Coordinator interface for editing Course Categories). + jsarray (reference to array of categories used to create Javascript arrays for Domain Coordinator interface for editing Course Categories). +subcats (reference to hash of arrays containing all subcategories within each + category, -recursive) + Returns: nothing Side effects: populates trails and allitems hash references. @@ -8119,7 +9396,7 @@ Side effects: populates trails and allit =cut sub extract_categories { - my ($categories,$cats,$trails,$allitems,$idx,$jsarray) = @_; + my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_; if (ref($categories) eq 'HASH') { &gather_categories($categories,$cats,$idx,$jsarray); if (ref($cats->[0]) eq 'ARRAY') { @@ -8140,7 +9417,14 @@ sub extract_categories { if (ref($cats->[1]{$name}) eq 'ARRAY') { for (my $j=0; $j<@{$cats->[1]{$name}}; $j++) { my $category = $cats->[1]{$name}[$j]; - &recurse_categories($cats,2,$category,$trails,$allitems,\@parents); + if (ref($subcats) eq 'HASH') { + push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1'); + } + &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats); + } + } else { + if (ref($subcats) eq 'HASH') { + $subcats->{$item} = []; } } } @@ -8156,13 +9440,19 @@ sub extract_categories { Recursively used to generate breadcrumb trails for course categories. Inputs: + cats (reference to array of arrays/hashes which encapsulates hierarchy of categories and subcategories). + depth (current depth in hierarchy of categories and sub-categories - 0 indexed). -category (current course category, for which breadcrumb trail is being generated). -trails (reference to array of breacrumb trails for each category). + +category (current course category, for which breadcrumb trail is being generated). + +trails (reference to array of breadcrumb trails for each category). + allitems (reference to hash - key is category key (format: escaped(name):escaped(parent category):depth in hierarchy). + parents (array containing containers directories for current category, back to top level). @@ -8170,12 +9460,10 @@ Returns: nothing Side effects: populates trails and allitems hash references -=back - =cut sub recurse_categories { - my ($cats,$depth,$category,$trails,$allitems,$parents) = @_; + my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_; my $shallower = $depth - 1; if (ref($cats->[$depth]{$category}) eq 'ARRAY') { for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) { @@ -8188,7 +9476,21 @@ sub recurse_categories { } my $deeper = $depth+1; push(@{$parents},$category); - &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents); + if (ref($subcats) eq 'HASH') { + my $subcat = &escape($name).':'.$category.':'.$depth; + for (my $j=@{$parents}; $j>=0; $j--) { + my $higher; + if ($j > 0) { + $higher = &escape($parents->[$j]).':'. + &escape($parents->[$j-1]).':'.$j; + } else { + $higher = &escape($parents->[$j]).'::'.$j; + } + push(@{$subcats->{$higher}},$subcat); + } + } + &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents, + $subcats); pop(@{$parents}); } } else { @@ -8202,17 +9504,147 @@ sub recurse_categories { return; } +=pod + +=item *&assign_categories_table() + +Create a datatable for display of hierarchical categories in a domain, +with checkboxes to allow a course to be categorized. + +Inputs: + +cathash - reference to hash of categories defined for the domain (from + configuration.db) + +currcat - scalar with an & separated list of categories assigned to a course. + +Returns: $output (markup to be displayed) + +=cut + +sub assign_categories_table { + my ($cathash,$currcat) = @_; + my $output; + if (ref($cathash) eq 'HASH') { + my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth); + &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray); + $maxdepth = scalar(@cats); + if (@cats > 0) { + my $itemcount = 0; + if (ref($cats[0]) eq 'ARRAY') { + $output = &Apache::loncommon::start_data_table(); + my @currcategories; + if ($currcat ne '') { + @currcategories = split('&',$currcat); + } + for (my $i=0; $i<@{$cats[0]}; $i++) { + my $parent = $cats[0][$i]; + my $css_class = $itemcount%2?' class="LC_odd_row"':''; + next if ($parent eq 'instcode'); + my $item = &escape($parent).'::0'; + my $checked = ''; + if (@currcategories > 0) { + if (grep(/^\Q$item\E$/,@currcategories)) { + $checked = ' checked="checked"'; + } + } + $output .= ''. + ''.$parent.''. + ''; + my $depth = 1; + push(@path,$parent); + $output .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories); + pop(@path); + $output .= ''; + $itemcount ++; + } + $output .= &Apache::loncommon::end_data_table(); + } + } + } + return $output; +} + +=pod + +=item *&assign_category_rows() + +Create a datatable row for display of nested categories in a domain, +with checkboxes to allow a course to be categorized,called recursively. + +Inputs: + +itemcount - track row number for alternating colors + +cats - reference to array of arrays/hashes which encapsulates hierarchy of + categories and subcategories. + +depth - current depth in hierarchy of categories and sub-categories - 0 indexed. + +parent - parent of current category item + +path - Array containing all categories back up through the hierarchy from the + current category to the top level. + +currcategories - reference to array of current categories assigned to the course + +Returns: $output (markup to be displayed). + +=cut + +sub assign_category_rows { + my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_; + my ($text,$name,$item,$chgstr); + if (ref($cats) eq 'ARRAY') { + my $maxdepth = scalar(@{$cats}); + if (ref($cats->[$depth]) eq 'HASH') { + if (ref($cats->[$depth]{$parent}) eq 'ARRAY') { + my $numchildren = @{$cats->[$depth]{$parent}}; + my $css_class = $itemcount%2?' class="LC_odd_row"':''; + $text .= ''; + for (my $j=0; $j<$numchildren; $j++) { + $name = $cats->[$depth]{$parent}[$j]; + $item = &escape($name).':'.&escape($parent).':'.$depth; + my $deeper = $depth+1; + my $checked = ''; + if (ref($currcategories) eq 'ARRAY') { + if (@{$currcategories} > 0) { + if (grep(/^\Q$item\E$/,@{$currcategories})) { + $checked = ' checked="checked"'; + } + } + } + $text .= ''; + } + $text .= '
'. + ''. + ''; + if (ref($path) eq 'ARRAY') { + push(@{$path},$name); + $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories); + pop(@{$path}); + } + $text .= '
'; + } + } + } + return $text; +} + ############################################################ ############################################################ sub commit_customrole { - my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_; + my ($udom,$uname,$url,$three,$four,$five,$start,$end,$context) = @_; my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.':'.$three.' in '.$url. ($start?', '.&mt('starting').' '.localtime($start):''). ($end?', ending '.localtime($end):'').': '. &Apache::lonnet::assigncustomrole( - $udom,$uname,$url,$three,$four,$five,$end,$start). + $udom,$uname,$url,$three,$four,$five,$end,$start,undef,undef,$context). '
'; return $output; } @@ -8486,7 +9918,9 @@ sub construct_course { 'policy.email', 'comment.email', 'pch.users.denied', - 'plc.users.denied'], + 'plc.users.denied', + 'hidefromcat', + 'categories'], $$crsudom,$$crsunum); } @@ -8714,10 +10148,10 @@ sub construct_course { $outcome .= ($fatal?$errtext:'read ok').' - '; my $title; my $url; if ($args->{'firstres'} eq 'syl') { - $title='Syllabus'; + $title=&mt('Syllabus'); $url='/public/'.$$crsudom.'/'.$$crsunum.'/syllabus'; } else { - $title='Navigate Contents'; + $title=&mt('Navigate Contents'); $url='/adm/navmaps'; } @@ -8771,28 +10205,14 @@ sub icon { return &lonhttpdurl($iconname); } -sub lonhttpd_port { - my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'}; - if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; } - # IE doesn't like a secure page getting images from a non-secure - # port (when logging we haven't parsed the browser type so default - # back to secure - if ((!exists($env{'browser.type'}) || $env{'browser.type'} eq 'explorer') - && $ENV{'SERVER_PORT'} == 443) { - return 443; - } - return $lonhttpd_port; - -} - sub lonhttpdurl { +# +# Had been used for "small fry" static images on separate port 8080. +# Modify here if lightweight http functionality desired again. +# Currently eliminated due to increasing firewall issues. +# my ($url)=@_; - - my $lonhttpd_port = &lonhttpd_port(); - if ($lonhttpd_port == 443) { - return 'https://'.$ENV{'SERVER_NAME'}.$url; - } - return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url; + return $url; } sub connection_aborted { @@ -8828,7 +10248,23 @@ sub escape_url { return join('/',@urlslices).'/'.$lastitem; } -# -------------------------------------------------------- Initliaze user login +sub compare_arrays { + my ($arrayref1,$arrayref2) = @_; + my (@difference,%count); + @difference = (); + %count = (); + if ((ref($arrayref1) eq 'ARRAY') && (ref($arrayref2) eq 'ARRAY')) { + foreach my $element (@{$arrayref1}, @{$arrayref2}) { $count{$element}++; } + foreach my $element (keys(%count)) { + if ($count{$element} == 1) { + push(@difference,$element); + } + } + } + return @difference; +} + +# -------------------------------------------------------- Initialize user login sub init_user_environment { my ($r, $username, $domain, $authhost, $form, $args) = @_; my $lonids=$Apache::lonnet::perlvar{'lonIDsDir'}; @@ -8870,7 +10306,7 @@ sub init_user_environment { } # Give them a new cookie my $id = ($args->{'robot'} ? 'robot'.$args->{'robot'} - : $now); + : $now.$$.int(rand(10000))); $cookie="$username\_$id\_$domain\_$authhost"; # Initialize roles @@ -8882,19 +10318,6 @@ sub init_user_environment { my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml, $clientunicode,$clientos) = &decode_user_agent($r); -# -------------------------------------- Any accessibility options to remember? - if (($form->{'interface'}) && ($form->{'remember'} eq 'true')) { - foreach my $option ('imagesuppress','appletsuppress', - 'embedsuppress','fontenhance','blackwhite') { - if ($form->{$option} eq 'true') { - &Apache::lonnet::put('environment',{$option => 'on'}, - $domain,$username); - } else { - &Apache::lonnet::del('environment',[$option], - $domain,$username); - } - } - } # ------------------------------------------------------------- Get environment my %userenv = &Apache::lonnet::dump('environment',$domain,$username); @@ -8912,10 +10335,8 @@ sub init_user_environment { if ($userenv{'texengine'} eq 'ttm') { $clientmathml=1; } # --------------- Do not trust query string to be put directly into environment - foreach my $option ('imagesuppress','appletsuppress', - 'embedsuppress','fontenhance','blackwhite', - 'interface','localpath','localres') { - $form->{$option}=~s/[\n\r\=]//gs; + foreach my $option ('interface','localpath','localres') { + $form->{$option}=~s/[\n\r\=]//gs; } # --------------------------------------------------------- Write first profile @@ -8949,15 +10370,19 @@ sub init_user_environment { $form->{'interface'}=~s/\W//gs; $initial_env{"browser.interface"} = $form->{'interface'}; $env{'browser.interface'}=$form->{'interface'}; - foreach my $option ('imagesuppress','appletsuppress', - 'embedsuppress','fontenhance','blackwhite') { - if (($form->{$option} eq 'true') || - ($userenv{$option} eq 'on')) { - $initial_env{"browser.$option"} = "on"; - } - } } + foreach my $tool ('aboutme','blog','portfolio') { + $userenv{'availabletools.'.$tool} = + &Apache::lonnet::usertools_access($username,$domain,$tool,'reload'); + } + + foreach my $crstype ('official','unofficial') { + $userenv{'canrequest.'.$crstype} = + &Apache::lonnet::usertools_access($username,$domain,$crstype, + 'reload','requestcourses'); + } + $env{'user.environment'} = "$lonids/$cookie.id"; if (tie(my %disk_env,'GDBM_File',"$lonids/$cookie.id", @@ -8970,8 +10395,8 @@ sub init_user_environment { } untie(%disk_env); } else { - &Apache::lonnet::logthis("WARNING: ". - 'Could not create environment storage in lonauth: '.$!.''); + &Apache::lonnet::logthis("WARNING: ". + 'Could not create environment storage in lonauth: '.$!.''); return 'error: '.$!; } } @@ -8985,12 +10410,54 @@ sub init_user_environment { sub _add_to_env { my ($idf,$env_data,$prefix) = @_; - while (my ($key,$value) = each(%$env_data)) { - $idf->{$prefix.$key} = $value; - $env{$prefix.$key} = $value; + if (ref($env_data) eq 'HASH') { + while (my ($key,$value) = each(%$env_data)) { + $idf->{$prefix.$key} = $value; + $env{$prefix.$key} = $value; + } + } +} + +# --- Get the symbolic name of a problem and the url +sub get_symb { + my ($request,$silent) = @_; + (my $url=$env{'form.url'}) =~ s-^https?\://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--; + my $symb=($env{'form.symb'} ne '' ? $env{'form.symb'} : (&Apache::lonnet::symbread($url))); + if ($symb eq '') { + if (!$silent) { + $request->print("Unable to handle ambiguous references:$url:."); + return (); + } + } + &Apache::lonenc::check_decrypt(\$symb); + return ($symb); +} + +# --------------------------------------------------------------Get annotation + +sub get_annotation { + my ($symb,$enc) = @_; + + my $key = $symb; + if (!$enc) { + $key = + &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]); } + my %annotation=&Apache::lonnet::get('nohist_annotations',[$key]); + return $annotation{$key}; } +sub clean_symb { + my ($symb,$delete_enc) = @_; + + &Apache::lonenc::check_decrypt(\$symb); + my $enc = $env{'request.enc'}; + if ($delete_enc) { + delete($env{'request.enc'}); + } + + return ($symb,$enc); +} =pod