File:  [LON-CAPA] / loncom / interface / lonchatfetch.pm
Revision 1.31: download - view: text, annotated - select for diffs
Mon Jul 17 16:10:57 2006 UTC (17 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, HEAD
Group privs allow access if role includes section specification.

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

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