File:  [LON-CAPA] / loncom / interface / lonchatfetch.pm
Revision 1.24: download - view: text, annotated - select for diffs
Thu May 18 01:08:50 2006 UTC (18 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
&coursegroups() and &get_group_settings() moved to longroup.pm, which contains general utility functions for asking about groups.  Also contains &group_changes() which is used to add/drop group memberships as a result of role changes, as determined by group settings for auto-add and auto-drop. 'none' and 'all' are now reserved words which may not be used as section or group names, so they can be used instead of _all and _none  when specifying auto-add and auto-drop settings for all sections or no section roles.

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

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