--- loncom/interface/loncommon.pm 2004/04/19 21:28:19 1.188 +++ loncom/interface/loncommon.pm 2004/10/21 09:53:44 1.221 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common routines # -# $Id: loncommon.pm,v 1.188 2004/04/19 21:28:19 matthew Exp $ +# $Id: loncommon.pm,v 1.221 2004/10/21 09:53:44 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -74,6 +74,7 @@ my $readit; my %language; my %supported_language; my %cprtag; +my %scprtag; my %fe; my %fd; my %category_extensions; @@ -131,6 +132,20 @@ BEGIN { close($fh); } } +# ------------------------------------------------------------------ source copyrights + { + my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}. + '/source_copyright.tab'; + if ( open (my $fh,"<$sourcecopyrightfile") ) { + while (<$fh>) { + next if /^\#/; + chomp; + my ($key,$val)=(split(/\s+/,$_,2)); + $scprtag{$key}=$val; + } + close($fh); + } + } # -------------------------------------------------------------- domain designs @@ -235,8 +250,11 @@ of the element the selection from the se =cut sub browser_and_searcher_javascript { + my ($mode)=@_; + if (!defined($mode)) { $mode='edit'; } my $resurl=&lastresurl(); return <<END; +// <!-- BEGIN LON-CAPA Internal var editbrowser = null; function openbrowser(formname,elementname,only,omit,titleelement) { var url = '$resurl/?'; @@ -244,20 +262,26 @@ sub browser_and_searcher_javascript { url += 'launch=1&'; } url += 'catalogmode=interactive&'; - url += 'mode=edit&'; + url += 'mode=$mode&'; url += 'form=' + formname + '&'; if (only != null) { url += 'only=' + only + '&'; - } + } else { + url += 'only=&'; + } if (omit != null) { url += 'omit=' + omit + '&'; - } + } else { + url += 'omit=&'; + } if (titleelement != null) { url += 'titleelement=' + titleelement + '&'; - } + } else { + url += 'titleelement=&'; + } url += 'element=' + elementname + ''; var title = 'Browser'; - var options = 'scrollbars=1,resizable=1,menubar=0'; + var options = 'scrollbars=1,resizable=1,menubar=1,location=1'; options += ',width=700,height=600'; editbrowser = open(url,title,options,'1'); editbrowser.focus(); @@ -269,11 +293,13 @@ sub browser_and_searcher_javascript { url += 'launch=1&'; } url += 'catalogmode=interactive&'; - url += 'mode=edit&'; + url += 'mode=$mode&'; url += 'form=' + formname + '&'; if (titleelement != null) { url += 'titleelement=' + titleelement + '&'; - } + } else { + url += 'titleelement=&'; + } url += 'element=' + elementname + ''; var title = 'Search'; var options = 'scrollbars=1,resizable=1,menubar=0'; @@ -281,6 +307,7 @@ sub browser_and_searcher_javascript { editsearcher = open(url,title,options,'1'); editsearcher.focus(); } +// END LON-CAPA Internal --> END } @@ -466,7 +493,7 @@ sub linked_select_forms { my $first = "document.$formname.$firstselectname"; # output the javascript to do the changing my $result = ''; - $result.="<script>\n"; + $result.="<script type=\"text/javascript\">\n"; $result.="var select2data = new Object();\n"; $" = '","'; my $debug = ''; @@ -594,8 +621,9 @@ sub help_open_topic { # Add the graphic my $title = &mt('Online Help'); + my $helpicon=&lonhttpdurl("/adm/help/gif/smallHelp.gif"); $template .= <<"ENDTEMPLATE"; - <a href="$link" title="$title"><image src="/adm/help/gif/smallHelp.gif" border="0" alt="(Help: $topic)" /></a> + <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help: $topic)" /></a> ENDTEMPLATE if ($text ne '') { $template.='</td></tr></table>' }; return $template; @@ -622,6 +650,77 @@ sub helpLatexCheatsheet { .'</td></tr></table>'; } +sub help_open_menu { + my ($color,$topic,$component_help,$function,$faq,$bug,$stayOnPage,$width,$height,$text) = @_; + $text = "" if (not defined $text); + $stayOnPage = 0 if (not defined $stayOnPage); + if ($ENV{'browser.interface'} eq 'textual' || + $ENV{'environment.remote'} eq 'off' ) { + $stayOnPage=1; + } + $width = 620 if (not defined $width); + $height = 600 if (not defined $height); + my $link=''; + my $title = &mt('Get help'); + my $origurl = $ENV{'REQUEST_URI'}; + my $timestamp = time; + foreach (\$color,\$function,\$topic,\$component_help,\$faq,\$bug,\$origurl) { + $$_ = &Apache::lonnet::escape($$_); + } + + if (!$stayOnPage) { + $link = "javascript:helpMenu('open')"; + } else { + $link = "javascript:helpMenu('display')"; + } + my $banner_link = "/adm/helpmenu?page=banner&color=$color&function=$function&topic=$topic&component_help=$component_help&faq=$faq&bug=$bug&origurl=$origurl&stamp=$timestamp&stayonpage=$stayOnPage"; + my $details_link = "/adm/helpmenu?page=body&color=$color&function=$function&topic=$topic&component_help=$component_help&faq=$faq&bug=$bug&origurl=$origurl&stamp=$timestamp"; + my $template; + if ($text ne "") { + $template .= + "<table bgcolor='#773311' cellspacing='1' cellpadding='1' border='0'><tr>". + "<td bgcolor='#886622'><a href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>"; + } + my $helpicon=&lonhttpdurl("/adm/lonIcons/helpgateway.gif"); + $template .= <<"ENDTEMPLATE"; + <script type="text/javascript"> +//<!-- BEGIN LON-CAPA Internal +function helpMenu(caller) { + if (caller == 'open') { + newWindow = window.open("","helpmenu","HEIGHT=$height,WIDTH=$width,resize=yes,scrollbars=yes" ) + caller = newWindow.document + } else { + caller = this.document + } + caller.write("<html><head><title>LON-CAPA Help Menu</title><meta http-equiv='pragma' content='no-cache'></head>") + caller.write("<frameset rows='105,*' border='0'><frame name='bannerframe' src='$banner_link'><frame name='bodyframe' src='$details_link'></frameset>") + caller.write("</html>") + caller.close() + if (caller == newWindow.document) { + caller.focus() + } +} +// END LON-CAPA Internal --> + </script> + <a href="$link" title="$title"><img src="$helpicon" border="0" alt="(Help Menu)" /></a> +ENDTEMPLATE + if ($component_help) { + if (!$text) { + $template=&help_open_topic($component_help,undef,$stayOnPage, + $width,$height).' '.$template; + } else { + my $help_text; + $help_text=&Apache::lonnet::unescape($topic); + $template='<table><tr><td>'. + &help_open_topic($component_help,$help_text,$stayOnPage, + $width,$height).'</td><td>'.$template. + '</td></tr></table>'; + } + } + if ($text ne '') { $template.='</td></tr></table>' }; + return $template; +} + sub help_open_bug { my ($topic, $text, $stayOnPage, $width, $height) = @_; unless ($ENV{'user.adv'}) { return ''; } @@ -658,8 +757,9 @@ sub help_open_bug { # Add the graphic my $title = &mt('Report a Bug'); + my $bugicon=&lonhttpdurl("/adm/lonMisc/smallBug.gif"); $template .= <<"ENDTEMPLATE"; - <a href="$link" title="$title"><image src="/adm/lonMisc/smallBug.gif" border="0" alt="(Bug: $topic)" /></a> + <a href="$link" title="$title"><img src="$bugicon" border="0" alt="(Bug: $topic)" /></a> ENDTEMPLATE if ($text ne '') { $template.='</td></tr></table>' }; return $template; @@ -702,8 +802,9 @@ sub help_open_faq { # Add the graphic my $title = &mt('View the FAQ'); + my $faqicon=&lonhttpdurl("/adm/lonMisc/smallFAQ.gif"); $template .= <<"ENDTEMPLATE"; - <a href="$link" title="$title"><image src="/adm/lonMisc/smallFAQ.gif" border="0" alt="(FAQ: $topic)" /></a> + <a href="$link" title="$title"><img src="$faqicon" border="0" alt="(FAQ: $topic)" /></a> ENDTEMPLATE if ($text ne '') { $template.='</td></tr></table>' }; return $template; @@ -727,7 +828,7 @@ format. sub csv_translate { my $text = shift; $text =~ s/\"/\"\"/g; - $text =~ s/\n//g; + $text =~ s/\n/ /g; return $text; } @@ -778,7 +879,7 @@ sub define_excel_formats { $format->{'h2'} = $workbook->add_format(bold=>1, size=>16); $format->{'h3'} = $workbook->add_format(bold=>1, size=>14); $format->{'date'} = $workbook->add_format(num_format=> - 'mmm d yyyy hh:mm AM/PM'); + 'mm/dd/yyyy hh:mm:ss'); return $format; } @@ -907,21 +1008,27 @@ sub domain_select { } &get_domains; if ($multiple) { $domains{''}=&mt('Any domain'); - return &multiple_select_form($name,$value,%domains); + return &multiple_select_form($name,$value,4,%domains); } else { return &select_form($name,$value,%domains); } } sub multiple_select_form { - my ($name,$value,%hash)=@_; + my ($name,$value,$size,%hash)=@_; my %selected = map { $_ => 1 } ref($value)?@{$value}:($value); my $output=''; - my $size =(scalar keys %hash<4?scalar keys %hash:4); + if (! defined($size)) { + $size = 4; + if (scalar(keys(%hash))<4) { + $size = scalar(keys(%hash)); + } + } $output.="\n<select name='$name' size='$size' multiple='1'>"; - foreach (sort keys %hash) { - $output.="<option name='$_'". - ($selected{$_}?' selected="1"' :'').">$hash{$_}</option>\n"; + foreach (sort(keys(%hash))) { + $output.='<option value="'.$_.'" '; + $output.='selected ' if ($selected{$_}); + $output.='>'.$hash{$_}."</option>\n"; } $output.="</select>\n"; return $output; @@ -1651,6 +1758,7 @@ sub plainname { $names{'lastname'}.' '.$names{'generation'}; $name=~s/\s+$//; $name=~s/\s+/ /g; + if ($name !~ /\S/) { $name=$uname.'@'.$udom; } return $name; } @@ -1673,8 +1781,19 @@ if the user does not sub nickname { my ($uname,$udom)=@_; - my %names=&Apache::lonnet::get('environment', - ['nickname','firstname','middlename','lastname','generation'],$udom,$uname); + my %names; + if ($uname eq $ENV{'user.name'} && + $udom eq $ENV{'user.domain'}) { + %names=('nickname' => $ENV{'environment.nickname'} , + 'firstname' => $ENV{'environment.firstname'} , + 'middlename' => $ENV{'environment.middlename'}, + 'lastname' => $ENV{'environment.lastname'} , + 'generation' => $ENV{'environment.generation'}); + } else { + %names=&Apache::lonnet::get('environment', + ['nickname','firstname','middlename', + 'lastname','generation'],$udom,$uname); + } my $name=$names{'nickname'}; if ($name) { $name='"'.$name.'"'; @@ -1700,17 +1819,21 @@ Gets a users screenname and returns it a sub screenname { my ($uname,$udom)=@_; - my %names= - &Apache::lonnet::get('environment',['screenname'],$udom,$uname); + if ($uname eq $ENV{'user.name'} && + $udom eq $ENV{'user.domain'}) {return $ENV{'environment.screenname'};} + my %names=&Apache::lonnet::get('environment',['screenname'],$udom,$uname); return $names{'screenname'}; } + # ------------------------------------------------------------- Message Wrapper sub messagewrapper { - my ($link,$un,$do)=@_; + my ($link,$username,$domain)=@_; return -"<a href='/adm/email?compose=individual&recname=$un&recdom=$do'>$link</a>"; + '<a href="/adm/email?compose=individual&'. + 'recname='.$username.'&recdom='.$domain.'" '. + 'title="'.&mt('Send message').'">'.$link.'</a>'; } # --------------------------------------------------------------- Notes Wrapper @@ -1723,8 +1846,8 @@ sub noteswrapper { sub aboutmewrapper { my ($link,$username,$domain,$target)=@_; - return "<a href='/adm/$domain/$username/aboutme'". - ($target?" target='$target'":'').">$link</a>"; + return '<a href="/adm/'.$domain.'/'.$username.'/aboutme"'. + ($target?' target="$target"':'').' title="'.&mt('View this users personal page').'">'.$link.'</a>'; } # ------------------------------------------------------------ Syllabus Wrapper @@ -1735,9 +1858,28 @@ sub syllabuswrapper { if ($fontcolor) { $linktext='<font color="'.$fontcolor.'">'.$linktext.'</font>'; } - return "<a href='/public/$domain/$coursedir/syllabus'>$linktext</a>"; + return qq{<a href="/public/$domain/$coursedir/syllabus">$linktext</a>}; } +sub track_student_link { + my ($linktext,$sname,$sdom,$target) = @_; + my $link ="/adm/trackstudent"; + my $title = 'View recent activity'; + if (defined($sname) && $sname !~ /^\s*$/ && + defined($sdom) && $sdom !~ /^\s*$/) { + $link .= "?selected_student=$sname:$sdom"; + $title .= ' of this student'; + } + if (defined($target) && $target !~ /^\s*$/) { + $target = qq{target="$target"}; + } else { + $target = ''; + } + return qq{<a href="$link" title="$title" $target>$linktext</a>}; +} + + + =pod =back @@ -1807,6 +1949,30 @@ sub copyrightdescription { =pod +=item * source_copyrightids() + +returns list of all source copyrights + +=cut + +sub source_copyrightids { + return sort(keys(%scprtag)); +} + +=pod + +=item * source_copyrightdescription() + +returns description of a specified source copyright id + +=cut + +sub source_copyrightdescription { + return &mt($scprtag{shift(@_)}); +} + +=pod + =item * filecategories() returns list of all file categories @@ -1845,7 +2011,7 @@ sub fileembstyle { sub filecategoryselect { my ($name,$value)=@_; - return &select_form($name,$value, + return &select_form($value,$name, '' => &mt('Any category'), map { $_,$_ } sort(keys(%category_extensions))); } @@ -2194,7 +2360,7 @@ sub maketime { my %th=@_; return POSIX::mktime( ($th{'seconds'},$th{'minutes'},$th{'hours'}, - $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,$th{'dlsav'})); + $th{'day'},$th{'month'}-1,$th{'year'}-1900,0,0,-1)); } ######################################### @@ -2265,10 +2431,8 @@ sub domainlogo { my $domain = &determinedomain(shift); # See if there is a logo if (-e '/home/httpd/html/adm/lonDomLogos/'.$domain.'.gif') { - my $lonhttpdPort=$Apache::lonnet::perlvar{'lonhttpdPort'}; - if (!defined($lonhttpdPort)) { $lonhttpdPort='8080'; } - return '<img src="http://'.$ENV{'HTTP_HOST'}.':'.$lonhttpdPort. - '/adm/lonDomLogos/'.$domain.'.gif" alt="'.$domain.'" />'; + my $logo=&lonhttpdurl("/adm/lonDomLogos/$domain.gif"); + return '<img src="'.$logo.'" alt="'.$domain.'" />'; } elsif(exists($Apache::lonnet::domaindescription{$domain})) { return $Apache::lonnet::domaindescription{$domain}; } else { @@ -2405,10 +2569,26 @@ END '<h1>LON-CAPA: '.$title.'</h1>'; } elsif ($ENV{'environment.remote'} eq 'off') { # No Remote + my $roleinfo=(<<ENDROLE); +<td bgcolor="$tabbg" align="right"> +<p> +<font size="2" face="Arial, Helvetica, sans-serif"> + $ENV{'environment.firstname'} + $ENV{'environment.middlename'} + $ENV{'environment.lastname'} + $ENV{'environment.generation'} + </font> +<br /> +<font size="2" face="Arial, Helvetica, sans-serif">$role</font> +<br /> +<font size="2" face="Arial, Helvetica, sans-serif">$realm</font> +</p> +</td> +ENDROLE return $bodytag.&Apache::lonmenu::menubuttons($forcereg,'web', $forcereg). - '<table bgcolor="'.$pgbg.'" width="100%" border="0" cellspacing="3" cellpadding="3"><tr><td bgcolor="'.$tabbg.'"><font face="Arial, Helvetica, sans-serif" size="+3" color="'.$font.'"><b>'.$title. -'</b></font></td></tr></table>'; + '<table bgcolor="'.$pgbg.'" width="100%" border="0" cellspacing="3" cellpadding="3"><tr><td rowspan="3" bgcolor="'.$tabbg.'"><font face="Arial, Helvetica, sans-serif" size="+3" color="'.$font.'"><b>'.$title. +'</b></font></td>'.$roleinfo.'</tr></table>'; } # @@ -2438,7 +2618,7 @@ $upperleft</td> </td></tr> <tr> <td bgcolor="$tabbg" align="right"><font size="2" face="Arial, Helvetica, sans-serif">$realm</font> </td></tr> -</table><br> +</table><br /> ENDBODY } @@ -2579,12 +2759,12 @@ returns cache-controlling header code =cut sub cacheheader { - unless ($ENV{'request.method'} eq 'GET') { return ''; } - my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime); - my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" /> + unless ($ENV{'request.method'} eq 'GET') { return ''; } + my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime); + my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" /> <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" /> <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />'; - return $output; + return $output; } =pod @@ -2596,12 +2776,13 @@ specifies header code to not have cache =cut sub no_cache { - my ($r) = @_; - unless ($ENV{'request.method'} eq 'GET') { return ''; } - #my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime); - $r->no_cache(1); - $r->header_out("Pragma" => "no-cache"); - #$r->header_out("Expires" => $date); + my ($r) = @_; + if ($ENV{'REQUEST_METHOD'} ne 'GET' && + $ENV{'request.method'} ne 'GET') { return ''; } + my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime(time)); + $r->no_cache(1); + $r->header_out("Expires" => $date); + $r->header_out("Pragma" => "no-cache"); } sub content_type { @@ -3112,7 +3293,11 @@ sub DrawBarGraph { } # my ($height,$width,$xskip,$bar_width) = (200,120,1,15); - if ($NumBars < 10) { + if ($NumBars < 5) { + $width = 120+$NumBars*25; + $xskip = 1; + $bar_width = 25; + } elsif ($NumBars < 10) { $width = 120+$NumBars*15; $xskip = 1; $bar_width = 15; @@ -3395,8 +3580,8 @@ Inputs: sub chartlink { my ($linktext, $sname, $sdomain) = @_; my $link = '<a href="/adm/statistics?reportSelected=student_assessment'. - '&SelectedStudent='.&Apache::lonnet::escape($sname.':'.$sdomain). - '&chartoutputmode='.HTML::Entities::encode('html, with all links'). + '&SelectedStudent='.&Apache::lonnet::escape($sname.':'.$sdomain). + '&chartoutputmode='.HTML::Entities::encode('html, with all links'). '">'.$linktext.'</a>'; } @@ -3533,6 +3718,45 @@ sub icon { return $iconname; } +sub lonhttpdurl { + my ($url)=@_; + my $lonhttpd_port=$Apache::lonnet::perlvar{'lonhttpdPort'}; + if (!defined($lonhttpd_port)) { $lonhttpd_port='8080'; } + return 'http://'.$ENV{'SERVER_NAME'}.':'.$lonhttpd_port.$url; +} + +sub connection_aborted { + my ($r)=@_; + $r->print(" ");$r->rflush(); + my $c = $r->connection; + return $c->aborted(); +} + +# +# Escapes strings that may have embedded 's that will be put into +# javascript strings as 'strings'. +# The assumptions are: +# There has been no effort to escape ' with \' +# Any \'s in the string are intended to be there as part of the URL +# and must also be escaped. +# Parameters: +# input - The string to escape. +# Returns: +# The escaped string (' replaced by \' and \ replaced by \\). +# +sub javascript_escape { + my ($input) = @_; + + # I imagine a regexp wizard could combine the two expressions below. + # If you do you might want to comment the result. + + $input =~ s/\\/\\\\/g; # Escape the /'s..(must be first)> + $input =~ s/\'/\\\'/g; # Esacpe the 's.... + + return $input; +} + + =pod =back