File:  [LON-CAPA] / loncom / interface / lonchatfetch.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue May 9 14:38:09 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changing loncommon::coursegroups to be easier to use

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

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