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