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