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