File:  [LON-CAPA] / loncom / interface / lonchatfetch.pm
Revision 1.22: download - view: text, annotated - select for diffs
Wed Mar 29 21:16:18 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- style police

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

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