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