Annotation of loncom/interface/lonannounce.pm, revision 1.15
1.1 www 1: # The LearningOnline Network
1.2 www 2: # Announce
1.1 www 3: #
1.15 ! www 4: # $Id: lonannounce.pm,v 1.14 2003/06/18 15:11:20 www Exp $
1.1 www 5: #
1.3 www 6: # Copyright Michigan State University Board of Trustees
1.1 www 7: #
1.3 www 8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
1.1 www 9: #
1.3 www 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.
1.1 www 14: #
1.3 www 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:
1.2 www 29: package Apache::lonannounce;
1.1 www 30:
31: use strict;
32: use Apache::Constants qw(:common);
1.3 www 33: use Apache::loncommon;
1.8 matthew 34: use Apache::lonhtmlcommon();
1.15 ! www 35: use HTML::Entities();
1.3 www 36:
1.12 www 37: my %todayhash;
38:
1.10 www 39: sub editfield {
40: my ($r,$start,$end,$text)=@_;
41: # Deal with date forms
42: my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
43: 'startdate',
44: $start);
45: my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
46: 'enddate',
47: $end);
48:
49: $r->print(<<ENDFORM);
50: <form name="anno" method="post">
51: <input type="hidden" value='' name="action" >
52: <table><tr><td>Starting date:</td><td>$startdateform</td></tr>
53: <tr><td>Ending date:</td><td>$enddateform</td></tr></table>
54: <textarea name="msg" rows="4" cols="60">$text</textarea>
55: <input type="button" onClick="trysubmit()" value="Post Announcement"><hr>
56: ENDFORM
57: }
58:
1.3 www 59: sub readcalendar {
60: my $courseid=shift;
61: my $coursenum=$ENV{'course.'.$courseid.'.num'};
62: my $coursedom=$ENV{'course.'.$courseid.'.domain'};
63: my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
64: my %returnhash=();
65: foreach (keys %thiscal) {
66: unless (($_=~/^error\:/) || ($thiscal{$_}=~/^error\:/)) {
67: $returnhash{$courseid.'@'.$_}=$thiscal{$_};
68: }
69: }
70: return %returnhash;
71: }
72:
73: sub emptycell {
74: return '<td bgcolor="#AAAAAA"> </td>';
75: }
76:
77: sub normalcell {
1.12 www 78: my ($day,$month,$year,$text)=@_;
1.4 www 79: my $output='';
80: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
81: if ($_) {
1.5 www 82: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
83: my $msg=join('@',@msg);
1.11 www 84: my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
85: ', '.localtime($start).' - '.localtime($end).': '.$msg;
1.5 www 86: if ($courseid eq $ENV{'request.course.id'}) {
87: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
88: $output.='<input type="checkbox" name="remove_'.$start.'_'.
89: $end.'">';
90: }
91: }
1.15 ! www 92: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
! 93: $fullmsg=~s/&/\\&/g;
1.5 www 94: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.11 www 95: substr($msg,0,20).'...</a><br />';
1.4 www 96: }
97: }
1.12 www 98: return '<td'.
99: ((($day eq $todayhash{'day'}) &&
100: ($month eq $todayhash{'month'}) &&
101: ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
102: '><b>'.$day.'</b><br>'.$output.'</td>';
1.3 www 103: }
104:
1.11 www 105: sub plaincell {
106: my ($text)=@_;
107: my $output='';
108: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
109: if ($_) {
110: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
111: my $msg=join('@',@msg);
112: my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
113: ', '.localtime($start).' - '.localtime($end).': '.$msg;
1.15 ! www 114: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
! 115: $fullmsg=~s/&/\\&/g;
1.11 www 116: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
117: substr($msg,0,40).'...</a><br />';
118: }
119: }
120: return $output;
121: }
122:
123: sub listcell {
124: my ($text)=@_;
125: my $output='';
126: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
127: if ($_) {
128: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
129: my $msg=join('@',@msg);
130: my $fullmsg=localtime($start).' to '.localtime($end).':<br /><b>'.
131: $msg.'</b>';
132: $output.='<li>'.$fullmsg.'</li>';
133: }
134: }
135: return $output;
136: }
137:
1.3 www 138: sub nextday {
139: my %th=@_;
140: $th{'day'}++;
141: return (&Apache::loncommon::maketime(%th),$th{'month'});
142: }
143:
144: sub showday {
1.11 www 145: my ($tk,$mode,%allcal)=@_;
1.3 www 146: my %th=&Apache::loncommon::timehash($tk);
147: my ($nextday,$nextmonth)=&nextday(%th);
148: my $outp='';
1.4 www 149: my $oneday=24*3600;
1.3 www 150: foreach (keys %allcal) {
151: my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
1.4 www 152: if (($startdate<$nextday) && ($enddate>$tk)) {
1.5 www 153: $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
154: $allcal{$_};
1.3 www 155: }
156: }
1.11 www 157: unless ($mode) {
1.12 www 158: return ($nextday,$nextmonth,&normalcell(
159: $th{'day'},$th{'month'},$th{'year'},$outp));
1.11 www 160: } elsif ($outp) {
161: if ($mode==1) {
162: return '<br />'.&plaincell($outp);
163: } else {
164: return '<ul>'.&listcell($outp).'</ul>';
165: }
166: } else {
167: return '';
168: }
1.3 www 169: }
1.1 www 170:
171: sub handler {
172: my $r = shift;
173: $r->content_type('text/html');
174: $r->send_http_header;
175: return OK if $r->header_only;
176:
1.3 www 177: # ---------------------------------------------------------- Get time right now
178: my $today=time;
1.12 www 179: %todayhash=&Apache::loncommon::timehash($today);
1.3 www 180:
181: # ---------------------------------------------------------- Get month and year
182: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
183: ['month','year']);
184: # --------------------------------------------------- Decide what month to show
185: my $year=$todayhash{'year'};
186: if ($ENV{'form.year'}) { $year=$ENV{'form.year'}; }
187: my $month=$todayhash{'month'};
188: if ($ENV{'form.month'}) { $month=$ENV{'form.month'}; }
189: # --------------------------------------------- Find out first day of the month
190:
191: my %firstday=&Apache::loncommon::timehash(
192: &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
193: 'year' => $year, 'hours' => 0,
194: 'minutes' => 0, 'seconds' => 0,
195: 'dlsav' => $todayhash{'dlsav'} ));
196: my $weekday=$firstday{'weekday'};
197: # ------------------------------------------------------------ Print the screen
1.9 www 198:
1.1 www 199: $r->print(<<ENDDOCUMENT);
200: <html>
201: <head>
202: <title>The LearningOnline Network with CAPA</title>
1.3 www 203: <script>
204:
205: function trysubmit() {
206: document.anno.action.value="new";
207: document.anno.submit();
208: }
209:
210: function removesub() {
211: document.anno.action.value="del";
212: document.anno.submit();
213: }
214: </script>
1.1 www 215: </head>
216: ENDDOCUMENT
1.6 www 217: $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
1.3 www 218: # does this user have privileges to post, etc?
219: my $allowed=0;
220: if ($ENV{'request.course.id'}) {
221: $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
222: }
223:
224: if ($allowed) {
225: my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
226: my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
227: # ----------------------------------------------------- Store new submitted one
228: if ($ENV{'form.action'} eq 'new') {
1.9 www 229: my $startdate =
230: &Apache::lonhtmlcommon::get_date_from_form('startdate');
231: my $enddate =
232: &Apache::lonhtmlcommon::get_date_from_form('enddate');
233: unless ($startdate=~/^\d+$/) { $startdate=time; }
234: unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
235: if ($startdate>$enddate) {
236: my $buffer=$startdate;
237: $startdate=$enddate;
238: $enddate=$buffer;
239: }
1.3 www 240: &Apache::lonnet::put('calendar',{
1.9 www 241: $startdate.'_'.$enddate =>
1.3 www 242: $ENV{'form.msg'} },$coursedom,$coursenum);
243: }
244: # ---------------------------------------------------------------- Remove items
245: if ($ENV{'form.action'} eq 'del') {
246: my @delwhich=();
247: foreach (keys %ENV) {
248: if ($_=~/^form\.remove\_(.+)$/) {
249: push(@delwhich,$1);
250: }
251: }
252: &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
253: }
254: # -------------------------------------------------------- Form to post new one
255: my %tomorrowhash=%todayhash;
256: $tomorrowhash{'day'}++;
257: my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
258:
1.10 www 259: &editfield($r,$today,$tomorrow,'');
1.3 www 260: }
1.5 www 261: # ----------------------------------------------------- Summarize all calendars
262: my %allcal=();
263: foreach (&Apache::loncommon::findallcourses()) {
264: %allcal=(%allcal,&readcalendar($_));
265: }
266:
267: # ------------------------------- Initialize table and forward backward buttons
1.3 www 268: my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
269: if ($pm<1) { ($pm,$py)=(12,$year-1); }
270: if ($fm>12){ ($fm,$fy)=(1,$year+1); }
1.14 www 271:
1.3 www 272: $r->print('<h1>'.('','January','February','March','April','May',
273: 'June','July','August','September','October',
1.13 www 274: 'November','December')[$month].' '.$year.'</h1>');
275: # Reached the end of times, give up
276: if (($year<1970) || ($year>2037)) {
277: $r->print('<h3>No calendar available for this date.</h3>'.
278: '<a href="/adm/announcements?month='.$todayhash{'month'}.
279: '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
280: return OK;
281: }
282: $r->print(
1.3 www 283: '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
1.12 www 284: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
285: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
286: '&year='.$todayhash{'year'}.'">Current Month</a><p>'.
1.3 www 287: '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
1.7 matthew 288: '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
1.3 www 289:
290: my $tk=&Apache::loncommon::maketime(%firstday);
291: my $outp;
292: my $nm;
293:
294: # ---------------------------------------------------------------- Actual table
295: $r->print('<tr>');
296: for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
297: for (my $i=$weekday;$i<=6;$i++) {
1.11 www 298: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3 www 299: $r->print($outp);
300: }
301: $r->print('</tr>');
302:
303: for (my $k=0;$k<=3;$k++) {
304: $r->print('<tr>');
305: for (my $i=0;$i<=6;$i++) {
1.11 www 306: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3 www 307: if ($month!=$nm) { $outp=&emptycell; }
308: $r->print($outp);
309: }
310: $r->print('</tr>');
311: }
312: # ------------------------------------------------------------------- End table
1.7 matthew 313: $r->print('</table>');
1.9 www 314: if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
1.7 matthew 315: $r->print('<p>'.
1.3 www 316: '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
1.12 www 317: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
318: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
319: '&year='.$todayhash{'year'}.'">Current Month</a></p>'.
1.3 www 320: '</body></html>');
1.1 www 321: return OK;
322: }
323:
324: 1;
325: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>