1: # The LearningOnline Network
2: # Chat Fetching
3: #
4: # $Id: lonchatfetch.pm,v 1.36 2009/10/23 10:21:00 bisitz 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 Apache::lonlocal;
38: use lib '/home/httpd/lib/perl/';
39: use LONCAPA;
40:
41:
42: sub handler {
43: my $r = shift;
44:
45: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
46: ['lastid','group']);
47: my ($group,$grouptitle);
48: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
49: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
50: if (defined($env{'form.group'})) {
51: $group = $env{'form.group'};
52: if ((! &Apache::lonnet::allowed('pgc',$env{'request.course.id'}.'/'.
53: $group)) &&
54: (! &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
55: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
56: return HTTP_NOT_ACCEPTABLE;
57: }
58: my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
59: if (%curr_groups) {
60: my %group_info =
61: &Apache::longroup::get_group_settings($curr_groups{$group});
62: $grouptitle =
63: '<b>'.&unescape($group_info{description}).
64: '</b><br />';
65: }
66: } elsif (! &Apache::lonnet::allowed('pch',$env{'request.course.id'}.
67: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
68: ) {
69: return HTTP_NOT_ACCEPTABLE;
70: }
71:
72: my $loaderror=&Apache::lonnet::overloaderror($r);
73: if ($loaderror) { return $loaderror; }
74: $loaderror=
75: &Apache::lonnet::overloaderror($r,
76: $env{'course.'.$env{'request.course.id'}.'.home'});
77: if ($loaderror) { return $loaderror; }
78:
79: &Apache::loncommon::content_type($r,'text/html');
80: $r->send_http_header;
81: return OK if $r->header_only;
82:
83: # ------------------------------------------------------------ retrieve entries
84:
85: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
86:
87: my @entries=split(/\:/,
88: &Apache::lonnet::reply(
89: "chatretr:$cdom:$cnum:$env{'user.domain'}:$env{'user.name'}:$group",
90: $chome));
91: # Figure out what the last valid entry-id is
92: my ($lastid,$thentime,$idnum);
93: foreach my $entry (@entries) {
94: $entry =~/^(\w+)/;
95: if ($1 ne 'active_participant') {
96: $lastid=$1;
97: ($thentime,$idnum)=split(/\_/,$lastid);
98: }
99: }
100: # ----------------------------------------------------------- Can see identity?
101: my $seeid = &get_seeid_status();
102: # -------------------------------------------------------- see which ones apply
103: my $include=0;
104: my $header;
105: my $newstuff='';
106: my $bottomid='';
107: unless ($env{'form.lastid'}) {
108: $include=1;
109: $header =
110: &Apache::loncommon::start_page(undef,undef,
111: {'only_body' => 1,
112: 'bgcolor' => '#FFFFFF',
113: 'js_ready' => 1,});
114: }
115: my @participants=();
116: foreach my $entry (@entries) {
117: my ($id,$msg,$udom)=split(/\:/,&unescape($entry));
118: if ($id eq 'active_participant') {
119: chomp($udom);
120: my $participant= &Apache::loncommon::nickname($msg,$udom);
121: unless ($participant=~/\w/) { $participant=$msg.':'.$udom; }
122: $participants[$#participants+1]=$participant;
123: } elsif ($include) {
124: chomp($msg);
125: my ($msgtime,$msgnum)=split(/\_/,$id);
126: my ($sdom,$snum,$anon,$contrib)=split(/\:/,
127: &unescape($msg));
128: $contrib=&unescape($contrib);
129: &Apache::lonfeedback::newline_to_br(\$contrib);
130: ($contrib,my $errors)=&Apache::lontexconvert::msgtexconverted($contrib);
131: if ($errors) {
132: $contrib.=' <span class="LC_error">'
133: .&mt('(Message not fully displayed due to incorrect embedded TeX.)')
134: .'</span>';
135: }
136: if ($errors && $snum eq $env{'user.name'} &&
137: $sdom eq $env{'user.domain'} ) {
138: $contrib.='<br /><span class="LC_error">'
139: .&mt('TeX error message: [_1]',$errors)
140: .'</span>';
141: }
142: $contrib=~s/\n/ /g;
143: $contrib=~s/\'/\&\#39\;/g;
144: my $sender='';
145: if ($seeid) {
146: $sender=&Apache::loncommon::plainname($snum,$sdom);
147: my $nick=&Apache::loncommon::nickname($snum,$sdom);
148: if (($nick) && ($nick ne $sender)) {
149: $sender.=' '.$nick;
150: }
151: unless ($sender) { $sender=$snum.':'.$sdom; }
152: if ($anon) { $sender.=' [Anon]' };
153: } elsif (!$anon) {
154: $sender=&Apache::loncommon::nickname($snum,$sdom);
155: unless ($sender) { $sender=$snum.':'.$sdom; }
156: } else {
157: $sender=&Apache::loncommon::screenname($snum,$sdom);
158: unless ($sender) { $sender=&mt("Anonymous"); }
159: }
160: $sender=~s/\'/\&\#39\;/g;
161: my $color=$sender;
162: $color=~tr/a-j/0-9/;
163: $color=~tr/A-J/0-9/;
164: $color=~tr/k-t/0-9/;
165: $color=~tr/K-T/0-9/;
166: $color=~tr/u-z/0-5/;
167: $color=~tr/U-Z/0-5/;
168: $color=~s/\D//g;
169: $color=substr($color,0,6);
170: my $timestamp=localtime($msgtime);
171: my ($mhour,$mmin,$msec)=($timestamp=~/(\d\d)\:(\d\d)\:(\d\d)/);
172: $newstuff.='<span style="color:#'.$color.'"><a name="LC_'.$id.'"></a><b>'.
173: $sender.'</b> ('.$mhour.':'.$mmin.':'.$msec.'): '.
174: $contrib."</span><br />";
175: $bottomid='LC_'.$id;
176: } else {
177: $entry=~/^(\w+)/;
178: if ($1 eq $env{'form.lastid'}) { $include=1; }
179: }
180: }
181: my $participant_output=join('<br />',sort @participants);
182: my $refresh_cmd = "/adm/chatfetch?lastid=$lastid";
183: if (defined($group)) {
184: $refresh_cmd .= "&group=$group";
185: }
186: my $headarg;
187: my ($blocked,$blocktext) = blockstatus();
188: if ($blocked) {
189: $newstuff = $blocktext;
190: $headarg = {'only_body' => 1,};
191:
192: $r->print(<<ENDSCRIPT);
193: <script type="text/javascript">
194: parent.location.href="/adm/blockingstatus/?activity=chat"
195: </script>
196: ENDSCRIPT
197: } else {
198: $headarg = {'redirect' => [5,$refresh_cmd,1],
199: 'only_body' => 1,};
200: }
201: my $start_page = &Apache::loncommon::start_page('Chat Room',undef,$headarg);
202: my $end_page = &Apache::loncommon::end_page();
203: $r->print(<<ENDDOCUMENT);
204: $start_page
205: <script type="text/javascript">
206: parent.chatout.document.writeln('$header$newstuff');
207: parent.chatout.scroll(0,10000000);
208: </script>
209: $grouptitle
210: $participant_output
211: $end_page
212: ENDDOCUMENT
213: return OK;
214: }
215:
216: sub get_seeid_status {
217: my $crs='/'.$env{'request.course.id'};
218: my $seeid;
219: if (exists($env{'form.group'})) {
220: $seeid = &Apache::lonnet::allowed('rci',$crs.'/'.$env{'form.group'});
221: } else {
222: if ($env{'request.course.sec'}) {
223: $crs.='_'.$env{'request.course.sec'};
224: }
225: $crs=~s/\_/\//g;
226: $seeid=&Apache::lonnet::allowed('rin',$crs);
227: }
228: return $seeid;
229: }
230:
231: sub blockstatus {
232: my ($blocked,$output);
233: my %setters;
234: my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'chat');
235: if ($startblock && $endblock) {
236: $blocked = 1;
237: my $endblocktime = &Apache::lonlocal::locallocaltime($endblock);
238: $output .= &mt('Chat Room will be unavailable to you until [_1] because communication is blocked in one or more of your courses:',$endblocktime).'<br /><br />';
239: foreach my $course (keys(%setters)) {
240: my %courseinfo=&Apache::lonnet::coursedescription($course);
241: for (my $i=0; $i<@{$setters{$course}{staff}}; $i++) {
242: my ($uname,$udom) = @{$setters{$course}{staff}[$i]};
243: my $fullname = &Apache::loncommon::plainname($uname,$udom);
244: my ($openblock,$closeblock) = @{$setters{$course}{times}[$i]};
245: $openblock = &Apache::lonlocal::locallocaltime($openblock);
246: $closeblock= &Apache::lonlocal::locallocaltime($closeblock);
247: $output .= &mt('Block for [_1] starts: [_2], ends [_3], set by: [_4]<br />',$courseinfo{'description'},$openblock,$closeblock,$fullname);
248: }
249: }
250: }
251: return ($blocked,$output);
252: }
253:
254: 1;
255: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>