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