File:  [LON-CAPA] / loncom / interface / lonchatfetch.pm
Revision 1.27: download - view: text, annotated - select for diffs
Tue Jun 27 00:12:23 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- update comment

    1: # The LearningOnline Network
    2: # Chat Fetching
    3: #
    4: # $Id: lonchatfetch.pm,v 1.27 2006/06/27 00:12:23 albertel Exp $
    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: #
   28: 
   29: package Apache::lonchatfetch;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use Apache::lontexconvert;
   34: use Apache::loncommon;
   35: use Apache::lonnet;
   36: use Apache::longroup;
   37: use lib '/home/httpd/lib/perl/';
   38: use LONCAPA;
   39:  
   40: 
   41: sub handler {
   42:     my $r = shift;
   43: 
   44:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   45: 					    ['lastid','group']);
   46:     my ($group,$grouptitle);
   47:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
   48:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
   49:     if (defined($env{'form.group'})) {
   50:         $group = $env{'form.group'};
   51:         if (! &Apache::lonnet::allowed('pgc',$env{'request.course.id'}.'/'.
   52: 				       $group) ) {
   53:             return HTTP_NOT_ACCEPTABLE;
   54:         }
   55:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
   56:         if (%curr_groups) {
   57:             my %group_info = 
   58: 		&Apache::longroup::get_group_settings($curr_groups{$group});
   59:             $grouptitle = 
   60: 		'<b>'.&unescape($group_info{description}).
   61: 		'</b><br />';
   62:         }
   63:     } elsif (! &Apache::lonnet::allowed('pch',$env{'request.course.id'}.
   64:              ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
   65:         ) {
   66:         return HTTP_NOT_ACCEPTABLE;
   67:     }
   68: 
   69:     my $loaderror=&Apache::lonnet::overloaderror($r);
   70:     if ($loaderror) { return $loaderror; }
   71:     $loaderror=
   72:        &Apache::lonnet::overloaderror($r,
   73:          $env{'course.'.$env{'request.course.id'}.'.home'});
   74:     if ($loaderror) { return $loaderror; }
   75: 
   76:     &Apache::loncommon::content_type($r,'text/html');
   77:     $r->send_http_header;
   78:     return OK if $r->header_only;
   79: 
   80: # ------------------------------------------------------------ retrieve entries
   81: 
   82:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
   83: 
   84:     my @entries=split(/\:/,
   85:        &Apache::lonnet::reply(
   86:         "chatretr:$cdom:$cnum:$env{'user.domain'}:$env{'user.name'}:$group",
   87:         $chome));
   88: # Figure out what the last valid entry-id is
   89:     my ($lastid,$thentime,$idnum);
   90:     foreach (@entries) {
   91: 	$_=~/^(\w+)/;
   92:         if ($1 ne 'active_participant') {
   93: 	    $lastid=$1;
   94:             ($thentime,$idnum)=split(/\_/,$lastid);
   95: 	}
   96:     }
   97: # ----------------------------------------------------------- Can see identity?
   98:     my $seeid = &get_seeid_status();
   99: # -------------------------------------------------------- see which ones apply
  100:     my $include=0;
  101:     my $newstuff='';
  102:     my $bottomid='';
  103:     unless ($env{'form.lastid'}) { 
  104: 	$include=1; 
  105: 	$newstuff .=
  106: 	    &Apache::loncommon::start_page(undef,undef,
  107: 					   {'only_body' => 1,
  108: 					    'bgcolor'   => '#FFFFFF',
  109: 					    'js_ready'  => 1,});
  110: 	# removing the CSS reference for now
  111: 	# see BUG# 4839
  112:         # to fix, need to either write the whole webpage to the bottom frame
  113:         # everytime or store the page in a js variable somewhere.
  114: 	# although in tryng both of these ideas, the .scroll to 
  115: 	# the bottom seems to fail.
  116: 	$newstuff =~ s|(<link.*?/>)||;
  117:     }
  118:     my @participants=();
  119:     foreach (@entries) {
  120: 	my ($id,$msg,$udom)=split(/\:/,&unescape($_));
  121:  	if ($id eq 'active_participant') {
  122:            chomp($udom);
  123: 	   my $participant= &Apache::loncommon::nickname($msg,$udom);
  124: 	   unless ($participant=~/\w/) { $participant=$msg.'@'.$udom; }
  125: 	   $participants[$#participants+1]=$participant;
  126: 	} elsif ($include) {
  127: 	    chomp($msg);
  128: 	    my ($msgtime,$msgnum)=split(/\_/,$id);
  129: 	    my ($sdom,$snum,$anon,$contrib)=split(/\:/,
  130: 					     &unescape($msg));
  131: 	    $contrib=&unescape($contrib);
  132: 	    &Apache::lonfeedback::newline_to_br(\$contrib);
  133: 	    ($contrib,my $errors)=&Apache::lontexconvert::msgtexconverted($contrib);
  134: 	    if ($errors) { $contrib.="[Message not fully displayed due to incorrect embedded TeX]"; }
  135: 	    if ($errors && $snum eq $env{'user.name'} &&
  136: 		$sdom eq $env{'user.domain'} ) {
  137: 		$contrib.="<br />[TeX error message: $errors]";
  138: 	    }
  139: 	    $contrib=~s/\n/ /g;
  140: 	    $contrib=~s/\'/\&\#39\;/g;
  141: 	    my $sender='';
  142: 	    if ($seeid) {
  143: 		$sender=&Apache::loncommon::plainname($snum,$sdom);
  144: 		my $nick=&Apache::loncommon::nickname($snum,$sdom);
  145: 		if (($nick) && ($nick ne $sender)) {
  146: 		    $sender.=' '.$nick;
  147: 		}
  148: 		unless ($sender) { $sender=$snum.'@'.$sdom; }
  149: 		if ($anon) { $sender.=' [Anon]' };
  150: 	    } elsif (!$anon) {
  151: 		$sender=&Apache::loncommon::nickname($snum,$sdom);
  152: 		unless ($sender) { $sender=$snum.'@'.$sdom; }
  153: 	    } else {
  154: 		$sender=&Apache::loncommon::screenname($snum,$sdom);
  155: 		unless ($sender) { $sender="Anonymous"; }
  156: 	    }
  157: 	    $sender=~s/\'/\&\#39\;/g;
  158: 	    my $color=$sender;
  159: 	    $color=~tr/a-j/0-9/;
  160: 	    $color=~tr/A-J/0-9/;
  161: 	    $color=~tr/k-t/0-9/;
  162: 	    $color=~tr/K-T/0-9/;
  163: 	    $color=~tr/u-z/0-5/;
  164: 	    $color=~tr/U-Z/0-5/;
  165: 	    $color=~s/\D//g;
  166: 	    $color=substr($color,0,6);
  167: 	    my $timestamp=localtime($msgtime);
  168: 	    my ($mhour,$mmin,$msec)=($timestamp=~/(\d\d)\:(\d\d)\:(\d\d)/);
  169: 	    $newstuff.='<font color="#'.$color.'"><a name="LC_'.$id.'"></a><b>'.
  170: 		$sender.'</b> ('.$mhour.':'.$mmin.':'.$msec.'): '.
  171: 		$contrib."</font><br />";
  172: 	    $bottomid='LC_'.$id;
  173: 	} else {
  174: 	    $_=~/^(\w+)/;
  175: 	    if ($1 eq $env{'form.lastid'}) { $include=1; }
  176: 	}
  177:     }
  178:     my $participant_output=join('<br />',sort @participants);
  179:     my $refresh_cmd = "/adm/chatfetch?lastid=$lastid";
  180:     if (defined($group)) {
  181:         $refresh_cmd .= "&amp;group=$group";
  182:     }
  183:     my $start_page = 
  184: 	&Apache::loncommon::start_page('Chat',undef,
  185: 				       {'redirect'  => [5,$refresh_cmd],
  186: 					'only_body' => 1,});
  187:     my $end_page = &Apache::loncommon::end_page();
  188:     $r->print(<<ENDDOCUMENT);
  189: $start_page
  190: <script type="text/javascript">
  191: parent.chatout.document.writeln('$newstuff');
  192: parent.chatout.scroll(0,10000000);
  193: </script>
  194: $grouptitle
  195: $participant_output
  196: $end_page
  197: ENDDOCUMENT
  198:     return OK;
  199: }
  200: 
  201: sub get_seeid_status {
  202:     my $crs='/'.$env{'request.course.id'};
  203:     my $seeid;
  204:     if (exists($env{'form.group'})) {
  205:         $seeid = &Apache::lonnet::allowed('rci',$crs.'/'.$env{'form.group'});
  206:     } else {
  207:         if ($env{'request.course.sec'}) {
  208:             $crs.='_'.$env{'request.course.sec'};
  209:         }
  210:         $crs=~s/\_/\//g;
  211:         $seeid=&Apache::lonnet::allowed('rin',$crs);
  212:     }
  213:     return $seeid;
  214: }
  215: 
  216: 1;
  217: __END__

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