1: # The LearningOnline Network
2: # Announce
3: #
4: # $Id: lonannounce.pm,v 1.18 2003/09/02 21:15:59 www 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::lonannounce;
30:
31: use strict;
32: use Apache::Constants qw(:common);
33: use Apache::loncommon;
34: use Apache::lonhtmlcommon();
35: use HTML::Entities();
36:
37: my %todayhash;
38: my %showedcheck;
39:
40: sub editfield {
41: my ($r,$start,$end,$text)=@_;
42: # Deal with date forms
43: my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
44: 'startdate',
45: $start);
46: my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
47: 'enddate',
48: $end);
49:
50: $r->print(<<ENDFORM);
51: <form name="anno" method="post">
52: <input type="hidden" value='' name="action" >
53: <table><tr><td>Starting date:</td><td>$startdateform</td></tr>
54: <tr><td>Ending date:</td><td>$enddateform</td></tr></table>
55: <textarea name="msg" rows="4" cols="60">$text</textarea>
56: <input type="button" onClick="trysubmit()" value="Post Announcement"><hr>
57: ENDFORM
58: }
59:
60: sub readcalendar {
61: my $courseid=shift;
62: my $coursenum=$ENV{'course.'.$courseid.'.num'};
63: my $coursedom=$ENV{'course.'.$courseid.'.domain'};
64: my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
65: my %returnhash=();
66: foreach (keys %thiscal) {
67: unless (($_=~/^error\:/) || ($thiscal{$_}=~/^error\:/)) {
68: $returnhash{$courseid.'@'.$_}=$thiscal{$_};
69: }
70: }
71: return %returnhash;
72: }
73:
74: sub emptycell {
75: return '<td bgcolor="#AAAAAA"> </td>';
76: }
77:
78: sub normalcell {
79: my ($day,$month,$year,$text)=@_;
80: my $output='';
81: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
82: if ($_) {
83: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
84: my $msg=join('@',@msg);
85: my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
86: ', '.localtime($start).' - '.localtime($end).': '.$msg;
87: if ($courseid eq $ENV{'request.course.id'}) {
88: if ((&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))
89: && (!$showedcheck{$start.'_'.$end})) {
90: $output.='<input type="checkbox" name="remove_'.$start.'_'.
91: $end.'">';
92: $showedcheck{$start.'_'.$end}=1;
93: }
94: }
95: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
96: $fullmsg=~s/&/\\&/g;
97: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
98: substr($msg,0,20).'...</a><br />';
99: }
100: }
101: return '<td'.
102: ((($day eq $todayhash{'day'}) &&
103: ($month eq $todayhash{'month'}) &&
104: ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
105: '><b>'.$day.'</b><br>'.$output.'</td>';
106: }
107:
108: sub plaincell {
109: my ($text)=@_;
110: my $output='';
111: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
112: if ($_) {
113: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
114: my $msg=join('@',@msg);
115: my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
116: ', '.localtime($start).' - '.localtime($end).': '.$msg;
117: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
118: $fullmsg=~s/&/\\&/g;
119: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
120: substr($msg,0,40).'...</a><br />';
121: }
122: }
123: return $output;
124: }
125:
126: sub listcell {
127: my ($text)=@_;
128: my $output='';
129: foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
130: if ($_) {
131: my ($courseid,$start,$end,@msg)=split(/\@/,$_);
132: my $msg=join('@',@msg);
133: my $fullmsg=localtime($start).' to '.localtime($end).':<br /><b>'.
134: $msg.'</b>';
135: $output.='<li>'.$fullmsg.'</li>';
136: }
137: }
138: return $output;
139: }
140:
141: sub nextday {
142: my %th=@_;
143: $th{'day'}++;
144: return (&Apache::loncommon::maketime(%th),$th{'month'});
145: }
146:
147: sub showday {
148: my ($tk,$mode,%allcal)=@_;
149: my %th=&Apache::loncommon::timehash($tk);
150: my ($nextday,$nextmonth)=&nextday(%th);
151: my $outp='';
152: my $oneday=24*3600;
153: foreach (keys %allcal) {
154: my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
155: if (($startdate<$nextday) && ($enddate>$tk)) {
156: $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
157: $allcal{$_};
158: }
159: }
160: unless ($mode) {
161: return ($nextday,$nextmonth,&normalcell(
162: $th{'day'},$th{'month'},$th{'year'},$outp));
163: } elsif ($outp) {
164: if ($mode==1) {
165: return '<br />'.&plaincell($outp);
166: } else {
167: return '<ul>'.&listcell($outp).'</ul>';
168: }
169: } else {
170: return '';
171: }
172: }
173:
174: sub handler {
175: my $r = shift;
176: $r->content_type('text/html');
177: $r->send_http_header;
178: return OK if $r->header_only;
179:
180: # ---------------------------------------------------------- Get time right now
181: my $today=time;
182: %todayhash=&Apache::loncommon::timehash($today);
183: # ----------------------------------------------------------------- Check marks
184: %showedcheck=();
185: undef %showedcheck;
186: # ---------------------------------------------------------- Get month and year
187: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
188: ['month','year','pickdate']);
189: # --------------------------------------------------- Decide what month to show
190: my $year=$todayhash{'year'};
191: if ($ENV{'form.year'}) { $year=$ENV{'form.year'}; }
192: my $month=$todayhash{'month'};
193: if ($ENV{'form.month'}) { $month=$ENV{'form.month'}; }
194:
195: # ---------------------------------------------- See if we are in pickdate mode
196: my $pickdatemode=($ENV{'form.pickdate'} eq 'yes');
197:
198: # --------------------------------------------- Find out first day of the month
199:
200: my %firstday=&Apache::loncommon::timehash(
201: &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
202: 'year' => $year, 'hours' => 0,
203: 'minutes' => 0, 'seconds' => 0,
204: 'dlsav' => $todayhash{'dlsav'} ));
205: my $weekday=$firstday{'weekday'};
206: # ------------------------------------------------------------ Print the screen
207:
208: $r->print(<<ENDDOCUMENT);
209: <html>
210: <head>
211: <title>The LearningOnline Network with CAPA</title>
212: <script>
213:
214: function trysubmit() {
215: document.anno.action.value="new";
216: document.anno.submit();
217: }
218:
219: function removesub() {
220: document.anno.action.value="del";
221: document.anno.submit();
222: }
223: </script>
224: </head>
225: ENDDOCUMENT
226: if ($pickdatemode) {
227: # no big header in pickdate mode
228: $r->print(&Apache::loncommon::bodytag("Pick a Date",'','',1).
229: '<font size="1">');
230: } else {
231: $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
232: }
233: # does this user have privileges to post, etc?
234: my $allowed=0;
235: if ($ENV{'request.course.id'}) {
236: $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
237: }
238: # does this user have privileges to post to servers?
239: my $serverpost=0;
240: if ($ENV{'request.role.domain'}) {
241: $serverpost=&Apache::lonnet::allowed('psa',
242: $ENV{'request.role.domain'});
243: } else {
244: $serverpost=&Apache::lonnet::allowed('psa','/');
245: }
246: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
247: if ($pickdatemode) {
248: $serverpost=0;
249: $allowed=0;
250: }
251: # ------------------------------------------------------------ Process commands
252: if ($serverpost) {
253: if ($ENV{'form.serveraction'}) {
254: foreach (keys %ENV) {
255: if ($_=~/^form\.postto\_(\w+)/) {
256: $r->print(
257: '<br />Posting '.$1.': '.&Apache::lonnet::postannounce
258: ($1,$ENV{'form.serverannnounce'}));
259: }
260: }
261: }
262: $r->print(<<SERVERANNOUNCE);
263: <form name="serveranno" method="post">
264: <h3>Post Server Announcements</h3>
265: Post announcements to the system login and roles screen<br />
266: <i>(leave blank to delete announcement)</i><br />
267: <textarea name="serverannnounce" cols="60" rows="5"></textarea><br />
268: Check machines:<br />
269: SERVERANNOUNCE
270: # list servers
271: foreach (sort keys %Apache::lonnet::hostname) {
272: if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$_})) {
273: $r->print ('<br /><input type="checkbox" name="postto_'.$_.'" /> '.
274: $_.' <tt>'.$Apache::lonnet::hostname{$_}.'</tt> '.
275: '<a href="http://'.$Apache::lonnet::hostname{$_}.
276: '/announcement.txt" target="annowin">current</a>');
277: }
278: }
279: $r->print(
280: '<br /><input type="submit" name="serveraction" value="Post"></form><hr />');
281: }
282: if ($allowed) {
283: my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
284: my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
285: # ----------------------------------------------------- Store new submitted one
286: if ($ENV{'form.action'} eq 'new') {
287: my $startdate =
288: &Apache::lonhtmlcommon::get_date_from_form('startdate');
289: my $enddate =
290: &Apache::lonhtmlcommon::get_date_from_form('enddate');
291: unless ($startdate=~/^\d+$/) { $startdate=time; }
292: unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
293: if ($startdate>$enddate) {
294: my $buffer=$startdate;
295: $startdate=$enddate;
296: $enddate=$buffer;
297: }
298: &Apache::lonnet::put('calendar',{
299: $startdate.'_'.$enddate =>
300: $ENV{'form.msg'} },$coursedom,$coursenum);
301: }
302: # ---------------------------------------------------------------- Remove items
303: if ($ENV{'form.action'} eq 'del') {
304: my @delwhich=();
305: foreach (keys %ENV) {
306: if ($_=~/^form\.remove\_(.+)$/) {
307: push(@delwhich,$1);
308: }
309: }
310: &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
311: }
312: # -------------------------------------------------------- Form to post new one
313: my %tomorrowhash=%todayhash;
314: $tomorrowhash{'day'}++;
315: my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
316:
317: &editfield($r,$today,$tomorrow,'');
318: }
319: # ----------------------------------------------------- Summarize all calendars
320: my %allcal=();
321: foreach (&Apache::loncommon::findallcourses()) {
322: %allcal=(%allcal,&readcalendar($_));
323: }
324:
325: # ------------------------------- Initialize table and forward backward buttons
326: my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
327: if ($pm<1) { ($pm,$py)=(12,$year-1); }
328: if ($fm>12){ ($fm,$fy)=(1,$year+1); }
329:
330: $r->print('<h1>'.('','January','February','March','April','May',
331: 'June','July','August','September','October',
332: 'November','December')[$month].' '.$year.'</h1>');
333: # Reached the end of times, give up
334: if (($year<1970) || ($year>2037)) {
335: $r->print('<h3>No calendar available for this date.</h3>'.
336: '<a href="/adm/announcements?month='.$todayhash{'month'}.
337: '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
338: return OK;
339: }
340: $r->print(
341: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
342: ($pickdatemode?'&pickdate=yes':'').'">Previous Month</a> '.
343: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
344: ($pickdatemode?'&pickdate=yes':'').'">Next Month</a>'.
345: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
346: '&year='.$todayhash{'year'}.
347: ($pickdatemode?'&pickdate=yes':'').'">Current Month</a><p>'.
348: '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
349: '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
350:
351: my $tk=&Apache::loncommon::maketime(%firstday);
352: my $outp;
353: my $nm;
354:
355: # ---------------------------------------------------------------- Actual table
356: $r->print('<tr>');
357: for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
358: for (my $i=$weekday;$i<=6;$i++) {
359: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
360: $r->print($outp);
361: }
362: $r->print('</tr>');
363:
364: for (my $k=0;$k<=3;$k++) {
365: $r->print('<tr>');
366: for (my $i=0;$i<=6;$i++) {
367: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
368: if ($month!=$nm) { $outp=&emptycell; }
369: $r->print($outp);
370: }
371: $r->print('</tr>');
372: }
373: # ------------------------------------------------------------------- End table
374: $r->print('</table>');
375: # ----------------------------------------------------------------- Check marks
376: %showedcheck=();
377: undef %showedcheck;
378: # --------------------------------------------------------------- Remove button
379: if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
380: $r->print('<p>'.
381: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
382: ($pickdatemode?'&pickdate=yes':'').'">Previous Month</a> '.
383: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
384: ($pickdatemode?'&pickdate=yes':'').'">Next Month</a>'.
385: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
386: '&year='.$todayhash{'year'}.
387: ($pickdatemode?'&pickdate=yes':'').'">Current Month</a></p>'.
388: ($pickdatemode?'</font>':'').
389: '</body></html>');
390: return OK;
391: }
392:
393: 1;
394: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>